Skip to content

Commit 927640b

Browse files
committed
Unshared Rule 21.2, and implement with MISRA rules
MISRA has slightly different rules to CERT, so unshare the rule.
1 parent 6998ed0 commit 927640b

File tree

6 files changed

+220
-8
lines changed

6 files changed

+220
-8
lines changed

c/misra/src/rules/RULE-21-2/DoNotDeclareAReservedIdentifier.ql

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515

1616
import cpp
1717
import codingstandards.c.misra
18-
import codingstandards.cpp.rules.declaredareservedidentifier.DeclaredAReservedIdentifier
18+
import codingstandards.cpp.ReservedNames
1919

20-
class DoNotDeclareAReservedIdentifierQuery extends DeclaredAReservedIdentifierSharedQuery {
21-
DoNotDeclareAReservedIdentifierQuery() {
22-
this = Declarations1Package::doNotDeclareAReservedIdentifierQuery()
23-
}
24-
}
20+
from Element e, string message
21+
where
22+
not isExcluded(e, Declarations1Package::doNotDeclareAReservedIdentifierQuery()) and
23+
ReservedNames::C11::isAReservedIdentifier(e, message, true) and
24+
// Not covered by this rule - covered by Rule 21.2
25+
not e instanceof PreprocessorDirective
26+
select e, message
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
| test.c:11:8:11:9 | _s | Type '_s' declares a name beginning with _ which is reserved in the tag name space. |
2+
| test.c:15:6:15:7 | _f | Function '_f' declares a name beginning with _ which is reserved in the ordinary name space. |
3+
| test.c:19:7:19:12 | malloc | Function 'malloc' declares a name which is reserved for external linkage from the C11 standard library header 'stdlib.h'. |
4+
| test.c:25:5:25:9 | errno | Global variable 'errno' declares a name which is reserved for external linkage from the C11 standard library header 'errno.h'. |
5+
| test.c:39:16:39:18 | log | Parameter 'log' declares a name reserved for a macro from the C11 standard library header 'tgmath.h'. |
6+
| test.c:43:5:43:16 | _Test_global | Global variable '_Test_global' declares a reserved name beginning _ followed by an uppercase letter. |
7+
| test.c:44:6:44:15 | _Test_func | Function '_Test_func' declares a reserved name beginning _ followed by an uppercase letter. |
8+
| test.c:45:9:45:19 | _Test_param | Parameter '_Test_param' declares a reserved name beginning _ followed by an uppercase letter. |
9+
| test.c:46:7:46:17 | _Test_local | Local variable '_Test_local' declares a reserved name beginning _ followed by an uppercase letter. |
10+
| test.c:47:10:47:27 | _Test_struct_local | Type '_Test_struct_local' declares a reserved name beginning _ followed by an uppercase letter. |
11+
| test.c:49:9:49:20 | _Test_member | Member variable '_Test_member' declares a reserved name beginning _ followed by an uppercase letter. |
12+
| test.c:52:8:52:19 | _Test_struct | Type '_Test_struct' declares a reserved name beginning _ followed by an uppercase letter. |
13+
| test.c:53:7:53:18 | _Test_member | Member variable '_Test_member' declares a reserved name beginning _ followed by an uppercase letter. |
14+
| test.c:59:5:59:24 | __test_double_global | Global variable '__test_double_global' declares a reserved name beginning with __. |
15+
| test.c:60:6:60:23 | __test_double_func | Function '__test_double_func' declares a reserved name beginning with __. |
16+
| test.c:61:9:61:27 | __test_double_param | Parameter '__test_double_param' declares a reserved name beginning with __. |
17+
| test.c:62:7:62:25 | __test_double_local | Local variable '__test_double_local' declares a reserved name beginning with __. |
18+
| test.c:63:10:63:35 | __test_double_struct_local | Type '__test_double_struct_local' declares a reserved name beginning with __. |
19+
| test.c:64:9:64:28 | __test_double_member | Member variable '__test_double_member' declares a reserved name beginning with __. |
20+
| test.c:67:8:67:27 | __test_double_struct | Type '__test_double_struct' declares a reserved name beginning with __. |
21+
| test.c:68:7:68:26 | __test_double_member | Member variable '__test_double_member' declares a reserved name beginning with __. |
22+
| test.c:77:5:77:22 | _test_lower_global | Global variable '_test_lower_global' declares a name beginning with _ which is reserved in the ordinary name space. |
23+
| test.c:78:6:78:21 | _test_lower_func | Function '_test_lower_func' declares a name beginning with _ which is reserved in the ordinary name space. |
24+
| test.c:87:8:87:19 | _test_struct | Type '_test_struct' declares a name beginning with _ which is reserved in the tag name space. |
25+
| test.c:93:5:93:14 | FE_INEXACT | Global variable 'FE_INEXACT' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
26+
| test.c:94:6:94:17 | FE_DIVBYZERO | Function 'FE_DIVBYZERO' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
27+
| test.c:95:9:95:18 | FE_INEXACT | Parameter 'FE_INEXACT' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
28+
| test.c:96:7:96:16 | FE_INVALID | Local variable 'FE_INVALID' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
29+
| test.c:97:10:97:19 | FE_INEXACT | Type 'FE_INEXACT' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
30+
| test.c:98:9:98:18 | FE_INEXACT | Member variable 'FE_INEXACT' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
31+
| test.c:101:8:101:17 | FE_INEXACT | Type 'FE_INEXACT' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
32+
| test.c:102:7:102:16 | FE_INEXACT | Member variable 'FE_INEXACT' declares a name reserved for a macro from the C11 standard library header 'fenv.h'. |
33+
| test.c:113:5:113:8 | exit | Global variable 'exit' declares a name which is reserved for external linkage from the C11 standard library header 'stdlib.h'. |
34+
| test.c:115:6:115:9 | free | Function 'free' declares a name which is reserved for external linkage from the C11 standard library header 'stdlib.h'. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-21-2/DoNotDeclareAReservedIdentifier.ql

