Douglas Gregor | 62d311f | 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 | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 10 | #include "clang/AST/ASTContext.h" |
Douglas Gregor | 6b2a474 | 2010-02-09 22:37:58 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTDiagnostic.h" |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTImporter.h" |
Douglas Gregor | 7f95d26 | 2010-04-05 23:52:57 +0000 | [diff] [blame] | 13 | #include "clang/Basic/Diagnostic.h" |
Chandler Carruth | 3a02247 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/CompilerInstance.h" |
| 15 | #include "clang/Frontend/FrontendActions.h" |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 16 | |
| 17 | using namespace clang; |
| 18 | |
David Blaikie | 6beb6aa | 2014-08-10 19:56:51 +0000 | [diff] [blame] | 19 | std::unique_ptr<ASTConsumer> |
| 20 | ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { |
Douglas Gregor | 62d311f | 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 | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 25 | StringRef Filename) { |
Douglas Gregor | 62d311f | 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 | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 29 | AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit()); |
Douglas Gregor | 62d311f | 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 | 2fbe558 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 36 | CI.getDiagnostics().getClient()->BeginSourceFile( |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 37 | CI.getASTContext().getLangOpts()); |
Douglas Gregor | 6b2a474 | 2010-02-09 22:37:58 +0000 | [diff] [blame] | 38 | CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, |
| 39 | &CI.getASTContext()); |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 40 | IntrusiveRefCntPtr<DiagnosticIDs> |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 41 | DiagIDs(CI.getDiagnostics().getDiagnosticIDs()); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 42 | for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) { |
Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 43 | IntrusiveRefCntPtr<DiagnosticsEngine> |
Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 44 | Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(), |
Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 45 | new ForwardingDiagnosticConsumer( |
| 46 | *CI.getDiagnostics().getClient()), |
| 47 | /*ShouldOwnClient=*/true)); |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 48 | std::unique_ptr<ASTUnit> Unit = |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 49 | ASTUnit::LoadFromASTFile(ASTFiles[I], CI.getPCHContainerReader(), |
Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 50 | Diags, CI.getFileSystemOpts(), false); |
| 51 | |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 52 | if (!Unit) |
| 53 | continue; |
| 54 | |
Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 55 | ASTImporter Importer(CI.getASTContext(), |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 56 | CI.getFileManager(), |
Douglas Gregor | 811663e | 2010-02-10 00:15:17 +0000 | [diff] [blame] | 57 | Unit->getASTContext(), |
Douglas Gregor | 0a79167 | 2011-01-18 03:11:38 +0000 | [diff] [blame] | 58 | Unit->getFileManager(), |
| 59 | /*MinimalImport=*/false); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 60 | |
| 61 | TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl(); |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 62 | for (auto *D : TU->decls()) { |
Douglas Gregor | 9503c46 | 2010-02-16 00:04:46 +0000 | [diff] [blame] | 63 | // Don't re-import __va_list_tag, __builtin_va_list. |
Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 64 | if (const auto *ND = dyn_cast<NamedDecl>(D)) |
Douglas Gregor | 9503c46 | 2010-02-16 00:04:46 +0000 | [diff] [blame] | 65 | if (IdentifierInfo *II = ND->getIdentifier()) |
| 66 | if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list")) |
| 67 | continue; |
| 68 | |
Sean Callanan | 61ea057 | 2015-04-28 18:24:12 +0000 | [diff] [blame] | 69 | Decl *ToD = Importer.Import(D); |
| 70 | |
| 71 | if (ToD) { |
| 72 | DeclGroupRef DGR(ToD); |
| 73 | CI.getASTConsumer().HandleTopLevelDecl(DGR); |
| 74 | } |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 75 | } |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Douglas Gregor | 2fbe558 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 78 | AdaptedAction->ExecuteAction(); |
| 79 | CI.getDiagnostics().getClient()->EndSourceFile(); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | void ASTMergeAction::EndSourceFileAction() { |
| 83 | return AdaptedAction->EndSourceFileAction(); |
| 84 | } |
| 85 | |
Argyrios Kyrtzidis | d35e98f | 2016-02-07 19:28:36 +0000 | [diff] [blame] | 86 | ASTMergeAction::ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction, |
Argyrios Kyrtzidis | 3d97a9b | 2012-02-04 01:36:04 +0000 | [diff] [blame] | 87 | ArrayRef<std::string> ASTFiles) |
Argyrios Kyrtzidis | d35e98f | 2016-02-07 19:28:36 +0000 | [diff] [blame] | 88 | : AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) { |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 89 | assert(AdaptedAction && "ASTMergeAction needs an action to adapt"); |
| 90 | } |
| 91 | |
| 92 | ASTMergeAction::~ASTMergeAction() { |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | bool ASTMergeAction::usesPreprocessorOnly() const { |
| 96 | return AdaptedAction->usesPreprocessorOnly(); |
| 97 | } |
| 98 | |
Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 99 | TranslationUnitKind ASTMergeAction::getTranslationUnitKind() { |
| 100 | return AdaptedAction->getTranslationUnitKind(); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | bool ASTMergeAction::hasPCHSupport() const { |
| 104 | return AdaptedAction->hasPCHSupport(); |
| 105 | } |
| 106 | |
Daniel Dunbar | fa6214c | 2010-06-07 23:24:43 +0000 | [diff] [blame] | 107 | bool ASTMergeAction::hasASTFileSupport() const { |
| 108 | return AdaptedAction->hasASTFileSupport(); |
Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | bool ASTMergeAction::hasCodeCompletionSupport() const { |
| 112 | return AdaptedAction->hasCodeCompletionSupport(); |
| 113 | } |