clang  3.7.0
ASTMerge.cpp
Go to the documentation of this file.
1 //===-- ASTMerge.cpp - AST Merging Frontent 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"
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  StringRef Filename) {
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.
30  AdaptedAction->setCompilerInstance(&CI);
31  return AdaptedAction->BeginSourceFileAction(CI, Filename);
32 }
33 
37  CI.getASTContext().getLangOpts());
39  &CI.getASTContext());
41  DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
42  for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
44  Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
46  *CI.getDiagnostics().getClient()),
47  /*ShouldOwnClient=*/true));
48  std::unique_ptr<ASTUnit> Unit =
50  Diags, CI.getFileSystemOpts(), false);
51 
52  if (!Unit)
53  continue;
54 
55  ASTImporter Importer(CI.getASTContext(),
56  CI.getFileManager(),
57  Unit->getASTContext(),
58  Unit->getFileManager(),
59  /*MinimalImport=*/false);
60 
61  TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
63  for (auto *D : TU->decls()) {
64  // Don't re-import __va_list_tag, __builtin_va_list.
65  if (const auto *ND = dyn_cast<NamedDecl>(D))
66  if (IdentifierInfo *II = ND->getIdentifier())
67  if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
68  continue;
69 
70  Decl *ToD = Importer.Import(D);
71 
72  if (ToD) {
73  DeclGroupRef DGR(ToD);
75  }
76  }
77  }
78 
79  AdaptedAction->ExecuteAction();
81 }
82 
84  return AdaptedAction->EndSourceFileAction();
85 }
86 
88  ArrayRef<std::string> ASTFiles)
89  : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
90  assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
91 }
92 
94  delete AdaptedAction;
95 }
96 
98  return AdaptedAction->usesPreprocessorOnly();
99 }
100 
102  return AdaptedAction->getTranslationUnitKind();
103 }
104 
106  return AdaptedAction->hasPCHSupport();
107 }
108 
110  return AdaptedAction->hasASTFileSupport();
111 }
112 
114  return AdaptedAction->hasCodeCompletionSupport();
115 }
Defines the clang::ASTContext interface.
ASTContext & getASTContext() const
DiagnosticConsumer * getClient()
Definition: Diagnostic.h:368
bool hasPCHSupport() const override
Does this action support use with PCH?
Definition: ASTMerge.cpp:105
ASTMergeAction(FrontendAction *AdaptedAction, ArrayRef< std::string > ASTFiles)
Definition: ASTMerge.cpp:87
bool BeginSourceFileAction(CompilerInstance &CI, StringRef Filename) override
Callback at the start of processing a single input.
Definition: ASTMerge.cpp:24
virtual bool hasCodeCompletionSupport() const
Does this action support use with code completion?
Abstract base class for actions which can be performed by the frontend.
CompilerInstance & getCompilerInstance() const
DiagnosticOptions & getDiagnosticOpts()
std::unique_ptr< ASTUnit > takeCurrentASTUnit()
virtual bool usesPreprocessorOnly() const =0
Does this action only use the preprocessor?
std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile) override
Create the AST consumer object for this action, if supported.
Definition: ASTMerge.cpp:20
virtual void EndSourceFile()
Callback to inform the diagnostic client that processing of a source file has ended.
Definition: Diagnostic.h:1342
const FrontendInputFile & getCurrentInput() const
virtual bool hasASTFileSupport() const
Does this action support use with AST files?
static std::unique_ptr< ASTUnit > LoadFromASTFile(const std::string &Filename, const PCHContainerReader &PCHContainerRdr, IntrusiveRefCntPtr< DiagnosticsEngine > Diags, const FileSystemOptions &FileSystemOpts, 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:651
bool usesPreprocessorOnly() const override
Does this action only use the preprocessor?
Definition: ASTMerge.cpp:97
ASTConsumer & getASTConsumer() const
bool hasCodeCompletionSupport() const override
Does this action support use with code completion?
Definition: ASTMerge.cpp:113
const LangOptions & getLangOpts() const
Definition: ASTContext.h:533
Concrete class used by the front-end to report problems and issues.
Definition: Diagnostic.h:135
const IntrusiveRefCntPtr< DiagnosticIDs > & getDiagnosticIDs() const
Definition: Diagnostic.h:353
virtual bool BeginSourceFileAction(CompilerInstance &CI, StringRef Filename)
Callback at the start of processing a single input.
void setCurrentInput(const FrontendInputFile &CurrentInput, std::unique_ptr< ASTUnit > AST=nullptr)
virtual void EndSourceFileAction()
Callback at the end of processing a single input.
virtual std::unique_ptr< ASTConsumer > CreateASTConsumer(CompilerInstance &CI, StringRef InFile)=0
Create the AST consumer object for this action, if supported.
DiagnosticsEngine & getDiagnostics() const
Get the current diagnostics engine.
virtual void ExecuteAction()=0
Callback to run the program action, using the initialized compiler instance.
FileSystemOptions & getFileSystemOpts()
~ASTMergeAction() override
Definition: ASTMerge.cpp:93
const PCHContainerReader & getPCHContainerReader() const
virtual TranslationUnitKind getTranslationUnitKind()
For AST-based actions, the kind of translation unit we're handling.
Diagnostic consumer that forwards diagnostics along to an existing, already-initialized diagnostic co...
Definition: Diagnostic.h:1376
void SetArgToStringFn(ArgToStringFnTy Fn, void *Cookie)
Definition: Diagnostic.h:618
bool hasASTFileSupport() const override
Does this action support use with AST files?
Definition: ASTMerge.cpp:109
FileManager & getFileManager() const
Return the current file manager to the caller.
Defines the Diagnostic-related interfaces.
virtual bool hasPCHSupport() const
Does this action support use with PCH?
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:38
void EndSourceFileAction() override
Callback at the end of processing a single input.
Definition: ASTMerge.cpp:83
void setCompilerInstance(CompilerInstance *Value)
void ExecuteAction() override
Callback to run the program action, using the initialized compiler instance.
Definition: ASTMerge.cpp:34
virtual void Initialize(ASTContext &Context)
Definition: ASTConsumer.h:50
TranslationUnitKind
Describes the kind of translation unit being processed.
Definition: LangOptions.h:163
TranslationUnitKind getTranslationUnitKind() override
For AST-based actions, the kind of translation unit we're handling.
Definition: ASTMerge.cpp:101
TranslationUnitDecl - The top declaration context.
Definition: Decl.h:78
virtual bool HandleTopLevelDecl(DeclGroupRef D)
Definition: ASTConsumer.cpp:20
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:1334