diff --git a/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp b/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp --- a/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp +++ b/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp @@ -191,7 +191,8 @@ OldArgs.data() + OldPos, OldArgs.data() + Pos); } - if (Std != LangStandard::lang_unspecified) // -std take precedence over -x + // Make use of -std iff -x was missing. + if (Type == types::TY_INVALID && Std != LangStandard::lang_unspecified) Type = toType(LangStandard::getLangStandardForKind(Std).getLanguage()); Type = foldType(*Type); // The contract is to store None instead of TY_INVALID. diff --git a/clang/unittests/Tooling/CompilationDatabaseTest.cpp b/clang/unittests/Tooling/CompilationDatabaseTest.cpp --- a/clang/unittests/Tooling/CompilationDatabaseTest.cpp +++ b/clang/unittests/Tooling/CompilationDatabaseTest.cpp @@ -749,6 +749,7 @@ add("dir/foo.cpp", "-std=c++17"); add("dir/bar.c", ""); add("dir/baz.cee", "-x c"); + add("dir/aux.cpp", "-std=c++17 -x objective-c++"); // .h is ambiguous, so we add explicit language flags EXPECT_EQ(getCommand("foo.h"), @@ -767,6 +768,9 @@ Entries.erase(path(StringRef("dir/bar.c"))); // Now we transfer across languages, so drop -std too. EXPECT_EQ(getCommand("foo.c"), "clang -D dir/foo.cpp"); + // Prefer -x over -std when overriding language. + EXPECT_EQ(getCommand("aux.h"), + "clang -D dir/aux.cpp -x objective-c++-header -std=c++17"); } TEST_F(InterpolateTest, Strip) {