Skip to content

Commit 9cf82af

Browse files
authored
Merge branch 'main' into jsinglet/rule-fix-dir-1-2
2 parents 2dede33 + d0ca048 commit 9cf82af

33 files changed

+142
-39
lines changed

.github/workflows/bump-version.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ on:
1010
jobs:
1111

1212
apply-version-bump:
13-
runs-on: ubuntu-latest
13+
runs-on: ubuntu-22.04
1414
name: Apply Version Bump
1515
steps:
1616
- name: Checkout

.github/workflows/code-scanning-pack-gen.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ env:
1919
jobs:
2020
prepare-code-scanning-pack-matrix:
2121
name: Prepare CodeQL Code Scanning pack matrix
22-
runs-on: ubuntu-latest
22+
runs-on: ubuntu-22.04
2323
outputs:
2424
matrix: ${{ steps.export-code-scanning-pack-matrix.outputs.matrix }}
2525
steps:

.github/workflows/codeql_unit_tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ on:
1414
jobs:
1515
prepare-unit-test-matrix:
1616
name: Prepare CodeQL unit test matrix
17-
runs-on: ubuntu-latest
17+
runs-on: ubuntu-22.04
1818
outputs:
1919
matrix: ${{ steps.export-unit-test-matrix.outputs.matrix }}
2020
steps:
@@ -157,7 +157,7 @@ jobs:
157157
validate-test-results:
158158
name: Validate test results
159159
needs: [run-test-suites]
160-
runs-on: ubuntu-latest
160+
runs-on: ubuntu-22.04
161161
steps:
162162
- name: Collect test results
163163
uses: actions/download-artifact@v2

.github/workflows/create-draft-release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ on:
2121
jobs:
2222
create-draft-release:
2323
name: Create draft release
24-
runs-on: ubuntu-latest
24+
runs-on: ubuntu-22.04
2525
env:
2626
# AWS CONFIGURATION
2727
AWS_EC2_INSTANCE_TYPE: ${{ github.event.inputs.aws_ec2_instance_type }}

.github/workflows/generate-html-docs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ on:
1515
jobs:
1616
generate-html-doc:
1717
name: Generate HTML documentation
18-
runs-on: ubuntu-latest
18+
runs-on: ubuntu-22.04
1919
steps:
2020
- name: Checkout
2121
uses: actions/checkout@v2

.github/workflows/standard_library_upgrade_tests.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ on:
1414
jobs:
1515
prepare-unit-test-matrix:
1616
name: Prepare CodeQL unit test matrix
17-
runs-on: ubuntu-latest
17+
runs-on: ubuntu-22.04
1818
outputs:
1919
matrix: ${{ steps.export-unit-test-matrix.outputs.matrix }}
2020
steps:
@@ -154,7 +154,7 @@ jobs:
154154
validate-test-results:
155155
name: Validate test results
156156
needs: [run-test-suites]
157-
runs-on: ubuntu-latest
157+
runs-on: ubuntu-22.04
158158
steps:
159159
- name: Install Python
160160
uses: actions/setup-python@v4

.github/workflows/upgrade_codeql_dependencies.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
env:
2121
CODEQL_CLI_VERSION: ${{ github.event.inputs.codeql_cli_version }}
2222
CODEQL_LIB_COMMIT: ${{ github.event.inputs.codeql_standard_library_commit }}
23-
runs-on: ubuntu-latest
23+
runs-on: ubuntu-22.04
2424
steps:
2525
- name: Checkout
2626
uses: actions/checkout@v2

