clang  8.0.0
ASTMerge.cpp
Go to the documentation of this file.
1 //===-- ASTMerge.cpp - AST Merging Frontend Action --------------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
10 #include "clang/AST/ASTContext.h"
12 #include "clang/AST/ASTImporter.h"
14 #include "clang/Basic/Diagnostic.h"
17 
18 using namespace clang;
19 
20 std::unique_ptr<ASTConsumer>
22  return AdaptedAction->CreateASTConsumer(CI, InFile);
23 }
24 
26  // FIXME: This is a hack. We need a better way to communicate the
27  // AST file, compiler instance, and file name than member variables
28  // of FrontendAction.
29  AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit());
30  AdaptedAction->setCompilerInstance(&CI);
31  return AdaptedAction->BeginSourceFileAction(CI);
32 }
33 
37  CI.getASTContext().getLangOpts());
39  &CI.getASTContext());
41  DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
42  ASTImporterLookupTable LookupTable(
44  for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
46  Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
48  *CI.getDiagnostics().getClient()),
49  /*ShouldOwnClient=*/true));
50  std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
51  ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
52  CI.getFileSystemOpts(), false);
53 
54  if (!Unit)
55  continue;
56 
57  ASTImporter Importer(CI.getASTContext(), CI.getFileManager(),
58  Unit->getASTContext(), Unit->getFileManager(),
59  /*MinimalImport=*/false, &LookupTable);
60 
61  TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
62  for (auto *D : TU->decls()) {
63  // Don't re-import __va_list_tag, __builtin_va_list.
64  if (const auto *ND = dyn_cast<NamedDecl>(D))
65  if (IdentifierInfo *II = ND->getIdentifier())
66  if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
67  continue;
68 
69  Decl *ToD = Importer.Import(D);
70 
71  if (ToD) {
72  DeclGroupRef DGR(ToD);
74  }
75  }
76  }
77 
78  AdaptedAction->ExecuteAction();
80 }
81 
83  return AdaptedAction->EndSourceFileAction();
84 }
85 
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 
93 }
94 
96  return AdaptedAction->usesPreprocessorOnly();
97 }
98 
100  return AdaptedAction->getTranslationUnitKind();
101 }
102 
104  return AdaptedAction->hasPCHSupport();
105 }
106 
108  return AdaptedAction->hasASTFileSupport();
109 }
110 
112  return AdaptedAction->hasCodeCompletionSupport();
113 }
Defines the clang::ASTContext interface.
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:510
bool hasPCHSupport() const override
Does this action support use with PCH?
Definition: ASTMerge.cpp:103
Load everything, including Sema.
Definition: ASTUnit.h:671
Decl - This represents one declaration (or definition), e.g.
Definition: DeclBase.h:87
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:733
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: ASTMerge.cpp:21
ASTContext & getASTContext() const
virtual void EndSourceFile()
Callback to inform the diagnostic client that processing of a source file has ended.
Definition: Diagnostic.h:1520
bool BeginSourceFileAction(CompilerInstance &CI) override
Callback at the start of processing a single input.
Definition: ASTMerge.cpp:25
One of these records is kept for each identifier that is lexed.
Definition: Format.h:2072
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Definition: ASTMerge.cpp:95
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
Definition: ASTMerge.cpp:111
CompilerInstance & getCompilerInstance() const
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:149
ASTConsumer & getASTConsumer() const
Defines the Diagnostic-related interfaces.
ASTMergeAction(std::unique_ptr< FrontendAction > AdaptedAction, ArrayRef< std::string > ASTFiles)
Definition: ASTMerge.cpp:86
FileSystemOptions & getFileSystemOpts()
CompilerInstance - Helper class for managing a single instance of the Clang compiler.
~ASTMergeAction() override
Definition: ASTMerge.cpp:92
Diagnostic consumer that forwards diagnostics along to an existing, already-initialized diagnostic co...
Definition: Diagnostic.h:1555
void SetArgToStringFn(ArgToStringFnTy Fn, void *Cookie)
Definition: Diagnostic.h:792
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:107
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:82
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: ASTMerge.cpp:34
TranslationUnitDecl * getTranslationUnitDecl() const
Definition: ASTContext.h:1009
const IntrusiveRefCntPtr< DiagnosticIDs > & getDiagnosticIDs() const
Definition: Diagnostic.h:495
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:362
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:99
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
The top declaration context.
Definition: Decl.h:108
virtual bool HandleTopLevelDecl(DeclGroupRef D)
HandleTopLevelDecl - Handle the specified top-level declaration.
Definition: ASTConsumer.cpp:19
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:1512
const LangOptions & getLangOpts() const
Definition: ASTContext.h:707