Skip to content

Commit 1c1a4b9

Browse files
committed
[clang][driver] Emit a warning if -xc/-xc++ is after the last input file
This follows the same warning GCC produces. Differential Revision: https://reviews.llvm.org/D121683
1 parent 5464fd3 commit 1c1a4b9

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,9 @@ def warn_drv_preprocessed_input_file_unused : Warning<
365365
def warn_drv_unused_argument : Warning<
366366
"argument unused during compilation: '%0'">,
367367
InGroup<UnusedCommandLineArgument>;
368+
def warn_drv_unused_x : Warning<
369+
"‘-x %0’ after last input file has no effect">,
370+
InGroup<UnusedCommandLineArgument>;
368371
def warn_drv_empty_joined_argument : Warning<
369372
"joined argument expects additional value: '%0'">,
370373
InGroup<UnusedCommandLineArgument>;

clang/lib/Driver/Driver.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,6 +2308,15 @@ void Driver::BuildInputs(const ToolChain &TC, DerivedArgList &Args,
23082308
assert(!Args.hasArg(options::OPT_x) && "-x and /TC or /TP is not allowed");
23092309
}
23102310

2311+
// Warn -x after last input file has no effect
2312+
{
2313+
Arg *LastXArg = Args.getLastArgNoClaim(options::OPT_x);
2314+
Arg *LastInputArg = Args.getLastArgNoClaim(options::OPT_INPUT);
2315+
if (LastInputArg->getIndex() < LastXArg->getIndex()) {
2316+
Diag(clang::diag::warn_drv_unused_x) << LastXArg->getValue();
2317+
}
2318+
}
2319+
23112320
for (Arg *A : Args) {
23122321
if (A->getOption().getKind() == Option::InputClass) {
23132322
const char *Value = A->getValue();

clang/test/Driver/x-args.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// RUN: %clang -fsyntax-only -Werror -xc %s
2+
// RUN: %clang -fsyntax-only -Werror %s -xc %s
3+
4+
// RUN: %clang -fsyntax-only %s -xc++ -fsyntax-only 2>&1 | FileCheck %s
5+
// RUN: %clang -fsyntax-only -xc %s -xc++ -fsyntax-only 2>&1 | FileCheck %s
6+
// RUN: %clang -fsyntax-only %s -xc %s -xc++ -fsyntax-only 2>&1 | FileCheck %s
7+
// CHECK: ‘-x c++’ after last input file has no effect

0 commit comments

Comments
 (0)