[clang] [clang][driver] Suppress gnu-line-marker when saving temps (PR #134621)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Apr 21 04:44:11 PDT 2025
https://github.com/macurtis-amd updated https://github.com/llvm/llvm-project/pull/134621
>From 9d38a08bc91273d961fa1bd855d212b09249d826 Mon Sep 17 00:00:00 2001
From: Matthew Curtis <macurtis at amd.com>
Date: Fri, 18 Apr 2025 12:46:23 -0500
Subject: [PATCH 1/2] [clang] Suppress gnu-line-marker when saving temps
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
preprocessor input (specified via -x argument or deduced from the input file
name). This matches gcc behavior.
fixes #63802
---
clang/lib/Frontend/InitPreprocessor.cpp | 8 ++++++++
clang/test/Preprocessor/line-directive-suppressed.c | 9 +++++++++
2 files changed, 17 insertions(+)
create mode 100644 clang/test/Preprocessor/line-directive-suppressed.c
diff --git a/clang/lib/Frontend/InitPreprocessor.cpp b/clang/lib/Frontend/InitPreprocessor.cpp
index 1f297f228fc1b..5d32eeb5f3b28 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/FileManager.h"
#include "clang/Basic/HLSLRuntime.h"
#include "clang/Basic/MacroBuilder.h"
@@ -1643,4 +1644,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..db97f211c28f4
--- /dev/null
+++ b/clang/test/Preprocessor/line-directive-suppressed.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -std=c99 -fsyntax-only -pedantic %s 2>&1 | grep 'warning: this style of line directive is a GNU extension'
+
+// RUN: %clang_cc1 -std=c99 -fsyntax-only -pedantic -x c-cpp-output %s 2>&1 | not grep warning
+// RUN: cp %s %t.i
+// RUN: %clang_cc1 -std=c99 -fsyntax-only -pedantic %t.i 2>&1 | not grep warning
+
+# 0 "zero"
+# 1 "one" 1
+# 2 "two" 1 3 4
>From 814c2d570d0e4c5eba6de15fbe85bf28ec02b94d Mon Sep 17 00:00:00 2001
From: Matthew Curtis <macurtis at amd.com>
Date: Mon, 21 Apr 2025 06:43:08 -0500
Subject: [PATCH 2/2] fixup! [clang] Suppress gnu-line-marker when saving temps
---
clang/test/Preprocessor/line-directive-suppressed.c | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
diff --git a/clang/test/Preprocessor/line-directive-suppressed.c b/clang/test/Preprocessor/line-directive-suppressed.c
index db97f211c28f4..b03c49b8a9f90 100644
--- a/clang/test/Preprocessor/line-directive-suppressed.c
+++ b/clang/test/Preprocessor/line-directive-suppressed.c
@@ -1,9 +1,16 @@
-// RUN: %clang_cc1 -std=c99 -fsyntax-only -pedantic %s 2>&1 | grep 'warning: this style of line directive is a GNU extension'
+// RUN: %clang_cc1 -std=c99 -fsyntax-only -pedantic %s 2>&1 | FileCheck %s
-// RUN: %clang_cc1 -std=c99 -fsyntax-only -pedantic -x c-cpp-output %s 2>&1 | not grep warning
// RUN: cp %s %t.i
-// RUN: %clang_cc1 -std=c99 -fsyntax-only -pedantic %t.i 2>&1 | not grep warning
+// 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:
More information about the cfe-commits
mailing list