https://github.com/a-tarasyuk created
https://github.com/llvm/llvm-project/pull/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
```
>From 26d565a311f0d0c5de2675e6d1bd2dd2a27e6064 Mon Sep 17 00:00:00 2001
From: Oleksandr Tarasiuk <[email protected]>
Date: Sat, 20 Sep 2025 16:06:50 +0300
Subject: [PATCH] [Clang]: prevent assertion on empty filename arg in
__has_embed
---
clang/docs/ReleaseNotes.rst | 1 +
clang/lib/Lex/PPMacroExpansion.cpp | 4 +++-
clang/test/Preprocessor/embed___has_embed_parsing_errors.c | 3 +++
3 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 46d56bb3f07f5..b229960eff65c 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@+2 {{expected value in expression}}
#if __has_embed (__FILE__ limit(-100000000000000000)) !=
__STDC_EMBED_NOT_FOUND__
#endif
+
+#if __has_embed("") // expected-error {{empty filename}}
+#endif
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits