/* If EXPR involves an anonymous VLA type, prepend a DECL_EXPR for that type
to trigger gimplify_type_sizes; otherwise a cast to pointer-to-VLA confuses
- the middle-end (c++/88256). */
+ the middle-end (c++/88256). If EXPR is a DECL, use add_stmt and return
+ NULL_TREE; otherwise return a COMPOUND_STMT of the DECL_EXPR and EXPR. */
-static tree
+tree
predeclare_vla (tree expr)
{
tree type = TREE_TYPE (expr);
if (type == error_mark_node)
return expr;
+ if (is_typedef_decl (expr))
+ type = DECL_ORIGINAL_TYPE (expr);
/* We need to strip pointers for gimplify_type_sizes. */
tree vla = type;
DECL_ARTIFICIAL (decl) = 1;
TYPE_NAME (vla) = decl;
tree dexp = build_stmt (input_location, DECL_EXPR, decl);
- expr = build2 (COMPOUND_EXPR, type, dexp, expr);
- return expr;
+ if (DECL_P (expr))
+ {
+ add_stmt (dexp);
+ return NULL_TREE;
+ }
+ else
+ {
+ expr = build2 (COMPOUND_EXPR, type, dexp, expr);
+ return expr;
+ }
}
/* Perform any pre-gimplification lowering of C++ front end trees to
extern tree cp_fold_rvalue (tree);
extern tree cp_fully_fold (tree);
extern tree cp_fully_fold_init (tree);
+extern tree predeclare_vla (tree);
extern void clear_fold_cache (void);
extern tree lookup_hotness_attribute (tree);
extern tree process_stmt_hotness_attribute (tree, location_t);
if (ndecl != error_mark_node)
cp_maybe_mangle_decomp (ndecl, first, cnt);
+ /* In a non-template function, VLA type declarations are
+ handled in grokdeclarator; for templates, handle them
+ now. */
+ predeclare_vla (decl);
+
cp_finish_decl (decl, init, const_init, NULL_TREE,
constinit_p ? LOOKUP_CONSTINIT : 0);
--- /dev/null
+// PR c++/95232
+// { dg-additional-options "-Wno-vla -ftrapv -fnon-call-exceptions -O -fsanitize=undefined" }
+
+template <typename T>
+int tmain(T argc) {
+ typedef double (*chunk_t)[argc[0][0]];
+ chunk_t var;
+ (void)var[0][0];
+ return 0;
+}
+
+int main (int argc, char **argv) {
+ return tmain(argv);
+}