From f56ec53fa1b92553847bcb9e90dbedddcd7a28e2 Mon Sep 17 00:00:00 2001 From: Marco Bubke Date: Mon, 8 Aug 2016 15:58:48 +0200 Subject: [PATCH] Clang: Test for file path RequestSourceLocationsForRenamingMessage Change-Id: Ia384deb60e644d8894d7c56f81b3deb8b7706c11 Reviewed-by: Tim Jenssen --- .../sourcelocationscontainer.h | 6 +++++ .../source/sourcelocationsutils.h | 25 ++++++++++++++----- tests/unit/unittest/refactoringservertest.cpp | 9 +++++-- 3 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/libs/clangbackendipc/sourcelocationscontainer.h b/src/libs/clangbackendipc/sourcelocationscontainer.h index 109fc3114e1..6df6f178645 100644 --- a/src/libs/clangbackendipc/sourcelocationscontainer.h +++ b/src/libs/clangbackendipc/sourcelocationscontainer.h @@ -105,6 +105,12 @@ public: return SourceLocationsContainer(Utils::clone(filePathHash), Utils::clone(sourceLocationContainers_)); } + + const std::unordered_map &filePathsForTestOnly() const + { + return filePathHash; + } + private: std::unordered_map filePathHash; std::vector sourceLocationContainers_; diff --git a/src/tools/clangrefactoringbackend/source/sourcelocationsutils.h b/src/tools/clangrefactoringbackend/source/sourcelocationsutils.h index c2ae107f677..70e14b92d22 100644 --- a/src/tools/clangrefactoringbackend/source/sourcelocationsutils.h +++ b/src/tools/clangrefactoringbackend/source/sourcelocationsutils.h @@ -43,14 +43,26 @@ namespace ClangBackEnd { inline -Utils::SmallString absolutePath(const char *path) +llvm::SmallString<256> absolutePath(const char *path) { llvm::SmallString<256> absolutePath(path); if (!llvm::sys::path::is_absolute(absolutePath)) llvm::sys::fs::make_absolute(absolutePath); - return Utils::SmallString(absolutePath.begin(), absolutePath.end()); + return absolutePath; +} + +template +Utils::SmallString fromNativePath(Container container) +{ + Utils::SmallString path(container.data(), container.size()); + +#ifdef WIN32 + std::replace(path.begin(), path.end(), '\\', '/'); +#endif + + return path; } inline @@ -65,12 +77,13 @@ void appendSourceLocationsToSourceLocationsContainer( clang::FullSourceLoc fullSourceLocation(sourceLocation, sourceManager); auto fileId = fullSourceLocation.getFileID(); auto fileEntry = sourceManager.getFileEntryForID(fileId); - auto fileName = fileEntry->getName(); - auto fileDirectoryPath = absolutePath(fileEntry->getDir()->getName()); + auto filePath = absolutePath(fileEntry->getName()); + auto fileName = llvm::sys::path::filename(filePath); + llvm::sys::path::remove_filename(filePath); sourceLocationsContainer.insertFilePath(fileId.getHashValue(), - std::move(fileDirectoryPath), - fileName); + fromNativePath(filePath), + fromNativePath(fileName)); sourceLocationsContainer.insertSourceLocation(fileId.getHashValue(), fullSourceLocation.getSpellingLineNumber(), fullSourceLocation.getSpellingColumnNumber()); diff --git a/tests/unit/unittest/refactoringservertest.cpp b/tests/unit/unittest/refactoringservertest.cpp index 906ff59d555..257b37104ec 100644 --- a/tests/unit/unittest/refactoringservertest.cpp +++ b/tests/unit/unittest/refactoringservertest.cpp @@ -39,12 +39,15 @@ namespace { using testing::AllOf; using testing::Contains; +using testing::Pair; using testing::PrintToString; using testing::Property; +using testing::_; using ClangBackEnd::RequestSourceLocationsForRenamingMessage; using ClangBackEnd::SourceLocationsContainer; using ClangBackEnd::SourceLocationsForRenamingMessage; +using ClangBackEnd::FilePath; MATCHER_P2(IsSourceLocation, line, column, std::string(negation ? "isn't" : "is") @@ -83,9 +86,11 @@ TEST_F(RefactoringServer, RequestSourceLocationsForRenamingMessage) sourceLocationsForRenamingMessage( AllOf(Property(&SourceLocationsForRenamingMessage::symbolName, "v"), Property(&SourceLocationsForRenamingMessage::sourceLocations, - Property(&SourceLocationsContainer::sourceLocationContainers, + AllOf(Property(&SourceLocationsContainer::sourceLocationContainers, AllOf(Contains(IsSourceLocation(1, 5)), - Contains(IsSourceLocation(3, 9)))))))) + Contains(IsSourceLocation(3, 9)))), + Property(&SourceLocationsContainer::filePathsForTestOnly, + Contains(Pair(_, FilePath(TESTDATA_DIR, "renamevariable.cpp"))))))))) .Times(1); refactoringServer.requestSourceLocationsForRenamingMessage(std::move(requestSourceLocationsForRenamingMessage));