Specify nullptr for source_filename in clang_parseTranslationUnit2 so that we do not need to strip main source filename from args

This commit is contained in:
Fangrui Song 2017-11-26 22:17:51 -08:00 committed by Jacob Dufault
parent bf2aaf470e
commit 2b54296992
3 changed files with 11 additions and 24 deletions

View File

@ -96,7 +96,7 @@ are skipped. Here's an example:
``` ```
# Language # Language
-xc++ clang++
-std=c++11 -std=c++11
# Includes # Includes

View File

@ -27,7 +27,7 @@ std::unique_ptr<ClangTranslationUnit> ClangTranslationUnit::Create(
CXTranslationUnit cx_tu; CXTranslationUnit cx_tu;
CXErrorCode error_code = clang_parseTranslationUnit2FullArgv( CXErrorCode error_code = clang_parseTranslationUnit2FullArgv(
index->cx_index, filepath.c_str(), args.data(), (int)args.size(), index->cx_index, nullptr, args.data(), (int)args.size(),
unsaved_files.data(), (unsigned)unsaved_files.size(), flags, &cx_tu); unsaved_files.data(), (unsigned)unsaved_files.size(), flags, &cx_tu);
switch (error_code) { switch (error_code) {

View File

@ -53,9 +53,6 @@ static std::vector<std::string> kBlacklistMulti = {
// Blacklisted flags which are always removed from the command line. // Blacklisted flags which are always removed from the command line.
static std::vector<std::string> kBlacklist = { static std::vector<std::string> kBlacklist = {
"-c", "-MP", "-MD", "-MMD", "--fcolor-diagnostics", "-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 // Arguments which are followed by a potentially relative path. We need to make
@ -117,10 +114,6 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(
return NormalizePathWithTestOptOut(entry.directory + "/" + path); return NormalizePathWithTestOptOut(entry.directory + "/" + path);
}; };
// Do not include path.
if (result.filename == cleanup_maybe_relative_path(arg))
continue;
// If blacklist skip. // If blacklist skip.
if (!next_flag_is_path) { if (!next_flag_is_path) {
if (StartsWithAny(arg, kBlacklistMulti)) { if (StartsWithAny(arg, kBlacklistMulti)) {
@ -174,14 +167,6 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(
for (const auto& flag : config->extra_flags) for (const auto& flag : config->extra_flags)
result.args.push_back(flag); 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 (!AnyStartsWith(result.args, "-std=")) {
if (IsCFile(entry.file)) if (IsCFile(entry.file))
result.args.push_back("-std=c11"); result.args.push_back("-std=c11");
@ -221,6 +206,7 @@ std::vector<Project::Entry> LoadFromDirectoryListing(ProjectConfig* config) {
CompileCommandsEntry e; CompileCommandsEntry e;
e.file = NormalizePathWithTestOptOut(file); e.file = NormalizePathWithTestOptOut(file);
e.args = args; e.args = args;
e.args.push_back(e.file);
result.push_back(GetCompilationEntryFromCompileCommandEntry(config, e)); result.push_back(GetCompilationEntryFromCompileCommandEntry(config, e));
} }
} }
@ -463,15 +449,15 @@ TEST_SUITE("Project") {
TEST_CASE("strip meta-compiler invocations") { TEST_CASE("strip meta-compiler invocations") {
CheckFlags( CheckFlags(
/* raw */ {"clang", "-lstdc++", "myfile.cc"}, /* raw */ {"clang", "-lstdc++", "myfile.cc"},
/* expected */ {"clang", "-lstdc++", "myfile.cc", "-xc++", "-std=c++11", /* expected */ {"clang", "-lstdc++", "myfile.cc", "-std=c++11",
"-resource-dir=/w/resource_dir/"}); "-resource-dir=/w/resource_dir/"});
CheckFlags(/* raw */ {"goma", "clang"}, CheckFlags(/* raw */ {"goma", "clang"},
/* expected */ {"clang", "-xc++", "-std=c++11", /* expected */ {"clang", "-std=c++11",
"-resource-dir=/w/resource_dir/"}); "-resource-dir=/w/resource_dir/"});
CheckFlags(/* raw */ {"goma", "clang", "--foo"}, CheckFlags(/* raw */ {"goma", "clang", "--foo"},
/* expected */ {"clang", "--foo", "-xc++", "-std=c++11", /* expected */ {"clang", "--foo", "-std=c++11",
"-resource-dir=/w/resource_dir/"}); "-resource-dir=/w/resource_dir/"});
} }
@ -481,7 +467,7 @@ TEST_SUITE("Project") {
"/home/user", "/home/user/foo/bar.c", "/home/user", "/home/user/foo/bar.c",
/* raw */ {"cc", "-O0", "foo/bar.c"}, /* raw */ {"cc", "-O0", "foo/bar.c"},
/* expected */ /* expected */
{"cc", "-O0", "-xc", "-std=c11", "-resource-dir=/w/resource_dir/"}); {"cc", "-O0", "foo/bar.c", "-std=c11", "-resource-dir=/w/resource_dir/"});
} }
// Checks flag parsing for a random chromium file in comparison to what // Checks flag parsing for a random chromium file in comparison to what
@ -825,7 +811,7 @@ TEST_SUITE("Project") {
"debian_jessie_amd64-sysroot", "debian_jessie_amd64-sysroot",
"-fno-exceptions", "-fno-exceptions",
"-fvisibility-inlines-hidden", "-fvisibility-inlines-hidden",
"-xc++", "../../ash/login/ui/lock_screen_sanity_unittest.cc",
"-resource-dir=/w/resource_dir/"}); "-resource-dir=/w/resource_dir/"});
} }
@ -994,7 +980,8 @@ TEST_SUITE("Project") {
"-isystem../../buildtools/third_party/libc++abi/trunk/include", "-isystem../../buildtools/third_party/libc++abi/trunk/include",
"--sysroot=../../build/linux/debian_jessie_amd64-sysroot", "--sysroot=../../build/linux/debian_jessie_amd64-sysroot",
"-fno-exceptions", "-fno-exceptions",
"-fvisibility-inlines-hidden"}, "-fvisibility-inlines-hidden",
"../../apps/app_lifetime_monitor.cc"},
/* expected */ /* expected */
{"../../third_party/llvm-build/Release+Asserts/bin/clang++", {"../../third_party/llvm-build/Release+Asserts/bin/clang++",
@ -1144,7 +1131,7 @@ TEST_SUITE("Project") {
"debian_jessie_amd64-sysroot", "debian_jessie_amd64-sysroot",
"-fno-exceptions", "-fno-exceptions",
"-fvisibility-inlines-hidden", "-fvisibility-inlines-hidden",
"-xc++", "../../apps/app_lifetime_monitor.cc",
"-resource-dir=/w/resource_dir/"}); "-resource-dir=/w/resource_dir/"});
} }