commit 588794e66871c1042ab96a644b0da19681268e78 Author: jiang Date: Sun Jun 22 15:28:34 2014 +0800 Add warning diff --git a/libtcc.c b/libtcc.c index d761582..0f8477c 100644 --- a/libtcc.c +++ b/libtcc.c @@ -1404,8 +1404,9 @@ static const FlagDef warning_defs[] = { { offsetof(TCCState, warn_unsupported), 0, "unsupported" }, { offsetof(TCCState, warn_write_strings), 0, "write-strings" }, { offsetof(TCCState, warn_error), 0, "error" }, - { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL, - "implicit-function-declaration" }, + { offsetof(TCCState, warn_implicit_function_declaration), WD_ALL, "implicit-function-declaration" }, + { offsetof(TCCState, warn_return_type), WD_ALL, "return-type" }, + { offsetof(TCCState, warn_char_subscripts), WD_ALL, "char-subscripts" }, }; ST_FUNC int set_flag(TCCState *s, const FlagDef *flags, int nb_flags, diff --git a/tcc.h b/tcc.h index d0859d3..5cf639f 100644 --- a/tcc.h +++ b/tcc.h @@ -601,6 +601,8 @@ struct TCCState { int warn_error; int warn_none; int warn_implicit_function_declaration; + int warn_return_type; + int warn_char_subscripts; /* compile with debug symbol (and use them if error during execution) */ int do_debug; diff --git a/tccgen.c b/tccgen.c index b2a7717..f134586 100644 --- a/tccgen.c +++ b/tccgen.c @@ -2927,13 +2927,12 @@ static void struct_decl(CType *type, int u, int tdef) if (tok != ':') { type_decl(&type1, &ad, &v, TYPE_DIRECT | TYPE_ABSTRACT); if (v == 0 && (type1.t & VT_BTYPE) != VT_STRUCT) - expect("identifier"); + expect("specifier-qualifier-list at end of input"); if (type_size(&type1, &align) < 0) { - if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY)) - flexible = 1; - else - tcc_error("field '%s' has incomplete type", - get_tok_str(v, NULL)); + if ((a == TOK_STRUCT) && (type1.t & VT_ARRAY)) + flexible = 1; + else + tcc_error("field '%s' has incomplete type", get_tok_str(v, NULL)); } if ((type1.t & VT_BTYPE) == VT_FUNC || (type1.t & (VT_TYPEDEF | VT_STATIC | VT_EXTERN | VT_INLINE))) @@ -2945,13 +2944,14 @@ static void struct_decl(CType *type, int u, int tdef) bit_size = expr_const(); /* XXX: handle v = 0 case for messages */ if (bit_size < 0) - tcc_error("negative width in bit-field '%s'", - get_tok_str(v, NULL)); + tcc_error("negative width in bit-field '%s'", get_tok_str(v, NULL)); if (v && bit_size == 0) - tcc_error("zero width for bit-field '%s'", - get_tok_str(v, NULL)); + tcc_error("zero width for bit-field '%s'", get_tok_str(v, NULL)); } - size = type_size(&type1, &align); + if(type1.t & VT_VLA) + size = 0, align = 1; + else + size = type_size(&type1, &align); if (ad.a.aligned) { if (align < ad.a.aligned) align = ad.a.aligned; @@ -3041,13 +3041,18 @@ static void struct_decl(CType *type, int u, int tdef) *ps = ss; ps = &ss->next; } - if (tok == ';' || tok == TOK_EOF) + if (tok == ';' || tok == '}') break; - skip(','); + if(tok == ',') + next(); } - skip(';'); + if(tok == '}'){ + tcc_warning("no ';' at end of struct or union"); + break; + }else + skip(';'); } - skip('}'); + next(); if (!c && flexible) tcc_error("flexible array member '%s' in otherwise empty struct", get_tok_str(v, NULL)); /* store size and alignment */ @@ -4061,6 +4066,8 @@ ST_FUNC void unary(void) } else if (tok == '[') { next(); gexpr(); + if(tcc_state->warn_char_subscripts && (vtop->type.t & (VT_BTYPE|VT_UNSIGNED)) == VT_BYTE) + tcc_warning("array subscript has type 'char'"); gen_op('+'); indir(); skip(']'); @@ -5542,6 +5549,8 @@ static void decl_initializer_alloc(CType *type, AttributeDef *ad, int r, flexible_array = NULL; is_putz = 0; + if (has_init && (type->t & VT_VLA)) + tcc_error("Variable length array cannot be initialized"); if ((type->t & VT_BTYPE) == VT_STRUCT) { Sym *field = type->ref->next; if (field) { @@ -5969,10 +5978,15 @@ static int decl0(int l, int is_for_loop_init) break; btype.t = VT_INT; } - if (((btype.t & VT_BTYPE) == VT_ENUM || - (btype.t & VT_BTYPE) == VT_STRUCT) && - tok == ';') { + if (tok == ';') { + int bt = btype.t & VT_BTYPE; /* we accept no variable after */ + if(btype.t & (VT_CONSTANT|VT_VOLATILE)) + tcc_warning("useless type qualifier in empty declaration.'%s'before", get_tok_str(tok, NULL)); + if(bt != VT_STRUCT && bt != VT_ENUM) + tcc_warning("useless type name in empty declaration '%s'", get_tok_str(tok, NULL)); + if((bt == VT_STRUCT) && ((btype.ref->v & ~SYM_STRUCT) >= SYM_FIRST_ANOM)) + tcc_warning("unnamed struct/union that defines no instances"); next(); continue; } @@ -6143,8 +6157,6 @@ static int decl0(int l, int is_for_loop_init) r |= lvalue_type(type.t); } has_init = (tok == '='); - if (has_init && (type.t & VT_VLA)) - tcc_error("Variable length array cannot be initialized"); if ((btype.t & VT_EXTERN) || ((type.t & VT_BTYPE) == VT_FUNC) || ((type.t & VT_ARRAY) && (type.t & VT_STATIC) && !has_init && l == VT_CONST && type.ref->c < 0)) {