Skip to content

Commit 793f9a7

Browse files
authored
Merge branch 'main' into lcartey/exceptions-exclude-noexcept
2 parents a85b789 + 4d804eb commit 793f9a7

20 files changed

+77
-85
lines changed
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: 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.

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/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+
};

cpp/autosar/test/rules/M9-3-3/test.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,35 @@ class Z22 : Z1 {
161161
void f2() final {} // COMPLIANT
162162
void f3() { this->a = 100; } // COMPLIANT
163163
};
164+
165+
template <class T> class Array {
166+
public:
167+
T &back();
168+
169+
private:
170+
T data[128];
171+
unsigned int size;
172+
};
173+
174+
template <class T, template <class...> class U> class Stack {
175+
public:
176+
T &Top() {
177+
return this->data.back();
178+
} // COMPLIANT[FALSE_NEGATIVE|TRUE_NEGATIVE] - exception not specified in the
179+
// standard, we opt to not raise an issue because the template can be both
180+
// compliant and non-compliant depending on instantiations.
181+
private:
182+
U<T> data;
183+
};
184+
185+
using IntVectorStack = Stack<int, Array>;
186+
187+
void test_template() {
188+
IntVectorStack s;
189+
190+
int i = s.Top();
191+
}
192+
193+
class Z3 {
194+
void f(int) = delete; // COMPLIANT
195+
};

cpp/common/src/codingstandards/cpp/exclusions/cpp/Const.qll

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import codingstandards.cpp.exclusions.RuleMetadata
55

66
newtype ConstQuery =
77
TRemoveConstOrVolatileQualificationAutosarQuery() or
8-
TDeclarationUnmodifiedParamMissingConstSpecifierQuery() or
98
TDeclarationUnmodifiedObjectMissingConstSpecifierQuery() or
109
TVariableMissingConstexprQuery() or
1110
TFunctionMissingConstexprQuery() or
@@ -28,15 +27,6 @@ predicate isConstQueryMetadata(Query query, string queryId, string ruleId, strin
2827
ruleId = "A5-2-3" and
2928
category = "required"
3029
or
31-
query =
32-
// `Query` instance for the `declarationUnmodifiedParamMissingConstSpecifier` query
33-
ConstPackage::declarationUnmodifiedParamMissingConstSpecifierQuery() and
34-
queryId =
35-
// `@id` for the `declarationUnmodifiedParamMissingConstSpecifier` query
36-
"cpp/autosar/declaration-unmodified-param-missing-const-specifier" and
37-
ruleId = "A7-1-1" and
38-
category = "required"
39-
or
4030
query =
4131
// `Query` instance for the `declarationUnmodifiedObjectMissingConstSpecifier` query
4232
ConstPackage::declarationUnmodifiedObjectMissingConstSpecifierQuery() and
@@ -145,13 +135,6 @@ module ConstPackage {
145135
TQueryCPP(TConstPackageQuery(TRemoveConstOrVolatileQualificationAutosarQuery()))
146136
}
147137

148-
Query declarationUnmodifiedParamMissingConstSpecifierQuery() {
149-
//autogenerate `Query` type
150-
result =
151-
// `Query` type for `declarationUnmodifiedParamMissingConstSpecifier` query
152-
TQueryCPP(TConstPackageQuery(TDeclarationUnmodifiedParamMissingConstSpecifierQuery()))
153-
}
154-
155138
Query declarationUnmodifiedObjectMissingConstSpecifierQuery() {
156139
//autogenerate `Query` type
157140
result =

cpp/common/src/codingstandards/cpp/rules/nonbooleanifstmt/NonBooleanIfStmt.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ query predicate problems(Expr condition, string message) {
1414
not isExcluded(condition, getQuery()) and
1515
exists(IfStmt ifStmt, Type explicitConversionType |
1616
condition = ifStmt.getCondition() and
17+
//exclude any generated conditions
18+
not condition.isCompilerGenerated() and
1719
not ifStmt.isFromUninstantiatedTemplate(_) and
1820
explicitConversionType = condition.getExplicitlyConverted().getUnderlyingType() and
1921
not explicitConversionType instanceof BoolType and

cpp/common/src/codingstandards/cpp/rules/nonbooleaniterationstmt/NonBooleanIterationStmt.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ query predicate problems(Loop loopStmt, string message) {
1616
condition = loopStmt.getCondition() and
1717
explicitConversionType = condition.getExplicitlyConverted().getType().getUnspecifiedType() and
1818
not explicitConversionType instanceof BoolType and
19+
//exclude any generated conditions
20+
not condition.isCompilerGenerated() and
1921
message = "Iteration condition has non boolean type " + explicitConversionType + "."
2022
)
2123
}

rule_packages/cpp/Const.json

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,6 @@
3333
"obligation": "required"
3434
},
3535
"queries": [
36-
{
37-
"description": "`Constexpr`/`const` specifiers prevent unintentional data modification for parameters intended as immutable.",
38-
"kind": "problem",
39-
"name": "Constexpr or const specifiers shall be used for immutable parameter usage",
40-
"precision": "high",
41-
"severity": "warning",
42-
"short_name": "DeclarationUnmodifiedParamMissingConstSpecifier",
43-
"tags": [
44-
"correctness",
45-
"maintainability",
46-
"readability"
47-
]
48-
},
4936
{
5037
"description": "`Constexpr`/`const` specifiers prevent unintentional data modification for data intended as immutable.",
5138
"kind": "problem",
@@ -57,7 +44,10 @@
5744
"correctness",
5845
"maintainability",
5946
"readability"
60-
]
47+
],
48+
"implementation_scope": {
49+
"description": "We exclude function parameters from this rule in line with the rule intention as described in the C++ Core Guidelines Con.1 which excludes function parameters."
50+
}
6151
}
6252
],
6353
"title": "Constexpr or const specifiers shall be used for immutable data declaration."

0 commit comments

Comments
 (0)