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/AST/ASTContext.h"
11 #include "clang/AST/ASTDiagnostic.h"
12 #include "clang/AST/ASTImporter.h"
13 #include "clang/Basic/Diagnostic.h"
14 #include "clang/Frontend/CompilerInstance.h"
15 #include "clang/Frontend/FrontendActions.h"
17 using namespace clang;
19 ASTConsumer *ASTMergeAction::CreateASTConsumer(CompilerInstance &CI,
21 return AdaptedAction->CreateASTConsumer(CI, InFile);
24 bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
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->setCurrentInput(getCurrentInput(), takeCurrentASTUnit());
30 AdaptedAction->setCompilerInstance(&CI);
31 return AdaptedAction->BeginSourceFileAction(CI, Filename);
34 void ASTMergeAction::ExecuteAction() {
35 CompilerInstance &CI = getCompilerInstance();
36 CI.getDiagnostics().getClient()->BeginSourceFile(
37 CI.getASTContext().getLangOpts());
38 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
40 IntrusiveRefCntPtr<DiagnosticIDs>
41 DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
42 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
43 IntrusiveRefCntPtr<DiagnosticsEngine>
44 Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
45 new ForwardingDiagnosticConsumer(
46 *CI.getDiagnostics().getClient()),
47 /*ShouldOwnClient=*/true));
48 ASTUnit *Unit = ASTUnit::LoadFromASTFile(ASTFiles[I], Diags,
49 CI.getFileSystemOpts(), false);
53 ASTImporter Importer(CI.getASTContext(),
55 Unit->getASTContext(),
56 Unit->getFileManager(),
57 /*MinimalImport=*/false);
59 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
60 for (DeclContext::decl_iterator D = TU->decls_begin(),
61 DEnd = TU->decls_end();
63 // Don't re-import __va_list_tag, __builtin_va_list.
64 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
65 if (IdentifierInfo *II = ND->getIdentifier())
66 if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
75 AdaptedAction->ExecuteAction();
76 CI.getDiagnostics().getClient()->EndSourceFile();
79 void ASTMergeAction::EndSourceFileAction() {
80 return AdaptedAction->EndSourceFileAction();
83 ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
84 ArrayRef<std::string> ASTFiles)
85 : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
86 assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
89 ASTMergeAction::~ASTMergeAction() {
93 bool ASTMergeAction::usesPreprocessorOnly() const {
94 return AdaptedAction->usesPreprocessorOnly();
97 TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
98 return AdaptedAction->getTranslationUnitKind();
101 bool ASTMergeAction::hasPCHSupport() const {
102 return AdaptedAction->hasPCHSupport();
105 bool ASTMergeAction::hasASTFileSupport() const {
106 return AdaptedAction->hasASTFileSupport();
109 bool ASTMergeAction::hasCodeCompletionSupport() const {
110 return AdaptedAction->hasCodeCompletionSupport();