Skip to content

Commit 500097c

Browse files
committed
CPP: Examples Preprocessor.qll.
1 parent 4b9532c commit 500097c

1 file changed

Lines changed: 84 additions & 20 deletions

File tree

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() }

0 commit comments

Comments
 (0)