Skip to content

Commit f202511

Browse files
Merge pull request #1771 from geoffw0/qldoceg8
CPP: Add syntax examples to QLDoc in NameQualifiers.qll
2 parents 36b42de + a84f192 commit f202511

File tree

4 files changed

+72
-8
lines changed

4 files changed

+72
-8
lines changed

cpp/ql/src/semmle/code/cpp/Enum.qll

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,16 @@ import semmle.code.cpp.Type
22
private import semmle.code.cpp.internal.ResolveClass
33

44
/**
5-
* A C/C++ enum [N4140 7.2]. For example, the type `MyEnum` in:
5+
* A C/C++ enum [N4140 7.2]. For example, the types `MyEnum` and
6+
* `MyScopedEnum` in:
67
* ```
78
* enum MyEnum {
89
* MyEnumConstant
910
* };
11+
*
12+
* enum class MyScopedEnum {
13+
* MyScopedEnumConstant
14+
* };
1015
* ```
1116
* This includes C++ scoped enums, see the `ScopedEnum` QL class.
1217
*/

cpp/ql/src/semmle/code/cpp/NameQualifiers.qll

Lines changed: 40 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
11
import cpp
22

33
/**
4-
* A C++ name qualifier, for example `N::`.
4+
* A C++ name qualifier, for example `N::` in the following code:
5+
* ```
6+
* namespace N {
7+
* int f() {
8+
* ...
9+
* }
10+
* }
11+
*
12+
* int g() {
13+
* return N::f();
14+
* }
15+
* ```
516
*/
617
class NameQualifier extends NameQualifiableElement, @namequalifier {
718
/**
@@ -61,10 +72,21 @@ class NameQualifier extends NameQualifiableElement, @namequalifier {
6172

6273
/**
6374
* A C++ element that can be qualified with a name. This is in practice
64-
* either an expression or a name qualifier. For instance, in
65-
* `N1::N2::f()`, there are two name-qualifiable elements: the expression
66-
* `f()` and the name qualifier `N2::`. The former is qualified by `N2` and
67-
* the latter is qualified by `N1`.
75+
* either an expression or a name qualifier. For example, there are two
76+
* name-qualifiable elements in the following code, the expression `f()`
77+
* (which is qualified by `N::`), and the qualifier `N::` (which is not
78+
* itself qualified in this example):
79+
* ```
80+
* namespace N {
81+
* int f() {
82+
* ...
83+
* }
84+
* }
85+
*
86+
* int g() {
87+
* return N::f();
88+
* }
89+
* ```
6890
*/
6991
class NameQualifiableElement extends Element, @namequalifiableelement {
7092
/**
@@ -99,8 +121,19 @@ class NameQualifiableElement extends Element, @namequalifiableelement {
99121
}
100122

101123
/**
102-
* A C++ element that can qualify a name. For example, `N` in `N::f()`. A
103-
* name-qualifying element is either a namespace or a user-defined type.
124+
* A C++ element that can qualify a name. For example, the namespaces `A` and
125+
* `A::B` and the class `A::C` in the following code:
126+
* ```
127+
* namespace A {
128+
* namespace B {
129+
* ...
130+
* }
131+
*
132+
* class C {
133+
* ...
134+
* };
135+
* }
136+
* ```
104137
*/
105138
class NameQualifyingElement extends Element, @namequalifyingelement {
106139
/**
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
| file://:0:0:0:0 | (global namespace) | NameQualifyingElement |
2+
| file://:0:0:0:0 | B | NameQualifyingElement |
3+
| namespaces.cpp:1:11:1:11 | A | NameQualifyingElement |
4+
| namespaces.cpp:9:11:9:11 | C | NameQualifyingElement |
5+
| namespaces.cpp:11:13:11:13 | C::D | NameQualifyingElement |
6+
| namespaces.cpp:13:30:13:30 | 0 | NameQualifiableElement |
7+
| namespaces.cpp:15:12:15:12 | E | NameQualifyingElement |
8+
| namespaces.cpp:32:11:32:13 | C:: | NameQualifiableElement |
9+
| namespaces.cpp:32:11:32:17 | call to f | NameQualifiableElement |
10+
| namespaces.cpp:32:14:32:16 | D:: | NameQualifiableElement |
11+
| namespaces.cpp:36:11:36:13 | std | NameQualifyingElement |
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import cpp
2+
3+
string describe(Element e) {
4+
(
5+
e instanceof NameQualifiableElement and
6+
result = "NameQualifiableElement"
7+
) or (
8+
e instanceof NameQualifyingElement and
9+
result = "NameQualifyingElement"
10+
)
11+
}
12+
13+
from Element e
14+
where e.getFile().fromSource() or e instanceof Namespace
15+
select e, strictconcat(describe(e), ", ")

0 commit comments

Comments
 (0)