Use nullptr for main source filename in clang_parseTranslationUnit2FullArgv and rely on -working-directory for relative filename resolution

This commit is contained in:
Fangrui Song 2017-11-26 22:17:51 -08:00 committed by Jacob Dufault
parent bb2e94964f
commit 08e856b85a
3 changed files with 125 additions and 116 deletions

View File

@ -86,7 +86,7 @@ std::unique_ptr<ClangTranslationUnit> ClangTranslationUnit::Create(
CXErrorCode error_code; CXErrorCode error_code;
{ {
error_code = clang_parseTranslationUnit2FullArgv( 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);
} }

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
@ -108,10 +105,21 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(
size_t i = 0; size_t i = 0;
// Strip all arguments before the -, as there may be non-compiler related // Strip all arguments consisting the compiler command,
// commands beforehand, ie, compiler schedular such as goma. This allows // as there may be non-compiler related commands beforehand,
// correct parsing for command lines like "goma clang -c foo". // ie, compiler schedular such as goma. This allows correct parsing for
while (i < entry.args.size() && entry.args[i][0] != '-') // command lines like "goma clang -c foo".
while (i < entry.args.size() && entry.args[i][0] != '-' &&
// Do not skip over main source filename
NormalizePathWithTestOptOut(entry.args[i]) != result.filename &&
// There may be other filenames (e.g. more than one source filenames)
// preceding main source filename.
// We use a heuristic here. `.` may occur in both command names and
// source filenames. If `.` occurs in the last 4 bytes of
// entry.args[i], e.g. .c .cpp, We take it as a source filename. Others
// (like ./a/b/goma) are seen as commands.
(entry.args[i].rfind('.') == std::string::npos ||
entry.args[i].rfind('.') + 4 < entry.args[i].size()))
++i; ++i;
// Include the compiler in the args. // Include the compiler in the args.
if (i > 0) if (i > 0)
@ -151,10 +159,6 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(
for (; i < entry.args.size(); ++i) { for (; i < entry.args.size(); ++i) {
std::string arg = entry.args[i]; std::string arg = entry.args[i];
// 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)) {
@ -167,11 +171,11 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(
// Cleanup path for previous argument. // Cleanup path for previous argument.
if (next_flag_is_path) { if (next_flag_is_path) {
arg = cleanup_maybe_relative_path(arg); std::string normalized_arg = cleanup_maybe_relative_path(arg);
if (add_next_flag_to_quote_dirs) if (add_next_flag_to_quote_dirs)
config->quote_dirs.insert(arg); config->quote_dirs.insert(normalized_arg);
if (add_next_flag_to_angle_dirs) if (add_next_flag_to_angle_dirs)
config->angle_dirs.insert(arg); config->angle_dirs.insert(normalized_arg);
next_flag_is_path = false; next_flag_is_path = false;
add_next_flag_to_quote_dirs = false; add_next_flag_to_quote_dirs = false;
@ -191,10 +195,10 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry(
std::string path = arg.substr(flag_type.size()); std::string path = arg.substr(flag_type.size());
assert(!path.empty()); assert(!path.empty());
path = cleanup_maybe_relative_path(path); path = cleanup_maybe_relative_path(path);
arg = flag_type + path; std::string normalized_path = flag_type + path;
if (ShouldAddToQuoteIncludes(arg)) if (ShouldAddToQuoteIncludes(normalized_path))
config->quote_dirs.insert(path); config->quote_dirs.insert(path);
if (ShouldAddToAngleIncludes(arg)) if (ShouldAddToAngleIncludes(normalized_path))
config->angle_dirs.insert(path); config->angle_dirs.insert(path);
break; break;
} }
@ -247,9 +251,10 @@ std::vector<Project::Entry> LoadFromDirectoryListing(ProjectConfig* config) {
for (const std::string& file : files) { for (const std::string& file : files) {
if (SourceFileType(file)) { if (SourceFileType(file)) {
CompileCommandsEntry e; CompileCommandsEntry e;
e.file = NormalizePathWithTestOptOut(file);
e.args = args;
e.directory = config->project_dir; e.directory = config->project_dir;
e.file = file;
e.args = args;
e.args.push_back(e.file);
result.push_back(GetCompilationEntryFromCompileCommandEntry(config, e)); result.push_back(GetCompilationEntryFromCompileCommandEntry(config, e));
} }
} }
@ -307,13 +312,13 @@ std::vector<Project::Entry> LoadCompilationEntriesFromDirectory(
// LOG_S(INFO) << "Got args " << StringJoin(entry.args); // LOG_S(INFO) << "Got args " << StringJoin(entry.args);
our_time.Resume(); our_time.Resume();
entry.directory = directory;
std::string absolute_filename; std::string absolute_filename;
if (!relative_filename.empty() && relative_filename[0] == '/') if (!relative_filename.empty() && relative_filename[0] == '/')
absolute_filename = relative_filename; absolute_filename = relative_filename;
else else
absolute_filename = directory + "/" + relative_filename; absolute_filename = directory + "/" + relative_filename;
entry.file = NormalizePathWithTestOptOut(absolute_filename); entry.file = NormalizePathWithTestOptOut(absolute_filename);
entry.directory = directory;
result.push_back(GetCompilationEntryFromCompileCommandEntry(config, entry)); result.push_back(GetCompilationEntryFromCompileCommandEntry(config, entry));
our_time.Pause(); our_time.Pause();
@ -516,7 +521,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", "-working-directory", "/home/user", "-xc", "-std=gnu11", "-O0", "-resource-dir=/w/resource_dir/", {"cc", "-working-directory", "/home/user", "-xc", "-std=gnu11", "-O0", "foo/bar.c", "-resource-dir=/w/resource_dir/",
"-Wno-unknown-warning-option", "-fparse-all-comments"}); "-Wno-unknown-warning-option", "-fparse-all-comments"});
} }
@ -766,56 +771,56 @@ TEST_SUITE("Project") {
"-DGR_GL_IGNORE_ES3_MSAA=0", "-DGR_GL_IGNORE_ES3_MSAA=0",
"-DSK_SUPPORT_GPU=1", "-DSK_SUPPORT_GPU=1",
"-DMESA_EGL_NO_X11_HEADERS", "-DMESA_EGL_NO_X11_HEADERS",
"-I&/w/c/s/out/Release/../..", "-I../..",
"-I&/w/c/s/out/Release/gen", "-Igen",
"-I&/w/c/s/out/Release/../../third_party/libwebp/src", "-I../../third_party/libwebp/src",
"-I&/w/c/s/out/Release/../../third_party/khronos", "-I../../third_party/khronos",
"-I&/w/c/s/out/Release/../../gpu", "-I../../gpu",
"-I&/w/c/s/out/Release/../../third_party/googletest/src/googletest/" "-I../../third_party/googletest/src/googletest/"
"include", "include",
"-I&/w/c/s/out/Release/../../third_party/WebKit", "-I../../third_party/WebKit",
"-I&/w/c/s/out/Release/gen/third_party/WebKit", "-Igen/third_party/WebKit",
"-I&/w/c/s/out/Release/../../v8/include", "-I../../v8/include",
"-I&/w/c/s/out/Release/gen/v8/include", "-Igen/v8/include",
"-I&/w/c/s/out/Release/../../third_party/icu/source/common", "-I../../third_party/icu/source/common",
"-I&/w/c/s/out/Release/../../third_party/icu/source/i18n", "-I../../third_party/icu/source/i18n",
"-I&/w/c/s/out/Release/../../third_party/protobuf/src", "-I../../third_party/protobuf/src",
"-I&/w/c/s/out/Release/gen/protoc_out", "-Igen/protoc_out",
"-I&/w/c/s/out/Release/../../third_party/protobuf/src", "-I../../third_party/protobuf/src",
"-I&/w/c/s/out/Release/../../third_party/boringssl/src/include", "-I../../third_party/boringssl/src/include",
"-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" "-I../../build/linux/debian_jessie_amd64-sysroot/"
"usr/include/nss", "usr/include/nss",
"-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" "-I../../build/linux/debian_jessie_amd64-sysroot/"
"usr/include/nspr", "usr/include/nspr",
"-I&/w/c/s/out/Release/../../skia/config", "-I../../skia/config",
"-I&/w/c/s/out/Release/../../skia/ext", "-I../../skia/ext",
"-I&/w/c/s/out/Release/../../third_party/skia/include/c", "-I../../third_party/skia/include/c",
"-I&/w/c/s/out/Release/../../third_party/skia/include/config", "-I../../third_party/skia/include/config",
"-I&/w/c/s/out/Release/../../third_party/skia/include/core", "-I../../third_party/skia/include/core",
"-I&/w/c/s/out/Release/../../third_party/skia/include/effects", "-I../../third_party/skia/include/effects",
"-I&/w/c/s/out/Release/../../third_party/skia/include/encode", "-I../../third_party/skia/include/encode",
"-I&/w/c/s/out/Release/../../third_party/skia/include/gpu", "-I../../third_party/skia/include/gpu",
"-I&/w/c/s/out/Release/../../third_party/skia/include/images", "-I../../third_party/skia/include/images",
"-I&/w/c/s/out/Release/../../third_party/skia/include/lazy", "-I../../third_party/skia/include/lazy",
"-I&/w/c/s/out/Release/../../third_party/skia/include/pathops", "-I../../third_party/skia/include/pathops",
"-I&/w/c/s/out/Release/../../third_party/skia/include/pdf", "-I../../third_party/skia/include/pdf",
"-I&/w/c/s/out/Release/../../third_party/skia/include/pipe", "-I../../third_party/skia/include/pipe",
"-I&/w/c/s/out/Release/../../third_party/skia/include/ports", "-I../../third_party/skia/include/ports",
"-I&/w/c/s/out/Release/../../third_party/skia/include/utils", "-I../../third_party/skia/include/utils",
"-I&/w/c/s/out/Release/../../third_party/skia/third_party/vulkan", "-I../../third_party/skia/third_party/vulkan",
"-I&/w/c/s/out/Release/../../third_party/skia/include/codec", "-I../../third_party/skia/include/codec",
"-I&/w/c/s/out/Release/../../third_party/skia/src/gpu", "-I../../third_party/skia/src/gpu",
"-I&/w/c/s/out/Release/../../third_party/skia/src/sksl", "-I../../third_party/skia/src/sksl",
"-I&/w/c/s/out/Release/../../third_party/ced/src", "-I../../third_party/ced/src",
"-I&/w/c/s/out/Release/../../third_party/mesa/src/include", "-I../../third_party/mesa/src/include",
"-I&/w/c/s/out/Release/../../third_party/libwebm/source", "-I../../third_party/libwebm/source",
"-I&/w/c/s/out/Release/gen", "-Igen",
"-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" "-I../../build/linux/debian_jessie_amd64-sysroot/"
"usr/include/dbus-1.0", "usr/include/dbus-1.0",
"-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" "-I../../build/linux/debian_jessie_amd64-sysroot/"
"usr/lib/x86_64-linux-gnu/dbus-1.0/include", "usr/lib/x86_64-linux-gnu/dbus-1.0/include",
"-I&/w/c/s/out/Release/../../third_party/googletest/custom", "-I../../third_party/googletest/custom",
"-I&/w/c/s/out/Release/../../third_party/googletest/src/googlemock/" "-I../../third_party/googletest/src/googlemock/"
"include", "include",
"-fno-strict-aliasing", "-fno-strict-aliasing",
"-Wno-builtin-macro-redefined", "-Wno-builtin-macro-redefined",
@ -863,16 +868,17 @@ TEST_SUITE("Project") {
"-std=gnu++14", "-std=gnu++14",
"-fno-rtti", "-fno-rtti",
"-nostdinc++", "-nostdinc++",
"-isystem&/w/c/s/out/Release/../../buildtools/third_party/libc++/" "-isystem../../buildtools/third_party/libc++/"
"trunk/" "trunk/"
"include", "include",
"-isystem&/w/c/s/out/Release/../../buildtools/third_party/libc++abi/" "-isystem../../buildtools/third_party/libc++abi/"
"trunk/" "trunk/"
"include", "include",
"--sysroot=&/w/c/s/out/Release/../../build/linux/" "--sysroot=../../build/linux/"
"debian_jessie_amd64-sysroot", "debian_jessie_amd64-sysroot",
"-fno-exceptions", "-fno-exceptions",
"-fvisibility-inlines-hidden", "-fvisibility-inlines-hidden",
"../../ash/login/ui/lock_screen_sanity_unittest.cc",
"-resource-dir=/w/resource_dir/", "-resource-dir=/w/resource_dir/",
"-Wno-unknown-warning-option", "-Wno-unknown-warning-option",
"-fparse-all-comments"}); "-fparse-all-comments"});
@ -1043,7 +1049,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++",
@ -1098,50 +1105,50 @@ TEST_SUITE("Project") {
"-DMESA_EGL_NO_X11_HEADERS", "-DMESA_EGL_NO_X11_HEADERS",
"-DBORINGSSL_SHARED_LIBRARY", "-DBORINGSSL_SHARED_LIBRARY",
"-DUSING_V8_SHARED", "-DUSING_V8_SHARED",
"-I&/w/c/s/out/Release/../..", "-I../..",
"-I&/w/c/s/out/Release/gen", "-Igen",
"-I&/w/c/s/out/Release/../../third_party/libwebp/src", "-I../../third_party/libwebp/src",
"-I&/w/c/s/out/Release/../../third_party/khronos", "-I../../third_party/khronos",
"-I&/w/c/s/out/Release/../../gpu", "-I../../gpu",
"-I&/w/c/s/out/Release/../../third_party/ced/src", "-I../../third_party/ced/src",
"-I&/w/c/s/out/Release/../../third_party/icu/source/common", "-I../../third_party/icu/source/common",
"-I&/w/c/s/out/Release/../../third_party/icu/source/i18n", "-I../../third_party/icu/source/i18n",
"-I&/w/c/s/out/Release/../../third_party/protobuf/src", "-I../../third_party/protobuf/src",
"-I&/w/c/s/out/Release/../../skia/config", "-I../../skia/config",
"-I&/w/c/s/out/Release/../../skia/ext", "-I../../skia/ext",
"-I&/w/c/s/out/Release/../../third_party/skia/include/c", "-I../../third_party/skia/include/c",
"-I&/w/c/s/out/Release/../../third_party/skia/include/config", "-I../../third_party/skia/include/config",
"-I&/w/c/s/out/Release/../../third_party/skia/include/core", "-I../../third_party/skia/include/core",
"-I&/w/c/s/out/Release/../../third_party/skia/include/effects", "-I../../third_party/skia/include/effects",
"-I&/w/c/s/out/Release/../../third_party/skia/include/encode", "-I../../third_party/skia/include/encode",
"-I&/w/c/s/out/Release/../../third_party/skia/include/gpu", "-I../../third_party/skia/include/gpu",
"-I&/w/c/s/out/Release/../../third_party/skia/include/images", "-I../../third_party/skia/include/images",
"-I&/w/c/s/out/Release/../../third_party/skia/include/lazy", "-I../../third_party/skia/include/lazy",
"-I&/w/c/s/out/Release/../../third_party/skia/include/pathops", "-I../../third_party/skia/include/pathops",
"-I&/w/c/s/out/Release/../../third_party/skia/include/pdf", "-I../../third_party/skia/include/pdf",
"-I&/w/c/s/out/Release/../../third_party/skia/include/pipe", "-I../../third_party/skia/include/pipe",
"-I&/w/c/s/out/Release/../../third_party/skia/include/ports", "-I../../third_party/skia/include/ports",
"-I&/w/c/s/out/Release/../../third_party/skia/include/utils", "-I../../third_party/skia/include/utils",
"-I&/w/c/s/out/Release/../../third_party/skia/third_party/vulkan", "-I../../third_party/skia/third_party/vulkan",
"-I&/w/c/s/out/Release/../../third_party/skia/src/gpu", "-I../../third_party/skia/src/gpu",
"-I&/w/c/s/out/Release/../../third_party/skia/src/sksl", "-I../../third_party/skia/src/sksl",
"-I&/w/c/s/out/Release/../../third_party/mesa/src/include", "-I../../third_party/mesa/src/include",
"-I&/w/c/s/out/Release/../../third_party/libwebm/source", "-I../../third_party/libwebm/source",
"-I&/w/c/s/out/Release/../../third_party/protobuf/src", "-I../../third_party/protobuf/src",
"-I&/w/c/s/out/Release/gen/protoc_out", "-Igen/protoc_out",
"-I&/w/c/s/out/Release/../../third_party/boringssl/src/include", "-I../../third_party/boringssl/src/include",
"-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" "-I../../build/linux/debian_jessie_amd64-sysroot/"
"usr/include/nss", "usr/include/nss",
"-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" "-I../../build/linux/debian_jessie_amd64-sysroot/"
"usr/include/nspr", "usr/include/nspr",
"-I&/w/c/s/out/Release/gen", "-Igen",
"-I&/w/c/s/out/Release/../../third_party/WebKit", "-I../../third_party/WebKit",
"-I&/w/c/s/out/Release/gen/third_party/WebKit", "-Igen/third_party/WebKit",
"-I&/w/c/s/out/Release/../../v8/include", "-I../../v8/include",
"-I&/w/c/s/out/Release/gen/v8/include", "-Igen/v8/include",
"-I&/w/c/s/out/Release/gen", "-Igen",
"-I&/w/c/s/out/Release/../../third_party/flatbuffers/src/include", "-I../../third_party/flatbuffers/src/include",
"-I&/w/c/s/out/Release/gen", "-Igen",
"-fno-strict-aliasing", "-fno-strict-aliasing",
"-Wno-builtin-macro-redefined", "-Wno-builtin-macro-redefined",
"-D__DATE__=", "-D__DATE__=",
@ -1186,16 +1193,17 @@ TEST_SUITE("Project") {
"-std=gnu++14", "-std=gnu++14",
"-fno-rtti", "-fno-rtti",
"-nostdinc++", "-nostdinc++",
"-isystem&/w/c/s/out/Release/../../buildtools/third_party/libc++/" "-isystem../../buildtools/third_party/libc++/"
"trunk/" "trunk/"
"include", "include",
"-isystem&/w/c/s/out/Release/../../buildtools/third_party/libc++abi/" "-isystem../../buildtools/third_party/libc++abi/"
"trunk/" "trunk/"
"include", "include",
"--sysroot=&/w/c/s/out/Release/../../build/linux/" "--sysroot=../../build/linux/"
"debian_jessie_amd64-sysroot", "debian_jessie_amd64-sysroot",
"-fno-exceptions", "-fno-exceptions",
"-fvisibility-inlines-hidden", "-fvisibility-inlines-hidden",
"../../apps/app_lifetime_monitor.cc",
"-resource-dir=/w/resource_dir/", "-resource-dir=/w/resource_dir/",
"-Wno-unknown-warning-option", "-Wno-unknown-warning-option",
"-fparse-all-comments"}); "-fparse-all-comments"});

View File

@ -158,6 +158,7 @@ bool RunIndexTests(const std::string& filter_path, bool enable_update) {
std::cout << "For " << path << std::endl; std::cout << "For " << path << std::endl;
std::cout << " flags: " << StringJoin(flags) << std::endl; std::cout << " flags: " << StringJoin(flags) << std::endl;
} }
flags.push_back(path);
// Run test. // Run test.
Config config; Config config;