Skip to content

Conversation

a-tarasyuk
Copy link
Member

Fixes #162951


This PR addresses the issue of Clang asserting when processing #embed with an empty filename

#embed <>

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Oct 12, 2025
@a-tarasyuk a-tarasyuk requested a review from Fznamznon October 12, 2025 14:11
@llvmbot
Copy link
Member

llvmbot commented Oct 12, 2025

@llvm/pr-subscribers-clang

Author: Oleksandr T. (a-tarasyuk)

Changes

Fixes #162951


This PR addresses the issue of Clang asserting when processing #embed with an empty filename

#embed &lt;&gt;

Full diff: https://github.com/llvm/llvm-project/pull/163072.diff

3 Files Affected:

  • (modified) clang/docs/ReleaseNotes.rst (+1)
  • (modified) clang/lib/Lex/PPDirectives.cpp (+4-1)
  • (added) clang/test/Preprocessor/embed_empty_file.c (+3)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 65b086caf3652..9d2a055b21994 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -405,6 +405,7 @@ Bug Fixes in This Version
   a function without arguments caused us to try to access a non-existent argument.
   (#GH159080)
 - Fixed a failed assertion with empty filename arguments in ``__has_embed``. (#GH159898)
+- Fixed a failed assertion with empty filename in ``#embed`` directive. (#GH162951)
 
 Bug Fixes to Compiler Builtins
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 360593d0f33df..5c6ecdbc304d6 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -3991,9 +3991,12 @@ void Preprocessor::HandleEmbedDirective(SourceLocation HashLoc, Token &EmbedTok,
   StringRef OriginalFilename = Filename;
   bool isAngled =
       GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
+
   // If GetIncludeFilenameSpelling set the start ptr to null, there was an
   // error.
-  assert(!Filename.empty());
+  if (Filename.empty())
+    return;
+
   OptionalFileEntryRef MaybeFileRef =
       this->LookupEmbedFile(Filename, isAngled, true, LookupFromFile);
   if (!MaybeFileRef) {
diff --git a/clang/test/Preprocessor/embed_empty_file.c b/clang/test/Preprocessor/embed_empty_file.c
new file mode 100644
index 0000000000000..dd73e72b0a488
--- /dev/null
+++ b/clang/test/Preprocessor/embed_empty_file.c
@@ -0,0 +1,3 @@
+// RUN: %clang_cc1 -std=c23 %s -E -verify
+
+#embed <> // expected-error {{empty filename}}

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tbaederr tbaederr added the embed #embed (not embedded systems) label Oct 13, 2025
@a-tarasyuk a-tarasyuk requested a review from Fznamznon October 13, 2025 10:46
Copy link
Contributor

@Fznamznon Fznamznon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@a-tarasyuk a-tarasyuk merged commit 32fffe5 into llvm:main Oct 13, 2025
11 checks passed
akadutta pushed a commit to akadutta/llvm-project that referenced this pull request Oct 14, 2025
…#163072)

Fixes llvm#162951

---

This PR addresses the issue of Clang asserting when processing `#embed`
with an empty filename

```c
#embed <>
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category embed #embed (not embedded systems)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang] Assertion `!Filename.empty()' failed.

5 participants