<commit_before>#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#include "phase.h"

enum { LINE_OFFSET = 1 };
enum { ACC_EOF_CHARACTER = 127 };

struct request {
   const char* given_path;
   const char* lang_dir;
   struct file_entry* file;
   struct file_entry* offset_file;
   struct source* source;
   bool err_open;
   bool err_loading;
   bool err_loaded_before;
   bool implicit_bcs_ext;
};

static void append_file( struct library* lib, struct file_entry* file );
static void init_request( struct request* request,
   struct file_entry* offset_file, const char* path );
static void init_request_module( struct request* request,
   struct file_entry* file );
static void add_lang_dir( struct parse* parse, struct request* request );
static void load_source( struct parse* parse, struct request* request );
static void find_source( struct parse* parse, struct request* request );
static bool source_loading( struct parse* parse, struct request* request );
static void load_module( struct parse* parse, struct request* request );
static void open_source_file( struct parse* parse, struct request* request );
static struct source* alloc_source( struct parse* parse );
static void reset_filepos( struct source* source );
static void create_entry( struct parse* parse, struct request* request,
   bool imported );
static void create_include_history_entry( struct parse* parse, int line );
static void create_include_history_entry_imported( struct parse* parse,
   struct import_dirc* dirc );
static void read_token_acs( struct parse* parse, struct token* token );
static void read_token_acs95( struct parse* parse, struct token* token );
static void read_token( struct parse* parse, struct token* token );
static void escape_ch( struct parse* parse, char*, struct str* text, bool );
static char read_ch( struct parse* parse );
static char peek_ch( struct parse* parse );
static void read_initial_ch( struct parse* parse );
static struct str* temp_text( struct parse* parse );
static void append_ch( struct str* str, char ch );
static void append_string_ch( struct str* text, char ch );

void p_load_main_source( struct parse* parse ) {
   struct request request;
   init_request( &request, NULL, parse->task->options->source_file );
   load_source( parse, &request );
   if ( request.source ) {
      parse->lib->file = request.file;
      parse->lib->file_pos.id = request.file->id;
      append_file( parse->lib, request.file );
      create_entry( parse, &request, false );
      create_include_history_entry( parse, 0 );
      t_update_err_file_dir( parse->task, request.file->full_path.value );
   }
   else {
      p_diag( parse, DIAG_ERR,
         "failed to load source file: \"%s\" (%s)",
         parse->task->options->source_file, strerror( errno ) );
      p_bail( parse );
   }
}

void p_load_imported_lib_source( struct parse* parse, struct import_dirc* dirc,
   struct file_entry* file ) {
   struct request request;
   init_request_module( &request, file ); 
   load_module( parse, &request );
   if ( request.source ) {
      parse->lib->file = file;
      parse->lib->file_pos.id = file->id;
      append_file( parse->lib, file );
      create_entry( parse, &request, true );
      create_include_history_entry_imported( parse, dirc );
   }
   else {
      p_diag( parse, DIAG_POS_ERR, &dirc->pos,
         "failed to load library file: \"%s\"", dirc->file_path );
      p_bail( parse );
   }
}

struct file_entry* p_find_module_file( struct parse* parse,
   struct library* importing_module, const char* path ) {<commit_msg>Fix: incorrect library file shown in diagnostic messages<commit_after>#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>

#include "phase.h"

enum { LINE_OFFSET = 1 };
enum { ACC_EOF_CHARACTER = 127 };

struct request {
   const char* given_path;
   const char* lang_dir;
   struct file_entry* file;
   struct file_entry* offset_file;
   struct source* source;
   bool err_open;
   bool err_loading;
   bool err_loaded_before;
   bool implicit_bcs_ext;
};

static void append_file( struct library* lib, struct file_entry* file );
static void init_request( struct request* request,
   struct file_entry* offset_file, const char* path );
static void init_request_module( struct request* request,
   struct file_entry* file );
static void add_lang_dir( struct parse* parse, struct request* request );
static void load_source( struct parse* parse, struct request* request );
static void find_source( struct parse* parse, struct request* request );
static bool source_loading( struct parse* parse, struct request* request );
static void load_module( struct parse* parse, struct request* request );
static void open_source_file( struct parse* parse, struct request* request );
static struct source* alloc_source( struct parse* parse );
static void reset_filepos( struct source* source );
static void create_entry( struct parse* parse, struct request* request,
   bool imported );
static void create_include_history_entry( struct parse* parse, int line );
static void create_include_history_entry_imported( struct parse* parse,
   struct import_dirc* dirc );
static void read_token_acs( struct parse* parse, struct token* token );
static void read_token_acs95( struct parse* parse, struct token* token );
static void read_token( struct parse* parse, struct token* token );
static void escape_ch( struct parse* parse, char*, struct str* text, bool );
static char read_ch( struct parse* parse );
static char peek_ch( struct parse* parse );
static void read_initial_ch( struct parse* parse );
static struct str* temp_text( struct parse* parse );
static void append_ch( struct str* str, char ch );
static void append_string_ch( struct str* text, char ch );

void p_load_main_source( struct parse* parse ) {
   struct request request;
   init_request( &request, NULL, parse->task->options->source_file );
   load_source( parse, &request );
   if ( request.source ) {
      append_file( parse->lib, request.file );
      create_entry( parse, &request, false );
      create_include_history_entry( parse, 0 );
      t_update_err_file_dir( parse->task, request.file->full_path.value );
      parse->lib->file_pos.id = request.source->file_entry_id;
      parse->lib->file = request.file;
   }
   else {
      p_diag( parse, DIAG_ERR,
         "failed to load source file: \"%s\" (%s)",
         parse->task->options->source_file, strerror( errno ) );
      p_bail( parse );
   }
}

void p_load_imported_lib_source( struct parse* parse, struct import_dirc* dirc,
   struct file_entry* file ) {
   struct request request;
   init_request_module( &request, file ); 
   load_module( parse, &request );
   if ( request.source ) {
      append_file( parse->lib, file );
      create_entry( parse, &request, true );
      create_include_history_entry_imported( parse, dirc );
      parse->lib->file_pos.id = request.source->file_entry_id;
      parse->lib->file = file;
   }
   else {
      p_diag( parse, DIAG_POS_ERR, &dirc->pos,
         "failed to load library file: \"%s\"", dirc->file_path );
      p_bail( parse );
   }
}

struct file_entry* p_find_module_file( struct parse* parse,
   struct library* importing_module, const char* path ) {
{'fraction_non_alphanumeric': 0.10491349480968858, 'fraction_numerical': 0.0019377162629757784, 'mean_word_length': 4.278305332359387, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5969303', 'n_tokens_mistral': 2177, 'n_tokens_neox': 2148, 'n_words': 721}