blob: 51064da270cc9c1c7fd834ee30b60e82164c4dd5 [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
24bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000025 StringRef Filename) {
Douglas Gregor62d311f2010-02-09 19:21:46 +000026 // 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 Gregor32fbe312012-01-20 16:28:04 +000029 AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit());
Douglas Gregor62d311f2010-02-09 19:21:46 +000030 AdaptedAction->setCompilerInstance(&CI);
31 return AdaptedAction->BeginSourceFileAction(CI, Filename);
32}
33
34void ASTMergeAction::ExecuteAction() {
35 CompilerInstance &CI = getCompilerInstance();
Douglas Gregor2fbe5582010-02-10 17:16:49 +000036 CI.getDiagnostics().getClient()->BeginSourceFile(
Douglas Gregor6b930962013-05-03 22:58:43 +000037 CI.getASTContext().getLangOpts());
Douglas Gregor6b2a4742010-02-09 22:37:58 +000038 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
39 &CI.getASTContext());
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000040 IntrusiveRefCntPtr<DiagnosticIDs>
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000041 DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
Douglas Gregor62d311f2010-02-09 19:21:46 +000042 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000043 IntrusiveRefCntPtr<DiagnosticsEngine>
Douglas Gregor811db4e2012-10-23 22:26:28 +000044 Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
Douglas Gregor6b930962013-05-03 22:58:43 +000045 new ForwardingDiagnosticConsumer(
46 *CI.getDiagnostics().getClient()),
47 /*ShouldOwnClient=*/true));
Adrian Prantlbb165fb2015-06-20 18:53:08 +000048 std::unique_ptr<ASTUnit> Unit =
Adrian Prantlfb2398d2015-07-17 01:19:54 +000049 ASTUnit::LoadFromASTFile(ASTFiles[I], CI.getPCHContainerReader(),
Adrian Prantlbb165fb2015-06-20 18:53:08 +000050 Diags, CI.getFileSystemOpts(), false);
51
Douglas Gregor62d311f2010-02-09 19:21:46 +000052 if (!Unit)
53 continue;
54
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000055 ASTImporter Importer(CI.getASTContext(),
Douglas Gregor811663e2010-02-10 00:15:17 +000056 CI.getFileManager(),
Douglas Gregor811663e2010-02-10 00:15:17 +000057 Unit->getASTContext(),
Douglas Gregor0a791672011-01-18 03:11:38 +000058 Unit->getFileManager(),
59 /*MinimalImport=*/false);
Douglas Gregor62d311f2010-02-09 19:21:46 +000060
61 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
Aaron Ballman629afae2014-03-07 19:56:05 +000062 for (auto *D : TU->decls()) {
Douglas Gregor9503c462010-02-16 00:04:46 +000063 // Don't re-import __va_list_tag, __builtin_va_list.
Aaron Ballman629afae2014-03-07 19:56:05 +000064 if (const auto *ND = dyn_cast<NamedDecl>(D))
Douglas Gregor9503c462010-02-16 00:04:46 +000065 if (IdentifierInfo *II = ND->getIdentifier())
66 if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
67 continue;
68
Sean Callanan61ea0572015-04-28 18:24:12 +000069 Decl *ToD = Importer.Import(D);
70
71 if (ToD) {
72 DeclGroupRef DGR(ToD);
73 CI.getASTConsumer().HandleTopLevelDecl(DGR);
74 }
Douglas Gregor62d311f2010-02-09 19:21:46 +000075 }
Douglas Gregor62d311f2010-02-09 19:21:46 +000076 }
77
Douglas Gregor2fbe5582010-02-10 17:16:49 +000078 AdaptedAction->ExecuteAction();
79 CI.getDiagnostics().getClient()->EndSourceFile();
Douglas Gregor62d311f2010-02-09 19:21:46 +000080}
81
82void ASTMergeAction::EndSourceFileAction() {
83 return AdaptedAction->EndSourceFileAction();
84}
85
Argyrios Kyrtzidisd35e98f2016-02-07 19:28:36 +000086ASTMergeAction::ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction,
Argyrios Kyrtzidis3d97a9b2012-02-04 01:36:04 +000087 ArrayRef<std::string> ASTFiles)
Argyrios Kyrtzidisd35e98f2016-02-07 19:28:36 +000088: AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
Douglas Gregor62d311f2010-02-09 19:21:46 +000089 assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
90}
91
92ASTMergeAction::~ASTMergeAction() {
Douglas Gregor62d311f2010-02-09 19:21:46 +000093}
94
95bool ASTMergeAction::usesPreprocessorOnly() const {
96 return AdaptedAction->usesPreprocessorOnly();
97}
98
Douglas Gregor69f74f82011-08-25 22:30:56 +000099TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
100 return AdaptedAction->getTranslationUnitKind();
Douglas Gregor62d311f2010-02-09 19:21:46 +0000101}
102
103bool ASTMergeAction::hasPCHSupport() const {
104 return AdaptedAction->hasPCHSupport();
105}
106
Daniel Dunbarfa6214c2010-06-07 23:24:43 +0000107bool ASTMergeAction::hasASTFileSupport() const {
108 return AdaptedAction->hasASTFileSupport();
Douglas Gregor62d311f2010-02-09 19:21:46 +0000109}
110
111bool ASTMergeAction::hasCodeCompletionSupport() const {
112 return AdaptedAction->hasCodeCompletionSupport();
113}