From 713a8eec50bc1750df9721eeccac856928a97c02 Mon Sep 17 00:00:00 2001 From: Eberhard Graether Date: Fri, 27 Jul 2018 12:39:55 +0200 Subject: [PATCH] data: Improved file path caching in CXX indexer * centralized all file path caching in CanonicalFilePathCache * added cache by clang::FileID * moved FileRegister into CanonicalFilePathCache and removed it from AST* classes * only record files once --- src/lib_cxx/data/parser/cxx/ASTAction.h | 16 ++-- .../data/parser/cxx/ASTActionFactory.cpp | 7 +- .../data/parser/cxx/ASTActionFactory.h | 6 +- src/lib_cxx/data/parser/cxx/ASTConsumer.cpp | 5 +- src/lib_cxx/data/parser/cxx/ASTConsumer.h | 2 - .../parser/cxx/CanonicalFilePathCache.cpp | 60 ++++++++++++++- .../data/parser/cxx/CanonicalFilePathCache.h | 18 ++++- .../data/parser/cxx/CommentHandler.cpp | 38 ++++------ src/lib_cxx/data/parser/cxx/CommentHandler.h | 5 +- src/lib_cxx/data/parser/cxx/CxxAstVisitor.cpp | 62 +++------------- src/lib_cxx/data/parser/cxx/CxxAstVisitor.h | 13 ---- .../data/parser/cxx/CxxDiagnosticConsumer.cpp | 7 +- .../data/parser/cxx/CxxDiagnosticConsumer.h | 3 - src/lib_cxx/data/parser/cxx/CxxParser.cpp | 15 ++-- .../data/parser/cxx/CxxVerboseAstVisitor.cpp | 4 +- .../data/parser/cxx/CxxVerboseAstVisitor.h | 3 +- .../data/parser/cxx/PreprocessorCallbacks.cpp | 74 +++++++------------ .../data/parser/cxx/PreprocessorCallbacks.h | 17 +---- .../cxx/name_resolver/CxxDeclNameResolver.cpp | 18 ++--- 19 files changed, 158 insertions(+), 215 deletions(-) diff --git a/src/lib_cxx/data/parser/cxx/ASTAction.h b/src/lib_cxx/data/parser/cxx/ASTAction.h index d797b73b..1cb91504 100644 --- a/src/lib_cxx/data/parser/cxx/ASTAction.h +++ b/src/lib_cxx/data/parser/cxx/ASTAction.h @@ -3,14 +3,13 @@ #include -#include "clang/Frontend/CompilerInstance.h" -#include "clang/Frontend/FrontendAction.h" -#include "clang/Lex/Preprocessor.h" +#include +#include +#include #include "data/parser/cxx/ASTConsumer.h" #include "data/parser/cxx/CommentHandler.h" #include "data/parser/cxx/PreprocessorCallbacks.h" -#include "utility/file/FileRegister.h" template class ASTAction @@ -19,13 +18,11 @@ class ASTAction public: explicit ASTAction( std::shared_ptr client, - std::shared_ptr fileRegister, std::shared_ptr canonicalFilePathCache ) : m_client(client) - , m_fileRegister(fileRegister) , m_canonicalFilePathCache(canonicalFilePathCache) - , m_commentHandler(client, fileRegister, canonicalFilePathCache) + , m_commentHandler(client, canonicalFilePathCache) {} virtual ~ASTAction() {} @@ -34,21 +31,20 @@ protected: virtual std::unique_ptr CreateASTConsumer(clang::CompilerInstance& compiler, llvm::StringRef inFile) override { return std::unique_ptr( - new ASTConsumer(&compiler.getASTContext(), &compiler.getPreprocessor(), m_client, m_fileRegister, m_canonicalFilePathCache)); + new ASTConsumer(&compiler.getASTContext(), &compiler.getPreprocessor(), m_client, m_canonicalFilePathCache)); } virtual bool BeginSourceFileAction(clang::CompilerInstance& compiler) override { clang::Preprocessor& preprocessor = compiler.getPreprocessor(); preprocessor.addPPCallbacks( - llvm::make_unique(compiler.getSourceManager(), m_client, m_fileRegister, m_canonicalFilePathCache)); + llvm::make_unique(compiler.getSourceManager(), m_client, m_canonicalFilePathCache)); preprocessor.addCommentHandler(&m_commentHandler); return true; } private: std::shared_ptr m_client; - std::shared_ptr m_fileRegister; std::shared_ptr m_canonicalFilePathCache; CommentHandler m_commentHandler; }; diff --git a/src/lib_cxx/data/parser/cxx/ASTActionFactory.cpp b/src/lib_cxx/data/parser/cxx/ASTActionFactory.cpp index 459521d7..d6b291e5 100644 --- a/src/lib_cxx/data/parser/cxx/ASTActionFactory.cpp +++ b/src/lib_cxx/data/parser/cxx/ASTActionFactory.cpp @@ -1,15 +1,14 @@ #include "data/parser/cxx/ASTActionFactory.h" -#include "clang/Frontend/FrontendActions.h" +#include + #include "data/parser/cxx/ASTAction.h" ASTActionFactory::ASTActionFactory( std::shared_ptr client, - std::shared_ptr fileRegister, std::shared_ptr canonicalFilePathCache ) : m_client(client) - , m_fileRegister(fileRegister) , m_canonicalFilePathCache(canonicalFilePathCache) { } @@ -20,5 +19,5 @@ ASTActionFactory::~ASTActionFactory() clang::FrontendAction* ASTActionFactory::create() { - return new ASTAction(m_client, m_fileRegister, m_canonicalFilePathCache); + return new ASTAction(m_client, m_canonicalFilePathCache); } diff --git a/src/lib_cxx/data/parser/cxx/ASTActionFactory.h b/src/lib_cxx/data/parser/cxx/ASTActionFactory.h index b52934aa..fcfde9e9 100644 --- a/src/lib_cxx/data/parser/cxx/ASTActionFactory.h +++ b/src/lib_cxx/data/parser/cxx/ASTActionFactory.h @@ -1,9 +1,7 @@ #ifndef AST_ACTION_FACTORY #define AST_ACTION_FACTORY -#include "clang/Tooling/Tooling.h" - -#include "utility/file/FileRegister.h" +#include class CanonicalFilePathCache; class ParserClient; @@ -14,7 +12,6 @@ class ASTActionFactory public: explicit ASTActionFactory( std::shared_ptr client, - std::shared_ptr fileRegister, std::shared_ptr canonicalFilePathCache ); @@ -24,7 +21,6 @@ public: private: std::shared_ptr m_client; - std::shared_ptr m_fileRegister; std::shared_ptr m_canonicalFilePathCache; }; diff --git a/src/lib_cxx/data/parser/cxx/ASTConsumer.cpp b/src/lib_cxx/data/parser/cxx/ASTConsumer.cpp index 7f565273..46127cd3 100644 --- a/src/lib_cxx/data/parser/cxx/ASTConsumer.cpp +++ b/src/lib_cxx/data/parser/cxx/ASTConsumer.cpp @@ -8,7 +8,6 @@ ASTConsumer::ASTConsumer( clang::ASTContext* context, clang::Preprocessor* preprocessor, std::shared_ptr client, - std::shared_ptr fileRegister, std::shared_ptr canonicalFilePathCache ) { @@ -16,11 +15,11 @@ ASTConsumer::ASTConsumer( if (appSettings->getLoggingEnabled() && appSettings->getVerboseIndexerLoggingEnabled()) { - m_visitor = std::make_shared(context, preprocessor, client, fileRegister, canonicalFilePathCache); + m_visitor = std::make_shared(context, preprocessor, client, canonicalFilePathCache); } else { - m_visitor = std::make_shared(context, preprocessor, client, fileRegister, canonicalFilePathCache); + m_visitor = std::make_shared(context, preprocessor, client, canonicalFilePathCache); } } diff --git a/src/lib_cxx/data/parser/cxx/ASTConsumer.h b/src/lib_cxx/data/parser/cxx/ASTConsumer.h index 871f621f..7fadaeeb 100644 --- a/src/lib_cxx/data/parser/cxx/ASTConsumer.h +++ b/src/lib_cxx/data/parser/cxx/ASTConsumer.h @@ -6,7 +6,6 @@ class CanonicalFilePathCache; class CxxAstVisitor; -class FileRegister; class ParserClient; class ASTConsumer @@ -17,7 +16,6 @@ public: clang::ASTContext* context, clang::Preprocessor* preprocessor, std::shared_ptr client, - std::shared_ptr fileRegister, std::shared_ptr canonicalFilePathCache ); diff --git a/src/lib_cxx/data/parser/cxx/CanonicalFilePathCache.cpp b/src/lib_cxx/data/parser/cxx/CanonicalFilePathCache.cpp index 29a17d9e..f03742ea 100644 --- a/src/lib_cxx/data/parser/cxx/CanonicalFilePathCache.cpp +++ b/src/lib_cxx/data/parser/cxx/CanonicalFilePathCache.cpp @@ -3,6 +3,40 @@ #include "utility/utilityString.h" #include "data/parser/cxx/utilityClang.h" +CanonicalFilePathCache::CanonicalFilePathCache(std::shared_ptr fileRegister) + : m_fileRegister(fileRegister) +{ +} + +std::shared_ptr CanonicalFilePathCache::getFileRegister() const +{ + return m_fileRegister; +} + +FilePath CanonicalFilePathCache::getCanonicalFilePath(const clang::FileID& fileId, const clang::SourceManager& sourceManager) +{ + if (!fileId.isValid()) + { + return FilePath(); + } + + auto it = m_fileIdMap.find(fileId); + if (it != m_fileIdMap.end()) + { + return it->second; + } + + FilePath filePath; + + const clang::FileEntry* fileEntry = sourceManager.getFileEntryForID(fileId); + if (fileEntry != nullptr && fileEntry->isValid()) + { + filePath = getCanonicalFilePath(fileEntry); + m_fileIdMap.emplace(fileId, filePath); + } + + return filePath; +} FilePath CanonicalFilePathCache::getCanonicalFilePath(const clang::FileEntry* entry) { @@ -13,8 +47,8 @@ FilePath CanonicalFilePathCache::getCanonicalFilePath(const std::wstring& path) { const std::wstring lowercasePath = utility::toLowerCase(path); - std::unordered_map::const_iterator it = m_map.find(lowercasePath); - if (it != m_map.end()) + auto it = m_fileStringMap.find(lowercasePath); + if (it != m_fileStringMap.end()) { return it->second; } @@ -22,8 +56,26 @@ FilePath CanonicalFilePathCache::getCanonicalFilePath(const std::wstring& path) const FilePath canonicalPath = FilePath(path).makeCanonical(); const std::wstring lowercaseCanonicalPath = utility::toLowerCase(canonicalPath.wstr()); - m_map.insert(std::make_pair(lowercasePath, canonicalPath)); - m_map.insert(std::make_pair(lowercaseCanonicalPath, canonicalPath)); + m_fileStringMap.emplace(std::move(lowercasePath), canonicalPath); + m_fileStringMap.emplace(std::move(lowercaseCanonicalPath), canonicalPath); return canonicalPath; } + +bool CanonicalFilePathCache::isProjectFile(const clang::FileID fileId, const clang::SourceManager& sourceManager) +{ + if (!fileId.isValid()) + { + return false; + } + + auto it = m_isProjectFileMap.find(fileId); + if (it != m_isProjectFileMap.end()) + { + return it->second; + } + + bool ret = m_fileRegister->hasFilePath(getCanonicalFilePath(fileId, sourceManager)); + m_isProjectFileMap.emplace(fileId, ret); + return ret; +} diff --git a/src/lib_cxx/data/parser/cxx/CanonicalFilePathCache.h b/src/lib_cxx/data/parser/cxx/CanonicalFilePathCache.h index 1606bfc4..e8ffbb75 100644 --- a/src/lib_cxx/data/parser/cxx/CanonicalFilePathCache.h +++ b/src/lib_cxx/data/parser/cxx/CanonicalFilePathCache.h @@ -4,17 +4,31 @@ #include #include -#include "clang/Basic/FileManager.h" +#include + #include "utility/file/FilePath.h" +#include "utility/file/FileRegister.h" class CanonicalFilePathCache { public: + CanonicalFilePathCache(std::shared_ptr fileRegister); + + std::shared_ptr getFileRegister() const; + + FilePath getCanonicalFilePath(const clang::FileID& fileId, const clang::SourceManager& sourceManager); FilePath getCanonicalFilePath(const clang::FileEntry* entry); FilePath getCanonicalFilePath(const std::wstring& path); + bool isProjectFile(const clang::FileID fileId, const clang::SourceManager& sourceManager); + private: - std::unordered_map m_map; + std::shared_ptr m_fileRegister; + + std::map m_fileIdMap; + std::unordered_map m_fileStringMap; + + std::map m_isProjectFileMap; }; #endif // CANONICAL_FILE_PATH_CACHE_H diff --git a/src/lib_cxx/data/parser/cxx/CommentHandler.cpp b/src/lib_cxx/data/parser/cxx/CommentHandler.cpp index 99a3561a..cb127866 100644 --- a/src/lib_cxx/data/parser/cxx/CommentHandler.cpp +++ b/src/lib_cxx/data/parser/cxx/CommentHandler.cpp @@ -4,15 +4,12 @@ #include "data/parser/cxx/utilityClang.h" #include "data/parser/ParseLocation.h" #include "data/parser/ParserClient.h" -#include "utility/file/FileRegister.h" CommentHandler::CommentHandler( std::shared_ptr client, - std::shared_ptr fileRegister, std::shared_ptr canonicalFilePathCache ) : m_client(client) - , m_fileRegister(fileRegister) , m_canonicalFilePathCache(canonicalFilePathCache) { } @@ -23,30 +20,23 @@ CommentHandler::~CommentHandler() bool CommentHandler::HandleComment(clang::Preprocessor& preprocessor, clang::SourceRange sourceRange) { - clang::SourceManager& sourceManager = preprocessor.getSourceManager(); - const clang::PresumedLoc& presumedBegin = sourceManager.getPresumedLoc(sourceRange.getBegin(), false); - const clang::PresumedLoc& presumedEnd = sourceManager.getPresumedLoc(sourceRange.getEnd(), false); + const clang::SourceManager& sourceManager = preprocessor.getSourceManager(); - clang::FileID fileId = sourceManager.getFileID(sourceRange.getBegin()); + const clang::FileID fileId = sourceManager.getFileID(sourceRange.getBegin()); + FilePath filePath = m_canonicalFilePathCache->getCanonicalFilePath(fileId, sourceManager); - // find the location file - if (fileId.isValid()) + if (m_canonicalFilePathCache->isProjectFile(fileId, sourceManager)) { - const clang::FileEntry* fileEntry = sourceManager.getFileEntryForID(fileId); - if (fileEntry != nullptr && fileEntry->isValid()) - { - FilePath filePath = m_canonicalFilePathCache->getCanonicalFilePath(fileEntry); - if (m_fileRegister->hasFilePath(filePath)) - { - m_client->recordComment(ParseLocation( - filePath, - presumedBegin.getLine(), - presumedBegin.getColumn(), - presumedEnd.getLine(), - presumedEnd.getColumn() - )); - } - } + const clang::PresumedLoc& presumedBegin = sourceManager.getPresumedLoc(sourceRange.getBegin(), false); + const clang::PresumedLoc& presumedEnd = sourceManager.getPresumedLoc(sourceRange.getEnd(), false); + + m_client->recordComment(ParseLocation( + filePath, + presumedBegin.getLine(), + presumedBegin.getColumn(), + presumedEnd.getLine(), + presumedEnd.getColumn() + )); } return false; diff --git a/src/lib_cxx/data/parser/cxx/CommentHandler.h b/src/lib_cxx/data/parser/cxx/CommentHandler.h index ea7f7121..19373231 100644 --- a/src/lib_cxx/data/parser/cxx/CommentHandler.h +++ b/src/lib_cxx/data/parser/cxx/CommentHandler.h @@ -1,10 +1,9 @@ #ifndef COMMENT_HANDLER_H #define COMMENT_HANDLER_H -#include "clang/Lex/Preprocessor.h" +#include class CanonicalFilePathCache; -class FileRegister; class ParserClient; class CommentHandler @@ -13,7 +12,6 @@ class CommentHandler public: CommentHandler( std::shared_ptr client, - std::shared_ptr fileRegister, std::shared_ptr canonicalFilePathCache ); @@ -23,7 +21,6 @@ public: private: std::shared_ptr m_client; - std::shared_ptr m_fileRegister; std::shared_ptr m_canonicalFilePathCache; }; diff --git a/src/lib_cxx/data/parser/cxx/CxxAstVisitor.cpp b/src/lib_cxx/data/parser/cxx/CxxAstVisitor.cpp index f2de127b..0dd5f745 100644 --- a/src/lib_cxx/data/parser/cxx/CxxAstVisitor.cpp +++ b/src/lib_cxx/data/parser/cxx/CxxAstVisitor.cpp @@ -17,7 +17,6 @@ #include "data/parser/cxx/utilityClang.h" #include "data/parser/ParserClient.h" #include "data/parser/ParseLocation.h" -#include "utility/file/FileRegister.h" #include "utility/utilityString.h" @@ -25,13 +24,11 @@ CxxAstVisitor::CxxAstVisitor( clang::ASTContext* astContext, clang::Preprocessor* preprocessor, std::shared_ptr client, - std::shared_ptr fileRegister, std::shared_ptr canonicalFilePathCache ) : m_astContext(astContext) , m_preprocessor(preprocessor) , m_client(client) - , m_fileRegister(fileRegister) , m_canonicalFilePathCache(canonicalFilePathCache) { m_declNameCache = std::make_shared([&](const clang::NamedDecl* decl) -> NameHierarchy @@ -702,14 +699,7 @@ ParseLocation CxxAstVisitor::getParseLocation(const clang::SourceLocation& sourc const clang::FileID fileId = sourceManager.getFileID(startLoc); // find the location file - if (!fileId.isInvalid()) - { - const clang::FileEntry* fileEntry = sourceManager.getFileEntryForID(fileId); - if (fileEntry != nullptr && fileEntry->isValid()) - { - parseLocation.filePath = m_canonicalFilePathCache->getCanonicalFilePath(fileEntry); - } - } + parseLocation.filePath = m_canonicalFilePathCache->getCanonicalFilePath(fileId, sourceManager); // find the start location { @@ -765,17 +755,10 @@ ParseLocation CxxAstVisitor::getParseLocation(const clang::SourceRange& sourceRa const clang::PresumedLoc presumedBegin = sourceManager.getPresumedLoc(beginLoc, false); const clang::PresumedLoc presumedEnd = sourceManager.getPresumedLoc(endLoc.isValid() ? endLoc : range.getEnd(), false); - FilePath filePath; + FilePath filePath = m_canonicalFilePathCache->getCanonicalFilePath(sourceManager.getFileID(beginLoc), sourceManager); + if (filePath.empty()) { - const clang::FileEntry* fileEntry = sourceManager.getFileEntryForID(sourceManager.getFileID(beginLoc)); - if (fileEntry != nullptr && fileEntry->isValid()) - { - filePath = m_canonicalFilePathCache->getCanonicalFilePath(fileEntry); - } - else - { - filePath = m_canonicalFilePathCache->getCanonicalFilePath(utility::decodeFromUtf8(presumedBegin.getFilename())); - } + filePath = m_canonicalFilePathCache->getCanonicalFilePath(utility::decodeFromUtf8(presumedBegin.getFilename())); } parseLocation = ParseLocation( @@ -845,36 +828,11 @@ bool CxxAstVisitor::shouldVisitReference(const clang::SourceLocation& referenceL bool CxxAstVisitor::isLocatedInProjectFile(clang::SourceLocation loc) const { + if (loc.isInvalid()) + { + return false; + } + const clang::SourceManager& sourceManager = m_astContext->getSourceManager(); - - clang::FileID fileId; - if (loc.isValid()) - { - if (sourceManager.isWrittenInMainFile(loc)) - { - return true; - } - - fileId = sourceManager.getFileID(loc); - } - - if (fileId.isValid()) - { - auto it = m_inProjectFileMap.find(fileId); - if (it != m_inProjectFileMap.end()) - { - return it->second; - } - - const clang::FileEntry* fileEntry = sourceManager.getFileEntryForID(fileId); - if (fileEntry != nullptr && fileEntry->isValid()) - { - FilePath filePath = m_canonicalFilePathCache->getCanonicalFilePath(fileEntry); - const bool ret = m_fileRegister->hasFilePath(filePath); - m_inProjectFileMap[fileId] = ret; - return ret; - } - } - - return false; + return m_canonicalFilePathCache->isProjectFile(sourceManager.getFileID(loc), sourceManager); } diff --git a/src/lib_cxx/data/parser/cxx/CxxAstVisitor.h b/src/lib_cxx/data/parser/cxx/CxxAstVisitor.h index 557d18a2..5d747dcc 100644 --- a/src/lib_cxx/data/parser/cxx/CxxAstVisitor.h +++ b/src/lib_cxx/data/parser/cxx/CxxAstVisitor.h @@ -11,7 +11,6 @@ class CanonicalFilePathCache; class ParserClient; struct ParseLocation; -class FileRegister; class FilePath; class CxxAstVisitorComponent; @@ -41,7 +40,6 @@ public: clang::ASTContext* astContext, clang::Preprocessor* preprocessor, std::shared_ptr client, - std::shared_ptr fileRegister, std::shared_ptr canonicalFilePathCache ); virtual ~CxxAstVisitor() = default; @@ -161,7 +159,6 @@ private: clang::ASTContext* m_astContext; clang::Preprocessor* m_preprocessor; std::shared_ptr m_client; - std::shared_ptr m_fileRegister; std::shared_ptr m_canonicalFilePathCache; MessageInterruptTasksCounter m_interruptCounter; @@ -176,16 +173,6 @@ private: std::shared_ptr m_declNameCache; std::shared_ptr m_typeNameCache; - - struct FileIdHash - { - size_t operator()(clang::FileID fileID) const - { - return fileID.getHashValue(); - } - }; - - mutable std::unordered_map m_inProjectFileMap; }; template <> diff --git a/src/lib_cxx/data/parser/cxx/CxxDiagnosticConsumer.cpp b/src/lib_cxx/data/parser/cxx/CxxDiagnosticConsumer.cpp index c3afb96c..e97442bb 100644 --- a/src/lib_cxx/data/parser/cxx/CxxDiagnosticConsumer.cpp +++ b/src/lib_cxx/data/parser/cxx/CxxDiagnosticConsumer.cpp @@ -7,21 +7,18 @@ #include "data/parser/cxx/utilityClang.h" #include "data/parser/ParseLocation.h" #include "data/parser/ParserClient.h" -#include "utility/file/FileRegister.h" #include "utility/utilityString.h" CxxDiagnosticConsumer::CxxDiagnosticConsumer( clang::raw_ostream &os, clang::DiagnosticOptions *diags, std::shared_ptr client, - std::shared_ptr fileRegister, std::shared_ptr canonicalFilePathCache, const FilePath& sourceFilePath, bool useLogging ) : clang::TextDiagnosticPrinter(os, diags) , m_client(client) - , m_register(fileRegister) , m_canonicalFilePathCache(canonicalFilePathCache) , m_sourceFilePath(sourceFilePath) , m_isParsingFile(false) @@ -36,8 +33,6 @@ void CxxDiagnosticConsumer::BeginSourceFile(const clang::LangOptions& langOption clang::TextDiagnosticPrinter::BeginSourceFile(langOptions, preProcessor); } - - m_isParsingFile = true; } @@ -111,7 +106,7 @@ void CxxDiagnosticConsumer::HandleDiagnostic(clang::DiagnosticsEngine::Level lev location, utility::decodeFromUtf8(message), level == clang::DiagnosticsEngine::Fatal, - m_register->hasFilePath(location.filePath), + m_canonicalFilePathCache->getFileRegister()->hasFilePath(location.filePath), m_sourceFilePath ); } diff --git a/src/lib_cxx/data/parser/cxx/CxxDiagnosticConsumer.h b/src/lib_cxx/data/parser/cxx/CxxDiagnosticConsumer.h index 154918ae..f95e8337 100644 --- a/src/lib_cxx/data/parser/cxx/CxxDiagnosticConsumer.h +++ b/src/lib_cxx/data/parser/cxx/CxxDiagnosticConsumer.h @@ -5,7 +5,6 @@ #include "utility/file/FilePath.h" class CanonicalFilePathCache; -class FileRegister; class ParserClient; class CxxDiagnosticConsumer @@ -16,7 +15,6 @@ public: clang::raw_ostream &os, clang::DiagnosticOptions *diags, std::shared_ptr client, - std::shared_ptr fileRegister, std::shared_ptr canonicalFilePathCache, const FilePath& sourceFilePath, bool useLogging = true @@ -29,7 +27,6 @@ public: private: std::shared_ptr m_client; - std::shared_ptr m_register; std::shared_ptr m_canonicalFilePathCache; const FilePath m_sourceFilePath; diff --git a/src/lib_cxx/data/parser/cxx/CxxParser.cpp b/src/lib_cxx/data/parser/cxx/CxxParser.cpp index 9f722ccd..14fbbccb 100644 --- a/src/lib_cxx/data/parser/cxx/CxxParser.cpp +++ b/src/lib_cxx/data/parser/cxx/CxxParser.cpp @@ -1,7 +1,7 @@ #include "data/parser/cxx/CxxParser.h" -#include "clang/Tooling/Tooling.h" -#include "llvm/Support/TargetSelect.h" +#include +#include #include "data/indexer/IndexerCommandCxxCdb.h" #include "data/indexer/IndexerCommandCxxEmpty.h" @@ -101,10 +101,11 @@ void CxxParser::buildIndex(std::shared_ptr indexerComman void CxxParser::buildIndex(const std::wstring& fileName, std::shared_ptr fileContent, std::vector compilerFlags) { - std::shared_ptr canonicalFilePathCache = std::make_shared(); + std::shared_ptr canonicalFilePathCache = + std::make_shared(m_fileRegister); std::shared_ptr diagnostics = getDiagnostics(FilePath(), canonicalFilePathCache, false); - ASTActionFactory actionFactory(m_client, m_fileRegister, canonicalFilePathCache); + ASTActionFactory actionFactory(m_client, canonicalFilePathCache); std::vector args = getCommandlineArgumentsEssential(compilerFlags, std::vector(), std::vector()); @@ -121,13 +122,13 @@ void CxxParser::runTool(clang::tooling::CompilationDatabase* compilationDatabase { clang::tooling::ClangTool tool(*compilationDatabase, std::vector(1, utility::encodeToUtf8(sourceFilePath.wstr()))); - std::shared_ptr canonicalFilePathCache = std::make_shared(); + std::shared_ptr canonicalFilePathCache = std::make_shared(m_fileRegister); std::shared_ptr diagnostics = getDiagnostics(sourceFilePath, canonicalFilePathCache, true); tool.setDiagnosticConsumer(diagnostics.get()); - ASTActionFactory actionFactory(m_client, m_fileRegister, canonicalFilePathCache); + ASTActionFactory actionFactory(m_client, canonicalFilePathCache); tool.run(&actionFactory); } @@ -201,6 +202,6 @@ std::shared_ptr CxxParser::getDiagnostics(const FilePath& { llvm::IntrusiveRefCntPtr options = new clang::DiagnosticOptions(); return std::make_shared( - llvm::errs(), &*options, m_client, m_fileRegister, canonicalFilePathCache, sourceFilePath, logErrors + llvm::errs(), &*options, m_client, canonicalFilePathCache, sourceFilePath, logErrors ); } diff --git a/src/lib_cxx/data/parser/cxx/CxxVerboseAstVisitor.cpp b/src/lib_cxx/data/parser/cxx/CxxVerboseAstVisitor.cpp index a3fa290f..b905efbf 100644 --- a/src/lib_cxx/data/parser/cxx/CxxVerboseAstVisitor.cpp +++ b/src/lib_cxx/data/parser/cxx/CxxVerboseAstVisitor.cpp @@ -7,7 +7,6 @@ #include "data/parser/ParseLocation.h" #include "data/parser/ParserClient.h" -#include "utility/file/FileRegister.h" #include "utility/logging/logging.h" #include "utility/ScopedSwitcher.h" @@ -16,10 +15,9 @@ CxxVerboseAstVisitor::CxxVerboseAstVisitor( clang::ASTContext* context, clang::Preprocessor* preprocessor, std::shared_ptr client, - std::shared_ptr fileRegister, std::shared_ptr canonicalFilePathCache ) - : base(context, preprocessor, client, fileRegister, canonicalFilePathCache) + : base(context, preprocessor, client, canonicalFilePathCache) , m_currentFilePath(L"") , m_indentation(0) { diff --git a/src/lib_cxx/data/parser/cxx/CxxVerboseAstVisitor.h b/src/lib_cxx/data/parser/cxx/CxxVerboseAstVisitor.h index c313cd98..ad987869 100644 --- a/src/lib_cxx/data/parser/cxx/CxxVerboseAstVisitor.h +++ b/src/lib_cxx/data/parser/cxx/CxxVerboseAstVisitor.h @@ -1,3 +1,4 @@ + #ifndef CXX_VERBOSE_AST_VISITOR_H #define CXX_VERBOSE_AST_VISITOR_H @@ -7,7 +8,6 @@ class CanonicalFilePathCache; class ParserClient; -class FileRegister; class CxxVerboseAstVisitor: public CxxAstVisitor { @@ -16,7 +16,6 @@ public: clang::ASTContext* context, clang::Preprocessor* preprocessor, std::shared_ptr client, - std::shared_ptr fileRegister, std::shared_ptr canonicalFilePathCache ); diff --git a/src/lib_cxx/data/parser/cxx/PreprocessorCallbacks.cpp b/src/lib_cxx/data/parser/cxx/PreprocessorCallbacks.cpp index fb2602a9..814f667f 100644 --- a/src/lib_cxx/data/parser/cxx/PreprocessorCallbacks.cpp +++ b/src/lib_cxx/data/parser/cxx/PreprocessorCallbacks.cpp @@ -1,8 +1,8 @@ #include "data/parser/cxx/PreprocessorCallbacks.h" -#include "clang/Driver/Util.h" -#include "clang/Basic/IdentifierTable.h" -#include "clang/Lex/MacroArgs.h" +#include +#include +#include #include "data/parser/cxx/CanonicalFilePathCache.h" #include "data/parser/cxx/utilityClang.h" @@ -10,18 +10,15 @@ #include "data/parser/ParseLocation.h" #include "utility/file/FileSystem.h" -#include "utility/file/FileRegister.h" #include "utility/utilityString.h" PreprocessorCallbacks::PreprocessorCallbacks( clang::SourceManager& sourceManager, std::shared_ptr client, - std::shared_ptr fileRegister, std::shared_ptr canonicalFilePathCache ) : m_sourceManager(sourceManager) , m_client(client) - , m_fileRegister(fileRegister) , m_canonicalFilePathCache(canonicalFilePathCache) { } @@ -29,17 +26,18 @@ PreprocessorCallbacks::PreprocessorCallbacks( void PreprocessorCallbacks::FileChanged( clang::SourceLocation location, FileChangeReason reason, clang::SrcMgr::CharacteristicKind, clang::FileID prevID) { - m_currentPath = FilePath(); + const clang::FileID fileId = m_sourceManager.getFileID(location); + m_currentPath = m_canonicalFilePathCache->getCanonicalFilePath(fileId, m_sourceManager); + m_currentPathIsProjectFile = false; - const clang::FileEntry* fileEntry = m_sourceManager.getFileEntryForID(m_sourceManager.getFileID(location)); - if (fileEntry != nullptr && fileEntry->isValid()) + if (!m_currentPath.empty()) { - m_currentPath = m_canonicalFilePathCache->getCanonicalFilePath(fileEntry); + m_currentPathIsProjectFile = m_canonicalFilePathCache->getFileRegister()->hasFilePath(m_currentPath); - if (!m_currentPath.empty()) + if (m_fileWasRecorded.find(fileId) == m_fileWasRecorded.end()) { - bool hasFilePath = m_fileRegister->hasFilePath(m_currentPath); - m_client->recordFile(FileSystem::getFileInfoForPath(m_currentPath), hasFilePath); // todo: fix for tests + m_client->recordFile(FileSystem::getFileInfoForPath(m_currentPath), m_currentPathIsProjectFile); // todo: fix for tests + m_fileWasRecorded.insert(fileId); } } } @@ -66,7 +64,7 @@ void PreprocessorCallbacks::InclusionDirective( void PreprocessorCallbacks::MacroDefined(const clang::Token& macroNameToken, const clang::MacroDirective* macroDirective) { - if (!m_currentPath.empty() && m_fileRegister->hasFilePath(m_currentPath)) + if (m_currentPathIsProjectFile) { // ignore builtin macros if (m_sourceManager.getSpellingLoc(macroNameToken.getLocation()).printToString(m_sourceManager)[0] == '<') @@ -119,7 +117,7 @@ void PreprocessorCallbacks::MacroExpands( void PreprocessorCallbacks::onMacroUsage(const clang::Token& macroNameToken) { - if (!m_currentPath.empty() && m_fileRegister->hasFilePath(m_currentPath) && isLocatedInProjectFile(macroNameToken.getLocation())) + if (m_currentPathIsProjectFile && isLocatedInProjectFile(macroNameToken.getLocation())) { const ParseLocation loc = getParseLocation(macroNameToken); @@ -140,11 +138,11 @@ ParseLocation PreprocessorCallbacks::getParseLocation(const clang::Token& macroN const clang::SourceLocation& location = m_sourceManager.getSpellingLoc(macroNameTok.getLocation()); const clang::SourceLocation& endLocation = m_sourceManager.getSpellingLoc(macroNameTok.getEndLoc()); - const clang::FileEntry* fileEntry = m_sourceManager.getFileEntryForID(m_sourceManager.getFileID(location)); - if (fileEntry != nullptr && fileEntry->isValid()) + FilePath filePath = m_canonicalFilePathCache->getCanonicalFilePath(m_sourceManager.getFileID(location), m_sourceManager); + if (!filePath.empty()) { return ParseLocation( - m_canonicalFilePathCache->getCanonicalFilePath(fileEntry), + filePath, m_sourceManager.getSpellingLineNumber(location), m_sourceManager.getSpellingColumnNumber(location), m_sourceManager.getSpellingLineNumber(endLocation), @@ -160,11 +158,11 @@ ParseLocation PreprocessorCallbacks::getParseLocation(const clang::MacroInfo* ma clang::SourceLocation location = macroInfo->getDefinitionLoc(); clang::SourceLocation endLocation = macroInfo->getDefinitionEndLoc(); - const clang::FileEntry* fileEntry = m_sourceManager.getFileEntryForID(m_sourceManager.getFileID(location)); - if (fileEntry != nullptr && fileEntry->isValid()) + FilePath filePath = m_canonicalFilePathCache->getCanonicalFilePath(m_sourceManager.getFileID(location), m_sourceManager); + if (!filePath.empty()) { return ParseLocation( - m_canonicalFilePathCache->getCanonicalFilePath(fileEntry), + filePath, m_sourceManager.getSpellingLineNumber(location), m_sourceManager.getSpellingColumnNumber(location), m_sourceManager.getSpellingLineNumber(endLocation), @@ -182,11 +180,13 @@ ParseLocation PreprocessorCallbacks::getParseLocation(const clang::SourceRange& const clang::PresumedLoc& presumedBegin = m_sourceManager.getPresumedLoc(sourceRange.getBegin(), false); const clang::PresumedLoc& presumedEnd = m_sourceManager.getPresumedLoc(sourceRange.getEnd(), false); - const clang::FileEntry *fileEntry = m_sourceManager.getFileEntryForID(m_sourceManager.getFileID(sourceRange.getBegin())); - if (fileEntry != nullptr && fileEntry->isValid()) + FilePath filePath = m_canonicalFilePathCache->getCanonicalFilePath( + m_sourceManager.getFileID(sourceRange.getBegin()), m_sourceManager); + + if (!filePath.empty()) { return ParseLocation( - m_canonicalFilePathCache->getCanonicalFilePath(fileEntry), + filePath, presumedBegin.getLine(), presumedBegin.getColumn(), presumedEnd.getLine(), @@ -201,30 +201,10 @@ bool PreprocessorCallbacks::isLocatedInProjectFile(const clang::SourceLocation l { // we need the spelling loc here, since this is the location where the macro comes from clang::SourceLocation spellingLoc = m_sourceManager.getSpellingLoc(loc); - - clang::FileID fileId; - if (spellingLoc.isValid()) + if (!spellingLoc.isValid()) { - fileId = m_sourceManager.getFileID(spellingLoc); + return false; } - if (fileId.isValid()) - { - auto it = m_inProjectFileMap.find(fileId); - if (it != m_inProjectFileMap.end()) - { - return it->second; - } - - const clang::FileEntry* fileEntry = m_sourceManager.getFileEntryForID(fileId); - if (fileEntry != nullptr && fileEntry->isValid()) - { - const FilePath filePath = m_canonicalFilePathCache->getCanonicalFilePath(fileEntry); - bool ret = m_fileRegister->hasFilePath(filePath); - m_inProjectFileMap[fileId] = ret; - return ret; - } - } - - return false; + return m_canonicalFilePathCache->isProjectFile(m_sourceManager.getFileID(spellingLoc), m_sourceManager); } diff --git a/src/lib_cxx/data/parser/cxx/PreprocessorCallbacks.h b/src/lib_cxx/data/parser/cxx/PreprocessorCallbacks.h index 0349a8a1..9de0fb08 100644 --- a/src/lib_cxx/data/parser/cxx/PreprocessorCallbacks.h +++ b/src/lib_cxx/data/parser/cxx/PreprocessorCallbacks.h @@ -2,7 +2,7 @@ #define PREPROCESSOR_CALLBACKS_H #include -#include +#include #include "clang/Basic/SourceManager.h" #include "clang/Lex/MacroInfo.h" @@ -12,7 +12,6 @@ #include "utility/file/FilePath.h" class CanonicalFilePathCache; -class FileRegister; class ParserClient; struct ParseLocation; @@ -24,7 +23,6 @@ public: explicit PreprocessorCallbacks( clang::SourceManager& sourceManager, std::shared_ptr client, - std::shared_ptr fileRegister, std::shared_ptr canonicalFilePathCache); void FileChanged( @@ -54,14 +52,6 @@ public: ) override; private: - struct FileIdHash - { - size_t operator()(clang::FileID fileID) const - { - return fileID.getHashValue(); - } - }; - void onMacroUsage(const clang::Token& macroNameToken); ParseLocation getParseLocation(const clang::Token& macroNameToc) const; @@ -71,11 +61,12 @@ private: const clang::SourceManager& m_sourceManager; std::shared_ptr m_client; - std::shared_ptr m_fileRegister; std::shared_ptr m_canonicalFilePathCache; - std::unordered_map m_inProjectFileMap; FilePath m_currentPath; + bool m_currentPathIsProjectFile = false; + + std::set m_fileWasRecorded; }; #endif // PREPROCESSOR_CALLBACKS_H diff --git a/src/lib_cxx/data/parser/cxx/name_resolver/CxxDeclNameResolver.cpp b/src/lib_cxx/data/parser/cxx/name_resolver/CxxDeclNameResolver.cpp index 383c3cca..1c055065 100644 --- a/src/lib_cxx/data/parser/cxx/name_resolver/CxxDeclNameResolver.cpp +++ b/src/lib_cxx/data/parser/cxx/name_resolver/CxxDeclNameResolver.cpp @@ -215,7 +215,7 @@ std::shared_ptr CxxDeclNameResolver::getDeclName(const clang::Named else if (clang::isa(declaration)) { return std::make_shared( - std::move(declNameString), + std::move(declNameString), getTemplateParameterStringsOfPatrialSpecialitarion( clang::dyn_cast(declaration) ) @@ -431,24 +431,20 @@ std::shared_ptr CxxDeclNameResolver::getDeclName(const clang::Named std::wstring CxxDeclNameResolver::getTranslationUnitMainFileName(const clang::Decl* declaration) { const clang::SourceManager& sourceManager = declaration->getASTContext().getSourceManager(); - clang::FileID fileId = sourceManager.getMainFileID(); - if (fileId.isValid()) - { - const clang::FileEntry* fileEntry = sourceManager.getFileEntryForID(fileId); - return getCanonicalFilePathCache()->getCanonicalFilePath(fileEntry).fileName(); - } - return L""; + return getCanonicalFilePathCache()->getCanonicalFilePath(sourceManager.getMainFileID(), sourceManager).fileName(); } std::wstring CxxDeclNameResolver::getDeclarationFileName(const clang::Decl* declaration) { const clang::SourceManager& sourceManager = declaration->getASTContext().getSourceManager(); - const clang::FileEntry* fileEntry = sourceManager.getFileEntryForID(sourceManager.getFileID(declaration->getLocStart())); + const clang::FileID fileId = sourceManager.getFileID(declaration->getLocStart()); + const clang::FileEntry* fileEntry = sourceManager.getFileEntryForID(fileId); if (fileEntry != nullptr && fileEntry->isValid()) { - return getCanonicalFilePathCache()->getCanonicalFilePath(fileEntry).fileName(); + return getCanonicalFilePathCache()->getCanonicalFilePath(fileId, sourceManager).fileName(); } - return getCanonicalFilePathCache()->getCanonicalFilePath(utility::decodeFromUtf8(sourceManager.getPresumedLoc(declaration->getLocStart()).getFilename())).fileName(); + return getCanonicalFilePathCache()->getCanonicalFilePath( + utility::decodeFromUtf8(sourceManager.getPresumedLoc(declaration->getLocStart()).getFilename())).fileName(); } std::wstring CxxDeclNameResolver::getNameForAnonymousSymbol(const std::wstring& symbolKindName, const clang::Decl* declaration)