Skip to content

Declarations3: fix RULE-5-5 #98

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion c/common/src/codingstandards/c/Identifiers.qll
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ class InterestingIdentifiers extends Declaration {
exists(this.getADeclarationLocation())
}

string getSignificantName() { result = this.getName().prefix(31) }
//this definition of significant relies on the number of significant characters for a macro name (C99)
//this is used on macro name comparisons only
//not necessarily against other types of identifiers
string getSignificantNameComparedToMacro() { result = this.getName().prefix(63) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ where
not isExcluded(i, Declarations3Package::identifiersNotDistinctFromMacroNamesQuery()) and
mName = iName and
(
//C99 states the first 31 characters of external identifiers are significant
//C90 states the first 6 characters of external identifiers are significant and case is not required to be significant
//C99 states the first 63 characters of macro identifiers are significant
//C90 states the first 31 characters of macro identifiers are significant
//C90 is not currently considered by this rule
if m.getName().length() > 31 then mName = m.getName().prefix(31) else mName = m.getName()
if m.getName().length() > 63 then mName = m.getName().prefix(63) else mName = m.getName()
) and
if i.getName().length() > 31 then iName = i.getSignificantName() else iName = i.getName()
if i.getName().length() > 63
then iName = i.getSignificantNameComparedToMacro()
else iName = i.getName()
select m, "Macro name is nonunique compared to $@.", i, i.getName()
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
| test.c:1:1:1:23 | #define Sum(x,y) x + y | Macro name is nonunique compared to $@. | test.c:4:5:4:7 | Sum | Sum |
| test.c:6:1:6:42 | #define iltiqzxgfqsgigwfuyntzghvzltueeaZ ; | Macro name is nonunique compared to $@. | test.c:7:12:7:43 | iltiqzxgfqsgigwfuyntzghvzltueeaQ | iltiqzxgfqsgigwfuyntzghvzltueeaQ |
| test.c:6:1:6:72 | #define iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA | Macro name is nonunique compared to $@. | test.c:11:5:11:68 | iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB | iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB |
11 changes: 9 additions & 2 deletions c/misra/test/rules/RULE-5-5/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,12 @@

int Sum;

#define iltiqzxgfqsgigwfuyntzghvzltueeaZ ; // NON_COMPLIANT - length 32
static int iltiqzxgfqsgigwfuyntzghvzltueeaQ; // NON_COMPLIANT - length 32
#define iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA // NON_COMPLIANT
// -
// length
// 64
static int
iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB; // NON_COMPLIANT
// -
// length
// 64