Author: macurtis-amd
Date: 2025-06-10T11:54:50-05:00
New Revision: 2ddf0caaed192495cac99e703cef2fe50191cf49

URL: 
https://github.com/llvm/llvm-project/commit/2ddf0caaed192495cac99e703cef2fe50191cf49
DIFF: 
https://github.com/llvm/llvm-project/commit/2ddf0caaed192495cac99e703cef2fe50191cf49.diff

LOG: [clang][driver] Suppress gnu-line-marker when saving temps (#134621)

When passing `-save-temps` to clang, the generated preprocessed output
uses gnu line markers. This unexpectedly triggers gnu-line-marker
warnings when used with `-Weverything` or `-pedantic`. Even worse,
compilation fails if `-Werror` is used.

This change suppresses gnu-line-marker warnings when invoking clang with
input from a preprocessor job and the user has not otherwise explictly
specified `-Wgnu-line-marker` somewhere on the command line. Note that
this does apply to user provided preprocessed files.

fixes #63802

Added: 
    clang/test/Preprocessor/line-directive-suppressed.c

Modified: 
    clang/lib/Frontend/InitPreprocessor.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Frontend/InitPreprocessor.cpp 
b/clang/lib/Frontend/InitPreprocessor.cpp
index 69a91eef6aedb..f64613fb4a6cb 100644
--- a/clang/lib/Frontend/InitPreprocessor.cpp
+++ b/clang/lib/Frontend/InitPreprocessor.cpp
@@ -10,6 +10,7 @@
 //
 
//===----------------------------------------------------------------------===//
 
+#include "clang/Basic/DiagnosticLex.h"
 #include "clang/Basic/HLSLRuntime.h"
 #include "clang/Basic/MacroBuilder.h"
 #include "clang/Basic/SourceManager.h"
@@ -1644,4 +1645,11 @@ void clang::InitializePreprocessor(Preprocessor &PP,
 
   // Copy PredefinedBuffer into the Preprocessor.
   PP.setPredefines(std::move(PredefineBuffer));
+
+  // Match gcc behavior regarding gnu-line-directive diagnostics, assuming that
+  // '-x <*>-cpp-output' is analogous to '-fpreprocessed'.
+  if (FEOpts.DashX.isPreprocessed()) {
+    PP.getDiagnostics().setSeverity(diag::ext_pp_gnu_line_directive,
+                                    diag::Severity::Ignored, SourceLocation());
+  }
 }

diff  --git a/clang/test/Preprocessor/line-directive-suppressed.c 
b/clang/test/Preprocessor/line-directive-suppressed.c
new file mode 100644
index 0000000000000..b03c49b8a9f90
--- /dev/null
+++ b/clang/test/Preprocessor/line-directive-suppressed.c
@@ -0,0 +1,16 @@
+// RUN: %clang_cc1 -std=c99 -fsyntax-only -pedantic %s 2>&1 | FileCheck %s
+
+// RUN: cp %s %t.i
+// RUN: %clang_cc1 -std=c99 -fsyntax-only -pedantic %t.i 2>&1 | FileCheck %s 
--check-prefix=NO-WARNING --allow-empty
+// RUN: %clang_cc1 -std=c99 -fsyntax-only -pedantic -x cpp-output %s 2>&1 | 
FileCheck %s --check-prefix=NO-WARNING --allow-empty
+
+# 0 "zero"
+// CHECK: line-directive-suppressed.c:[[@LINE-1]]:5: warning: {{.*}} 
[-Wgnu-line-marker]
+
+# 1 "one" 1
+// CHECK: zero:2:5: warning: {{.*}} [-Wgnu-line-marker]
+
+# 2 "two" 1 3 4
+// CHECK: one:3:5: warning: {{.*}} [-Wgnu-line-marker]
+
+// NO-WARNING-NOT: warning:


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to