Skip to content

Commit

Permalink
scripts/checkpatch: Check for patches adding #defines for libc APIs
Browse files Browse the repository at this point in the history
All code in the Zephyr core must use only the Zephyr C library API
according to rules A.4 and A.5. Such code is not permitted to request API
extensions from the C library via any of the API request mechanisms.

This addition to checkpatch.pl verifies that patches don't #define
any of these:

	__STRICT_ANSI__
	_POSIX_SOURCE
	_POSIX_C_SOURCE
	_XOPEN_SOURCE
	_ISOC99_SOURCE
	_ISOC11_SOURCE
	_ATFILE_SOURCE
	_GNU_SOURCE
	_BSD_SOURCE
	_SVID_SOURCE
	_DEFAULT_SOURCE

Reference: #49922

Signed-off-by: Keith Packard <keithp@keithp.com>
  • Loading branch information
keith-packard committed Jul 17, 2023
1 parent 2b1106a commit e596e94
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions scripts/checkpatch.pl
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,20 @@ sub hash_show_words {
["__ATTR", 2],
);

our $api_defines = qr{(?x:
_POSIX_SOURCE|
_POSIX_C_SOURCE|
_XOPEN_SOURCE|
_XOPEN_SOURCE_EXTENDED|
_ISOC99_SOURCE|
_ISOC11_SOURCE|
_ATFILE_SOURCE|
_GNU_SOURCE|
_BSD_SOURCE|
_SVID_SOURCE|
_DEFAULT_SOURCE
)};

my $word_pattern = '\b[A-Z]?[a-z]{2,}\b';

#Create a search pattern for all these functions to speed up a loop below
Expand Down Expand Up @@ -6527,6 +6541,13 @@ sub process {
}
}

# check for #defines of various API specifying things like _POSIX_C_SOURCE

if ($line =~ /#\s*define\s+$api_defines/) {
ERROR("API_DEFINE",
"do not specify a non-Zephyr API for libc\n" . "$here$rawline\n");
}

# check for IS_ENABLED() without CONFIG_<FOO> ($rawline for comments too)
if ($rawline =~ /\bIS_ENABLED\s*\(\s*(\w+)\s*\)/ && $1 !~ /^CONFIG_/) {
WARN("IS_ENABLED_CONFIG",
Expand Down

0 comments on commit e596e94

Please sign in to comment.