utility: added FilePath abstraction and rewrote data management to use it

The utility class FilePath wraps an instance of boost::filesystem::path. Using FilePath allows for easy comparision of
file paths on different platforms using absolute or relative paths. Whereever file paths are compared, this wrapper
should be used.
This commit is contained in:
Eberhard Graether
2015-03-09 01:51:32 +01:00
parent 7014c8ac4c
commit df888e4151
37 changed files with 387 additions and 126 deletions
+2 -2
View File
@@ -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<QtCodeFile> filePtr : m_files)
@@ -53,7 +53,7 @@ void QtCodeFileList::addCodeSnippet(
if (!file)
{
std::shared_ptr<QtCodeFile> filePtr = std::make_shared<QtCodeFile>(locationFile.getFilePath(), this);
std::shared_ptr<QtCodeFile> filePtr = std::make_shared<QtCodeFile>(locationFile.getFilePath().absoluteStr(), this);
m_files.push_back(filePtr);
file = filePtr.get();
+1 -1
View File
@@ -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;
+2
View File
@@ -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
+5 -5
View File
@@ -103,11 +103,11 @@ void Project::parseCode()
}
m_fileManager->fetchFilePaths();
std::set<std::string> addedFilePaths = m_fileManager->getAddedFilePaths();
std::set<std::string> updatedFilePaths = m_fileManager->getUpdatedFilePaths();
std::set<std::string> removedFilePaths = m_fileManager->getRemovedFilePaths();
std::set<FilePath> addedFilePaths = m_fileManager->getAddedFilePaths();
std::set<FilePath> updatedFilePaths = m_fileManager->getUpdatedFilePaths();
std::set<FilePath> removedFilePaths = m_fileManager->getRemovedFilePaths();
std::set<std::string> dependingFilePaths;
std::set<FilePath> 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<std::string> filesToParse;
std::vector<FilePath> filesToParse;
filesToParse.insert(filesToParse.end(), addedFilePaths.begin(), addedFilePaths.end());
filesToParse.insert(filesToParse.end(), updatedFilePaths.begin(), updatedFilePaths.end());
@@ -108,7 +108,7 @@ std::vector<CodeView::CodeSnippetParams> 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<CodeView::CodeSnippetParams> CodeController::getSnippetsForActiveTok
std::vector<CodeView::CodeSnippetParams> CodeController::getSnippetsForFile(const TokenLocationFile* file) const
{
std::shared_ptr<TextAccess> textAccess = TextAccess::createFromFile(file->getFilePath());
std::shared_ptr<TextAccess> textAccess = TextAccess::createFromFile(file->getFilePath().str());
std::vector<std::pair<uint, uint>> ranges = getSnippetRangesForFile(file);
std::vector<CodeView::CodeSnippetParams> snippets;
+7 -7
View File
@@ -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();
}
}
+14 -14
View File
@@ -42,17 +42,17 @@ void Storage::clear()
m_errorLocationCollection.clear();
}
void Storage::clearFileData(const std::set<std::string>& filePaths)
void Storage::clearFileData(const std::set<FilePath>& 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<std::string>& filePaths)
}
}
std::set<std::string> Storage::getDependingFilePathsAndRemoveFileNodes(const std::set<std::string>& filePaths)
std::set<FilePath> Storage::getDependingFilePathsAndRemoveFileNodes(const std::set<FilePath>& filePaths)
{
std::set<std::string> dependingFilePaths;
std::set<FilePath> 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<std::string> 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<std::string>(1, FileSystem::absoluteFilePath(filePath)));
Node* fileNode = addNodeHierarchy(Node::NODE_FILE, std::vector<std::string>(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<std::string>(1, FileSystem::absoluteFilePath(filePath)));
Node* includedFileNode = addNodeHierarchy(Node::NODE_FILE, std::vector<std::string>(1, FileSystem::absoluteFilePath(includedPath)));
Node* fileNode = addNodeHierarchy(Node::NODE_FILE, std::vector<std::string>(1, FilePath(filePath).absoluteStr()));
Node* includedFileNode = addNodeHierarchy(Node::NODE_FILE, std::vector<std::string>(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<std::string>* filePaths)
void Storage::addDependingFilePathsAndRemoveFileNodesRecursive(Node* fileNode, std::set<FilePath>* filePaths)
{
bool inserted = filePaths->insert(fileNode->getFullName()).second;
bool inserted = filePaths->insert(FilePath(fileNode->getFullName())).second;
if (!inserted)
{
return;
+5 -3
View File
@@ -4,6 +4,8 @@
#include <memory>
#include <vector>
#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<std::string>& filePaths);
std::set<std::string> getDependingFilePathsAndRemoveFileNodes(const std::set<std::string>& filePaths);
void clearFileData(const std::set<FilePath>& filePaths);
std::set<FilePath> getDependingFilePathsAndRemoveFileNodes(const std::set<FilePath>& 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<std::string>* filePaths);
void addDependingFilePathsAndRemoveFileNodesRecursive(Node* fileNode, std::set<FilePath>* filePaths);
void removeNodeIfUnreferenced(Node* node);
void log(std::string type, std::string str, const ParseLocation& location) const;
+1 -1
View File
@@ -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();
}
+2 -1
View File
@@ -5,6 +5,7 @@
#include <ostream>
#include <string>
#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);
@@ -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<std::string, std::shared_ptr<TokenLocationFile>>::const_iterator it =
std::map<FilePath, std::shared_ptr<TokenLocationFile>>::const_iterator it =
find_if(m_files.begin(), m_files.end(),
[&](const std::pair<std::string, std::shared_ptr<TokenLocationFile>>& p)
[&](const std::pair<FilePath, std::shared_ptr<TokenLocationFile>>& 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);
@@ -7,6 +7,7 @@
#include <ostream>
#include <string>
#include "utility/file/FilePath.h"
#include "utility/types.h"
class TokenLocation;
@@ -16,8 +17,8 @@ class TokenLocationLine;
class TokenLocationCollection
{
public:
typedef std::map<std::string, std::shared_ptr<TokenLocationFile> > TokenLocationFileMapType;
typedef std::pair<std::string, std::shared_ptr<TokenLocationFile> > TokenLocationFilePairType;
typedef std::map<FilePath, std::shared_ptr<TokenLocationFile> > TokenLocationFileMapType;
typedef std::pair<FilePath, std::shared_ptr<TokenLocationFile> > 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<void(TokenLocationFile*)> func) const;
void forEachTokenLocationLine(std::function<void(TokenLocationLine*)> 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<Id, TokenLocation*> m_locations;
+5 -4
View File
@@ -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';
+6 -5
View File
@@ -7,6 +7,7 @@
#include <ostream>
#include <string>
#include "utility/file/FilePath.h"
#include "utility/types.h"
class TokenLocation;
@@ -15,16 +16,16 @@ class TokenLocationLine;
class TokenLocationFile
{
public:
typedef std::map<unsigned int, std::shared_ptr<TokenLocationLine> > TokenLocationLineMapType;
typedef std::pair<unsigned int, std::shared_ptr<TokenLocationLine> > TokenLocationLinePairType;
typedef std::map<unsigned int, std::shared_ptr<TokenLocationLine>> TokenLocationLineMapType;
typedef std::pair<unsigned int, std::shared_ptr<TokenLocationLine>> 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<unsigned int, std::shared_ptr<TokenLocationLine> > m_lines;
std::string m_filePath;
FilePath m_filePath;
};
std::ostream& operator<<(std::ostream& ostream, const TokenLocationFile& file);
+1 -1
View File
@@ -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();
}
+2 -1
View File
@@ -7,6 +7,7 @@
#include <ostream>
#include <string>
#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;
+2 -1
View File
@@ -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<std::string>& filePaths,
const std::vector<FilePath>& filePaths,
const std::vector<std::string>& systemHeaderSearchPaths,
const std::vector<std::string>& headerSearchPaths) = 0;
virtual void parseFile(std::shared_ptr<TextAccess> textAccess) = 0;
+9 -3
View File
@@ -59,7 +59,7 @@ CxxParser::~CxxParser()
}
void CxxParser::parseFiles(
const std::vector<std::string>& filePaths,
const std::vector<FilePath>& filePaths,
const std::vector<std::string>& systemHeaderSearchPaths,
const std::vector<std::string>& headerSearchPaths
){
@@ -113,12 +113,18 @@ void CxxParser::parseFiles(
FileRegister fileRegister(m_fileManager, filePaths);
std::vector<std::string> sourcePaths;
for (const FilePath& path : fileRegister.getSourceFilePaths())
{
sourcePaths.push_back(path.absoluteStr());
}
llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> 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> textAccess)
llvm::IntrusiveRefCntPtr<clang::DiagnosticOptions> options = new clang::DiagnosticOptions();
CxxDiagnosticConsumer reporter(llvm::errs(), &*options, m_client, false);
FileRegister fileRegister(m_fileManager, std::vector<std::string>());
FileRegister fileRegister(m_fileManager, std::vector<FilePath>());
ASTActionFactory actionFactory(m_client, &fileRegister);
runToolOnCodeWithArgs(&reporter, actionFactory.create(), textAccess->getText(), args);
+1 -1
View File
@@ -11,7 +11,7 @@ public:
~CxxParser();
virtual void parseFiles(
const std::vector<std::string>& filePaths,
const std::vector<FilePath>& filePaths,
const std::vector<std::string>& systemHeaderSearchPaths,
const std::vector<std::string>& headerSearchPaths);
virtual void parseFile(std::shared_ptr<TextAccess> textAccess);
+1 -1
View File
@@ -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)
{
+5 -2
View File
@@ -2,13 +2,16 @@
#define FILE_INFO_H
#include <string>
#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;
};
+14 -14
View File
@@ -36,7 +36,7 @@ void FileManager::fetchFilePaths()
m_updatedFiles.clear();
m_removedFiles.clear();
for (std::map<std::string, FileInfo>::iterator it = m_files.begin(); it != m_files.end(); it++)
for (std::map<FilePath, FileInfo>::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<std::string, FileInfo>::iterator it = m_files.find(filePath);
const FilePath& filePath = fileInfo.path;
std::map<FilePath, FileInfo>::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<std::string, FileInfo>(filePath, fileInfo));
m_files.insert(std::pair<FilePath, FileInfo>(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<std::string> FileManager::getAddedFilePaths() const
std::set<FilePath> FileManager::getAddedFilePaths() const
{
return m_addedFiles;
}
std::set<std::string> FileManager::getUpdatedFilePaths() const
std::set<FilePath> FileManager::getUpdatedFilePaths() const
{
return m_updatedFiles;
}
std::set<std::string> FileManager::getRemovedFilePaths() const
std::set<FilePath> 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);
}
+10 -10
View File
@@ -20,13 +20,13 @@ public:
void reset();
void fetchFilePaths();
std::set<std::string> getAddedFilePaths() const;
std::set<std::string> getUpdatedFilePaths() const;
std::set<std::string> getRemovedFilePaths() const;
std::set<FilePath> getAddedFilePaths() const;
std::set<FilePath> getUpdatedFilePaths() const;
std::set<FilePath> 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<std::string> m_sourcePaths;
@@ -34,10 +34,10 @@ private:
std::vector<std::string> m_sourceExtensions;
std::vector<std::string> m_includeExtensions;
std::map<std::string, FileInfo> m_files;
std::set<std::string> m_addedFiles;
std::set<std::string> m_updatedFiles;
std::set<std::string> m_removedFiles;
std::map<FilePath, FileInfo> m_files;
std::set<FilePath> m_addedFiles;
std::set<FilePath> m_updatedFiles;
std::set<FilePath> m_removedFiles;
};
#endif // FILE_MANAGER_H
+79
View File
@@ -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<std::string>& 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;
}
+33
View File
@@ -0,0 +1,33 @@
#ifndef FILE_PATH_H
#define FILE_PATH_H
#include <string>
#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<std::string>& 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
+6 -6
View File
@@ -3,10 +3,10 @@
#include "utility/file/FileManager.h"
#include "utility/file/FileSystem.h"
FileRegister::FileRegister(const FileManager* fileManager, const std::vector<std::string>& filePaths)
FileRegister::FileRegister(const FileManager* fileManager, const std::vector<FilePath>& 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<std::string>& FileRegister::getSourceFilePaths() const
const std::vector<FilePath>& FileRegister::getSourceFilePaths() const
{
return m_sourceFilePaths;
}
bool FileRegister::includeFileIsParsing(const std::string& filePath) const
{
std::map<std::string, ParseState>::const_iterator it = m_includeFilePaths.find(FileSystem::absoluteFilePath(filePath));
std::map<FilePath, ParseState>::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<std::string, ParseState>::iterator it = m_includeFilePaths.find(FileSystem::absoluteFilePath(filePath));
std::map<FilePath, ParseState>::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<std::string, ParseState>&& p : m_includeFilePaths)
for (std::pair<FilePath, ParseState>&& p : m_includeFilePaths)
{
if (p.second == STATE_PARSING)
{
+6 -4
View File
@@ -5,16 +5,18 @@
#include <string>
#include <vector>
#include "utility/file/FilePath.h"
class FileManager;
class FileRegister
{
public:
FileRegister(const FileManager* fileManager, const std::vector<std::string>& filePaths);
FileRegister(const FileManager* fileManager, const std::vector<FilePath>& filePaths);
const FileManager* getFileManager() const;
const std::vector<std::string>& getSourceFilePaths() const;
const std::vector<FilePath>& getSourceFilePaths() const;
bool includeFileIsParsing(const std::string& filePath) const;
@@ -31,8 +33,8 @@ private:
const FileManager* m_fileManager;
std::vector<std::string> m_sourceFilePaths;
std::map<std::string, ParseState> m_includeFilePaths;
std::vector<FilePath> m_sourceFilePaths;
std::map<FilePath, ParseState> m_includeFilePaths;
};
#endif // FILE_REGISTER_H
+1 -1
View File
@@ -69,7 +69,7 @@ std::vector<FileInfo> 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;
}
+2 -1
View File
@@ -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
+3 -3
View File
@@ -1687,9 +1687,9 @@ public:
TestParserClient client;
CxxParser parser(&client, &fm);
std::vector<std::string> filePaths;
filePaths.push_back("data/CxxParserTestSuite/header.h");
filePaths.push_back("data/CxxParserTestSuite/code.cpp");
std::vector<FilePath> filePaths;
filePaths.push_back(FilePath("data/CxxParserTestSuite/header.h"));
filePaths.push_back(FilePath("data/CxxParserTestSuite/code.cpp"));
parser.parseFiles(filePaths, std::vector<std::string>(), std::vector<std::string>());
TS_ASSERT_EQUALS(client.errors.size(), 0);
+127
View File
@@ -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<std::string> 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);
}
};
+13 -13
View File
@@ -573,8 +573,8 @@ public:
TS_ASSERT_EQUALS(storage.tokenLocationCollection().getTokenLocations().size(), 4);
TS_ASSERT_EQUALS(storage.searchIndex().getNodeCount(), 3);
std::set<std::string> files;
files.insert(m_filePath);
std::set<FilePath> 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<std::string> files;
files.insert("file.cpp");
std::set<FilePath> 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<std::string> files;
files.insert("file.h");
std::set<FilePath> 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<std::string> filePaths;
filePaths.insert("file.cpp");
filePaths.insert("file.h");
std::set<FilePath> 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<std::string> filePaths;
filePaths.insert(name1);
std::set<std::string> dependingFilePaths = storage.getDependingFilePathsAndRemoveFileNodes(filePaths);
std::set<FilePath> filePaths;
filePaths.insert(FilePath(name1));
std::set<FilePath> 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));
+1 -1
View File
@@ -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()
+3 -3
View File
@@ -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;
}
+3 -3
View File
@@ -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