https://github.com/AaronBallman updated 
https://github.com/llvm/llvm-project/pull/166188

>From e89037e8d5c5ec944a51a6ab9c45cac656d2c63e Mon Sep 17 00:00:00 2001
From: Aaron Ballman <[email protected]>
Date: Mon, 3 Nov 2025 11:16:24 -0500
Subject: [PATCH 1/4] [C23] Correctly handle missing embed with -MG

-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
---
 clang/docs/ReleaseNotes.rst           |  2 ++
 clang/lib/Frontend/DependencyFile.cpp | 11 +++++++++++
 clang/lib/Lex/PPDirectives.cpp        |  2 +-
 clang/test/Driver/mg.c                |  4 +++-
 4 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 6959e61cac980..cbf1c1922c59f 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -209,6 +209,8 @@ C23 Feature Support
   `WG14 N2710 <https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2710.htm>`_.
 - Fixed accepting as compatible unnamed tag types with the same fields within
   the same translation unit but from different types.
+- ``-MG`` now silences the "file not found" errors with ``#embed`` when
+  scanning for dependencies and encountering an unknown file. #GH165632
 
 Non-comprehensive list of changes in this release
 -------------------------------------------------
diff --git a/clang/lib/Frontend/DependencyFile.cpp 
b/clang/lib/Frontend/DependencyFile.cpp
index 15fa7de35df97..d65740a03acd3 100644
--- a/clang/lib/Frontend/DependencyFile.cpp
+++ b/clang/lib/Frontend/DependencyFile.cpp
@@ -75,6 +75,17 @@ struct DepCollectorPPCallbacks : public PPCallbacks {
                                     /*IsMissing*/ false);
   }
 
+  bool EmbedFileNotFound(StringRef FileName) override {
+    DepCollector.maybeAddDependency(
+        llvm::sys::path::remove_leading_dotslash(FileName),
+        /*FromModule*/ false,
+        /*IsSystem*/ false,
+        /*IsModuleFile*/ false,
+        /*IsMissing*/ false);
+    // Return true to silence the file not found diagnostic.
+    return true;
+  }
+
   void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
                           StringRef FileName, bool IsAngled,
                           CharSourceRange FilenameRange,
diff --git a/clang/lib/Lex/PPDirectives.cpp b/clang/lib/Lex/PPDirectives.cpp
index 6a5e5d4bad3a6..891c8ab7f3155 100644
--- a/clang/lib/Lex/PPDirectives.cpp
+++ b/clang/lib/Lex/PPDirectives.cpp
@@ -4018,7 +4018,7 @@ void Preprocessor::HandleEmbedDirective(SourceLocation 
HashLoc, Token &EmbedTok,
       this->LookupEmbedFile(Filename, isAngled, true, LookupFromFile);
   if (!MaybeFileRef) {
     // could not find file
-    if (Callbacks && Callbacks->EmbedFileNotFound(OriginalFilename)) {
+    if (Callbacks && Callbacks->EmbedFileNotFound(Filename)) {
       return;
     }
     Diag(FilenameTok, diag::err_pp_file_not_found) << Filename;
diff --git a/clang/test/Driver/mg.c b/clang/test/Driver/mg.c
index 82d8a6084e5e0..b7458a08698d3 100644
--- a/clang/test/Driver/mg.c
+++ b/clang/test/Driver/mg.c
@@ -1,5 +1,7 @@
-// RUN: %clang -M -MG -include nonexistent-preinclude.h %s | FileCheck %s
+// RUN: %clang -M -MG -include nonexistent-preinclude.h -std=c23 %s | 
FileCheck %s
 // CHECK: nonexistent-preinclude.h
 // CHECK: nonexistent-ppinclude.h
+// CHECK: nonexistent-embed
 
 #include "nonexistent-ppinclude.h"
+#embed "nonexistent-embed"

>From 150c625f5cdd04e8fa55ba61e7ec08681aa33c70 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <[email protected]>
Date: Mon, 3 Nov 2025 12:18:09 -0500
Subject: [PATCH 2/4] IsMissing should be true, not false; NFC

This is an NFC change because that parameter is unused by the
DependencyCollector currently; there's no way to test for the change.
---
 clang/lib/Frontend/DependencyFile.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Frontend/DependencyFile.cpp 
b/clang/lib/Frontend/DependencyFile.cpp
index d65740a03acd3..0890a9a6cca7e 100644
--- a/clang/lib/Frontend/DependencyFile.cpp
+++ b/clang/lib/Frontend/DependencyFile.cpp
@@ -81,7 +81,7 @@ struct DepCollectorPPCallbacks : public PPCallbacks {
         /*FromModule*/ false,
         /*IsSystem*/ false,
         /*IsModuleFile*/ false,
-        /*IsMissing*/ false);
+        /*IsMissing*/ true);
     // Return true to silence the file not found diagnostic.
     return true;
   }

>From d103fcc32fc962d175defd0613a9cf3c718d9cb4 Mon Sep 17 00:00:00 2001
From: Aaron Ballman <[email protected]>
Date: Mon, 3 Nov 2025 12:54:27 -0500
Subject: [PATCH 3/4] Now with prettier comments; NFC

---
 clang/lib/Frontend/DependencyFile.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Frontend/DependencyFile.cpp 
b/clang/lib/Frontend/DependencyFile.cpp
index 0890a9a6cca7e..313ad5fb903e4 100644
--- a/clang/lib/Frontend/DependencyFile.cpp
+++ b/clang/lib/Frontend/DependencyFile.cpp
@@ -78,10 +78,10 @@ struct DepCollectorPPCallbacks : public PPCallbacks {
   bool EmbedFileNotFound(StringRef FileName) override {
     DepCollector.maybeAddDependency(
         llvm::sys::path::remove_leading_dotslash(FileName),
-        /*FromModule*/ false,
-        /*IsSystem*/ false,
-        /*IsModuleFile*/ false,
-        /*IsMissing*/ true);
+        /*FromModule=*/ false,
+        /*IsSystem=*/ false,
+        /*IsModuleFile=*/ false,
+        /*IsMissing=*/ true);
     // Return true to silence the file not found diagnostic.
     return true;
   }

>From 84be70f4e365c0289490c90faa2a32bffacaa6ed Mon Sep 17 00:00:00 2001
From: Aaron Ballman <[email protected]>
Date: Mon, 3 Nov 2025 13:04:58 -0500
Subject: [PATCH 4/4] Appease formatting bot; NFC

---
 clang/lib/Frontend/DependencyFile.cpp | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Frontend/DependencyFile.cpp 
b/clang/lib/Frontend/DependencyFile.cpp
index 313ad5fb903e4..93e012b163878 100644
--- a/clang/lib/Frontend/DependencyFile.cpp
+++ b/clang/lib/Frontend/DependencyFile.cpp
@@ -78,10 +78,10 @@ struct DepCollectorPPCallbacks : public PPCallbacks {
   bool EmbedFileNotFound(StringRef FileName) override {
     DepCollector.maybeAddDependency(
         llvm::sys::path::remove_leading_dotslash(FileName),
-        /*FromModule=*/ false,
-        /*IsSystem=*/ false,
-        /*IsModuleFile=*/ false,
-        /*IsMissing=*/ true);
+        /*FromModule=*/false,
+        /*IsSystem=*/false,
+        /*IsModuleFile=*/false,
+        /*IsMissing=*/true);
     // Return true to silence the file not found diagnostic.
     return true;
   }

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to