GCC Middle and Back End API Reference
file_cache Class Reference

#include <input.h>

Collaboration diagram for file_cache:

Data Structures

struct  input_context
 

Public Member Functions

 file_cache ()
 
 ~file_cache ()
 
void dump (FILE *out, int indent) const
 
void DEBUG_FUNCTION dump () const
 
file_cache_slotlookup_or_add_file (const char *file_path)
 
void forcibly_evict_file (const char *file_path)
 
void initialize_input_context (diagnostic_input_charset_callback ccb, bool should_skip_bom)
 
char_span get_source_file_content (const char *file_path)
 
char_span get_source_line (const char *file_path, int line)
 
bool missing_trailing_newline_p (const char *file_path)
 
void add_buffered_content (const char *file_path, const char *buffer, size_t sz)
 
void tune (size_t num_file_slots, size_t lines)
 

Private Member Functions

file_cache_slotevicted_cache_tab_entry (unsigned *highest_use_count)
 
file_cache_slotadd_file (const char *file_path)
 
file_cache_slotlookup_file (const char *file_path)
 

Private Attributes

size_t m_num_file_slots
 
file_cache_slotm_file_slots
 
input_context m_input_context
 

Detailed Description

A cache of source files for use when emitting diagnostics (and in a few places in the C/C++ frontends). Results are only valid until the next call to the cache, as slots can be evicted. Filenames are stored by pointer, and so must outlive the cache instance.

Constructor & Destructor Documentation

◆ file_cache()

file_cache::file_cache ( )
file_cache's ctor.

References initialize_input_context(), m_file_slots, and m_num_file_slots.

◆ ~file_cache()

file_cache::~file_cache ( )
file_cache's dtor.

References m_file_slots.

Member Function Documentation

◆ add_buffered_content()

void file_cache::add_buffered_content ( const char * file_path,
const char * buffer,
size_t sz )

◆ add_file()

file_cache_slot * file_cache::add_file ( const char * file_path)
private
Create the cache used for the content of a given file to be accessed by caret diagnostic. This cache is added to an array of cache and can be retrieved by lookup_file_in_cache_tab. This function returns the created cache. Note that only the last m_num_file_slots files are cached. This can return nullptr if the FILE_PATH can't be opened for reading, or if the content can't be converted to the input_charset.

References evicted_cache_tab_entry(), fopen, m_input_context, NULL, and r.

Referenced by lookup_or_add_file().

◆ dump() [1/2]

void file_cache::dump ( ) const

References dump().

Referenced by dump().

◆ dump() [2/2]

void file_cache::dump ( FILE * out,
int indent ) const

References i, m_file_slots, and m_num_file_slots.

◆ evicted_cache_tab_entry()

file_cache_slot * file_cache::evicted_cache_tab_entry ( unsigned * highest_use_count)
private
Return the file cache that has been less used, recently, or the first empty one. If HIGHEST_USE_COUNT is non-null, *HIGHEST_USE_COUNT is set to the highest use count of the entries in the cache table.

References file_cache_slot::get_file_path(), file_cache_slot::get_use_count(), i, m_file_slots, m_num_file_slots, and NULL.

Referenced by add_buffered_content(), and add_file().

◆ forcibly_evict_file()

void file_cache::forcibly_evict_file ( const char * file_path)
Purge any mention of FILENAME from the cache of files used for printing source code. For use in selftests when working with tempfiles.

References gcc_assert, lookup_file(), and r.

◆ get_source_file_content()

char_span file_cache::get_source_file_content ( const char * file_path)

◆ get_source_line()

char_span file_cache::get_source_line ( const char * file_path,
int line )
Return the physical source line that corresponds to FILE_PATH/LINE. The line is not nul-terminated. The returned pointer is only valid until the next call of location_get_source_line. Note that the line can contain several null characters, so the returned value's length has the actual length of the line. If the function fails, a NULL char_span is returned.

References lookup_or_add_file(), NULL, and file_cache_slot::read_line_num().

Referenced by blank_line_before_p(), dump_location_info(), edited_line::edited_line(), edited_file::get_num_lines(), get_source_text_between(), get_substring_ranges_for_loc(), location_compute_display_column(), edited_file::print_content(), edited_file::print_diff_hunk(), and edited_file::print_run_of_changed_lines().

◆ initialize_input_context()

void file_cache::initialize_input_context ( diagnostic_input_charset_callback ccb,
bool should_skip_bom )

References default_charset_callback(), and m_input_context.

Referenced by file_cache().

◆ lookup_file()

file_cache_slot * file_cache::lookup_file ( const char * file_path)
private
Lookup the cache used for the content of a given file accessed by caret diagnostic. Return the found cached file, or NULL if no cached file was found.

References gcc_assert, file_cache_slot::get_file_path(), i, file_cache_slot::inc_use_count(), m_file_slots, m_num_file_slots, NULL, and r.

Referenced by add_buffered_content(), forcibly_evict_file(), and lookup_or_add_file().

◆ lookup_or_add_file()

file_cache_slot * file_cache::lookup_or_add_file ( const char * file_path)
Lookup the cache used for the content of a given file accessed by caret diagnostic. If no cached file was found, create a new cache for this file, add it to the array of cached file and return it. This can return nullptr on a cache miss if FILE_PATH can't be opened for reading, or if the content can't be converted to the input_charset.

References add_file(), lookup_file(), NULL, and r.

Referenced by get_source_file_content(), get_source_line(), and missing_trailing_newline_p().

◆ missing_trailing_newline_p()

bool file_cache::missing_trailing_newline_p ( const char * file_path)
Determine if FILE_PATH missing a trailing newline on its final line. Only valid to call once all of the file has been loaded, by requesting a line number beyond the end of the file.

References gcc_assert, lookup_or_add_file(), and r.

Referenced by edited_file::get_num_lines().

◆ tune()

void file_cache::tune ( size_t num_file_slots,
size_t lines )
Tune file_cache.

References m_file_slots, m_num_file_slots, and file_cache_slot::tune().

Field Documentation

◆ m_file_slots

file_cache_slot* file_cache::m_file_slots
private

◆ m_input_context

input_context file_cache::m_input_context
private

◆ m_num_file_slots

size_t file_cache::m_num_file_slots
private

The documentation for this class was generated from the following files: