diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td --- a/clang/include/clang/Basic/DiagnosticDriverKinds.td +++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td @@ -568,6 +568,9 @@ "'%0' does not support '-%1'; flag ignored">, InGroup; +def warn_drv_ignored_option_x : Warning< + "'-x %0' after last input file has no effect">, InGroup; + def warn_drv_darwin_sdk_invalid_settings : Warning< "SDK settings were ignored as 'SDKSettings.json' could not be parsed">, InGroup>; diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -2269,6 +2269,7 @@ assert(!Args.hasArg(options::OPT_x) && "-x and /TC or /TP is not allowed"); } + size_t InputsBeforeOptX = 0; for (Arg *A : Args) { if (A->getOption().getKind() == Option::InputClass) { const char *Value = A->getValue(); @@ -2387,6 +2388,7 @@ InputTypeArg = A; InputType = types::lookupTypeForTypeSpecifier(A->getValue()); A->claim(); + InputsBeforeOptX = Inputs.size(); // Follow gcc behavior and treat as linker input for invalid -x // options. Its not clear why we shouldn't just revert to unknown; but @@ -2411,6 +2413,8 @@ Arg *A = MakeInputArg(Args, Opts, "-"); Inputs.push_back(std::make_pair(types::TY_C, A)); } + if (Inputs.size() == InputsBeforeOptX && InputTypeArg != nullptr) + Diag(diag::warn_drv_ignored_option_x) << InputTypeArg->getValue(0); } namespace { diff --git a/clang/test/Driver/redundant-args.c b/clang/test/Driver/redundant-args.c --- a/clang/test/Driver/redundant-args.c +++ b/clang/test/Driver/redundant-args.c @@ -1,2 +1,4 @@ -// RUN: %clang -target x86_64-apple-darwin10 \ -// RUN: -Werror -x c -x c -fsyntax-only %s +// RUN: %clang -target x86_64-apple-darwin10 -Werror -x c -x c -fsyntax-only %s +// RUN: %clang -target x86_64-apple-darwin10 %s -### -x c 2>&1 | FileCheck %s + +// CHECK: warning: '-x c' after last input file has no effect