blob: 986f98ae598b9985db86feeb6d896bb14f1c5aa9 [file] [log] [blame]
Douglas Gregor62d311f2010-02-09 19:21:46 +00001//===-- 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 Gregor62d311f2010-02-09 19:21:46 +000010#include "clang/AST/ASTContext.h"
Douglas Gregor6b2a4742010-02-09 22:37:58 +000011#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor62d311f2010-02-09 19:21:46 +000012#include "clang/AST/ASTImporter.h"
Douglas Gregor7f95d262010-04-05 23:52:57 +000013#include "clang/Basic/Diagnostic.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000014#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Frontend/FrontendActions.h"
Douglas Gregor62d311f2010-02-09 19:21:46 +000016
17using namespace clang;
18
David Blaikie6beb6aa2014-08-10 19:56:51 +000019std::unique_ptr<ASTConsumer>
20ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
Douglas Gregor62d311f2010-02-09 19:21:46 +000021 return AdaptedAction->CreateASTConsumer(CI, InFile);
22}
23
Richard Smithd9259c22017-06-09 01:36:10 +000024bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI) {
Douglas Gregor62d311f2010-02-09 19:21:46 +000025 // 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 Gregor32fbe312012-01-20 16:28:04 +000028 AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit());
Douglas Gregor62d311f2010-02-09 19:21:46 +000029 AdaptedAction->setCompilerInstance(&CI);
Richard Smithd9259c22017-06-09 01:36:10 +000030 return AdaptedAction->BeginSourceFileAction(CI);
Douglas Gregor62d311f2010-02-09 19:21:46 +000031}
32
33void ASTMergeAction::ExecuteAction() {
34 CompilerInstance &CI = getCompilerInstance();
Douglas Gregor2fbe5582010-02-10 17:16:49 +000035 CI.getDiagnostics().getClient()->BeginSourceFile(
Douglas Gregor6b930962013-05-03 22:58:43 +000036 CI.getASTContext().getLangOpts());
Douglas Gregor6b2a4742010-02-09 22:37:58 +000037 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
38 &CI.getASTContext());
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000039 IntrusiveRefCntPtr<DiagnosticIDs>
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000040 DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
Douglas Gregor62d311f2010-02-09 19:21:46 +000041 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000042 IntrusiveRefCntPtr<DiagnosticsEngine>
Douglas Gregor811db4e2012-10-23 22:26:28 +000043 Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
Douglas Gregor6b930962013-05-03 22:58:43 +000044 new ForwardingDiagnosticConsumer(
45 *CI.getDiagnostics().getClient()),
46 /*ShouldOwnClient=*/true));
Adrian Prantlbb165fb2015-06-20 18:53:08 +000047 std::unique_ptr<ASTUnit> Unit =
Adrian Prantlfb2398d2015-07-17 01:19:54 +000048 ASTUnit::LoadFromASTFile(ASTFiles[I], CI.getPCHContainerReader(),
Adrian Prantlbb165fb2015-06-20 18:53:08 +000049 Diags, CI.getFileSystemOpts(), false);
50
Douglas Gregor62d311f2010-02-09 19:21:46 +000051 if (!Unit)
52 continue;
53
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000054 ASTImporter Importer(CI.getASTContext(),
Douglas Gregor811663e2010-02-10 00:15:17 +000055 CI.getFileManager(),
Douglas Gregor811663e2010-02-10 00:15:17 +000056 Unit->getASTContext(),
Douglas Gregor0a791672011-01-18 03:11:38 +000057 Unit->getFileManager(),
58 /*MinimalImport=*/false);
Douglas Gregor62d311f2010-02-09 19:21:46 +000059
60 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
Aaron Ballman629afae2014-03-07 19:56:05 +000061 for (auto *D : TU->decls()) {
Douglas Gregor9503c462010-02-16 00:04:46 +000062 // Don't re-import __va_list_tag, __builtin_va_list.
Aaron Ballman629afae2014-03-07 19:56:05 +000063 if (const auto *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor9503c462010-02-16 00:04:46 +000064 if (IdentifierInfo *II = ND->getIdentifier())
65 if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
66 continue;
67
Sean Callanan61ea0572015-04-28 18:24:12 +000068 Decl *ToD = Importer.Import(D);
69
70 if (ToD) {
71 DeclGroupRef DGR(ToD);
72 CI.getASTConsumer().HandleTopLevelDecl(DGR);
73 }
Douglas Gregor62d311f2010-02-09 19:21:46 +000074 }
Douglas Gregor62d311f2010-02-09 19:21:46 +000075 }
76
Douglas Gregor2fbe5582010-02-10 17:16:49 +000077 AdaptedAction->ExecuteAction();
78 CI.getDiagnostics().getClient()->EndSourceFile();
Douglas Gregor62d311f2010-02-09 19:21:46 +000079}
80
81void ASTMergeAction::EndSourceFileAction() {
82 return AdaptedAction->EndSourceFileAction();
83}
84
Argyrios Kyrtzidisd35e98f2016-02-07 19:28:36 +000085ASTMergeAction::ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction,
Argyrios Kyrtzidis3d97a9b2012-02-04 01:36:04 +000086 ArrayRef<std::string> ASTFiles)
Argyrios Kyrtzidisd35e98f2016-02-07 19:28:36 +000087: AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
Douglas Gregor62d311f2010-02-09 19:21:46 +000088 assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
89}
90
91ASTMergeAction::~ASTMergeAction() {
Douglas Gregor62d311f2010-02-09 19:21:46 +000092}
93
94bool ASTMergeAction::usesPreprocessorOnly() const {
95 return AdaptedAction->usesPreprocessorOnly();
96}
97
Douglas Gregor69f74f82011-08-25 22:30:56 +000098TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
99 return AdaptedAction->getTranslationUnitKind();
Douglas Gregor62d311f2010-02-09 19:21:46 +0000100}
101
102bool ASTMergeAction::hasPCHSupport() const {
103 return AdaptedAction->hasPCHSupport();
104}
105
Daniel Dunbarfa6214c2010-06-07 23:24:43 +0000106bool ASTMergeAction::hasASTFileSupport() const {
107 return AdaptedAction->hasASTFileSupport();
Douglas Gregor62d311f2010-02-09 19:21:46 +0000108}
109
110bool ASTMergeAction::hasCodeCompletionSupport() const {
111 return AdaptedAction->hasCodeCompletionSupport();
112}