diff options
author | Daniel Jasper <djasper@google.com> | 2012-10-08 16:08:15 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2012-10-08 16:08:15 +0000 |
commit | d3420c906e3605d94c084e8b8b1f3fa490093c86 (patch) | |
tree | 2a1722cf436983cf2fcefbe3e56bdb6e538c3c57 /unittests/Tooling | |
parent | 6b34c176994aaa781eff6cd8755a48cfb109e809 (diff) |
Support symlinks and relative paths in complilation databases.
Review: http://llvm-reviews.chandlerc.com/D30
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@165392 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/Tooling')
-rw-r--r-- | unittests/Tooling/CompilationDatabaseTest.cpp | 95 |
1 files changed, 86 insertions, 9 deletions
diff --git a/unittests/Tooling/CompilationDatabaseTest.cpp b/unittests/Tooling/CompilationDatabaseTest.cpp index 560b931718..5bbbe2aa2e 100644 --- a/unittests/Tooling/CompilationDatabaseTest.cpp +++ b/unittests/Tooling/CompilationDatabaseTest.cpp @@ -11,6 +11,7 @@ #include "clang/AST/DeclCXX.h" #include "clang/AST/DeclGroup.h" #include "clang/Frontend/FrontendAction.h" +#include "clang/Tooling/FileMatchTrie.h" #include "clang/Tooling/JSONCompilationDatabase.h" #include "clang/Tooling/Tooling.h" #include "gtest/gtest.h" @@ -55,13 +56,13 @@ TEST(JSONCompilationDatabase, GetAllFiles) { getAllFiles("[]", ErrorMessage)) << ErrorMessage; std::vector<std::string> expected_files; - expected_files.push_back("file1"); - expected_files.push_back("file2"); + expected_files.push_back("/dir/file1"); + expected_files.push_back("/dir/file2"); EXPECT_EQ(expected_files, getAllFiles( - "[{\"directory\":\"dir\"," + "[{\"directory\":\"/dir\"," "\"command\":\"command\"," "\"file\":\"file1\"}," - " {\"directory\":\"dir\"," + " {\"directory\":\"/dir\"," "\"command\":\"command\"," "\"file\":\"file2\"}]", ErrorMessage)) << ErrorMessage; @@ -81,6 +82,82 @@ static CompileCommand findCompileArgsInJsonDatabase(StringRef FileName, return Commands[0]; } +struct FakeComparator : public PathComparator { + virtual ~FakeComparator() {} + virtual bool equivalent(const Twine &FileA, const Twine &FileB) const { + return StringRef(FileA.str()).equals_lower(FileB.str()); + } +}; + +class FileMatchTrieTest : public ::testing::Test { +protected: + FileMatchTrieTest() : Trie(new FakeComparator()) {} + + StringRef find(StringRef Path) { + llvm::raw_string_ostream ES(Error); + return Trie.findEquivalent(Path, ES); + } + + FileMatchTrie Trie; + std::string Error; +}; + +TEST_F(FileMatchTrieTest, InsertingRelativePath) { + Trie.insert("/path/file.cc"); + Trie.insert("file.cc"); + EXPECT_EQ("/path/file.cc", find("/path/file.cc")); +} + +TEST_F(FileMatchTrieTest, MatchingRelativePath) { + EXPECT_EQ("", find("file.cc")); +} + +TEST_F(FileMatchTrieTest, ReturnsBestResults) { + Trie.insert("/d/c/b.cc"); + Trie.insert("/d/b/b.cc"); + EXPECT_EQ("/d/b/b.cc", find("/d/b/b.cc")); +} + +TEST_F(FileMatchTrieTest, HandlesSymlinks) { + Trie.insert("/AA/file.cc"); + EXPECT_EQ("/AA/file.cc", find("/aa/file.cc")); +} + +TEST_F(FileMatchTrieTest, ReportsSymlinkAmbiguity) { + Trie.insert("/Aa/file.cc"); + Trie.insert("/aA/file.cc"); + EXPECT_TRUE(find("/aa/file.cc").empty()); + EXPECT_EQ("Path is ambiguous", Error); +} + +TEST_F(FileMatchTrieTest, LongerMatchingSuffixPreferred) { + Trie.insert("/src/Aa/file.cc"); + Trie.insert("/src/aA/file.cc"); + Trie.insert("/SRC/aa/file.cc"); + EXPECT_EQ("/SRC/aa/file.cc", find("/src/aa/file.cc")); +} + +TEST_F(FileMatchTrieTest, EmptyTrie) { + EXPECT_TRUE(find("/some/path").empty()); +} + +TEST_F(FileMatchTrieTest, NoResult) { + Trie.insert("/somepath/otherfile.cc"); + Trie.insert("/otherpath/somefile.cc"); + EXPECT_EQ("", find("/somepath/somefile.cc")); +} + +TEST_F(FileMatchTrieTest, RootElementDifferent) { + Trie.insert("/path/file.cc"); + Trie.insert("/otherpath/file.cc"); + EXPECT_EQ("/path/file.cc", find("/path/file.cc")); +} + +TEST_F(FileMatchTrieTest, CannotResolveRelativePath) { + EXPECT_EQ("", find("relative-path.cc")); + EXPECT_EQ("Cannot resolve relative paths", Error); +} + TEST(findCompileArgsInJsonDatabase, FindsNothingIfEmpty) { std::string ErrorMessage; CompileCommand NotFound = findCompileArgsInJsonDatabase( @@ -148,7 +225,7 @@ TEST(findCompileArgsInJsonDatabase, ReadsDirectoryWithSpaces) { } TEST(findCompileArgsInJsonDatabase, FindsEntry) { - StringRef Directory("directory"); + StringRef Directory("/directory"); StringRef FileName("file"); StringRef Command("command"); std::string JsonDatabase = "["; @@ -162,19 +239,19 @@ TEST(findCompileArgsInJsonDatabase, FindsEntry) { JsonDatabase += "]"; std::string ErrorMessage; CompileCommand FoundCommand = findCompileArgsInJsonDatabase( - "file4", JsonDatabase, ErrorMessage); - EXPECT_EQ("directory4", FoundCommand.Directory) << ErrorMessage; + "/directory4/file4", JsonDatabase, ErrorMessage); + EXPECT_EQ("/directory4", FoundCommand.Directory) << ErrorMessage; ASSERT_EQ(1u, FoundCommand.CommandLine.size()) << ErrorMessage; EXPECT_EQ("command4", FoundCommand.CommandLine[0]) << ErrorMessage; } static std::vector<std::string> unescapeJsonCommandLine(StringRef Command) { std::string JsonDatabase = - ("[{\"directory\":\"\", \"file\":\"test\", \"command\": \"" + + ("[{\"directory\":\"/root\", \"file\":\"test\", \"command\": \"" + Command + "\"}]").str(); std::string ErrorMessage; CompileCommand FoundCommand = findCompileArgsInJsonDatabase( - "test", JsonDatabase, ErrorMessage); + "/root/test", JsonDatabase, ErrorMessage); EXPECT_TRUE(ErrorMessage.empty()) << ErrorMessage; return FoundCommand.CommandLine; } |