ClangRefactoring: Don't update the database if something went wrong

We can get an compile error. In that case we should not update the
database. In the future we should have a mechanism to report about the
database state.

Task-number: QTCREATORBUG-21949
Change-Id: I203346d536b007171f7bf255047409431c44a85a
Reviewed-by: Ivan Donchevskii <ivan.donchevskii@qt.io>
This commit is contained in:
Marco Bubke
2019-02-07 12:20:51 +01:00
parent c52c531c3f
commit 97ec6b9ad2
9 changed files with 154 additions and 56 deletions

View File

@@ -45,8 +45,6 @@ bool CollectMacrosSourceFileCallbacks::handleBeginSource(clang::CompilerInstance
compilerInstance.getPreprocessorPtr(),
m_sourcesManager);
compilerInstance.getLangOpts().DelayedTemplateParsing = false;
compilerInstance.getPreprocessorPtr()->SetSuppressIncludeNotFoundError(true);
compilerInstance.getPreprocessorPtr()->addPPCallbacks(std::move(callbacks));
return true;

View File

@@ -34,6 +34,7 @@
#include <filepathcachingfwd.h>
#include <clang/Frontend/CompilerInstance.h>
#include <clang/Frontend/FrontendAction.h>
#include <clang/Index/IndexingAction.h>
@@ -52,19 +53,22 @@ public:
std::unique_ptr<clang::ASTConsumer> newASTConsumer(clang::CompilerInstance &compilerInstance,
llvm::StringRef inFile);
private:
class WrappedIndexAction : public clang::WrapperFrontendAction
class WrappedIndexAction final : public clang::WrapperFrontendAction
{
public:
WrappedIndexAction(std::shared_ptr<clang::index::IndexDataConsumer> m_indexDataConsumer,
WrappedIndexAction(std::shared_ptr<clang::index::IndexDataConsumer> indexDataConsumer,
clang::index::IndexingOptions indexingOptions)
: clang::WrapperFrontendAction(
clang::index::createIndexingAction(m_indexDataConsumer, indexingOptions, nullptr))
clang::index::createIndexingAction(indexDataConsumer, indexingOptions, nullptr))
{}
std::unique_ptr<clang::ASTConsumer>
CreateASTConsumer(clang::CompilerInstance &compilerInstance,
llvm::StringRef inFile) override
{
compilerInstance.getPreprocessor().SetSuppressIncludeNotFoundError(true);
compilerInstance.getLangOpts().DelayedTemplateParsing = false;
return WrapperFrontendAction::CreateASTConsumer(compilerInstance, inFile);
}
};

View File

