ASTMerge.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(), takeCurrentASTUnit());
  27. AdaptedAction->setCompilerInstance(&CI);
  28. return AdaptedAction->BeginSourceFileAction(CI, Filename);
  29. }
  30. void ASTMergeAction::ExecuteAction() {
  31. CompilerInstance &CI = getCompilerInstance();
  32. CI.getDiagnostics().getClient()->BeginSourceFile(
  33. CI.getASTContext().getLangOptions());
  34. CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
  35. &CI.getASTContext());
  36. llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics());
  37. for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
  38. ASTUnit *Unit = ASTUnit::LoadFromPCHFile(ASTFiles[I], Diags, false);
  39. if (!Unit)
  40. continue;
  41. ASTImporter Importer(CI.getDiagnostics(),
  42. CI.getASTContext(),
  43. CI.getFileManager(),
  44. Unit->getASTContext(),
  45. Unit->getFileManager());
  46. TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
  47. for (DeclContext::decl_iterator D = TU->decls_begin(),
  48. DEnd = TU->decls_end();
  49. D != DEnd; ++D) {
  50. // Don't re-import __va_list_tag, __builtin_va_list.
  51. if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
  52. if (IdentifierInfo *II = ND->getIdentifier())
  53. if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
  54. continue;
  55. Importer.Import(*D);
  56. }
  57. delete Unit;
  58. }
  59. AdaptedAction->ExecuteAction();
  60. CI.getDiagnostics().getClient()->EndSourceFile();
  61. }
  62. void ASTMergeAction::EndSourceFileAction() {
  63. return AdaptedAction->EndSourceFileAction();
  64. }
  65. ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
  66. std::string *ASTFiles, unsigned NumASTFiles)
  67. : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
  68. assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
  69. }
  70. ASTMergeAction::~ASTMergeAction() {
  71. delete AdaptedAction;
  72. }
  73. bool ASTMergeAction::usesPreprocessorOnly() const {
  74. return AdaptedAction->usesPreprocessorOnly();
  75. }
  76. bool ASTMergeAction::usesCompleteTranslationUnit() {
  77. return AdaptedAction->usesCompleteTranslationUnit();
  78. }
  79. bool ASTMergeAction::hasPCHSupport() const {
  80. return AdaptedAction->hasPCHSupport();
  81. }
  82. bool ASTMergeAction::hasASTSupport() const {
  83. return AdaptedAction->hasASTSupport();
  84. }
  85. bool ASTMergeAction::hasCodeCompletionSupport() const {
  86. return AdaptedAction->hasCodeCompletionSupport();
  87. }