blob: 3905b99b02a449b9530961fe6d919d2e56071ca5 [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,
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 Dunbar685ac662010-06-07 23:25:49 +000029 AdaptedAction->setCurrentFile(getCurrentFile(), getCurrentFileKind(),
30 takeCurrentASTUnit());
Douglas Gregor9bed8792010-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 Gregor0f962a82010-02-10 17:16:49 +000037 CI.getDiagnostics().getClient()->BeginSourceFile(
38 CI.getASTContext().getLangOptions());
Douglas Gregord343ff62010-02-09 22:37:58 +000039 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
40 &CI.getASTContext());
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000041 llvm::IntrusiveRefCntPtr<DiagnosticIDs>
42 DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
Douglas Gregor9bed8792010-02-09 19:21:46 +000043 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
Argyrios Kyrtzidis33e4e702010-11-18 20:06:41 +000044 llvm::IntrusiveRefCntPtr<Diagnostic>
45 Diags(new Diagnostic(DiagIDs, 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,
83 std::string *ASTFiles, unsigned NumASTFiles)
84 : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
85 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
96bool ASTMergeAction::usesCompleteTranslationUnit() {
97 return AdaptedAction->usesCompleteTranslationUnit();
98}
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}