Line data Source code
1 : /* User-facing descriptions of expressions at call sites.
2 : Copyright (C) 2019-2026 Free Software Foundation, Inc.
3 : Contributed by David Malcolm <dmalcolm@redhat.com>.
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it
8 : under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful, but
13 : WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #ifndef GCC_ANALYZER_CALLSITE_EXPR_H
22 : #define GCC_ANALYZER_CALLSITE_EXPR_H
23 :
24 : #include "pretty-print-markup.h"
25 :
26 : namespace ana {
27 :
28 : /* An ID representing an expression at a callsite:
29 : either a parameter index, or the return value (or unknown). */
30 :
31 : class callsite_expr
32 : {
33 : public:
34 1736 : callsite_expr () : m_val (-1) {}
35 :
36 335 : static callsite_expr from_zero_based_param (int idx)
37 : {
38 335 : return callsite_expr (idx + 1);
39 : }
40 :
41 95 : static callsite_expr from_return_value ()
42 : {
43 95 : return callsite_expr (0);
44 : }
45 :
46 457 : bool param_p () const
47 : {
48 327 : return m_val > 0;
49 : }
50 :
51 : /* Get 1-based param number. */
52 170 : int param_num () const
53 : {
54 170 : gcc_assert (param_p ());
55 170 : return m_val;
56 : }
57 :
58 : tree get_param_tree (tree fndecl) const;
59 :
60 : bool
61 : maybe_get_param_location (tree fndecl,
62 : location_t *out_loc) const;
63 :
64 185 : bool return_value_p () const
65 : {
66 185 : return m_val == 0;
67 : }
68 :
69 : private:
70 430 : callsite_expr (int val) : m_val (val) {}
71 :
72 : int m_val; /* 1-based parm, 0 for return value, or -1 for "unknown". */
73 : };
74 :
75 0 : class callsite_expr_element : public pp_element
76 : {
77 : public:
78 0 : callsite_expr_element (callsite_expr expr) : m_expr (expr) {}
79 :
80 : void
81 0 : add_to_phase_2 (pp_markup::context &ctxt) final override
82 : {
83 :
84 0 : if (m_expr.return_value_p ())
85 0 : pp_string (&ctxt.m_pp, "return value");
86 0 : else if (m_expr.param_p ())
87 : {
88 : /* We can't call pp_printf directly on ctxt.m_pp from within
89 : formatting. As a workaround, work with a clone of the pp. */
90 0 : std::unique_ptr<pretty_printer> pp (ctxt.m_pp.clone ());
91 0 : pp_printf (pp.get (), "parameter %i", m_expr.param_num ());
92 0 : pp_string (&ctxt.m_pp, pp_formatted_text (pp.get ()));
93 0 : }
94 : else
95 0 : pp_string (&ctxt.m_pp, "unknown");
96 0 : }
97 :
98 : private:
99 : callsite_expr m_expr;
100 : };
101 :
102 : } // namespace ana
103 :
104 : #endif /* GCC_ANALYZER_CALLSITE_EXPR_H */
|