@@ -124,33 +124,23 @@ bool IndexDataConsumer::skipSymbol(clang::FileID fileId, clang::index::SymbolRol
return isParsedDeclaration || isParsedReference;
}
bool IndexDataConsumer::handleDeclOccurence(const clang::Decl *declaration,
clang::index::SymbolRoleSet symbolRoles,
llvm::ArrayRef<clang::index::SymbolRelation> symbolRelations,
#if LLVM_VERSION_MAJOR >= 7
clang::SourceLocation sourceLocation,
#else
clang::FileID fileId,
unsigned offset,
#endif
IndexDataConsumer::ASTNodeInfo astNodeInfo)
bool IndexDataConsumer::handleDeclOccurence(
const clang::Decl *declaration,
clang::index::SymbolRoleSet symbolRoles,
llvm::ArrayRef<clang::index::SymbolRelation> /*symbolRelations*/,
clang::SourceLocation sourceLocation,
IndexDataConsumer::ASTNodeInfo /*astNodeInfo*/)
{
const auto *namedDeclaration = clang::dyn_cast<clang::NamedDecl>(declaration);
if (namedDeclaration) {
if (!namedDeclaration->getIdentifier())
return true;
#if LLVM_VERSION_MAJOR >= 7
if (skipSymbol(m_sourceManager->getFileID(sourceLocation), symbolRoles))
#else
if (skipSymbol(fileId, symbolRoles))
#endif
return true;
SymbolIndex globalId = toSymbolIndex(declaration->getCanonicalDecl());
#if LLVM_VERSION_MAJOR < 7
clang::SourceLocation sourceLocation = m_sourceManager->getLocForStartOfFile(fileId).getLocWithOffset(offset);
#endif
auto found = m_symbolEntries.find(globalId);
if (found == m_symbolEntries.end()) {

View File

@@ -55,12 +55,7 @@ public:
bool handleDeclOccurence(const clang::Decl *declaration,
clang::index::SymbolRoleSet symbolRoles,
llvm::ArrayRef<clang::index::SymbolRelation> symbolRelations,
#if LLVM_VERSION_MAJOR >= 7
clang::SourceLocation sourceLocation,
#else
clang::FileID fileId,
unsigned offset,
#endif
ASTNodeInfo astNodeInfo) override;
private:

View File

@@ -117,27 +117,32 @@ void SymbolIndexer::updateProjectPart(ProjectPartContainer &&projectPart)
std::vector<SymbolIndexerTask> symbolIndexerTask;
symbolIndexerTask.reserve(projectPart.sourcePathIds.size());
for (FilePathId sourcePathId : projectPart.sourcePathIds) {
auto indexing = [projectPartId, arguments = commandLineBuilder.commandLine, sourcePathId, this](
SymbolsCollectorInterface &symbolsCollector) {
auto indexing = [projectPartId,
arguments = commandLineBuilder.commandLine,
sourcePathId,
this](SymbolsCollectorInterface &symbolsCollector) {
symbolsCollector.setFile(sourcePathId, arguments);
symbolsCollector.collectSymbols();
bool success = symbolsCollector.collectSymbols();
Sqlite::ImmediateTransaction transaction{m_transactionInterface};
if (success) {
Sqlite::ImmediateTransaction transaction{m_transactionInterface};
m_symbolStorage.addSymbolsAndSourceLocations(symbolsCollector.symbols(),
symbolsCollector.sourceLocations());
m_symbolStorage.addSymbolsAndSourceLocations(symbolsCollector.symbols(),
symbolsCollector.sourceLocations());
m_symbolStorage.updateProjectPartSources(projectPartId, symbolsCollector.sourceFiles());
m_symbolStorage.updateProjectPartSources(projectPartId,
symbolsCollector.sourceFiles());
m_buildDependencyStorage.insertOrUpdateUsedMacros(symbolsCollector.usedMacros());
m_buildDependencyStorage.insertOrUpdateUsedMacros(symbolsCollector.usedMacros());
m_buildDependencyStorage.insertFileStatuses(symbolsCollector.fileStatuses());
m_buildDependencyStorage.insertFileStatuses(symbolsCollector.fileStatuses());
m_buildDependencyStorage.insertOrUpdateSourceDependencies(
symbolsCollector.sourceDependencies());
m_buildDependencyStorage.insertOrUpdateSourceDependencies(
symbolsCollector.sourceDependencies());
transaction.commit();
transaction.commit();
}
};
symbolIndexerTask.emplace_back(sourcePathId, projectPartId, std::move(indexing));
@@ -185,27 +190,31 @@ void SymbolIndexer::updateChangedPath(FilePathId filePathId,
CommandLineBuilder<ProjectPartArtefact, Utils::SmallStringVector>
builder{artefact, artefact.toolChainArguments, InputFileType::Source, {}, {}, pchPath};
auto indexing = [projectPartId = artefact.projectPartId, arguments=builder.commandLine, filePathId, this](
SymbolsCollectorInterface &symbolsCollector) {
auto indexing = [projectPartId = artefact.projectPartId,
arguments = builder.commandLine,
filePathId,
this](SymbolsCollectorInterface &symbolsCollector) {
symbolsCollector.setFile(filePathId, arguments);
symbolsCollector.collectSymbols();
bool success = symbolsCollector.collectSymbols();
Sqlite::ImmediateTransaction transaction{m_transactionInterface};
if (success) {
Sqlite::ImmediateTransaction transaction{m_transactionInterface};
m_symbolStorage.addSymbolsAndSourceLocations(symbolsCollector.symbols(),
symbolsCollector.sourceLocations());
m_symbolStorage.addSymbolsAndSourceLocations(symbolsCollector.symbols(),
symbolsCollector.sourceLocations());
m_symbolStorage.updateProjectPartSources(projectPartId, symbolsCollector.sourceFiles());
m_symbolStorage.updateProjectPartSources(projectPartId, symbolsCollector.sourceFiles());
m_buildDependencyStorage.insertOrUpdateUsedMacros(symbolsCollector.usedMacros());
m_buildDependencyStorage.insertOrUpdateUsedMacros(symbolsCollector.usedMacros());
m_buildDependencyStorage.insertFileStatuses(symbolsCollector.fileStatuses());
m_buildDependencyStorage.insertFileStatuses(symbolsCollector.fileStatuses());
m_buildDependencyStorage.insertOrUpdateSourceDependencies(
symbolsCollector.sourceDependencies());
m_buildDependencyStorage.insertOrUpdateSourceDependencies(
symbolsCollector.sourceDependencies());
transaction.commit();
transaction.commit();
}
};
symbolIndexerTask.emplace_back(filePathId, optionalArtefact->projectPartId, std::move(indexing));

View File

@@ -121,12 +121,14 @@ newFrontendActionFactory(Factory *consumerFactory,
new FrontendActionFactoryAdapter(consumerFactory, sourceFileCallbacks));
}
void SymbolsCollector::collectSymbols()
bool SymbolsCollector::collectSymbols()
{
auto tool = m_clangTool.createTool();
tool.run(ClangBackEnd::newFrontendActionFactory(&m_collectSymbolsAction,
&m_collectMacrosSourceFileCallbacks).get());
auto actionFactory = ClangBackEnd::newFrontendActionFactory(&m_collectSymbolsAction,
&m_collectMacrosSourceFileCallbacks);
return tool.run(actionFactory.get()) != 1;
}
void SymbolsCollector::doInMainThreadAfterFinished()

View File

@@ -52,7 +52,7 @@ public:
void clear() override;
void collectSymbols() override;
bool collectSymbols() override;
void doInMainThreadAfterFinished() override;

View File

@@ -44,7 +44,7 @@ class SymbolsCollectorInterface : public ProcessorInterface
{
public:
virtual void setFile(FilePathId filePathId, const Utils::SmallStringVector &arguments) = 0;
virtual void collectSymbols() = 0;
virtual bool collectSymbols() = 0;
virtual const SymbolEntries &symbols() const = 0;
virtual const SourceLocationEntries &sourceLocations() const = 0;