From d5951c0e699043f7f9d5ae910ae3729fc03ea71b Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Mon, 27 Nov 2017 08:20:42 -0800 Subject: [PATCH] Revert "Specify nullptr for source_filename in clang_parseTranslationUnit2 so that we do not need to strip main source filename from args" This reverts commit 6adc3a28c39a5f6b58a0a57bfae83916051c19f8. --- README.md | 2 +- src/clang_translation_unit.cc | 2 +- src/project.cc | 31 ++++++++++++++++++++++--------- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 60ef10cc..ca95ceab 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ are skipped. Here's an example: ``` # Language -clang++ +-xc++ -std=c++11 # Includes diff --git a/src/clang_translation_unit.cc b/src/clang_translation_unit.cc index 0ba7e0d6..d86fc266 100644 --- a/src/clang_translation_unit.cc +++ b/src/clang_translation_unit.cc @@ -27,7 +27,7 @@ std::unique_ptr ClangTranslationUnit::Create( CXTranslationUnit cx_tu; CXErrorCode error_code = clang_parseTranslationUnit2FullArgv( - index->cx_index, nullptr, args.data(), (int)args.size(), + index->cx_index, filepath.c_str(), args.data(), (int)args.size(), unsaved_files.data(), (unsigned)unsaved_files.size(), flags, &cx_tu); switch (error_code) { diff --git a/src/project.cc b/src/project.cc index bfdb142e..1c52ff93 100644 --- a/src/project.cc +++ b/src/project.cc @@ -53,6 +53,9 @@ static std::vector kBlacklistMulti = { // Blacklisted flags which are always removed from the command line. static std::vector kBlacklist = { "-c", "-MP", "-MD", "-MMD", "--fcolor-diagnostics", + + // This strips path-like args but is a bit hacky. + "/", "..", }; // Arguments which are followed by a potentially relative path. We need to make @@ -114,6 +117,10 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry( return NormalizePathWithTestOptOut(entry.directory + "/" + path); }; + // Do not include path. + if (result.filename == cleanup_maybe_relative_path(arg)) + continue; + // If blacklist skip. if (!next_flag_is_path) { if (StartsWithAny(arg, kBlacklistMulti)) { @@ -167,6 +174,14 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry( for (const auto& flag : config->extra_flags) result.args.push_back(flag); + // Clang does not have good hueristics for determining source language, we + // should explicitly specify it. + if (!AnyStartsWith(result.args, "-x")) { + if (IsCFile(entry.file)) + result.args.push_back("-xc"); + else + result.args.push_back("-xc++"); + } if (!AnyStartsWith(result.args, "-std=")) { if (IsCFile(entry.file)) result.args.push_back("-std=c11"); @@ -206,7 +221,6 @@ std::vector LoadFromDirectoryListing(ProjectConfig* config) { CompileCommandsEntry e; e.file = NormalizePathWithTestOptOut(file); e.args = args; - e.args.push_back(e.file); result.push_back(GetCompilationEntryFromCompileCommandEntry(config, e)); } } @@ -449,15 +463,15 @@ TEST_SUITE("Project") { TEST_CASE("strip meta-compiler invocations") { CheckFlags( /* raw */ {"clang", "-lstdc++", "myfile.cc"}, - /* expected */ {"clang", "-lstdc++", "myfile.cc", "-std=c++11", + /* expected */ {"clang", "-lstdc++", "myfile.cc", "-xc++", "-std=c++11", "-resource-dir=/w/resource_dir/"}); CheckFlags(/* raw */ {"goma", "clang"}, - /* expected */ {"clang", "-std=c++11", + /* expected */ {"clang", "-xc++", "-std=c++11", "-resource-dir=/w/resource_dir/"}); CheckFlags(/* raw */ {"goma", "clang", "--foo"}, - /* expected */ {"clang", "--foo", "-std=c++11", + /* expected */ {"clang", "--foo", "-xc++", "-std=c++11", "-resource-dir=/w/resource_dir/"}); } @@ -467,7 +481,7 @@ TEST_SUITE("Project") { "/home/user", "/home/user/foo/bar.c", /* raw */ {"cc", "-O0", "foo/bar.c"}, /* expected */ - {"cc", "-O0", "foo/bar.c", "-std=c11", "-resource-dir=/w/resource_dir/"}); + {"cc", "-O0", "-xc", "-std=c11", "-resource-dir=/w/resource_dir/"}); } // Checks flag parsing for a random chromium file in comparison to what @@ -811,7 +825,7 @@ TEST_SUITE("Project") { "debian_jessie_amd64-sysroot", "-fno-exceptions", "-fvisibility-inlines-hidden", - "../../ash/login/ui/lock_screen_sanity_unittest.cc", + "-xc++", "-resource-dir=/w/resource_dir/"}); } @@ -980,8 +994,7 @@ TEST_SUITE("Project") { "-isystem../../buildtools/third_party/libc++abi/trunk/include", "--sysroot=../../build/linux/debian_jessie_amd64-sysroot", "-fno-exceptions", - "-fvisibility-inlines-hidden", - "../../apps/app_lifetime_monitor.cc"}, + "-fvisibility-inlines-hidden"}, /* expected */ {"../../third_party/llvm-build/Release+Asserts/bin/clang++", @@ -1131,7 +1144,7 @@ TEST_SUITE("Project") { "debian_jessie_amd64-sysroot", "-fno-exceptions", "-fvisibility-inlines-hidden", - "../../apps/app_lifetime_monitor.cc", + "-xc++", "-resource-dir=/w/resource_dir/"}); }