blob: e916e200659cf58c4126a3ed58ef6cb7b98c98bf [file] [log] [blame]
Douglas Gregor9bed8792010-02-09 19:21:46 +00001//===-- 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//===----------------------------------------------------------------------===//
9#include "clang/Frontend/ASTUnit.h"
10#include "clang/Frontend/CompilerInstance.h"
11#include "clang/Frontend/FrontendActions.h"
12#include "clang/AST/ASTContext.h"
Douglas Gregord343ff62010-02-09 22:37:58 +000013#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor9bed8792010-02-09 19:21:46 +000014#include "clang/AST/ASTImporter.h"
Douglas Gregor28019772010-04-05 23:52:57 +000015#include "clang/Basic/Diagnostic.h"
Douglas Gregor9bed8792010-02-09 19:21:46 +000016
17using namespace clang;
18
19ASTConsumer *ASTMergeAction::CreateASTConsumer(CompilerInstance &CI,
20 llvm::StringRef InFile) {
21 return AdaptedAction->CreateASTConsumer(CI, InFile);
22}
23
24bool 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
28 // of FrontendAction.
Daniel Dunbar685ac662010-06-07 23:25:49 +000029 AdaptedAction->setCurrentFile(getCurrentFile(), getCurrentFileKind(),
30 takeCurrentASTUnit());
Douglas Gregor9bed8792010-02-09 19:21:46 +000031 AdaptedAction->setCompilerInstance(&CI);
32 return AdaptedAction->BeginSourceFileAction(CI, Filename);
33}
34
35void ASTMergeAction::ExecuteAction() {
36 CompilerInstance &CI = getCompilerInstance();
Douglas Gregor0f962a82010-02-10 17:16:49 +000037 CI.getDiagnostics().getClient()->BeginSourceFile(
38 CI.getASTContext().getLangOptions());
Douglas Gregord343ff62010-02-09 22:37:58 +000039 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
40 &CI.getASTContext());
Douglas Gregor28019772010-04-05 23:52:57 +000041 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics());
Douglas Gregor9bed8792010-02-09 19:21:46 +000042 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
Douglas Gregor28019772010-04-05 23:52:57 +000043 ASTUnit *Unit = ASTUnit::LoadFromPCHFile(ASTFiles[I], Diags, false);
Douglas Gregor9bed8792010-02-09 19:21:46 +000044 if (!Unit)
45 continue;
46
Douglas Gregor4800d952010-02-11 19:21:55 +000047 ASTImporter Importer(CI.getDiagnostics(),
48 CI.getASTContext(),
Douglas Gregor88523732010-02-10 00:15:17 +000049 CI.getFileManager(),
Douglas Gregor88523732010-02-10 00:15:17 +000050 Unit->getASTContext(),
Douglas Gregor4800d952010-02-11 19:21:55 +000051 Unit->getFileManager());
Douglas Gregor9bed8792010-02-09 19:21:46 +000052
53 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
54 for (DeclContext::decl_iterator D = TU->decls_begin(),
55 DEnd = TU->decls_end();
56 D != DEnd; ++D) {
Douglas Gregor9a945852010-02-16 00:04:46 +000057 // Don't re-import __va_list_tag, __builtin_va_list.
58 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
59 if (IdentifierInfo *II = ND->getIdentifier())
60 if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
61 continue;
62
Douglas Gregor44703f52010-02-15 22:05:17 +000063 Importer.Import(*D);
Douglas Gregor9bed8792010-02-09 19:21:46 +000064 }
65
66 delete Unit;
67 }
68
Douglas Gregor0f962a82010-02-10 17:16:49 +000069 AdaptedAction->ExecuteAction();
70 CI.getDiagnostics().getClient()->EndSourceFile();
Douglas Gregor9bed8792010-02-09 19:21:46 +000071}
72
73void ASTMergeAction::EndSourceFileAction() {
74 return AdaptedAction->EndSourceFileAction();
75}
76
77ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
78 std::string *ASTFiles, unsigned NumASTFiles)
79 : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
80 assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
81}
82
83ASTMergeAction::~ASTMergeAction() {
84 delete AdaptedAction;
85}
86
87bool ASTMergeAction::usesPreprocessorOnly() const {
88 return AdaptedAction->usesPreprocessorOnly();
89}
90
91bool ASTMergeAction::usesCompleteTranslationUnit() {
92 return AdaptedAction->usesCompleteTranslationUnit();
93}
94
95bool ASTMergeAction::hasPCHSupport() const {
96 return AdaptedAction->hasPCHSupport();
97}
98
Daniel Dunbareb58d832010-06-07 23:24:43 +000099bool ASTMergeAction::hasASTFileSupport() const {
100 return AdaptedAction->hasASTFileSupport();
Douglas Gregor9bed8792010-02-09 19:21:46 +0000101}
102
103bool ASTMergeAction::hasCodeCompletionSupport() const {
104 return AdaptedAction->hasCodeCompletionSupport();
105}