Skip to content

Commit 34fa59e

Browse files
authored
Merge branch 'main' into jsinglet/1224-1150-1223-1151
2 parents 7a7ff31 + 5a13000 commit 34fa59e

14 files changed

+131
-12
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `A5-2-2`
2+
- `CStyleCasts.ql` - exclude template parameters to avoid false positives when using the "functional notation" syntax. In addition, provide a greater explanation on limitations of this query.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- `A13-2-2` - `BinaryOperatorAndBitwiseOperatorReturnAPrvalue.ql`
2+
- The formatting of the query output message has been changed and operators are now displayed starting with the return type instead of ending with it.

cpp/autosar/src/rules/A12-1-1/ExplicitConstructorBaseClassInitialization.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ where
3535
not init.isCompilerGenerated()
3636
) and
3737
// Must be a defined constructor
38-
c.isDefined() and
38+
c.hasDefinition() and
3939
// Not a compiler-generated constructor
4040
not c.isCompilerGenerated() and
4141
// Not a defaulted constructor

cpp/autosar/src/rules/A13-2-2/BinaryOperatorAndBitwiseOperatorReturnAPrvalue.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import cpp
1818
import codingstandards.cpp.autosar
1919
import codingstandards.cpp.Operator
20+
import semmle.code.cpp.Print
2021

2122
from Operator o
2223
where
@@ -30,5 +31,5 @@ where
3031
o.getType() instanceof ReferenceType
3132
)
3233
select o,
33-
"User-defined bitwise or arithmetic operator " + o.getFullSignature() +
34+
"User-defined bitwise or arithmetic operator " + getIdentityString(o) +
3435
" does not return a prvalue."

cpp/autosar/src/rules/A2-10-4/IdentifierNameOfStaticFunctionReusedInNamespace.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import codingstandards.cpp.autosar
1717

1818
class CandidateFunction extends Function {
1919
CandidateFunction() {
20-
isDefined() and
20+
hasDefinition() and
2121
isStatic() and
2222
not isMember() and
2323
not (

cpp/autosar/src/rules/A3-1-1/ViolationsOfOneDefinitionRule.ql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ where
6565
or
6666
//an non-const object defined in a header
6767
exists(GlobalOrNamespaceVariable object |
68-
object.isDefined() and
68+
object.hasDefinition() and
6969
not (
7070
object.isConstexpr()
7171
or

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,43 @@ class LibraryMacro extends Macro {
4444
LibraryMacro() { not this instanceof UserProvidedMacro }
4545
}
4646

47+
/*
48+
* In theory this query should exclude casts using the "functional notation" syntax, e.g.
49+
* ```
50+
* int(x);
51+
* ```
52+
* This is because this is not a C-style cast, as it is not legitimate C code. However, our database
53+
* schema does not distinguish between C-style casts and functional casts, so we cannot exclude just
54+
* those.
55+
*
56+
* In addition, we do not get `CStyleCasts` in cases where the cast is converted to a `ConstructorCall`.
57+
* This holds for both the "functional notation" syntax and the "c-style" syntax, e.g. both of these
58+
* are represented in our model as `ConstructorCall`s only:
59+
* ```
60+
* class A { public: A(int); };
61+
* void create() {
62+
* (A)1;
63+
* A(1);
64+
* }
65+
* ```
66+
*
67+
* As a consequence this query:
68+
* - Produces false positives when primitive types are cast using the "functional notation" syntax.
69+
* - Produces false negatives when a C-style cast is converted to a `ConstructorCall` e.g. when the
70+
* argument type is compatible with a single-argument constructor.
71+
*/
72+
4773
from CStyleCast c, string extraMessage, Locatable l, string supplementary
4874
where
4975
not isExcluded(c, BannedSyntaxPackage::traditionalCStyleCastsUsedQuery()) and
5076
not c.isImplicit() and
5177
not c.getType() instanceof UnknownType and
78+
// For casts in templates that occur on types related to a template parameter, the copy of th
79+
// cast in the uninstantiated template is represented as a `CStyleCast` even if in practice all
80+
// the instantiations represent it as a `ConstructorCall`. To avoid the common false positive case
81+
// of using the functional cast notation to call a constructor we exclude all `CStyleCast`s on
82+
// uninstantiated templates, and instead rely on reporting results within instantiations.
83+
not c.isFromUninstantiatedTemplate(_) and
5284
// Exclude casts created from macro invocations of macros defined by third parties
5385
not getGeneratedFrom(c) instanceof LibraryMacro and
5486
// If the cast was generated from a user-provided macro, then report the macro that generated the
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
| test.cpp:16:9:16:17 | operator- | User-defined bitwise or arithmetic operator operator-(const A &, int) -> const A does not return a prvalue. |
2-
| test.cpp:20:4:20:12 | operator\| | User-defined bitwise or arithmetic operator operator\|(const A &, const A &) -> A * does not return a prvalue. |
3-
| test.cpp:24:9:24:18 | operator<< | User-defined bitwise or arithmetic operator operator<<(const A &, const A &) -> const A does not return a prvalue. |
4-
| test.cpp:34:6:34:14 | operator+ | User-defined bitwise or arithmetic operator NS_C::operator+(const C &, const C &) -> int & does not return a prvalue. |
1+
| test.cpp:16:9:16:17 | operator- | User-defined bitwise or arithmetic operator A const operator-(A const&, int) does not return a prvalue. |
2+
| test.cpp:20:4:20:12 | operator\| | User-defined bitwise or arithmetic operator A* operator\|(A const&, A const&) does not return a prvalue. |
3+
| test.cpp:24:9:24:18 | operator<< | User-defined bitwise or arithmetic operator A const operator<<(A const&, A const&) does not return a prvalue. |
4+
| test.cpp:34:6:34:14 | operator+ | User-defined bitwise or arithmetic operator int& NS_C::operator+(C const&, C const&) does not return a prvalue. |

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
| 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 |
66
| test.cpp:85:19:85:26 | (int)... | Use of explicit c-style cast to int. | test.cpp:85:19:85:26 | (int)... | |
77
| test.cpp:86:27:86:34 | (int)... | Use of explicit c-style cast to int. | test.cpp:86:27:86:34 | (int)... | |
8+
| test.cpp:114:10:114:13 | (int)... | Use of explicit c-style cast to int. | test.cpp:114:10:114:13 | (int)... | |
9+
| test.cpp:149:12:149:26 | (unsigned int)... | Use of explicit c-style cast to unsigned int. | test.cpp:149:12:149:26 | (unsigned int)... | |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
-I../../../../common/test/includes/custom-library

0 commit comments

Comments
 (0)