Skip to content

Commit eeab645

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 eeab645

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 27 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"
@@ -67,6 +68,8 @@
6768
#include "llvm/TargetParser/RISCVTargetParser.h"
6869
#include <cctype>
6970

71+
#include "/home/macurtis/mcc/src/MCC-PRINT.h"
72+
7073
using namespace clang::driver;
7174
using namespace clang::driver::tools;
7275
using namespace clang;
@@ -5077,6 +5080,7 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
50775080
const InputInfo &Input =
50785081
IsExtractAPI ? ExtractAPIPlaceholderInput : Inputs[0];
50795082

5083+
bool hasPreprocessorJobInput = false;
50805084
InputInfoList ExtractAPIInputs;
50815085
InputInfoList HostOffloadingInputs;
50825086
const InputInfo *CudaDeviceInput = nullptr;
@@ -5101,6 +5105,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
51015105
} else {
51025106
llvm_unreachable("unexpectedly given multiple inputs");
51035107
}
5108+
hasPreprocessorJobInput |=
5109+
I.getAction()->getKind() == Action::PreprocessJobClass;
51045110
}
51055111

51065112
const llvm::Triple *AuxTriple =
@@ -6506,6 +6512,27 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
65066512
Args.AddLastArg(CmdArgs, options::OPT_pedantic_errors);
65076513
Args.AddLastArg(CmdArgs, options::OPT_w);
65086514

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

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)