.github/workflows/validate-coding-standards.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ env:
1818
jobs:
1919
validate-package-files:
2020
name: Validate Package Files
21-
runs-on: ubuntu-latest
21+
runs-on: ubuntu-22.04
2222
steps:
2323
- name: Checkout
2424
uses: actions/checkout@v2
@@ -63,7 +63,7 @@ jobs:
6363
6464
validate-codeql-format:
6565
name: "Validate CodeQL Format"
66-
runs-on: ubuntu-latest
66+
runs-on: ubuntu-22.04
6767
steps:
6868
- name: Checkout
6969
uses: actions/checkout@v2
@@ -94,7 +94,7 @@ jobs:
9494
9595
validate-query-help-files:
9696
name: Validate Query Help Files
97-
runs-on: ubuntu-latest
97+
runs-on: ubuntu-22.04
9898
steps:
9999
- name: Checkout
100100
uses: actions/checkout@v2
@@ -129,7 +129,7 @@ jobs:
129129
130130
validate-cpp-test-files:
131131
name: Validate C++ Test Files
132-
runs-on: ubuntu-latest
132+
runs-on: ubuntu-22.04
133133
steps:
134134
- name: Checkout
135135
uses: actions/checkout@v2
@@ -152,7 +152,7 @@ jobs:
152152
153153
validate-c-test-files:
154154
name: Validate C Test Files
155-
runs-on: ubuntu-latest
155+
runs-on: ubuntu-22.04
156156
steps:
157157
- name: Checkout
158158
uses: actions/checkout@v2

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.9.0
2+
version: 2.11.0-dev
33
suites: codeql-suites
44
libraryPathDependencies: common-c-coding-standards

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.9.0
2+
version: 2.11.0-dev
33
libraryPathDependencies: cert-c-coding-standards
44
extractor: cpp

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.9.0
2+
version: 2.11.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.9.0
2+
version: 2.11.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.9.0
2+
version: 2.11.0-dev
33
suites: codeql-suites
44
libraryPathDependencies: common-c-coding-standards

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.9.0
2+
version: 2.11.0-dev
33
libraryPathDependencies: misra-c-coding-standards
44
extractor: cpp
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- `A5-2-2` - `TraditionalCStyleCastsUsed.ql`
2+
- Reduced false positives by excluding casts generated by library macros (i.e. macros defined outside the source location)
3+
- Improved the message to cite the macro which generated the c-style cast, if any.
4+
- Improved the message to cite the type being casted to, to aid with identification and remediation.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `M0-1-4` - `SingleUseMemberPODVariable.ql`
2+
- Reduce false positives by excluding any constexpr variable whose constant value is used as an argument to a template.

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

cpp/autosar/src/rules/A5-2-2/TraditionalCStyleCastsUsed.ql

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,55 @@
1616
import cpp
1717
import codingstandards.cpp.autosar
1818

19-
from CStyleCast c
19+
/**
20+
* Gets the macro (if any) that generated the given `CStyleCast`.
21+
*
22+
* If there are nested macro invocations, we identify the most specific macro that generated the
23+
* cast.
24+
*/
25+
Macro getGeneratedFrom(CStyleCast c) {
26+
exists(MacroInvocation mi |
27+
mi = result.getAnInvocation() and
28+
mi.getAGeneratedElement() = c and
29+
mi.getLocation().getStartColumn() = c.getLocation().getStartColumn() and
30+
not exists(MacroInvocation child |
31+
child.getParentInvocation() = mi and
32+
child.getAGeneratedElement() = c
33+
)
34+
)
35+
}
36+
37+
/** A macro within the source location of this project. */
38+
class UserProvidedMacro extends Macro {
39+
UserProvidedMacro() { exists(this.getFile().getRelativePath()) }
40+
}
41+
42+
/** A macro defined within a library used by this project. */
43+
class LibraryMacro extends Macro {
44+
LibraryMacro() { not this instanceof UserProvidedMacro }
45+
}
46+
47+
from CStyleCast c, string extraMessage, Locatable l, string supplementary
2048
where
2149
not isExcluded(c, BannedSyntaxPackage::traditionalCStyleCastsUsedQuery()) and
2250
not c.isImplicit() and
23-
not c.getType() instanceof UnknownType
24-
select c, "Use of explicit C-style Cast"
51+
not c.getType() instanceof UnknownType and
52+
// Exclude casts created from macro invocations of macros defined by third parties
53+
not getGeneratedFrom(c) instanceof LibraryMacro and
54+
// If the cast was generated from a user-provided macro, then report the macro that generated the
55+
// cast, as the macro itself may have generated the cast
56+
if getGeneratedFrom(c) instanceof UserProvidedMacro
57+
then
58+
extraMessage = " generated from macro $@" and
59+
// Add macro as explanatory link
60+
l = getGeneratedFrom(c) and
61+
supplementary = getGeneratedFrom(c).getName()
62+
else (
63+
// No extra message required
64+
extraMessage = "" and
65+
// No explanatory link required, but we still need to set these to valid values
66+
l = c and
67+
supplementary = ""
68+
)
69+
select c, "Use of explicit c-style cast to " + c.getType().getName() + extraMessage + ".", l,
70+
supplementary

