diff --git a/bin/test/data/FilePathTestSuite/a.cpp b/bin/test/data/FilePathTestSuite/a.cpp new file mode 100644 index 00000000..e69de29b diff --git a/bin/test/data/FilePathTestSuite/b.cc b/bin/test/data/FilePathTestSuite/b.cc new file mode 100644 index 00000000..e69de29b diff --git a/src/app/qt/element/QtCodeFileList.cpp b/src/app/qt/element/QtCodeFileList.cpp index 64bed1f0..415b9a1b 100644 --- a/src/app/qt/element/QtCodeFileList.cpp +++ b/src/app/qt/element/QtCodeFileList.cpp @@ -39,7 +39,7 @@ void QtCodeFileList::addCodeSnippet( const std::string& code, const TokenLocationFile& locationFile ){ - std::string fileName = FileSystem::fileName(locationFile.getFilePath()); + std::string fileName = locationFile.getFilePath().fileName(); QtCodeFile* file = nullptr; for (std::shared_ptr filePtr : m_files) @@ -53,7 +53,7 @@ void QtCodeFileList::addCodeSnippet( if (!file) { - std::shared_ptr filePtr = std::make_shared(locationFile.getFilePath(), this); + std::shared_ptr filePtr = std::make_shared(locationFile.getFilePath().absoluteStr(), this); m_files.push_back(filePtr); file = filePtr.get(); diff --git a/src/app/qt/view/QtCodeView.cpp b/src/app/qt/view/QtCodeView.cpp index b9d9bbf4..d90067e7 100644 --- a/src/app/qt/view/QtCodeView.cpp +++ b/src/app/qt/view/QtCodeView.cpp @@ -104,7 +104,7 @@ void QtCodeView::doShowCodeFile(const CodeSnippetParams& params) ptr->setErrorMessages(m_errorMessages); ptr->addCodeSnippet(1, params.code, params.locationFile); - ptr->setWindowTitle(FileSystem::fileName(params.locationFile.getFilePath()).c_str()); + ptr->setWindowTitle(params.locationFile.getFilePath().fileName().c_str()); ptr->show(); float percent = float(params.startLineNumber + params.endLineNumber) / float(params.lineCount) / 2; diff --git a/src/lib/CMakeLists.txt b/src/lib/CMakeLists.txt index cebfecfb..b8bf44eb 100644 --- a/src/lib/CMakeLists.txt +++ b/src/lib/CMakeLists.txt @@ -192,6 +192,8 @@ add_files( utility/file/FileInfo.h utility/file/FileManager.cpp utility/file/FileManager.h + utility/file/FilePath.cpp + utility/file/FilePath.h utility/file/FileRegister.cpp utility/file/FileRegister.h utility/file/FileSystem.cpp diff --git a/src/lib/Project.cpp b/src/lib/Project.cpp index 01514b87..d99abc51 100644 --- a/src/lib/Project.cpp +++ b/src/lib/Project.cpp @@ -103,11 +103,11 @@ void Project::parseCode() } m_fileManager->fetchFilePaths(); - std::set addedFilePaths = m_fileManager->getAddedFilePaths(); - std::set updatedFilePaths = m_fileManager->getUpdatedFilePaths(); - std::set removedFilePaths = m_fileManager->getRemovedFilePaths(); + std::set addedFilePaths = m_fileManager->getAddedFilePaths(); + std::set updatedFilePaths = m_fileManager->getUpdatedFilePaths(); + std::set removedFilePaths = m_fileManager->getRemovedFilePaths(); - std::set dependingFilePaths; + std::set dependingFilePaths; dependingFilePaths = m_storage->getDependingFilePathsAndRemoveFileNodes(updatedFilePaths); updatedFilePaths.insert(dependingFilePaths.begin(), dependingFilePaths.end()); @@ -117,7 +117,7 @@ void Project::parseCode() m_storage->clearFileData(updatedFilePaths); m_storage->clearFileData(removedFilePaths); - std::vector filesToParse; + std::vector filesToParse; filesToParse.insert(filesToParse.end(), addedFilePaths.begin(), addedFilePaths.end()); filesToParse.insert(filesToParse.end(), updatedFilePaths.begin(), updatedFilePaths.end()); diff --git a/src/lib/component/controller/CodeController.cpp b/src/lib/component/controller/CodeController.cpp index 18dd93da..e2d9f383 100644 --- a/src/lib/component/controller/CodeController.cpp +++ b/src/lib/component/controller/CodeController.cpp @@ -108,7 +108,7 @@ std::vector CodeController::getSnippetsForActiveTok for (CodeView::CodeSnippetParams& params : fileSnippets) { params.locationFile = m_locationAccess->getTokenLocationsForLinesInFile( - file->getFilePath(), params.startLineNumber, params.endLineNumber); + file->getFilePath().str(), params.startLineNumber, params.endLineNumber); } if (declarationId != 0) @@ -144,7 +144,7 @@ std::vector CodeController::getSnippetsForActiveTok std::vector CodeController::getSnippetsForFile(const TokenLocationFile* file) const { - std::shared_ptr textAccess = TextAccess::createFromFile(file->getFilePath()); + std::shared_ptr textAccess = TextAccess::createFromFile(file->getFilePath().str()); std::vector> ranges = getSnippetRangesForFile(file); std::vector snippets; diff --git a/src/lib/component/view/CodeView.cpp b/src/lib/component/view/CodeView.cpp index 3f9d374a..5a616f8c 100644 --- a/src/lib/component/view/CodeView.cpp +++ b/src/lib/component/view/CodeView.cpp @@ -40,21 +40,21 @@ bool CodeView::CodeSnippetParams::sort(const CodeSnippetParams& a, const CodeSni return false; } + const FilePath& aFilePath = a.locationFile.getFilePath(); + const FilePath& bFilePath = b.locationFile.getFilePath(); + // different files - if (a.locationFile.getFilePath() != b.locationFile.getFilePath()) + if (aFilePath != bFilePath) { // first header - if (FileSystem::filePathWithoutExtension(a.locationFile.getFilePath()) == - FileSystem::filePathWithoutExtension(b.locationFile.getFilePath())) + if (aFilePath.withoutExtension() == bFilePath.withoutExtension()) { - return FileSystem::extension(a.locationFile.getFilePath()) > - FileSystem::extension(b.locationFile.getFilePath()); + return aFilePath.extension() > bFilePath.extension(); } // alphabetical filepath without extension else { - return FileSystem::filePathWithoutExtension(a.locationFile.getFilePath()) - < FileSystem::filePathWithoutExtension(b.locationFile.getFilePath()); + return aFilePath.withoutExtension() < bFilePath.withoutExtension(); } } diff --git a/src/lib/data/Storage.cpp b/src/lib/data/Storage.cpp index ebf9186a..3074b268 100644 --- a/src/lib/data/Storage.cpp +++ b/src/lib/data/Storage.cpp @@ -42,17 +42,17 @@ void Storage::clear() m_errorLocationCollection.clear(); } -void Storage::clearFileData(const std::set& filePaths) +void Storage::clearFileData(const std::set& filePaths) { - for (const std::string& filePath : filePaths) + for (const FilePath& filePath : filePaths) { - TokenLocationFile* errorFile = m_errorLocationCollection.findTokenLocationFileByPath(filePath); + TokenLocationFile* errorFile = m_errorLocationCollection.findTokenLocationFileByPath(filePath.str()); if (errorFile) { m_errorLocationCollection.removeTokenLocationFile(errorFile); } - TokenLocationFile* file = m_locationCollection.findTokenLocationFileByPath(filePath); + TokenLocationFile* file = m_locationCollection.findTokenLocationFileByPath(filePath.str()); if (!file) { continue; @@ -99,13 +99,13 @@ void Storage::clearFileData(const std::set& filePaths) } } -std::set Storage::getDependingFilePathsAndRemoveFileNodes(const std::set& filePaths) +std::set Storage::getDependingFilePathsAndRemoveFileNodes(const std::set& filePaths) { - std::set dependingFilePaths; + std::set dependingFilePaths; - for (const std::string& filePath : filePaths) + for (const FilePath& filePath : filePaths) { - SearchNode* searchNode = m_tokenIndex.getNode(filePath); + SearchNode* searchNode = m_tokenIndex.getNode(filePath.absoluteStr()); if (!searchNode || searchNode->getTokenIds().size() != 1) { continue; @@ -121,7 +121,7 @@ std::set Storage::getDependingFilePathsAndRemoveFileNodes(const std addDependingFilePathsAndRemoveFileNodesRecursive(fileNode, &dependingFilePaths); } - for (const std::string& path : filePaths) + for (const FilePath& path : filePaths) { dependingFilePaths.erase(path); } @@ -640,7 +640,7 @@ Id Storage::onFileParsed(const std::string& filePath) { log("file", filePath, ParseLocation()); - Node* fileNode = addNodeHierarchy(Node::NODE_FILE, std::vector(1, FileSystem::absoluteFilePath(filePath))); + Node* fileNode = addNodeHierarchy(Node::NODE_FILE, std::vector(1, FilePath(filePath).absoluteStr())); return fileNode->getId(); } @@ -648,8 +648,8 @@ Id Storage::onFileIncludeParsed(const ParseLocation& location, const std::string { log("include", includedPath, location); - Node* fileNode = addNodeHierarchy(Node::NODE_FILE, std::vector(1, FileSystem::absoluteFilePath(filePath))); - Node* includedFileNode = addNodeHierarchy(Node::NODE_FILE, std::vector(1, FileSystem::absoluteFilePath(includedPath))); + Node* fileNode = addNodeHierarchy(Node::NODE_FILE, std::vector(1, FilePath(filePath).absoluteStr())); + Node* includedFileNode = addNodeHierarchy(Node::NODE_FILE, std::vector(1, FilePath(includedPath).absoluteStr())); Edge* edge = m_graph.createEdge(Edge::EDGE_INCLUDE, fileNode, includedFileNode); addTokenLocation(edge, location); @@ -1257,9 +1257,9 @@ bool Storage::getQuerySearchResults(const std::string& query, const std::string& return true; } -void Storage::addDependingFilePathsAndRemoveFileNodesRecursive(Node* fileNode, std::set* filePaths) +void Storage::addDependingFilePathsAndRemoveFileNodesRecursive(Node* fileNode, std::set* filePaths) { - bool inserted = filePaths->insert(fileNode->getFullName()).second; + bool inserted = filePaths->insert(FilePath(fileNode->getFullName())).second; if (!inserted) { return; diff --git a/src/lib/data/Storage.h b/src/lib/data/Storage.h index adb97434..0736eabe 100644 --- a/src/lib/data/Storage.h +++ b/src/lib/data/Storage.h @@ -4,6 +4,8 @@ #include #include +#include "utility/file/FilePath.h" + #include "data/access/GraphAccess.h" #include "data/access/LocationAccess.h" #include "data/graph/StorageGraph.h" @@ -23,8 +25,8 @@ public: virtual ~Storage(); void clear(); - void clearFileData(const std::set& filePaths); - std::set getDependingFilePathsAndRemoveFileNodes(const std::set& filePaths); + void clearFileData(const std::set& filePaths); + std::set getDependingFilePathsAndRemoveFileNodes(const std::set& filePaths); void logGraph() const; void logLocations() const; @@ -148,7 +150,7 @@ private: bool getQuerySearchResults(const std::string& query, const std::string& word, SearchResults* results) const; - void addDependingFilePathsAndRemoveFileNodesRecursive(Node* fileNode, std::set* filePaths); + void addDependingFilePathsAndRemoveFileNodesRecursive(Node* fileNode, std::set* filePaths); void removeNodeIfUnreferenced(Node* node); void log(std::string type, std::string str, const ParseLocation& location) const; diff --git a/src/lib/data/location/TokenLocation.cpp b/src/lib/data/location/TokenLocation.cpp index bbe8c296..3c944ae9 100644 --- a/src/lib/data/location/TokenLocation.cpp +++ b/src/lib/data/location/TokenLocation.cpp @@ -84,7 +84,7 @@ unsigned int TokenLocation::getLineNumber() const return m_line->getLineNumber(); } -const std::string& TokenLocation::getFilePath() const +const FilePath& TokenLocation::getFilePath() const { return m_line->getFilePath(); } diff --git a/src/lib/data/location/TokenLocation.h b/src/lib/data/location/TokenLocation.h index 58f51164..cc68150f 100644 --- a/src/lib/data/location/TokenLocation.h +++ b/src/lib/data/location/TokenLocation.h @@ -5,6 +5,7 @@ #include #include +#include "utility/file/FilePath.h" #include "utility/types.h" class Token; @@ -36,7 +37,7 @@ public: unsigned int getColumnNumber() const; unsigned int getLineNumber() const; - const std::string& getFilePath() const; + const FilePath& getFilePath() const; TokenLocation* getOtherTokenLocation() const; void setOtherTokenLocation(TokenLocation* location); diff --git a/src/lib/data/location/TokenLocationCollection.cpp b/src/lib/data/location/TokenLocationCollection.cpp index e6194ec6..e9ab0a43 100644 --- a/src/lib/data/location/TokenLocationCollection.cpp +++ b/src/lib/data/location/TokenLocationCollection.cpp @@ -38,7 +38,7 @@ size_t TokenLocationCollection::getTokenLocationCount() const } TokenLocation* TokenLocationCollection::addTokenLocation( - Id tokenId, const std::string& filePath, + Id tokenId, const FilePath& filePath, unsigned int startLineNumber, unsigned int startColumnNumber, unsigned int endLineNumber, unsigned int endColumnNumber) { @@ -87,13 +87,13 @@ TokenLocation* TokenLocationCollection::findTokenLocationById(Id id) const return nullptr; } -TokenLocationFile* TokenLocationCollection::findTokenLocationFileByPath(const std::string& filePath) const +TokenLocationFile* TokenLocationCollection::findTokenLocationFileByPath(const FilePath& filePath) const { - std::map>::const_iterator it = + std::map>::const_iterator it = find_if(m_files.begin(), m_files.end(), - [&](const std::pair>& p) + [&](const std::pair>& p) { - return FileSystem::equivalent(p.first, filePath); + return p.first == filePath; } ); @@ -143,7 +143,7 @@ void TokenLocationCollection::removeTokenLocationFile(TokenLocationFile* file) TokenLocation* TokenLocationCollection::addTokenLocationAsPlainCopy(const TokenLocation* location) { - const std::string& filePath = location->getTokenLocationLine()->getTokenLocationFile()->getFilePath(); + const FilePath& filePath = location->getTokenLocationLine()->getTokenLocationFile()->getFilePath(); TokenLocationFile* file = createTokenLocationFile(filePath); TokenLocation* copy = file->addTokenLocationAsPlainCopy(location); @@ -157,7 +157,7 @@ void TokenLocationCollection::clear() m_files.clear(); } -TokenLocationFile* TokenLocationCollection::createTokenLocationFile(const std::string& filePath) +TokenLocationFile* TokenLocationCollection::createTokenLocationFile(const FilePath& filePath) { TokenLocationFile* file = findTokenLocationFileByPath(filePath); diff --git a/src/lib/data/location/TokenLocationCollection.h b/src/lib/data/location/TokenLocationCollection.h index c8f8081c..651807a9 100644 --- a/src/lib/data/location/TokenLocationCollection.h +++ b/src/lib/data/location/TokenLocationCollection.h @@ -7,6 +7,7 @@ #include #include +#include "utility/file/FilePath.h" #include "utility/types.h" class TokenLocation; @@ -16,8 +17,8 @@ class TokenLocationLine; class TokenLocationCollection { public: - typedef std::map > TokenLocationFileMapType; - typedef std::pair > TokenLocationFilePairType; + typedef std::map > TokenLocationFileMapType; + typedef std::pair > TokenLocationFilePairType; TokenLocationCollection(); ~TokenLocationCollection(); @@ -29,13 +30,13 @@ public: size_t getTokenLocationCount() const; TokenLocation* addTokenLocation( - Id tokenId, const std::string& filePath, + Id tokenId, const FilePath& filePath, unsigned int startLineNumber, unsigned int startColumnNumber, unsigned int endLineNumber, unsigned int endColumnNumber); void removeTokenLocation(TokenLocation* location); TokenLocation* findTokenLocationById(Id id) const; - TokenLocationFile* findTokenLocationFileByPath(const std::string& filePath) const; + TokenLocationFile* findTokenLocationFileByPath(const FilePath& filePath) const; void forEachTokenLocationFile(std::function func) const; void forEachTokenLocationLine(std::function func) const; @@ -48,7 +49,7 @@ public: void clear(); private: - TokenLocationFile* createTokenLocationFile(const std::string& filePath); + TokenLocationFile* createTokenLocationFile(const FilePath& filePath); TokenLocationFileMapType m_files; std::map m_locations; diff --git a/src/lib/data/location/TokenLocationFile.cpp b/src/lib/data/location/TokenLocationFile.cpp index ebe24d23..f7fb0f01 100644 --- a/src/lib/data/location/TokenLocationFile.cpp +++ b/src/lib/data/location/TokenLocationFile.cpp @@ -1,10 +1,11 @@ #include "data/location/TokenLocationFile.h" +#include "utility/logging/logging.h" + #include "data/location/TokenLocation.h" #include "data/location/TokenLocationLine.h" -#include "utility/logging/logging.h" -TokenLocationFile::TokenLocationFile(const std::string& filePath) +TokenLocationFile::TokenLocationFile(const FilePath& filePath) : m_filePath(filePath) { } @@ -23,7 +24,7 @@ size_t TokenLocationFile::getTokenLocationLineCount() const return m_lines.size(); } -const std::string& TokenLocationFile::getFilePath() const +const FilePath& TokenLocationFile::getFilePath() const { return m_filePath; } @@ -156,7 +157,7 @@ TokenLocationLine* TokenLocationFile::createTokenLocationLine(unsigned int lineN std::ostream& operator<<(std::ostream& ostream, const TokenLocationFile& file) { - ostream << "file \"" << file.getFilePath() << "\"\n"; + ostream << "file \"" << file.getFilePath().str() << "\"\n"; file.forEachTokenLocationLine([&ostream](TokenLocationLine* l) { ostream << *l << '\n'; diff --git a/src/lib/data/location/TokenLocationFile.h b/src/lib/data/location/TokenLocationFile.h index cb82e8ff..b2e8d177 100644 --- a/src/lib/data/location/TokenLocationFile.h +++ b/src/lib/data/location/TokenLocationFile.h @@ -7,6 +7,7 @@ #include #include +#include "utility/file/FilePath.h" #include "utility/types.h" class TokenLocation; @@ -15,16 +16,16 @@ class TokenLocationLine; class TokenLocationFile { public: - typedef std::map > TokenLocationLineMapType; - typedef std::pair > TokenLocationLinePairType; + typedef std::map> TokenLocationLineMapType; + typedef std::pair> TokenLocationLinePairType; - TokenLocationFile(const std::string& filePath); + TokenLocationFile(const FilePath& filePath); ~TokenLocationFile(); const TokenLocationLineMapType& getTokenLocationLines() const; size_t getTokenLocationLineCount() const; - const std::string& getFilePath() const; + const FilePath& getFilePath() const; TokenLocation* addTokenLocation( Id tokenId, @@ -44,7 +45,7 @@ private: TokenLocationLine* createTokenLocationLine(unsigned int lineNumber); std::map > m_lines; - std::string m_filePath; + FilePath m_filePath; }; std::ostream& operator<<(std::ostream& ostream, const TokenLocationFile& file); diff --git a/src/lib/data/location/TokenLocationLine.cpp b/src/lib/data/location/TokenLocationLine.cpp index f8b44dbd..bd9c748d 100644 --- a/src/lib/data/location/TokenLocationLine.cpp +++ b/src/lib/data/location/TokenLocationLine.cpp @@ -29,7 +29,7 @@ TokenLocationFile* TokenLocationLine::getTokenLocationFile() const return m_file; } -const std::string& TokenLocationLine::getFilePath() const +const FilePath& TokenLocationLine::getFilePath() const { return m_file->getFilePath(); } diff --git a/src/lib/data/location/TokenLocationLine.h b/src/lib/data/location/TokenLocationLine.h index a59f402f..65faa5c4 100644 --- a/src/lib/data/location/TokenLocationLine.h +++ b/src/lib/data/location/TokenLocationLine.h @@ -7,6 +7,7 @@ #include #include +#include "utility/file/FilePath.h" #include "utility/types.h" class TokenLocation; @@ -25,7 +26,7 @@ public: size_t getTokenLocationCount() const; TokenLocationFile* getTokenLocationFile() const; - const std::string& getFilePath() const; + const FilePath& getFilePath() const; unsigned int getLineNumber() const; diff --git a/src/lib/data/parser/Parser.h b/src/lib/data/parser/Parser.h index 78b30a10..4c72a46e 100644 --- a/src/lib/data/parser/Parser.h +++ b/src/lib/data/parser/Parser.h @@ -7,6 +7,7 @@ #include "data/parser/ParserClient.h" +class FilePath; class TextAccess; class Parser @@ -16,7 +17,7 @@ public: virtual ~Parser(); virtual void parseFiles( - const std::vector& filePaths, + const std::vector& filePaths, const std::vector& systemHeaderSearchPaths, const std::vector& headerSearchPaths) = 0; virtual void parseFile(std::shared_ptr textAccess) = 0; diff --git a/src/lib/data/parser/cxx/CxxParser.cpp b/src/lib/data/parser/cxx/CxxParser.cpp index 684fcf46..9a145160 100644 --- a/src/lib/data/parser/cxx/CxxParser.cpp +++ b/src/lib/data/parser/cxx/CxxParser.cpp @@ -59,7 +59,7 @@ CxxParser::~CxxParser() } void CxxParser::parseFiles( - const std::vector& filePaths, + const std::vector& filePaths, const std::vector& systemHeaderSearchPaths, const std::vector& headerSearchPaths ){ @@ -113,12 +113,18 @@ void CxxParser::parseFiles( FileRegister fileRegister(m_fileManager, filePaths); + std::vector sourcePaths; + for (const FilePath& path : fileRegister.getSourceFilePaths()) + { + sourcePaths.push_back(path.absoluteStr()); + } + llvm::IntrusiveRefCntPtr options = new clang::DiagnosticOptions(); CxxDiagnosticConsumer reporter(llvm::errs(), &*options, m_client); ASTActionFactory actionFactory(m_client, &fileRegister); - clang::tooling::ClangTool tool(*compilationDatabase, fileRegister.getSourceFilePaths()); + clang::tooling::ClangTool tool(*compilationDatabase, sourcePaths); tool.setDiagnosticConsumer(&reporter); tool.run(&actionFactory); } @@ -131,7 +137,7 @@ void CxxParser::parseFile(std::shared_ptr textAccess) llvm::IntrusiveRefCntPtr options = new clang::DiagnosticOptions(); CxxDiagnosticConsumer reporter(llvm::errs(), &*options, m_client, false); - FileRegister fileRegister(m_fileManager, std::vector()); + FileRegister fileRegister(m_fileManager, std::vector()); ASTActionFactory actionFactory(m_client, &fileRegister); runToolOnCodeWithArgs(&reporter, actionFactory.create(), textAccess->getText(), args); diff --git a/src/lib/data/parser/cxx/CxxParser.h b/src/lib/data/parser/cxx/CxxParser.h index edd06d15..c88582fb 100644 --- a/src/lib/data/parser/cxx/CxxParser.h +++ b/src/lib/data/parser/cxx/CxxParser.h @@ -11,7 +11,7 @@ public: ~CxxParser(); virtual void parseFiles( - const std::vector& filePaths, + const std::vector& filePaths, const std::vector& systemHeaderSearchPaths, const std::vector& headerSearchPaths); virtual void parseFile(std::shared_ptr textAccess); diff --git a/src/lib/utility/file/FileInfo.cpp b/src/lib/utility/file/FileInfo.cpp index 7a0d5ce6..a16ce432 100644 --- a/src/lib/utility/file/FileInfo.cpp +++ b/src/lib/utility/file/FileInfo.cpp @@ -1,6 +1,6 @@ #include "FileInfo.h" -FileInfo::FileInfo(std::string path, boost::posix_time::ptime lastWriteTime) +FileInfo::FileInfo(const FilePath& path, boost::posix_time::ptime lastWriteTime) : path(path) , lastWriteTime(lastWriteTime) { diff --git a/src/lib/utility/file/FileInfo.h b/src/lib/utility/file/FileInfo.h index 1dd06f21..7ac31ac9 100644 --- a/src/lib/utility/file/FileInfo.h +++ b/src/lib/utility/file/FileInfo.h @@ -2,13 +2,16 @@ #define FILE_INFO_H #include + #include "boost/date_time.hpp" +#include "utility/file/FilePath.h" + struct FileInfo { - FileInfo(std::string path, boost::posix_time::ptime lastWriteTime); + FileInfo(const FilePath& path, boost::posix_time::ptime lastWriteTime); - std::string path; + FilePath path; boost::posix_time::ptime lastWriteTime; }; diff --git a/src/lib/utility/file/FileManager.cpp b/src/lib/utility/file/FileManager.cpp index f58917df..401f575c 100644 --- a/src/lib/utility/file/FileManager.cpp +++ b/src/lib/utility/file/FileManager.cpp @@ -36,7 +36,7 @@ void FileManager::fetchFilePaths() m_updatedFiles.clear(); m_removedFiles.clear(); - for (std::map::iterator it = m_files.begin(); it != m_files.end(); it++) + for (std::map::iterator it = m_files.begin(); it != m_files.end(); it++) { m_removedFiles.insert(it->first); } @@ -52,8 +52,8 @@ void FileManager::fetchFilePaths() for (FileInfo fileInfo: fileInfos) { - const std::string& filePath = fileInfo.path; - std::map::iterator it = m_files.find(filePath); + const FilePath& filePath = fileInfo.path; + std::map::iterator it = m_files.find(filePath); if (it != m_files.end()) { m_removedFiles.erase(filePath); @@ -65,44 +65,44 @@ void FileManager::fetchFilePaths() } else { - m_files.insert(std::pair(filePath, fileInfo)); + m_files.insert(std::pair(filePath, fileInfo)); m_addedFiles.insert(filePath); } } } - for (const std::string filePath : m_removedFiles) + for (const FilePath& filePath : m_removedFiles) { m_files.erase(filePath); } } -std::set FileManager::getAddedFilePaths() const +std::set FileManager::getAddedFilePaths() const { return m_addedFiles; } -std::set FileManager::getUpdatedFilePaths() const +std::set FileManager::getUpdatedFilePaths() const { return m_updatedFiles; } -std::set FileManager::getRemovedFilePaths() const +std::set FileManager::getRemovedFilePaths() const { return m_removedFiles; } -bool FileManager::hasFilePath(const std::string& filePath) const +bool FileManager::hasFilePath(const FilePath& filePath) const { - return (m_files.find(FileSystem::absoluteFilePath(filePath)) != m_files.end()); + return (m_files.find(filePath) != m_files.end()); } -bool FileManager::hasSourceExtension(const std::string& filePath) const +bool FileManager::hasSourceExtension(const FilePath& filePath) const { - return FileSystem::hasExtension(filePath, m_sourceExtensions); + return filePath.hasExtension(m_sourceExtensions); } -bool FileManager::hasIncludeExtension(const std::string& filePath) const +bool FileManager::hasIncludeExtension(const FilePath& filePath) const { - return FileSystem::hasExtension(filePath, m_includeExtensions); + return filePath.hasExtension(m_includeExtensions); } diff --git a/src/lib/utility/file/FileManager.h b/src/lib/utility/file/FileManager.h index 9cf9da3c..9196b07a 100644 --- a/src/lib/utility/file/FileManager.h +++ b/src/lib/utility/file/FileManager.h @@ -20,13 +20,13 @@ public: void reset(); void fetchFilePaths(); - std::set getAddedFilePaths() const; - std::set getUpdatedFilePaths() const; - std::set getRemovedFilePaths() const; + std::set getAddedFilePaths() const; + std::set getUpdatedFilePaths() const; + std::set getRemovedFilePaths() const; - virtual bool hasFilePath(const std::string& filePath) const; - virtual bool hasSourceExtension(const std::string& filePath) const; - virtual bool hasIncludeExtension(const std::string& filePath) const; + virtual bool hasFilePath(const FilePath& filePath) const; + virtual bool hasSourceExtension(const FilePath& filePath) const; + virtual bool hasIncludeExtension(const FilePath& filePath) const; private: std::vector m_sourcePaths; @@ -34,10 +34,10 @@ private: std::vector m_sourceExtensions; std::vector m_includeExtensions; - std::map m_files; - std::set m_addedFiles; - std::set m_updatedFiles; - std::set m_removedFiles; + std::map m_files; + std::set m_addedFiles; + std::set m_updatedFiles; + std::set m_removedFiles; }; #endif // FILE_MANAGER_H diff --git a/src/lib/utility/file/FilePath.cpp b/src/lib/utility/file/FilePath.cpp new file mode 100644 index 00000000..240b1bf2 --- /dev/null +++ b/src/lib/utility/file/FilePath.cpp @@ -0,0 +1,79 @@ +#include "utility/file/FilePath.h" + +FilePath::FilePath(const char* filePath) + : m_path(filePath) +{ +} + +FilePath::FilePath(const std::string& filePath) + : m_path(filePath) +{ +} + +FilePath::FilePath(const boost::filesystem::path& filePath) + : m_path(filePath) +{ +} + +bool FilePath::exists() const +{ + return boost::filesystem::exists(m_path); +} + +std::string FilePath::str() const +{ + return m_path.generic_string(); +} + +std::string FilePath::absoluteStr() const +{ + return boost::filesystem::absolute(m_path).generic_string(); +} + +std::string FilePath::fileName() const +{ + return m_path.filename().generic_string(); +} + +std::string FilePath::extension() const +{ + return m_path.extension().generic_string(); +} + +FilePath FilePath::withoutExtension() const +{ + return FilePath(boost::filesystem::path(m_path).replace_extension()); +} + +bool FilePath::hasExtension(const std::vector& extensions) const +{ + std::string e = extension(); + for (std::string ext : extensions) + { + if (e == ext) + { + return true; + } + } + return false; +} + +bool FilePath::operator==(const FilePath& other) const +{ + if (exists() && other.exists()) + { + return boost::filesystem::equivalent(m_path, other.m_path); + } + + return m_path.compare(other.m_path) == 0; +} + +bool FilePath::operator!=(const FilePath& other) const +{ + return !(*this == other); +} + +bool FilePath::operator<(const FilePath& other) const +{ + return m_path.compare(other.m_path) < 0; +} diff --git a/src/lib/utility/file/FilePath.h b/src/lib/utility/file/FilePath.h new file mode 100644 index 00000000..1a38737c --- /dev/null +++ b/src/lib/utility/file/FilePath.h @@ -0,0 +1,33 @@ +#ifndef FILE_PATH_H +#define FILE_PATH_H + +#include + +#include "boost/filesystem.hpp" + +class FilePath +{ +public: + FilePath(const char* filePath); + FilePath(const std::string& filePath); + FilePath(const boost::filesystem::path& filePath); + + bool exists() const; + + std::string str() const; + std::string absoluteStr() const; + std::string fileName() const; + + std::string extension() const; + FilePath withoutExtension() const; + bool hasExtension(const std::vector& extensions) const; + + bool operator==(const FilePath& other) const; + bool operator!=(const FilePath& other) const; + bool operator<(const FilePath& other) const; + +private: + boost::filesystem::path m_path; +}; + +#endif // FILE_PATH_H diff --git a/src/lib/utility/file/FileRegister.cpp b/src/lib/utility/file/FileRegister.cpp index 0cc79cbf..b759f1cb 100644 --- a/src/lib/utility/file/FileRegister.cpp +++ b/src/lib/utility/file/FileRegister.cpp @@ -3,10 +3,10 @@ #include "utility/file/FileManager.h" #include "utility/file/FileSystem.h" -FileRegister::FileRegister(const FileManager* fileManager, const std::vector& filePaths) +FileRegister::FileRegister(const FileManager* fileManager, const std::vector& filePaths) : m_fileManager(fileManager) { - for (const std::string& path : filePaths) + for (const FilePath& path : filePaths) { if (m_fileManager->hasSourceExtension(path)) { @@ -24,14 +24,14 @@ const FileManager* FileRegister::getFileManager() const return m_fileManager; } -const std::vector& FileRegister::getSourceFilePaths() const +const std::vector& FileRegister::getSourceFilePaths() const { return m_sourceFilePaths; } bool FileRegister::includeFileIsParsing(const std::string& filePath) const { - std::map::const_iterator it = m_includeFilePaths.find(FileSystem::absoluteFilePath(filePath)); + std::map::const_iterator it = m_includeFilePaths.find(FilePath(filePath)); if (it == m_includeFilePaths.end()) { return false; @@ -42,7 +42,7 @@ bool FileRegister::includeFileIsParsing(const std::string& filePath) const void FileRegister::markIncludeFileParsing(const std::string& filePath) { - std::map::iterator it = m_includeFilePaths.find(FileSystem::absoluteFilePath(filePath)); + std::map::iterator it = m_includeFilePaths.find(FilePath(filePath)); if (it == m_includeFilePaths.end()) { return; @@ -56,7 +56,7 @@ void FileRegister::markIncludeFileParsing(const std::string& filePath) void FileRegister::markParsingIncludeFilesParsed() { - for (std::pair&& p : m_includeFilePaths) + for (std::pair&& p : m_includeFilePaths) { if (p.second == STATE_PARSING) { diff --git a/src/lib/utility/file/FileRegister.h b/src/lib/utility/file/FileRegister.h index 723f7471..7429004f 100644 --- a/src/lib/utility/file/FileRegister.h +++ b/src/lib/utility/file/FileRegister.h @@ -5,16 +5,18 @@ #include #include +#include "utility/file/FilePath.h" + class FileManager; class FileRegister { public: - FileRegister(const FileManager* fileManager, const std::vector& filePaths); + FileRegister(const FileManager* fileManager, const std::vector& filePaths); const FileManager* getFileManager() const; - const std::vector& getSourceFilePaths() const; + const std::vector& getSourceFilePaths() const; bool includeFileIsParsing(const std::string& filePath) const; @@ -31,8 +33,8 @@ private: const FileManager* m_fileManager; - std::vector m_sourceFilePaths; - std::map m_includeFilePaths; + std::vector m_sourceFilePaths; + std::map m_includeFilePaths; }; #endif // FILE_REGISTER_H diff --git a/src/lib/utility/file/FileSystem.cpp b/src/lib/utility/file/FileSystem.cpp index 2f9e01e7..4aa90e3e 100644 --- a/src/lib/utility/file/FileSystem.cpp +++ b/src/lib/utility/file/FileSystem.cpp @@ -69,7 +69,7 @@ std::vector FileSystem::getFileInfosFromDirectoryPaths( { std::time_t t = boost::filesystem::last_write_time(*it); boost::posix_time::ptime lastWriteTime = boost::posix_time::from_time_t(t); - files.push_back(FileInfo(absoluteFilePath(it->path().generic_string()), lastWriteTime)); + files.push_back(FileInfo(it->path(), lastWriteTime)); } ++it; } diff --git a/src/test/CMakeLists.txt b/src/test/CMakeLists.txt index dc28570e..cad2de90 100644 --- a/src/test/CMakeLists.txt +++ b/src/test/CMakeLists.txt @@ -5,7 +5,7 @@ add_files( helper/TestFileManager.h helper/TestStorage.cpp helper/TestStorage.h - + TestSuiteFixture.cpp TestSuiteFixture.h @@ -14,6 +14,7 @@ add_files( DataTypeTestSuite.h DictionaryTestSuite.h FileManagerTestSuite.h + FilePathTestSuite.h FileSystemTestSuite.h GraphTestSuite.h GraphFilterTestSuite.h diff --git a/src/test/CxxParserTestSuite.h b/src/test/CxxParserTestSuite.h index 683d7e0b..d444376f 100644 --- a/src/test/CxxParserTestSuite.h +++ b/src/test/CxxParserTestSuite.h @@ -1687,9 +1687,9 @@ public: TestParserClient client; CxxParser parser(&client, &fm); - std::vector filePaths; - filePaths.push_back("data/CxxParserTestSuite/header.h"); - filePaths.push_back("data/CxxParserTestSuite/code.cpp"); + std::vector filePaths; + filePaths.push_back(FilePath("data/CxxParserTestSuite/header.h")); + filePaths.push_back(FilePath("data/CxxParserTestSuite/code.cpp")); parser.parseFiles(filePaths, std::vector(), std::vector()); TS_ASSERT_EQUALS(client.errors.size(), 0); diff --git a/src/test/FilePathTestSuite.h b/src/test/FilePathTestSuite.h new file mode 100644 index 00000000..fc91f49c --- /dev/null +++ b/src/test/FilePathTestSuite.h @@ -0,0 +1,127 @@ +#include "cxxtest/TestSuite.h" + +#include "utility/file/FilePath.h" + +class FilePathTestSuite : public CxxTest::TestSuite +{ +public: + void test_file_path_gets_created_with_char_array() + { + FilePath path("data/FilePathTestSuite/main.cpp"); + + TS_ASSERT_EQUALS(path.str(), "data/FilePathTestSuite/main.cpp"); + } + + void test_file_path_gets_created_with_string() + { + std::string str("data/FilePathTestSuite/main.cpp"); + FilePath path(str); + + TS_ASSERT_EQUALS(path.str(), str); + } + + void test_file_path_gets_created_other_file_path() + { + FilePath path("data/FilePathTestSuite/main.cpp"); + FilePath path2(path); + + TS_ASSERT_EQUALS(path, path2); + } + + void test_file_path_exists() + { + FilePath path("data/FilePathTestSuite/a.cpp"); + + TS_ASSERT(path.exists()); + } + + void test_file_path_not_exists() + { + FilePath path("data/FilePathTestSuite/a.h"); + + TS_ASSERT(!path.exists()); + } + + void test_file_path_file_name() + { + FilePath path("data/FilePathTestSuite/abc.h"); + + TS_ASSERT_EQUALS(path.fileName(), "abc.h"); + } + + void test_file_path_extension() + { + FilePath path("data/FilePathTestSuite/a.h"); + + TS_ASSERT_EQUALS(path.extension(), ".h"); + } + + void test_file_path_without_extension() + { + FilePath path("data/FilePathTestSuite/a.h"); + + TS_ASSERT_EQUALS(path.withoutExtension(), "data/FilePathTestSuite/a"); + } + + void test_file_path_has_extension() + { + std::vector extensions; + extensions.push_back(".h"); + extensions.push_back(".cpp"); + extensions.push_back(".cc"); + + TS_ASSERT(FilePath("data/FilePathTestSuite/a.h").hasExtension(extensions)); + TS_ASSERT(FilePath("data/FilePathTestSuite/b.cpp").hasExtension(extensions)); + TS_ASSERT(!FilePath("data/FilePathTestSuite/a.m").hasExtension(extensions)); + } + + void test_file_path_equals_file_with_different_relative_paths() + { + FilePath pathA("data/FilePathTestSuite/a.cpp"); + FilePath pathA2("data/../data/FilePathTestSuite/./a.cpp"); + + TS_ASSERT_EQUALS(pathA, pathA2); + } + + void test_file_path_equals_relative_and_absolute_paths() + { + FilePath pathA("data/FilePathTestSuite/a.cpp"); + FilePath pathA2(pathA.absoluteStr()); + + TS_ASSERT_EQUALS(pathA, pathA2); + } + + void test_file_path_compares_paths_with_posix_and_windows_format() + { +#ifdef _WIN32 + FilePath pathB("data/FilePathTestSuite/b.cc"); + FilePath pathB2("data\\FilePathTestSuite\\b.cc"); + + TS_ASSERT_EQUALS(pathB, pathB2); +#endif + } + + void test_file_path_differs_for_different_existing_files() + { + FilePath pathA("data/FilePathTestSuite/a.cpp"); + FilePath pathB("data/FilePathTestSuite/b.cc"); + + TS_ASSERT_DIFFERS(pathA, pathB); + } + + void test_file_path_differs_for_different_nonexisting_files() + { + FilePath pathA("data/FilePathTestSuite/a.h"); + FilePath pathB("data/FilePathTestSuite/b.c"); + + TS_ASSERT_DIFFERS(pathA, pathB); + } + + void test_file_path_differs_for_existing_and_nonexisting_files() + { + FilePath pathA("data/FilePathTestSuite/a.h"); + FilePath pathB("data/FilePathTestSuite/b.cc"); + + TS_ASSERT_DIFFERS(pathA, pathB); + } +}; diff --git a/src/test/StorageTestSuite.h b/src/test/StorageTestSuite.h index b597cc81..17ca10c6 100644 --- a/src/test/StorageTestSuite.h +++ b/src/test/StorageTestSuite.h @@ -573,8 +573,8 @@ public: TS_ASSERT_EQUALS(storage.tokenLocationCollection().getTokenLocations().size(), 4); TS_ASSERT_EQUALS(storage.searchIndex().getNodeCount(), 3); - std::set files; - files.insert(m_filePath); + std::set files; + files.insert(FilePath(m_filePath)); storage.clearFileData(files); TS_ASSERT_EQUALS(storage.graph().getNodeCount(), 0); @@ -604,8 +604,8 @@ public: TS_ASSERT_EQUALS(storage.tokenLocationCollection().getTokenLocations().size(), 9); TS_ASSERT_EQUALS(storage.searchIndex().getNodeCount(), 6); - std::set files; - files.insert("file.cpp"); + std::set files; + files.insert(FilePath("file.cpp")); storage.clearFileData(files); TS_ASSERT_EQUALS(storage.graph().getNodeCount(), 3); @@ -635,8 +635,8 @@ public: TS_ASSERT_EQUALS(storage.tokenLocationCollection().getTokenLocations().size(), 9); TS_ASSERT_EQUALS(storage.searchIndex().getNodeCount(), 5); - std::set files; - files.insert("file.h"); + std::set files; + files.insert(FilePath("file.h")); storage.clearFileData(files); TS_ASSERT_EQUALS(storage.graph().getNodeCount(), 4); @@ -666,9 +666,9 @@ public: TS_ASSERT_EQUALS(storage.tokenLocationCollection().getTokenLocations().size(), 9); TS_ASSERT_EQUALS(storage.searchIndex().getNodeCount(), 5); - std::set filePaths; - filePaths.insert("file.cpp"); - filePaths.insert("file.h"); + std::set filePaths; + filePaths.insert(FilePath("file.cpp")); + filePaths.insert(FilePath("file.h")); storage.clearFileData(filePaths); TS_ASSERT_EQUALS(storage.graph().getNodeCount(), 0); @@ -722,12 +722,12 @@ public: std::string name1 = storage.getNodeWithId(id2)->getFullName(); std::string name2 = storage.getNodeWithId(id3)->getFullName(); - std::set filePaths; - filePaths.insert(name1); - std::set dependingFilePaths = storage.getDependingFilePathsAndRemoveFileNodes(filePaths); + std::set filePaths; + filePaths.insert(FilePath(name1)); + std::set dependingFilePaths = storage.getDependingFilePathsAndRemoveFileNodes(filePaths); TS_ASSERT_EQUALS(dependingFilePaths.size(), 1); - TS_ASSERT_EQUALS(*dependingFilePaths.begin(), name2); + TS_ASSERT_EQUALS(dependingFilePaths.begin()->str(), name2); TS_ASSERT(storage.getNodeWithId(id1)); TS_ASSERT(!storage.getNodeWithId(id2)); diff --git a/src/test/TokenLocationCollectionTestSuite.h b/src/test/TokenLocationCollectionTestSuite.h index f682bea8..763b3a0f 100644 --- a/src/test/TokenLocationCollectionTestSuite.h +++ b/src/test/TokenLocationCollectionTestSuite.h @@ -69,7 +69,7 @@ public: TS_ASSERT_EQUALS(3, a->getColumnNumber()); TS_ASSERT_EQUALS(4, a->getOtherTokenLocation()->getLineNumber()); TS_ASSERT_EQUALS(5, a->getOtherTokenLocation()->getColumnNumber()); - TS_ASSERT_EQUALS("file.c", a->getFilePath()); + TS_ASSERT_EQUALS("file.c", a->getFilePath().str()); } void test_finding_token_locations_by_id() diff --git a/src/test/helper/TestFileManager.cpp b/src/test/helper/TestFileManager.cpp index 1ff7d9e4..4286c120 100644 --- a/src/test/helper/TestFileManager.cpp +++ b/src/test/helper/TestFileManager.cpp @@ -10,17 +10,17 @@ TestFileManager::TestFileManager() { } -bool TestFileManager::hasFilePath(const std::string& filePath) const +bool TestFileManager::hasFilePath(const FilePath& filePath) const { return true; } -bool TestFileManager::hasSourceExtension(const std::string& filePath) const +bool TestFileManager::hasSourceExtension(const FilePath& filePath) const { return true; } -bool TestFileManager::hasIncludeExtension(const std::string& filePath) const +bool TestFileManager::hasIncludeExtension(const FilePath& filePath) const { return true; } diff --git a/src/test/helper/TestFileManager.h b/src/test/helper/TestFileManager.h index 02f9fc18..c37cb2e1 100644 --- a/src/test/helper/TestFileManager.h +++ b/src/test/helper/TestFileManager.h @@ -8,9 +8,9 @@ class TestFileManager: public FileManager public: TestFileManager(); - virtual bool hasFilePath(const std::string& filePath) const; - virtual bool hasSourceExtension(const std::string& filePath) const; - virtual bool hasIncludeExtension(const std::string& filePath) const; + virtual bool hasFilePath(const FilePath& filePath) const; + virtual bool hasSourceExtension(const FilePath& filePath) const; + virtual bool hasIncludeExtension(const FilePath& filePath) const; }; #endif // TEST_FILE_MANAGER_H