From e7044061fb38af8b2c3299327554e94703113e93 Mon Sep 17 00:00:00 2001 From: Jeroen Ketema Date: Mon, 19 Jan 2026 15:46:03 +0100 Subject: [PATCH] C++: Support C23/C++26 `#embed` preprocessor directives --- cpp/ql/lib/change-notes/2026-01-19-embed.md | 4 ++++ cpp/ql/lib/semmle/code/cpp/Preprocessor.qll | 24 +++++++++++++++++++++ cpp/ql/lib/semmlecode.cpp.dbscheme | 6 ++++++ 3 files changed, 34 insertions(+) create mode 100644 cpp/ql/lib/change-notes/2026-01-19-embed.md diff --git a/cpp/ql/lib/change-notes/2026-01-19-embed.md b/cpp/ql/lib/change-notes/2026-01-19-embed.md new file mode 100644 index 00000000000..26d495277b5 --- /dev/null +++ b/cpp/ql/lib/change-notes/2026-01-19-embed.md @@ -0,0 +1,4 @@ +--- +category: feature +--- +* Added a subclass `Embed` of `PreprocessorDirective` for C23 and C++26 `#embed` preprocessor directives. diff --git a/cpp/ql/lib/semmle/code/cpp/Preprocessor.qll b/cpp/ql/lib/semmle/code/cpp/Preprocessor.qll index 6a57f0bcae7..726670c81a6 100644 --- a/cpp/ql/lib/semmle/code/cpp/Preprocessor.qll +++ b/cpp/ql/lib/semmle/code/cpp/Preprocessor.qll @@ -328,3 +328,27 @@ class PreprocessorPragma extends PreprocessorDirective, @ppd_pragma { class PreprocessorLine extends PreprocessorDirective, @ppd_line { override string toString() { result = "#line " + this.getHead() } } + +/** + * A C23 or C++26`#embed` preprocessor directive. For example, the following code + * contains one `Embed` directive: + * ```cpp + * char arr[] = { + * #embed "bin" + * }; + * ``` + */ +class Embed extends PreprocessorDirective, @ppd_embed { + override string toString() { result = "#embed " + this.getIncludeText() } + + /** + * Gets the token which occurs after `#embed`, for example `"filename"` + * or ``. + */ + string getIncludeText() { result = this.getHead() } + + /** + * Gets the file directly embedded by this `#embed`. + */ + File getEmbeddedFile() { embeds(underlyingElement(this), unresolveElement(result)) } +} diff --git a/cpp/ql/lib/semmlecode.cpp.dbscheme b/cpp/ql/lib/semmlecode.cpp.dbscheme index 83100310bf7..9439176c1d1 100644 --- a/cpp/ql/lib/semmlecode.cpp.dbscheme +++ b/cpp/ql/lib/semmlecode.cpp.dbscheme @@ -2353,6 +2353,7 @@ case @preprocdirect.kind of | 14 = @ppd_ms_import | 15 = @ppd_elifdef | 16 = @ppd_elifndef +| 17 = @ppd_embed | 18 = @ppd_warning ; @@ -2379,6 +2380,11 @@ includes( int included: @file ref ); +embeds( + unique int id: @ppd_embed ref, + int included: @file ref +); + link_targets( int id: @link_target, int binary: @file ref