Skip to content

Commit bfdc09a

Browse files
Handle overloads
1 parent ac06fe8 commit bfdc09a

File tree

4 files changed

+29
-22
lines changed

4 files changed

+29
-22
lines changed

cpp/misra/src/rules/RULE-6-2-3/SourceCodeImplementedOnlyOnce.ql

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,29 @@
1616

1717
import cpp
1818
import codingstandards.cpp.misra
19+
import codingstandards.cpp.types.Compatible
1920

20-
predicate isInline(DeclarationEntry d) {
21-
// There is no way to detect if a `GlobalVariable` is declared inline.
22-
d.getDeclaration().(Function).isInline()
21+
predicate isInline(FunctionDeclarationEntry d) { d.getDeclaration().isInline() }
22+
23+
predicate interestedInFunctions(FunctionDeclarationEntry f1, FunctionDeclarationEntry f2) {
24+
f1.isDefinition() and
25+
f2.isDefinition() and
26+
f1.getDeclaration().getQualifiedName() = f2.getDeclaration().getQualifiedName() and
27+
isInline(f1) and
28+
isInline(f2) and
29+
not f1.getFile() = f2.getFile()
2330
}
2431

25-
from DeclarationEntry d1, DeclarationEntry d2, string namespace, string name
32+
module FunDeclEquiv =
33+
FunctionDeclarationTypeEquivalence<TypesCompatibleConfig, interestedInFunctions/2>;
34+
35+
from FunctionDeclarationEntry d1, FunctionDeclarationEntry d2, string namespace, string name
2636
where
2737
not isExcluded([d1, d2], Declarations8Package::sourceCodeImplementedOnlyOnceQuery()) and
28-
d1 != d2 and
29-
d1.isDefinition() and
30-
d2.isDefinition() and
31-
isInline(d1) and
32-
isInline(d2) and
38+
interestedInFunctions(d1, d2) and
39+
FunDeclEquiv::equalParameterTypes(d1, d2) and
3340
d1.getDeclaration().hasQualifiedName(namespace, name) and
3441
d2.getDeclaration().hasQualifiedName(namespace, name) and
35-
d1.getFile() != d2.getFile() and
3642
d1.getFile().getAbsolutePath() < d2.getFile().getAbsolutePath()
37-
select d1,
38-
"Inline variable '" + d1.getName() +
39-
"' is defined in multiple files, violating the source code uniqueness requirement."
43+
select d1, "Inline function '" + d1.getName() + "' is implemented in multiple files: $@ and $@.",
44+
d1, d1.getFile().getBaseName(), d2, d2.getFile().getBaseName()
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
| test.cpp:15:8:15:22 | definition of StructRedefined | Type 'StructRedefined' is defined in files $@ and $@. | test.cpp:15:8:15:22 | definition of StructRedefined | test.cpp | test2.cpp:16:8:16:22 | definition of StructRedefined | test2.cpp |
2-
| test.cpp:22:29:22:40 | definition of TplRedefined<T> | Type 'TplRedefined<T>' is defined in files $@ and $@. | test.cpp:22:29:22:40 | definition of TplRedefined<T> | test.cpp | test2.cpp:22:29:22:40 | definition of TplRedefined<T> | test2.cpp |
3-
| test.cpp:26:6:26:18 | definition of DuplicateEnum | Type 'DuplicateEnum' is defined in files $@ and $@. | test.cpp:26:6:26:18 | definition of DuplicateEnum | test.cpp | test2.cpp:25:6:25:18 | definition of DuplicateEnum | test2.cpp |
4-
| test.cpp:27:12:27:29 | definition of DuplicateEnumClass | Type 'DuplicateEnumClass' is defined in files $@ and $@. | test.cpp:27:12:27:29 | definition of DuplicateEnumClass | test.cpp | test2.cpp:26:12:26:29 | definition of DuplicateEnumClass | test2.cpp |
5-
| test.cpp:29:17:29:32 | declaration of DuplicateTypedef | Type 'DuplicateTypedef' is defined in files $@ and $@. | test.cpp:29:17:29:32 | declaration of DuplicateTypedef | test.cpp | test2.cpp:28:17:28:32 | declaration of DuplicateTypedef | test2.cpp |
6-
| test.cpp:30:7:30:20 | declaration of DuplicateUsing | Type 'DuplicateUsing' is defined in files $@ and $@. | test.cpp:30:7:30:20 | declaration of DuplicateUsing | test.cpp | test2.cpp:29:7:29:20 | declaration of DuplicateUsing | test2.cpp |
7-
| test.cpp:31:7:31:20 | definition of DuplicateUnion | Type 'DuplicateUnion' is defined in files $@ and $@. | test.cpp:31:7:31:20 | definition of DuplicateUnion | test.cpp | test2.cpp:30:7:30:20 | definition of DuplicateUnion | test2.cpp |
8-
| test.cpp:36:7:36:11 | definition of Outer | Type 'ns1::Outer' is defined in files $@ and $@. | test.cpp:36:7:36:11 | definition of Outer | test.cpp | test2.cpp:35:7:35:11 | definition of Outer | test2.cpp |
9-
| test.cpp:37:9:37:13 | definition of Inner | Type 'ns1::Outer::Inner' is defined in files $@ and $@. | test.cpp:37:9:37:13 | definition of Inner | test.cpp | test2.cpp:36:9:36:13 | definition of Inner | test2.cpp |
1+
| test.cpp:16:8:16:22 | definition of StructRedefined | Type 'StructRedefined' is defined in files $@ and $@. | test.cpp:16:8:16:22 | definition of StructRedefined | test.cpp | test2.cpp:17:8:17:22 | definition of StructRedefined | test2.cpp |
2+
| test.cpp:23:29:23:40 | definition of TplRedefined<T> | Type 'TplRedefined<T>' is defined in files $@ and $@. | test.cpp:23:29:23:40 | definition of TplRedefined<T> | test.cpp | test2.cpp:23:29:23:40 | definition of TplRedefined<T> | test2.cpp |
3+
| test.cpp:27:6:27:18 | definition of DuplicateEnum | Type 'DuplicateEnum' is defined in files $@ and $@. | test.cpp:27:6:27:18 | definition of DuplicateEnum | test.cpp | test2.cpp:26:6:26:18 | definition of DuplicateEnum | test2.cpp |
4+
| test.cpp:28:12:28:29 | definition of DuplicateEnumClass | Type 'DuplicateEnumClass' is defined in files $@ and $@. | test.cpp:28:12:28:29 | definition of DuplicateEnumClass | test.cpp | test2.cpp:27:12:27:29 | definition of DuplicateEnumClass | test2.cpp |
5+
| test.cpp:30:17:30:32 | declaration of DuplicateTypedef | Type 'DuplicateTypedef' is defined in files $@ and $@. | test.cpp:30:17:30:32 | declaration of DuplicateTypedef | test.cpp | test2.cpp:29:17:29:32 | declaration of DuplicateTypedef | test2.cpp |
6+
| test.cpp:31:7:31:20 | declaration of DuplicateUsing | Type 'DuplicateUsing' is defined in files $@ and $@. | test.cpp:31:7:31:20 | declaration of DuplicateUsing | test.cpp | test2.cpp:30:7:30:20 | declaration of DuplicateUsing | test2.cpp |
7+
| test.cpp:32:7:32:20 | definition of DuplicateUnion | Type 'DuplicateUnion' is defined in files $@ and $@. | test.cpp:32:7:32:20 | definition of DuplicateUnion | test.cpp | test2.cpp:31:7:31:20 | definition of DuplicateUnion | test2.cpp |
8+
| test.cpp:37:7:37:11 | definition of Outer | Type 'ns1::Outer' is defined in files $@ and $@. | test.cpp:37:7:37:11 | definition of Outer | test.cpp | test2.cpp:36:7:36:11 | definition of Outer | test2.cpp |
9+
| test.cpp:38:9:38:13 | definition of Inner | Type 'ns1::Outer::Inner' is defined in files $@ and $@. | test.cpp:38:9:38:13 | definition of Inner | test.cpp | test2.cpp:37:9:37:13 | definition of Inner | test2.cpp |

cpp/misra/test/rules/RULE-6-2-3/test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ inline int16_t global_redeclared = 0; // COMPLIANT
66
inline void func_redefined() {} // NON_COMPLIANT
77
inline void func_unique() {} // COMPLIANT
88
inline void func_redeclared() {} // COMPLIANT
9+
inline void func_overloaded(int) {} // COMPLIANT
910

1011
// Violates our implementation of 6.2.1, but legal in our implementation
1112
// of 6.2.3

cpp/misra/test/rules/RULE-6-2-3/test2.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ inline int16_t global_redefined = 0; // NON_COMPLIANT[False negative]
77
extern inline int16_t global_redeclared; // COMPLIANT
88
inline void func_redefined() {} // NON_COMPLIANT -- flagged in test.cpp
99
inline void func_redeclared(); // COMPLIANT
10+
inline void func_overloaded(double) {} // COMPLIANT
1011

1112
// Violates our implementation of 6.2.1, but legal in our implementation
1213
// of 6.2.3

0 commit comments

Comments
 (0)