diff --git a/src/project.cc b/src/project.cc index f0d41863..62026507 100644 --- a/src/project.cc +++ b/src/project.cc @@ -6,7 +6,6 @@ #include "serializer.h" #include "utils.h" - #include #include #include @@ -27,6 +26,11 @@ MAKE_REFLECT_STRUCT(CompileCommandsEntry, directory, file, command, args); namespace { +struct IncludeDirectories { + std::unordered_set quote_dirs; + std::unordered_set angle_dirs; +}; + static const char* kBlacklistMulti[] = {"-MF", "-Xclang"}; // Blacklisted flags which are always removed from the command line. @@ -84,8 +88,7 @@ bool IsCFile(const std::string& path) { } Project::Entry GetCompilationEntryFromCompileCommandEntry( - std::unordered_set& quote_includes, - std::unordered_set& angle_includes, + IncludeDirectories* includes, const std::vector& extra_flags, const CompileCommandsEntry& entry) { Project::Entry result; @@ -119,9 +122,9 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry( make_next_flag_absolute = false; if (add_next_flag_quote) - quote_includes.insert(arg); + includes->quote_dirs.insert(arg); if (add_next_flag_angle) - angle_includes.insert(arg); + includes->angle_dirs.insert(arg); add_next_flag_quote = false; add_next_flag_angle = false; } @@ -145,9 +148,9 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry( arg = flag_type + path; } if (ShouldAddToQuoteIncludes(arg)) - quote_includes.insert(path); + includes->quote_dirs.insert(path); if (ShouldAddToAngleIncludes(arg)) - angle_includes.insert(path); + includes->angle_dirs.insert(path); break; } } @@ -208,8 +211,7 @@ angle_includes, extra_flags, entry)); */ std::vector LoadFromDirectoryListing( - std::unordered_set& quote_includes, - std::unordered_set& angle_includes, + IncludeDirectories* includes, const std::vector& extra_flags, const std::string& project_directory) { std::vector result; @@ -234,8 +236,8 @@ std::vector LoadFromDirectoryListing( CompileCommandsEntry e; e.file = NormalizePath(file); e.args = args; - result.push_back(GetCompilationEntryFromCompileCommandEntry( - quote_includes, angle_includes, extra_flags, e)); + result.push_back( + GetCompilationEntryFromCompileCommandEntry(includes, extra_flags, e)); } } @@ -243,8 +245,7 @@ std::vector LoadFromDirectoryListing( } std::vector LoadCompilationEntriesFromDirectory( - std::unordered_set& quote_includes, - std::unordered_set& angle_includes, + IncludeDirectories* includes, const std::vector& extra_flags, const std::string& project_directory) { // TODO: Figure out if this function or the clang one is faster. @@ -258,8 +259,7 @@ std::vector LoadCompilationEntriesFromDirectory( std::cerr << "Unable to load compile_commands.json located at \"" << project_directory << "\"; using directory listing instead." << std::endl; - return LoadFromDirectoryListing(quote_includes, angle_includes, extra_flags, - project_directory); + return LoadFromDirectoryListing(includes, extra_flags, project_directory); } CXCompileCommands cx_commands = @@ -288,7 +288,7 @@ std::vector LoadCompilationEntriesFromDirectory( clang::ToString(clang_CompileCommand_getArg(cx_command, j))); result.push_back(GetCompilationEntryFromCompileCommandEntry( - quote_includes, angle_includes, extra_flags, entry)); + includes, extra_flags, entry)); } clang_CompileCommands_dispose(cx_commands); @@ -339,16 +339,14 @@ int ComputeGuessScore(const std::string& a, const std::string& b) { void Project::Load(const std::vector& extra_flags, const std::string& directory) { - std::unordered_set unique_quote_includes; - std::unordered_set unique_angle_includes; + IncludeDirectories includes; + entries = + LoadCompilationEntriesFromDirectory(&includes, extra_flags, directory); - entries = LoadCompilationEntriesFromDirectory( - unique_quote_includes, unique_angle_includes, extra_flags, directory); - - quote_include_directories.assign(unique_quote_includes.begin(), - unique_quote_includes.end()); - angle_include_directories.assign(unique_angle_includes.begin(), - unique_angle_includes.end()); + quote_include_directories.assign(includes.quote_dirs.begin(), + includes.quote_dirs.end()); + angle_include_directories.assign(includes.angle_dirs.begin(), + includes.angle_dirs.end()); for (std::string& path : quote_include_directories) { EnsureEndsInSlash(path); @@ -410,6 +408,114 @@ void Project::ForAllFilteredFiles( TEST_SUITE("Project"); +#if false +TEST_CASE("chromium") { + std::string compile_commands = R"( + [ + { + "directory": "/work2/chrome/src/out/Release", + "command": "/work/goma/gomacc ../../third_party/llvm-build/Release+Asserts/bin/clang++ -MMD -MF obj/apps/apps/app_lifetime_monitor.o.d -DV8_DEPRECATION_WARNINGS -DDCHECK_ALWAYS_ON=1 -DUSE_UDEV -DUSE_ASH=1 -DUSE_AURA=1 -DUSE_NSS_CERTS=1 -DUSE_OZONE=1 -DDISABLE_NACL -DFULL_SAFE_BROWSING -DSAFE_BROWSING_CSD -DSAFE_BROWSING_DB_LOCAL -DCHROMIUM_BUILD -DFIELDTRIAL_TESTING_ENABLED -DCR_CLANG_REVISION=\"310694-1\" -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -DCOMPONENT_BUILD -DOS_CHROMEOS -DNDEBUG -DNVALGRIND -DDYNAMIC_ANNOTATIONS_ENABLED=0 -DGL_GLEXT_PROTOTYPES -DUSE_GLX -DUSE_EGL -DANGLE_ENABLE_RELEASE_ASSERTS -DTOOLKIT_VIEWS=1 -DV8_USE_EXTERNAL_STARTUP_DATA -DU_USING_ICU_NAMESPACE=0 -DU_ENABLE_DYLOAD=0 -DICU_UTIL_DATA_IMPL=ICU_UTIL_DATA_FILE -DUCHAR_TYPE=uint16_t -DGOOGLE_PROTOBUF_NO_RTTI -DGOOGLE_PROTOBUF_NO_STATIC_INITIALIZER -DHAVE_PTHREAD -DPROTOBUF_USE_DLLS -DSK_IGNORE_LINEONLY_AA_CONVEX_PATH_OPTS -DSK_HAS_PNG_LIBRARY -DSK_HAS_WEBP_LIBRARY -DSK_HAS_JPEG_LIBRARY -DSKIA_DLL -DGR_GL_IGNORE_ES3_MSAA=0 -DSK_SUPPORT_GPU=1 -DMESA_EGL_NO_X11_HEADERS -DBORINGSSL_SHARED_LIBRARY -DUSING_V8_SHARED -I../.. -Igen -I../../third_party/libwebp/src -I../../third_party/khronos -I../../gpu -I../../third_party/ced/src -I../../third_party/icu/source/common -I../../third_party/icu/source/i18n -I../../third_party/protobuf/src -I../../skia/config -I../../skia/ext -I../../third_party/skia/include/c -I../../third_party/skia/include/config -I../../third_party/skia/include/core -I../../third_party/skia/include/effects -I../../third_party/skia/include/encode -I../../third_party/skia/include/gpu -I../../third_party/skia/include/images -I../../third_party/skia/include/lazy -I../../third_party/skia/include/pathops -I../../third_party/skia/include/pdf -I../../third_party/skia/include/pipe -I../../third_party/skia/include/ports -I../../third_party/skia/include/utils -I../../third_party/skia/third_party/vulkan -I../../third_party/skia/src/gpu -I../../third_party/skia/src/sksl -I../../third_party/mesa/src/include -I../../third_party/libwebm/source -I../../third_party/protobuf/src -Igen/protoc_out -I../../third_party/boringssl/src/include -I../../build/linux/debian_jessie_amd64-sysroot/usr/include/nss -I../../build/linux/debian_jessie_amd64-sysroot/usr/include/nspr -Igen -I../../third_party/WebKit -Igen/third_party/WebKit -I../../v8/include -Igen/v8/include -Igen -I../../third_party/flatbuffers/src/include -Igen -fno-strict-aliasing -Wno-builtin-macro-redefined -D__DATE__= -D__TIME__= -D__TIMESTAMP__= -funwind-tables -fPIC -pipe -B../../third_party/binutils/Linux_x64/Release/bin -pthread -fcolor-diagnostics -m64 -march=x86-64 -Wall -Werror -Wextra -Wno-missing-field-initializers -Wno-unused-parameter -Wno-c++11-narrowing -Wno-covered-switch-default -Wno-unneeded-internal-declaration -Wno-inconsistent-missing-override -Wno-undefined-var-template -Wno-nonportable-include-path -Wno-address-of-packed-member -Wno-unused-lambda-capture -Wno-user-defined-warnings -Wno-enum-compare-switch -O2 -fno-ident -fdata-sections -ffunction-sections -fno-omit-frame-pointer -g0 -fvisibility=hidden -Xclang -load -Xclang ../../third_party/llvm-build/Release+Asserts/lib/libFindBadConstructs.so -Xclang -add-plugin -Xclang find-bad-constructs -Xclang -plugin-arg-find-bad-constructs -Xclang check-auto-raw-pointer -Xclang -plugin-arg-find-bad-constructs -Xclang check-ipc -Wheader-hygiene -Wstring-conversion -Wtautological-overlap-compare -Wexit-time-destructors -Wno-header-guard -Wno-exit-time-destructors -std=gnu++14 -fno-rtti -nostdinc++ -isystem../../buildtools/third_party/libc++/trunk/include -isystem../../buildtools/third_party/libc++abi/trunk/include --sysroot=../../build/linux/debian_jessie_amd64-sysroot -fno-exceptions -fvisibility-inlines-hidden -c ../../apps/app_lifetime_monitor.cc -o obj/apps/apps/app_lifetime_monitor.o", + "file": "../../apps/app_lifetime_monitor.cc" + } + ] + )"; + + std::string project_directory = "/work2/chrome/src/"; + std::vector extra_flags; + + std::unordered_set quote_includes; + std::unordered_set angle_includes; + std::vector result; + + REQUIRE(TryLoadFromCompileCommandsJson(&result, "e_includes, &angle_includes, project_directory, extra_flags, compile_commands)); + + std::vector expected_args{ + "-DV8_DEPRECATION_WARNINGS", "-DDCHECK_ALWAYS_ON=1", "-DUSE_UDEV", "-DUSE_ASH=1", "-DUSE_AURA=1", "-DUSE_NSS_CERTS=1", "-DUSE_OZONE=1", "-DDISABLE_NACL", "-DFULL_SAFE_BROWSING", "-DSAFE_BROWSING_CSD", "-DSAFE_BROWSING_DB_LOCAL", "-DCHROMIUM_BUILD", "-DFIELDTRIAL_TESTING_ENABLED", "-DCR_CLANG_REVISION=\"310694-1\"", "-D_FILE_OFFSET_BITS=64", "-D_LARGEFILE_SOURCE", "-D_LARGEFILE64_SOURCE", "-D__STDC_CONSTANT_MACROS", "-D__STDC_FORMAT_MACROS", "-DCOMPONENT_BUILD", "-DOS_CHROMEOS", "-DNDEBUG", "-DNVALGRIND", "-DDYNAMIC_ANNOTATIONS_ENABLED=0", "-DGL_GLEXT_PROTOTYPES", "-DUSE_GLX", "-DUSE_EGL", "-DANGLE_ENABLE_RELEASE_ASSERTS", "-DTOOLKIT_VIEWS=1", "-DV8_USE_EXTERNAL_STARTUP_DATA", "-DU_USING_ICU_NAMESPACE=0", "-DU_ENABLE_DYLOAD=0", "-DICU_UTIL_DATA_IMPL=ICU_UTIL_DATA_FILE", "-DUCHAR_TYPE=uint16_t", "-DGOOGLE_PROTOBUF_NO_RTTI", "-DGOOGLE_PROTOBUF_NO_STATIC_INITIALIZER", "-DHAVE_PTHREAD", "-DPROTOBUF_USE_DLLS", "-DSK_IGNORE_LINEONLY_AA_CONVEX_PATH_OPTS", "-DSK_HAS_PNG_LIBRARY", "-DSK_HAS_WEBP_LIBRARY", "-DSK_HAS_JPEG_LIBRARY", "-DSKIA_DLL", "-DGR_GL_IGNORE_ES3_MSAA=0", "-DSK_SUPPORT_GPU=1", "-DMESA_EGL_NO_X11_HEADERS", "-DBORINGSSL_SHARED_LIBRARY", "-DUSING_V8_SHARED", "-IC:/work2/chrome/src", "-IC:/work2/chrome/src/out/Release/gen", "-IC:/work2/chrome/src/third_party/libwebp/src", "-IC:/work2/chrome/src/third_party/khronos", "-IC:/work2/chrome/src/gpu", "-IC:/work2/chrome/src/third_party/ced/src", "-IC:/work2/chrome/src/third_party/icu/source/common", "-IC:/work2/chrome/src/third_party/icu/source/i18n", "-IC:/work2/chrome/src/third_party/protobuf/src", "-IC:/work2/chrome/src/skia/config", "-IC:/work2/chrome/src/skia/ext", "-IC:/work2/chrome/src/third_party/skia/include/c", "-IC:/work2/chrome/src/third_party/skia/include/config", "-IC:/work2/chrome/src/third_party/skia/include/core", "-IC:/work2/chrome/src/third_party/skia/include/effects", "-IC:/work2/chrome/src/third_party/skia/include/encode", "-IC:/work2/chrome/src/third_party/skia/include/gpu", "-IC:/work2/chrome/src/third_party/skia/include/images", "-IC:/work2/chrome/src/third_party/skia/include/lazy", "-IC:/work2/chrome/src/third_party/skia/include/pathops", "-IC:/work2/chrome/src/third_party/skia/include/pdf", "-IC:/work2/chrome/src/third_party/skia/include/pipe", "-IC:/work2/chrome/src/third_party/skia/include/ports", "-IC:/work2/chrome/src/third_party/skia/include/utils", "-IC:/work2/chrome/src/third_party/skia/third_party/vulkan", "-IC:/work2/chrome/src/third_party/skia/src/gpu", "-IC:/work2/chrome/src/third_party/skia/src/sksl", "-IC:/work2/chrome/src/third_party/mesa/src/include", "-IC:/work2/chrome/src/third_party/libwebm/source", "-IC:/work2/chrome/src/third_party/protobuf/src", "-IC:/work2/chrome/src/out/Release/gen/protoc_out", "-IC:/work2/chrome/src/third_party/boringssl/src/include", "-IC:/work2/chrome/src/build/linux/debian_jessie_amd64-sysroot/usr/include/nss", "-IC:/work2/chrome/src/build/linux/debian_jessie_amd64-sysroot/usr/include/nspr", "-IC:/work2/chrome/src/out/Release/gen", "-IC:/work2/chrome/src/third_party/WebKit", "-IC:/work2/chrome/src/out/Release/gen/third_party/WebKit", "-IC:/work2/chrome/src/v8/include", "-IC:/work2/chrome/src/out/Release/gen/v8/include", "-IC:/work2/chrome/src/out/Release/gen", "-IC:/work2/chrome/src/third_party/flatbuffers/src/include", "-IC:/work2/chrome/src/out/Release/gen", "-fno-strict-aliasing", "-Wno-builtin-macro-redefined", "-D__DATE__=", "-D__TIME__=", "-D__TIMESTAMP__=", "-funwind-tables", "-fPIC", "-pipe", "-pthread", "-fcolor-diagnostics", "-m64", "-Wall", "-Werror", "-Wextra", "-Wno-missing-field-initializers", "-Wno-unused-parameter", "-Wno-c++11-narrowing", "-Wno-covered-switch-default", "-Wno-unneeded-internal-declaration", "-Wno-inconsistent-missing-override", "-Wno-undefined-var-template", "-Wno-nonportable-include-path", "-Wno-address-of-packed-member", "-Wno-user-defined-warnings", "-Wno-enum-compare-switch", "-O2", "-fno-ident", "-fdata-sections", "-ffunction-sections", "-fno-omit-frame-pointer", "-g0", "-fvisibility=hidden", "-Wheader-hygiene", "-Wstring-conversion", "-Wtautological-overlap-compare", "-Wexit-time-destructors", "-Wno-header-guard", "-Wno-exit-time-destructors", "-std=gnu++14", "-fno-rtti", "-nostdinc++", "-isystemC:/work2/chrome/src/buildtools/third_party/libc++/trunk/include", "-isystemC:/work2/chrome/src/buildtools/third_party/libc++abi/trunk/include", "--sysroot=C:/work2/chrome/src/build/linux/debian_jessie_amd64-sysroot", "-fno-exceptions", "-fvisibility-inlines-hidden", "-c", "-o", "obj/apps/apps/app_lifetime_monitor.o", "-xc++" + //"-DV8_DEPRECATION_WARNINGS" + }; + std::cout << "Expected - Actual\n\n"; + for (int i = 0; i < std::min(result[0].args.size(), expected_args.size()); ++i) { + if (result[0].args[i] != expected_args[i]) + std::cout << "mismatch at " << i << "; expected " << expected_args[i] << " but got " << result[0].args[i] << std::endl; + } + //std::cout << StringJoin(expected_args) << "\n"; + //std::cout << StringJoin(result[0].args) << "\n"; + REQUIRE(result.size() == 1); + REQUIRE(result[0].args == expected_args); +} + +TEST_CASE("argument parsing") { + std::string compile_commands = R"( + [ + { + "directory": "/usr/local/google/code/naive/build", + "command": "clang++ -std=c++14 -Werror -I/usr/include/pixman-1 -I/usr/include/libdrm -I/usr/local/google/code/naive/src -o CMakeFiles/naive.dir/src/wm/window.cc.o -c /usr/local/google/code/naive/src/wm/window.cc", + "file": "/usr/local/google/code/naive/src/wm/window.cc" + } + ] + )"; + + std::string project_directory = "/usr/local/google/code/naive/src/"; + std::vector extra_flags; + + std::unordered_set quote_includes; + std::unordered_set angle_includes; + std::vector result; + + REQUIRE(TryLoadFromCompileCommandsJson(&result, "e_includes, &angle_includes, project_directory, extra_flags, compile_commands)); + + std::vector expected_args{ + "-std=c++14", + "-Werror", + "-I/usr/include/pixman-1", + "-I/usr/include/libdrm", + "-I/usr/local/google/code/naive/src", + "-o", + "CMakeFiles/naive.dir/src/wm/window.cc.o", + "-c", + "/usr/local/google/code/naive/src/wm/window.cc", + "-xc++" + }; + REQUIRE(result.size() == 1); + REQUIRE(result[0].args == expected_args); +} + +TEST_CASE("relative directories") { + std::string compile_commands = R"( + [ + { + "directory": "/build", + "command": "clang++ -std=c++14 -Werror", + "file": "bar/foo.cc" + } + ] + )"; + + std::string project_directory = "/build/"; + std::vector extra_flags; + + std::unordered_set quote_includes; + std::unordered_set angle_includes; + std::vector result; + + REQUIRE(TryLoadFromCompileCommandsJson(&result, "e_includes, &angle_includes, project_directory, extra_flags, compile_commands)); + REQUIRE(result.size() == 1); + REQUIRE(result[0].filename == "/build/bar/foo.cc"); +} + +TEST_CASE("Directory extraction") { + std::string build_directory = "/base/"; + std::string filename = "foo.cc"; + std::vector args = { "-I/absolute1", "-I", "/absolute2", "-Irelative1", "-I", "relative2" }; + std::unordered_set angle_includes; + std::unordered_set quote_includes; + CleanupArguments(build_directory, filename, &args, &angle_includes, "e_includes); + + std::unordered_set expected{ "/absolute1", "/absolute2", "/base/relative1", "/base/relative2" }; + REQUIRE(angle_includes == expected); +} +#endif + TEST_CASE("Entry inference") { Project p; { diff --git a/src/project.h b/src/project.h index 3282051c..ce568f89 100644 --- a/src/project.h +++ b/src/project.h @@ -42,6 +42,7 @@ struct Project { // will infer one based on existing project structure. Entry FindCompilationEntryForFile(const std::string& filename); + // Run |action| on every file in the project. void ForAllFilteredFiles( Config* config, std::function action);