1 //===-- 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" 13 #include "clang/AST/ASTDiagnostic.h" 14 #include "clang/AST/ASTImporter.h" 15 16 using namespace clang; 17 18 ASTConsumer *ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, 19 llvm::StringRef InFile) { 20 return AdaptedAction->CreateASTConsumer(CI, InFile); 21 } 22 23 bool 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 33 void ASTMergeAction::ExecuteAction() { 34 CompilerInstance &CI = getCompilerInstance(); 35 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, 36 &CI.getASTContext()); 37 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) { 38 Diagnostic ASTDiags(CI.getDiagnostics().getClient()); 39 40 ASTUnit *Unit = ASTUnit::LoadFromPCHFile(ASTFiles[I], ASTDiags, 41 false, true); 42 if (!Unit) 43 continue; 44 45 ASTDiags.SetArgToStringFn(&FormatASTNodeDiagnosticArgument, 46 &Unit->getASTContext()); 47 ASTImporter Importer(CI.getASTContext(), 48 CI.getFileManager(), 49 CI.getDiagnostics(), 50 Unit->getASTContext(), 51 Unit->getFileManager(), 52 ASTDiags); 53 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); 64 (void)Merged; 65 } 66 } 67 68 delete Unit; 69 } 70 71 72 return AdaptedAction->ExecuteAction(); 73 } 74 75 void ASTMergeAction::EndSourceFileAction() { 76 return AdaptedAction->EndSourceFileAction(); 77 } 78 79 ASTMergeAction::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 85 ASTMergeAction::~ASTMergeAction() { 86 delete AdaptedAction; 87 } 88 89 bool ASTMergeAction::usesPreprocessorOnly() const { 90 return AdaptedAction->usesPreprocessorOnly(); 91 } 92 93 bool ASTMergeAction::usesCompleteTranslationUnit() { 94 return AdaptedAction->usesCompleteTranslationUnit(); 95 } 96 97 bool ASTMergeAction::hasPCHSupport() const { 98 return AdaptedAction->hasPCHSupport(); 99 } 100 101 bool ASTMergeAction::hasASTSupport() const { 102 return AdaptedAction->hasASTSupport(); 103 } 104 105 bool ASTMergeAction::hasCodeCompletionSupport() const { 106 return AdaptedAction->hasCodeCompletionSupport(); 107 } 108