Line data Source code
1 : /* Caching input files for use by diagnostics.
2 : Copyright (C) 2004-2026 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_DIAGNOSTICS_FILE_CACHE_H
21 : #define GCC_DIAGNOSTICS_FILE_CACHE_H
22 :
23 : namespace diagnostics {
24 :
25 : /* A class capturing the bounds of a buffer, to allow for run-time
26 : bounds-checking in a checked build. */
27 :
28 : class char_span
29 : {
30 : public:
31 1783076 : char_span (const char *ptr, size_t n_elts) : m_ptr (ptr), m_n_elts (n_elts) {}
32 :
33 : /* Test for a non-NULL pointer. */
34 1003778 : operator bool() const { return m_ptr; }
35 :
36 : /* Get length, not including any 0-terminator (which may not be,
37 : in fact, present). */
38 976168 : size_t length () const { return m_n_elts; }
39 :
40 742597 : const char *get_buffer () const { return m_ptr; }
41 :
42 3556227 : char operator[] (int idx) const
43 : {
44 3556227 : gcc_assert (idx >= 0);
45 3556227 : gcc_assert ((size_t)idx < m_n_elts);
46 3556227 : return m_ptr[idx];
47 : }
48 :
49 38590 : char_span subspan (int offset, int n_elts) const
50 : {
51 38590 : gcc_assert (offset >= 0);
52 38590 : gcc_assert (offset < (int)m_n_elts);
53 38590 : gcc_assert (n_elts >= 0);
54 38590 : gcc_assert (offset + n_elts <= (int)m_n_elts);
55 38590 : return char_span (m_ptr + offset, n_elts);
56 : }
57 :
58 37401 : char *xstrdup () const
59 : {
60 37401 : return ::xstrndup (m_ptr, m_n_elts);
61 : }
62 :
63 : private:
64 : const char *m_ptr;
65 : size_t m_n_elts;
66 : };
67 :
68 : /* Forward decl of slot within file_cache, so that the definition doesn't
69 : need to be in this header. */
70 : class file_cache_slot;
71 :
72 : /* A cache of source files for use when emitting diagnostics
73 : (and in a few places in the C/C++ frontends).
74 :
75 : Results are only valid until the next call to the cache, as
76 : slots can be evicted.
77 :
78 : Filenames are stored by pointer, and so must outlive the cache
79 : instance. */
80 :
81 : class file_cache
82 : {
83 : public:
84 : file_cache ();
85 : ~file_cache ();
86 :
87 : void dump (FILE *out, int indent) const;
88 : void DEBUG_FUNCTION dump () const;
89 :
90 : file_cache_slot *lookup_or_add_file (const char *file_path);
91 : void forcibly_evict_file (const char *file_path);
92 :
93 : /* See comments in diagnostic.h about the input conversion context. */
94 : struct input_context
95 : {
96 : diagnostic_input_charset_callback ccb;
97 : bool should_skip_bom;
98 : };
99 : void initialize_input_context (diagnostic_input_charset_callback ccb,
100 : bool should_skip_bom);
101 :
102 : char_span get_source_file_content (const char *file_path);
103 : char_span get_source_line (const char *file_path, int line);
104 : bool missing_trailing_newline_p (const char *file_path);
105 :
106 : void add_buffered_content (const char *file_path,
107 : const char *buffer,
108 : size_t sz);
109 :
110 : void tune (size_t num_file_slots, size_t lines);
111 :
112 : private:
113 : file_cache_slot *evicted_cache_tab_entry (unsigned *highest_use_count);
114 : file_cache_slot *add_file (const char *file_path);
115 : file_cache_slot *lookup_file (const char *file_path);
116 :
117 : private:
118 : size_t m_num_file_slots;
119 : file_cache_slot *m_file_slots;
120 : input_context m_input_context;
121 : };
122 :
123 : } // namespace diagnostics
124 :
125 : #endif // #ifndef GCC_DIAGNOSTICS_FILE_CACHE_H
|