blob: fd34ddae12bca3e737afbe1b2a3efe831568971d [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 Gregord343ff62010-02-09 22:37:58 +000040 Diagnostic ASTDiags(CI.getDiagnostics().getClient());
41
42 ASTUnit *Unit = ASTUnit::LoadFromPCHFile(ASTFiles[I], ASTDiags,
Douglas Gregor9bed8792010-02-09 19:21:46 +000043 false, true);
44 if (!Unit)
45 continue;
46
Douglas Gregord343ff62010-02-09 22:37:58 +000047 ASTDiags.SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
48 &Unit->getASTContext());
Douglas Gregor88523732010-02-10 00:15:17 +000049 ASTImporter Importer(CI.getASTContext(),
50 CI.getFileManager(),
51 CI.getDiagnostics(),
52 Unit->getASTContext(),
53 Unit->getFileManager(),
54 ASTDiags);
Douglas Gregor9bed8792010-02-09 19:21:46 +000055
56 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
57 for (DeclContext::decl_iterator D = TU->decls_begin(),
58 DEnd = TU->decls_end();
59 D != DEnd; ++D) {
Douglas Gregora404ea62010-02-10 19:54:31 +000060 // FIXME: We only merge variables whose names start with x and functions
61 // whose names start with 'f'. Why would anyone want anything else?
62 if (VarDecl *VD = dyn_cast<VarDecl>(*D)) {
Douglas Gregor9bed8792010-02-09 19:21:46 +000063 if (VD->getIdentifier() &&
64 *VD->getIdentifier()->getNameStart() == 'x') {
65 Decl *Merged = Importer.Import(VD);
Douglas Gregord343ff62010-02-09 22:37:58 +000066 (void)Merged;
Douglas Gregor9bed8792010-02-09 19:21:46 +000067 }
Douglas Gregora404ea62010-02-10 19:54:31 +000068 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*D)) {
69 if (FD->getIdentifier() &&
70 *FD->getIdentifier()->getNameStart() == 'f') {
71 Decl *Merged = Importer.Import(FD);
72 (void)Merged;
73 }
74 }
Douglas Gregor9bed8792010-02-09 19:21:46 +000075 }
76
77 delete Unit;
78 }
79
Douglas Gregor0f962a82010-02-10 17:16:49 +000080 AdaptedAction->ExecuteAction();
81 CI.getDiagnostics().getClient()->EndSourceFile();
Douglas Gregor9bed8792010-02-09 19:21:46 +000082}
83
84void ASTMergeAction::EndSourceFileAction() {
85 return AdaptedAction->EndSourceFileAction();
86}
87
88ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
89 std::string *ASTFiles, unsigned NumASTFiles)
90 : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
91 assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
92}
93
94ASTMergeAction::~ASTMergeAction() {
95 delete AdaptedAction;
96}
97
98bool ASTMergeAction::usesPreprocessorOnly() const {
99 return AdaptedAction->usesPreprocessorOnly();
100}
101
102bool ASTMergeAction::usesCompleteTranslationUnit() {
103 return AdaptedAction->usesCompleteTranslationUnit();
104}
105
106bool ASTMergeAction::hasPCHSupport() const {
107 return AdaptedAction->hasPCHSupport();
108}
109
110bool ASTMergeAction::hasASTSupport() const {
111 return AdaptedAction->hasASTSupport();
112}
113
114bool ASTMergeAction::hasCodeCompletionSupport() const {
115 return AdaptedAction->hasCodeCompletionSupport();
116}