Mercurial > hg > CbC > CbC_gcc
comparison gcc/c-family/c-common.c @ 132:d34655255c78
update gcc-8.2
author | mir3636 |
---|---|
date | Thu, 25 Oct 2018 10:21:07 +0900 |
parents | bdf41c9fa0b7 84e7813d76e9 |
children | 351920fa3827 |
comparison
equal
deleted
inserted
replaced
130:e108057fa461 | 132:d34655255c78 |
---|---|
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 }, |
378 #endif | 380 #endif |
379 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY }, | 381 { "__builtin_choose_expr", RID_CHOOSE_EXPR, D_CONLY }, |
380 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY }, | 382 { "__builtin_complex", RID_BUILTIN_COMPLEX, D_CONLY }, |
381 { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY }, | 383 { "__builtin_launder", RID_BUILTIN_LAUNDER, D_CXXONLY }, |
382 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 }, | 384 { "__builtin_shuffle", RID_BUILTIN_SHUFFLE, 0 }, |
385 { "__builtin_tgmath", RID_BUILTIN_TGMATH, D_CONLY }, | |
383 { "__builtin_offsetof", RID_OFFSETOF, 0 }, | 386 { "__builtin_offsetof", RID_OFFSETOF, 0 }, |
384 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY }, | 387 { "__builtin_types_compatible_p", RID_TYPES_COMPATIBLE_P, D_CONLY }, |
385 { "__builtin_va_arg", RID_VA_ARG, 0 }, | 388 { "__builtin_va_arg", RID_VA_ARG, 0 }, |
386 { "__complex", RID_COMPLEX, 0 }, | 389 { "__complex", RID_COMPLEX, 0 }, |
387 { "__complex__", RID_COMPLEX, 0 }, | 390 { "__complex__", RID_COMPLEX, 0 }, |
876 substr_loc.get_end_idx (), | 879 substr_loc.get_end_idx (), |
877 out_loc); | 880 out_loc); |
878 } | 881 } |
879 | 882 |
880 | 883 |
881 /* Fold X for consideration by one of the warning functions when checking | |
882 whether an expression has a constant value. */ | |
883 | |
884 tree | |
885 fold_for_warn (tree x) | |
886 { | |
887 if (c_dialect_cxx ()) | |
888 return c_fully_fold (x, /*for_init*/false, /*maybe_constp*/NULL); | |
889 else | |
890 /* The C front-end has already folded X appropriately. */ | |
891 return x; | |
892 } | |
893 | |
894 /* Return true iff T is a boolean promoted to int. */ | 884 /* Return true iff T is a boolean promoted to int. */ |
895 | 885 |
896 bool | 886 bool |
897 bool_promoted_to_int_p (tree t) | 887 bool_promoted_to_int_p (tree t) |
898 { | 888 { |
949 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) | 939 && tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2))) |
950 return true; | 940 return true; |
951 | 941 |
952 convertible_lax = | 942 convertible_lax = |
953 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)) | 943 (tree_int_cst_equal (TYPE_SIZE (t1), TYPE_SIZE (t2)) |
954 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE || | 944 && (TREE_CODE (TREE_TYPE (t1)) != REAL_TYPE |
955 TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)) | 945 || known_eq (TYPE_VECTOR_SUBPARTS (t1), |
946 TYPE_VECTOR_SUBPARTS (t2))) | |
956 && (INTEGRAL_TYPE_P (TREE_TYPE (t1)) | 947 && (INTEGRAL_TYPE_P (TREE_TYPE (t1)) |
957 == INTEGRAL_TYPE_P (TREE_TYPE (t2)))); | 948 == INTEGRAL_TYPE_P (TREE_TYPE (t2)))); |
958 | 949 |
959 if (!convertible_lax || flag_lax_vector_conversions) | 950 if (!convertible_lax || flag_lax_vector_conversions) |
960 return convertible_lax; | 951 return convertible_lax; |
961 | 952 |
962 if (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2) | 953 if (known_eq (TYPE_VECTOR_SUBPARTS (t1), TYPE_VECTOR_SUBPARTS (t2)) |
963 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))) | 954 && lang_hooks.types_compatible_p (TREE_TYPE (t1), TREE_TYPE (t2))) |
964 return true; | 955 return true; |
965 | 956 |
966 if (emit_lax_note && !emitted_lax_note) | 957 if (emit_lax_note && !emitted_lax_note) |
967 { | 958 { |
1025 error_at (loc, "__builtin_shuffle argument vectors must be of " | 1016 error_at (loc, "__builtin_shuffle argument vectors must be of " |
1026 "the same type"); | 1017 "the same type"); |
1027 return error_mark_node; | 1018 return error_mark_node; |
1028 } | 1019 } |
1029 | 1020 |
1030 if (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)) | 1021 if (maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v0)), |
1031 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)) | 1022 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))) |
1032 && TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)) | 1023 && maybe_ne (TYPE_VECTOR_SUBPARTS (TREE_TYPE (v1)), |
1033 != TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask))) | 1024 TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask)))) |
1034 { | 1025 { |
1035 if (complain) | 1026 if (complain) |
1036 error_at (loc, "__builtin_shuffle number of elements of the " | 1027 error_at (loc, "__builtin_shuffle number of elements of the " |
1037 "argument vector(s) and the mask vector should " | 1028 "argument vector(s) and the mask vector should " |
1038 "be the same"); | 1029 "be the same"); |
2287 inner_mode = GET_MODE_INNER (mode); | 2278 inner_mode = GET_MODE_INNER (mode); |
2288 inner_type = c_common_type_for_mode (inner_mode, unsignedp); | 2279 inner_type = c_common_type_for_mode (inner_mode, unsignedp); |
2289 if (inner_type != NULL_TREE) | 2280 if (inner_type != NULL_TREE) |
2290 return build_complex_type (inner_type); | 2281 return build_complex_type (inner_type); |
2291 } | 2282 } |
2292 else if (VECTOR_MODE_P (mode)) | 2283 else if (GET_MODE_CLASS (mode) == MODE_VECTOR_BOOL |
2284 && valid_vector_subparts_p (GET_MODE_NUNITS (mode))) | |
2285 { | |
2286 unsigned int elem_bits = vector_element_size (GET_MODE_BITSIZE (mode), | |
2287 GET_MODE_NUNITS (mode)); | |
2288 tree bool_type = build_nonstandard_boolean_type (elem_bits); | |
2289 return build_vector_type_for_mode (bool_type, mode); | |
2290 } | |
2291 else if (VECTOR_MODE_P (mode) | |
2292 && valid_vector_subparts_p (GET_MODE_NUNITS (mode))) | |
2293 { | 2293 { |
2294 machine_mode inner_mode = GET_MODE_INNER (mode); | 2294 machine_mode inner_mode = GET_MODE_INNER (mode); |
2295 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp); | 2295 tree inner_type = c_common_type_for_mode (inner_mode, unsignedp); |
2296 if (inner_type != NULL_TREE) | 2296 if (inner_type != NULL_TREE) |
2297 return build_vector_type_for_mode (inner_type, mode); | 2297 return build_vector_type_for_mode (inner_type, mode); |
2701 case BIT_XOR_EXPR: | 2701 case BIT_XOR_EXPR: |
2702 opname = "^"; break; | 2702 opname = "^"; break; |
2703 default: | 2703 default: |
2704 gcc_unreachable (); | 2704 gcc_unreachable (); |
2705 } | 2705 } |
2706 error_at_rich_loc (richloc, | 2706 error_at (richloc, |
2707 "invalid operands to binary %s (have %qT and %qT)", | 2707 "invalid operands to binary %s (have %qT and %qT)", |
2708 opname, type0, type1); | 2708 opname, type0, type1); |
2709 } | 2709 } |
2710 | 2710 |
2711 /* Given an expression as a tree, return its original type. Do this | 2711 /* Given an expression as a tree, return its original type. Do this |
2712 by stripping any conversion that preserves the sign and precision. */ | 2712 by stripping any conversion that preserves the sign and precision. */ |
2713 static tree | 2713 static tree |
3300 case ADDR_EXPR: | 3300 case ADDR_EXPR: |
3301 { | 3301 { |
3302 tree inner = TREE_OPERAND (expr, 0); | 3302 tree inner = TREE_OPERAND (expr, 0); |
3303 if (decl_with_nonnull_addr_p (inner)) | 3303 if (decl_with_nonnull_addr_p (inner)) |
3304 { | 3304 { |
3305 /* Common Ada/Pascal programmer's mistake. */ | 3305 /* Common Ada programmer's mistake. */ |
3306 warning_at (location, | 3306 warning_at (location, |
3307 OPT_Waddress, | 3307 OPT_Waddress, |
3308 "the address of %qD will always evaluate as %<true%>", | 3308 "the address of %qD will always evaluate as %<true%>", |
3309 inner); | 3309 inner); |
3310 return truthvalue_true_node; | 3310 return truthvalue_true_node; |
3323 false); | 3323 false); |
3324 goto ret; | 3324 goto ret; |
3325 | 3325 |
3326 case NEGATE_EXPR: | 3326 case NEGATE_EXPR: |
3327 case ABS_EXPR: | 3327 case ABS_EXPR: |
3328 case ABSU_EXPR: | |
3328 case FLOAT_EXPR: | 3329 case FLOAT_EXPR: |
3329 case EXCESS_PRECISION_EXPR: | 3330 case EXCESS_PRECISION_EXPR: |
3330 /* These don't change whether an object is nonzero or zero. */ | 3331 /* These don't change whether an object is nonzero or zero. */ |
3331 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0)); | 3332 return c_common_truthvalue_conversion (location, TREE_OPERAND (expr, 0)); |
3332 | 3333 |
3941 #include "builtins.def" | 3942 #include "builtins.def" |
3942 | 3943 |
3943 targetm.init_builtins (); | 3944 targetm.init_builtins (); |
3944 | 3945 |
3945 build_common_builtin_nodes (); | 3946 build_common_builtin_nodes (); |
3946 | |
3947 if (flag_cilkplus) | |
3948 cilk_init_builtins (); | |
3949 } | 3947 } |
3950 | 3948 |
3951 /* Like get_identifier, but avoid warnings about null arguments when | 3949 /* Like get_identifier, but avoid warnings about null arguments when |
3952 the argument may be NULL for targets where GCC lacks stdint.h type | 3950 the argument may be NULL for targets where GCC lacks stdint.h type |
3953 information. */ | 3951 information. */ |
4910 add_stmt (build_stmt (loc, LABEL_EXPR, t)); | 4908 add_stmt (build_stmt (loc, LABEL_EXPR, t)); |
4911 } | 4909 } |
4912 return error_mark_node; | 4910 return error_mark_node; |
4913 } | 4911 } |
4914 | 4912 |
4913 /* Subroutine of c_switch_covers_all_cases_p, called via | |
4914 splay_tree_foreach. Return 1 if it doesn't cover all the cases. | |
4915 ARGS[0] is initially NULL and after the first iteration is the | |
4916 so far highest case label. ARGS[1] is the minimum of SWITCH_COND's | |
4917 type. */ | |
4918 | |
4919 static int | |
4920 c_switch_covers_all_cases_p_1 (splay_tree_node node, void *data) | |
4921 { | |
4922 tree label = (tree) node->value; | |
4923 tree *args = (tree *) data; | |
4924 | |
4925 /* If there is a default case, we shouldn't have called this. */ | |
4926 gcc_assert (CASE_LOW (label)); | |
4927 | |
4928 if (args[0] == NULL_TREE) | |
4929 { | |
4930 if (wi::to_widest (args[1]) < wi::to_widest (CASE_LOW (label))) | |
4931 return 1; | |
4932 } | |
4933 else if (wi::add (wi::to_widest (args[0]), 1) | |
4934 != wi::to_widest (CASE_LOW (label))) | |
4935 return 1; | |
4936 if (CASE_HIGH (label)) | |
4937 args[0] = CASE_HIGH (label); | |
4938 else | |
4939 args[0] = CASE_LOW (label); | |
4940 return 0; | |
4941 } | |
4942 | |
4943 /* Return true if switch with CASES and switch condition with type | |
4944 covers all possible values in the case labels. */ | |
4945 | |
4946 bool | |
4947 c_switch_covers_all_cases_p (splay_tree cases, tree type) | |
4948 { | |
4949 /* If there is default:, this is always the case. */ | |
4950 splay_tree_node default_node | |
4951 = splay_tree_lookup (cases, (splay_tree_key) NULL); | |
4952 if (default_node) | |
4953 return true; | |
4954 | |
4955 if (!INTEGRAL_TYPE_P (type)) | |
4956 return false; | |
4957 | |
4958 tree args[2] = { NULL_TREE, TYPE_MIN_VALUE (type) }; | |
4959 if (splay_tree_foreach (cases, c_switch_covers_all_cases_p_1, args)) | |
4960 return false; | |
4961 | |
4962 /* If there are no cases at all, or if the highest case label | |
4963 is smaller than TYPE_MAX_VALUE, return false. */ | |
4964 if (args[0] == NULL_TREE | |
4965 || wi::to_widest (args[0]) < wi::to_widest (TYPE_MAX_VALUE (type))) | |
4966 return false; | |
4967 | |
4968 return true; | |
4969 } | |
4970 | |
4915 /* Finish an expression taking the address of LABEL (an | 4971 /* Finish an expression taking the address of LABEL (an |
4916 IDENTIFIER_NODE). Returns an expression for the address. | 4972 IDENTIFIER_NODE). Returns an expression for the address. |
4917 | 4973 |
4918 LOC is the location for the expression returned. */ | 4974 LOC is the location for the expression returned. */ |
4919 | 4975 |
5265 && (warn_strict_null_sentinel || null_node != sentinel)) | 5321 && (warn_strict_null_sentinel || null_node != sentinel)) |
5266 warning (OPT_Wformat_, "missing sentinel in function call"); | 5322 warning (OPT_Wformat_, "missing sentinel in function call"); |
5267 } | 5323 } |
5268 } | 5324 } |
5269 | 5325 |
5270 /* Check that the same argument isn't passed to restrict arguments | 5326 /* Check that the same argument isn't passed to two or more |
5271 and other arguments. */ | 5327 restrict-qualified formal and issue a -Wrestrict warning |
5272 | 5328 if it is. Return true if a warning has been issued. */ |
5273 static void | 5329 |
5330 static bool | |
5274 check_function_restrict (const_tree fndecl, const_tree fntype, | 5331 check_function_restrict (const_tree fndecl, const_tree fntype, |
5275 int nargs, tree *argarray) | 5332 int nargs, tree *argarray) |
5276 { | 5333 { |
5277 int i; | 5334 int i; |
5278 tree parms; | 5335 tree parms = TYPE_ARG_TYPES (fntype); |
5279 | 5336 |
5280 if (fndecl | 5337 if (fndecl |
5281 && TREE_CODE (fndecl) == FUNCTION_DECL | 5338 && TREE_CODE (fndecl) == FUNCTION_DECL) |
5282 && DECL_ARGUMENTS (fndecl)) | 5339 { |
5283 parms = DECL_ARGUMENTS (fndecl); | 5340 /* Avoid diagnosing calls built-ins with a zero size/bound |
5284 else | 5341 here. They are checked in more detail elsewhere. */ |
5285 parms = TYPE_ARG_TYPES (fntype); | 5342 if (fndecl_built_in_p (fndecl, BUILT_IN_NORMAL) |
5343 && nargs == 3 | |
5344 && TREE_CODE (argarray[2]) == INTEGER_CST | |
5345 && integer_zerop (argarray[2])) | |
5346 return false; | |
5347 | |
5348 if (DECL_ARGUMENTS (fndecl)) | |
5349 parms = DECL_ARGUMENTS (fndecl); | |
5350 } | |
5286 | 5351 |
5287 for (i = 0; i < nargs; i++) | 5352 for (i = 0; i < nargs; i++) |
5288 TREE_VISITED (argarray[i]) = 0; | 5353 TREE_VISITED (argarray[i]) = 0; |
5354 | |
5355 bool warned = false; | |
5289 | 5356 |
5290 for (i = 0; i < nargs && parms && parms != void_list_node; i++) | 5357 for (i = 0; i < nargs && parms && parms != void_list_node; i++) |
5291 { | 5358 { |
5292 tree type; | 5359 tree type; |
5293 if (TREE_CODE (parms) == PARM_DECL) | 5360 if (TREE_CODE (parms) == PARM_DECL) |
5301 parms = TREE_CHAIN (parms); | 5368 parms = TREE_CHAIN (parms); |
5302 } | 5369 } |
5303 if (POINTER_TYPE_P (type) | 5370 if (POINTER_TYPE_P (type) |
5304 && TYPE_RESTRICT (type) | 5371 && TYPE_RESTRICT (type) |
5305 && !TYPE_READONLY (TREE_TYPE (type))) | 5372 && !TYPE_READONLY (TREE_TYPE (type))) |
5306 warn_for_restrict (i, argarray, nargs); | 5373 warned |= warn_for_restrict (i, argarray, nargs); |
5307 } | 5374 } |
5308 | 5375 |
5309 for (i = 0; i < nargs; i++) | 5376 for (i = 0; i < nargs; i++) |
5310 TREE_VISITED (argarray[i]) = 0; | 5377 TREE_VISITED (argarray[i]) = 0; |
5378 | |
5379 return warned; | |
5311 } | 5380 } |
5312 | 5381 |
5313 /* Helper for check_function_nonnull; given a list of operands which | 5382 /* Helper for check_function_nonnull; given a list of operands which |
5314 must be non-null in ARGS, determine if operand PARAM_NUM should be | 5383 must be non-null in ARGS, determine if operand PARAM_NUM should be |
5315 checked. */ | 5384 checked. */ |
5345 list (which means to check every pointer argument). */ | 5414 list (which means to check every pointer argument). */ |
5346 | 5415 |
5347 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE) | 5416 if (TREE_CODE (TREE_TYPE (param)) != POINTER_TYPE) |
5348 return; | 5417 return; |
5349 | 5418 |
5350 /* When not optimizing diagnose the simple cases of null arguments. | 5419 /* Diagnose the simple cases of null arguments. */ |
5351 When optimization is enabled defer the checking until expansion | 5420 if (integer_zerop (fold_for_warn (param))) |
5352 when more cases can be detected. */ | |
5353 if (integer_zerop (param)) | |
5354 { | 5421 { |
5355 warning_at (pctx->loc, OPT_Wnonnull, "null argument where non-null " | 5422 warning_at (pctx->loc, OPT_Wnonnull, "null argument where non-null " |
5356 "required (argument %lu)", (unsigned long) param_num); | 5423 "required (argument %lu)", (unsigned long) param_num); |
5357 pctx->warned_p = true; | 5424 pctx->warned_p = true; |
5358 } | 5425 } |
5365 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp) | 5432 get_nonnull_operand (tree arg_num_expr, unsigned HOST_WIDE_INT *valp) |
5366 { | 5433 { |
5367 /* Verify the arg number is a small constant. */ | 5434 /* Verify the arg number is a small constant. */ |
5368 if (tree_fits_uhwi_p (arg_num_expr)) | 5435 if (tree_fits_uhwi_p (arg_num_expr)) |
5369 { | 5436 { |
5370 *valp = TREE_INT_CST_LOW (arg_num_expr); | 5437 *valp = tree_to_uhwi (arg_num_expr); |
5371 return true; | 5438 return true; |
5372 } | 5439 } |
5373 else | 5440 else |
5374 return false; | 5441 return false; |
5375 } | 5442 } |
5546 return true; | 5613 return true; |
5547 } | 5614 } |
5548 | 5615 |
5549 | 5616 |
5550 /* Check for valid arguments being passed to a function with FNTYPE. | 5617 /* Check for valid arguments being passed to a function with FNTYPE. |
5551 There are NARGS arguments in the array ARGARRAY. LOC should be used for | 5618 There are NARGS arguments in the array ARGARRAY. LOC should be used |
5552 diagnostics. Return true if -Wnonnull warning has been diagnosed. */ | 5619 for diagnostics. Return true if either -Wnonnull or -Wrestrict has |
5620 been issued. */ | |
5621 | |
5553 bool | 5622 bool |
5554 check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype, | 5623 check_function_arguments (location_t loc, const_tree fndecl, const_tree fntype, |
5555 int nargs, tree *argarray, vec<location_t> *arglocs) | 5624 int nargs, tree *argarray, vec<location_t> *arglocs) |
5556 { | 5625 { |
5557 bool warned_p = false; | 5626 bool warned_p = false; |
5570 | 5639 |
5571 if (warn_format) | 5640 if (warn_format) |
5572 check_function_sentinel (fntype, nargs, argarray); | 5641 check_function_sentinel (fntype, nargs, argarray); |
5573 | 5642 |
5574 if (warn_restrict) | 5643 if (warn_restrict) |
5575 check_function_restrict (fndecl, fntype, nargs, argarray); | 5644 warned_p |= check_function_restrict (fndecl, fntype, nargs, argarray); |
5576 return warned_p; | 5645 return warned_p; |
5577 } | 5646 } |
5578 | 5647 |
5579 /* Generic argument checking recursion routine. PARAM is the argument to | 5648 /* Generic argument checking recursion routine. PARAM is the argument to |
5580 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked | 5649 be checked. PARAM_NUM is the number of the argument. CALLBACK is invoked |
5696 | 5765 |
5697 bool | 5766 bool |
5698 check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc, | 5767 check_builtin_function_arguments (location_t loc, vec<location_t> arg_loc, |
5699 tree fndecl, int nargs, tree *args) | 5768 tree fndecl, int nargs, tree *args) |
5700 { | 5769 { |
5701 if (!DECL_BUILT_IN (fndecl) | 5770 if (!fndecl_built_in_p (fndecl, BUILT_IN_NORMAL)) |
5702 || DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_NORMAL) | |
5703 return true; | 5771 return true; |
5704 | 5772 |
5705 switch (DECL_FUNCTION_CODE (fndecl)) | 5773 switch (DECL_FUNCTION_CODE (fndecl)) |
5706 { | 5774 { |
5707 case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX: | 5775 case BUILT_IN_ALLOCA_WITH_ALIGN_AND_MAX: |
5897 The caller is responsible for deleting the returned pointer. */ | 5965 The caller is responsible for deleting the returned pointer. */ |
5898 | 5966 |
5899 static char * | 5967 static char * |
5900 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size) | 5968 catenate_strings (const char *lhs, const char *rhs_start, int rhs_size) |
5901 { | 5969 { |
5902 const int lhs_size = strlen (lhs); | 5970 const size_t lhs_size = strlen (lhs); |
5903 char *result = XNEWVEC (char, lhs_size + rhs_size); | 5971 char *result = XNEWVEC (char, lhs_size + rhs_size); |
5904 strncpy (result, lhs, lhs_size); | 5972 memcpy (result, lhs, lhs_size); |
5905 strncpy (result + lhs_size, rhs_start, rhs_size); | 5973 memcpy (result + lhs_size, rhs_start, rhs_size); |
5906 return result; | 5974 return result; |
5907 } | 5975 } |
5908 | 5976 |
5909 /* Issue the error given by GMSGID at RICHLOC, indicating that it occurred | 5977 /* Issue the error given by GMSGID at RICHLOC, indicating that it occurred |
5910 before TOKEN, which had the associated VALUE. */ | 5978 before TOKEN, which had the associated VALUE. */ |
5951 if (val <= UCHAR_MAX && ISGRAPH (val)) | 6019 if (val <= UCHAR_MAX && ISGRAPH (val)) |
5952 message = catenate_messages (gmsgid, " before %s'%c'"); | 6020 message = catenate_messages (gmsgid, " before %s'%c'"); |
5953 else | 6021 else |
5954 message = catenate_messages (gmsgid, " before %s'\\x%x'"); | 6022 message = catenate_messages (gmsgid, " before %s'\\x%x'"); |
5955 | 6023 |
5956 error_at_rich_loc (richloc, message, prefix, val); | 6024 error_at (richloc, message, prefix, val); |
5957 free (message); | 6025 free (message); |
5958 message = NULL; | 6026 message = NULL; |
5959 } | 6027 } |
5960 else if (token_type == CPP_CHAR_USERDEF | 6028 else if (token_type == CPP_CHAR_USERDEF |
5961 || token_type == CPP_WCHAR_USERDEF | 6029 || token_type == CPP_WCHAR_USERDEF |
5979 else if (token_type == CPP_NUMBER) | 6047 else if (token_type == CPP_NUMBER) |
5980 message = catenate_messages (gmsgid, " before numeric constant"); | 6048 message = catenate_messages (gmsgid, " before numeric constant"); |
5981 else if (token_type == CPP_NAME) | 6049 else if (token_type == CPP_NAME) |
5982 { | 6050 { |
5983 message = catenate_messages (gmsgid, " before %qE"); | 6051 message = catenate_messages (gmsgid, " before %qE"); |
5984 error_at_rich_loc (richloc, message, value); | 6052 error_at (richloc, message, value); |
5985 free (message); | 6053 free (message); |
5986 message = NULL; | 6054 message = NULL; |
5987 } | 6055 } |
5988 else if (token_type == CPP_PRAGMA) | 6056 else if (token_type == CPP_PRAGMA) |
5989 message = catenate_messages (gmsgid, " before %<#pragma%>"); | 6057 message = catenate_messages (gmsgid, " before %<#pragma%>"); |
5992 else if (token_type == CPP_DECLTYPE) | 6060 else if (token_type == CPP_DECLTYPE) |
5993 message = catenate_messages (gmsgid, " before %<decltype%>"); | 6061 message = catenate_messages (gmsgid, " before %<decltype%>"); |
5994 else if (token_type < N_TTYPES) | 6062 else if (token_type < N_TTYPES) |
5995 { | 6063 { |
5996 message = catenate_messages (gmsgid, " before %qs token"); | 6064 message = catenate_messages (gmsgid, " before %qs token"); |
5997 error_at_rich_loc (richloc, message, cpp_type2name (token_type, token_flags)); | 6065 error_at (richloc, message, cpp_type2name (token_type, token_flags)); |
5998 free (message); | 6066 free (message); |
5999 message = NULL; | 6067 message = NULL; |
6000 } | 6068 } |
6001 else | 6069 else |
6002 error_at_rich_loc (richloc, gmsgid); | 6070 error_at (richloc, gmsgid); |
6003 | 6071 |
6004 if (message) | 6072 if (message) |
6005 { | 6073 { |
6006 error_at_rich_loc (richloc, message); | 6074 error_at (richloc, message); |
6007 free (message); | 6075 free (message); |
6008 } | 6076 } |
6009 #undef catenate_messages | 6077 #undef catenate_messages |
6010 } | 6078 } |
6011 | 6079 |
6012 /* Return the gcc option code associated with the reason for a cpp | 6080 /* Return the gcc option code associated with the reason for a cpp |
6013 message, or 0 if none. */ | 6081 message, or 0 if none. */ |
6014 | 6082 |
6015 static int | 6083 static int |
6016 c_option_controlling_cpp_error (int reason) | 6084 c_option_controlling_cpp_diagnostic (enum cpp_warning_reason reason) |
6017 { | 6085 { |
6018 const struct cpp_reason_option_codes_t *entry; | 6086 const struct cpp_reason_option_codes_t *entry; |
6019 | 6087 |
6020 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++) | 6088 for (entry = cpp_reason_option_codes; entry->reason != CPP_W_NONE; entry++) |
6021 { | 6089 { |
6023 return entry->option_code; | 6091 return entry->option_code; |
6024 } | 6092 } |
6025 return 0; | 6093 return 0; |
6026 } | 6094 } |
6027 | 6095 |
6028 /* Callback from cpp_error for PFILE to print diagnostics from the | 6096 /* Callback from cpp_diagnostic for PFILE to print diagnostics from the |
6029 preprocessor. The diagnostic is of type LEVEL, with REASON set | 6097 preprocessor. The diagnostic is of type LEVEL, with REASON set |
6030 to the reason code if LEVEL is represents a warning, at location | 6098 to the reason code if LEVEL is represents a warning, at location |
6031 RICHLOC unless this is after lexing and the compiler's location | 6099 RICHLOC unless this is after lexing and the compiler's location |
6032 should be used instead; MSG is the translated message and AP | 6100 should be used instead; MSG is the translated message and AP |
6033 the arguments. Returns true if a diagnostic was emitted, false | 6101 the arguments. Returns true if a diagnostic was emitted, false |
6034 otherwise. */ | 6102 otherwise. */ |
6035 | 6103 |
6036 bool | 6104 bool |
6037 c_cpp_error (cpp_reader *pfile ATTRIBUTE_UNUSED, int level, int reason, | 6105 c_cpp_diagnostic (cpp_reader *pfile ATTRIBUTE_UNUSED, |
6038 rich_location *richloc, | 6106 enum cpp_diagnostic_level level, |
6039 const char *msg, va_list *ap) | 6107 enum cpp_warning_reason reason, |
6108 rich_location *richloc, | |
6109 const char *msg, va_list *ap) | |
6040 { | 6110 { |
6041 diagnostic_info diagnostic; | 6111 diagnostic_info diagnostic; |
6042 diagnostic_t dlevel; | 6112 diagnostic_t dlevel; |
6043 bool save_warn_system_headers = global_dc->dc_warn_system_headers; | 6113 bool save_warn_system_headers = global_dc->dc_warn_system_headers; |
6044 bool ret; | 6114 bool ret; |
6074 break; | 6144 break; |
6075 default: | 6145 default: |
6076 gcc_unreachable (); | 6146 gcc_unreachable (); |
6077 } | 6147 } |
6078 if (done_lexing) | 6148 if (done_lexing) |
6079 richloc->set_range (line_table, 0, input_location, true); | 6149 richloc->set_range (0, input_location, SHOW_RANGE_WITH_CARET); |
6080 diagnostic_set_info_translated (&diagnostic, msg, ap, | 6150 diagnostic_set_info_translated (&diagnostic, msg, ap, |
6081 richloc, dlevel); | 6151 richloc, dlevel); |
6082 diagnostic_override_option_index (&diagnostic, | 6152 diagnostic_override_option_index |
6083 c_option_controlling_cpp_error (reason)); | 6153 (&diagnostic, |
6154 c_option_controlling_cpp_diagnostic (reason)); | |
6084 ret = diagnostic_report_diagnostic (global_dc, &diagnostic); | 6155 ret = diagnostic_report_diagnostic (global_dc, &diagnostic); |
6085 if (level == CPP_DL_WARNING_SYSHDR) | 6156 if (level == CPP_DL_WARNING_SYSHDR) |
6086 global_dc->dc_warn_system_headers = save_warn_system_headers; | 6157 global_dc->dc_warn_system_headers = save_warn_system_headers; |
6087 return ret; | 6158 return ret; |
6088 } | 6159 } |
6108 return uc; | 6179 return uc; |
6109 } | 6180 } |
6110 | 6181 |
6111 /* Fold an offsetof-like expression. EXPR is a nested sequence of component | 6182 /* Fold an offsetof-like expression. EXPR is a nested sequence of component |
6112 references with an INDIRECT_REF of a constant at the bottom; much like the | 6183 references with an INDIRECT_REF of a constant at the bottom; much like the |
6113 traditional rendering of offsetof as a macro. Return the folded result. */ | 6184 traditional rendering of offsetof as a macro. TYPE is the desired type of |
6185 the whole expression. Return the folded result. */ | |
6114 | 6186 |
6115 tree | 6187 tree |
6116 fold_offsetof_1 (tree expr, enum tree_code ctx) | 6188 fold_offsetof (tree expr, tree type, enum tree_code ctx) |
6117 { | 6189 { |
6118 tree base, off, t; | 6190 tree base, off, t; |
6119 tree_code code = TREE_CODE (expr); | 6191 tree_code code = TREE_CODE (expr); |
6120 switch (code) | 6192 switch (code) |
6121 { | 6193 { |
6136 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0))) | 6208 if (!TREE_CONSTANT (TREE_OPERAND (expr, 0))) |
6137 { | 6209 { |
6138 error ("cannot apply %<offsetof%> to a non constant address"); | 6210 error ("cannot apply %<offsetof%> to a non constant address"); |
6139 return error_mark_node; | 6211 return error_mark_node; |
6140 } | 6212 } |
6141 return TREE_OPERAND (expr, 0); | 6213 return convert (type, TREE_OPERAND (expr, 0)); |
6142 | 6214 |
6143 case COMPONENT_REF: | 6215 case COMPONENT_REF: |
6144 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code); | 6216 base = fold_offsetof (TREE_OPERAND (expr, 0), type, code); |
6145 if (base == error_mark_node) | 6217 if (base == error_mark_node) |
6146 return base; | 6218 return base; |
6147 | 6219 |
6148 t = TREE_OPERAND (expr, 1); | 6220 t = TREE_OPERAND (expr, 1); |
6149 if (DECL_C_BIT_FIELD (t)) | 6221 if (DECL_C_BIT_FIELD (t)) |
6156 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t)) | 6228 size_int (tree_to_uhwi (DECL_FIELD_BIT_OFFSET (t)) |
6157 / BITS_PER_UNIT)); | 6229 / BITS_PER_UNIT)); |
6158 break; | 6230 break; |
6159 | 6231 |
6160 case ARRAY_REF: | 6232 case ARRAY_REF: |
6161 base = fold_offsetof_1 (TREE_OPERAND (expr, 0), code); | 6233 base = fold_offsetof (TREE_OPERAND (expr, 0), type, code); |
6162 if (base == error_mark_node) | 6234 if (base == error_mark_node) |
6163 return base; | 6235 return base; |
6164 | 6236 |
6165 t = TREE_OPERAND (expr, 1); | 6237 t = TREE_OPERAND (expr, 1); |
6166 | 6238 |
6212 break; | 6284 break; |
6213 | 6285 |
6214 case COMPOUND_EXPR: | 6286 case COMPOUND_EXPR: |
6215 /* Handle static members of volatile structs. */ | 6287 /* Handle static members of volatile structs. */ |
6216 t = TREE_OPERAND (expr, 1); | 6288 t = TREE_OPERAND (expr, 1); |
6217 gcc_assert (VAR_P (t)); | 6289 gcc_checking_assert (VAR_P (get_base_address (t))); |
6218 return fold_offsetof_1 (t); | 6290 return fold_offsetof (t, type); |
6219 | 6291 |
6220 default: | 6292 default: |
6221 gcc_unreachable (); | 6293 gcc_unreachable (); |
6222 } | 6294 } |
6223 | 6295 |
6296 if (!POINTER_TYPE_P (type)) | |
6297 return size_binop (PLUS_EXPR, base, convert (type, off)); | |
6224 return fold_build_pointer_plus (base, off); | 6298 return fold_build_pointer_plus (base, off); |
6225 } | 6299 } |
6226 | |
6227 /* Likewise, but convert it to the return type of offsetof. */ | |
6228 | |
6229 tree | |
6230 fold_offsetof (tree expr) | |
6231 { | |
6232 return convert (size_type_node, fold_offsetof_1 (expr)); | |
6233 } | |
6234 | |
6235 | 6300 |
6236 /* *PTYPE is an incomplete array. Complete it with a domain based on | 6301 /* *PTYPE is an incomplete array. Complete it with a domain based on |
6237 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT | 6302 INITIAL_VALUE. If INITIAL_VALUE is not present, use 1 if DO_DEFAULT |
6238 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, | 6303 is true. Return 0 if successful, 1 if INITIAL_VALUE can't be deciphered, |
6239 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */ | 6304 2 if INITIAL_VALUE was NULL, and 3 if INITIAL_VALUE was empty. */ |
6374 | 6439 |
6375 *ptype = type; | 6440 *ptype = type; |
6376 return failure; | 6441 return failure; |
6377 } | 6442 } |
6378 | 6443 |
6444 /* INIT is an constructor of a structure with a flexible array member. | |
6445 Complete the flexible array member with a domain based on it's value. */ | |
6446 void | |
6447 complete_flexible_array_elts (tree init) | |
6448 { | |
6449 tree elt, type; | |
6450 | |
6451 if (init == NULL_TREE || TREE_CODE (init) != CONSTRUCTOR) | |
6452 return; | |
6453 | |
6454 if (vec_safe_is_empty (CONSTRUCTOR_ELTS (init))) | |
6455 return; | |
6456 | |
6457 elt = CONSTRUCTOR_ELTS (init)->last ().value; | |
6458 type = TREE_TYPE (elt); | |
6459 if (TREE_CODE (type) == ARRAY_TYPE | |
6460 && TYPE_SIZE (type) == NULL_TREE) | |
6461 complete_array_type (&TREE_TYPE (elt), elt, false); | |
6462 else | |
6463 complete_flexible_array_elts (elt); | |
6464 } | |
6465 | |
6379 /* Like c_mark_addressable but don't check register qualifier. */ | 6466 /* Like c_mark_addressable but don't check register qualifier. */ |
6380 void | 6467 void |
6381 c_common_mark_addressable_vec (tree t) | 6468 c_common_mark_addressable_vec (tree t) |
6382 { | 6469 { |
6383 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR) | 6470 if (TREE_CODE (t) == C_MAYBE_CONST_EXPR) |
6402 tree | 6489 tree |
6403 builtin_type_for_size (int size, bool unsignedp) | 6490 builtin_type_for_size (int size, bool unsignedp) |
6404 { | 6491 { |
6405 tree type = c_common_type_for_size (size, unsignedp); | 6492 tree type = c_common_type_for_size (size, unsignedp); |
6406 return type ? type : error_mark_node; | 6493 return type ? type : error_mark_node; |
6494 } | |
6495 | |
6496 /* Work out the size of the first argument of a call to | |
6497 __builtin_speculation_safe_value. Only pointers and integral types | |
6498 are permitted. Return -1 if the argument type is not supported or | |
6499 the size is too large; 0 if the argument type is a pointer or the | |
6500 size if it is integral. */ | |
6501 static enum built_in_function | |
6502 speculation_safe_value_resolve_call (tree function, vec<tree, va_gc> *params) | |
6503 { | |
6504 /* Type of the argument. */ | |
6505 tree type; | |
6506 int size; | |
6507 | |
6508 if (vec_safe_is_empty (params)) | |
6509 { | |
6510 error ("too few arguments to function %qE", function); | |
6511 return BUILT_IN_NONE; | |
6512 } | |
6513 | |
6514 type = TREE_TYPE ((*params)[0]); | |
6515 if (TREE_CODE (type) == ARRAY_TYPE && c_dialect_cxx ()) | |
6516 { | |
6517 /* Force array-to-pointer decay for C++. */ | |
6518 (*params)[0] = default_conversion ((*params)[0]); | |
6519 type = TREE_TYPE ((*params)[0]); | |
6520 } | |
6521 | |
6522 if (POINTER_TYPE_P (type)) | |
6523 return BUILT_IN_SPECULATION_SAFE_VALUE_PTR; | |
6524 | |
6525 if (!INTEGRAL_TYPE_P (type)) | |
6526 goto incompatible; | |
6527 | |
6528 if (!COMPLETE_TYPE_P (type)) | |
6529 goto incompatible; | |
6530 | |
6531 size = tree_to_uhwi (TYPE_SIZE_UNIT (type)); | |
6532 if (size == 1 || size == 2 || size == 4 || size == 8 || size == 16) | |
6533 return ((enum built_in_function) | |
6534 ((int) BUILT_IN_SPECULATION_SAFE_VALUE_1 + exact_log2 (size))); | |
6535 | |
6536 incompatible: | |
6537 /* Issue the diagnostic only if the argument is valid, otherwise | |
6538 it would be redundant at best and could be misleading. */ | |
6539 if (type != error_mark_node) | |
6540 error ("operand type %qT is incompatible with argument %d of %qE", | |
6541 type, 1, function); | |
6542 | |
6543 return BUILT_IN_NONE; | |
6544 } | |
6545 | |
6546 /* Validate and coerce PARAMS, the arguments to ORIG_FUNCTION to fit | |
6547 the prototype for FUNCTION. The first argument is mandatory, a second | |
6548 argument, if present, must be type compatible with the first. */ | |
6549 static bool | |
6550 speculation_safe_value_resolve_params (location_t loc, tree orig_function, | |
6551 vec<tree, va_gc> *params) | |
6552 { | |
6553 tree val; | |
6554 | |
6555 if (params->length () == 0) | |
6556 { | |
6557 error_at (loc, "too few arguments to function %qE", orig_function); | |
6558 return false; | |
6559 } | |
6560 | |
6561 else if (params->length () > 2) | |
6562 { | |
6563 error_at (loc, "too many arguments to function %qE", orig_function); | |
6564 return false; | |
6565 } | |
6566 | |
6567 val = (*params)[0]; | |
6568 if (TREE_CODE (TREE_TYPE (val)) == ARRAY_TYPE) | |
6569 val = default_conversion (val); | |
6570 if (!(TREE_CODE (TREE_TYPE (val)) == POINTER_TYPE | |
6571 || TREE_CODE (TREE_TYPE (val)) == INTEGER_TYPE)) | |
6572 { | |
6573 error_at (loc, | |
6574 "expecting argument of type pointer or of type integer " | |
6575 "for argument 1"); | |
6576 return false; | |
6577 } | |
6578 (*params)[0] = val; | |
6579 | |
6580 if (params->length () == 2) | |
6581 { | |
6582 tree val2 = (*params)[1]; | |
6583 if (TREE_CODE (TREE_TYPE (val2)) == ARRAY_TYPE) | |
6584 val2 = default_conversion (val2); | |
6585 if (!(TREE_TYPE (val) == TREE_TYPE (val2) | |
6586 || useless_type_conversion_p (TREE_TYPE (val), TREE_TYPE (val2)))) | |
6587 { | |
6588 error_at (loc, "both arguments must be compatible"); | |
6589 return false; | |
6590 } | |
6591 (*params)[1] = val2; | |
6592 } | |
6593 | |
6594 return true; | |
6595 } | |
6596 | |
6597 /* Cast the result of the builtin back to the type of the first argument, | |
6598 preserving any qualifiers that it might have. */ | |
6599 static tree | |
6600 speculation_safe_value_resolve_return (tree first_param, tree result) | |
6601 { | |
6602 tree ptype = TREE_TYPE (first_param); | |
6603 tree rtype = TREE_TYPE (result); | |
6604 ptype = TYPE_MAIN_VARIANT (ptype); | |
6605 | |
6606 if (tree_int_cst_equal (TYPE_SIZE (ptype), TYPE_SIZE (rtype))) | |
6607 return convert (ptype, result); | |
6608 | |
6609 return result; | |
6407 } | 6610 } |
6408 | 6611 |
6409 /* A helper function for resolve_overloaded_builtin in resolving the | 6612 /* A helper function for resolve_overloaded_builtin in resolving the |
6410 overloaded __sync_ builtins. Returns a positive power of 2 if the | 6613 overloaded __sync_ builtins. Returns a positive power of 2 if the |
6411 first operand of PARAMS is a pointer to a supported data type. | 6614 first operand of PARAMS is a pointer to a supported data type. |
6675 | 6878 |
6676 /* Check memory model parameters for validity. */ | 6879 /* Check memory model parameters for validity. */ |
6677 for (x = n_param - n_model ; x < n_param; x++) | 6880 for (x = n_param - n_model ; x < n_param; x++) |
6678 { | 6881 { |
6679 tree p = (*params)[x]; | 6882 tree p = (*params)[x]; |
6883 if (!INTEGRAL_TYPE_P (TREE_TYPE (p))) | |
6884 { | |
6885 error_at (loc, "non-integer memory model argument %d of %qE", x + 1, | |
6886 function); | |
6887 return 0; | |
6888 } | |
6889 p = fold_for_warn (p); | |
6680 if (TREE_CODE (p) == INTEGER_CST) | 6890 if (TREE_CODE (p) == INTEGER_CST) |
6681 { | 6891 { |
6682 int i = tree_to_uhwi (p); | 6892 /* memmodel_base masks the low 16 bits, thus ignore any bits above |
6683 if (i < 0 || (memmodel_base (i) >= MEMMODEL_LAST)) | 6893 it by using TREE_INT_CST_LOW instead of tree_to_*hwi. Those high |
6684 { | 6894 bits will be checked later during expansion in target specific |
6685 warning_at (loc, OPT_Winvalid_memory_model, | 6895 way. */ |
6686 "invalid memory model argument %d of %qE", x + 1, | 6896 if (memmodel_base (TREE_INT_CST_LOW (p)) >= MEMMODEL_LAST) |
6687 function); | 6897 warning_at (loc, OPT_Winvalid_memory_model, |
6688 } | 6898 "invalid memory model argument %d of %qE", x + 1, |
6899 function); | |
6689 } | 6900 } |
6690 else | 6901 } |
6691 if (!INTEGRAL_TYPE_P (TREE_TYPE (p))) | |
6692 { | |
6693 error_at (loc, "non-integer memory model argument %d of %qE", x + 1, | |
6694 function); | |
6695 return 0; | |
6696 } | |
6697 } | |
6698 | 6902 |
6699 return size_0; | 6903 return size_0; |
6700 } | 6904 } |
6701 | 6905 |
6702 | 6906 |
7057 } | 7261 } |
7058 | 7262 |
7059 /* Handle BUILT_IN_NORMAL here. */ | 7263 /* Handle BUILT_IN_NORMAL here. */ |
7060 switch (orig_code) | 7264 switch (orig_code) |
7061 { | 7265 { |
7266 case BUILT_IN_SPECULATION_SAFE_VALUE_N: | |
7267 { | |
7268 tree new_function, first_param, result; | |
7269 enum built_in_function fncode | |
7270 = speculation_safe_value_resolve_call (function, params);; | |
7271 | |
7272 first_param = (*params)[0]; | |
7273 if (fncode == BUILT_IN_NONE | |
7274 || !speculation_safe_value_resolve_params (loc, function, params)) | |
7275 return error_mark_node; | |
7276 | |
7277 if (targetm.have_speculation_safe_value (true)) | |
7278 { | |
7279 new_function = builtin_decl_explicit (fncode); | |
7280 result = build_function_call_vec (loc, vNULL, new_function, params, | |
7281 NULL); | |
7282 | |
7283 if (result == error_mark_node) | |
7284 return result; | |
7285 | |
7286 return speculation_safe_value_resolve_return (first_param, result); | |
7287 } | |
7288 else | |
7289 { | |
7290 /* This target doesn't have, or doesn't need, active mitigation | |
7291 against incorrect speculative execution. Simply return the | |
7292 first parameter to the builtin. */ | |
7293 if (!targetm.have_speculation_safe_value (false)) | |
7294 /* The user has invoked __builtin_speculation_safe_value | |
7295 even though __HAVE_SPECULATION_SAFE_VALUE is not | |
7296 defined: emit a warning. */ | |
7297 warning_at (input_location, 0, | |
7298 "this target does not define a speculation barrier; " | |
7299 "your program will still execute correctly, " | |
7300 "but incorrect speculation may not be be " | |
7301 "restricted"); | |
7302 | |
7303 /* If the optional second argument is present, handle any side | |
7304 effects now. */ | |
7305 if (params->length () == 2 | |
7306 && TREE_SIDE_EFFECTS ((*params)[1])) | |
7307 return build2 (COMPOUND_EXPR, TREE_TYPE (first_param), | |
7308 (*params)[1], first_param); | |
7309 | |
7310 return first_param; | |
7311 } | |
7312 } | |
7313 | |
7062 case BUILT_IN_ATOMIC_EXCHANGE: | 7314 case BUILT_IN_ATOMIC_EXCHANGE: |
7063 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: | 7315 case BUILT_IN_ATOMIC_COMPARE_EXCHANGE: |
7064 case BUILT_IN_ATOMIC_LOAD: | 7316 case BUILT_IN_ATOMIC_LOAD: |
7065 case BUILT_IN_ATOMIC_STORE: | 7317 case BUILT_IN_ATOMIC_STORE: |
7066 { | 7318 { |
7227 t1 = TREE_TYPE (t1); | 7479 t1 = TREE_TYPE (t1); |
7228 t2 = TREE_TYPE (t2); | 7480 t2 = TREE_TYPE (t2); |
7229 | 7481 |
7230 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2); | 7482 enum tree_code c1 = TREE_CODE (t1), c2 = TREE_CODE (t2); |
7231 | 7483 |
7232 gcc_assert ((c1 == INTEGER_TYPE || c1 == REAL_TYPE || c1 == FIXED_POINT_TYPE) | 7484 gcc_assert ((INTEGRAL_TYPE_P (t1) |
7233 && (c2 == INTEGER_TYPE || c2 == REAL_TYPE | 7485 || c1 == REAL_TYPE |
7486 || c1 == FIXED_POINT_TYPE) | |
7487 && (INTEGRAL_TYPE_P (t2) | |
7488 || c2 == REAL_TYPE | |
7234 || c2 == FIXED_POINT_TYPE)); | 7489 || c2 == FIXED_POINT_TYPE)); |
7235 | 7490 |
7236 t1 = c_common_signed_type (t1); | 7491 t1 = c_common_signed_type (t1); |
7237 t2 = c_common_signed_type (t2); | 7492 t2 = c_common_signed_type (t2); |
7238 /* Equality works here because c_common_signed_type uses | 7493 /* Equality works here because c_common_signed_type uses |
7239 TYPE_MAIN_VARIANT. */ | 7494 TYPE_MAIN_VARIANT. */ |
7240 if (t1 == t2) | 7495 if (t1 == t2) |
7241 return true; | 7496 return true; |
7242 if (opaque && c1 == c2 | 7497 if (opaque && c1 == c2 |
7243 && (c1 == INTEGER_TYPE || c1 == REAL_TYPE) | 7498 && (INTEGRAL_TYPE_P (t1) || c1 == REAL_TYPE) |
7244 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)) | 7499 && TYPE_PRECISION (t1) == TYPE_PRECISION (t2)) |
7245 return true; | 7500 return true; |
7246 return false; | 7501 return false; |
7247 } | 7502 } |
7248 | 7503 |
7574 void | 7829 void |
7575 c_common_init_ts (void) | 7830 c_common_init_ts (void) |
7576 { | 7831 { |
7577 MARK_TS_TYPED (C_MAYBE_CONST_EXPR); | 7832 MARK_TS_TYPED (C_MAYBE_CONST_EXPR); |
7578 MARK_TS_TYPED (EXCESS_PRECISION_EXPR); | 7833 MARK_TS_TYPED (EXCESS_PRECISION_EXPR); |
7579 MARK_TS_TYPED (ARRAY_NOTATION_REF); | |
7580 } | 7834 } |
7581 | 7835 |
7582 /* Build a user-defined numeric literal out of an integer constant type VALUE | 7836 /* Build a user-defined numeric literal out of an integer constant type VALUE |
7583 with identifier SUFFIX. */ | 7837 with identifier SUFFIX. */ |
7584 | 7838 |
7608 | 7862 |
7609 ret = !lvalue_p (*vecp); | 7863 ret = !lvalue_p (*vecp); |
7610 | 7864 |
7611 if (TREE_CODE (index) == INTEGER_CST) | 7865 if (TREE_CODE (index) == INTEGER_CST) |
7612 if (!tree_fits_uhwi_p (index) | 7866 if (!tree_fits_uhwi_p (index) |
7613 || tree_to_uhwi (index) >= TYPE_VECTOR_SUBPARTS (type)) | 7867 || maybe_ge (tree_to_uhwi (index), TYPE_VECTOR_SUBPARTS (type))) |
7614 warning_at (loc, OPT_Warray_bounds, "index value is out of bound"); | 7868 warning_at (loc, OPT_Warray_bounds, "index value is out of bound"); |
7615 | 7869 |
7616 /* We are building an ARRAY_REF so mark the vector as addressable | 7870 /* We are building an ARRAY_REF so mark the vector as addressable |
7617 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P | 7871 to not run into the gimplifiers premature setting of DECL_GIMPLE_REG_P |
7618 for function parameters. */ | 7872 for function parameters. */ |
7784 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */) | 8038 reject_gcc_builtin (const_tree expr, location_t loc /* = UNKNOWN_LOCATION */) |
7785 { | 8039 { |
7786 if (TREE_CODE (expr) == ADDR_EXPR) | 8040 if (TREE_CODE (expr) == ADDR_EXPR) |
7787 expr = TREE_OPERAND (expr, 0); | 8041 expr = TREE_OPERAND (expr, 0); |
7788 | 8042 |
8043 STRIP_ANY_LOCATION_WRAPPER (expr); | |
8044 | |
7789 if (TREE_TYPE (expr) | 8045 if (TREE_TYPE (expr) |
7790 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE | 8046 && TREE_CODE (TREE_TYPE (expr)) == FUNCTION_TYPE |
7791 && TREE_CODE (expr) == FUNCTION_DECL | 8047 && TREE_CODE (expr) == FUNCTION_DECL |
7792 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids | 8048 /* The intersection of DECL_BUILT_IN and DECL_IS_BUILTIN avoids |
7793 false positives for user-declared built-ins such as abs or | 8049 false positives for user-declared built-ins such as abs or |
7794 strlen, and for C++ operators new and delete. | 8050 strlen, and for C++ operators new and delete. |
7795 The c_decl_implicit() test avoids false positives for implicitly | 8051 The c_decl_implicit() test avoids false positives for implicitly |
7796 declared built-ins with library fallbacks (such as abs). */ | 8052 declared built-ins with library fallbacks (such as abs). */ |
7797 && DECL_BUILT_IN (expr) | 8053 && fndecl_built_in_p (expr) |
7798 && DECL_IS_BUILTIN (expr) | 8054 && DECL_IS_BUILTIN (expr) |
7799 && !c_decl_implicit (expr) | 8055 && !c_decl_implicit (expr) |
7800 && !DECL_ASSEMBLER_NAME_SET_P (expr)) | 8056 && !DECL_ASSEMBLER_NAME_SET_P (expr)) |
7801 { | 8057 { |
7802 if (loc == UNKNOWN_LOCATION) | 8058 if (loc == UNKNOWN_LOCATION) |
7816 than half of the address space. Return true if the size of the array | 8072 than half of the address space. Return true if the size of the array |
7817 is valid, false otherwise. TYPE is the type of the array and NAME is | 8073 is valid, false otherwise. TYPE is the type of the array and NAME is |
7818 the name of the array, or NULL_TREE for unnamed arrays. */ | 8074 the name of the array, or NULL_TREE for unnamed arrays. */ |
7819 | 8075 |
7820 bool | 8076 bool |
7821 valid_array_size_p (location_t loc, tree type, tree name) | 8077 valid_array_size_p (location_t loc, tree type, tree name, bool complain) |
7822 { | 8078 { |
7823 if (type != error_mark_node | 8079 if (type != error_mark_node |
7824 && COMPLETE_TYPE_P (type) | 8080 && COMPLETE_TYPE_P (type) |
7825 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST | 8081 && TREE_CODE (TYPE_SIZE_UNIT (type)) == INTEGER_CST |
7826 && !valid_constant_size_p (TYPE_SIZE_UNIT (type))) | 8082 && !valid_constant_size_p (TYPE_SIZE_UNIT (type))) |
7827 { | 8083 { |
7828 if (name) | 8084 if (complain) |
7829 error_at (loc, "size of array %qE is too large", name); | 8085 { |
7830 else | 8086 if (name) |
7831 error_at (loc, "size of unnamed array is too large"); | 8087 error_at (loc, "size of array %qE is too large", name); |
8088 else | |
8089 error_at (loc, "size of unnamed array is too large"); | |
8090 } | |
7832 return false; | 8091 return false; |
7833 } | 8092 } |
7834 return true; | 8093 return true; |
7835 } | 8094 } |
7836 | 8095 |
8116 { | 8375 { |
8117 fixit_hint *hint = richloc->get_last_fixit_hint (); | 8376 fixit_hint *hint = richloc->get_last_fixit_hint (); |
8118 location_t hint_loc = hint->get_start_loc (); | 8377 location_t hint_loc = hint->get_start_loc (); |
8119 location_t old_loc = richloc->get_loc (); | 8378 location_t old_loc = richloc->get_loc (); |
8120 | 8379 |
8121 richloc->set_range (line_table, 0, hint_loc, true); | 8380 richloc->set_range (0, hint_loc, SHOW_RANGE_WITH_CARET); |
8122 richloc->add_range (old_loc, false); | 8381 richloc->add_range (old_loc); |
8123 } | 8382 } |
8124 } | 8383 } |
8125 | 8384 |
8126 #if CHECKING_P | 8385 #if CHECKING_P |
8127 | 8386 |
8128 namespace selftest { | 8387 namespace selftest { |
8388 | |
8389 /* Verify that fold_for_warn on error_mark_node is safe. */ | |
8390 | |
8391 static void | |
8392 test_fold_for_warn () | |
8393 { | |
8394 ASSERT_EQ (error_mark_node, fold_for_warn (error_mark_node)); | |
8395 } | |
8396 | |
8397 /* Run all of the selftests within this file. */ | |
8398 | |
8399 static void | |
8400 c_common_c_tests () | |
8401 { | |
8402 test_fold_for_warn (); | |
8403 } | |
8129 | 8404 |
8130 /* Run all of the tests within c-family. */ | 8405 /* Run all of the tests within c-family. */ |
8131 | 8406 |
8132 void | 8407 void |
8133 c_family_tests (void) | 8408 c_family_tests (void) |
8134 { | 8409 { |
8410 c_common_c_tests (); | |
8135 c_format_c_tests (); | 8411 c_format_c_tests (); |
8412 c_indentation_c_tests (); | |
8413 c_pretty_print_c_tests (); | |
8414 c_spellcheck_cc_tests (); | |
8136 } | 8415 } |
8137 | 8416 |
8138 } // namespace selftest | 8417 } // namespace selftest |
8139 | 8418 |
8140 #endif /* #if CHECKING_P */ | 8419 #endif /* #if CHECKING_P */ |
8172 for (unsigned int i = 0; i < LINEMAPS_ORDINARY_USED (line_table); i++) | 8451 for (unsigned int i = 0; i < LINEMAPS_ORDINARY_USED (line_table); i++) |
8173 { | 8452 { |
8174 const line_map_ordinary *ord_map | 8453 const line_map_ordinary *ord_map |
8175 = LINEMAPS_ORDINARY_MAP_AT (line_table, i); | 8454 = LINEMAPS_ORDINARY_MAP_AT (line_table, i); |
8176 | 8455 |
8177 const line_map_ordinary *from = INCLUDED_FROM (line_table, ord_map); | 8456 if (const line_map_ordinary *from |
8178 if (from) | 8457 = linemap_included_from_linemap (line_table, ord_map)) |
8179 if (from->to_file == file) | 8458 if (from->to_file == file) |
8180 { | 8459 { |
8181 last_include_ord_map = from; | 8460 last_include_ord_map = from; |
8182 last_ord_map_after_include = NULL; | 8461 last_ord_map_after_include = NULL; |
8183 } | 8462 } |
8235 /* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n" | 8514 /* Attempt to add a fix-it hint to RICHLOC, adding "#include HEADER\n" |
8236 in a suitable location within the file of RICHLOC's primary | 8515 in a suitable location within the file of RICHLOC's primary |
8237 location. | 8516 location. |
8238 | 8517 |
8239 This function is idempotent: a header will be added at most once to | 8518 This function is idempotent: a header will be added at most once to |
8240 any given file. */ | 8519 any given file. |
8520 | |
8521 If OVERRIDE_LOCATION is true, then if a fix-it is added and will be | |
8522 printed, then RICHLOC's primary location will be replaced by that of | |
8523 the fix-it hint (for use by "inform" notes where the location of the | |
8524 issue has already been reported). */ | |
8241 | 8525 |
8242 void | 8526 void |
8243 maybe_add_include_fixit (rich_location *richloc, const char *header) | 8527 maybe_add_include_fixit (rich_location *richloc, const char *header, |
8528 bool override_location) | |
8244 { | 8529 { |
8245 location_t loc = richloc->get_loc (); | 8530 location_t loc = richloc->get_loc (); |
8246 const char *file = LOCATION_FILE (loc); | 8531 const char *file = LOCATION_FILE (loc); |
8247 if (!file) | 8532 if (!file) |
8248 return; | 8533 return; |
8266 return; | 8551 return; |
8267 | 8552 |
8268 char *text = xasprintf ("#include %s\n", header); | 8553 char *text = xasprintf ("#include %s\n", header); |
8269 richloc->add_fixit_insert_before (include_insert_loc, text); | 8554 richloc->add_fixit_insert_before (include_insert_loc, text); |
8270 free (text); | 8555 free (text); |
8556 | |
8557 if (override_location && global_dc->show_caret) | |
8558 { | |
8559 /* Replace the primary location with that of the insertion point for the | |
8560 fix-it hint. | |
8561 | |
8562 We use SHOW_LINES_WITHOUT_RANGE so that we don't meaningless print a | |
8563 caret for the insertion point (or colorize it). | |
8564 | |
8565 Hence we print e.g.: | |
8566 | |
8567 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2 | |
8568 73 | # include <debug/vector> | |
8569 +++ |+#include <vector> | |
8570 74 | #endif | |
8571 | |
8572 rather than: | |
8573 | |
8574 ../x86_64-pc-linux-gnu/libstdc++-v3/include/vector:74:1: note: msg 2 | |
8575 73 | # include <debug/vector> | |
8576 +++ |+#include <vector> | |
8577 74 | #endif | |
8578 | ^ | |
8579 | |
8580 avoiding the caret on the first column of line 74. */ | |
8581 richloc->set_range (0, include_insert_loc, SHOW_LINES_WITHOUT_RANGE); | |
8582 } | |
8583 } | |
8584 | |
8585 /* Attempt to convert a braced array initializer list CTOR for array | |
8586 TYPE into a STRING_CST for convenience and efficiency. Return | |
8587 the converted string on success or the original ctor on failure. */ | |
8588 | |
8589 tree | |
8590 braced_list_to_string (tree type, tree ctor) | |
8591 { | |
8592 if (!tree_fits_uhwi_p (TYPE_SIZE_UNIT (type))) | |
8593 return ctor; | |
8594 | |
8595 /* If the array has an explicit bound, use it to constrain the size | |
8596 of the string. If it doesn't, be sure to create a string that's | |
8597 as long as implied by the index of the last zero specified via | |
8598 a designator, as in: | |
8599 const char a[] = { [7] = 0 }; */ | |
8600 unsigned HOST_WIDE_INT maxelts = tree_to_uhwi (TYPE_SIZE_UNIT (type)); | |
8601 maxelts /= tree_to_uhwi (TYPE_SIZE_UNIT (TREE_TYPE (type))); | |
8602 | |
8603 /* Avoid converting initializers for zero-length arrays. */ | |
8604 if (!maxelts) | |
8605 return ctor; | |
8606 | |
8607 unsigned HOST_WIDE_INT nelts = CONSTRUCTOR_NELTS (ctor); | |
8608 | |
8609 auto_vec<char> str; | |
8610 str.reserve (nelts + 1); | |
8611 | |
8612 unsigned HOST_WIDE_INT i; | |
8613 tree index, value; | |
8614 | |
8615 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, index, value) | |
8616 { | |
8617 unsigned HOST_WIDE_INT idx = i; | |
8618 if (index) | |
8619 { | |
8620 if (!tree_fits_uhwi_p (index)) | |
8621 return ctor; | |
8622 idx = tree_to_uhwi (index); | |
8623 } | |
8624 | |
8625 /* auto_vec is limited to UINT_MAX elements. */ | |
8626 if (idx > UINT_MAX) | |
8627 return ctor; | |
8628 | |
8629 /* Avoid non-constant initializers. */ | |
8630 if (!tree_fits_shwi_p (value)) | |
8631 return ctor; | |
8632 | |
8633 /* Skip over embedded nuls except the last one (initializer | |
8634 elements are in ascending order of indices). */ | |
8635 HOST_WIDE_INT val = tree_to_shwi (value); | |
8636 if (!val && i + 1 < nelts) | |
8637 continue; | |
8638 | |
8639 if (idx < str.length()) | |
8640 return ctor; | |
8641 | |
8642 /* Bail if the CTOR has a block of more than 256 embedded nuls | |
8643 due to implicitly initialized elements. */ | |
8644 unsigned nchars = (idx - str.length ()) + 1; | |
8645 if (nchars > 256) | |
8646 return ctor; | |
8647 | |
8648 if (nchars > 1) | |
8649 { | |
8650 str.reserve (idx); | |
8651 str.quick_grow_cleared (idx); | |
8652 } | |
8653 | |
8654 if (idx >= maxelts) | |
8655 return ctor; | |
8656 | |
8657 str.safe_insert (idx, val); | |
8658 } | |
8659 | |
8660 /* Append a nul string termination. */ | |
8661 if (str.length () < maxelts) | |
8662 str.safe_push (0); | |
8663 | |
8664 /* Build a STRING_CST with the same type as the array. */ | |
8665 tree res = build_string (str.length (), str.begin ()); | |
8666 TREE_TYPE (res) = type; | |
8667 return res; | |
8271 } | 8668 } |
8272 | 8669 |
8273 #include "gt-c-family-c-common.h" | 8670 #include "gt-c-family-c-common.h" |