Skip to content

Commit

Permalink
Merge pull request #610 from github/lcartey/a3-1-5
Browse files Browse the repository at this point in the history
A3-1-5: Remove extraneous cases and handle template instantiations
  • Loading branch information
knewbury01 authored Jan 13, 2025
2 parents 0bd67b6 + ae12bd7 commit 53f7c90
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 94 deletions.
4 changes: 4 additions & 0 deletions change_notes/2024-06-03-a3-1-5-trivial-defs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- `A3-1-5` - `TrivialOrTemplateFunctionDefinedOutsideClassDefinition.ql`:
- Query deleted - rule was never intended to cover this case (see https://forum.misra.org.uk/archive/index.php?thread-1588.html).
- `A3-1-5` - `NonTrivialNonTemplateFunctionDefinedInsideClassDefinition.ql`:
- Removed false positives caused by flagging member functions in template instantiations

This file was deleted.

This file was deleted.

This file was deleted.

58 changes: 50 additions & 8 deletions cpp/autosar/test/rules/A3-1-5/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class A {

int complexCalculation();

int gcd(int a, int b) {
int gcd(int a, int b) { // NON_COMPLIANT
if (b == 0)
return a;
int result = gcd(b, (a % b));
Expand Down Expand Up @@ -62,11 +62,11 @@ inline int A::complexCalculation() { // COMPLIANT
return 1;
}

int A::getB() { return 1; } // NON_COMPLIANT
int A::getB() { return 1; } // COMPLIANT

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

int A::b() { return 3; } // NON_COMPLIANT
int A::b() { return 3; } // COMPLIANT

template <typename C> class B {
public:
Expand All @@ -83,9 +83,30 @@ template <typename C> class B {
template <typename T> T d(T t);

int complexCalculation();

int complexCalculation2() { // COMPLIANT - template
;
;
;
;
;
;
;
;
;
;
;
;
return 1;
}
};

template <typename C> inline int B<C>::complexCalculation() { // NON_COMPLIANT
void test_B() {
B<int> b;
b.complexCalculation2();
}

template <typename C> inline int B<C>::complexCalculation() { // COMPLIANT
;
;
;
Expand All @@ -101,16 +122,16 @@ template <typename C> inline int B<C>::complexCalculation() { // NON_COMPLIANT
return 1;
}

template <typename C> template <typename T> T B<C>::d(T t) { // NON_COMPLIANT
template <typename C> template <typename T> T B<C>::d(T t) { // COMPLIANT
return t;
}

template <typename C> int B<C>::b() { // NON_COMPLIANT
template <typename C> int B<C>::b() { // COMPLIANT
C c;
return 3;
}

template <typename C> int B<C>::getB() { return 3; } // NON_COMPLIANT
template <typename C> int B<C>::getB() { return 3; } // COMPLIANT

template <typename T> class Foo {
public:
Expand All @@ -128,8 +149,29 @@ class FooBar {
public:
~FooBar();
int f1(int a, int b);

template <typename C> int complexCalculation() { // COMPLIANT - template
;
;
;
;
;
;
;
;
;
;
;
;
return 1;
}
};

void test_FooBar() {
FooBar foobar;
foobar.complexCalculation<int>();
}

FooBar::~FooBar() {} // COMPLIANT want to ignore pImpl uses of destructors

int FooBar::f1(int a, int b) { // COMPLIANT not a trivial function
Expand Down
8 changes: 7 additions & 1 deletion cpp/common/src/codingstandards/cpp/Class.qll
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,13 @@ class TrivialMemberFunction extends IntrospectedMemberFunction {
* class.
*/
class TemplateOrTemplateClassMemberFunction extends MemberFunction {
TemplateOrTemplateClassMemberFunction() { isFromUninstantiatedTemplate(_) }
TemplateOrTemplateClassMemberFunction() {
(
isFromUninstantiatedTemplate(_) or
isFromTemplateInstantiation(_)
) and
not this.isCompilerGenerated()
}
}

/**
Expand Down
17 changes: 0 additions & 17 deletions cpp/common/src/codingstandards/cpp/exclusions/cpp/Classes.qll
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ newtype ClassesQuery =
TClassDataMembersInitializationConditionQuery() or
TRedundantMemberFunctionsShouldBeDefaultedOrLeftUndefinedQuery() or
TNonTemplateMemberDefinedInTemplateQuery() or
TTrivialOrTemplateFunctionDefinedOutsideClassDefinitionQuery() or
TNonTrivialNonTemplateFunctionDefinedInsideClassDefinitionQuery() or
TInParametersForNotCheapToCopyTypesNotPassedByReferenceQuery() or
TInParametersForCheapToCopyTypesNotPassedByValueQuery() or
Expand Down Expand Up @@ -105,15 +104,6 @@ predicate isClassesQueryMetadata(Query query, string queryId, string ruleId, str
ruleId = "A14-5-2" and
category = "advisory"
or
query =
// `Query` instance for the `trivialOrTemplateFunctionDefinedOutsideClassDefinition` query
ClassesPackage::trivialOrTemplateFunctionDefinedOutsideClassDefinitionQuery() and
queryId =
// `@id` for the `trivialOrTemplateFunctionDefinedOutsideClassDefinition` query
"cpp/autosar/trivial-or-template-function-defined-outside-class-definition" and
ruleId = "A3-1-5" and
category = "required"
or
query =
// `Query` instance for the `nonTrivialNonTemplateFunctionDefinedInsideClassDefinition` query
ClassesPackage::nonTrivialNonTemplateFunctionDefinedInsideClassDefinitionQuery() and
Expand Down Expand Up @@ -251,13 +241,6 @@ module ClassesPackage {
TQueryCPP(TClassesPackageQuery(TNonTemplateMemberDefinedInTemplateQuery()))
}

Query trivialOrTemplateFunctionDefinedOutsideClassDefinitionQuery() {
//autogenerate `Query` type
result =
// `Query` type for `trivialOrTemplateFunctionDefinedOutsideClassDefinition` query
TQueryCPP(TClassesPackageQuery(TTrivialOrTemplateFunctionDefinedOutsideClassDefinitionQuery()))
}

Query nonTrivialNonTemplateFunctionDefinedInsideClassDefinitionQuery() {
//autogenerate `Query` type
result =
Expand Down
9 changes: 0 additions & 9 deletions rule_packages/cpp/Classes.json
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,6 @@
"obligation": "required"
},
"queries": [
{
"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.",
"kind": "problem",
"name": "A function shall be defined with a class body if and only if it is intended to be inlined",
"precision": "very-high",
"severity": "recommendation",
"short_name": "TrivialOrTemplateFunctionDefinedOutsideClassDefinition",
"tags": []
},
{
"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.",
"kind": "problem",
Expand Down

0 comments on commit 53f7c90

Please sign in to comment.