Last active
August 25, 2025 12:31
-
-
Save ole/f7249f856ca2bb686f06462bd07fa324 to your computer and use it in GitHub Desktop.
swift-list-diagnostic-groups.sh: Shell script for listing the names of diagnostic groups in the Swift compiler.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# List Swift diagnostic groups. | |
# | |
# Author: Ole Begemann | |
# | |
# Usage: | |
# | |
# swift-list-diagnostic-groups [version] # default: main branch | |
# | |
# Examples: | |
# | |
# swift-list-diagnostic-groups # Queries main branch | |
# swift-list-diagnostic-groups 6.2 # Queries release/6.2 branch | |
# | |
# The output is in CSV format (delimiter: comma, including a header row). | |
# This allows you to format/filter/process the output by piping it into other | |
# tools. | |
# | |
# NOTE: See also `swift -print-diagnostic-groups` | |
# | |
# Example output: | |
# | |
# ``` | |
# .enableUpcomingFeature("ExistentialAny"), | |
# .enableUpcomingFeature("InferIsolatedConformances"), | |
# .enableUpcomingFeature("InternalImportsByDefault"), | |
# .enableUpcomingFeature("MemberImportVisibility"), | |
# .enableUpcomingFeature("NonisolatedNonsendingByDefault"), | |
# ``` | |
# | |
# This script uses curl to download the file in which the language features | |
# are defined from the Swift repo and uses Clang to parse it. | |
# Examples: | |
# | |
# 1) Print a nicely formatted table using [xan](https://github.com/medialab/xan): | |
# | |
# ```sh | |
# swift-list-diagnostic-groups.sh 6.2 | xan view --all | |
# ``` | |
# | |
# 2) Transform the docs_file column into a URL to the relevant documentation: | |
# | |
# ```sh | |
# swift-list-diagnostic-groups.sh 6.2 | xan transform --rename url docs_file '"https://docs.swift.org/compiler/documentation/diagnostics/" ++ _' | xan view --all --cols 1000 --groupby parent | |
# ``` | |
swift_version=$1 | |
if test -z "$swift_version" || test "$swift_version" = "main"; then | |
branch="main" | |
else | |
branch="release/${swift_version}" | |
fi | |
GITHUB_URL="https://raw.githubusercontent.com/apple/swift/${branch}/include/swift/AST/DiagnosticGroups.def" | |
DIAGGROUPS_DEF_FILE="$(curl --fail-with-body --silent "${GITHUB_URL}")" | |
curlStatus=$? | |
if test $curlStatus -ne 0; then | |
echo "$DIAGGROUPS_DEF_FILE" | |
echo "Error: failed to download '$GITHUB_URL'. Invalid URL?" | |
exit $curlStatus | |
fi | |
# DocsFile base URL: https://docs.swift.org/compiler/documentation/diagnostics/ | |
# The `2> /dev/null` argument in the clang call suppresses | |
# the error message that it can't find an included header file. | |
echo "name,parent,docs_file" | |
clang --preprocess --no-line-commands -nostdinc -x c - 2> /dev/null <<EOF | |
#define GROUP(Name, DocsFile) Name,,DocsFile | |
#define GROUP_LINK(Parent, Child) Child,Parent, | |
${DIAGGROUPS_DEF_FILE} | |
EOF |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example output: