Skip to content

Commit b633ee1

Browse files
committed
C++: Add more tests of resolveClass
These tests exercise the problematic cases where a variable can appear to have multiple types because of how we fail to account for qualified names when comparing type names.
1 parent c9cb2a0 commit b633ee1

File tree

12 files changed

+110
-0
lines changed

12 files changed

+110
-0
lines changed

cpp/ql/test/library-tests/structs/compatible_cpp/b1.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,9 @@ class Damson {
2424
int damson_x;
2525
void foo();
2626
};
27+
28+
namespace unrelated {
29+
class AppleCompatible {
30+
long apple_x;
31+
};
32+
}

cpp/ql/test/library-tests/structs/compatible_cpp/compatible.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
| b1.cpp:23:7:23:12 | Damson | 5 members | 2 locations | 1 | foo |
3333
| b1.cpp:23:7:23:12 | Damson | 5 members | 2 locations | 2 | operator= |
3434
| b1.cpp:23:7:23:12 | Damson | 5 members | 2 locations | 3 | operator= |
35+
| b1.cpp:29:9:29:23 | AppleCompatible | 3 members | 1 locations | 0 | apple_x |
36+
| b1.cpp:29:9:29:23 | AppleCompatible | 3 members | 1 locations | 1 | operator= |
37+
| b1.cpp:29:9:29:23 | AppleCompatible | 3 members | 1 locations | 2 | operator= |
3538
| b2.cpp:2:7:2:21 | AppleCompatible | 3 members | 2 locations | 0 | apple_x |
3639
| b2.cpp:2:7:2:21 | AppleCompatible | 3 members | 2 locations | 1 | operator= |
3740
| b2.cpp:2:7:2:21 | AppleCompatible | 3 members | 2 locations | 2 | operator= |

cpp/ql/test/library-tests/structs/compatible_cpp/compatible_types.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
| b1.cpp:11:7:11:22 | BananaCompatible | 0 | file://:0:0:0:0 | int | 1 types |
1111
| b1.cpp:16:7:16:12 | Cherry | 0 | file://:0:0:0:0 | int | 1 types |
1212
| b1.cpp:23:7:23:12 | Damson | 0 | file://:0:0:0:0 | int | 1 types |
13+
| b1.cpp:29:9:29:23 | AppleCompatible | 0 | file://:0:0:0:0 | long | 1 types |
1314
| b2.cpp:2:7:2:21 | AppleCompatible | 0 | file://:0:0:0:0 | int | 1 types |
1415
| b2.cpp:9:7:9:22 | BananaCompatible | 0 | file://:0:0:0:0 | int | 1 types |
1516
| b2.cpp:14:7:14:12 | Cherry | 0 | file://:0:0:0:0 | short | 1 types |
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,11 @@
1+
| a.h:5:8:5:13 | cheese | x.cpp:6:10:6:12 | Foo | 3 |
2+
| a.h:5:8:5:13 | cheese | x.cpp:12:9:12:11 | Foo | 3 |
13
| a.h:5:8:5:13 | cheese | y.cpp:4:8:4:10 | Foo | 3 |
24
| x.cpp:3:6:3:10 | bar_x | a.h:4:8:4:10 | Bar | 3 |
5+
| x.cpp:19:6:19:10 | foo_x | x.cpp:6:10:6:12 | Foo | 3 |
6+
| x.cpp:19:6:19:10 | foo_x | x.cpp:12:9:12:11 | Foo | 3 |
7+
| x.cpp:19:6:19:10 | foo_x | y.cpp:4:8:4:10 | Foo | 3 |
8+
| x.cpp:23:5:23:17 | templateField | x.cpp:6:10:6:12 | Foo | 3 |
9+
| x.cpp:23:5:23:17 | templateField | x.cpp:12:9:12:11 | Foo | 3 |
10+
| x.cpp:26:18:26:29 | template_foo | x.cpp:22:7:22:14 | Template<Foo *> | 3 |
11+
| x.cpp:26:18:26:29 | template_foo | x.cpp:22:7:22:14 | Template<Foo *> | 3 |
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
11
#include "a.h"
22

33
Bar *bar_x;
4+
5+
namespace unrelated {
6+
struct Foo {
7+
short val;
8+
};
9+
}
10+
11+
struct ContainsAnotherFoo {
12+
class Foo {
13+
long val;
14+
};
15+
};
16+
17+
// The type of `foo_x` should not refer to any of the above classes, none of
18+
// which are named `Foo` in the global scope.
19+
Foo *foo_x;
20+
21+
template<typename T>
22+
class Template {
23+
T templateField;
24+
};
25+
26+
Template<Foo *> *template_foo;
27+
28+
// Instantiation of the template with unrelated classes named `Foo` should not
29+
// get mixed up with the instantiation above.
30+
template class Template<unrelated::Foo *>;
31+
template class Template<ContainsAnotherFoo::Foo *>;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "header.h"
2+
3+
struct MultipleDefsButSameHeader {
4+
int i;
5+
};
6+
7+
struct OneDefInDifferentFile {
8+
int i;
9+
};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include "header.h"
2+
3+
struct MultipleDefsButSameHeader {
4+
char char1;
5+
char char2;
6+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
namespace foo {
2+
class C;
3+
4+
C *x;
5+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace foo {
2+
class C {
3+
};
4+
}
5+
6+
namespace bar {
7+
class C {
8+
};
9+
}
10+
11+
class DefinedAndDeclared {
12+
};
13+
14+
// Despite this declaration being present, the variable below is associated
15+
// with the definition above rather than this declaration.
16+
class DefinedAndDeclared;
17+
18+
DefinedAndDeclared *definedAndDeclared;
19+
20+
#include "header.h"
21+
22+
// Because there are multiple definitions of `MultipleDefsButSameHeader`, the
23+
// type of this variable will refer to the declaration in `header.h` rather
24+
// than any of the definitions.
25+
MultipleDefsButSameHeader *mdbsh;
26+
27+
// Because there is only one definition of `OneDefInDifferentFile`, the type of
28+
// this variable will refer to that definition.
29+
OneDefInDifferentFile *odidf;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
struct MultipleDefsButSameHeader;
2+
3+
struct OneDefInDifferentFile;

0 commit comments

Comments
 (0)