Skip to content

Commit 5724fb0

Browse files
authored
Merge pull request #1706 from geoffw0/qldoceg3
CPP: Add syntax examples to QLDoc in Struct.qll, Union.qll.
2 parents a6cae2b + 11d17b8 commit 5724fb0

File tree

11 files changed

+143
-26
lines changed

11 files changed

+143
-26
lines changed

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

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@ import semmle.code.cpp.Type
22
import semmle.code.cpp.Class
33

44
/**
5-
* A C/C++ structure or union.
5+
* A C/C++ structure or union. For example, the types `MyStruct` and `MyUnion`
6+
* in:
7+
* ```
8+
* struct MyStruct {
9+
* int x, y, z;
10+
* };
11+
*
12+
* union MyUnion {
13+
* int i;
14+
* float f;
15+
* };
16+
* ```
617
*/
718
class Struct extends Class {
819

@@ -16,7 +27,15 @@ class Struct extends Class {
1627
}
1728

1829
/**
19-
* A C++ struct that is directly enclosed by a function.
30+
* A C/C++ struct that is directly enclosed by a function. For example, the type
31+
* `MyLocalStruct` in:
32+
* ```
33+
* void myFunction() {
34+
* struct MyLocalStruct {
35+
* int x, y, z;
36+
* };
37+
* }
38+
* ```
2039
*/
2140
class LocalStruct extends Struct {
2241
LocalStruct() {
@@ -28,7 +47,15 @@ class LocalStruct extends Struct {
2847
}
2948

3049
/**
31-
* A C++ nested struct. See 11.12.
50+
* A C/C++ nested struct. See 11.12. For example, the type `MyNestedStruct` in:
51+
* ```
52+
* class MyClass {
53+
* public:
54+
* struct MyNestedStruct {
55+
* int x, y, z;
56+
* };
57+
* };
58+
* ```
3259
*/
3360
class NestedStruct extends Struct {
3461
NestedStruct() {

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import semmle.code.cpp.Type
22
import semmle.code.cpp.Struct
33

44
/**
5-
* A C/C++ union. See C.8.2.
5+
* A C/C++ union. See C.8.2. For example, the type `MyUnion` in:
6+
* ```
7+
* union MyUnion {
8+
* int i;
9+
* float f;
10+
* };
11+
* ```
612
*/
713
class Union extends Struct {
814

@@ -17,7 +23,16 @@ class Union extends Struct {
1723
}
1824

1925
/**
20-
* A C++ union that is directly enclosed by a function.
26+
* A C/C++ union that is directly enclosed by a function. For example, the type
27+
* `MyLocalUnion` in:
28+
* ```
29+
* void myFunction() {
30+
* union MyLocalUnion {
31+
* int i;
32+
* float f;
33+
* };
34+
* }
35+
* ```
2136
*/
2237
class LocalUnion extends Union {
2338
LocalUnion() {
@@ -28,7 +43,16 @@ class LocalUnion extends Union {
2843
}
2944

3045
/**
31-
* A C++ nested union.
46+
* A C/C++ nested union. For example, the type `MyNestedUnion` in:
47+
* ```
48+
* class MyClass {
49+
* public:
50+
* union MyNestedUnion {
51+
* int i;
52+
* float f;
53+
* };
54+
* };
55+
* ```
3256
*/
3357
class NestedUnion extends Union {
3458
NestedUnion() {

cpp/ql/test/library-tests/structs/structs/structs.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,9 @@ void f(void) {
1010
l = s.i;
1111
}
1212

13+
void myFunction()
14+
{
15+
struct MyLocalStruct {
16+
int x, y, z;
17+
};
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
class MyClass
3+
{
4+
public:
5+
struct MyNestedStruct {
6+
int x, y, z;
7+
};
8+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1+
structs
2+
| structs.c:2:8:2:10 | foo | |
3+
| structs.c:15:10:15:22 | MyLocalStruct | LocalStruct |
4+
| structs.cpp:5:10:5:23 | MyNestedStruct | NestedStruct |
5+
assignments
16
| structs.c:10:5:10:11 | ... = ... | structs.c:10:5:10:5 | l | int | structs.c:10:11:10:11 | i | int |
Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,24 @@
11
import cpp
22

3-
from Assignment a
4-
select a,
5-
a.getLValue() as l,
6-
l.getType().explain(),
7-
a.getRValue() as r,
8-
r.getType().explain()
3+
string describe(Struct s)
4+
{
5+
(
6+
s instanceof LocalStruct and
7+
result = "LocalStruct"
8+
) or (
9+
s instanceof NestedStruct and
10+
result = "NestedStruct"
11+
)
12+
}
913

14+
query predicate structs(Struct s, string descStr) {
15+
s.fromSource() and
16+
descStr = concat(describe(s), ", ")
17+
}
18+
19+
query predicate assignments(Assignment a, Expr l, string explainL, Expr r, string explainR) {
20+
l = a.getLValue() and
21+
explainL = l.getType().explain() and
22+
r = a.getRValue() and
23+
explainR = r.getType().explain()
24+
}
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1-
| unions.cpp:4:8:4:12 | Entry | struct | |
2-
| unions.cpp:13:7:13:11 | Value | struct | union |
3-
| unions.cpp:19:8:19:22 | EntryWithMethod | struct | |
1+
| unions.cpp:4:8:4:12 | Entry | Struct | | operator= |
2+
| unions.cpp:13:7:13:11 | Value | Struct, Union | | operator= |
3+
| unions.cpp:19:8:19:22 | EntryWithMethod | Struct | Entry | getAsInt, operator= |
4+
| unions.cpp:27:9:27:20 | MyLocalUnion | LocalUnion, Struct, Union | | operator= |
5+
| unions.cpp:33:7:33:13 | MyClass | | | operator= |
6+
| unions.cpp:36:9:36:21 | MyNestedUnion | NestedUnion, Struct, Union | | operator= |
Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
11
import cpp
22

3-
from Class t, string struct, string union
4-
where if t instanceof Struct then struct = "struct" else struct = ""
5-
and if t instanceof Union then union = "union" else union = ""
6-
select t, struct, union
3+
string describe(Class c)
4+
{
5+
(
6+
c instanceof Struct and
7+
result = "Struct"
8+
) or (
9+
c instanceof Union and
10+
result = "Union"
11+
) or (
12+
c instanceof LocalUnion and
13+
result = "LocalUnion"
14+
) or (
15+
c instanceof NestedUnion and
16+
result = "NestedUnion"
17+
)
18+
}
19+
20+
from Class c
21+
select
22+
c,
23+
concat(describe(c), ", "),
24+
concat(c.getABaseClass().toString(), ", "),
25+
concat(c.getAMemberFunction().toString(), ", ")

cpp/ql/test/library-tests/unions/Unions4.expected

Lines changed: 0 additions & 3 deletions
This file was deleted.

cpp/ql/test/library-tests/unions/Unions4.ql

Lines changed: 0 additions & 4 deletions
This file was deleted.

0 commit comments

Comments
 (0)