Skip to content

Commit 1d7f97a

Browse files
authored
Merge branch 'main' into knewbury01/Declarations4
2 parents 6309dbc + 0291b4c commit 1d7f97a

File tree

54 files changed

+501
-43
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+501
-43
lines changed

.github/touch

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

.vscode/tasks.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@
224224
"Iterators",
225225
"Lambdas",
226226
"Language1",
227+
"Language2",
227228
"Literals",
228229
"Loops",
229230
"Macros",
@@ -255,6 +256,7 @@
255256
"Preprocessor3",
256257
"Preprocessor4",
257258
"Preprocessor5",
259+
"Preprocessor6",
258260
"IntegerConversion",
259261
"Expressions",
260262
"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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @id c/misra/usage-of-assembly-language-should-be-documented
3+
* @name DIR-4-2: All usage of assembly language should be documented
4+
* @description Assembly language is not portable and should be documented.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity warning
8+
* @tags external/misra/id/dir-4-2
9+
* maintainability
10+
* readability
11+
* external/misra/obligation/advisory
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.rules.usageofassemblernotdocumented.UsageOfAssemblerNotDocumented
17+
18+
class UsageOfAssemblyLanguageShouldBeDocumentedQuery extends UsageOfAssemblerNotDocumentedSharedQuery {
19+
UsageOfAssemblyLanguageShouldBeDocumentedQuery() {
20+
this = Language2Package::usageOfAssemblyLanguageShouldBeDocumentedQuery()
21+
}
22+
}
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."
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @id c/misra/emergent-language-features-used
3+
* @name RULE-1-4: Emergent language features shall not be used
4+
* @description Emergent language features may have unpredictable behavior and should not be used.
5+
* @kind problem
6+
* @precision very-high
7+
* @problem.severity warning
8+
* @tags external/misra/id/rule-1-4
9+
* maintainability
10+
* readability
11+
* external/misra/obligation/required
12+
*/
13+
14+
import cpp
15+
import codingstandards.c.misra
16+
import codingstandards.cpp.Emergent
17+
18+
from C11::EmergentLanguageFeature ef
19+
where not isExcluded(ef, Language2Package::emergentLanguageFeaturesUsedQuery())
20+
select ef, "Usage of emergent language feature."
21+

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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
cpp/common/test/rules/usageofassemblernotdocumented/UsageOfAssemblerNotDocumented.ql
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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
| test.c:1:1:1:21 | #include <stdalign.h> | Usage of emergent language feature. |
2+
| test.c:2:1:2:22 | #include <stdatomic.h> | Usage of emergent language feature. |
3+
| test.c:3:1:3:24 | #include <stdnoreturn.h> | Usage of emergent language feature. |
4+
| test.c:4:1:4:20 | #include <threads.h> | Usage of emergent language feature. |
5+
| test.c:6:1:6:49 | #define MACRO(x) _Generic((x), int : 0, long : 1) | Usage of emergent language feature. |
6+
| test.c:7:1:7:32 | #define __STDC_WANT_LIB_EXT1__ 1 | Usage of emergent language feature. |
7+
| test.c:9:16:9:17 | f0 | Usage of emergent language feature. |
8+
| test.c:12:26:12:40 | atomic_new_type | Usage of emergent language feature. |
9+
| test.c:17:15:17:15 | i | Usage of emergent language feature. |
10+
| test.c:19:3:19:10 | alignas(...) | Usage of emergent language feature. |
11+
| test.c:20:3:20:9 | alignas(...) | Usage of emergent language feature. |
12+
| test.c:21:11:21:23 | alignof(int) | Usage of emergent language feature. |
13+
| test.c:22:12:22:23 | alignof(int) | Usage of emergent language feature. |
14+
| test.c:24:27:24:28 | i3 | Usage of emergent language feature. |
15+
| test.c:25:28:25:29 | i4 | Usage of emergent language feature. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-1-4/EmergentLanguageFeaturesUsed.ql

