ASTMerge.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. llvm::StringRef InFile) {
  19. return AdaptedAction->CreateASTConsumer(CI, InFile);
  20. }
  21. bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI,
  22. llvm::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->setCurrentFile(getCurrentFile(), getCurrentFileKind(),
  27. takeCurrentASTUnit());
  28. AdaptedAction->setCompilerInstance(&CI);
  29. return AdaptedAction->BeginSourceFileAction(CI, Filename);
  30. }
  31. void ASTMergeAction::ExecuteAction() {
  32. CompilerInstance &CI = getCompilerInstance();
  33. CI.getDiagnostics().getClient()->BeginSourceFile(
  34. CI.getASTContext().getLangOptions());
  35. CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
  36. &CI.getASTContext());
  37. llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics());
  38. for (unsigned I = 0, N = ASTFiles.size(); I != N; ++I) {
  39. ASTUnit *Unit = ASTUnit::LoadFromASTFile(ASTFiles[I], Diags,
  40. CI.getFileSystemOpts(), false);
  41. if (!Unit)
  42. continue;
  43. // Reset the argument -> string function so that it has the AST
  44. // context we want, since the Sema object created by
  45. // LoadFromASTFile will override it.
  46. CI.getDiagnostics().SetArgToStringFn(&FormatASTNodeDiagnosticArgument,
  47. &CI.getASTContext());
  48. ASTImporter Importer(CI.getDiagnostics(),
  49. CI.getASTContext(),
  50. CI.getFileManager(),
  51. CI.getFileSystemOpts(),
  52. Unit->getASTContext(),
  53. Unit->getFileManager(),
  54. Unit->getFileSystemOpts());
  55. TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
  56. for (DeclContext::decl_iterator D = TU->decls_begin(),
  57. DEnd = TU->decls_end();
  58. D != DEnd; ++D) {
  59. // Don't re-import __va_list_tag, __builtin_va_list.
  60. if (NamedDecl *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. Importer.Import(*D);
  65. }
  66. delete Unit;
  67. }
  68. AdaptedAction->ExecuteAction();
  69. CI.getDiagnostics().getClient()->EndSourceFile();
  70. }
  71. void ASTMergeAction::EndSourceFileAction() {
  72. return AdaptedAction->EndSourceFileAction();
  73. }
  74. ASTMergeAction::ASTMergeAction(FrontendAction *AdaptedAction,
  75. std::string *ASTFiles, unsigned NumASTFiles)
  76. : AdaptedAction(AdaptedAction), ASTFiles(ASTFiles, ASTFiles + NumASTFiles) {
  77. assert(AdaptedAction && "ASTMergeAction needs an action to adapt");
  78. }
  79. ASTMergeAction::~ASTMergeAction() {
  80. delete AdaptedAction;
  81. }
  82. bool ASTMergeAction::usesPreprocessorOnly() const {
  83. return AdaptedAction->usesPreprocessorOnly();
  84. }
  85. bool ASTMergeAction::usesCompleteTranslationUnit() {
  86. return AdaptedAction->usesCompleteTranslationUnit();
  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. }