Skip to content

Commit 122a020

Browse files
committed
Exclude identifiers generated from library macros
Library macros are not under user control
1 parent ca51fda commit 122a020

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

c/common/test/rules/declaredareservedidentifier/test.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,10 @@ tss_delete( // COMPLIANT - threads.h not included, not external linkage
169169
struct thrd_yield { // COMPLIANT - threads.h not included
170170
int thrd_exit; // COMPLIANT - threads.h not included
171171
};
172+
173+
#include <sys/select.h>
174+
void test_macro() {
175+
fd_set test_set;
176+
FD_ZERO(&test_set); // COMPLIANT - macro expands to variables with `__`
177+
// prefixes, but should be excluded
178+
}

cpp/common/src/codingstandards/cpp/rules/declaredareservedidentifier/DeclaredAReservedIdentifier.qll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,19 @@ predicate isCIdentifier(
138138
)
139139
}
140140

141+
Macro getGeneratedFrom(Element e) {
142+
isCIdentifier(e, _, _, _, _) and
143+
exists(MacroInvocation mi |
144+
mi = result.getAnInvocation() and
145+
mi.getAGeneratedElement() = e and
146+
mi.getLocation().getStartColumn() = e.getLocation().getStartColumn() and
147+
not exists(MacroInvocation child |
148+
child.getParentInvocation() = mi and
149+
child.getAGeneratedElement() = e
150+
)
151+
)
152+
}
153+
141154
module TargetedCLibrary = CStandardLibrary::C11;
142155

143156
query predicate problems(Element m, string message) {
@@ -146,6 +159,8 @@ query predicate problems(Element m, string message) {
146159
string name, Scope scope, CNameSpace cNameSpace, string reason, string identifierDescription
147160
|
148161
isCIdentifier(m, name, scope, cNameSpace, identifierDescription) and
162+
// Exclude cases generated from library macros, because the user does not control them
163+
not getGeneratedFrom(m) instanceof LibraryMacro and
149164
message = identifierDescription + " '" + name + "' " + reason + "."
150165
|
151166
// C11 7.1.3/1

0 commit comments

Comments
 (0)