RefactoringCallbacks.cpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. // //
  15. ///////////////////////////////////////////////////////////////////////////////
  16. namespace clang {
  17. namespace tooling {
  18. RefactoringCallback::RefactoringCallback() {}
  19. tooling::Replacements &RefactoringCallback::getReplacements() {
  20. return Replace;
  21. }
  22. static Replacement replaceStmtWithText(SourceManager &Sources,
  23. const Stmt &From,
  24. StringRef Text) {
  25. return tooling::Replacement(Sources, CharSourceRange::getTokenRange(
  26. From.getSourceRange()), Text);
  27. }
  28. static Replacement replaceStmtWithStmt(SourceManager &Sources,
  29. const Stmt &From,
  30. const Stmt &To) {
  31. return replaceStmtWithText(Sources, From, Lexer::getSourceText(
  32. CharSourceRange::getTokenRange(To.getSourceRange()),
  33. Sources, LangOptions()));
  34. }
  35. ReplaceStmtWithText::ReplaceStmtWithText(StringRef FromId, StringRef ToText)
  36. : FromId(FromId), ToText(ToText) {}
  37. void ReplaceStmtWithText::run(
  38. const ast_matchers::MatchFinder::MatchResult &Result) {
  39. if (const Stmt *FromMatch = Result.Nodes.getStmtAs<Stmt>(FromId)) {
  40. Replace.insert(tooling::Replacement(
  41. *Result.SourceManager,
  42. CharSourceRange::getTokenRange(FromMatch->getSourceRange()),
  43. ToText));
  44. }
  45. }
  46. ReplaceStmtWithStmt::ReplaceStmtWithStmt(StringRef FromId, StringRef ToId)
  47. : FromId(FromId), ToId(ToId) {}
  48. void ReplaceStmtWithStmt::run(
  49. const ast_matchers::MatchFinder::MatchResult &Result) {
  50. const Stmt *FromMatch = Result.Nodes.getStmtAs<Stmt>(FromId);
  51. const Stmt *ToMatch = Result.Nodes.getStmtAs<Stmt>(ToId);
  52. if (FromMatch && ToMatch)
  53. Replace.insert(replaceStmtWithStmt(
  54. *Result.SourceManager, *FromMatch, *ToMatch));
  55. }
  56. ReplaceIfStmtWithItsBody::ReplaceIfStmtWithItsBody(StringRef Id,
  57. bool PickTrueBranch)
  58. : Id(Id), PickTrueBranch(PickTrueBranch) {}
  59. void ReplaceIfStmtWithItsBody::run(
  60. const ast_matchers::MatchFinder::MatchResult &Result) {
  61. if (const IfStmt *Node = Result.Nodes.getStmtAs<IfStmt>(Id)) {
  62. const Stmt *Body = PickTrueBranch ? Node->getThen() : Node->getElse();
  63. if (Body) {
  64. Replace.insert(replaceStmtWithStmt(*Result.SourceManager, *Node, *Body));
  65. } else if (!PickTrueBranch) {
  66. // If we want to use the 'else'-branch, but it doesn't exist, delete
  67. // the whole 'if'.
  68. Replace.insert(replaceStmtWithText(*Result.SourceManager, *Node, ""));
  69. }
  70. }
  71. }
  72. } // end namespace tooling
  73. } // end namespace clang