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

via cfe-commits cfe-commits at lists.llvm.org
Mon Apr 7 06:22:54 PDT 2025


https://github.com/macurtis-amd updated https://github.com/llvm/llvm-project/pull/134621

>From 2f2396511137feb21446b93dc344cd82a245627f Mon Sep 17 00:00:00 2001
From: Matthew Curtis <macurtis at amd.com>
Date: Sat, 5 Apr 2025 04:34:03 -0500
Subject: [PATCH] [clang][driver] 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 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
---
 clang/lib/Driver/ToolChains/Clang.cpp | 25 +++++++++++++++++++++++++
 clang/test/Driver/save-temps.c        | 16 ++++++++++++++++
 2 files changed, 41 insertions(+)

diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp
index 70489adf01c94..3e90dc7a1bb52 100644
--- a/clang/lib/Driver/ToolChains/Clang.cpp
+++ b/clang/lib/Driver/ToolChains/Clang.cpp
@@ -28,6 +28,7 @@
 #include "clang/Basic/CLWarnings.h"
 #include "clang/Basic/CharInfo.h"
 #include "clang/Basic/CodeGenOptions.h"
+#include "clang/Basic/DiagnosticLex.h"
 #include "clang/Basic/HeaderInclude.h"
 #include "clang/Basic/LangOptions.h"
 #include "clang/Basic/MakeSupport.h"
@@ -5077,6 +5078,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
   const InputInfo &Input =
       IsExtractAPI ? ExtractAPIPlaceholderInput : Inputs[0];
 
+  bool hasPreprocessorJobInput = false;
   InputInfoList ExtractAPIInputs;
   InputInfoList HostOffloadingInputs;
   const InputInfo *CudaDeviceInput = nullptr;
@@ -5101,6 +5103,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
     } else {
       llvm_unreachable("unexpectedly given multiple inputs");
     }
+    hasPreprocessorJobInput |=
+        I.getAction()->getKind() == Action::PreprocessJobClass;
   }
 
   const llvm::Triple *AuxTriple =
@@ -6506,6 +6510,27 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
   Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
   Args.AddLastArg(CmdArgs, options::OPT_w);
 
+  // Possibly suppress gnu-line-marker diagnostics. This suppresses spurious
+  // warnings or errors when using '-save-temps' with, for example, '-Werror
+  // -Weverything' or '-pedantic'.
+  if (hasPreprocessorJobInput &&
+      !D.getDiags().isIgnored(diag::ext_pp_gnu_line_directive,
+                              SourceLocation())) {
+    auto IDs = D.getDiags().getDiagnosticIDs();
+    StringRef gnuLineMarker =
+        IDs->getWarningOptionForDiag(diag::ext_pp_gnu_line_directive);
+
+    // If for some reason the user has explicitly specified -Wgnu-line-marker,
+    // they are on their own.
+    bool hasGnuLineMarker = false;
+    for (std::string &V : Args.getAllArgValues(options::OPT_W_Joined)) {
+      if ((hasGnuLineMarker = V == gnuLineMarker))
+        break;
+    }
+    if (!hasGnuLineMarker)
+      CmdArgs.push_back(Args.MakeArgString("-Wno-" + gnuLineMarker));
+  }
+
   Args.addOptInFlag(CmdArgs, options::OPT_ffixed_point,
                     options::OPT_fno_fixed_point);
 
diff --git a/clang/test/Driver/save-temps.c b/clang/test/Driver/save-temps.c
index b0cfa4fd814a8..48a590700076e 100644
--- a/clang/test/Driver/save-temps.c
+++ b/clang/test/Driver/save-temps.c
@@ -90,3 +90,19 @@
 // CHECK-SAVE-TEMPS-CMSE: -cc1as
 // CHECK-SAVE-TEMPS-CMSE: +8msecext
 // CHECK-SAVE-TEMPS-CMSE-NOT: '+cmse' is not a recognized feature for this target (ignoring feature)
+
+// RUN: %clang -target x86_64-unknown-linux-gnu -Werror -pedantic -save-temps %s -### 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=CHECK-SAVE-TEMPS-GLM-000
+// CHECK-SAVE-TEMPS-GLM-000: "-o" "save-temps.i"
+// CHECK-SAVE-TEMPS-GLM-000: "-pedantic" "-Wno-gnu-line-marker"{{.*}} "-x" "cpp-output" "save-temps.i"
+
+// RUN: %clang -target x86_64-unknown-linux-gnu -Werror -Weverything -save-temps %s -### 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=CHECK-SAVE-TEMPS-GLM-001
+// CHECK-SAVE-TEMPS-GLM-001: "-o" "save-temps.i"
+// CHECK-SAVE-TEMPS-GLM-001: "-Weverything" "-Wno-gnu-line-marker"{{.*}} "-x" "cpp-output" "save-temps.i"
+
+// RUN: %clang -target x86_64-unknown-linux-gnu -Werror -Weverything -Wgnu-line-marker -save-temps %s -### 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=CHECK-SAVE-TEMPS-GLM-002
+// RUN: %clang -target x86_64-unknown-linux-gnu -Werror -Weverything -save-temps -x cpp-output %s -### 2>&1 \
+// RUN:   | FileCheck %s -check-prefix=CHECK-SAVE-TEMPS-GLM-002
+// CHECK-SAVE-TEMPS-GLM-002-NOT: "-Wno-gnu-line-marker"



More information about the cfe-commits mailing list