NodeIntrospection.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- NodeIntrospection.h ------------------------------------*- 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 contains the implementation of the NodeIntrospection.
  15. //
  16. //===----------------------------------------------------------------------===//
  17. #ifndef LLVM_CLANG_TOOLING_NODEINTROSPECTION_H
  18. #define LLVM_CLANG_TOOLING_NODEINTROSPECTION_H
  19. #include "clang/AST/ASTTypeTraits.h"
  20. #include "clang/AST/DeclarationName.h"
  21. #include "llvm/ADT/IntrusiveRefCntPtr.h"
  22. #include <set>
  23. namespace clang {
  24. class Stmt;
  25. class Decl;
  26. class CXXCtorInitializer;
  27. class NestedNameSpecifierLoc;
  28. class TemplateArgumentLoc;
  29. class CXXBaseSpecifier;
  30. struct DeclarationNameInfo;
  31. namespace tooling {
  32. class LocationCall;
  33. using SharedLocationCall = llvm::IntrusiveRefCntPtr<LocationCall>;
  34. class LocationCall : public llvm::ThreadSafeRefCountedBase<LocationCall> {
  35. public:
  36. enum LocationCallFlags { NoFlags, ReturnsPointer, IsCast };
  37. LocationCall(SharedLocationCall on, std::string name,
  38. LocationCallFlags flags = NoFlags)
  39. : m_flags(flags), m_on(std::move(on)), m_name(std::move(name)) {}
  40. LocationCall *on() const { return m_on.get(); }
  41. StringRef name() const { return m_name; }
  42. bool returnsPointer() const { return m_flags & ReturnsPointer; }
  43. bool isCast() const { return m_flags & IsCast; }
  44. private:
  45. LocationCallFlags m_flags;
  46. SharedLocationCall m_on;
  47. std::string m_name;
  48. };
  49. class LocationCallFormatterCpp {
  50. public:
  51. static void print(const LocationCall &Call, llvm::raw_ostream &OS);
  52. static std::string format(const LocationCall &Call);
  53. };
  54. namespace internal {
  55. struct RangeLessThan {
  56. bool operator()(std::pair<SourceRange, SharedLocationCall> const &LHS,
  57. std::pair<SourceRange, SharedLocationCall> const &RHS) const;
  58. bool
  59. operator()(std::pair<SourceLocation, SharedLocationCall> const &LHS,
  60. std::pair<SourceLocation, SharedLocationCall> const &RHS) const;
  61. };
  62. } // namespace internal
  63. // Note that this container stores unique results in a deterministic, but
  64. // the location calls are in an unspecified order. Clients which desire
  65. // a particular order for the location calls, such as alphabetical,
  66. // should sort results after retrieval, because the order is dependent
  67. // on how the LocationCalls are formatted.
  68. template <typename T, typename U>
  69. using UniqueMultiMap = std::set<std::pair<T, U>, internal::RangeLessThan>;
  70. using SourceLocationMap = UniqueMultiMap<SourceLocation, SharedLocationCall>;
  71. using SourceRangeMap = UniqueMultiMap<SourceRange, SharedLocationCall>;
  72. struct NodeLocationAccessors {
  73. SourceLocationMap LocationAccessors;
  74. SourceRangeMap RangeAccessors;
  75. };
  76. namespace NodeIntrospection {
  77. bool hasIntrospectionSupport();
  78. NodeLocationAccessors GetLocations(clang::Stmt const *Object);
  79. NodeLocationAccessors GetLocations(clang::Decl const *Object);
  80. NodeLocationAccessors GetLocations(clang::CXXCtorInitializer const *Object);
  81. NodeLocationAccessors GetLocations(clang::NestedNameSpecifierLoc const &);
  82. NodeLocationAccessors GetLocations(clang::TemplateArgumentLoc const &);
  83. NodeLocationAccessors GetLocations(clang::CXXBaseSpecifier const *);
  84. NodeLocationAccessors GetLocations(clang::TypeLoc const &);
  85. NodeLocationAccessors GetLocations(clang::DeclarationNameInfo const &);
  86. NodeLocationAccessors GetLocations(clang::DynTypedNode const &Node);
  87. } // namespace NodeIntrospection
  88. } // namespace tooling
  89. } // namespace clang
  90. #endif
  91. #ifdef __GNUC__
  92. #pragma GCC diagnostic pop
  93. #endif