Skip to content

Instantly share code, notes, and snippets.

@ole
Last active August 25, 2025 12:31
Show Gist options
  • Save ole/f7249f856ca2bb686f06462bd07fa324 to your computer and use it in GitHub Desktop.
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.
#!/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
@ole
Copy link
Author

ole commented Aug 25, 2025

Example output:

$ 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

Displaying 3 cols from 32 rows of <stdin>
┌────┬────────────────────────────────┬─────────┬─────────────────────────────────────────────────────────────────────────────────────────────────┐
│ -  │ name                           │ parent  │ url                                                                                             │
├────┼────────────────────────────────┼─────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ 0  │ no_group                       │ <empty> │ https://docs.swift.org/compiler/documentation/diagnostics/                                      │
│ 1  │ ActorIsolatedCall              │         │ https://docs.swift.org/compiler/documentation/diagnostics/actor-isolated-call                   │
│ 2  │ AvailabilityUnrecognizedName   │         │ https://docs.swift.org/compiler/documentation/diagnostics/availability-unrecognized-name        │
│ 3  │ ClangDeclarationImport         │         │ https://docs.swift.org/compiler/documentation/diagnostics/clang-declaration-import              │
│ 4  │ ConformanceIsolation           │         │ https://docs.swift.org/compiler/documentation/diagnostics/conformance-isolation                 │
│ 5  │ DeprecatedDeclaration          │         │ https://docs.swift.org/compiler/documentation/diagnostics/deprecated-declaration                │
│ 6  │ DynamicCallable                │         │ https://docs.swift.org/compiler/documentation/diagnostics/dynamic-callable-requirements         │
│ 7  │ ErrorInFutureSwiftVersion      │         │ https://docs.swift.org/compiler/documentation/diagnostics/error-in-future-swift-version         │
│ 8  │ ExistentialAny                 │         │ https://docs.swift.org/compiler/documentation/diagnostics/existential-any                       │
│ 9  │ ExistentialMemberAccess        │         │ https://docs.swift.org/compiler/documentation/diagnostics/existential-member-access-limitations │
│ 10 │ ImplementationOnlyDeprecated   │         │ https://docs.swift.org/compiler/documentation/diagnostics/implementation-only-deprecated        │
│ 11 │ IsolatedConformances           │         │ https://docs.swift.org/compiler/documentation/diagnostics/isolated-conformances                 │
│ 12 │ MemberImportVisibility         │         │ https://docs.swift.org/compiler/documentation/diagnostics/member-import-visibility              │
│ 13 │ MissingModuleOnKnownPaths      │         │ https://docs.swift.org/compiler/documentation/diagnostics/missing-module-on-known-paths         │
│ 14 │ MultipleInheritance            │         │ https://docs.swift.org/compiler/documentation/diagnostics/multiple-inheritance                  │
│ 15 │ MutableGlobalVariable          │         │ https://docs.swift.org/compiler/documentation/diagnostics/mutable-global-variable               │
│ 16 │ NominalTypes                   │         │ https://docs.swift.org/compiler/documentation/diagnostics/nominal-types                         │
│ 17 │ NonisolatedNonsendingByDefault │         │ https://docs.swift.org/compiler/documentation/diagnostics/nonisolated-nonsending-by-default     │
│ 18 │ OpaqueTypeInference            │         │ https://docs.swift.org/compiler/documentation/diagnostics/opaque-type-inference                 │
│ 19 │ PreconcurrencyImport           │         │ https://docs.swift.org/compiler/documentation/diagnostics/preconcurrency-import                 │
│ 20 │ PropertyWrappers               │         │ https://docs.swift.org/compiler/documentation/diagnostics/property-wrapper-requirements         │
│ 21 │ ProtocolTypeNonConformance     │         │ https://docs.swift.org/compiler/documentation/diagnostics/protocol-type-non-conformance         │
│ 22 │ ResultBuilderMethods           │         │ https://docs.swift.org/compiler/documentation/diagnostics/result-builder-methods                │
│ 23 │ SendableClosureCaptures        │         │ https://docs.swift.org/compiler/documentation/diagnostics/sendable-closure-captures             │
│ 24 │ SendableMetatypes              │         │ https://docs.swift.org/compiler/documentation/diagnostics/sendable-metatypes                    │
│ 25 │ SendingRisksDataRace           │         │ https://docs.swift.org/compiler/documentation/diagnostics/sending-risks-data-race               │
│ 26 │ StrictLanguageFeatures         │         │ https://docs.swift.org/compiler/documentation/diagnostics/strict-language-features              │
│ 27 │ StrictMemorySafety             │         │ https://docs.swift.org/compiler/documentation/diagnostics/strict-memory-safety                  │
│ 28 │ StringInterpolationConformance │         │ https://docs.swift.org/compiler/documentation/diagnostics/string-interpolation-conformance      │
│ 29 │ TemporaryPointers              │         │ https://docs.swift.org/compiler/documentation/diagnostics/temporary-pointers                    │
│ 30 │ TrailingClosureMatching        │         │ https://docs.swift.org/compiler/documentation/diagnostics/trailing-closure-matching             │
│ 31 │ UnknownWarningGroup            │         │ https://docs.swift.org/compiler/documentation/diagnostics/unknown-warning-group                 │
├────┼────────────────────────────────┼─────────┼─────────────────────────────────────────────────────────────────────────────────────────────────┤
│ -  │ name                           │ parent  │ url                                                                                             │
└────┴────────────────────────────────┴─────────┴─────────────────────────────────────────────────────────────────────────────────────────────────┘
Displaying 3 cols from 32 rows of <stdin>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment