blob: bfb30836d81964b0aa7ee91c0c90482701db6abe [file] [log] [blame]
Douglas Gregor9bed8792010-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 Gregor9bed8792010-02-09 19:21:46 +000010#include "clang/AST/ASTContext.h"
Douglas Gregord343ff62010-02-09 22:37:58 +000011#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor9bed8792010-02-09 19:21:46 +000012#include "clang/AST/ASTImporter.h"
Douglas Gregor28019772010-04-05 23:52:57 +000013#include "clang/Basic/Diagnostic.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000014#include "clang/Frontend/CompilerInstance.h"
15#include "clang/Frontend/FrontendActions.h"
Douglas Gregor9bed8792010-02-09 19:21:46 +000016
17using namespace clang;
18
19ASTConsumer *ASTMergeAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000020 StringRef InFile) {
Douglas Gregor9bed8792010-02-09 19:21:46 +000021 return AdaptedAction->CreateASTConsumer(CI, InFile);
22}
23
24bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
Chris Lattner5f9e2722011-07-23 10:55:15 +000025 StringRef Filename) {
Douglas Gregor9bed8792010-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 Gregor1f6b2b52012-01-20 16:28:04 +000029 AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit());
Douglas Gregor9bed8792010-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 Gregor0f962a82010-02-10 17:16:49 +000036 CI.getDiagnostics().getClient()->BeginSourceFile(
David Blaikie4e4d0842012-03-11 07:00:24 +000037 CI.getASTContext().getLangOpts());
Douglas Gregord343ff62010-02-09 22:37:58 +000038 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
39 &CI.getASTContext());
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000040 IntrusiveRefCntPtr<DiagnosticIDs>
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000041 DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
Douglas Gregor9bed8792010-02-09 19:21:46 +000042 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
Dylan Noblesmithc93dc782012-02-20 14:00:23 +000043 IntrusiveRefCntPtr<DiagnosticsEngine>
Douglas Gregor02c23eb2012-10-23 22:26:28 +000044 Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
45 CI.getDiagnostics().getClient(),
46 /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +000047 ASTUnit *Unit = ASTUnit::LoadFromASTFile(ASTFiles[I], Diags,
48 CI.getFileSystemOpts(), false);
Douglas Gregor9bed8792010-02-09 19:21:46 +000049 if (!Unit)
50 continue;
51
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000052 ASTImporter Importer(CI.getASTContext(),
Douglas Gregor88523732010-02-10 00:15:17 +000053 CI.getFileManager(),
Douglas Gregor88523732010-02-10 00:15:17 +000054 Unit->getASTContext(),
Douglas Gregord8868a62011-01-18 03:11:38 +000055 Unit->getFileManager(),
56 /*MinimalImport=*/false);
Douglas Gregor9bed8792010-02-09 19:21:46 +000057
58 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
59 for (DeclContext::decl_iterator D = TU->decls_begin(),
60 DEnd = TU->decls_end();
61 D != DEnd; ++D) {
Douglas Gregor9a945852010-02-16 00:04:46 +000062 // Don't re-import __va_list_tag, __builtin_va_list.
63 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
64 if (IdentifierInfo *II = ND->getIdentifier())
65 if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
66 continue;
67
Douglas Gregor44703f52010-02-15 22:05:17 +000068 Importer.Import(*D);
Douglas Gregor9bed8792010-02-09 19:21:46 +000069 }
70
71 delete Unit;
72 }
73
Douglas Gregor0f962a82010-02-10 17:16:49 +000074 AdaptedAction->ExecuteAction();
75 CI.getDiagnostics().getClient()->EndSourceFile();
Douglas Gregor9bed8792010-02-09 19:21:46 +000076}
77
78void ASTMergeAction::EndSourceFileAction() {
79 return AdaptedAction->EndSourceFileAction();
80}
81
82ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
Argyrios Kyrtzidisb3ca2632012-02-04 01:36:04 +000083 ArrayRef<std::string> ASTFiles)
84 : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
Douglas Gregor9bed8792010-02-09 19:21:46 +000085 assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
86}
87
88ASTMergeAction::~ASTMergeAction() {
89 delete AdaptedAction;
90}
91
92bool ASTMergeAction::usesPreprocessorOnly() const {
93 return AdaptedAction->usesPreprocessorOnly();
94}
95
Douglas Gregor467dc882011-08-25 22:30:56 +000096TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
97 return AdaptedAction->getTranslationUnitKind();
Douglas Gregor9bed8792010-02-09 19:21:46 +000098}
99
100bool ASTMergeAction::hasPCHSupport() const {
101 return AdaptedAction->hasPCHSupport();
102}
103
Daniel Dunbareb58d832010-06-07 23:24:43 +0000104bool ASTMergeAction::hasASTFileSupport() const {
105 return AdaptedAction->hasASTFileSupport();
Douglas Gregor9bed8792010-02-09 19:21:46 +0000106}
107
108bool ASTMergeAction::hasCodeCompletionSupport() const {
109 return AdaptedAction->hasCodeCompletionSupport();
110}