Skip to content

Commit 2f23965

Browse files
committed
[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
1 parent 37deb09 commit 2f23965

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "clang/Basic/CLWarnings.h"
2929
#include "clang/Basic/CharInfo.h"
3030
#include "clang/Basic/CodeGenOptions.h"
31+
#include "clang/Basic/DiagnosticLex.h"
3132
#include "clang/Basic/HeaderInclude.h"
3233
#include "clang/Basic/LangOptions.h"
3334
#include "clang/Basic/MakeSupport.h"
@@ -5077,6 +5078,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
50775078
const InputInfo &Input =
50785079
IsExtractAPI ? ExtractAPIPlaceholderInput : Inputs[0];
50795080

5081+
bool hasPreprocessorJobInput = false;
50805082
InputInfoList ExtractAPIInputs;
50815083
InputInfoList HostOffloadingInputs;
50825084
const InputInfo *CudaDeviceInput = nullptr;
@@ -5101,6 +5103,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
51015103
} else {
51025104
llvm_unreachable("unexpectedly given multiple inputs");
51035105
}
5106+
hasPreprocessorJobInput |=
5107+
I.getAction()->getKind() == Action::PreprocessJobClass;
51045108
}
51055109

51065110
const llvm::Triple *AuxTriple =
@@ -6506,6 +6510,27 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
65066510
Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
65076511
Args.AddLastArg(CmdArgs, options::OPT_w);
65086512

6513+
// Possibly suppress gnu-line-marker diagnostics. This suppresses spurious
6514+
// warnings or errors when using '-save-temps' with, for example, '-Werror
6515+
// -Weverything' or '-pedantic'.
6516+
if (hasPreprocessorJobInput &&
6517+
!D.getDiags().isIgnored(diag::ext_pp_gnu_line_directive,
6518+
SourceLocation())) {
6519+
auto IDs = D.getDiags().getDiagnosticIDs();
6520+
StringRef gnuLineMarker =
6521+
IDs->getWarningOptionForDiag(diag::ext_pp_gnu_line_directive);
6522+
6523+
// If for some reason the user has explicitly specified -Wgnu-line-marker,
6524+
// they are on their own.
6525+
bool hasGnuLineMarker = false;
6526+
for (std::string &V : Args.getAllArgValues(options::OPT_W_Joined)) {
6527+
if ((hasGnuLineMarker = V == gnuLineMarker))
6528+
break;
6529+
}
6530+
if (!hasGnuLineMarker)
6531+
CmdArgs.push_back(Args.MakeArgString("-Wno-" + gnuLineMarker));
6532+
}
6533+
65096534
Args.addOptInFlag(CmdArgs, options::OPT_ffixed_point,
65106535
options::OPT_fno_fixed_point);
65116536

clang/test/Driver/save-temps.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,19 @@
9090
// CHECK-SAVE-TEMPS-CMSE: -cc1as
9191
// CHECK-SAVE-TEMPS-CMSE: +8msecext
9292
// CHECK-SAVE-TEMPS-CMSE-NOT: '+cmse' is not a recognized feature for this target (ignoring feature)
93+
94+
// RUN: %clang -target x86_64-unknown-linux-gnu -Werror -pedantic -save-temps %s -### 2>&1 \
95+
// RUN: | FileCheck %s -check-prefix=CHECK-SAVE-TEMPS-GLM-000
96+
// CHECK-SAVE-TEMPS-GLM-000: "-o" "save-temps.i"
97+
// CHECK-SAVE-TEMPS-GLM-000: "-pedantic" "-Wno-gnu-line-marker"{{.*}} "-x" "cpp-output" "save-temps.i"
98+
99+
// RUN: %clang -target x86_64-unknown-linux-gnu -Werror -Weverything -save-temps %s -### 2>&1 \
100+
// RUN: | FileCheck %s -check-prefix=CHECK-SAVE-TEMPS-GLM-001
101+
// CHECK-SAVE-TEMPS-GLM-001: "-o" "save-temps.i"
102+
// CHECK-SAVE-TEMPS-GLM-001: "-Weverything" "-Wno-gnu-line-marker"{{.*}} "-x" "cpp-output" "save-temps.i"
103+
104+
// RUN: %clang -target x86_64-unknown-linux-gnu -Werror -Weverything -Wgnu-line-marker -save-temps %s -### 2>&1 \
105+
// RUN: | FileCheck %s -check-prefix=CHECK-SAVE-TEMPS-GLM-002
106+
// RUN: %clang -target x86_64-unknown-linux-gnu -Werror -Weverything -save-temps -x cpp-output %s -### 2>&1 \
107+
// RUN: | FileCheck %s -check-prefix=CHECK-SAVE-TEMPS-GLM-002
108+
// CHECK-SAVE-TEMPS-GLM-002-NOT: "-Wno-gnu-line-marker"

0 commit comments

Comments
 (0)