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."

c/misra/test/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-tests
2-
version: 2.11.0-dev
2+
version: 2.12.0-dev
33
libraryPathDependencies: misra-c-coding-standards
44
extractor: cpp
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
| test.c:6:1:6:25 | #define MACRO4(x) (x + 1) | Macro used instead of a function. |
2+
| test.c:11:1:11:48 | #define MACRO9() printf_custom("output = %d", 7) | Macro used instead of a function. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/DIR-4-9/FunctionOverFunctionLikeMacro.ql

c/misra/test/rules/DIR-4-9/test.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include <assert.h>
2+
3+
#define MACRO(OP, L, R) ((L)OP(R)) // COMPLIANT
4+
#define MACRO2(L, R) (L + R) // COMPLIANT
5+
#define MACRO3(L, R) (L " " R " " L) // COMPLIANT
6+
#define MACRO4(x) (x + 1) // NON_COMPLIANT
7+
#define MACRO5(L, LR) (LR + 1) // COMPLIANT
8+
#define MACRO6(x) printf_custom("output = %d", test##x) // COMPLIANT
9+
#define MACRO7(x) #x // COMPLIANT
10+
#define MACRO8(x) "NOP" // COMPLIANT
11+
#define MACRO9() printf_custom("output = %d", 7) // NON_COMPLIANT
12+
#define MACRO10(x) // COMPLIANT
13+
#define MY_ASSERT(X) assert(X) // NON_COMPLIANT[FALSE_NEGATIVE]
14+
15+
const char a1[MACRO2(1, 1) + 6];
16+
extern printf_custom();
17+
int test1;
18+
19+
void f() {
20+
int i = MACRO(+, 1, 1);
21+
int i2 = MACRO2(7, 10);
22+
23+
static int i3 = MACRO2(1, 1);
24+
25+
char *i4 = MACRO3("prefix", "suffix");
26+
27+
int i5 = MACRO4(1);
28+
29+
int i6 = MACRO4(MACRO2(1, 1));
30+
31+
int i7 = MACRO5(1, 1);
32+
33+
MACRO6(1);
34+
35+
char *i10 = MACRO7("prefix");
36+
37+
asm(MACRO8(1));
38+
39+
MY_ASSERT(1);
40+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- `EXP30-C` - `DependenceOnOrderOfFunctionArgumentsForSideEffects.ql`:
2+
- Prefer the `GlobalValueNumbering` CodeQL library over the `GlobalValueNumberingImpl` library, as the former yields higher quality results and the latter is going to be deprecated. This also improves performance when multiple queries are evaluated, due to more sharing of intermediate computations.
3+
- `EXP50-CPP` - `DoNotDependOnTheOrderOfEvaluationForSideEffectsInFunctionCallsAsFunctionArguments.ql`:
4+
- Prefer the `GlobalValueNumbering` CodeQL library over the `GlobalValueNumberingImpl` library, as the former yields higher quality results and the latter is going to be deprecated. This also improves performance when multiple queries are evaluated, due to more sharing of intermediate computations.

cpp/autosar/src/qlpack.yml

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

cpp/autosar/test/qlpack.yml

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

cpp/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-cpp-coding-standards
2-
version: 2.11.0-dev
2+
version: 2.12.0-dev
33
suites: codeql-suites
44
libraryPathDependencies: common-cpp-coding-standards

cpp/cert/src/rules/EXP50-CPP/DoNotDependOnTheOrderOfEvaluationForSideEffectsInFunctionCallsAsFunctionArguments.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import codingstandards.cpp.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) {

cpp/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-cpp-coding-standards-tests
2-
version: 2.11.0-dev
2+
version: 2.12.0-dev
33
libraryPathDependencies: cert-cpp-coding-standards
44
extractor: cpp

cpp/cert/test/rules/EXP50-CPP/DoNotDependOnTheOrderOfEvaluationForSideEffectsInFunctionCallsAsFunctionArguments.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@
22
| test.cpp:84:3:84:4 | call to f2 | Depending on the order of evaluation for the arguments $@ and $@ for side effects on shared state is unspecified and can result in unexpected behavior. | test.cpp:84:6:84:7 | call to f5 | call to f5 | test.cpp:84:12:84:13 | call to f7 | call to f7 |
33
| test.cpp:87:3:87:4 | call to f2 | Depending on the order of evaluation for the arguments $@ and $@ for side effects on shared state is unspecified and can result in unexpected behavior. | test.cpp:87:9:87:10 | call to m1 | call to m1 | test.cpp:87:18:87:19 | call to m1 | call to m1 |
44
| test.cpp:89:3:89:4 | call to f2 | Depending on the order of evaluation for the arguments $@ and $@ for side effects on shared state is unspecified and can result in unexpected behavior. | test.cpp:89:9:89:10 | call to m2 | call to m2 | test.cpp:89:18:89:19 | call to m2 | call to m2 |
5+
| test.cpp:92:3:92:4 | call to f2 | Depending on the order of evaluation for the arguments $@ and $@ for side effects on shared state is unspecified and can result in unexpected behavior. | test.cpp:92:6:92:7 | call to f8 | call to f8 | test.cpp:92:14:92:15 | call to f9 | call to f9 |
6+
| test.cpp:93:3:93:4 | call to f2 | Depending on the order of evaluation for the arguments $@ and $@ for side effects on shared state is unspecified and can result in unexpected behavior. | test.cpp:93:6:93:7 | call to f8 | call to f8 | test.cpp:93:14:93:16 | call to f11 | call to f11 |
7+
| test.cpp:95:3:95:4 | call to f2 | Depending on the order of evaluation for the arguments $@ and $@ for side effects on shared state is unspecified and can result in unexpected behavior. | test.cpp:95:6:95:7 | call to f8 | call to f8 | test.cpp:95:13:95:14 | call to f9 | call to f9 |
8+
| test.cpp:96:3:96:4 | call to f2 | Depending on the order of evaluation for the arguments $@ and $@ for side effects on shared state is unspecified and can result in unexpected behavior. | test.cpp:96:6:96:7 | call to f8 | call to f8 | test.cpp:96:13:96:15 | call to f11 | call to f11 |
59
| test.cpp:99:3:99:4 | call to f2 | Depending on the order of evaluation for the arguments $@ and $@ for side effects on shared state is unspecified and can result in unexpected behavior. | test.cpp:99:9:99:10 | call to m1 | call to m1 | test.cpp:99:18:99:19 | call to m1 | call to m1 |
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//** THIS FILE IS AUTOGENERATED, DO NOT MODIFY DIRECTLY. **/
2+
import cpp
3+
import RuleMetadata
4+
import codingstandards.cpp.exclusions.RuleMetadata
5+
6+
newtype Preprocessor6Query = TFunctionOverFunctionLikeMacroQuery()
7+
8+
predicate isPreprocessor6QueryMetadata(Query query, string queryId, string ruleId) {
9+
query =
10+
// `Query` instance for the `functionOverFunctionLikeMacro` query
11+
Preprocessor6Package::functionOverFunctionLikeMacroQuery() and
12+
queryId =
13+
// `@id` for the `functionOverFunctionLikeMacro` query
14+
"c/misra/function-over-function-like-macro" and
15+
ruleId = "DIR-4-9"
16+
}
17+
18+
module Preprocessor6Package {
19+
Query functionOverFunctionLikeMacroQuery() {
20+
//autogenerate `Query` type
21+
result =
22+
// `Query` type for `functionOverFunctionLikeMacro` query
23+
TQueryC(TPreprocessor6PackageQuery(TFunctionOverFunctionLikeMacroQuery()))
24+
}
25+
}

cpp/common/src/codingstandards/cpp/exclusions/c/RuleMetadata.qll

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import Preprocessor2
3030
import Preprocessor3
3131
import Preprocessor4
3232
import Preprocessor5
33+
import Preprocessor6
3334
import SideEffects1
3435
import SideEffects2
3536
import Strings1
@@ -67,6 +68,7 @@ newtype TCQuery =
6768
TPreprocessor3PackageQuery(Preprocessor3Query q) or
6869
TPreprocessor4PackageQuery(Preprocessor4Query q) or
6970
TPreprocessor5PackageQuery(Preprocessor5Query q) or
71+
TPreprocessor6PackageQuery(Preprocessor6Query q) or
7072
TSideEffects1PackageQuery(SideEffects1Query q) or
7173
TSideEffects2PackageQuery(SideEffects2Query q) or
7274
TStrings1PackageQuery(Strings1Query q) or
@@ -104,6 +106,7 @@ predicate isQueryMetadata(Query query, string queryId, string ruleId) {
104106
isPreprocessor3QueryMetadata(query, queryId, ruleId) or
105107
isPreprocessor4QueryMetadata(query, queryId, ruleId) or
106108
isPreprocessor5QueryMetadata(query, queryId, ruleId) or
109+
isPreprocessor6QueryMetadata(query, queryId, ruleId) or
107110
isSideEffects1QueryMetadata(query, queryId, ruleId) or
108111
isSideEffects2QueryMetadata(query, queryId, ruleId) or
109112
isStrings1QueryMetadata(query, queryId, ruleId) or

cpp/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-cpp-coding-standards
2-
version: 2.11.0-dev
2+
version: 2.12.0-dev
33
libraryPathDependencies: codeql-cpp

cpp/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-cpp-coding-standards-tests
2-
version: 2.11.0-dev
2+
version: 2.12.0-dev
33
libraryPathDependencies: common-cpp-coding-standards
44
extractor: cpp

cpp/misra/src/qlpack.yml

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

cpp/misra/test/qlpack.yml

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

cpp/report/src/qlpack.yml

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

0 commit comments

Comments
 (0)