Skip to content

Instantly share code, notes, and snippets.

@flamewing
Last active June 15, 2021 14:26
Show Gist options
  • Save flamewing/286f74078bc83f3af479a9a6cd5a9621 to your computer and use it in GitHub Desktop.
Save flamewing/286f74078bc83f3af479a9a6cd5a9621 to your computer and use it in GitHub Desktop.
Bash script to get a GCC equivalent of -Weverything
#!/bin/bash
# Change extension here for the language you want warnings for.
BASEFILE=a.cc
# Option for language standard goes here.
STD=(-std=c++14)
# Create temporary for detection of warnings.
touch ${BASEFILE}
# This causes GCC to print a list of warnings with their current values.
# A few examples to illustrate:
# * Set of values : -Waligned-new=[none|global|all] none
# * Range of values : -Wattribute-alias=<0,2> 1
# * Aliases another : -Wshadow-compatible-local -Wshadow=compatible-local
# * For another language: -Wsurprising [available in Fortran]
# * Currently disabled : -Wswitch-default [disabled]
# * Currently enabled : -Wswitch-unreachable [enabled]
# * Ignored warning : -Wunreachable-code [ignored]
WHELP=(-Q --help=warnings)
# Enables most warnings.
WALL=(-Wall -Wextra -Wpedantic)
# Select whether you want the compatibility warnings with other standards or not.
mapfile -t WCOMPAT < <(g++ "${STD[@]}" "${WALL[@]}" "${WHELP[@]}" ${BASEFILE} | grep -o '[-]Wc++[A-Za-z0-9+-]\+-compat')
mapfile -t WNOCOMPAT < <(echo "${WCOMPAT[@]}" | sed -r 's/-W([A-Za-z0-9+-]+)/-Wno-\1/g')
# We force enable the compat warnings for the next two sets of warnings so that
# the options warnings enable will not be enabled when the compat warnings are
# not enabled.
# We also filter out -Wabi, -Wabi-tag, and -Wnormalized.
mapfile -t WLIST < <(g++ "${STD[@]}" "${WALL[@]}" "${WCOMPAT[@]}" "${WHELP[@]}" ${BASEFILE} | grep '^\s*-W[^ ]' | grep -v '\(-Wc++[A-Za-z0-9+-]\+-compat\|-Wnormalized\|-Wstack-usage\|larger-than\|\[\(available.\+\|enabled\)\]\|Wabi\)')
# All binary warnings not enabled by the previous warning flag.
mapfile -t WADDITIONAL < <(echo "${WLIST[@]}" | grep '\[disabled\]' | grep -o '[-]W[a-zA-Z0-9+=-]\+' | grep -v '[-]W[a-zA-Z0-9+-]\+=\s*$')
# Maximizes all integer range warning options. Might be a good idea to filter out
# -Wstrict-overflow=5 to -Wstrict-overflow=4 here.
mapfile -t WMAXRANGES < <(echo "${WLIST[@]}" | grep -Eo '^\s*-W[A-Za-z0-9+-]+=<[0-9]+,[0-9]+>' | sed -r 's/\s*(-W[A-Za-z0-9+-]+=)<[0-9]+,([0-9]+)>/\1\2/')
# We have two warnings with a set of allowable values, -Waligned-new and -Wnormalized.
# -Wnormalized is best left at the default, and we filtered it out before.
mapfile -t WVALUESET < <(echo "${WLIST[@]}" | grep -Eo '^\s*-W[A-Za-z0-9+-]+=\[[A-Za-z0-9]+\|+[A-Za-z0-9]+\]' | sed -r 's/\s+(-W[A-Za-z0-9+-]+=)\[([A-Za-z0-9]+\|)+([A-Za-z0-9]+)\]/\1\3/')
# Print all warnings.
echo "${WCOMPAT[@]}"
echo
echo "${WNOCOMPAT[@]}"
echo
echo "${STD[@]}" "${WALL[@]}" "${WCOMPAT[@]}" "${WADDITIONAL[@]}" "${WMAXRANGES[@]}" "${WVALUESET[@]}"
# Cleanup.
rm ${BASEFILE}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment