clang  9.0.0svn
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 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"
17 using namespace clang;
19 std::unique_ptr<ASTConsumer>
21  return AdaptedAction->CreateASTConsumer(CI, InFile);
22 }
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 }
36  CI.getASTContext().getLangOpts());
38  &CI.getASTContext());
40  DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
41  ASTImporterLookupTable LookupTable(
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);
53  if (!Unit)
54  continue;
56  ASTImporter Importer(CI.getASTContext(), CI.getFileManager(),
57  Unit->getASTContext(), Unit->getFileManager(),
58  /*MinimalImport=*/false, &LookupTable);
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;
68  Decl *ToD = Importer.Import(D);
70  if (ToD) {
71  DeclGroupRef DGR(ToD);
73  }
74  }
75  }
77  AdaptedAction->ExecuteAction();
79 }
82  return AdaptedAction->EndSourceFileAction();
83 }
85 ASTMergeAction::ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction,
86  ArrayRef<std::string> ASTFiles)
87 : AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
88  assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
89 }
92 }
95  return AdaptedAction->usesPreprocessorOnly();
96 }
99  return AdaptedAction->getTranslationUnitKind();
100 }
103  return AdaptedAction->hasPCHSupport();
104 }
107  return AdaptedAction->hasASTFileSupport();
108 }
111  return AdaptedAction->hasCodeCompletionSupport();
112 }
Defines the clang::ASTContext interface.
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:509
bool hasPCHSupport() const override
Does this action support use with PCH?
Definition: ASTMerge.cpp:102
Load everything, including Sema.
Definition: ASTUnit.h:670
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:86
DiagnosticOptions & getDiagnosticOpts()
std::unique_ptr< ASTUnit > takeCurrentASTUnit()
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, ArrayRef< RemappedFile > RemappedFiles=None, bool CaptureDiagnostics=false, bool AllowPCHWithCompilerErrors=false, bool UserFilesAreVolatile=false)
Create a ASTUnit from an AST file.
Definition: ASTUnit.cpp:732
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: ASTMerge.cpp:20
ASTContext & getASTContext() const
virtual void EndSourceFile()
Callback to inform the diagnostic client that processing of a source file has ended.
Definition: Diagnostic.h:1519
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
Definition: ASTMerge.cpp:24
One of these records is kept for each identifier that is lexed.
Definition: Format.h:2071
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Definition: ASTMerge.cpp:94
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
Definition: ASTMerge.cpp:110
CompilerInstance & getCompilerInstance() const
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:148
ASTConsumer & getASTConsumer() const
Defines the Diagnostic-related interfaces.
ASTMergeAction(std::unique_ptr< FrontendAction > AdaptedAction, ArrayRef< std::string > ASTFiles)
Definition: ASTMerge.cpp:85
FileSystemOptions & getFileSystemOpts()
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
~ASTMergeAction() override
Definition: ASTMerge.cpp:91
Diagnostic consumer that forwards diagnostics along to an existing, already-initialized diagnostic co...
Definition: Diagnostic.h:1554
void SetArgToStringFn(ArgToStringFnTy Fn, void *Cookie)
Definition: Diagnostic.h:791
Dataflow Directional Tag Classes.
FileManager & getFileManager() const
Return the current file manager to the caller.
const FrontendInputFile & getCurrentInput() const
bool hasASTFileSupport() const override
Does this action support use with AST files?
Definition: ASTMerge.cpp:106
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.
Imports selected nodes from one AST context into another context, merging AST nodes where appropriate...
Definition: ASTImporter.h:83
void EndSourceFileAction() override
Callback at the end of processing a single input.
Definition: ASTMerge.cpp:81
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: ASTMerge.cpp:33
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1008
const IntrusiveRefCntPtr< DiagnosticIDs > & getDiagnosticIDs() const
Definition: Diagnostic.h:494
Describes the kind of translation unit being processed.
Definition: LangOptions.h:361
const PCHContainerReader & getPCHContainerReader() const
Return the appropriate PCHContainerReader depending on the current CodeGenOptions.
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we&#39;re handling.
Definition: ASTMerge.cpp:98
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
The top declaration context.
Definition: Decl.h:107
virtual bool HandleTopLevelDecl(DeclGroupRef D)
HandleTopLevelDecl - Handle the specified top-level declaration.
Definition: ASTConsumer.cpp:18
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:1511
const LangOptions & getLangOpts() const
Definition: ASTContext.h:706