cpp: avoid errenous multiple expansions
[m6w6/ext-psi] / src / cpp_tokiter.c
index 2ea9adc80a6411dbff8b2d20b08c591c8404245f..b62b351c21cc01ce8f458e754f489e6baa2d1a90 100644 (file)
@@ -233,7 +233,8 @@ bool psi_cpp_tokiter_defined(struct psi_cpp *cpp)
        return false;
 }
 
-void psi_cpp_tokiter_expand_tokens(struct psi_cpp *cpp, struct psi_plist *tokens)
+static size_t psi_cpp_tokiter_expand_tokens(struct psi_cpp *cpp,
+               struct psi_token *target, struct psi_plist *tokens)
 {
        if (tokens && psi_plist_count(tokens)) {
                size_t i = 0, n = 0;
@@ -243,40 +244,49 @@ void psi_cpp_tokiter_expand_tokens(struct psi_cpp *cpp, struct psi_plist *tokens
                while (psi_plist_get(tokens, i++, &tok)) {
                        struct psi_token *new_tok;
 
+                       if (tok->type == PSI_T_EOL) {
+                               continue;
+                       }
                        if (tok->type == PSI_T_HASH) {
-                               if (stringify) {
-                                       stringify = false;
-                                       paste = true;
-                               } else {
-                                       stringify = true;
-                               }
+                               stringify = true;
+                               continue;
+                       }
+                       if (tok->type == PSI_T_CPP_PASTE) {
+                               paste = true;
                                continue;
                        }
 
-                       if (paste && n > 0 && exp_tokens[n - 1] &&
-                                       (new_tok = psi_token_cat(NULL, 2, exp_tokens[n - 1], tok))) {
-                               free(exp_tokens[n - 1]);
+                       if (paste && n > 0 && exp_tokens[n - 1]) {
+                               struct psi_token *old_tok = exp_tokens[n - 1];
+
+                               new_tok = psi_token_init(old_tok->type, "", 0,
+                                               target->col, target->line, target->file?:"");
+
+                               new_tok = psi_token_cat(NULL, 2, new_tok, old_tok, tok);
+                               free(old_tok);
+
                                exp_tokens[n - 1] = new_tok;
                        } else {
-                               struct psi_token *cpy = psi_token_copy(tok);
+                               new_tok = psi_token_init(stringify ? PSI_T_QUOTED_STRING : tok->type,
+                                               tok->text, tok->size, target->col, target->line, target->file?:"");
 
-                               if (stringify) {
-                                       cpy = psi_token_append(NULL,
-                                                       psi_token_prepend(NULL, cpy, 1, "\""), 1, "\"");
-                                       cpy->type = PSI_T_QUOTED_STRING;
-                               }
-                               exp_tokens[n++] = cpy;
+                               exp_tokens[n++] = new_tok;
                        }
 
 #if PSI_CPP_DEBUG
                        fprintf(stderr, "PSI: CPP expand > ");
                        psi_token_dump(2, tok);
 #endif
+
                        paste = false;
                        stringify = false;
                }
                psi_cpp_tokiter_ins_range(cpp, psi_cpp_tokiter_index(cpp), n, (void *) exp_tokens);
                free(exp_tokens);
+
+               return n;
+       } else {
+               return 0;
        }
 }
 
@@ -373,7 +383,8 @@ fail:
 }
 
 static void psi_cpp_tokiter_expand_call_tokens(struct psi_cpp *cpp,
-               struct psi_cpp_macro_decl *macro, struct psi_plist **arg_tokens_list)
+               struct psi_token *target, struct psi_cpp_macro_decl *macro,
+               struct psi_plist **arg_tokens_list)
 {
        size_t i;
        struct psi_token *tok;
@@ -403,12 +414,12 @@ static void psi_cpp_tokiter_expand_call_tokens(struct psi_cpp *cpp,
                }
        }
 
-       psi_cpp_tokiter_expand_tokens(cpp, tokens);
+       psi_cpp_tokiter_expand_tokens(cpp, target, tokens);
        psi_plist_free(tokens);
 }
 
 static bool psi_cpp_tokiter_expand_call(struct psi_cpp *cpp,
-               struct psi_cpp_macro_decl *macro)
+               struct psi_token *target, struct psi_cpp_macro_decl *macro)
 {
        /* function-like macro
         * #define FOO(a,b) a>b // macro->sig == {a, b}, macro->tokens = {a, >, b}
@@ -429,7 +440,7 @@ static bool psi_cpp_tokiter_expand_call(struct psi_cpp *cpp,
        psi_cpp_tokiter_seek(cpp, start);
 
        /* insert and expand macro tokens */
-       psi_cpp_tokiter_expand_call_tokens(cpp, macro, arg_tokens_list);
+       psi_cpp_tokiter_expand_call_tokens(cpp, target, macro, arg_tokens_list);
        psi_cpp_tokiter_free_call_tokens(arg_tokens_list, psi_plist_count(macro->sig), true);
 
        /* back to where we took off */
@@ -438,6 +449,39 @@ static bool psi_cpp_tokiter_expand_call(struct psi_cpp *cpp,
        return true;
 }
 
+static bool psi_cpp_tokiter_expand_def(struct psi_cpp *cpp,
+               struct psi_token *target, struct psi_cpp_macro_decl *macro)
+{
+       size_t index = psi_cpp_tokiter_index(cpp);
+
+       /* delete current token from stream */
+       psi_cpp_tokiter_del_cur(cpp, false);
+
+       if (index != psi_cpp_tokiter_index(cpp)) {
+               /* might have been last token */
+               psi_cpp_tokiter_next(cpp);
+       }
+       /* replace with tokens from macro */
+       psi_cpp_tokiter_expand_tokens(cpp, target, macro->tokens);
+
+       free(target);
+       ++cpp->expanded;
+       return true;
+}
+
+static inline int psi_cpp_tokiter_expand_cmp(struct psi_token *t,
+               struct psi_cpp_macro_decl *m)
+{
+       if (psi_plist_count(m->tokens) == 1) {
+               struct psi_token *r;
+
+               psi_plist_get(m->tokens, 0, &r);
+
+               return strcmp(r->text, t->text);
+       }
+       return -1;
+}
+
 bool psi_cpp_tokiter_expand(struct psi_cpp *cpp)
 {
        if (psi_cpp_tokiter_valid(cpp)) {
@@ -454,22 +498,9 @@ bool psi_cpp_tokiter_expand(struct psi_cpp *cpp)
                                psi_token_dump(2, current);
 #endif
                                if (macro->sig) {
-                                       return psi_cpp_tokiter_expand_call(cpp, macro);
-                               } else {
-                                       size_t index = psi_cpp_tokiter_index(cpp);
-
-                                       /* delete current token from stream */
-                                       psi_cpp_tokiter_del_cur(cpp, true);
-
-                                       if (index != psi_cpp_tokiter_index(cpp)) {
-                                               /* might have been last token */
-                                               psi_cpp_tokiter_next(cpp);
-                                       }
-                                       /* replace with tokens from macro */
-                                       psi_cpp_tokiter_expand_tokens(cpp, macro->tokens);
-
-                                       ++cpp->expanded;
-                                       return true;
+                                       return psi_cpp_tokiter_expand_call(cpp, current, macro);
+                               } else if (psi_cpp_tokiter_expand_cmp(current, macro)) {
+                                       return psi_cpp_tokiter_expand_def(cpp, current, macro);
                                }
                        }
                }