Line data Source code
1 : /* Helper class for deferring path creation until a diagnostic is emitted.
2 : Copyright (C) 2024-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 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_DIAGNOSTICS_LAZY_PATHS_H
22 : #define GCC_DIAGNOSTICS_LAZY_PATHS_H
23 :
24 : #include "diagnostics/paths.h"
25 :
26 : namespace diagnostics {
27 : namespace paths {
28 :
29 : /* An implementation of diagnostics::paths::path which has a trivial ctor
30 : and lazily creates another path the first time the path
31 : is queried, deferring to this inner path for all queries.
32 :
33 : Use this to avoid expensive path creation logic when creating
34 : rich_location instances, so that expense can be deferred until the path
35 : is actually used by a diagnostic, and thus avoided for warnings that
36 : are disabled. */
37 :
38 : class lazy_path : public path
39 : {
40 : public:
41 : virtual ~lazy_path () {}
42 :
43 : unsigned num_events () const final override;
44 : const event & get_event (int idx) const final override;
45 : unsigned num_threads () const final override;
46 : const thread &
47 : get_thread (thread_id_t) const final override;
48 : bool
49 : same_function_p (int event_idx_a,
50 : int event_idx_b) const final override;
51 :
52 24 : bool generated_p () const { return m_inner_path != nullptr; }
53 :
54 : protected:
55 890290 : lazy_path (const logical_locations::manager &logical_loc_mgr)
56 890290 : : path (logical_loc_mgr)
57 : {
58 : }
59 :
60 : private:
61 : void lazily_generate_path () const;
62 : virtual std::unique_ptr<path> make_inner_path () const = 0;
63 :
64 : mutable std::unique_ptr<path> m_inner_path;
65 : };
66 :
67 : } // namespace paths
68 : } // namespace diagnostics
69 :
70 : #endif /* ! GCC_DIAGNOSTICS_LAZY_PATHS_H */
|