-
Notifications
You must be signed in to change notification settings - Fork 67
RULE-5-4
: Exclude results which do not occur in the same compilation, improve alert message
#769
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
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f5437d2
Add an "Includes" library for detecting conditional includes
lcartey 650842b
PreprocessDirectives: Add detection for conditionally defined macros
lcartey 1543019
Includes: Improve performance
lcartey 34abb42
Rule 5.4: Exclude cases where the two macros are conditional
lcartey 08c0aaf
Rule 5.4: add conditional test cases
lcartey 671620f
Rule 5.4: Improve alert message, ensure efficient performance
lcartey 892cb96
Add change note
lcartey 57dd748
Rule 5.4: Address review comments
lcartey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
c/misra/test/rules/RULE-5-4/MacroIdentifiersNotDistinct.expected
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
| header3.h:7:1:7:24 | #define MULTIPLE_INCLUDE | Definition of macro MULTIPLE_INCLUDE is not distinct from alternative definition of $@ in rules/RULE-5-4/header4.h. | header4.h:1:1:1:24 | #define MULTIPLE_INCLUDE | MULTIPLE_INCLUDE | | ||
| header3.h:14:1:14:21 | #define NOT_PROTECTED | Definition of macro NOT_PROTECTED is not distinct from alternative definition of $@ in rules/RULE-5-4/header4.h. | header4.h:12:1:12:23 | #define NOT_PROTECTED 1 | NOT_PROTECTED | | ||
| test.c:2:1:2:72 | #define iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB | Macro identifer iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyB is nondistinct in first 63 characters, compared to $@. | test.c:1:1:1:72 | #define iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA | iltiqzxgfqsgigwfuyntzghvzltueatcxqnqofnnvjyszmcsylyohvqaosjbqyyA | | ||
| test.c:8:1:8:31 | #define FUNCTION_MACRO(X) X + 1 | Macro identifer FUNCTION_MACRO is nondistinct in first 63 characters, compared to $@. | test.c:7:1:7:57 | #define FUNCTION_MACRO(FUNCTION_MACRO) FUNCTION_MACRO + 1 | FUNCTION_MACRO | | ||
| test.c:8:1:8:31 | #define FUNCTION_MACRO(X) X + 1 | Definition of macro FUNCTION_MACRO is not distinct from alternative definition of $@ in rules/RULE-5-4/test.c. | test.c:7:1:7:57 | #define FUNCTION_MACRO(FUNCTION_MACRO) FUNCTION_MACRO + 1 | FUNCTION_MACRO | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifdef FOO | ||
#include "header1.h" | ||
#else | ||
#include "header2.h" | ||
#endif | ||
|
||
#ifdef FOO | ||
#define A_MACRO 1 // COMPLIANT | ||
#else | ||
#define A_MACRO 2 // COMPLIANT | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#define REPEATED 11 // COMPLIANT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#define REPEATED 1 // COMPLIANT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef HEADER3_H | ||
#define HEADER3_H | ||
|
||
// We should ignore the header guards in this file | ||
|
||
// This is defined unconditionally by both header3.h and header4.h | ||
#define MULTIPLE_INCLUDE // NON_COMPLIANT | ||
|
||
// This is redefined in header3.h, but only if it isn't already defined | ||
#define PROTECTED // COMPLIANT | ||
|
||
// This is redefined in header3.h, but is conditional on some other condition, | ||
// so this is redefined | ||
#define NOT_PROTECTED // NON_COMPLIANT | ||
|
||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#define MULTIPLE_INCLUDE // NON_COMPLIANT | ||
|
||
// This case is triggered from root2.c | ||
// because PROTECTED isn't defined in | ||
// that case | ||
#ifndef PROTECTED | ||
#define PROTECTED // COMPLIANT - checked by guard | ||
#endif | ||
|
||
// Always enabled, so conflicts in root1.c case | ||
#ifdef MULTIPLE_INCLUDE | ||
#define NOT_PROTECTED 1 // NON_COMPLIANT | ||
#endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
#define FOO 1 | ||
#include "conditional.h" | ||
|
||
// Both headers define MULTIPLE_INCLUDE | ||
#include "header3.h" | ||
#include "header4.h" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#include "conditional.h" | ||
|
||
#include "header4.h" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
- `RULE-5-4` - `MacroIdentifiersNotDistinct.ql`: | ||
- Exclude false positives related to conditional compilation, where a macro may be defined twice, but not within the same compilation. | ||
- Improve alert message in the case the 63 char limit is not relevant by using the form "Definition of macro `<MACRO_NAME>` is not distinct from alternative definition of `<MACRO_NAME>` in `<relative_file_path>`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** A library which supports analysis of includes. */ | ||
|
||
import cpp | ||
import codingstandards.cpp.PreprocessorDirective | ||
import semmle.code.cpp.headers.MultipleInclusion | ||
|
||
pragma[noinline] | ||
private predicate hasIncludeLocation(Include include, string filepath, int startline) { | ||
include.getLocation().hasLocationInfo(filepath, startline, _, _, _) | ||
} | ||
|
||
/** | ||
* Holds if `include` is included conditionally based on the branch directive `b1`. | ||
*/ | ||
pragma[noinline] | ||
predicate isConditionallyIncluded(PreprocessorBranchDirective bd, Include include) { | ||
not bd = any(CorrectIncludeGuard c).getIfndef() and | ||
not bd.getHead().regexpMatch(".*_H(_.*)?") and | ||
exists(string filepath, int startline, int endline, int includeline | | ||
isBranchDirectiveRange(bd, filepath, startline, endline) and | ||
hasIncludeLocation(include, filepath, includeline) and | ||
startline < includeline and | ||
endline > includeline | ||
) | ||
} | ||
|
||
/** | ||
* Gets a file which is directly included from `fromFile` unconditionally. | ||
*/ | ||
File getAnUnconditionallyIncludedFile(File fromFile) { | ||
// Find an include which isn't conditional | ||
exists(Include i | | ||
i.getFile() = fromFile and | ||
not isConditionallyIncluded(_, i) and | ||
result = i.getIncludedFile() | ||
) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.