| Alexander Kornienko | 2a8c18d | 2018-04-06 15:14:32 +0000 | [diff] [blame] | 1 | //===-- ASTMerge.cpp - AST Merging Frontend Action --------------*- C++ -*-===// |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 2 | // |
| Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| Nico Weber | ade321e | 2018-04-10 18:53:28 +0000 | [diff] [blame] | 8 | #include "clang/Frontend/ASTUnit.h" |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 9 | #include "clang/AST/ASTContext.h" |
| Douglas Gregor | 6b2a474 | 2010-02-09 22:37:58 +0000 | [diff] [blame] | 10 | #include "clang/AST/ASTDiagnostic.h" |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 11 | #include "clang/AST/ASTImporter.h" |
| Gabor Marton | 54058b5 | 2018-12-17 13:53:12 +0000 | [diff] [blame] | 12 | #include "clang/AST/ASTImporterLookupTable.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 | |
| Richard Smith | d9259c2 | 2017-06-09 01:36:10 +0000 | [diff] [blame] | 24 | bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI) { |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 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. |
| Douglas Gregor | 32fbe31 | 2012-01-20 16:28:04 +0000 | [diff] [blame] | 28 | AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit()); |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 29 | AdaptedAction->setCompilerInstance(&CI); |
| Richard Smith | d9259c2 | 2017-06-09 01:36:10 +0000 | [diff] [blame] | 30 | return AdaptedAction->BeginSourceFileAction(CI); |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | void ASTMergeAction::ExecuteAction() { |
| 34 | CompilerInstance &CI = getCompilerInstance(); |
| Douglas Gregor | 2fbe558 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 35 | CI.getDiagnostics().getClient()->BeginSourceFile( |
| Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 36 | CI.getASTContext().getLangOpts()); |
| Douglas Gregor | 6b2a474 | 2010-02-09 22:37:58 +0000 | [diff] [blame] | 37 | CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, |
| 38 | &CI.getASTContext()); |
| Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 39 | IntrusiveRefCntPtr<DiagnosticIDs> |
| Argyrios Kyrtzidis | d004064 | 2010-11-18 20:06:41 +0000 | [diff] [blame] | 40 | DiagIDs(CI.getDiagnostics().getDiagnosticIDs()); |
| Gabor Marton | 54058b5 | 2018-12-17 13:53:12 +0000 | [diff] [blame] | 41 | ASTImporterLookupTable LookupTable( |
| 42 | *CI.getASTContext().getTranslationUnitDecl()); |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 43 | for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) { |
| Dylan Noblesmith | c95d819 | 2012-02-20 14:00:23 +0000 | [diff] [blame] | 44 | IntrusiveRefCntPtr<DiagnosticsEngine> |
| Douglas Gregor | 811db4e | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 45 | Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(), |
| Douglas Gregor | 6b93096 | 2013-05-03 22:58:43 +0000 | [diff] [blame] | 46 | new ForwardingDiagnosticConsumer( |
| 47 | *CI.getDiagnostics().getClient()), |
| 48 | /*ShouldOwnClient=*/true)); |
| Richard Smith | dbafb6c | 2017-06-29 23:23:46 +0000 | [diff] [blame] | 49 | std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile( |
| 50 | ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags, |
| 51 | CI.getFileSystemOpts(), false); |
| Adrian Prantl | bb165fb | 2015-06-20 18:53:08 +0000 | [diff] [blame] | 52 | |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 53 | if (!Unit) |
| 54 | continue; |
| 55 | |
| Gabor Marton | 54058b5 | 2018-12-17 13:53:12 +0000 | [diff] [blame] | 56 | ASTImporter Importer(CI.getASTContext(), CI.getFileManager(), |
| 57 | Unit->getASTContext(), Unit->getFileManager(), |
| 58 | /*MinimalImport=*/false, &LookupTable); |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 59 | |
| 60 | TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl(); |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 61 | for (auto *D : TU->decls()) { |
| Douglas Gregor | 9503c46 | 2010-02-16 00:04:46 +0000 | [diff] [blame] | 62 | // Don't re-import __va_list_tag, __builtin_va_list. |
| Aaron Ballman | 629afae | 2014-03-07 19:56:05 +0000 | [diff] [blame] | 63 | if (const auto *ND = dyn_cast<NamedDecl>(D)) |
| Douglas Gregor | 9503c46 | 2010-02-16 00:04:46 +0000 | [diff] [blame] | 64 | if (IdentifierInfo *II = ND->getIdentifier()) |
| 65 | if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list")) |
| 66 | continue; |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 67 | |
| Sean Callanan | 61ea057 | 2015-04-28 18:24:12 +0000 | [diff] [blame] | 68 | Decl *ToD = Importer.Import(D); |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 69 | |
| Sean Callanan | 61ea057 | 2015-04-28 18:24:12 +0000 | [diff] [blame] | 70 | if (ToD) { |
| 71 | DeclGroupRef DGR(ToD); |
| 72 | CI.getASTConsumer().HandleTopLevelDecl(DGR); |
| 73 | } |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 74 | } |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| Douglas Gregor | 2fbe558 | 2010-02-10 17:16:49 +0000 | [diff] [blame] | 77 | AdaptedAction->ExecuteAction(); |
| 78 | CI.getDiagnostics().getClient()->EndSourceFile(); |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | void ASTMergeAction::EndSourceFileAction() { |
| 82 | return AdaptedAction->EndSourceFileAction(); |
| 83 | } |
| 84 | |
| Argyrios Kyrtzidis | d35e98f | 2016-02-07 19:28:36 +0000 | [diff] [blame] | 85 | ASTMergeAction::ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction, |
| Argyrios Kyrtzidis | 3d97a9b | 2012-02-04 01:36:04 +0000 | [diff] [blame] | 86 | ArrayRef<std::string> ASTFiles) |
| Argyrios Kyrtzidis | d35e98f | 2016-02-07 19:28:36 +0000 | [diff] [blame] | 87 | : AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) { |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 88 | assert(AdaptedAction && "ASTMergeAction needs an action to adapt"); |
| 89 | } |
| 90 | |
| Fangrui Song | 6907ce2 | 2018-07-30 19:24:48 +0000 | [diff] [blame] | 91 | ASTMergeAction::~ASTMergeAction() { |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | bool ASTMergeAction::usesPreprocessorOnly() const { |
| 95 | return AdaptedAction->usesPreprocessorOnly(); |
| 96 | } |
| 97 | |
| Douglas Gregor | 69f74f8 | 2011-08-25 22:30:56 +0000 | [diff] [blame] | 98 | TranslationUnitKind ASTMergeAction::getTranslationUnitKind() { |
| 99 | return AdaptedAction->getTranslationUnitKind(); |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | bool ASTMergeAction::hasPCHSupport() const { |
| 103 | return AdaptedAction->hasPCHSupport(); |
| 104 | } |
| 105 | |
| Daniel Dunbar | fa6214c | 2010-06-07 23:24:43 +0000 | [diff] [blame] | 106 | bool ASTMergeAction::hasASTFileSupport() const { |
| 107 | return AdaptedAction->hasASTFileSupport(); |
| Douglas Gregor | 62d311f | 2010-02-09 19:21:46 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | bool ASTMergeAction::hasCodeCompletionSupport() const { |
| 111 | return AdaptedAction->hasCodeCompletionSupport(); |
| 112 | } |