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