cpp: fix relative includes
[m6w6/ext-psi] / src / cpp.c
index 0c64246098ae8ba42844bea3e52170a8365fa329..b181e270a0151b7342c2a3b9783dd278278a2f81 100644 (file)
--- a/src/cpp.c
+++ b/src/cpp.c
@@ -25,6 +25,8 @@
 
 #include "php_psi_stdinc.h"
 
+#include <libgen.h>
+
 #include "cpp.h"
 #include "parser.h"
 
@@ -32,6 +34,8 @@
 #define PSI_CPP_PREDEF
 #include "php_psi_cpp.h"
 
+#include "php_psi.h"
+
 static void free_cpp_def(zval *p)
 {
        if (Z_TYPE_P(p) == IS_PTR) {
@@ -44,8 +48,8 @@ struct psi_cpp *psi_cpp_init(struct psi_parser *P)
        struct psi_cpp *cpp = calloc(1, sizeof(*cpp));
 
        cpp->parser = P;
-       ALLOC_HASHTABLE(cpp->defs);
-       zend_hash_init(cpp->defs, 0, NULL, free_cpp_def, 1);
+       zend_hash_init(&cpp->defs, 0, NULL, free_cpp_def, 1);
+       zend_hash_init(&cpp->once, 0, NULL, NULL, 1);
 
        return cpp;
 }
@@ -83,10 +87,10 @@ void psi_cpp_free(struct psi_cpp **cpp_ptr)
                *cpp_ptr = NULL;
                if (cpp->parser->flags & PSI_DEBUG) {
                        fprintf(stderr, "PSI: CPP decls:\n");
-                       zend_hash_apply(cpp->defs, dump_def);
+                       zend_hash_apply(&cpp->defs, dump_def);
                }
-               zend_hash_destroy(cpp->defs);
-               FREE_HASHTABLE(cpp->defs);
+               zend_hash_destroy(&cpp->defs);
+               zend_hash_destroy(&cpp->once);
                free(cpp);
        }
 }
