Skip to content

Commit 8575e92

Browse files
committed
misra.py: Add standard library functions for C90 and C99
This is required to add additional analysis for some MISRA rules. This commit improve R21.1 check and close trac ticket 9603.
1 parent ca30957 commit 8575e92

2 files changed

Lines changed: 281 additions & 7 deletions

File tree

‎addons/misra.py‎

Lines changed: 277 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,267 @@ def rawlink(rawtoken):
106106
}
107107

108108

109+
# Identifiers described in Section 7 "Library" of C90 Standard
110+
# Based on ISO/IEC9899:1990 Annex D -- Library summary and
111+
# Annex E -- Implementation limits.
112+
C90_STDLIB_IDENTIFIERS = {
113+
# D.1 Errors
114+
'errno.h': ['EDOM', 'ERANGE', 'errno'],
115+
# D.2 Common definitions
116+
'stddef.h': ['NULL', 'offsetof', 'ptrdiff_t', 'size_t', 'wchar_t'],
117+
# D.3 Diagnostics
118+
'assert.h': ['NDEBUG', 'assert'],
119+
# D.4 Character handling
120+
'ctype.h': [
121+
'isalnum', 'isalpha', 'isblank', 'iscntrl', 'isdigit',
122+
'isgraph', 'islower', 'isprint', 'ispunct', 'isspace',
123+
'isupper', 'isxdigit', 'tolower', 'toupper',
124+
],
125+
# D.5 Localization
126+
'locale.h': [
127+
'LC_ALL', 'LC_COLLATE', 'LC_CTYPE', 'LC_MONETARY',
128+
'LC_NUMERIC', 'LC_TIME', 'NULL', 'lconv',
129+
'setlocale', 'localeconv',
130+
],
131+
# D.6 Mathematics
132+
'math.h': [
133+
'HUGE_VAL' 'acos', 'asin' , 'atan2', 'cos', 'sin', 'tan', 'cosh',
134+
'sinh', 'tanh', 'exp', 'frexp', 'ldexp', 'log', 'loglO', 'modf',
135+
'pow', 'sqrt', 'ceil', 'fabs', 'floor', 'fmod',
136+
],
137+
# D.7 Nonlocal jumps
138+
'setjmp.h': ['jmp_buf', 'setjmp', 'longjmp'],
139+
# D.8 Signal handling
140+
'signal.h': [
141+
'sig_atomic_t', 'SIG_DFL', 'SIG_ERR', 'SIG_IGN', 'SIGABRT', 'SIGFPE',
142+
'SIGILL', 'SIGINT', 'SIGSEGV', 'SIGTERM', 'signal', 'raise',
143+
],
144+
# D.9 Variable arguments
145+
'stdarg.h': ['va_list', 'va_start', 'va_arg', 'va_end'],
146+
# D.10 Input/output
147+
'stdio.h': [
148+
'_IOFBF', '_IOLBF', '_IONBF', 'BUFSIZ', 'EOF', 'FILE', 'FILENAME_MAX',
149+
'FOPEN_MAX', 'fpos_t', 'L_tmpnam', 'NULL', 'SEEK_CUR', 'SEEK_END',
150+
'SEEK_SET', 'size_t', 'stderr', 'stdin', 'stdout', 'TMP_MAX',
151+
'remove', 'rename', 'tmpfile', 'tmpnam', 'fclose', 'fflush', 'fopen',
152+
'freopen', 'setbuf', 'setvbuf', 'fprintf', 'fscanf', 'printf',
153+
'scanf', 'sprintf', 'sscanf', 'vfprintf', 'vprintf', 'vsprintf',
154+
'fgetc', 'fgets', 'fputc', 'fputs', 'getc', 'getchar', 'gets', 'putc',
155+
'putchar', 'puts', 'ungetc', 'fread', 'fwrite', 'fgetpos', 'fseek',
156+
'fsetpos', 'rewind', 'clearerr', 'feof', 'ferror', 'perror',
157+
],
158+
# D.11 General utilities
159+
'stdlib.h': [
160+
'EXIT_FAILURE', 'EXIT_SUCCESS', 'MB_CUR_MAX', 'NULL', 'RAND_MAX',
161+
'div_t', 'ldiv_t', 'wchar_t', 'atof', 'atoi', 'strtod', 'rand',
162+
'srand', 'calloc', 'free', 'malloc', 'realloc', 'abort', 'atexit',
163+
'exit', 'getenv', 'system', 'bsearch', 'qsort', 'abs', 'div', 'ldiv',
164+
'mblen', 'mbtowc', 'wctomb', 'mbstowcs', 'wcstombs',
165+
],
166+
# D.12 String handling
167+
'string.h': [
168+
'NULL', 'size_t', 'memcpy', 'memmove', 'strcpy', 'strncpy', 'strcat',
169+
'strncat', 'memcmp', 'strcmp', 'strcoll', 'strncmp', 'strxfrm',
170+
'memchr', 'strchr', 'strcspn', 'strpbrk', 'strrchr', 'strspn',
171+
'strstr', 'strtok', 'memset', 'strerror', 'strlen',
172+
],
173+
# D.13 Date and time
174+
'time.h': [
175+
'CLK_TCK', 'NULL', 'clock_t', 'time_t', 'size_t', 'tm', 'clock',
176+
'difftime', 'mktime', 'time', 'asctime', 'ctime', 'gmtime',
177+
'localtime', 'strftime',
178+
],
179+
# Annex E: Implementation limits
180+
'limits.h': [
181+
'CHAR_BIT', 'SCHAR_MIN', 'SCHAR_MAX', 'UCHAR_MAX', 'CHAR_MIN',
182+
'CHAR_MAX', 'MB_LEN_MAX', 'SHRT_MIN', 'SHRT_MAX', 'USHRT_MAX',
183+
'INT_MIN', 'INT_MAX', 'UINT_MAX', 'LONG_MIN', 'LONG_MAX', 'ULONG_MAX',
184+
],
185+
'float.h': [
186+
'FLT_ROUNDS', 'FLT_RADIX', 'FLT_MANT_DIG', 'DBL_MANT_DIG',
187+
'LDBL_MANT_DIG', 'DECIMAL_DIG', 'FLT_DIG', 'DBL_DIG', 'LDBL_DIG',
188+
'DBL_MIN_EXP', 'LDBL_MIN_EXP', 'FLT_MIN_10_EXP', 'DBL_MIN_10_EXP',
189+
'LDBL_MIN_10_EXP', 'FLT_MAX_EXP', 'DBL_MAX_EXP', 'LDBL_MAX_EXP',
190+
'FLT_MAX_10_EXP', 'DBL_MAX_10_EXP', 'LDBL_MAX_10_EXP', 'FLT_MAX',
191+
'DBL_MAX', 'LDBL_MAX', 'FLT_MIN', 'DBL_MIN', 'LDBL_MIN',
192+
'FLT_EPSILON', 'DBL_EPSILON', 'LDBL_EPSILON'
193+
],
194+
}
195+
196+
197+
# Identifiers described in Section 7 "Library" of C99 Standard
198+
# Based on ISO/IEC 9899 WF14/N1256 Annex B -- Library summary
199+
C99_STDLIB_IDENTIFIERS = {
200+
# B.1 Diagnostics
201+
'assert.h': C90_STDLIB_IDENTIFIERS['assert.h'],
202+
# B.2 Complex
203+
'complex.h': [
204+
'complex', 'imaginary', 'I', '_Complex_I', '_Imaginary_I',
205+
'CX_LIMITED_RANGE',
206+
'cacos', 'cacosf', 'cacosl',
207+
'casin', 'casinf', 'casinl',
208+
'catan', 'catanf', 'catanl',
209+
'ccos', 'ccosf', 'ccosl',
210+
'csin', 'csinf', 'csinl',
211+
'ctan', 'ctanf', 'ctanl',
212+
'cacosh', 'cacoshf', 'cacoshl',
213+
'casinh', 'casinhf', 'casinhl',
214+
'catanh', 'catanhf', 'catanhl',
215+
'ccosh', 'ccoshf', 'ccoshl',
216+
'csinh', 'csinhf', 'csinhl',
217+
'ctanh', 'ctanhf', 'ctanhl',
218+
'cexp', 'cexpf', 'cexpl',
219+
'clog', 'clogf', 'clogl',
220+
'cabs', 'cabsf', 'cabsl',
221+
'cpow', 'cpowf', 'cpowl',
222+
'csqrt', 'csqrtf', 'csqrtl',
223+
'carg', 'cargf', 'cargl',
224+
'cimag', 'cimagf', 'cimagl',
225+
'conj', 'conjf', 'conjl',
226+
'cproj', 'cprojf', 'cprojl',
227+
'creal', 'crealf', 'creall',
228+
],
229+
# B.3 Character handling
230+
'ctype.h': C90_STDLIB_IDENTIFIERS['ctype.h'],
231+
# B.4 Errors
232+
'errno.h': C90_STDLIB_IDENTIFIERS['errno.h'] + ['EILSEQ'],
233+
# B.5 Floating-point environment
234+
'fenv.h': [
235+
'fenv_t', 'FE_OVERFLOW', 'FE_TOWARDZERO',
236+
'fexcept_t', 'FE_UNDERFLOW', 'FE_UPWARD',
237+
'FE_DIVBYZERO', 'FE_ALL_EXCEPT', 'FE_DFL_ENV',
238+
'FE_INEXACT', 'FE_DOWNWARD',
239+
'FE_INVALID', 'FE_TONEAREST',
240+
'FENV_ACCESS',
241+
'feclearexcept', 'fegetexceptflag', 'fegetround',
242+
'fesetround', 'fegetenv', 'feholdexcept',
243+
'fesetenv', 'feupdateenv',
244+
],
245+
# B.6 Characteristics of floating types
246+
'float.h': C90_STDLIB_IDENTIFIERS['float.h'] + ['FLT_EVAL_METHOD'],
247+
# B.7 Format conversion of integer types
248+
'inttypes.h': [
249+
'imaxdiv_t', 'imaxabs', 'imaxdiv', 'strtoimax',
250+
'strtoumax', 'wcstoimax', 'wcstoumax',
251+
],
252+
# B.8 Alternative spellings
253+
'iso646.h': [
254+
'and', 'and_eq', 'bitand', 'bitor', 'compl', 'not', 'not_eq',
255+
'or', 'or_eq', 'xor', 'xor_eq',
256+
],
257+
# B.9 Size of integer types
258+
'limits.h': C90_STDLIB_IDENTIFIERS['limits.h'] +
259+
['LLONG_MIN', 'LLONG_MAX', 'ULLONG_MAX'],
260+
# B.10 Localization
261+
'locale.h': C90_STDLIB_IDENTIFIERS['locale.h'],
262+
# B.11 Mathematics
263+
'math.h': C90_STDLIB_IDENTIFIERS['math.h'] + [
264+
'float_t', 'double_t', 'HUGE_VAL', 'HUGE_VALF', 'HUGE_VALL',
265+
'INFINITY', 'NAN', 'FP_INFINITE', 'FP_NAN', 'FP_NORMAL',
266+
'FP_SUBNORMAL', 'FP_ZERO', 'FP_FAST_FMA', 'FP_FAST_FMAF',
267+
'FP_FAST_FMAL', 'FP_ILOGB0', 'FP_ILOGBNAN', 'MATH_ERRNO',
268+
'MATH_ERREXCEPT', 'math_errhandling', 'FP_CONTRACT', 'fpclassify',
269+
'isfinite', 'isinf', 'isnan', 'isnormal', 'signbit', 'acosf', 'acosl',
270+
'asinf', 'asinl', 'atanf', 'atanl', 'atan2', 'atan2f', 'atan2l',
271+
'cosf', 'cosl', 'sinf', 'sinl', 'tanf', 'tanl', 'acosh', 'acoshf',
272+
'acoshl', 'asinh', 'asinhf', 'asinhl', 'atanh', 'atanhf', 'atanhl',
273+
'cosh', 'coshf', 'coshl', 'sinh', 'sinhf', 'sinhl', 'tanh', 'tanhf',
274+
'tanhl', 'expf', 'expl', 'exp2', 'exp2f', 'exp2l', 'expm1', 'expm1f',
275+
'expm1l', 'frexpf', 'frexpl', 'ilogb', 'ilogbf', 'ilogbl', 'float',
276+
'ldexpl', 'logf', 'logl', 'log10f', 'log10l', 'log1p', 'log1pf',
277+
'log1pl', 'log2', 'log2f', 'log2l', 'logb', 'logbf', 'logbl', 'modff',
278+
'modfl', 'scalbn', 'scalbnf', 'scalbnl', 'scalbln', 'scalblnf',
279+
'scalblnl', 'hypotl', 'powf', 'powl', 'sqrtf', 'sqrtl', 'erf', 'erff',
280+
'erfl', 'erfc', 'erfcf', 'erfcl', 'lgamma', 'lgammaf', 'lgammal',
281+
'tgamma', 'tgammaf', 'tgammal', 'ceilf', 'ceill', 'floorf', 'floorl',
282+
'nearbyint', 'nearbyintf', 'nearbyintl', 'rint', 'rintf', 'rintl',
283+
'lrint', 'lrintf', 'lrintl', 'llrint', 'llrintf', 'llrintl', 'round',
284+
'roundf', 'roundl', 'lround', 'lroundf', 'lroundl', 'llround',
285+
'llroundf', 'llroundl', 'trunc', 'truncf', 'truncl', 'fmodf', 'fmodl',
286+
'remainder', 'remainderf', 'remainderl', 'remquo', 'remquof',
287+
'remquol', 'copysign', 'copysignf', 'copysignl', 'nan', 'nanf',
288+
'nanl', 'nextafter', 'nextafterf', 'nextafterl', 'nexttoward',
289+
'nexttowardf', 'nexttowardl', 'fdim', 'fdimf', 'fdiml', 'fmax',
290+
'fmaxf', 'fmaxl', 'fmin', 'fminf', 'fminl', 'fmal', 'isgreater',
291+
'isgreaterequal', 'isless', 'islessequal', 'islessgreater',
292+
'isunordered',
293+
],
294+
# B.12 Nonlocal jumps
295+
'setjmp.h': C90_STDLIB_IDENTIFIERS['setjmp.h'],
296+
# B.13 Signal handling
297+
'signal.h': C90_STDLIB_IDENTIFIERS['signal.h'],
298+
# B.14 Variable arguments
299+
'stdarg.h': C90_STDLIB_IDENTIFIERS['stdarg.h'] + ['va_copy'],
300+
# B.15 Boolean type and values
301+
'stdbool.h': ['bool', 'true', 'false', '__bool_true_false_are_defined'],
302+
# B.16 Common definitions
303+
'stddef.h': C90_STDLIB_IDENTIFIERS['stddef.h'],
304+
# B.17 Integer types
305+
'stdint.h': [
306+
'intptr_t', 'uintptr_t', 'intmax_t', 'uintmax_t', 'INTN_MIN',
307+
'INTN_MAX', 'UINTN_MAX', 'INT_LEASTN_MIN', 'INT_LEASTN_MAX',
308+
'UINT_LEASTN_MAX', 'INT_FASTN_MIN', 'INT_FASTN_MAX', 'UINT_FASTN_MAX',
309+
'INTPTR_MIN', 'INTPTR_MAX', 'UINTPTR_MAX', 'INTMAX_MIN', 'INTMAX_MAX',
310+
'UINTMAX_MAX', 'PTRDIFF_MIN', 'PTRDIFF_MAX', 'SIG_ATOMIC_MIN',
311+
'SIG_ATOMIC_MAX', 'SIZE_MAX', 'WCHAR_MIN', 'WCHAR_MAX', 'WINT_MIN',
312+
'WINT_MAX', 'INTN_C', 'UINTN_C', 'INTMAX_C', 'UINTMAX_C',
313+
] + ['%s%d_t' % (n, v) for n,v in itertools.product(
314+
['int', 'uint', 'int_least', 'uint_least', 'int_fast', 'uint_fast'],
315+
[8, 16,32,64])],
316+
# B.18 Input/output
317+
'stdio.h': C90_STDLIB_IDENTIFIERS['stdio.h'] + [
318+
'mode', 'restrict', 'snprintf', 'vfscanf', 'vscanf',
319+
'vsnprintf', 'vsscanf',
320+
],
321+
# B.19 General utilities
322+
'stdlib.h': C90_STDLIB_IDENTIFIERS['stdlib.h'] + [
323+
'_Exit', 'labs', 'llabs', 'lldiv', 'lldiv_t', 'strtof', 'strtol',
324+
'strtold', 'strtoll', 'strtoul', 'strtoull'
325+
],
326+
# B.20 String handling
327+
'string.h': C90_STDLIB_IDENTIFIERS['string.h'],
328+
# B.21 Type-generic math
329+
'tgmath.h': [
330+
'acos', 'asin', 'atan', 'acosh', 'asinh', 'atanh', 'cos', 'sin', 'tan',
331+
'cosh', 'sinh', 'tanh', 'exp', 'log', 'pow', 'sqrt', 'fabs', 'atan2',
332+
'cbrt', 'ceil', 'copysign', 'erf', 'erfc', 'exp2', 'expm1', 'fdim',
333+
'floor', 'fma', 'fmax', 'fmin', 'fmod', 'frexp', 'hypot', 'ilogb',
334+
'ldexp', 'lgamma', 'llrint', 'llround', 'log10', 'log1p', 'log2',
335+
'logb', 'lrint', 'lround', 'nearbyint', 'nextafter', 'nexttoward',
336+
'remainder', 'remquo', 'rint', 'round', 'scalbn', 'scalbln', 'tgamma',
337+
'trunc', 'carg', 'cimag', 'conj', 'cproj', 'creal',
338+
],
339+
# B.22 Date and time
340+
'time.h': C90_STDLIB_IDENTIFIERS['time.h'] + ['CLOCKS_PER_SEC'],
341+
# B.23 Extended multibyte/wide character utilities
342+
'wchar.h': [
343+
'wchar_t', 'size_t', 'mbstate_t', 'wint_t', 'tm', 'NULL', 'WCHAR_MAX',
344+
'WCHAR_MIN', 'WEOF', 'fwprintf', 'fwscanf', 'swprintf', 'swscanf',
345+
'vfwprintf', 'vfwscanf', 'vswprintf', 'vswscanf', 'vwprintf',
346+
'vwscanf', 'wprintf', 'wscanf', 'fgetwc', 'fgetws', 'fputwc', 'fputws',
347+
'fwide', 'getwc', 'getwchar', 'putwc', 'putwchar', 'ungetwc', 'wcstod',
348+
'wcstof', 'double', 'int', 'long', 'long', 'long', 'wcscpy', 'wcsncpy',
349+
'wmemcpy', 'wmemmove', 'wcscat', 'wcsncat', 'wcscmp', 'wcscoll',
350+
'wcsncmp', 'wcsxfrm', 'wmemcmp', 'wcschr', 'wcscspn', 'wcspbrk',
351+
'wcsrchr', 'wcsspn', 'wcsstr', 'wcstok', 'wmemchr', 'wcslen',
352+
'wmemset', 'wcsftime', 'btowc', 'wctob', 'mbsinit', 'mbrlen',
353+
'mbrtowc', 'wcrtomb', 'mbsrtowcs', 'wcsrtombs',
354+
],
355+
}
356+
357+
358+
def isStdLibId(id_, standard='c99'):
359+
id_lists = []
360+
if standard == 'c89':
361+
id_lists = C90_STDLIB_IDENTIFIERS.values()
362+
elif standard == 'c99':
363+
id_lists = C99_STDLIB_IDENTIFIERS.values()
364+
for l in id_lists:
365+
if id_ in l:
366+
return True
367+
return False
368+
369+
109370
def getEssentialTypeCategory(expr):
110371
if not expr:
111372
return None
@@ -2075,14 +2336,23 @@ def misra_20_14(self, data):
20752336
ifStack.pop()
20762337

