blob: f4aacbb76bf62946ce5675877cacd3d3a1118310 [file] [log] [blame]
Alexander Kornienko2a8c18d2018-04-06 15:14:32 +00001//===-- ASTMerge.cpp - AST Merging Frontend Action --------------*- C++ -*-===//
Douglas Gregor62d311f2010-02-09 19:21:46 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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 Gregor62d311f2010-02-09 19:21:46 +00006//
7//===----------------------------------------------------------------------===//
Nico Weberade321e2018-04-10 18:53:28 +00008#include "clang/Frontend/ASTUnit.h"
Douglas Gregor62d311f2010-02-09 19:21:46 +00009#include "clang/AST/ASTContext.h"
Douglas Gregor6b2a4742010-02-09 22:37:58 +000010#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor62d311f2010-02-09 19:21:46 +000011#include "clang/AST/ASTImporter.h"
Gabor Marton54058b52018-12-17 13:53:12 +000012#include "clang/AST/ASTImporterLookupTable.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());
Gabor Marton54058b52018-12-17 13:53:12 +000041 ASTImporterLookupTable LookupTable(
42 *CI.getASTContext().getTranslationUnitDecl());
Douglas Gregor62d311f2010-02-09 19:21:46 +000043 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
Dylan Noblesmithc95d8192012-02-20 14:00:23 +000044 IntrusiveRefCntPtr<DiagnosticsEngine>
Douglas Gregor811db4e2012-10-23 22:26:28 +000045 Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
Douglas Gregor6b930962013-05-03 22:58:43 +000046 new ForwardingDiagnosticConsumer(
47 *CI.getDiagnostics().getClient()),
48 /*ShouldOwnClient=*/true));
Richard Smithdbafb6c2017-06-29 23:23:46 +000049 std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
50 ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
51 CI.getFileSystemOpts(), false);
Adrian Prantlbb165fb2015-06-20 18:53:08 +000052
Douglas Gregor62d311f2010-02-09 19:21:46 +000053 if (!Unit)
54 continue;
55
Gabor Marton54058b52018-12-17 13:53:12 +000056 ASTImporter Importer(CI.getASTContext(), CI.getFileManager(),
57 Unit->getASTContext(), Unit->getFileManager(),
58 /*MinimalImport=*/false, &LookupTable);
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;
Fangrui Song6907ce22018-07-30 19:24:48 +000067
Sean Callanan61ea0572015-04-28 18:24:12 +000068 Decl *ToD = Importer.Import(D);
Fangrui Song6907ce22018-07-30 19:24:48 +000069
Sean Callanan61ea0572015-04-28 18:24:12 +000070 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
Fangrui Song6907ce22018-07-30 19:24:48 +000091ASTMergeAction::~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}