Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
| 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 Gregor | d343ff6 | 2010-02-09 22:37:58 +0000 | [diff] [blame] | 13 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 14 | #include "clang/AST/ASTImporter.h" |
| 15 | |
| 16 | using namespace clang; |
| 17 | |
| 18 | ASTConsumer *ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, |
| 19 | llvm::StringRef InFile) { |
| 20 | return AdaptedAction->CreateASTConsumer(CI, InFile); |
| 21 | } |
| 22 | |
| 23 | bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI, |
| 24 | llvm::StringRef Filename) { |
| 25 | // FIXME: This is a hack. We need a better way to communicate the |
| 26 | // AST file, compiler instance, and file name than member variables |
| 27 | // of FrontendAction. |
| 28 | AdaptedAction->setCurrentFile(getCurrentFile(), takeCurrentASTUnit()); |
| 29 | AdaptedAction->setCompilerInstance(&CI); |
| 30 | return AdaptedAction->BeginSourceFileAction(CI, Filename); |
| 31 | } |
| 32 | |
| 33 | void ASTMergeAction::ExecuteAction() { |
| 34 | CompilerInstance &CI = getCompilerInstance(); |
Douglas Gregor | 0f962a8 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 35 | CI.getDiagnostics().getClient()->BeginSourceFile( |
| 36 | CI.getASTContext().getLangOptions()); |
Douglas Gregor | d343ff6 | 2010-02-09 22:37:58 +0000 | [diff] [blame] | 37 | CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, |
| 38 | &CI.getASTContext()); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 39 | for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) { |
Douglas Gregor | 4800d95 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 40 | ASTUnit *Unit = ASTUnit::LoadFromPCHFile(ASTFiles[I], CI.getDiagnostics(), |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 41 | false, true); |
| 42 | if (!Unit) |
| 43 | continue; |
| 44 | |
Douglas Gregor | 4800d95 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 45 | ASTImporter Importer(CI.getDiagnostics(), |
| 46 | CI.getASTContext(), |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 47 | CI.getFileManager(), |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 48 | Unit->getASTContext(), |
Douglas Gregor | 4800d95 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 49 | Unit->getFileManager()); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 50 | |
| 51 | TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl(); |
| 52 | for (DeclContext::decl_iterator D = TU->decls_begin(), |
| 53 | DEnd = TU->decls_end(); |
| 54 | D != DEnd; ++D) { |
Douglas Gregor | 44703f5 | 2010-02-15 22:05:17 +0000 | [diff] [blame^] | 55 | Importer.Import(*D); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | delete Unit; |
| 59 | } |
| 60 | |
Douglas Gregor | 0f962a8 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 61 | AdaptedAction->ExecuteAction(); |
| 62 | CI.getDiagnostics().getClient()->EndSourceFile(); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void ASTMergeAction::EndSourceFileAction() { |
| 66 | return AdaptedAction->EndSourceFileAction(); |
| 67 | } |
| 68 | |
| 69 | ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction, |
| 70 | std::string *ASTFiles, unsigned NumASTFiles) |
| 71 | : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) { |
| 72 | assert(AdaptedAction && "ASTMergeAction needs an action to adapt"); |
| 73 | } |
| 74 | |
| 75 | ASTMergeAction::~ASTMergeAction() { |
| 76 | delete AdaptedAction; |
| 77 | } |
| 78 | |
| 79 | bool ASTMergeAction::usesPreprocessorOnly() const { |
| 80 | return AdaptedAction->usesPreprocessorOnly(); |
| 81 | } |
| 82 | |
| 83 | bool ASTMergeAction::usesCompleteTranslationUnit() { |
| 84 | return AdaptedAction->usesCompleteTranslationUnit(); |
| 85 | } |
| 86 | |
| 87 | bool ASTMergeAction::hasPCHSupport() const { |
| 88 | return AdaptedAction->hasPCHSupport(); |
| 89 | } |
| 90 | |
| 91 | bool ASTMergeAction::hasASTSupport() const { |
| 92 | return AdaptedAction->hasASTSupport(); |
| 93 | } |
| 94 | |
| 95 | bool ASTMergeAction::hasCodeCompletionSupport() const { |
| 96 | return AdaptedAction->hasCodeCompletionSupport(); |
| 97 | } |