FixIt.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- FixIt.h - FixIt Hint utilities -------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file implements functions to ease source rewriting from AST-nodes.
  15. //
  16. // Example swapping A and B expressions:
  17. //
  18. // Expr *A, *B;
  19. // tooling::fixit::createReplacement(*A, *B);
  20. // tooling::fixit::createReplacement(*B, *A);
  21. //
  22. //===----------------------------------------------------------------------===//
  23. #ifndef LLVM_CLANG_TOOLING_FIXIT_H
  24. #define LLVM_CLANG_TOOLING_FIXIT_H
  25. #include "clang/AST/ASTContext.h"
  26. namespace clang {
  27. namespace tooling {
  28. namespace fixit {
  29. namespace internal {
  30. StringRef getText(CharSourceRange Range, const ASTContext &Context);
  31. /// Returns the token CharSourceRange corresponding to \p Range.
  32. inline CharSourceRange getSourceRange(const SourceRange &Range) {
  33. return CharSourceRange::getTokenRange(Range);
  34. }
  35. /// Returns the CharSourceRange of the token at Location \p Loc.
  36. inline CharSourceRange getSourceRange(const SourceLocation &Loc) {
  37. return CharSourceRange::getTokenRange(Loc, Loc);
  38. }
  39. /// Returns the CharSourceRange of an given Node. \p Node is typically a
  40. /// 'Stmt', 'Expr' or a 'Decl'.
  41. template <typename T> CharSourceRange getSourceRange(const T &Node) {
  42. return CharSourceRange::getTokenRange(Node.getSourceRange());
  43. }
  44. } // end namespace internal
  45. /// Returns a textual representation of \p Node.
  46. template <typename T>
  47. StringRef getText(const T &Node, const ASTContext &Context) {
  48. return internal::getText(internal::getSourceRange(Node), Context);
  49. }
  50. // Returns a FixItHint to remove \p Node.
  51. // TODO: Add support for related syntactical elements (i.e. comments, ...).
  52. template <typename T> FixItHint createRemoval(const T &Node) {
  53. return FixItHint::CreateRemoval(internal::getSourceRange(Node));
  54. }
  55. // Returns a FixItHint to replace \p Destination by \p Source.
  56. template <typename D, typename S>
  57. FixItHint createReplacement(const D &Destination, const S &Source,
  58. const ASTContext &Context) {
  59. return FixItHint::CreateReplacement(internal::getSourceRange(Destination),
  60. getText(Source, Context));
  61. }
  62. // Returns a FixItHint to replace \p Destination by \p Source.
  63. template <typename D>
  64. FixItHint createReplacement(const D &Destination, StringRef Source) {
  65. return FixItHint::CreateReplacement(internal::getSourceRange(Destination),
  66. Source);
  67. }
  68. } // end namespace fixit
  69. } // end namespace tooling
  70. } // end namespace clang
  71. #endif // LLVM_CLANG_TOOLING_FIXIT_H
  72. #ifdef __GNUC__
  73. #pragma GCC diagnostic pop
  74. #endif