Skip to content

Commit 4543aaf

Browse files
committed
CPP: Examples Access.qll.
1 parent 8896fa5 commit 4543aaf

File tree

1 file changed

+111
-18
lines changed

1 file changed

+111
-18
lines changed

cpp/ql/src/semmle/code/cpp/exprs/Access.qll

Lines changed: 111 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,18 @@ abstract class Access extends Expr, NameQualifiableElement {
1818
}
1919

2020
/**
21-
* A C/C++ enum constant access expression.
21+
* A C/C++ enum constant access expression. For example the access to
22+
* `MYENUMCONST1` in `myFunction` in the following code:
23+
* ```
24+
* enum MyEnum {
25+
* MYENUMCONST1,
26+
* MYENUMCONST2
27+
* };
28+
*
29+
* void myFunction() {
30+
* MyEnum v = MYENUMCONST1;
31+
* };
32+
* ```
2233
*/
2334
class EnumConstantAccess extends Access, @varaccess {
2435
override string getCanonicalQLClass() { result = "EnumConstantAccess" }
@@ -35,7 +46,15 @@ class EnumConstantAccess extends Access, @varaccess {
3546
}
3647

3748
/**
38-
* A C/C++ variable access expression.
49+
* A C/C++ variable access expression. For example the accesses to
50+
* `x` and `y` in `myFunction` in the following code:
51+
* ```
52+
* int x;
53+
*
54+
* void myFunction(int y) {
55+
* x = y;
56+
* };
57+
* ```
3958
*/
4059
class VariableAccess extends Access, @varaccess {
4160
override string getCanonicalQLClass() { result = "VariableAccess" }
@@ -129,7 +148,18 @@ class VariableAccess extends Access, @varaccess {
129148
}
130149

131150
/**
132-
* A C/C++ field access expression.
151+
* A C/C++ field access expression. For example the accesses to
152+
* `x` and `y` in `myMethod` in the following code:
153+
* ```
154+
* class MyClass {
155+
* public:
156+
* void myMethod(MyClass &other) {
157+
* x = other.y;
158+
* }
159+
*
160+
* int x, y;
161+
* };
162+
* ```
133163
*/
134164
class FieldAccess extends VariableAccess {
135165
override string getCanonicalQLClass() { result = "FieldAccess" }
@@ -141,8 +171,20 @@ class FieldAccess extends VariableAccess {
141171
}
142172

143173
/**
144-
* A field access of the form `obj->field`. The type of `obj` is a pointer,
145-
* so this is equivalent to `(*obj).field`.
174+
* A field access whose qualifier is a pointer to a class, struct or union.
175+
* These typically take the form `obj->field`, but also includes many accesses
176+
* with an implicit `this->` qualifier. For example the accesses to `x` and
177+
* `y` in `myMethod` in the following code:
178+
* ```
179+
* class MyClass {
180+
* public:
181+
* void myMethod(MyClass *other) {
182+
other->x = y;
183+
* }
184+
*
185+
* int x, y;
186+
* };
187+
* ```
146188
*/
147189
class PointerFieldAccess extends FieldAccess {
148190
override string getCanonicalQLClass() { result = "PointerFieldAccess" }
@@ -169,7 +211,18 @@ class DotFieldAccess extends FieldAccess {
169211

170212
/**
171213
* A field access of the form `obj.field`, where the type of `obj` is a
172-
* reference to a class/struct/union.
214+
* reference to a class/struct/union. For example the accesses to `y` in
215+
* `myMethod` in the following code:
216+
* ```
217+
* class MyClass {
218+
* public:
219+
* void myMethod(MyClass a, MyClass &b) {
220+
* a.x = b.y;
221+
* }
222+
*
223+
* int x, y;
224+
* };
225+
* ```
173226
*/
174227
class ReferenceFieldAccess extends DotFieldAccess {
175228
override string getCanonicalQLClass() { result = "ReferenceFieldAccess" }
@@ -179,7 +232,18 @@ class ReferenceFieldAccess extends DotFieldAccess {
179232

180233
/**
181234
* A field access of the form `obj.field`, where the type of `obj` is a
182-
* class/struct/union (and not a reference).
235+
* class/struct/union (and not a reference). For example the accesses to `x`
236+
* in `myMethod` in the following code:
237+
* ```
238+
* class MyClass {
239+
* public:
240+
* void myMethod(MyClass a, MyClass &b) {
241+
* a.x = b.y;
242+
* }
243+
*
244+
* int x, y;
245+
* };
246+
* ```
183247
*/
184248
class ValueFieldAccess extends DotFieldAccess {
185249
override string getCanonicalQLClass() { result = "ValueFieldAccess" }
@@ -198,20 +262,18 @@ private predicate referenceConversion(Conversion c) {
198262
/**
199263
* Holds if `e` is a reference expression (that is, it has a type of the
200264
* form `T&`), which is converted to a value. For example:
201-
*
202265
* ```
203266
* int myfcn(MyStruct &x) {
204267
* return x.field;
205268
* }
206269
* ```
207-
*
208270
* In this example, the type of `x` is `MyStruct&`, but it gets implicitly
209271
* converted to `MyStruct` in the expression `x.field`.
210272
*/
211273
private predicate exprHasReferenceConversion(Expr e) { referenceConversion(e.getConversion+()) }
212274

213275
/**
214-
* A field access of a field of `this`. The access has no qualifier because
276+
* A field access of a field of `this` which has no qualifier because
215277
* the use of `this` is implicit. For example, `field` is equivalent to
216278
* `this->field` if `field` is a member of `this`.
217279
*
@@ -250,7 +312,15 @@ class PointerToFieldLiteral extends ImplicitThisFieldAccess {
250312
}
251313

252314
/**
253-
* A C/C++ function access expression.
315+
* A C/C++ function access expression. For example the access to
316+
* `myFunctionTarget` in `myFunction` in the following code:
317+
* ```
318+
* int myFunctionTarget(int);
319+
*
320+
* void myFunction() {
321+
* int (*myFunctionPointer)(int) = &myTarget;
322+
* }
323+
* ```
254324
*/
255325
class FunctionAccess extends Access, @routineexpr {
256326
FunctionAccess() { not iscall(underlyingElement(this), _) }
@@ -269,7 +339,7 @@ class FunctionAccess extends Access, @routineexpr {
269339
}
270340

271341
/**
272-
* An access to a parameter of a function signature for the purposes of a decltype.
342+
* An access to a parameter of a function signature for the purposes of a `decltype`.
273343
*
274344
* For example, given the following code:
275345
* ```
@@ -279,15 +349,30 @@ class FunctionAccess extends Access, @routineexpr {
279349
* }
280350
* ```
281351
* The return type of the function is a decltype, the expression of which contains
282-
* an add expression, which in turn has two ParamAccessForType children.
352+
* an add expression, which in turn has two `ParamAccessForType` children.
283353
*/
284354
class ParamAccessForType extends Expr, @param_ref {
285355
override string toString() { result = "param access" }
286356
}
287357

288358
/**
289359
* An access to a type. This occurs in certain contexts where a built-in
290-
* works on types directly rather than variables, expressions etc.
360+
* works on types directly rather than variables, expressions etc. For
361+
* example the reference to `MyClass` in `__is_pod` in the following code:
362+
* ```
363+
* class MyClass {
364+
* ...
365+
* };
366+
*
367+
* void myFunction() {
368+
* if (__is_pod(MyClass))
369+
* {
370+
* ...
371+
* } else {
372+
* ...
373+
* }
374+
* }
375+
* ```
291376
*/
292377
class TypeName extends Expr, @type_operand {
293378
override string getCanonicalQLClass() { result = "TypeName" }
@@ -296,24 +381,32 @@ class TypeName extends Expr, @type_operand {
296381
}
297382

298383
/**
299-
* A C/C++ array access expression.
384+
* A C/C++ array access expression. For example, the access to `as` in
385+
* `myFunction` in the following code:
386+
* ```
387+
* int as[10];
300388
*
301-
* For calls to operator[], which look syntactically identical, see OverloadedArrayExpr.
389+
* void myFunction() {
390+
* as[0]++;
391+
* }
392+
* ```
393+
* For calls to `operator[]`, which look syntactically identical, see
394+
* `OverloadedArrayExpr`.
302395
*/
303396
class ArrayExpr extends Expr, @subscriptexpr {
304397
override string getCanonicalQLClass() { result = "ArrayExpr" }
305398

306399
/**
307400
* Gets the array or pointer expression being subscripted.
308401
*
309-
* This is arr in both arr[0] and 0[arr].
402+
* This is `arr` in both `arr[0]` and `0[arr]`.
310403
*/
311404
Expr getArrayBase() { result = this.getChild(0) }
312405

313406
/**
314407
* Gets the expression giving the index into the array.
315408
*
316-
* This is 0 in both arr[0] and 0[arr].
409+
* This is `0` in both `arr[0]` and `0[arr]`.
317410
*/
318411
Expr getArrayOffset() { result = this.getChild(1) }
319412

0 commit comments

Comments
 (0)