[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
This commit is contained in:
macurtis-amd
2025-06-10 11:54:50 -05:00
committed by GitHub
parent 88f041f3e0
commit 2ddf0caaed
2 changed files with 24 additions and 0 deletions

View File

@@ -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());
}
}

View File

@@ -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: