Skip to content

Commit b7e7ee6

Browse files
committed
Merge remote-tracking branch 'origin/jsinglet/language2' into jsinglet/language2
2 parents b144d5c + f2a9dcb commit b7e7ee6

31 files changed

+248
-31
lines changed

.github/touch

Lines changed: 0 additions & 1 deletion
This file was deleted.

.vscode/tasks.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@
254254
"Preprocessor3",
255255
"Preprocessor4",
256256
"Preprocessor5",
257+
"Preprocessor6",
257258
"IntegerConversion",
258259
"Expressions",
259260
"DeadCode",

c/cert/src/qlpack.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
name: cert-c-coding-standards
2-
version: 2.11.0-dev
2+
version: 2.12.0-dev
33
suites: codeql-suites
44
libraryPathDependencies: common-c-coding-standards

c/cert/src/rules/EXP30-C/DependenceOnOrderOfFunctionArgumentsForSideEffects.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import codingstandards.c.cert
1616
import codingstandards.cpp.SideEffect
1717
import semmle.code.cpp.dataflow.DataFlow
1818
import semmle.code.cpp.dataflow.TaintTracking
19-
import semmle.code.cpp.valuenumbering.GlobalValueNumberingImpl
19+
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
2020

2121
/** Holds if the function's return value is derived from the `AliasParamter` p. */
2222
predicate returnValueDependsOnAliasParameter(AliasParameter p) {

c/cert/test/qlpack.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
name: cert-c-coding-standards-tests
2-
version: 2.11.0-dev
2+
version: 2.12.0-dev
33
libraryPathDependencies: cert-c-coding-standards
44
extractor: cpp
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import cpp
2+
import codingstandards.cpp.Macro
3+
import codingstandards.cpp.Naming
4+
5+
/**
6+
* Macros that cannot be replaced by functions
7+
*/
8+
abstract class IrreplaceableFunctionLikeMacro extends FunctionLikeMacro { }
9+
10+
/** A function like macro that contains the use of a stringize or tokenize operator should not be replaced by a function. */
11+
private class StringizeOrTokenizeMacro extends IrreplaceableFunctionLikeMacro {
12+
StringizeOrTokenizeMacro() {
13+
exists(TokenPastingOperator t | t.getMacro() = this) or
14+
exists(StringizingOperator s | s.getMacro() = this)
15+
}
16+
}
17+
18+
/** A standard library function like macro that should not be replaced by a function. */
19+
private class StandardLibraryFunctionLikeMacro extends IrreplaceableFunctionLikeMacro {
20+
StandardLibraryFunctionLikeMacro() { Naming::Cpp14::hasStandardLibraryMacroName(this.getName()) }
21+
}
22+
23+
/** A function like macro invocation as an `asm` argument cannot be replaced by a function. */
24+
private class AsmArgumentInvoked extends IrreplaceableFunctionLikeMacro {
25+
AsmArgumentInvoked() {
26+
any(AsmStmt s).getLocation().subsumes(this.getAnInvocation().getLocation())
27+
}
28+
}
29+
30+
/** 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. */
31+
private class OnlyConstantArgsInvoked extends IrreplaceableFunctionLikeMacro {
32+
OnlyConstantArgsInvoked() {
33+
forex(MacroInvocation mi | mi = this.getAnInvocation() |
34+
//int/float literals
35+
mi.getUnexpandedArgument(_).regexpMatch("\\d+")
36+
or
37+
//char literal or string literal, which is a literal surrounded by single quotes or double quotes
38+
mi.getUnexpandedArgument(_).regexpMatch("('[^']*'|\"[^\"]*\")")
39+
)
40+
}
41+
}
42+
43+
/** A function like macro invoked to initialize an object with static storage that cannot be replaced with a function call. */
44+
private class UsedToStaticInitialize extends IrreplaceableFunctionLikeMacro {
45+
UsedToStaticInitialize() {
46+
any(StaticStorageDurationVariable v).getInitializer().getExpr() =
47+
this.getAnInvocation().getExpr()
48+
}
49+
}
50+
51+
/** A function like macro that is called with an argument that is an operator that cannot be replaced with a function call. */
52+
private class FunctionLikeMacroWithOperatorArgument extends IrreplaceableFunctionLikeMacro {
53+
FunctionLikeMacroWithOperatorArgument() {
54+
exists(MacroInvocation mi | mi.getMacro() = this |
55+
mi.getUnexpandedArgument(_) = any(Operation op).getOperator()
56+
)
57+
}
58+
}

c/common/src/qlpack.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
name: common-c-coding-standards
2-
version: 2.11.0-dev
2+
version: 2.12.0-dev
33
libraryPathDependencies: common-cpp-coding-standards

c/common/test/qlpack.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
name: common-c-coding-standards-tests
2-
version: 2.11.0-dev
2+
version: 2.12.0-dev
33
libraryPathDependencies: common-c-coding-standards
44
extractor: cpp

c/misra/src/qlpack.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
name: misra-c-coding-standards
2-
version: 2.11.0-dev
2+
version: 2.12.0-dev
33
suites: codeql-suites
44
libraryPathDependencies: common-c-coding-standards
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @id c/misra/function-over-function-like-macro
3+
* @name DIR-4-9: A function should be used in preference to a function-like macro where they are interchangeable
4+
* @description Using a function-like macro instead of a function can lead to unexpected program
5+
* behaviour.
6+
* @kind problem
7+
* @precision medium
8+
* @problem.severity recommendation
9+
* @tags external/misra/id/dir-4-9
10+
* external/misra/audit
11+
* maintainability
12+
* readability
13+
* external/misra/obligation/advisory
14+
*/
15+
16+
import cpp
17+
import codingstandards.c.misra
18+
import codingstandards.c.IrreplaceableFunctionLikeMacro
19+
20+
predicate partOfConstantExpr(MacroInvocation i) {
21+
exists(Expr e |
22+
e.isConstant() and
23+
not i.getExpr() = e and
24+
i.getExpr().getParent+() = e
25+
)
26+
}
27+
28+
from FunctionLikeMacro m
29+
where
30+
not isExcluded(m, Preprocessor6Package::functionOverFunctionLikeMacroQuery()) and
31+
not m instanceof IrreplaceableFunctionLikeMacro and
32+
//macros can have empty body
33+
not m.getBody().length() = 0 and
34+
//function call not allowed in a constant expression (where constant expr is parent)
35+
forall(MacroInvocation i | i = m.getAnInvocation() | not partOfConstantExpr(i))
36+
select m, "Macro used instead of a function."

0 commit comments

Comments
 (0)