Skip to content

Commit 53f7c90

Browse files
authored
Merge pull request #610 from github/lcartey/a3-1-5
A3-1-5: Remove extraneous cases and handle template instantiations
2 parents 0bd67b6 + ae12bd7 commit 53f7c90

File tree

8 files changed

+61
-94
lines changed

8 files changed

+61
-94
lines changed

Diff for: change_notes/2024-06-03-a3-1-5-trivial-defs.md

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
- `A3-1-5` - `TrivialOrTemplateFunctionDefinedOutsideClassDefinition.ql`:
2+
- Query deleted - rule was never intended to cover this case (see https://forum.misra.org.uk/archive/index.php?thread-1588.html).
3+
- `A3-1-5` - `NonTrivialNonTemplateFunctionDefinedInsideClassDefinition.ql`:
4+
- Removed false positives caused by flagging member functions in template instantiations

Diff for: cpp/autosar/src/rules/A3-1-5/TrivialOrTemplateFunctionDefinedOutsideClassDefinition.ql

-51
This file was deleted.

Diff for: cpp/autosar/test/rules/A3-1-5/TrivialOrTemplateFunctionDefinedOutsideClassDefinition.expected

-7
This file was deleted.

Diff for: cpp/autosar/test/rules/A3-1-5/TrivialOrTemplateFunctionDefinedOutsideClassDefinition.qlref

-1
This file was deleted.

Diff for: cpp/autosar/test/rules/A3-1-5/test.cpp

+50-8
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class A {
2323

2424
int complexCalculation();
2525

26-
int gcd(int a, int b) {
26+
int gcd(int a, int b) { // NON_COMPLIANT
2727
if (b == 0)
2828
return a;
2929
int result = gcd(b, (a % b));
@@ -62,11 +62,11 @@ inline int A::complexCalculation() { // COMPLIANT
6262
return 1;
6363
}
6464

65-
int A::getB() { return 1; } // NON_COMPLIANT
65+
int A::getB() { return 1; } // COMPLIANT
6666

67-
template <typename T> T A::d(T t) { return t; } // NON_COMPLIANT
67+
template <typename T> T A::d(T t) { return t; } // COMPLIANT
6868

69-
int A::b() { return 3; } // NON_COMPLIANT
69+
int A::b() { return 3; } // COMPLIANT
7070

7171
template <typename C> class B {
7272
public:
@@ -83,9 +83,30 @@ template <typename C> class B {
8383
template <typename T> T d(T t);
8484

8585
int complexCalculation();
86+
87+
int complexCalculation2() { // COMPLIANT - template
88+
;
89+
;
90+
;
91+
;
92+
;
93+
;
94+
;
95+
;
96+
;
97+
;
98+
;
99+
;
100+
return 1;
101+
}
86102
};
87103

88-
template <typename C> inline int B<C>::complexCalculation() { // NON_COMPLIANT
104+
void test_B() {
105+
B<int> b;
106+
b.complexCalculation2();
107+
}
108+
109+
template <typename C> inline int B<C>::complexCalculation() { // COMPLIANT
89110
;
90111
;
91112
;
@@ -101,16 +122,16 @@ template <typename C> inline int B<C>::complexCalculation() { // NON_COMPLIANT
101122
return 1;
102123
}
103124

104-
template <typename C> template <typename T> T B<C>::d(T t) { // NON_COMPLIANT
125+
template <typename C> template <typename T> T B<C>::d(T t) { // COMPLIANT
105126
return t;
106127
}
107128

108-
template <typename C> int B<C>::b() { // NON_COMPLIANT
129+
template <typename C> int B<C>::b() { // COMPLIANT
109130
C c;
110131
return 3;
111132
}
112133

113-
template <typename C> int B<C>::getB() { return 3; } // NON_COMPLIANT
134+
template <typename C> int B<C>::getB() { return 3; } // COMPLIANT
114135

115136
template <typename T> class Foo {
116137
public:
@@ -128,8 +149,29 @@ class FooBar {
128149
public:
129150
~FooBar();
130151
int f1(int a, int b);
152+
153+
template <typename C> int complexCalculation() { // COMPLIANT - template
154+
;
155+
;
156+
;
157+
;
158+
;
159+
;
160+
;
161+
;
162+
;
163+
;
164+
;
165+
;
166+
return 1;
167+
}
131168
};
132169

170+
void test_FooBar() {
171+
FooBar foobar;
172+
foobar.complexCalculation<int>();
173+
}
174+
133175
FooBar::~FooBar() {} // COMPLIANT want to ignore pImpl uses of destructors
134176

135177
int FooBar::f1(int a, int b) { // COMPLIANT not a trivial function

Diff for: cpp/common/src/codingstandards/cpp/Class.qll

+7-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,13 @@ class TrivialMemberFunction extends IntrospectedMemberFunction {
191191
* class.
192192
*/
193193
class TemplateOrTemplateClassMemberFunction extends MemberFunction {
194-
TemplateOrTemplateClassMemberFunction() { isFromUninstantiatedTemplate(_) }
194+
TemplateOrTemplateClassMemberFunction() {
195+
(
196+
isFromUninstantiatedTemplate(_) or
197+
isFromTemplateInstantiation(_)
198+
) and
199+
not this.isCompilerGenerated()
200+
}
195201
}
196202

197203
/**

Diff for: cpp/common/src/codingstandards/cpp/exclusions/cpp/Classes.qll

-17
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ newtype ClassesQuery =
1313
TClassDataMembersInitializationConditionQuery() or
1414
TRedundantMemberFunctionsShouldBeDefaultedOrLeftUndefinedQuery() or
1515
TNonTemplateMemberDefinedInTemplateQuery() or
16-
TTrivialOrTemplateFunctionDefinedOutsideClassDefinitionQuery() or
1716
TNonTrivialNonTemplateFunctionDefinedInsideClassDefinitionQuery() or
1817
TInParametersForNotCheapToCopyTypesNotPassedByReferenceQuery() or
1918
TInParametersForCheapToCopyTypesNotPassedByValueQuery() or
@@ -105,15 +104,6 @@ predicate isClassesQueryMetadata(Query query, string queryId, string ruleId, str
105104
ruleId = "A14-5-2" and
106105
category = "advisory"
107106
or
108-
query =
109-
// `Query` instance for the `trivialOrTemplateFunctionDefinedOutsideClassDefinition` query
110-
ClassesPackage::trivialOrTemplateFunctionDefinedOutsideClassDefinitionQuery() and
111-
queryId =
112-
// `@id` for the `trivialOrTemplateFunctionDefinedOutsideClassDefinition` query
113-
"cpp/autosar/trivial-or-template-function-defined-outside-class-definition" and
114-
ruleId = "A3-1-5" and
115-
category = "required"
116-
or
117107
query =
118108
// `Query` instance for the `nonTrivialNonTemplateFunctionDefinedInsideClassDefinition` query
119109
ClassesPackage::nonTrivialNonTemplateFunctionDefinedInsideClassDefinitionQuery() and
@@ -251,13 +241,6 @@ module ClassesPackage {
251241
TQueryCPP(TClassesPackageQuery(TNonTemplateMemberDefinedInTemplateQuery()))
252242
}
253243

254-
Query trivialOrTemplateFunctionDefinedOutsideClassDefinitionQuery() {
255-
//autogenerate `Query` type
256-
result =
257-
// `Query` type for `trivialOrTemplateFunctionDefinedOutsideClassDefinition` query
258-
TQueryCPP(TClassesPackageQuery(TTrivialOrTemplateFunctionDefinedOutsideClassDefinitionQuery()))
259-
}
260-
261244
Query nonTrivialNonTemplateFunctionDefinedInsideClassDefinitionQuery() {
262245
//autogenerate `Query` type
263246
result =

Diff for: rule_packages/cpp/Classes.json

-9
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,6 @@
178178
"obligation": "required"
179179
},
180180
"queries": [
181-
{
182-
"description": "A function that is either trivial, a template function, or a member of a template class may not be defined outside of a class body.",
183-
"kind": "problem",
184-
"name": "A function shall be defined with a class body if and only if it is intended to be inlined",
185-
"precision": "very-high",
186-
"severity": "recommendation",
187-
"short_name": "TrivialOrTemplateFunctionDefinedOutsideClassDefinition",
188-
"tags": []
189-
},
190181
{
191182
"description": "A function that is not either trivial, a template function, or a member of a template class may not be defined within a class body.",
192183
"kind": "problem",

0 commit comments

Comments
 (0)