diff --git a/src/command_line.cc b/src/command_line.cc index 5f983dac..c746aad2 100644 --- a/src/command_line.cc +++ b/src/command_line.cc @@ -3257,193 +3257,187 @@ int main(int argc, char** argv) { } } -TEST_SUITE("LexFunctionDeclaration"); +TEST_SUITE("LexFunctionDeclaration") { + TEST_CASE("simple") { + std::string buffer_content = " void Foo(); "; + lsPosition declaration = CharPos(buffer_content, 'F'); + std::string insert_text; + int newlines_after_name = 0; -TEST_CASE("simple") { - std::string buffer_content = " void Foo(); "; - lsPosition declaration = CharPos(buffer_content, 'F'); - std::string insert_text; - int newlines_after_name = 0; + LexFunctionDeclaration(buffer_content, declaration, nullopt, &insert_text, + &newlines_after_name); + REQUIRE(insert_text == "void Foo() {\n}"); + REQUIRE(newlines_after_name == 0); - LexFunctionDeclaration(buffer_content, declaration, nullopt, &insert_text, - &newlines_after_name); - REQUIRE(insert_text == "void Foo() {\n}"); - REQUIRE(newlines_after_name == 0); + LexFunctionDeclaration(buffer_content, declaration, std::string("Type"), + &insert_text, &newlines_after_name); + REQUIRE(insert_text == "void Type::Foo() {\n}"); + REQUIRE(newlines_after_name == 0); + } - LexFunctionDeclaration(buffer_content, declaration, std::string("Type"), - &insert_text, &newlines_after_name); - REQUIRE(insert_text == "void Type::Foo() {\n}"); - REQUIRE(newlines_after_name == 0); + TEST_CASE("ctor") { + std::string buffer_content = " Foo(); "; + lsPosition declaration = CharPos(buffer_content, 'F'); + std::string insert_text; + int newlines_after_name = 0; + + LexFunctionDeclaration(buffer_content, declaration, std::string("Foo"), + &insert_text, &newlines_after_name); + REQUIRE(insert_text == "Foo::Foo() {\n}"); + REQUIRE(newlines_after_name == 0); + } + + TEST_CASE("dtor") { + std::string buffer_content = " ~Foo(); "; + lsPosition declaration = CharPos(buffer_content, '~'); + std::string insert_text; + int newlines_after_name = 0; + + LexFunctionDeclaration(buffer_content, declaration, std::string("Foo"), + &insert_text, &newlines_after_name); + REQUIRE(insert_text == "Foo::~Foo() {\n}"); + REQUIRE(newlines_after_name == 0); + } + + TEST_CASE("complex return type") { + std::string buffer_content = " std::vector Foo(); "; + lsPosition declaration = CharPos(buffer_content, 'F'); + std::string insert_text; + int newlines_after_name = 0; + + LexFunctionDeclaration(buffer_content, declaration, nullopt, &insert_text, + &newlines_after_name); + REQUIRE(insert_text == "std::vector Foo() {\n}"); + REQUIRE(newlines_after_name == 0); + + LexFunctionDeclaration(buffer_content, declaration, std::string("Type"), + &insert_text, &newlines_after_name); + REQUIRE(insert_text == "std::vector Type::Foo() {\n}"); + REQUIRE(newlines_after_name == 0); + } + + TEST_CASE("extra complex return type") { + std::string buffer_content = " std::function < int() > \n Foo(); "; + lsPosition declaration = CharPos(buffer_content, 'F'); + std::string insert_text; + int newlines_after_name = 0; + + LexFunctionDeclaration(buffer_content, declaration, nullopt, &insert_text, + &newlines_after_name); + REQUIRE(insert_text == "std::function < int() > \n Foo() {\n}"); + REQUIRE(newlines_after_name == 0); + + LexFunctionDeclaration(buffer_content, declaration, std::string("Type"), + &insert_text, &newlines_after_name); + REQUIRE(insert_text == "std::function < int() > \n Type::Foo() {\n}"); + REQUIRE(newlines_after_name == 0); + } + + TEST_CASE("parameters") { + std::string buffer_content = "void Foo(int a,\n\n int b); "; + lsPosition declaration = CharPos(buffer_content, 'F'); + std::string insert_text; + int newlines_after_name = 0; + + LexFunctionDeclaration(buffer_content, declaration, nullopt, &insert_text, + &newlines_after_name); + REQUIRE(insert_text == "void Foo(int a,\n\n int b) {\n}"); + REQUIRE(newlines_after_name == 2); + + LexFunctionDeclaration(buffer_content, declaration, std::string("Type"), + &insert_text, &newlines_after_name); + REQUIRE(insert_text == "void Type::Foo(int a,\n\n int b) {\n}"); + REQUIRE(newlines_after_name == 2); + } } -TEST_CASE("ctor") { - std::string buffer_content = " Foo(); "; - lsPosition declaration = CharPos(buffer_content, 'F'); - std::string insert_text; - int newlines_after_name = 0; +TEST_SUITE("LexWordAroundPos") { + TEST_CASE("edges") { + std::string content = "Foobar"; + REQUIRE(LexWordAroundPos(CharPos(content, 'F'), content) == "Foobar"); + REQUIRE(LexWordAroundPos(CharPos(content, 'o'), content) == "Foobar"); + REQUIRE(LexWordAroundPos(CharPos(content, 'b'), content) == "Foobar"); + REQUIRE(LexWordAroundPos(CharPos(content, 'a'), content) == "Foobar"); + REQUIRE(LexWordAroundPos(CharPos(content, 'r'), content) == "Foobar"); + } - LexFunctionDeclaration(buffer_content, declaration, std::string("Foo"), - &insert_text, &newlines_after_name); - REQUIRE(insert_text == "Foo::Foo() {\n}"); - REQUIRE(newlines_after_name == 0); + TEST_CASE("simple") { + std::string content = " Foobar "; + REQUIRE(LexWordAroundPos(CharPos(content, 'F'), content) == "Foobar"); + REQUIRE(LexWordAroundPos(CharPos(content, 'o'), content) == "Foobar"); + REQUIRE(LexWordAroundPos(CharPos(content, 'b'), content) == "Foobar"); + REQUIRE(LexWordAroundPos(CharPos(content, 'a'), content) == "Foobar"); + REQUIRE(LexWordAroundPos(CharPos(content, 'r'), content) == "Foobar"); + } + + TEST_CASE("underscores and numbers") { + std::string content = " _my_t5ype7 "; + REQUIRE(LexWordAroundPos(CharPos(content, '_'), content) == "_my_t5ype7"); + REQUIRE(LexWordAroundPos(CharPos(content, '5'), content) == "_my_t5ype7"); + REQUIRE(LexWordAroundPos(CharPos(content, 'e'), content) == "_my_t5ype7"); + REQUIRE(LexWordAroundPos(CharPos(content, '7'), content) == "_my_t5ype7"); + } + + TEST_CASE("dot, dash, colon are skipped") { + std::string content = "1. 2- 3:"; + REQUIRE(LexWordAroundPos(CharPos(content, '1'), content) == "1"); + REQUIRE(LexWordAroundPos(CharPos(content, '2'), content) == "2"); + REQUIRE(LexWordAroundPos(CharPos(content, '3'), content) == "3"); + } } -TEST_CASE("dtor") { - std::string buffer_content = " ~Foo(); "; - lsPosition declaration = CharPos(buffer_content, '~'); - std::string insert_text; - int newlines_after_name = 0; +TEST_SUITE("FindIncludeLine") { + TEST_CASE("in document") { + std::vector lines = { + "#include ", // 0 + "#include " // 1 + }; - LexFunctionDeclaration(buffer_content, declaration, std::string("Foo"), - &insert_text, &newlines_after_name); - REQUIRE(insert_text == "Foo::~Foo() {\n}"); - REQUIRE(newlines_after_name == 0); + REQUIRE(FindIncludeLine(lines, "#include ") == nullopt); + } + + TEST_CASE("insert before") { + std::vector lines = { + "#include ", // 0 + "#include " // 1 + }; + + REQUIRE(FindIncludeLine(lines, "#include ") == 0); + } + + TEST_CASE("insert middle") { + std::vector lines = { + "#include ", // 0 + "#include " // 1 + }; + + REQUIRE(FindIncludeLine(lines, "#include ") == 1); + } + + TEST_CASE("insert after") { + std::vector lines = { + "#include ", // 0 + "#include ", // 1 + "", // 2 + }; + + REQUIRE(FindIncludeLine(lines, "#include ") == 2); + } + + TEST_CASE("ignore header") { + std::vector lines = { + "// FOOBAR", // 0 + "// FOOBAR", // 1 + "// FOOBAR", // 2 + "// FOOBAR", // 3 + "", // 4 + "#include ", // 5 + "#include ", // 6 + "", // 7 + }; + + REQUIRE(FindIncludeLine(lines, "#include ") == 5); + REQUIRE(FindIncludeLine(lines, "#include ") == 6); + REQUIRE(FindIncludeLine(lines, "#include ") == 7); + } } - -TEST_CASE("complex return type") { - std::string buffer_content = " std::vector Foo(); "; - lsPosition declaration = CharPos(buffer_content, 'F'); - std::string insert_text; - int newlines_after_name = 0; - - LexFunctionDeclaration(buffer_content, declaration, nullopt, &insert_text, - &newlines_after_name); - REQUIRE(insert_text == "std::vector Foo() {\n}"); - REQUIRE(newlines_after_name == 0); - - LexFunctionDeclaration(buffer_content, declaration, std::string("Type"), - &insert_text, &newlines_after_name); - REQUIRE(insert_text == "std::vector Type::Foo() {\n}"); - REQUIRE(newlines_after_name == 0); -} - -TEST_CASE("extra complex return type") { - std::string buffer_content = " std::function < int() > \n Foo(); "; - lsPosition declaration = CharPos(buffer_content, 'F'); - std::string insert_text; - int newlines_after_name = 0; - - LexFunctionDeclaration(buffer_content, declaration, nullopt, &insert_text, - &newlines_after_name); - REQUIRE(insert_text == "std::function < int() > \n Foo() {\n}"); - REQUIRE(newlines_after_name == 0); - - LexFunctionDeclaration(buffer_content, declaration, std::string("Type"), - &insert_text, &newlines_after_name); - REQUIRE(insert_text == "std::function < int() > \n Type::Foo() {\n}"); - REQUIRE(newlines_after_name == 0); -} - -TEST_CASE("parameters") { - std::string buffer_content = "void Foo(int a,\n\n int b); "; - lsPosition declaration = CharPos(buffer_content, 'F'); - std::string insert_text; - int newlines_after_name = 0; - - LexFunctionDeclaration(buffer_content, declaration, nullopt, &insert_text, - &newlines_after_name); - REQUIRE(insert_text == "void Foo(int a,\n\n int b) {\n}"); - REQUIRE(newlines_after_name == 2); - - LexFunctionDeclaration(buffer_content, declaration, std::string("Type"), - &insert_text, &newlines_after_name); - REQUIRE(insert_text == "void Type::Foo(int a,\n\n int b) {\n}"); - REQUIRE(newlines_after_name == 2); -} - -TEST_SUITE_END(); - -TEST_SUITE("LexWordAroundPos"); - -TEST_CASE("edges") { - std::string content = "Foobar"; - REQUIRE(LexWordAroundPos(CharPos(content, 'F'), content) == "Foobar"); - REQUIRE(LexWordAroundPos(CharPos(content, 'o'), content) == "Foobar"); - REQUIRE(LexWordAroundPos(CharPos(content, 'b'), content) == "Foobar"); - REQUIRE(LexWordAroundPos(CharPos(content, 'a'), content) == "Foobar"); - REQUIRE(LexWordAroundPos(CharPos(content, 'r'), content) == "Foobar"); -} - -TEST_CASE("simple") { - std::string content = " Foobar "; - REQUIRE(LexWordAroundPos(CharPos(content, 'F'), content) == "Foobar"); - REQUIRE(LexWordAroundPos(CharPos(content, 'o'), content) == "Foobar"); - REQUIRE(LexWordAroundPos(CharPos(content, 'b'), content) == "Foobar"); - REQUIRE(LexWordAroundPos(CharPos(content, 'a'), content) == "Foobar"); - REQUIRE(LexWordAroundPos(CharPos(content, 'r'), content) == "Foobar"); -} - -TEST_CASE("underscores and numbers") { - std::string content = " _my_t5ype7 "; - REQUIRE(LexWordAroundPos(CharPos(content, '_'), content) == "_my_t5ype7"); - REQUIRE(LexWordAroundPos(CharPos(content, '5'), content) == "_my_t5ype7"); - REQUIRE(LexWordAroundPos(CharPos(content, 'e'), content) == "_my_t5ype7"); - REQUIRE(LexWordAroundPos(CharPos(content, '7'), content) == "_my_t5ype7"); -} - -TEST_CASE("dot, dash, colon are skipped") { - std::string content = "1. 2- 3:"; - REQUIRE(LexWordAroundPos(CharPos(content, '1'), content) == "1"); - REQUIRE(LexWordAroundPos(CharPos(content, '2'), content) == "2"); - REQUIRE(LexWordAroundPos(CharPos(content, '3'), content) == "3"); -} - -TEST_SUITE_END(); - -TEST_SUITE("FindIncludeLine"); - -TEST_CASE("in document") { - std::vector lines = { - "#include ", // 0 - "#include " // 1 - }; - - REQUIRE(FindIncludeLine(lines, "#include ") == nullopt); -} - -TEST_CASE("insert before") { - std::vector lines = { - "#include ", // 0 - "#include " // 1 - }; - - REQUIRE(FindIncludeLine(lines, "#include ") == 0); -} - -TEST_CASE("insert middle") { - std::vector lines = { - "#include ", // 0 - "#include " // 1 - }; - - REQUIRE(FindIncludeLine(lines, "#include ") == 1); -} - -TEST_CASE("insert after") { - std::vector lines = { - "#include ", // 0 - "#include ", // 1 - "", // 2 - }; - - REQUIRE(FindIncludeLine(lines, "#include ") == 2); -} - -TEST_CASE("ignore header") { - std::vector lines = { - "// FOOBAR", // 0 - "// FOOBAR", // 1 - "// FOOBAR", // 2 - "// FOOBAR", // 3 - "", // 4 - "#include ", // 5 - "#include ", // 6 - "", // 7 - }; - - REQUIRE(FindIncludeLine(lines, "#include ") == 5); - REQUIRE(FindIncludeLine(lines, "#include ") == 6); - REQUIRE(FindIncludeLine(lines, "#include ") == 7); -} - -TEST_SUITE_END(); diff --git a/src/language_server_api.cc b/src/language_server_api.cc index abdf4d6d..85fb8ef4 100644 --- a/src/language_server_api.cc +++ b/src/language_server_api.cc @@ -82,8 +82,6 @@ optional ReadJsonRpcContentFrom( return content; } -TEST_SUITE("FindIncludeLine"); - std::function()> MakeContentReader(std::string* content, bool can_be_empty) { return [content, can_be_empty]() -> optional { @@ -97,31 +95,31 @@ std::function()> MakeContentReader(std::string* content, }; } -TEST_CASE("ReadContentFromSource") { - auto parse_correct = [](std::string content) -> std::string { - auto reader = MakeContentReader(&content, false /*can_be_empty*/); - auto got = ReadJsonRpcContentFrom(reader); - REQUIRE(got); - return got.value(); - }; +TEST_SUITE("FindIncludeLine") { + TEST_CASE("ReadContentFromSource") { + auto parse_correct = [](std::string content) -> std::string { + auto reader = MakeContentReader(&content, false /*can_be_empty*/); + auto got = ReadJsonRpcContentFrom(reader); + REQUIRE(got); + return got.value(); + }; - auto parse_incorrect = [](std::string content) -> optional { - auto reader = MakeContentReader(&content, true /*can_be_empty*/); - return ReadJsonRpcContentFrom(reader); - }; + auto parse_incorrect = [](std::string content) -> optional { + auto reader = MakeContentReader(&content, true /*can_be_empty*/); + return ReadJsonRpcContentFrom(reader); + }; - REQUIRE(parse_correct("Content-Length: 0\r\n\r\n") == ""); - REQUIRE(parse_correct("Content-Length: 1\r\n\r\na") == "a"); - REQUIRE(parse_correct("Content-Length: 4\r\n\r\nabcd") == "abcd"); + REQUIRE(parse_correct("Content-Length: 0\r\n\r\n") == ""); + REQUIRE(parse_correct("Content-Length: 1\r\n\r\na") == "a"); + REQUIRE(parse_correct("Content-Length: 4\r\n\r\nabcd") == "abcd"); - REQUIRE(parse_incorrect("ggg") == optional()); - REQUIRE(parse_incorrect("Content-Length: 0\r\n") == optional()); - REQUIRE(parse_incorrect("Content-Length: 5\r\n\r\nab") == - optional()); + REQUIRE(parse_incorrect("ggg") == optional()); + REQUIRE(parse_incorrect("Content-Length: 0\r\n") == optional()); + REQUIRE(parse_incorrect("Content-Length: 5\r\n\r\nab") == + optional()); + } } -TEST_SUITE_END(); - optional ReadCharFromStdinBlocking() { // Bad stdin means parent process has probably exited. Either way, cquery // can no longer be communicated with so just exit. diff --git a/src/lex_utils.cc b/src/lex_utils.cc index 8fcac602..29efbfae 100644 --- a/src/lex_utils.cc +++ b/src/lex_utils.cc @@ -213,59 +213,55 @@ bool SubstringMatch(const std::string& search, const std::string& content) { return false; } -TEST_SUITE("Offset"); +TEST_SUITE("Offset") { + TEST_CASE("past end") { + std::string content = "foo"; + int offset = GetOffsetForPosition(lsPosition(10, 10), content); + REQUIRE(offset <= content.size()); + } -TEST_CASE("past end") { - std::string content = "foo"; - int offset = GetOffsetForPosition(lsPosition(10, 10), content); - REQUIRE(offset <= content.size()); -} + TEST_CASE("in middle of content") { + std::string content = "abcdefghijk"; + for (int i = 0; i < content.size(); ++i) { + int offset = GetOffsetForPosition(lsPosition(0, i), content); + REQUIRE(i == offset); + } + } -TEST_CASE("in middle of content") { - std::string content = "abcdefghijk"; - for (int i = 0; i < content.size(); ++i) { - int offset = GetOffsetForPosition(lsPosition(0, i), content); - REQUIRE(i == offset); + TEST_CASE("at end of content") { + REQUIRE(GetOffsetForPosition(lsPosition(0, 0), "") == 0); + REQUIRE(GetOffsetForPosition(lsPosition(0, 1), "a") == 1); } } -TEST_CASE("at end of content") { - REQUIRE(GetOffsetForPosition(lsPosition(0, 0), "") == 0); - REQUIRE(GetOffsetForPosition(lsPosition(0, 1), "a") == 1); +TEST_SUITE("Substring") { + TEST_CASE("match") { + // Sanity. + REQUIRE(SubstringMatch("a", "aa")); + REQUIRE(SubstringMatch("aa", "aa")); + + // Empty string matches anything. + REQUIRE(SubstringMatch("", "")); + REQUIRE(SubstringMatch("", "aa")); + + // Match in start/middle/end. + REQUIRE(SubstringMatch("a", "abbbb")); + REQUIRE(SubstringMatch("a", "bbabb")); + REQUIRE(SubstringMatch("a", "bbbba")); + REQUIRE(SubstringMatch("aa", "aabbb")); + REQUIRE(SubstringMatch("aa", "bbaab")); + REQUIRE(SubstringMatch("aa", "bbbaa")); + + // Capitalization. + REQUIRE(SubstringMatch("aa", "aA")); + REQUIRE(SubstringMatch("aa", "Aa")); + REQUIRE(SubstringMatch("aa", "AA")); + + // Token skipping. + REQUIRE(SubstringMatch("ad", "abcd")); + REQUIRE(SubstringMatch("ad", "ABCD")); + + // Ordering. + REQUIRE(!SubstringMatch("ad", "dcba")); + } } - -TEST_SUITE_END(); - -TEST_SUITE("Substring"); - -TEST_CASE("match") { - // Sanity. - REQUIRE(SubstringMatch("a", "aa")); - REQUIRE(SubstringMatch("aa", "aa")); - - // Empty string matches anything. - REQUIRE(SubstringMatch("", "")); - REQUIRE(SubstringMatch("", "aa")); - - // Match in start/middle/end. - REQUIRE(SubstringMatch("a", "abbbb")); - REQUIRE(SubstringMatch("a", "bbabb")); - REQUIRE(SubstringMatch("a", "bbbba")); - REQUIRE(SubstringMatch("aa", "aabbb")); - REQUIRE(SubstringMatch("aa", "bbaab")); - REQUIRE(SubstringMatch("aa", "bbbaa")); - - // Capitalization. - REQUIRE(SubstringMatch("aa", "aA")); - REQUIRE(SubstringMatch("aa", "Aa")); - REQUIRE(SubstringMatch("aa", "AA")); - - // Token skipping. - REQUIRE(SubstringMatch("ad", "abcd")); - REQUIRE(SubstringMatch("ad", "ABCD")); - - // Ordering. - REQUIRE(!SubstringMatch("ad", "dcba")); -} - -TEST_SUITE_END(); \ No newline at end of file diff --git a/src/match.cc b/src/match.cc index 9c830b75..74593ab5 100644 --- a/src/match.cc +++ b/src/match.cc @@ -76,16 +76,14 @@ bool GroupMatch::IsMatch(const std::string& value, return true; } -TEST_SUITE("Matcher"); - -TEST_CASE("sanity") { - // Matcher m("abc"); - // TODO: check case - // CHECK(m.IsMatch("abc")); - // CHECK(m.IsMatch("fooabc")); - // CHECK(m.IsMatch("abc")); - // CHECK(m.IsMatch("abcfoo")); - // CHECK(m.IsMatch("11a11b11c11")); +TEST_SUITE("Matcher") { + TEST_CASE("sanity") { + // Matcher m("abc"); + // TODO: check case + // CHECK(m.IsMatch("abc")); + // CHECK(m.IsMatch("fooabc")); + // CHECK(m.IsMatch("abc")); + // CHECK(m.IsMatch("abcfoo")); + // CHECK(m.IsMatch("11a11b11c11")); + } } - -TEST_SUITE_END(); diff --git a/src/platform.cc b/src/platform.cc index b534f29b..c8b56015 100644 --- a/src/platform.cc +++ b/src/platform.cc @@ -87,40 +87,38 @@ void MakeDirectoryRecursive(std::string path) { } } -TEST_SUITE("Platform"); +TEST_SUITE("Platform") { + TEST_CASE("Split strings") { + std::vector actual = Split("/a/b/c/", '/'); + std::vector expected{"a", "b", "c"}; + REQUIRE(actual == expected); + } -TEST_CASE("Split strings") { - std::vector actual = Split("/a/b/c/", '/'); - std::vector expected{"a", "b", "c"}; - REQUIRE(actual == expected); -} + TEST_CASE("Mutex lock/unlock (single process)") { + auto m1 = CreatePlatformMutex("indexer-platformmutexttest"); + auto l1 = CreatePlatformScopedMutexLock(m1.get()); + auto m2 = CreatePlatformMutex("indexer-platformmutexttest"); -TEST_CASE("Mutex lock/unlock (single process)") { - auto m1 = CreatePlatformMutex("indexer-platformmutexttest"); - auto l1 = CreatePlatformScopedMutexLock(m1.get()); - auto m2 = CreatePlatformMutex("indexer-platformmutexttest"); + int value = 0; - int value = 0; - - volatile bool did_run = false; - std::thread t([&]() { - did_run = true; - auto l2 = CreatePlatformScopedMutexLock(m2.get()); - value = 1; - }); - while (!did_run) + volatile bool did_run = false; + std::thread t([&]() { + did_run = true; + auto l2 = CreatePlatformScopedMutexLock(m2.get()); + value = 1; + }); + while (!did_run) + std::this_thread::sleep_for(std::chrono::milliseconds(1)); std::this_thread::sleep_for(std::chrono::milliseconds(1)); - std::this_thread::sleep_for(std::chrono::milliseconds(1)); - // Other thread has had a chance to run, but it should not have - // written to value yet (ie, it should be waiting). - REQUIRE(value == 0); + // Other thread has had a chance to run, but it should not have + // written to value yet (ie, it should be waiting). + REQUIRE(value == 0); - // Release the lock, wait for other thread to finish. Verify it - // wrote the expected value. - l1.reset(); - t.join(); - REQUIRE(value == 1); + // Release the lock, wait for other thread to finish. Verify it + // wrote the expected value. + l1.reset(); + t.join(); + REQUIRE(value == 1); + } } - -TEST_SUITE_END(); diff --git a/src/project.cc b/src/project.cc index ef4e0c49..d7749acc 100644 --- a/src/project.cc +++ b/src/project.cc @@ -411,858 +411,856 @@ void Project::ForAllFilteredFiles( } } -TEST_SUITE("Project"); +TEST_SUITE("Project") { + void CheckFlags(const std::string& directory, + const std::string& file, + std::vector raw, + std::vector expected) { + g_disable_normalize_path_for_test = true; -void CheckFlags(const std::string& directory, - const std::string& file, - std::vector raw, - std::vector expected) { - g_disable_normalize_path_for_test = true; + ProjectConfig config; + config.project_dir = "/w/c/s/"; + config.resource_dir = "/w/resource_dir/"; - ProjectConfig config; - config.project_dir = "/w/c/s/"; - config.resource_dir = "/w/resource_dir/"; + CompileCommandsEntry entry; + entry.directory = directory; + entry.args = raw; + entry.file = file; + Project::Entry result = + GetCompilationEntryFromCompileCommandEntry(&config, entry); - CompileCommandsEntry entry; - entry.directory = directory; - entry.args = raw; - entry.file = file; - Project::Entry result = - GetCompilationEntryFromCompileCommandEntry(&config, entry); + bool printed_header = false; + for (int i = 0; i < std::min(result.args.size(), expected.size()); ++i) { + if (result.args[i] != expected[i]) { + if (!printed_header) { + printed_header = true; + std::cout << "Expected - Actual\n\n"; + } - bool printed_header = false; - for (int i = 0; i < std::min(result.args.size(), expected.size()); ++i) { - if (result.args[i] != expected[i]) { - if (!printed_header) { - printed_header = true; - std::cout << "Expected - Actual\n\n"; + std::cout << "mismatch at " << i << "; expected " << expected[i] + << " but got " << result.args[i] << std::endl; } + } + REQUIRE(result.args == expected); + } - std::cout << "mismatch at " << i << "; expected " << expected[i] - << " but got " << result.args[i] << std::endl; + void CheckFlags(std::vector raw, + std::vector expected) { + CheckFlags("/dir/", "file.cc", raw, expected); + } + + TEST_CASE("strip meta-compiler invocations") { + CheckFlags( + /* raw */ {"clang", "-lstdc++", "myfile.cc"}, + /* expected */ {"clang", "-lstdc++", "myfile.cc", "-xc++", "-std=c++11", + "-resource-dir=/w/resource_dir/"}); + + CheckFlags(/* raw */ {"goma", "clang"}, + /* expected */ {"clang", "-xc++", "-std=c++11", + "-resource-dir=/w/resource_dir/"}); + + CheckFlags(/* raw */ {"goma", "clang", "--foo"}, + /* expected */ {"clang", "--foo", "-xc++", "-std=c++11", + "-resource-dir=/w/resource_dir/"}); + } + + // Checks flag parsing for a random chromium file in comparison to what + // YouCompleteMe fetches. + TEST_CASE("ycm") { + CheckFlags( + "/w/c/s/out/Release", "../../ash/login/lock_screen_sanity_unittest.cc", + + /* raw */ + { + "/work/goma/gomacc", + "../../third_party/llvm-build/Release+Asserts/bin/clang++", + "-MMD", + "-MF", + "obj/ash/ash_unittests/lock_screen_sanity_unittest.o.d", + "-DV8_DEPRECATION_WARNINGS", + "-DDCHECK_ALWAYS_ON=1", + "-DUSE_UDEV", + "-DUSE_AURA=1", + "-DUSE_NSS_CERTS=1", + "-DUSE_OZONE=1", + "-DFULL_SAFE_BROWSING", + "-DSAFE_BROWSING_CSD", + "-DSAFE_BROWSING_DB_LOCAL", + "-DCHROMIUM_BUILD", + "-DFIELDTRIAL_TESTING_ENABLED", + "-D_FILE_OFFSET_BITS=64", + "-D_LARGEFILE_SOURCE", + "-D_LARGEFILE64_SOURCE", + "-DCR_CLANG_REVISION=\"313786-1\"", + "-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", + "-DGTEST_API_=", + "-DGTEST_HAS_POSIX_RE=0", + "-DGTEST_LANG_CXX11=1", + "-DUNIT_TEST", + "-DUSING_V8_SHARED", + "-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", + "-DBORINGSSL_SHARED_LIBRARY", + "-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", + "-I../..", + "-Igen", + "-I../../third_party/libwebp/src", + "-I../../third_party/khronos", + "-I../../gpu", + "-I../../third_party/googletest/src/googletest/include", + "-I../../third_party/WebKit", + "-Igen/third_party/WebKit", + "-I../../v8/include", + "-Igen/v8/include", + "-I../../third_party/icu/source/common", + "-I../../third_party/icu/source/i18n", + "-I../../third_party/protobuf/src", + "-Igen/protoc_out", + "-I../../third_party/protobuf/src", + "-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", + "-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/include/codec", + "-I../../third_party/skia/src/gpu", + "-I../../third_party/skia/src/sksl", + "-I../../third_party/ced/src", + "-I../../third_party/mesa/src/include", + "-I../../third_party/libwebm/source", + "-Igen", + "-I../../build/linux/debian_jessie_amd64-sysroot/usr/include/" + "dbus-1.0", + "-I../../build/linux/debian_jessie_amd64-sysroot/usr/lib/" + "x86_64-linux-gnu/dbus-1.0/include", + "-I../../third_party/googletest/custom", + "-I../../third_party/googletest/src/googlemock/include", + "-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", + "-no-canonical-prefixes", + "-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", + "-Wno-tautological-unsigned-zero-compare", + "-Wno-null-pointer-arithmetic", + "-Wno-tautological-unsigned-enum-zero-compare", + "-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", + "-Wno-header-guard", + "-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", + "../../ash/login/ui/lock_screen_sanity_unittest.cc", + "-o", + "obj/ash/ash_unittests/lock_screen_sanity_unittest.o", + }, + + /* expected */ + {"../../third_party/llvm-build/Release+Asserts/bin/clang++", + "-DV8_DEPRECATION_WARNINGS", + "-DDCHECK_ALWAYS_ON=1", + "-DUSE_UDEV", + "-DUSE_AURA=1", + "-DUSE_NSS_CERTS=1", + "-DUSE_OZONE=1", + "-DFULL_SAFE_BROWSING", + "-DSAFE_BROWSING_CSD", + "-DSAFE_BROWSING_DB_LOCAL", + "-DCHROMIUM_BUILD", + "-DFIELDTRIAL_TESTING_ENABLED", + "-D_FILE_OFFSET_BITS=64", + "-D_LARGEFILE_SOURCE", + "-D_LARGEFILE64_SOURCE", + "-DCR_CLANG_REVISION=\"313786-1\"", + "-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", + "-DGTEST_API_=", + "-DGTEST_HAS_POSIX_RE=0", + "-DGTEST_LANG_CXX11=1", + "-DUNIT_TEST", + "-DUSING_V8_SHARED", + "-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", + "-DBORINGSSL_SHARED_LIBRARY", + "-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", + "-I&/w/c/s/out/Release/../..", + "-I&/w/c/s/out/Release/gen", + "-I&/w/c/s/out/Release/../../third_party/libwebp/src", + "-I&/w/c/s/out/Release/../../third_party/khronos", + "-I&/w/c/s/out/Release/../../gpu", + "-I&/w/c/s/out/Release/../../third_party/googletest/src/googletest/" + "include", + "-I&/w/c/s/out/Release/../../third_party/WebKit", + "-I&/w/c/s/out/Release/gen/third_party/WebKit", + "-I&/w/c/s/out/Release/../../v8/include", + "-I&/w/c/s/out/Release/gen/v8/include", + "-I&/w/c/s/out/Release/../../third_party/icu/source/common", + "-I&/w/c/s/out/Release/../../third_party/icu/source/i18n", + "-I&/w/c/s/out/Release/../../third_party/protobuf/src", + "-I&/w/c/s/out/Release/gen/protoc_out", + "-I&/w/c/s/out/Release/../../third_party/protobuf/src", + "-I&/w/c/s/out/Release/../../third_party/boringssl/src/include", + "-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" + "usr/include/nss", + "-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" + "usr/include/nspr", + "-I&/w/c/s/out/Release/../../skia/config", + "-I&/w/c/s/out/Release/../../skia/ext", + "-I&/w/c/s/out/Release/../../third_party/skia/include/c", + "-I&/w/c/s/out/Release/../../third_party/skia/include/config", + "-I&/w/c/s/out/Release/../../third_party/skia/include/core", + "-I&/w/c/s/out/Release/../../third_party/skia/include/effects", + "-I&/w/c/s/out/Release/../../third_party/skia/include/encode", + "-I&/w/c/s/out/Release/../../third_party/skia/include/gpu", + "-I&/w/c/s/out/Release/../../third_party/skia/include/images", + "-I&/w/c/s/out/Release/../../third_party/skia/include/lazy", + "-I&/w/c/s/out/Release/../../third_party/skia/include/pathops", + "-I&/w/c/s/out/Release/../../third_party/skia/include/pdf", + "-I&/w/c/s/out/Release/../../third_party/skia/include/pipe", + "-I&/w/c/s/out/Release/../../third_party/skia/include/ports", + "-I&/w/c/s/out/Release/../../third_party/skia/include/utils", + "-I&/w/c/s/out/Release/../../third_party/skia/third_party/vulkan", + "-I&/w/c/s/out/Release/../../third_party/skia/include/codec", + "-I&/w/c/s/out/Release/../../third_party/skia/src/gpu", + "-I&/w/c/s/out/Release/../../third_party/skia/src/sksl", + "-I&/w/c/s/out/Release/../../third_party/ced/src", + "-I&/w/c/s/out/Release/../../third_party/mesa/src/include", + "-I&/w/c/s/out/Release/../../third_party/libwebm/source", + "-I&/w/c/s/out/Release/gen", + "-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" + "usr/include/dbus-1.0", + "-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" + "usr/lib/x86_64-linux-gnu/dbus-1.0/include", + "-I&/w/c/s/out/Release/../../third_party/googletest/custom", + "-I&/w/c/s/out/Release/../../third_party/googletest/src/googlemock/" + "include", + "-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", + "-no-canonical-prefixes", + "-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", + "-Wno-tautological-unsigned-zero-compare", + "-Wno-null-pointer-arithmetic", + "-Wno-tautological-unsigned-enum-zero-compare", + "-O2", + "-fno-ident", + "-fdata-sections", + "-ffunction-sections", + "-fno-omit-frame-pointer", + "-g0", + "-fvisibility=hidden", + "-Wheader-hygiene", + "-Wstring-conversion", + "-Wtautological-overlap-compare", + "-Wno-header-guard", + "-std=gnu++14", + "-fno-rtti", + "-nostdinc++", + "-isystem&/w/c/s/out/Release/../../buildtools/third_party/libc++/trunk/" + "include", + "-isystem&/w/c/s/out/Release/../../buildtools/third_party/libc++abi/" + "trunk/" + "include", + "--sysroot=&/w/c/s/out/Release/../../build/linux/" + "debian_jessie_amd64-sysroot", + "-fno-exceptions", + "-fvisibility-inlines-hidden", + "-xc++", + "-resource-dir=/w/resource_dir/"}); + } + + // Checks flag parsing for an example chromium file. + TEST_CASE("chromium") { + CheckFlags( + "/w/c/s/out/Release", "../../apps/app_lifetime_monitor.cc", + /* raw */ + {"/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"}, + + /* expected */ + {"../../third_party/llvm-build/Release+Asserts/bin/clang++", + "-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&/w/c/s/out/Release/../..", + "-I&/w/c/s/out/Release/gen", + "-I&/w/c/s/out/Release/../../third_party/libwebp/src", + "-I&/w/c/s/out/Release/../../third_party/khronos", + "-I&/w/c/s/out/Release/../../gpu", + "-I&/w/c/s/out/Release/../../third_party/ced/src", + "-I&/w/c/s/out/Release/../../third_party/icu/source/common", + "-I&/w/c/s/out/Release/../../third_party/icu/source/i18n", + "-I&/w/c/s/out/Release/../../third_party/protobuf/src", + "-I&/w/c/s/out/Release/../../skia/config", + "-I&/w/c/s/out/Release/../../skia/ext", + "-I&/w/c/s/out/Release/../../third_party/skia/include/c", + "-I&/w/c/s/out/Release/../../third_party/skia/include/config", + "-I&/w/c/s/out/Release/../../third_party/skia/include/core", + "-I&/w/c/s/out/Release/../../third_party/skia/include/effects", + "-I&/w/c/s/out/Release/../../third_party/skia/include/encode", + "-I&/w/c/s/out/Release/../../third_party/skia/include/gpu", + "-I&/w/c/s/out/Release/../../third_party/skia/include/images", + "-I&/w/c/s/out/Release/../../third_party/skia/include/lazy", + "-I&/w/c/s/out/Release/../../third_party/skia/include/pathops", + "-I&/w/c/s/out/Release/../../third_party/skia/include/pdf", + "-I&/w/c/s/out/Release/../../third_party/skia/include/pipe", + "-I&/w/c/s/out/Release/../../third_party/skia/include/ports", + "-I&/w/c/s/out/Release/../../third_party/skia/include/utils", + "-I&/w/c/s/out/Release/../../third_party/skia/third_party/vulkan", + "-I&/w/c/s/out/Release/../../third_party/skia/src/gpu", + "-I&/w/c/s/out/Release/../../third_party/skia/src/sksl", + "-I&/w/c/s/out/Release/../../third_party/mesa/src/include", + "-I&/w/c/s/out/Release/../../third_party/libwebm/source", + "-I&/w/c/s/out/Release/../../third_party/protobuf/src", + "-I&/w/c/s/out/Release/gen/protoc_out", + "-I&/w/c/s/out/Release/../../third_party/boringssl/src/include", + "-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" + "usr/include/nss", + "-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" + "usr/include/nspr", + "-I&/w/c/s/out/Release/gen", + "-I&/w/c/s/out/Release/../../third_party/WebKit", + "-I&/w/c/s/out/Release/gen/third_party/WebKit", + "-I&/w/c/s/out/Release/../../v8/include", + "-I&/w/c/s/out/Release/gen/v8/include", + "-I&/w/c/s/out/Release/gen", + "-I&/w/c/s/out/Release/../../third_party/flatbuffers/src/include", + "-I&/w/c/s/out/Release/gen", + "-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", + "-Wheader-hygiene", + "-Wstring-conversion", + "-Wtautological-overlap-compare", + "-Wexit-time-destructors", + "-Wno-header-guard", + "-Wno-exit-time-destructors", + "-std=gnu++14", + "-fno-rtti", + "-nostdinc++", + "-isystem&/w/c/s/out/Release/../../buildtools/third_party/libc++/trunk/" + "include", + "-isystem&/w/c/s/out/Release/../../buildtools/third_party/libc++abi/" + "trunk/" + "include", + "--sysroot=&/w/c/s/out/Release/../../build/linux/" + "debian_jessie_amd64-sysroot", + "-fno-exceptions", + "-fvisibility-inlines-hidden", + "-xc++", + "-resource-dir=/w/resource_dir/"}); + } + + TEST_CASE("Directory extraction") { + ProjectConfig config; + config.project_dir = "/w/c/s/"; + + CompileCommandsEntry entry; + entry.directory = "/base"; + entry.args = {"clang", + "-I/a_absolute1", + "--foobar", + "-I", + "/a_absolute2", + "--foobar", + "-Ia_relative1", + "--foobar", + "-I", + "a_relative2", + "--foobar", + "-iquote/q_absolute1", + "--foobar", + "-iquote", + "/q_absolute2", + "--foobar", + "-iquoteq_relative1", + "--foobar", + "-iquote", + "q_relative2", + "--foobar", + "foo.cc"}; + entry.file = "foo.cc"; + Project::Entry result = + GetCompilationEntryFromCompileCommandEntry(&config, entry); + + std::unordered_set angle_expected{ + "&/a_absolute1", "&/a_absolute2", "&/base/a_relative1", + "&/base/a_relative2"}; + std::unordered_set quote_expected{ + "&/q_absolute1", "&/q_absolute2", "&/base/q_relative1", + "&/base/q_relative2"}; + REQUIRE(config.angle_dirs == angle_expected); + REQUIRE(config.quote_dirs == quote_expected); + } + + TEST_CASE("Entry inference") { + Project p; + { + Project::Entry e; + e.args = {"arg1"}; + e.filename = "/a/b/c/d/bar.cc"; + p.entries.push_back(e); + } + { + Project::Entry e; + e.args = {"arg2"}; + e.filename = "/a/b/c/baz.cc"; + p.entries.push_back(e); + } + + // Guess at same directory level, when there are parent directories. + { + optional entry = + p.FindCompilationEntryForFile("/a/b/c/d/new.cc"); + REQUIRE(entry.has_value()); + REQUIRE(entry->args == std::vector{"arg1"}); + } + + // Guess at same directory level, when there are child directories. + { + optional entry = + p.FindCompilationEntryForFile("/a/b/c/new.cc"); + REQUIRE(entry.has_value()); + REQUIRE(entry->args == std::vector{"arg2"}); + } + + // Guess at new directory (use the closest parent directory). + { + optional entry = + p.FindCompilationEntryForFile("/a/b/c/new/new.cc"); + REQUIRE(entry.has_value()); + REQUIRE(entry->args == std::vector{"arg2"}); } } - REQUIRE(result.args == expected); -} -void CheckFlags(std::vector raw, - std::vector expected) { - CheckFlags("/dir/", "file.cc", raw, expected); -} + TEST_CASE("Entry inference prefers same file endings") { + Project p; + { + Project::Entry e; + e.args = {"arg1"}; + e.filename = "common/simple_browsertest.cc"; + p.entries.push_back(e); + } + { + Project::Entry e; + e.args = {"arg2"}; + e.filename = "common/simple_unittest.cc"; + p.entries.push_back(e); + } + { + Project::Entry e; + e.args = {"arg3"}; + e.filename = "common/a/simple_unittest.cc"; + p.entries.push_back(e); + } -TEST_CASE("strip meta-compiler invocations") { - CheckFlags( - /* raw */ {"clang", "-lstdc++", "myfile.cc"}, - /* expected */ {"clang", "-lstdc++", "myfile.cc", "-xc++", "-std=c++11", - "-resource-dir=/w/resource_dir/"}); + // Prefer files with the same ending. + { + optional entry = + p.FindCompilationEntryForFile("my_browsertest.cc"); + REQUIRE(entry.has_value()); + REQUIRE(entry->args == std::vector{"arg1"}); + } + { + optional entry = + p.FindCompilationEntryForFile("my_unittest.cc"); + REQUIRE(entry.has_value()); + REQUIRE(entry->args == std::vector{"arg2"}); + } + { + optional entry = + p.FindCompilationEntryForFile("common/my_browsertest.cc"); + REQUIRE(entry.has_value()); + REQUIRE(entry->args == std::vector{"arg1"}); + } + { + optional entry = + p.FindCompilationEntryForFile("common/my_unittest.cc"); + REQUIRE(entry.has_value()); + REQUIRE(entry->args == std::vector{"arg2"}); + } - CheckFlags(/* raw */ {"goma", "clang"}, - /* expected */ {"clang", "-xc++", "-std=c++11", - "-resource-dir=/w/resource_dir/"}); - - CheckFlags(/* raw */ {"goma", "clang", "--foo"}, - /* expected */ {"clang", "--foo", "-xc++", "-std=c++11", - "-resource-dir=/w/resource_dir/"}); -} - -// Checks flag parsing for a random chromium file in comparison to what -// YouCompleteMe fetches. -TEST_CASE("ycm") { - CheckFlags( - "/w/c/s/out/Release", "../../ash/login/lock_screen_sanity_unittest.cc", - - /* raw */ - { - "/work/goma/gomacc", - "../../third_party/llvm-build/Release+Asserts/bin/clang++", - "-MMD", - "-MF", - "obj/ash/ash_unittests/lock_screen_sanity_unittest.o.d", - "-DV8_DEPRECATION_WARNINGS", - "-DDCHECK_ALWAYS_ON=1", - "-DUSE_UDEV", - "-DUSE_AURA=1", - "-DUSE_NSS_CERTS=1", - "-DUSE_OZONE=1", - "-DFULL_SAFE_BROWSING", - "-DSAFE_BROWSING_CSD", - "-DSAFE_BROWSING_DB_LOCAL", - "-DCHROMIUM_BUILD", - "-DFIELDTRIAL_TESTING_ENABLED", - "-D_FILE_OFFSET_BITS=64", - "-D_LARGEFILE_SOURCE", - "-D_LARGEFILE64_SOURCE", - "-DCR_CLANG_REVISION=\"313786-1\"", - "-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", - "-DGTEST_API_=", - "-DGTEST_HAS_POSIX_RE=0", - "-DGTEST_LANG_CXX11=1", - "-DUNIT_TEST", - "-DUSING_V8_SHARED", - "-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", - "-DBORINGSSL_SHARED_LIBRARY", - "-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", - "-I../..", - "-Igen", - "-I../../third_party/libwebp/src", - "-I../../third_party/khronos", - "-I../../gpu", - "-I../../third_party/googletest/src/googletest/include", - "-I../../third_party/WebKit", - "-Igen/third_party/WebKit", - "-I../../v8/include", - "-Igen/v8/include", - "-I../../third_party/icu/source/common", - "-I../../third_party/icu/source/i18n", - "-I../../third_party/protobuf/src", - "-Igen/protoc_out", - "-I../../third_party/protobuf/src", - "-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", - "-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/include/codec", - "-I../../third_party/skia/src/gpu", - "-I../../third_party/skia/src/sksl", - "-I../../third_party/ced/src", - "-I../../third_party/mesa/src/include", - "-I../../third_party/libwebm/source", - "-Igen", - "-I../../build/linux/debian_jessie_amd64-sysroot/usr/include/" - "dbus-1.0", - "-I../../build/linux/debian_jessie_amd64-sysroot/usr/lib/" - "x86_64-linux-gnu/dbus-1.0/include", - "-I../../third_party/googletest/custom", - "-I../../third_party/googletest/src/googlemock/include", - "-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", - "-no-canonical-prefixes", - "-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", - "-Wno-tautological-unsigned-zero-compare", - "-Wno-null-pointer-arithmetic", - "-Wno-tautological-unsigned-enum-zero-compare", - "-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", - "-Wno-header-guard", - "-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", - "../../ash/login/ui/lock_screen_sanity_unittest.cc", - "-o", - "obj/ash/ash_unittests/lock_screen_sanity_unittest.o", - }, - - /* expected */ - {"../../third_party/llvm-build/Release+Asserts/bin/clang++", - "-DV8_DEPRECATION_WARNINGS", - "-DDCHECK_ALWAYS_ON=1", - "-DUSE_UDEV", - "-DUSE_AURA=1", - "-DUSE_NSS_CERTS=1", - "-DUSE_OZONE=1", - "-DFULL_SAFE_BROWSING", - "-DSAFE_BROWSING_CSD", - "-DSAFE_BROWSING_DB_LOCAL", - "-DCHROMIUM_BUILD", - "-DFIELDTRIAL_TESTING_ENABLED", - "-D_FILE_OFFSET_BITS=64", - "-D_LARGEFILE_SOURCE", - "-D_LARGEFILE64_SOURCE", - "-DCR_CLANG_REVISION=\"313786-1\"", - "-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", - "-DGTEST_API_=", - "-DGTEST_HAS_POSIX_RE=0", - "-DGTEST_LANG_CXX11=1", - "-DUNIT_TEST", - "-DUSING_V8_SHARED", - "-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", - "-DBORINGSSL_SHARED_LIBRARY", - "-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", - "-I&/w/c/s/out/Release/../..", - "-I&/w/c/s/out/Release/gen", - "-I&/w/c/s/out/Release/../../third_party/libwebp/src", - "-I&/w/c/s/out/Release/../../third_party/khronos", - "-I&/w/c/s/out/Release/../../gpu", - "-I&/w/c/s/out/Release/../../third_party/googletest/src/googletest/" - "include", - "-I&/w/c/s/out/Release/../../third_party/WebKit", - "-I&/w/c/s/out/Release/gen/third_party/WebKit", - "-I&/w/c/s/out/Release/../../v8/include", - "-I&/w/c/s/out/Release/gen/v8/include", - "-I&/w/c/s/out/Release/../../third_party/icu/source/common", - "-I&/w/c/s/out/Release/../../third_party/icu/source/i18n", - "-I&/w/c/s/out/Release/../../third_party/protobuf/src", - "-I&/w/c/s/out/Release/gen/protoc_out", - "-I&/w/c/s/out/Release/../../third_party/protobuf/src", - "-I&/w/c/s/out/Release/../../third_party/boringssl/src/include", - "-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" - "usr/include/nss", - "-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" - "usr/include/nspr", - "-I&/w/c/s/out/Release/../../skia/config", - "-I&/w/c/s/out/Release/../../skia/ext", - "-I&/w/c/s/out/Release/../../third_party/skia/include/c", - "-I&/w/c/s/out/Release/../../third_party/skia/include/config", - "-I&/w/c/s/out/Release/../../third_party/skia/include/core", - "-I&/w/c/s/out/Release/../../third_party/skia/include/effects", - "-I&/w/c/s/out/Release/../../third_party/skia/include/encode", - "-I&/w/c/s/out/Release/../../third_party/skia/include/gpu", - "-I&/w/c/s/out/Release/../../third_party/skia/include/images", - "-I&/w/c/s/out/Release/../../third_party/skia/include/lazy", - "-I&/w/c/s/out/Release/../../third_party/skia/include/pathops", - "-I&/w/c/s/out/Release/../../third_party/skia/include/pdf", - "-I&/w/c/s/out/Release/../../third_party/skia/include/pipe", - "-I&/w/c/s/out/Release/../../third_party/skia/include/ports", - "-I&/w/c/s/out/Release/../../third_party/skia/include/utils", - "-I&/w/c/s/out/Release/../../third_party/skia/third_party/vulkan", - "-I&/w/c/s/out/Release/../../third_party/skia/include/codec", - "-I&/w/c/s/out/Release/../../third_party/skia/src/gpu", - "-I&/w/c/s/out/Release/../../third_party/skia/src/sksl", - "-I&/w/c/s/out/Release/../../third_party/ced/src", - "-I&/w/c/s/out/Release/../../third_party/mesa/src/include", - "-I&/w/c/s/out/Release/../../third_party/libwebm/source", - "-I&/w/c/s/out/Release/gen", - "-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" - "usr/include/dbus-1.0", - "-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" - "usr/lib/x86_64-linux-gnu/dbus-1.0/include", - "-I&/w/c/s/out/Release/../../third_party/googletest/custom", - "-I&/w/c/s/out/Release/../../third_party/googletest/src/googlemock/" - "include", - "-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", - "-no-canonical-prefixes", - "-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", - "-Wno-tautological-unsigned-zero-compare", - "-Wno-null-pointer-arithmetic", - "-Wno-tautological-unsigned-enum-zero-compare", - "-O2", - "-fno-ident", - "-fdata-sections", - "-ffunction-sections", - "-fno-omit-frame-pointer", - "-g0", - "-fvisibility=hidden", - "-Wheader-hygiene", - "-Wstring-conversion", - "-Wtautological-overlap-compare", - "-Wno-header-guard", - "-std=gnu++14", - "-fno-rtti", - "-nostdinc++", - "-isystem&/w/c/s/out/Release/../../buildtools/third_party/libc++/trunk/" - "include", - "-isystem&/w/c/s/out/Release/../../buildtools/third_party/libc++abi/" - "trunk/" - "include", - "--sysroot=&/w/c/s/out/Release/../../build/linux/" - "debian_jessie_amd64-sysroot", - "-fno-exceptions", - "-fvisibility-inlines-hidden", - "-xc++", - "-resource-dir=/w/resource_dir/"}); -} - -// Checks flag parsing for an example chromium file. -TEST_CASE("chromium") { - CheckFlags( - "/w/c/s/out/Release", "../../apps/app_lifetime_monitor.cc", - /* raw */ - {"/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"}, - - /* expected */ - {"../../third_party/llvm-build/Release+Asserts/bin/clang++", - "-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&/w/c/s/out/Release/../..", - "-I&/w/c/s/out/Release/gen", - "-I&/w/c/s/out/Release/../../third_party/libwebp/src", - "-I&/w/c/s/out/Release/../../third_party/khronos", - "-I&/w/c/s/out/Release/../../gpu", - "-I&/w/c/s/out/Release/../../third_party/ced/src", - "-I&/w/c/s/out/Release/../../third_party/icu/source/common", - "-I&/w/c/s/out/Release/../../third_party/icu/source/i18n", - "-I&/w/c/s/out/Release/../../third_party/protobuf/src", - "-I&/w/c/s/out/Release/../../skia/config", - "-I&/w/c/s/out/Release/../../skia/ext", - "-I&/w/c/s/out/Release/../../third_party/skia/include/c", - "-I&/w/c/s/out/Release/../../third_party/skia/include/config", - "-I&/w/c/s/out/Release/../../third_party/skia/include/core", - "-I&/w/c/s/out/Release/../../third_party/skia/include/effects", - "-I&/w/c/s/out/Release/../../third_party/skia/include/encode", - "-I&/w/c/s/out/Release/../../third_party/skia/include/gpu", - "-I&/w/c/s/out/Release/../../third_party/skia/include/images", - "-I&/w/c/s/out/Release/../../third_party/skia/include/lazy", - "-I&/w/c/s/out/Release/../../third_party/skia/include/pathops", - "-I&/w/c/s/out/Release/../../third_party/skia/include/pdf", - "-I&/w/c/s/out/Release/../../third_party/skia/include/pipe", - "-I&/w/c/s/out/Release/../../third_party/skia/include/ports", - "-I&/w/c/s/out/Release/../../third_party/skia/include/utils", - "-I&/w/c/s/out/Release/../../third_party/skia/third_party/vulkan", - "-I&/w/c/s/out/Release/../../third_party/skia/src/gpu", - "-I&/w/c/s/out/Release/../../third_party/skia/src/sksl", - "-I&/w/c/s/out/Release/../../third_party/mesa/src/include", - "-I&/w/c/s/out/Release/../../third_party/libwebm/source", - "-I&/w/c/s/out/Release/../../third_party/protobuf/src", - "-I&/w/c/s/out/Release/gen/protoc_out", - "-I&/w/c/s/out/Release/../../third_party/boringssl/src/include", - "-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" - "usr/include/nss", - "-I&/w/c/s/out/Release/../../build/linux/debian_jessie_amd64-sysroot/" - "usr/include/nspr", - "-I&/w/c/s/out/Release/gen", - "-I&/w/c/s/out/Release/../../third_party/WebKit", - "-I&/w/c/s/out/Release/gen/third_party/WebKit", - "-I&/w/c/s/out/Release/../../v8/include", - "-I&/w/c/s/out/Release/gen/v8/include", - "-I&/w/c/s/out/Release/gen", - "-I&/w/c/s/out/Release/../../third_party/flatbuffers/src/include", - "-I&/w/c/s/out/Release/gen", - "-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", - "-Wheader-hygiene", - "-Wstring-conversion", - "-Wtautological-overlap-compare", - "-Wexit-time-destructors", - "-Wno-header-guard", - "-Wno-exit-time-destructors", - "-std=gnu++14", - "-fno-rtti", - "-nostdinc++", - "-isystem&/w/c/s/out/Release/../../buildtools/third_party/libc++/trunk/" - "include", - "-isystem&/w/c/s/out/Release/../../buildtools/third_party/libc++abi/" - "trunk/" - "include", - "--sysroot=&/w/c/s/out/Release/../../build/linux/" - "debian_jessie_amd64-sysroot", - "-fno-exceptions", - "-fvisibility-inlines-hidden", - "-xc++", - "-resource-dir=/w/resource_dir/"}); -} - -TEST_CASE("Directory extraction") { - ProjectConfig config; - config.project_dir = "/w/c/s/"; - - CompileCommandsEntry entry; - entry.directory = "/base"; - entry.args = {"clang", - "-I/a_absolute1", - "--foobar", - "-I", - "/a_absolute2", - "--foobar", - "-Ia_relative1", - "--foobar", - "-I", - "a_relative2", - "--foobar", - "-iquote/q_absolute1", - "--foobar", - "-iquote", - "/q_absolute2", - "--foobar", - "-iquoteq_relative1", - "--foobar", - "-iquote", - "q_relative2", - "--foobar", - "foo.cc"}; - entry.file = "foo.cc"; - Project::Entry result = - GetCompilationEntryFromCompileCommandEntry(&config, entry); - - std::unordered_set angle_expected{ - "&/a_absolute1", "&/a_absolute2", "&/base/a_relative1", - "&/base/a_relative2"}; - std::unordered_set quote_expected{ - "&/q_absolute1", "&/q_absolute2", "&/base/q_relative1", - "&/base/q_relative2"}; - REQUIRE(config.angle_dirs == angle_expected); - REQUIRE(config.quote_dirs == quote_expected); -} - -TEST_CASE("Entry inference") { - Project p; - { - Project::Entry e; - e.args = {"arg1"}; - e.filename = "/a/b/c/d/bar.cc"; - p.entries.push_back(e); - } - { - Project::Entry e; - e.args = {"arg2"}; - e.filename = "/a/b/c/baz.cc"; - p.entries.push_back(e); - } - - // Guess at same directory level, when there are parent directories. - { - optional entry = - p.FindCompilationEntryForFile("/a/b/c/d/new.cc"); - REQUIRE(entry.has_value()); - REQUIRE(entry->args == std::vector{"arg1"}); - } - - // Guess at same directory level, when there are child directories. - { - optional entry = - p.FindCompilationEntryForFile("/a/b/c/new.cc"); - REQUIRE(entry.has_value()); - REQUIRE(entry->args == std::vector{"arg2"}); - } - - // Guess at new directory (use the closest parent directory). - { - optional entry = - p.FindCompilationEntryForFile("/a/b/c/new/new.cc"); - REQUIRE(entry.has_value()); - REQUIRE(entry->args == std::vector{"arg2"}); + // Prefer the same directory over matching file-ending. + { + optional entry = + p.FindCompilationEntryForFile("common/a/foo.cc"); + REQUIRE(entry.has_value()); + REQUIRE(entry->args == std::vector{"arg3"}); + } } } - -TEST_CASE("Entry inference prefers same file endings") { - Project p; - { - Project::Entry e; - e.args = {"arg1"}; - e.filename = "common/simple_browsertest.cc"; - p.entries.push_back(e); - } - { - Project::Entry e; - e.args = {"arg2"}; - e.filename = "common/simple_unittest.cc"; - p.entries.push_back(e); - } - { - Project::Entry e; - e.args = {"arg3"}; - e.filename = "common/a/simple_unittest.cc"; - p.entries.push_back(e); - } - - // Prefer files with the same ending. - { - optional entry = - p.FindCompilationEntryForFile("my_browsertest.cc"); - REQUIRE(entry.has_value()); - REQUIRE(entry->args == std::vector{"arg1"}); - } - { - optional entry = - p.FindCompilationEntryForFile("my_unittest.cc"); - REQUIRE(entry.has_value()); - REQUIRE(entry->args == std::vector{"arg2"}); - } - { - optional entry = - p.FindCompilationEntryForFile("common/my_browsertest.cc"); - REQUIRE(entry.has_value()); - REQUIRE(entry->args == std::vector{"arg1"}); - } - { - optional entry = - p.FindCompilationEntryForFile("common/my_unittest.cc"); - REQUIRE(entry.has_value()); - REQUIRE(entry->args == std::vector{"arg2"}); - } - - // Prefer the same directory over matching file-ending. - { - optional entry = - p.FindCompilationEntryForFile("common/a/foo.cc"); - REQUIRE(entry.has_value()); - REQUIRE(entry->args == std::vector{"arg3"}); - } -} - -TEST_SUITE_END(); diff --git a/src/query.cc b/src/query.cc index 24ca7c55..432616fd 100644 --- a/src/query.cc +++ b/src/query.cc @@ -823,132 +823,130 @@ void QueryDatabase::UpdateDetailedNames(size_t* qualified_name_index, } } -TEST_SUITE("query"); +TEST_SUITE("query") { + IndexUpdate GetDelta(IndexFile previous, IndexFile current) { + QueryDatabase db; + IdMap previous_map(&db, previous.id_cache); + IdMap current_map(&db, current.id_cache); + return IndexUpdate::CreateDelta(&previous_map, ¤t_map, &previous, + ¤t); + } -IndexUpdate GetDelta(IndexFile previous, IndexFile current) { - QueryDatabase db; - IdMap previous_map(&db, previous.id_cache); - IdMap current_map(&db, current.id_cache); - return IndexUpdate::CreateDelta(&previous_map, ¤t_map, &previous, - ¤t); + TEST_CASE("remove defs") { + IndexFile previous("foo.cc"); + IndexFile current("foo.cc"); + + previous.Resolve(previous.ToTypeId("usr1"))->def.definition_spelling = + Range(Position(1, 0)); + previous.Resolve(previous.ToFuncId("usr2"))->def.definition_spelling = + Range(Position(2, 0)); + previous.Resolve(previous.ToVarId("usr3"))->def.definition_spelling = + Range(Position(3, 0)); + + IndexUpdate update = GetDelta(previous, current); + + REQUIRE(update.types_removed == std::vector{"usr1"}); + REQUIRE(update.funcs_removed == std::vector{"usr2"}); + REQUIRE(update.vars_removed == std::vector{"usr3"}); + } + + TEST_CASE("do not remove ref-only defs") { + IndexFile previous("foo.cc"); + IndexFile current("foo.cc"); + + previous.Resolve(previous.ToTypeId("usr1")) + ->uses.push_back(Range(Position(1, 0))); + previous.Resolve(previous.ToFuncId("usr2")) + ->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)), + false /*is_implicit*/)); + previous.Resolve(previous.ToVarId("usr3")) + ->uses.push_back(Range(Position(3, 0))); + + IndexUpdate update = GetDelta(previous, current); + + REQUIRE(update.types_removed == std::vector{}); + REQUIRE(update.funcs_removed == std::vector{}); + REQUIRE(update.vars_removed == std::vector{}); + } + + TEST_CASE("func callers") { + IndexFile previous("foo.cc"); + IndexFile current("foo.cc"); + + IndexFunc* pf = previous.Resolve(previous.ToFuncId("usr")); + IndexFunc* cf = current.Resolve(current.ToFuncId("usr")); + + pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)), + false /*is_implicit*/)); + cf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)), + false /*is_implicit*/)); + + IndexUpdate update = GetDelta(previous, current); + + REQUIRE(update.funcs_removed == std::vector{}); + REQUIRE(update.funcs_callers.size() == 1); + REQUIRE(update.funcs_callers[0].id == QueryFuncId(0)); + REQUIRE(update.funcs_callers[0].to_remove.size() == 1); + REQUIRE(update.funcs_callers[0].to_remove[0].loc.range == + Range(Position(1, 0))); + REQUIRE(update.funcs_callers[0].to_add.size() == 1); + REQUIRE(update.funcs_callers[0].to_add[0].loc.range == Range(Position(2, 0))); + } + + TEST_CASE("type usages") { + IndexFile previous("foo.cc"); + IndexFile current("foo.cc"); + + IndexType* pt = previous.Resolve(previous.ToTypeId("usr")); + IndexType* ct = current.Resolve(current.ToTypeId("usr")); + + pt->uses.push_back(Range(Position(1, 0))); + ct->uses.push_back(Range(Position(2, 0))); + + IndexUpdate update = GetDelta(previous, current); + + REQUIRE(update.types_removed == std::vector{}); + REQUIRE(update.types_def_update == std::vector{}); + REQUIRE(update.types_uses.size() == 1); + REQUIRE(update.types_uses[0].to_remove.size() == 1); + REQUIRE(update.types_uses[0].to_remove[0].range == Range(Position(1, 0))); + REQUIRE(update.types_uses[0].to_add.size() == 1); + REQUIRE(update.types_uses[0].to_add[0].range == Range(Position(2, 0))); + } + + TEST_CASE("apply delta") { + IndexFile previous("foo.cc"); + IndexFile current("foo.cc"); + + IndexFunc* pf = previous.Resolve(previous.ToFuncId("usr")); + IndexFunc* cf = current.Resolve(current.ToFuncId("usr")); + pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)), + false /*is_implicit*/)); + pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)), + false /*is_implicit*/)); + cf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(4, 0)), + false /*is_implicit*/)); + cf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(5, 0)), + false /*is_implicit*/)); + + QueryDatabase db; + IdMap previous_map(&db, previous.id_cache); + IdMap current_map(&db, current.id_cache); + REQUIRE(db.funcs.size() == 1); + + IndexUpdate import_update = + IndexUpdate::CreateDelta(nullptr, &previous_map, nullptr, &previous); + IndexUpdate delta_update = IndexUpdate::CreateDelta( + &previous_map, ¤t_map, &previous, ¤t); + + db.ApplyIndexUpdate(&import_update); + REQUIRE(db.funcs[0].callers.size() == 2); + REQUIRE(db.funcs[0].callers[0].loc.range == Range(Position(1, 0))); + REQUIRE(db.funcs[0].callers[1].loc.range == Range(Position(2, 0))); + + db.ApplyIndexUpdate(&delta_update); + REQUIRE(db.funcs[0].callers.size() == 2); + REQUIRE(db.funcs[0].callers[0].loc.range == Range(Position(4, 0))); + REQUIRE(db.funcs[0].callers[1].loc.range == Range(Position(5, 0))); + } } - -TEST_CASE("remove defs") { - IndexFile previous("foo.cc"); - IndexFile current("foo.cc"); - - previous.Resolve(previous.ToTypeId("usr1"))->def.definition_spelling = - Range(Position(1, 0)); - previous.Resolve(previous.ToFuncId("usr2"))->def.definition_spelling = - Range(Position(2, 0)); - previous.Resolve(previous.ToVarId("usr3"))->def.definition_spelling = - Range(Position(3, 0)); - - IndexUpdate update = GetDelta(previous, current); - - REQUIRE(update.types_removed == std::vector{"usr1"}); - REQUIRE(update.funcs_removed == std::vector{"usr2"}); - REQUIRE(update.vars_removed == std::vector{"usr3"}); -} - -TEST_CASE("do not remove ref-only defs") { - IndexFile previous("foo.cc"); - IndexFile current("foo.cc"); - - previous.Resolve(previous.ToTypeId("usr1")) - ->uses.push_back(Range(Position(1, 0))); - previous.Resolve(previous.ToFuncId("usr2")) - ->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)), - false /*is_implicit*/)); - previous.Resolve(previous.ToVarId("usr3")) - ->uses.push_back(Range(Position(3, 0))); - - IndexUpdate update = GetDelta(previous, current); - - REQUIRE(update.types_removed == std::vector{}); - REQUIRE(update.funcs_removed == std::vector{}); - REQUIRE(update.vars_removed == std::vector{}); -} - -TEST_CASE("func callers") { - IndexFile previous("foo.cc"); - IndexFile current("foo.cc"); - - IndexFunc* pf = previous.Resolve(previous.ToFuncId("usr")); - IndexFunc* cf = current.Resolve(current.ToFuncId("usr")); - - pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)), - false /*is_implicit*/)); - cf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)), - false /*is_implicit*/)); - - IndexUpdate update = GetDelta(previous, current); - - REQUIRE(update.funcs_removed == std::vector{}); - REQUIRE(update.funcs_callers.size() == 1); - REQUIRE(update.funcs_callers[0].id == QueryFuncId(0)); - REQUIRE(update.funcs_callers[0].to_remove.size() == 1); - REQUIRE(update.funcs_callers[0].to_remove[0].loc.range == - Range(Position(1, 0))); - REQUIRE(update.funcs_callers[0].to_add.size() == 1); - REQUIRE(update.funcs_callers[0].to_add[0].loc.range == Range(Position(2, 0))); -} - -TEST_CASE("type usages") { - IndexFile previous("foo.cc"); - IndexFile current("foo.cc"); - - IndexType* pt = previous.Resolve(previous.ToTypeId("usr")); - IndexType* ct = current.Resolve(current.ToTypeId("usr")); - - pt->uses.push_back(Range(Position(1, 0))); - ct->uses.push_back(Range(Position(2, 0))); - - IndexUpdate update = GetDelta(previous, current); - - REQUIRE(update.types_removed == std::vector{}); - REQUIRE(update.types_def_update == std::vector{}); - REQUIRE(update.types_uses.size() == 1); - REQUIRE(update.types_uses[0].to_remove.size() == 1); - REQUIRE(update.types_uses[0].to_remove[0].range == Range(Position(1, 0))); - REQUIRE(update.types_uses[0].to_add.size() == 1); - REQUIRE(update.types_uses[0].to_add[0].range == Range(Position(2, 0))); -} - -TEST_CASE("apply delta") { - IndexFile previous("foo.cc"); - IndexFile current("foo.cc"); - - IndexFunc* pf = previous.Resolve(previous.ToFuncId("usr")); - IndexFunc* cf = current.Resolve(current.ToFuncId("usr")); - pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(1, 0)), - false /*is_implicit*/)); - pf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(2, 0)), - false /*is_implicit*/)); - cf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(4, 0)), - false /*is_implicit*/)); - cf->callers.push_back(IndexFuncRef(IndexFuncId(0), Range(Position(5, 0)), - false /*is_implicit*/)); - - QueryDatabase db; - IdMap previous_map(&db, previous.id_cache); - IdMap current_map(&db, current.id_cache); - REQUIRE(db.funcs.size() == 1); - - IndexUpdate import_update = - IndexUpdate::CreateDelta(nullptr, &previous_map, nullptr, &previous); - IndexUpdate delta_update = IndexUpdate::CreateDelta( - &previous_map, ¤t_map, &previous, ¤t); - - db.ApplyIndexUpdate(&import_update); - REQUIRE(db.funcs[0].callers.size() == 2); - REQUIRE(db.funcs[0].callers[0].loc.range == Range(Position(1, 0))); - REQUIRE(db.funcs[0].callers[1].loc.range == Range(Position(2, 0))); - - db.ApplyIndexUpdate(&delta_update); - REQUIRE(db.funcs[0].callers.size() == 2); - REQUIRE(db.funcs[0].callers[0].loc.range == Range(Position(4, 0))); - REQUIRE(db.funcs[0].callers[1].loc.range == Range(Position(5, 0))); -} - -TEST_SUITE_END(); \ No newline at end of file diff --git a/src/working_files.cc b/src/working_files.cc index 21c10b83..97fcc9b6 100644 --- a/src/working_files.cc +++ b/src/working_files.cc @@ -379,107 +379,105 @@ std::vector WorkingFiles::AsUnsavedFiles() { return result; } -TEST_SUITE("WorkingFile"); - lsPosition CharPos(const WorkingFile& file, - char character, - int character_offset = 0) { + char character, + int character_offset = 0) { return CharPos(file.buffer_content, character, character_offset); } -TEST_CASE("simple call") { - WorkingFile f("foo.cc", "abcd(1, 2"); - int active_param = 0; - REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '('), &active_param) == - "abcd"); - REQUIRE(active_param == 0); - REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '1'), &active_param) == - "abcd"); - REQUIRE(active_param == 0); - REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ','), &active_param) == - "abcd"); - REQUIRE(active_param == 1); - REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ' '), &active_param) == - "abcd"); - REQUIRE(active_param == 1); - REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '2'), &active_param) == - "abcd"); - REQUIRE(active_param == 1); +TEST_SUITE("WorkingFile") { + TEST_CASE("simple call") { + WorkingFile f("foo.cc", "abcd(1, 2"); + int active_param = 0; + REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '('), &active_param) == + "abcd"); + REQUIRE(active_param == 0); + REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '1'), &active_param) == + "abcd"); + REQUIRE(active_param == 0); + REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ','), &active_param) == + "abcd"); + REQUIRE(active_param == 1); + REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ' '), &active_param) == + "abcd"); + REQUIRE(active_param == 1); + REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '2'), &active_param) == + "abcd"); + REQUIRE(active_param == 1); + } + + TEST_CASE("nested call") { + WorkingFile f("foo.cc", "abcd(efg(), 2"); + int active_param = 0; + REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '('), &active_param) == + "abcd"); + REQUIRE(active_param == 0); + REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'e'), &active_param) == + "abcd"); + REQUIRE(active_param == 0); + REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'f'), &active_param) == + "abcd"); + REQUIRE(active_param == 0); + REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g'), &active_param) == + "abcd"); + REQUIRE(active_param == 0); + REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g', 1), &active_param) == + "efg"); + REQUIRE(active_param == 0); + REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g', 2), &active_param) == + "efg"); + REQUIRE(active_param == 0); + REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ','), &active_param) == + "abcd"); + REQUIRE(active_param == 1); + REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ' '), &active_param) == + "abcd"); + REQUIRE(active_param == 1); + } + + TEST_CASE("auto-insert )") { + WorkingFile f("foo.cc", "abc()"); + int active_param = 0; + REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ')'), &active_param) == + "abc"); + REQUIRE(active_param == 0); + } + + TEST_CASE("existing completion") { + WorkingFile f("foo.cc", "zzz.asdf"); + bool is_global_completion; + std::string existing_completion; + + f.FindStableCompletionSource(CharPos(f, '.'), &is_global_completion, + &existing_completion); + REQUIRE(existing_completion == "zzz"); + f.FindStableCompletionSource(CharPos(f, 'a', 1), &is_global_completion, + &existing_completion); + REQUIRE(existing_completion == "a"); + f.FindStableCompletionSource(CharPos(f, 's', 1), &is_global_completion, + &existing_completion); + REQUIRE(existing_completion == "as"); + f.FindStableCompletionSource(CharPos(f, 'd', 1), &is_global_completion, + &existing_completion); + REQUIRE(existing_completion == "asd"); + f.FindStableCompletionSource(CharPos(f, 'f', 1), &is_global_completion, + &existing_completion); + REQUIRE(existing_completion == "asdf"); + } + + TEST_CASE("existing completion underscore") { + WorkingFile f("foo.cc", "ABC_DEF"); + bool is_global_completion; + std::string existing_completion; + + f.FindStableCompletionSource(CharPos(f, 'C'), &is_global_completion, + &existing_completion); + REQUIRE(existing_completion == "AB"); + f.FindStableCompletionSource(CharPos(f, '_'), &is_global_completion, + &existing_completion); + REQUIRE(existing_completion == "ABC"); + f.FindStableCompletionSource(CharPos(f, 'D'), &is_global_completion, + &existing_completion); + REQUIRE(existing_completion == "ABC_"); + } } - -TEST_CASE("nested call") { - WorkingFile f("foo.cc", "abcd(efg(), 2"); - int active_param = 0; - REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, '('), &active_param) == - "abcd"); - REQUIRE(active_param == 0); - REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'e'), &active_param) == - "abcd"); - REQUIRE(active_param == 0); - REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'f'), &active_param) == - "abcd"); - REQUIRE(active_param == 0); - REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g'), &active_param) == - "abcd"); - REQUIRE(active_param == 0); - REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g', 1), &active_param) == - "efg"); - REQUIRE(active_param == 0); - REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, 'g', 2), &active_param) == - "efg"); - REQUIRE(active_param == 0); - REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ','), &active_param) == - "abcd"); - REQUIRE(active_param == 1); - REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ' '), &active_param) == - "abcd"); - REQUIRE(active_param == 1); -} - -TEST_CASE("auto-insert )") { - WorkingFile f("foo.cc", "abc()"); - int active_param = 0; - REQUIRE(f.FindClosestCallNameInBuffer(CharPos(f, ')'), &active_param) == - "abc"); - REQUIRE(active_param == 0); -} - -TEST_CASE("existing completion") { - WorkingFile f("foo.cc", "zzz.asdf"); - bool is_global_completion; - std::string existing_completion; - - f.FindStableCompletionSource(CharPos(f, '.'), &is_global_completion, - &existing_completion); - REQUIRE(existing_completion == "zzz"); - f.FindStableCompletionSource(CharPos(f, 'a', 1), &is_global_completion, - &existing_completion); - REQUIRE(existing_completion == "a"); - f.FindStableCompletionSource(CharPos(f, 's', 1), &is_global_completion, - &existing_completion); - REQUIRE(existing_completion == "as"); - f.FindStableCompletionSource(CharPos(f, 'd', 1), &is_global_completion, - &existing_completion); - REQUIRE(existing_completion == "asd"); - f.FindStableCompletionSource(CharPos(f, 'f', 1), &is_global_completion, - &existing_completion); - REQUIRE(existing_completion == "asdf"); -} - -TEST_CASE("existing completion underscore") { - WorkingFile f("foo.cc", "ABC_DEF"); - bool is_global_completion; - std::string existing_completion; - - f.FindStableCompletionSource(CharPos(f, 'C'), &is_global_completion, - &existing_completion); - REQUIRE(existing_completion == "AB"); - f.FindStableCompletionSource(CharPos(f, '_'), &is_global_completion, - &existing_completion); - REQUIRE(existing_completion == "ABC"); - f.FindStableCompletionSource(CharPos(f, 'D'), &is_global_completion, - &existing_completion); - REQUIRE(existing_completion == "ABC_"); -} - -TEST_SUITE_END(); diff --git a/third_party/doctest b/third_party/doctest index 11147357..79a37982 160000 --- a/third_party/doctest +++ b/third_party/doctest @@ -1 +1 @@ -Subproject commit 11147357234d0166c9ec96c4d9e328464b884da3 +Subproject commit 79a379827251cd819c5286070834ccd0ac628af9 diff --git a/third_party/loguru b/third_party/loguru index ac23215b..83b6f3c3 160000 --- a/third_party/loguru +++ b/third_party/loguru @@ -1 +1 @@ -Subproject commit ac23215b4b9e878dfe5c2fd3d4afbf7a63cdad12 +Subproject commit 83b6f3c3d16e40453ec0d12d3baef42cd2f37c3b diff --git a/third_party/rapidjson b/third_party/rapidjson index 0163a53f..17ae6ffa 160000 --- a/third_party/rapidjson +++ b/third_party/rapidjson @@ -1 +1 @@ -Subproject commit 0163a53f4a1c72e6a05848a63d80eee0d8e3f387 +Subproject commit 17ae6ffa857173c25708e61610121bc908c0a6cd diff --git a/third_party/sparsepp b/third_party/sparsepp index b1d54fbe..bfb0de71 160000 --- a/third_party/sparsepp +++ b/third_party/sparsepp @@ -1 +1 @@ -Subproject commit b1d54fbe547cab3a13d181f16f1de758d6827f81 +Subproject commit bfb0de71ee7fa12a5f12c3ef61ce9f1d6d86d907