Skip to content

Commit 34dfbb0

Browse files
authored
[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 ```
1 parent dc6a915 commit 34dfbb0

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ Bug Fixes in This Version
366366
- Fixed an assertion when an improper use of the ``malloc`` attribute targeting
367367
a function without arguments caused us to try to access a non-existent argument.
368368
(#GH159080)
369+
- Fixed a failed assertion with empty filename arguments in ``__has_embed``. (#GH159898)
369370

370371
Bug Fixes to Compiler Builtins
371372
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Lex/PPMacroExpansion.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1282,11 +1282,13 @@ EmbedResult Preprocessor::EvaluateHasEmbed(Token &Tok, IdentifierInfo *II) {
12821282

12831283
SmallString<128> FilenameBuffer;
12841284
StringRef Filename = this->getSpelling(FilenameTok, FilenameBuffer);
1285+
if (Filename.empty())
1286+
return EmbedResult::Empty;
1287+
12851288
bool isAngled =
12861289
this->GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
12871290
// If GetIncludeFilenameSpelling set the start ptr to null, there was an
12881291
// error.
1289-
assert(!Filename.empty());
12901292
const FileEntry *LookupFromFile =
12911293
this->getCurrentFileLexer() ? *this->getCurrentFileLexer()->getFileEntry()
12921294
: static_cast<FileEntry *>(nullptr);

clang/test/Preprocessor/embed___has_embed_parsing_errors.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,3 +247,6 @@
247247
expected-error@+2 {{expected value in expression}}
248248
#if __has_embed (__FILE__ limit(-100000000000000000)) != __STDC_EMBED_NOT_FOUND__
249249
#endif
250+
251+
#if __has_embed("") // expected-error {{empty filename}}
252+
#endif

0 commit comments

Comments
 (0)