c/misra/test/rules/RULE-21-2/DoNotDeclareAReservedIdentifier.testref

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#ifndef _RESERVED_MACRO
2+
#define _RESERVED_MACRO // Not reported by this rule
3+
#endif /* _RESERVED_MACRO */
4+
5+
#ifndef _also_reserved_MACRO
6+
#define _also_reserved_MACRO // Not reported by this rule
7+
#endif /* _not_reserved_MACRO */
8+
9+
static const int INT_LIMIT_MAX = 12000; // COMPLIANT future library directions
10+
11+
struct _s { // NON_COMPLIANT
12+
struct _s *_next; // COMPLIANT not file scope
13+
};
14+
15+
void _f() { // NON_COMPLIANT
16+
int _p; // COMPLIANT not file scope
17+
}
18+
19+
void *malloc(int bytes) { // NON_COMPLIANT
20+
void *ptr;
21+
return ptr;
22+
}
23+
24+
extern int
25+
errno; // NON_COMPLIANT - errno is explicitly reserved for external linkage
26+
27+
void output(int a, int b, int c);
28+
29+
#define DEBUG(...) \
30+
output(__VA_ARGS__) // COMPLIANT - using not declaring `__VA_ARGS__`
31+
32+
void test() {
33+
DEBUG(1, 2, 3);
34+
__FUNCTION__; // COMPLIANT - use, not declaration of `__FUNCTION__`
35+
__PRETTY_FUNCTION__; // COMPLIANT - use, not declaration of
36+
// `__PRETTY_FUNCTION__`
37+
}
38+
#include <tgmath.h>
39+
void test2(int log); // NON_COMPLIANT - tgmath.h defines log as a reserved macro
40+
41+
/* Test _[A-Z] */
42+
43+
int _Test_global; // NON_COMPLIANT - _ followed by capital is reserved
44+
void _Test_func( // NON_COMPLIANT - _ followed by capital is reserved
45+
int _Test_param) { // NON_COMPLIANT - _ followed by capital is reserved
46+
int _Test_local; // NON_COMPLIANT - _ followed by capital is reserved
47+
struct _Test_struct_local { // NON_COMPLIANT - _ followed by capital is
48+
// reserved
49+
int _Test_member; // NON_COMPLIANT - _ followed by capital is reserved
50+
};
51+
}
52+
struct _Test_struct { // NON_COMPLIANT - _ followed by capital is reserved
53+
int _Test_member; // NON_COMPLIANT - _ followed by capital is reserved
54+
};
55+
#define _TEST_MACRO x // Not reported by this rule
56+
57+
/* Test __ */
58+
59+
int __test_double_global; // NON_COMPLIANT - double _ is reserved
60+
void __test_double_func( // NON_COMPLIANT - double _ is reserved
61+
int __test_double_param) { // NON_COMPLIANT - double _ is reserved
62+
int __test_double_local; // NON_COMPLIANT - double _ is reserved
63+
struct __test_double_struct_local { // NON_COMPLIANT - double _ is reserved
64+
int __test_double_member; // NON_COMPLIANT - double _ is reserved
65+
};
66+
}
67+
struct __test_double_struct { // NON_COMPLIANT - double _ is reserved
68+
int __test_double_member; // NON_COMPLIANT - double _ is reserved
69+
};
70+
#define __TEST_MACRO x // Not reported by this rule
71+
72+
/*
73+
* Test _, but not followed by underscore or upper case, which is reserved in
74+
* file scope and ordinary/tag name spaces
75+
*/
76+
77+
int _test_lower_global; // NON_COMPLIANT - _ is reserved in ordinary name space
78+
void _test_lower_func( // NON_COMPLIANT - _ is reserved as a function name in
79+
// ordinary name space
80+
int _test_lower_param) { // COMPLIANT - _ is not reserved in the block name
81+
// space
82+
int _test; // COMPLIANT - _ is not reserved in the block name space
83+
struct _test_struct { // COMPLIANT - _ is not reserved in the block name space
84+
int _test; // COMPLIANT - _ is not reserved in the block name space
85+
};
86+
}
87+
struct _test_struct { // NON_COMPLIANT - _ is reserved in the tag name space
88+
int _test; // COMPLIANT - _ is not reserved in the member name space
89+
};
90+
#define _test_macro x // Not reported by this rule
91+
92+
/* Identify names reserved as a macro when the relevant header is included. */
93+
int FE_INEXACT; // NON_COMPLIANT - FE_INEXACT is reserved as a macro name
94+
void FE_DIVBYZERO( // NON_COMPLIANT - FE_DIVBYZERO is reserved as a macro name
95+
int FE_INEXACT) { // NON_COMPLIANT - FE_INEXACT is reserved as a macro name
96+
int FE_INVALID; // NON_COMPLIANT - FE_INEXACT is reserved as a macro name
97+
struct FE_INEXACT { // NON_COMPLIANT - FE_INEXACT is reserved as a macro name
98+
int FE_INEXACT; // NON_COMPLIANT - FE_INEXACT is reserved as a macro name
99+
};
100+
}
101+
struct FE_INEXACT { // NON_COMPLIANT - FE_INEXACT is reserved as a macro name
102+
int FE_INEXACT; // NON_COMPLIANT - FE_INEXACT is reserved as a macro name
103+
};
104+
#define FE_INEXACT x // Not reported by this rule
105+
106+
// We include the header after the declarations to avoid the inbuilt macros
107+
// expanding in the declarations above. The rule is not, however, sensitive
108+
// to location, so we should still report in this case.
109+
#include <fenv.h>
110+
111+
/* Functions and objects with external linkage */
112+
113+
int exit; // NON_COMPLIANT - reserved for external linkage, even though header
114+
// was not included
115+
void free() { // NON_COMPLIANT - reserved for external linkage, even though
116+
// header was not included
117+
}
118+
static int rand; // COMPLIANT - only reserved for external linkage or if random
119+
// was included
120+
static int srand() { // COMPLIANT - only reserved for external linkage or if
121+
// random was included
122+
}
123+
124+
#include <string.h>
125+
#define strlen 0 // Not reported by this rule
126+
127+
// The examples below are non compliant, because those symbols are reserved in
128+
// file scope when string.h is included. However, the compiler/edg rejects the
129+
// declaration cases, so we cannot test them.
130+
131+
// static int strlen;
132+
// static void *memcpy(void *s1, const void *s2, size_t n) {}
133+
134+
#include <time.h>
135+
#define tm_sec 0 // Not reported by this rule
136+
137+
// The examples below are non compliant, because those symbols are reserved in
138+
// file scope when time.h is included. However, the compiler/edg rejects the
139+
// declaration cases, so we cannot test them.
140+
141+
// struct tm {
142+
// int tm_sec;
143+
// int tm_min;
144+
// int tm_hour;
145+
// int tm_mday;
146+
// int tm_mon;
147+
// int tm_year;
148+
// int tm_wday;
149+
// int tm_yday;
150+
// int tm_isdst;
151+
// long __tm_gmtoff;
152+
// const char *__tm_zone;
153+
// };
154+
155+
// Examples below are compliant because threads.h is not included
156+
157+
#define tss_set 0 // Not reported by this rule
158+
static int tss_get; // COMPLIANT - threads.h not included, not external linkage
159+
static void
160+
tss_delete( // COMPLIANT - threads.h not included, not external linkage
161+
int tss_create // COMPLIANT - threads.h not included
162+
) {
163+
int thrd_detach; // COMPLIANT - threads.h not included
164+
struct thrd_join { // COMPLIANT - threads.h not included
165+
int thrd_equal; // COMPLIANT - threads.h not included
166+
};
167+
}
168+
struct thrd_yield { // COMPLIANT - threads.h not included
169+
int thrd_exit; // COMPLIANT - threads.h not included
170+
};
171+
172+
#include <sys/select.h>
173+
void test_macro() {
174+
fd_set test_set;
175+
FD_ZERO(&test_set); // COMPLIANT - macro expands to variables with `__`
176+
// prefixes, but should be excluded
177+
}

rule_packages/c/Declarations1.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
"precision": "very-high",
7676
"severity": "warning",
7777
"short_name": "DoNotDeclareAReservedIdentifier",
78-
"shared_implementation_short_name": "DeclaredAReservedIdentifier",
7978
"tags": [
8079
"correctness",
8180
"maintainability",

0 commit comments

Comments
 (0)