1 /* $NetBSD: lint1.h,v 1.238 2026/02/03 20:41:38 rillig Exp $ */ 2 3 /* 4 * Copyright (c) 1996 Christopher G. Demetriou. All Rights Reserved. 5 * Copyright (c) 1994, 1995 Jochen Pohl 6 * All Rights Reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by Jochen Pohl for 19 * The NetBSD Project. 20 * 4. The name of the author may not be used to endorse or promote products 21 * derived from this software without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 32 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include "lint.h" 36 #include "op.h" 37 38 /* 39 * A memory pool collects allocated objects that must be available until: 40 * - the end of a block, 41 * - the end of an expression, or 42 * - the end of the translation unit. 43 */ 44 typedef struct memory_pool { 45 struct memory_pool_item { 46 void *p; 47 #ifdef DEBUG_MEM 48 size_t size; 49 const char *descr; 50 #endif 51 } *items; 52 size_t len; 53 size_t cap; 54 } memory_pool; 55 56 /* See saved_lwarn in cgram.y. */ 57 #define LWARN_ALL (-2) 58 #define LWARN_NONE (-1) 59 60 /* 61 * Describes the position of a declaration or anything else. 62 * 63 * FIXME: Just a single file:lineno pair is not enough to accurately describe 64 * the position of a symbol. The whole inclusion path at that point must be 65 * stored as well. This makes a difference for symbols from included 66 * headers, see print_stack_trace. 67 */ 68 typedef struct { 69 const char *p_file; 70 int p_line; 71 int p_uniq; /* uniquifier */ 72 } pos_t; 73 74 typedef struct { 75 bool tq_const; 76 bool tq_restrict; 77 bool tq_volatile; 78 bool tq_atomic; 79 } type_qualifiers; 80 81 typedef struct { 82 bool used; 83 bool noreturn; 84 unsigned bit_width; 85 } type_attributes; 86 87 /* A bool, integer or floating-point value. */ 88 typedef struct { 89 tspec_t v_tspec; 90 /* 91 * Set if an integer constant is unsigned only in C90 and later, but 92 * not in traditional C. 93 * 94 * See the operators table in oper.c, columns "l r". 95 */ 96 bool v_unsigned_since_c90; 97 bool v_char_constant; 98 union { 99 int64_t integer; 100 long double floating; 101 } u; 102 } val_t; 103 104 typedef struct sym sym_t; 105 106 /* 107 * Structures of type struct_or_union uniquely identify structures. This can't 108 * be done in structures of type type_t, because these are copied if they must 109 * be modified. So it would not be possible to check if two structures are 110 * identical by comparing the pointers to the type structures. 111 * 112 * If the structure has no tag name, its first typedef name is used to identify 113 * the structure in lint2. 114 */ 115 typedef struct { 116 unsigned int sou_size_in_bits; 117 unsigned int sou_align; 118 bool sou_incomplete:1; 119 sym_t *sou_first_member; 120 sym_t *sou_tag; 121 sym_t *sou_first_typedef; 122 } struct_or_union; 123 124 /* The same as in struct_or_union, only for enums. */ 125 typedef struct { 126 bool en_incomplete:1; 127 sym_t *en_first_enumerator; 128 sym_t *en_tag; 129 sym_t *en_first_typedef; 130 } enumeration; 131 132 /* The type of an expression, object or function. */ 133 struct lint1_type { 134 tspec_t t_tspec; /* type specifier */ 135 bool t_incomplete_array:1; 136 bool t_const:1; 137 bool t_volatile:1; 138 bool t_proto:1; /* function prototype (u.params valid) */ 139 bool t_vararg:1; /* prototype with '...' */ 140 bool t_noreturn:1; /* function never returns normally */ 141 bool t_typedef:1; /* type defined with typedef */ 142 bool t_typeof:1; /* type defined with GCC's __typeof__ */ 143 bool t_bitfield:1; 144 /* 145 * Either the type is currently an enum (having t_tspec ENUM), or it 146 * is an integer type (typically INT) that has been implicitly 147 * converted from an enum type. In both cases, u.enumer is valid. 148 */ 149 bool t_is_enum:1; 150 bool t_packed:1; 151 union { 152 int dimension; /* if ARRAY */ 153 struct_or_union *sou; 154 enumeration *enumer; 155 sym_t *params; /* if t_proto */ 156 } u; 157 unsigned int t_bit_field_width:8; 158 unsigned int t_bit_field_offset:24; 159 struct lint1_type *t_subt; /*- element type (if ARRAY), 160 * return value (if FUNC), 161 * target type (if PTR) */ 162 }; 163 164 typedef enum { 165 SK_VCFT, /* variable, constant, function, type */ 166 SK_MEMBER, /* member of a struct or union */ 167 SK_TAG, 168 SK_LABEL 169 } symbol_kind; 170 171 /* storage classes and related things */ 172 typedef enum { 173 NO_SCL, 174 EXTERN, /* external symbols (independent of decl_t) */ 175 STATIC, /* static symbols (local and global) */ 176 AUTO, /* automatic symbols (except register) */ 177 REG, /* register */ 178 TYPEDEF, 179 THREAD_LOCAL, 180 STRUCT_TAG, 181 UNION_TAG, 182 ENUM_TAG, 183 STRUCT_MEMBER, 184 UNION_MEMBER, 185 BOOL_CONST, 186 ENUM_CONST, 187 ABSTRACT, /* abstract symbol (sizeof, casts, unnamed 188 * argument) */ 189 } scl_t; 190 191 /* C23 6.7.4 */ 192 typedef enum { 193 FS_INLINE, /* since C99 */ 194 FS_NORETURN, /* since C11 */ 195 } function_specifier; 196 197 typedef enum { 198 NC_FALSE, /* since C23 */ 199 NC_TRUE, /* since C23 */ 200 NC_NULLPTR, /* since C23 */ 201 } named_constant; 202 203 /* A type, variable, keyword; basically anything that has a name. */ 204 struct sym { 205 const char *s_name; 206 const char *s_rename; /* renamed symbol's given name */ 207 pos_t s_def_pos; /* position of last (prototype) definition, 208 * prototype declaration, no-prototype-def., 209 * tentative definition or declaration, in this 210 * order */ 211 pos_t s_set_pos; /* position of first initialization */ 212 pos_t s_use_pos; /* position of first use */ 213 symbol_kind s_kind; 214 const struct keyword *s_keyword; 215 bool s_bitfield:1; 216 bool s_set:1; 217 bool s_used:1; 218 bool s_param:1; 219 bool s_register:1; 220 bool s_defparam:1; /* undefined symbol in old-style function 221 * definition */ 222 bool s_return_type_implicit_int:1; 223 bool s_osdef:1; /* symbol stems from old-style function def. */ 224 bool s_inline:1; 225 sym_t *s_ext_sym; /* for locally declared external symbols, the 226 * pointer to the external symbol with the same 227 * name */ 228 def_t s_def; /* declared, tentative defined, defined */ 229 scl_t s_scl; 230 int s_block_level; /* level of declaration, -1 if not in symbol 231 * table */ 232 type_t *s_type; 233 union { 234 bool s_bool_constant; 235 int s_enum_constant; 236 struct { 237 struct_or_union *sm_containing_type; 238 unsigned int sm_offset_in_bits; 239 } s_member; 240 struct { 241 int sk_token; 242 union { 243 /* if T_TYPE or T_STRUCT_OR_UNION */ 244 tspec_t sk_tspec; 245 /* if T_QUAL */ 246 type_qualifiers sk_type_qualifier; 247 /* if T_FUNCTION_SPECIFIER */ 248 function_specifier function_specifier; 249 /* if T_CON */ 250 named_constant named_constant; 251 } u; 252 } s_keyword; 253 sym_t *s_old_style_params; /* parameters in an old-style 254 * function definition */ 255 /* 256 * Only for string initializers with automatic size. 257 * Does not take '\0' characters in the middle into account. 258 */ 259 size_t s_array_nonnull_dimension; 260 } u; 261 sym_t *s_symtab_next; /* next symbol in the same symtab bucket */ 262 sym_t **s_symtab_ref; /* pointer to s_symtab_next of the previous 263 * symbol */ 264 sym_t *s_next; /* next struct/union member, enumerator, 265 * parameter */ 266 sym_t *s_level_next; /* next symbol declared on the same level */ 267 }; 268 269 /* 270 * Used to keep some information about symbols before they are entered 271 * into the symbol table. 272 */ 273 typedef struct { 274 const char *sb_name; /* name of symbol */ 275 size_t sb_len; /* length (without '\0') */ 276 sym_t *sb_sym; /* symbol table entry */ 277 } sbuf_t; 278 279 280 typedef struct { 281 struct tnode *func; 282 struct tnode **args; 283 size_t args_len; 284 size_t args_cap; 285 } function_call; 286 287 typedef struct tnode tnode_t; 288 289 /* An expression, forming an abstract syntax tree. */ 290 struct tnode { 291 op_t tn_op; 292 type_t *tn_type; 293 bool tn_lvalue:1; 294 bool tn_cast:1; /* if tn_op == CVT, it's an explicit cast */ 295 bool tn_parenthesized:1; 296 bool tn_sys:1; /* comes from a system header */ 297 bool tn_system_dependent:1; /* depends on sizeof or offsetof */ 298 union { 299 struct { 300 tnode_t *left; /* (left) operand */ 301 tnode_t *right; /* right operand */ 302 } ops; 303 sym_t *sym; /* if NAME */ 304 val_t value; /* if CON */ 305 buffer *str_literals; /* if STRING; for 306 * character strings, 'data' points to 307 * the concatenated string literals in 308 * source form, and 'len' is the 309 * length of the concatenation; for 310 * wide strings, 'data' is NULL and 311 * 'len' is the number of resulting 312 * characters */ 313 function_call *call; /* if CALL */ 314 } u; 315 }; 316 317 struct generic_association { 318 type_t *ga_arg; /* NULL means default or error */ 319 tnode_t *ga_result; /* NULL means error */ 320 struct generic_association *ga_prev; 321 }; 322 323 typedef struct { 324 bool has_dim; 325 int dim; 326 } array_size; 327 328 typedef enum decl_level_kind { 329 DLK_EXTERN, /* global types, variables or functions */ 330 DLK_STRUCT, /* struct members */ 331 DLK_UNION, /* union members */ 332 DLK_ENUM, /* enum constants */ 333 DLK_OLD_STYLE_PARAMS, /* parameters in an old-style function 334 * definition */ 335 DLK_PROTO_PARAMS, /* parameters in a prototype function 336 * definition */ 337 DLK_AUTO, /* local types or variables */ 338 DLK_ABSTRACT /* abstract (unnamed) declaration; type name; 339 * used in casts and sizeof */ 340 } decl_level_kind; 341 342 /* 343 * A declaration level collects information for a declarator in a struct, 344 * union or enum declaration, a parameter declaration list, or a plain 345 * declaration in or outside a function body. 346 * 347 * For nested declarations, the global 'dcs' holds all information needed for 348 * the current level, the outer levels are available via 'd_enclosing'. 349 */ 350 typedef struct decl_level { 351 decl_level_kind d_kind; 352 tspec_t d_abstract_type;/* VOID, BOOL, CHAR, INT or COMPLEX */ 353 tspec_t d_complex_mod; /* FLOAT or DOUBLE */ 354 tspec_t d_sign_mod; /* SIGNED or UNSIGN */ 355 tspec_t d_rank_mod; /* SHORT, LONG or LLONG */ 356 scl_t d_scl; /* storage class */ 357 type_t *d_type; /* after dcs_end_type, the pointer to the type 358 * used for all declarators */ 359 sym_t *d_redeclared_symbol; 360 unsigned int d_sou_size_in_bits; /* size of the structure or 361 * union being built, without 362 * trailing padding */ 363 unsigned int d_sou_align; /* alignment of the structure 364 * or union being built */ 365 unsigned int d_mem_align; /* alignment of the structure 366 * or union member */ 367 type_qualifiers d_qual; /* in declaration specifiers */ 368 bool d_inline:1; /* inline in declaration specifiers */ 369 bool d_multiple_storage_classes:1; /* reported in dcs_end_type */ 370 bool d_invalid_type_combination:1; 371 bool d_nonempty_decl:1; /* in a function declaration, whether at 372 * least one tag was declared */ 373 bool d_no_type_specifier:1; 374 bool d_asm:1; /* set if d_ctx == AUTO and asm() present */ 375 bool d_packed:1; 376 bool d_used:1; 377 bool d_noreturn:1; /* function never returns normally */ 378 type_t *d_tag_type; /* during a member or enumerator declaration, 379 * the tag type to which the member belongs */ 380 sym_t *d_func_params; /* during a function declaration, the 381 * parameters, stored in the enclosing level */ 382 pos_t d_func_def_pos; /* position of the function definition */ 383 sym_t *d_first_dlsym; /* first symbol declared at this level */ 384 sym_t **d_last_dlsym; /* points to s_level_next in the last symbol 385 declaration at this level */ 386 sym_t *d_func_proto_syms; /* symbols defined in prototype, such 387 * as tagged types or parameter names, 388 * may overlap d_func_params */ 389 bool d_finished; /* the declaration-specifiers are complete, 390 * the declarator is being parsed */ 391 struct decl_level *d_enclosing; /* the enclosing declaration level */ 392 } decl_level; 393 394 typedef struct { 395 sym_t *first; 396 bool vararg:1; 397 bool prototype:1; 398 bool used:1; 399 bool noreturn:1; 400 bool identifier:1; 401 } parameter_list; 402 403 /* 404 * A sequence of asterisks and qualifiers, from right to left. For example, 405 * 'const ***volatile **const volatile' results in [c-v-, ----, --v-, ----, 406 * ----]. The leftmost 'const' is not included in this list, it is stored in 407 * dcs->d_qual instead. 408 */ 409 typedef struct qual_ptr { 410 type_qualifiers qualifiers; 411 struct qual_ptr *p_next; 412 } qual_ptr; 413 414 /* The values of the 'case' labels. */ 415 typedef struct { 416 val_t *vals; 417 size_t len; 418 size_t cap; 419 } case_labels; 420 421 typedef enum { 422 CS_DO_WHILE, 423 CS_FOR, 424 CS_FUNCTION_BODY, 425 CS_IF, 426 CS_SWITCH, 427 CS_WHILE 428 } control_statement_kind; 429 430 /* 431 * Used to keep information about nested control statements. 432 */ 433 typedef struct control_statement { 434 control_statement_kind c_kind; /* to ensure proper nesting */ 435 bool c_loop:1; /* 'continue' and 'break' are valid */ 436 bool c_switch:1; /* 'case' and 'break' are valid */ 437 bool c_break:1; /* the loop/switch has a reachable 'break' 438 * statement */ 439 bool c_continue:1; /* the loop has a reachable 'continue' 440 * statement */ 441 bool c_default:1; /* the switch has a 'default' label */ 442 bool c_maybe_endless:1; /* the controlling expression is 443 * always true (as in 'for (;;)' or 444 * 'while (1)'), there may be break 445 * statements though */ 446 bool c_always_then:1; 447 bool c_reached_end_of_then:1; 448 bool c_had_return_noval:1; /* had "return;" */ 449 bool c_had_return_value:1; /* had "return expr;" */ 450 451 type_t *c_switch_type; /* type of switch expression */ 452 tnode_t *c_switch_expr; 453 case_labels c_case_labels; /* list of case values */ 454 455 memory_pool c_for_expr3_mem; /* saved memory for end of loop 456 * expression in for() */ 457 tnode_t *c_for_expr3; /* end of loop expr in for() */ 458 pos_t c_for_expr3_pos; /* position of end of loop expr */ 459 pos_t c_for_expr3_csrc_pos; /* same for csrc_pos */ 460 461 struct control_statement *c_surrounding; 462 } control_statement; 463 464 typedef struct { 465 size_t lo; /* inclusive */ 466 size_t hi; /* inclusive */ 467 } range_t; 468 469 typedef enum designator_kind { 470 DK_MEMBER, /* .member */ 471 DK_SUBSCRIPT, /* [subscript] */ 472 DK_SCALAR /* no textual representation, not generated by 473 * the parser; used for scalar initializer 474 * expressions surrounded by braces */ 475 } designator_kind; 476 477 /* 478 * A single component on the path from the "current object" of a brace level 479 * to the sub-object that is initialized by an expression. 480 * 481 * C99 6.7.8p6, 6.7.8p7 482 */ 483 typedef struct designator { 484 designator_kind dr_kind; 485 const sym_t *dr_member; /* for DK_MEMBER */ 486 size_t dr_subscript; /* for DK_SUBSCRIPT */ 487 bool dr_done; 488 } designator; 489 490 /* 491 * The path from the "current object" of a brace level to the sub-object that 492 * is initialized by an expression. Examples of designations are '.member' 493 * or '.member[123].member.member[1][1]'. 494 * 495 * C99 6.7.8p6, 6.7.8p7 496 */ 497 typedef struct designation { 498 designator *dn_items; 499 size_t dn_len; 500 size_t dn_cap; 501 } designation; 502 503 typedef enum { 504 LC_ARGSUSED, 505 LC_BITFIELDTYPE, 506 LC_FALLTHROUGH, 507 LC_LINTLIBRARY, 508 LC_LINTED, 509 LC_LONGLONG, 510 LC_NOTREACHED, 511 LC_PRINTFLIKE, 512 LC_PROTOLIB, 513 LC_SCANFLIKE, 514 LC_VARARGS, 515 } lint_comment; 516 517 typedef struct { 518 size_t start; 519 size_t end; 520 uint64_t value; 521 bool escaped; /* \n, \003, \x24 */ 522 bool named_escape; /* \a, \n, etc. */ 523 bool literal_escape; /* \?, \\, etc. */ 524 uint8_t octal_digits; /* 1 to 3; 0 means not applicable */ 525 uint8_t hex_digits; /* 1 to 3; 0 means not applicable */ 526 bool next_literal; /* when a new string literal begins */ 527 bool invalid_escape; /* single-character escape, recoverable */ 528 bool overflow; /* for octal and hex escapes */ 529 bool missing_hex_digits; 530 bool unescaped_newline; /* stops iterating */ 531 } quoted_iterator; 532 533 typedef enum { 534 TK_IDENTIFIER, 535 TK_CONSTANT, 536 TK_STRING_LITERALS, 537 TK_PUNCTUATOR, 538 } token_kind; 539 540 typedef struct token { 541 token_kind kind; 542 union { 543 const char *identifier; 544 val_t constant; 545 buffer string_literals; 546 const char *punctuator; 547 } u; 548 } token; 549 550 typedef struct balanced_token_sequence balanced_token_sequence; 551 typedef struct balanced_token balanced_token; 552 553 struct balanced_token_sequence { 554 balanced_token *tokens; 555 size_t len; 556 size_t cap; 557 }; 558 559 struct balanced_token { 560 char kind; // '\0', '(', '[', '{' 561 union { 562 token token; 563 balanced_token_sequence tokens; 564 } u; 565 }; 566 567 typedef struct { 568 const char *prefix; 569 const char *name; 570 balanced_token_sequence *arg; 571 } attribute; 572 573 typedef struct { 574 attribute *attrs; 575 size_t len; 576 size_t cap; 577 } attribute_list; 578 579 #include "externs1.h" 580 581 #define lint_assert(cond) \ 582 do { \ 583 if (!(cond)) \ 584 assert_failed(__FILE__, __LINE__, __func__, #cond); \ 585 } while (false) 586 587 #ifdef DEBUG 588 # include "err-msgs.h" 589 590 /* ARGSUSED */ 591 static inline void __printflike(1, 2) 592 check_printf(const char *fmt, ...) 593 { 594 } 595 596 # define wrap_check_printf_at(func, msgid, pos, args...) \ 597 do { \ 598 check_printf(__CONCAT(MSG_, msgid), ##args); \ 599 (func)(msgid, pos, ##args); \ 600 } while (false) 601 602 # define error_at(msgid, pos, args...) \ 603 wrap_check_printf_at(error_at, msgid, pos, ##args) 604 # define warning_at(msgid, pos, args...) \ 605 wrap_check_printf_at(warning_at, msgid, pos, ##args) 606 # define message_at(msgid, pos, args...) \ 607 wrap_check_printf_at(message_at, msgid, pos, ##args) 608 609 # define wrap_check_printf(func, cond, msgid, args...) \ 610 ({ \ 611 if (cond) \ 612 debug_step("%s:%d: %s %d '%s' in %s", \ 613 __FILE__, __LINE__, #func, msgid, \ 614 __CONCAT(MSG_, msgid), __func__); \ 615 check_printf(__CONCAT(MSG_, msgid), ##args); \ 616 (func)(msgid, ##args); \ 617 /* LINTED 129 */ \ 618 }) 619 620 # define error(msgid, args...) wrap_check_printf(error, \ 621 true, msgid, ##args) 622 # define warning(msgid, args...) wrap_check_printf(warning, \ 623 true, msgid, ##args) 624 # define gnuism(msgid, args...) wrap_check_printf(gnuism, \ 625 !allow_gcc || (!allow_trad && !allow_c99), msgid, ##args) 626 # define c99ism(msgid, args...) wrap_check_printf(c99ism, \ 627 !allow_c99 && (!allow_gcc || !allow_trad), msgid, ##args) 628 # define c11ism(msgid, args...) wrap_check_printf(c11ism, \ 629 !allow_c11 && !allow_gcc, msgid, ##args) 630 # define c23ism(msgid, args...) wrap_check_printf(c23ism, \ 631 !allow_c23, msgid, ##args) 632 #endif 633 634 #ifdef DEBUG 635 # define query_message(query_id, args...) \ 636 do { \ 637 debug_step("%s:%d: query %d '%s' in %s", \ 638 __FILE__, __LINE__, \ 639 query_id, __CONCAT(MSG_Q, query_id), __func__); \ 640 check_printf(__CONCAT(MSG_Q, query_id), ##args); \ 641 (query_message)(query_id, ##args); \ 642 } while (false) 643 #else 644 # define query_message(...) \ 645 do { \ 646 if (any_query_enabled) \ 647 (query_message)(__VA_ARGS__); \ 648 } while (false) 649 #endif 650 651 /* Copies curr_pos, keeping things unique. */ 652 static inline pos_t 653 unique_curr_pos(void) 654 { 655 pos_t curr = curr_pos; 656 curr_pos.p_uniq++; 657 if (curr_pos.p_file == csrc_pos.p_file) 658 csrc_pos.p_uniq++; 659 return curr; 660 } 661 662 static inline bool 663 is_nonzero_val(const val_t *val) 664 { 665 return is_floating(val->v_tspec) 666 ? val->u.floating != 0.0 667 : val->u.integer != 0; 668 } 669 670 static inline bool 671 constant_is_nonzero(const tnode_t *tn) 672 { 673 lint_assert(tn->tn_op == CON); 674 lint_assert(tn->tn_type->t_tspec == tn->u.value.v_tspec); 675 return is_nonzero_val(&tn->u.value); 676 } 677 678 static inline bool 679 is_zero(const tnode_t *tn) 680 { 681 return tn != NULL && tn->tn_op == CON && !is_nonzero_val(&tn->u.value); 682 } 683 684 static inline bool 685 is_nonzero(const tnode_t *tn) 686 { 687 return tn != NULL && tn->tn_op == CON && is_nonzero_val(&tn->u.value); 688 } 689 690 static inline const char * 691 op_name(op_t op) 692 { 693 return modtab[op].m_name; 694 } 695 696 static inline bool 697 is_binary(const tnode_t *tn) 698 { 699 return modtab[tn->tn_op].m_binary; 700 } 701 702 static inline uint64_t 703 bit(unsigned i) 704 { 705 /* 706 * TODO: Add proper support for INT128. This involves changing val_t to 707 * 128 bits. 708 */ 709 if (i >= 64) 710 return 0; /* XXX: not correct for INT128 and UINT128 */ 711 712 lint_assert(i < 64); 713 return (uint64_t)1 << i; 714 } 715 716 static inline bool 717 msb(int64_t si, tspec_t t) 718 { 719 return ((uint64_t)si & bit(size_in_bits(t) - 1)) != 0; 720 } 721 722 static inline uint64_t 723 value_bits(unsigned bitsize) 724 { 725 lint_assert(bitsize > 0); 726 727 /* for long double (80 or 128), double _Complex (128) */ 728 /* 729 * XXX: double _Complex does not have 128 bits of precision, therefore 730 * it should never be necessary to query the value bits of such a type; 731 * see d_c99_complex_split.c to trigger this case. 732 */ 733 if (bitsize >= 64) 734 return ~(uint64_t)0; 735 736 return ~(~(uint64_t)0 << bitsize); 737 } 738 739 /* C99 6.7.8p7 */ 740 static inline bool 741 is_struct_or_union(tspec_t t) 742 { 743 return t == STRUCT || t == UNION; 744 } 745 746 static inline bool 747 is_member(const sym_t *sym) 748 { 749 return sym->s_scl == STRUCT_MEMBER || sym->s_scl == UNION_MEMBER; 750 } 751 752 static inline void 753 set_sym_kind(symbol_kind kind) 754 { 755 if (yflag) 756 debug_step("%s: %s -> %s", __func__, 757 symbol_kind_name(sym_kind), symbol_kind_name(kind)); 758 sym_kind = kind; 759 } 760 761 static inline type_attributes 762 merge_type_attributes(type_attributes a, type_attributes b) 763 { 764 return (type_attributes){ 765 .used = a.used || b.used, 766 .noreturn = a.noreturn || b.noreturn, 767 .bit_width = b.bit_width > 0 ? b.bit_width : a.bit_width, 768 }; 769 } 770 771 static inline type_attributes 772 no_type_attributes(void) 773 { 774 return (type_attributes){ .used = false }; 775 } 776