ASTMerge.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #include "clang/Basic/Diagnostic.h"
  16. using namespace clang;
  17. ASTConsumer *ASTMergeAction::CreateASTConsumer(CompilerInstance &CI,
  18. llvm::StringRef InFile) {
  19. return AdaptedAction->CreateASTConsumer(CI, InFile);
  20. }
  21. bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
  22. llvm::StringRef Filename) {
  23. // FIXME: This is a hack. We need a better way to communicate the
  24. // AST file, compiler instance, and file name than member variables
  25. // of FrontendAction.
  26. AdaptedAction->setCurrentFile(getCurrentFile(), getCurrentFileKind(),
  27. takeCurrentASTUnit());
  28. AdaptedAction->setCompilerInstance(&CI);
  29. return AdaptedAction->BeginSourceFileAction(CI, Filename);
  30. }
  31. void ASTMergeAction::ExecuteAction() {
  32. CompilerInstance &CI = getCompilerInstance();
  33. CI.getDiagnostics().getClient()->BeginSourceFile(
  34. CI.getASTContext().getLangOptions());
  35. CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
  36. &CI.getASTContext());
  37. llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics());
  38. for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
  39. ASTUnit *Unit = ASTUnit::LoadFromPCHFile(ASTFiles[I], Diags, false);
  40. if (!Unit)
  41. continue;
  42. // Reset the argument -> string function so that it has the AST
  43. // context we want, since the Sema object created by
  44. // LoadFromPCHFile will override it.
  45. CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
  46. &CI.getASTContext());
  47. ASTImporter Importer(CI.getDiagnostics(),
  48. CI.getASTContext(),
  49. CI.getFileManager(),
  50. Unit->getASTContext(),
  51. Unit->getFileManager());
  52. TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
  53. for (DeclContext::decl_iterator D = TU->decls_begin(),
  54. DEnd = TU->decls_end();
  55. D != DEnd; ++D) {
  56. // Don't re-import __va_list_tag, __builtin_va_list.
  57. if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
  58. if (IdentifierInfo *II = ND->getIdentifier())
  59. if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
  60. continue;
  61. Importer.Import(*D);
  62. }
  63. delete Unit;
  64. }
  65. AdaptedAction->ExecuteAction();
  66. CI.getDiagnostics().getClient()->EndSourceFile();
  67. }
  68. void ASTMergeAction::EndSourceFileAction() {
  69. return AdaptedAction->EndSourceFileAction();
  70. }
  71. ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
  72. std::string *ASTFiles, unsigned NumASTFiles)
  73. : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
  74. assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
  75. }
  76. ASTMergeAction::~ASTMergeAction() {
  77. delete AdaptedAction;
  78. }
  79. bool ASTMergeAction::usesPreprocessorOnly() const {
  80. return AdaptedAction->usesPreprocessorOnly();
  81. }
  82. bool ASTMergeAction::usesCompleteTranslationUnit() {
  83. return AdaptedAction->usesCompleteTranslationUnit();
  84. }
  85. bool ASTMergeAction::hasPCHSupport() const {
  86. return AdaptedAction->hasPCHSupport();
  87. }
  88. bool ASTMergeAction::hasASTFileSupport() const {
  89. return AdaptedAction->hasASTFileSupport();
  90. }
  91. bool ASTMergeAction::hasCodeCompletionSupport() const {
  92. return AdaptedAction->hasCodeCompletionSupport();
  93. }