[clang] 34dfbb0 - [Clang]: prevent assertion on empty filename arg in __has_embed (#159928)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Sep 22 02:49:34 PDT 2025
Author: Oleksandr T.
Date: 2025-09-22T12:49:29+03:00
New Revision: 34dfbb0ec98c205bc1165a1e58c686a0159ca23c
URL: https://github.com/llvm/llvm-project/commit/34dfbb0ec98c205bc1165a1e58c686a0159ca23c
DIFF: https://github.com/llvm/llvm-project/commit/34dfbb0ec98c205bc1165a1e58c686a0159ca23c.diff
LOG: [Clang]: prevent assertion on empty filename arg in __has_embed (#159928)
Fixes #159898
---
This PR addresses the issue of Clang asserting when `__has_embed` is
used with an empty filename
```c
#if __has_embed("")
#endif
```
Added:
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/Lex/PPMacroExpansion.cpp
clang/test/Preprocessor/embed___has_embed_parsing_errors.c
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6daf2ab23bbf2..fb429a675476d 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -366,6 +366,7 @@ Bug Fixes in This Version
- Fixed an assertion when an improper use of the ``malloc`` attribute targeting
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)
Bug Fixes to Compiler Builtins
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Lex/PPMacroExpansion.cpp b/clang/lib/Lex/PPMacroExpansion.cpp
index 6f12ac80d677e..dec1956ea0f9a 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1282,11 +1282,13 @@ EmbedResult Preprocessor::EvaluateHasEmbed(Token &Tok, IdentifierInfo *II) {
SmallString<128> FilenameBuffer;
StringRef Filename = this->getSpelling(FilenameTok, FilenameBuffer);
+ if (Filename.empty())
+ return EmbedResult::Empty;
+
bool isAngled =
this->GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
// If GetIncludeFilenameSpelling set the start ptr to null, there was an
// error.
- assert(!Filename.empty());
const FileEntry *LookupFromFile =
this->getCurrentFileLexer() ? *this->getCurrentFileLexer()->getFileEntry()
: static_cast<FileEntry *>(nullptr);
diff --git a/clang/test/Preprocessor/embed___has_embed_parsing_errors.c b/clang/test/Preprocessor/embed___has_embed_parsing_errors.c
index 0591c595253bc..9c512a4882e2d 100644
--- a/clang/test/Preprocessor/embed___has_embed_parsing_errors.c
+++ b/clang/test/Preprocessor/embed___has_embed_parsing_errors.c
@@ -247,3 +247,6 @@
expected-error at +2 {{expected value in expression}}
#if __has_embed (__FILE__ limit(-100000000000000000)) != __STDC_EMBED_NOT_FOUND__
#endif
+
+#if __has_embed("") // expected-error {{empty filename}}
+#endif
More information about the cfe-commits
mailing list