Skip to content

Commit 5a6d7ca

Browse files
committed
Deviations: Support attribute inheritence
Attributes are inherited from their parent. Includes support for features which are not currently enabled, due to lack of support in CodeQL itself.
1 parent bf62d9a commit 5a6d7ca

File tree

5 files changed

+57
-7
lines changed

5 files changed

+57
-7
lines changed

cpp/common/src/codingstandards/cpp/deviations/CodeIdentifierDeviation.qll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,21 @@ class DeviationAttribute extends StdAttribute {
231231
result.(Stmt).getEnclosingFunction() = this.getASuppressedElement()
232232
or
233233
result.(LocalVariable) = this.getASuppressedElement().(DeclStmt).getADeclaration()
234+
or
235+
result.(Function).getDeclaringType() = this.getASuppressedElement()
236+
or
237+
result.(Variable).getDeclaringType() = this.getASuppressedElement()
238+
or
239+
exists(LambdaExpression expr |
240+
expr = this.getASuppressedElement() and
241+
result = expr.getLambdaFunction()
242+
)
243+
or
244+
exists(Function f |
245+
f = this.getASuppressedElement() and
246+
// A suppression on the function should apply to the noexcept expression
247+
result = f.getADeclarationEntry().getNoExceptExpr()
248+
)
234249
}
235250
}
236251

cpp/common/test/deviations/deviations_basic_test/TypeLongDoubleUsed.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
| attribute_syntax.cpp:21:15:21:17 | d10 | Use of long double type. |
33
| attribute_syntax.cpp:29:15:29:17 | d14 | Use of long double type. |
44
| attribute_syntax.cpp:33:20:33:22 | d16 | Use of long double type. |
5+
| attribute_syntax.cpp:55:15:55:16 | d1 | Use of long double type. |
6+
| attribute_syntax.cpp:57:17:57:18 | d2 | Use of long double type. |
7+
| attribute_syntax.cpp:60:17:60:18 | d3 | Use of long double type. |
58
| main.cpp:13:15:13:16 | d1 | Use of long double type. |
69
| main.cpp:18:15:18:16 | d4 | Use of long double type. |
710
| main.cpp:21:15:21:16 | d6 | Use of long double type. |

cpp/common/test/deviations/deviations_basic_test/UnusedReturnValue.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
| attribute_syntax.cpp:26:5:26:8 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
66
| attribute_syntax.cpp:30:3:30:6 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
77
| attribute_syntax.cpp:41:3:41:6 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
8+
| attribute_syntax.cpp:49:5:49:8 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
9+
| attribute_syntax.cpp:61:5:61:8 | call to getZ | Return value from call to $@ is unused. | attribute_syntax.cpp:1:5:1:8 | getZ | getZ |
810
| main.cpp:12:3:12:6 | call to getX | Return value from call to $@ is unused. | main.cpp:8:5:8:8 | getX | getX |
911
| main.cpp:25:3:25:6 | call to getX | Return value from call to $@ is unused. | main.cpp:8:5:8:8 | getX | getX |
1012
| main.cpp:27:3:27:6 | call to getX | Return value from call to $@ is unused. | main.cpp:8:5:8:8 | getX | getX |

cpp/common/test/deviations/deviations_basic_test/attribute_syntax.cpp

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,41 @@ int alt() {
3636
}
3737

3838
[[codeql::autosar_deviation("a-0-4-2-deviation")]]
39-
int alt2() {
39+
int test_function_deviation() {
4040
int x = 0; // COMPLIANT[DEVIATED]
4141
getZ(); // NON_COMPLIANT
4242
long double dd1; // COMPLIANT[DEVIATED]
43-
[[codeql::autosar_deviation(
44-
"a-0-4-2-deviation")]] long double dd2; // COMPLIANT[DEVIATED]
45-
}
43+
}
44+
45+
[[codeql::autosar_deviation("a-0-4-2-deviation")]]
46+
void test_lambdas() {
47+
auto l = []() {
48+
long double d4; // COMPLIANT[DEVIATED]
49+
getZ(); // NON_COMPLIANT
50+
};
51+
}
52+
53+
// Attributes are not supported on a class level at the moment
54+
[[codeql::autosar_deviation("a-0-4-2-deviation")]] class ClassA {
55+
long double d1; // COMPLIANT[DEVIATED - false positive]
56+
class ClassNested {
57+
long double d2; // COMPLIANT[DEVIATED - false positive]
58+
};
59+
void test() {
60+
long double d3; // COMPLIANT[DEVIATED - false positive]
61+
getZ(); // NON_COMPLIANT
62+
}
63+
};
64+
65+
// static_assert, templates, noexcept, multiple declarations
66+
67+
// Namespaces not currently supported by attributes
68+
// [[codeql::autosar_deviation("a-0-4-2-deviation")]] namespace NS {
69+
// long double d1; // COMPLIANT[DEVIATED]
70+
// class ClassA {
71+
// long double d1; // COMPLIANT[DEVIATED]
72+
// };
73+
// void test() {
74+
// long double d1; // COMPLIANT[DEVIATED]
75+
// }
76+
// }

docs/user_manual.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,9 +446,8 @@ This attribute may be added to the following program elements:
446446
* Functions
447447
* Statements
448448
* Variables
449-
* Type declarations
450449

451-
Deviation attributes are inherited from parents in the code structure. For example, a deviation attribute applied to a function will apply the deviation to all code within the function. Note: deviations are not inherited by lambda expressions.
450+
Deviation attributes are inherited from parents in the code structure. For example, a deviation attribute applied to a function will apply the deviation to all code within the function.
452451

453452
Multiple code identifiers may be passed in a single attribute to apply multiple deviations, for example:
454453

@@ -507,7 +506,7 @@ Here are some examples, using the deviation record with the `a-0-4-2-deviation`
507506
6 |
508507
7 | // codeql::<standard>_deviation_end(a-0-4-2-deviation)
509508
```
510-
Here, Line 1 will pair with Line 7, and Line 3 will pair with Line 8.
509+
Here, Line 1 will pair with Line 7, and Line 3 will pair with Line 5.
511510

512511
A `codeql::<standard>_deviation_end` without a matching `codeql::<standard>_deviation_begin`, or `codeql::<standard>_deviation_begin` without a matching `codeql::<standard>_deviation_end` is invalid and will be ignored.
513512

0 commit comments

Comments
 (0)