Branch data Line data Source code
1 : : /* Support for offering suggestions for handling unrecognized names.
2 : : Copyright (C) 2016-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify it under
7 : : the terms of the GNU General Public License as published by the Free
8 : : Software Foundation; either version 3, or (at your option) any later
9 : : version.
10 : :
11 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : : for more details.
15 : :
16 : : You should have received a copy of the GNU General Public License
17 : : along with GCC; see the file COPYING3. If not see
18 : : <http://www.gnu.org/licenses/>. */
19 : :
20 : : #ifndef GCC_NAME_HINT_H
21 : : #define GCC_NAME_HINT_H
22 : :
23 : : enum lookup_name_fuzzy_kind {
24 : : /* Names of types. */
25 : : FUZZY_LOOKUP_TYPENAME,
26 : :
27 : : /* Names of function decls. */
28 : : FUZZY_LOOKUP_FUNCTION_NAME,
29 : :
30 : : /* Any name. */
31 : : FUZZY_LOOKUP_NAME
32 : : };
33 : :
34 : : /* A deferred_diagnostic is a wrapper around optional extra diagnostics
35 : : that we may want to bundle into a name_hint.
36 : :
37 : : The diagnostic is emitted by the subclass destructor, which should
38 : : check that is_suppressed_p () is not true. */
39 : :
40 : : class deferred_diagnostic
41 : : {
42 : : public:
43 : 383 : virtual ~deferred_diagnostic () {}
44 : :
45 : 554 : location_t get_location () const { return m_loc; }
46 : :
47 : : /* Call this if the corresponding warning was not emitted,
48 : : in which case we should also not emit the deferred_diagnostic. */
49 : 0 : void suppress ()
50 : : {
51 : 0 : m_suppress = true;
52 : 0 : }
53 : :
54 : 383 : bool is_suppressed_p () const { return m_suppress; }
55 : :
56 : : protected:
57 : 662 : deferred_diagnostic (location_t loc)
58 : 645 : : m_loc (loc), m_suppress (false) {}
59 : :
60 : : private:
61 : : location_t m_loc;
62 : : bool m_suppress;
63 : : };
64 : :
65 : : /* A name_hint is an optional string suggestion, along with an
66 : : optional deferred_diagnostic.
67 : : For example:
68 : :
69 : : error: unknown foo named 'bar'
70 : :
71 : : if the SUGGESTION is "baz", then one might print:
72 : :
73 : : error: unknown foo named 'bar'; did you mean 'baz'?
74 : :
75 : : and the deferred_diagnostic allows for additional (optional)
76 : : diagnostics e.g.:
77 : :
78 : : note: did you check behind the couch?
79 : :
80 : : The deferred_diagnostic is emitted by its destructor, when the
81 : : name_hint goes out of scope. */
82 : :
83 : 19655 : class name_hint
84 : : {
85 : : public:
86 : 12666 : name_hint () : m_suggestion (NULL), m_deferred () {}
87 : :
88 : 4904 : name_hint (const char *suggestion, deferred_diagnostic *deferred)
89 : 4886 : : m_suggestion (suggestion), m_deferred (deferred)
90 : : {
91 : 4848 : }
92 : :
93 : 5789 : const char *suggestion () const { return m_suggestion; }
94 : :
95 : : /* Does this name_hint have a suggestion or a deferred diagnostic? */
96 : 2232 : operator bool () const { return (m_suggestion != NULL
97 : 2232 : || m_deferred != NULL); }
98 : :
99 : : /* Take ownership of this name_hint's deferred_diagnostic, for use
100 : : in chaining up deferred diagnostics. */
101 : 3 : std::unique_ptr<deferred_diagnostic> take_deferred () { return std::move (m_deferred); }
102 : :
103 : : /* Call this on a name_hint if the corresponding warning was not emitted,
104 : : in which case we should also not emit the deferred_diagnostic. */
105 : :
106 : 1069 : void suppress ()
107 : : {
108 : 1069 : if (m_deferred)
109 : 0 : m_deferred->suppress ();
110 : : }
111 : :
112 : : private:
113 : : const char *m_suggestion;
114 : : std::unique_ptr<deferred_diagnostic> m_deferred;
115 : : };
116 : :
117 : : extern name_hint lookup_name_fuzzy (tree, enum lookup_name_fuzzy_kind,
118 : : location_t);
119 : :
120 : : #endif /* ! GCC_NAME_HINT_H */
|