From 48dc31aeca90225675ec8399ec700daf0a9e2cf6 Mon Sep 17 00:00:00 2001 From: pfg Date: Sun, 15 Dec 2013 03:47:31 +0000 Subject: [PATCH] MFC rr258501, r258507; gcc: Bring updates from Google's enhanced gcc-4.2.1. Google released and enhanced version of gcc-4.2.1 plus their local patches for Android[1]. The patches are owned by Google and the license hasn't been changed from the original GPLv2. We are only bringing a subset of the available patches that may be helpful in FreeBSD, in other words, changes specific to android are not included. From the README.google file[1]. Patches applied to google_vendor_src_branch/gcc/gcc-4.2.1: gcc/Makefile.in gcc/c-common.c gcc/c-common.h gcc/c-opts.c gcc/c-typeck.c gcc/cp/typeck.c gcc/doc/invoke.texi gcc/flags.h gcc/opts.c gcc/tree-flow.h gcc/tree-ssa-alias-warnings.c gcc/tree-ssa-alias.c Backport of -Wstrict-aliasing from mainline. Silvius Rus gcc/coverage.c: Patch coverage_checksum_string for PR 25351. Seongbae Park Not yet submitted to FSF. gcc/c-opts.c gcc/c-ppoutput.c gcc/c.opt gcc/doc/cppopts.texi libcpp/Makefile.in libcpp/directives-only.c libcpp/directives.c libcpp/files.c libcpp/include/cpplib.h libcpp/init.c libcpp/internal.h libcpp/macro.c Support for -fdirectives-only. Ollie Wild . Submitted to FSF but not yet approved. libstdc++-v3/include/ext/hashtable.h http://b/742065 http://b/629994 Reduce min size of hashtable for hash_map, hash_set from 53 to 5 libstdc++-v3/include/ext/hashtable.h http://b/629994 Do not iterate over buckets if hashtable is empty. gcc/common.opt gcc/doc/invoke.texi gcc/flags.h gcc/gimplify.c gcc/opts.c Add Saito's patch for -finstrument-functions-exclude-* options. gcc/common.opt gcc/doc/invoke.texi gcc/final.c gcc/flags.h gcc/opts.c gcc/testsuite/gcc.dg/Wframe-larger-than.c Add a new flag -Wframe-larger-than- which enables a new warning when a frame size of a function is larger than specified. This patch hasn't been integrated into gcc mainline yet. gcc/tree-vrp.c Add a hack to avoid using ivopts information for pointers starting at constant values. Reference: [1] https://android.googlesource.com/toolchain/gcc/+/master/gcc-4.2.1/ Obtained from: Google Inc. git-svn-id: svn://svn.freebsd.org/base/stable/10@259405 ccf9f872-aa2e-dd11-9fc8-001c23d0bc1f --- contrib/gcc/c-common.c | 216 +++-- contrib/gcc/c-common.h | 2 +- contrib/gcc/c-opts.c | 21 +- contrib/gcc/c-ppoutput.c | 25 + contrib/gcc/c-typeck.c | 16 +- contrib/gcc/c.opt | 4 + contrib/gcc/common.opt | 14 +- contrib/gcc/coverage.c | 100 +- contrib/gcc/cp/typeck.c | 16 +- contrib/gcc/doc/cppopts.texi | 16 + contrib/gcc/doc/invoke.texi | 77 +- contrib/gcc/final.c | 10 +- contrib/gcc/flags.h | 19 + contrib/gcc/gimplify.c | 3 +- contrib/gcc/opts.c | 127 +++ contrib/gcc/tree-flow.h | 2 + contrib/gcc/tree-ssa-alias-warnings.c | 1036 +++++++++++++++++++++ contrib/gcc/tree-ssa-alias.c | 5 +- contrib/gcc/tree-vrp.c | 8 +- contrib/gcclibs/libcpp/Makefile.in | 12 +- contrib/gcclibs/libcpp/directives-only.c | 229 +++++ contrib/gcclibs/libcpp/directives.c | 7 +- contrib/gcclibs/libcpp/files.c | 3 +- contrib/gcclibs/libcpp/include/cpplib.h | 7 + contrib/gcclibs/libcpp/init.c | 27 +- contrib/gcclibs/libcpp/internal.h | 11 + contrib/gcclibs/libcpp/macro.c | 3 + contrib/libstdc++/include/ext/hashtable.h | 7 +- gnu/usr.bin/cc/cc_int/Makefile | 1 + gnu/usr.bin/cc/libcpp/Makefile | 6 +- 30 files changed, 1866 insertions(+), 164 deletions(-) create mode 100644 contrib/gcc/tree-ssa-alias-warnings.c create mode 100644 contrib/gcclibs/libcpp/directives-only.c diff --git a/contrib/gcc/c-common.c b/contrib/gcc/c-common.c index 9a0413051..327dbcd44 100644 --- a/contrib/gcc/c-common.c +++ b/contrib/gcc/c-common.c @@ -983,35 +983,67 @@ unsigned_conversion_warning (tree result, tree operand) strict aliasing mode is in effect. OTYPE is the original TREE_TYPE of EXPR, and TYPE the type we're casting to. */ -void +bool strict_aliasing_warning (tree otype, tree type, tree expr) { - if (flag_strict_aliasing && warn_strict_aliasing - && POINTER_TYPE_P (type) && POINTER_TYPE_P (otype) - && TREE_CODE (expr) == ADDR_EXPR + if (!(flag_strict_aliasing && POINTER_TYPE_P (type) + && POINTER_TYPE_P (otype) && !VOID_TYPE_P (TREE_TYPE (type)))) + return false; + + if ((warn_strict_aliasing > 1) && TREE_CODE (expr) == ADDR_EXPR && (DECL_P (TREE_OPERAND (expr, 0)) - || handled_component_p (TREE_OPERAND (expr, 0))) - && !VOID_TYPE_P (TREE_TYPE (type))) + || handled_component_p (TREE_OPERAND (expr, 0)))) { /* Casting the address of an object to non void pointer. Warn if the cast breaks type based aliasing. */ - if (!COMPLETE_TYPE_P (TREE_TYPE (type))) - warning (OPT_Wstrict_aliasing, "type-punning to incomplete type " - "might break strict-aliasing rules"); + if (!COMPLETE_TYPE_P (TREE_TYPE (type)) && warn_strict_aliasing == 2) + { + warning (OPT_Wstrict_aliasing, "type-punning to incomplete type " + "might break strict-aliasing rules"); + return true; + } else { - HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); + /* warn_strict_aliasing >= 3. This includes the default (3). + Only warn if the cast is dereferenced immediately. */ + HOST_WIDE_INT set1 = + get_alias_set (TREE_TYPE (TREE_OPERAND (expr, 0))); HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type)); if (!alias_sets_conflict_p (set1, set2)) - warning (OPT_Wstrict_aliasing, "dereferencing type-punned " - "pointer will break strict-aliasing rules"); - else if (warn_strict_aliasing > 1 - && !alias_sets_might_conflict_p (set1, set2)) - warning (OPT_Wstrict_aliasing, "dereferencing type-punned " - "pointer might break strict-aliasing rules"); + { + warning (OPT_Wstrict_aliasing, "dereferencing type-punned " + "pointer will break strict-aliasing rules"); + return true; + } + else if (warn_strict_aliasing == 2 + && !alias_sets_might_conflict_p (set1, set2)) + { + warning (OPT_Wstrict_aliasing, "dereferencing type-punned " + "pointer might break strict-aliasing rules"); + return true; + } } } + else + if ((warn_strict_aliasing == 1) && !VOID_TYPE_P (TREE_TYPE (otype))) + { + /* At this level, warn for any conversions, even if an address is + not taken in the same statement. This will likely produce many + false positives, but could be useful to pinpoint problems that + are not revealed at higher levels. */ + HOST_WIDE_INT set1 = get_alias_set (TREE_TYPE (otype)); + HOST_WIDE_INT set2 = get_alias_set (TREE_TYPE (type)); + if (!COMPLETE_TYPE_P(type) + || !alias_sets_might_conflict_p (set1, set2)) + { + warning (OPT_Wstrict_aliasing, "dereferencing type-punned " + "pointer might break strict-aliasing rules"); + return true; + } + } + + return false; } @@ -3108,6 +3140,85 @@ def_fn_type (builtin_type def, builtin_type ret, bool var, int n, ...) builtin_types[def] = t; } +/* Build builtin functions common to both C and C++ language + frontends. */ + +static void +c_define_builtins (tree va_list_ref_type_node, tree va_list_arg_type_node) +{ +#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \ + builtin_types[ENUM] = VALUE; +#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \ + def_fn_type (ENUM, RETURN, 0, 0); +#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \ + def_fn_type (ENUM, RETURN, 0, 1, ARG1); +#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \ + def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2); +#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ + def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3); +#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ + def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4); +#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ + def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5); +#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ + ARG6) \ + def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6); +#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ + ARG6, ARG7) \ + def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7); +#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \ + def_fn_type (ENUM, RETURN, 1, 0); +#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \ + def_fn_type (ENUM, RETURN, 1, 1, ARG1); +#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \ + def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2); +#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ + def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3); +#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ + def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4); +#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ + def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5); +#define DEF_POINTER_TYPE(ENUM, TYPE) \ + builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]); + +#include "builtin-types.def" + +#undef DEF_PRIMITIVE_TYPE +#undef DEF_FUNCTION_TYPE_1 +#undef DEF_FUNCTION_TYPE_2 +#undef DEF_FUNCTION_TYPE_3 +#undef DEF_FUNCTION_TYPE_4 +#undef DEF_FUNCTION_TYPE_5 +#undef DEF_FUNCTION_TYPE_6 +#undef DEF_FUNCTION_TYPE_VAR_0 +#undef DEF_FUNCTION_TYPE_VAR_1 +#undef DEF_FUNCTION_TYPE_VAR_2 +#undef DEF_FUNCTION_TYPE_VAR_3 +#undef DEF_FUNCTION_TYPE_VAR_4 +#undef DEF_FUNCTION_TYPE_VAR_5 +#undef DEF_POINTER_TYPE + builtin_types[(int) BT_LAST] = NULL_TREE; + + c_init_attributes (); + +#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \ + NONANSI_P, ATTRS, IMPLICIT, COND) \ + if (NAME && COND) \ + def_builtin_1 (ENUM, NAME, CLASS, \ + builtin_types[(int) TYPE], \ + builtin_types[(int) LIBTYPE], \ + BOTH_P, FALLBACK_P, NONANSI_P, \ + built_in_attributes[(int) ATTRS], IMPLICIT); +#include "builtins.def" +#undef DEF_BUILTIN + + build_common_builtin_nodes (); + + targetm.init_builtins (); + if (flag_mudflap) + mudflap_init (); +} + /* Build tree nodes and builtin functions common to both C and C++ language frontends. */ @@ -3320,77 +3431,8 @@ c_common_nodes_and_builtins (void) va_list_ref_type_node = build_reference_type (va_list_type_node); } -#define DEF_PRIMITIVE_TYPE(ENUM, VALUE) \ - builtin_types[ENUM] = VALUE; -#define DEF_FUNCTION_TYPE_0(ENUM, RETURN) \ - def_fn_type (ENUM, RETURN, 0, 0); -#define DEF_FUNCTION_TYPE_1(ENUM, RETURN, ARG1) \ - def_fn_type (ENUM, RETURN, 0, 1, ARG1); -#define DEF_FUNCTION_TYPE_2(ENUM, RETURN, ARG1, ARG2) \ - def_fn_type (ENUM, RETURN, 0, 2, ARG1, ARG2); -#define DEF_FUNCTION_TYPE_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ - def_fn_type (ENUM, RETURN, 0, 3, ARG1, ARG2, ARG3); -#define DEF_FUNCTION_TYPE_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ - def_fn_type (ENUM, RETURN, 0, 4, ARG1, ARG2, ARG3, ARG4); -#define DEF_FUNCTION_TYPE_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ - def_fn_type (ENUM, RETURN, 0, 5, ARG1, ARG2, ARG3, ARG4, ARG5); -#define DEF_FUNCTION_TYPE_6(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ - ARG6) \ - def_fn_type (ENUM, RETURN, 0, 6, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6); -#define DEF_FUNCTION_TYPE_7(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5, \ - ARG6, ARG7) \ - def_fn_type (ENUM, RETURN, 0, 7, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7); -#define DEF_FUNCTION_TYPE_VAR_0(ENUM, RETURN) \ - def_fn_type (ENUM, RETURN, 1, 0); -#define DEF_FUNCTION_TYPE_VAR_1(ENUM, RETURN, ARG1) \ - def_fn_type (ENUM, RETURN, 1, 1, ARG1); -#define DEF_FUNCTION_TYPE_VAR_2(ENUM, RETURN, ARG1, ARG2) \ - def_fn_type (ENUM, RETURN, 1, 2, ARG1, ARG2); -#define DEF_FUNCTION_TYPE_VAR_3(ENUM, RETURN, ARG1, ARG2, ARG3) \ - def_fn_type (ENUM, RETURN, 1, 3, ARG1, ARG2, ARG3); -#define DEF_FUNCTION_TYPE_VAR_4(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4) \ - def_fn_type (ENUM, RETURN, 1, 4, ARG1, ARG2, ARG3, ARG4); -#define DEF_FUNCTION_TYPE_VAR_5(ENUM, RETURN, ARG1, ARG2, ARG3, ARG4, ARG5) \ - def_fn_type (ENUM, RETURN, 1, 5, ARG1, ARG2, ARG3, ARG4, ARG5); -#define DEF_POINTER_TYPE(ENUM, TYPE) \ - builtin_types[(int) ENUM] = build_pointer_type (builtin_types[(int) TYPE]); - -#include "builtin-types.def" - -#undef DEF_PRIMITIVE_TYPE -#undef DEF_FUNCTION_TYPE_1 -#undef DEF_FUNCTION_TYPE_2 -#undef DEF_FUNCTION_TYPE_3 -#undef DEF_FUNCTION_TYPE_4 -#undef DEF_FUNCTION_TYPE_5 -#undef DEF_FUNCTION_TYPE_6 -#undef DEF_FUNCTION_TYPE_VAR_0 -#undef DEF_FUNCTION_TYPE_VAR_1 -#undef DEF_FUNCTION_TYPE_VAR_2 -#undef DEF_FUNCTION_TYPE_VAR_3 -#undef DEF_FUNCTION_TYPE_VAR_4 -#undef DEF_FUNCTION_TYPE_VAR_5 -#undef DEF_POINTER_TYPE - builtin_types[(int) BT_LAST] = NULL_TREE; - - c_init_attributes (); - -#define DEF_BUILTIN(ENUM, NAME, CLASS, TYPE, LIBTYPE, BOTH_P, FALLBACK_P, \ - NONANSI_P, ATTRS, IMPLICIT, COND) \ - if (NAME && COND) \ - def_builtin_1 (ENUM, NAME, CLASS, \ - builtin_types[(int) TYPE], \ - builtin_types[(int) LIBTYPE], \ - BOTH_P, FALLBACK_P, NONANSI_P, \ - built_in_attributes[(int) ATTRS], IMPLICIT); -#include "builtins.def" -#undef DEF_BUILTIN - - build_common_builtin_nodes (); - - targetm.init_builtins (); - if (flag_mudflap) - mudflap_init (); + if (!flag_preprocess_only) + c_define_builtins (va_list_ref_type_node, va_list_arg_type_node); main_identifier_node = get_identifier ("main"); diff --git a/contrib/gcc/c-common.h b/contrib/gcc/c-common.h index eea0f355b..453a2d6b2 100644 --- a/contrib/gcc/c-common.h +++ b/contrib/gcc/c-common.h @@ -654,7 +654,7 @@ extern void binary_op_error (enum tree_code); extern tree fix_string_type (tree); struct varray_head_tag; extern void constant_expression_warning (tree); -extern void strict_aliasing_warning(tree, tree, tree); +extern bool strict_aliasing_warning (tree, tree, tree); extern void empty_body_warning (tree, tree); extern tree convert_and_check (tree, tree); extern void overflow_warning (tree); diff --git a/contrib/gcc/c-opts.c b/contrib/gcc/c-opts.c index 7f193749f..0c584eb8c 100644 --- a/contrib/gcc/c-opts.c +++ b/contrib/gcc/c-opts.c @@ -396,7 +396,7 @@ c_common_handle_option (size_t scode, const char *arg, int value) if (c_dialect_cxx ()) warn_sign_compare = value; warn_switch = value; - warn_strict_aliasing = value; + set_warn_strict_aliasing (value); warn_strict_overflow = value; warn_address = value; @@ -606,6 +606,10 @@ c_common_handle_option (size_t scode, const char *arg, int value) disable_builtin_function (arg); break; + case OPT_fdirectives_only: + cpp_opts->directives_only = 1; + break; + case OPT_fdollars_in_identifiers: cpp_opts->dollars_in_ident = value; break; @@ -1329,6 +1333,11 @@ sanitize_cpp_opts (void) if (flag_dump_macros == 'M') flag_no_output = 1; + /* By default, -fdirectives-only implies -dD. This allows subsequent phases + to perform proper macro expansion. */ + if (cpp_opts->directives_only && !cpp_opts->preprocessed && !flag_dump_macros) + flag_dump_macros = 'D'; + /* Disable -dD, -dN and -dI if normal output is suppressed. Allow -dM since at least glibc relies on -M -dM to work. */ /* Also, flag_no_output implies flag_no_line_commands, always. */ @@ -1359,6 +1368,14 @@ sanitize_cpp_opts (void) actually output the current directory? */ if (flag_working_directory == -1) flag_working_directory = (debug_info_level != DINFO_LEVEL_NONE); + + if (cpp_opts->directives_only) + { + if (warn_unused_macros) + error ("-fdirectives-only is incompatible with -Wunused_macros"); + if (cpp_opts->traditional) + error ("-fdirectives-only is incompatible with -traditional"); + } } /* Add include path with a prefix at the front of its name. */ @@ -1442,6 +1459,8 @@ finish_options (void) } } } + else if (cpp_opts->directives_only) + cpp_init_special_builtins (parse_in); include_cursor = 0; push_command_line_include (); diff --git a/contrib/gcc/c-ppoutput.c b/contrib/gcc/c-ppoutput.c index 10540b7f1..62b350b9e 100644 --- a/contrib/gcc/c-ppoutput.c +++ b/contrib/gcc/c-ppoutput.c @@ -41,6 +41,8 @@ static struct /* General output routines. */ static void scan_translation_unit (cpp_reader *); +static void print_lines_directives_only (int, const void *, size_t); +static void scan_translation_unit_directives_only (cpp_reader *); static void scan_translation_unit_trad (cpp_reader *); static void account_for_newlines (const unsigned char *, size_t); static int dump_macro (cpp_reader *, cpp_hashnode *, void *); @@ -75,6 +77,9 @@ preprocess_file (cpp_reader *pfile) } else if (cpp_get_options (pfile)->traditional) scan_translation_unit_trad (pfile); + else if (cpp_get_options (pfile)->directives_only + && !cpp_get_options (pfile)->preprocessed) + scan_translation_unit_directives_only (pfile); else scan_translation_unit (pfile); @@ -179,6 +184,26 @@ scan_translation_unit (cpp_reader *pfile) } } +static void +print_lines_directives_only (int lines, const void *buf, size_t size) +{ + print.src_line += lines; + fwrite (buf, 1, size, print.outf); +} + +/* Writes out the preprocessed file, handling spacing and paste + avoidance issues. */ +static void +scan_translation_unit_directives_only (cpp_reader *pfile) +{ + struct _cpp_dir_only_callbacks cb; + + cb.print_lines = print_lines_directives_only; + cb.maybe_print_line = maybe_print_line; + + _cpp_preprocess_dir_only (pfile, &cb); +} + /* Adjust print.src_line for newlines embedded in output. */ static void account_for_newlines (const unsigned char *str, size_t len) diff --git a/contrib/gcc/c-typeck.c b/contrib/gcc/c-typeck.c index e10a2224f..897c1e9be 100644 --- a/contrib/gcc/c-typeck.c +++ b/contrib/gcc/c-typeck.c @@ -1876,6 +1876,19 @@ build_indirect_ref (tree ptr, const char *errorstring) if (TREE_CODE (type) == POINTER_TYPE) { + if (TREE_CODE (pointer) == CONVERT_EXPR + || TREE_CODE (pointer) == NOP_EXPR + || TREE_CODE (pointer) == VIEW_CONVERT_EXPR) + { + /* If a warning is issued, mark it to avoid duplicates from + the backend. This only needs to be done at + warn_strict_aliasing > 2. */ + if (warn_strict_aliasing > 2) + if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (pointer, 0)), + type, TREE_OPERAND (pointer, 0))) + TREE_NO_WARNING (pointer) = 1; + } + if (TREE_CODE (pointer) == ADDR_EXPR && (TREE_TYPE (TREE_OPERAND (pointer, 0)) == TREE_TYPE (type))) @@ -3562,7 +3575,8 @@ build_c_cast (tree type, tree expr) warning (OPT_Wint_to_pointer_cast, "cast to pointer from integer " "of different size"); - strict_aliasing_warning (otype, type, expr); + if (warn_strict_aliasing <= 2) + strict_aliasing_warning (otype, type, expr); /* If pedantic, warn for conversions between function and object pointer types, except for converting a null pointer constant diff --git a/contrib/gcc/c.opt b/contrib/gcc/c.opt index 820e5c986..80b5312df 100644 --- a/contrib/gcc/c.opt +++ b/contrib/gcc/c.opt @@ -494,6 +494,10 @@ fdefault-inline C++ ObjC++ Inline member functions by default +fdirectives-only +C ObjC C++ ObjC++ +Preprocess directives only. + fdollars-in-identifiers C ObjC C++ ObjC++ Permit '$' as an identifier character diff --git a/contrib/gcc/common.opt b/contrib/gcc/common.opt index 2270d0d8e..7dd3909aa 100644 --- a/contrib/gcc/common.opt +++ b/contrib/gcc/common.opt @@ -95,7 +95,11 @@ Warn when an inlined function cannot be inlined Wlarger-than- Common RejectNegative Joined UInteger --Wlarger-than- Warn if an object is larger than bytes +-Wlarger-than- Warn if an object is larger than bytes + +Wframe-larger-than- +Common RejectNegative Joined UInteger +-Wframe-larger-than- Warn if the frame size of a function is larger than bytes Wunsafe-loop-optimizations Common Var(warn_unsafe_loop_optimizations) @@ -537,6 +541,14 @@ finstrument-functions Common Report Var(flag_instrument_function_entry_exit) Instrument function entry and exit with profiling calls +finstrument-functions-exclude-function-list= +Common RejectNegative Joined +-finstrument-functions-exclude-function-list=name,... Do not instrument listed functions + +finstrument-functions-exclude-file-list= +Common RejectNegative Joined +-finstrument-functions-exclude-file-list=filename,... Do not instrument functions listed in files + fipa-cp Common Report Var(flag_ipa_cp) Perform Interprocedural constant propagation diff --git a/contrib/gcc/coverage.c b/contrib/gcc/coverage.c index 5eaf4888f..101c2acb6 100644 --- a/contrib/gcc/coverage.c +++ b/contrib/gcc/coverage.c @@ -429,57 +429,75 @@ tree_coverage_counter_ref (unsigned counter, unsigned no) static unsigned coverage_checksum_string (unsigned chksum, const char *string) { - int i; char *dup = NULL; + char *ptr; /* Look for everything that looks if it were produced by get_file_function_name_long and zero out the second part that may result from flag_random_seed. This is not critical as the checksums are used only for sanity checking. */ - for (i = 0; string[i]; i++) +#define GLOBAL_PREFIX "_GLOBAL__" +#define TRAILING_N "N_" +#define ISCAPXDIGIT(a) (((a) >= '0' && (a) <= '9') || ((a) >= 'A' && (a) <= 'F')) + if ((ptr = strstr (string, GLOBAL_PREFIX))) { - int offset = 0; - if (!strncmp (string + i, "_GLOBAL__N_", 11)) - offset = 11; - if (!strncmp (string + i, "_GLOBAL__", 9)) - offset = 9; - - /* C++ namespaces do have scheme: - _GLOBAL__N___functionname - since filename might contain extra underscores there seems - to be no better chance then walk all possible offsets looking - for magicnuber. */ - if (offset) - { - for (i = i + offset; string[i]; i++) - if (string[i]=='_') - { - int y; - - for (y = 1; y < 9; y++) - if (!(string[i + y] >= '0' && string[i + y] <= '9') - && !(string[i + y] >= 'A' && string[i + y] <= 'F')) - break; - if (y != 9 || string[i + 9] != '_') - continue; - for (y = 10; y < 18; y++) - if (!(string[i + y] >= '0' && string[i + y] <= '9') - && !(string[i + y] >= 'A' && string[i + y] <= 'F')) - break; - if (y != 18) - continue; - if (!dup) - string = dup = xstrdup (string); - for (y = 10; y < 18; y++) - dup[i + y] = '0'; - } - break; - } + /* Skip _GLOBAL__. */ + ptr += strlen (GLOBAL_PREFIX); + + /* Skip optional N_ (in case __GLOBAL_N__). */ + if (!strncmp (ptr, TRAILING_N, strlen (TRAILING_N))) + ptr += strlen (TRAILING_N); + /* At this point, ptr should point after "_GLOBAL__N_" or "_GLOBAL__". */ + + while ((ptr = strchr (ptr, '_')) != NULL) + { + int y; + /* For every "_" in the rest of the string, + try the follwing pattern matching */ + + /* Skip over '_'. */ + ptr++; +#define NDIGITS (8) + /* Try matching the pattern: + <8-digit hex>_<8-digit hex> + The second number is randomly generated + so we want to mask it out before computing the checksum. */ + for (y = 0; *ptr != 0 && y < NDIGITS; y++, ptr++) + if (!ISCAPXDIGIT (*ptr)) + break; + if (y != NDIGITS || *ptr != '_') + continue; + /* Skip over '_' again. */ + ptr++; + for (y = 0; *ptr != 0 && y < NDIGITS; y++, ptr++) + if (!ISCAPXDIGIT (*ptr)) + break; + + if (y == NDIGITS) + { + /* We have a match. + Duplicate the string and mask out + the second 8-digit number. */ + dup = xstrdup (string); + ptr = dup + (ptr - string); + for(y = -NDIGITS - 1 ; y < 0; y++) + { + ptr[y] = '0'; + } + ptr = dup; + break; + } + } + /* "ptr" should be NULL if we couldn't find the match + (strchr will return NULL if no match is found), + or it should point to dup which contains the string + with the random part masked. */ } - chksum = crc32_string (chksum, string); + chksum = crc32_string (chksum, (ptr) ? ptr : string); + if (dup) - free (dup); + free (dup); return chksum; } diff --git a/contrib/gcc/cp/typeck.c b/contrib/gcc/cp/typeck.c index dd92b2237..d17d0bb52 100644 --- a/contrib/gcc/cp/typeck.c +++ b/contrib/gcc/cp/typeck.c @@ -2334,6 +2334,19 @@ build_indirect_ref (tree ptr, const char *errorstring) types. */ tree t = canonical_type_variant (TREE_TYPE (type)); + if (TREE_CODE (ptr) == CONVERT_EXPR + || TREE_CODE (ptr) == NOP_EXPR + || TREE_CODE (ptr) == VIEW_CONVERT_EXPR) + { + /* If a warning is issued, mark it to avoid duplicates from + the backend. This only needs to be done at + warn_strict_aliasing > 2. */ + if (warn_strict_aliasing > 2) + if (strict_aliasing_warning (TREE_TYPE (TREE_OPERAND (ptr, 0)), + type, TREE_OPERAND (ptr, 0))) + TREE_NO_WARNING (ptr) = 1; + } + if (VOID_TYPE_P (t)) { /* A pointer to incomplete type (other than cv void) can be @@ -5256,7 +5269,8 @@ build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p, /* We need to strip nops here, because the frontend likes to create (int *)&a for array-to-pointer decay, instead of &a[0]. */ STRIP_NOPS (sexpr); - strict_aliasing_warning (intype, type, sexpr); + if (warn_strict_aliasing <= 2) + strict_aliasing_warning (intype, type, sexpr); return fold_if_not_in_template (build_nop (type, expr)); } diff --git a/contrib/gcc/doc/cppopts.texi b/contrib/gcc/doc/cppopts.texi index 024d25504..69738cc22 100644 --- a/contrib/gcc/doc/cppopts.texi +++ b/contrib/gcc/doc/cppopts.texi @@ -506,6 +506,22 @@ Search @var{dir} only for header files requested with @xref{Search Path}. @end ifset +@item -fdirectives-only +@opindex fdirectives-only +This option provides a simplified preprocessor to improve the +performance of distributed build systems such as distcc. It's +behavior depends on a number of other flags. + +If the @option{-E} option is enabled, it suppresses things like macro +expansion, trigraph conversion, and escaped newline splicing +outside of directives. All directives are processed normally, except that +macro definitions are output similar to the @option{-dD} option. + +If the @option{-fpreprocessed} option is enabled, it suppresses +predefinition of most builtin and command line macros. This +prevents duplicate definition of macros output with the @option{-E} +option. + @item -fdollars-in-identifiers @opindex fdollars-in-identifiers @anchor{fdollars-in-identifiers} diff --git a/contrib/gcc/doc/invoke.texi b/contrib/gcc/doc/invoke.texi index ce6f07190..51ac30f0d 100644 --- a/contrib/gcc/doc/invoke.texi +++ b/contrib/gcc/doc/invoke.texi @@ -214,7 +214,8 @@ in the following sections. -Wimport -Wno-import -Winit-self -Winline @gol -Wno-int-to-pointer-cast @gol -Wno-invalid-offsetof -Winvalid-pch @gol --Wlarger-than-@var{len} -Wunsafe-loop-optimizations -Wlong-long @gol +-Wlarger-than-@var{len} -Wframe-larger-than-@var{len} @gol +-Wunsafe-loop-optimizations -Wlong-long @gol -Wmain -Wmissing-braces -Wmissing-field-initializers @gol -Wmissing-format-attribute -Wmissing-include-dirs @gol -Wmissing-noreturn @gol @@ -758,6 +759,8 @@ See S/390 and zSeries Options. -fnon-call-exceptions -funwind-tables @gol -fasynchronous-unwind-tables @gol -finhibit-size-directive -finstrument-functions @gol +-finstrument-functions-exclude-function-list=@var{sym},@var{sym},@dots{} @gol +-finstrument-functions-exclude-file-list=@var{file},@var{file},@dots{} @gol -fno-common -fno-ident @gol -fpcc-struct-return -fpic -fPIC -fpie -fPIE @gol -fno-jump-tables @gol @@ -2505,14 +2508,40 @@ It warns about code which might break the strict aliasing rules that the compiler is using for optimization. The warning does not catch all cases, but does attempt to catch the more common pitfalls. It is included in @option{-Wall}. +It is equivalent to -Wstrict-aliasing=3 -@item -Wstrict-aliasing=2 -@opindex Wstrict-aliasing=2 +@item -Wstrict-aliasing=n +@opindex Wstrict-aliasing=n This option is only active when @option{-fstrict-aliasing} is active. It warns about code which might break the strict aliasing rules that the -compiler is using for optimization. This warning catches more cases than -@option{-Wstrict-aliasing}, but it will also give a warning for some ambiguous -cases that are safe. +compiler is using for optimization. +Higher levels correspond to higher accuracy (fewer false positives). +Higher levels also correspond to more effort, similar to the way -O works. +@option{-Wstrict-aliasing} is equivalent to @option{-Wstrict-aliasing=n}, +with n=3. + +Level 1: Most aggressive, quick, least accurate. +Possibly useful when higher levels +do not warn but -fstrict-aliasing still breaks the code, as it has very few +false negatives. However, it has many false positives. +Warns for all pointer conversions between possibly incompatible types, +even if never dereferenced. Runs in the frontend only. + +Level 2: Aggressive, quick, not too precise. +May still have many false positives (not as many as level 1 though), +and few false negatives (but possibly more than level 1). +Unlike level 1, it only warns when an address is taken. Warns about +incomplete types. Runs in the frontend only. + +Level 3 (default for @option{-Wstrict-aliasing}): +Should have very few false positives and few false +negatives. Slightly slower than levels 1 or 2 when optimization is enabled. +Takes care of the common punn+dereference pattern in the frontend: +@code{*(int*)&some_float}. +If optimization is enabled, it also runs in the backend, where it deals +with multiple statement cases using flow-sensitive points-to information. +Only warns when the converted pointer is dereferenced. +Does not warn about incomplete types. @item -Wstrict-overflow @item -Wstrict-overflow=@var{n} @@ -2828,6 +2857,10 @@ global variable or whenever a built-in function is shadowed. @opindex Wlarger-than Warn whenever an object of larger than @var{len} bytes is defined. +@item -Wframe-larger-than-@var{len} +@opindex Wframe-larger-than +Warn whenever the frame size of a function is larger than @var{len} bytes. + @item -Wunsafe-loop-optimizations @opindex Wunsafe-loop-optimizations Warn if the loop cannot be optimized because the compiler could not @@ -13355,6 +13388,37 @@ interrupt routines, and any functions from which the profiling functions cannot safely be called (perhaps signal handlers, if the profiling routines generate output or allocate memory). +@item -finstrument-functions-exclude-file-list=@var{file},@var{file},@dots{} +@opindex finstrument-functions-exclude-file-list + +Set the list of functions that are excluded from instrumentation (see +the description of @code{-finstrument-functions}). If the file that +contains a function definition matches with one of @var{file}, then +that function is not instrumented. The match is done on substrings: +if the @var{file} parameter is a substring of the file name, it is +considered to be a match. + +For example, +@code{-finstrument-functions-exclude-file-list=/bits/stl,include/sys} +will exclude any inline function defined in files whose pathnames +contain @code{/bits/stl} or @code{include/sys}. + +If, for some reason, you want to include letter @code{','} in one of +@var{sym}, write @code{'\,'}. For example, +@code{-finstrument-functions-exclude-file-list='\,\,tmp'} +(note the single quote surrounding the option). + +@item -finstrument-functions-exclude-function-list=@var{sym},@var{sym},@dots{} +@opindex finstrument-functions-exclude-function-list + +This is similar to @code{-finstrument-functions-exclude-file-list}, +but this option sets the list of function names to be excluded from +instrumentation. The function name to be matched is its user-visible +name, such as @code{vector blah(const vector &)}, not the +internal mangled name (e.g., @code{_Z4blahRSt6vectorIiSaIiEE}). The +match is done on substrings: if the @var{sym} parameter is a substring +of the function name, it is considered to be a match. + @item -fstack-check @opindex fstack-check Generate code to verify that you do not go beyond the boundary of the @@ -13932,3 +13996,4 @@ exist, because otherwise they won't get converted. @xref{Protoize Caveats}, for more information on how to use @code{protoize} successfully. + diff --git a/contrib/gcc/final.c b/contrib/gcc/final.c index d64f0c3e2..ef50ed933 100644 --- a/contrib/gcc/final.c +++ b/contrib/gcc/final.c @@ -1425,6 +1425,15 @@ final_start_function (rtx first ATTRIBUTE_UNUSED, FILE *file, TREE_ASM_WRITTEN (DECL_INITIAL (current_function_decl)) = 1; } + if (warn_frame_larger_than + && get_frame_size () > frame_larger_than_size) + { + /* Issue a warning */ + warning (OPT_Wframe_larger_than_, + "the frame size of %wd bytes is larger than %wd bytes", + get_frame_size (), frame_larger_than_size); + } + /* First output the function prologue: code to set up the stack frame. */ targetm.asm_out.function_prologue (file, get_frame_size ()); @@ -4083,4 +4092,3 @@ struct tree_opt_pass pass_clean_state = 0, /* todo_flags_finish */ 0 /* letter */ }; - diff --git a/contrib/gcc/flags.h b/contrib/gcc/flags.h index d94c76ca8..6bcd0289e 100644 --- a/contrib/gcc/flags.h +++ b/contrib/gcc/flags.h @@ -122,6 +122,15 @@ extern bool extra_warnings; extern void set_Wunused (int setting); +/* Used to set the level of -Wstrict-aliasing, when no level is specified. + The external way to set the default level is to use + -Wstrict-aliasing=level. + ONOFF is assumed to take value 1 when -Wstrict-aliasing is specified, + and 0 otherwise. After calling this function, wstrict_aliasing will be + set to the default value of -Wstrict_aliasing=level. */ + +extern void set_warn_strict_aliasing (int onoff); + /* Nonzero means warn about any objects definitions whose size is larger than N bytes. Also want about function definitions whose returned values are larger than N bytes. The value N is in `larger_than_size'. */ @@ -129,6 +138,12 @@ extern void set_Wunused (int setting); extern bool warn_larger_than; extern HOST_WIDE_INT larger_than_size; +/* Nonzero means warn about any function whose frame size is larger + than N bytes. */ + +extern bool warn_frame_larger_than; +extern HOST_WIDE_INT frame_larger_than_size; + /* Nonzero means warn about constructs which might not be strict aliasing safe. */ @@ -287,6 +302,10 @@ extern const char *flag_random_seed; #define abi_version_at_least(N) \ (flag_abi_version == 0 || flag_abi_version >= (N)) +/* Return whether the function should be excluded from + instrumentation. */ +extern bool flag_instrument_functions_exclude_p (tree fndecl); + /* True if the given mode has a NaN representation and the treatment of NaN operands is important. Certain optimizations, such as folding x * 0 into 0, are not correct for NaN operands, and are normally diff --git a/contrib/gcc/gimplify.c b/contrib/gcc/gimplify.c index 7efae388c..be45f478f 100644 --- a/contrib/gcc/gimplify.c +++ b/contrib/gcc/gimplify.c @@ -6397,7 +6397,8 @@ gimplify_function_tree (tree fndecl) catch the exit hook. */ /* ??? Add some way to ignore exceptions for this TFE. */ if (flag_instrument_function_entry_exit - && ! DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl)) + && !DECL_NO_INSTRUMENT_FUNCTION_ENTRY_EXIT (fndecl) + && !flag_instrument_functions_exclude_p (fndecl)) { tree tf, x, bind; diff --git a/contrib/gcc/opts.c b/contrib/gcc/opts.c index 1ded3c711..87f8984a1 100644 --- a/contrib/gcc/opts.c +++ b/contrib/gcc/opts.c @@ -59,6 +59,11 @@ bool extra_warnings; bool warn_larger_than; HOST_WIDE_INT larger_than_size; +/* True to warn about any function whose frame size is larger + * than N bytes. */ +bool warn_frame_larger_than; +HOST_WIDE_INT frame_larger_than_size; + /* Nonzero means warn about constructs which might not be strict-aliasing safe. */ int warn_strict_aliasing; @@ -358,6 +363,15 @@ static bool flag_unroll_loops_set, flag_tracer_set; static bool flag_value_profile_transformations_set; static bool flag_peel_loops_set, flag_branch_probabilities_set; +/* Functions excluded from profiling. */ + +typedef char *char_p; /* For DEF_VEC_P. */ +DEF_VEC_P(char_p); +DEF_VEC_ALLOC_P(char_p,heap); + +static VEC(char_p,heap) *flag_instrument_functions_exclude_functions; +static VEC(char_p,heap) *flag_instrument_functions_exclude_files; + /* Input file names. */ const char **in_fnames; unsigned num_in_fnames; @@ -604,6 +618,87 @@ add_input_filename (const char *filename) in_fnames[num_in_fnames - 1] = filename; } +/* Add functions or file names to a vector of names to exclude from + instrumentation. */ + +static void +add_instrument_functions_exclude_list (VEC(char_p,heap) **pvec, + const char* arg) +{ + char *tmp; + char *r; + char *w; + char *token_start; + + /* We never free this string. */ + tmp = xstrdup (arg); + + r = tmp; + w = tmp; + token_start = tmp; + + while (*r != '\0') + { + if (*r == ',') + { + *w++ = '\0'; + ++r; + VEC_safe_push (char_p, heap, *pvec, token_start); + token_start = w; + } + if (*r == '\\' && r[1] == ',') + { + *w++ = ','; + r += 2; + } + else + *w++ = *r++; + } + if (*token_start != '\0') + VEC_safe_push (char_p, heap, *pvec, token_start); +} + +/* Return whether we should exclude FNDECL from instrumentation. */ + +bool +flag_instrument_functions_exclude_p (tree fndecl) +{ + if (VEC_length (char_p, flag_instrument_functions_exclude_functions) > 0) + { + const char *name; + int i; + char *s; + + name = lang_hooks.decl_printable_name (fndecl, 0); + for (i = 0; + VEC_iterate (char_p, flag_instrument_functions_exclude_functions, + i, s); + ++i) + { + if (strstr (name, s) != NULL) + return true; + } + } + + if (VEC_length (char_p, flag_instrument_functions_exclude_files) > 0) + { + const char *name; + int i; + char *s; + + name = DECL_SOURCE_FILE (fndecl); + for (i = 0; + VEC_iterate (char_p, flag_instrument_functions_exclude_files, i, s); + ++i) + { + if (strstr (name, s) != NULL) + return true; + } + } + + return false; +} + /* Decode and handle the vector of command line options. LANG_MASK contains has a single bit set representing the current language. */ @@ -979,7 +1074,15 @@ common_handle_option (size_t scode, const char *arg, int value, warn_larger_than = value != -1; break; + case OPT_Wframe_larger_than_: + frame_larger_than_size = value; + warn_frame_larger_than = value != -1; + break; + case OPT_Wstrict_aliasing: + set_warn_strict_aliasing (value); + break; + case OPT_Wstrict_aliasing_: warn_strict_aliasing = value; break; @@ -1086,6 +1189,16 @@ common_handle_option (size_t scode, const char *arg, int value, set_param_value ("max-inline-insns-auto", value / 2); break; + case OPT_finstrument_functions_exclude_function_list_: + add_instrument_functions_exclude_list + (&flag_instrument_functions_exclude_functions, arg); + break; + + case OPT_finstrument_functions_exclude_file_list_: + add_instrument_functions_exclude_list + (&flag_instrument_functions_exclude_files, arg); + break; + case OPT_fmessage_length_: pp_set_line_maximum_length (global_dc->printer, value); break; @@ -1348,6 +1461,20 @@ set_Wunused (int setting) warn_unused_value = setting; } +/* Used to set the level of strict aliasing warnings, + when no level is specified (i.e., when -Wstrict-aliasing, and not + -Wstrict-aliasing=level was given). + ONOFF is assumed to take value 1 when -Wstrict-aliasing is specified, + and 0 otherwise. After calling this function, wstrict_aliasing will be + set to the default value of -Wstrict_aliasing=level, currently 3. */ +void +set_warn_strict_aliasing (int onoff) +{ + gcc_assert (onoff == 0 || onoff == 1); + if (onoff != 0) + warn_strict_aliasing = 3; +} + /* The following routines are useful in setting all the flags that -ffast-math and -fno-fast-math imply. */ void diff --git a/contrib/gcc/tree-flow.h b/contrib/gcc/tree-flow.h index 1d03188e8..ac8d9d288 100644 --- a/contrib/gcc/tree-flow.h +++ b/contrib/gcc/tree-flow.h @@ -694,6 +694,8 @@ static inline bool overlap_subvar (unsigned HOST_WIDE_INT, definition, a function with this prototype is called. */ typedef bool (*walk_use_def_chains_fn) (tree, tree, void *); +/* In tree-ssa-alias-warnings.c */ +extern void strict_aliasing_warning_backend (void); /* In tree-ssa.c */ extern void init_tree_ssa (void); diff --git a/contrib/gcc/tree-ssa-alias-warnings.c b/contrib/gcc/tree-ssa-alias-warnings.c new file mode 100644 index 000000000..0c7875219 --- /dev/null +++ b/contrib/gcc/tree-ssa-alias-warnings.c @@ -0,0 +1,1036 @@ +/* Strict aliasing checks. + Copyright (C) 2007 Free Software Foundation, Inc. + Contributed by Silvius Rus . + + This file is part of GCC. + + GCC is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GCC is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GCC; see the file COPYING. If not, write to + the Free Software Foundation, 51 Franklin Street, Fifth Floor, + Boston, MA 02110-1301, USA. */ + +#include "config.h" +#include "system.h" +#include "coretypes.h" +#include "tm.h" +#include "alloc-pool.h" +#include "tree.h" +#include "tree-dump.h" +#include "tree-flow.h" +#include "params.h" +#include "function.h" +#include "expr.h" +#include "toplev.h" +#include "diagnostic.h" +#include "tree-ssa-structalias.h" +#include "tree-ssa-propagate.h" +#include "langhooks.h" + +/* Module to issue a warning when a program uses data through a type + different from the type through which the data were defined. + Implements -Wstrict-aliasing and -Wstrict-aliasing=n. + These checks only happen when -fstrict-aliasing is present. + + The idea is to use the compiler to identify occurrences of nonstandard + aliasing, and report them to programmers. Programs free of such aliasing + are more portable, maintainable, and can usually be optimized better. + + The current, as of April 2007, C and C++ language standards forbid + accessing data of type A through an lvalue of another type B, + with certain exceptions. See the C Standard ISO/IEC 9899:1999, + section 6.5, paragraph 7, and the C++ Standard ISO/IEC 14882:1998, + section 3.10, paragraph 15. + + Example 1:*a is used as int but was defined as a float, *b. + int* a = ...; + float* b = reinterpret_cast (a); + *b = 2.0; + return *a + + Unfortunately, the problem is in general undecidable if we take into + account arithmetic expressions such as array indices or pointer arithmetic. + (It is at least as hard as Peano arithmetic decidability.) + Even ignoring arithmetic, the problem is still NP-hard, because it is + at least as hard as flow-insensitive may-alias analysis, which was proved + NP-hard by Horwitz et al, TOPLAS 1997. + + It is clear that we need to choose some heuristics. + Unfortunately, various users have different goals which correspond to + different time budgets so a common approach will not suit all. + We present the user with three effort/accuracy levels. By accuracy, we mean + a common-sense mix of low count of false positives with a + reasonably low number of false negatives. We are heavily biased + towards a low count of false positives. + The effort (compilation time) is likely to increase with the level. + + -Wstrict-aliasing=1 + =================== + Most aggressive, least accurate. Possibly useful when higher levels + do not warn but -fstrict-aliasing still breaks the code, as + it has very few false negatives. + Warn for all bad pointer conversions, even if never dereferenced. + Implemented in the front end (c-common.c). + Uses alias_sets_might_conflict to compare types. + + -Wstrict-aliasing=2 + =================== + Aggressive, not too precise. + May still have many false positives (not as many as level 1 though), + and few false negatives (but possibly more than level 1). + Runs only in the front end. Uses alias_sets_might_conflict to + compare types. Does not check for pointer dereferences. + Only warns when an address is taken. Warns about incomplete type punning. + + -Wstrict-aliasing=3 (default) + =================== + Should have very few false positives and few false negatives. + Takes care of the common punn+dereference pattern in the front end: + *(int*)&some_float. + Takes care of multiple statement cases in the back end, + using flow-sensitive points-to information (-O required). + Uses alias_sets_conflict_p to compare types and only warns + when the converted pointer is dereferenced. + Does not warn about incomplete type punning. + + Future improvements can be included by adding higher levels. + + In summary, expression level analysis is performed in the front-end, + and multiple-statement analysis is performed in the backend. + The remainder of this discussion is only about the backend analysis. + + This implementation uses flow-sensitive points-to information. + Flow-sensitivity refers to accesses to the pointer, and not the object + pointed. For instance, we do not warn about the following case. + + Example 2. + int* a = (int*)malloc (...); + float* b = reinterpret_cast (a); + *b = 2.0; + a = (int*)malloc (...); + return *a; + + In SSA, it becomes clear that the INT value *A_2 referenced in the + return statement is not aliased to the FLOAT defined through *B_1. + int* a_1 = (int*)malloc (...); + float* b_1 = reinterpret_cast (a_1); + *b_1 = 2.0; + a_2 = (int*)malloc (...); + return *a_2; + + + Algorithm Outline + ================= + + ForEach (ptr, object) in the points-to table + If (incompatible_types (*ptr, object)) + If (referenced (ptr, current function) + and referenced (object, current function)) + Issue warning (ptr, object, reference locations) + + The complexity is: + O (sizeof (points-to table) + + sizeof (function body) * lookup_time (points-to table)) + + Pointer dereference locations are looked up on demand. The search is + a single scan of the function body, in which all references to pointers + and objects in the points-to table are recorded. However, this dominant + time factor occurs rarely, only when cross-type aliasing was detected. + + + Limitations of the Proposed Implementation + ========================================== + + 1. We do not catch the following case, because -fstrict-aliasing will + associate different tags with MEM while building points-to information, + thus before we get to analyze it. + XXX: this could be solved by either running with -fno-strict-aliasing + or by recording the points-to information before splitting the orignal + tag based on type. + + Example 3. + void* mem = malloc (...); + int* pi = reinterpret_cast (mem); + float* b = reinterpret_cast (mem); + *b = 2.0; + return *pi+1; + + 2. We do not check whether the two conflicting (de)references can + reach each other in the control flow sense. If we fixed limitation + 1, we would wrongly issue a warning in the following case. + + Example 4. + void* raw = malloc (...); + if (...) { + float* b = reinterpret_cast (raw); + *b = 2.0; + return (int)*b; + } else { + int* a = reinterpret_cast (raw); + *a = 1; + return *a; + + 3. Only simple types are compared, thus no structures, unions or classes + are analyzed. A first attempt to deal with structures introduced much + complication and has not showed much improvement in preliminary tests, + so it was left out. + + 4. All analysis is intraprocedural. */ + + +/* Local declarations. */ +static void find_references_in_function (void); + + + +/* Get main type of tree TYPE, stripping array dimensions and qualifiers. */ + +static tree +get_main_type (tree type) +{ + while (TREE_CODE (type) == ARRAY_TYPE) + type = TREE_TYPE (type); + return TYPE_MAIN_VARIANT (type); +} + + +/* Get the type of the given object. If IS_PTR is true, get the type of the + object pointed to or referenced by OBJECT instead. + For arrays, return the element type. Ignore all qualifiers. */ + +static tree +get_otype (tree object, bool is_ptr) +{ + tree otype = TREE_TYPE (object); + + if (is_ptr) + { + gcc_assert (POINTER_TYPE_P (otype)); + otype = TREE_TYPE (otype); + } + return get_main_type (otype); +} + + +/* Return true if tree TYPE is struct, class or union. */ + +static bool +struct_class_union_p (tree type) +{ + return (TREE_CODE (type) == RECORD_TYPE + || TREE_CODE (type) == UNION_TYPE + || TREE_CODE (type) == QUAL_UNION_TYPE); +} + + + +/* Keep data during a search for an aliasing site. + RHS = object or pointer aliased. No LHS is specified because we are only + looking in the UseDef paths of a given variable, so LHS will always be + an SSA name of the same variable. + When IS_RHS_POINTER = true, we are looking for ... = RHS. Otherwise, + we are looking for ... = &RHS. + SITE is the output of a search, non-NULL if the search succeeded. */ + +struct alias_match +{ + tree rhs; + bool is_rhs_pointer; + tree site; +}; + + +/* Callback for find_alias_site. Return true if the right hand site + of STMT matches DATA. */ + +static bool +find_alias_site_helper (tree var ATTRIBUTE_UNUSED, tree stmt, void *data) +{ + struct alias_match *match = (struct alias_match *) data; + tree rhs_pointer = get_rhs (stmt); + tree to_match = NULL_TREE; + + while (TREE_CODE (rhs_pointer) == NOP_EXPR + || TREE_CODE (rhs_pointer) == CONVERT_EXPR + || TREE_CODE (rhs_pointer) == VIEW_CONVERT_EXPR) + rhs_pointer = TREE_OPERAND (rhs_pointer, 0); + + if (!rhs_pointer) + /* Not a type conversion. */ + return false; + + if (TREE_CODE (rhs_pointer) == ADDR_EXPR && !match->is_rhs_pointer) + to_match = TREE_OPERAND (rhs_pointer, 0); + else if (POINTER_TYPE_P (rhs_pointer) && match->is_rhs_pointer) + to_match = rhs_pointer; + + if (to_match != match->rhs) + /* Type conversion, but not a name match. */ + return false; + + /* Found it. */ + match->site = stmt; + return true; +} + + +/* Find the statement where OBJECT1 gets aliased to OBJECT2. + If IS_PTR2 is true, consider OBJECT2 to be the name of a pointer or + reference rather than the actual aliased object. + For now, just implement the case where OBJECT1 is an SSA name defined + by a PHI statement. */ + +static tree +find_alias_site (tree object1, bool is_ptr1 ATTRIBUTE_UNUSED, + tree object2, bool is_ptr2) +{ + struct alias_match match; + + match.rhs = object2; + match.is_rhs_pointer = is_ptr2; + match.site = NULL_TREE; + + if (TREE_CODE (object1) != SSA_NAME) + return NULL_TREE; + + walk_use_def_chains (object1, find_alias_site_helper, &match, false); + return match.site; +} + + +/* Structure to store temporary results when trying to figure out whether + an object is referenced. Just its presence in the text is not enough, + as we may just be taking its address. */ + +struct match_info +{ + tree object; + bool is_ptr; + /* The difference between the number of references to OBJECT + and the number of occurences of &OBJECT. */ + int found; +}; + + +/* Return the base if EXPR is an SSA name. Return EXPR otherwise. */ + +static tree +get_ssa_base (tree expr) +{ + if (TREE_CODE (expr) == SSA_NAME) + return SSA_NAME_VAR (expr); + else + return expr; +} + + +/* Record references to objects and pointer dereferences across some piece of + code. The number of references is recorded for each item. + References to an object just to take its address are not counted. + For instance, if PTR is a pointer and OBJ is an object: + 1. Expression &obj + *ptr will have the following reference match structure: + ptrs: + objs: + OBJ does not appear as referenced because we just take its address. + 2. Expression ptr + *ptr will have the following reference match structure: + ptrs: + objs: + PTR shows up twice as an object, but is dereferenced only once. + + The elements of the hash tables are tree_map objects. */ +struct reference_matches +{ + htab_t ptrs; + htab_t objs; +}; + + +/* Return the match, if any. Otherwise, return NULL_TREE. It will + return NULL_TREE even when a match was found, if the value associated + to KEY is NULL_TREE. */ + +static inline tree +match (htab_t ref_map, tree key) +{ + struct tree_map to_find; + struct tree_map *found; + void **slot = NULL; + + to_find.from = key; + to_find.hash = htab_hash_pointer (key); + slot = htab_find_slot (ref_map, &to_find, NO_INSERT); + + if (!slot) + return NULL_TREE; + + found = (struct tree_map *) *slot; + return found->to; +} + + +/* Set the entry corresponding to KEY, but only if the entry + already exists and its value is NULL_TREE. Otherwise, do nothing. */ + +static inline void +maybe_add_match (htab_t ref_map, struct tree_map *key) +{ + struct tree_map *found = htab_find (ref_map, key); + + if (found && !found->to) + found->to = key->to; +} + + +/* Add an entry to HT, with key T and value NULL_TREE. */ + +static void +add_key (htab_t ht, tree t, alloc_pool references_pool) +{ + void **slot; + struct tree_map *tp = pool_alloc (references_pool); + + tp->from = t; + tp->to = NULL_TREE; + tp->hash = htab_hash_pointer(tp->from); + + slot = htab_find_slot (ht, tp, INSERT); + *slot = (void *) tp; +} + + +/* Some memory to keep the objects in the reference table. */ + +static alloc_pool ref_table_alloc_pool = NULL; + + +/* Get some memory to keep the objects in the reference table. */ + +static inline alloc_pool +reference_table_alloc_pool (bool build) +{ + if (ref_table_alloc_pool || !build) + return ref_table_alloc_pool; + + ref_table_alloc_pool = + create_alloc_pool ("ref_table_alloc_pool", sizeof (struct tree_map), 20); + + return ref_table_alloc_pool; +} + + +/* Initialize the reference table by adding all pointers in the points-to + table as keys, and NULL_TREE as associated values. */ + +static struct reference_matches * +build_reference_table (void) +{ + unsigned int i; + struct reference_matches *ref_table = NULL; + alloc_pool references_pool = reference_table_alloc_pool (true); + + ref_table = XNEW (struct reference_matches); + ref_table->objs = htab_create (10, tree_map_hash, tree_map_eq, NULL); + ref_table->ptrs = htab_create (10, tree_map_hash, tree_map_eq, NULL); + + for (i = 1; i < num_ssa_names; i++) + { + tree ptr = ssa_name (i); + struct ptr_info_def *pi; + + if (ptr == NULL_TREE) + continue; + + pi = SSA_NAME_PTR_INFO (ptr); + + if (!SSA_NAME_IN_FREE_LIST (ptr) && pi && pi->name_mem_tag) + { + /* Add pointer to the interesting dereference list. */ + add_key (ref_table->ptrs, ptr, references_pool); + + /* Add all aliased names to the interesting reference list. */ + if (pi->pt_vars) + { + unsigned ix; + bitmap_iterator bi; + + EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix, bi) + { + tree alias = referenced_var (ix); + add_key (ref_table->objs, alias, references_pool); + } + } + } + } + + return ref_table; +} + + +/* Reference table. */ + +static struct reference_matches *ref_table = NULL; + + +/* Clean up the reference table if allocated. */ + +static void +maybe_free_reference_table (void) +{ + if (ref_table) + { + htab_delete (ref_table->ptrs); + htab_delete (ref_table->objs); + free (ref_table); + ref_table = NULL; + } + + if (ref_table_alloc_pool) + { + free_alloc_pool (ref_table_alloc_pool); + ref_table_alloc_pool = NULL; + } +} + + +/* Get the reference table. Initialize it if needed. */ + +static inline struct reference_matches * +reference_table (bool build) +{ + if (ref_table || !build) + return ref_table; + + ref_table = build_reference_table (); + find_references_in_function (); + return ref_table; +} + + +/* Callback for find_references_in_function. + Check whether *TP is an object reference or pointer dereference for the + variables given in ((struct match_info*)DATA)->OBJS or + ((struct match_info*)DATA)->PTRS. The total number of references + is stored in the same structures. */ + +static tree +find_references_in_tree_helper (tree *tp, + int *walk_subtrees ATTRIBUTE_UNUSED, + void *data) +{ + struct tree_map match; + static int parent_tree_code = ERROR_MARK; + + /* Do not report references just for the purpose of taking an address. + XXX: we rely on the fact that the tree walk is in preorder + and that ADDR_EXPR is not a leaf, thus cannot be carried over across + walks. */ + if (parent_tree_code == ADDR_EXPR) + goto finish; + + match.to = (tree) data; + + if (TREE_CODE (*tp) == INDIRECT_REF) + { + match.from = TREE_OPERAND (*tp, 0); + match.hash = htab_hash_pointer (match.from); + maybe_add_match (reference_table (true)->ptrs, &match); + } + else + { + match.from = *tp; + match.hash = htab_hash_pointer (match.from); + maybe_add_match (reference_table (true)->objs, &match); + } + +finish: + parent_tree_code = TREE_CODE (*tp); + return NULL_TREE; +} + + +/* Find all the references to aliased variables in the current function. */ + +static void +find_references_in_function (void) +{ + basic_block bb; + block_stmt_iterator i; + + FOR_EACH_BB (bb) + for (i = bsi_start (bb); !bsi_end_p (i); bsi_next (&i)) + walk_tree (bsi_stmt_ptr (i), find_references_in_tree_helper, + (void *) *bsi_stmt_ptr (i), NULL); +} + + +/* Find the reference site for OBJECT. + If IS_PTR is true, look for derferences of OBJECT instead. + XXX: only the first site is returned in the current + implementation. If there are no matching sites, return NULL_TREE. */ + +static tree +reference_site (tree object, bool is_ptr) +{ + if (is_ptr) + return match (reference_table (true)->ptrs, object); + else + return match (reference_table (true)->objs, object); +} + + +/* Try to get more location info when something is missing. + OBJECT1 and OBJECT2 are aliased names. If IS_PTR1 or IS_PTR2, the alias + is on the memory referenced or pointed to by OBJECT1 and OBJECT2. + ALIAS_SITE, DEREF_SITE1 and DEREF_SITE2 are the statements where the + alias takes place (some pointer assignment usually) and where the + alias is referenced through OBJECT1 and OBJECT2 respectively. + REF_TYPE1 and REF_TYPE2 will return the type of the reference at the + respective sites. Only the first matching reference is returned for + each name. If no statement is found, the function header is returned. */ + +static void +maybe_find_missing_stmts (tree object1, bool is_ptr1, + tree object2, bool is_ptr2, + tree *alias_site, + tree *deref_site1, + tree *deref_site2) +{ + if (object1 && object2) + { + if (!*alias_site || !EXPR_HAS_LOCATION (*alias_site)) + *alias_site = find_alias_site (object1, is_ptr1, object2, is_ptr2); + + if (!*deref_site1 || !EXPR_HAS_LOCATION (*deref_site1)) + *deref_site1 = reference_site (object1, is_ptr1); + + if (!*deref_site2 || !EXPR_HAS_LOCATION (*deref_site2)) + *deref_site2 = reference_site (object2, is_ptr2); + } + + /* If we could not find the alias site, set it to one of the dereference + sites, if available. */ + if (!*alias_site) + { + if (*deref_site1) + *alias_site = *deref_site1; + else if (*deref_site2) + *alias_site = *deref_site2; + } + + /* If we could not find the dereference sites, set them to the alias site, + if known. */ + if (!*deref_site1 && *alias_site) + *deref_site1 = *alias_site; + if (!*deref_site2 && *alias_site) + *deref_site2 = *alias_site; +} + + +/* Callback for find_first_artificial_name. + Find out if there are no artificial names at tree node *T. */ + +static tree +ffan_walker (tree *t, + int *go_below ATTRIBUTE_UNUSED, + void *data ATTRIBUTE_UNUSED) +{ + if (TREE_CODE (*t) == VAR_DECL || TREE_CODE (*t) == PARM_DECL) + if (DECL_ARTIFICIAL (*t)) + return *t; + + return NULL_TREE; +} + +/* Return the first artificial name within EXPR, or NULL_TREE if + none exists. */ + +static tree +find_first_artificial_name (tree expr) +{ + return walk_tree_without_duplicates (&expr, ffan_walker, NULL); +} + + +/* Get a name from the original program for VAR. */ + +static const char * +get_var_name (tree var) +{ + if (TREE_CODE (var) == SSA_NAME) + return get_var_name (get_ssa_base (var)); + + if (find_first_artificial_name (var)) + return "{unknown}"; + + if (TREE_CODE (var) == VAR_DECL || TREE_CODE (var) == PARM_DECL) + if (DECL_NAME (var)) + return IDENTIFIER_POINTER (DECL_NAME (var)); + + return "{unknown}"; +} + + +/* Return true if VAR contains an artificial name. */ + +static bool +contains_artificial_name_p (tree var) +{ + if (TREE_CODE (var) == SSA_NAME) + return contains_artificial_name_p (get_ssa_base (var)); + + return find_first_artificial_name (var) != NULL_TREE; +} + + +/* Return "*" if OBJECT is not the actual alias but a pointer to it, or + "" otherwise. + IS_PTR is true when OBJECT is not the actual alias. + In addition to checking IS_PTR, we also make sure that OBJECT is a pointer + since IS_PTR would also be true for C++ references, but we should only + print a * before a pointer and not before a reference. */ + +static const char * +get_maybe_star_prefix (tree object, bool is_ptr) +{ + gcc_assert (object); + return (is_ptr + && TREE_CODE (TREE_TYPE (object)) == POINTER_TYPE) ? "*" : ""; +} + + +/* Callback for contains_node_type_p. + Returns true if *T has tree code *(int*)DATA. */ + +static tree +contains_node_type_p_callback (tree *t, + int *go_below ATTRIBUTE_UNUSED, + void *data) +{ + return ((int) TREE_CODE (*t) == *((int *) data)) ? *t : NULL_TREE; +} + + +/* Return true if T contains a node with tree code TYPE. */ + +static bool +contains_node_type_p (tree t, int type) +{ + return (walk_tree_without_duplicates (&t, contains_node_type_p_callback, + (void *) &type) + != NULL_TREE); +} + + +/* Return true if a warning was issued in the front end at STMT. */ + +static bool +already_warned_in_frontend_p (tree stmt) +{ + tree rhs_pointer; + + if (stmt == NULL_TREE) + return false; + + rhs_pointer = get_rhs (stmt); + + if ((TREE_CODE (rhs_pointer) == NOP_EXPR + || TREE_CODE (rhs_pointer) == CONVERT_EXPR + || TREE_CODE (rhs_pointer) == VIEW_CONVERT_EXPR) + && TREE_NO_WARNING (rhs_pointer)) + return true; + else + return false; +} + + +/* Return true if and only if TYPE is a function or method pointer type, + or pointer to a pointer to ... to a function or method. */ + +static bool +is_method_pointer (tree type) +{ + while (TREE_CODE (type) == POINTER_TYPE) + type = TREE_TYPE (type); + return TREE_CODE (type) == METHOD_TYPE || TREE_CODE (type) == FUNCTION_TYPE; +} + + +/* Issue a -Wstrict-aliasing warning. + OBJECT1 and OBJECT2 are aliased names. + If IS_PTR1 and/or IS_PTR2 is true, then the corresponding name + OBJECT1/OBJECT2 is a pointer or reference to the aliased memory, + rather than actual storage. + ALIAS_SITE is a statement where the alias took place. In the most common + case, that is where a pointer was assigned to the address of an object. */ + +static bool +strict_aliasing_warn (tree alias_site, + tree object1, bool is_ptr1, + tree object2, bool is_ptr2, + bool filter_artificials) +{ + tree ref_site1 = NULL_TREE; + tree ref_site2 = NULL_TREE; + const char *name1; + const char *name2; + location_t alias_loc; + location_t ref1_loc; + location_t ref2_loc; + gcc_assert (object1); + gcc_assert (object2); + + if (contains_artificial_name_p (object1) + || contains_artificial_name_p (object2)) + return false; + + name1 = get_var_name (object1); + name2 = get_var_name (object2); + + if (is_method_pointer (get_main_type (TREE_TYPE (object2)))) + return false; + + maybe_find_missing_stmts (object1, is_ptr1, object2, is_ptr2, &alias_site, + &ref_site1, &ref_site2); + + if (!alias_site) + return false; + + if (EXPR_HAS_LOCATION (alias_site)) + alias_loc = EXPR_LOCATION (alias_site); + else + return false; + + if (EXPR_HAS_LOCATION (ref_site1)) + ref1_loc = EXPR_LOCATION (ref_site1); + else + ref1_loc = alias_loc; + + if (EXPR_HAS_LOCATION (ref_site2)) + ref2_loc = EXPR_LOCATION (ref_site2); + else + ref2_loc = alias_loc; + + if (already_warned_in_frontend_p (alias_site)) + return false; + + /* If they are not SSA names, but contain SSA names, drop the warning + because it cannot be displayed well. + Also drop it if they both contain artificials. + XXX: this is a hack, must figure out a better way to display them. */ + if (filter_artificials) + if ((find_first_artificial_name (get_ssa_base (object1)) + && find_first_artificial_name (get_ssa_base (object2))) + || (TREE_CODE (object1) != SSA_NAME + && contains_node_type_p (object1, SSA_NAME)) + || (TREE_CODE (object2) != SSA_NAME + && contains_node_type_p (object2, SSA_NAME))) + return false; + + /* XXX: In the following format string, %s:%d should be replaced by %H. + However, in my tests only the first %H printed ok, while the + second and third were printed as blanks. */ + warning (OPT_Wstrict_aliasing, + "%Hlikely type-punning may break strict-aliasing rules: " + "object %<%s%s%> of main type %qT is referenced at or around " + "%s:%d and may be " + "aliased to object %<%s%s%> of main type %qT which is referenced " + "at or around %s:%d.", + &alias_loc, + get_maybe_star_prefix (object1, is_ptr1), + name1, get_otype (object1, is_ptr1), + LOCATION_FILE (ref1_loc), LOCATION_LINE (ref1_loc), + get_maybe_star_prefix (object2, is_ptr2), + name2, get_otype (object2, is_ptr2), + LOCATION_FILE (ref2_loc), LOCATION_LINE (ref2_loc)); + + return true; +} + + + +/* Return true when any objects of TYPE1 and TYPE2 respectively + may not be aliased according to the language standard. */ + +static bool +nonstandard_alias_types_p (tree type1, tree type2) +{ + HOST_WIDE_INT set1; + HOST_WIDE_INT set2; + + if (VOID_TYPE_P (type1) || VOID_TYPE_P (type2)) + return false; + + set1 = get_alias_set (type1); + set2 = get_alias_set (type2); + return !alias_sets_conflict_p (set1, set2); +} + + + +/* Returns true if the given name is a struct field tag (SFT). */ + +static bool +struct_field_tag_p (tree var) +{ + return TREE_CODE (var) == STRUCT_FIELD_TAG; +} + + +/* Returns true when *PTR may not be aliased to ALIAS. + See C standard 6.5p7 and C++ standard 3.10p15. + If PTR_PTR is true, ALIAS represents a pointer or reference to the + aliased storage rather than its actual name. */ + +static bool +nonstandard_alias_p (tree ptr, tree alias, bool ptr_ptr) +{ + /* Find the types to compare. */ + tree ptr_type = get_otype (ptr, true); + tree alias_type = get_otype (alias, ptr_ptr); + + /* XXX: for now, say it's OK if the alias escapes. + Not sure this is needed in general, but otherwise GCC will not + bootstrap. */ + if (var_ann (get_ssa_base (alias))->escape_mask != NO_ESCAPE) + return false; + + /* XXX: don't get into structures for now. It brings much complication + and little benefit. */ + if (struct_class_union_p (ptr_type) || struct_class_union_p (alias_type)) + return false; + + /* XXX: In 4.2.1, field resolution in alias is not as good as in pre-4.3 + This fixes problems found during the backport, where a pointer to the + first field of a struct appears to be aliased to the whole struct. */ + if (struct_field_tag_p (alias)) + return false; + + /* If they are both SSA names of artificials, let it go, the warning + is too confusing. */ + if (find_first_artificial_name (ptr) && find_first_artificial_name (alias)) + return false; + + /* Compare the types. */ + return nonstandard_alias_types_p (ptr_type, alias_type); +} + + +/* Return true when we should skip analysis for pointer PTR based on the + fact that their alias information *PI is not considered relevant. */ + +static bool +skip_this_pointer (tree ptr ATTRIBUTE_UNUSED, struct ptr_info_def *pi) +{ + /* If it is not dereferenced, it is not a problem (locally). */ + if (!pi->is_dereferenced) + return true; + + /* This would probably cause too many false positives. */ + if (pi->value_escapes_p || pi->pt_anything) + return true; + + return false; +} + + +/* Find aliasing to named objects for pointer PTR. */ + +static void +dsa_named_for (tree ptr) +{ + struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr); + + if (pi) + { + if (skip_this_pointer (ptr, pi)) + return; + + /* For all the variables it could be aliased to. */ + if (pi->pt_vars) + { + unsigned ix; + bitmap_iterator bi; + + EXECUTE_IF_SET_IN_BITMAP (pi->pt_vars, 0, ix, bi) + { + tree alias = referenced_var (ix); + + if (is_global_var (alias)) + continue; + + if (nonstandard_alias_p (ptr, alias, false)) + strict_aliasing_warn (SSA_NAME_DEF_STMT (ptr), + ptr, true, alias, false, true); + } + } + } +} + + +/* Detect and report strict aliasing violation of named objects. */ + +static void +detect_strict_aliasing_named (void) +{ + unsigned int i; + + for (i = 1; i < num_ssa_names; i++) + { + tree ptr = ssa_name (i); + struct ptr_info_def *pi; + + if (ptr == NULL_TREE) + continue; + + pi = SSA_NAME_PTR_INFO (ptr); + + if (!SSA_NAME_IN_FREE_LIST (ptr) && pi && pi->name_mem_tag) + dsa_named_for (ptr); + } +} + + +/* Return false only the first time I see each instance of FUNC. */ + +static bool +processed_func_p (tree func) +{ + static htab_t seen = NULL; + void **slot; + + if (!seen) + seen = htab_create (100, htab_hash_pointer, htab_eq_pointer, NULL); + + slot = htab_find_slot (seen, func, INSERT); + gcc_assert (slot); + + if (*slot) + return true; + + *slot = func; + return false; +} + + +/* Detect and warn about type-punning using points-to information. */ + +void +strict_aliasing_warning_backend (void) +{ + if (!(flag_strict_aliasing + && warn_strict_aliasing == 3 + && !processed_func_p (current_function_decl))) + return; + + detect_strict_aliasing_named (); + maybe_free_reference_table (); +} diff --git a/contrib/gcc/tree-ssa-alias.c b/contrib/gcc/tree-ssa-alias.c index 04c45ebd3..c70073372 100644 --- a/contrib/gcc/tree-ssa-alias.c +++ b/contrib/gcc/tree-ssa-alias.c @@ -652,7 +652,7 @@ static unsigned int compute_may_aliases (void) { struct alias_info *ai; - + memset (&alias_stats, 0, sizeof (alias_stats)); /* Initialize aliasing information. */ @@ -710,6 +710,9 @@ compute_may_aliases (void) dump_alias_info (dump_file); } + /* Report strict aliasing violations. */ + strict_aliasing_warning_backend (); + /* Deallocate memory used by aliasing data structures. */ delete_alias_info (ai); diff --git a/contrib/gcc/tree-vrp.c b/contrib/gcc/tree-vrp.c index 0c9824d54..f97bd6131 100644 --- a/contrib/gcc/tree-vrp.c +++ b/contrib/gcc/tree-vrp.c @@ -2509,12 +2509,18 @@ adjust_range_with_scev (value_range_t *vr, struct loop *loop, tree stmt, true)) return; + type = TREE_TYPE (var); + + /* If we see a pointer type starting at a constant, then we have an + unusual ivopt. It may legitimately wrap. */ + if (POINTER_TYPE_P (type) && is_gimple_min_invariant (init)) + return; + /* We use TYPE_MIN_VALUE and TYPE_MAX_VALUE here instead of negative_overflow_infinity and positive_overflow_infinity, because we have concluded that the loop probably does not wrap. */ - type = TREE_TYPE (var); if (POINTER_TYPE_P (type) || !TYPE_MIN_VALUE (type)) tmin = lower_bound_in_type (type, type); else diff --git a/contrib/gcclibs/libcpp/Makefile.in b/contrib/gcclibs/libcpp/Makefile.in index 7a0b74f20..697318daa 100644 --- a/contrib/gcclibs/libcpp/Makefile.in +++ b/contrib/gcclibs/libcpp/Makefile.in @@ -69,14 +69,14 @@ INCLUDES = -I$(srcdir) -I. -I$(srcdir)/../include @INCINTL@ \ ALL_CFLAGS = $(CFLAGS) $(WARN_CFLAGS) $(INCLUDES) $(CPPFLAGS) -libcpp_a_OBJS = charset.o directives.o errors.o expr.o files.o \ - identifiers.o init.o lex.o line-map.o macro.o mkdeps.o \ - pch.o symtab.o traditional.o +libcpp_a_OBJS = charset.o directives.o directives-only.o errors.o \ + expr.o files.o identifiers.o init.o lex.o line-map.o macro.o \ + mkdeps.o pch.o symtab.o traditional.o makedepend_OBJS = makedepend.o -libcpp_a_SOURCES = charset.c directives.c errors.c expr.c files.c \ - identifiers.c init.c lex.c line-map.c macro.c mkdeps.c \ - pch.c symtab.c traditional.c +libcpp_a_SOURCES = charset.c directives.c directives-only.c errors.c \ + expr.c files.c identifiers.c init.c lex.c line-map.c macro.c \ + mkdeps.c pch.c symtab.c traditional.c all: libcpp.a makedepend$(EXEEXT) $(USED_CATALOGS) diff --git a/contrib/gcclibs/libcpp/directives-only.c b/contrib/gcclibs/libcpp/directives-only.c new file mode 100644 index 000000000..5990fbf4f --- /dev/null +++ b/contrib/gcclibs/libcpp/directives-only.c @@ -0,0 +1,229 @@ +/* CPP Library - directive only preprocessing for distributed compilation. + Copyright (C) 2007 + Free Software Foundation, Inc. + Contributed by Ollie Wild . + +This program is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 2, or (at your option) any +later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#include "config.h" +#include "system.h" +#include "cpplib.h" +#include "internal.h" + +/* DO (Directive only) flags. */ +#define DO_BOL (1 << 0) /* At the beginning of a logical line. */ +#define DO_STRING (1 << 1) /* In a string constant. */ +#define DO_CHAR (1 << 2) /* In a character constant. */ +#define DO_BLOCK_COMMENT (1 << 3) /* In a block comment. */ +#define DO_LINE_COMMENT (1 << 4) /* In a single line "//-style" comment. */ + +#define DO_LINE_SPECIAL (DO_STRING | DO_CHAR | DO_LINE_COMMENT) +#define DO_SPECIAL (DO_LINE_SPECIAL | DO_BLOCK_COMMENT) + +/* Writes out the preprocessed file, handling spacing and paste + avoidance issues. */ +void +_cpp_preprocess_dir_only (cpp_reader *pfile, + const struct _cpp_dir_only_callbacks *cb) +{ + struct cpp_buffer *buffer; + const unsigned char *cur, *base, *next_line, *rlimit; + cppchar_t c, last_c; + unsigned flags; + int lines, col; + source_location loc; + + restart: + /* Buffer initialization ala _cpp_clean_line(). */ + buffer = pfile->buffer; + buffer->cur_note = buffer->notes_used = 0; + buffer->cur = buffer->line_base = buffer->next_line; + buffer->need_line = false; + + /* This isn't really needed. It prevents a compiler warning, though. */ + loc = pfile->line_table->highest_line; + + /* Scan initialization. */ + next_line = cur = base = buffer->cur; + rlimit = buffer->rlimit; + flags = DO_BOL; + lines = 0; + col = 1; + + for (last_c = '\n', c = *cur; cur < rlimit; last_c = c, c = *++cur, ++col) + { + /* Skip over escaped newlines. */ + if (__builtin_expect (c == '\\', false)) + { + const unsigned char *tmp = cur + 1; + + while (is_nvspace (*tmp) && tmp < rlimit) + tmp++; + if (*tmp == '\r') + tmp++; + if (*tmp == '\n' && tmp < rlimit) + { + CPP_INCREMENT_LINE (pfile, 0); + lines++; + col = 0; + cur = tmp; + c = last_c; + continue; + } + } + + if (__builtin_expect (last_c == '#', false) && !(flags & DO_SPECIAL)) + { + if (c != '#' && (flags & DO_BOL)) + { + struct line_maps *line_table; + + if (!pfile->state.skipping && next_line != base) + cb->print_lines (lines, base, next_line - base); + + /* Prep things for directive handling. */ + buffer->next_line = cur; + buffer->need_line = true; + _cpp_get_fresh_line (pfile); + + /* Ensure proper column numbering for generated error messages. */ + buffer->line_base -= col - 1; + + _cpp_handle_directive (pfile, 0 /* ignore indented */); + + /* Sanitize the line settings. Duplicate #include's can mess + things up. */ + line_table = pfile->line_table; + line_table->highest_location = line_table->highest_line; + + /* The if block prevents us from outputing line information when + the file ends with a directive and no newline. Note that we + must use pfile->buffer, not buffer. */ + if (pfile->buffer->cur != pfile->buffer->rlimit) + cb->maybe_print_line (pfile->line_table->highest_line); + + goto restart; + } + + flags &= ~DO_BOL; + pfile->mi_valid = false; + } + else if (__builtin_expect (last_c == '/', false) \ + && !(flags & DO_SPECIAL) && c != '*' && c != '/') + { + /* If a previous slash is not starting a block comment, clear the + DO_BOL flag. */ + flags &= ~DO_BOL; + pfile->mi_valid = false; + } + + switch (c) + { + case '/': + if ((flags & DO_BLOCK_COMMENT) && last_c == '*') + { + flags &= ~DO_BLOCK_COMMENT; + c = 0; + } + else if (!(flags & DO_SPECIAL) && last_c == '/') + flags |= DO_LINE_COMMENT; + else if (!(flags & DO_SPECIAL)) + /* Mark the position for possible error reporting. */ + LINEMAP_POSITION_FOR_COLUMN (loc, pfile->line_table, col); + + break; + + case '*': + if (!(flags & DO_SPECIAL)) + { + if (last_c == '/') + flags |= DO_BLOCK_COMMENT; + else + { + flags &= ~DO_BOL; + pfile->mi_valid = false; + } + } + + break; + + case '\'': + case '"': + { + unsigned state = (c == '"') ? DO_STRING : DO_CHAR; + + if (!(flags & DO_SPECIAL)) + { + flags |= state; + flags &= ~DO_BOL; + pfile->mi_valid = false; + } + else if ((flags & state) && last_c != '\\') + flags &= ~state; + + break; + } + + case '\\': + { + if ((flags & (DO_STRING | DO_CHAR)) && last_c == '\\') + c = 0; + + if (!(flags & DO_SPECIAL)) + { + flags &= ~DO_BOL; + pfile->mi_valid = false; + } + + break; + } + + case '\n': + CPP_INCREMENT_LINE (pfile, 0); + lines++; + col = 0; + flags &= ~DO_LINE_SPECIAL; + if (!(flags & DO_SPECIAL)) + flags |= DO_BOL; + break; + + case '#': + next_line = cur; + /* Don't update DO_BOL yet. */ + break; + + case ' ': case '\t': case '\f': case '\v': case '\0': + break; + + default: + if (!(flags & DO_SPECIAL)) + { + flags &= ~DO_BOL; + pfile->mi_valid = false; + } + break; + } + } + + if (flags & DO_BLOCK_COMMENT) + cpp_error_with_line (pfile, CPP_DL_ERROR, loc, 0, "unterminated comment"); + + if (!pfile->state.skipping && cur != base) + cb->print_lines (lines, base, cur - base); + + _cpp_pop_buffer (pfile); + if (pfile->buffer) + goto restart; +} diff --git a/contrib/gcclibs/libcpp/directives.c b/contrib/gcclibs/libcpp/directives.c index 7fb142e48..70118428e 100644 --- a/contrib/gcclibs/libcpp/directives.c +++ b/contrib/gcclibs/libcpp/directives.c @@ -423,8 +423,13 @@ _cpp_handle_directive (cpp_reader *pfile, int indented) does not cause '#define foo bar' to get executed when compiled with -save-temps, we recognize directives in -fpreprocessed mode only if the # is in column 1. macro.c - puts a space in front of any '#' at the start of a macro. */ + puts a space in front of any '#' at the start of a macro. + + We exclude the -fdirectives-only case because macro expansion + has not been performed yet, and block comments can cause spaces + to preceed the directive. */ if (CPP_OPTION (pfile, preprocessed) + && !CPP_OPTION (pfile, directives_only) && (indented || !(dir->flags & IN_I))) { skip = 0; diff --git a/contrib/gcclibs/libcpp/files.c b/contrib/gcclibs/libcpp/files.c index 3751184d0..0e9f97782 100644 --- a/contrib/gcclibs/libcpp/files.c +++ b/contrib/gcclibs/libcpp/files.c @@ -775,7 +775,8 @@ _cpp_stack_file (cpp_reader *pfile, _cpp_file *file, bool import) /* Stack the buffer. */ buffer = cpp_push_buffer (pfile, file->buffer, file->st.st_size, - CPP_OPTION (pfile, preprocessed)); + CPP_OPTION (pfile, preprocessed) + && !CPP_OPTION (pfile, directives_only)); buffer->file = file; buffer->sysp = sysp; diff --git a/contrib/gcclibs/libcpp/include/cpplib.h b/contrib/gcclibs/libcpp/include/cpplib.h index 7fd73b87d..5acef415c 100644 --- a/contrib/gcclibs/libcpp/include/cpplib.h +++ b/contrib/gcclibs/libcpp/include/cpplib.h @@ -440,6 +440,9 @@ struct cpp_options /* True means error callback should be used for diagnostics. */ bool client_diagnostic; + + /* True disables tokenization outside of preprocessing directives. */ + bool directives_only; }; /* Callback for header lookup for HEADER, which is the name of a @@ -644,6 +647,10 @@ extern struct deps *cpp_get_deps (cpp_reader *); too. If there was an error opening the file, it returns NULL. */ extern const char *cpp_read_main_file (cpp_reader *, const char *); +/* Set up built-ins with special behavior. Use cpp_init_builtins() + instead unless your know what you are doing. */ +extern void cpp_init_special_builtins (cpp_reader *); + /* Set up built-ins like __FILE__. */ extern void cpp_init_builtins (cpp_reader *, int); diff --git a/contrib/gcclibs/libcpp/init.c b/contrib/gcclibs/libcpp/init.c index 8280f2fe1..248d84f53 100644 --- a/contrib/gcclibs/libcpp/init.c +++ b/contrib/gcclibs/libcpp/init.c @@ -348,11 +348,8 @@ mark_named_operators (cpp_reader *pfile) } } -/* Read the builtins table above and enter them, and language-specific - macros, into the hash table. HOSTED is true if this is a hosted - environment. */ void -cpp_init_builtins (cpp_reader *pfile, int hosted) +cpp_init_special_builtins (cpp_reader *pfile) { const struct builtin *b; size_t n = ARRAY_SIZE (builtin_array); @@ -361,10 +358,7 @@ cpp_init_builtins (cpp_reader *pfile, int hosted) n -= 2; else if (! CPP_OPTION (pfile, stdc_0_in_system_headers) || CPP_OPTION (pfile, std)) - { - n--; - _cpp_define_builtin (pfile, "__STDC__ 1"); - } + n--; for (b = builtin_array; b < builtin_array + n; b++) { @@ -373,6 +367,20 @@ cpp_init_builtins (cpp_reader *pfile, int hosted) hp->flags |= NODE_BUILTIN | NODE_WARN; hp->value.builtin = (enum builtin_type) b->value; } +} + +/* Read the builtins table above and enter them, and language-specific + macros, into the hash table. HOSTED is true if this is a hosted + environment. */ +void +cpp_init_builtins (cpp_reader *pfile, int hosted) +{ + cpp_init_special_builtins (pfile); + + if (!CPP_OPTION (pfile, traditional) + && (! CPP_OPTION (pfile, stdc_0_in_system_headers) + || CPP_OPTION (pfile, std))) + _cpp_define_builtin (pfile, "__STDC__ 1"); if (CPP_OPTION (pfile, cplusplus)) _cpp_define_builtin (pfile, "__cplusplus 1"); @@ -620,7 +628,8 @@ post_options (cpp_reader *pfile) preprocessed text. Read preprocesed source in ISO mode. */ if (CPP_OPTION (pfile, preprocessed)) { - pfile->state.prevent_expansion = 1; + if (!CPP_OPTION (pfile, directives_only)) + pfile->state.prevent_expansion = 1; CPP_OPTION (pfile, traditional) = 0; } diff --git a/contrib/gcclibs/libcpp/internal.h b/contrib/gcclibs/libcpp/internal.h index 1661f7a9a..ebc2ca9d4 100644 --- a/contrib/gcclibs/libcpp/internal.h +++ b/contrib/gcclibs/libcpp/internal.h @@ -567,6 +567,17 @@ extern void _cpp_do_file_change (cpp_reader *, enum lc_reason, const char *, unsigned int, unsigned int); extern void _cpp_pop_buffer (cpp_reader *); +/* In directives.c */ +struct _cpp_dir_only_callbacks +{ + /* Called to print a block of lines. */ + void (*print_lines) (int, const void *, size_t); + void (*maybe_print_line) (source_location); +}; + +extern void _cpp_preprocess_dir_only (cpp_reader *, + const struct _cpp_dir_only_callbacks *); + /* In traditional.c. */ extern bool _cpp_scan_out_logical_line (cpp_reader *, cpp_macro *); extern bool _cpp_read_logical_line_trad (cpp_reader *); diff --git a/contrib/gcclibs/libcpp/macro.c b/contrib/gcclibs/libcpp/macro.c index 1eec5788f..d294f0442 100644 --- a/contrib/gcclibs/libcpp/macro.c +++ b/contrib/gcclibs/libcpp/macro.c @@ -264,6 +264,9 @@ _cpp_builtin_macro_text (cpp_reader *pfile, cpp_hashnode *node) break; case BT_COUNTER: + if (CPP_OPTION (pfile, directives_only) && pfile->state.in_directive) + cpp_error (pfile, CPP_DL_ERROR, + "__COUNTER__ expanded inside directive with -fdirectives-only"); number = pfile->nextcounter++; break; } diff --git a/contrib/libstdc++/include/ext/hashtable.h b/contrib/libstdc++/include/ext/hashtable.h index 233806fb7..b3f98df2e 100644 --- a/contrib/libstdc++/include/ext/hashtable.h +++ b/contrib/libstdc++/include/ext/hashtable.h @@ -210,10 +210,11 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) }; // Note: assumes long is at least 32 bits. - enum { _S_num_primes = 28 }; + enum { _S_num_primes = 29 }; static const unsigned long __stl_prime_list[_S_num_primes] = { + 5ul, // 5ul mini size is a Google addition 53ul, 97ul, 193ul, 389ul, 769ul, 1543ul, 3079ul, 6151ul, 12289ul, 24593ul, 49157ul, 98317ul, 196613ul, 393241ul, 786433ul, @@ -1076,6 +1077,10 @@ _GLIBCXX_BEGIN_NAMESPACE(__gnu_cxx) hashtable<_Val, _Key, _HF, _Ex, _Eq, _All>:: clear() { + // Google addition: do not iterate over buckets when empty + if (_M_num_elements == 0) + return; + for (size_type __i = 0; __i < _M_buckets.size(); ++__i) { _Node* __cur = _M_buckets[__i]; diff --git a/gnu/usr.bin/cc/cc_int/Makefile b/gnu/usr.bin/cc/cc_int/Makefile index 86c179190..33808a536 100644 --- a/gnu/usr.bin/cc/cc_int/Makefile +++ b/gnu/usr.bin/cc/cc_int/Makefile @@ -24,6 +24,7 @@ OBJS-common = \ tree-outof-ssa.o tree-ssa-ccp.o tree-vn.o tree-ssa-uncprop.o \ tree-ssa-dce.o tree-ssa-copy.o tree-nrv.o tree-ssa-copyrename.o \ tree-ssa-pre.o tree-ssa-live.o tree-ssa-operands.o tree-ssa-alias.o \ + tree-ssa-alias-warnings.o \ tree-ssa-phiopt.o tree-ssa-forwprop.o tree-nested.o tree-ssa-dse.o \ tree-ssa-dom.o domwalk.o tree-tailcall.o gimple-low.o tree-iterator.o \ omp-low.o tree-phinodes.o tree-ssanames.o tree-sra.o tree-complex.o \ diff --git a/gnu/usr.bin/cc/libcpp/Makefile b/gnu/usr.bin/cc/libcpp/Makefile index bfaea7d4a..d21bd8352 100644 --- a/gnu/usr.bin/cc/libcpp/Makefile +++ b/gnu/usr.bin/cc/libcpp/Makefile @@ -11,9 +11,9 @@ CFLAGS+= -I${.CURDIR} -I. LIB= cpp SRCS= localedir.h -SRCS+= charset.c directives.c errors.c expr.c files.c \ - identifiers.c init.c lex.c line-map.c macro.c mkdeps.c \ - pch.c symtab.c traditional.c +SRCS+= charset.c directives.c directives-only.c errors.c expr.c \ + files.c identifiers.c init.c lex.c line-map.c macro.c \ + mkdeps.c pch.c symtab.c traditional.c INTERNALLIB= WARNS?= 1 -- 2.45.0