ASTMerge.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. StringRef InFile) {
  19. return AdaptedAction->CreateASTConsumer(CI, InFile);
  20. }
  21. bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
  22. 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->setCurrentInput(getCurrentInput(), 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().getLangOpts());
  34. CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
  35. &CI.getASTContext());
  36. IntrusiveRefCntPtr<DiagnosticIDs>
  37. DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
  38. for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
  39. IntrusiveRefCntPtr<DiagnosticsEngine>
  40. Diags(new DiagnosticsEngine(DiagIDs, CI.getDiagnostics().getClient(),
  41. /*ShouldOwnClient=*/false));
  42. ASTUnit *Unit = ASTUnit::LoadFromASTFile(ASTFiles[I], Diags,
  43. CI.getFileSystemOpts(), false);
  44. if (!Unit)
  45. continue;
  46. ASTImporter Importer(CI.getASTContext(),
  47. CI.getFileManager(),
  48. Unit->getASTContext(),
  49. Unit->getFileManager(),
  50. /*MinimalImport=*/false);
  51. TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
  52. for (DeclContext::decl_iterator D = TU->decls_begin(),
  53. DEnd = TU->decls_end();
  54. D != DEnd; ++D) {
  55. // Don't re-import __va_list_tag, __builtin_va_list.
  56. if (NamedDecl *ND = dyn_cast<NamedDecl>(*D))
  57. if (IdentifierInfo *II = ND->getIdentifier())
  58. if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
  59. continue;
  60. Importer.Import(*D);
  61. }
  62. delete Unit;
  63. }
  64. AdaptedAction->ExecuteAction();
  65. CI.getDiagnostics().getClient()->EndSourceFile();
  66. }
  67. void ASTMergeAction::EndSourceFileAction() {
  68. return AdaptedAction->EndSourceFileAction();
  69. }
  70. ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
  71. ArrayRef<std::string> ASTFiles)
  72. : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
  73. assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
  74. }
  75. ASTMergeAction::~ASTMergeAction() {
  76. delete AdaptedAction;
  77. }
  78. bool ASTMergeAction::usesPreprocessorOnly() const {
  79. return AdaptedAction->usesPreprocessorOnly();
  80. }
  81. TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
  82. return AdaptedAction->getTranslationUnitKind();
  83. }
  84. bool ASTMergeAction::hasPCHSupport() const {
  85. return AdaptedAction->hasPCHSupport();
  86. }
  87. bool ASTMergeAction::hasASTFileSupport() const {
  88. return AdaptedAction->hasASTFileSupport();
  89. }
  90. bool ASTMergeAction::hasCodeCompletionSupport() const {
  91. return AdaptedAction->hasCodeCompletionSupport();
  92. }