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