clang  16.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 //===----------------------------------------------------------------------===//
9 #include "clang/AST/ASTContext.h"
11 #include "clang/AST/ASTImporter.h"
13 #include "clang/Basic/Diagnostic.h"
16 
17 using namespace clang;
18 
19 std::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 
36  CI.getASTContext().getLangOpts());
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(),
47  *CI.getDiagnostics().getClient()),
48  /*ShouldOwnClient=*/true));
49  std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
50  ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
51  CI.getFileSystemOpts(), false);
52 
53  if (!Unit)
54  continue;
55 
56  ASTImporter Importer(CI.getASTContext(), CI.getFileManager(),
57  Unit->getASTContext(), Unit->getFileManager(),
58  /*MinimalImport=*/false, SharedState);
59 
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"))
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 
87 ASTMergeAction::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 }
clang::FrontendAction::takeCurrentASTUnit
std::unique_ptr< ASTUnit > takeCurrentASTUnit()
Definition: FrontendAction.h:163
clang::DiagnosticsEngine::SetArgToStringFn
void SetArgToStringFn(ArgToStringFnTy Fn, void *Cookie)
Definition: Diagnostic.h:884
clang::ForwardingDiagnosticConsumer
Diagnostic consumer that forwards diagnostics along to an existing, already-initialized diagnostic co...
Definition: Diagnostic.h:1806
clang::ASTUnit::LoadFromASTFile
static std::unique_ptr< ASTUnit > LoadFromASTFile(const std::string &Filename, const PCHContainerReader &PCHContainerRdr, WhatToLoad ToLoad, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, const FileSystemOptions &FileSystemOpts, bool UseDebugInfo=false, bool OnlyLocalDecls=false, CaptureDiagsKind CaptureDiagnostics=CaptureDiagsKind::None, bool AllowASTWithCompilerErrors=false, bool UserFilesAreVolatile=false, IntrusiveRefCntPtr< llvm::vfs::FileSystem > VFS=llvm::vfs::getRealFileSystem())
Create a ASTUnit from an AST file.
Definition: ASTUnit.cpp:757
clang::ASTMergeAction::BeginSourceFileAction
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
Definition: ASTMerge.cpp:24
clang::RISCV::TU
@ TU
Definition: RISCVVIntrinsicUtils.h:97
clang::DiagnosticsEngine
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:192
clang::ASTMergeAction::hasPCHSupport
bool hasPCHSupport() const override
Does this action support use with PCH?
Definition: ASTMerge.cpp:104
clang::CompilerInstance::getFileSystemOpts
FileSystemOptions & getFileSystemOpts()
Definition: CompilerInstance.h:275
llvm::Expected
Definition: LLVM.h:41
clang::FrontendAction::ASTMergeAction
friend class ASTMergeAction
Definition: FrontendAction.h:40
clang::DiagnosticConsumer::EndSourceFile
virtual void EndSourceFile()
Callback to inform the diagnostic client that processing of a source file has ended.
Definition: Diagnostic.h:1771
clang::ASTContext::getTranslationUnitDecl
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1089
clang::CompilerInstance::getPCHContainerReader
const PCHContainerReader & getPCHContainerReader() const
Return the appropriate PCHContainerReader depending on the current CodeGenOptions.
Definition: CompilerInstance.h:542
clang::ASTMergeAction::CreateASTConsumer
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: ASTMerge.cpp:20
clang::CompilerInstance::getFileManager
FileManager & getFileManager() const
Return the current file manager to the caller.
Definition: CompilerInstance.h:402
clang::TranslationUnitDecl
The top declaration context.
Definition: Decl.h:80
clang::ASTMergeAction::hasCodeCompletionSupport
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
Definition: ASTMerge.cpp:112
Diagnostic.h
clang::DiagnosticsEngine::getDiagnosticIDs
const IntrusiveRefCntPtr< DiagnosticIDs > & getDiagnosticIDs() const
Definition: Diagnostic.h:552
clang::DiagnosticsEngine::getClient
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:567
clang::FrontendAction::getCompilerInstance
CompilerInstance & getCompilerInstance() const
Definition: FrontendAction.h:119
clang::ASTMergeAction::usesPreprocessorOnly
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Definition: ASTMerge.cpp:96
ASTContext.h
clang::CompilerInstance::getASTContext
ASTContext & getASTContext() const
Definition: CompilerInstance.h:462
clang::CompilerInstance
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
Definition: CompilerInstance.h:72
ASTDiagnostic.h
clang::ASTConsumer::HandleTopLevelDecl
virtual bool HandleTopLevelDecl(DeclGroupRef D)
HandleTopLevelDecl - Handle the specified top-level declaration.
Definition: ASTConsumer.cpp:18
clang::ASTImporter
Imports selected nodes from one AST context into another context, merging AST nodes where appropriate...
Definition: ASTImporter.h:62
clang::FrontendAction::getCurrentInput
const FrontendInputFile & getCurrentInput() const
Definition: FrontendAction.h:135
clang::ASTMergeAction::~ASTMergeAction
~ASTMergeAction() override
Definition: ASTMerge.cpp:93
clang::ASTMergeAction::hasASTFileSupport
bool hasASTFileSupport() const override
Does this action support use with AST files?
Definition: ASTMerge.cpp:108
clang::DiagnosticConsumer::BeginSourceFile
virtual void BeginSourceFile(const LangOptions &LangOpts, const Preprocessor *PP=nullptr)
Callback to inform the diagnostic client that processing of a source file is beginning.
Definition: Diagnostic.h:1763
llvm::ArrayRef< std::string >
clang::DeclGroupRef
Definition: DeclGroup.h:51
FrontendActions.h
ASTUnit.h
clang::CompilerInstance::getASTConsumer
ASTConsumer & getASTConsumer() const
Definition: CompilerInstance.h:485
clang::IdentifierInfo
One of these records is kept for each identifier that is lexed.
Definition: IdentifierTable.h:85
clang::ASTImporter::Import
llvm::Expected< ExprWithCleanups::CleanupObject > Import(ExprWithCleanups::CleanupObject From)
Import cleanup objects owned by ExprWithCleanup.
Definition: ASTImporter.cpp:8611
ASTImporter.h
std
Definition: Format.h:4477
clang::TranslationUnitKind
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:914
clang::CompilerInstance::getDiagnosticOpts
DiagnosticOptions & getDiagnosticOpts()
Definition: CompilerInstance.h:268
clang
Definition: CalledOnceCheck.h:17
clang::ASTMergeAction::EndSourceFileAction
void EndSourceFileAction() override
Callback at the end of processing a single input.
Definition: ASTMerge.cpp:83
clang::ASTUnit::LoadEverything
@ LoadEverything
Load everything, including Sema.
Definition: ASTUnit.h:678
ASTImporterSharedState.h
CompilerInstance.h
clang::CompilerInstance::getDiagnostics
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
Definition: CompilerInstance.h:334
clang::FormatASTNodeDiagnosticArgument
void FormatASTNodeDiagnosticArgument(DiagnosticsEngine::ArgumentKind Kind, intptr_t Val, StringRef Modifier, StringRef Argument, ArrayRef< DiagnosticsEngine::ArgumentValue > PrevArgs, SmallVectorImpl< char > &Output, void *Cookie, ArrayRef< intptr_t > QualTypeVals)
DiagnosticsEngine argument formatting function for diagnostics that involve AST nodes.
Definition: ASTDiagnostic.cpp:355
clang::ASTMergeAction::getTranslationUnitKind
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
Definition: ASTMerge.cpp:100
clang::ASTContext::getLangOpts
const LangOptions & getLangOpts() const
Definition: ASTContext.h:791
llvm::IntrusiveRefCntPtr
Definition: LLVM.h:47
clang::ASTMergeAction::ExecuteAction
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: ASTMerge.cpp:33