1 //===-- ASTMerge.cpp - AST Merging Frontent Action --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 #include "clang/Frontend/ASTUnit.h"
10 #include "clang/AST/ASTContext.h"
11 #include "clang/AST/ASTDiagnostic.h"
12 #include "clang/AST/ASTImporter.h"
13 #include "clang/Basic/Diagnostic.h"
14 #include "clang/Frontend/CompilerInstance.h"
15 #include "clang/Frontend/FrontendActions.h"
17 using namespace clang;
19 std::unique_ptr<ASTConsumer>
20 ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
21 return AdaptedAction->CreateASTConsumer(CI, InFile);
24 bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI) {
25 // FIXME: This is a hack. We need a better way to communicate the
26 // AST file, compiler instance, and file name than member variables
28 AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit());
29 AdaptedAction->setCompilerInstance(&CI);
30 return AdaptedAction->BeginSourceFileAction(CI);
33 void ASTMergeAction::ExecuteAction() {
34 CompilerInstance &CI = getCompilerInstance();
35 CI.getDiagnostics().getClient()->BeginSourceFile(
36 CI.getASTContext().getLangOpts());
37 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
39 IntrusiveRefCntPtr<DiagnosticIDs>
40 DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
41 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
42 IntrusiveRefCntPtr<DiagnosticsEngine>
43 Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
44 new ForwardingDiagnosticConsumer(
45 *CI.getDiagnostics().getClient()),
46 /*ShouldOwnClient=*/true));
47 std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
48 ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
49 CI.getFileSystemOpts(), false);
54 ASTImporter Importer(CI.getASTContext(),
56 Unit->getASTContext(),
57 Unit->getFileManager(),
58 /*MinimalImport=*/false);
60 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
61 for (auto *D : TU->decls()) {
62 // Don't re-import __va_list_tag, __builtin_va_list.
63 if (const auto *ND = dyn_cast<NamedDecl>(D))
64 if (IdentifierInfo *II = ND->getIdentifier())
65 if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
68 Decl *ToD = Importer.Import(D);
71 DeclGroupRef DGR(ToD);
72 CI.getASTConsumer().HandleTopLevelDecl(DGR);
77 AdaptedAction->ExecuteAction();
78 CI.getDiagnostics().getClient()->EndSourceFile();
81 void ASTMergeAction::EndSourceFileAction() {
82 return AdaptedAction->EndSourceFileAction();
85 ASTMergeAction::ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction,
86 ArrayRef<std::string> ASTFiles)
87 : AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
88 assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
91 ASTMergeAction::~ASTMergeAction() {
94 bool ASTMergeAction::usesPreprocessorOnly() const {
95 return AdaptedAction->usesPreprocessorOnly();
98 TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
99 return AdaptedAction->getTranslationUnitKind();
102 bool ASTMergeAction::hasPCHSupport() const {
103 return AdaptedAction->hasPCHSupport();
106 bool ASTMergeAction::hasASTFileSupport() const {
107 return AdaptedAction->hasASTFileSupport();
110 bool ASTMergeAction::hasCodeCompletionSupport() const {
111 return AdaptedAction->hasCodeCompletionSupport();