Skip to content

Commit

Permalink
__LINE__ and __FILE__
Browse files Browse the repository at this point in the history
  • Loading branch information
thradams committed Mar 1, 2024
1 parent ed64cc0 commit 3fd53c6
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 37 deletions.
17 changes: 8 additions & 9 deletions src/file.c
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#if __LINE__ != 1
#error
#endif

void* _Owner malloc(unsigned long size);
void free(void* _Owner ptr);

int main()
{
void* _Owner p = malloc(1);
static_debug(p);
free(p);
static_debug(p);
}
#if __LINE__ != 6
#error
#endif

__FILE__
55 changes: 43 additions & 12 deletions src/lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -5716,7 +5716,7 @@ static struct macro_argument_list collect_macro_arguments(struct preprocessor_ct
return macro_argument_list;
}

struct token_list expand_macro(struct preprocessor_ctx* ctx, struct macro_expanded* p_list, struct macro* macro, struct macro_argument_list* arguments, int level);
struct token_list expand_macro(struct preprocessor_ctx* ctx, struct macro_expanded* p_list, struct macro* macro, struct macro_argument_list* arguments, int level, const struct token * origin);
struct token_list replacement_list_reexamination(struct preprocessor_ctx* ctx, struct macro_expanded* p_list, struct token_list* oldlist, int level);


Expand Down Expand Up @@ -6021,7 +6021,11 @@ static bool macro_already_expanded(struct macro_expanded* p_list, const char* na
return false;
}

