ASTMerge.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. //===-- ASTMerge.cpp - AST Merging Frontend Action --------------*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "clang/Frontend/ASTUnit.h"
  9. #include "clang/AST/ASTContext.h"
  10. #include "clang/AST/ASTDiagnostic.h"
  11. #include "clang/AST/ASTImporter.h"
  12. #include "clang/AST/ASTImporterSharedState.h"
  13. #include "clang/Basic/Diagnostic.h"
  14. #include "clang/Frontend/CompilerInstance.h"
  15. #include "clang/Frontend/FrontendActions.h"
  16. using namespace clang;
  17. std::unique_ptr<ASTConsumer>
  18. ASTMergeAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
  19. return AdaptedAction->CreateASTConsumer(CI, InFile);
  20. }
  21. bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI) {
  22. // FIXME: This is a hack. We need a better way to communicate the
  23. // AST file, compiler instance, and file name than member variables
  24. // of FrontendAction.
  25. AdaptedAction->setCurrentInput(getCurrentInput(), takeCurrentASTUnit());
  26. AdaptedAction->setCompilerInstance(&CI);
  27. return AdaptedAction->BeginSourceFileAction(CI);
  28. }
  29. void ASTMergeAction::ExecuteAction() {
  30. CompilerInstance &CI = getCompilerInstance();
  31. CI.getDiagnostics().getClient()->BeginSourceFile(
  32. CI.getASTContext().getLangOpts());
  33. CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
  34. &CI.getASTContext());
  35. IntrusiveRefCntPtr<DiagnosticIDs>
  36. DiagIDs(CI.getDiagnostics().getDiagnosticIDs());
  37. auto SharedState = std::make_shared<ASTImporterSharedState>(
  38. *CI.getASTContext().getTranslationUnitDecl());
  39. for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
  40. IntrusiveRefCntPtr<DiagnosticsEngine>
  41. Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
  42. new ForwardingDiagnosticConsumer(
  43. *CI.getDiagnostics().getClient()),
  44. /*ShouldOwnClient=*/true));
  45. std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
  46. ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, Diags,
  47. CI.getFileSystemOpts(), false);
  48. if (!Unit)
  49. continue;
  50. ASTImporter Importer(CI.getASTContext(), CI.getFileManager(),
  51. Unit->getASTContext(), Unit->getFileManager(),
  52. /*MinimalImport=*/false, SharedState);
  53. TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
  54. for (auto *D : TU->decls()) {
  55. // Don't re-import __va_list_tag, __builtin_va_list.
  56. if (const auto *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. llvm::Expected<Decl *> ToDOrError = Importer.Import(D);
  61. if (ToDOrError) {
  62. DeclGroupRef DGR(*ToDOrError);
  63. CI.getASTConsumer().HandleTopLevelDecl(DGR);
  64. } else {
  65. llvm::consumeError(ToDOrError.takeError());
  66. }
  67. }
  68. }
  69. AdaptedAction->ExecuteAction();
  70. CI.getDiagnostics().getClient()->EndSourceFile();
  71. }
  72. void ASTMergeAction::EndSourceFileAction() {
  73. return AdaptedAction->EndSourceFileAction();
  74. }
  75. ASTMergeAction::ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction,
  76. ArrayRef<std::string> ASTFiles)
  77. : AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
  78. assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
  79. }
  80. ASTMergeAction::~ASTMergeAction() {
  81. }
  82. bool ASTMergeAction::usesPreprocessorOnly() const {
  83. return AdaptedAction->usesPreprocessorOnly();
  84. }
  85. TranslationUnitKind ASTMergeAction::getTranslationUnitKind() {
  86. return AdaptedAction->getTranslationUnitKind();
  87. }
  88. bool ASTMergeAction::hasPCHSupport() const {
  89. return AdaptedAction->hasPCHSupport();
  90. }
  91. bool ASTMergeAction::hasASTFileSupport() const {
  92. return AdaptedAction->hasASTFileSupport();
  93. }
  94. bool ASTMergeAction::hasCodeCompletionSupport() const {
  95. return AdaptedAction->hasCodeCompletionSupport();
  96. }