glsl: Correctly handle line numbering.
This commit is contained in:
parent
0d9c5eafeb
commit
4aa3222df3
|
@ -37,6 +37,7 @@ sl_pp_context_init(struct sl_pp_context *context)
|
||||||
context->if_ptr = SL_PP_MAX_IF_NESTING;
|
context->if_ptr = SL_PP_MAX_IF_NESTING;
|
||||||
context->if_value = 1;
|
context->if_value = 1;
|
||||||
memset(context->error_msg, 0, sizeof(context->error_msg));
|
memset(context->error_msg, 0, sizeof(context->error_msg));
|
||||||
|
context->line = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
@ -48,6 +48,8 @@ struct sl_pp_context {
|
||||||
int if_value;
|
int if_value;
|
||||||
|
|
||||||
char error_msg[SL_PP_MAX_ERROR_MSG];
|
char error_msg[SL_PP_MAX_ERROR_MSG];
|
||||||
|
|
||||||
|
unsigned int line;
|
||||||
};
|
};
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
@ -29,16 +29,44 @@
|
||||||
#include "sl_pp_process.h"
|
#include "sl_pp_process.h"
|
||||||
|
|
||||||
|
|
||||||
|
static int
|
||||||
|
_parse_integer(const char *input,
|
||||||
|
unsigned int *number)
|
||||||
|
{
|
||||||
|
unsigned int n = 0;
|
||||||
|
|
||||||
|
while (*input >= '0' && *input <= '9') {
|
||||||
|
if (n * 10 < n) {
|
||||||
|
/* Overflow. */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
n = n * 10 + (*input++ - '0');
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*input != '\0') {
|
||||||
|
/* Invalid decimal number. */
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
*number = n;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
int
|
int
|
||||||
sl_pp_process_line(struct sl_pp_context *context,
|
sl_pp_process_line(struct sl_pp_context *context,
|
||||||
const struct sl_pp_token_info *input,
|
const struct sl_pp_token_info *input,
|
||||||
unsigned int first,
|
unsigned int first,
|
||||||
unsigned int last)
|
unsigned int last,
|
||||||
|
struct sl_pp_process_state *pstate)
|
||||||
{
|
{
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
struct sl_pp_process_state state;
|
struct sl_pp_process_state state;
|
||||||
int line_number = -1;
|
int line_number = -1;
|
||||||
int file_number = -1;
|
int file_number = -1;
|
||||||
|
const char *str;
|
||||||
|
unsigned int line;
|
||||||
|
|
||||||
memset(&state, 0, sizeof(state));
|
memset(&state, 0, sizeof(state));
|
||||||
for (i = first; i < last;) {
|
for (i = first; i < last;) {
|
||||||
|
@ -89,7 +117,22 @@ sl_pp_process_line(struct sl_pp_context *context,
|
||||||
|
|
||||||
free(state.out);
|
free(state.out);
|
||||||
|
|
||||||
/* TODO: Do something with line and file numbers. */
|
str = sl_pp_context_cstr(context, line_number);
|
||||||
|
if (_parse_integer(str, &line)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (context->line != line) {
|
||||||
|
struct sl_pp_token_info ti;
|
||||||
|
|
||||||
|
ti.token = SL_PP_LINE;
|
||||||
|
ti.data.line = line;
|
||||||
|
if (sl_pp_process_out(pstate, &ti)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* TODO: Do something with the file number. */
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -131,14 +131,14 @@ sl_pp_macro_expand(struct sl_pp_context *context,
|
||||||
macro_name = input[*pi].data.identifier;
|
macro_name = input[*pi].data.identifier;
|
||||||
macro_str = sl_pp_context_cstr(context, macro_name);
|
macro_str = sl_pp_context_cstr(context, macro_name);
|
||||||
|
|
||||||
/* TODO: Having the following built-ins hardcoded is a bit lame. */
|
|
||||||
if (!strcmp(macro_str, "__LINE__")) {
|
if (!strcmp(macro_str, "__LINE__")) {
|
||||||
if (!mute && _out_number(context, state, 1)) {
|
if (!mute && _out_number(context, state, context->line)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
(*pi)++;
|
(*pi)++;
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
/* TODO: Having the following built-ins hardcoded is a bit lame. */
|
||||||
if (!strcmp(macro_str, "__FILE__")) {
|
if (!strcmp(macro_str, "__FILE__")) {
|
||||||
if (!mute && _out_number(context, state, 0)) {
|
if (!mute && _out_number(context, state, 0)) {
|
||||||
return -1;
|
return -1;
|
||||||
|
|
|
@ -75,6 +75,21 @@ sl_pp_process(struct sl_pp_context *context,
|
||||||
|
|
||||||
memset(&state, 0, sizeof(state));
|
memset(&state, 0, sizeof(state));
|
||||||
|
|
||||||
|
if (context->line > 1) {
|
||||||
|
struct sl_pp_token_info ti;
|
||||||
|
|
||||||
|
ti.token = SL_PP_LINE;
|
||||||
|
ti.data.line = context->line - 1;
|
||||||
|
if (sl_pp_process_out(&state, &ti)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ti.token = SL_PP_NEWLINE;
|
||||||
|
if (sl_pp_process_out(&state, &ti)) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
while (!found_eof) {
|
while (!found_eof) {
|
||||||
skip_whitespace(input, &i);
|
skip_whitespace(input, &i);
|
||||||
if (input[i].token == SL_PP_HASH) {
|
if (input[i].token == SL_PP_HASH) {
|
||||||
|
@ -156,7 +171,7 @@ sl_pp_process(struct sl_pp_context *context,
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
} else if (!strcmp(name, "line")) {
|
} else if (!strcmp(name, "line")) {
|
||||||
if (sl_pp_process_line(context, input, first, last)) {
|
if (sl_pp_process_line(context, input, first, last, &state)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
} else if (!strcmp(name, "pragma")) {
|
} else if (!strcmp(name, "pragma")) {
|
||||||
|
@ -176,6 +191,7 @@ sl_pp_process(struct sl_pp_context *context,
|
||||||
if (sl_pp_process_out(&state, &endof)) {
|
if (sl_pp_process_out(&state, &endof)) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
context->line++;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -184,6 +200,7 @@ sl_pp_process(struct sl_pp_context *context,
|
||||||
if (sl_pp_process_out(&state, &input[i])) {
|
if (sl_pp_process_out(&state, &input[i])) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
context->line++;
|
||||||
i++;
|
i++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -214,6 +231,7 @@ sl_pp_process(struct sl_pp_context *context,
|
||||||
if (sl_pp_process_out(&state, &input[i])) {
|
if (sl_pp_process_out(&state, &input[i])) {
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
context->line++;
|
||||||
i++;
|
i++;
|
||||||
found_eol = 1;
|
found_eol = 1;
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -116,7 +116,8 @@ int
|
||||||
sl_pp_process_line(struct sl_pp_context *context,
|
sl_pp_process_line(struct sl_pp_context *context,
|
||||||
const struct sl_pp_token_info *input,
|
const struct sl_pp_token_info *input,
|
||||||
unsigned int first,
|
unsigned int first,
|
||||||
unsigned int last);
|
unsigned int last,
|
||||||
|
struct sl_pp_process_state *state);
|
||||||
|
|
||||||
int
|
int
|
||||||
sl_pp_process_out(struct sl_pp_process_state *state,
|
sl_pp_process_out(struct sl_pp_process_state *state,
|
||||||
|
|
|
@ -96,6 +96,8 @@ enum sl_pp_token {
|
||||||
SL_PP_EXTENSION_WARN,
|
SL_PP_EXTENSION_WARN,
|
||||||
SL_PP_EXTENSION_DISABLE,
|
SL_PP_EXTENSION_DISABLE,
|
||||||
|
|
||||||
|
SL_PP_LINE,
|
||||||
|
|
||||||
SL_PP_EOF
|
SL_PP_EOF
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -105,6 +107,7 @@ union sl_pp_token_data {
|
||||||
char other;
|
char other;
|
||||||
int pragma;
|
int pragma;
|
||||||
int extension;
|
int extension;
|
||||||
|
unsigned int line;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct sl_pp_token_info {
|
struct sl_pp_token_info {
|
||||||
|
|
|
@ -60,6 +60,7 @@ sl_pp_version(struct sl_pp_context *context,
|
||||||
unsigned int *tokens_eaten)
|
unsigned int *tokens_eaten)
|
||||||
{
|
{
|
||||||
unsigned int i = 0;
|
unsigned int i = 0;
|
||||||
|
unsigned int line = context->line;
|
||||||
|
|
||||||
/* Default values if `#version' is not present. */
|
/* Default values if `#version' is not present. */
|
||||||
*version = 110;
|
*version = 110;
|
||||||
|
@ -77,8 +78,10 @@ sl_pp_version(struct sl_pp_context *context,
|
||||||
/* Skip whitespace and newlines and seek for hash. */
|
/* Skip whitespace and newlines and seek for hash. */
|
||||||
while (!found_hash) {
|
while (!found_hash) {
|
||||||
switch (input[i].token) {
|
switch (input[i].token) {
|
||||||
case SL_PP_WHITESPACE:
|
|
||||||
case SL_PP_NEWLINE:
|
case SL_PP_NEWLINE:
|
||||||
|
line++;
|
||||||
|
/* pass thru */
|
||||||
|
case SL_PP_WHITESPACE:
|
||||||
i++;
|
i++;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -156,9 +159,12 @@ sl_pp_version(struct sl_pp_context *context,
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SL_PP_NEWLINE:
|
case SL_PP_NEWLINE:
|
||||||
|
line++;
|
||||||
|
/* pass thru */
|
||||||
case SL_PP_EOF:
|
case SL_PP_EOF:
|
||||||
i++;
|
i++;
|
||||||
*tokens_eaten = i;
|
*tokens_eaten = i;
|
||||||
|
context->line = line;
|
||||||
found_end = 1;
|
found_end = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue