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/AST/ASTContext.h" 11 #include "clang/AST/ASTDiagnostic.h" 12 #include "clang/AST/ASTImporter.h" 13 #include "clang/Basic/Diagnostic.h" 14 #include "clang/Frontend/CompilerInstance.h" 15 #include "clang/Frontend/FrontendActions.h" 16 17 using namespace clang; 18 19 std::unique_ptr<ASTConsumer> 20 ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) { 21 return AdaptedAction->CreateASTConsumer(CI, InFile); 22 } 23 24 bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI, 25 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. 29 AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit()); 30 AdaptedAction->setCompilerInstance(&CI); 31 return AdaptedAction->BeginSourceFileAction(CI, Filename); 32 } 33 34 void ASTMergeAction::ExecuteAction() { 35 CompilerInstance &CI = getCompilerInstance(); 36 CI.getDiagnostics().getClient()->BeginSourceFile( 37 CI.getASTContext().getLangOpts()); 38 CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument, 39 &CI.getASTContext()); 40 IntrusiveRefCntPtr<DiagnosticIDs> 41 DiagIDs(CI.getDiagnostics().getDiagnosticIDs()); 42 for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) { 43 IntrusiveRefCntPtr<DiagnosticsEngine> 44 Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(), 45 new ForwardingDiagnosticConsumer( 46 *CI.getDiagnostics().getClient()), 47 /*ShouldOwnClient=*/true)); 48 std::unique_ptr<ASTUnit> Unit = 49 ASTUnit::LoadFromASTFile(ASTFiles[I], CI.getPCHContainerOperations(), 50 Diags, CI.getFileSystemOpts(), false); 51 52 if (!Unit) 53 continue; 54 55 ASTImporter Importer(CI.getASTContext(), 56 CI.getFileManager(), 57 Unit->getASTContext(), 58 Unit->getFileManager(), 59 /*MinimalImport=*/false); 60 61 TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl(); 62 CI.getASTConsumer().Initialize(CI.getASTContext()); 63 for (auto *D : TU->decls()) { 64 // Don't re-import __va_list_tag, __builtin_va_list. 65 if (const auto *ND = dyn_cast<NamedDecl>(D)) 66 if (IdentifierInfo *II = ND->getIdentifier()) 67 if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list")) 68 continue; 69 70 Decl *ToD = Importer.Import(D); 71 72 if (ToD) { 73 DeclGroupRef DGR(ToD); 74 CI.getASTConsumer().HandleTopLevelDecl(DGR); 75 } 76 } 77 } 78 79 AdaptedAction->ExecuteAction(); 80 CI.getDiagnostics().getClient()->EndSourceFile(); 81 } 82 83 void ASTMergeAction::EndSourceFileAction() { 84 return AdaptedAction->EndSourceFileAction(); 85 } 86 87 ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction, 88 ArrayRef<std::string> ASTFiles) 89 : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles.begin(), ASTFiles.end()) { 90 assert(AdaptedAction && "ASTMergeAction needs an action to adapt"); 91 } 92 93 ASTMergeAction::~ASTMergeAction() { 94 delete AdaptedAction; 95 } 96 97 bool ASTMergeAction::usesPreprocessorOnly() const { 98 return AdaptedAction->usesPreprocessorOnly(); 99 } 100 101 TranslationUnitKind ASTMergeAction::getTranslationUnitKind() { 102 return AdaptedAction->getTranslationUnitKind(); 103 } 104 105 bool ASTMergeAction::hasPCHSupport() const { 106 return AdaptedAction->hasPCHSupport(); 107 } 108 109 bool ASTMergeAction::hasASTFileSupport() const { 110 return AdaptedAction->hasASTFileSupport(); 111 } 112 113 bool ASTMergeAction::hasCodeCompletionSupport() const { 114 return AdaptedAction->hasCodeCompletionSupport(); 115 } 116