clang API Documentation

ASTMerge.cpp
Go to the documentation of this file.
00001 //===-- ASTMerge.cpp - AST Merging Frontent Action --------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 #include "clang/Frontend/ASTUnit.h"
00010 #include "clang/AST/ASTContext.h"
00011 #include "clang/AST/ASTDiagnostic.h"
00012 #include "clang/AST/ASTImporter.h"
00013 #include "clang/Basic/Diagnostic.h"
00014 #include "clang/Frontend/CompilerInstance.h"
00015 #include "clang/Frontend/FrontendActions.h"
00016 
00017 using namespace clang;
00018 
00019 std::unique_ptr<ASTConsumer>
00020 ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
00021   return AdaptedAction->CreateASTConsumer(CI, InFile);
00022 }
00023 
00024 bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
00025                                            StringRef Filename) {
00026   // FIXME: This is a hack. We need a better way to communicate the
00027   // AST file, compiler instance, and file name than member variables
00028   // of FrontendAction.
00029   AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit());
00030   AdaptedAction->setCompilerInstance(&CI);
00031   return AdaptedAction->BeginSourceFileAction(CI, Filename);
00032 }
00033 
00034 void ASTMergeAction::ExecuteAction() {
00035   CompilerInstance &CI = getCompilerInstance();
00036   CI.getDiagnostics().getClient()->BeginSourceFile(
00037                                              CI.getASTContext().getLangOpts());
00038   CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
00039                                        &CI.getASTContext());
00040   IntrusiveRefCntPtr<DiagnosticIDs>
00041       DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
00042   for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
00043     IntrusiveRefCntPtr<DiagnosticsEngine>
00044         Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
00045                                     new ForwardingDiagnosticConsumer(
00046                                           *CI.getDiagnostics().getClient()),
00047                                     /*ShouldOwnClient=*/true));
00048     std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
00049         ASTFiles[I], Diags, CI.getFileSystemOpts(), false);
00050     if (!Unit)
00051       continue;
00052 
00053     ASTImporter Importer(CI.getASTContext(), 
00054                          CI.getFileManager(),
00055                          Unit->getASTContext(), 
00056                          Unit->getFileManager(),
00057                          /*MinimalImport=*/false);
00058 
00059     TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
00060     for (auto *D : TU->decls()) {
00061       // Don't re-import __va_list_tag, __builtin_va_list.
00062       if (const auto *ND = dyn_cast<NamedDecl>(D))
00063         if (IdentifierInfo *II = ND->getIdentifier())
00064           if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
00065             continue;
00066       
00067       Importer.Import(D);
00068     }
00069   }
00070 
00071   AdaptedAction->ExecuteAction();
00072   CI.getDiagnostics().getClient()->EndSourceFile();
00073 }
00074 
00075 void ASTMergeAction::EndSourceFileAction() {
00076   return AdaptedAction->EndSourceFileAction();
00077 }
00078 
00079 ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
00080                                ArrayRef<std::string> ASTFiles)
00081   : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
00082   assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
00083 }
00084 
00085 ASTMergeAction::~ASTMergeAction() { 
00086   delete AdaptedAction;
00087 }
00088 
00089 bool ASTMergeAction::usesPreprocessorOnly() const {
00090   return AdaptedAction->usesPreprocessorOnly();
00091 }
00092 
00093 TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
00094   return AdaptedAction->getTranslationUnitKind();
00095 }
00096 
00097 bool ASTMergeAction::hasPCHSupport() const {
00098   return AdaptedAction->hasPCHSupport();
00099 }
00100 
00101 bool ASTMergeAction::hasASTFileSupport() const {
00102   return AdaptedAction->hasASTFileSupport();
00103 }
00104 
00105 bool ASTMergeAction::hasCodeCompletionSupport() const {
00106   return AdaptedAction->hasCodeCompletionSupport();
00107 }