data: ClangTool gets invoked for cpp files only, headers are parsed on-the-fly
This change adds the class FileRegister, that splits cpp and h files for the Parser and keeps track which headers have been parsed yet. TranslationUnits get only created for cpp files now, and headers are parsed on the fly. This reduces parse time by around half.
This commit is contained in:
@@ -96,3 +96,13 @@ bool FileManager::hasFilePath(const std::string& filePath) const
|
||||
{
|
||||
return (m_files.find(FileSystem::absoluteFilePath(filePath)) != m_files.end());
|
||||
}
|
||||
|
||||
bool FileManager::hasSourceExtension(const std::string& filePath) const
|
||||
{
|
||||
return FileSystem::hasExtension(filePath, m_sourceExtensions);
|
||||
}
|
||||
|
||||
bool FileManager::hasIncludeExtension(const std::string& filePath) const
|
||||
{
|
||||
return FileSystem::hasExtension(filePath, m_includeExtensions);
|
||||
}
|
||||
|
||||
@@ -25,6 +25,8 @@ public:
|
||||
std::set<std::string> 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;
|
||||
|
||||
private:
|
||||
std::vector<std::string> m_sourcePaths;
|
||||
|
||||
@@ -0,0 +1,66 @@
|
||||
#include "utility/file/FileRegister.h"
|
||||
|
||||
#include "utility/file/FileManager.h"
|
||||
#include "utility/file/FileSystem.h"
|
||||
|
||||
FileRegister::FileRegister(const FileManager* fileManager, const std::vector<std::string>& filePaths)
|
||||
: m_fileManager(fileManager)
|
||||
{
|
||||
for (const std::string& path : filePaths)
|
||||
{
|
||||
if (m_fileManager->hasSourceExtension(path))
|
||||
{
|
||||
m_sourceFilePaths.push_back(path);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_includeFilePaths.emplace(path, STATE_UNPARSED);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const FileManager* FileRegister::getFileManager() const
|
||||
{
|
||||
return m_fileManager;
|
||||
}
|
||||
|
||||
const std::vector<std::string>& 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));
|
||||
if (it == m_includeFilePaths.end())
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return it->second == STATE_PARSING;
|
||||
}
|
||||
|
||||
void FileRegister::markIncludeFileParsing(const std::string& filePath)
|
||||
{
|
||||
std::map<std::string, ParseState>::iterator it = m_includeFilePaths.find(FileSystem::absoluteFilePath(filePath));
|
||||
if (it == m_includeFilePaths.end())
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (it->second != STATE_PARSED)
|
||||
{
|
||||
it->second = STATE_PARSING;
|
||||
}
|
||||
}
|
||||
|
||||
void FileRegister::markParsingIncludeFilesParsed()
|
||||
{
|
||||
for (std::pair<std::string, ParseState>&& p : m_includeFilePaths)
|
||||
{
|
||||
if (p.second == STATE_PARSING)
|
||||
{
|
||||
p.second = STATE_PARSED;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#ifndef FILE_REGISTER_H
|
||||
#define FILE_REGISTER_H
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class FileManager;
|
||||
|
||||
class FileRegister
|
||||
{
|
||||
public:
|
||||
FileRegister(const FileManager* fileManager, const std::vector<std::string>& filePaths);
|
||||
|
||||
const FileManager* getFileManager() const;
|
||||
|
||||
const std::vector<std::string>& getSourceFilePaths() const;
|
||||
|
||||
bool includeFileIsParsing(const std::string& filePath) const;
|
||||
|
||||
void markIncludeFileParsing(const std::string& filePath);
|
||||
void markParsingIncludeFilesParsed();
|
||||
|
||||
private:
|
||||
enum ParseState
|
||||
{
|
||||
STATE_UNPARSED,
|
||||
STATE_PARSING,
|
||||
STATE_PARSED
|
||||
};
|
||||
|
||||
const FileManager* m_fileManager;
|
||||
|
||||
std::vector<std::string> m_sourceFilePaths;
|
||||
std::map<std::string, ParseState> m_includeFilePaths;
|
||||
};
|
||||
|
||||
#endif // FILE_REGISTER_H
|
||||
@@ -15,7 +15,7 @@ std::vector<std::string> FileSystem::getFileNamesFromDirectory(
|
||||
boost::filesystem::recursive_directory_iterator endit;
|
||||
while (it != endit)
|
||||
{
|
||||
if (boost::filesystem::is_regular_file(*it) && isValidExtension(it->path().string(), extensions))
|
||||
if (boost::filesystem::is_regular_file(*it) && hasExtension(it->path().string(), extensions))
|
||||
{
|
||||
files.push_back(it->path().generic_string());
|
||||
}
|
||||
@@ -38,7 +38,7 @@ std::vector<std::string> FileSystem::getFileNamesFromDirectoryUpdatedAfter(
|
||||
boost::filesystem::recursive_directory_iterator endit;
|
||||
while (it != endit)
|
||||
{
|
||||
if (boost::filesystem::is_regular_file(*it) && isValidExtension(it->path().string(), extensions))
|
||||
if (boost::filesystem::is_regular_file(*it) && hasExtension(it->path().string(), extensions))
|
||||
{
|
||||
std::time_t t = boost::filesystem::last_write_time(*it);
|
||||
boost::posix_time::ptime lastWriteTime = boost::posix_time::from_time_t(t);
|
||||
@@ -65,7 +65,7 @@ std::vector<FileInfo> FileSystem::getFileInfosFromDirectoryPaths(
|
||||
boost::filesystem::recursive_directory_iterator endit;
|
||||
while (it != endit)
|
||||
{
|
||||
if (boost::filesystem::is_regular_file(*it) && isValidExtension(it->path().string(), fileExtensions))
|
||||
if (boost::filesystem::is_regular_file(*it) && hasExtension(it->path().string(), fileExtensions))
|
||||
{
|
||||
std::time_t t = boost::filesystem::last_write_time(*it);
|
||||
boost::posix_time::ptime lastWriteTime = boost::posix_time::from_time_t(t);
|
||||
@@ -93,6 +93,11 @@ std::string FileSystem::fileName(const std::string& path)
|
||||
return boost::filesystem::path(path).filename().generic_string();
|
||||
}
|
||||
|
||||
std::string FileSystem::absoluteFilePath(const std::string& path)
|
||||
{
|
||||
return boost::filesystem::absolute(boost::filesystem::path(path)).generic_string();
|
||||
}
|
||||
|
||||
std::string FileSystem::extension(const std::string& path)
|
||||
{
|
||||
return boost::filesystem::path(path).extension().generic_string();
|
||||
@@ -103,22 +108,7 @@ std::string FileSystem::filePathWithoutExtension(const std::string& path)
|
||||
return boost::filesystem::path(path).replace_extension().generic_string();
|
||||
}
|
||||
|
||||
std::string FileSystem::absoluteFilePath(const std::string& path)
|
||||
{
|
||||
return boost::filesystem::absolute(boost::filesystem::path(path)).generic_string();
|
||||
}
|
||||
|
||||
bool FileSystem::equivalent(const std::string& pathA, const std::string& pathB)
|
||||
{
|
||||
if (exists(pathA) && exists(pathB))
|
||||
{
|
||||
return boost::filesystem::equivalent(boost::filesystem::path(pathA), boost::filesystem::path(pathB));
|
||||
}
|
||||
|
||||
return boost::filesystem::path(pathA).compare(boost::filesystem::path(pathB)) == 0;
|
||||
}
|
||||
|
||||
bool FileSystem::isValidExtension(const std::string& filepath, const std::vector<std::string>& extensions)
|
||||
bool FileSystem::hasExtension(const std::string& filepath, const std::vector<std::string>& extensions)
|
||||
{
|
||||
boost::filesystem::path path(filepath);
|
||||
|
||||
@@ -131,3 +121,13 @@ bool FileSystem::isValidExtension(const std::string& filepath, const std::vector
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool FileSystem::equivalent(const std::string& pathA, const std::string& pathB)
|
||||
{
|
||||
if (exists(pathA) && exists(pathB))
|
||||
{
|
||||
return boost::filesystem::equivalent(boost::filesystem::path(pathA), boost::filesystem::path(pathB));
|
||||
}
|
||||
|
||||
return boost::filesystem::path(pathA).compare(boost::filesystem::path(pathB)) == 0;
|
||||
}
|
||||
|
||||
@@ -21,14 +21,13 @@ public:
|
||||
|
||||
static bool exists(const std::string& path);
|
||||
static std::string fileName(const std::string& path);
|
||||
static std::string extension(const std::string& path);
|
||||
static std::string filePathWithoutExtension(const std::string& path);
|
||||
static std::string absoluteFilePath(const std::string& path);
|
||||
|
||||
static bool equivalent(const std::string& pathA, const std::string& pathB);
|
||||
static std::string extension(const std::string& path);
|
||||
static std::string filePathWithoutExtension(const std::string& path);
|
||||
static bool hasExtension(const std::string& filepath, const std::vector<std::string>& extensions);
|
||||
|
||||
private:
|
||||
static bool isValidExtension(const std::string& filepath, const std::vector<std::string>& extensions);
|
||||
static bool equivalent(const std::string& pathA, const std::string& pathB);
|
||||
};
|
||||
|
||||
#endif // FILE_SYSTEM_H
|
||||
|
||||
Reference in New Issue
Block a user