ASTMerge.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // //
  17. ///////////////////////////////////////////////////////////////////////////////
  18. using namespace clang;
  19. std::unique_ptr<ASTConsumer>
  20. ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
  21. return AdaptedAction->CreateASTConsumer(CI, InFile);
  22. }
  23. bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
  24. StringRef Filename) {
  25. // FIXME: This is a hack. We need a better way to communicate the
  26. // AST file, compiler instance, and file name than member variables
  27. // of FrontendAction.
  28. AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit());
  29. AdaptedAction->setCompilerInstance(&CI);
  30. return AdaptedAction->BeginSourceFileAction(CI, Filename);
  31. }
  32. void ASTMergeAction::ExecuteAction() {
  33. CompilerInstance &CI = getCompilerInstance();
  34. CI.getDiagnostics().getClient()->BeginSourceFile(
  35. CI.getASTContext().getLangOpts());
  36. CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
  37. &CI.getASTContext());
  38. IntrusiveRefCntPtr<DiagnosticIDs>
  39. DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
  40. for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
  41. IntrusiveRefCntPtr<DiagnosticsEngine>
  42. Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
  43. new ForwardingDiagnosticConsumer(
  44. *CI.getDiagnostics().getClient()),
  45. /*ShouldOwnClient=*/true));
  46. std::unique_ptr<ASTUnit> Unit =
  47. ASTUnit::LoadFromASTFile(ASTFiles[I], CI.getPCHContainerReader(),
  48. Diags, CI.getFileSystemOpts(), false);
  49. if (!Unit)
  50. continue;
  51. ASTImporter Importer(CI.getASTContext(),
  52. CI.getFileManager(),
  53. Unit->getASTContext(),
  54. Unit->getFileManager(),
  55. /*MinimalImport=*/false);
  56. TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
  57. CI.getASTConsumer().Initialize(CI.getASTContext());
  58. for (auto *D : TU->decls()) {
  59. // Don't re-import __va_list_tag, __builtin_va_list.
  60. if (const auto *ND = dyn_cast<NamedDecl>(D))
  61. if (IdentifierInfo *II = ND->getIdentifier())
  62. if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
  63. continue;
  64. Decl *ToD = Importer.Import(D);
  65. if (ToD) {
  66. DeclGroupRef DGR(ToD);
  67. CI.getASTConsumer().HandleTopLevelDecl(DGR);
  68. }
  69. }
  70. }
  71. AdaptedAction->ExecuteAction();
  72. CI.getDiagnostics().getClient()->EndSourceFile();
  73. }
  74. void ASTMergeAction::EndSourceFileAction() {
  75. return AdaptedAction->EndSourceFileAction();
  76. }
  77. ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
  78. ArrayRef<std::string> ASTFiles)
  79. : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
  80. assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
  81. }
  82. ASTMergeAction::~ASTMergeAction() {
  83. delete AdaptedAction;
  84. }
  85. bool ASTMergeAction::usesPreprocessorOnly() const {
  86. return AdaptedAction->usesPreprocessorOnly();
  87. }
  88. TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
  89. return AdaptedAction->getTranslationUnitKind();
  90. }
  91. bool ASTMergeAction::hasPCHSupport() const {
  92. return AdaptedAction->hasPCHSupport();
  93. }
  94. bool ASTMergeAction::hasASTFileSupport() const {
  95. return AdaptedAction->hasASTFileSupport();
  96. }
  97. bool ASTMergeAction::hasCodeCompletionSupport() const {
  98. return AdaptedAction->hasCodeCompletionSupport();
  99. }