1*7330f729Sjoerg //===-- ASTMerge.cpp - AST Merging Frontend Action --------------*- C++ -*-===//
2*7330f729Sjoerg //
3*7330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*7330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
5*7330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*7330f729Sjoerg //
7*7330f729Sjoerg //===----------------------------------------------------------------------===//
8*7330f729Sjoerg #include "clang/Frontend/ASTUnit.h"
9*7330f729Sjoerg #include "clang/AST/ASTContext.h"
10*7330f729Sjoerg #include "clang/AST/ASTDiagnostic.h"
11*7330f729Sjoerg #include "clang/AST/ASTImporter.h"
12*7330f729Sjoerg #include "clang/AST/ASTImporterSharedState.h"
13*7330f729Sjoerg #include "clang/Basic/Diagnostic.h"
14*7330f729Sjoerg #include "clang/Frontend/CompilerInstance.h"
15*7330f729Sjoerg #include "clang/Frontend/FrontendActions.h"
16*7330f729Sjoerg
17*7330f729Sjoerg using namespace clang;
18*7330f729Sjoerg
19*7330f729Sjoerg std::unique_ptr<ASTConsumer>
CreateASTConsumer(CompilerInstance & CI,StringRef InFile)20*7330f729Sjoerg ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
21*7330f729Sjoerg return AdaptedAction->CreateASTConsumer(CI, InFile);
22*7330f729Sjoerg }
23*7330f729Sjoerg
BeginSourceFileAction(CompilerInstance & CI)24*7330f729Sjoerg bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI) {
25*7330f729Sjoerg // FIXME: This is a hack. We need a better way to communicate the
26*7330f729Sjoerg // AST file, compiler instance, and file name than member variables
27*7330f729Sjoerg // of FrontendAction.
28*7330f729Sjoerg AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit());
29*7330f729Sjoerg AdaptedAction->setCompilerInstance(&CI);
30*7330f729Sjoerg return AdaptedAction->BeginSourceFileAction(CI);
31*7330f729Sjoerg }
32*7330f729Sjoerg
ExecuteAction()33*7330f729Sjoerg void ASTMergeAction::ExecuteAction() {
34*7330f729Sjoerg CompilerInstance &CI = getCompilerInstance();
35*7330f729Sjoerg CI.getDiagnostics().getClient()->BeginSourceFile(
36*7330f729Sjoerg CI.getASTContext().getLangOpts());
37*7330f729Sjoerg CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
38*7330f729Sjoerg &CI.getASTContext());
39*7330f729Sjoerg IntrusiveRefCntPtr<DiagnosticIDs>
40*7330f729Sjoerg DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
41*7330f729Sjoerg auto SharedState = std::make_shared<ASTImporterSharedState>(
42*7330f729Sjoerg *CI.getASTContext().getTranslationUnitDecl());
43*7330f729Sjoerg for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
44*7330f729Sjoerg IntrusiveRefCntPtr<DiagnosticsEngine>
45*7330f729Sjoerg Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
46*7330f729Sjoerg new ForwardingDiagnosticConsumer(
47*7330f729Sjoerg *CI.getDiagnostics().getClient()),
48*7330f729Sjoerg /*ShouldOwnClient=*/true));
49*7330f729Sjoerg std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
50*7330f729Sjoerg ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
51*7330f729Sjoerg CI.getFileSystemOpts(), false);
52*7330f729Sjoerg
53*7330f729Sjoerg if (!Unit)
54*7330f729Sjoerg continue;
55*7330f729Sjoerg
56*7330f729Sjoerg ASTImporter Importer(CI.getASTContext(), CI.getFileManager(),
57*7330f729Sjoerg Unit->getASTContext(), Unit->getFileManager(),
58*7330f729Sjoerg /*MinimalImport=*/false, SharedState);
59*7330f729Sjoerg
60*7330f729Sjoerg TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
61*7330f729Sjoerg for (auto *D : TU->decls()) {
62*7330f729Sjoerg // Don't re-import __va_list_tag, __builtin_va_list.
63*7330f729Sjoerg if (const auto *ND = dyn_cast<NamedDecl>(D))
64*7330f729Sjoerg if (IdentifierInfo *II = ND->getIdentifier())
65*7330f729Sjoerg if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
66*7330f729Sjoerg continue;
67*7330f729Sjoerg
68*7330f729Sjoerg llvm::Expected<Decl *> ToDOrError = Importer.Import(D);
69*7330f729Sjoerg
70*7330f729Sjoerg if (ToDOrError) {
71*7330f729Sjoerg DeclGroupRef DGR(*ToDOrError);
72*7330f729Sjoerg CI.getASTConsumer().HandleTopLevelDecl(DGR);
73*7330f729Sjoerg } else {
74*7330f729Sjoerg llvm::consumeError(ToDOrError.takeError());
75*7330f729Sjoerg }
76*7330f729Sjoerg }
77*7330f729Sjoerg }
78*7330f729Sjoerg
79*7330f729Sjoerg AdaptedAction->ExecuteAction();
80*7330f729Sjoerg CI.getDiagnostics().getClient()->EndSourceFile();
81*7330f729Sjoerg }
82*7330f729Sjoerg
EndSourceFileAction()83*7330f729Sjoerg void ASTMergeAction::EndSourceFileAction() {
84*7330f729Sjoerg return AdaptedAction->EndSourceFileAction();
85*7330f729Sjoerg }
86*7330f729Sjoerg
ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction,ArrayRef<std::string> ASTFiles)87*7330f729Sjoerg ASTMergeAction::ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction,
88*7330f729Sjoerg ArrayRef<std::string> ASTFiles)
89*7330f729Sjoerg : AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
90*7330f729Sjoerg assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
91*7330f729Sjoerg }
92*7330f729Sjoerg
~ASTMergeAction()93*7330f729Sjoerg ASTMergeAction::~ASTMergeAction() {
94*7330f729Sjoerg }
95*7330f729Sjoerg
usesPreprocessorOnly() const96*7330f729Sjoerg bool ASTMergeAction::usesPreprocessorOnly() const {
97*7330f729Sjoerg return AdaptedAction->usesPreprocessorOnly();
98*7330f729Sjoerg }
99*7330f729Sjoerg
getTranslationUnitKind()100*7330f729Sjoerg TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
101*7330f729Sjoerg return AdaptedAction->getTranslationUnitKind();
102*7330f729Sjoerg }
103*7330f729Sjoerg
hasPCHSupport() const104*7330f729Sjoerg bool ASTMergeAction::hasPCHSupport() const {
105*7330f729Sjoerg return AdaptedAction->hasPCHSupport();
106*7330f729Sjoerg }
107*7330f729Sjoerg
hasASTFileSupport() const108*7330f729Sjoerg bool ASTMergeAction::hasASTFileSupport() const {
109*7330f729Sjoerg return AdaptedAction->hasASTFileSupport();
110*7330f729Sjoerg }
111*7330f729Sjoerg
hasCodeCompletionSupport() const112*7330f729Sjoerg bool ASTMergeAction::hasCodeCompletionSupport() const {
113*7330f729Sjoerg return AdaptedAction->hasCodeCompletionSupport();
114*7330f729Sjoerg }
115