Skip to content

Commit 880c785

Browse files
committed
C++: Ensure we can round trip between (forward) class declarations
This was already possible when the forward class declaration and the class definition occurred in the same scope. However, there is a common C++ usage pattern in which this is not the case (when only a pointer to the class is needed). In this latter scenario we could not round trip between the (forward) `DeclarationEntry` and the `Declaration`. Effectively this changes the code to: ``` if exists(TypeDeclarationEntry e | e.getType() = this) then result.getType() = this else ... ``` We use `type_decls` instead to stay close to the original code.
1 parent a8833a0 commit 880c785

File tree

3 files changed

+4
-4
lines changed

3 files changed

+4
-4
lines changed

cpp/ql/lib/semmle/code/cpp/UserType.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ class UserType extends Type, Declaration, NameQualifyingElement, AccessHolder, @
4848
}
4949

5050
override TypeDeclarationEntry getADeclarationEntry() {
51-
if type_decls(_, underlyingElement(this), _)
52-
then type_decls(unresolveElement(result), underlyingElement(this), _)
51+
if type_decls(_, unresolveElement(this), _)
52+
then type_decls(underlyingElement(result), unresolveElement(this), _)
5353
else exists(Class t | this.(Class).isConstructedFrom(t) and result = t.getADeclarationEntry())
5454
}
5555

cpp/ql/test/library-tests/declarationEntry/declarationEntry/declarationEntry.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
| declarationEntry.cpp:39:7:39:7 | operator= | declarationEntry.cpp:39:7:39:7 | declaration of operator= | 1 | 1 |
3131
| declarationEntry.cpp:39:7:39:7 | operator= | declarationEntry.cpp:39:7:39:7 | declaration of operator= | 1 | 1 |
3232
| declarationEntry.cpp:39:7:39:13 | myClass | declarationEntry.cpp:39:7:39:13 | definition of myClass | 1 | 1 |
33-
| declarationEntry.cpp:39:7:39:13 | myClass | forwardDeclaration.cpp:1:7:1:13 | declaration of myClass | 0 | 1 |
33+
| declarationEntry.cpp:39:7:39:13 | myClass | forwardDeclaration.cpp:1:7:1:13 | declaration of myClass | 1 | 1 |
3434
| declarationEntry.cpp:42:6:42:21 | myMemberVariable | declarationEntry.cpp:42:6:42:21 | definition of myMemberVariable | 1 | 1 |
3535
| forwardDeclaration.cpp:3:10:3:19 | myClassPtr | forwardDeclaration.cpp:3:10:3:19 | definition of myClassPtr | 1 | 1 |
3636
| macro.c:2:1:2:3 | foo | macro.c:2:1:2:3 | declaration of foo | 1 | 1 |

cpp/ql/test/library-tests/declarationEntry/declarationEntry/roundTrip.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
| file://:0:0:0:0 | definition of gp_offset | file://:0:0:0:0 | gp_offset | yes |
4040
| file://:0:0:0:0 | definition of overflow_arg_area | file://:0:0:0:0 | overflow_arg_area | yes |
4141
| file://:0:0:0:0 | definition of reg_save_area | file://:0:0:0:0 | reg_save_area | yes |
42-
| forwardDeclaration.cpp:1:7:1:13 | declaration of myClass | declarationEntry.cpp:39:7:39:13 | myClass | no |
42+
| forwardDeclaration.cpp:1:7:1:13 | declaration of myClass | declarationEntry.cpp:39:7:39:13 | myClass | yes |
4343
| forwardDeclaration.cpp:3:10:3:19 | definition of myClassPtr | forwardDeclaration.cpp:3:10:3:19 | myClassPtr | yes |
4444
| macro.c:2:1:2:3 | declaration of foo | macro.c:2:1:2:3 | foo | yes |
4545
| macro.c:4:5:4:8 | definition of main | macro.c:4:5:4:8 | main | yes |

0 commit comments

Comments
 (0)