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 ()
 
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)
 

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

file_cache_slotm_file_slots
 
input_context in_context
 

Static Private Attributes

static const size_t num_file_slots = 16
 

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().

◆ ~file_cache()

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

References m_file_slots.

Member Function Documentation

◆ 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
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, ggc_alloc(), in_context, NULL, and r.

Referenced by lookup_or_add_file().

◆ 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(), ggc_alloc(), i, m_file_slots, NULL, and num_file_slots.

Referenced by 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, ggc_alloc(), lookup_file(), and r.

◆ get_source_file_content()

◆ 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 ggc_alloc(), lookup_or_add_file(), NULL, and file_cache_slot::read_line_num().

Referenced by asm_show_source(), edited_file::get_num_lines(), sarif_builder::get_source_lines(), edited_file::print_content(), edited_file::print_diff_hunk(), and edited_file::print_run_of_changed_lines().

◆ initialize_input_context()

◆ 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(), ggc_alloc(), i, file_cache_slot::inc_use_count(), m_file_slots, NULL, num_file_slots, and r.

Referenced by 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(), ggc_alloc(), 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, ggc_alloc(), lookup_or_add_file(), and r.

Referenced by edited_file::get_num_lines().

Field Documentation

◆ in_context

input_context file_cache::in_context
private

◆ m_file_slots

file_cache_slot* file_cache::m_file_slots
private

◆ num_file_slots

const size_t file_cache::num_file_slots = 16
staticprivate

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