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
|
// RUN: %clang_cc1 -fsyntax-only -verify %s
// RUN: %clang_cc1 -fexperimental-late-parse-attributes -fsyntax-only -verify %s
#define __counted_by(f) __attribute__((counted_by(f)))
// =============================================================================
// # Struct incomplete type with attribute in the decl position
// =============================================================================
// Note: This could be considered misleading. The typedef isn't actually on this
// line. Also note the discrepancy in diagnostic count (27 vs 51) is due to
// the pointer arithmetic on incomplete pointee type diagnostic always using
// diagnostic text that refers to the underlying forward decl, even when the
// typedef is used.
// expected-note@+3 27{{consider providing a complete definition for 'Incomplete_t' (aka 'struct IncompleteTy')}}
// The 'forward declaration' notes come from 'arithmetic on a pointer to an incomplete type' errors
// expected-note@+1 24{{forward declaration of 'struct IncompleteTy'}}
struct IncompleteTy; // expected-note 27{{consider providing a complete definition for 'struct IncompleteTy'}}
typedef struct IncompleteTy Incomplete_t;
struct CBBufDeclPos {
int count;
struct IncompleteTy* buf __counted_by(count); // OK expected-note 27{{consider using '__sized_by' instead of '__counted_by'}}
Incomplete_t* buf_typedef __counted_by(count); // OK expected-note 27{{consider using '__sized_by' instead of '__counted_by'}}
};
void consume_struct_IncompleteTy(struct IncompleteTy* buf);
int idx(void);
void test_CBBufDeclPos(struct CBBufDeclPos* ptr) {
// ===========================================================================
// ## Local variable initialization
// ===========================================================================
struct CBBufDeclPos explicit_desig_init = {
.count = 0,
// expected-error@+1{{cannot initialize 'CBBufDeclPos::buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
.buf = 0x0,
// expected-error@+1{{cannot initialize 'CBBufDeclPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
.buf_typedef = 0x0
};
// Variable is not currently marked as invalid so uses of the variable allows
// diagnostics to fire.
// expected-error@+1{{cannot assign to 'CBBufDeclPos::buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
explicit_desig_init.buf = 0x0;
// expected-error@+1{{cannot assign to 'CBBufDeclPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
explicit_desig_init.buf_typedef = 0x0;
// expected-error@+1{{cannot use 'explicit_desig_init.buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
void *tmp = explicit_desig_init.buf;
// expected-error@+1{{cannot use 'explicit_desig_init.buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
void *tmp2 = explicit_desig_init.buf_typedef;
struct CBBufDeclPos partial_explicit_desig_init = {
.count = 0,
// .buf and .buf_typedef are implicit zero initialized
// expected-error@+2{{cannot implicitly initialize 'CBBufDeclPos::buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
// expected-error@+1{{cannot implicitly initialize 'CBBufDeclPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
};
struct CBBufDeclPos implicit_full_init = {
0
// expected-error@+2{{cannot implicitly initialize 'CBBufDeclPos::buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
// expected-error@+1{{cannot implicitly initialize 'CBBufDeclPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
};
// Variable is not currently marked as invalid so uses of the variable allows
// diagnostics to fire.
// expected-error@+1{{cannot assign to 'CBBufDeclPos::buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
implicit_full_init.buf = 0x0;
// expected-error@+1{{cannot assign to 'CBBufDeclPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
implicit_full_init.buf_typedef = 0x0;
// expected-error@+1{{cannot use 'implicit_full_init.buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
void* tmp3 = implicit_full_init.buf;
// expected-error@+1{{cannot use 'implicit_full_init.buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
void* tmp4 = implicit_full_init.buf_typedef;
struct CBBufDeclPos explicit_non_desig_init = {
0,
// expected-error@+1{{cannot initialize 'CBBufDeclPos::buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
0x0,
// expected-error@+1{{cannot initialize 'CBBufDeclPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
0x0
};
// ===========================================================================
// ## Assignment to fields
// ===========================================================================
struct CBBufDeclPos uninit;
uninit.count = 0;
// expected-error@+1{{cannot assign to 'CBBufDeclPos::buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
uninit.buf = 0x0;
// expected-error@+1{{cannot assign to 'CBBufDeclPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
uninit.buf_typedef = 0x0;
ptr->count = 0;
// expected-error@+1{{cannot assign to 'CBBufDeclPos::buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
ptr->buf = 0x0;
// expected-error@+1{{cannot assign to 'CBBufDeclPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
ptr->buf_typedef = 0x0;
// ===========================================================================
// ## Make sure modifying the fields through other assignment operators is not
// allowed
// ===========================================================================
uninit.buf++; // expected-error{{arithmetic on a pointer to an incomplete type 'struct IncompleteTy'}}
++uninit.buf; // expected-error{{arithmetic on a pointer to an incomplete type 'struct IncompleteTy'}}
uninit.buf += 1; // expected-error{{arithmetic on a pointer to an incomplete type 'struct IncompleteTy'}}
uninit.buf_typedef++; // // expected-error{{arithmetic on a pointer to an incomplete type 'Incomplete_t' (aka 'struct IncompleteTy')}}
++uninit.buf_typedef; // expected-error{{arithmetic on a pointer to an incomplete type 'Incomplete_t' (aka 'struct IncompleteTy')}}
uninit.buf_typedef -= 1; // expected-error{{arithmetic on a pointer to an incomplete type 'Incomplete_t' (aka 'struct IncompleteTy')}}
uninit.buf--; // expected-error{{arithmetic on a pointer to an incomplete type 'struct IncompleteTy'}}
--uninit.buf; // expected-error{{arithmetic on a pointer to an incomplete type 'struct IncompleteTy'}}
uninit.buf -= 1; // expected-error{{arithmetic on a pointer to an incomplete type 'struct IncompleteTy'}}
uninit.buf_typedef--; // expected-error{{arithmetic on a pointer to an incomplete type 'Incomplete_t' (aka 'struct IncompleteTy')}}
--uninit.buf_typedef; // expected-error{{arithmetic on a pointer to an incomplete type 'Incomplete_t' (aka 'struct IncompleteTy')}}
uninit.buf_typedef -= 1; // expected-error{{arithmetic on a pointer to an incomplete type 'Incomplete_t' (aka 'struct IncompleteTy')}}
ptr->buf++; // expected-error{{arithmetic on a pointer to an incomplete type 'struct IncompleteTy'}}
++ptr->buf; // expected-error{{arithmetic on a pointer to an incomplete type 'struct IncompleteTy'}}
ptr->buf += 1; // expected-error{{arithmetic on a pointer to an incomplete type 'struct IncompleteTy'}}
ptr->buf--; // expected-error{{arithmetic on a pointer to an incomplete type 'struct IncompleteTy'}}
--ptr->buf; // expected-error{{arithmetic on a pointer to an incomplete type 'struct IncompleteTy'}}
ptr->buf -= 1; // expected-error{{arithmetic on a pointer to an incomplete type 'struct IncompleteTy'}}
ptr->buf_typedef++; // expected-error{{arithmetic on a pointer to an incomplete type 'Incomplete_t' (aka 'struct IncompleteTy')}}
++ptr->buf_typedef; // expected-error{{arithmetic on a pointer to an incomplete type 'Incomplete_t' (aka 'struct IncompleteTy')}}
ptr->buf_typedef += 1; // expected-error{{arithmetic on a pointer to an incomplete type 'Incomplete_t' (aka 'struct IncompleteTy')}}
ptr->buf_typedef--; // expected-error{{arithmetic on a pointer to an incomplete type 'Incomplete_t' (aka 'struct IncompleteTy')}}
--ptr->buf_typedef; // expected-error{{arithmetic on a pointer to an incomplete type 'Incomplete_t' (aka 'struct IncompleteTy')}}
ptr->buf_typedef -= 1; // expected-error{{arithmetic on a pointer to an incomplete type 'Incomplete_t' (aka 'struct IncompleteTy')}}
// ===========================================================================
// ## Use of fields in expressions
// ===========================================================================
// expected-error@+2{{cannot use 'uninit.buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
void* addr =
((char*) uninit.buf ) + 1;
// expected-error@+2{{cannot use 'uninit.buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
void* addr_typedef =
((char*) uninit.buf_typedef ) + 1;
// expected-error@+2{{cannot use 'ptr->buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
void* addr_ptr =
((char*) ptr->buf ) + 1;
// expected-error@+2{{cannot use 'ptr->buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
void* addr_ptr_typedef =
((char*) ptr->buf_typedef ) + 1;
// ===========================================================================
// ## Take address of fields
// ===========================================================================
// TODO: This should be forbidden, not because of the incomplete pointee type
// but because in the -fbounds-safety language model the address of a
// `counted_by` pointer cannot be taken to avoid it being possible to modify
// the `counted_by` pointer through another pointer. Whether or not this
// should be forbidden when `-fbounds-safety` is off is TBD.
//
// The incomplete pointee type isn't actually a problem here for
// `-fbounds-safety` because taking the address of a pointer returns a pointer
// that have the bounds of a single `void*`, so bounds checks on the resulting
// pointer don't need to know `sizeof(struct IncompleteTy)` but instead
// `sizeof(struct IncompleteTy* buf __counted_by(count))` which is just the
// size of a pointer.
void* take_addr = &uninit.buf;
void* take_addr_typedef = &uninit.buf_typedef;
void* take_addr_ptr = &ptr->buf;
void* take_addr_ptr_typedef = &ptr->buf_typedef;
// expected-error@+1{{cannot use 'uninit.buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
struct IncompleteTy* addr_elt_zero = &uninit.buf[0];
// expected-error@+1{{cannot use 'uninit.buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
struct IncompleteTy* addr_elt_idx = &uninit.buf[idx()];
// expected-error@+1{{cannot use 'uninit.buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
struct IncompleteTy* addr_elt_zero_typedef = &uninit.buf_typedef[0];
// expected-error@+1{{cannot use 'uninit.buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
struct IncompleteTy* addr_elt_idx_typedef = &uninit.buf_typedef[idx()];
// expected-error@+1{{cannot use 'ptr->buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
struct IncompleteTy* addr_elt_zero_ptr = &ptr->buf[0];
// expected-error@+1{{cannot use 'ptr->buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
struct IncompleteTy* addr_elt_idx_ptr = &ptr->buf[idx()];
// expected-error@+1{{cannot use 'ptr->buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
struct IncompleteTy* addr_elt_zero_ptr_typedef = &ptr->buf_typedef[0];
// expected-error@+1{{cannot use 'ptr->buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
struct IncompleteTy* addr_elt_idx_ptr_typedef = &ptr->buf_typedef[idx()];
// ===========================================================================
// ## Use fields as call arguments
// ===========================================================================
// expected-error@+1{{cannot use 'uninit.buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
consume_struct_IncompleteTy(uninit.buf);
// expected-error@+1{{cannot use 'uninit.buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
consume_struct_IncompleteTy(uninit.buf_typedef);
// expected-error@+1{{cannot use 'ptr->buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
consume_struct_IncompleteTy(ptr->buf);
// expected-error@+1{{cannot use 'ptr->buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
consume_struct_IncompleteTy(ptr->buf_typedef);
// ===========================================================================
// ## Use [] operator on fields
// ===========================================================================
// expected-error@+1 2{{cannot use 'uninit.buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
uninit.buf[0] = uninit.buf[1];
// expected-error@+1 2{{cannot use 'uninit.buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
uninit.buf_typedef[0] = uninit.buf_typedef[1];
// expected-error@+1 2{{cannot use 'ptr->buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
ptr->buf[0] = ptr->buf[1];
// expected-error@+1 2{{cannot use 'ptr->buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
ptr->buf_typedef[0] = ptr->buf_typedef[1];
}
// =============================================================================
// ## Global initialization
// =============================================================================
struct CBBufDeclPos global_explicit_desig_init = {
.count = 0,
// expected-error@+1{{cannot initialize 'CBBufDeclPos::buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
.buf = 0x0,
// expected-error@+1{{cannot initialize 'CBBufDeclPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
.buf_typedef = 0x0
};
void use_global_explicit_desig_init(void) {
// Variable isn't marked as invalid so diagnostics still fire
// expected-error@+1{{cannot assign to 'CBBufDeclPos::buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
global_explicit_desig_init.buf = 0x0;
// expected-error@+1{{cannot assign to 'CBBufDeclPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
global_explicit_desig_init.buf_typedef = 0x0;
}
struct CBBufDeclPos global_partial_explicit_desig_init = {
.count = 0,
// .buf and .buf_typedef are implicit zero initialized
// expected-error@+2{{cannot implicitly initialize 'CBBufDeclPos::buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
// expected-error@+1{{cannot implicitly initialize 'CBBufDeclPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
};
struct CBBufDeclPos global_implicit_full_init = {
0
// expected-error@+2{{cannot implicitly initialize 'CBBufDeclPos::buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
// expected-error@+1{{cannot implicitly initialize 'CBBufDeclPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
};
struct CBBufDeclPos global_explicit_non_desig_init = {
0,
// expected-error@+1{{cannot initialize 'CBBufDeclPos::buf' with '__counted_by' attributed type 'struct IncompleteTy * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'struct IncompleteTy' is incomplete}}
0x0,
// expected-error@+1{{cannot initialize 'CBBufDeclPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_t * __counted_by(count)' (aka 'struct IncompleteTy *') because the pointee type 'Incomplete_t' (aka 'struct IncompleteTy') is incomplete}}
0x0
};
extern struct CBBufDeclPos global_declaration; // OK
// TODO: These tentative definitions are implicitly empty initialized to zero.
// This should generate an error diagnostic but currently doesn't. There should
// be a carve out to allow `__counted_by(0)` which is the only constant count
// version of the attribute where it is valid to assign NULL.
struct CBBufDeclPos global_tentative_defn;
static struct CBBufDeclPos global_tentative_defn2;
// =============================================================================
// ## Completing the definition of the type allows use of CBBufDeclPos fields
// =============================================================================
struct IncompleteTy {
int field;
};
void test_CBBufDeclPos_completed(struct CBBufDeclPos* ptr) {
// Initialization is ok
struct CBBufDeclPos explicit_desig_init = {
.count = 0,
.buf = 0x0,
.buf_typedef = 0x0
};
struct CBBufDeclPos partial_explicit_desig_init = {
.count = 0,
// .buf and .buf_typedef are implicit zero initialized
};
struct CBBufDeclPos implicit_full_init = {0};
struct CBBufDeclPos explicit_non_desig_init = {
0,
0x0,
0x0
};
// Assignment to fields is ok
ptr->buf = 0x0;
ptr->buf_typedef = 0x0;
// Use of fields in expressions is ok
void* tmp = ptr->buf;
void* tmp2 = ptr->buf_typedef;
// Take address of fields is ok
void* take_addr_ptr = &ptr->buf;
void* take_addr_ptr_typedef = &ptr->buf_typedef;
struct IncompleteTy* addr_elt_zero_ptr = &ptr->buf[0];
struct IncompleteTy* addr_elt_idx_ptr = &ptr->buf[idx()];
struct IncompleteTy* addr_elt_zero_ptr_typedef = &ptr->buf_typedef[0];
struct IncompleteTy* addr_elt_idx_ptr_typedef = &ptr->buf_typedef[idx()];
// As call arguments is ok
consume_struct_IncompleteTy(ptr->buf);
consume_struct_IncompleteTy(ptr->buf_typedef);
// In [] operator is ok
ptr->buf[0] = ptr->buf[1];
ptr->buf_typedef[0] = ptr->buf_typedef[1];
}
// Global initialization is ok
struct CBBufDeclPos global_explicit_desig_init_completed = {
.count = 0,
.buf = 0x0,
.buf_typedef = 0x0
};
struct CBBufDeclPos global_partial_explicit_desig_init_completed = {
.count = 0,
// .buf and .buf_typedef are implicit zero initialized
};
struct CBBufDeclPos global_implicit_full_init_completed = {0};
struct CBBufDeclPos global_explicit_non_desig_init_completed = {
0,
0x0,
0x0
};
extern struct CBBufDeclPos global_declaration;
struct CBBufDeclPos global_tentative_defn;
static struct CBBufDeclPos global_tentative_defn2;
// =============================================================================
// # Struct incomplete type with attribute in the pointer position
// =============================================================================
// expected-note@+1 8{{consider providing a complete definition for 'Incomplete_ty2' (aka 'struct IncompleteTy2')}}
struct IncompleteTy2; // expected-note 8{{consider providing a complete definition for 'struct IncompleteTy2'}}
typedef struct IncompleteTy2 Incomplete_ty2;
void consume_struct_IncompleteTy2(struct IncompleteTy2* buf);
struct CBBufTyPos {
int count;
struct IncompleteTy2* __counted_by(count) buf ; // OK expected-note 8{{consider using '__sized_by' instead of '__counted_by'}}
Incomplete_ty2 *__counted_by(count) buf_typedef; // OK expected-note 8{{consider using '__sized_by' instead of '__counted_by'}}
};
void use_CBBufTyPos(struct CBBufTyPos* ptr) {
struct CBBufTyPos explicit_desig_init = {
.count = 0,
// expected-error@+1{{cannot initialize 'CBBufTyPos::buf' with '__counted_by' attributed type 'struct IncompleteTy2 * __counted_by(count)' (aka 'struct IncompleteTy2 *') because the pointee type 'struct IncompleteTy2' is incomplete}}
.buf = 0x0,
// expected-error@+1{{cannot initialize 'CBBufTyPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_ty2 * __counted_by(count)' (aka 'struct IncompleteTy2 *') because the pointee type 'Incomplete_ty2' (aka 'struct IncompleteTy2') is incomplete}}
.buf_typedef = 0x0
};
// Assignment
// expected-error@+1{{cannot assign to 'CBBufTyPos::buf' with '__counted_by' attributed type 'struct IncompleteTy2 * __counted_by(count)' (aka 'struct IncompleteTy2 *') because the pointee type 'struct IncompleteTy2' is incomplete}}
explicit_desig_init.buf = 0x0;
// expected-error@+1{{cannot assign to 'CBBufTyPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_ty2 * __counted_by(count)' (aka 'struct IncompleteTy2 *') because the pointee type 'Incomplete_ty2' (aka 'struct IncompleteTy2') is incomplete}}
explicit_desig_init.buf_typedef = 0x0;
// expected-error@+1{{cannot assign to 'CBBufTyPos::buf' with '__counted_by' attributed type 'struct IncompleteTy2 * __counted_by(count)' (aka 'struct IncompleteTy2 *') because the pointee type 'struct IncompleteTy2' is incomplete}}
ptr->buf = 0x0;
// expected-error@+1{{cannot assign to 'CBBufTyPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_ty2 * __counted_by(count)' (aka 'struct IncompleteTy2 *') because the pointee type 'Incomplete_ty2' (aka 'struct IncompleteTy2') is incomplete}}
ptr->buf_typedef = 0x0;
// Use
// expected-error@+2{{cannot use 'ptr->buf' with '__counted_by' attributed type 'struct IncompleteTy2 * __counted_by(count)' (aka 'struct IncompleteTy2 *') because the pointee type 'struct IncompleteTy2' is incomplete}}
void* addr =
((char*) ptr->buf ) + 1;
// expected-error@+2{{cannot use 'ptr->buf_typedef' with '__counted_by' attributed type 'Incomplete_ty2 * __counted_by(count)' (aka 'struct IncompleteTy2 *') because the pointee type 'Incomplete_ty2' (aka 'struct IncompleteTy2') is incomplete}}
void* addr_typedef =
((char*) ptr->buf_typedef ) + 1;
// expected-error@+1{{cannot use 'ptr->buf' with '__counted_by' attributed type 'struct IncompleteTy2 * __counted_by(count)' (aka 'struct IncompleteTy2 *') because the pointee type 'struct IncompleteTy2' is incomplete}}
consume_struct_IncompleteTy2(ptr->buf);
// expected-error@+1{{cannot use 'ptr->buf_typedef' with '__counted_by' attributed type 'Incomplete_ty2 * __counted_by(count)' (aka 'struct IncompleteTy2 *') because the pointee type 'Incomplete_ty2' (aka 'struct IncompleteTy2') is incomplete}}
consume_struct_IncompleteTy2(ptr->buf_typedef);
// expected-error@+1 2{{cannot use 'ptr->buf' with '__counted_by' attributed type 'struct IncompleteTy2 * __counted_by(count)' (aka 'struct IncompleteTy2 *') because the pointee type 'struct IncompleteTy2' is incomplete}}
ptr->buf[0] = ptr->buf[1];
// expected-error@+1 2{{cannot use 'ptr->buf_typedef' with '__counted_by' attributed type 'Incomplete_ty2 * __counted_by(count)' (aka 'struct IncompleteTy2 *') because the pointee type 'Incomplete_ty2' (aka 'struct IncompleteTy2') is incomplete}}
ptr->buf_typedef[0] = ptr->buf_typedef[1];
}
struct CBBufTyPos global_explicit_desig_init_struct_type_pos = {
.count = 0,
// expected-error@+1{{cannot initialize 'CBBufTyPos::buf' with '__counted_by' attributed type 'struct IncompleteTy2 * __counted_by(count)' (aka 'struct IncompleteTy2 *') because the pointee type 'struct IncompleteTy2' is incomplete}}
.buf = 0x0,
// expected-error@+1{{cannot initialize 'CBBufTyPos::buf_typedef' with '__counted_by' attributed type 'Incomplete_ty2 * __counted_by(count)' (aka 'struct IncompleteTy2 *') because the pointee type 'Incomplete_ty2' (aka 'struct IncompleteTy2') is incomplete}}
.buf_typedef = 0x0
};
// Defining the type makes `CBBufTyPos` fields usable
struct IncompleteTy2 {
int field;
};
void use_CBBufTyPos_completed(struct CBBufTyPos* ptr) {
ptr->buf = 0x0;
ptr->buf_typedef = 0x0;
void* addr = ((char*) ptr->buf) + 1;
void* addr_typedef = ((char*) ptr->buf_typedef) + 1;
}
// =============================================================================
// # union incomplete type
// =============================================================================
// expected-note@+1 8{{consider providing a complete definition for 'IncompleteUnion_ty' (aka 'union IncompleteUnionTy')}}
union IncompleteUnionTy; // expected-note 8{{consider providing a complete definition for 'union IncompleteUnionTy'}}
typedef union IncompleteUnionTy IncompleteUnion_ty;
void consume_struct_IncompleteUnionTy(union IncompleteUnionTy* buf);
struct CBBufUnionTyPos {
int count;
union IncompleteUnionTy* __counted_by(count) buf ; // OK expected-note 8{{consider using '__sized_by' instead of '__counted_by'}}
IncompleteUnion_ty *__counted_by(count) buf_typedef; // OK expected-note 8{{consider using '__sized_by' instead of '__counted_by'}}
};
void use_CBBufUnionTyPos(struct CBBufUnionTyPos* ptr) {
struct CBBufUnionTyPos explicit_desig_init = {
.count = 0,
// expected-error@+1{{cannot initialize 'CBBufUnionTyPos::buf' with '__counted_by' attributed type 'union IncompleteUnionTy * __counted_by(count)' (aka 'union IncompleteUnionTy *') because the pointee type 'union IncompleteUnionTy' is incomplete}}
.buf = 0x0,
// expected-error@+1{{cannot initialize 'CBBufUnionTyPos::buf_typedef' with '__counted_by' attributed type 'IncompleteUnion_ty * __counted_by(count)' (aka 'union IncompleteUnionTy *') because the pointee type 'IncompleteUnion_ty' (aka 'union IncompleteUnionTy') is incomplete}}
.buf_typedef = 0x0
};
// Assignment
// expected-error@+1{{cannot assign to 'CBBufUnionTyPos::buf' with '__counted_by' attributed type 'union IncompleteUnionTy * __counted_by(count)' (aka 'union IncompleteUnionTy *') because the pointee type 'union IncompleteUnionTy' is incomplete}}
explicit_desig_init.buf = 0x0;
// expected-error@+1{{cannot assign to 'CBBufUnionTyPos::buf_typedef' with '__counted_by' attributed type 'IncompleteUnion_ty * __counted_by(count)' (aka 'union IncompleteUnionTy *') because the pointee type 'IncompleteUnion_ty' (aka 'union IncompleteUnionTy') is incomplete}}
explicit_desig_init.buf_typedef = 0x0;
// expected-error@+1{{cannot assign to 'CBBufUnionTyPos::buf' with '__counted_by' attributed type 'union IncompleteUnionTy * __counted_by(count)' (aka 'union IncompleteUnionTy *') because the pointee type 'union IncompleteUnionTy' is incomplete}}
ptr->buf = 0x0;
// expected-error@+1{{cannot assign to 'CBBufUnionTyPos::buf_typedef' with '__counted_by' attributed type 'IncompleteUnion_ty * __counted_by(count)' (aka 'union IncompleteUnionTy *') because the pointee type 'IncompleteUnion_ty' (aka 'union IncompleteUnionTy') is incomplete}}
ptr->buf_typedef = 0x0;
// Use
// expected-error@+2{{cannot use 'ptr->buf' with '__counted_by' attributed type 'union IncompleteUnionTy * __counted_by(count)' (aka 'union IncompleteUnionTy *') because the pointee type 'union IncompleteUnionTy' is incomplete}}
void* addr =
((char*) ptr->buf ) + 1;
// expected-error@+2{{cannot use 'ptr->buf_typedef' with '__counted_by' attributed type 'IncompleteUnion_ty * __counted_by(count)' (aka 'union IncompleteUnionTy *') because the pointee type 'IncompleteUnion_ty' (aka 'union IncompleteUnionTy') is incomplete}}
void* addr_typedef =
((char*) ptr->buf_typedef ) + 1;
// expected-error@+1{{cannot use 'ptr->buf' with '__counted_by' attributed type 'union IncompleteUnionTy * __counted_by(count)' (aka 'union IncompleteUnionTy *') because the pointee type 'union IncompleteUnionTy' is incomplete}}
consume_struct_IncompleteUnionTy(ptr->buf);
// expected-error@+1{{cannot use 'ptr->buf_typedef' with '__counted_by' attributed type 'IncompleteUnion_ty * __counted_by(count)' (aka 'union IncompleteUnionTy *') because the pointee type 'IncompleteUnion_ty' (aka 'union IncompleteUnionTy') is incomplete}}
consume_struct_IncompleteUnionTy(ptr->buf_typedef);
// expected-error@+1 2{{cannot use 'ptr->buf' with '__counted_by' attributed type 'union IncompleteUnionTy * __counted_by(count)' (aka 'union IncompleteUnionTy *') because the pointee type 'union IncompleteUnionTy' is incomplete}}
ptr->buf[0] = ptr->buf[1];
// expected-error@+1 2{{cannot use 'ptr->buf_typedef' with '__counted_by' attributed type 'IncompleteUnion_ty * __counted_by(count)' (aka 'union IncompleteUnionTy *') because the pointee type 'IncompleteUnion_ty' (aka 'union IncompleteUnionTy') is incomplete}}
ptr->buf_typedef[0] = ptr->buf_typedef[1];
}
struct CBBufUnionTyPos global_explicit_desig_init_union_type_pos = {
.count = 0,
// expected-error@+1{{cannot initialize 'CBBufUnionTyPos::buf' with '__counted_by' attributed type 'union IncompleteUnionTy * __counted_by(count)' (aka 'union IncompleteUnionTy *') because the pointee type 'union IncompleteUnionTy' is incomplete}}
.buf = 0x0,
// expected-error@+1{{cannot initialize 'CBBufUnionTyPos::buf_typedef' with '__counted_by' attributed type 'IncompleteUnion_ty * __counted_by(count)' (aka 'union IncompleteUnionTy *') because the pointee type 'IncompleteUnion_ty' (aka 'union IncompleteUnionTy') is incomplete}}
.buf_typedef = 0x0
};
// Defining the type makes `CBBufUnionTyPos` fields usable
union IncompleteUnionTy {
int field;
};
void use_CBBufUnionTyPos_completed(struct CBBufUnionTyPos* ptr) {
ptr->buf = 0x0;
ptr->buf_typedef = 0x0;
void* addr = ((char*) ptr->buf) + 1;
void* addr_typedef = ((char*) ptr->buf_typedef) + 1;
}
// =============================================================================
// # enum incomplete type
// =============================================================================
// expected-note@+1 8{{consider providing a complete definition for 'IncompleteEnum_ty' (aka 'enum IncompleteEnumTy')}}
enum IncompleteEnumTy; // expected-note 8{{consider providing a complete definition for 'enum IncompleteEnumTy'}}
typedef enum IncompleteEnumTy IncompleteEnum_ty;
void consume_struct_IncompleteEnumTy(enum IncompleteEnumTy* buf);
struct CBBufEnumTyPos {
int count;
enum IncompleteEnumTy* __counted_by(count) buf ; // OK expected-note 8{{consider using '__sized_by' instead of '__counted_by'}}
IncompleteEnum_ty *__counted_by(count) buf_typedef; // OK expected-note 8{{consider using '__sized_by' instead of '__counted_by'}}
};
void use_CBBufEnumTyPos(struct CBBufEnumTyPos* ptr) {
struct CBBufEnumTyPos explicit_desig_init = {
.count = 0,
// expected-error@+1{{cannot initialize 'CBBufEnumTyPos::buf' with '__counted_by' attributed type 'enum IncompleteEnumTy * __counted_by(count)' (aka 'enum IncompleteEnumTy *') because the pointee type 'enum IncompleteEnumTy' is incomplete}}
.buf = 0x0,
// expected-error@+1{{cannot initialize 'CBBufEnumTyPos::buf_typedef' with '__counted_by' attributed type 'IncompleteEnum_ty * __counted_by(count)' (aka 'enum IncompleteEnumTy *') because the pointee type 'IncompleteEnum_ty' (aka 'enum IncompleteEnumTy') is incomplete}}
.buf_typedef = 0x0
};
// Assignment
// expected-error@+1{{cannot assign to 'CBBufEnumTyPos::buf' with '__counted_by' attributed type 'enum IncompleteEnumTy * __counted_by(count)' (aka 'enum IncompleteEnumTy *') because the pointee type 'enum IncompleteEnumTy' is incomplete}}
explicit_desig_init.buf = 0x0;
// expected-error@+1{{cannot assign to 'CBBufEnumTyPos::buf_typedef' with '__counted_by' attributed type 'IncompleteEnum_ty * __counted_by(count)' (aka 'enum IncompleteEnumTy *') because the pointee type 'IncompleteEnum_ty' (aka 'enum IncompleteEnumTy') is incomplete}}
explicit_desig_init.buf_typedef = 0x0;
// expected-error@+1{{cannot assign to 'CBBufEnumTyPos::buf' with '__counted_by' attributed type 'enum IncompleteEnumTy * __counted_by(count)' (aka 'enum IncompleteEnumTy *') because the pointee type 'enum IncompleteEnumTy' is incomplete}}
ptr->buf = 0x0;
// expected-error@+1{{cannot assign to 'CBBufEnumTyPos::buf_typedef' with '__counted_by' attributed type 'IncompleteEnum_ty * __counted_by(count)' (aka 'enum IncompleteEnumTy *') because the pointee type 'IncompleteEnum_ty' (aka 'enum IncompleteEnumTy') is incomplete}}
ptr->buf_typedef = 0x0;
// Use
// expected-error@+2{{cannot use 'ptr->buf' with '__counted_by' attributed type 'enum IncompleteEnumTy * __counted_by(count)' (aka 'enum IncompleteEnumTy *') because the pointee type 'enum IncompleteEnumTy' is incomplete}}
void* addr =
((char*) ptr->buf ) + 1;
// expected-error@+2{{cannot use 'ptr->buf_typedef' with '__counted_by' attributed type 'IncompleteEnum_ty * __counted_by(count)' (aka 'enum IncompleteEnumTy *') because the pointee type 'IncompleteEnum_ty' (aka 'enum IncompleteEnumTy') is incomplete}}
void* addr_typedef =
((char*) ptr->buf_typedef ) + 1;
// expected-error@+1{{cannot use 'ptr->buf' with '__counted_by' attributed type 'enum IncompleteEnumTy * __counted_by(count)' (aka 'enum IncompleteEnumTy *') because the pointee type 'enum IncompleteEnumTy' is incomplete}}
consume_struct_IncompleteEnumTy(ptr->buf);
// expected-error@+1{{cannot use 'ptr->buf_typedef' with '__counted_by' attributed type 'IncompleteEnum_ty * __counted_by(count)' (aka 'enum IncompleteEnumTy *') because the pointee type 'IncompleteEnum_ty' (aka 'enum IncompleteEnumTy') is incomplete}}
consume_struct_IncompleteEnumTy(ptr->buf_typedef);
// expected-error@+1 2{{cannot use 'ptr->buf' with '__counted_by' attributed type 'enum IncompleteEnumTy * __counted_by(count)' (aka 'enum IncompleteEnumTy *') because the pointee type 'enum IncompleteEnumTy' is incomplete}}
ptr->buf[0] = ptr->buf[1];
// expected-error@+1 2{{cannot use 'ptr->buf_typedef' with '__counted_by' attributed type 'IncompleteEnum_ty * __counted_by(count)' (aka 'enum IncompleteEnumTy *') because the pointee type 'IncompleteEnum_ty' (aka 'enum IncompleteEnumTy') is incomplete}}
ptr->buf_typedef[0] = ptr->buf_typedef[1];
}
struct CBBufEnumTyPos global_explicit_desig_init_enum_type_pos = {
.count = 0,
// expected-error@+1{{cannot initialize 'CBBufEnumTyPos::buf' with '__counted_by' attributed type 'enum IncompleteEnumTy * __counted_by(count)' (aka 'enum IncompleteEnumTy *') because the pointee type 'enum IncompleteEnumTy' is incomplete}}
.buf = 0x0,
// expected-error@+1{{cannot initialize 'CBBufEnumTyPos::buf_typedef' with '__counted_by' attributed type 'IncompleteEnum_ty * __counted_by(count)' (aka 'enum IncompleteEnumTy *') because the pointee type 'IncompleteEnum_ty' (aka 'enum IncompleteEnumTy') is incomplete}}
.buf_typedef = 0x0
};
// Defining the type makes `CBBufEnumTyPos` fields usable
enum IncompleteEnumTy {
ONE,
TWO
};
void use_CBBufEnumTyPos_completed(struct CBBufEnumTyPos* ptr) {
ptr->buf = 0x0;
ptr->buf_typedef = 0x0;
void* addr = ((char*) ptr->buf) + 1;
void* addr_typedef = ((char*) ptr->buf_typedef) + 1;
}
// Make a complete enum by providing an underlying type
enum CompleteEnumTy : unsigned;
typedef enum CompleteEnumTy CompleteEnum_ty;
struct CBBufEnumTyPos2 {
int count;
enum CompleteEnumTy* __counted_by(count) buf;
CompleteEnum_ty *__counted_by(count) buf_typedef;
};
void use_CBBufEnumTyPos2(struct CBBufEnumTyPos2* ptr) {
struct CBBufEnumTyPos2 explicit_desig_init = {
.count = 0,
.buf = 0x0, // OK
.buf_typedef = 0x0 // OK
};
}
// Make a complete enum by providing a concrete declaration
enum CompleteEnumTy2 {
VALUE_ONE,
VALUE_TWO
};
typedef enum CompleteEnumTy2 CompleteEnum_ty2;
struct CBBufEnumTyPos3 {
int count;
enum CompleteEnumTy2* __counted_by(count) buf;
CompleteEnum_ty2 *__counted_by(count) buf_typedef;
};
void use_CBBufEnumTyPos3(struct CBBufEnumTyPos3* ptr) {
struct CBBufEnumTyPos3 explicit_desig_init = {
.count = 0,
.buf = 0x0, // OK
.buf_typedef = 0x0 // OK
};
}
// =============================================================================
// # Array of __counted_by pointers
// =============================================================================
struct IncompleteTy3;
struct CBBufFAMofCountedByPtrs {
int size;
// TODO: This is misleading. The attribute is written in the type position
// but clang currently doesn't treat it like that and it gets treated as
// an attribute on the array, rather than on the element type.
struct IncompleteTy3* __counted_by(size) arr[];
};
void arr_of_counted_by_ptr(struct CBBufFAMofCountedByPtrs* ptr) {
// TODO: Should be disallowed once parsing attributes in the type position
// works.
ptr->arr[0] = 0x0;
void* addr = ((char*) ptr->arr[0]) + 1;
}
|