fix a few warnings reported by cparser
[cparser] / preprocessor.c
index f56a182..e876e99 100644 (file)
@@ -61,7 +61,7 @@ struct pp_input_t {
        pp_input_t        *parent;
 };
 
-pp_input_t input;
+static pp_input_t input;
 #define CC input.c
 
 static pp_input_t     *input_stack;
@@ -70,7 +70,7 @@ static struct obstack  input_obstack;
 
 static pp_conditional_t *conditional_stack;
 
-token_t                   pp_token;
+static token_t            pp_token;
 static bool               resolve_escape_sequences = false;
 static bool               do_print_spaces          = true;
 static bool               do_expansions;
@@ -85,7 +85,6 @@ static pp_definition_t   *current_expansion  = NULL;
 static inline void next_char(void);
 static void next_preprocessing_token(void);
 static void print_line_directive(const source_position_t *pos, const char *add);
-static void print_spaces(void);
 
 static bool open_input(const char *filename)
 {
@@ -98,7 +97,7 @@ static bool open_input(const char *filename)
        input.bufpos              = NULL;
        input.had_non_space       = false;
        input.position.input_name = filename;
-       input.position.linenr     = 1;
+       input.position.lineno     = 1;
 
        /* indicate that we're at a new input */
        print_line_directive(&input.position, input_stack != NULL ? "1" : NULL);
@@ -184,7 +183,7 @@ static inline void next_real_char(void)
        if (input.bufpos >= input.bufend) {
                size_t s = fread(input.buf + MAX_PUTBACK, 1,
                                 sizeof(input.buf) - MAX_PUTBACK, input.file);
-               if(s == 0) {
+               if (s == 0) {
                        CC = EOF;
                        return;
                }
@@ -215,14 +214,14 @@ static inline void put_back(int pc)
                if(CC == '\n') {                      \
                        next_char();                      \
                }                                     \
-               ++input.position.linenr;              \
+               ++input.position.lineno;              \
                code                                  \
        case '\n':                                \
                next_char();                          \
-               ++input.position.linenr;              \
+               ++input.position.lineno;              \
                code
 
-#define eat(c_type)  do { assert(CC == c_type); next_char(); } while(0)
+#define eat(c_type) (assert(CC == c_type), next_char())
 
 static void maybe_concat_lines(void)
 {
@@ -315,7 +314,8 @@ static inline bool is_octal_digit(int chr)
  * Returns the value of a digit.
  * The only portable way to do it ...
  */
-static int digit_value(int digit) {
+static int digit_value(int digit)
+{
        switch (digit) {
        case '0': return 0;
        case '1': return 1;
@@ -429,7 +429,7 @@ static int parse_escape_sequence(void)
 
 static void parse_string_literal(void)
 {
-       const unsigned start_linenr = input.position.linenr;
+       const unsigned start_linenr = input.position.lineno;
 
        eat('"');
 
@@ -451,7 +451,7 @@ static void parse_string_literal(void)
                case EOF: {
                        source_position_t source_position;
                        source_position.input_name = pp_token.source_position.input_name;
-                       source_position.linenr     = start_linenr;
+                       source_position.lineno     = start_linenr;
                        errorf(&source_position, "string has no end");
                        pp_token.type = TP_ERROR;
                        return;
@@ -484,9 +484,9 @@ end_of_string:
        const char *const result = string;
 #endif
 
-       pp_token.type           = TP_STRING_LITERAL;
-       pp_token.v.string.begin = result;
-       pp_token.v.string.size  = size;
+       pp_token.type          = TP_STRING_LITERAL;
+       pp_token.literal.begin = result;
+       pp_token.literal.size  = size;
 }
 
 static void parse_wide_character_constant(void)
@@ -532,70 +532,9 @@ end_of_wide_char_constant:
        /* TODO... */
 }
 
-static void parse_wide_string_literal(void)
-{
-       const unsigned start_linenr = input.position.linenr;
-
-       assert(CC == '"');
-       next_char();
-
-       while(1) {
-               switch(CC) {
-               case '\\': {
-                       wchar_rep_t tc = parse_escape_sequence();
-                       obstack_grow(&symbol_obstack, &tc, sizeof(tc));
-                       break;
-               }
-
-               case EOF: {
-                       source_position_t source_position;
-                       source_position.input_name = pp_token.source_position.input_name;
-                       source_position.linenr     = start_linenr;
-                       errorf(&source_position, "string has no end");
-                       pp_token.type = TP_ERROR;
-                       return;
-               }
-
-               case '"':
-                       next_char();
-                       goto end_of_string;
-
-               default: {
-                       wchar_rep_t tc = CC;
-                       obstack_grow(&symbol_obstack, &tc, sizeof(tc));
-                       next_char();
-                       break;
-               }
-               }
-       }
-
-end_of_string:;
-       /* add finishing 0 to the string */
-       static const wchar_rep_t nul = L'\0';
-       obstack_grow(&symbol_obstack, &nul, sizeof(nul));
-
-       const size_t size
-               = (size_t)obstack_object_size(&symbol_obstack) / sizeof(wchar_rep_t);
-       const wchar_rep_t *const string = obstack_finish(&symbol_obstack);
-
-#if 0 /* TODO hash */
-       /* check if there is already a copy of the string */
-       const wchar_rep_t *const result = strset_insert(&stringset, string);
-       if(result != string) {
-               obstack_free(&symbol_obstack, string);
-       }
-#else
-       const wchar_rep_t *const result = string;
-#endif
-
-       pp_token.type                = TP_WIDE_STRING_LITERAL;
-       pp_token.v.wide_string.begin = result;
-       pp_token.v.wide_string.size  = size;
-}
-
 static void parse_character_constant(void)
 {
-       const unsigned start_linenr = input.position.linenr;
+       const unsigned start_linenr = input.position.lineno;
 
        eat('\'');
 
@@ -615,7 +554,7 @@ static void parse_character_constant(void)
                case EOF: {
                        source_position_t source_position;
                        source_position.input_name = pp_token.source_position.input_name;
-                       source_position.linenr     = start_linenr;
+                       source_position.lineno     = start_linenr;
                        errorf(&source_position, "EOF while parsing character constant");
                        pp_token.type = TP_ERROR;
                        return;
@@ -637,9 +576,9 @@ end_of_char_constant:;
        const size_t      size   = (size_t)obstack_object_size(&symbol_obstack);
        const char *const string = obstack_finish(&symbol_obstack);
 
-       pp_token.type           = TP_CHARACTER_CONSTANT;
-       pp_token.v.string.begin = string;
-       pp_token.v.string.size  = size;
+       pp_token.type          = TP_CHARACTER_CONSTANT;
+       pp_token.literal.begin = string;
+       pp_token.literal.size  = size;
 }
 
 #define SYMBOL_CHARS_WITHOUT_E_P \
@@ -747,7 +686,7 @@ restart:
                return;
 
        /* if it was an identifier then we might need to expand again */
-       pp_definition_t *symbol_definition = pp_token.v.symbol->pp_definition;
+       pp_definition_t *symbol_definition = pp_token.symbol->pp_definition;
        if(symbol_definition != NULL && !symbol_definition->is_expanding) {
                symbol_definition->parent_expansion = definition;
                symbol_definition->expand_pos       = 0;
@@ -784,7 +723,7 @@ static void skip_multiline_comment(void)
        if(do_print_spaces)
                counted_spaces++;
 
-       unsigned start_linenr = input.position.linenr;
+       unsigned start_linenr = input.position.lineno;
        while(1) {
                switch(CC) {
                case '/':
@@ -812,7 +751,7 @@ static void skip_multiline_comment(void)
                case EOF: {
                        source_position_t source_position;
                        source_position.input_name = pp_token.source_position.input_name;
-                       source_position.linenr     = start_linenr;
+                       source_position.lineno     = start_linenr;
                        errorf(&source_position, "at end of file while looking for comment end");
                        return;
                }
@@ -831,9 +770,9 @@ static void skip_spaces(bool skip_newline)
                switch (CC) {
                case ' ':
                case '\t':
-                       if(do_print_spaces)
+                       if (do_print_spaces)
                                counted_spaces++;
-                       next_char();
+                       next_char();
                        continue;
                case '/':
                        next_char();
@@ -859,7 +798,7 @@ static void skip_spaces(bool skip_newline)
                        if(CC == '\n') {
                                next_char();
                        }
-                       ++input.position.linenr;
+                       ++input.position.lineno;
                        if (do_print_spaces)
                                ++counted_newlines;
                        continue;
@@ -869,7 +808,7 @@ static void skip_spaces(bool skip_newline)
                                return;
 
                        next_char();
-                       ++input.position.linenr;
+                       ++input.position.lineno;
                        if (do_print_spaces)
                                ++counted_newlines;
                        continue;
@@ -880,7 +819,7 @@ static void skip_spaces(bool skip_newline)
        }
 }
 
-static void eat_pp(preprocessor_token_type_t type)
+static void eat_pp(int type)
 {
        (void) type;
        assert(pp_token.type == type);
@@ -912,7 +851,7 @@ end_symbol:
        /* might be a wide string or character constant ( L"string"/L'c' ) */
        if (CC == '"' && string[0] == 'L' && string[1] == '\0') {
                obstack_free(&symbol_obstack, string);
-               parse_wide_string_literal();
+               /* TODO */
                return;
        } else if (CC == '\'' && string[0] == 'L' && string[1] == '\0') {
                obstack_free(&symbol_obstack, string);
@@ -922,8 +861,8 @@ end_symbol:
 
        symbol_t *symbol = symbol_table_insert(string);
 
-       pp_token.type     = symbol->pp_ID;
-       pp_token.v.symbol = symbol;
+       pp_token.type   = symbol->pp_ID;
+       pp_token.symbol = symbol;
 
        /* we can free the memory from symbol obstack if we already had an entry in
         * the symbol table */
@@ -993,13 +932,12 @@ end_number:
        size_t  size   = obstack_object_size(&symbol_obstack);
        char   *string = obstack_finish(&symbol_obstack);
 
-       pp_token.type           = TP_NUMBER;
-       pp_token.v.string.begin = string;
-       pp_token.v.string.size  = size;
+       pp_token.type          = TP_NUMBER;
+       pp_token.literal.begin = string;
+       pp_token.literal.size  = size;
 }
 
 
-
 #define MAYBE_PROLOG                                       \
                        next_char();                                   \
                        while(1) {                                     \
@@ -1013,15 +951,14 @@ end_number:
 
 #define ELSE_CODE(code)                                    \
                                default:                                   \
-                                       code;                                  \
+                                       code                                   \
+                                       return;                                \
                                }                                          \
                        } /* end of while(1) */                        \
-                       break;
 
 #define ELSE(set_type)                                     \
                ELSE_CODE(                                         \
                        pp_token.type = set_type;                      \
-                       return;                                        \
                )
 
 static void next_preprocessing_token(void)
@@ -1037,9 +974,9 @@ restart:
        switch(CC) {
        case ' ':
        case '\t':
-               if(do_print_spaces)
+               if (do_print_spaces)
                        counted_spaces++;
-               next_char();
+               next_char();
                goto restart;
 
        MATCH_NEWLINE(
@@ -1089,7 +1026,6 @@ restart:
                                        put_back(CC);
                                        CC = '.';
                                        pp_token.type = '.';
-                                       return;
                                )
                ELSE('.')
        case '&':
@@ -1141,7 +1077,6 @@ restart:
                                                        put_back(CC);
                                                        CC = '%';
                                                        pp_token.type = '#';
-                                                       return;
                                                )
                                ELSE('#')
                ELSE('%')
@@ -1239,7 +1174,7 @@ static void print_quoted_string(const char *const string)
                case '\?':  fputs("\\?", out); break;
                default:
                        if(!isprint(*c)) {
-                               fprintf(out, "\\%03o", *c);
+                               fprintf(out, "\\%03o", (unsigned)*c);
                                break;
                        }
                        fputc(*c, out);
@@ -1251,7 +1186,7 @@ static void print_quoted_string(const char *const string)
 
 static void print_line_directive(const source_position_t *pos, const char *add)
 {
-       fprintf(out, "# %d ", pos->linenr);
+       fprintf(out, "# %u ", pos->lineno);
        print_quoted_string(pos->input_name);
        if (add != NULL) {
                fputc(' ', out);
@@ -1264,7 +1199,7 @@ static void print_line_directive(const source_position_t *pos, const char *add)
 
 static void print_spaces(void)
 {
-       if (counted_newlines >= 8) {
+       if (counted_newlines >= 9) {
                if (input.had_non_space) {
                        fputc('\n', out);
                }
@@ -1292,14 +1227,14 @@ static void emit_pp_token(void)
 
        switch(pp_token.type) {
        case TP_IDENTIFIER:
-               fputs(pp_token.v.symbol->string, out);
+               fputs(pp_token.symbol->string, out);
                break;
        case TP_NUMBER:
-               fputs(pp_token.v.string.begin, out);
+               fputs(pp_token.literal.begin, out);
                break;
        case TP_STRING_LITERAL:
                fputc('"', out);
-               fputs(pp_token.v.string.begin, out);
+               fputs(pp_token.literal.begin, out);
                fputc('"', out);
                break;
        case '\n':
@@ -1332,22 +1267,6 @@ static bool strings_equal(const string_t *string1, const string_t *string2)
        return true;
 }
 
-static bool wide_strings_equal(const wide_string_t *string1,
-                               const wide_string_t *string2)
-{
-       size_t size = string1->size;
-       if(size != string2->size)
-               return false;
-
-       const wchar_rep_t *c1 = string1->begin;
-       const wchar_rep_t *c2 = string2->begin;
-       for(size_t i = 0; i < size; ++i, ++c1, ++c2) {
-               if(*c1 != *c2)
-                       return false;
-       }
-       return true;
-}
-
 static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
 {
        if(token1->type != token2->type)
@@ -1358,16 +1277,12 @@ static bool pp_tokens_equal(const token_t *token1, const token_t *token2)
                /* TODO */
                return false;
        case TP_IDENTIFIER:
-               return token1->v.symbol == token2->v.symbol;
+               return token1->symbol == token2->symbol;
        case TP_NUMBER:
        case TP_CHARACTER_CONSTANT:
        case TP_STRING_LITERAL:
-               return strings_equal(&token1->v.string, &token2->v.string);
+               return strings_equal(&token1->literal, &token2->literal);
 
-       case TP_WIDE_CHARACTER_CONSTANT:
-       case TP_WIDE_STRING_LITERAL:
-               return wide_strings_equal(&token1->v.wide_string,
-                                         &token2->v.wide_string);
        default:
                return true;
        }
@@ -1399,7 +1314,7 @@ static void parse_define_directive(void)
                       "expected identifier after #define, got '%t'", &pp_token);
                goto error_out;
        }
-       symbol_t *symbol = pp_token.v.symbol;
+       symbol_t *symbol = pp_token.symbol;
 
        pp_definition_t *new_definition
                = obstack_alloc(&pp_obstack, sizeof(new_definition[0]));
@@ -1427,7 +1342,7 @@ static void parse_define_directive(void)
                                }
                                break;
                        case TP_IDENTIFIER:
-                               obstack_ptr_grow(&pp_obstack, pp_token.v.symbol);
+                               obstack_ptr_grow(&pp_obstack, pp_token.symbol);
                                next_preprocessing_token();
 
                                if (pp_token.type == ',') {
@@ -1477,8 +1392,7 @@ static void parse_define_directive(void)
        pp_definition_t *old_definition = symbol->pp_definition;
        if (old_definition != NULL) {
                if (!pp_definitions_equal(old_definition, new_definition)) {
-                       warningf(&input.position, "multiple definition of macro '%Y' (first defined %P)",
-                                symbol, &old_definition->source_position);
+                       warningf(WARN_OTHER, &input.position, "multiple definition of macro '%Y' (first defined %P)", symbol, &old_definition->source_position);
                } else {
                        /* reuse the old definition */
                        obstack_free(&pp_obstack, new_definition);
@@ -1508,12 +1422,12 @@ static void parse_undef_directive(void)
                return;
        }
 
-       symbol_t *symbol = pp_token.v.symbol;
+       symbol_t *symbol = pp_token.symbol;
        symbol->pp_definition = NULL;
        next_preprocessing_token();
 
        if(pp_token.type != '\n') {
-               warningf(&input.position, "extra tokens at end of #undef directive");
+               warningf(WARN_OTHER, &input.position, "extra tokens at end of #undef directive");
        }
        /* eat until '\n' */
        eat_pp_directive();
@@ -1609,8 +1523,7 @@ static bool parse_include_directive(void)
        }
 
        if (pp_token.type != '\n' && pp_token.type != TP_EOF) {
-               warningf(&pp_token.source_position,
-                        "extra tokens at end of #include directive");
+               warningf(WARN_OTHER, &pp_token.source_position, "extra tokens at end of #include directive");
                eat_pp_directive();
        }
 
@@ -1696,7 +1609,7 @@ static void parse_ifdef_ifndef_directive(void)
                /* just take the true case in the hope to avoid further errors */
                condition = true;
        } else {
-               symbol_t        *symbol        = pp_token.v.symbol;
+               symbol_t        *symbol        = pp_token.symbol;
                pp_definition_t *pp_definition = symbol->pp_definition;
                next_preprocessing_token();
 
@@ -1726,7 +1639,7 @@ static void parse_else_directive(void)
 
        if (pp_token.type != '\n') {
                if (!skip_mode) {
-                       warningf(&pp_token.source_position, "extra tokens at end of #else");
+                       warningf(WARN_OTHER, &pp_token.source_position, "extra tokens at end of #else");
                }
                eat_pp_directive();
        }
@@ -1758,8 +1671,7 @@ static void parse_endif_directive(void)
 
        if (pp_token.type != '\n') {
                if (!skip_mode) {
-                       warningf(&pp_token.source_position,
-                                "extra tokens at end of #endif");
+                       warningf(WARN_OTHER, &pp_token.source_position, "extra tokens at end of #endif");
                }
                eat_pp_directive();
        }