blob: f3e6ea85be839161d0ed4ca3ed225842fe879b1a [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"
10#include "clang/Frontend/CompilerInstance.h"
11#include "clang/Frontend/FrontendActions.h"
12#include "clang/AST/ASTContext.h"
Douglas Gregor6b2a4742010-02-09 22:37:58 +000013#include "clang/AST/ASTDiagnostic.h"
Douglas Gregor62d311f2010-02-09 19:21:46 +000014#include "clang/AST/ASTImporter.h"
Douglas Gregor7f95d262010-04-05 23:52:57 +000015#include "clang/Basic/Diagnostic.h"
Douglas Gregor62d311f2010-02-09 19:21:46 +000016
17using namespace clang;
18
19ASTConsumer *ASTMergeAction::CreateASTConsumer(CompilerInstance &CI,
Chris Lattner0e62c1c2011-07-23 10:55:15 +000020 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(
37 CI.getASTContext().getLangOptions());
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>
David Blaikie9c902b52011-09-25 23:23:43 +000044 Diags(new DiagnosticsEngine(DiagIDs, CI.getDiagnostics().getClient(),
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000045 /*ShouldOwnClient=*/false));
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +000046 ASTUnit *Unit = ASTUnit::LoadFromASTFile(ASTFiles[I], Diags,
47 CI.getFileSystemOpts(), false);
Douglas Gregor62d311f2010-02-09 19:21:46 +000048 if (!Unit)
49 continue;
50
Argyrios Kyrtzidisd0040642010-11-18 20:06:41 +000051 ASTImporter Importer(CI.getASTContext(),
Douglas Gregor811663e2010-02-10 00:15:17 +000052 CI.getFileManager(),
Douglas Gregor811663e2010-02-10 00:15:17 +000053 Unit->getASTContext(),
Douglas Gregor0a791672011-01-18 03:11:38 +000054 Unit->getFileManager(),
55 /*MinimalImport=*/false);
Douglas Gregor62d311f2010-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 Gregor9503c462010-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 Gregor522e22d2010-02-15 22:05:17 +000067 Importer.Import(*D);
Douglas Gregor62d311f2010-02-09 19:21:46 +000068 }
69
70 delete Unit;
71 }
72
Douglas Gregor2fbe5582010-02-10 17:16:49 +000073 AdaptedAction->ExecuteAction();
74 CI.getDiagnostics().getClient()->EndSourceFile();
Douglas Gregor62d311f2010-02-09 19:21:46 +000075}
76
77void ASTMergeAction::EndSourceFileAction() {
78 return AdaptedAction->EndSourceFileAction();
79}
80
81ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
Argyrios Kyrtzidis3d97a9b2012-02-04 01:36:04 +000082 ArrayRef<std::string> ASTFiles)
83 : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
Douglas Gregor62d311f2010-02-09 19:21:46 +000084 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 Gregor69f74f82011-08-25 22:30:56 +000095TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
96 return AdaptedAction->getTranslationUnitKind();
Douglas Gregor62d311f2010-02-09 19:21:46 +000097}
98
99bool ASTMergeAction::hasPCHSupport() const {
100 return AdaptedAction->hasPCHSupport();
101}
102
Daniel Dunbarfa6214c2010-06-07 23:24:43 +0000103bool ASTMergeAction::hasASTFileSupport() const {
104 return AdaptedAction->hasASTFileSupport();
Douglas Gregor62d311f2010-02-09 19:21:46 +0000105}
106
107bool ASTMergeAction::hasCodeCompletionSupport() const {
108 return AdaptedAction->hasCodeCompletionSupport();
109}