blob: d51647bb161d03ddf109028f2c3a70ee2d59df9b [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 Gregord343ff62010-02-09 22:37:58 +000035 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
36 &CI.getASTContext());
Douglas Gregor9bed8792010-02-09 19:21:46 +000037 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
Douglas Gregord343ff62010-02-09 22:37:58 +000038 Diagnostic ASTDiags(CI.getDiagnostics().getClient());
39
40 ASTUnit *Unit = ASTUnit::LoadFromPCHFile(ASTFiles[I], ASTDiags,
Douglas Gregor9bed8792010-02-09 19:21:46 +000041 false, true);
42 if (!Unit)
43 continue;
44
Douglas Gregord343ff62010-02-09 22:37:58 +000045 ASTDiags.SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
46 &Unit->getASTContext());
Douglas Gregor88523732010-02-10 00:15:17 +000047 ASTImporter Importer(CI.getASTContext(),
48 CI.getFileManager(),
49 CI.getDiagnostics(),
50 Unit->getASTContext(),
51 Unit->getFileManager(),
52 ASTDiags);
Douglas Gregor9bed8792010-02-09 19:21:46 +000053
54 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
55 for (DeclContext::decl_iterator D = TU->decls_begin(),
56 DEnd = TU->decls_end();
57 D != DEnd; ++D) {
58 // FIXME: We only merge variables whose names start with x. Why
59 // would anyone want anything else?
60 if (VarDecl *VD = dyn_cast<VarDecl>(*D))
61 if (VD->getIdentifier() &&
62 *VD->getIdentifier()->getNameStart() == 'x') {
63 Decl *Merged = Importer.Import(VD);
Douglas Gregord343ff62010-02-09 22:37:58 +000064 (void)Merged;
Douglas Gregor9bed8792010-02-09 19:21:46 +000065 }
66 }
67
68 delete Unit;
69 }
70
71
72 return AdaptedAction->ExecuteAction();
73}
74
75void ASTMergeAction::EndSourceFileAction() {
76 return AdaptedAction->EndSourceFileAction();
77}
78
79ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
80 std::string *ASTFiles, unsigned NumASTFiles)
81 : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
82 assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
83}
84
85ASTMergeAction::~ASTMergeAction() {
86 delete AdaptedAction;
87}
88
89bool ASTMergeAction::usesPreprocessorOnly() const {
90 return AdaptedAction->usesPreprocessorOnly();
91}
92
93bool ASTMergeAction::usesCompleteTranslationUnit() {
94 return AdaptedAction->usesCompleteTranslationUnit();
95}
96
97bool ASTMergeAction::hasPCHSupport() const {
98 return AdaptedAction->hasPCHSupport();
99}
100
101bool ASTMergeAction::hasASTSupport() const {
102 return AdaptedAction->hasASTSupport();
103}
104
105bool ASTMergeAction::hasCodeCompletionSupport() const {
106 return AdaptedAction->hasCodeCompletionSupport();
107}