Skip to content

Commit 1e9c547

Browse files
committed
Address review comments
1 parent 8eb98c0 commit 1e9c547

File tree

6 files changed

+26
-7
lines changed

6 files changed

+26
-7
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
- `DCL40-C` - `IncompatibleObjectDeclarations.ql`:
2-
- Enhanced the query's ability to compare objects with array type, which will reduce false positives.
2+
- The rule now is capable to detect equivalence based on compatible types instead of exact types (which was what it was previously checking on objects). This will eliminate false positives for some array types and types using equivalent typedefs.

cpp/common/src/codingstandards/cpp/rules/incompatibleobjectdeclaration/IncompatibleObjectDeclaration.qll

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ signature module IncompatibleObjectDeclarationConfigSig {
1515
Query getQuery();
1616
}
1717

18+
class VariableDeclarationEntryExternal extends VariableDeclarationEntry {
19+
VariableDeclarationEntryExternal() { this.getDeclaration() instanceof ExternalIdentifiers }
20+
}
21+
1822
predicate relevantTypes(Type a, Type b) {
19-
exists(VariableDeclarationEntry varA, VariableDeclarationEntry varB |
23+
exists(VariableDeclarationEntryExternal varA, VariableDeclarationEntryExternal varB |
2024
not varA = varB and
2125
varA.getVariable().getName() = varB.getVariable().getName() and
2226
a = varA.getType() and
@@ -26,16 +30,14 @@ predicate relevantTypes(Type a, Type b) {
2630

2731
module IncompatibleObjectDeclaration<IncompatibleObjectDeclarationConfigSig Config> {
2832
query predicate problems(
29-
VariableDeclarationEntry decl1, string message, VariableDeclarationEntry decl2,
33+
VariableDeclarationEntryExternal decl1, string message, VariableDeclarationEntryExternal decl2,
3034
string secondMessage
3135
) {
3236
not isExcluded(decl1, Config::getQuery()) and
3337
not isExcluded(decl2, Config::getQuery()) and
3438
not TypeEquivalence<TypesCompatibleConfig, relevantTypes/2>::equalTypes(decl1.getType(),
3539
decl2.getType()) and
3640
not decl1 = decl2 and
37-
decl1.getDeclaration() instanceof ExternalIdentifiers and
38-
decl2.getDeclaration() instanceof ExternalIdentifiers and
3941
decl1.getLocation().getStartLine() >= decl2.getLocation().getStartLine() and
4042
decl1.getVariable().getName() = decl2.getVariable().getName() and
4143
secondMessage = decl2.getName() and
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
| test1.cpp:1:12:1:12 | declaration of i | The object is not compatible with a re-declaration $@. | test.cpp:1:7:1:7 | definition of i | i |
22
| test1.cpp:2:13:2:13 | declaration of a | The object is not compatible with a re-declaration $@. | test.cpp:2:5:2:5 | definition of a | a |
3+
| test1.cpp:9:18:9:18 | declaration of x | The object is not compatible with a re-declaration $@. | test.cpp:8:17:8:17 | declaration of x | x |
4+
| test1.cpp:11:10:11:11 | declaration of a1 | The object is not compatible with a re-declaration $@. | test.cpp:10:10:10:11 | declaration of a1 | a1 |
35
| test.cpp:1:7:1:7 | definition of i | The object is not compatible with a re-declaration $@. | test1.cpp:1:12:1:12 | declaration of i | i |
46
| test.cpp:2:5:2:5 | definition of a | The object is not compatible with a re-declaration $@. | test1.cpp:2:13:2:13 | declaration of a | a |

cpp/common/test/rules/incompatibleobjectdeclaration/test.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@ short i; // NON_COMPLIANT
22
int a[] = {1, 2, 3, 4}; // NON_COMPLIANT
33
long b; // NON_COMPLIANT[FALSE_NEGATIVE] -- compiler does not extract c linkage
44
extern int c[]; // COMPLIANT
5-
extern int d; // COMPLIANT
5+
extern int d; // COMPLIANT
6+
7+
#include "test.h"
8+
extern C<int[]> x; // NON_COMPLIANT
9+
10+
extern B a1; // NON_COMPLIANT
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
template <typename T> class C {};
3+
4+
class A {};
5+
class B {};

cpp/common/test/rules/incompatibleobjectdeclaration/test1.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,9 @@ extern int *a; // NON_COMPLIANT
33
extern "C" long
44
b; // NON_COMPLIANT[FALSE_NEGATIVE] -- compiler does not extract c linkage
55
extern int c[1]; // COMPLIANT
6-
extern int d{1}; // COMPLIANT
6+
extern int d{1}; // COMPLIANT
7+
8+
#include "test.h"
9+
extern C<int[4]> x; // NON_COMPLIANT
10+
11+
extern A a1; // NON_COMPLIANT

0 commit comments

Comments
 (0)