Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions utils/find-unused-diagnostics.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@
# This script produces a list of all diagnostics that are defined
# but not used in sources.
#
set -euo pipefail

# Gather all diagnostic identifiers.
ALL_DIAGS=$(grep -E --only-matching --no-filename 'ERROR\([a-z_]+,' include/swift/AST/Diagnostics*.def | sed -e 's/ERROR(//' -e 's/,//')
ALL_DIAGS=$(
grep -Eho '(ERROR|WARNING|NOTE|REMARK)\([a-z_]+,' include/swift/AST/Diagnostics*.def \
| sed -E 's/(ERROR|WARNING|NOTE|REMARK)\(([a-z_]+),/\2/')

# Now look for all potential identifiers in the source files.
ALL_SOURCES=$(find lib include tools -name \*.cpp -or -name \*.h)
DIAGS_IN_SOURCES=$(grep -E --only-matching --no-filename 'diag::[a-z_]+' $ALL_SOURCES | sed -e 's/diag:://')
# Now look for all potential identifiers in the (C++) source files.
CXX_SOURCES=$(find lib include tools -name \*.cpp -or -name \*.h)
DIAGS_IN_CXX_SOURCES=$(
grep -Ehoz 'diag::\n?\s*[a-z_]+' $CXX_SOURCES \
| sed -e 's/diag:://' -e 's/[[:space:]]//g')

# Print all diags that occur in the .td files but not in the source.
comm -23 <(sort -u <<< "$ALL_DIAGS") <(sort -u <<< "$DIAGS_IN_SOURCES")
# Get potentially unused diags from C++ sources.
POTENTIALLY_UNUSED=$(comm -23 <(sort -u <<< "$ALL_DIAGS") <(sort -u <<< "$DIAGS_IN_CXX_SOURCES"))

# Finally, check if any of the possibly-unused diags appear in Swift sources, and exclude them.
SWIFT_SOURCES=$(find SwiftCompilerSources -name \*.swift)
if [ -n "$SWIFT_SOURCES" ] && [ -n "$POTENTIALLY_UNUSED" ]; then
DIAGS_IN_SWIFT=$(grep -Fho -f <(echo "$POTENTIALLY_UNUSED") $SWIFT_SOURCES)
comm -23 <(echo "$POTENTIALLY_UNUSED") <(sort -u <<< "$DIAGS_IN_SWIFT")
else
echo "$POTENTIALLY_UNUSED"
fi