-
Notifications
You must be signed in to change notification settings - Fork 67
Package Preprocessor6 #99
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 all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c5f0a59
Preprocessor6: add DIR-4-9
knewbury01 b10990a
Preprocessor6: fix metadata
knewbury01 93a709d
Preprocessor6: simplify DIR-4-9
knewbury01 5245fcc
Preprocessor6: simplify DIR-4-9
knewbury01 5ff8929
Preprocessor6: address review comments DIR-4-9
knewbury01 c6e4d08
Merge branch 'main' into knewbury01/Preprocessor6
knewbury01 055e83f
Merge branch 'main' into knewbury01/Preprocessor6
knewbury01 8973820
Preprocessor6: improve DIR-4-9
knewbury01 8572310
Merge branch 'main' into knewbury01/Preprocessor6
knewbury01 f15f8f5
Preprocessor6: improve DIR-4-9
knewbury01 fe0d3e4
Preprocessor6: format DIR-4-9
knewbury01 0a22c71
Merge branch 'main' into knewbury01/Preprocessor6
knewbury01 b6acb52
Preprocessor6: move IrreplaceableFunctionLikeMacro lib location
knewbury01 a649cd2
Merge branch 'knewbury01/Preprocessor6' of https://github.com/knewbur…
knewbury01 9af039a
Merge branch 'main' into knewbury01/Preprocessor6
knewbury01 669fee3
Merge branch 'main' into knewbury01/Preprocessor6
knewbury01 d266292
Fix rules.csv accidental revert for rule
knewbury01 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
58 changes: 58 additions & 0 deletions
58
c/common/src/codingstandards/c/IrreplaceableFunctionLikeMacro.qll
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,58 @@ | ||
import cpp | ||
import codingstandards.cpp.Macro | ||
import codingstandards.cpp.Naming | ||
|
||
/** | ||
* Macros that cannot be replaced by functions | ||
*/ | ||
abstract class IrreplaceableFunctionLikeMacro extends FunctionLikeMacro { } | ||
|
||
/** A function like macro that contains the use of a stringize or tokenize operator should not be replaced by a function. */ | ||
private class StringizeOrTokenizeMacro extends IrreplaceableFunctionLikeMacro { | ||
StringizeOrTokenizeMacro() { | ||
exists(TokenPastingOperator t | t.getMacro() = this) or | ||
exists(StringizingOperator s | s.getMacro() = this) | ||
} | ||
} | ||
|
||
/** A standard library function like macro that should not be replaced by a function. */ | ||
private class StandardLibraryFunctionLikeMacro extends IrreplaceableFunctionLikeMacro { | ||
StandardLibraryFunctionLikeMacro() { Naming::Cpp14::hasStandardLibraryMacroName(this.getName()) } | ||
} | ||
|
||
/** A function like macro invocation as an `asm` argument cannot be replaced by a function. */ | ||
private class AsmArgumentInvoked extends IrreplaceableFunctionLikeMacro { | ||
AsmArgumentInvoked() { | ||
any(AsmStmt s).getLocation().subsumes(this.getAnInvocation().getLocation()) | ||
} | ||
} | ||
|
||
/** A macro that is only invoked with constant arguments is more likely to be compile-time evaluated than a function call so do not suggest replacement. */ | ||
private class OnlyConstantArgsInvoked extends IrreplaceableFunctionLikeMacro { | ||
OnlyConstantArgsInvoked() { | ||
forex(MacroInvocation mi | mi = this.getAnInvocation() | | ||
//int/float literals | ||
mi.getUnexpandedArgument(_).regexpMatch("\\d+") | ||
or | ||
//char literal or string literal, which is a literal surrounded by single quotes or double quotes | ||
mi.getUnexpandedArgument(_).regexpMatch("('[^']*'|\"[^\"]*\")") | ||
) | ||
} | ||
} | ||
|
||
/** A function like macro invoked to initialize an object with static storage that cannot be replaced with a function call. */ | ||
private class UsedToStaticInitialize extends IrreplaceableFunctionLikeMacro { | ||
UsedToStaticInitialize() { | ||
any(StaticStorageDurationVariable v).getInitializer().getExpr() = | ||
this.getAnInvocation().getExpr() | ||
} | ||
} | ||
|
||
/** A function like macro that is called with an argument that is an operator that cannot be replaced with a function call. */ | ||
private class FunctionLikeMacroWithOperatorArgument extends IrreplaceableFunctionLikeMacro { | ||
FunctionLikeMacroWithOperatorArgument() { | ||
exists(MacroInvocation mi | mi.getMacro() = this | | ||
mi.getUnexpandedArgument(_) = any(Operation op).getOperator() | ||
) | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
c/misra/src/rules/DIR-4-9/FunctionOverFunctionLikeMacro.ql
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,36 @@ | ||
/** | ||
* @id c/misra/function-over-function-like-macro | ||
* @name DIR-4-9: A function should be used in preference to a function-like macro where they are interchangeable | ||
* @description Using a function-like macro instead of a function can lead to unexpected program | ||
* behaviour. | ||
* @kind problem | ||
* @precision medium | ||
* @problem.severity recommendation | ||
* @tags external/misra/id/dir-4-9 | ||
* external/misra/audit | ||
* maintainability | ||
* readability | ||
* external/misra/obligation/advisory | ||
*/ | ||
|
||
import cpp | ||
import codingstandards.c.misra | ||
import codingstandards.c.IrreplaceableFunctionLikeMacro | ||
|
||
predicate partOfConstantExpr(MacroInvocation i) { | ||
exists(Expr e | | ||
e.isConstant() and | ||
not i.getExpr() = e and | ||
i.getExpr().getParent+() = e | ||
) | ||
} | ||
|
||
from FunctionLikeMacro m | ||
where | ||
not isExcluded(m, Preprocessor6Package::functionOverFunctionLikeMacroQuery()) and | ||
not m instanceof IrreplaceableFunctionLikeMacro and | ||
//macros can have empty body | ||
not m.getBody().length() = 0 and | ||
//function call not allowed in a constant expression (where constant expr is parent) | ||
forall(MacroInvocation i | i = m.getAnInvocation() | not partOfConstantExpr(i)) | ||
select m, "Macro used instead of a function." |
2 changes: 2 additions & 0 deletions
2
c/misra/test/rules/DIR-4-9/FunctionOverFunctionLikeMacro.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 |
---|---|---|
@@ -0,0 +1,2 @@ | ||
| test.c:6:1:6:25 | #define MACRO4(x) (x + 1) | Macro used instead of a function. | | ||
| test.c:11:1:11:48 | #define MACRO9() printf_custom("output = %d", 7) | Macro used instead of a function. | |
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 @@ | ||
rules/DIR-4-9/FunctionOverFunctionLikeMacro.ql |
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,40 @@ | ||
#include <assert.h> | ||
|
||
#define MACRO(OP, L, R) ((L)OP(R)) // COMPLIANT | ||
#define MACRO2(L, R) (L + R) // COMPLIANT | ||
#define MACRO3(L, R) (L " " R " " L) // COMPLIANT | ||
#define MACRO4(x) (x + 1) // NON_COMPLIANT | ||
#define MACRO5(L, LR) (LR + 1) // COMPLIANT | ||
#define MACRO6(x) printf_custom("output = %d", test##x) // COMPLIANT | ||
#define MACRO7(x) #x // COMPLIANT | ||
#define MACRO8(x) "NOP" // COMPLIANT | ||
#define MACRO9() printf_custom("output = %d", 7) // NON_COMPLIANT | ||
#define MACRO10(x) // COMPLIANT | ||
#define MY_ASSERT(X) assert(X) // NON_COMPLIANT[FALSE_NEGATIVE] | ||
|
||
const char a1[MACRO2(1, 1) + 6]; | ||
extern printf_custom(); | ||
int test1; | ||
|
||
void f() { | ||
int i = MACRO(+, 1, 1); | ||
int i2 = MACRO2(7, 10); | ||
|
||
static int i3 = MACRO2(1, 1); | ||
|
||
char *i4 = MACRO3("prefix", "suffix"); | ||
|
||
int i5 = MACRO4(1); | ||
|
||
int i6 = MACRO4(MACRO2(1, 1)); | ||
|
||
int i7 = MACRO5(1, 1); | ||
|
||
MACRO6(1); | ||
|
||
char *i10 = MACRO7("prefix"); | ||
|
||
asm(MACRO8(1)); | ||
|
||
MY_ASSERT(1); | ||
} |
25 changes: 25 additions & 0 deletions
25
cpp/common/src/codingstandards/cpp/exclusions/c/Preprocessor6.qll
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,25 @@ | ||
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/ | ||
import cpp | ||
import RuleMetadata | ||
import codingstandards.cpp.exclusions.RuleMetadata | ||
|
||
newtype Preprocessor6Query = TFunctionOverFunctionLikeMacroQuery() | ||
|
||
predicate isPreprocessor6QueryMetadata(Query query, string queryId, string ruleId) { | ||
query = | ||
// `Query` instance for the `functionOverFunctionLikeMacro` query | ||
Preprocessor6Package::functionOverFunctionLikeMacroQuery() and | ||
queryId = | ||
// `@id` for the `functionOverFunctionLikeMacro` query | ||
"c/misra/function-over-function-like-macro" and | ||
ruleId = "DIR-4-9" | ||
} | ||
|
||
module Preprocessor6Package { | ||
Query functionOverFunctionLikeMacroQuery() { | ||
//autogenerate `Query` type | ||
result = | ||
// `Query` type for `functionOverFunctionLikeMacro` query | ||
TQueryC(TPreprocessor6PackageQuery(TFunctionOverFunctionLikeMacroQuery())) | ||
} | ||
} |
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
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,25 @@ | ||
{ | ||
"MISRA-C-2012": { | ||
"DIR-4-9": { | ||
"properties": { | ||
"obligation": "advisory" | ||
}, | ||
"queries": [ | ||
{ | ||
"description": "Using a function-like macro instead of a function can lead to unexpected program behaviour.", | ||
"kind": "problem", | ||
"name": "A function should be used in preference to a function-like macro where they are interchangeable", | ||
"precision": "medium", | ||
"severity": "recommendation", | ||
"short_name": "FunctionOverFunctionLikeMacro", | ||
"tags": [ | ||
"external/misra/audit", | ||
"maintainability", | ||
"readability" | ||
] | ||
} | ||
], | ||
"title": "A function should be used in preference to a function-like macro where they are interchangeable" | ||
} | ||
} | ||
} |
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
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.