2018-05-15 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / c-family / name-hint.h
blobef0e4a363740eb0651e64236e496fe35b71172c8
1 /* Support for offering suggestions for handling unrecognized names.
2 Copyright (C) 2016-2018 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #ifndef GCC_NAME_HINT_H
21 #define GCC_NAME_HINT_H
23 /* This header uses gnu::unique_ptr, but unique-ptr.h can't be directly
24 included due to issues with macros. Hence it must be included from
25 system.h by defining INCLUDE_UNIQUE_PTR in any source file using it. */
27 #ifndef GNU_UNIQUE_PTR_H
28 # error "You must define INCLUDE_UNIQUE_PTR before including system.h to use name-hint.h"
29 #endif
31 enum lookup_name_fuzzy_kind {
32 /* Names of types. */
33 FUZZY_LOOKUP_TYPENAME,
35 /* Names of function decls. */
36 FUZZY_LOOKUP_FUNCTION_NAME,
38 /* Any name. */
39 FUZZY_LOOKUP_NAME
42 /* A deferred_diagnostic is a wrapper around optional extra diagnostics
43 that we may want to bundle into a name_hint.
45 The diagnostic is emitted by the subclass destructor, which should
46 check that is_suppressed_p () is not true. */
48 class deferred_diagnostic
50 public:
51 virtual ~deferred_diagnostic () {}
53 location_t get_location () const { return m_loc; }
55 /* Call this if the corresponding warning was not emitted,
56 in which case we should also not emit the deferred_diagnostic. */
57 void suppress ()
59 m_suppress = true;
62 bool is_suppressed_p () const { return m_suppress; }
64 protected:
65 deferred_diagnostic (location_t loc)
66 : m_loc (loc), m_suppress (false) {}
68 private:
69 location_t m_loc;
70 bool m_suppress;
73 /* A name_hint is an optional string suggestion, along with an
74 optional deferred_diagnostic.
75 For example:
77 error: unknown foo named 'bar'
79 if the SUGGESTION is "baz", then one might print:
81 error: unknown foo named 'bar'; did you mean 'baz'?
83 and the deferred_diagnostic allows for additional (optional)
84 diagnostics e.g.:
86 note: did you check behind the couch?
88 The deferred_diagnostic is emitted by its destructor, when the
89 name_hint goes out of scope. */
91 class name_hint
93 public:
94 name_hint () : m_suggestion (NULL), m_deferred () {}
96 name_hint (const char *suggestion, deferred_diagnostic *deferred)
97 : m_suggestion (suggestion), m_deferred (deferred)
101 const char *suggestion () const { return m_suggestion; }
102 operator bool () const { return m_suggestion != NULL; }
104 /* Call this on a name_hint if the corresponding warning was not emitted,
105 in which case we should also not emit the deferred_diagnostic. */
107 void suppress ()
109 if (m_deferred)
110 m_deferred->suppress ();
113 private:
114 const char *m_suggestion;
115 gnu::unique_ptr<deferred_diagnostic> m_deferred;
118 extern name_hint lookup_name_fuzzy (tree, enum lookup_name_fuzzy_kind,
119 location_t);
121 #endif /* ! GCC_NAME_HINT_H */