more work towards expansion of macros with arguments
authorMatthias Braun <matze@braunis.de>
Fri, 7 Nov 2008 08:18:40 +0000 (08:18 +0000)
committerMatthias Braun <matze@braunis.de>
Fri, 7 Nov 2008 08:18:40 +0000 (08:18 +0000)
[r23505]

preprocessor.c
preproctest/directives.c

index ca342c0..f56a182 100644 (file)
 #define MAX_PUTBACK 3
 #define INCLUDE_LIMIT 199  /* 199 is for gcc "compatibility" */
 
+struct pp_argument_t {
+       size_t   list_len;
+       token_t *token_list;
+};
+
 struct pp_definition_t {
        symbol_t          *symbol;
        source_position_t  source_position;
        pp_definition_t   *parent_expansion;
        size_t             expand_pos;
-       bool               is_variadic   : 1;
-       bool               is_expanding  : 1;
-       bool               has_arguments : 1;
-       size_t             n_arguments;
-       symbol_t          *arguments;
+       bool               is_variadic    : 1;
+       bool               is_expanding   : 1;
+       bool               has_parameters : 1;
+       size_t             n_parameters;
+       symbol_t          *parameters;
+
+       /* replacement */
        size_t             list_len;
-       token_t           *replacement_list;
+       token_t           *token_list;
+
 };
 
 typedef struct pp_conditional_t pp_conditional_t;
@@ -732,7 +740,7 @@ restart:
                current_expansion = definition;
                goto restart;
        }
-       pp_token = definition->replacement_list[definition->expand_pos];
+       pp_token = definition->token_list[definition->expand_pos];
        ++definition->expand_pos;
 
        if(pp_token.type != TP_IDENTIFIER)
@@ -750,6 +758,135 @@ restart:
        }
 }
 