@@ -95,12 +99,15 @@ static bool psi_cpp_stage1(struct psi_cpp *cpp)
 {
        bool name = false, define = false, hash = false, eol = true, esc = false, ws = false;
 
+       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP %s\n", "stage1");
+
        psi_cpp_tokiter_reset(cpp);
        while (psi_cpp_tokiter_valid(cpp)) {
                struct psi_token *token = psi_cpp_tokiter_current(cpp);
 
-               /* strip comments */
-               if (token->type == PSI_T_COMMENT) {
+               /* strip comments and attributes */
+               if (token->type == PSI_T_COMMENT
+                               || token->type == PSI_T_CPP_ATTRIBUTE) {
                        psi_cpp_tokiter_del_cur(cpp, true);
                        continue;
                }
@@ -125,6 +132,9 @@ static bool psi_cpp_stage1(struct psi_cpp *cpp)
                 */
 
                if (token->type == PSI_T_WHITESPACE) {
+                       if (name) {
+                               name = false;
+                       }
                        ws = true;
                        psi_cpp_tokiter_del_cur(cpp, true);
                        continue;
@@ -181,126 +191,131 @@ static bool psi_cpp_stage1(struct psi_cpp *cpp)
 static bool psi_cpp_stage2(struct psi_cpp *cpp)
 {
        struct psi_plist *parser_tokens = psi_plist_init((psi_plist_dtor) psi_token_free);
+       bool is_eol = true, do_cpp = false, do_expansion = true, skip_paren = false, skip_all = false;
 
-       do {
-               bool is_eol = true, do_cpp = false, do_expansion = true, skip_paren = false, skip_all = false;
+       PSI_DEBUG_PRINT(cpp->parser, "PSI: CPP %s\n", "stage2");
 
-               psi_cpp_tokiter_reset(cpp);
-
-               while (psi_cpp_tokiter_valid(cpp)) {
-                       struct psi_token *current = psi_cpp_tokiter_current(cpp);
+       psi_cpp_tokiter_reset(cpp);
+       while (psi_cpp_tokiter_valid(cpp)) {
+               struct psi_token *current = psi_cpp_tokiter_current(cpp);
 
-                       if (current->type == PSI_T_HASH) {
-                               if (is_eol) {
-                                       do_cpp = true;
-                                       is_eol = false;
-                               }
-                       } else if (current->type == PSI_T_EOL) {
+               if (current->type == PSI_T_HASH) {
+                       if (is_eol) {
+                               do_cpp = true;
+                               is_eol = false;
+                       }
+               } else if (current->type == PSI_T_EOL) {
 #if PSI_CPP_DEBUG
-                               fprintf(stderr, "PSI: CPP do_expansion=true, PSI_T_EOL\n");
+                       fprintf(stderr, "PSI: CPP do_expansion=true, PSI_T_EOL\n");
 #endif
-                               is_eol = true;
-                               skip_all = false;
-                               do_expansion = true;
-                               if (!do_cpp) {
-                                       psi_cpp_tokiter_del_cur(cpp, true);
-                                       continue;
-                               }
-                       } else {
-                               is_eol = false;
+                       is_eol = true;
+                       skip_all = false;
+                       do_expansion = true;
+                       if (!do_cpp) {
+                               psi_cpp_tokiter_del_cur(cpp, true);
+                               continue;
+                       }
+               } else {
+                       is_eol = false;
 
-                               if (do_cpp) {
-                                       switch (current->type) {
-                                       case PSI_T_DEFINE:
+                       if (do_cpp) {
+                               switch (current->type) {
+                               case PSI_T_DEFINE:
 #if PSI_CPP_DEBUG
-                                               fprintf(stderr, "PSI: CPP do_expansion=false, PSI_T_DEFINE, skip_all\n");
+                                       fprintf(stderr, "PSI: CPP do_expansion=false, PSI_T_DEFINE, skip_all\n");
 #endif
-                                               do_expansion = false;
-                                               skip_all = true;
-                                               break;
-                                       case PSI_T_DEFINED:
-                                               skip_paren = true;
-                                               /* no break */
-                                       case PSI_T_IFDEF:
-                                       case PSI_T_IFNDEF:
-                                       case PSI_T_UNDEF:
+                                       do_expansion = false;
+                                       skip_all = true;
+                                       break;
+                               case PSI_T_DEFINED:
+                                       skip_paren = true;
+                                       /* no break */
+                               case PSI_T_IFDEF:
+                               case PSI_T_IFNDEF:
+                               case PSI_T_UNDEF:
 #if PSI_CPP_DEBUG
-                                               fprintf(stderr, "PSI: CPP do_expansion=false, PSI_T_{IF{,N},UN}DEF\n");
+                                       fprintf(stderr, "PSI: CPP do_expansion=false, PSI_T_{IF{,N},UN}DEF\n");
 #endif
-                                               do_expansion = false;
-                                               break;
-                                       case PSI_T_LPAREN:
-
-                                               if (!skip_all) {
-                                                       if (skip_paren) {
-                                                               skip_paren = false;
-                                                       } else {
-                                                               do_expansion = true;
+                                       do_expansion = false;
+                                       break;
+                               case PSI_T_LPAREN:
+
+                                       if (!skip_all) {
+                                               if (skip_paren) {
+                                                       skip_paren = false;
+                                               } else {
+                                                       do_expansion = true;
 #if PSI_CPP_DEBUG
-                                                               fprintf(stderr, "PSI: CPP do_expansion=true, PSI_T_LPAREN, !skip_all, !skip_paren\n");
+                                                       fprintf(stderr, "PSI: CPP do_expansion=true, PSI_T_LPAREN, !skip_all, !skip_paren\n");
 #endif
-                                                       }
                                                }
-                                               break;
-                                       case PSI_T_NAME:
-                                               break;
-                                       default:
-                                               do_expansion = !skip_all;
+                                       }
+                                       break;
+                               case PSI_T_NAME:
+                                       break;
+                               default:
+                                       do_expansion = !skip_all;
 #if PSI_CPP_DEBUG
-                                               fprintf(stderr, "PSI: CPP do_expansion=%s, <- !skip_all\n", do_expansion?"true":"false");
+                                       fprintf(stderr, "PSI: CPP do_expansion=%s, <- !skip_all\n", do_expansion?"true":"false");
 #endif
-                                       }
                                }
                        }
+               }
 
-                       if (cpp->skip) {
-                               /* FIXME: del_range */
-                               if (!do_cpp) {
+               if (cpp->skip) {
+                       /* FIXME: del_range */
+                       if (!do_cpp) {
 #if PSI_CPP_DEBUG
-                                       fprintf(stderr, "PSI: CPP skip ");
-                                       psi_token_dump(2, current);
+                               fprintf(stderr, "PSI: CPP skip ");
+                               psi_token_dump(2, current);
 #endif
-                                       psi_cpp_tokiter_del_cur(cpp, true);
-                                       continue;
-                               }
+                               psi_cpp_tokiter_del_cur(cpp, true);
+                               continue;
                        }
+               }
 
-                       if (do_expansion && current->type == PSI_T_NAME && psi_cpp_tokiter_defined(cpp)) {
-                               bool expanded = false;
+               if (do_expansion && current->type == PSI_T_NAME && psi_cpp_tokiter_defined(cpp)) {
+                       bool expanded = false;
 
-                               while (psi_cpp_tokiter_expand(cpp)) {
-                                       expanded = true;
-                               }
-                               if (expanded) {
-                                       continue;
-                               }
+                       while (psi_cpp_tokiter_expand(cpp)) {
+                               expanded = true;
+                       }
+                       if (expanded) {
+                               continue;
                        }
+               }
 
-                       if (do_cpp) {
-                               parser_tokens = psi_plist_add(parser_tokens, &current);
-                               psi_cpp_tokiter_del_cur(cpp, false);
+               if (do_cpp) {
+                       parser_tokens = psi_plist_add(parser_tokens, &current);
 
-                               if (is_eol) {
-                                       size_t processed = 0;
+                       if (is_eol) {
+                               size_t processed = 0;
+                               bool parsed = psi_parser_process(cpp->parser, parser_tokens, &processed);
 
-                                       if (!psi_parser_process(cpp->parser, parser_tokens, &processed)) {
-                                               psi_plist_free(parser_tokens);
-                                               return false;
-                                       }
-                                       psi_plist_clean(parser_tokens);
-                                       do_cpp = false;
+                               /* EOL */
+                               psi_plist_pop(parser_tokens, NULL);
+                               psi_plist_clean(parser_tokens);
+                               do_cpp = false;
+
+                               if (!parsed) {
+                                       psi_plist_free(parser_tokens);
+                                       return false;
                                }
+                       } else {
+                               /* leave EOLs in the input stream, else we might end up
+                                * with a hash not preceded with a new line after include */
+                               psi_cpp_tokiter_del_cur(cpp, false);
+                       }
 
 #if PSI_CPP_DEBUG > 1
-                               psi_cpp_tokiter_dump(2, cpp);
+                       psi_cpp_tokiter_dump(2, cpp);
 #endif
 
-                               continue;
-                       }
-
-                       psi_cpp_tokiter_next(cpp);
+                       continue;
                }
-       } while (cpp->expanded);
+
+               psi_cpp_tokiter_next(cpp);
+       }
 
        psi_plist_free(parser_tokens);
 
@@ -309,17 +324,21 @@ static bool psi_cpp_stage2(struct psi_cpp *cpp)
 
 bool psi_cpp_process(struct psi_cpp *cpp, struct psi_plist **tokens)
 {
+       bool parsed = false;
        struct psi_cpp temp = *cpp;
 
-       temp.tokens = *tokens;
+       cpp->tokens = *tokens;
+       if (psi_cpp_stage1(cpp) && psi_cpp_stage2(cpp)) {
+               parsed = true;
+       }
+       *tokens = cpp->tokens;
 
-       if (psi_cpp_stage1(&temp) && psi_cpp_stage2(&temp)) {
-               *tokens = temp.tokens;
-               return true;
+       if (temp.tokens) {
+               cpp->tokens = temp.tokens;
+               cpp->index = temp.index;
        }
 
-       *tokens = temp.tokens;
-       return false;
+       return parsed;
 }
 
 bool psi_cpp_defined(struct psi_cpp *cpp, struct psi_token *tok)
@@ -327,13 +346,17 @@ bool psi_cpp_defined(struct psi_cpp *cpp, struct psi_token *tok)
        bool defined;
 
        if (tok->type == PSI_T_NAME) {
-               defined = zend_hash_str_exists(cpp->defs, tok->text, tok->size);
+               defined = zend_hash_str_exists(&cpp->defs, tok->text, tok->size);
        } else {
                defined = false;
        }
 
 #if PSI_CPP_DEBUG
        fprintf(stderr, "PSI: CPP defined -> %s ", defined ? "true" : "false");
+       if (defined) {
+               struct psi_cpp_macro_decl *macro = zend_hash_str_find_ptr(&cpp->defs, tok->text, tok->size);
+               fprintf(stderr, " @ %s:%u ", macro->token->file, macro->token->line);
+       }
        psi_token_dump(2, tok);
 #endif
 
@@ -342,7 +365,7 @@ bool psi_cpp_defined(struct psi_cpp *cpp, struct psi_token *tok)
 
 void psi_cpp_define(struct psi_cpp *cpp, struct psi_cpp_macro_decl *decl)
 {
-       struct psi_cpp_macro_decl *old = zend_hash_str_find_ptr(cpp->defs, decl->token->text, decl->token->size);
+       struct psi_cpp_macro_decl *old = zend_hash_str_find_ptr(&cpp->defs, decl->token->text, decl->token->size);
 
        if (old && !psi_cpp_macro_decl_equal(old, decl)) {
                cpp->parser->error(PSI_DATA(cpp->parser), decl->token, PSI_WARNING,
@@ -350,12 +373,12 @@ void psi_cpp_define(struct psi_cpp *cpp, struct psi_cpp_macro_decl *decl)
                cpp->parser->error(PSI_DATA(cpp->parser), old->token, PSI_WARNING,
                                "'%s' previously defined", old->token->text);
        }
-       zend_hash_str_update_ptr(cpp->defs, decl->token->text, decl->token->size, decl);
+       zend_hash_str_update_ptr(&cpp->defs, decl->token->text, decl->token->size, decl);
 }
 
 bool psi_cpp_undef(struct psi_cpp *cpp, struct psi_token *tok)
 {
-       return SUCCESS == zend_hash_str_del(cpp->defs, tok->text, tok->size);
+       return SUCCESS == zend_hash_str_del(&cpp->defs, tok->text, tok->size);
 }
 
 bool psi_cpp_if(struct psi_cpp *cpp, struct psi_cpp_exp *exp)
@@ -363,7 +386,7 @@ bool psi_cpp_if(struct psi_cpp *cpp, struct psi_cpp_exp *exp)
        if (!psi_num_exp_validate(PSI_DATA(cpp->parser), exp->data.num, NULL, NULL, NULL, NULL, NULL)) {
                return false;
        }
-       if (!psi_long_num_exp(exp->data.num, NULL, cpp->defs)) {
+       if (!psi_long_num_exp(exp->data.num, NULL, &cpp->defs)) {
                return false;
        }
        return true;
@@ -383,38 +406,87 @@ static inline bool try_include(struct psi_cpp *cpp, const char *path, bool *pars
 
                tokens = psi_parser_scan(cpp->parser, include);
                if (tokens) {
-                       if ((*parsed = psi_cpp_process(cpp, &tokens))) {
-                               psi_cpp_tokiter_ins_range(cpp, psi_cpp_tokiter_index(cpp),
-                                               psi_plist_count(tokens), psi_plist_eles(tokens));
+                       *parsed = psi_cpp_process(cpp, &tokens);
+
+                       if (*parsed) {
+                               size_t num_tokens = psi_plist_count(tokens);
+
+                               ++cpp->expanded;
+                               psi_cpp_tokiter_ins_range(cpp, cpp->index,
+                                               num_tokens, psi_plist_eles(tokens));
+                               /* skip already processed tokens */
+                               cpp->index += num_tokens;
+                               free(tokens);
+                       } else {
+                               psi_plist_free(tokens);
                        }
-                       psi_plist_free(tokens);
                }
                free(include);
+
+               zend_hash_str_add_empty_element(&cpp->once, path, strlen(path));
                return true;
        }
        return false;
 }
 
-bool psi_cpp_include(struct psi_cpp *cpp, const char *file, unsigned flags)
+static inline void include_path(const struct psi_token *file, char **path)
+{
+       if (*file->text == '/') {
+               *path = file->text;
+       } else {
+               char *dir;
+               size_t len;
+
+               strncpy(*path, file->file, PATH_MAX);
+
+               dir = dirname(*path);
+               len = strlen(dir);
+
+               assert(len + file->size + 1 < PATH_MAX);
+
+               memmove(*path, dir, len);
+               (*path)[len] = '/';
+               memcpy(&(*path)[len + 1], file->text, file->size + 1);
+       }
+}
+
+bool psi_cpp_include(struct psi_cpp *cpp, const struct psi_token *file, unsigned flags)
 {
-       char path[PATH_MAX];
        bool parsed = false;
-       int f_len = strlen(file) - 2;
+       int f_len = strlen(file->text);
 
-       if (file[1] == '/' && PATH_MAX > snprintf(path, PATH_MAX, "%.*s", f_len, file + 1)) {
-               return try_include(cpp, path, &parsed) && parsed;
-       } else {
+       if (!(flags & PSI_CPP_INCLUDE_NEXT) || *file->text == '/') {
+               /* first try as is, full or relative path */
+               char temp[PATH_MAX], *path = temp;
+
+               include_path(file, &path);
+
+               if ((flags & PSI_CPP_INCLUDE_ONCE) && zend_hash_str_exists(&cpp->once, path, f_len)) {
+                       return true;
+               }
+               if (try_include(cpp, path, &parsed)) {
+                       /* found */
+                       return parsed;
+               }
+       }
+
+       /* look through search paths */
+       if (*file->text != '/') {
+               char path[PATH_MAX];
                const char *sep;
+               int p_len;
 
                if ((flags & PSI_CPP_INCLUDE_NEXT) && cpp->search) {
                        if ((sep = strchr(cpp->search, ':'))) {
                                cpp->search = sep + 1;
                        } else {
-                               cpp->search += strlen(cpp->search); /* point to end of string */
+                               /* point to end of string */
+                               cpp->search += strlen(cpp->search);
                        }
                }
+
                if (!(flags & PSI_CPP_INCLUDE_NEXT) || !cpp->search) {
-                       cpp->search = &psi_cpp_search[0];
+                       cpp->search = PSI_G(search_path);
                }
 
                do {
@@ -423,12 +495,18 @@ bool psi_cpp_include(struct psi_cpp *cpp, const char *file, unsigned flags)
                        sep = strchr(cpp->search, ':');
                        d_len = sep ? sep - cpp->search : strlen(cpp->search);
 
-                       if (PATH_MAX > snprintf(path, PATH_MAX, "%.*s/%.*s", d_len, cpp->search, f_len, file + 1)) {
+                       if (PATH_MAX > (p_len = snprintf(path, PATH_MAX, "%.*s/%.*s", d_len, cpp->search, f_len, file->text))) {
+                               if ((flags & PSI_CPP_INCLUDE_ONCE) && zend_hash_str_exists(&cpp->once, path, p_len)) {
+                                       return true;
+                               }
                                if (try_include(cpp, path, &parsed)) {
                                        break;
                                }
                        }
-                       cpp->search = sep + 1;
+
+                       if (sep) {
+                               cpp->search = sep + 1;
+                       }
                } while (sep);
        }