Skip to content

Commit e704406

Browse files
committed
C++: Support C23/C++26 #embed preprocessor directives
1 parent f8f5094 commit e704406

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: feature
3+
---
4+
* Added a subclass `Embed` of `PreprocessorDirective` for C23 and C++26 `#embed` preprocessor directives.

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,27 @@ class PreprocessorPragma extends PreprocessorDirective, @ppd_pragma {
328328
class PreprocessorLine extends PreprocessorDirective, @ppd_line {
329329
override string toString() { result = "#line " + this.getHead() }
330330
}
331+
332+
/**
333+
* A C23 or C++26`#embed` preprocessor directive. For example, the following code
334+
* contains one `Embed` directive:
335+
* ```cpp
336+
* char arr[] = {
337+
* #embed "bin"
338+
* };
339+
* ```
340+
*/
341+
class Embed extends PreprocessorDirective, @ppd_embed {
342+
override string toString() { result = "#embed " + this.getIncludeText() }
343+
344+
/**
345+
* Gets the token which occurs after `#embed`, for example `"filename"`
346+
* or `<filename>`.
347+
*/
348+
string getIncludeText() { result = this.getHead() }
349+
350+
/**
351+
* Gets the file directly embedded by this `#embed`.
352+
*/
353+
File getEmbeddedFile() { embeds(underlyingElement(this), unresolveElement(result)) }
354+
}

cpp/ql/lib/semmlecode.cpp.dbscheme

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2353,6 +2353,7 @@ case @preprocdirect.kind of
23532353
| 14 = @ppd_ms_import
23542354
| 15 = @ppd_elifdef
23552355
| 16 = @ppd_elifndef
2356+
| 17 = @ppd_embed
23562357
| 18 = @ppd_warning
23572358
;
23582359

@@ -2379,6 +2380,11 @@ includes(
23792380
int included: @file ref
23802381
);
23812382

2383+
embeds(
2384+
unique int id: @ppd_embed ref,
2385+
int included: @file ref
2386+
);
2387+
23822388
link_targets(
23832389
int id: @link_target,
23842390
int binary: @file ref

0 commit comments

Comments
 (0)