GCC Middle and Back End API Reference
file-cache.h
Go to the documentation of this file.
1/* Caching input files for use by diagnostics.
2 Copyright (C) 2004-2025 Free Software Foundation, Inc.
3
4This file is part of GCC.
5
6GCC is free software; you can redistribute it and/or modify it under
7the terms of the GNU General Public License as published by the Free
8Software Foundation; either version 3, or (at your option) any later
9version.
10
11GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or
13FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14for more details.
15
16You should have received a copy of the GNU General Public License
17along 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
23namespace diagnostics {
24
25/* A class capturing the bounds of a buffer, to allow for run-time
26 bounds-checking in a checked build. */
27
29{
30 public:
31 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 operator bool() const { return m_ptr; }
35
36 /* Get length, not including any 0-terminator (which may not be,
37 in fact, present). */
38 size_t length () const { return m_n_elts; }
39
40 const char *get_buffer () const { return m_ptr; }
41
42 char operator[] (int idx) const
43 {
44 gcc_assert (idx >= 0);
45 gcc_assert ((size_t)idx < m_n_elts);
46 return m_ptr[idx];
47 }
48
49 char_span subspan (int offset, int n_elts) const
50 {
51 gcc_assert (offset >= 0);
52 gcc_assert (offset < (int)m_n_elts);
53 gcc_assert (n_elts >= 0);
54 gcc_assert (offset + n_elts <= (int)m_n_elts);
55 return char_span (m_ptr + offset, n_elts);
56 }
57
58 char *xstrdup () const
59 {
60 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. */
70class 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
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. */
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:
121};
122
123} // namespace diagnostics
124
125#endif // #ifndef GCC_DIAGNOSTICS_FILE_CACHE_H
Definition buffering.h:59
Definition file-cache.h:29
char operator[](int idx) const
Definition file-cache.h:42
const char * get_buffer() const
Definition file-cache.h:40
size_t m_n_elts
Definition file-cache.h:65
char_span(const char *ptr, size_t n_elts)
Definition file-cache.h:31
const char * m_ptr
Definition file-cache.h:64
char * xstrdup() const
Definition file-cache.h:58
size_t length() const
Definition file-cache.h:38
char_span subspan(int offset, int n_elts) const
Definition file-cache.h:49
Definition file-cache.cc:51
void initialize_input_context(diagnostic_input_charset_callback ccb, bool should_skip_bom)
Definition file-cache.cc:41
~file_cache()
Definition file-cache.cc:467
void add_buffered_content(const char *file_path, const char *buffer, size_t sz)
Definition file-cache.cc:274
file_cache_slot * lookup_or_add_file(const char *file_path)
Definition file-cache.cc:498
char_span get_source_line(const char *file_path, int line)
Definition file-cache.cc:932
file_cache_slot * evicted_cache_tab_entry(unsigned *highest_use_count)
Definition file-cache.cc:317
file_cache_slot * m_file_slots
Definition file-cache.h:119
size_t m_num_file_slots
Definition file-cache.h:118
void tune(size_t num_file_slots, size_t lines)
Definition file-cache.cc:202
void forcibly_evict_file(const char *file_path)
Definition file-cache.cc:248
char_span get_source_file_content(const char *file_path)
Definition file-cache.cc:955
input_context m_input_context
Definition file-cache.h:120
bool missing_trailing_newline_p(const char *file_path)
Definition file-cache.cc:265
file_cache_slot * lookup_file(const char *file_path)
Definition file-cache.cc:221
file_cache()
Definition file-cache.cc:459
file_cache_slot * add_file(const char *file_path)
Definition file-cache.cc:357
void DEBUG_FUNCTION dump() const
Definition file-cache.cc:484
const char *(* diagnostic_input_charset_callback)(const char *)
Definition coretypes.h:173
Definition coretypes.h:167
Definition file-cache.h:95
diagnostic_input_charset_callback ccb
Definition file-cache.h:96
bool should_skip_bom
Definition file-cache.h:97
#define gcc_assert(EXPR)
Definition system.h:814
#define bool
Definition system.h:886
#define DEBUG_FUNCTION
Definition system.h:1236