+static void skip_line_comment(void)
+{
+       if(do_print_spaces)
+               counted_spaces++;
+
+       while(1) {
+               switch(CC) {
+               case EOF:
+                       return;
+
+               case '\n':
+               case '\r':
+                       return;
+
+               default:
+                       next_char();
+                       break;
+               }
+       }
+}
+
+static void skip_multiline_comment(void)
+{
+       if(do_print_spaces)
+               counted_spaces++;
+
+       unsigned start_linenr = input.position.linenr;
+       while(1) {
+               switch(CC) {
+               case '/':
+                       next_char();
+                       if (CC == '*') {
+                               /* TODO: nested comment, warn here */
+                       }
+                       break;
+               case '*':
+                       next_char();
+                       if(CC == '/') {
+                               next_char();
+                               return;
+                       }
+                       break;
+
+               MATCH_NEWLINE(
+                       if(do_print_spaces) {
+                               counted_newlines++;
+                               counted_spaces = 0;
+                       }
+                       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, "at end of file while looking for comment end");
+                       return;
+               }
+
+               default:
+                       next_char();
+                       break;
+               }
+       }
+}
+
+/* skip spaces advancing at the start of the next preprocessing token */
+static void skip_spaces(bool skip_newline)
+{
+       while (true) {
+               switch (CC) {
+               case ' ':
+               case '\t':
+                       if(do_print_spaces)
+                               counted_spaces++;
+                       next_char();
+                       continue;
+               case '/':
+                       next_char();
+                       if (CC == '/') {
+                               next_char();
+                               skip_line_comment();
+                               continue;
+                       } else if (CC == '*') {
+                               next_char();
+                               skip_multiline_comment();
+                               continue;
+                       } else {
+                               put_back(CC);
+                               CC = '/';
+                       }
+                       return;
+
+               case '\r':
+                       if (!skip_newline)
+                               return;
+
+                       next_char();
+                       if(CC == '\n') {
+                               next_char();
+                       }
+                       ++input.position.linenr;
+                       if (do_print_spaces)
+                               ++counted_newlines;
+                       continue;
+
+               case '\n':
+                       if (!skip_newline)
+                               return;
+
+                       next_char();
+                       ++input.position.linenr;
+                       if (do_print_spaces)
+                               ++counted_newlines;
+                       continue;
+
+               default:
+                       return;
+               }
+       }
+}
+
+static void eat_pp(preprocessor_token_type_t type)
+{
+       (void) type;
+       assert(pp_token.type == type);
+       next_preprocessing_token();
+}
+
 static void parse_symbol(void)
 {
        obstack_1grow(&symbol_obstack, (char) CC);
@@ -773,11 +910,11 @@ end_symbol:
        char *string = obstack_finish(&symbol_obstack);
 
        /* might be a wide string or character constant ( L"string"/L'c' ) */
-       if(CC == '"' && string[0] == 'L' && string[1] == '\0') {
+       if (CC == '"' && string[0] == 'L' && string[1] == '\0') {
                obstack_free(&symbol_obstack, string);
                parse_wide_string_literal();
                return;
-       } else if(CC == '\'' && string[0] == 'L' && string[1] == '\0') {
+       } else if (CC == '\'' && string[0] == 'L' && string[1] == '\0') {
                obstack_free(&symbol_obstack, string);
                parse_wide_character_constant();
                return;
@@ -790,17 +927,34 @@ end_symbol:
 
        /* we can free the memory from symbol obstack if we already had an entry in
         * the symbol table */
-       if(symbol->string != string) {
+       if (symbol->string != string) {
                obstack_free(&symbol_obstack, string);
        }
+       if (!do_expansions)
+               return;
 
        pp_definition_t *pp_definition = symbol->pp_definition;
-       if(do_expansions && pp_definition != NULL) {
-               pp_definition->expand_pos   = 0;
-               pp_definition->is_expanding = true,
-               current_expansion           = pp_definition;
-               expand_next();
+       if (pp_definition == NULL)
+               return;
+
+       if (pp_definition->has_parameters) {
+               skip_spaces(true);
+               /* no opening brace -> no expansion */
+               if (CC != '(')
+                       return;
+               next_preprocessing_token();
+               eat_pp('(');
+
+               /* parse arguments (TODO) */
+               while (pp_token.type != TP_EOF && pp_token.type != ')')
+                       next_preprocessing_token();
+               next_preprocessing_token();
        }
+
+       pp_definition->expand_pos   = 0;
+       pp_definition->is_expanding = true,
+       current_expansion           = pp_definition;
+       expand_next();
 }
 
 static void parse_number(void)
@@ -844,72 +998,6 @@ end_number:
        pp_token.v.string.size  = size;
 }
 
-static void skip_multiline_comment(void)
-{
-       if(do_print_spaces)
-               counted_spaces++;
-
-       unsigned start_linenr = input.position.linenr;
-       while(1) {
-               switch(CC) {
-               case '/':
-                       next_char();
-                       if (CC == '*') {
-                               /* TODO: nested comment, warn here */
-                       }
-                       break;
-               case '*':
-                       next_char();
-                       if(CC == '/') {
-                               next_char();
-                               return;
-                       }
-                       break;
-
-               MATCH_NEWLINE(
-                       if(do_print_spaces) {
-                               counted_newlines++;
-                               counted_spaces = 0;
-                       }
-                       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, "at end of file while looking for comment end");
-                       return;
-               }
-
-               default:
-                       next_char();
-                       break;
-               }
-       }
-}
-
-static void skip_line_comment(void)
-{
-       if(do_print_spaces)
-               counted_spaces++;
-
-       while(1) {
-               switch(CC) {
-               case EOF:
-                       return;
-
-               case '\n':
-               case '\r':
-                       return;
-
-               default:
-                       next_char();
-                       break;
-               }
-       }
-}
-
 
 
 #define MAYBE_PROLOG                                       \
@@ -1222,13 +1310,6 @@ static void emit_pp_token(void)
        }
 }
 
