1c79f767941fdfedb02d20296a042ad951a593890Daniel Dunbar// RUN: %clang_cc1 %s -verify -fsyntax-only -ffreestanding 24ef8dd6e8736097bf9e3c387139c668565d89dcaChris Lattner 3bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedman#include <stddef.h> 4392cf91c7b3270a9b27baa3c5b21a836f3330d98Sebastian Redl#include <stdint.h> 5392cf91c7b3270a9b27baa3c5b21a836f3330d98Sebastian Redl 64ef8dd6e8736097bf9e3c387139c668565d89dcaChris Lattnertypedef void (* fp)(void); 74ef8dd6e8736097bf9e3c387139c668565d89dcaChris Lattnervoid foo(void); 8f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor 9f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregor// PR clang/3377 10f6c717c3dca839dcd189b4a6fa46c8fe7a8bec1dDouglas Gregorfp a[(short int)1] = { foo }; 114ef8dd6e8736097bf9e3c387139c668565d89dcaChris Lattner 124cc627111453b75519d5130b57e06256da7b00e8Chris Lattnerint myArray[5] = {1, 2, 3, 4, 5}; 134cc627111453b75519d5130b57e06256da7b00e8Chris Lattnerint *myPointer2 = myArray; 144cc627111453b75519d5130b57e06256da7b00e8Chris Lattnerint *myPointer = &(myArray[2]); 154cc627111453b75519d5130b57e06256da7b00e8Chris Lattner 16d411e04db18f7d07a889d51086861b23cbe05518Chris Lattner 17d411e04db18f7d07a889d51086861b23cbe05518Chris Lattnerextern int x; 18d411e04db18f7d07a889d51086861b23cbe05518Chris Lattnervoid *g = &x; 19d411e04db18f7d07a889d51086861b23cbe05518Chris Lattnerint *h = &x; 20d411e04db18f7d07a889d51086861b23cbe05518Chris Lattner 213e78b19e51034dd27c6e0d811522148345e299e4Aaron Ballmanstruct union_crash 223e78b19e51034dd27c6e0d811522148345e299e4Aaron Ballman{ 233e78b19e51034dd27c6e0d811522148345e299e4Aaron Ballman union 243e78b19e51034dd27c6e0d811522148345e299e4Aaron Ballman { 253e78b19e51034dd27c6e0d811522148345e299e4Aaron Ballman }; 263e78b19e51034dd27c6e0d811522148345e299e4Aaron Ballman}; 270ca7e8bf904d1c2cf70d271f3a06c1d71ff7e4fbAaron Ballman 28a312ce2bdaaff62f21d560bad6cb0519f613d334Eli Friedmanint test() { 290ca7e8bf904d1c2cf70d271f3a06c1d71ff7e4fbAaron Ballman int a[10]; 300ca7e8bf904d1c2cf70d271f3a06c1d71ff7e4fbAaron Ballman int b[10] = a; // expected-error {{array initializer must be an initializer list}} 310ca7e8bf904d1c2cf70d271f3a06c1d71ff7e4fbAaron Ballman int +; // expected-error {{expected identifier or '('}} 320ca7e8bf904d1c2cf70d271f3a06c1d71ff7e4fbAaron Ballman 330ca7e8bf904d1c2cf70d271f3a06c1d71ff7e4fbAaron Ballman struct union_crash u = { .d = 1 }; // expected-error {{field designator 'd' does not refer to any field in type 'struct union_crash'}} 34a312ce2bdaaff62f21d560bad6cb0519f613d334Eli Friedman} 35be34ac67225fc4af6134c7f3507c777cceeec867Chris Lattner 36be34ac67225fc4af6134c7f3507c777cceeec867Chris Lattner 37be34ac67225fc4af6134c7f3507c777cceeec867Chris Lattner// PR2050 38be34ac67225fc4af6134c7f3507c777cceeec867Chris Lattnerstruct cdiff_cmd { 39be34ac67225fc4af6134c7f3507c777cceeec867Chris Lattner const char *name; 40be34ac67225fc4af6134c7f3507c777cceeec867Chris Lattner unsigned short argc; 41be34ac67225fc4af6134c7f3507c777cceeec867Chris Lattner int (*handler)(); 42be34ac67225fc4af6134c7f3507c777cceeec867Chris Lattner}; 43be34ac67225fc4af6134c7f3507c777cceeec867Chris Lattnerint cdiff_cmd_open(); 44be34ac67225fc4af6134c7f3507c777cceeec867Chris Lattnerstruct cdiff_cmd commands[] = { 45be34ac67225fc4af6134c7f3507c777cceeec867Chris Lattner {"OPEN", 1, &cdiff_cmd_open } 46be34ac67225fc4af6134c7f3507c777cceeec867Chris Lattner}; 47be34ac67225fc4af6134c7f3507c777cceeec867Chris Lattner 4897c0a391138d20e1066174a9cfa92860fb06e5a1Eli Friedman// PR2348 4997c0a391138d20e1066174a9cfa92860fb06e5a1Eli Friedmanstatic struct { int z; } s[2]; 5097c0a391138d20e1066174a9cfa92860fb06e5a1Eli Friedmanint *t = &(*s).z; 5197c0a391138d20e1066174a9cfa92860fb06e5a1Eli Friedman 5297c0a391138d20e1066174a9cfa92860fb06e5a1Eli Friedman// PR2349 5397c0a391138d20e1066174a9cfa92860fb06e5a1Eli Friedmanshort *a2(void) 5497c0a391138d20e1066174a9cfa92860fb06e5a1Eli Friedman{ 5597c0a391138d20e1066174a9cfa92860fb06e5a1Eli Friedman short int b; 56d8803632d248a360a040ff03eff1162988058036Chris Lattner static short *bp = &b; // expected-error {{initializer element is not a compile-time constant}} 5797c0a391138d20e1066174a9cfa92860fb06e5a1Eli Friedman 5897c0a391138d20e1066174a9cfa92860fb06e5a1Eli Friedman return bp; 5997c0a391138d20e1066174a9cfa92860fb06e5a1Eli Friedman} 60f8f873deef78de611dd793a1e1201bef0d5a54a3Eli Friedman 61f8f873deef78de611dd793a1e1201bef0d5a54a3Eli Friedmanint pbool(void) { 62f8f873deef78de611dd793a1e1201bef0d5a54a3Eli Friedman typedef const _Bool cbool; 63f8f873deef78de611dd793a1e1201bef0d5a54a3Eli Friedman _Bool pbool1 = (void *) 0; 64f8f873deef78de611dd793a1e1201bef0d5a54a3Eli Friedman cbool pbool2 = &pbool; 65f8f873deef78de611dd793a1e1201bef0d5a54a3Eli Friedman return pbool2; 66f8f873deef78de611dd793a1e1201bef0d5a54a3Eli Friedman} 67f8f873deef78de611dd793a1e1201bef0d5a54a3Eli Friedman 68d5bac5704768606c3182dcc1386de12b3944601bChris Lattner 69d5bac5704768606c3182dcc1386de12b3944601bChris Lattner// rdar://5870981 70d5bac5704768606c3182dcc1386de12b3944601bChris Lattnerunion { float f; unsigned u; } u = { 1.0f }; 71d5bac5704768606c3182dcc1386de12b3944601bChris Lattner 721e465df22f312a3661d70fc15b1a55dc97ebfce8Daniel Dunbar// rdar://6156694 731e465df22f312a3661d70fc15b1a55dc97ebfce8Daniel Dunbarint f3(int x) { return x; } 741e465df22f312a3661d70fc15b1a55dc97ebfce8Daniel Dunbartypedef void (*vfunc)(void); 751e465df22f312a3661d70fc15b1a55dc97ebfce8Daniel Dunbarvoid *bar = (vfunc) f3; 76b529d830b3b1667157da92ab0b5c32e1acfafbabEli Friedman 77b529d830b3b1667157da92ab0b5c32e1acfafbabEli Friedman// PR2747 78b529d830b3b1667157da92ab0b5c32e1acfafbabEli Friedmanstruct sym_reg { 79b529d830b3b1667157da92ab0b5c32e1acfafbabEli Friedman char nc_gpreg; 80b529d830b3b1667157da92ab0b5c32e1acfafbabEli Friedman}; 81b529d830b3b1667157da92ab0b5c32e1acfafbabEli Friedmanint sym_fw1a_scr[] = { 8267c8601677a6a4b01a77855b8b51240ef44acfb3Nuno Lopes ((int)(&((struct sym_reg *)0)->nc_gpreg)) & 0, 8367c8601677a6a4b01a77855b8b51240ef44acfb3Nuno Lopes 8 * ((int)(&((struct sym_reg *)0)->nc_gpreg)) 84b529d830b3b1667157da92ab0b5c32e1acfafbabEli Friedman}; 85e2f56196e916e4d2f9eeec5c8978490cdedb3f64Chris Lattner 86e2f56196e916e4d2f9eeec5c8978490cdedb3f64Chris Lattner// PR3001 877727acf243ee61c0757d86c95b69dbee56a3d898John McCallstruct s1 s2 = { // expected-error {{variable has incomplete type 'struct s1'}} \ 887727acf243ee61c0757d86c95b69dbee56a3d898John McCall // expected-note {{forward declaration of 'struct s1'}} 894ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor .a = sizeof(struct s3), // expected-error {{invalid application of 'sizeof'}} \ 904ec339f43c0cae2678334850c90926bea10999c7Douglas Gregor // expected-note{{forward declaration of 'struct s3'}} 91e2f56196e916e4d2f9eeec5c8978490cdedb3f64Chris Lattner .b = bogus // expected-error {{use of undeclared identifier 'bogus'}} 92e2f56196e916e4d2f9eeec5c8978490cdedb3f64Chris Lattner} 93e2f56196e916e4d2f9eeec5c8978490cdedb3f64Chris Lattner 9491b9f2072a20906dd34ac6cb83813debf3595328Anders Carlsson// PR3382 9591b9f2072a20906dd34ac6cb83813debf3595328Anders Carlssonchar t[] = ("Hello"); 96d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregor 97d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregor// <rdar://problem/6094855> 98d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregortypedef struct { } empty; 99d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregor 100d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregortypedef struct { 101d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregor empty e; 102d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregor int i2; 103d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregor} st; 104d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregor 105d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregorst st1 = { .i2 = 1 }; 106d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregor 107d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregor// <rdar://problem/6096826> 108d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregorstruct { 109d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregor int a; 110d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregor int z[2]; 111d079b2d6005bdade40ae4a0f228aba2affc591cfDouglas Gregor} y = { .z = {} }; 112d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregor 113d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregorint bbb[10]; 114d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregor 115d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregorstruct foo2 { 116c91b7d9216e2747bc2fa8886cf1776e769539b88Eli Friedman uintptr_t a; 117d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregor}; 118d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregor 119d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregorstruct foo2 bar2[] = { 120392cf91c7b3270a9b27baa3c5b21a836f3330d98Sebastian Redl { (intptr_t)bbb } 121d863517ab7e936cbc3244a0fc431c8b672f5ece4Douglas Gregor}; 1227c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregor 1237c53ca6e03833adab4465462b7d5c888741b715dDouglas Gregorstruct foo2 bar3 = { 1, 2 }; // expected-warning{{excess elements in struct initializer}} 12491110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman 12591110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedmanint* ptest1 = __builtin_choose_expr(1, (int*)0, (int*)0); 12691110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman 12791110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedmantypedef int32_t ivector4 __attribute((vector_size(16))); 12891110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedmanivector4 vtest1 = 1 ? (ivector4){1} : (ivector4){1}; 12991110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedmanivector4 vtest2 = __builtin_choose_expr(1, (ivector4){1}, (ivector4){1}); 13091110ee24e3475e0a3a38938c7b98439b5cf0b0eEli Friedman 13142edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedmanuintptr_t ptrasintadd1 = (uintptr_t)&a - 4; 13242edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedmanuintptr_t ptrasintadd2 = (uintptr_t)&a + 4; 13342edd0d32a729d2735a6fb152ba6bf349bf0a169Eli Friedmanuintptr_t ptrasintadd3 = 4 + (uintptr_t)&a; 134bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedman 135bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedman// PR4285 136bb6415c69fc6440c337970e39749d4d482d9de42Eli Friedmanconst wchar_t widestr[] = L"asdf"; 13767f85fcdab64f36233a7211f963ed1cccffcbfb8Eli Friedman 13867f85fcdab64f36233a7211f963ed1cccffcbfb8Eli Friedman// PR5447 13967f85fcdab64f36233a7211f963ed1cccffcbfb8Eli Friedmanconst double pr5447 = (0.05 < -1.0) ? -1.0 : 0.0499878; 14067f85fcdab64f36233a7211f963ed1cccffcbfb8Eli Friedman 141a7d3c04fcfe9d4af2f7758f46aef26b1a8f8ac09Rafael Espindola// PR4386 142a7d3c04fcfe9d4af2f7758f46aef26b1a8f8ac09Rafael Espindola 143a7d3c04fcfe9d4af2f7758f46aef26b1a8f8ac09Rafael Espindola// None of these are constant initializers, but we implement GCC's old 144a7d3c04fcfe9d4af2f7758f46aef26b1a8f8ac09Rafael Espindola// behaviour of accepting bar and zed but not foo. GCC's behaviour was 145a7d3c04fcfe9d4af2f7758f46aef26b1a8f8ac09Rafael Espindola// changed in 2007 (rev 122551), so we should be able to change too one 146a7d3c04fcfe9d4af2f7758f46aef26b1a8f8ac09Rafael Espindola// day. 147a7d3c04fcfe9d4af2f7758f46aef26b1a8f8ac09Rafael Espindolaint PR4386_bar(); 148a7d3c04fcfe9d4af2f7758f46aef26b1a8f8ac09Rafael Espindolaint PR4386_foo() __attribute((weak)); 149a7d3c04fcfe9d4af2f7758f46aef26b1a8f8ac09Rafael Espindolaint PR4386_zed(); 150a7d3c04fcfe9d4af2f7758f46aef26b1a8f8ac09Rafael Espindola 151a7d3c04fcfe9d4af2f7758f46aef26b1a8f8ac09Rafael Espindolaint PR4386_a = ((void *) PR4386_bar) != 0; 152a7d3c04fcfe9d4af2f7758f46aef26b1a8f8ac09Rafael Espindolaint PR4386_b = ((void *) PR4386_foo) != 0; // expected-error{{initializer element is not a compile-time constant}} 153a7d3c04fcfe9d4af2f7758f46aef26b1a8f8ac09Rafael Espindolaint PR4386_c = ((void *) PR4386_zed) != 0; 154a7d3c04fcfe9d4af2f7758f46aef26b1a8f8ac09Rafael Espindolaint PR4386_zed() __attribute((weak)); 15516c5dea6c2d3e4cf529de9d9b37f6a40441acb2cChris Lattner 1568a5d929268beeb69b654120fb0b4f8893ad230ebEli Friedman// <rdar://problem/10185490> (derived from SPEC vortex benchmark) 1578a5d929268beeb69b654120fb0b4f8893ad230ebEli Friedmantypedef char strty[10]; 1588a5d929268beeb69b654120fb0b4f8893ad230ebEli Friedmanstruct vortexstruct { strty s; }; 1598a5d929268beeb69b654120fb0b4f8893ad230ebEli Friedmanstruct vortexstruct vortexvar = { "asdf" }; 16021cde050b64eefbb5094af67985752eee42d00e2Eli Friedman 16121cde050b64eefbb5094af67985752eee42d00e2Eli Friedmantypedef struct { uintptr_t x : 2; } StructWithBitfield; 16221cde050b64eefbb5094af67985752eee42d00e2Eli FriedmanStructWithBitfield bitfieldvar = { (uintptr_t)&bitfieldvar }; // expected-error {{initializer element is not a compile-time constant}} 163