From c2377eae286bbe616267bef772ee736b030dd007 Mon Sep 17 00:00:00 2001 From: Kadir Cetinkaya Date: Thu, 19 Dec 2019 22:22:01 +0100 Subject: [clang][Tooling] Prefer -x over -std when interpolating Summary: Currently interpolation logic prefers -std over -x. But the latter is a more strong signal, so this patch inverts the order and only makes use of -std if -x didn't exist. Fixes https://github.com/clangd/clangd/issues/185 Thanks @sammccall for tracking this down! Reviewers: sammccall Subscribers: ilya-biryukov, usaxena95, cfe-commits, sammccall Tags: #clang Differential Revision: https://reviews.llvm.org/D71727 --- clang/lib/Tooling/InterpolatingCompilationDatabase.cpp | 3 ++- clang/unittests/Tooling/CompilationDatabaseTest.cpp | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp b/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp index 59b66abe65e..2cc819a498c 100644 --- a/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp +++ b/clang/lib/Tooling/InterpolatingCompilationDatabase.cpp @@ -191,7 +191,8 @@ struct TransferableCommand { 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 index 91685c0d0c7..56ebadc5eae 100644 --- a/clang/unittests/Tooling/CompilationDatabaseTest.cpp +++ b/clang/unittests/Tooling/CompilationDatabaseTest.cpp @@ -749,6 +749,7 @@ TEST_F(InterpolateTest, Language) { 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 @@ TEST_F(InterpolateTest, Language) { 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) { -- cgit v1.2.3