[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
```
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6daf2ab..fb429a6 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -366,6 +366,7 @@
- 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 6f12ac8..dec1956 100644
--- a/clang/lib/Lex/PPMacroExpansion.cpp
+++ b/clang/lib/Lex/PPMacroExpansion.cpp
@@ -1282,11 +1282,13 @@
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 0591c59..9c512a4 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