cpp/autosar/src/rules/M0-1-4/SingleUsePODVariable.qll

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ import cpp
44
import codingstandards.cpp.TrivialType
55
import codingstandards.cpp.deadcode.UnusedVariables
66

7+
/** Gets the constant value of a constexpr variable. */
8+
private string getConstExprValue(Variable v) {
9+
result = v.getInitializer().getExpr().getValue() and
10+
v.isConstexpr()
11+
}
12+
713
/** Gets a "use" count according to rule M0-1-4. */
814
int getUseCount(Variable v) {
915
exists(int initializers |
@@ -12,7 +18,14 @@ int getUseCount(Variable v) {
1218
result =
1319
initializers +
1420
count(VariableAccess access | access = v.getAnAccess() and not access.isCompilerGenerated())
15-
+ count(UserProvidedConstructorFieldInit cfi | cfi.getTarget() = v)
21+
+ count(UserProvidedConstructorFieldInit cfi | cfi.getTarget() = v) +
22+
// For constexpr variables used as template arguments, we don't see accesses (just the
23+
// appropriate literals). We therefore take a conservative approach and count the number of
24+
// template instantiations that use the given constant, and consider each one to be a use
25+
// of the variable
26+
count(ClassTemplateInstantiation cti |
27+
cti.getTemplateArgument(_).(Expr).getValue() = getConstExprValue(v)
28+
)
1629
)
1730
}
1831

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.9.0
2+
version: 2.11.0-dev
33
libraryPathDependencies: autosar-cpp-coding-standards
44
extractor: cpp
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1-
| test.cpp:8:22:8:37 | (uint32_t)... | Use of explicit C-style Cast |
2-
| test.cpp:9:22:9:32 | (unsigned int)... | Use of explicit C-style Cast |
3-
| test.cpp:15:3:15:13 | (void)... | Use of explicit C-style Cast |
1+
| test.cpp:8:22:8:37 | (uint32_t)... | Use of explicit c-style cast to uint32_t. | test.cpp:8:22:8:37 | (uint32_t)... | |
2+
| test.cpp:9:22:9:32 | (unsigned int)... | Use of explicit c-style cast to unsigned int. | test.cpp:9:22:9:32 | (unsigned int)... | |
3+
| test.cpp:15:3:15:13 | (void)... | Use of explicit c-style cast to void. | test.cpp:15:3:15:13 | (void)... | |
4+
| test.cpp:77:3:77:11 | (int)... | Use of explicit c-style cast to int generated from macro $@. | test.cpp:70:1:70:31 | #define ADD_ONE(x) ((int)x) + 1 | ADD_ONE |
5+
| test.cpp:79:3:79:18 | (int)... | Use of explicit c-style cast to int generated from macro $@. | test.cpp:71:1:71:36 | #define NESTED_ADD_ONE(x) ADD_ONE(x) | NESTED_ADD_ONE |
6+
| test.cpp:85:19:85:26 | (int)... | Use of explicit c-style cast to int. | test.cpp:85:19:85:26 | (int)... | |
7+
| test.cpp:86:27:86:34 | (int)... | Use of explicit c-style cast to int. | test.cpp:86:27:86:34 | (int)... | |

cpp/autosar/test/rules/A5-2-2/test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,25 @@ class A5_2_2 final {
6565
void a5_2_2_test() {
6666
A5_2_2 a;
6767
a.f("");
68+
}
69+
70+
#define ADD_ONE(x) ((int)x) + 1
71+
#define NESTED_ADD_ONE(x) ADD_ONE(x)
72+
#define NO_CAST_ADD_ONE(x) x + 1
73+
74+
#include "macro_c_style_casts.h"
75+
76+
void test_macro_cast() {
77+
ADD_ONE(1); // NON_COMPLIANT - expansion of user-defined macro creates
78+
// c-style cast
79+
NESTED_ADD_ONE(1); // NON_COMPLIANT - expansion of user-defined macro creates
80+
// c-style cast
81+
LIBRARY_ADD_TWO(1); // COMPLIANT - macro generating the cast is defined in a
82+
// library, and is not modifiable by the user
83+
LIBRARY_NESTED_ADD_TWO(1); // COMPLIANT - macro generating the cast is defined
84+
// in a library, and is not modifiable by the user
85+
NO_CAST_ADD_ONE((int)1.0); // NON_COMPLIANT - cast in argument to macro
86+
LIBRARY_NO_CAST_ADD_TWO((int)1.0); // NON_COMPLIANT - library macro with
87+
// c-style cast in argument, written by
88+
// user so should be reported
6889
}

cpp/autosar/test/rules/M0-1-4/SingleUseMemberPODVariable.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
| test.cpp:36:24:36:29 | unused | Member POD variable unused in C1 is only $@. | test.cpp:36:31:36:31 | initializer for unused | used once |
12
| test_global_or_namespace.cpp:16:7:16:7 | x | Member POD variable x in GA is only $@. | test_global_or_namespace.cpp:38:6:38:6 | x | used once |
23
| test_global_or_namespace.cpp:54:7:54:7 | x | Member POD variable x in N1A is only $@. | test_global_or_namespace.cpp:76:6:76:6 | x | used once |
34
| test_member.cpp:5:7:5:8 | m2 | Member POD variable m2 in A is only $@. | test_member.cpp:9:21:9:25 | constructor init of field m2 | used once |

cpp/autosar/test/rules/M0-1-4/test.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/** Test cases for `SingleUseLocalPODVariable.ql` */
2-
2+
#include <array>
33
class A {};
44

55
class B {
@@ -30,4 +30,12 @@ void test_templates() {
3030
f1<B>(); // Triggers a NON_COMPLIANT case in f1(), because B is a POD type
3131
f1<C>(); // Does not trigger a NON_COMPLIANT case in f1(), because C is not a
3232
// POD type
33-
}
33+
}
34+
35+
class C1 {
36+
static constexpr int unused{1}; // NON_COMPLIANT
37+
static constexpr int used{2}; // COMPLIANT
38+
int test_use() { return used; }
39+
static constexpr int size{3}; // COMPLIANT
40+
std::array<bool, size> array{false, false}; // size is used here
41+
};

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

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

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.9.0
2+
version: 2.11.0-dev
33
libraryPathDependencies: codeql-cpp
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Macros used in test for A5-2-2
2+
#define LIBRARY_ADD_TWO(x) ((int)x) + 2
3+
#define LIBRARY_NESTED_ADD_TWO(x) LIBRARY_ADD_TWO(x)
4+
#define LIBRARY_NO_CAST_ADD_TWO(x) x + 1

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.9.0
2+
version: 2.11.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.9.0
2+
version: 2.11.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.9.0
2+
version: 2.11.0-dev
33
libraryPathDependencies: misra-cpp-coding-standards
44
extractor: cpp

cpp/options

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
semmle-extractor-options:--clang -std=c++14 -nostdinc++ -I../../../../common/test/includes/standard-library
1+
semmle-extractor-options:--clang -std=c++14 -nostdinc++ -I../../../../common/test/includes/standard-library -I../../../../common/test/includes/custom-library

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.9.0
2+
version: 2.11.0-dev
33
libraryPathDependencies: codeql-cpp

0 commit comments

Comments
 (0)