ASTMerge.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //===-- ASTMerge.cpp - AST Merging Frontend 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. 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. for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
  38. IntrusiveRefCntPtr<DiagnosticsEngine>
  39. Diags(new DiagnosticsEngine(DiagIDs, &CI.getDiagnosticOpts(),
  40. new ForwardingDiagnosticConsumer(
  41. *CI.getDiagnostics().getClient()),
  42. /*ShouldOwnClient=*/true));
  43. std::unique_ptr<ASTUnit> Unit = ASTUnit::LoadFromASTFile(
  44. ASTFiles[I], CI.getPCHContainerReader(), ASTUnit::LoadEverything, 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 (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. Decl *ToD = Importer.Import(D);
  61. if (ToD) {
  62. DeclGroupRef DGR(ToD);
  63. CI.getASTConsumer().HandleTopLevelDecl(DGR);
  64. }
  65. }
  66. }
  67. AdaptedAction->ExecuteAction();
  68. CI.getDiagnostics().getClient()->EndSourceFile();
  69. }
  70. void ASTMergeAction::EndSourceFileAction() {
  71. return AdaptedAction->EndSourceFileAction();
  72. }
  73. ASTMergeAction::ASTMergeAction(std::unique_ptr<FrontendAction> adaptedAction,
  74. ArrayRef<std::string> ASTFiles)
  75. : AdaptedAction(std::move(adaptedAction)), ASTFiles(ASTFiles.begin(), ASTFiles.end()) {
  76. assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
  77. }
  78. ASTMergeAction::~ASTMergeAction() {
  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. }