1 //===-- ASTMerge.cpp - AST Merging Frontend Action --------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 #include "clang/Frontend/ASTUnit.h"
9 #include "clang/AST/ASTContext.h"
10 #include "clang/AST/ASTDiagnostic.h"
11 #include "clang/AST/ASTImporter.h"
12 #include "clang/AST/ASTImporterSharedState.h"
13 #include "clang/Basic/Diagnostic.h"
14 #include "clang/Frontend/CompilerInstance.h"
15 #include "clang/Frontend/FrontendActions.h"
16
17 using namespace clang;
18
19 std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)20 ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
21 return AdaptedAction->CreateASTConsumer(CI, InFile);
22 }
23
BeginSourceFileAction(CompilerInstance & CI)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
27 // of FrontendAction.
28 AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit());
29 AdaptedAction->setCompilerInstance(&CI);
30 return AdaptedAction->BeginSourceFileAction(CI);
31 }
32
ExecuteAction()33 void ASTMergeAction::ExecuteAction() {
34 CompilerInstance &CI = getCompilerInstance();
35 CI.getDiagnostics().getClient()->BeginSourceFile(
36 CI.getASTContext().getLangOpts());
37 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
38 &CI.getASTContext());
39 IntrusiveRefCntPtr<DiagnosticIDs>
40 DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
41 auto SharedState = std::make_shared<ASTImporterSharedState>(
42 *CI.getASTContext().getTranslationUnitDecl());
43 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
44 IntrusiveRefCntPtr<DiagnosticsEngine> Diags(new DiagnosticsEngine(
45 DiagIDs, CI.getDiagnosticOpts(),
46 new ForwardingDiagnosticConsumer(*CI.getDiagnostics().getClient()),
47 /*ShouldOwnClient=*/true));
48 std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
49 ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything,
50 nullptr, Diags, CI.getFileSystemOpts(), CI.getHeaderSearchOpts());
51
52 if (!Unit)
53 continue;
54
55 ASTImporter Importer(CI.getASTContext(), CI.getFileManager(),
56 Unit->getASTContext(), Unit->getFileManager(),
57 /*MinimalImport=*/false, SharedState);
58
59 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
60 for (auto *D : TU->decls()) {
61 // Don't re-import __va_list_tag, __builtin_va_list.
62 if (const auto *ND = dyn_cast<NamedDecl>(D))
63 if (IdentifierInfo *II = ND->getIdentifier())
64 if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
65 continue;
66
67 llvm::Expected<Decl *> ToDOrError = Importer.Import(D);
68
69 if (ToDOrError) {
70 DeclGroupRef DGR(*ToDOrError);
71 CI.getASTConsumer().HandleTopLevelDecl(DGR);
72 } else {
73 llvm::consumeError(ToDOrError.takeError());
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