cpp: fix dumping strings and chars
[m6w6/ext-psi] / src / types / cpp_macro_decl.c
1 /*******************************************************************************
2 Copyright (c) 2017, Michael Wallner <mike@php.net>.
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in the
12 documentation and/or other materials provided with the distribution.
13
14 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
18 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
20 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
21 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 *******************************************************************************/
25
26 #include "php_psi_stdinc.h"
27
28 #include "cpp.h"
29 #include "data.h"
30
31 struct psi_cpp_macro_decl *psi_cpp_macro_decl_init(struct psi_plist *sig,
32 struct psi_plist *tokens, struct psi_num_exp *exp)
33 {
34 struct psi_cpp_macro_decl *macro = calloc(1, sizeof(*macro));
35 macro->exp = exp;
36 macro->sig = sig;
37 macro->tokens = tokens;
38 return macro;
39 }
40
41 void psi_cpp_macro_decl_free(struct psi_cpp_macro_decl **macro_ptr)
42 {
43 if (*macro_ptr) {
44 struct psi_cpp_macro_decl *macro = *macro_ptr;
45
46 *macro_ptr = NULL;
47 if (macro->token) {
48 free(macro->token);
49 }
50 if (macro->exp) {
51 psi_num_exp_free(&macro->exp);
52 }
53 if (macro->sig) {
54 psi_plist_free(macro->sig);
55 }
56 if (macro->tokens) {
57 psi_plist_free(macro->tokens);
58 }
59 free(macro);
60 }
61 }
62
63 void psi_cpp_macro_decl_dump(int fd, struct psi_cpp_macro_decl *macro)
64 {
65 dprintf(fd, "%s", macro->token->text);
66
67 if (macro->sig) {
68 size_t i = 0;
69 struct psi_token *tok;
70
71 dprintf(fd, "(");
72 while (psi_plist_get(macro->sig, i++, &tok)) {
73 dprintf(fd, "%s%s", i>1?",":"", tok->text);
74 }
75 dprintf(fd, ")");
76 }
77
78 if (macro->exp) {
79 dprintf(fd, " ");
80 psi_num_exp_dump(fd, macro->exp);
81 } else if (macro->tokens) {
82 size_t i = 0;
83 struct psi_token *tok;
84
85 while (psi_plist_get(macro->tokens, i++, &tok)) {
86 switch (tok->type) {
87 case PSI_T_QUOTED_STRING:
88 dprintf(fd, " \"%s\"", tok->text);
89 break;
90 case PSI_T_QUOTED_CHAR:
91 dprintf(fd, " '%s'", tok->text);
92 break;
93 default:
94 dprintf(fd, " %s", tok->text);
95 }
96 }
97 }
98 }
99
100 static inline bool cmp_token_list(struct psi_plist *l1, struct psi_plist *l2)
101 {
102 size_t c = psi_plist_count(l1), i;
103
104 if (c != psi_plist_count(l2)) {
105 return false;
106 }
107
108 for (i = 0; i < c; ++i) {
109 struct psi_token *t1, *t2;
110
111 psi_plist_get(l1, i, &t1);
112 psi_plist_get(l2, i, &t2);
113
114 if (strcmp(t1->text, t2->text)) {
115 return false;
116 }
117 }
118
119 return true;
120 }
121
122 bool psi_cpp_macro_decl_equal(struct psi_cpp_macro_decl *d1, struct psi_cpp_macro_decl *d2)
123 {
124 if (d1->sig) {
125 if (!d2->sig) {
126 return false;
127 }
128
129 if (!cmp_token_list(d1->sig, d2->sig)) {
130 return false;
131 }
132 }
133
134 if (d1->tokens) {
135 if (!d2->tokens) {
136 return false;
137 }
138
139 if (!cmp_token_list(d1->tokens, d2->tokens)) {
140 return false;
141 }
142 }
143
144 /* FIXME compare num_exps */
145
146 return true;
147 }