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 : 386 : : 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 : 19792 : class name_hint
84 : : {
85 : : public:
86 : 12822 : name_hint () : m_suggestion (NULL), m_deferred () {}
87 : :
88 : 4878 : name_hint (const char *suggestion,
89 : : std::unique_ptr<deferred_diagnostic> deferred)
90 : 4878 : : m_suggestion (suggestion),
91 : 4878 : m_deferred (std::move (deferred))
92 : : {
93 : : }
94 : :
95 : 5785 : const char *suggestion () const { return m_suggestion; }
96 : :
97 : : /* Does this name_hint have a suggestion or a deferred diagnostic? */
98 : 2239 : operator bool () const { return (m_suggestion != NULL
99 : 2239 : || m_deferred != NULL); }
100 : :
101 : : /* Take ownership of this name_hint's deferred_diagnostic, for use
102 : : in chaining up deferred diagnostics. */
103 : 3 : std::unique_ptr<deferred_diagnostic> take_deferred () { return std::move (m_deferred); }
104 : :
105 : : /* Call this on a name_hint if the corresponding warning was not emitted,
106 : : in which case we should also not emit the deferred_diagnostic. */
107 : :
108 : 1069 : void suppress ()
109 : : {
110 : 1069 : if (m_deferred)
111 : 0 : m_deferred->suppress ();
112 : : }
113 : :
114 : : private:
115 : : const char *m_suggestion;
116 : : std::unique_ptr<deferred_diagnostic> m_deferred;
117 : : };
118 : :
119 : : extern name_hint lookup_name_fuzzy (tree, enum lookup_name_fuzzy_kind,
120 : : location_t);
121 : :
122 : : #endif /* ! GCC_NAME_HINT_H */
|