Skip to content

Commit e826932

Browse files
authored
Merge branch 'main' into rvermeulen/fix-issue-#311
2 parents 2cbb152 + 4d804eb commit e826932

File tree

42 files changed

+246
-100
lines changed

Some content is hidden

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

42 files changed

+246
-100
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// Reuse the `IntegerLiteral` class
2+
import codingstandards.cpp.Cpp14Literal
3+
4+
class IntegerLiteral = Cpp14Literal::IntegerLiteral;

c/misra/src/rules/RULE-7-3/LowercaseCharacterLUsedInLiteralSuffix.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
import cpp
1616
import codingstandards.c.misra
17+
import codingstandards.c.Literals
1718

18-
from Literal l
19+
from IntegerLiteral l
1920
where
2021
not isExcluded(l, SyntaxPackage::lowercaseCharacterLUsedInLiteralSuffixQuery()) and
21-
not l instanceof StringLiteral and
2222
exists(l.getValueText().indexOf("l"))
2323
select l, "Lowercase 'l' used as a literal suffix."

c/misra/test/rules/RULE-7-3/cpp/LowercaseCharacterLUsedInLiteralSuffix.expected

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/RULE-7-3/LowercaseCharacterLUsedInLiteralSuffix.ql
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This test case was added to validate FP report [#319](https://github.com/github/codeql-coding-standards/issues/319) that occurs when this rule is run on a translation unit with language mode c++.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
semmle-extractor-options:--clang -std=c++14 --edg --diag_error=implicit_func_decl -nostdinc -I../../../../../cpp/common/test/includes/standard-library
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
int x = false; // COMPLIANT - reported as FP in #319

c/misra/test/rules/RULE-7-3/test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,4 @@ long d9 = 001LU; // COMPLIANT
4141

4242
char *e1 = "";
4343
char *e2 = "ul";
44-
char *e3 = "UL";
44+
char *e3 = "UL";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* `A7-1-1` - no longer report parameters as contravening this rule. This is inline with the rule intent as described in the referenced C++ Core Guidelines rule [CON.1](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#con1-by-default-make-objects-immutable), which states "To avoid confusion and lots of false positives, don’t enforce this rule for function parameters."
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- `A3-9-1` - `VariableWidthIntegerTypesUsed.ql`:
2+
- Exclude the plain char type. Still includes `signed char` and `unsigned char`.
3+
- Include CV-qualified variable width integer types.
4+
- `A3-9-1` - `VariableWidthPlainCharTypeUsed.ql`:
5+
- New query to support fine grained deviation support for the plain char type.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`RULE-7-3`: `c/misra/lowercase-character-l-used-in-literal-suffix`
2+
- Exclude non integer literals. This removes a false positive triggered when analyzing C++ code containing the `false` literal.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`M9-3-3` - `MemberFunctionConstIfPossible.ql`, `MemberFunctionStaticIfPossible.ql`:
2+
- Fixes #413. Exclude deleted member functions.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
`A8-4-7` - `InParametersForCheapToCopyTypesNotPassedByValue.ql`, `InParametersForNotCheapToCopyTypesNotPassedByReference.ql`:
2+
- Fixes #397. Exclude user defined operators and move constructors.`
3+
- Exclude parameters for instantiated templates because the declaration location of the function does not contain enough information about the type used in the instantiation to make an actionable alert.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`A5-0-2` - `NonBooleanIfStmt.qll`, `NonBooleanIterationStmt.qll`:
2+
- Exclude compiler generated conditions.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`M9-3-3`: `MemberFunctionConstIfPossible.ql`:
2+
- Fix FP reported in 467. Excluding candidates in uninstantiated templates.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`A7-1-1` - `DeclarationUnmodifiedObjectMissingConstSpecifier.ql`
2+
- Fix FP reported in #372. Exclude compiler generated variables.

cpp/autosar/src/rules/A3-9-1/VariableWidthIntegerTypesUsed.ql

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
/**
22
* @id cpp/autosar/variable-width-integer-types-used
33
* @name A3-9-1: Use fixed-width integer types instead of basic, variable-width, integer types
4-
* @description The basic numerical types of char, int, short, long are not supposed to be used. The
5-
* specific-length types from <cstdint> header need be used instead.
4+
* @description The basic numerical types of signed/unsigned char, int, short, long are not supposed
5+
* to be used. The specific-length types from <cstdint> header need be used instead.
66
* @kind problem
77
* @precision very-high
88
* @problem.severity error
@@ -19,15 +19,16 @@ import cpp
1919
import codingstandards.cpp.autosar
2020
import codingstandards.cpp.EncapsulatingFunctions
2121
import codingstandards.cpp.BuiltInNumericTypes
22+
import codingstandards.cpp.Type
2223

23-
from Variable v
24+
from Variable v, Type typeStrippedOfSpecifiers
2425
where
2526
not isExcluded(v, DeclarationsPackage::variableWidthIntegerTypesUsedQuery()) and
27+
typeStrippedOfSpecifiers = stripSpecifiers(v.getType()) and
2628
(
27-
v.getType() instanceof BuiltInIntegerType or
28-
v.getType() instanceof PlainCharType or
29-
v.getType() instanceof UnsignedCharType or
30-
v.getType() instanceof SignedCharType
29+
typeStrippedOfSpecifiers instanceof BuiltInIntegerType or
30+
typeStrippedOfSpecifiers instanceof UnsignedCharType or
31+
typeStrippedOfSpecifiers instanceof SignedCharType
3132
) and
3233
not v instanceof ExcludedVariable
3334
select v, "Variable '" + v.getName() + "' has variable-width type."
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* @id cpp/autosar/variable-width-plain-char-type-used
3+
* @name A3-9-1: Use a fixed-width integer type instead of a char type
4+
* @description The basic numerical type char is not supposed to be used. The specific-length types
5+
* from <cstdint> header need be used instead.
6+
* @kind problem
7+
* @precision very-high
8+
* @problem.severity error
9+
* @tags external/autosar/id/a3-9-1
10+
* correctness
11+
* security
12+
* maintainability
13+
* external/autosar/allocated-target/implementation
14+
* external/autosar/enforcement/automated
15+
* external/autosar/obligation/required
16+
*/
17+
18+
import cpp
19+
import codingstandards.cpp.autosar
20+
import codingstandards.cpp.Type
21+
22+
from Variable variable
23+
where
24+
not isExcluded(variable, DeclarationsPackage::variableWidthPlainCharTypeUsedQuery()) and
25+
stripSpecifiers(variable.getType()) instanceof PlainCharType
26+
select variable, "Variable '" + variable.getName() + "' has variable-width char type."

cpp/autosar/src/rules/A7-1-1/DeclarationUnmodifiedObjectMissingConstSpecifier.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,6 @@ where
3636
else cond = " is used for an object"
3737
) and
3838
not exists(LambdaExpression lc | lc.getACapture().getField() = v) and
39-
not v.isFromUninstantiatedTemplate(_)
39+
not v.isFromUninstantiatedTemplate(_) and
40+
not v.isCompilerGenerated()
4041
select v, "Non-constant variable " + v.getName() + cond + " and is not modified."

cpp/autosar/src/rules/A7-1-1/DeclarationUnmodifiedParamMissingConstSpecifier.ql

Lines changed: 0 additions & 42 deletions
This file was deleted.

cpp/autosar/src/rules/A8-4-7/InParametersForCheapToCopyTypesNotPassedByValue.ql

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import cpp
1717
import codingstandards.cpp.autosar
1818
import TriviallySmallType
1919
import codingstandards.cpp.CommonTypes as CommonTypes
20+
import codingstandards.cpp.Class
2021

2122
/*
2223
* For the purposes of this rule, "cheap to copy" is defined as a trivially copyable type that is no
@@ -34,8 +35,10 @@ where
3435
) and
3536
t.isConst() and
3637
not exists(CatchBlock cb | cb.getParameter() = v) and
37-
not exists(CopyConstructor cc | cc.getAParameter() = v) and
38-
not v.isFromUninstantiatedTemplate(_)
38+
not exists(SpecialMemberFunction cc | cc.getAParameter() = v) and
39+
not exists(Operator op | op.getAParameter() = v) and
40+
not v.isFromUninstantiatedTemplate(_) and
41+
not v.isFromTemplateInstantiation(_)
3942
select v,
40-
"Parameter " + v.getName() + " is the trivially copyable type " + t.getName() +
41-
" but it is passed by reference instead of by value."
43+
"Parameter '" + v.getName() + "' is the trivially copyable type '" + t.getName() +
44+
"' but it is passed by reference instead of by value."

cpp/autosar/src/rules/A8-4-7/InParametersForNotCheapToCopyTypesNotPassedByReference.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ where
3131
not v.getType() instanceof TriviallySmallType and
3232
not v.getType().getUnderlyingType() instanceof ReferenceType and
3333
not exists(CatchBlock cb | cb.getParameter() = v) and
34-
not v.isFromUninstantiatedTemplate(_)
34+
not v.isFromUninstantiatedTemplate(_) and
35+
not v.isFromTemplateInstantiation(_)
3536
select v,
3637
"Parameter " + v.getName() +
3738
" is the trivially non-copyable type $@ but it is passed by value instead of by reference.",

cpp/autosar/src/rules/M9-3-3/MemberFunctionConstIfPossible.ql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ class ConstMemberFunctionCandidate extends NonConstMemberFunction {
5454
not this instanceof Destructor and
5555
not this instanceof Operator and
5656
//less interested in MemberFunctions with no definition
57-
this.hasDefinition()
57+
this.hasDefinition() and
58+
// For uninstantiated templates we have only partial information that prevents us from determining
59+
// if the candidate calls non-const functions. Therefore we exclude these.
60+
not this.isFromUninstantiatedTemplate(_)
5861
}
5962

6063
/**
@@ -121,5 +124,6 @@ where
121124
not f.callsNonConstOwnMember() and
122125
not f.callsNonConstFromMemberVariable() and
123126
not f.isOverride() and
124-
not f.isFinal()
127+
not f.isFinal() and
128+
not f.isDeleted()
125129
select f, "Member function can be declared as const."

cpp/autosar/src/rules/M9-3-3/MemberFunctionStaticIfPossible.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ from NonStaticMemberFunction nonstatic
3939
where
4040
not isExcluded(nonstatic, ConstPackage::memberFunctionStaticIfPossibleQuery()) and
4141
not exists(ThisExpr t | t.getEnclosingFunction() = nonstatic) and
42-
not nonstatic.isVirtual()
42+
not nonstatic.isVirtual() and
43+
not nonstatic.isDeleted()
4344
select nonstatic, "Member function can be declared as static."

cpp/autosar/test/rules/A3-9-1/VariableWidthIntegerTypesUsed.expected

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
| test.cpp:4:8:4:8 | c | Variable 'c' has variable-width type. |
21
| test.cpp:5:17:5:18 | uc | Variable 'uc' has variable-width type. |
32
| test.cpp:6:15:6:16 | sc | Variable 'sc' has variable-width type. |
43
| test.cpp:8:7:8:7 | i | Variable 'i' has variable-width type. |
@@ -12,3 +11,29 @@
1211
| test.cpp:18:8:18:8 | l | Variable 'l' has variable-width type. |
1312
| test.cpp:19:17:19:18 | ul | Variable 'ul' has variable-width type. |
1413
| test.cpp:20:15:20:16 | sl | Variable 'sl' has variable-width type. |
14+
| test.cpp:39:23:39:25 | uc1 | Variable 'uc1' has variable-width type. |
15+
| test.cpp:40:21:40:23 | sc1 | Variable 'sc1' has variable-width type. |
16+
| test.cpp:42:13:42:14 | i1 | Variable 'i1' has variable-width type. |
17+
| test.cpp:43:22:43:24 | ui1 | Variable 'ui1' has variable-width type. |
18+
| test.cpp:44:18:44:19 | u1 | Variable 'u1' has variable-width type. |
19+
| test.cpp:45:20:45:22 | si1 | Variable 'si1' has variable-width type. |
20+
| test.cpp:46:16:46:17 | s1 | Variable 's1' has variable-width type. |
21+
| test.cpp:48:15:48:17 | sh1 | Variable 'sh1' has variable-width type. |
22+
| test.cpp:49:24:49:27 | ush1 | Variable 'ush1' has variable-width type. |
23+
| test.cpp:50:22:50:25 | ssh1 | Variable 'ssh1' has variable-width type. |
24+
| test.cpp:52:14:52:15 | l1 | Variable 'l1' has variable-width type. |
25+
| test.cpp:53:23:53:25 | ul1 | Variable 'ul1' has variable-width type. |
26+
| test.cpp:54:21:54:23 | sl1 | Variable 'sl1' has variable-width type. |
27+
| test.cpp:57:26:57:28 | uc2 | Variable 'uc2' has variable-width type. |
28+
| test.cpp:58:24:58:26 | sc2 | Variable 'sc2' has variable-width type. |
29+
| test.cpp:60:16:60:17 | i2 | Variable 'i2' has variable-width type. |
30+
| test.cpp:61:25:61:27 | ui2 | Variable 'ui2' has variable-width type. |
31+
| test.cpp:62:21:62:22 | u2 | Variable 'u2' has variable-width type. |
32+
| test.cpp:63:23:63:25 | si2 | Variable 'si2' has variable-width type. |
33+
| test.cpp:64:19:64:20 | s2 | Variable 's2' has variable-width type. |
34+
| test.cpp:66:18:66:20 | sh2 | Variable 'sh2' has variable-width type. |
35+
| test.cpp:67:27:67:30 | ush2 | Variable 'ush2' has variable-width type. |
36+
| test.cpp:68:25:68:28 | ssh2 | Variable 'ssh2' has variable-width type. |
37+
| test.cpp:70:17:70:18 | l2 | Variable 'l2' has variable-width type. |
38+
| test.cpp:71:26:71:28 | ul2 | Variable 'ul2' has variable-width type. |
39+
| test.cpp:72:24:72:26 | sl2 | Variable 'sl2' has variable-width type. |
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.cpp:4:8:4:8 | c | Variable 'c' has variable-width char type. |
2+
| test.cpp:38:14:38:15 | c1 | Variable 'c1' has variable-width char type. |
3+
| test.cpp:56:17:56:18 | c2 | Variable 'c2' has variable-width char type. |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
rules/A3-9-1/VariableWidthPlainCharTypeUsed.ql

cpp/autosar/test/rules/A3-9-1/test.cpp

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
void test_variable_width_type_variables() {
44
char c; // NON_COMPLIANT
55
unsigned char uc; // NON_COMPLIANT
6-
signed char sc; // NON_COMPLIANt
6+
signed char sc; // NON_COMPLIANT
77

88
int i; // NON_COMPLIANT
99
unsigned int ui; // NON_COMPLIANT
@@ -32,4 +32,42 @@ void test_variable_width_type_variables() {
3232

3333
int main(int argc, char *argv[]) { // COMPLIANT
3434
// main as an exception
35+
}
36+
37+
void test_variable_width_type_qualified_variables() {
38+
const char c1 = 0; // NON_COMPLIANT
39+
const unsigned char uc1 = 0; // NON_COMPLIANT
40+
const signed char sc1 = 0; // NON_COMPLIANt
41+
42+
const int i1 = 0; // NON_COMPLIANT
43+
const unsigned int ui1 = 0; // NON_COMPLIANT
44+
const unsigned u1 = 0; // NON_COMPLIANT
45+
const signed int si1 = 0; // NON_COMPLIANT
46+
const signed s1 = 0; // NON_COMPLIANT
47+
48+
const short sh1 = 0; // NON_COMPLIANT
49+
const unsigned short ush1 = 0; // NON_COMPLIANT
50+
const signed short ssh1 = 0; // NON_COMPLIANT
51+
52+
const long l1 = 0; // NON_COMPLIANT
53+
const unsigned long ul1 = 0; // NON_COMPLIANT
54+
const signed long sl1 = 0; // NON_COMPLIANT
55+
56+
volatile char c2; // NON_COMPLIANT
57+
volatile unsigned char uc2; // NON_COMPLIANT
58+
volatile signed char sc2; // NON_COMPLIANt
59+
60+
volatile int i2; // NON_COMPLIANT
61+
volatile unsigned int ui2; // NON_COMPLIANT
62+
volatile unsigned u2; // NON_COMPLIANT
63+
volatile signed int si2; // NON_COMPLIANT
64+
volatile signed s2; // NON_COMPLIANT
65+
66+
volatile short sh2; // NON_COMPLIANT
67+
volatile unsigned short ush2; // NON_COMPLIANT
68+
volatile signed short ssh2; // NON_COMPLIANT
69+
70+
volatile long l2; // NON_COMPLIANT
71+
volatile unsigned long ul2; // NON_COMPLIANT
72+
volatile signed long sl2; // NON_COMPLIANT
3573
}

cpp/autosar/test/rules/A7-1-1/DeclarationUnmodifiedParamMissingConstSpecifier.expected

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

cpp/autosar/test/rules/A7-1-1/DeclarationUnmodifiedParamMissingConstSpecifier.qlref

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

cpp/autosar/test/rules/A7-1-1/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
void f1(int *p) { // COMPLIANT
55
*p += 2;
66
}
7-
void f2(int *p) { // NON_COMPLIANT
7+
void f2(int *p) { // COMPLIANT - we ignore parameters for this rule
88
int l4 = 1; // NON_COMPLIANT
99
int *p1 = p; // NON_COMPLIANT
1010
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
| test.cpp:20:19:20:21 | f5a | Parameter f5a is the trivially copyable type const S1 but it is passed by reference instead of by value. |
1+
| test.cpp:20:19:20:21 | f5a | Parameter 'f5a' is the trivially copyable type 'const S1' but it is passed by reference instead of by value. |

cpp/autosar/test/rules/A8-4-7/test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,12 @@ inline S1 Value(size_t n, const char *data) {} // COMPLIANT
3737
struct A {
3838
int n;
3939
A(const A &a) : n(a.n) {} // COMPLIANT user-defined copy ctor
40+
A(const A &&other_a); // COMPLIANT user-defined move ctor
4041
};
42+
43+
class C1 {};
44+
45+
class C2 : public C1 {
46+
public:
47+
C2 &operator=(const C2 &); // COMPLIANT
48+
};

0 commit comments

Comments
 (0)