blob: b6c644eba7838ebe8e90578bf80605ef47c4d0f3 [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(
Douglas Gregora4a90ca2013-05-03 22:58:43 +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(),
Douglas Gregora4a90ca2013-05-03 22:58:43 +000045 new ForwardingDiagnosticConsumer(
46 *CI.getDiagnostics().getClient()),
47 /*ShouldOwnClient=*/true));
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +000048 ASTUnit *Unit = ASTUnit::LoadFromASTFile(ASTFiles[I], Diags,
49 CI.getFileSystemOpts(), false);
Douglas Gregor9bed8792010-02-09 19:21:46 +000050 if (!Unit)
51 continue;
52
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000053 ASTImporter Importer(CI.getASTContext(),
Douglas Gregor88523732010-02-10 00:15:17 +000054 CI.getFileManager(),
Douglas Gregor88523732010-02-10 00:15:17 +000055 Unit->getASTContext(),
Douglas Gregord8868a62011-01-18 03:11:38 +000056 Unit->getFileManager(),
57 /*MinimalImport=*/false);
Douglas Gregor9bed8792010-02-09 19:21:46 +000058
59 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
60 for (DeclContext::decl_iterator D = TU->decls_begin(),
61 DEnd = TU->decls_end();
62 D != DEnd; ++D) {
Douglas Gregor9a945852010-02-16 00:04:46 +000063 // Don't re-import __va_list_tag, __builtin_va_list.
64 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
65 if (IdentifierInfo *II = ND->getIdentifier())
66 if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
67 continue;
68
Douglas Gregor44703f52010-02-15 22:05:17 +000069 Importer.Import(*D);
Douglas Gregor9bed8792010-02-09 19:21:46 +000070 }
71
72 delete Unit;
73 }
74
Douglas Gregor0f962a82010-02-10 17:16:49 +000075 AdaptedAction->ExecuteAction();
76 CI.getDiagnostics().getClient()->EndSourceFile();
Douglas Gregor9bed8792010-02-09 19:21:46 +000077}
78
79void ASTMergeAction::EndSourceFileAction() {
80 return AdaptedAction->EndSourceFileAction();
81}
82
83ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
Argyrios Kyrtzidisb3ca2632012-02-04 01:36:04 +000084 ArrayRef<std::string> ASTFiles)
85 : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
Douglas Gregor9bed8792010-02-09 19:21:46 +000086 assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
87}
88
89ASTMergeAction::~ASTMergeAction() {
90 delete AdaptedAction;
91}
92
93bool ASTMergeAction::usesPreprocessorOnly() const {
94 return AdaptedAction->usesPreprocessorOnly();
95}
96
Douglas Gregor467dc882011-08-25 22:30:56 +000097TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
98 return AdaptedAction->getTranslationUnitKind();
Douglas Gregor9bed8792010-02-09 19:21:46 +000099}
100
101bool ASTMergeAction::hasPCHSupport() const {
102 return AdaptedAction->hasPCHSupport();
103}
104
Daniel Dunbareb58d832010-06-07 23:24:43 +0000105bool ASTMergeAction::hasASTFileSupport() const {
106 return AdaptedAction->hasASTFileSupport();
Douglas Gregor9bed8792010-02-09 19:21:46 +0000107}
108
109bool ASTMergeAction::hasCodeCompletionSupport() const {
110 return AdaptedAction->hasCodeCompletionSupport();
111}