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 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660
|
/*
* sparse/dissect.c
*
* Started by Oleg Nesterov <oleg@redhat.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "dissect.h"
#define U_VOID 0x00
#define U_SELF ((1 << U_SHIFT) - 1)
#define U_MASK (U_R_VAL | U_W_VAL | U_R_AOF)
#define DO_LIST(l__, p__, expr__) \
do { \
typeof(l__->list[0]) p__; \
FOR_EACH_PTR(l__, p__) \
expr__; \
END_FOR_EACH_PTR(p__); \
} while (0)
#define DO_2_LIST(l1__,l2__, p1__,p2__, expr__) \
do { \
typeof(l1__->list[0]) p1__; \
typeof(l2__->list[0]) p2__; \
PREPARE_PTR_LIST(l1__, p1__); \
FOR_EACH_PTR(l2__, p2__) \
expr__; \
NEXT_PTR_LIST(p1__); \
END_FOR_EACH_PTR(p2__); \
FINISH_PTR_LIST(p1__); \
} while (0)
typedef unsigned usage_t;
struct symbol *dissect_ctx;
static struct reporter *reporter;
static void do_sym_list(struct symbol_list *list);
static struct symbol
*base_type(struct symbol *sym),
*do_initializer(struct symbol *type, struct expression *expr),
*do_expression(usage_t mode, struct expression *expr),
*do_statement(usage_t mode, struct statement *stmt);
static inline int is_ptr(struct symbol *type)
{
return type->type == SYM_PTR || type->type == SYM_ARRAY;
}
static inline usage_t u_rval(usage_t mode)
{
return mode & (U_R_VAL | (U_MASK << U_SHIFT))
? U_R_VAL : 0;
}
static inline usage_t u_addr(usage_t mode)
{
return mode = mode & U_MASK
? U_R_AOF | (mode & U_W_AOF) : 0;
}
static usage_t u_lval(struct symbol *type)
{
int wptr = is_ptr(type) && !(type->ctype.modifiers & MOD_CONST);
return wptr || type == &bad_ctype
? U_W_AOF | U_R_VAL : U_R_VAL;
}
static usage_t fix_mode(struct symbol *type, usage_t mode)
{
mode &= (U_SELF | (U_SELF << U_SHIFT));
switch (type->type) {
case SYM_BASETYPE:
if (!type->ctype.base_type)
break;
case SYM_ENUM:
case SYM_BITFIELD:
if (mode & U_MASK)
mode &= U_SELF;
default:
break; case SYM_FN:
if (mode & U_R_VAL)
mode |= U_R_AOF;
mode &= ~(U_R_VAL | U_W_AOF);
break; case SYM_ARRAY:
if (mode & (U_MASK << U_SHIFT))
mode >>= U_SHIFT;
else if (mode != U_W_VAL)
mode = u_addr(mode);
}
if (!(mode & U_R_AOF))
mode &= ~U_W_AOF;
return mode;
}
static struct symbol *report_member(usage_t mode, struct position *pos,
struct symbol *type, struct symbol *mem)
{
struct symbol *ret = mem->ctype.base_type;
if (mem->ident || mem->type == SYM_BAD)
reporter->r_member(fix_mode(ret, mode), pos, type, mem);
return ret;
}
static void report_implicit(usage_t mode, struct position *pos, struct symbol *type)
{
if (type->type != SYM_STRUCT && type->type != SYM_UNION)
return;
if (type->ident != NULL)
reporter->r_member(mode, pos, type, NULL);
DO_LIST(type->symbol_list, mem,
report_implicit(mode, pos, base_type(mem)));
}
static inline struct symbol *expr_symbol(struct expression *expr)
{
struct symbol *sym = expr->symbol;
if (!sym) {
sym = lookup_symbol(expr->symbol_name, NS_SYMBOL);
if (!sym) {
sym = alloc_symbol(expr->pos, SYM_BAD);
bind_symbol(sym, expr->symbol_name, NS_SYMBOL);
sym->kind = expr->op ?: 'v'; /* see EXPR_CALL */
}
}
if (!sym->ctype.base_type)
sym->ctype.base_type = &bad_ctype;
return sym;
}
static struct symbol *report_symbol(usage_t mode, struct expression *expr)
{
struct symbol *sym = expr_symbol(expr);
struct symbol *ret = base_type(sym);
if (0 && ret->type == SYM_ENUM)
return report_member(mode, &expr->pos, ret, expr->symbol);
reporter->r_symbol(fix_mode(ret, mode), &expr->pos, sym);
return ret;
}
static bool deanon(struct symbol *base, struct ident *node, struct symbol *parent)
{
struct ident *pi = parent ? parent->ident : NULL;
char name[256];
if (!node) {
base->ident = pi;
return false;
}
snprintf(name, sizeof(name), "%.*s:%.*s",
pi ? pi->len : 0, pi ? pi->name : NULL, node->len, node->name);
base->ident = built_in_ident(name);
return true;
}
static void report_memdef(struct symbol *sym, struct symbol *mem)
{
mem->kind = 'm';
if (sym && mem->ident)
reporter->r_memdef(sym, mem);
}
static void examine_sym_node(struct symbol *node, struct symbol *parent)
{
struct ident *name = node->ident;
struct symbol *base, *dctx;
if (node->visited)
return;
node->visited = 1;
node->kind = 'v';
while ((base = node->ctype.base_type) != NULL)
switch (base->type) {
case SYM_TYPEOF:
node->ctype.base_type =
do_expression(U_VOID, base->initializer);
break;
case SYM_ARRAY:
do_expression(U_R_VAL, base->array_size);
case SYM_PTR:
node = base;
break;
case SYM_FN:
node->kind = 'f';
node = base;
break;
case SYM_STRUCT: case SYM_UNION: //case SYM_ENUM:
if (base->inspected)
return;
base->inspected = 1;
base->kind = 's';
if (!base->symbol_list)
return;
dctx = dissect_ctx;
if (toplevel(base->scope))
dissect_ctx = NULL;
if (base->ident || deanon(base, name, parent))
reporter->r_symdef(base);
if (base->ident)
parent = base;
DO_LIST(base->symbol_list, mem,
examine_sym_node(mem, parent);
report_memdef(parent, mem));
dissect_ctx = dctx;
default:
return;
}
}
static struct symbol *base_type(struct symbol *sym)
{
if (!sym)
return &bad_ctype;
if (sym->type == SYM_NODE)
examine_sym_node(sym, NULL);
return sym->ctype.base_type // builtin_fn_type
?: &bad_ctype;
}
static struct symbol *__lookup_member(struct symbol *type, struct ident *name, int *p_addr)
{
struct symbol *node;
int addr = 0;
FOR_EACH_PTR(type->symbol_list, node)
if (!name) {
if (addr == *p_addr)
return node;
}
else if (node->ident == NULL) {
node = __lookup_member(node->ctype.base_type, name, NULL);
if (node)
goto found;
}
else if (node->ident == name) {
found:
if (p_addr)
*p_addr = addr;
return node;
}
addr++;
END_FOR_EACH_PTR(node);
return NULL;
}
static struct symbol *lookup_member(struct symbol *type, struct ident *name, int *addr)
{
struct symbol *mem = __lookup_member(type, name, addr);
if (!mem) {
static struct symbol bad_member = {
.type = SYM_BAD,
.ctype.base_type = &bad_ctype,
.kind = 'm',
};
if (!type->symbol_list)
type->scope = file_scope;
mem = &bad_member;
mem->ident = name;
}
return mem;
}
static struct expression *peek_preop(struct expression *expr, int op)
{
do {
if (expr->type != EXPR_PREOP)
break;
if (expr->op == op)
return expr->unop;
if (expr->op == '(')
expr = expr->unop;
else
break;
} while (expr);
return NULL;
}
static struct symbol *do_expression(usage_t mode, struct expression *expr)
{
struct symbol *ret = &int_ctype;
again:
if (expr) switch (expr->type) {
default:
warning(expr->pos, "bad expr->type: %d", expr->type);
case EXPR_TYPE: // [struct T]; Why ???
case EXPR_VALUE:
case EXPR_FVALUE:
break; case EXPR_LABEL:
ret = &label_ctype;
break; case EXPR_STRING:
ret = &string_ctype;
break; case EXPR_STATEMENT:
ret = do_statement(mode, expr->statement);
break; case EXPR_SIZEOF: case EXPR_ALIGNOF: case EXPR_PTRSIZEOF:
do_expression(U_VOID, expr->cast_expression);
break; case EXPR_COMMA:
do_expression(U_VOID, expr->left);
ret = do_expression(mode, expr->right);
break; case EXPR_CAST: case EXPR_FORCE_CAST: //case EXPR_IMPLIED_CAST:
ret = base_type(expr->cast_type);
do_initializer(ret, expr->cast_expression);
break; case EXPR_COMPARE: case EXPR_LOGICAL:
mode = u_rval(mode);
do_expression(mode, expr->left);
do_expression(mode, expr->right);
break; case EXPR_CONDITIONAL: //case EXPR_SELECT:
do_expression(expr->cond_true
? U_R_VAL : U_R_VAL | mode,
expr->conditional);
ret = do_expression(mode, expr->cond_true);
ret = do_expression(mode, expr->cond_false);
break; case EXPR_CALL:
if (expr->fn->type == EXPR_SYMBOL)
expr->fn->op = 'f'; /* for expr_symbol() */
ret = do_expression(U_R_PTR, expr->fn);
if (is_ptr(ret))
ret = ret->ctype.base_type;
DO_2_LIST(ret->arguments, expr->args, arg, val,
do_expression(u_lval(base_type(arg)), val));
ret = ret->type == SYM_FN ? base_type(ret)
: &bad_ctype;
break; case EXPR_ASSIGNMENT:
mode |= U_W_VAL | U_R_VAL;
if (expr->op == '=')
mode &= ~U_R_VAL;
ret = do_expression(mode, expr->left);
report_implicit(mode, &expr->pos, ret);
mode = expr->op == '='
? u_lval(ret) : U_R_VAL;
do_expression(mode, expr->right);
break; case EXPR_BINOP: {
struct symbol *l, *r;
mode |= u_rval(mode);
l = do_expression(mode, expr->left);
r = do_expression(mode, expr->right);
if (expr->op != '+' && expr->op != '-')
;
else if (!is_ptr_type(r))
ret = l;
else if (!is_ptr_type(l))
ret = r;
}
break; case EXPR_PREOP: case EXPR_POSTOP: {
struct expression *unop = expr->unop;
switch (expr->op) {
case SPECIAL_INCREMENT:
case SPECIAL_DECREMENT:
mode |= U_W_VAL | U_R_VAL;
default:
mode |= u_rval(mode);
case '(':
ret = do_expression(mode, unop);
break; case '&':
if ((expr = peek_preop(unop, '*')))
goto again;
ret = alloc_symbol(unop->pos, SYM_PTR);
ret->ctype.base_type =
do_expression(u_addr(mode), unop);
break; case '*':
if ((expr = peek_preop(unop, '&')))
goto again;
if (mode & (U_MASK << U_SHIFT))
mode |= U_R_VAL;
mode <<= U_SHIFT;
if (mode & (U_R_AOF << U_SHIFT))
mode |= U_R_VAL;
if (mode & (U_W_VAL << U_SHIFT))
mode |= U_W_AOF;
ret = do_expression(mode, unop);
ret = is_ptr(ret) ? base_type(ret)
: &bad_ctype;
}
}
break; case EXPR_DEREF: {
struct symbol *p_type;
usage_t p_mode;
p_mode = mode & U_SELF;
if (!(mode & U_MASK) && (mode & (U_MASK << U_SHIFT)))
p_mode = U_R_VAL;
p_type = do_expression(p_mode, expr->deref);
ret = report_member(mode, &expr->pos, p_type,
lookup_member(p_type, expr->member, NULL));
}
break; case EXPR_OFFSETOF: {
struct symbol *in = base_type(expr->in);
do {
if (expr->op == '.') {
in = report_member(U_VOID, &expr->pos, in,
lookup_member(in, expr->ident, NULL));
} else {
do_expression(U_R_VAL, expr->index);
in = in->ctype.base_type;
}
} while ((expr = expr->down));
}
break; case EXPR_GENERIC: {
struct type_expression *map;
do_expression(U_VOID, expr->control);
for (map = expr->map; map; map = map->next)
ret = do_expression(mode, map->expr);
if (expr->def)
ret = do_expression(mode, expr->def);
}
break; case EXPR_SYMBOL:
ret = report_symbol(mode, expr);
}
return ret;
}
static void do_asm_xputs(usage_t mode, struct asm_operand_list *xputs)
{
DO_LIST(xputs, op, do_expression(U_W_AOF | mode, op->expr));
}
static struct symbol *do_statement(usage_t mode, struct statement *stmt)
{
struct symbol *ret = &void_ctype;
if (stmt) switch (stmt->type) {
default:
warning(stmt->pos, "bad stmt->type: %d", stmt->type);
case STMT_NONE:
case STMT_RANGE:
case STMT_CONTEXT:
break; case STMT_DECLARATION:
do_sym_list(stmt->declaration);
break; case STMT_EXPRESSION:
ret = do_expression(mode, stmt->expression);
break; case STMT_RETURN: {
struct symbol *type = dissect_ctx->ctype.base_type;
do_expression(u_lval(base_type(type)), stmt->expression);
}
break; case STMT_ASM:
do_expression(U_R_VAL, stmt->asm_string);
do_asm_xputs(U_W_VAL, stmt->asm_outputs);
do_asm_xputs(U_R_VAL, stmt->asm_inputs);
break; case STMT_COMPOUND: {
int count;
count = statement_list_size(stmt->stmts);
DO_LIST(stmt->stmts, st,
ret = do_statement(--count ? U_VOID : mode, st));
}
break; case STMT_ITERATOR:
do_sym_list(stmt->iterator_syms);
do_statement(U_VOID, stmt->iterator_pre_statement);
do_expression(U_R_VAL, stmt->iterator_pre_condition);
do_statement(U_VOID, stmt->iterator_post_statement);
do_statement(U_VOID, stmt->iterator_statement);
do_expression(U_R_VAL, stmt->iterator_post_condition);
break; case STMT_IF:
do_expression(U_R_VAL, stmt->if_conditional);
do_statement(U_VOID, stmt->if_true);
do_statement(U_VOID, stmt->if_false);
break; case STMT_SWITCH:
do_expression(U_R_VAL, stmt->switch_expression);
do_statement(U_VOID, stmt->switch_statement);
break; case STMT_CASE:
do_expression(U_R_VAL, stmt->case_expression);
do_expression(U_R_VAL, stmt->case_to);
do_statement(U_VOID, stmt->case_statement);
break; case STMT_GOTO:
do_expression(U_R_PTR, stmt->goto_expression);
break; case STMT_LABEL:
do_statement(mode, stmt->label_statement);
}
return ret;
}
static struct symbol *do_initializer(struct symbol *type, struct expression *expr)
{
struct symbol *m_type;
struct expression *m_expr;
int m_addr;
if (expr) switch (expr->type) {
default:
do_expression(u_lval(type), expr);
break; case EXPR_INDEX:
do_initializer(base_type(type), expr->idx_expression);
break; case EXPR_INITIALIZER:
m_addr = 0;
FOR_EACH_PTR(expr->expr_list, m_expr) {
if (type->type == SYM_ARRAY) {
m_type = base_type(type);
if (m_expr->type == EXPR_INDEX)
m_expr = m_expr->idx_expression;
} else {
int *m_atop = &m_addr;
m_type = type;
while (m_expr->type == EXPR_IDENTIFIER) {
m_type = report_member(U_W_VAL, &m_expr->pos, m_type,
lookup_member(m_type, m_expr->expr_ident, m_atop));
m_expr = m_expr->ident_expression;
m_atop = NULL;
}
if (m_atop) {
m_type = report_member(U_W_VAL, &m_expr->pos, m_type,
lookup_member(m_type, NULL, m_atop));
}
if (m_expr->type != EXPR_INITIALIZER)
report_implicit(U_W_VAL, &m_expr->pos, m_type);
}
do_initializer(m_type, m_expr);
m_addr++;
} END_FOR_EACH_PTR(m_expr);
}
return type;
}
static inline struct symbol *do_symbol(struct symbol *sym)
{
struct symbol *type = base_type(sym);
struct symbol *dctx = dissect_ctx;
struct statement *stmt;
reporter->r_symdef(sym);
switch (type->type) {
default:
if (!sym->initializer)
break;
reporter->r_symbol(U_W_VAL, &sym->pos, sym);
if (!dctx)
dissect_ctx = sym;
do_initializer(type, sym->initializer);
dissect_ctx = dctx;
break; case SYM_FN:
stmt = sym->ctype.modifiers & MOD_INLINE
? type->inline_stmt : type->stmt;
if (!stmt)
break;
if (dctx)
sparse_error(dctx->pos, "dissect_ctx change %s -> %s",
show_ident(dctx->ident), show_ident(sym->ident));
dissect_ctx = sym;
do_sym_list(type->arguments);
do_statement(U_VOID, stmt);
dissect_ctx = dctx;
}
return type;
}
static void do_sym_list(struct symbol_list *list)
{
DO_LIST(list, sym, do_symbol(sym));
}
void dissect(struct reporter *rep, struct string_list *filelist)
{
reporter = rep;
DO_LIST(filelist, file, do_sym_list(__sparse(file)));
}
|