first draft of a simple cpp scanner
[m6w6/ext-psi] / src / token.c
index a9675f8eaed50e34c9bd25623f61e9459c0fd92b..01c45f10e0b1932e5a5bbcdf5920c290c0fe6fab 100644 (file)
@@ -1,16 +1,30 @@
-#ifdef HAVE_CONFIG_H
-# include "config.h"
-#else
-# include "php_config.h"
-#endif
-
-#include <stdlib.h>
-#include <stdio.h>
-#include <string.h>
-#include <inttypes.h>
-#include <sys/param.h>
-
-#include "parser_proc.h"
+/*******************************************************************************
+ Copyright (c) 2016, Michael Wallner <mike@php.net>.
+ All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without
+ modification, are permitted provided that the following conditions are met:
+
+     * Redistributions of source code must retain the above copyright notice,
+       this list of conditions and the following disclaimer.
+     * Redistributions in binary form must reproduce the above copyright
+       notice, this list of conditions and the following disclaimer in the
+       documentation and/or other materials provided with the distribution.
+
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+*******************************************************************************/
+
+#include "php_psi_stdinc.h"
+
 #include "token.h"
 #include "parser.h"
 
@@ -29,7 +43,7 @@ struct psi_token *psi_token_alloc(struct psi_parser *P) {
 
        token_typ = P->num;
        token_len = P->cur - P->tok;
-       fname_len = strlen(P->psi.file.fn);
+       fname_len = strlen(P->file.fn);
 
        T = calloc(1, psi_token_alloc_size(token_len, fname_len));
        T->type = token_typ;
@@ -40,7 +54,7 @@ struct psi_token *psi_token_alloc(struct psi_parser *P) {
        T->col = P->col;
 
        memcpy(T->text, P->tok, token_len);
-       memcpy(T->file, P->psi.file.fn, fname_len);
+       memcpy(T->file, P->file.fn, fname_len);
 
        return T;
 }
@@ -68,8 +82,16 @@ struct psi_token *psi_token_cat(unsigned argc, ...) {
 
                if (T) {
                        size_t token_len = T->size, fname_len = strlen(T->file);
+                       struct psi_token *tmp = realloc(T, psi_token_alloc_size(T->size += arg->size + 1, fname_len));
+
+                       if (tmp) {
+                               T = tmp;
+                       } else {
+                               free(T);
+                               va_end(argv);
+                               return NULL;
+                       }
 
-                       T = realloc(T, psi_token_alloc_size(T->size += arg->size + 1, fname_len));
                        T->text = &T->buf[0];
                        T->file = &T->buf[T->size + 1];
                        T->buf[token_len] = ' ';
@@ -114,29 +136,29 @@ struct psi_token *psi_token_translit(struct psi_token *T, char *from, char *to)
 
 static inline uint64_t psi_hash(char *digest_buf, ...)
 {
-    uint64_t hash = 5381;
-    uint8_t c;
-    const uint8_t *ptr;
-    va_list argv;
+       uint64_t hash = 5381;
+       uint8_t c;
+       const uint8_t *ptr;
+       va_list argv;
 
-    va_start(argv, digest_buf);
-    while ((ptr = va_arg(argv, const uint8_t *))) {
+       va_start(argv, digest_buf);
+       while ((ptr = va_arg(argv, const uint8_t *))) {
                while ((c = *ptr++)) {
                        hash = ((hash << 5) + hash) + c;
                }
-    }
-    va_end(argv);
+       }
+       va_end(argv);
 
-    if (digest_buf) {
-       sprintf(digest_buf, "%" PRIx64, hash);
-    }
+       if (digest_buf) {
+               sprintf(digest_buf, "%" PRIx64, hash);
+       }
 
-    return hash;
+       return hash;
 }
 
 uint64_t psi_token_hash(struct psi_token *t, char *digest_buf) {
        char loc_buf[48];
 
        sprintf(loc_buf, "%u%u", t->line, t->col);
-       return psi_hash(digest_buf, t->file, loc_buf, NULL);
+       return psi_hash(digest_buf, t->file, loc_buf, (char *) NULL);
 }