struct token_list replacement_list_reexamination(struct preprocessor_ctx* ctx, struct macro_expanded* p_list, struct token_list* oldlist, int level)
struct token_list replacement_list_reexamination(struct preprocessor_ctx* ctx,
struct macro_expanded* p_list,
struct token_list* oldlist,
int level,
const struct token * origin)
{
struct token_list r = { 0 };
try
Expand Down Expand Up @@ -6094,7 +6098,7 @@ struct token_list replacement_list_reexamination(struct preprocessor_ctx* ctx, s
}


struct token_list r3 = expand_macro(ctx, p_list, macro, &arguments, level);
struct token_list r3 = expand_macro(ctx, p_list, macro, &arguments, level, origin);
if (ctx->n_errors > 0)
{
token_list_destroy(&new_list);
Expand Down Expand Up @@ -6246,14 +6250,18 @@ struct token_list copy_replacement_list(struct token_list* list)



struct token_list macro_copy_replacement_list(struct preprocessor_ctx* ctx, struct macro* macro)
struct token_list macro_copy_replacement_list(struct preprocessor_ctx* ctx, struct macro* macro, const struct token* origin)
{
/*macros de conteudo dinamico*/
if (strcmp(macro->name, "__LINE__") == 0)
{
struct tokenizer_ctx tctx = { 0 };
char line[50] = { 0 };

assert(origin != NULL);
snprintf(line, sizeof line, "%d", origin->line);

struct token_list r = tokenizer(&tctx, "1", "", 0, TK_FLAG_NONE);
struct token_list r = tokenizer(&tctx, line, "", 0, TK_FLAG_NONE);
token_list_pop_front(&r);
r.head->flags = 0;
return r;
Expand Down Expand Up @@ -6290,7 +6298,12 @@ void print_literal2(const char* s);
para o primeiro item da expansao
caso contrario, se p nao for macro, retorna null.
*/
struct token_list expand_macro(struct preprocessor_ctx* ctx, struct macro_expanded* list, struct macro* macro, struct macro_argument_list* arguments, int level)
struct token_list expand_macro(struct preprocessor_ctx* ctx,
struct macro_expanded* list,
struct macro* macro,
struct macro_argument_list* arguments,
int level,
const struct token * origin)
{
macro->usage++;

Expand All @@ -6307,9 +6320,9 @@ struct token_list expand_macro(struct preprocessor_ctx* ctx, struct macro_expand
macro_expanded.p_previous = list;
if (macro->is_function)
{
struct token_list copy = macro_copy_replacement_list(ctx, macro);
struct token_list copy = macro_copy_replacement_list(ctx, macro, origin);
struct token_list copy2 = replace_macro_arguments(ctx, &macro_expanded, &copy, arguments);
struct token_list r2 = replacement_list_reexamination(ctx, &macro_expanded, &copy2, level);
struct token_list r2 = replacement_list_reexamination(ctx, &macro_expanded, &copy2, level, origin);

token_list_append_list(&r, &r2);

Expand All @@ -6321,8 +6334,8 @@ struct token_list expand_macro(struct preprocessor_ctx* ctx, struct macro_expand
}
else
{
struct token_list copy = macro_copy_replacement_list(ctx, macro);
struct token_list r3 = replacement_list_reexamination(ctx, &macro_expanded, &copy, level);
struct token_list copy = macro_copy_replacement_list(ctx, macro, origin);
struct token_list r3 = replacement_list_reexamination(ctx, &macro_expanded, &copy, level, origin);
if (ctx->n_errors > 0)
{
token_list_destroy(&copy);
Expand Down Expand Up @@ -6360,9 +6373,11 @@ static struct token_list text_line(struct preprocessor_ctx* ctx, struct token_li
{
struct macro* macro = NULL;
struct token* start_token = input_list->head;
const struct token* origin = NULL;

if (is_active && input_list->head->type == TK_IDENTIFIER)
{
origin = input_list->head;
macro = find_macro(ctx, input_list->head->lexeme);
if (macro &&
macro->is_function &&
Expand Down Expand Up @@ -6425,7 +6440,7 @@ static struct token_list text_line(struct preprocessor_ctx* ctx, struct token_li
}


struct token_list start_macro = expand_macro(ctx, NULL, macro, &arguments, level);
struct token_list start_macro = expand_macro(ctx, NULL, macro, &arguments, level, origin);
if (start_macro.head)
{
start_macro.head->flags |= flags;
Expand Down Expand Up @@ -6502,7 +6517,7 @@ static struct token_list text_line(struct preprocessor_ctx* ctx, struct token_li
}


struct token_list r3 = expand_macro(ctx, NULL, macro, &arguments2, level);
struct token_list r3 = expand_macro(ctx, NULL, macro, &arguments2, level, origin);
if (ctx->n_errors > 0)
{
macro_argument_list_destroy(&arguments2);
Expand Down Expand Up @@ -39042,6 +39057,22 @@ void comflittype()
}
//https://developers.redhat.com/articles/2023/05/04/new-c-features-gcc-13#c2x_features]

void linemacro()
{
const char* source
=
"#if __LINE__ != 1 \n"
"#error\n"
"#endif\n"
"\n"
"\n"
"#if __LINE__ != 6 \n"
"#error\n"
"#endif\n"
"\n"
"";
assert(compile_without_errors_warnings(true, false /*nullcheck disabled*/, source));
}
#endif


17 changes: 17 additions & 0 deletions src/tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -3497,5 +3497,22 @@ void comflittype()
}
//https://developers.redhat.com/articles/2023/05/04/new-c-features-gcc-13#c2x_features]

void linemacro()
{
const char* source
=
"#if __LINE__ != 1 \n"
"#error\n"
"#endif\n"
"\n"
"\n"
"#if __LINE__ != 6 \n"
"#error\n"
"#endif\n"
"\n"
"";
assert(compile_without_errors_warnings(true, false /*nullcheck disabled*/, source));
}

#endif

43 changes: 28 additions & 15 deletions src/tokenizer.c
Original file line number Diff line number Diff line change
Expand Up @@ -3388,7 +3388,7 @@ static struct macro_argument_list collect_macro_arguments(struct preprocessor_ct
return macro_argument_list;
}

struct token_list expand_macro(struct preprocessor_ctx* ctx, struct macro_expanded* p_list, struct macro* macro, struct macro_argument_list* arguments, int level);
struct token_list expand_macro(struct preprocessor_ctx* ctx, struct macro_expanded* p_list, struct macro* macro, struct macro_argument_list* arguments, int level, const struct token * origin);
struct token_list replacement_list_reexamination(struct preprocessor_ctx* ctx, struct macro_expanded* p_list, struct token_list* oldlist, int level);


Expand Down Expand Up @@ -3693,7 +3693,11 @@ static bool macro_already_expanded(struct macro_expanded* p_list, const char* na
return false;
}

struct token_list replacement_list_reexamination(struct preprocessor_ctx* ctx, struct macro_expanded* p_list, struct token_list* oldlist, int level)
struct token_list replacement_list_reexamination(struct preprocessor_ctx* ctx,
struct macro_expanded* p_list,
struct token_list* oldlist,
int level,
const struct token * origin)
{
struct token_list r = { 0 };
try
Expand Down Expand Up @@ -3766,7 +3770,7 @@ struct token_list replacement_list_reexamination(struct preprocessor_ctx* ctx, s
}


struct token_list r3 = expand_macro(ctx, p_list, macro, &arguments, level);
struct token_list r3 = expand_macro(ctx, p_list, macro, &arguments, level, origin);
if (ctx->n_errors > 0)
{
token_list_destroy(&new_list);
Expand Down Expand Up @@ -3918,24 +3922,26 @@ struct token_list copy_replacement_list(struct token_list* list)



struct token_list macro_copy_replacement_list(struct preprocessor_ctx* ctx, struct macro* macro)
struct token_list macro_copy_replacement_list(struct preprocessor_ctx* ctx, struct macro* macro, const struct token* origin)
{
/*macros de conteudo dinamico*/
if (strcmp(macro->name, "__LINE__") == 0)
{
struct tokenizer_ctx tctx = { 0 };
char line[50] = { 0 };

assert(origin != NULL);
snprintf(line, sizeof line, "%d", origin->line);

struct token_list r = tokenizer(&tctx, "1", "", 0, TK_FLAG_NONE);
struct token_list r = tokenizer(&tctx, line, "", 0, TK_FLAG_NONE);
token_list_pop_front(&r);
r.head->flags = 0;
return r;
}
else if (strcmp(macro->name, "__FILE__") == 0)
{
struct tokenizer_ctx tctx = { 0 };


struct token_list r = tokenizer(&tctx, "\"file\"", "", 0, TK_FLAG_NONE);
struct token_list r = tokenizer(&tctx, origin->token_origin->lexeme, "", 0, TK_FLAG_NONE);
token_list_pop_front(&r);
r.head->flags = 0;
return r;
Expand All @@ -3962,7 +3968,12 @@ void print_literal2(const char* s);
para o primeiro item da expansao
caso contrario, se p nao for macro, retorna null.
*/
struct token_list expand_macro(struct preprocessor_ctx* ctx, struct macro_expanded* list, struct macro* macro, struct macro_argument_list* arguments, int level)
struct token_list expand_macro(struct preprocessor_ctx* ctx,
struct macro_expanded* list,
struct macro* macro,
struct macro_argument_list* arguments,
int level,
const struct token * origin)
{
macro->usage++;

Expand All @@ -3979,9 +3990,9 @@ struct token_list expand_macro(struct preprocessor_ctx* ctx, struct macro_expand
macro_expanded.p_previous = list;
if (macro->is_function)
{
struct token_list copy = macro_copy_replacement_list(ctx, macro);
struct token_list copy = macro_copy_replacement_list(ctx, macro, origin);
struct token_list copy2 = replace_macro_arguments(ctx, &macro_expanded, &copy, arguments);
struct token_list r2 = replacement_list_reexamination(ctx, &macro_expanded, &copy2, level);
struct token_list r2 = replacement_list_reexamination(ctx, &macro_expanded, &copy2, level, origin);

token_list_append_list(&r, &r2);

Expand All @@ -3993,8 +4004,8 @@ struct token_list expand_macro(struct preprocessor_ctx* ctx, struct macro_expand
}
else
{
struct token_list copy = macro_copy_replacement_list(ctx, macro);
struct token_list r3 = replacement_list_reexamination(ctx, &macro_expanded, &copy, level);
struct token_list copy = macro_copy_replacement_list(ctx, macro, origin);
struct token_list r3 = replacement_list_reexamination(ctx, &macro_expanded, &copy, level, origin);
if (ctx->n_errors > 0)
{
token_list_destroy(&copy);
Expand Down Expand Up @@ -4032,9 +4043,11 @@ static struct token_list text_line(struct preprocessor_ctx* ctx, struct token_li
{
struct macro* macro = NULL;
struct token* start_token = input_list->head;
const struct token* origin = NULL;

if (is_active && input_list->head->type == TK_IDENTIFIER)
{
origin = input_list->head;
macro = find_macro(ctx, input_list->head->lexeme);
if (macro &&
macro->is_function &&
Expand Down Expand Up @@ -4097,7 +4110,7 @@ static struct token_list text_line(struct preprocessor_ctx* ctx, struct token_li
}


struct token_list start_macro = expand_macro(ctx, NULL, macro, &arguments, level);
struct token_list start_macro = expand_macro(ctx, NULL, macro, &arguments, level, origin);
if (start_macro.head)
{
start_macro.head->flags |= flags;
Expand Down Expand Up @@ -4174,7 +4187,7 @@ static struct token_list text_line(struct preprocessor_ctx* ctx, struct token_li
}


struct token_list r3 = expand_macro(ctx, NULL, macro, &arguments2, level);
struct token_list r3 = expand_macro(ctx, NULL, macro, &arguments2, level, origin);
if (ctx->n_errors > 0)
{
macro_argument_list_destroy(&arguments2);
Expand Down
2 changes: 2 additions & 0 deletions src/unit_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ void assertbuiltin(void);
void valueoflit(void);
void enum_type(void);
void comflittype(void);
void linemacro(void);

/*end of forward declarations*/

Expand Down Expand Up @@ -453,6 +454,7 @@ g_unit_test_success_count = 0;
valueoflit();
enum_type();
comflittype();
linemacro();
return g_unit_test_error_count;

}
Expand Down
2 changes: 1 addition & 1 deletion vc/cprimev3.vcxproj.user
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LocalDebuggerCommandArguments>file.c -fanalyzer</LocalDebuggerCommandArguments>
<LocalDebuggerCommandArguments>file.c -E</LocalDebuggerCommandArguments>
<DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>
<LocalDebuggerWorkingDirectory>$(ProjectDir)/../src/</LocalDebuggerWorkingDirectory>
</PropertyGroup>
Expand Down

0 comments on commit 3fd53c6

Please sign in to comment.