Branch data Line data Source code
1 : : /* Helper class for deferring path creation until a diagnostic is emitted.
2 : : Copyright (C) 2024-2025 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 under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : 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_LAZY_DIAGNOSTIC_PATH_H
22 : : #define GCC_LAZY_DIAGNOSTIC_PATH_H
23 : :
24 : : #include "diagnostic-path.h"
25 : :
26 : : /* An implementation of diagnostic_path which has a trivial ctor
27 : : and lazily creates another diagnostic_path the first time the path
28 : : is queried, deferring to this inner path for all queries.
29 : :
30 : : Use this to avoid expensive path creation logic when creating
31 : : rich_location instances, so that expense can be deferred until the path
32 : : is actually used by a diagnostic, and thus avoided for warnings that
33 : : are disabled. */
34 : :
35 : 12 : class lazy_diagnostic_path : public diagnostic_path
36 : : {
37 : : public:
38 : 4 : virtual ~lazy_diagnostic_path () {}
39 : :
40 : : unsigned num_events () const final override;
41 : : const diagnostic_event & get_event (int idx) const final override;
42 : : unsigned num_threads () const final override;
43 : : const diagnostic_thread &
44 : : get_thread (diagnostic_thread_id_t) const final override;
45 : : bool
46 : : same_function_p (int event_idx_a,
47 : : int event_idx_b) const final override;
48 : :
49 : 24 : bool generated_p () const { return m_inner_path != nullptr; }
50 : :
51 : : private:
52 : : void lazily_generate_path () const;
53 : : virtual std::unique_ptr<diagnostic_path> make_inner_path () const = 0;
54 : :
55 : : mutable std::unique_ptr<diagnostic_path> m_inner_path;
56 : : };
57 : :
58 : : #endif /* ! GCC_LAZY_DIAGNOSTIC_PATH_H */
|