1 //===-- ASTMerge.cpp - AST Merging Frontent Action --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 #include "clang/Frontend/ASTUnit.h"
10 #include "clang/Frontend/CompilerInstance.h"
11 #include "clang/Frontend/FrontendActions.h"
12 #include "clang/AST/ASTContext.h"
13 #include "clang/AST/ASTDiagnostic.h"
14 #include "clang/AST/ASTImporter.h"
15 #include "clang/Basic/Diagnostic.h"
17 using namespace clang
;
19 ASTConsumer
*ASTMergeAction::CreateASTConsumer(CompilerInstance
&CI
,
20 llvm::StringRef InFile
) {
21 return AdaptedAction
->CreateASTConsumer(CI
, InFile
);
24 bool ASTMergeAction::BeginSourceFileAction(CompilerInstance
&CI
,
25 llvm::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
29 AdaptedAction
->setCurrentFile(getCurrentFile(), getCurrentFileKind(),
30 takeCurrentASTUnit());
31 AdaptedAction
->setCompilerInstance(&CI
);
32 return AdaptedAction
->BeginSourceFileAction(CI
, Filename
);
35 void ASTMergeAction::ExecuteAction() {
36 CompilerInstance
&CI
= getCompilerInstance();
37 CI
.getDiagnostics().getClient()->BeginSourceFile(
38 CI
.getASTContext().getLangOptions());
39 CI
.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument
,
41 llvm::IntrusiveRefCntPtr
<DiagnosticIDs
>
42 DiagIDs(CI
.getDiagnostics().getDiagnosticIDs());
43 for (unsigned I
= 0, N
= ASTFiles
.size(); I
!= N
; ++I
) {
44 llvm::IntrusiveRefCntPtr
<Diagnostic
>
45 Diags(new Diagnostic(DiagIDs
, CI
.getDiagnostics().getClient(),
46 /*ShouldOwnClient=*/false));
47 ASTUnit
*Unit
= ASTUnit::LoadFromASTFile(ASTFiles
[I
], Diags
,
48 CI
.getFileSystemOpts(), false);
52 ASTImporter
Importer(CI
.getASTContext(),
54 Unit
->getASTContext(),
55 Unit
->getFileManager(),
56 /*MinimalImport=*/false);
58 TranslationUnitDecl
*TU
= Unit
->getASTContext().getTranslationUnitDecl();
59 for (DeclContext::decl_iterator D
= TU
->decls_begin(),
60 DEnd
= TU
->decls_end();
62 // Don't re-import __va_list_tag, __builtin_va_list.
63 if (NamedDecl
*ND
= dyn_cast
<NamedDecl
>(*D
))
64 if (IdentifierInfo
*II
= ND
->getIdentifier())
65 if (II
->isStr("__va_list_tag") || II
->isStr("__builtin_va_list"))
74 AdaptedAction
->ExecuteAction();
75 CI
.getDiagnostics().getClient()->EndSourceFile();
78 void ASTMergeAction::EndSourceFileAction() {
79 return AdaptedAction
->EndSourceFileAction();
82 ASTMergeAction::ASTMergeAction(FrontendAction
*AdaptedAction
,
83 std::string
*ASTFiles
, unsigned NumASTFiles
)
84 : AdaptedAction(AdaptedAction
), ASTFiles(ASTFiles
, ASTFiles
+ NumASTFiles
) {
85 assert(AdaptedAction
&& "ASTMergeAction needs an action to adapt");
88 ASTMergeAction::~ASTMergeAction() {
92 bool ASTMergeAction::usesPreprocessorOnly() const {
93 return AdaptedAction
->usesPreprocessorOnly();
96 bool ASTMergeAction::usesCompleteTranslationUnit() {
97 return AdaptedAction
->usesCompleteTranslationUnit();
100 bool ASTMergeAction::hasPCHSupport() const {
101 return AdaptedAction
->hasPCHSupport();
104 bool ASTMergeAction::hasASTFileSupport() const {
105 return AdaptedAction
->hasASTFileSupport();
108 bool ASTMergeAction::hasCodeCompletionSupport() const {
109 return AdaptedAction
->hasCodeCompletionSupport();