c/misra/test/rules/RULE-1-4/test.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdalign.h> //NON_COMPLIANT
2+
#include <stdatomic.h> //NON_COMPLIANT
3+
#include <stdnoreturn.h> //NON_COMPLIANT
4+
#include <threads.h> //NON_COMPLIANT
5+
6+
#define MACRO(x) _Generic((x), int : 0, long : 1) // NON_COMPLIANT
7+
#define __STDC_WANT_LIB_EXT1__ 1 // NON_COMPLIANT
8+
9+
_Noreturn void f0(); // NON_COMPLIANT
10+
11+
typedef int new_type; // COMPLIANT
12+
typedef _Atomic new_type atomic_new_type; // NON_COMPLIANT
13+
14+
void f(int p) {
15+
int i0 = _Generic(p, int : 0, long : 1); // NON_COMPLIANT[FALSE_NEGATIVE]
16+
17+
_Atomic int i; // NON_COMPLIANT
18+
19+
_Alignas(4) int i1; // NON_COMPLIANT
20+
alignas(4) int i2; // NON_COMPLIANT
21+
int a = _Alignof(int); // NON_COMPLIANT
22+
int a1 = alignof(int); // NON_COMPLIANT
23+
24+
static thread_local int i3; // NON_COMPLIANT
25+
static _Thread_local int i4; // NON_COMPLIANT
26+
}
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.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
- `M27-0-1`
2+
- `CstdioTypesUsed.ql` - Exclude `size_t` from this rule, as it can be provided by headers other than `<cstdio>`.
3+
- `CstdioMacrosUsed.ql` - Exclude `NULL` from this rule, as it can be provided by headers other than `<cstdio>`.

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/src/rules/A18-5-8/UnnecessaryUseOfDynamicStorage.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ class MakeSharedOrUnique extends FunctionCall, CandidateFunctionLocalHeapAllocat
6464
* An `AllocationExpr` that allocates heap memory, where the memory is freed on at least one path
6565
* through the enclosing function.
6666
*/
67-
class AllocationExprFunctionLocal extends AllocationExpr, CandidateFunctionLocalHeapAllocationExpr {
67+
class AllocationExprFunctionLocal extends CandidateFunctionLocalHeapAllocationExpr instanceof AllocationExpr {
6868
AllocationExprFunctionLocal() {
6969
this.getSizeBytes() < 1024 and
7070
TaintTracking::localExprTaint(this, any(DeallocationExpr de).getFreedExpr())
7171
}
7272

73-
override int getHeapSizeBytes() { result = this.getSizeBytes() }
73+
override int getHeapSizeBytes() { result = super.getSizeBytes() }
7474

7575
DeallocationExpr getADeallocation() { TaintTracking::localExprTaint(this, result.getFreedExpr()) }
7676

cpp/autosar/src/rules/M27-0-1/CstdioMacrosUsed.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ from MacroInvocation mi
2222
where
2323
not isExcluded(mi, BannedLibrariesPackage::cstdioMacrosUsedQuery()) and
2424
mi.getMacroName() in [
25-
"BUFSIZ", "EOF", "FILENAME_MAX", "FOPEN_MAX", "L_tmpnam", "NULL", "TMP_MAX", "_IOFBF",
26-
"IOLBF", "_IONBF", "SEEK_CUR", "SEEK_END", "SEEK_SET"
25+
"BUFSIZ", "EOF", "FILENAME_MAX", "FOPEN_MAX", "L_tmpnam", "TMP_MAX", "_IOFBF", "IOLBF",
26+
"_IONBF", "SEEK_CUR", "SEEK_END", "SEEK_SET"
2727
]
2828
select mi, "Use of <cstdio> macro '" + mi.getMacroName() + "'."

cpp/autosar/src/rules/M27-0-1/CstdioTypesUsed.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ from TypeMention tm, UserType ut
2222
where
2323
not isExcluded(tm, BannedLibrariesPackage::cstdioTypesUsedQuery()) and
2424
ut = tm.getMentionedType() and
25-
ut.hasGlobalOrStdName(["FILE", "fpos_t", "size_t"])
25+
ut.hasGlobalOrStdName(["FILE", "fpos_t"])
2626
select tm, "Use of <cstdio> type '" + ut.getQualifiedName() + "'."

cpp/autosar/src/rules/M7-4-1/UsageOfAssemblerNotDocumented.ql

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
import cpp
1919
import codingstandards.cpp.autosar
20+
import codingstandards.cpp.rules.usageofassemblernotdocumented.UsageOfAssemblerNotDocumented
2021

21-
from AsmStmt a
22-
where
23-
not isExcluded(a, BannedLibrariesPackage::usageOfAssemblerNotDocumentedQuery()) and
24-
not exists(Comment c | c.getCommentedElement() = a) and
25-
not a.isAffectedByMacro()
26-
select a, "Use of assembler is not documented."
22+
class UsageOfAssemblerNotDocumentedQuery extends UsageOfAssemblerNotDocumentedSharedQuery {
23+
UsageOfAssemblerNotDocumentedQuery() {
24+
this = BannedLibrariesPackage::usageOfAssemblerNotDocumentedQuery()
25+
}
26+
}

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/autosar/test/rules/M27-0-1/CstdioMacrosUsed.expected

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,3 @@
77
| test.cpp:30:29:30:37 | FOPEN_MAX | Use of <cstdio> macro 'FOPEN_MAX'. |
88
| test.cpp:41:14:41:16 | EOF | Use of <cstdio> macro 'EOF'. |
99
| test.cpp:50:24:50:31 | SEEK_SET | Use of <cstdio> macro 'SEEK_SET'. |
10-
| test.cpp:60:10:60:13 | NULL | Use of <cstdio> macro 'NULL'. |
Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
| test.cpp:4:8:4:11 | type mention | Use of <cstdio> type 'std::FILE'. |
22
| test.cpp:6:8:6:13 | type mention | Use of <cstdio> type 'std::fpos_t'. |
3-
| test.cpp:20:18:20:23 | type mention | Use of <cstdio> type 'size_t'. |
4-
| test.cpp:21:18:21:23 | type mention | Use of <cstdio> type 'size_t'. |
53
| test.cpp:34:3:34:6 | type mention | Use of <cstdio> type 'FILE'. |
64
| test.cpp:36:3:36:8 | type mention | Use of <cstdio> type 'fpos_t'. |
7-
| test.cpp:50:14:50:19 | type mention | Use of <cstdio> type 'size_t'. |

cpp/autosar/test/rules/M27-0-1/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ void *test_cstdio_is_used() {
5757
printf("foo"); // NON_COMPLIANT
5858
puts("all done!"); // NON_COMPLIANT
5959

60-
return NULL; // NON_COMPLIANT
60+
return NULL; // COMPLIANT - NULL is not uniquely defined by cstdio
6161
}

cpp/autosar/test/rules/M7-4-1/UsageOfAssemblerNotDocumented.qlref

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

0 commit comments

Comments
 (0)