From aba38a9da1b9280200116eef95eac71ffa7cb87d Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Tue, 13 Feb 2018 21:23:35 -0800 Subject: [PATCH] Ask user for relevant diagnostics when clang translation unit creation fails. Closes issue #450. --- src/clang_translation_unit.cc | 18 ++++++++++-------- src/project.cc | 25 ++++++++++++++++--------- 2 files changed, 26 insertions(+), 17 deletions(-) diff --git a/src/clang_translation_unit.cc b/src/clang_translation_unit.cc index 1ad8e2e6..4a26921d 100644 --- a/src/clang_translation_unit.cc +++ b/src/clang_translation_unit.cc @@ -93,24 +93,26 @@ std::unique_ptr ClangTranslationUnit::Create( if (error_code != CXError_Success && cx_tu) EmitDiagnostics(filepath, args, cx_tu); + auto make_msg = [&]() { + return "Please try running the following, identify which flag causes the " + "issue, and report a bug. cquery will then filter the flag for you " + " automatically:\n$ " + StringJoin(args, " "); + }; + switch (error_code) { case CXError_Success: return MakeUnique(cx_tu); case CXError_Failure: - LOG_S(ERROR) << "libclang generic failure for " << filepath - << " with args " << StringJoin(args); + LOG_S(ERROR) << "libclang generic failure for " << filepath << ". " << make_msg(); return nullptr; case CXError_Crashed: - LOG_S(ERROR) << "libclang crashed for " << filepath << " with args " - << StringJoin(args); + LOG_S(ERROR) << "libclang crashed for " << filepath << ". " << make_msg(); return nullptr; case CXError_InvalidArguments: - LOG_S(ERROR) << "libclang had invalid arguments for " - << " with args " << StringJoin(args) << filepath; + LOG_S(ERROR) << "libclang had invalid arguments for " << filepath << ". " << make_msg(); return nullptr; case CXError_ASTReadError: - LOG_S(ERROR) << "libclang had ast read error for " << filepath - << " with args " << StringJoin(args); + LOG_S(ERROR) << "libclang had ast read error for " << filepath << ". " << make_msg(); return nullptr; } diff --git a/src/project.cc b/src/project.cc index fbade5ca..337deea5 100644 --- a/src/project.cc +++ b/src/project.cc @@ -265,7 +265,12 @@ Project::Entry GetCompilationEntryFromCompileCommandEntry( if (!AnyStartsWith(result.args, "-Wno-unknown-warning-option")) result.args.push_back("-Wno-unknown-warning-option"); - // Using -fparse-all-comments enables documententation in the indexer and in + // We sometimes dump the command to logs and ask the user to run it. Include + // -fsyntax-only so they don't do a full compile. + if (!AnyStartsWith(result.args, "-fsyntax-only")) + result.args.push_back("-fsyntax-only"); + + // Using -fparse-all-comments enables documentation in the indexer and in // code completion. if (init_opts->index.comments > 1 && !AnyStartsWith(result.args, "-fparse-all-comments")) { @@ -598,28 +603,28 @@ TEST_SUITE("Project") { /* expected */ {"clang", "-working-directory", "/dir/", "-xc++", "-std=c++14", "-lstdc++", "&/dir/myfile.cc", "-resource-dir=/w/resource_dir/", - "-Wno-unknown-warning-option", "-fparse-all-comments"}); + "-Wno-unknown-warning-option", "-fsyntax-only", "-fparse-all-comments"}); CheckFlags( /* raw */ {"clang.exe"}, /* expected */ {"clang.exe", "-working-directory", "/dir/", "-xc++", "-std=c++14", "-resource-dir=/w/resource_dir/", "-Wno-unknown-warning-option", - "-fparse-all-comments"}); + "-fsyntax-only", "-fparse-all-comments"}); CheckFlags( /* raw */ {"goma", "clang"}, /* expected */ {"clang", "-working-directory", "/dir/", "-xc++", "-std=c++14", "-resource-dir=/w/resource_dir/", "-Wno-unknown-warning-option", - "-fparse-all-comments"}); + "-fsyntax-only", "-fparse-all-comments"}); CheckFlags( /* raw */ {"goma", "clang", "--foo"}, /* expected */ {"clang", "-working-directory", "/dir/", "-xc++", "-std=c++14", "--foo", "-resource-dir=/w/resource_dir/", "-Wno-unknown-warning-option", - "-fparse-all-comments"}); + "-fsyntax-only", "-fparse-all-comments"}); } TEST_CASE("Windows path normalization") { @@ -628,7 +633,7 @@ TEST_SUITE("Project") { /* expected */ {"clang", "-working-directory", "E:/workdir", "-xc++", "-std=c++14", "&E:/workdir/bar.cc", "-resource-dir=/w/resource_dir/", - "-Wno-unknown-warning-option", "-fparse-all-comments"}); + "-Wno-unknown-warning-option", "-fsyntax-only", "-fparse-all-comments"}); CheckFlags( "E:/workdir", "E:/workdir/bar.cc", @@ -636,7 +641,7 @@ TEST_SUITE("Project") { /* expected */ {"clang", "-working-directory", "E:/workdir", "-xc++", "-std=c++14", "&E:/workdir/bar.cc", "-resource-dir=/w/resource_dir/", - "-Wno-unknown-warning-option", "-fparse-all-comments"}); + "-Wno-unknown-warning-option", "-fsyntax-only", "-fparse-all-comments"}); } TEST_CASE("Path in args") { @@ -646,7 +651,7 @@ TEST_SUITE("Project") { /* expected */ {"cc", "-working-directory", "/home/user", "-xc", "-std=gnu11", "-O0", "&/home/user/foo/bar.c", "-resource-dir=/w/resource_dir/", - "-Wno-unknown-warning-option", "-fparse-all-comments"}); + "-Wno-unknown-warning-option", "-fsyntax-only", "-fparse-all-comments"}); } TEST_CASE("Implied binary") { @@ -656,7 +661,7 @@ TEST_SUITE("Project") { /* expected */ {"clang++", "-working-directory", "/home/user", "-xc++", "-std=c++14", "-DDONT_IGNORE_ME", "-resource-dir=/w/resource_dir/", - "-Wno-unknown-warning-option", "-fparse-all-comments"}); + "-Wno-unknown-warning-option", "-fsyntax-only", "-fparse-all-comments"}); } // Checks flag parsing for a random chromium file in comparison to what @@ -1007,6 +1012,7 @@ TEST_SUITE("Project") { "lock_screen_sanity_unittest.cc", "-resource-dir=/w/resource_dir/", "-Wno-unknown-warning-option", + "-fsyntax-only", "-fparse-all-comments"}); } @@ -1332,6 +1338,7 @@ TEST_SUITE("Project") { "&/w/c/s/out/Release/../../apps/app_lifetime_monitor.cc", "-resource-dir=/w/resource_dir/", "-Wno-unknown-warning-option", + "-fsyntax-only", "-fparse-all-comments"}); }