Skip to content

Commit 6e5d56c

Browse files
authored
Merge pull request #5097 from geoffw0/qldoceg11
C++: QLDoc Improvements
2 parents 474ddc9 + 55b0dbd commit 6e5d56c

File tree

4 files changed

+147
-35
lines changed

4 files changed

+147
-35
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,12 @@ private predicate isFromUninstantiatedTemplateRec(Element e, Element template) {
270270
}
271271

272272
/**
273-
* A C++11 `static_assert` or C11 `_Static_assert` construct.
273+
* A C++11 `static_assert` or C11 `_Static_assert` construct. For example each
274+
* line in the following example contains a static assert:
275+
* ```
276+
* static_assert(sizeof(MyStruct) <= 4096);
277+
* static_assert(sizeof(MyStruct) <= 4096, "MyStruct is too big!");
278+
* ```
274279
*/
275280
class StaticAssert extends Locatable, @static_assert {
276281
override string toString() { result = "static_assert(..., \"" + getMessage() + "\")" }

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

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,21 @@ import semmle.code.cpp.Type
77
import semmle.code.cpp.metrics.MetricNamespace
88

99
/**
10-
* A C++ namespace.
10+
* A C++ namespace. For example the (single) namespace `A` in the following
11+
* code:
12+
* ```
13+
* namespace A
14+
* {
15+
* // ...
16+
* }
1117
*
18+
* // ...
19+
*
20+
* namespace A
21+
* {
22+
* // ...
23+
* }
24+
* ```
1225
* Note that namespaces are somewhat nebulous entities, as they do not in
1326
* general have a single well-defined location in the source code. The
1427
* related notion of a `NamespaceDeclarationEntry` is rather more concrete,
@@ -96,10 +109,22 @@ class Namespace extends NameQualifyingElement, @namespace {
96109
}
97110

98111
/**
99-
* A declaration of (part of) a C++ namespace.
112+
* A declaration of (part of) a C++ namespace. This corresponds to a single
113+
* `namespace N { ... }` occurrence in the source code. For example the two
114+
* mentions of `A` in the following code:
115+
* ```
116+
* namespace A
117+
* {
118+
* // ...
119+
* }
120+
*
121+
* // ...
100122
*
101-
* This corresponds to a single `namespace N { ... }` occurrence in the
102-
* source code.
123+
* namespace A
124+
* {
125+
* // ...
126+
* }
127+
* ```
103128
*/
104129
class NamespaceDeclarationEntry extends Locatable, @namespace_decl {
105130
/**
@@ -143,8 +168,9 @@ class UsingEntry extends Locatable, @using {
143168

144169
/**
145170
* A C++ `using` declaration. For example:
146-
*
147-
* `using std::string;`
171+
* ```
172+
* using std::string;
173+
* ```
148174
*/
149175
class UsingDeclarationEntry extends UsingEntry {
150176
UsingDeclarationEntry() {
@@ -162,8 +188,9 @@ class UsingDeclarationEntry extends UsingEntry {
162188

163189
/**
164190
* A C++ `using` directive. For example:
165-
*
166-
* `using namespace std;`
191+
* ```
192+
* using namespace std;
193+
* ```
167194
*/
168195
class UsingDirectiveEntry extends UsingEntry {
169196
UsingDirectiveEntry() {

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

Lines changed: 84 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,14 @@ import semmle.code.cpp.Location
22
import semmle.code.cpp.Element
33

44
/**
5-
* A C/C++ preprocessor directive.
6-
*
7-
* For example: `#ifdef`, `#line`, or `#pragma`.
5+
* A C/C++ preprocessor directive. For example each of the following lines of
6+
* code contains a `PreprocessorDirective`:
7+
* ```
8+
* #pragma once
9+
* #ifdef MYDEFINE
10+
* #include "myfile.h"
11+
* #line 1 "source.c"
12+
* ```
813
*/
914
class PreprocessorDirective extends Locatable, @preprocdirect {
1015
override string toString() { result = "Preprocessor directive" }
@@ -98,9 +103,9 @@ class PreprocessorBranchDirective extends PreprocessorDirective, TPreprocessorBr
98103
* A C/C++ preprocessor branching directive: `#if`, `#ifdef`, `#ifndef`, or
99104
* `#elif`.
100105
*
101-
* A branching directive can have its condition evaluated at compile-time,
102-
* and as a result, the preprocessor will either take the branch, or not
103-
* take the branch.
106+
* A branching directive has a condition and that condition may be evaluated
107+
* at compile-time. As a result, the preprocessor will either take the
108+
* branch, or not take the branch.
104109
*
105110
* However, there are also situations in which a branch's condition isn't
106111
* evaluated. The obvious case of this is when the directive is contained
@@ -136,8 +141,13 @@ class PreprocessorBranch extends PreprocessorBranchDirective, @ppd_branch {
136141
}
137142

138143
/**
139-
* A C/C++ preprocessor `#if` directive.
140-
*
144+
* A C/C++ preprocessor `#if` directive. For example there is a
145+
* `PreprocessorIf` on the first line of the following code:
146+
* ```
147+
* #if defined(MYDEFINE)
148+
* // ...
149+
* #endif
150+
* ```
141151
* For the related notion of a directive which causes branching (which
142152
* includes `#if`, plus also `#ifdef`, `#ifndef`, and `#elif`), see
143153
* `PreprocessorBranch`.
@@ -147,8 +157,13 @@ class PreprocessorIf extends PreprocessorBranch, @ppd_if {
147157
}
148158

149159
/**
150-
* A C/C++ preprocessor `#ifdef` directive.
151-
*
160+
* A C/C++ preprocessor `#ifdef` directive. For example there is a
161+
* `PreprocessorIfdef` on the first line of the following code:
162+
* ```
163+
* #ifdef MYDEFINE
164+
* // ...
165+
* #endif
166+
* ```
152167
* The syntax `#ifdef X` is shorthand for `#if defined(X)`.
153168
*/
154169
class PreprocessorIfdef extends PreprocessorBranch, @ppd_ifdef {
@@ -158,51 +173,94 @@ class PreprocessorIfdef extends PreprocessorBranch, @ppd_ifdef {
158173
}
159174

160175
/**
161-
* A C/C++ preprocessor `#ifndef` directive.
162-
*
176+
* A C/C++ preprocessor `#ifndef` directive. For example there is a
177+
* `PreprocessorIfndef` on the first line of the following code:
178+
* ```
179+
* #ifndef MYDEFINE
180+
* // ...
181+
* #endif
182+
* ```
163183
* The syntax `#ifndef X` is shorthand for `#if !defined(X)`.
164184
*/
165185
class PreprocessorIfndef extends PreprocessorBranch, @ppd_ifndef {
166186
override string toString() { result = "#ifndef " + this.getHead() }
167187
}
168188

169189
/**
170-
* A C/C++ preprocessor `#else` directive.
190+
* A C/C++ preprocessor `#else` directive. For example there is a
191+
* `PreprocessorElse` on the fifth line of the following code:
192+
* ```
193+
* #ifdef MYDEFINE1
194+
* // ...
195+
* #elif MYDEFINE2
196+
* // ...
197+
* #else
198+
* // ...
199+
* #endif
200+
* ```
171201
*/
172202
class PreprocessorElse extends PreprocessorBranchDirective, @ppd_else {
173203
override string toString() { result = "#else" }
174204
}
175205

176206
/**
177-
* A C/C++ preprocessor `#elif` directive.
207+
* A C/C++ preprocessor `#elif` directive. For example there is a
208+
* `PreprocessorElif` on the third line of the following code:
209+
* ```
210+
* #ifdef MYDEFINE1
211+
* // ...
212+
* #elif MYDEFINE2
213+
* // ...
214+
* #else
215+
* // ...
216+
* #endif
217+
* ```
178218
*/
179219
class PreprocessorElif extends PreprocessorBranch, @ppd_elif {
180220
override string toString() { result = "#elif " + this.getHead() }
181221
}
182222

183223
/**
184-
* A C/C++ preprocessor `#endif` directive.
224+
* A C/C++ preprocessor `#endif` directive. For example there is a
225+
* `PreprocessorEndif` on the third line of the following code:
226+
* ```
227+
* #ifdef MYDEFINE
228+
* // ...
229+
* #endif
230+
* ```
185231
*/
186232
class PreprocessorEndif extends PreprocessorBranchDirective, @ppd_endif {
187233
override string toString() { result = "#endif" }
188234
}
189235

190236
/**
191-
* A C/C++ preprocessor `#warning` directive.
237+
* A C/C++ preprocessor `#warning` directive. For example:
238+
* ```
239+
* #warning "This configuration is not supported."
240+
* ```
192241
*/
193242
class PreprocessorWarning extends PreprocessorDirective, @ppd_warning {
194243
override string toString() { result = "#warning " + this.getHead() }
195244
}
196245

197246
/**
198-
* A C/C++ preprocessor `#error` directive.
247+
* A C/C++ preprocessor `#error` directive. For example:
248+
* ```
249+
* #error "This configuration is not implemented."
250+
* ```
199251
*/
200252
class PreprocessorError extends PreprocessorDirective, @ppd_error {
201253
override string toString() { result = "#error " + this.getHead() }
202254
}
203255

204256
/**
205-
* A C/C++ preprocessor `#undef` directive.
257+
* A C/C++ preprocessor `#undef` directive. For example there is a
258+
* `PreprocessorUndef` on the second line of the following code:
259+
* ```
260+
* #ifdef MYMACRO
261+
* #undef MYMACRO
262+
* #endif
263+
* ```
206264
*/
207265
class PreprocessorUndef extends PreprocessorDirective, @ppd_undef {
208266
override string toString() { result = "#undef " + this.getHead() }
@@ -214,7 +272,10 @@ class PreprocessorUndef extends PreprocessorDirective, @ppd_undef {
214272
}
215273

216274
/**
217-
* A C/C++ preprocessor `#pragma` directive.
275+
* A C/C++ preprocessor `#pragma` directive. For example:
276+
* ```
277+
* #pragma once
278+
* ```
218279
*/
219280
class PreprocessorPragma extends PreprocessorDirective, @ppd_pragma {
220281
override string toString() {
@@ -223,7 +284,10 @@ class PreprocessorPragma extends PreprocessorDirective, @ppd_pragma {
223284
}
224285

225286
/**
226-
* A C/C++ preprocessor `#line` directive.
287+
* A C/C++ preprocessor `#line` directive. For example:
288+
* ```
289+
* #line 1 "source.c"
290+
* ```
227291
*/
228292
class PreprocessorLine extends PreprocessorDirective, @ppd_line {
229293
override string toString() { result = "#line " + this.getHead() }

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

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,11 @@ class StdAttribute extends Attribute, @stdattribute {
171171
}
172172

173173
/**
174-
* An attribute introduced by Microsoft's `__declspec(name)` syntax, for
175-
* example: `__declspec(dllimport)`.
174+
* An attribute introduced by Microsoft's `__declspec(name)` syntax. For
175+
* example the attribute on the following declaration:
176+
* ```
177+
* __declspec(dllimport) void myFunction();
178+
* ```
176179
*/
177180
class Declspec extends Attribute, @declspec { }
178181

@@ -186,8 +189,13 @@ class MicrosoftAttribute extends Attribute, @msattribute {
186189
}
187190

188191
/**
189-
* A C++11 `alignas` construct.
190-
*
192+
* A C++11 `alignas` construct. For example the attribute in the following
193+
* code:
194+
* ```
195+
* struct alignas(16) MyStruct {
196+
* int x;
197+
* };
198+
* ```
191199
* Though it doesn't use the attribute syntax, `alignas(...)` is presented
192200
* as an `Attribute` for consistency with the `[[align(...)]]` attribute.
193201
*/
@@ -197,7 +205,11 @@ class AlignAs extends Attribute, @alignas {
197205

198206
/**
199207
* A GNU `format` attribute of the form `__attribute__((format(archetype, format-index, first-arg)))`
200-
* that declares a function to accept a `printf` style format string.
208+
* that declares a function to accept a `printf` style format string. For example the attribute
209+
* on the following declaration:
210+
* ```
211+
* int myPrintf(const char *format, ...) __attribute__((format(printf, 1, 2)));
212+
* ```
201213
*/
202214
class FormatAttribute extends GnuAttribute {
203215
FormatAttribute() { getName() = "format" }
@@ -242,7 +254,11 @@ class FormatAttribute extends GnuAttribute {
242254
}
243255

244256
/**
245-
* An argument to an `Attribute`.
257+
* An argument to an `Attribute`. For example the argument "dllimport" on the
258+
* attribute in the following code:
259+
* ```
260+
* __declspec(dllimport) void myFunction();
261+
* ```
246262
*/
247263
class AttributeArgument extends Element, @attribute_arg {
248264
/**

0 commit comments

Comments
 (0)