mojoshader_preprocessor.c
changeset 1128 a66416e507d1
parent 1126 91c22d2de774
child 1129 908551a4334c
equal deleted inserted replaced
1127:4d7716f32464 1128:a66416e507d1
   435 } // remove_define
   435 } // remove_define
   436 
   436 
   437 
   437 
   438 static const Define *find_define(Context *ctx, const char *sym)
   438 static const Define *find_define(Context *ctx, const char *sym)
   439 {
   439 {
   440     if ( (ctx->file_macro) && (strcmp(sym, "__FILE__") == 0) )
   440     const uint8 hash = hash_define(sym);
       
   441     Define *bucket = ctx->define_hashtable[hash];
       
   442     while (bucket)
       
   443     {
       
   444         if (strcmp(bucket->identifier, sym) == 0)
       
   445             return bucket;
       
   446         bucket = bucket->next;
       
   447     } // while
       
   448 
       
   449     const uint8 filestrhash = 67;
       
   450     assert(hash_define("__FILE__") == filestrhash);
       
   451 
       
   452     const uint8 linestrhash = 75;
       
   453     assert(hash_define("__LINE__") == linestrhash);
       
   454 
       
   455     if ( (hash == filestrhash) && (ctx->file_macro) && (strcmp(sym, "__FILE__") == 0) )
   441     {
   456     {
   442         Free(ctx, (char *) ctx->file_macro->definition);
   457         Free(ctx, (char *) ctx->file_macro->definition);
   443         const IncludeState *state = ctx->include_stack;
   458         const IncludeState *state = ctx->include_stack;
   444         const char *fname = state ? state->filename : "";
   459         const char *fname = state ? state->filename : "";
   445         const size_t len = strlen(fname) + 2;
   460         const size_t len = strlen(fname) + 2;
   451         str[len - 1] = '\"';
   466         str[len - 1] = '\"';
   452         ctx->file_macro->definition = str;
   467         ctx->file_macro->definition = str;
   453         return ctx->file_macro;
   468         return ctx->file_macro;
   454     } // if
   469     } // if
   455 
   470 
   456     else if ( (ctx->line_macro) && (strcmp(sym, "__LINE__") == 0) )
   471     else if ( (hash == linestrhash) && (ctx->line_macro) && (strcmp(sym, "__LINE__") == 0) )
   457     {
   472     {
   458         Free(ctx, (char *) ctx->line_macro->definition);
   473         Free(ctx, (char *) ctx->line_macro->definition);
   459         const IncludeState *state = ctx->include_stack;
   474         const IncludeState *state = ctx->include_stack;
   460         const size_t bufsize = 32;
   475         const size_t bufsize = 32;
   461         char *str = (char *) Malloc(ctx, bufsize);
   476         char *str = (char *) Malloc(ctx, bufsize);
   466         assert(len < bufsize);
   481         assert(len < bufsize);
   467         ctx->line_macro->definition = str;
   482         ctx->line_macro->definition = str;
   468         return ctx->line_macro;
   483         return ctx->line_macro;
   469     } // else
   484     } // else
   470 
   485 
   471     const uint8 hash = hash_define(sym);
       
   472     Define *bucket = ctx->define_hashtable[hash];
       
   473     while (bucket)
       
   474     {
       
   475         if (strcmp(bucket->identifier, sym) == 0)
       
   476             return bucket;
       
   477         bucket = bucket->next;
       
   478     } // while
       
   479     return NULL;
   486     return NULL;
   480 } // find_define
   487 } // find_define
   481 
   488 
   482 
   489 
   483 static const Define *find_define_by_token(Context *ctx)
   490 static const Define *find_define_by_token(Context *ctx)