diff options
author | Marco Bubke <marco.bubke@qt.io> | 2018-10-18 17:22:47 +0200 |
---|---|---|
committer | Marco Bubke <marco.bubke@qt.io> | 2018-10-25 13:35:02 +0000 |
commit | ee2122be702dd3ef502c679535296d7fadad01a4 (patch) | |
tree | 4a7825cc601b3781997a73ed505bcee0ecdf5f25 /src/tools | |
parent | e571220663a013fb7fd6b86e7066915eedae89a4 (diff) | |
download | qt-creator-ee2122be702dd3ef502c679535296d7fadad01a4.tar.gz |
ClangPchManager: Add build dependencies to include collector
It's a first step but we have to refactor the interface later.
Task-number: QTCREATORBUG-21379
Change-Id: Idda666bcaec950203f001c993c54a926779527e0
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
Diffstat (limited to 'src/tools')
9 files changed, 222 insertions, 60 deletions
diff --git a/src/tools/clangpchmanagerbackend/source/collectincludesaction.h b/src/tools/clangpchmanagerbackend/source/collectincludesaction.h index 3a0f00b31c..17498028b8 100644 --- a/src/tools/clangpchmanagerbackend/source/collectincludesaction.h +++ b/src/tools/clangpchmanagerbackend/source/collectincludesaction.h @@ -44,13 +44,23 @@ public: FilePathIds &topsSystemIncludeIds, const FilePathCachingInterface &filePathCache, std::vector<uint> &excludedIncludeUID, - std::vector<uint> &alreadyIncludedFileUIDs) + std::vector<uint> &alreadyIncludedFileUIDs, + UsedMacros &usedMacros, + SourcesManager &sourcesManager, + SourceDependencies &sourceDependencies, + FilePathIds &sourceFiles, + FileStatuses &fileStatuses) : m_includeIds(includeIds), m_topIncludeIds(topIncludeIds), m_topsSystemIncludeIds(topsSystemIncludeIds), m_filePathCache(filePathCache), m_excludedIncludeUID(excludedIncludeUID), - m_alreadyIncludedFileUIDs(alreadyIncludedFileUIDs) + m_alreadyIncludedFileUIDs(alreadyIncludedFileUIDs), + m_usedMacros(usedMacros), + m_sourcesManager(sourcesManager), + m_sourceDependencies(sourceDependencies), + m_sourceFiles(sourceFiles), + m_fileStatuses(fileStatuses) { } @@ -68,7 +78,13 @@ public: m_filePathCache, m_excludedIncludeUID, m_alreadyIncludedFileUIDs, - compilerInstance.getSourceManager()); + compilerInstance.getSourceManager(), + m_usedMacros, + m_sourcesManager, + compilerInstance.getPreprocessorPtr(), + m_sourceDependencies, + m_sourceFiles, + m_fileStatuses); preprocessor.addPPCallbacks(std::unique_ptr<clang::PPCallbacks>(macroPreprocessorCallbacks)); @@ -90,6 +106,11 @@ private: const FilePathCachingInterface &m_filePathCache; std::vector<uint> &m_excludedIncludeUID; std::vector<uint> &m_alreadyIncludedFileUIDs; + UsedMacros &m_usedMacros; + SourcesManager &m_sourcesManager; + SourceDependencies &m_sourceDependencies; + FilePathIds &m_sourceFiles; + FileStatuses &m_fileStatuses; }; } // namespace ClangBackEnd diff --git a/src/tools/clangpchmanagerbackend/source/collectincludespreprocessorcallbacks.h b/src/tools/clangpchmanagerbackend/source/collectincludespreprocessorcallbacks.h index 4eeaa40877..5522fa7bce 100644 --- a/src/tools/clangpchmanagerbackend/source/collectincludespreprocessorcallbacks.h +++ b/src/tools/clangpchmanagerbackend/source/collectincludespreprocessorcallbacks.h @@ -25,18 +25,14 @@ #pragma once +#include "collectmacrospreprocessorcallbacks.h" #include "sourcelocationsutils.h" + #include <filepathcachinginterface.h> #include <filepathid.h> #include <utils/smallstringvector.h> -#include <clang/Basic/SourceManager.h> -#include <clang/Lex/MacroInfo.h> -#include <clang/Lex/HeaderSearch.h> -#include <clang/Lex/PPCallbacks.h> -#include <clang/Lex/Preprocessor.h> - #include <QFile> #include <QDir> #include <QTemporaryDir> @@ -45,7 +41,8 @@ namespace ClangBackEnd { -class CollectIncludesPreprocessorCallbacks final : public clang::PPCallbacks +class CollectIncludesPreprocessorCallbacks final : public clang::PPCallbacks, + public CollectUsedMacrosAndSourcesPreprocessorCallbacksBase { public: CollectIncludesPreprocessorCallbacks(FilePathIds &includeIds, @@ -54,16 +51,44 @@ public: const FilePathCachingInterface &filePathCache, const std::vector<uint> &excludedIncludeUID, std::vector<uint> &alreadyIncludedFileUIDs, - clang::SourceManager &sourceManager) - : m_includeIds(includeIds), + const clang::SourceManager &sourceManager, + UsedMacros &usedMacros, + SourcesManager &sourcesManager, + std::shared_ptr<clang::Preprocessor> preprocessor, + SourceDependencies &sourceDependencies, + FilePathIds &sourceFiles, + FileStatuses &fileStatuses) + : CollectUsedMacrosAndSourcesPreprocessorCallbacksBase(usedMacros, + filePathCache, + sourceManager, + sourcesManager, + preprocessor, + sourceDependencies, + sourceFiles, + fileStatuses), + m_includeIds(includeIds), m_topIncludeIds(topIncludeIds), m_topsSystemIncludeIds(topsSystemIncludeIds), - m_filePathCache(filePathCache), m_excludedIncludeUID(excludedIncludeUID), - m_alreadyIncludedFileUIDs(alreadyIncludedFileUIDs), - m_sourceManager(sourceManager) + m_alreadyIncludedFileUIDs(alreadyIncludedFileUIDs) {} + void FileChanged(clang::SourceLocation sourceLocation, + clang::PPCallbacks::FileChangeReason reason, + clang::SrcMgr::CharacteristicKind, + clang::FileID) override + { + if (reason == clang::PPCallbacks::EnterFile) + { + const clang::FileEntry *fileEntry = m_sourceManager->getFileEntryForID( + m_sourceManager->getFileID(sourceLocation)); + if (fileEntry) { + addFileStatus(fileEntry); + addSourceFile(fileEntry); + } + } + } + void InclusionDirective(clang::SourceLocation hashLocation, const clang::Token &/*includeToken*/, llvm::StringRef /*fileName*/, @@ -76,8 +101,9 @@ public: clang::SrcMgr::CharacteristicKind fileType) override { if (!m_skipInclude && file) { + addSourceDependency(file, hashLocation); auto fileUID = file->getUID(); - auto sourceFileUID = m_sourceManager.getFileEntryForID(m_sourceManager.getFileID(hashLocation))->getUID(); + auto sourceFileUID = m_sourceManager->getFileEntryForID(m_sourceManager->getFileID(hashLocation))->getUID(); if (isNotInExcludedIncludeUID(fileUID)) { auto notAlreadyIncluded = isNotAlreadyIncluded(fileUID); if (notAlreadyIncluded.first) { @@ -117,14 +143,41 @@ public: return true; } - bool isSystem(clang::SrcMgr::CharacteristicKind kind) + + void Ifndef(clang::SourceLocation, + const clang::Token ¯oNameToken, + const clang::MacroDefinition ¯oDefinition) override + { + addUsedMacro(macroNameToken, macroDefinition); + } + + void Ifdef(clang::SourceLocation, + const clang::Token ¯oNameToken, + const clang::MacroDefinition ¯oDefinition) override + { + addUsedMacro( macroNameToken, macroDefinition); + } + + void Defined(const clang::Token ¯oNameToken, + const clang::MacroDefinition ¯oDefinition, + clang::SourceRange) override + { + addUsedMacro(macroNameToken, macroDefinition); + } + + void MacroExpands(const clang::Token ¯oNameToken, + const clang::MacroDefinition ¯oDefinition, + clang::SourceRange, + const clang::MacroArgs *) override { - return kind != clang::SrcMgr::C_User && kind != clang::SrcMgr::C_User_ModuleMap; + addUsedMacro(macroNameToken, macroDefinition); } - bool isInSystemHeader(clang::SourceLocation location) + void EndOfMainFile() override { - return m_sourceManager.isInSystemHeader(location); + filterOutHeaderGuards(); + mergeUsedMacros(); + m_sourcesManager.updateModifiedTimeStamps(); } void ensureDirectory(const QString &directory, const QString &fileName) @@ -175,10 +228,8 @@ private: FilePathIds &m_includeIds; FilePathIds &m_topIncludeIds; FilePathIds &m_topsSystemIncludeIds; - const FilePathCachingInterface &m_filePathCache; const std::vector<uint> &m_excludedIncludeUID; std::vector<uint> &m_alreadyIncludedFileUIDs; - clang::SourceManager &m_sourceManager; bool m_skipInclude = false; }; diff --git a/src/tools/clangpchmanagerbackend/source/collectincludestoolaction.h b/src/tools/clangpchmanagerbackend/source/collectincludestoolaction.h index 6130a049ad..7820b095fc 100644 --- a/src/tools/clangpchmanagerbackend/source/collectincludestoolaction.h +++ b/src/tools/clangpchmanagerbackend/source/collectincludestoolaction.h @@ -41,12 +41,22 @@ public: FilePathIds &topIncludeIds, FilePathIds &topsSystemIncludeIds, const FilePathCachingInterface &filePathCache, - const Utils::PathStringVector &excludedIncludes) + const Utils::PathStringVector &excludedIncludes, + UsedMacros &usedMacros, + SourcesManager &sourcesManager, + SourceDependencies &sourceDependencies, + FilePathIds &sourceFiles, + FileStatuses &fileStatuses) : m_includeIds(includeIds), m_topIncludeIds(topIncludeIds), m_topsSystemIncludeIds(topsSystemIncludeIds), m_filePathCache(filePathCache), - m_excludedIncludes(excludedIncludes) + m_excludedIncludes(excludedIncludes), + m_usedMacros(usedMacros), + m_sourcesManager(sourcesManager), + m_sourceDependencies(sourceDependencies), + m_sourceFiles(sourceFiles), + m_fileStatuses(fileStatuses) {} @@ -71,7 +81,12 @@ public: m_topsSystemIncludeIds, m_filePathCache, m_excludedIncludeUIDs, - m_alreadyIncludedFileUIDs); + m_alreadyIncludedFileUIDs, + m_usedMacros, + m_sourcesManager, + m_sourceDependencies, + m_sourceFiles, + m_fileStatuses); } std::vector<uint> generateExcludedIncludeFileUIDs(clang::FileManager &fileManager) const @@ -99,6 +114,11 @@ private: FilePathIds &m_topsSystemIncludeIds; const FilePathCachingInterface &m_filePathCache; const Utils::PathStringVector &m_excludedIncludes; + UsedMacros &m_usedMacros; + SourcesManager &m_sourcesManager; + SourceDependencies &m_sourceDependencies; + FilePathIds &m_sourceFiles; + FileStatuses &m_fileStatuses; }; } // namespace ClangBackEnd diff --git a/src/tools/clangpchmanagerbackend/source/collectusedmacrosandsourcespreprocessorcallbacks.h b/src/tools/clangpchmanagerbackend/source/collectusedmacrosandsourcespreprocessorcallbacks.h index 48bd0ddec9..9ecd4964a2 100644 --- a/src/tools/clangpchmanagerbackend/source/collectusedmacrosandsourcespreprocessorcallbacks.h +++ b/src/tools/clangpchmanagerbackend/source/collectusedmacrosandsourcespreprocessorcallbacks.h @@ -52,7 +52,7 @@ class CollectUsedMacrosAndSourcesPreprocessorCallbacksBase : public SymbolsVisit { public: CollectUsedMacrosAndSourcesPreprocessorCallbacksBase(UsedMacros &usedMacros, - FilePathCachingInterface &filePathCache, + const FilePathCachingInterface &filePathCache, const clang::SourceManager &sourceManager, SourcesManager &sourcesManager, std::shared_ptr<clang::Preprocessor> preprocessor, diff --git a/src/tools/clangpchmanagerbackend/source/includecollector.cpp b/src/tools/clangpchmanagerbackend/source/includecollector.cpp index 338491cffc..6ca7623cc3 100644 --- a/src/tools/clangpchmanagerbackend/source/includecollector.cpp +++ b/src/tools/clangpchmanagerbackend/source/includecollector.cpp @@ -33,21 +33,21 @@ namespace ClangBackEnd { -IncludeCollector::IncludeCollector(const FilePathCachingInterface &filePathCache) - : m_filePathCache(filePathCache) +void IncludeCollector::collect() { -} - -void IncludeCollector::collectIncludes() -{ - clang::tooling::ClangTool tool = createTool(); + clang::tooling::ClangTool tool = m_clangTool.createTool(); auto action = std::unique_ptr<CollectIncludesToolAction>( new CollectIncludesToolAction(m_includeIds, m_topIncludeIds, m_topsSystemIncludeIds, m_filePathCache, - m_excludedIncludes)); + m_excludedIncludes, + m_usedMacros, + m_sourcesManager, + m_sourceDependencies, + m_sourceFiles, + m_fileStatuses)); tool.run(action.get()); } @@ -71,25 +71,33 @@ void IncludeCollector::setExcludedIncludes(Utils::PathStringVector &&excludedInc #endif } -FilePathIds IncludeCollector::takeIncludeIds() +void IncludeCollector::addFiles(const FilePathIds &filePathIds, + const Utils::SmallStringVector &arguments) { - std::sort(m_includeIds.begin(), m_includeIds.end()); - - return std::move(m_includeIds); + m_clangTool.addFiles(m_filePathCache.filePaths(filePathIds), arguments); + m_sourceFiles.insert(m_sourceFiles.end(), filePathIds.begin(), filePathIds.end()); } -FilePathIds IncludeCollector::takeTopIncludeIds() +void IncludeCollector::addFile(FilePathId filePathId, const Utils::SmallStringVector &arguments) { - std::sort(m_topIncludeIds.begin(), m_topIncludeIds.end()); - - return std::move(m_topIncludeIds); + addFiles({filePathId}, arguments); } -FilePathIds IncludeCollector::takeTopsSystemIncludeIds() +void IncludeCollector::addFile(FilePath filePath, + const FilePathIds &sourceFileIds, + const Utils::SmallStringVector &arguments) { - return std::move(m_topsSystemIncludeIds); + m_clangTool.addFiles({filePath}, arguments); + m_sourceFiles.insert(m_sourceFiles.end(), sourceFileIds.begin(), sourceFileIds.end()); } - +void IncludeCollector::clear() +{ + m_clangTool = ClangTool(); + m_usedMacros.clear(); + m_sourceFiles.clear(); + m_fileStatuses.clear(); + m_sourceDependencies.clear(); +} } // namespace ClangBackEnd diff --git a/src/tools/clangpchmanagerbackend/source/includecollector.h b/src/tools/clangpchmanagerbackend/source/includecollector.h index 2d54916cd0..f97a4901e7 100644 --- a/src/tools/clangpchmanagerbackend/source/includecollector.h +++ b/src/tools/clangpchmanagerbackend/source/includecollector.h @@ -26,6 +26,10 @@ #pragma once #include <clangtool.h> +#include <filestatus.h> +#include <sourcedependency.h> +#include <sourcesmanager.h> +#include <usedmacro.h> #include <filepathcachingfwd.h> @@ -34,22 +38,77 @@ namespace ClangBackEnd { class IncludeCollector : public ClangTool { public: - IncludeCollector(const FilePathCachingInterface &filePathCache); + IncludeCollector(const FilePathCachingInterface &filePathCache) + : m_filePathCache(filePathCache) + { + } - void collectIncludes(); + void collect(); void setExcludedIncludes(Utils::PathStringVector &&excludedIncludes); + void addFiles(const FilePathIds &filePathIds, + const Utils::SmallStringVector &arguments); + void addFile(FilePathId filePathId, + const Utils::SmallStringVector &arguments); + void addFile(FilePath filePath, + const FilePathIds &sourceFileIds, + const Utils::SmallStringVector &arguments); - FilePathIds takeIncludeIds(); - FilePathIds takeTopIncludeIds(); - FilePathIds takeTopsSystemIncludeIds(); + void clear(); + + const FileStatuses &fileStatuses() const + { + return m_fileStatuses; + } + + const FilePathIds &sourceFiles() const + { + return m_sourceFiles; + } + + const UsedMacros &usedMacros() const + { + return m_usedMacros; + } + + const SourceDependencies &sourceDependencies() const + { + return m_sourceDependencies; + } + + FilePathIds takeIncludeIds() + { + std::sort(m_includeIds.begin(), m_includeIds.end()); + + return std::move(m_includeIds); + } + + FilePathIds takeTopIncludeIds() + { + std::sort(m_topIncludeIds.begin(), m_topIncludeIds.end()); + + return std::move(m_topIncludeIds); + } + + FilePathIds takeTopsSystemIncludeIds() + { + std::sort(m_topsSystemIncludeIds.begin(), m_topsSystemIncludeIds.end()); + + return std::move(m_topsSystemIncludeIds); + } private: + ClangTool m_clangTool; Utils::PathStringVector m_excludedIncludes; FilePathIds m_includeIds; FilePathIds m_topIncludeIds; FilePathIds m_topsSystemIncludeIds; Utils::SmallStringVector m_directories; + SourcesManager m_sourcesManager; + UsedMacros m_usedMacros; + FilePathIds m_sourceFiles; + SourceDependencies m_sourceDependencies; + FileStatuses m_fileStatuses; const FilePathCachingInterface &m_filePathCache; }; diff --git a/src/tools/clangpchmanagerbackend/source/pchcreator.cpp b/src/tools/clangpchmanagerbackend/source/pchcreator.cpp index dda93d434d..51e0f129fe 100644 --- a/src/tools/clangpchmanagerbackend/source/pchcreator.cpp +++ b/src/tools/clangpchmanagerbackend/source/pchcreator.cpp @@ -243,21 +243,17 @@ PchCreatorIncludes PchCreator::generateProjectPartPchIncludes( Utils::SmallString jointedFilePath = generateProjectPartSourceFilePath(projectPart); auto jointFile = generateFileWithContent(jointedFilePath, jointedFileContent); Utils::SmallStringVector arguments = generateProjectPartCommandLine(projectPart); - arguments.push_back(jointedFilePath); FilePath filePath{Utils::PathString(jointedFilePath)}; IncludeCollector collector(m_filePathCache); collector.setExcludedIncludes(generateProjectPartSourcePaths(projectPart)); - collector.addFile(std::string(filePath.directory()), - std::string(filePath.name()), - {}, - arguments); + collector.addFile(filePath, projectPart.sourcePathIds, arguments); collector.addUnsavedFiles(m_unsavedFiles); - collector.collectIncludes(); + collector.collect(); jointFile->remove(); diff --git a/src/tools/clangrefactoringbackend/source/clangtool.cpp b/src/tools/clangrefactoringbackend/source/clangtool.cpp index 18f4e4cdcf..abee158e49 100644 --- a/src/tools/clangrefactoringbackend/source/clangtool.cpp +++ b/src/tools/clangrefactoringbackend/source/clangtool.cpp @@ -25,6 +25,8 @@ #include "clangtool.h" +#include <iostream> + namespace ClangBackEnd { namespace { diff --git a/src/tools/clangrefactoringbackend/source/symbolsvisitorbase.h b/src/tools/clangrefactoringbackend/source/symbolsvisitorbase.h index 1e4c6dcdc3..161acbe779 100644 --- a/src/tools/clangrefactoringbackend/source/symbolsvisitorbase.h +++ b/src/tools/clangrefactoringbackend/source/symbolsvisitorbase.h @@ -44,7 +44,7 @@ namespace ClangBackEnd { class SymbolsVisitorBase { public: - SymbolsVisitorBase(FilePathCachingInterface &filePathCache, + SymbolsVisitorBase(const FilePathCachingInterface &filePathCache, const clang::SourceManager *sourceManager, SourcesManager &sourcesManager) : m_filePathCache(filePathCache), @@ -152,8 +152,13 @@ public: bool isInSystemHeader(clang::FileID fileId) const { - return clang::SrcMgr::isSystem( - m_sourceManager->getSLocEntry(fileId).getFile().getFileCharacteristic()); + return isSystem(m_sourceManager->getSLocEntry(fileId).getFile().getFileCharacteristic()); + } + + static + bool isSystem(clang::SrcMgr::CharacteristicKind kind) + { + return clang::SrcMgr::isSystem(kind); } void clear() @@ -163,7 +168,7 @@ public: protected: std::vector<FilePathId> m_filePathIndices; - FilePathCachingInterface &m_filePathCache; + const FilePathCachingInterface &m_filePathCache; const clang::SourceManager *m_sourceManager = nullptr; SourcesManager &m_sourcesManager; }; |