1 //===-- ASTMerge.cpp - AST Merging Frontend Action --------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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/AST/ASTImporterLookupTable.h"
14 #include "clang/Basic/Diagnostic.h"
15 #include "clang/Frontend/CompilerInstance.h"
16 #include "clang/Frontend/FrontendActions.h"
17
18 using namespace clang;
19
20 std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)21 ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
22 return AdaptedAction->CreateASTConsumer(CI, InFile);
23 }
24
BeginSourceFileAction(CompilerInstance & CI)25 bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI) {
26 // FIXME: This is a hack. We need a better way to communicate the
27 // AST file, compiler instance, and file name than member variables
28 // of FrontendAction.
29 AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit());
30 AdaptedAction->setCompilerInstance(&CI);
31 return AdaptedAction->BeginSourceFileAction(CI);
32 }
33
ExecuteAction()34 void ASTMergeAction::ExecuteAction() {
35 CompilerInstance &CI = getCompilerInstance();
36 CI.getDiagnostics().getClient()->BeginSourceFile(
37 CI.getASTContext().getLangOpts());
38 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
39 &CI.getASTContext());
40 IntrusiveRefCntPtr<DiagnosticIDs>
41 DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
42 ASTImporterLookupTable LookupTable(
43 *CI.getASTContext().getTranslationUnitDecl());
44 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
45 IntrusiveRefCntPtr<DiagnosticsEngine>
46 Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
47 new ForwardingDiagnosticConsumer(
48 *CI.getDiagnostics().getClient()),
49 /*ShouldOwnClient=*/true));
50 std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
51 ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
52 CI.getFileSystemOpts(), false);
53
54 if (!Unit)
55 continue;
56
57 ASTImporter Importer(CI.getASTContext(), CI.getFileManager(),
58 Unit->getASTContext(), Unit->getFileManager(),
59 /*MinimalImport=*/false, &LookupTable);
60
61 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
62 for (auto *D : TU->decls()) {
63 // Don't re-import __va_list_tag, __builtin_va_list.
64 if (const auto *ND = dyn_cast<NamedDecl>(D))
65 if (IdentifierInfo *II = ND->getIdentifier())
66 if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
67 continue;
68
69 Decl *ToD = Importer.Import(D);
70
71 if (ToD) {
72 DeclGroupRef DGR(ToD);
73 CI.getASTConsumer().HandleTopLevelDecl(DGR);
74 }
75 }
76 }
77
78 AdaptedAction->ExecuteAction();
79 CI.getDiagnostics().getClient()->EndSourceFile();
80 }
81
EndSourceFileAction()82 void ASTMergeAction::EndSourceFileAction() {
83 return AdaptedAction->EndSourceFileAction();
84 }
85
ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction,ArrayRef<std::string> ASTFiles)86 ASTMergeAction::ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction,
87 ArrayRef<std::string> ASTFiles)
88 : AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
89 assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
90 }
91
~ASTMergeAction()92 ASTMergeAction::~ASTMergeAction() {
93 }
94
usesPreprocessorOnly() const95 bool ASTMergeAction::usesPreprocessorOnly() const {
96 return AdaptedAction->usesPreprocessorOnly();
97 }
98
getTranslationUnitKind()99 TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
100 return AdaptedAction->getTranslationUnitKind();
101 }
102
hasPCHSupport() const103 bool ASTMergeAction::hasPCHSupport() const {
104 return AdaptedAction->hasPCHSupport();
105 }
106
hasASTFileSupport() const107 bool ASTMergeAction::hasASTFileSupport() const {
108 return AdaptedAction->hasASTFileSupport();
109 }
110
hasCodeCompletionSupport() const111 bool ASTMergeAction::hasCodeCompletionSupport() const {
112 return AdaptedAction->hasCodeCompletionSupport();
113 }
114