X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-psi;a=blobdiff_plain;f=src%2Fcpp.c;h=fc723641dcca1e363399a35b933e248143c998c0;hp=26e324f7c5ecd66565ad3b67421e15476aac8bef;hb=0682ecb84dfe1de08b609c6c0ae641b06f3fa481;hpb=cd0567b4052ee24259bc6b8a9858af9d26f3de48 diff --git a/src/cpp.c b/src/cpp.c index 26e324f..fc72364 100644 --- a/src/cpp.c +++ b/src/cpp.c @@ -41,7 +41,57 @@ #define PSI_CPP_PREDEF #include "php_psi_predef.h" -HashTable psi_cpp_defaults; +static HashTable psi_cpp_defaults; +static HashTable psi_cpp_pragmas; + +typedef bool (*psi_cpp_pragma_func)(struct psi_cpp *cpp, struct psi_cpp_macro_decl *decl); + +static bool psi_cpp_pragma_once(struct psi_cpp *cpp, struct psi_cpp_macro_decl *decl) +{ + return NULL != zend_hash_add_empty_element(&cpp->once, decl->token->file); +} + +static bool psi_cpp_pragma_lib(struct psi_cpp *cpp, struct psi_cpp_macro_decl *decl) +{ + struct psi_token *lib = NULL; + zend_string *libname; + + if (!psi_plist_get(decl->tokens, 0, &lib) + || !lib || lib->type != PSI_T_QUOTED_STRING) { + return false; + } + + libname = zend_string_copy(lib->text); + cpp->parser->file.libnames = psi_plist_add(cpp->parser->file.libnames, + &libname); + return true; +} + +static bool psi_cpp_pragma_blacklist_decl(struct psi_cpp *cpp, struct psi_cpp_macro_decl *decl) +{ + struct psi_token *name; + + if (!psi_plist_get(decl->tokens, 0, &name) + || !name || name->type != PSI_T_QUOTED_STRING) { + return false; + } + + psi_blacklist_add_decl(name->text->val, name->text->len); + return true; +} + +static bool psi_cpp_pragma_blacklist_var(struct psi_cpp *cpp, struct psi_cpp_macro_decl *decl) +{ + struct psi_token *name; + + if (!psi_plist_get(decl->tokens, 0, &name) + || !name || name->type != PSI_T_QUOTED_STRING) { + return false; + } + + psi_blacklist_add_var(name->text->val, name->text->len); + return true; +} PHP_MINIT_FUNCTION(psi_cpp); PHP_MINIT_FUNCTION(psi_cpp) @@ -73,6 +123,14 @@ PHP_MINIT_FUNCTION(psi_cpp) psi_parser_dtor(&parser); +#define PSI_CPP_PRAGMA(name) \ + zend_hash_str_add_ptr(&psi_cpp_pragmas, #name, strlen(#name), psi_cpp_pragma_ ## name) + zend_hash_init(&psi_cpp_pragmas, 0, NULL, NULL, 1); + PSI_CPP_PRAGMA(once); + PSI_CPP_PRAGMA(lib); + PSI_CPP_PRAGMA(blacklist_decl); + PSI_CPP_PRAGMA(blacklist_var); + return SUCCESS; } @@ -306,10 +364,10 @@ static bool psi_cpp_stage2(struct psi_cpp *cpp) if (cpp->skip) { if (!cpp->do_cpp) { #if PSI_CPP_DEBUG - psi_debug_lock(PSI_DATA(cpp->parser)); - PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP skip "); - PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, current); - psi_debug_unlock(PSI_DATA(cpp->parser)); + PSI_DEBUG_LOCK(cpp->parser, + PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP skip "); + PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, current); + ); #endif psi_cpp_tokiter_del_cur(cpp, true); continue; @@ -317,12 +375,7 @@ static bool psi_cpp_stage2(struct psi_cpp *cpp) } if (do_expansion && psi_cpp_defined(cpp, current)) { - bool expanded = false; - if (psi_cpp_tokiter_expand(cpp)) { - expanded = true; - } - if (expanded) { continue; } } @@ -334,12 +387,7 @@ static bool psi_cpp_stage2(struct psi_cpp *cpp) bool parsed; cpp->do_cpp = false; - parsed = psi_parser_process(cpp->parser, cpp->tokens.exec, &processed); - - /* leave EOLs in the input stream, else we might end up - * with a hash not preceded with a new line after include */ - psi_plist_pop(cpp->tokens.exec, NULL); psi_plist_clean(cpp->tokens.exec); if (!parsed) { @@ -356,6 +404,7 @@ static bool psi_cpp_stage2(struct psi_cpp *cpp) } psi_plist_free(cpp->tokens.exec); + cpp->tokens.exec = NULL; return true; } @@ -380,19 +429,14 @@ bool psi_cpp_process(struct psi_cpp *cpp, struct psi_plist **tokens, zend_hash_del(&cpp->expanding, expanding->text); } - if (cpp->tokens.next) { - free(cpp->tokens.iter); - cpp->tokens.iter = cpp->tokens.next; - cpp->tokens.next = NULL; + *tokens = cpp->tokens.next; + psi_plist_free(cpp->tokens.iter); + if (cpp->tokens.exec) { + assert(!psi_plist_count(cpp->tokens.exec)); + psi_plist_free(cpp->tokens.exec); } - *tokens = cpp->tokens.iter; - - if (temp.tokens.iter) { - cpp->tokens.iter = temp.tokens.iter; - cpp->tokens.next = temp.tokens.next; - cpp->tokens.exec = temp.tokens.exec; - } + cpp->tokens = temp.tokens; cpp->index = temp.index; cpp->skip = temp.skip; cpp->level = temp.level; @@ -410,33 +454,29 @@ bool psi_cpp_defined(struct psi_cpp *cpp, struct psi_token *tok) if (psi_builtin_exists(tok->text)) { defined = true; } else if (!zend_hash_exists(&cpp->expanding, tok->text)) { - struct psi_macro_decl *macro = zend_hash_find_ptr(&cpp->defs, tok->text); - - if (macro) { - defined = true; - } + defined = zend_hash_exists(&cpp->defs, tok->text); } #if PSI_CPP_DEBUG - psi_debug_lock(PSI_DATA(cpp->parser)); - PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP defined -> %s ", defined ? "true" : "false"); - if (defined) { - struct psi_cpp_macro_decl *macro = zend_hash_find_ptr(&cpp->defs, tok->text); - if (macro) { - PSI_DEBUG_PRINT(cpp->parser, " @ %s:%u ", macro->token->file->val, macro->token->line); - } - } else { - zend_string *key; + PSI_DEBUG_LOCK(cpp->parser, + PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP defined -> %s ", defined ? "true" : "false"); + if (defined) { + struct psi_cpp_macro_decl *macro = zend_hash_find_ptr(&cpp->defs, tok->text); + if (macro) { + PSI_DEBUG_PRINT(cpp->parser, " @ %s:%u ", macro->token->file->val, macro->token->line); + } + } else { + zend_string *key; - PSI_DEBUG_PRINT(cpp->parser, " expanding="); - ZEND_HASH_FOREACH_STR_KEY(&cpp->expanding, key) - { - PSI_DEBUG_PRINT(cpp->parser, "%s,", key->val); - } - ZEND_HASH_FOREACH_END(); - PSI_DEBUG_PRINT(cpp->parser, "\t"); - } - PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, tok); - psi_debug_unlock(PSI_DATA(cpp->parser)); + PSI_DEBUG_PRINT(cpp->parser, " expanding="); + ZEND_HASH_FOREACH_STR_KEY(&cpp->expanding, key) + { + PSI_DEBUG_PRINT(cpp->parser, "%s,", key->val); + } + ZEND_HASH_FOREACH_END(); + PSI_DEBUG_PRINT(cpp->parser, "\t"); + } + PSI_DEBUG_DUMP(cpp->parser, psi_token_dump, tok); + ); #endif } @@ -454,15 +494,15 @@ void psi_cpp_define(struct psi_cpp *cpp, struct psi_cpp_macro_decl *decl) "'%s' previously defined", old->token->text->val); } #if PSI_CPP_DEBUG - psi_debug_lock(PSI_DATA(cpp->parser)); - if (decl->exp) { - PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO num_exp -> "); - } else { - PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO decl -> "); - } - PSI_DEBUG_DUMP(cpp->parser, psi_cpp_macro_decl_dump, decl); - PSI_DEBUG_PRINT(cpp->parser, "\n"); - psi_debug_unlock(PSI_DATA(cpp->parser)); + PSI_DEBUG_LOCK(cpp->parser, + if (decl->exp) { + PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO num_exp -> "); + } else { + PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP MACRO decl -> "); + } + PSI_DEBUG_DUMP(cpp->parser, psi_cpp_macro_decl_dump, decl); + PSI_DEBUG_PRINT(cpp->parser, "\n"); + ); #endif zend_hash_update_ptr(&cpp->defs, decl->token->text, decl); } @@ -475,17 +515,33 @@ bool psi_cpp_undef(struct psi_cpp *cpp, struct psi_token *tok) bool psi_cpp_if(struct psi_cpp *cpp, struct psi_cpp_exp *exp) { struct psi_validate_scope scope = {0}; + unsigned flags = cpp->parser->flags; scope.cpp = cpp; + cpp->parser->flags |= PSI_SILENT; if (!psi_num_exp_validate(PSI_DATA(cpp->parser), exp->data.num, &scope)) { + cpp->parser->flags = flags; return false; } + cpp->parser->flags = flags; if (!psi_num_exp_get_long(exp->data.num, NULL, cpp)) { return false; } return true; } +bool psi_cpp_pragma(struct psi_cpp *cpp, struct psi_cpp_macro_decl *decl) +{ + psi_cpp_pragma_func fn; + + fn = zend_hash_find_ptr(&psi_cpp_pragmas, decl->token->text); + if (!fn) { + return false; + } + + return fn(cpp, decl); +} + bool psi_cpp_include(struct psi_cpp *cpp, const struct psi_token *file, unsigned flags) { bool parsed = false; @@ -522,7 +578,10 @@ bool psi_cpp_include(struct psi_cpp *cpp, const struct psi_token *file, unsigned return false; } + ++cpp->include_level; parsed = psi_cpp_process(cpp, &tokens, NULL); + --cpp->include_level; + if (!parsed) { psi_plist_free(tokens); return false;