Skip to content

Commit 0652d2a

Browse files
authored
Merge pull request #1705 from geoffw0/qldoceg2
CPP: Add syntax examples to QLDoc in Enum.qll.
2 parents 0bf9529 + dc1ec63 commit 0652d2a

File tree

5 files changed

+86
-21
lines changed

5 files changed

+86
-21
lines changed

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

Lines changed: 43 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import semmle.code.cpp.Type
22
private import semmle.code.cpp.internal.ResolveClass
33

44
/**
5-
* A C/C++ enum [N4140 7.2].
5+
* A C/C++ enum [N4140 7.2]. For example, the type `MyEnum` in:
6+
* ```
7+
* enum MyEnum {
8+
* MyEnumConstant
9+
* };
10+
* ```
11+
* This includes C++ scoped enums, see the `ScopedEnum` QL class.
612
*/
713
class Enum extends UserType, IntegralOrEnumType {
814
/** Gets an enumerator of this enumeration. */
@@ -46,7 +52,15 @@ class Enum extends UserType, IntegralOrEnumType {
4652
}
4753

4854
/**
49-
* A C++ enum that is directly enclosed by a function.
55+
* A C/C++ enum that is directly enclosed by a function. For example, the type
56+
* `MyLocalEnum` in:
57+
* ```
58+
* void myFunction() {
59+
* enum MyLocalEnum {
60+
* MyLocalEnumConstant
61+
* };
62+
* }
63+
* ```
5064
*/
5165
class LocalEnum extends Enum {
5266
LocalEnum() {
@@ -57,7 +71,16 @@ class LocalEnum extends Enum {
5771
}
5872

5973
/**
60-
* A C++ enum that is declared within a class.
74+
* A C/C++ enum that is declared within a class/struct. For example, the type
75+
* `MyNestedEnum` in:
76+
* ```
77+
* class MyClass {
78+
* public:
79+
* enum MyNestedEnum {
80+
* MyNestedEnumConstant
81+
* };
82+
* };
83+
* ```
6184
*/
6285
class NestedEnum extends Enum {
6386

@@ -79,9 +102,14 @@ class NestedEnum extends Enum {
79102
}
80103

81104
/**
82-
* A C++ scoped enum.
83-
*
84-
* For example, `enum class Color { red, blue }`.
105+
* A C++ scoped enum, that is, an enum whose constants must be qualified with
106+
* the name of the enum. For example, the type `Color` in:
107+
* ```
108+
* enum class Color {
109+
* red,
110+
* blue
111+
* }
112+
* ```
85113
*/
86114
class ScopedEnum extends Enum {
87115
ScopedEnum() {
@@ -92,11 +120,16 @@ class ScopedEnum extends Enum {
92120
}
93121

94122
/**
95-
* A C/C++ enumerator [N4140 7.2].
96-
*
97-
* For example: `green` in `enum { red, green, blue }`.
123+
* A C/C++ enumerator [N4140 7.2], also known as an enumeration constant.
98124
*
99-
* Enumerators are also knowns as enumeration constants.
125+
* For example the enumeration constant `green` in:
126+
* ```
127+
* enum {
128+
* red,
129+
* green,
130+
* blue
131+
* }
132+
* ```
100133
*/
101134
class EnumConstant extends Declaration, @enumconstant {
102135
/**

cpp/ql/test/library-tests/enums/enums/Enums1.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
| enums.cpp:5:13:5:13 | b | b | 1 | Flag | 1 | 1 | 1 |
1212
| enums.cpp:5:22:5:22 | c | c | 1 | Flag | 1 | 1 | 1 |
1313
| enums.cpp:5:31:5:31 | d | d | 1 | Flag | 1 | 1 | 1 |
14+
| enums.cpp:21:3:21:21 | myLocalEnumConstant | myLocalEnumConstant | 1 | myLocalEnum | 1 | 1 | 1 |
15+
| enums.cpp:29:5:29:24 | MyNestedEnumConstant | MyNestedEnumConstant | 1 | MyNestedEnum | 1 | 1 | 1 |
1416
| enums.ms.c:2:3:2:6 | zero | zero | 1 | numbers | 1 | 1 | 1 |
1517
| enums.ms.c:2:9:2:11 | one | one | 1 | numbers | 1 | 1 | 1 |
1618
| scoped.cpp:3:5:3:5 | X | X | 1 | E1 | 1 | 1 | 1 |
Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
| enums.cpp:3:6:3:8 | Day | false |
2-
| enums.cpp:4:6:4:9 | Day2 | false |
3-
| enums.cpp:5:6:5:9 | Flag | false |
4-
| enums.ms.c:1:6:1:12 | numbers | false |
5-
| scoped.cpp:2:12:2:13 | E1 | true |
6-
| scoped.cpp:6:12:6:13 | E2 | true |
7-
| scoped.cpp:10:13:10:14 | E3 | true |
8-
| scoped.cpp:16:14:16:18 | State | true |
1+
| enums.cpp:3:6:3:8 | Day | |
2+
| enums.cpp:4:6:4:9 | Day2 | |
3+
| enums.cpp:5:6:5:9 | Flag | |
4+
| enums.cpp:19:7:19:17 | myLocalEnum | LocalEnum |
5+
| enums.cpp:27:8:27:19 | MyNestedEnum | NestedEnum |
6+
| enums.ms.c:1:6:1:12 | numbers | |
7+
| scoped.cpp:2:12:2:13 | E1 | ScopedEnum |
8+
| scoped.cpp:6:12:6:13 | E2 | ScopedEnum |
9+
| scoped.cpp:10:13:10:14 | E3 | ScopedEnum |
10+
| scoped.cpp:16:14:16:18 | State | NestedEnum, ScopedEnum |
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import cpp
22

3-
from Enum e, boolean isScoped
4-
where if e instanceof ScopedEnum then isScoped = true else isScoped = false
5-
select e, isScoped
3+
string describe(Enum e)
4+
{
5+
(
6+
e instanceof LocalEnum and
7+
result = "LocalEnum"
8+
) or (
9+
e instanceof NestedEnum and
10+
result = "NestedEnum"
11+
) or (
12+
e instanceof ScopedEnum and
13+
result = "ScopedEnum"
14+
)
15+
}
616

17+
from Enum e
18+
select e, concat(describe(e), ", ")

cpp/ql/test/library-tests/enums/enums/enums.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,19 @@ Day& operator++(Day& d)
1313
Day2 d2 = (Day2)d;
1414
return d = (sat==d) ? sun: Day(d+1);
1515
}
16+
17+
void myFunction()
18+
{
19+
enum myLocalEnum
20+
{
21+
myLocalEnumConstant
22+
};
23+
};
24+
25+
class MyClass
26+
{
27+
enum MyNestedEnum
28+
{
29+
MyNestedEnumConstant
30+
};
31+
};

0 commit comments

Comments
 (0)