Skip to content

Commit df4c576

Browse files
committed
C++: Support inline namespaces in hasQualifiedName
1 parent 4bb65fd commit df4c576

File tree

4 files changed

+28
-5
lines changed

4 files changed

+28
-5
lines changed

change-notes/1.21/analysis-cpp.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
## Changes to QL libraries
3333
- The predicate `Declaration.hasGlobalName` now only holds for declarations that are not nested in a class. For example, it no longer holds for a member function `MyClass::myFunction` or a constructor `MyClass::MyClass`, whereas previously it would classify those two declarations as global names.
34-
- In class `Declaration`, predicates `getQualifiedName/0` and `hasQualifiedName/1` are no longer recommended for finding functions by name. Instead, use `hasGlobalName/1` and the new `hasQualifiedName/2` and `hasQualifiedName/3` predicates. This improves performance and makes it more reliable to identify names involving templates.
34+
- In class `Declaration`, predicates `getQualifiedName/0` and `hasQualifiedName/1` are no longer recommended for finding functions by name. Instead, use `hasGlobalName/1` and the new `hasQualifiedName/2` and `hasQualifiedName/3` predicates. This improves performance and makes it more reliable to identify names involving templates and inline namespaces.
3535
- Additional support for definition by reference has been added to the `semmle.code.cpp.dataflow.TaintTracking` library.
3636
- The taint tracking library now includes taint-specific edges for functions modeled in `semmle.code.cpp.models.interfaces.DataFlow`.
3737
- The taint tracking library adds flow through library functions that are modeled in `semmle.code.cpp.models.interfaces.Taint`. Queries can add subclasses of `TaintFunction` to specify additional flow.

cpp/ql/src/semmle/code/cpp/internal/QualifiedName.qll

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,22 @@ class Namespace extends @namespace {
2626
else result = this.getName()
2727
}
2828

29+
string getQualifierForMembers() {
30+
if namespacembrs(_, this)
31+
then
32+
exists(Namespace ns |
33+
namespacembrs(ns, this)
34+
|
35+
result = ns.getQualifierForMembers() + "::" + this.getName()
36+
or
37+
// If this is an inline namespace, its members are also visible in any
38+
// namespace where the members of the parent are visible.
39+
namespace_inline(this) and
40+
result = ns.getQualifierForMembers()
41+
)
42+
else result = this.getName()
43+
}
44+
2945
Declaration getADeclaration() {
3046
if this.getName() = ""
3147
then result.isTopLevel() and not namespacembrs(_, result)
@@ -331,7 +347,7 @@ cached
331347
private predicate declarationHasQualifiedName(
332348
string baseName, string typeQualifier, string namespaceQualifier, Declaration d
333349
) {
334-
namespaceQualifier = d.getNamespace().getQualifiedName() and
350+
namespaceQualifier = d.getNamespace().getQualifierForMembers() and
335351
(
336352
if hasTypeQualifier(d)
337353
then typeQualifier = d.getTypeQualifierWithoutArgs()

cpp/ql/test/library-tests/identifiers/qualified_names/qualifiedNames.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ namespace templates {
6262

6363
namespace std {
6464
inline namespace cpp17 {
65-
void functionInTwoNamespaces(); // BUG: should also show up in `std`
66-
class classInTwoNameSpaces { // BUG: should also show up in `std`
65+
void functionInTwoNamespaces();
66+
class classInTwoNameSpaces {
6767
};
6868
inline namespace implementation {
6969
namespace ns {
70-
void functionInFourNamespaces(); // BUG: should also show up the outer namespaces
70+
void functionInFourNamespaces();
7171
}
7272
}
7373
}

cpp/ql/test/library-tests/identifiers/qualified_names/qualifiedNames.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,17 @@
5252
| qualifiedNames.cpp:53:12:53:12 | getMember | templates::getMember | templates | | getMember | (not global) |
5353
| qualifiedNames.cpp:53:12:53:20 | getMember | templates::getMember | templates | | getMember | (not global) |
5454
| qualifiedNames.cpp:57:8:57:10 | use | templates::use | templates | | use | (not global) |
55+
| qualifiedNames.cpp:65:10:65:32 | functionInTwoNamespaces | std::cpp17::functionInTwoNamespaces | std | | functionInTwoNamespaces | (not global) |
5556
| qualifiedNames.cpp:65:10:65:32 | functionInTwoNamespaces | std::cpp17::functionInTwoNamespaces | std::cpp17 | | functionInTwoNamespaces | (not global) |
57+
| qualifiedNames.cpp:66:11:66:11 | operator= | std::cpp17::classInTwoNameSpaces::operator= | std | classInTwoNameSpaces | operator= | (not global) |
58+
| qualifiedNames.cpp:66:11:66:11 | operator= | std::cpp17::classInTwoNameSpaces::operator= | std | classInTwoNameSpaces | operator= | (not global) |
5659
| qualifiedNames.cpp:66:11:66:11 | operator= | std::cpp17::classInTwoNameSpaces::operator= | std::cpp17 | classInTwoNameSpaces | operator= | (not global) |
5760
| qualifiedNames.cpp:66:11:66:11 | operator= | std::cpp17::classInTwoNameSpaces::operator= | std::cpp17 | classInTwoNameSpaces | operator= | (not global) |
61+
| qualifiedNames.cpp:66:11:66:30 | classInTwoNameSpaces | std::cpp17::classInTwoNameSpaces | std | | classInTwoNameSpaces | (not global) |
5862
| qualifiedNames.cpp:66:11:66:30 | classInTwoNameSpaces | std::cpp17::classInTwoNameSpaces | std::cpp17 | | classInTwoNameSpaces | (not global) |
5963
| qualifiedNames.cpp:70:14:70:37 | functionInFourNamespaces | std::cpp17::implementation::ns::functionInFourNamespaces | std::cpp17::implementation::ns | | functionInFourNamespaces | (not global) |
64+
| qualifiedNames.cpp:70:14:70:37 | functionInFourNamespaces | std::cpp17::implementation::ns::functionInFourNamespaces | std::cpp17::ns | | functionInFourNamespaces | (not global) |
65+
| qualifiedNames.cpp:70:14:70:37 | functionInFourNamespaces | std::cpp17::implementation::ns::functionInFourNamespaces | std::implementation::ns | | functionInFourNamespaces | (not global) |
66+
| qualifiedNames.cpp:70:14:70:37 | functionInFourNamespaces | std::cpp17::implementation::ns::functionInFourNamespaces | std::ns | | functionInFourNamespaces | (not global) |
6067
| qualifiedNames.cpp:78:7:78:15 | void_fptr | void_fptr | | | void_fptr | void_fptr |
6168
| qualifiedNames.cpp:79:11:79:14 | ptrs | ptrs | | | ptrs | ptrs |

0 commit comments

Comments
 (0)