blob: fbb87669d9a76a52ef1cd847bedd52765e3baa1e [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"
15
16using namespace clang;
17
18ASTConsumer *ASTMergeAction::CreateASTConsumer(CompilerInstance &CI,
19 llvm::StringRef InFile) {
20 return AdaptedAction->CreateASTConsumer(CI, InFile);
21}
22
23bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
24 llvm::StringRef Filename) {
25 // FIXME: This is a hack. We need a better way to communicate the
26 // AST file, compiler instance, and file name than member variables
27 // of FrontendAction.
28 AdaptedAction->setCurrentFile(getCurrentFile(), takeCurrentASTUnit());
29 AdaptedAction->setCompilerInstance(&CI);
30 return AdaptedAction->BeginSourceFileAction(CI, Filename);
31}
32
33void ASTMergeAction::ExecuteAction() {
34 CompilerInstance &CI = getCompilerInstance();
Douglas Gregor0f962a82010-02-10 17:16:49 +000035 CI.getDiagnostics().getClient()->BeginSourceFile(
36 CI.getASTContext().getLangOptions());
Douglas Gregord343ff62010-02-09 22:37:58 +000037 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
38 &CI.getASTContext());
Douglas Gregor9bed8792010-02-09 19:21:46 +000039 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
Douglas Gregor4800d952010-02-11 19:21:55 +000040 ASTUnit *Unit = ASTUnit::LoadFromPCHFile(ASTFiles[I], CI.getDiagnostics(),
Douglas Gregor9bed8792010-02-09 19:21:46 +000041 false, true);
42 if (!Unit)
43 continue;
44
Douglas Gregor4800d952010-02-11 19:21:55 +000045 ASTImporter Importer(CI.getDiagnostics(),
46 CI.getASTContext(),
Douglas Gregor88523732010-02-10 00:15:17 +000047 CI.getFileManager(),
Douglas Gregor88523732010-02-10 00:15:17 +000048 Unit->getASTContext(),
Douglas Gregor4800d952010-02-11 19:21:55 +000049 Unit->getFileManager());
Douglas Gregor9bed8792010-02-09 19:21:46 +000050
51 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
52 for (DeclContext::decl_iterator D = TU->decls_begin(),
53 DEnd = TU->decls_end();
54 D != DEnd; ++D) {
Douglas Gregora404ea62010-02-10 19:54:31 +000055 // FIXME: We only merge variables whose names start with x and functions
56 // whose names start with 'f'. Why would anyone want anything else?
57 if (VarDecl *VD = dyn_cast<VarDecl>(*D)) {
Douglas Gregor9bed8792010-02-09 19:21:46 +000058 if (VD->getIdentifier() &&
59 *VD->getIdentifier()->getNameStart() == 'x') {
60 Decl *Merged = Importer.Import(VD);
Douglas Gregord343ff62010-02-09 22:37:58 +000061 (void)Merged;
Douglas Gregor9bed8792010-02-09 19:21:46 +000062 }
Douglas Gregora404ea62010-02-10 19:54:31 +000063 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*D)) {
64 if (FD->getIdentifier() &&
65 *FD->getIdentifier()->getNameStart() == 'f') {
66 Decl *Merged = Importer.Import(FD);
67 (void)Merged;
68 }
69 }
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,
84 std::string *ASTFiles, unsigned NumASTFiles)
85 : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
86 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
97bool ASTMergeAction::usesCompleteTranslationUnit() {
98 return AdaptedAction->usesCompleteTranslationUnit();
99}
100
101bool ASTMergeAction::hasPCHSupport() const {
102 return AdaptedAction->hasPCHSupport();
103}
104
105bool ASTMergeAction::hasASTSupport() const {
106 return AdaptedAction->hasASTSupport();
107}
108
109bool ASTMergeAction::hasCodeCompletionSupport() const {
110 return AdaptedAction->hasCodeCompletionSupport();
111}