blob: a4104115f20a4338b0f1a1033bf6770accb2196b [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"
10#include "clang/Frontend/CompilerInstance.h"
11#include "clang/Frontend/FrontendActions.h"
12#include "clang/AST/ASTContext.h"
Douglas Gregord343ff62010-02-09 22:37:58 +000013#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor9bed8792010-02-09 19:21:46 +000014#include "clang/AST/ASTImporter.h"
Douglas Gregor28019772010-04-05 23:52:57 +000015#include "clang/Basic/Diagnostic.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(
37 CI.getASTContext().getLangOptions());
Douglas Gregord343ff62010-02-09 22:37:58 +000038 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
39 &CI.getASTContext());
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000040 llvm::IntrusiveRefCntPtr<DiagnosticIDs>
41 DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
Douglas Gregor9bed8792010-02-09 19:21:46 +000042 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
David Blaikied6471f72011-09-25 23:23:43 +000043 llvm::IntrusiveRefCntPtr<DiagnosticsEngine>
44 Diags(new DiagnosticsEngine(DiagIDs, CI.getDiagnostics().getClient(),
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000045 /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +000046 ASTUnit *Unit = ASTUnit::LoadFromASTFile(ASTFiles[I], Diags,
47 CI.getFileSystemOpts(), false);
Douglas Gregor9bed8792010-02-09 19:21:46 +000048 if (!Unit)
49 continue;
50
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000051 ASTImporter Importer(CI.getASTContext(),
Douglas Gregor88523732010-02-10 00:15:17 +000052 CI.getFileManager(),
Douglas Gregor88523732010-02-10 00:15:17 +000053 Unit->getASTContext(),
Douglas Gregord8868a62011-01-18 03:11:38 +000054 Unit->getFileManager(),
55 /*MinimalImport=*/false);
Douglas Gregor9bed8792010-02-09 19:21:46 +000056
57 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
58 for (DeclContext::decl_iterator D = TU->decls_begin(),
59 DEnd = TU->decls_end();
60 D != DEnd; ++D) {
Douglas Gregor9a945852010-02-16 00:04:46 +000061 // Don't re-import __va_list_tag, __builtin_va_list.
62 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
63 if (IdentifierInfo *II = ND->getIdentifier())
64 if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
65 continue;
66
Douglas Gregor44703f52010-02-15 22:05:17 +000067 Importer.Import(*D);
Douglas Gregor9bed8792010-02-09 19:21:46 +000068 }
69
70 delete Unit;
71 }
72
Douglas Gregor0f962a82010-02-10 17:16:49 +000073 AdaptedAction->ExecuteAction();
74 CI.getDiagnostics().getClient()->EndSourceFile();
Douglas Gregor9bed8792010-02-09 19:21:46 +000075}
76
77void ASTMergeAction::EndSourceFileAction() {
78 return AdaptedAction->EndSourceFileAction();
79}
80
81ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
82 std::string *ASTFiles, unsigned NumASTFiles)
83 : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
84 assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
85}
86
87ASTMergeAction::~ASTMergeAction() {
88 delete AdaptedAction;
89}
90
91bool ASTMergeAction::usesPreprocessorOnly() const {
92 return AdaptedAction->usesPreprocessorOnly();
93}
94
Douglas Gregor467dc882011-08-25 22:30:56 +000095TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
96 return AdaptedAction->getTranslationUnitKind();
Douglas Gregor9bed8792010-02-09 19:21:46 +000097}
98
99bool ASTMergeAction::hasPCHSupport() const {
100 return AdaptedAction->hasPCHSupport();
101}
102
Daniel Dunbareb58d832010-06-07 23:24:43 +0000103bool ASTMergeAction::hasASTFileSupport() const {
104 return AdaptedAction->hasASTFileSupport();
Douglas Gregor9bed8792010-02-09 19:21:46 +0000105}
106
107bool ASTMergeAction::hasCodeCompletionSupport() const {
108 return AdaptedAction->hasCodeCompletionSupport();
109}