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" |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 10 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | d343ff6 | 2010-02-09 22:37:58 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTImporter.h" |
Douglas Gregor | 2801977 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 13 | #include "clang/Basic/Diagnostic.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/CompilerInstance.h" |
| 15 | #include "clang/Frontend/FrontendActions.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, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 20 | StringRef InFile) { |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 21 | return AdaptedAction->CreateASTConsumer(CI, InFile); |
| 22 | } |
| 23 | |
| 24 | bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 25 | StringRef Filename) { |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 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. |
Douglas Gregor | 1f6b2b5 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 29 | AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit()); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 30 | AdaptedAction->setCompilerInstance(&CI); |
| 31 | return AdaptedAction->BeginSourceFileAction(CI, Filename); |
| 32 | } |
| 33 | |
| 34 | void ASTMergeAction::ExecuteAction() { |
| 35 | CompilerInstance &CI = getCompilerInstance(); |
Douglas Gregor | 0f962a8 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 36 | CI.getDiagnostics().getClient()->BeginSourceFile( |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 37 | CI.getASTContext().getLangOpts()); |
Douglas Gregor | d343ff6 | 2010-02-09 22:37:58 +0000 | [diff] [blame] | 38 | CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, |
| 39 | &CI.getASTContext()); |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 40 | IntrusiveRefCntPtr<DiagnosticIDs> |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 41 | DiagIDs(CI.getDiagnostics().getDiagnosticIDs()); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 42 | for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) { |
Dylan Noblesmith | c93dc78 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 43 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 44 | Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(), |
| 45 | CI.getDiagnostics().getClient(), |
| 46 | /*ShouldOwnClient=*/false)); |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 47 | ASTUnit *Unit = ASTUnit::LoadFromASTFile(ASTFiles[I], Diags, |
| 48 | CI.getFileSystemOpts(), false); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 49 | if (!Unit) |
| 50 | continue; |
| 51 | |
Argyrios Kyrtzidis | 33e4e70 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 52 | ASTImporter Importer(CI.getASTContext(), |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 53 | CI.getFileManager(), |
Douglas Gregor | 8852373 | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 54 | Unit->getASTContext(), |
Douglas Gregor | d8868a6 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 55 | Unit->getFileManager(), |
| 56 | /*MinimalImport=*/false); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 57 | |
| 58 | TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl(); |
| 59 | for (DeclContext::decl_iterator D = TU->decls_begin(), |
| 60 | DEnd = TU->decls_end(); |
| 61 | D != DEnd; ++D) { |
Douglas Gregor | 9a94585 | 2010-02-16 00:04:46 +0000 | [diff] [blame] | 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")) |
| 66 | continue; |
| 67 | |
Douglas Gregor | 44703f5 | 2010-02-15 22:05:17 +0000 | [diff] [blame] | 68 | Importer.Import(*D); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | delete Unit; |
| 72 | } |
| 73 | |
Douglas Gregor | 0f962a8 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 74 | AdaptedAction->ExecuteAction(); |
| 75 | CI.getDiagnostics().getClient()->EndSourceFile(); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | void ASTMergeAction::EndSourceFileAction() { |
| 79 | return AdaptedAction->EndSourceFileAction(); |
| 80 | } |
| 81 | |
| 82 | ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction, |
Argyrios Kyrtzidis | b3ca263 | 2012-02-04 01:36:04 +0000 | [diff] [blame] | 83 | ArrayRef<std::string> ASTFiles) |
| 84 | : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles.begin(), ASTFiles.end()) { |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 85 | assert(AdaptedAction && "ASTMergeAction needs an action to adapt"); |
| 86 | } |
| 87 | |
| 88 | ASTMergeAction::~ASTMergeAction() { |
| 89 | delete AdaptedAction; |
| 90 | } |
| 91 | |
| 92 | bool ASTMergeAction::usesPreprocessorOnly() const { |
| 93 | return AdaptedAction->usesPreprocessorOnly(); |
| 94 | } |
| 95 | |
Douglas Gregor | 467dc88 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 96 | TranslationUnitKind ASTMergeAction::getTranslationUnitKind() { |
| 97 | return AdaptedAction->getTranslationUnitKind(); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | bool ASTMergeAction::hasPCHSupport() const { |
| 101 | return AdaptedAction->hasPCHSupport(); |
| 102 | } |
| 103 | |
Daniel Dunbar | eb58d83 | 2010-06-07 23:24:43 +0000 | [diff] [blame] | 104 | bool ASTMergeAction::hasASTFileSupport() const { |
| 105 | return AdaptedAction->hasASTFileSupport(); |
Douglas Gregor | 9bed879 | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | bool ASTMergeAction::hasCodeCompletionSupport() const { |
| 109 | return AdaptedAction->hasCodeCompletionSupport(); |
| 110 | } |