NodeIntrospection.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. //===- NodeIntrospection.h -----------------------------------*- C++ -*----===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains the implementation of the NodeIntrospection.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "clang/Tooling/NodeIntrospection.h"
  13. #include "clang/AST/AST.h"
  14. #include "llvm/Support/raw_ostream.h"
  15. namespace clang {
  16. namespace tooling {
  17. void LocationCallFormatterCpp::print(const LocationCall &Call,
  18. llvm::raw_ostream &OS) {
  19. if (const LocationCall *On = Call.on()) {
  20. print(*On, OS);
  21. if (On->returnsPointer())
  22. OS << "->";
  23. else
  24. OS << '.';
  25. }
  26. OS << Call.name() << "()";
  27. }
  28. std::string LocationCallFormatterCpp::format(const LocationCall &Call) {
  29. std::string Result;
  30. llvm::raw_string_ostream OS(Result);
  31. print(Call, OS);
  32. OS.flush();
  33. return Result;
  34. }
  35. namespace internal {
  36. static bool locationCallLessThan(const LocationCall *LHS,
  37. const LocationCall *RHS) {
  38. if (!LHS && !RHS)
  39. return false;
  40. if (LHS && !RHS)
  41. return true;
  42. if (!LHS && RHS)
  43. return false;
  44. auto compareResult = LHS->name().compare(RHS->name());
  45. if (compareResult < 0)
  46. return true;
  47. if (compareResult > 0)
  48. return false;
  49. return locationCallLessThan(LHS->on(), RHS->on());
  50. }
  51. bool RangeLessThan::operator()(
  52. std::pair<SourceRange, SharedLocationCall> const &LHS,
  53. std::pair<SourceRange, SharedLocationCall> const &RHS) const {
  54. if (LHS.first.getBegin() < RHS.first.getBegin())
  55. return true;
  56. else if (LHS.first.getBegin() != RHS.first.getBegin())
  57. return false;
  58. if (LHS.first.getEnd() < RHS.first.getEnd())
  59. return true;
  60. else if (LHS.first.getEnd() != RHS.first.getEnd())
  61. return false;
  62. return locationCallLessThan(LHS.second.get(), RHS.second.get());
  63. }
  64. bool RangeLessThan::operator()(
  65. std::pair<SourceLocation, SharedLocationCall> const &LHS,
  66. std::pair<SourceLocation, SharedLocationCall> const &RHS) const {
  67. if (LHS.first == RHS.first)
  68. return locationCallLessThan(LHS.second.get(), RHS.second.get());
  69. return LHS.first < RHS.first;
  70. }
  71. } // namespace internal
  72. } // namespace tooling
  73. } // namespace clang
  74. #include "clang/Tooling/NodeIntrospection.inc"