2018-04-06 23:14:32 +08:00
|
|
|
//===-- ASTMerge.cpp - AST Merging Frontend Action --------------*- C++ -*-===//
|
2010-02-10 03:21:46 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-02-10 03:21:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2018-04-11 02:53:28 +08:00
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
2010-02-10 03:21:46 +08:00
|
|
|
#include "clang/AST/ASTContext.h"
|
2010-02-10 06:37:58 +08:00
|
|
|
#include "clang/AST/ASTDiagnostic.h"
|
2010-02-10 03:21:46 +08:00
|
|
|
#include "clang/AST/ASTImporter.h"
|
2019-07-01 23:37:07 +08:00
|
|
|
#include "clang/AST/ASTImporterSharedState.h"
|
2010-04-06 07:52:57 +08:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
2010-02-10 03:21:46 +08:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2010-02-10 03:21:46 +08:00
|
|
|
return AdaptedAction->CreateASTConsumer(CI, InFile);
|
|
|
|
}
|
|
|
|
|
2017-06-09 09:36:10 +08:00
|
|
|
bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI) {
|
2010-02-10 03:21:46 +08:00
|
|
|
// FIXME: This is a hack. We need a better way to communicate the
|
|
|
|
// AST file, compiler instance, and file name than member variables
|
|
|
|
// of FrontendAction.
|
2012-01-21 00:28:04 +08:00
|
|
|
AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit());
|
2010-02-10 03:21:46 +08:00
|
|
|
AdaptedAction->setCompilerInstance(&CI);
|
2017-06-09 09:36:10 +08:00
|
|
|
return AdaptedAction->BeginSourceFileAction(CI);
|
2010-02-10 03:21:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTMergeAction::ExecuteAction() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
2010-02-11 01:16:49 +08:00
|
|
|
CI.getDiagnostics().getClient()->BeginSourceFile(
|
2013-05-04 06:58:43 +08:00
|
|
|
CI.getASTContext().getLangOpts());
|
2010-02-10 06:37:58 +08:00
|
|
|
CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
|
|
|
|
&CI.getASTContext());
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs>
|
2010-11-19 04:06:41 +08:00
|
|
|
DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
|
2019-07-01 23:37:07 +08:00
|
|
|
auto SharedState = std::make_shared<ASTImporterSharedState>(
|
2018-12-17 21:53:12 +08:00
|
|
|
*CI.getASTContext().getTranslationUnitDecl());
|
2010-02-10 03:21:46 +08:00
|
|
|
for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
|
2012-02-20 22:00:23 +08:00
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine>
|
2012-10-24 06:26:28 +08:00
|
|
|
Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
|
2013-05-04 06:58:43 +08:00
|
|
|
new ForwardingDiagnosticConsumer(
|
|
|
|
*CI.getDiagnostics().getClient()),
|
|
|
|
/*ShouldOwnClient=*/true));
|
2017-06-30 07:23:46 +08:00
|
|
|
std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
|
|
|
|
ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
|
|
|
|
CI.getFileSystemOpts(), false);
|
2015-06-21 02:53:08 +08:00
|
|
|
|
2010-02-10 03:21:46 +08:00
|
|
|
if (!Unit)
|
|
|
|
continue;
|
|
|
|
|
2018-12-17 21:53:12 +08:00
|
|
|
ASTImporter Importer(CI.getASTContext(), CI.getFileManager(),
|
|
|
|
Unit->getASTContext(), Unit->getFileManager(),
|
2019-07-01 23:37:07 +08:00
|
|
|
/*MinimalImport=*/false, SharedState);
|
2010-02-10 03:21:46 +08:00
|
|
|
|
|
|
|
TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
|
2014-03-08 03:56:05 +08:00
|
|
|
for (auto *D : TU->decls()) {
|
2010-02-16 08:04:46 +08:00
|
|
|
// Don't re-import __va_list_tag, __builtin_va_list.
|
2014-03-08 03:56:05 +08:00
|
|
|
if (const auto *ND = dyn_cast<NamedDecl>(D))
|
2010-02-16 08:04:46 +08:00
|
|
|
if (IdentifierInfo *II = ND->getIdentifier())
|
|
|
|
if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
|
|
|
|
continue;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
[ASTImporter] Use llvm::Expected and Error in the importer API
Summary:
This is the final phase of the refactoring towards using llvm::Expected
and llvm::Error in the ASTImporter API.
This involves the following:
- remove old Import functions which returned with a pointer,
- use the Import_New functions (which return with Err or Expected) everywhere
and handle their return value
- rename Import_New functions to Import
This affects both Clang and LLDB.
Reviewers: shafik, teemperor, aprantl, a_sidorin, balazske, a.sidorin
Subscribers: rnkovacs, dkrupp, Szelethus, gamesh411, cfe-commits, lldb-commits
Tags: #clang, #lldb
Differential Revision: https://reviews.llvm.org/D61438
llvm-svn: 360760
2019-05-15 18:29:48 +08:00
|
|
|
llvm::Expected<Decl *> ToDOrError = Importer.Import(D);
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2019-04-08 21:59:15 +08:00
|
|
|
if (ToDOrError) {
|
|
|
|
DeclGroupRef DGR(*ToDOrError);
|
2015-04-29 02:24:12 +08:00
|
|
|
CI.getASTConsumer().HandleTopLevelDecl(DGR);
|
2019-04-08 21:59:15 +08:00
|
|
|
} else {
|
|
|
|
llvm::consumeError(ToDOrError.takeError());
|
2015-04-29 02:24:12 +08:00
|
|
|
}
|
2010-02-10 03:21:46 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-02-11 01:16:49 +08:00
|
|
|
AdaptedAction->ExecuteAction();
|
|
|
|
CI.getDiagnostics().getClient()->EndSourceFile();
|
2010-02-10 03:21:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void ASTMergeAction::EndSourceFileAction() {
|
|
|
|
return AdaptedAction->EndSourceFileAction();
|
|
|
|
}
|
|
|
|
|
2016-02-08 03:28:36 +08:00
|
|
|
ASTMergeAction::ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction,
|
2012-02-04 09:36:04 +08:00
|
|
|
ArrayRef<std::string> ASTFiles)
|
2016-02-08 03:28:36 +08:00
|
|
|
: AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
|
2010-02-10 03:21:46 +08:00
|
|
|
assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
|
|
|
|
}
|
|
|
|
|
2018-07-31 03:24:48 +08:00
|
|
|
ASTMergeAction::~ASTMergeAction() {
|
2010-02-10 03:21:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ASTMergeAction::usesPreprocessorOnly() const {
|
|
|
|
return AdaptedAction->usesPreprocessorOnly();
|
|
|
|
}
|
|
|
|
|
2011-08-26 06:30:56 +08:00
|
|
|
TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
|
|
|
|
return AdaptedAction->getTranslationUnitKind();
|
2010-02-10 03:21:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ASTMergeAction::hasPCHSupport() const {
|
|
|
|
return AdaptedAction->hasPCHSupport();
|
|
|
|
}
|
|
|
|
|
2010-06-08 07:24:43 +08:00
|
|
|
bool ASTMergeAction::hasASTFileSupport() const {
|
|
|
|
return AdaptedAction->hasASTFileSupport();
|
2010-02-10 03:21:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ASTMergeAction::hasCodeCompletionSupport() const {
|
|
|
|
return AdaptedAction->hasCodeCompletionSupport();
|
|
|
|
}
|