comparison gcc/c-family/c-common.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children d34655255c78 1830386684a0
comparison
equal deleted inserted replaced
111:04ced10e8804 131:84e7813d76e9
1 /* Subroutines shared by all languages that are variants of C. 1 /* Subroutines shared by all languages that are variants of C.
2 Copyright (C) 1992-2017 Free Software Foundation, Inc. 2 Copyright (C) 1992-2018 Free Software Foundation, Inc.
3 3
4 This file is part of GCC. 4 This file is part of GCC.
5 5
6 GCC is free software; you can redistribute it and/or modify it under 6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free 7 the terms of the GNU General Public License as published by the Free
46 #include "tree-iterator.h" 46 #include "tree-iterator.h"
47 #include "opts.h" 47 #include "opts.h"
48 #include "gimplify.h" 48 #include "gimplify.h"
49 #include "substring-locations.h" 49 #include "substring-locations.h"
50 #include "spellcheck.h" 50 #include "spellcheck.h"
51 #include "selftest.h"
51 52
52 cpp_reader *parse_in; /* Declared in c-pragma.h. */ 53 cpp_reader *parse_in; /* Declared in c-pragma.h. */
53 54
54 /* Mode used to build pointers (VOIDmode means ptr_mode). */ 55 /* Mode used to build pointers (VOIDmode means ptr_mode). */
55 56
210 int flag_isoc99; 211 int flag_isoc99;
211 212
212 /* Nonzero means use the ISO C11 dialect of C. */ 213 /* Nonzero means use the ISO C11 dialect of C. */
213 214
214 int flag_isoc11; 215 int flag_isoc11;
216
217 /* Nonzero means use the ISO C2X dialect of C. */
218
219 int flag_isoc2x;
215 220
216 /* Nonzero means that we have builtin functions, and main is an int. */ 221 /* Nonzero means that we have builtin functions, and main is an int. */
217 222
218 int flag_hosted = 1; 223 int flag_hosted = 1;
219 224
336 { "_Alignas", RID_ALIGNAS, D_CONLY }, 341 { "_Alignas", RID_ALIGNAS, D_CONLY },
337 { "_Alignof", RID_ALIGNOF, D_CONLY }, 342 { "_Alignof", RID_ALIGNOF, D_CONLY },
338 { "_Atomic", RID_ATOMIC, D_CONLY }, 343 { "_Atomic", RID_ATOMIC, D_CONLY },
339 { "_Bool", RID_BOOL, D_CONLY }, 344 { "_Bool", RID_BOOL, D_CONLY },
340 { "_Complex", RID_COMPLEX, 0 }, 345 { "_Complex", RID_COMPLEX, 0 },
341 { "_Cilk_spawn", RID_CILK_SPAWN, 0 },
342 { "_Cilk_sync", RID_CILK_SYNC, 0 },
343 { "_Cilk_for", RID_CILK_FOR, 0 },
344 { "_Imaginary", RID_IMAGINARY, D_CONLY }, 346 { "_Imaginary", RID_IMAGINARY, D_CONLY },
345 { "_Float16", RID_FLOAT16, D_CONLY }, 347 { "_Float16", RID_FLOAT16, D_CONLY },
346 { "_Float32", RID_FLOAT32, D_CONLY }, 348 { "_Float32", RID_FLOAT32, D_CONLY },
347 { "_Float64", RID_FLOAT64, D_CONLY }, 349 { "_Float64", RID_FLOAT64, D_CONLY },
348 { "_Float128", RID_FLOAT128, D_CONLY }, 350 { "_Float128", RID_FLOAT128, D_CONLY },
374 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY }, 376 RID_BUILTIN_CALL_WITH_STATIC_CHAIN, D_CONLY },
375 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY }, 377 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY },
376 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY }, 378 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY },
377 { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY }, 379 { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY },
378 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 }, 380 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 },
381 { "__builtin_tgmath", RID_BUILTIN_TGMATH, D_CONLY },
379 { "__builtin_offsetof", RID_OFFSETOF, 0 }, 382 { "__builtin_offsetof", RID_OFFSETOF, 0 },
380 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY }, 383 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY },
381 { "__builtin_va_arg", RID_VA_ARG, 0 }, 384 { "__builtin_va_arg", RID_VA_ARG, 0 },
382 { "__complex", RID_COMPLEX, 0 }, 385 { "__complex", RID_COMPLEX, 0 },
383 { "__complex__", RID_COMPLEX, 0 }, 386 { "__complex__", RID_COMPLEX, 0 },
867 substr_loc.get_end_idx (), 870 substr_loc.get_end_idx (),
868 out_loc); 871 out_loc);
869 } 872 }
870 873
871 874
872 /* Fold X for consideration by one of the warning functions when checking
873 whether an expression has a constant value. */
874
875 tree
876 fold_for_warn (tree x)
877 {
878 if (c_dialect_cxx ())
879 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL);
880 else
881 /* The C front-end has already folded X appropriately. */
882 return x;
883 }
884
885 /* Return true iff T is a boolean promoted to int. */ 875 /* Return true iff T is a boolean promoted to int. */
886 876
887 bool 877 bool
888 bool_promoted_to_int_p (tree t) 878 bool_promoted_to_int_p (tree t)
889 { 879 {
940 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) 930 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)))
941 return true; 931 return true;
942 932
943 convertible_lax = 933 convertible_lax =
944 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)) 934 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))
945 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE || 935 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE
946 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)) 936 || known_eq (TYPE_VECTOR_SUBPARTS (t1),
937 TYPE_VECTOR_SUBPARTS (t2)))
947 && (INTEGRAL_TYPE_P (TREE_TYPE (t1)) 938 && (INTEGRAL_TYPE_P (TREE_TYPE (t1))
948 == INTEGRAL_TYPE_P (TREE_TYPE (t2)))); 939 == INTEGRAL_TYPE_P (TREE_TYPE (t2))));
949 940
950 if (!convertible_lax || flag_lax_vector_conversions) 941 if (!convertible_lax || flag_lax_vector_conversions)
951 return convertible_lax; 942 return convertible_lax;
952 943
953 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) 944 if (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2))
954 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))) 945 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2)))
955 return true; 946 return true;
956 947
957 if (emit_lax_note && !emitted_lax_note) 948 if (emit_lax_note && !emitted_lax_note)
958 { 949 {
1016 error_at (loc, "__builtin_shuffle argument vectors must be of " 1007 error_at (loc, "__builtin_shuffle argument vectors must be of "
1017 "the same type"); 1008 "the same type");
1018 return error_mark_node; 1009 return error_mark_node;
1019 } 1010 }
1020 1011
1021 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)) 1012 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)),
1022 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)) 1013 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))
1023 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)) 1014 && maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)),
1024 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))) 1015 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))))
1025 { 1016 {
1026 if (complain) 1017 if (complain)
1027 error_at (loc, "__builtin_shuffle number of elements of the " 1018 error_at (loc, "__builtin_shuffle number of elements of the "
1028 "argument vector(s) and the mask vector should " 1019 "argument vector(s) and the mask vector should "
1029 "be the same"); 1020 "be the same");
2278 inner_mode = GET_MODE_INNER (mode); 2269 inner_mode = GET_MODE_INNER (mode);
2279 inner_type = c_common_type_for_mode (inner_mode, unsignedp); 2270 inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2280 if (inner_type != NULL_TREE) 2271 if (inner_type != NULL_TREE)
2281 return build_complex_type (inner_type); 2272 return build_complex_type (inner_type);
2282 } 2273 }
2283 else if (VECTOR_MODE_P (mode)) 2274 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL
2275 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2276 {
2277 unsigned int elem_bits = vector_element_size (GET_MODE_BITSIZE (mode),
2278 GET_MODE_NUNITS (mode));
2279 tree bool_type = build_nonstandard_boolean_type (elem_bits);
2280 return build_vector_type_for_mode (bool_type, mode);
2281 }
2282 else if (VECTOR_MODE_P (mode)
2283 && valid_vector_subparts_p (GET_MODE_NUNITS (mode)))
2284 { 2284 {
2285 machine_mode inner_mode = GET_MODE_INNER (mode); 2285 machine_mode inner_mode = GET_MODE_INNER (mode);
2286 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp); 2286 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp);
2287 if (inner_type != NULL_TREE) 2287 if (inner_type != NULL_TREE)
2288 return build_vector_type_for_mode (inner_type, mode); 2288 return build_vector_type_for_mode (inner_type, mode);
2692 case BIT_XOR_EXPR: 2692 case BIT_XOR_EXPR:
2693 opname = "^"; break; 2693 opname = "^"; break;
2694 default: 2694 default:
2695 gcc_unreachable (); 2695 gcc_unreachable ();
2696 } 2696 }
2697 error_at_rich_loc (richloc, 2697 error_at (richloc,
2698 "invalid operands to binary %s (have %qT and %qT)", 2698 "invalid operands to binary %s (have %qT and %qT)",
2699 opname, type0, type1); 2699 opname, type0, type1);
2700 } 2700 }
2701 2701
2702 /* Given an expression as a tree, return its original type. Do this 2702 /* Given an expression as a tree, return its original type. Do this
2703 by stripping any conversion that preserves the sign and precision. */ 2703 by stripping any conversion that preserves the sign and precision. */
2704 static tree 2704 static tree
3291 case ADDR_EXPR: 3291 case ADDR_EXPR:
3292 { 3292 {
3293 tree inner = TREE_OPERAND (expr, 0); 3293 tree inner = TREE_OPERAND (expr, 0);
3294 if (decl_with_nonnull_addr_p (inner)) 3294 if (decl_with_nonnull_addr_p (inner))
3295 { 3295 {
3296 /* Common Ada/Pascal programmer's mistake. */ 3296 /* Common Ada programmer's mistake. */
3297 warning_at (location, 3297 warning_at (location,
3298 OPT_Waddress, 3298 OPT_Waddress,
3299 "the address of %qD will always evaluate as %<true%>", 3299 "the address of %qD will always evaluate as %<true%>",
3300 inner); 3300 inner);
3301 return truthvalue_true_node; 3301 return truthvalue_true_node;
3314 false); 3314 false);
3315 goto ret; 3315 goto ret;
3316 3316
3317 case NEGATE_EXPR: 3317 case NEGATE_EXPR:
3318 case ABS_EXPR: 3318 case ABS_EXPR:
3319 case ABSU_EXPR:
3319 case FLOAT_EXPR: 3320 case FLOAT_EXPR:
3320 case EXCESS_PRECISION_EXPR: 3321 case EXCESS_PRECISION_EXPR:
3321 /* These don't change whether an object is nonzero or zero. */ 3322 /* These don't change whether an object is nonzero or zero. */
3322 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0)); 3323 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0));
3323 3324
3932 #include "builtins.def" 3933 #include "builtins.def"
3933 3934
3934 targetm.init_builtins (); 3935 targetm.init_builtins ();
3935 3936
3936 build_common_builtin_nodes (); 3937 build_common_builtin_nodes ();
3937
3938 if (flag_cilkplus)
3939 cilk_init_builtins ();
3940 } 3938 }
3941 3939
3942 /* Like get_identifier, but avoid warnings about null arguments when 3940 /* Like get_identifier, but avoid warnings about null arguments when
3943 the argument may be NULL for targets where GCC lacks stdint.h type 3941 the argument may be NULL for targets where GCC lacks stdint.h type
3944 information. */ 3942 information. */
4901 add_stmt (build_stmt (loc, LABEL_EXPR, t)); 4899 add_stmt (build_stmt (loc, LABEL_EXPR, t));
4902 } 4900 }
4903 return error_mark_node; 4901 return error_mark_node;
4904 } 4902 }
4905 4903
4904 /* Subroutine of c_switch_covers_all_cases_p, called via
4905 splay_tree_foreach. Return 1 if it doesn't cover all the cases.
4906 ARGS[0] is initially NULL and after the first iteration is the
4907 so far highest case label. ARGS[1] is the minimum of SWITCH_COND's
4908 type. */
4909
4910 static int
4911 c_switch_covers_all_cases_p_1 (splay_tree_node node, void *data)
4912 {
4913 tree label = (tree) node->value;
4914 tree *args = (tree *) data;
4915
4916 /* If there is a default case, we shouldn't have called this. */
4917 gcc_assert (CASE_LOW (label));
4918
4919 if (args[0] == NULL_TREE)
4920 {
4921 if (wi::to_widest (args[1]) < wi::to_widest (CASE_LOW (label)))
4922 return 1;
4923 }
4924 else if (wi::add (wi::to_widest (args[0]), 1)
4925 != wi::to_widest (CASE_LOW (label)))
4926 return 1;
4927 if (CASE_HIGH (label))
4928 args[0] = CASE_HIGH (label);
4929 else
4930 args[0] = CASE_LOW (label);
4931 return 0;
4932 }
4933
4934 /* Return true if switch with CASES and switch condition with type
4935 covers all possible values in the case labels. */
4936
4937 bool
4938 c_switch_covers_all_cases_p (splay_tree cases, tree type)
4939 {
4940 /* If there is default:, this is always the case. */
4941 splay_tree_node default_node
4942 = splay_tree_lookup (cases, (splay_tree_key) NULL);
4943 if (default_node)
4944 return true;
4945
4946 if (!INTEGRAL_TYPE_P (type))
4947 return false;
4948
4949 tree args[2] = { NULL_TREE, TYPE_MIN_VALUE (type) };
4950 if (splay_tree_foreach (cases, c_switch_covers_all_cases_p_1, args))
4951 return false;
4952
4953 /* If there are no cases at all, or if the highest case label
4954 is smaller than TYPE_MAX_VALUE, return false. */
4955 if (args[0] == NULL_TREE
4956 || wi::to_widest (args[0]) < wi::to_widest (TYPE_MAX_VALUE (type)))
4957 return false;
4958
4959 return true;
4960 }
4961
4906 /* Finish an expression taking the address of LABEL (an 4962 /* Finish an expression taking the address of LABEL (an
4907 IDENTIFIER_NODE). Returns an expression for the address. 4963 IDENTIFIER_NODE). Returns an expression for the address.
4908 4964
4909 LOC is the location for the expression returned. */ 4965 LOC is the location for the expression returned. */
4910 4966
5256 && (warn_strict_null_sentinel || null_node != sentinel)) 5312 && (warn_strict_null_sentinel || null_node != sentinel))
5257 warning (OPT_Wformat_, "missing sentinel in function call"); 5313 warning (OPT_Wformat_, "missing sentinel in function call");
5258 } 5314 }
5259 } 5315 }
5260 5316
5261 /* Check that the same argument isn't passed to restrict arguments 5317 /* Check that the same argument isn't passed to two or more
5262 and other arguments. */ 5318 restrict-qualified formal and issue a -Wrestrict warning
5263 5319 if it is. Return true if a warning has been issued. */
5264 static void 5320
5321 static bool
5265 check_function_restrict (const_tree fndecl, const_tree fntype, 5322 check_function_restrict (const_tree fndecl, const_tree fntype,
5266 int nargs, tree *argarray) 5323 int nargs, tree *argarray)
5267 { 5324 {
5268 int i; 5325 int i;
5269 tree parms; 5326 tree parms = TYPE_ARG_TYPES (fntype);
5270 5327
5271 if (fndecl 5328 if (fndecl
5272 && TREE_CODE (fndecl) == FUNCTION_DECL 5329 && TREE_CODE (fndecl) == FUNCTION_DECL)
5273 && DECL_ARGUMENTS (fndecl)) 5330 {
5274 parms = DECL_ARGUMENTS (fndecl); 5331 /* Avoid diagnosing calls built-ins with a zero size/bound
5275 else 5332 here. They are checked in more detail elsewhere. */
5276 parms = TYPE_ARG_TYPES (fntype); 5333 if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)
5334 && nargs == 3
5335 && TREE_CODE (argarray[2]) == INTEGER_CST
5336 && integer_zerop (argarray[2]))
5337 return false;
5338
5339 if (DECL_ARGUMENTS (fndecl))
5340 parms = DECL_ARGUMENTS (fndecl);
5341 }
5277 5342
5278 for (i = 0; i < nargs; i++) 5343 for (i = 0; i < nargs; i++)
5279 TREE_VISITED (argarray[i]) = 0; 5344 TREE_VISITED (argarray[i]) = 0;
5345
5346 bool warned = false;
5280 5347
5281 for (i = 0; i < nargs && parms && parms != void_list_node; i++) 5348 for (i = 0; i < nargs && parms && parms != void_list_node; i++)
5282 { 5349 {
5283 tree type; 5350 tree type;
5284 if (TREE_CODE (parms) == PARM_DECL) 5351 if (TREE_CODE (parms) == PARM_DECL)
5292 parms = TREE_CHAIN (parms); 5359 parms = TREE_CHAIN (parms);
5293 } 5360 }
5294 if (POINTER_TYPE_P (type) 5361 if (POINTER_TYPE_P (type)
5295 && TYPE_RESTRICT (type) 5362 && TYPE_RESTRICT (type)
5296 && !TYPE_READONLY (TREE_TYPE (type))) 5363 && !TYPE_READONLY (TREE_TYPE (type)))
5297 warn_for_restrict (i, argarray, nargs); 5364 warned |= warn_for_restrict (i, argarray, nargs);
5298 } 5365 }
5299 5366
5300 for (i = 0; i < nargs; i++) 5367 for (i = 0; i < nargs; i++)
5301 TREE_VISITED (argarray[i]) = 0; 5368 TREE_VISITED (argarray[i]) = 0;
5369
5370 return warned;
5302 } 5371 }
5303 5372
5304 /* Helper for check_function_nonnull; given a list of operands which 5373 /* Helper for check_function_nonnull; given a list of operands which
5305 must be non-null in ARGS, determine if operand PARAM_NUM should be 5374 must be non-null in ARGS, determine if operand PARAM_NUM should be
5306 checked. */ 5375 checked. */
5336 list (which means to check every pointer argument). */ 5405 list (which means to check every pointer argument). */
5337 5406
5338 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE) 5407 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE)
5339 return; 5408 return;
5340 5409
5341 /* When not optimizing diagnose the simple cases of null arguments. 5410 /* Diagnose the simple cases of null arguments. */
5342 When optimization is enabled defer the checking until expansion 5411 if (integer_zerop (fold_for_warn (param)))
5343 when more cases can be detected. */
5344 if (integer_zerop (param))
5345 { 5412 {
5346 warning_at (pctx->loc, OPT_Wnonnull, "null argument where non-null " 5413 warning_at (pctx->loc, OPT_Wnonnull, "null argument where non-null "
5347 "required (argument %lu)", (unsigned long) param_num); 5414 "required (argument %lu)", (unsigned long) param_num);
5348 pctx->warned_p = true; 5415 pctx->warned_p = true;
5349 } 5416 }
5356 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp) 5423 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp)
5357 { 5424 {
5358 /* Verify the arg number is a small constant. */ 5425 /* Verify the arg number is a small constant. */
5359 if (tree_fits_uhwi_p (arg_num_expr)) 5426 if (tree_fits_uhwi_p (arg_num_expr))
5360 { 5427 {
5361 *valp = TREE_INT_CST_LOW (arg_num_expr); 5428 *valp = tree_to_uhwi (arg_num_expr);
5362 return true; 5429 return true;
5363 } 5430 }
5364 else 5431 else
5365 return false; 5432 return false;
5366 } 5433 }
5537 return true; 5604 return true;
5538 } 5605 }
5539 5606
5540 5607
5541 /* Check for valid arguments being passed to a function with FNTYPE. 5608 /* Check for valid arguments being passed to a function with FNTYPE.
5542 There are NARGS arguments in the array ARGARRAY. LOC should be used for 5609 There are NARGS arguments in the array ARGARRAY. LOC should be used
5543 diagnostics. Return true if -Wnonnull warning has been diagnosed. */ 5610 for diagnostics. Return true if either -Wnonnull or -Wrestrict has
5611 been issued. */
5612
5544 bool 5613 bool
5545 check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype, 5614 check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype,
5546 int nargs, tree *argarray, vec<location_t> *arglocs) 5615 int nargs, tree *argarray, vec<location_t> *arglocs)
5547 { 5616 {
5548 bool warned_p = false; 5617 bool warned_p = false;
5561 5630
5562 if (warn_format) 5631 if (warn_format)
5563 check_function_sentinel (fntype, nargs, argarray); 5632 check_function_sentinel (fntype, nargs, argarray);
5564 5633
5565 if (warn_restrict) 5634 if (warn_restrict)
5566 check_function_restrict (fndecl, fntype, nargs, argarray); 5635 warned_p |= check_function_restrict (fndecl, fntype, nargs, argarray);
5567 return warned_p; 5636 return warned_p;
5568 } 5637 }
5569 5638
5570 /* Generic argument checking recursion routine. PARAM is the argument to 5639 /* Generic argument checking recursion routine. PARAM is the argument to
5571 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked 5640 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked
5687 5756
5688 bool 5757 bool
5689 check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc, 5758 check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc,
5690 tree fndecl, int nargs, tree *args) 5759 tree fndecl, int nargs, tree *args)
5691 { 5760 {
5692 if (!DECL_BUILT_IN (fndecl) 5761 if (!fndecl_built_in_p (fndecl, BUILT_IN_NORMAL))
5693 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL)
5694 return true; 5762 return true;
5695 5763
5696 switch (DECL_FUNCTION_CODE (fndecl)) 5764 switch (DECL_FUNCTION_CODE (fndecl))
5697 { 5765 {
5698 case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX: 5766 case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX:
5888 The caller is responsible for deleting the returned pointer. */ 5956 The caller is responsible for deleting the returned pointer. */
5889 5957
5890 static char * 5958 static char *
5891 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size) 5959 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size)
5892 { 5960 {
5893 const int lhs_size = strlen (lhs); 5961 const size_t lhs_size = strlen (lhs);
5894 char *result = XNEWVEC (char, lhs_size + rhs_size); 5962 char *result = XNEWVEC (char, lhs_size + rhs_size);
5895 strncpy (result, lhs, lhs_size); 5963 memcpy (result, lhs, lhs_size);
5896 strncpy (result + lhs_size, rhs_start, rhs_size); 5964 memcpy (result + lhs_size, rhs_start, rhs_size);
5897 return result; 5965 return result;
5898 } 5966 }
5899 5967
5900 /* Issue the error given by GMSGID at RICHLOC, indicating that it occurred 5968 /* Issue the error given by GMSGID at RICHLOC, indicating that it occurred
5901 before TOKEN, which had the associated VALUE. */ 5969 before TOKEN, which had the associated VALUE. */
5942 if (val <= UCHAR_MAX && ISGRAPH (val)) 6010 if (val <= UCHAR_MAX && ISGRAPH (val))
5943 message = catenate_messages (gmsgid, " before %s'%c'"); 6011 message = catenate_messages (gmsgid, " before %s'%c'");
5944 else 6012 else
5945 message = catenate_messages (gmsgid, " before %s'\\x%x'"); 6013 message = catenate_messages (gmsgid, " before %s'\\x%x'");
5946 6014
5947 error_at_rich_loc (richloc, message, prefix, val); 6015 error_at (richloc, message, prefix, val);
5948 free (message); 6016 free (message);
5949 message = NULL; 6017 message = NULL;
5950 } 6018 }
5951 else if (token_type == CPP_CHAR_USERDEF 6019 else if (token_type == CPP_CHAR_USERDEF
5952 || token_type == CPP_WCHAR_USERDEF 6020 || token_type == CPP_WCHAR_USERDEF
5970 else if (token_type == CPP_NUMBER) 6038 else if (token_type == CPP_NUMBER)
5971 message = catenate_messages (gmsgid, " before numeric constant"); 6039 message = catenate_messages (gmsgid, " before numeric constant");
5972 else if (token_type == CPP_NAME) 6040 else if (token_type == CPP_NAME)
5973 { 6041 {
5974 message = catenate_messages (gmsgid, " before %qE"); 6042 message = catenate_messages (gmsgid, " before %qE");
5975 error_at_rich_loc (richloc, message, value); 6043 error_at (richloc, message, value);
5976 free (message); 6044 free (message);
5977 message = NULL; 6045 message = NULL;
5978 } 6046 }
5979 else if (token_type == CPP_PRAGMA) 6047 else if (token_type == CPP_PRAGMA)
5980 message = catenate_messages (gmsgid, " before %<#pragma%>"); 6048 message = catenate_messages (gmsgid, " before %<#pragma%>");
5983 else if (token_type == CPP_DECLTYPE) 6051 else if (token_type == CPP_DECLTYPE)
5984 message = catenate_messages (gmsgid, " before %<decltype%>"); 6052 message = catenate_messages (gmsgid, " before %<decltype%>");
5985 else if (token_type < N_TTYPES) 6053 else if (token_type < N_TTYPES)
5986 { 6054 {
5987 message = catenate_messages (gmsgid, " before %qs token"); 6055 message = catenate_messages (gmsgid, " before %qs token");
5988 error_at_rich_loc (richloc, message, cpp_type2name (token_type, token_flags)); 6056 error_at (richloc, message, cpp_type2name (token_type, token_flags));
5989 free (message); 6057 free (message);
5990 message = NULL; 6058 message = NULL;
5991 } 6059 }
5992 else 6060 else
5993 error_at_rich_loc (richloc, gmsgid); 6061 error_at (richloc, gmsgid);
5994 6062
5995 if (message) 6063 if (message)
5996 { 6064 {
5997 error_at_rich_loc (richloc, message); 6065 error_at (richloc, message);
5998 free (message); 6066 free (message);
5999 } 6067 }
6000 #undef catenate_messages 6068 #undef catenate_messages
6001 } 6069 }
6002 6070
6003 /* Return the gcc option code associated with the reason for a cpp 6071 /* Return the gcc option code associated with the reason for a cpp
6004 message, or 0 if none. */ 6072 message, or 0 if none. */
6005 6073
6006 static int 6074 static int
6007 c_option_controlling_cpp_error (int reason) 6075 c_option_controlling_cpp_diagnostic (enum cpp_warning_reason reason)
6008 { 6076 {
6009 const struct cpp_reason_option_codes_t *entry; 6077 const struct cpp_reason_option_codes_t *entry;
6010 6078
6011 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++) 6079 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++)
6012 { 6080 {
6014 return entry->option_code; 6082 return entry->option_code;
6015 } 6083 }
6016 return 0; 6084 return 0;
6017 } 6085 }
6018 6086
6019 /* Callback from cpp_error for PFILE to print diagnostics from the 6087 /* Callback from cpp_diagnostic for PFILE to print diagnostics from the
6020 preprocessor. The diagnostic is of type LEVEL, with REASON set 6088 preprocessor. The diagnostic is of type LEVEL, with REASON set
6021 to the reason code if LEVEL is represents a warning, at location 6089 to the reason code if LEVEL is represents a warning, at location
6022 RICHLOC unless this is after lexing and the compiler's location 6090 RICHLOC unless this is after lexing and the compiler's location
6023 should be used instead; MSG is the translated message and AP 6091 should be used instead; MSG is the translated message and AP
6024 the arguments. Returns true if a diagnostic was emitted, false 6092 the arguments. Returns true if a diagnostic was emitted, false
6025 otherwise. */ 6093 otherwise. */
6026 6094
6027 bool 6095 bool
6028 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason, 6096 c_cpp_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED,
6029 rich_location *richloc, 6097 enum cpp_diagnostic_level level,
6030 const char *msg, va_list *ap) 6098 enum cpp_warning_reason reason,
6099 rich_location *richloc,
6100 const char *msg, va_list *ap)
6031 { 6101 {
6032 diagnostic_info diagnostic; 6102 diagnostic_info diagnostic;
6033 diagnostic_t dlevel; 6103 diagnostic_t dlevel;
6034 bool save_warn_system_headers = global_dc->dc_warn_system_headers; 6104 bool save_warn_system_headers = global_dc->dc_warn_system_headers;
6035 bool ret; 6105 bool ret;
6065 break; 6135 break;
6066 default: 6136 default:
6067 gcc_unreachable (); 6137 gcc_unreachable ();
6068 } 6138 }
6069 if (done_lexing) 6139 if (done_lexing)
6070 richloc->set_range (line_table, 0, input_location, true); 6140 richloc->set_range (0, input_location, SHOW_RANGE_WITH_CARET);
6071 diagnostic_set_info_translated (&diagnostic, msg, ap, 6141 diagnostic_set_info_translated (&diagnostic, msg, ap,
6072 richloc, dlevel); 6142 richloc, dlevel);
6073 diagnostic_override_option_index (&diagnostic, 6143 diagnostic_override_option_index
6074 c_option_controlling_cpp_error (reason)); 6144 (&diagnostic,
6145 c_option_controlling_cpp_diagnostic (reason));
6075 ret = diagnostic_report_diagnostic (global_dc, &diagnostic); 6146 ret = diagnostic_report_diagnostic (global_dc, &diagnostic);
6076 if (level == CPP_DL_WARNING_SYSHDR) 6147 if (level == CPP_DL_WARNING_SYSHDR)
6077 global_dc->dc_warn_system_headers = save_warn_system_headers; 6148 global_dc->dc_warn_system_headers = save_warn_system_headers;
6078 return ret; 6149 return ret;
6079 } 6150 }
6099 return uc; 6170 return uc;
6100 } 6171 }
6101 6172
6102 /* Fold an offsetof-like expression. EXPR is a nested sequence of component 6173 /* Fold an offsetof-like expression. EXPR is a nested sequence of component
6103 references with an INDIRECT_REF of a constant at the bottom; much like the 6174 references with an INDIRECT_REF of a constant at the bottom; much like the
6104 traditional rendering of offsetof as a macro. Return the folded result. */ 6175 traditional rendering of offsetof as a macro. TYPE is the desired type of
6176 the whole expression. Return the folded result. */
6105 6177
6106 tree 6178 tree
6107 fold_offsetof_1 (tree expr, enum tree_code ctx) 6179 fold_offsetof (tree expr, tree type, enum tree_code ctx)
6108 { 6180 {
6109 tree base, off, t; 6181 tree base, off, t;
6110 tree_code code = TREE_CODE (expr); 6182 tree_code code = TREE_CODE (expr);
6111 switch (code) 6183 switch (code)
6112 { 6184 {
6127 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0))) 6199 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0)))
6128 { 6200 {
6129 error ("cannot apply %<offsetof%> to a non constant address"); 6201 error ("cannot apply %<offsetof%> to a non constant address");
6130 return error_mark_node; 6202 return error_mark_node;
6131 } 6203 }
6132 return TREE_OPERAND (expr, 0); 6204 return convert (type, TREE_OPERAND (expr, 0));
6133 6205
6134 case COMPONENT_REF: 6206 case COMPONENT_REF:
6135 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code); 6207 base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
6136 if (base == error_mark_node) 6208 if (base == error_mark_node)
6137 return base; 6209 return base;
6138 6210
6139 t = TREE_OPERAND (expr, 1); 6211 t = TREE_OPERAND (expr, 1);
6140 if (DECL_C_BIT_FIELD (t)) 6212 if (DECL_C_BIT_FIELD (t))
6147 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t)) 6219 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t))
6148 / BITS_PER_UNIT)); 6220 / BITS_PER_UNIT));
6149 break; 6221 break;
6150 6222
6151 case ARRAY_REF: 6223 case ARRAY_REF:
6152 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code); 6224 base = fold_offsetof (TREE_OPERAND (expr, 0), type, code);
6153 if (base == error_mark_node) 6225 if (base == error_mark_node)
6154 return base; 6226 return base;
6155 6227
6156 t = TREE_OPERAND (expr, 1); 6228 t = TREE_OPERAND (expr, 1);
6157 6229
6203 break; 6275 break;
6204 6276
6205 case COMPOUND_EXPR: 6277 case COMPOUND_EXPR:
6206 /* Handle static members of volatile structs. */ 6278 /* Handle static members of volatile structs. */
6207 t = TREE_OPERAND (expr, 1); 6279 t = TREE_OPERAND (expr, 1);
6208 gcc_assert (VAR_P (t)); 6280 gcc_checking_assert (VAR_P (get_base_address (t)));
6209 return fold_offsetof_1 (t); 6281 return fold_offsetof (t, type);
6210 6282
6211 default: 6283 default:
6212 gcc_unreachable (); 6284 gcc_unreachable ();
6213 } 6285 }
6214 6286
6287 if (!POINTER_TYPE_P (type))
6288 return size_binop (PLUS_EXPR, base, convert (type, off));
6215 return fold_build_pointer_plus (base, off); 6289 return fold_build_pointer_plus (base, off);
6216 } 6290 }
6217
6218 /* Likewise, but convert it to the return type of offsetof. */
6219
6220 tree
6221 fold_offsetof (tree expr)
6222 {
6223 return convert (size_type_node, fold_offsetof_1 (expr));
6224 }
6225
6226 6291
6227 /* *PTYPE is an incomplete array. Complete it with a domain based on 6292 /* *PTYPE is an incomplete array. Complete it with a domain based on
6228 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT 6293 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT
6229 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, 6294 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered,
6230 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */ 6295 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */
6365 6430
6366 *ptype = type; 6431 *ptype = type;
6367 return failure; 6432 return failure;
6368 } 6433 }
6369 6434
6435 /* INIT is an constructor of a structure with a flexible array member.
6436 Complete the flexible array member with a domain based on it's value. */
6437 void
6438 complete_flexible_array_elts (tree init)
6439 {
6440 tree elt, type;
6441
6442 if (init == NULL_TREE || TREE_CODE (init) != CONSTRUCTOR)
6443 return;
6444
6445 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init)))
6446 return;
6447
6448 elt = CONSTRUCTOR_ELTS (init)->last ().value;
6449 type = TREE_TYPE (elt);
6450 if (TREE_CODE (type) == ARRAY_TYPE
6451 && TYPE_SIZE (type) == NULL_TREE)
6452 complete_array_type (&TREE_TYPE (elt), elt, false);
6453 else
6454 complete_flexible_array_elts (elt);
6455 }
6456
6370 /* Like c_mark_addressable but don't check register qualifier. */ 6457 /* Like c_mark_addressable but don't check register qualifier. */
6371 void 6458 void
6372 c_common_mark_addressable_vec (tree t) 6459 c_common_mark_addressable_vec (tree t)
6373 { 6460 {
6374 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR) 6461 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR)
6393 tree 6480 tree
6394 builtin_type_for_size (int size, bool unsignedp) 6481 builtin_type_for_size (int size, bool unsignedp)
6395 { 6482 {
6396 tree type = c_common_type_for_size (size, unsignedp); 6483 tree type = c_common_type_for_size (size, unsignedp);
6397 return type ? type : error_mark_node; 6484 return type ? type : error_mark_node;
6485 }
6486
6487 /* Work out the size of the first argument of a call to
6488 __builtin_speculation_safe_value. Only pointers and integral types
6489 are permitted. Return -1 if the argument type is not supported or
6490 the size is too large; 0 if the argument type is a pointer or the
6491 size if it is integral. */
6492 static enum built_in_function
6493 speculation_safe_value_resolve_call (tree function, vec<tree, va_gc> *params)
6494 {
6495 /* Type of the argument. */
6496 tree type;
6497 int size;
6498
6499 if (vec_safe_is_empty (params))
6500 {
6501 error ("too few arguments to function %qE", function);
6502 return BUILT_IN_NONE;
6503 }
6504
6505 type = TREE_TYPE ((*params)[0]);
6506 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ())
6507 {
6508 /* Force array-to-pointer decay for C++. */
6509 (*params)[0] = default_conversion ((*params)[0]);
6510 type = TREE_TYPE ((*params)[0]);
6511 }
6512
6513 if (POINTER_TYPE_P (type))
6514 return BUILT_IN_SPECULATION_SAFE_VALUE_PTR;
6515
6516 if (!INTEGRAL_TYPE_P (type))
6517 goto incompatible;
6518
6519 if (!COMPLETE_TYPE_P (type))
6520 goto incompatible;
6521
6522 size = tree_to_uhwi (TYPE_SIZE_UNIT (type));
6523 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16)
6524 return ((enum built_in_function)
6525 ((int) BUILT_IN_SPECULATION_SAFE_VALUE_1 + exact_log2 (size)));
6526
6527 incompatible:
6528 /* Issue the diagnostic only if the argument is valid, otherwise
6529 it would be redundant at best and could be misleading. */
6530 if (type != error_mark_node)
6531 error ("operand type %qT is incompatible with argument %d of %qE",
6532 type, 1, function);
6533
6534 return BUILT_IN_NONE;
6535 }
6536
6537 /* Validate and coerce PARAMS, the arguments to ORIG_FUNCTION to fit
6538 the prototype for FUNCTION. The first argument is mandatory, a second
6539 argument, if present, must be type compatible with the first. */
6540 static bool
6541 speculation_safe_value_resolve_params (location_t loc, tree orig_function,
6542 vec<tree, va_gc> *params)
6543 {
6544 tree val;
6545
6546 if (params->length () == 0)
6547 {
6548 error_at (loc, "too few arguments to function %qE", orig_function);
6549 return false;
6550 }
6551
6552 else if (params->length () > 2)
6553 {
6554 error_at (loc, "too many arguments to function %qE", orig_function);
6555 return false;
6556 }
6557
6558 val = (*params)[0];
6559 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE)
6560 val = default_conversion (val);
6561 if (!(TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE
6562 || TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE))
6563 {
6564 error_at (loc,
6565 "expecting argument of type pointer or of type integer "
6566 "for argument 1");
6567 return false;
6568 }
6569 (*params)[0] = val;
6570
6571 if (params->length () == 2)
6572 {
6573 tree val2 = (*params)[1];
6574 if (TREE_CODE (TREE_TYPE (val2)) == ARRAY_TYPE)
6575 val2 = default_conversion (val2);
6576 if (!(TREE_TYPE (val) == TREE_TYPE (val2)
6577 || useless_type_conversion_p (TREE_TYPE (val), TREE_TYPE (val2))))
6578 {
6579 error_at (loc, "both arguments must be compatible");
6580 return false;
6581 }
6582 (*params)[1] = val2;
6583 }
6584
6585 return true;
6586 }
6587
6588 /* Cast the result of the builtin back to the type of the first argument,
6589 preserving any qualifiers that it might have. */
6590 static tree
6591 speculation_safe_value_resolve_return (tree first_param, tree result)
6592 {
6593 tree ptype = TREE_TYPE (first_param);
6594 tree rtype = TREE_TYPE (result);
6595 ptype = TYPE_MAIN_VARIANT (ptype);
6596
6597 if (tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype)))
6598 return convert (ptype, result);
6599
6600 return result;
6398 } 6601 }
6399 6602
6400 /* A helper function for resolve_overloaded_builtin in resolving the 6603 /* A helper function for resolve_overloaded_builtin in resolving the
6401 overloaded __sync_ builtins. Returns a positive power of 2 if the 6604 overloaded __sync_ builtins. Returns a positive power of 2 if the
6402 first operand of PARAMS is a pointer to a supported data type. 6605 first operand of PARAMS is a pointer to a supported data type.
6666 6869
6667 /* Check memory model parameters for validity. */ 6870 /* Check memory model parameters for validity. */
6668 for (x = n_param - n_model ; x < n_param; x++) 6871 for (x = n_param - n_model ; x < n_param; x++)
6669 { 6872 {
6670 tree p = (*params)[x]; 6873 tree p = (*params)[x];
6874 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
6875 {
6876 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
6877 function);
6878 return 0;
6879 }
6880 p = fold_for_warn (p);
6671 if (TREE_CODE (p) == INTEGER_CST) 6881 if (TREE_CODE (p) == INTEGER_CST)
6672 { 6882 {
6673 int i = tree_to_uhwi (p); 6883 /* memmodel_base masks the low 16 bits, thus ignore any bits above
6674 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST)) 6884 it by using TREE_INT_CST_LOW instead of tree_to_*hwi. Those high
6675 { 6885 bits will be checked later during expansion in target specific
6676 warning_at (loc, OPT_Winvalid_memory_model, 6886 way. */
6677 "invalid memory model argument %d of %qE", x + 1, 6887 if (memmodel_base (TREE_INT_CST_LOW (p)) >= MEMMODEL_LAST)
6678 function); 6888 warning_at (loc, OPT_Winvalid_memory_model,
6679 } 6889 "invalid memory model argument %d of %qE", x + 1,
6890 function);
6680 } 6891 }
6681 else 6892 }
6682 if (!INTEGRAL_TYPE_P (TREE_TYPE (p)))
6683 {
6684 error_at (loc, "non-integer memory model argument %d of %qE", x + 1,
6685 function);
6686 return 0;
6687 }
6688 }
6689 6893
6690 return size_0; 6894 return size_0;
6691 } 6895 }
6692 6896
6693 6897
7048 } 7252 }
7049 7253
7050 /* Handle BUILT_IN_NORMAL here. */ 7254 /* Handle BUILT_IN_NORMAL here. */
7051 switch (orig_code) 7255 switch (orig_code)
7052 { 7256 {
7257 case BUILT_IN_SPECULATION_SAFE_VALUE_N:
7258 {
7259 tree new_function, first_param, result;
7260 enum built_in_function fncode
7261 = speculation_safe_value_resolve_call (function, params);;
7262
7263 first_param = (*params)[0];
7264 if (fncode == BUILT_IN_NONE
7265 || !speculation_safe_value_resolve_params (loc, function, params))
7266 return error_mark_node;
7267
7268 if (targetm.have_speculation_safe_value (true))
7269 {
7270 new_function = builtin_decl_explicit (fncode);
7271 result = build_function_call_vec (loc, vNULL, new_function, params,
7272 NULL);
7273
7274 if (result == error_mark_node)
7275 return result;
7276
7277 return speculation_safe_value_resolve_return (first_param, result);
7278 }
7279 else
7280 {
7281 /* This target doesn't have, or doesn't need, active mitigation
7282 against incorrect speculative execution. Simply return the
7283 first parameter to the builtin. */
7284 if (!targetm.have_speculation_safe_value (false))
7285 /* The user has invoked __builtin_speculation_safe_value
7286 even though __HAVE_SPECULATION_SAFE_VALUE is not
7287 defined: emit a warning. */
7288 warning_at (input_location, 0,
7289 "this target does not define a speculation barrier; "
7290 "your program will still execute correctly, "
7291 "but incorrect speculation may not be be "
7292 "restricted");
7293
7294 /* If the optional second argument is present, handle any side
7295 effects now. */
7296 if (params->length () == 2
7297 && TREE_SIDE_EFFECTS ((*params)[1]))
7298 return build2 (COMPOUND_EXPR, TREE_TYPE (first_param),
7299 (*params)[1], first_param);
7300
7301 return first_param;
7302 }
7303 }
7304
7053 case BUILT_IN_ATOMIC_EXCHANGE: 7305 case BUILT_IN_ATOMIC_EXCHANGE:
7054 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: 7306 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE:
7055 case BUILT_IN_ATOMIC_LOAD: 7307 case BUILT_IN_ATOMIC_LOAD:
7056 case BUILT_IN_ATOMIC_STORE: 7308 case BUILT_IN_ATOMIC_STORE:
7057 { 7309 {
7218 t1 = TREE_TYPE (t1); 7470 t1 = TREE_TYPE (t1);
7219 t2 = TREE_TYPE (t2); 7471 t2 = TREE_TYPE (t2);
7220 7472
7221 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2); 7473 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2);
7222 7474
7223 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE) 7475 gcc_assert ((INTEGRAL_TYPE_P (t1)
7224 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE 7476 || c1 == REAL_TYPE
7477 || c1 == FIXED_POINT_TYPE)
7478 && (INTEGRAL_TYPE_P (t2)
7479 || c2 == REAL_TYPE
7225 || c2 == FIXED_POINT_TYPE)); 7480 || c2 == FIXED_POINT_TYPE));
7226 7481
7227 t1 = c_common_signed_type (t1); 7482 t1 = c_common_signed_type (t1);
7228 t2 = c_common_signed_type (t2); 7483 t2 = c_common_signed_type (t2);
7229 /* Equality works here because c_common_signed_type uses 7484 /* Equality works here because c_common_signed_type uses
7230 TYPE_MAIN_VARIANT. */ 7485 TYPE_MAIN_VARIANT. */
7231 if (t1 == t2) 7486 if (t1 == t2)
7232 return true; 7487 return true;
7233 if (opaque && c1 == c2 7488 if (opaque && c1 == c2
7234 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE) 7489 && (INTEGRAL_TYPE_P (t1) || c1 == REAL_TYPE)
7235 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)) 7490 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2))
7236 return true; 7491 return true;
7237 return false; 7492 return false;
7238 } 7493 }
7239 7494
7565 void 7820 void
7566 c_common_init_ts (void) 7821 c_common_init_ts (void)
7567 { 7822 {
7568 MARK_TS_TYPED (C_MAYBE_CONST_EXPR); 7823 MARK_TS_TYPED (C_MAYBE_CONST_EXPR);
7569 MARK_TS_TYPED (EXCESS_PRECISION_EXPR); 7824 MARK_TS_TYPED (EXCESS_PRECISION_EXPR);
7570 MARK_TS_TYPED (ARRAY_NOTATION_REF);
7571 } 7825 }
7572 7826
7573 /* Build a user-defined numeric literal out of an integer constant type VALUE 7827 /* Build a user-defined numeric literal out of an integer constant type VALUE
7574 with identifier SUFFIX. */ 7828 with identifier SUFFIX. */
7575 7829
7599 7853
7600 ret = !lvalue_p (*vecp); 7854 ret = !lvalue_p (*vecp);
7601 7855
7602 if (TREE_CODE (index) == INTEGER_CST) 7856 if (TREE_CODE (index) == INTEGER_CST)
7603 if (!tree_fits_uhwi_p (index) 7857 if (!tree_fits_uhwi_p (index)
7604 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type)) 7858 || maybe_ge (tree_to_uhwi (index), TYPE_VECTOR_SUBPARTS (type)))
7605 warning_at (loc, OPT_Warray_bounds, "index value is out of bound"); 7859 warning_at (loc, OPT_Warray_bounds, "index value is out of bound");
7606 7860
7607 /* We are building an ARRAY_REF so mark the vector as addressable 7861 /* We are building an ARRAY_REF so mark the vector as addressable
7608 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P 7862 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P
7609 for function parameters. */ 7863 for function parameters. */
7775 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */) 8029 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */)
7776 { 8030 {
7777 if (TREE_CODE (expr) == ADDR_EXPR) 8031 if (TREE_CODE (expr) == ADDR_EXPR)
7778 expr = TREE_OPERAND (expr, 0); 8032 expr = TREE_OPERAND (expr, 0);
7779 8033
8034 STRIP_ANY_LOCATION_WRAPPER (expr);
8035
7780 if (TREE_TYPE (expr) 8036 if (TREE_TYPE (expr)
7781 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE 8037 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE
7782 && TREE_CODE (expr) == FUNCTION_DECL 8038 && TREE_CODE (expr) == FUNCTION_DECL
7783 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids 8039 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids
7784 false positives for user-declared built-ins such as abs or 8040 false positives for user-declared built-ins such as abs or
7785 strlen, and for C++ operators new and delete. 8041 strlen, and for C++ operators new and delete.
7786 The c_decl_implicit() test avoids false positives for implicitly 8042 The c_decl_implicit() test avoids false positives for implicitly
7787 declared built-ins with library fallbacks (such as abs). */ 8043 declared built-ins with library fallbacks (such as abs). */
7788 && DECL_BUILT_IN (expr) 8044 && fndecl_built_in_p (expr)
7789 && DECL_IS_BUILTIN (expr) 8045 && DECL_IS_BUILTIN (expr)
7790 && !c_decl_implicit (expr) 8046 && !c_decl_implicit (expr)
7791 && !DECL_ASSEMBLER_NAME_SET_P (expr)) 8047 && !DECL_ASSEMBLER_NAME_SET_P (expr))
7792 { 8048 {
7793 if (loc == UNKNOWN_LOCATION) 8049 if (loc == UNKNOWN_LOCATION)
7807 than half of the address space. Return true if the size of the array 8063 than half of the address space. Return true if the size of the array
7808 is valid, false otherwise. TYPE is the type of the array and NAME is 8064 is valid, false otherwise. TYPE is the type of the array and NAME is
7809 the name of the array, or NULL_TREE for unnamed arrays. */ 8065 the name of the array, or NULL_TREE for unnamed arrays. */
7810 8066
7811 bool 8067 bool
7812 valid_array_size_p (location_t loc, tree type, tree name) 8068 valid_array_size_p (location_t loc, tree type, tree name, bool complain)
7813 { 8069 {
7814 if (type != error_mark_node 8070 if (type != error_mark_node
7815 && COMPLETE_TYPE_P (type) 8071 && COMPLETE_TYPE_P (type)
7816 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST 8072 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST
7817 && !valid_constant_size_p (TYPE_SIZE_UNIT (type))) 8073 && !valid_constant_size_p (TYPE_SIZE_UNIT (type)))
7818 { 8074 {
7819 if (name) 8075 if (complain)
7820 error_at (loc, "size of array %qE is too large", name); 8076 {
7821 else 8077 if (name)
7822 error_at (loc, "size of unnamed array is too large"); 8078 error_at (loc, "size of array %qE is too large", name);
8079 else
8080 error_at (loc, "size of unnamed array is too large");
8081 }
7823 return false; 8082 return false;
7824 } 8083 }
7825 return true; 8084 return true;
7826 } 8085 }
7827 8086
8107 { 8366 {
8108 fixit_hint *hint = richloc->get_last_fixit_hint (); 8367 fixit_hint *hint = richloc->get_last_fixit_hint ();
8109 location_t hint_loc = hint->get_start_loc (); 8368 location_t hint_loc = hint->get_start_loc ();
8110 location_t old_loc = richloc->get_loc (); 8369 location_t old_loc = richloc->get_loc ();
8111 8370
8112 richloc->set_range (line_table, 0, hint_loc, true); 8371 richloc->set_range (0, hint_loc, SHOW_RANGE_WITH_CARET);
8113 richloc->add_range (old_loc, false); 8372 richloc->add_range (old_loc);
8114 } 8373 }
8115 } 8374 }
8116 8375
8117 #if CHECKING_P 8376 #if CHECKING_P
8118 8377
8119 namespace selftest { 8378 namespace selftest {
8379
8380 /* Verify that fold_for_warn on error_mark_node is safe. */
8381
8382 static void
8383 test_fold_for_warn ()
8384 {
8385 ASSERT_EQ (error_mark_node, fold_for_warn (error_mark_node));
8386 }
8387
8388 /* Run all of the selftests within this file. */
8389
8390 static void
8391 c_common_c_tests ()
8392 {
8393 test_fold_for_warn ();
8394 }
8120 8395
8121 /* Run all of the tests within c-family. */ 8396 /* Run all of the tests within c-family. */
8122 8397
8123 void 8398 void
8124 c_family_tests (void) 8399 c_family_tests (void)
8125 { 8400 {
8401 c_common_c_tests ();
8126 c_format_c_tests (); 8402 c_format_c_tests ();
8403 c_indentation_c_tests ();
8404 c_pretty_print_c_tests ();
8405 c_spellcheck_cc_tests ();
8127 } 8406 }
8128 8407
8129 } // namespace selftest 8408 } // namespace selftest
8130 8409
8131 #endif /* #if CHECKING_P */ 8410 #endif /* #if CHECKING_P */
8163 for (unsigned int i = 0; i < LINEMAPS_ORDINARY_USED (line_table); i++) 8442 for (unsigned int i = 0; i < LINEMAPS_ORDINARY_USED (line_table); i++)
8164 { 8443 {
8165 const line_map_ordinary *ord_map 8444 const line_map_ordinary *ord_map
8166 = LINEMAPS_ORDINARY_MAP_AT (line_table, i); 8445 = LINEMAPS_ORDINARY_MAP_AT (line_table, i);
8167 8446
8168 const line_map_ordinary *from = INCLUDED_FROM (line_table, ord_map); 8447 if (const line_map_ordinary *from
8169 if (from) 8448 = linemap_included_from_linemap (line_table, ord_map))
8170 if (from->to_file == file) 8449 if (from->to_file == file)
8171 { 8450 {
8172 last_include_ord_map = from; 8451 last_include_ord_map = from;
8173 last_ord_map_after_include = NULL; 8452 last_ord_map_after_include = NULL;
8174 } 8453 }
8226 /* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n" 8505 /* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n"
8227 in a suitable location within the file of RICHLOC's primary 8506 in a suitable location within the file of RICHLOC's primary
8228 location. 8507 location.
8229 8508
8230 This function is idempotent: a header will be added at most once to 8509 This function is idempotent: a header will be added at most once to
8231 any given file. */ 8510 any given file.
8511
8512 If OVERRIDE_LOCATION is true, then if a fix-it is added and will be
8513 printed, then RICHLOC's primary location will be replaced by that of
8514 the fix-it hint (for use by "inform" notes where the location of the
8515 issue has already been reported). */
8232 8516
8233 void 8517 void
8234 maybe_add_include_fixit (rich_location *richloc, const char *header) 8518 maybe_add_include_fixit (rich_location *richloc, const char *header,
8519 bool override_location)
8235 { 8520 {
8236 location_t loc = richloc->get_loc (); 8521 location_t loc = richloc->get_loc ();
8237 const char *file = LOCATION_FILE (loc); 8522 const char *file = LOCATION_FILE (loc);
8238 if (!file) 8523 if (!file)
8239 return; 8524 return;
8257 return; 8542 return;
8258 8543
8259 char *text = xasprintf ("#include %s\n", header); 8544 char *text = xasprintf ("#include %s\n", header);
8260 richloc->add_fixit_insert_before (include_insert_loc, text); 8545 richloc->add_fixit_insert_before (include_insert_loc, text);
8261 free (text); 8546 free (text);
8547
8548 if (override_location && global_dc->show_caret)
8549 {
8550 /* Replace the primary location with that of the insertion point for the
8551 fix-it hint.
8552
8553 We use SHOW_LINES_WITHOUT_RANGE so that we don't meaningless print a
8554 caret for the insertion point (or colorize it).
8555
8556 Hence we print e.g.:
8557
8558 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8559 73 | # include <debug/vector>
8560 +++ |+#include <vector>
8561 74 | #endif
8562
8563 rather than:
8564
8565 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2
8566 73 | # include <debug/vector>
8567 +++ |+#include <vector>
8568 74 | #endif
8569 | ^
8570
8571 avoiding the caret on the first column of line 74. */
8572 richloc->set_range (0, include_insert_loc, SHOW_LINES_WITHOUT_RANGE);
8573 }
8574 }
8575
8576 /* Attempt to convert a braced array initializer list CTOR for array
8577 TYPE into a STRING_CST for convenience and efficiency. Return
8578 the converted string on success or the original ctor on failure. */
8579
8580 tree
8581 braced_list_to_string (tree type, tree ctor)
8582 {
8583 if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (type)))
8584 return ctor;
8585
8586 /* If the array has an explicit bound, use it to constrain the size
8587 of the string. If it doesn't, be sure to create a string that's
8588 as long as implied by the index of the last zero specified via
8589 a designator, as in:
8590 const char a[] = { [7] = 0 }; */
8591 unsigned HOST_WIDE_INT maxelts = tree_to_uhwi (TYPE_SIZE_UNIT (type));
8592 maxelts /= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type)));
8593
8594 /* Avoid converting initializers for zero-length arrays. */
8595 if (!maxelts)
8596 return ctor;
8597
8598 unsigned HOST_WIDE_INT nelts = CONSTRUCTOR_NELTS (ctor);
8599
8600 auto_vec<char> str;
8601 str.reserve (nelts + 1);
8602
8603 unsigned HOST_WIDE_INT i;
8604 tree index, value;
8605
8606 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, index, value)
8607 {
8608 unsigned HOST_WIDE_INT idx = i;
8609 if (index)
8610 {
8611 if (!tree_fits_uhwi_p (index))
8612 return ctor;
8613 idx = tree_to_uhwi (index);
8614 }
8615
8616 /* auto_vec is limited to UINT_MAX elements. */
8617 if (idx > UINT_MAX)
8618 return ctor;
8619
8620 /* Avoid non-constant initializers. */
8621 if (!tree_fits_shwi_p (value))
8622 return ctor;
8623
8624 /* Skip over embedded nuls except the last one (initializer
8625 elements are in ascending order of indices). */
8626 HOST_WIDE_INT val = tree_to_shwi (value);
8627 if (!val && i + 1 < nelts)
8628 continue;
8629
8630 if (idx < str.length())
8631 return ctor;
8632
8633 /* Bail if the CTOR has a block of more than 256 embedded nuls
8634 due to implicitly initialized elements. */
8635 unsigned nchars = (idx - str.length ()) + 1;
8636 if (nchars > 256)
8637 return ctor;
8638
8639 if (nchars > 1)
8640 {
8641 str.reserve (idx);
8642 str.quick_grow_cleared (idx);
8643 }
8644
8645 if (idx >= maxelts)
8646 return ctor;
8647
8648 str.safe_insert (idx, val);
8649 }
8650
8651 /* Append a nul string termination. */
8652 if (str.length () < maxelts)
8653 str.safe_push (0);
8654
8655 /* Build a STRING_CST with the same type as the array. */
8656 tree res = build_string (str.length (), str.begin ());
8657 TREE_TYPE (res) = type;
8658 return res;
8262 } 8659 }
8263 8660
8264 #include "gt-c-family-c-common.h" 8661 #include "gt-c-family-c-common.h"