Skip to content

Commit

Permalink
Add predefined __INCLUDED__ macro
Browse files Browse the repository at this point in the history
When an #included file is processed, this macro is present; when no #included
files are being processing, this macro is not present.
  • Loading branch information
positively-charged committed Jan 8, 2017
1 parent abeec9d commit eb32f44
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/parse/phase.h
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ struct macro {
PREDEFMACRO_TIME,
PREDEFMACRO_DATE,
PREDEFMACRO_IMPORTED,
PREDEFMACRO_INCLUDED,
} predef;
bool func_like;
bool variadic;
Expand Down Expand Up @@ -290,6 +291,7 @@ struct source_entry {
struct token_queue peeked;
enum tk prev_tk;
bool main;
bool imported;
bool line_beginning;
};

Expand Down Expand Up @@ -528,7 +530,9 @@ void p_read_target_lib( struct parse* parse );
void p_clear_macros( struct parse* parse );
void p_define_predef_macros( struct parse* parse );
void p_define_imported_macro( struct parse* parse );
void p_define_included_macro( struct parse* parse );
void p_define_cmdline_macros( struct parse* parse );
void p_undefine_included_macro( struct parse* parse );
void p_read_func_body( struct parse* parse, struct func* func );
int p_determine_lang_from_file_path( const char* path );
bool p_is_macro_defined( struct parse* parse, const char* name );
Expand Down
19 changes: 19 additions & 0 deletions src/parse/token/dirc.c
Original file line number Diff line number Diff line change
Expand Up @@ -873,6 +873,25 @@ void p_define_imported_macro( struct parse* parse ) {
append_macro( parse, macro );
}

// The predefined __INCLUDED__ macro is present as long as an #included file is
// being processed.
void p_define_included_macro( struct parse* parse ) {
struct macro* macro = p_find_macro( parse, "__INCLUDED__" );
if ( ! macro ) {
macro = alloc_macro( parse );
macro->name = "__INCLUDED__";
macro->predef = PREDEFMACRO_INCLUDED;
append_macro( parse, macro );
}
}

void p_undefine_included_macro( struct parse* parse ) {
struct macro* macro = remove_macro( parse, "__INCLUDED__" );
if ( macro ) {
free_macro( parse, macro );
}
}

void p_define_predef_macros( struct parse* parse ) {
// Macro: __LINE__
struct macro* macro = alloc_macro( parse );
Expand Down
19 changes: 14 additions & 5 deletions src/parse/token/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ static bool source_loading( 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 );
static void create_entry( struct parse* parse, struct request* request,
bool imported );
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 );
Expand All @@ -31,7 +32,7 @@ void p_load_main_source( struct parse* parse ) {
parse->lib->file = request.file;
parse->lib->file_pos.id = request.file->id;
append_file( parse->lib, request.file );
create_entry( parse, &request );
create_entry( parse, &request, false );
t_update_err_file_dir( parse->task, request.file->full_path.value );
}
else {
Expand All @@ -52,7 +53,7 @@ void p_load_imported_lib_source( struct parse* parse, struct import_dirc* dirc,
parse->lib->file = file;
parse->lib->file_pos.id = file->id;
append_file( parse->lib, file );
create_entry( parse, &request );
create_entry( parse, &request, true );
}
else {
p_diag( parse, DIAG_POS_ERR, &dirc->pos,
Expand All @@ -78,7 +79,8 @@ void p_load_included_source( struct parse* parse, const char* file_path,
p_load_source( parse, &request );
if ( request.source ) {
append_file( parse->lib, request.file );
create_entry( parse, &request );
create_entry( parse, &request, false );
p_define_included_macro( parse );
}
else {
if ( request.err_loading ) {
Expand Down Expand Up @@ -209,7 +211,8 @@ void reset_filepos( struct source* source ) {
source->column = 0;
}

void create_entry( struct parse* parse, struct request* request ) {
void create_entry( struct parse* parse, struct request* request,
bool imported ) {
struct source_entry* entry;
if ( parse->source_entry_free ) {
entry = parse->source_entry_free;
Expand All @@ -223,6 +226,7 @@ void create_entry( struct parse* parse, struct request* request ) {
entry->macro_expan = NULL;
p_init_token_queue( &entry->peeked, false );
entry->main = ( entry->prev == NULL );
entry->imported = imported;
entry->prev_tk = TK_NL;
entry->line_beginning = true;
parse->source_entry = entry;
Expand Down Expand Up @@ -255,6 +259,11 @@ void p_pop_source( struct parse* parse ) {
// Free entry.
entry->prev = parse->source_entry_free;
parse->source_entry_free = entry;
// We are now back to the library file. Remove the __INCLUDED__ macro.
if ( parse->lang == LANG_BCS && ( parse->source_entry->main ||
parse->source_entry->imported ) ) {
p_undefine_included_macro( parse );
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/parse/token/stream.c
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ void expand_predef_macro( struct parse* parse, struct macro_expan* expan ) {
expand_predef_date( parse, expan );
break;
case PREDEFMACRO_IMPORTED:
case PREDEFMACRO_INCLUDED:
expand_predef_imported( parse, expan );
break;
default:
Expand Down Expand Up @@ -533,6 +534,7 @@ void expand_predef_date( struct parse* parse, struct macro_expan* expan ) {
output( parse, expan, &token );
}

// Both __IMPORTED__ and __INCLUDED__ have the same body.
void expand_predef_imported( struct parse* parse, struct macro_expan* expan ) {
struct token token;
p_init_token( &token );
Expand Down

0 comments on commit eb32f44

Please sign in to comment.