Skip to content

Commit 6266962

Browse files
committed
Add support for templates to base class detection
Detect template base classes through class template instantiations.
1 parent 17d73e4 commit 6266962

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

cpp/autosar/test/rules/A12-8-6/CopyAndMoveNotDeclaredProtected.expected

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,11 @@
1212
| test.cpp:76:3:76:12 | declaration of BaseClass6 | Move constructor for base class BaseClass6 (the class is abstract) is not declared protected or deleted. |
1313
| test.cpp:77:15:77:23 | declaration of operator= | Copy assignment operator for base class BaseClass6 (the class is abstract) is not declared protected or deleted. |
1414
| test.cpp:78:15:78:23 | declaration of operator= | Move assignment operator for base class BaseClass6 (the class is abstract) is not declared protected or deleted. |
15+
| test.cpp:85:3:85:12 | declaration of BaseClass7 | Copy constructor for base class BaseClass7<T1> (a derived class exists) is not declared protected or deleted. |
16+
| test.cpp:86:3:86:12 | declaration of BaseClass7 | Move constructor for base class BaseClass7<T1> (a derived class exists) is not declared protected or deleted. |
17+
| test.cpp:87:15:87:23 | declaration of operator= | Copy assignment operator for base class BaseClass7<T1> (a derived class exists) is not declared protected or deleted. |
18+
| test.cpp:88:15:88:23 | declaration of operator= | Move assignment operator for base class BaseClass7<T1> (a derived class exists) is not declared protected or deleted. |
19+
| test.cpp:108:3:108:12 | declaration of BaseClass8 | Copy constructor for base class BaseClass8 (a derived class exists) is not declared protected or deleted. |
20+
| test.cpp:109:3:109:12 | declaration of BaseClass8 | Move constructor for base class BaseClass8 (a derived class exists) is not declared protected or deleted. |
21+
| test.cpp:110:15:110:23 | declaration of operator= | Copy assignment operator for base class BaseClass8 (a derived class exists) is not declared protected or deleted. |
22+
| test.cpp:111:15:111:23 | declaration of operator= | Move assignment operator for base class BaseClass8 (a derived class exists) is not declared protected or deleted. |

cpp/autosar/test/rules/A12-8-6/test.cpp

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,52 @@ class BaseClass6 {
7777
BaseClass6 &operator=(BaseClass6 const &) = default; // NON_COMPLIANT
7878
BaseClass6 &operator=(BaseClass6 &&) = default; // NON_COMPLIANT
7979
virtual void test() = 0; // pure virtual function, making this abstract
80-
};
80+
};
81+
82+
template <class T1> class BaseClass7 {
83+
public:
84+
BaseClass7() {}
85+
BaseClass7(BaseClass7 const &) = default; // NON_COMPLIANT
86+
BaseClass7(BaseClass7 &&) = default; // NON_COMPLIANT
87+
BaseClass7 &operator=(BaseClass7 const &) = default; // NON_COMPLIANT
88+
BaseClass7 &operator=(BaseClass7 &&) = default; // NON_COMPLIANT
89+
int operator=(int i); // COMPLIANT - not an assignment operator
90+
}; // COMPLIANT
91+
92+
template <class T>
93+
class DerivedClass7 // COMPLIANT - not a base class itself
94+
: public BaseClass7<T> {
95+
public:
96+
DerivedClass7() {}
97+
};
98+
99+
class DerivedClass8 // COMPLIANT - not a base class itself
100+
: public BaseClass7<int> {
101+
public:
102+
DerivedClass8() {}
103+
};
104+
105+
class BaseClass8 {
106+
public:
107+
BaseClass8() {}
108+
BaseClass8(BaseClass8 const &) = default; // NON_COMPLIANT
109+
BaseClass8(BaseClass8 &&) = default; // NON_COMPLIANT
110+
BaseClass8 &operator=(BaseClass8 const &) = default; // NON_COMPLIANT
111+
BaseClass8 &operator=(BaseClass8 &&) = default; // NON_COMPLIANT
112+
};
113+
114+
template <class T>
115+
class DerivedClass9 // COMPLIANT - not a base class itself
116+
: public BaseClass8 {
117+
public:
118+
DerivedClass9() {}
119+
120+
private:
121+
T t;
122+
};
123+
124+
void test() {
125+
BaseClass7<int> b;
126+
DerivedClass7<int> d;
127+
DerivedClass9<int> e;
128+
}

cpp/common/src/codingstandards/cpp/Class.qll

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,19 @@ import codingstandards.cpp.Expr
1010
*/
1111
predicate isPossibleBaseClass(Class c, string reason) {
1212
// There exists a derivation in this database
13-
exists(c.getADerivedClass()) and reason = "a derived class exists"
13+
(
14+
// We make a distinction between class template instantiations, regular classes and template classes.
15+
// For template classes we do have derived classes, because derived classes would derive from a
16+
// class template instantiation.
17+
// Therefore, we check for derived classes for regular classes
18+
not c instanceof ClassTemplateInstantiation and not c instanceof TemplateClass and exists(c.getADerivedClass())
19+
or
20+
// and use template instantiations to check for derived classes for template classes
21+
exists(ClassTemplateInstantiation instantiation |
22+
exists(instantiation.getADerivedClass()) and c = instantiation.getTemplate()
23+
)
24+
) and
25+
reason = "a derived class exists"
1426
or
1527
// The class must be extended at some point
1628
c.isAbstract() and reason = "the class is abstract"

0 commit comments

Comments
 (0)