blob: f2708463430533dbdc610a241535d190426afa7d [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,
20 llvm::StringRef InFile) {
21 return AdaptedAction->CreateASTConsumer(CI, InFile);
22}
23
24bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
25 llvm::StringRef Filename) {
26 // 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.
Daniel Dunbarfcb2e6d2010-06-07 23:25:49 +000029 AdaptedAction->setCurrentFile(getCurrentFile(), getCurrentFileKind(),
30 takeCurrentASTUnit());
Douglas Gregor62d311f2010-02-09 19:21:46 +000031 AdaptedAction->setCompilerInstance(&CI);
32 return AdaptedAction->BeginSourceFileAction(CI, Filename);
33}
34
35void ASTMergeAction::ExecuteAction() {
36 CompilerInstance &CI = getCompilerInstance();
Douglas Gregor2fbe5582010-02-10 17:16:49 +000037 CI.getDiagnostics().getClient()->BeginSourceFile(
38 CI.getASTContext().getLangOptions());
Douglas Gregor6b2a4742010-02-09 22:37:58 +000039 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
40 &CI.getASTContext());
Douglas Gregor7f95d262010-04-05 23:52:57 +000041 llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics());
Douglas Gregor62d311f2010-02-09 19:21:46 +000042 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +000043 ASTUnit *Unit = ASTUnit::LoadFromASTFile(ASTFiles[I], Diags,
44 CI.getFileSystemOpts(), false);
Douglas Gregor62d311f2010-02-09 19:21:46 +000045 if (!Unit)
46 continue;
47
Douglas Gregor6fd55e02010-08-13 03:15:25 +000048 // Reset the argument -> string function so that it has the AST
49 // context we want, since the Sema object created by
Sebastian Redld44cd6a2010-08-18 23:57:06 +000050 // LoadFromASTFile will override it.
Douglas Gregor6fd55e02010-08-13 03:15:25 +000051 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
52 &CI.getASTContext());
53
Douglas Gregor7eeb5972010-02-11 19:21:55 +000054 ASTImporter Importer(CI.getDiagnostics(),
55 CI.getASTContext(),
Douglas Gregor811663e2010-02-10 00:15:17 +000056 CI.getFileManager(),
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +000057 CI.getFileSystemOpts(),
Douglas Gregor811663e2010-02-10 00:15:17 +000058 Unit->getASTContext(),
Argyrios Kyrtzidis71731d62010-11-03 22:45:23 +000059 Unit->getFileManager(),
60 Unit->getFileSystemOpts());
Douglas Gregor62d311f2010-02-09 19:21:46 +000061
62 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
63 for (DeclContext::decl_iterator D = TU->decls_begin(),
64 DEnd = TU->decls_end();
65 D != DEnd; ++D) {
Douglas Gregor9503c462010-02-16 00:04:46 +000066 // Don't re-import __va_list_tag, __builtin_va_list.
67 if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
68 if (IdentifierInfo *II = ND->getIdentifier())
69 if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
70 continue;
71
Douglas Gregor522e22d2010-02-15 22:05:17 +000072 Importer.Import(*D);
Douglas Gregor62d311f2010-02-09 19:21:46 +000073 }
74
75 delete Unit;
76 }
77
Douglas Gregor2fbe5582010-02-10 17:16:49 +000078 AdaptedAction->ExecuteAction();
79 CI.getDiagnostics().getClient()->EndSourceFile();
Douglas Gregor62d311f2010-02-09 19:21:46 +000080}
81
82void ASTMergeAction::EndSourceFileAction() {
83 return AdaptedAction->EndSourceFileAction();
84}
85
86ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
87 std::string *ASTFiles, unsigned NumASTFiles)
88 : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
89 assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
90}
91
92ASTMergeAction::~ASTMergeAction() {
93 delete AdaptedAction;
94}
95
96bool ASTMergeAction::usesPreprocessorOnly() const {
97 return AdaptedAction->usesPreprocessorOnly();
98}
99
100bool ASTMergeAction::usesCompleteTranslationUnit() {
101 return AdaptedAction->usesCompleteTranslationUnit();
102}
103
104bool ASTMergeAction::hasPCHSupport() const {
105 return AdaptedAction->hasPCHSupport();
106}
107
Daniel Dunbarfa6214c2010-06-07 23:24:43 +0000108bool ASTMergeAction::hasASTFileSupport() const {
109 return AdaptedAction->hasASTFileSupport();
Douglas Gregor62d311f2010-02-09 19:21:46 +0000110}
111
112bool ASTMergeAction::hasCodeCompletionSupport() const {
113 return AdaptedAction->hasCodeCompletionSupport();
114}