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
+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);