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) {
if (free_tokens) {
while (num_eles--) {
- if (ptr[num_eles]) {
- free(ptr[num_eles]);
- }
+ psi_token_free(&ptr[num_eles]);
}
free(ptr);
}
return false;
}
-void psi_cpp_tokiter_expand_tokens(struct psi_cpp *cpp, struct psi_token *target,
- 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;
}
if (paste && n > 0 && exp_tokens[n - 1]) {
- struct psi_token *old_tok = exp_tokens[n - 1];
+ struct psi_token *tmp_tok, *old_tok = exp_tokens[n - 1];
- new_tok = psi_token_init(old_tok->type, "", 0,
- target->col, target->line, target->file);
+ tmp_tok = psi_token_init(old_tok->type, "", 0,
+ target->col, target->line,
+ target->file ? target->file->val : "");
- new_tok = psi_token_cat(NULL, 2, new_tok, old_tok, tok);
- free(old_tok);
+ 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 {
new_tok = psi_token_init(stringify ? PSI_T_QUOTED_STRING : tok->type,
- tok->text, tok->size, target->col, target->line, target->file);
+ tok->text->val, tok->text->len, target->col, target->line, target->file?:"");
exp_tokens[n++] = new_tok;
}
}
psi_cpp_tokiter_ins_range(cpp, psi_cpp_tokiter_index(cpp), n, (void *) exp_tokens);
free(exp_tokens);
+
+ return n;
+ } else {
+ return 0;
}
}
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]);
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);
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;
}
}
}
/* 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) {
#endif
if (macro->sig) {
return psi_cpp_tokiter_expand_call(cpp, current, macro);
- } else {
- 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, current, macro->tokens);
-
- free(current);
- ++cpp->expanded;
- return true;
+ } else if (psi_cpp_tokiter_expand_cmp(current, macro)) {
+ return psi_cpp_tokiter_expand_def(cpp, current, macro);
}
}
}