RefactoringCallbacks.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //===--- RefactoringCallbacks.cpp - Structural query framework ------------===//
  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. //
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Lex/Lexer.h"
  13. #include "clang/Tooling/RefactoringCallbacks.h"
  14. namespace clang {
  15. namespace tooling {
  16. RefactoringCallback::RefactoringCallback() {}
  17. tooling::Replacements &RefactoringCallback::getReplacements() {
  18. return Replace;
  19. }
  20. static Replacement replaceStmtWithText(SourceManager &Sources,
  21. const Stmt &From,
  22. StringRef Text) {
  23. return tooling::Replacement(Sources, CharSourceRange::getTokenRange(
  24. From.getSourceRange()), Text);
  25. }
  26. static Replacement replaceStmtWithStmt(SourceManager &Sources,
  27. const Stmt &From,
  28. const Stmt &To) {
  29. return replaceStmtWithText(Sources, From, Lexer::getSourceText(
  30. CharSourceRange::getTokenRange(To.getSourceRange()),
  31. Sources, LangOptions()));
  32. }
  33. ReplaceStmtWithText::ReplaceStmtWithText(StringRef FromId, StringRef ToText)
  34. : FromId(FromId), ToText(ToText) {}
  35. void ReplaceStmtWithText::run(
  36. const ast_matchers::MatchFinder::MatchResult &Result) {
  37. if (const Stmt *FromMatch = Result.Nodes.getNodeAs<Stmt>(FromId)) {
  38. auto Err = Replace.add(tooling::Replacement(
  39. *Result.SourceManager,
  40. CharSourceRange::getTokenRange(FromMatch->getSourceRange()), ToText));
  41. // FIXME: better error handling. For now, just print error message in the
  42. // release version.
  43. if (Err)
  44. llvm::errs() << llvm::toString(std::move(Err)) << "\n";
  45. assert(!Err);
  46. }
  47. }
  48. ReplaceStmtWithStmt::ReplaceStmtWithStmt(StringRef FromId, StringRef ToId)
  49. : FromId(FromId), ToId(ToId) {}
  50. void ReplaceStmtWithStmt::run(
  51. const ast_matchers::MatchFinder::MatchResult &Result) {
  52. const Stmt *FromMatch = Result.Nodes.getNodeAs<Stmt>(FromId);
  53. const Stmt *ToMatch = Result.Nodes.getNodeAs<Stmt>(ToId);
  54. if (FromMatch && ToMatch) {
  55. auto Err = Replace.add(
  56. replaceStmtWithStmt(*Result.SourceManager, *FromMatch, *ToMatch));
  57. // FIXME: better error handling. For now, just print error message in the
  58. // release version.
  59. if (Err)
  60. llvm::errs() << llvm::toString(std::move(Err)) << "\n";
  61. assert(!Err);
  62. }
  63. }
  64. ReplaceIfStmtWithItsBody::ReplaceIfStmtWithItsBody(StringRef Id,
  65. bool PickTrueBranch)
  66. : Id(Id), PickTrueBranch(PickTrueBranch) {}
  67. void ReplaceIfStmtWithItsBody::run(
  68. const ast_matchers::MatchFinder::MatchResult &Result) {
  69. if (const IfStmt *Node = Result.Nodes.getNodeAs<IfStmt>(Id)) {
  70. const Stmt *Body = PickTrueBranch ? Node->getThen() : Node->getElse();
  71. if (Body) {
  72. auto Err =
  73. Replace.add(replaceStmtWithStmt(*Result.SourceManager, *Node, *Body));
  74. // FIXME: better error handling. For now, just print error message in the
  75. // release version.
  76. if (Err)
  77. llvm::errs() << llvm::toString(std::move(Err)) << "\n";
  78. assert(!Err);
  79. } else if (!PickTrueBranch) {
  80. // If we want to use the 'else'-branch, but it doesn't exist, delete
  81. // the whole 'if'.
  82. auto Err =
  83. Replace.add(replaceStmtWithText(*Result.SourceManager, *Node, ""));
  84. // FIXME: better error handling. For now, just print error message in the
  85. // release version.
  86. if (Err)
  87. llvm::errs() << llvm::toString(std::move(Err)) << "\n";
  88. assert(!Err);
  89. }
  90. }
  91. }
  92. } // end namespace tooling
  93. } // end namespace clang