2018-07-28 07:06:01 -04:00
|
|
|
//===-- ASTMerge.cpp - AST Merging Frontend Action --------------*- C++ -*-===//
|
2010-02-16 04:31:36 -05:00
|
|
|
//
|
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
|
//
|
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
|
//
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
#include "clang/Frontend/ASTUnit.h"
|
|
|
|
|
#include "clang/AST/ASTContext.h"
|
|
|
|
|
#include "clang/AST/ASTDiagnostic.h"
|
|
|
|
|
#include "clang/AST/ASTImporter.h"
|
2019-01-19 05:04:05 -05:00
|
|
|
#include "clang/AST/ASTImporterLookupTable.h"
|
2010-04-06 11:53:59 -04:00
|
|
|
#include "clang/Basic/Diagnostic.h"
|
2013-04-08 14:45:10 -04:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
2010-02-16 04:31:36 -05:00
|
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
2015-01-18 11:23:48 -05:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
|
ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2010-02-16 04:31:36 -05:00
|
|
|
return AdaptedAction->CreateASTConsumer(CI, InFile);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-10 09:44:22 -04:00
|
|
|
bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI) {
|
2010-02-16 04:31:36 -05: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-04-14 10:01:31 -04:00
|
|
|
AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit());
|
2010-02-16 04:31:36 -05:00
|
|
|
AdaptedAction->setCompilerInstance(&CI);
|
2017-06-10 09:44:22 -04:00
|
|
|
return AdaptedAction->BeginSourceFileAction(CI);
|
2010-02-16 04:31:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ASTMergeAction::ExecuteAction() {
|
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
|
CI.getDiagnostics().getClient()->BeginSourceFile(
|
2013-06-10 16:45:12 -04:00
|
|
|
CI.getASTContext().getLangOpts());
|
2010-02-16 04:31:36 -05:00
|
|
|
CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
|
|
|
|
|
&CI.getASTContext());
|
2012-04-14 10:01:31 -04:00
|
|
|
IntrusiveRefCntPtr<DiagnosticIDs>
|
2011-02-20 08:06:31 -05:00
|
|
|
DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
|
2019-01-19 05:04:05 -05:00
|
|
|
ASTImporterLookupTable LookupTable(
|
|
|
|
|
*CI.getASTContext().getTranslationUnitDecl());
|
2010-02-16 04:31:36 -05:00
|
|
|
for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
|
2012-04-14 10:01:31 -04:00
|
|
|
IntrusiveRefCntPtr<DiagnosticsEngine>
|
2012-12-02 08:20:44 -05:00
|
|
|
Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
|
2013-06-10 16:45:12 -04:00
|
|
|
new ForwardingDiagnosticConsumer(
|
|
|
|
|
*CI.getDiagnostics().getClient()),
|
|
|
|
|
/*ShouldOwnClient=*/true));
|
2017-07-01 09:24:05 -04:00
|
|
|
std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
|
|
|
|
|
ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
|
|
|
|
|
CI.getFileSystemOpts(), false);
|
2015-06-21 10:00:56 -04:00
|
|
|
|
2010-02-16 04:31:36 -05:00
|
|
|
if (!Unit)
|
|
|
|
|
continue;
|
|
|
|
|
|
2019-01-19 05:04:05 -05:00
|
|
|
ASTImporter Importer(CI.getASTContext(), CI.getFileManager(),
|
|
|
|
|
Unit->getASTContext(), Unit->getFileManager(),
|
|
|
|
|
/*MinimalImport=*/false, &LookupTable);
|
2010-02-16 04:31:36 -05:00
|
|
|
|
|
|
|
|
TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
|
2014-11-24 04:15:30 -05:00
|
|
|
for (auto *D : TU->decls()) {
|
2010-02-16 04:31:36 -05:00
|
|
|
// Don't re-import __va_list_tag, __builtin_va_list.
|
2014-11-24 04:15:30 -05:00
|
|
|
if (const auto *ND = dyn_cast<NamedDecl>(D))
|
2010-02-16 04:31:36 -05:00
|
|
|
if (IdentifierInfo *II = ND->getIdentifier())
|
|
|
|
|
if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
|
|
|
|
|
continue;
|
2018-08-02 13:33:11 -04:00
|
|
|
|
2015-05-27 14:47:56 -04:00
|
|
|
Decl *ToD = Importer.Import(D);
|
2018-08-02 13:33:11 -04:00
|
|
|
|
2015-05-27 14:47:56 -04:00
|
|
|
if (ToD) {
|
|
|
|
|
DeclGroupRef DGR(ToD);
|
|
|
|
|
CI.getASTConsumer().HandleTopLevelDecl(DGR);
|
|
|
|
|
}
|
2010-02-16 04:31:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AdaptedAction->ExecuteAction();
|
|
|
|
|
CI.getDiagnostics().getClient()->EndSourceFile();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ASTMergeAction::EndSourceFileAction() {
|
|
|
|
|
return AdaptedAction->EndSourceFileAction();
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-23 16:44:14 -04:00
|
|
|
ASTMergeAction::ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction,
|
2012-04-14 10:01:31 -04:00
|
|
|
ArrayRef<std::string> ASTFiles)
|
2016-07-23 16:44:14 -04:00
|
|
|
: AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
|
2010-02-16 04:31:36 -05:00
|
|
|
assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 13:33:11 -04:00
|
|
|
ASTMergeAction::~ASTMergeAction() {
|
2010-02-16 04:31:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ASTMergeAction::usesPreprocessorOnly() const {
|
|
|
|
|
return AdaptedAction->usesPreprocessorOnly();
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-20 17:14:49 -04:00
|
|
|
TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
|
|
|
|
|
return AdaptedAction->getTranslationUnitKind();
|
2010-02-16 04:31:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ASTMergeAction::hasPCHSupport() const {
|
|
|
|
|
return AdaptedAction->hasPCHSupport();
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-13 13:21:42 -04:00
|
|
|
bool ASTMergeAction::hasASTFileSupport() const {
|
|
|
|
|
return AdaptedAction->hasASTFileSupport();
|
2010-02-16 04:31:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool ASTMergeAction::hasCodeCompletionSupport() const {
|
|
|
|
|
return AdaptedAction->hasCodeCompletionSupport();
|
|
|
|
|
}
|