#include <config.h>
+#include "diagnostic.h"
#include "lexer.h"
#include "token_t.h"
#include "symbol_table_t.h"
#include "type_t.h"
#include "target_architecture.h"
#include "parser.h"
+#include "warning.h"
#include <assert.h>
#include <errno.h>
static inline void next_real_char(void)
{
- bufpos++;
- if(bufpos >= bufend) {
+ assert(bufpos <= bufend);
+ if (bufpos >= bufend) {
size_t s = fread(buf + MAX_PUTBACK, 1, sizeof(buf) - MAX_PUTBACK,
input);
if(s == 0) {
bufpos = buf + MAX_PUTBACK;
bufend = buf + MAX_PUTBACK + s;
}
- c = *(bufpos);
+ c = *bufpos++;
}
static inline void put_back(int pc)
{
- assert(bufpos >= buf);
- //assert(bufpos < buf+MAX_PUTBACK || *bufpos == pc);
-
- char *p = buf + (bufpos - buf);
- *p = (char) pc;
-
- /* going backwards in the buffer is legal as long as it's not more often
- * than MAX_PUTBACK */
- bufpos--;
+ assert(bufpos > buf);
+ *(--bufpos - buf + buf) = (char) pc;
#ifdef DEBUG_CHARS
printf("putback '%c'\n", pc);
next_char(); \
} \
lexer_token.source_position.linenr++; \
- code; \
+ code \
case '\n': \
next_char(); \
lexer_token.source_position.linenr++; \
- code;
+ code
#define eat(c_type) do { assert(c == c_type); next_char(); } while(0)
}
}
-const char *concat_strings(const char *s1, const char *s2)
+string_t concat_strings(const string_t *const s1, const string_t *const s2)
{
- size_t len1 = strlen(s1);
- size_t len2 = strlen(s2);
+ const size_t len1 = s1->size - 1;
+ const size_t len2 = s2->size - 1;
- char *concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
- memcpy(concat, s1, len1);
- memcpy(concat + len1, s2, len2 + 1);
+ char *const concat = obstack_alloc(&symbol_obstack, len1 + len2 + 1);
+ memcpy(concat, s1->begin, len1);
+ memcpy(concat + len1, s2->begin, len2 + 1);
+#if 0 /* TODO hash */
const char *result = strset_insert(&stringset, concat);
if(result != concat) {
obstack_free(&symbol_obstack, concat);
}
return result;
+#else
+ return (string_t){ concat, len1 + len2 + 1 };
+#endif
}
static void parse_string_literal(void)
{
- unsigned start_linenr = lexer_token.source_position.linenr;
- char *string;
- const char *result;
+ const unsigned start_linenr = lexer_token.source_position.linenr;
assert(c == '"');
next_char();
/* add finishing 0 to the string */
obstack_1grow(&symbol_obstack, '\0');
- string = obstack_finish(&symbol_obstack);
+ const size_t size = (size_t)obstack_object_size(&symbol_obstack);
+ const char *const string = obstack_finish(&symbol_obstack);
+#if 0 /* TODO hash */
/* check if there is already a copy of the string */
result = strset_insert(&stringset, string);
if(result != string) {
obstack_free(&symbol_obstack, string);
}
+#else
+ const char *const result = string;
+#endif
- lexer_token.type = T_STRING_LITERAL;
- lexer_token.v.string = result;
+ lexer_token.type = T_STRING_LITERAL;
+ lexer_token.v.string.begin = result;
+ lexer_token.v.string.size = size;
}
static void parse_wide_character_constant(void)
next_pp_token();
}
if(pp_token.type == T_STRING_LITERAL) {
- lexer_token.source_position.input_name = pp_token.v.string;
+ lexer_token.source_position.input_name = pp_token.v.string.begin;
next_pp_token();
}
error_directive();
break;
case TP_pragma:
+ if (warning.unknown_pragmas) {
+ warningf(lexer_token.source_position, "encountered unknown #pragma");
+ }
+ eat_until_newline();
break;
}
}
lexer_token.source_position.input_name = input_name;
symbol_L = symbol_table_insert("L");
+ bufpos = NULL;
+ bufend = NULL;
/* place a virtual \n at the beginning so the lexer knows that we're
* at the beginning of a line */