Skip to content

Commit 2e89b71

Browse files
authored
[C23] Correctly handle missing embed with -MG (#166188)
-MG is supposed to suppress "file not found" diagnostics and instead treat those as generated files for purposes of dependency scanning. Clang was previously emitting the diagnostic instead of emitting the name of the embedded file. Fixes #165632
1 parent f771f1e commit 2e89b71

File tree

4 files changed

+17
-2
lines changed

4 files changed

+17
-2
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ C23 Feature Support
209209
`WG14 N2710 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2710.htm>`_.
210210
- Fixed accepting as compatible unnamed tag types with the same fields within
211211
the same translation unit but from different types.
212+
- ``-MG`` now silences the "file not found" errors with ``#embed`` when
213+
scanning for dependencies and encountering an unknown file. #GH165632
212214

213215
Non-comprehensive list of changes in this release
214216
-------------------------------------------------

clang/lib/Frontend/DependencyFile.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,17 @@ struct DepCollectorPPCallbacks : public PPCallbacks {
7575
/*IsMissing*/ false);
7676
}
7777

78+
bool EmbedFileNotFound(StringRef FileName) override {
79+
DepCollector.maybeAddDependency(
80+
llvm::sys::path::remove_leading_dotslash(FileName),
81+
/*FromModule=*/false,
82+
/*IsSystem=*/false,
83+
/*IsModuleFile=*/false,
84+
/*IsMissing=*/true);
85+
// Return true to silence the file not found diagnostic.
86+
return true;
87+
}
88+
7889
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
7990
StringRef FileName, bool IsAngled,
8091
CharSourceRange FilenameRange,

clang/lib/Lex/PPDirectives.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4018,7 +4018,7 @@ void Preprocessor::HandleEmbedDirective(SourceLocation HashLoc, Token &EmbedTok,
40184018
this->LookupEmbedFile(Filename, isAngled, true, LookupFromFile);
40194019
if (!MaybeFileRef) {
40204020
// could not find file
4021-
if (Callbacks && Callbacks->EmbedFileNotFound(OriginalFilename)) {
4021+
if (Callbacks && Callbacks->EmbedFileNotFound(Filename)) {
40224022
return;
40234023
}
40244024
Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;

clang/test/Driver/mg.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
// RUN: %clang -M -MG -include nonexistent-preinclude.h %s | FileCheck %s
1+
// RUN: %clang -M -MG -include nonexistent-preinclude.h -std=c23 %s | FileCheck %s
22
// CHECK: nonexistent-preinclude.h
33
// CHECK: nonexistent-ppinclude.h
4+
// CHECK: nonexistent-embed
45

56
#include "nonexistent-ppinclude.h"
7+
#embed "nonexistent-embed"

0 commit comments

Comments
 (0)