StmtGraphTraits.h 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- StmtGraphTraits.h - Graph Traits for the class Stmt ------*- 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 defines a template specialization of llvm::GraphTraits to
  15. // treat ASTs (Stmt*) as graphs
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_AST_STMTGRAPHTRAITS_H
  19. #define LLVM_CLANG_AST_STMTGRAPHTRAITS_H
  20. #include "clang/AST/Stmt.h"
  21. #include "llvm/ADT/DepthFirstIterator.h"
  22. #include "llvm/ADT/GraphTraits.h"
  23. namespace llvm {
  24. template <> struct GraphTraits<clang::Stmt *> {
  25. using NodeRef = clang::Stmt *;
  26. using ChildIteratorType = clang::Stmt::child_iterator;
  27. using nodes_iterator = llvm::df_iterator<clang::Stmt *>;
  28. static NodeRef getEntryNode(clang::Stmt *S) { return S; }
  29. static ChildIteratorType child_begin(NodeRef N) {
  30. if (N) return N->child_begin();
  31. else return ChildIteratorType();
  32. }
  33. static ChildIteratorType child_end(NodeRef N) {
  34. if (N) return N->child_end();
  35. else return ChildIteratorType();
  36. }
  37. static nodes_iterator nodes_begin(clang::Stmt* S) {
  38. return df_begin(S);
  39. }
  40. static nodes_iterator nodes_end(clang::Stmt* S) {
  41. return df_end(S);
  42. }
  43. };
  44. template <> struct GraphTraits<const clang::Stmt *> {
  45. using NodeRef = const clang::Stmt *;
  46. using ChildIteratorType = clang::Stmt::const_child_iterator;
  47. using nodes_iterator = llvm::df_iterator<const clang::Stmt *>;
  48. static NodeRef getEntryNode(const clang::Stmt *S) { return S; }
  49. static ChildIteratorType child_begin(NodeRef N) {
  50. if (N) return N->child_begin();
  51. else return ChildIteratorType();
  52. }
  53. static ChildIteratorType child_end(NodeRef N) {
  54. if (N) return N->child_end();
  55. else return ChildIteratorType();
  56. }
  57. static nodes_iterator nodes_begin(const clang::Stmt* S) {
  58. return df_begin(S);
  59. }
  60. static nodes_iterator nodes_end(const clang::Stmt* S) {
  61. return df_end(S);
  62. }
  63. };
  64. } // namespace llvm
  65. #endif // LLVM_CLANG_AST_STMTGRAPHTRAITS_H
  66. #ifdef __GNUC__
  67. #pragma GCC diagnostic pop
  68. #endif