From f7bc306c19a3bd8f9a31a78d81a7bb5160bbbaf8 Mon Sep 17 00:00:00 2001 From: Larry Knox Date: Tue, 2 Jun 2020 15:53:58 -0500 Subject: Add files for adding warning flags for clang compilers in autotools configure. --- config/apple | 3 + config/clang-cxxflags | 218 +++++++++++++++++++++++++++++ config/clang-flags | 218 +++++++++++++++++++++++++++++ config/clang-warnings/cxx-error-general | 32 +++++ config/clang-warnings/cxx-general | 31 ++++ config/clang-warnings/no-developer-general | 2 +- config/linux-gnulibc1 | 21 +++ configure.ac | 52 +++++++ 8 files changed, 576 insertions(+), 1 deletion(-) create mode 100644 config/clang-cxxflags create mode 100644 config/clang-flags create mode 100644 config/clang-warnings/cxx-error-general create mode 100644 config/clang-warnings/cxx-general diff --git a/config/apple b/config/apple index 0b15dfe..aba48d1 100644 --- a/config/apple +++ b/config/apple @@ -49,6 +49,7 @@ fi # Figure out compiler flags . $srcdir/config/gnu-flags +. $srcdir/config/clang-flags # temp patch: if GCC 4.2.1 is used in Lion or Mountain Lion systems, do not # use -O option as it causes failures in test/dt_arith. case "$host_os" in @@ -155,11 +156,13 @@ esac # get c++ version info case $CXX in clang++) + . $srcdir/config/clang-cxxflags cxx_version_info=`$CXX $CXXFLAGS $H5_CXXFLAGS --version 2>&1 |\ grep 'Apple' | sed 's/(.*//'` ;; *g++*) + . $srcdir/config/gnu-cxxflags cxx_version_info=`$CXX $CXXFLAGS $H5_CXXFLAGS --version 2>&1 |\ grep 'GCC' | sed 's/.*\((GCC) [-a-z0-9\. ]*.*\)/\1/'` ;; diff --git a/config/clang-cxxflags b/config/clang-cxxflags new file mode 100644 index 0000000..c6b84c4 --- /dev/null +++ b/config/clang-cxxflags @@ -0,0 +1,218 @@ +# -*- shell-script -*- +# +# Copyright by The HDF Group. +# Copyright by the Board of Trustees of the University of Illinois. +# All rights reserved. +# +# This file is part of HDF5. The full HDF5 copyright notice, including +# terms governing use, modification, and redistribution, is contained in +# the COPYING file, which can be found at the root of the source code +# distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases. +# If you do not have access to either file, you may request a copy from +# help@hdfgroup.org. + + +# This file should be sourced into configure if the compiler is the +# Clang clang++ compiler or a derivative. It is careful not to do anything +# if the compiler is not Clang; otherwise 'cxx_flags_set' is set to 'yes' +# + +# +# For now, do not promote any warnings to errors. +# +PROMOTE_ERRORS_DFLT=no + +# +# This filter rewrites -Werror= as -W, in that way demoting warnings +# promoted to errors back to warnings, if PROMOTE_ERRORS is no. +# +demote_errors() +{ + if [ ${PROMOTE_ERRORS:-${PROMOTE_ERRORS_DFLT}} = no ]; then + sed 's,-Werror=,-W,g' + else + cat + fi +} + +# +# Prepend `$srcdir/config/clang-warnings/` to the filename suffix(es) given as +# subroutine argument(s), remove comments starting with # and ending +# at EOL, replace spans of whitespace (including newlines) with spaces, +# and re-emit the file(s) thus filtered on the standard output stream. +# +load_clang_arguments() +{ + set -- $(for arg; do + sed 's,#.*$,,' $srcdir/config/clang-warnings/${arg} + done) + IFS=' ' echo "$*" +} +# Get the compiler version in a way that works for clang++ +# unless a compiler version is already known +# +# cxx_vendor: The compiler name: clang++ +# cxx_version: Version number: 6.0.0, 7.3.0, ... 10.0.1 +# +if test "X-" = "X-$cxx_flags_set"; then + # clang++ -v will return version number following "clang" on Linux machines, + # but on Macs the version number will follow "Apple LLVM version" + cxx_version="`$CXX $CXXFLAGS $H5_CXXFLAGS -v 2>&1 |\ + grep 'clang version' | sed 's/.*clang version \([-a-z0-9\.]*\).*/\1/'`" + if test -n "$cxx_version"; then + cxx_vendor="clang" + else + cxx_version="`$CXX $CXXFLAGS $H5_CXXFLAGS -v 2>&1 |\ + grep 'Apple LLVM version' | sed 's/.*Apple LLVM version \([-a-z0-9\.]*\).*/\1/'`" + cxx_vendor="Apple LLVM" + fi + if test "X-" != "X-$cxx_version"; then + echo "compiler '$CXX' is $cxx_vendor-$cxx_version" + + # Get the compiler version numbers + cxx_vers_major=`echo $cxx_version | cut -f1 -d.` + cxx_vers_minor=`echo $cxx_version | cut -f2 -d.` + cxx_vers_patch=`echo $cxx_version | cut -f3 -d.` + test -n "$cxx_vers_major" || cxx_vers_major=0 + test -n "$cxx_vers_minor" || cxx_vers_minor=0 + test -n "$cxx_vers_patch" || cxx_vers_patch=0 + fi +fi + +if test "X-clang" = "X-$cxx_vendor" -o "X-Apple LLVM" = "X-$cxx_vendor"; then + echo "compiler '$CXX' is $cxx_vendor-$cxx_version" + ############################### + # Architecture-specific flags # + ############################### + + arch= + case "$host_os-$host_cpu" in + # FreeBSD sets the information from "uname -m" to the general machine + # architecture, not the specific CPU for the machine, so even our + # Pentium II Xeon server is set to "i386". Once we know we are on a FreeBSD + # machine, use the "sysctl" command to get the CPU hardware model. + freebsd*-i386) + host_cpu_model=`sysctl -n hw.model` + case "$host_cpu_model" in + # Hmm.. this might not catch Celerons, but it won't hurt them either... + *Pro*|*II*|*III*|*IV*|*Athlon*) + # architecture-specific optimizations cause problems + # for some users who build binaries to be used on + # multiple architectures. + # arch="-march=i686" + ;; + esac + ;; + + *-i686) + # architecture-specific optimizations cause problems + # for some users who build binaries to be used on + # multiple architectures. + # arch="-march=i686" + ;; + esac + + case "$host_os-$host_cpu" in + # cygwin needs the "-std=c99" flag removed, so make + # a specific case for Cygwin without the flag and a default + # case to add the flag everywhere else + cygwin-*) + ;; + + *) + H5_CXXFLAGS="$H5_CXXFLAGS -std=c++11" + ;; + esac + + H5_CXXFLAGS="$H5_CXXFLAGS $arch" + + ############## + # Production # + ############## + + # NDEBUG is handled explicitly by the configure script + if test $cxx_vers_major -le 4; then + PROD_CXXFLAGS= + else + PROD_CXXFLAGS="-fstdarg-opt" + fi + + ######### + # Debug # + ######### + + # NDEBUG is handled explicitly by the configure script + # -g is handled by the symbols flags + if test $cxx_vers_major -le 4; then + DEBUG_CXXFLAGS= + else + DEBUG_CXXFLAGS="-ftrapv -fno-common -fsanitize=address" + fi + + ########### + # Symbols # + ########### + + NO_SYMBOLS_CXXFLAGS="-s" + SYMBOLS_CXXFLAGS="-g -fno-omit-frame-pointer" + + ############# + # Profiling # + ############# + + PROFILE_CXXFLAGS="-pg" + + ################ + # Optimization # + ################ + + if test $cxx_vers_major -le 4; then + HIGH_OPT_CXXFLAGS="-O3" + DEBUG_OPT_CXXFLAGS= + else + HIGH_OPT_CXXFLAGS="-O3" + DEBUG_OPT_CXXFLAGS="-g" + fi + NO_OPT_CXXFLAGS="-O0" + + ############ + # Warnings # + ############ + + ########### + # General # + ########### + + if test $cxx_vers_major -eq 4 -a $cxx_vers_minor -ge 2 -o $cxx_vers_major -gt 4; then + H5_CXXFLAGS="$H5_CXXFLAGS $(load_clang_arguments general)" + fi +# if test $cxx_vers_major -eq 4 -a $cxx_vers_minor -ge 8 -o $cxx_vers_major -gt 4; then +# H5_ECXXFLAGS="$H5_ECXXFLAGS $(load_clang_arguments error-general)" +# fi + +echo "H5_CXXFLAGS: $H5_CXXFLAGS" + + ###################### + # Developer warnings # + ###################### + + NO_DEVELOPER_WARNING_CXXFLAGS=$(load_clang_arguments no-developer-general) + DEVELOPER_WARNING_CXXFLAGS=$(load_clang_arguments developer-general) + + ############################# + # Version-specific warnings # + ############################# + + + ################# + # Flags are set # + ################# + cxx_flags_set=yes +fi + +# Clear cxx info if no flags set +if test "X$cxx_flags_set" = "X"; then + cxx_vendor= + cxx_version= +fi + diff --git a/config/clang-flags b/config/clang-flags new file mode 100644 index 0000000..32c29d4 --- /dev/null +++ b/config/clang-flags @@ -0,0 +1,218 @@ +# -*- shell-script -*- +# +# Copyright by The HDF Group. +# Copyright by the Board of Trustees of the University of Illinois. +# All rights reserved. +# +# This file is part of HDF5. The full HDF5 copyright notice, including +# terms governing use, modification, and redistribution, is contained in +# the COPYING file, which can be found at the root of the source code +# distribution tree, or in https://support.hdfgroup.org/ftp/HDF5/releases. +# If you do not have access to either file, you may request a copy from +# help@hdfgroup.org. + + +# This file should be sourced into configure if the compiler is the +# Clang compiler or a derivative. It is careful not to do anything +# if the compiler is not Clang; otherwise 'cc_flags_set' is set to 'yes' +# + +# +# For now, do not promote any warnings to errors. +# +PROMOTE_ERRORS_DFLT=no + +# +# This filter rewrites -Werror= as -W, in that way demoting warnings +# promoted to errors back to warnings, if PROMOTE_ERRORS is no. +# +demote_errors() +{ + if [ ${PROMOTE_ERRORS:-${PROMOTE_ERRORS_DFLT}} = no ]; then + sed 's,-Werror=,-W,g' + else + cat + fi +} + +# +# Prepend `$srcdir/config/clang-warnings/` to the filename suffix(es) given as +# subroutine argument(s), remove comments starting with # and ending +# at EOL, replace spans of whitespace (including newlines) with spaces, +# and re-emit the file(s) thus filtered on the standard output stream. +# +load_clang_arguments() +{ + set -- $(for arg; do + sed 's,#.*$,,' $srcdir/config/clang-warnings/${arg} + done) + IFS=' ' echo "$*" +} +# Get the compiler version in a way that works for clang +# unless a compiler version is already known +# +# cc_vendor: The compiler name: clang +# cc_version: Version number: 6.0.0, 7.3.0, ... 10.0.1 +# +if test "X-" = "X-$cc_flags_set"; then + # clang -v will return version number following "clang" on Linux machines, + # but on Macs the version number will follow "Apple LLVM version" + cc_version="`$CC $CFLAGS $H5_CFLAGS -v 2>&1 |\ + grep 'clang version' | sed 's/.*clang version \([-a-z0-9\.]*\).*/\1/'`" + if test -n "$cc_version"; then + cc_vendor="clang" + else + cc_version="`$CC $CFLAGS $H5_CFLAGS -v 2>&1 |\ + grep 'Apple LLVM version' | sed 's/.*Apple LLVM version \([-a-z0-9\.]*\).*/\1/'`" + cc_vendor="Apple LLVM" + fi + if test "X-" != "X-$cc_version"; then + echo "compiler '$CC' is $cc_vendor-$cc_version" + + # Get the compiler version numbers + cc_vers_major=`echo $cc_version | cut -f1 -d.` + cc_vers_minor=`echo $cc_version | cut -f2 -d.` + cc_vers_patch=`echo $cc_version | cut -f3 -d.` + test -n "$cc_vers_major" || cc_vers_major=0 + test -n "$cc_vers_minor" || cc_vers_minor=0 + test -n "$cc_vers_patch" || cc_vers_patch=0 + fi +fi + +if test "X-clang" = "X-$cc_vendor" -o "X-Apple LLVM" = "X-$cc_vendor"; then + echo "compiler '$CC' is $cc_vendor-$cc_version" + ############################### + # Architecture-specific flags # + ############################### + + arch= + case "$host_os-$host_cpu" in + # FreeBSD sets the information from "uname -m" to the general machine + # architecture, not the specific CPU for the machine, so even our + # Pentium II Xeon server is set to "i386". Once we know we are on a FreeBSD + # machine, use the "sysctl" command to get the CPU hardware model. + freebsd*-i386) + host_cpu_model=`sysctl -n hw.model` + case "$host_cpu_model" in + # Hmm.. this might not catch Celerons, but it won't hurt them either... + *Pro*|*II*|*III*|*IV*|*Athlon*) + # architecture-specific optimizations cause problems + # for some users who build binaries to be used on + # multiple architectures. + # arch="-march=i686" + ;; + esac + ;; + + *-i686) + # architecture-specific optimizations cause problems + # for some users who build binaries to be used on + # multiple architectures. + # arch="-march=i686" + ;; + esac + + case "$host_os-$host_cpu" in + # cygwin needs the "-std=c99" flag removed, so make + # a specific case for Cygwin without the flag and a default + # case to add the flag everywhere else + cygwin-*) + ;; + + *) + H5_CFLAGS="$H5_CFLAGS -std=c99" + ;; + esac + + H5_CFLAGS="$H5_CFLAGS $arch" + + ############## + # Production # + ############## + + # NDEBUG is handled explicitly by the configure script + if test $cc_vers_major -le 4; then + PROD_CFLAGS= + else + PROD_CFLAGS="-fstdarg-opt" + fi + + ######### + # Debug # + ######### + + # NDEBUG is handled explicitly by the configure script + # -g is handled by the symbols flags + if test $cc_vers_major -le 4; then + DEBUG_CFLAGS= + else + DEBUG_CFLAGS="-ftrapv -fno-common" + fi + + ########### + # Symbols # + ########### + + NO_SYMBOLS_CFLAGS="-s" + SYMBOLS_CFLAGS="-g" + + ############# + # Profiling # + ############# + + PROFILE_CFLAGS="-pg" + + ################ + # Optimization # + ################ + + if test $cc_vers_major -le 4; then + HIGH_OPT_CFLAGS="-O3" + DEBUG_OPT_CFLAGS= + else + HIGH_OPT_CFLAGS="-O3" + DEBUG_OPT_CFLAGS="-g" + fi + NO_OPT_CFLAGS="-O0" + + ############ + # Warnings # + ############ + + ########### + # General # + ########### + + if test $cc_vers_major -eq 4 -a $cc_vers_minor -ge 2 -o $cc_vers_major -gt 4; then + H5_CFLAGS="$H5_CFLAGS $(load_clang_arguments general)" + fi +# if test $cc_vers_major -eq 4 -a $cc_vers_minor -ge 8 -o $cc_vers_major -gt 4; then +# H5_ECFLAGS="$H5_ECFLAGS $(load_clang_arguments error-general)" +# fi + +echo "H5_CFLAGS: $H5_CFLAGS" + + ###################### + # Developer warnings # + ###################### + + NO_DEVELOPER_WARNING_CFLAGS=$(load_clang_arguments no-developer-general) + DEVELOPER_WARNING_CFLAGS=$(load_clang_arguments developer-general) + + ############################# + # Version-specific warnings # + ############################# + + + ################# + # Flags are set # + ################# + cc_flags_set=yes +fi + +# Clear cc info if no flags set +if test "X$cc_flags_set" = "X"; then + cc_vendor= + cc_version= +fi + diff --git a/config/clang-warnings/cxx-error-general b/config/clang-warnings/cxx-error-general new file mode 100644 index 0000000..85cd9a3 --- /dev/null +++ b/config/clang-warnings/cxx-error-general @@ -0,0 +1,32 @@ +# +# HDF5 code should not trigger the following warnings under any +# circumstances, so ask the compiler to treat them as errors: +# +# NOTE: c++/test files are not compatible with these warnings as errors +# c++/test/tcompound.cpp +# -Werror=cast-align +-Wcast-align +# NOTE: c++/src files are not compatible with these warnings as errors +# c++/src/H5Object.cpp +# -Werror=missing-declarations +-Wmissing-declarations +-Werror=packed +-Werror=redundant-decls +-Werror=switch +# NOTE: c++/test files are not compatible with these warnings as errors +# c++/test/tattr.cpp +# -Werror=unused-but-set-variable +-Wunused-but-set-variable +-Werror=unused-function +-Werror=unused-variable +# NOTE: c++/src files are not compatible with these warnings as errors +# c++/src/H5Object.cpp,c++/src/H5StrType.cpp,c++/src/H5PredType.cpp +# -Werror=unused-parameter +-Wunused-parameter +# +# Other files not compatible +# NOTE: c++/test files are not compatible with these warnings as errors +# c++/test/titerate.cpp,c++/test/tarray.cpp +# -Werror=missing-declarations +# c++/test/titerate.cpp,c++/test/tarray.cpp,c++/test/tlinks.cpp,c++/test/ttypes.cpp,c++/test/dsets.cpp +# -Werror=unused-parameter diff --git a/config/clang-warnings/cxx-general b/config/clang-warnings/cxx-general new file mode 100644 index 0000000..9548cc0 --- /dev/null +++ b/config/clang-warnings/cxx-general @@ -0,0 +1,31 @@ +# Note that some of the flags listed here really should be developer +# flags (listed in separate files, gnu-warnings-developer*) but we put +# them here because they are not raised by the current code and we'd like to +# know if they do start showing up. +# +# NOTE: Don't add -Wpadded here since we can't/won't fix the (many) +# warnings that are emitted. If you need it, add it from the +# environment variable at configure time. +-Wall +-Wcast-qual +-Wconversion +-Wctor-dtor-privacy +-Weffc++ +-Wextra +-Wfloat-equal +-Wformat=2 +-Winit-self +-Winvalid-pch +-Wmissing-include-dirs +-Wno-format-nonliteral +-Wnon-virtual-dtor +-Wold-style-cast +-Woverloaded-virtual +-Wreorder +-Wshadow +-Wsign-promo +##-Wunreachable-code +-Wundef +##-Wvariadic-macros +-Wwrite-strings +-pedantic diff --git a/config/clang-warnings/no-developer-general b/config/clang-warnings/no-developer-general index 2f4e0c5..2bf1703 100644 --- a/config/clang-warnings/no-developer-general +++ b/config/clang-warnings/no-developer-general @@ -1 +1 @@ --Wmissing-noreturn +-Wno-missing-noreturn diff --git a/config/linux-gnulibc1 b/config/linux-gnulibc1 index 39225e7..1a12006 100644 --- a/config/linux-gnulibc1 +++ b/config/linux-gnulibc1 @@ -42,6 +42,9 @@ fi # Figure out Intel C compiler flags . $srcdir/config/intel-flags +# Figure out Clang C compiler flags +. $srcdir/config/clang-flags + # Use default Fortran 90 compiler according to what C compiler is used. if test "X-" = "X-$FC"; then case $CC_BASENAME in @@ -61,6 +64,10 @@ if test "X-" = "X-$FC"; then FC=mpif90 FC_BASENAME=mpif90 ;; + clang*) + FC=gfortran + FC_BASENAME=gfortran + ;; esac else case $FC in @@ -194,6 +201,9 @@ fi # Figure out GNU CXX compiler flags . $srcdir/config/gnu-cxxflags +# Figure out Clang CXX compiler flags +. $srcdir/config/clang-cxxflags + # compiler version strings # check if the compiler_version_info is already set @@ -223,6 +233,17 @@ case $CC in sed 's/\(Intel.* Compiler\).*\( Version [a-z0-9\.]*\).*\( Build [0-9]*\)/\1\2\3/'` ;; + *clang*) + # clang -v will return version number following "clang" on Linux machines, + # but on Macs the version number will follow "Apple LLVM version" + cc_version_info="`$CC $CFLAGS $H5_CFLAGS -v 2>&1 |\ + grep 'clang version' | sed 's/.*clang version \([-a-z0-9\.]*\).*/\1/'`" + else + cc_version_info="`$CC $CFLAGS $H5_CFLAGS -v 2>&1 |\ + grep 'Apple LLVM version' | sed 's/.*Apple LLVM version \([-a-z0-9\.]*\).*/\1/'`" + fi + ;; + *) echo "No match to get cc_version_info for $CC" ;; diff --git a/configure.ac b/configure.ac index db0cddf..50f823e 100644 --- a/configure.ac +++ b/configure.ac @@ -289,6 +289,58 @@ while test -n "$hname"; do test "$hname_tmp" = "$hname" && break done +## +## Enable/disable sanitizer checks for clang compilers, initially address sanitizer +## +AC_MSG_CHECKING([for clang sanitizer checks]) +AC_ARG_ENABLE([sanitize-checks], + [AS_HELP_STRING([--enable-sanitize-checks=address], + [(clang/clang++ compilers only) Enable sanitize checks. + Address is useful for detecting issues dealing with + memory. See AddressSanitizer in config/sanitizer/README.md + for more information. + [default=none] + ])], + [CLANG_SANITIZE_CHECKS=$enableval]) + +# Set default +if test "X-$CLANG_SANITIZE_CHECKS" = X- ; then + CLANG_SANITIZE_CHECKS=none +fi + +if test "X$CC_BASENAME" = "Xclang"; then + AC_SUBST([CLANG_SANITIZE_CHECKS]) + + ## These are all the packages that use H5*_DEBUG. + ## There is no harm in specifying a package not in this list; + ## you'll just get an unused H5_DEBUG symbol. + ## + ## Some packages that define debug checks or output are + ## too specialized or have huge performance hits. These + ## are not listed in the "all" packages list. + ## + ##all_packages="Address" + + case "X-$CLANG_SANITIZE_CHECKS" in + X-no|X-none) + CLANG_SANITIZE_CHECKS=none + CLANG_SANITIZE_LIST= + ;; + *) + CLANG_SANITIZE_LIST=$CLANG_SANITIZE_CHECKS + ;; + esac + AC_MSG_RESULT([$CLANG_SANITIZE_CHECKS]) + + ## Define H5*_DEBUG symbols that control package output + ## NOTE: No sanity checking done here! + if test -n "$CLANG_SANITIZE_LIST"; then + H5_CFLAGS="$H5_CFLAGS -fno-omit-frame-pointer" + for sanitizer in `echo $CLANG_SANITIZE_LIST`; do + H5_CFLAGS="$H5_CFLAGS -fsanitize=${sanitizer}" + done + fi +fi ## ---------------------------------------------------------------------- ## Determine build mode (debug, production, clean). -- cgit v0.12