X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Fcpp_tokiter.c;h=8073281ea7c80f12043e2d0644edbd6844dee53b;hp=d980e61fde38353c33dba25d3ef613c8be9a638d;hb=47dd00ab6df0a093b13d4f573ba01c79a6bcc72f;hpb=42f44eb5bf4ecd36e26e051fada79d861d0f92d2 diff --git a/src/cpp_tokiter.c b/src/cpp_tokiter.c index d980e61..8073281 100644 --- a/src/cpp_tokiter.c +++ b/src/cpp_tokiter.c @@ -28,17 +28,28 @@ #include "cpp.h" #include "parser.h" - +#if PSI_CPP_DEBUG > 1 void psi_cpp_tokiter_dump(int fd, struct psi_cpp *cpp) { - size_t i; + size_t i = cpp->index; struct psi_token *T; - for (i = 0; psi_plist_get(cpp->tokens, i, &T); ++i) { + if (i > 20) { + i -= 20; + } else { + i = 0; + } + while (psi_plist_get(cpp->tokens, i, &T)) { dprintf(fd, "PSI: CPP tokens %5zu %c ", i, cpp->index == i ? '*' : ' '); psi_token_dump(fd, T); + if (i >= cpp->index + 10) { + dprintf(fd, "PSI: CPP tokens .....\n"); + break; + } + ++i; } } +#endif void psi_cpp_tokiter_reset(struct psi_cpp *cpp) { @@ -117,8 +128,8 @@ bool psi_cpp_tokiter_del_cur(struct psi_cpp *cpp, bool free_token) fprintf(stderr, "NULL\n"); } #endif - if (cur && free_token) { - free(cur); + if (free_token) { + psi_token_free(&cur); } count = psi_plist_count(cpp->tokens); if (deleted && cpp->index >= count) { @@ -150,14 +161,19 @@ bool psi_cpp_tokiter_del_range(struct psi_cpp *cpp, size_t offset, size_t num_el deleted = psi_plist_del_r(cpp->tokens, offset, num_eles, (void *) ptr); if (deleted) { - if (cpp->index >= psi_plist_count(cpp->tokens)) { - cpp->index = MAX(0, psi_plist_count(cpp->tokens)-1); + size_t count = psi_plist_count(cpp->tokens); + + if (cpp->index >= count) { + if (count > 0) { + cpp->index = count - 1; + } else { + cpp->index = 0; + } } + if (free_tokens) { while (num_eles--) { - if (ptr[num_eles]) { - free(ptr[num_eles]); - } + psi_token_free(&ptr[num_eles]); } free(ptr); } @@ -215,7 +231,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; @@ -225,40 +242,52 @@ 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 *tmp_tok, *old_tok = exp_tokens[n - 1]; + + tmp_tok = psi_token_init(old_tok->type, "", 0, + target->col, target->line, + target->file ? target->file : zend_empty_string); + + new_tok = psi_token_cat(NULL, 3, tmp_tok, old_tok, tok); + psi_token_free(&old_tok); + psi_token_free(&tmp_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->val, tok->text->len, target->col, target->line, + target->file ?: zend_empty_string); - 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; } } @@ -272,7 +301,7 @@ static void psi_cpp_tokiter_free_call_tokens(struct psi_plist **arg_tokens_list, struct psi_token *tok; while (psi_plist_pop(arg_tokens_list[i], &tok)) { - free(tok); + psi_token_free(&tok); } } psi_plist_free(arg_tokens_list[i]); @@ -291,10 +320,6 @@ static struct psi_plist **psi_cpp_tokiter_read_call_tokens( arg_tokens[0] = psi_plist_init(NULL); - /* free macro name token on success */ - tok = psi_cpp_tokiter_current(cpp); - free_tokens = psi_plist_add(free_tokens, &tok); - /* next token must be a LPAREN for a macro call */ psi_cpp_tokiter_next(cpp); tok = psi_cpp_tokiter_current(cpp); @@ -355,7 +380,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; @@ -369,11 +395,9 @@ static void psi_cpp_tokiter_expand_call_tokens(struct psi_cpp *cpp, struct psi_token *arg_name; for (s = 0; psi_plist_get(macro->sig, s, &arg_name); ++s) { - if (arg_name->size == tok->size) { - if (!memcmp(arg_name->text, tok->text, tok->size)) { - arg_tokens = arg_tokens_list[s]; - break; - } + if (zend_string_equals(arg_name->text, tok->text)) { + arg_tokens = arg_tokens_list[s]; + break; } } } @@ -385,12 +409,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} @@ -411,23 +435,58 @@ 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 */ psi_cpp_tokiter_seek(cpp, start); + + psi_token_free(&target); + ++cpp->expanded; + 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); + + psi_token_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 !zend_string_equals(r->text, t->text); + } + return -1; +} + bool psi_cpp_tokiter_expand(struct psi_cpp *cpp) { if (psi_cpp_tokiter_valid(cpp)) { struct psi_token *current = psi_cpp_tokiter_current(cpp); if (current) { - struct psi_cpp_macro_decl *macro = zend_hash_str_find_ptr( - cpp->defs, current->text, current->size); + struct psi_cpp_macro_decl *macro = zend_hash_find_ptr( + &cpp->defs, current->text); /* don't expand itself */ if (macro && macro->token != current) { @@ -436,22 +495,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); } } }