clang 20.0.0git
ASTMerge.cpp
Go to the documentation of this file.
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//===----------------------------------------------------------------------===//
16
17using namespace clang;
18
19std::unique_ptr<ASTConsumer>
21 return AdaptedAction->CreateASTConsumer(CI, InFile);
22}
23
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
38 &CI.getASTContext());
40 DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
41 auto SharedState = std::make_shared<ASTImporterSharedState>(
43 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
45 Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
48 /*ShouldOwnClient=*/true));
49 std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
50 ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
52
53 if (!Unit)
54 continue;
55
56 ASTImporter Importer(CI.getASTContext(), CI.getFileManager(),
57 Unit->getASTContext(), Unit->getFileManager(),
58 /*MinimalImport=*/false, SharedState);
59
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"))
66 continue;
67
68 llvm::Expected<Decl *> ToDOrError = Importer.Import(D);
69
70 if (ToDOrError) {
71 DeclGroupRef DGR(*ToDOrError);
73 } else {
74 llvm::consumeError(ToDOrError.takeError());
75 }
76 }
77 }
78
79 AdaptedAction->ExecuteAction();
81}
82
84 return AdaptedAction->EndSourceFileAction();
85}
86
87ASTMergeAction::ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction,
88 ArrayRef<std::string> ASTFiles)
89: AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
90 assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
91}
92
94}
95
97 return AdaptedAction->usesPreprocessorOnly();
98}
99
101 return AdaptedAction->getTranslationUnitKind();
102}
103
105 return AdaptedAction->hasPCHSupport();
106}
107
109 return AdaptedAction->hasASTFileSupport();
110}
111
113 return AdaptedAction->hasCodeCompletionSupport();
114}
Defines the clang::ASTContext interface.
Defines the Diagnostic-related interfaces.
const Decl * D
virtual bool HandleTopLevelDecl(DeclGroupRef D)
HandleTopLevelDecl - Handle the specified top-level declaration.
Definition: ASTConsumer.cpp:18
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1141
const LangOptions & getLangOpts() const
Definition: ASTContext.h:834
Imports selected nodes from one AST context into another context, merging AST nodes where appropriate...
Definition: ASTImporter.h:62
llvm::Expected< ExprWithCleanups::CleanupObject > Import(ExprWithCleanups::CleanupObject From)
Import cleanup objects owned by ExprWithCleanup.
Definition: