RangeSelector.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===--- RangeSelector.h - Source-selection library ---------*- 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. /// \file
  15. /// Defines a combinator library supporting the definition of _selectors_,
  16. /// which select source ranges based on (bound) AST nodes.
  17. ///
  18. //===----------------------------------------------------------------------===//
  19. #ifndef LLVM_CLANG_TOOLING_TRANSFORMER_RANGESELECTOR_H
  20. #define LLVM_CLANG_TOOLING_TRANSFORMER_RANGESELECTOR_H
  21. #include "clang/ASTMatchers/ASTMatchFinder.h"
  22. #include "clang/Basic/SourceLocation.h"
  23. #include "clang/Tooling/Transformer/MatchConsumer.h"
  24. #include "llvm/Support/Error.h"
  25. #include <functional>
  26. #include <string>
  27. namespace clang {
  28. namespace transformer {
  29. using RangeSelector = MatchConsumer<CharSourceRange>;
  30. inline RangeSelector charRange(CharSourceRange R) {
  31. return [R](const ast_matchers::MatchFinder::MatchResult &)
  32. -> Expected<CharSourceRange> { return R; };
  33. }
  34. /// Selects from the start of \p Begin and to the end of \p End.
  35. RangeSelector enclose(RangeSelector Begin, RangeSelector End);
  36. /// Convenience version of \c range where end-points are bound nodes.
  37. RangeSelector encloseNodes(std::string BeginID, std::string EndID);
  38. /// DEPRECATED. Use `enclose`.
  39. inline RangeSelector range(RangeSelector Begin, RangeSelector End) {
  40. return enclose(std::move(Begin), std::move(End));
  41. }
  42. /// DEPRECATED. Use `encloseNodes`.
  43. inline RangeSelector range(std::string BeginID, std::string EndID) {
  44. return encloseNodes(std::move(BeginID), std::move(EndID));
  45. }
  46. /// Selects the (empty) range [B,B) when \p Selector selects the range [B,E).
  47. RangeSelector before(RangeSelector Selector);
  48. /// Selects the point immediately following \p Selector. That is, the
  49. /// (empty) range [E,E), when \p Selector selects either
  50. /// * the CharRange [B,E) or
  51. /// * the TokenRange [B,E'] where the token at E' spans the range [E',E).
  52. RangeSelector after(RangeSelector Selector);
  53. /// Selects the range between `R1` and `R2.
  54. inline RangeSelector between(RangeSelector R1, RangeSelector R2) {
  55. return enclose(after(std::move(R1)), before(std::move(R2)));
  56. }
  57. /// Selects a node, including trailing semicolon, if any (for declarations and
  58. /// non-expression statements). \p ID is the node's binding in the match result.
  59. RangeSelector node(std::string ID);
  60. /// Selects a node, including trailing semicolon (always). Useful for selecting
  61. /// expression statements. \p ID is the node's binding in the match result.
  62. RangeSelector statement(std::string ID);
  63. /// Given a \c MemberExpr, selects the member token. \p ID is the node's
  64. /// binding in the match result.
  65. RangeSelector member(std::string ID);
  66. /// Given a node with a "name", (like \c NamedDecl, \c DeclRefExpr, \c
  67. /// CxxCtorInitializer, and \c TypeLoc) selects the name's token. Only selects
  68. /// the final identifier of a qualified name, but not any qualifiers or template
  69. /// arguments. For example, for `::foo::bar::baz` and `::foo::bar::baz<int>`,
  70. /// it selects only `baz`.
  71. ///
  72. /// \param ID is the node's binding in the match result.
  73. RangeSelector name(std::string ID);
  74. // Given a \c CallExpr (bound to \p ID), selects the arguments' source text (all
  75. // source between the call's parentheses).
  76. RangeSelector callArgs(std::string ID);
  77. // Given a \c CompoundStmt (bound to \p ID), selects the source of the
  78. // statements (all source between the braces).
  79. RangeSelector statements(std::string ID);
  80. // Given a \c InitListExpr (bound to \p ID), selects the range of the elements
  81. // (all source between the braces).
  82. RangeSelector initListElements(std::string ID);
  83. /// Given an \IfStmt (bound to \p ID), selects the range of the else branch,
  84. /// starting from the \c else keyword.
  85. RangeSelector elseBranch(std::string ID);
  86. /// Selects the range from which `S` was expanded (possibly along with other
  87. /// source), if `S` is an expansion, and `S` itself, otherwise. Corresponds to
  88. /// `SourceManager::getExpansionRange`.
  89. RangeSelector expansion(RangeSelector S);
  90. } // namespace transformer
  91. } // namespace clang
  92. #endif // LLVM_CLANG_TOOLING_TRANSFORMER_RANGESELECTOR_H
  93. #ifdef __GNUC__
  94. #pragma GCC diagnostic pop
  95. #endif