1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
|
/*
* (c) Thomas Pornin 1999 - 2002
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. 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.
* 4. The name of the authors may not be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT 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 AUTHORS 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 "tune.h"
#include <stdio.h>
#include <string.h>
#include <stddef.h>
#include <limits.h>
#include <time.h>
#include "ucppi.h"
#include "mem.h"
#include "nhash.h"
/*
* Assertion support. Each assertion is indexed by its predicate, and
* the list of 'questions' which yield a true answer.
*/
static HTT assertions;
static int assertions_init_done = 0;
static struct assert *new_assertion(void)
{
struct assert *a = getmem(sizeof(struct assert));
a->nbval = 0;
return a;
}
static void del_token_fifo(struct token_fifo *tf)
{
size_t i;
for (i = 0; i < tf->nt; i ++)
if (S_TOKEN(tf->t[i].type)) freemem(tf->t[i].name);
if (tf->nt) freemem(tf->t);
}
static void del_assertion(void *va)
{
struct assert *a = va;
size_t i;
for (i = 0; i < a->nbval; i ++) del_token_fifo(a->val + i);
if (a->nbval) freemem(a->val);
freemem(a);
}
/*
* print the contents of a token list
*/
static void print_token_fifo(struct token_fifo *tf)
{
size_t i;
for (i = 0; i < tf->nt; i ++)
if (ttMWS(tf->t[i].type)) fputc(' ', emit_output);
else fputs(token_name(tf->t + i), emit_output);
}
/*
* print all assertions related to a given name
*/
static void print_assert(void *va)
{
struct assert *a = va;
size_t i;
for (i = 0; i < a->nbval; i ++) {
fprintf(emit_output, "#assert %s(", HASH_ITEM_NAME(a));
print_token_fifo(a->val + i);
fprintf(emit_output, ")\n");
}
}
/*
* compare two token_fifo, return 0 if they are identical, 1 otherwise.
* All whitespace tokens are considered identical, but sequences of
* whitespace are not shrinked.
*/
int cmp_token_list(struct token_fifo *f1, struct token_fifo *f2)
{
size_t i;
if (f1->nt != f2->nt) return 1;
for (i = 0; i < f1->nt; i ++) {
if (ttMWS(f1->t[i].type) && ttMWS(f2->t[i].type)) continue;
if (f1->t[i].type != f2->t[i].type) return 1;
if (f1->t[i].type == MACROARG
&& f1->t[i].line != f2->t[i].line) return 1;
if (S_TOKEN(f1->t[i].type)
&& strcmp(f1->t[i].name, f2->t[i].name)) return 1;
}
return 0;
}
/*
* for #assert
* Assertions are not part of the ISO-C89 standard, but they are sometimes
* encountered, for instance in Solaris standard include files.
*/
int handle_assert(struct lexer_state *ls)
{
int ina = 0, ltww;
struct token t;
struct token_fifo *atl = 0;
struct assert *a;
char *aname;
int ret = -1;
long l = ls->line;
int nnp;
size_t i;
while (!next_token(ls)) {
if (ls->ctok->type == NEWLINE) break;
if (ttMWS(ls->ctok->type)) continue;
if (ls->ctok->type == NAME) {
if (!(a = HTT_get(&assertions, ls->ctok->name))) {
a = new_assertion();
aname = sdup(ls->ctok->name);
ina = 1;
}
goto handle_assert_next;
}
error(l, "illegal assertion name for #assert");
goto handle_assert_warp_ign;
}
goto handle_assert_trunc;
handle_assert_next:
while (!next_token(ls)) {
if (ls->ctok->type == NEWLINE) break;
if (ttMWS(ls->ctok->type)) continue;
if (ls->ctok->type != LPAR) {
error(l, "syntax error in #assert");
goto handle_assert_warp_ign;
}
goto handle_assert_next2;
}
goto handle_assert_trunc;
handle_assert_next2:
atl = getmem(sizeof(struct token_fifo));
atl->art = atl->nt = 0;
for (nnp = 1, ltww = 1; nnp && !next_token(ls);) {
if (ls->ctok->type == NEWLINE) break;
if (ltww && ttMWS(ls->ctok->type)) continue;
ltww = ttMWS(ls->ctok->type);
if (ls->ctok->type == LPAR) nnp ++;
else if (ls->ctok->type == RPAR) {
if (!(-- nnp)) goto handle_assert_next3;
}
t.type = ls->ctok->type;
if (S_TOKEN(t.type)) t.name = sdup(ls->ctok->name);
aol(atl->t, atl->nt, t, TOKEN_LIST_MEMG);
}
goto handle_assert_trunc;
handle_assert_next3:
while (!next_token(ls) && ls->ctok->type != NEWLINE) {
if (!ttWHI(ls->ctok->type) && (ls->flags & WARN_STANDARD)) {
warning(l, "trailing garbage in #assert");
}
}
if (atl->nt && ttMWS(atl->t[atl->nt - 1].type) && (-- atl->nt) == 0)
freemem(atl->t);
if (atl->nt == 0) {
error(l, "void assertion in #assert");
goto handle_assert_error;
}
for (i = 0; i < a->nbval && cmp_token_list(atl, a->val + i); i ++);
if (i != a->nbval) {
/* we already have it */
ret = 0;
goto handle_assert_error;
}
/* This is a new assertion. Let's keep it. */
aol(a->val, a->nbval, *atl, TOKEN_LIST_MEMG);
if (ina) {
HTT_put(&assertions, a, aname);
freemem(aname);
}
if (emit_assertions) {
fprintf(emit_output, "#assert %s(", HASH_ITEM_NAME(a));
print_token_fifo(atl);
fputs(")\n", emit_output);
}
freemem(atl);
return 0;
handle_assert_trunc:
error(l, "unfinished #assert");
handle_assert_error:
if (atl) {
del_token_fifo(atl);
freemem(atl);
}
if (ina) {
freemem(aname);
freemem(a);
}
return ret;
handle_assert_warp_ign:
while (!next_token(ls) && ls->ctok->type != NEWLINE);
if (ina) {
freemem(aname);
freemem(a);
}
return ret;
}
/*
* for #unassert
*/
int handle_unassert(struct lexer_state *ls)
{
int ltww;
struct token t;
struct token_fifo atl;
struct assert *a;
int ret = -1;
long l = ls->line;
int nnp;
size_t i;
atl.art = atl.nt = 0;
while (!next_token(ls)) {
if (ls->ctok->type == NEWLINE) break;
if (ttMWS(ls->ctok->type)) continue;
if (ls->ctok->type == NAME) {
if (!(a = HTT_get(&assertions, ls->ctok->name))) {
ret = 0;
goto handle_unassert_warp;
}
goto handle_unassert_next;
}
error(l, "illegal assertion name for #unassert");
goto handle_unassert_warp;
}
goto handle_unassert_trunc;
handle_unassert_next:
while (!next_token(ls)) {
if (ls->ctok->type == NEWLINE) break;
if (ttMWS(ls->ctok->type)) continue;
if (ls->ctok->type != LPAR) {
error(l, "syntax error in #unassert");
goto handle_unassert_warp;
}
goto handle_unassert_next2;
}
if (emit_assertions)
fprintf(emit_output, "#unassert %s\n", HASH_ITEM_NAME(a));
HTT_del(&assertions, HASH_ITEM_NAME(a));
return 0;
handle_unassert_next2:
for (nnp = 1, ltww = 1; nnp && !next_token(ls);) {
if (ls->ctok->type == NEWLINE) break;
if (ltww && ttMWS(ls->ctok->type)) continue;
ltww = ttMWS(ls->ctok->type);
if (ls->ctok->type == LPAR) nnp ++;
else if (ls->ctok->type == RPAR) {
if (!(-- nnp)) goto handle_unassert_next3;
}
t.type = ls->ctok->type;
if (S_TOKEN(t.type)) t.name = sdup(ls->ctok->name);
aol(atl.t, atl.nt, t, TOKEN_LIST_MEMG);
}
goto handle_unassert_trunc;
handle_unassert_next3:
while (!next_token(ls) && ls->ctok->type != NEWLINE) {
if (!ttWHI(ls->ctok->type) && (ls->flags & WARN_STANDARD)) {
warning(l, "trailing garbage in #unassert");
}
}
if (atl.nt && ttMWS(atl.t[atl.nt - 1].type) && (-- atl.nt) == 0)
freemem(atl.t);
if (atl.nt == 0) {
error(l, "void assertion in #unassert");
return ret;
}
for (i = 0; i < a->nbval && cmp_token_list(&atl, a->val + i); i ++);
if (i != a->nbval) {
/* we have it, undefine it */
del_token_fifo(a->val + i);
if (i < (a->nbval - 1))
mmvwo(a->val + i, a->val + i + 1, (a->nbval - i - 1)
* sizeof(struct token_fifo));
if ((-- a->nbval) == 0) freemem(a->val);
if (emit_assertions) {
fprintf(emit_output, "#unassert %s(",
HASH_ITEM_NAME(a));
print_token_fifo(&atl);
fputs(")\n", emit_output);
}
}
ret = 0;
goto handle_unassert_finish;
handle_unassert_trunc:
error(l, "unfinished #unassert");
handle_unassert_finish:
if (atl.nt) del_token_fifo(&atl);
return ret;
handle_unassert_warp:
while (!next_token(ls) && ls->ctok->type != NEWLINE);
return ret;
}
/*
* Add the given assertion (as string).
*/
int make_assertion(char *aval)
{
struct lexer_state lls;
size_t n = strlen(aval) + 1;
char *c = sdup(aval);
int ret;
*(c + n - 1) = '\n';
init_buf_lexer_state(&lls, 0);
lls.flags = DEFAULT_LEXER_FLAGS;
lls.input = 0;
lls.input_string = (unsigned char *)c;
lls.pbuf = 0;
lls.ebuf = n;
lls.line = -1;
ret = handle_assert(&lls);
freemem(c);
free_lexer_state(&lls);
return ret;
}
/*
* Remove the given assertion (as string).
*/
int destroy_assertion(char *aval)
{
struct lexer_state lls;
size_t n = strlen(aval) + 1;
char *c = sdup(aval);
int ret;
*(c + n - 1) = '\n';
init_buf_lexer_state(&lls, 0);
lls.flags = DEFAULT_LEXER_FLAGS;
lls.input = 0;
lls.input_string = (unsigned char *)c;
lls.pbuf = 0;
lls.ebuf = n;
lls.line = -1;
ret = handle_unassert(&lls);
freemem(c);
free_lexer_state(&lls);
return ret;
}
/*
* erase the assertion table
*/
void wipe_assertions(void)
{
if (assertions_init_done) HTT_kill(&assertions);
assertions_init_done = 0;
}
/*
* initialize the assertion table
*/
void init_assertions(void)
{
wipe_assertions();
HTT_init(&assertions, del_assertion);
assertions_init_done = 1;
}
/*
* retrieve an assertion from the hash table
*/
struct assert *get_assertion(char *name)
{
return HTT_get(&assertions, name);
}
/*
* print already defined assertions
*/
void print_assertions(void)
{
HTT_scan(&assertions, print_assert);
}
|