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" |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 15 | #include "clang/Basic/Diagnostic.h" |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace clang; |
| 18 | |
| 19 | ASTConsumer *ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, |
| 20 | llvm::StringRef InFile) { |
| 21 | return AdaptedAction->CreateASTConsumer(CI, InFile); |
| 22 | } |
| 23 | |
| 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 |
| 28 | // of FrontendAction. |
Daniel Dunbar | 685ac66 | 2010-06-07 23:25:49 +0000 | [diff] [blame] | 29 | AdaptedAction->setCurrentFile(getCurrentFile(), getCurrentFileKind(), |
| 30 | takeCurrentASTUnit()); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 31 | AdaptedAction->setCompilerInstance(&CI); |
| 32 | return AdaptedAction->BeginSourceFileAction(CI, Filename); |
| 33 | } |
| 34 | |
| 35 | void ASTMergeAction::ExecuteAction() { |
| 36 | CompilerInstance &CI = getCompilerInstance(); |
Douglas Gregor | 0f962a8 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 37 | CI.getDiagnostics().getClient()->BeginSourceFile( |
| 38 | CI.getASTContext().getLangOptions()); |
Douglas Gregor | d343ff6 | 2010-02-09 22:37:58 +0000 | [diff] [blame] | 39 | CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, |
| 40 | &CI.getASTContext()); |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 41 | llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics()); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 42 | for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) { |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 43 | ASTUnit *Unit = ASTUnit::LoadFromPCHFile(ASTFiles[I], Diags, false); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 44 | if (!Unit) |
| 45 | continue; |
| 46 | |
Douglas Gregor | 914ed9d | 2010-08-13 03:15:25 +0000 | [diff] [blame] | 47 | // Reset the argument -> string function so that it has the AST |
| 48 | // context we want, since the Sema object created by |
| 49 | // LoadFromPCHFile will override it. |
| 50 | CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, |
| 51 | &CI.getASTContext()); |
| 52 | |
Douglas Gregor | 4800d95 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 53 | ASTImporter Importer(CI.getDiagnostics(), |
| 54 | CI.getASTContext(), |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 55 | CI.getFileManager(), |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 56 | Unit->getASTContext(), |
Douglas Gregor | 4800d95 | 2010-02-11 19:21:55 +0000 | [diff] [blame] | 57 | Unit->getFileManager()); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 58 | |
| 59 | TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl(); |
| 60 | for (DeclContext::decl_iterator D = TU->decls_begin(), |
| 61 | DEnd = TU->decls_end(); |
| 62 | D != DEnd; ++D) { |
Douglas Gregor | 9a94585 | 2010-02-16 00:04:46 +0000 | [diff] [blame] | 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")) |
| 67 | continue; |
| 68 | |
Douglas Gregor | 44703f5 | 2010-02-15 22:05:17 +0000 | [diff] [blame] | 69 | Importer.Import(*D); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | delete Unit; |
| 73 | } |
| 74 | |
Douglas Gregor | 0f962a8 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 75 | AdaptedAction->ExecuteAction(); |
| 76 | CI.getDiagnostics().getClient()->EndSourceFile(); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | void ASTMergeAction::EndSourceFileAction() { |
| 80 | return AdaptedAction->EndSourceFileAction(); |
| 81 | } |
| 82 | |
| 83 | ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction, |
| 84 | std::string *ASTFiles, unsigned NumASTFiles) |
| 85 | : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) { |
| 86 | assert(AdaptedAction && "ASTMergeAction needs an action to adapt"); |
| 87 | } |
| 88 | |
| 89 | ASTMergeAction::~ASTMergeAction() { |
| 90 | delete AdaptedAction; |
| 91 | } |
| 92 | |
| 93 | bool ASTMergeAction::usesPreprocessorOnly() const { |
| 94 | return AdaptedAction->usesPreprocessorOnly(); |
| 95 | } |
| 96 | |
| 97 | bool ASTMergeAction::usesCompleteTranslationUnit() { |
| 98 | return AdaptedAction->usesCompleteTranslationUnit(); |
| 99 | } |
| 100 | |
| 101 | bool ASTMergeAction::hasPCHSupport() const { |
| 102 | return AdaptedAction->hasPCHSupport(); |
| 103 | } |
| 104 | |
Daniel Dunbar | eb58d83 | 2010-06-07 23:24:43 +0000 | [diff] [blame] | 105 | bool ASTMergeAction::hasASTFileSupport() const { |
| 106 | return AdaptedAction->hasASTFileSupport(); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | bool ASTMergeAction::hasCodeCompletionSupport() const { |
| 110 | return AdaptedAction->hasCodeCompletionSupport(); |
| 111 | } |