20772338
def misra_21_1(self, data):
2078-
# Reference: n1570 7.1.3 - Reserved identifiers
2079-
re_forbidden_macro = re.compile(r'#define (errno|_[_A-Z]+)')
2339+
re_forbidden_macro = re.compile(r'#(?:define|undef) _[_A-Z]+')
2340+
re_macro_name = re.compile(r'#(?:define|undef) (.+)[ $]')
20802341

2081-
# Search for forbidden identifiers in macro names
2082-
for directive in data.directives:
2083-
res = re.search(re_forbidden_macro, directive.str)
2084-
if res:
2085-
self.reportError(directive, 21, 1)
2342+
for d in data.directives:
2343+
# Search for forbidden identifiers
2344+
m = re.search(re_forbidden_macro, d.str)
2345+
if m:
2346+
self.reportError(d, 21, 1)
2347+
continue
2348+
2349+
# Search standard library identifiers in macro names
2350+
m = re.search(re_macro_name, d.str)
2351+
if not m:
2352+
continue
2353+
name = m.group(1)
2354+
if isStdLibId(name, data.standards.c):
2355+
self.reportError(d, 21, 1)
20862356

20872357
type_name_tokens = (t for t in data.tokenlist if t.typeScopeId)
20882358
type_fields_tokens = (t for t in data.tokenlist if t.valueType and t.valueType.typeScopeId)

‎addons/test/misra/misra-test.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,10 @@ union __union_21_1 { int a; }; // 21.1 19.2
10291029
typedef union { int a; } _union_21_1_t; // 21.1 19.2
10301030
typedef union { int a; } _Union_21_1_t; // 21.1 19.2
10311031
typedef union { int a; } __union_21_1_t; // 21.1 19.2
1032+
#define sqrt bar // 21.1
1033+
#define cos mycos // 21.1
1034+
#define sin foo // 21.1
1035+
#undef __LINE__ // 21.1 20.5
10321036

10331037
void misra_21_3() {
10341038
p1=malloc(10); // 21.3

0 commit comments

Comments
 (0)