Clang: Handle native file in the file cache

Different types are introduced for normalized and native file path. So the
compiler is warning you if you try the wrong format.

Change-Id: I1da0686b142cbf9bb7578468c2b50f90a94cebf9
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
Reviewed-by: Tim Jenssen <tim.jenssen@qt.io>
This commit is contained in:
Marco Bubke
2017-11-16 17:48:53 +01:00
parent bb2f9574b4
commit a15250051d
38 changed files with 985 additions and 287 deletions

View File

@@ -32,9 +32,10 @@
namespace {
using ClangBackEnd::FilePathId;
using FPCB = ClangBackEnd::FilePathCacheBase;
using Cache = ClangBackEnd::FilePathCache<NiceMock<MockFilePathStorage>>;
using ClangBackEnd::FilePathId;
using NFP = ClangBackEnd::FilePath;
using ClangBackEnd::FilePathView;
class FilePathCache : public testing::Test
{
@@ -46,199 +47,86 @@ protected:
Cache cache{mockStorage};
};
TEST_F(FilePathCache, FilePathSlashForEmptyPath)
{
auto slashIndex = FPCB::lastSlashIndex("");
ASSERT_THAT(slashIndex, -1);
}
TEST_F(FilePathCache, FilePathSlashForSingleSlash)
{
auto slashIndex = FPCB::lastSlashIndex("/");
ASSERT_THAT(slashIndex, 0);
}
TEST_F(FilePathCache, FilePathSlashForFileInRoot)
{
auto slashIndex = FPCB::lastSlashIndex("/file.h");
ASSERT_THAT(slashIndex, 0);
}
TEST_F(FilePathCache, FilePathSlashForSomeLongerPath)
{
auto slashIndex = FPCB::lastSlashIndex("/path/to/some/file.h");
ASSERT_THAT(slashIndex, 13);
}
TEST_F(FilePathCache, FilePathSlashForFileNameOnly)
{
auto slashIndex = FPCB::lastSlashIndex("file.h");
ASSERT_THAT(slashIndex, -1);
}
TEST_F(FilePathCache, DirectoryPathForEmptyPath)
{
auto slashIndex = FPCB::lastSlashIndex("");
auto directoryPath = FPCB::directoryPath("", slashIndex);
ASSERT_THAT(directoryPath, "");
}
TEST_F(FilePathCache, DirectoryPathForSingleSlashPath)
{
Utils::SmallStringView singleSlashPath{"/"};
auto slashIndex = FPCB::lastSlashIndex(singleSlashPath);
auto directoryPath = FPCB::directoryPath(singleSlashPath, slashIndex);
ASSERT_THAT(directoryPath, "");
}
TEST_F(FilePathCache, DirectoryPathForLongerPath)
{
Utils::SmallStringView longerPath{"/path/to/some/file.h"};
auto slashIndex = FPCB::lastSlashIndex(longerPath);
auto directoryPath = FPCB::directoryPath(longerPath, slashIndex);
ASSERT_THAT(directoryPath, "/path/to/some");
}
TEST_F(FilePathCache, DirectoryPathForFileNameOnly)
{
Utils::SmallStringView longerPath{"file.h"};
auto slashIndex = FPCB::lastSlashIndex(longerPath);
auto directoryPath = FPCB::directoryPath(longerPath, slashIndex);
ASSERT_THAT(directoryPath, IsEmpty());
}
TEST_F(FilePathCache, FileNameForEmptyPath)
{
auto slashIndex = FPCB::lastSlashIndex("");
auto fileName = FPCB::fileName("", slashIndex);
ASSERT_THAT(fileName, "");
}
TEST_F(FilePathCache, FileNameForSingleSlashPath)
{
Utils::SmallStringView singleSlashPath{"/"};
auto slashIndex = FPCB::lastSlashIndex(singleSlashPath);
auto fileName = FPCB::fileName(singleSlashPath, slashIndex);
ASSERT_THAT(fileName, "");
}
TEST_F(FilePathCache, FileNameForLongerPath)
{
Utils::SmallStringView longerPath{"/path/to/some/file.h"};
auto slashIndex = FPCB::lastSlashIndex(longerPath);
auto fileName = FPCB::fileName(longerPath, slashIndex);
ASSERT_THAT(fileName, "file.h");
}
TEST_F(FilePathCache, FileNameForFileNameOnly)
{
Utils::SmallStringView longerPath{"file.h"};
auto slashIndex = FPCB::lastSlashIndex(longerPath);
auto fileName = FPCB::fileName(longerPath, slashIndex);
ASSERT_THAT(fileName, "file.h");
}
TEST_F(FilePathCache, FilePathIdWithOutAnyEntryCallDirectoryId)
{
EXPECT_CALL(mockStorage, fetchDirectoryId(Eq("/path/to")));
cache.filePathId("/path/to/file.cpp");
cache.filePathId(FilePathView("/path/to/file.cpp"));
}
TEST_F(FilePathCache, FilePathIdWithOutAnyEntryCalls)
{
EXPECT_CALL(mockStorage, fetchSourceId(5, Eq("file.cpp")));
cache.filePathId("/path/to/file.cpp");
cache.filePathId(FilePathView("/path/to/file.cpp"));
}
TEST_F(FilePathCache, DirectoryIdOfFilePathIdWithOutAnyEntry)
{
auto filePathId = cache.filePathId("/path/to/file.cpp");
auto filePathId = cache.filePathId(FilePathView("/path/to/file.cpp"));
ASSERT_THAT(filePathId.directoryId, 5);
}
TEST_F(FilePathCache, FileNameIdOfFilePathIdWithOutAnyEntry)
{
auto filePathId = cache.filePathId("/path/to/file.cpp");
auto filePathId = cache.filePathId(FilePathView("/path/to/file.cpp"));
ASSERT_THAT(filePathId.fileNameId, 42);
}
TEST_F(FilePathCache, IfEntryExistsDontCallInStrorage)
{
cache.filePathId("/path/to/file.cpp");
cache.filePathId(FilePathView("/path/to/file.cpp"));
EXPECT_CALL(mockStorage, fetchDirectoryId(Eq("/path/to"))).Times(0);
EXPECT_CALL(mockStorage, fetchSourceId(5, Eq("file.cpp"))).Times(0);
cache.filePathId("/path/to/file.cpp");
cache.filePathId(FilePathView("/path/to/file.cpp"));
}
TEST_F(FilePathCache, IfDirectoryEntryExistsDontCallFetchDirectoryIdButStillCallFetchSourceId)
{
cache.filePathId("/path/to/file2.cpp");
cache.filePathId(FilePathView("/path/to/file2.cpp"));
EXPECT_CALL(mockStorage, fetchDirectoryId(Eq("/path/to"))).Times(0);
EXPECT_CALL(mockStorage, fetchSourceId(5, Eq("file.cpp")));
cache.filePathId("/path/to/file.cpp");
cache.filePathId(FilePathView("/path/to/file.cpp"));
}
TEST_F(FilePathCache, GetFileNameIdWithCachedValue)
{
cache.filePathId("/path/to/file.cpp");
cache.filePathId(FilePathView("/path/to/file.cpp"));
auto filePathId = cache.filePathId("/path/to/file.cpp");
auto filePathId = cache.filePathId(FilePathView("/path/to/file.cpp"));
ASSERT_THAT(filePathId.fileNameId, 42);
}
TEST_F(FilePathCache, GetFileNameIdWithDirectoryIdCached)
{
cache.filePathId("/path/to/file.cpp");
cache.filePathId(FilePathView("/path/to/file.cpp"));
auto filePathId = cache.filePathId("/path/to/file2.cpp");
auto filePathId = cache.filePathId(FilePathView("/path/to/file2.cpp"));
ASSERT_THAT(filePathId.fileNameId, 63);
}
TEST_F(FilePathCache, GetDirectyIdWithCachedValue)
{
cache.filePathId("/path/to/file.cpp");
cache.filePathId(FilePathView("/path/to/file.cpp"));
auto filePathId = cache.filePathId("/path/to/file2.cpp");
auto filePathId = cache.filePathId(FilePathView("/path/to/file2.cpp"));
ASSERT_THAT(filePathId.directoryId, 5);
}
TEST_F(FilePathCache, GetDirectyIdWithDirectoryIdCached)
{
cache.filePathId("/path/to/file.cpp");
cache.filePathId(FilePathView("/path/to/file.cpp"));
auto filePathId = cache.filePathId("/path/to/file2.cpp");
auto filePathId = cache.filePathId(FilePathView("/path/to/file2.cpp"));
ASSERT_THAT(filePathId.directoryId, 5);
}
@@ -252,11 +140,11 @@ TEST_F(FilePathCache, ThrowForGettingAFilePathWithAnInvalidId)
TEST_F(FilePathCache, GetAFilePath)
{
FilePathId filePathId = cache.filePathId("/path/to/file.cpp");
FilePathId filePathId = cache.filePathId(FilePathView("/path/to/file.cpp"));
auto filePath = cache.filePath(filePathId);
ASSERT_THAT(filePath, Eq("/path/to/file.cpp"));
ASSERT_THAT(filePath, Eq(FilePathView{"/path/to/file.cpp"}));
}
TEST_F(FilePathCache, GetAFilePathWithCachedFilePathId)
@@ -265,7 +153,7 @@ TEST_F(FilePathCache, GetAFilePathWithCachedFilePathId)
auto filePath = cache.filePath(filePathId);
ASSERT_THAT(filePath, Eq("/path/to/file.cpp"));
ASSERT_THAT(filePath, Eq(FilePathView{"/path/to/file.cpp"}));
}
void FilePathCache::SetUp()
@@ -283,4 +171,3 @@ void FilePathCache::SetUp()
}
}