-static void eat_pp(preprocessor_token_type_t type)
-{
-       (void) type;
-       assert(pp_token.type == type);
-       next_preprocessing_token();
-}
-
 static void eat_pp_directive(void)
 {
        while(pp_token.type != '\n' && pp_token.type != TP_EOF) {
@@ -1299,8 +1380,8 @@ static bool pp_definitions_equal(const pp_definition_t *definition1,
                return false;
 
        size_t         len = definition1->list_len;
-       const token_t *t1  = definition1->replacement_list;
-       const token_t *t2  = definition2->replacement_list;
+       const token_t *t1  = definition1->token_list;
+       const token_t *t2  = definition2->token_list;
        for(size_t i = 0; i < len; ++i, ++t1, ++t2) {
                if(!pp_tokens_equal(t1, t2))
                        return false;
@@ -1362,6 +1443,7 @@ static void parse_define_directive(void)
                                }
                                break;
                        case ')':
+                               next_preprocessing_token();
                                goto finish_argument_list;
                        default:
                                errorf(&pp_token.source_position,
@@ -1372,10 +1454,10 @@ static void parse_define_directive(void)
                }
 
        finish_argument_list:
-               new_definition->has_arguments = true;
-               new_definition->n_arguments
-                       = obstack_object_size(&pp_obstack) / sizeof(new_definition->arguments[0]);
-               new_definition->arguments = obstack_finish(&pp_obstack);
+               new_definition->has_parameters = true;
+               new_definition->n_parameters
+                       = obstack_object_size(&pp_obstack) / sizeof(new_definition->parameters[0]);
+               new_definition->parameters = obstack_finish(&pp_obstack);
        } else {
                next_preprocessing_token();
        }
@@ -1389,8 +1471,8 @@ static void parse_define_directive(void)
                next_preprocessing_token();
        }
 
-       new_definition->list_len         = list_len;
-       new_definition->replacement_list = obstack_finish(&pp_obstack);
+       new_definition->list_len   = list_len;
+       new_definition->token_list = obstack_finish(&pp_obstack);
 
        pp_definition_t *old_definition = symbol->pp_definition;
        if (old_definition != NULL) {
@@ -1437,38 +1519,6 @@ static void parse_undef_directive(void)
        eat_pp_directive();
 }
 
-/* skip spaces advancing at the start of the next preprocessing token */
-static void skip_spaces(void)
-{
-       while (true) {
-               switch (CC) {
-               case ' ':
-               case '\t':
-                       if(do_print_spaces)
-                               counted_spaces++;
-                       next_char();
-                       continue;
-               case '/':
-                       next_char();
-                       if (CC == '/') {
-                               next_char();
-                               skip_line_comment();
-                               continue;
-                       } else if (CC == '*') {
-                               next_char();
-                               skip_multiline_comment();
-                               continue;
-                       } else {
-                               put_back(CC);
-                               CC = '/';
-                       }
-                       return;
-               default:
-                       return;
-               }
-       }
-}
-
 static const char *parse_headername(void)
 {
        /* behind an #include we can have the special headername lexems.
@@ -1477,7 +1527,7 @@ static const char *parse_headername(void)
         * exception here */
 
        /* skip spaces so we reach start of next preprocessing token */
-       skip_spaces();
+       skip_spaces(false);
 
        assert(obstack_object_size(&input_obstack) == 0);
 
index 362eefd..021f573 100644 (file)
@@ -6,10 +6,14 @@
 #/*blup*/      /*bla*/define FOO5 bar
 #/*blup
   */define FOO6 bar
+#define FO\
+O7 bar
 #//foobar
-define FOO7 bar
-#
 define FOO8 bar
+#
+define FOO9 bar
+#define FOO10\
+(x) bar(x)
 
 FOO1
 FOO2
@@ -19,6 +23,8 @@ FOO5
 FOO6
 FOO7
 FOO8
+FOO9
+FOO10
 
 #define foo foo1
 #/*inc*/include/* haha