|
15 | 15 |
|
16 | 16 | import cpp |
17 | 17 | import codingstandards.cpp.autosar |
| 18 | +import codingstandards.cpp.Class |
18 | 19 |
|
19 | | -from FunctionDeclarationEntry overridingDecl, FunctionDeclarationEntry hiddenDecl |
| 20 | +/** |
| 21 | + * Holds if the class has a non-virtual member function with the given name. |
| 22 | + */ |
| 23 | +pragma[noinline, nomagic] |
| 24 | +predicate hasNonVirtualMemberFunction(Class clazz, MemberFunction mf, string name) { |
| 25 | + mf.getDeclaringType() = clazz and |
| 26 | + mf.getName() = name and |
| 27 | + not mf.isVirtual() and |
| 28 | + // Exclude private member functions, which cannot be inherited. |
| 29 | + not mf.isPrivate() |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Holds if the member function is in a class with the given base class, and has the given name. |
| 34 | + */ |
| 35 | +pragma[noinline, nomagic] |
| 36 | +predicate hasDeclarationBaseClass(MemberFunction mf, Class baseClass, string functionName) { |
| 37 | + baseClass = mf.getDeclaringType().getABaseClass() and |
| 38 | + functionName = mf.getName() |
| 39 | +} |
| 40 | + |
| 41 | +from MemberFunction overridingDecl, MemberFunction hiddenDecl, Class baseClass, string name |
20 | 42 | where |
21 | 43 | not isExcluded(overridingDecl, ScopePackage::hiddenInheritedNonOverridableMemberFunctionQuery()) and |
22 | 44 | // Check if we are overriding a non-virtual inherited member function |
23 | | - overridingDecl.getName() = hiddenDecl.getName() and |
24 | | - overridingDecl.getDeclaration().getDeclaringType().getABaseClass() = |
25 | | - hiddenDecl.getDeclaration().getDeclaringType() and |
26 | | - not hiddenDecl.getDeclaration().isVirtual() and |
| 45 | + hasNonVirtualMemberFunction(baseClass, hiddenDecl, name) and |
| 46 | + hasDeclarationBaseClass(overridingDecl, baseClass, name) and |
27 | 47 | // Where the hidden member function isn't explicitly brought in scope through a using declaration. |
28 | 48 | not exists(UsingDeclarationEntry ude | |
29 | | - ude.getDeclaration() = hiddenDecl.getDeclaration() and |
30 | | - ude.getEnclosingElement() = overridingDecl.getDeclaration().getDeclaringType() and |
31 | | - ude.getLocation().getStartLine() < overridingDecl.getLocation().getStartLine() |
| 49 | + ude.getDeclaration() = hiddenDecl and |
| 50 | + ude.getEnclosingElement() = overridingDecl.getDeclaringType() |
32 | 51 | ) and |
33 | 52 | // Exclude compiler generated member functions which include things like copy constructor that hide base class |
34 | 53 | // copy constructors. |
35 | | - not overridingDecl.getDeclaration().isCompilerGenerated() |
| 54 | + not overridingDecl.isCompilerGenerated() and |
| 55 | + // Exclude special member functions, which cannot be inherited. |
| 56 | + not overridingDecl instanceof SpecialMemberFunction |
36 | 57 | select overridingDecl, |
37 | | - "Declaration for member '" + overridingDecl.getName() + |
38 | | - "' hides non-overridable inherited member function $@", hiddenDecl, hiddenDecl.getName() |
| 58 | + "Declaration for member '" + name + "' hides non-overridable inherited member function $@", |
| 59 | + hiddenDecl, hiddenDecl.getName() |
0 commit comments