diagnostic-path.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* Paths through the code associated with a diagnostic.
  2. Copyright (C) 2019-2020 Free Software Foundation, Inc.
  3. Contributed by David Malcolm <dmalcolm@redhat.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify it under
  6. the terms of the GNU General Public License as published by the Free
  7. Software Foundation; either version 3, or (at your option) any later
  8. version.
  9. GCC is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #ifndef GCC_DIAGNOSTIC_PATH_H
  17. #define GCC_DIAGNOSTIC_PATH_H
  18. #include "diagnostic.h" /* for ATTRIBUTE_GCC_DIAG. */
  19. #include "diagnostic-event-id.h"
  20. /* A diagnostic_path is an optional additional piece of metadata associated
  21. with a diagnostic (via its rich_location).
  22. It describes a sequence of events predicted by the compiler that
  23. lead to the problem occurring, with their locations in the user's source,
  24. and text descriptions.
  25. For example, the following error has a 3-event path:
  26. test.c: In function 'demo':
  27. test.c:29:5: error: passing NULL as argument 1 to 'PyList_Append' which
  28. requires a non-NULL parameter
  29. 29 | PyList_Append(list, item);
  30. | ^~~~~~~~~~~~~~~~~~~~~~~~~
  31. 'demo': events 1-3
  32. |
  33. | 25 | list = PyList_New(0);
  34. | | ^~~~~~~~~~~~~
  35. | | |
  36. | | (1) when 'PyList_New' fails, returning NULL
  37. | 26 |
  38. | 27 | for (i = 0; i < count; i++) {
  39. | | ~~~
  40. | | |
  41. | | (2) when 'i < count'
  42. | 28 | item = PyLong_FromLong(random());
  43. | 29 | PyList_Append(list, item);
  44. | | ~~~~~~~~~~~~~~~~~~~~~~~~~
  45. | | |
  46. | | (3) when calling 'PyList_Append', passing NULL from (1) as argument 1
  47. |
  48. The diagnostic-printing code has consolidated the path into a single
  49. run of events, since all the events are near each other and within the same
  50. function; more complicated examples (such as interprocedural paths)
  51. might be printed as multiple runs of events. */
  52. /* Abstract base classes, describing events within a path, and the paths
  53. themselves. */
  54. /* One event within a diagnostic_path. */
  55. class diagnostic_event
  56. {
  57. public:
  58. virtual ~diagnostic_event () {}
  59. virtual location_t get_location () const = 0;
  60. virtual tree get_fndecl () const = 0;
  61. /* Stack depth, so that consumers can visualizes the interprocedural
  62. calls, returns, and frame nesting. */
  63. virtual int get_stack_depth () const = 0;
  64. /* Get a localized (and possibly colorized) description of this event. */
  65. virtual label_text get_desc (bool can_colorize) const = 0;
  66. };
  67. /* Abstract base class for getting at a sequence of events. */
  68. class diagnostic_path
  69. {
  70. public:
  71. virtual ~diagnostic_path () {}
  72. virtual unsigned num_events () const = 0;
  73. virtual const diagnostic_event & get_event (int idx) const = 0;
  74. bool interprocedural_p () const;
  75. };
  76. /* Concrete subclasses. */
  77. /* A simple implementation of diagnostic_event. */
  78. class simple_diagnostic_event : public diagnostic_event
  79. {
  80. public:
  81. simple_diagnostic_event (location_t loc, tree fndecl, int depth,
  82. const char *desc);
  83. ~simple_diagnostic_event ();
  84. location_t get_location () const FINAL OVERRIDE { return m_loc; }
  85. tree get_fndecl () const FINAL OVERRIDE { return m_fndecl; }
  86. int get_stack_depth () const FINAL OVERRIDE { return m_depth; }
  87. label_text get_desc (bool) const FINAL OVERRIDE
  88. {
  89. return label_text::borrow (m_desc);
  90. }
  91. private:
  92. location_t m_loc;
  93. tree m_fndecl;
  94. int m_depth;
  95. char *m_desc; // has been i18n-ed and formatted
  96. };
  97. /* A simple implementation of diagnostic_path, as a vector of
  98. simple_diagnostic_event instances. */
  99. class simple_diagnostic_path : public diagnostic_path
  100. {
  101. public:
  102. simple_diagnostic_path (pretty_printer *event_pp)
  103. : m_event_pp (event_pp) {}
  104. unsigned num_events () const FINAL OVERRIDE;
  105. const diagnostic_event & get_event (int idx) const FINAL OVERRIDE;
  106. diagnostic_event_id_t add_event (location_t loc, tree fndecl, int depth,
  107. const char *fmt, ...)
  108. ATTRIBUTE_GCC_DIAG(5,6);
  109. private:
  110. auto_delete_vec<simple_diagnostic_event> m_events;
  111. /* (for use by add_event). */
  112. pretty_printer *m_event_pp;
  113. };
  114. extern void debug (diagnostic_path *path);
  115. #endif /* ! GCC_DIAGNOSTIC_PATH_H */