From ffd6080f9e1e742c73ac38354b31c6fc4e3963ba Mon Sep 17 00:00:00 2001 From: xleroy Date: Tue, 15 Mar 2011 12:41:45 +0000 Subject: Revised handling of sizeof(string-literal) git-svn-id: https://yquem.inria.fr/compcert/svn/compcert/trunk@1611 fca1b0fc-160b-0410-b1d3-a4f43f01ea2e --- cparser/Elab.ml | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'cparser/Elab.ml') diff --git a/cparser/Elab.ml b/cparser/Elab.ml index b79aa10b..92452b90 100644 --- a/cparser/Elab.ml +++ b/cparser/Elab.ml @@ -837,15 +837,22 @@ let elab_expr loc env a = | CAST ((spec, dcl), _) -> error "cast of initializer expression is not supported" - | EXPR_SIZEOF(CONSTANT(CONST_STRING s)) -> - let cst = CInt(Int64.of_int (String.length s), size_t_ikind, "") in - { edesc = EConst cst; etyp = type_of_constant cst } - | EXPR_SIZEOF a1 -> let b1 = elab a1 in if sizeof env b1.etyp = None then err "incomplete type %a" Cprint.typ b1.etyp; - { edesc = ESizeof b1.etyp; etyp = TInt(size_t_ikind, []) } + let bdesc = + (* Catch special cases sizeof("string literal") *) + match b1.edesc with + | EConst(CStr s) -> + let sz = String.length s + 1 in + EConst(CInt(Int64.of_int sz, size_t_ikind, "")) + | EConst(CWStr s) -> + let sz = (!config).sizeof_wchar * (List.length s + 1) in + EConst(CInt(Int64.of_int sz, size_t_ikind, "")) + | _ -> + ESizeof b1.etyp in + { edesc = bdesc; etyp = TInt(size_t_ikind, []) } | TYPE_SIZEOF (spec, dcl) -> let ty = elab_type loc env spec dcl in -- cgit