Skip to content
Draft
Show file tree
Hide file tree
Changes from 9 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
53 changes: 53 additions & 0 deletions Sources/BasicContainers/RigidArray/RigidArray+Formatter.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
//
//===----------------------------------------------------------------------===//

// Generated with: ./Utils/Debugger/generate_formatters.sh

#if swift(>=6.3)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think about adding either a comment at the top of this file with instructions for how to regenerate it from the python source?
I guess that could even be a feature of the compiler script...

Copy link
Copy Markdown
Author

@kastiglione kastiglione Mar 31, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A comment is now added as part of the new generate_formatters.sh script.

#if objectFormat(MachO)
@section("__DATA_CONST,__lldbformatters")
#else
@section(".lldbformatters")
#endif
@used
let `^BasicContainers[.]RigidArray<.+>$ formatter`: (UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8, UInt8) = (
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will fail to work in single-module mode (COLLECTIONS_SINGLE_MODULE), where the sources are combined into a single module. (The Xcode project inside Xcode is a good way to exercise that. The module name in that configuration is Collections in this repo, but we edit that when we build for adopters inside Apple OSes.)

We could surround the template with #if !COLLECTIONS_SINGLE_MODULE, but that would make debugging code less pleasant for OS engineers, so the right move is probably to duplicate this:

#if COLLECTIONS_SINGLE_MODULE
...
let `^BasicContainers[.]RigidArray<.+>$ formatter`: ... = ...
#else 
...
let `^Collections[.]RigidArray<.+>$ formatter`: ... = ...
#endif

That's yucky, but it works. (I don't think we can do #if conditions just around the symbol name... 😞

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could put the set of known module names into the regex:

^(BasicContainers|Collections)[.]RigidArray<.+>$

We've discussed having the module name be implicit, but I don't yet know if that's doable in practice.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a much better solution! 👍

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated

// version
0x01,
// remaining record size
0x62,
// type name size
0x22,
// type name
0x5e, 0x42, 0x61, 0x73, 0x69, 0x63, 0x43, 0x6f, 0x6e, 0x74, 0x61, 0x69, 0x6e, 0x65, 0x72, 0x73, 0x5b, 0x2e, 0x5d, 0x52, 0x69, 0x67, 0x69, 0x64, 0x41, 0x72, 0x72, 0x61, 0x79, 0x3c, 0x2e, 0x2b, 0x3e, 0x24,
// flags
0x00,
// sig_update
0x06,
// program size
0x2a,
// program
0x20, 0x00, 0x03, 0x22, 0x08, 0x5f, 0x73, 0x74, 0x6f, 0x72, 0x61, 0x67, 0x65, 0x23, 0x12, 0x60, 0x23, 0x18, 0x60, 0x20, 0x00, 0x03, 0x22, 0x06, 0x5f, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x23, 0x12, 0x60, 0x23, 0x18, 0x60, 0x23, 0x21, 0x60, 0x21, 0x01, 0x13,
// sig_get_num_children
0x02,
// program size
0x04,
// program
0x20, 0x02, 0x03, 0x13,
// sig_get_child_at_index
0x04,
// program size
0x0a,
// program
0x20, 0x01, 0x03, 0x20, 0x03, 0x03, 0x23, 0x11, 0x60, 0x13,
)
#endif
41 changes: 41 additions & 0 deletions Utils/Debugger/Formatters/RigidArray.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#!/usr/bin/env python3
##===----------------------------------------------------------------------===##
##
## This source file is part of the Swift Collections open source project
##
## Copyright (c) YEARS Apple Inc. and the Swift project authors
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
## Copyright (c) YEARS Apple Inc. and the Swift project authors
## Copyright (c) 2026 Apple Inc. and the Swift project authors

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

## Licensed under Apache License v2.0 with Runtime Library Exception
##
## See https://swift.org/LICENSE.txt for license information
##
## SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
##
##===----------------------------------------------------------------------===##

import lldb


class RigidArraySynthetic:
valobj: lldb.SBValue
storage: lldb.SBValue
count: int

def __init__(self, valobj: lldb.SBValue, _) -> None:
self.valobj = valobj

def num_children(self) -> int:
return self.count

def get_child_at_index(self, idx: int) -> lldb.SBValue:
return self.storage.GetChildAtIndex(idx)

def update(self) -> bool:
self.storage = self.valobj.GetChildMemberWithName(
"_storage"
).GetSyntheticValue()
self.count = (
self.valobj.GetChildMemberWithName("_count")
.GetSyntheticValue()
.GetValueAsUnsigned()
)
return True
13 changes: 13 additions & 0 deletions Utils/Debugger/HEADER.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift Collections open source project
//
// Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// Copyright (c) 2024 - 2026 Apple Inc. and the Swift project authors
// Copyright (c) 2026 Apple Inc. and the Swift project authors

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
//
// SPDX-License-Identifier: Apache-2.0 WITH Swift-exception
//
//===----------------------------------------------------------------------===//

55 changes: 55 additions & 0 deletions Utils/Debugger/generate_formatters.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#!/bin/bash
#===----------------------------------------------------------------------
#
# This source file is part of the Swift Collections open source project
#
# Copyright (c) 2022 - 2026 Apple Inc. and the Swift project authors
# Licensed under Apache License v2.0 with Runtime Library Exception
#
# See https://swift.org/LICENSE.txt for license information
#
#===----------------------------------------------------------------------

set -euo pipefail

COMPILER_VERSION="f74f32b529c35531198621fd104cde5d2cf77e0c"

base=$(git rev-parse --show-toplevel)

# Download formatter_bytecode.py compiler to a temp file
compiler=$(mktemp /tmp/formatter_bytecode.XXXXXX)
trap 'rm -f "$compiler"' EXIT
curl -fsSL \
"https://raw.githubusercontent.com/llvm/llvm-project/$COMPILER_VERSION/lldb/examples/python/formatter_bytecode.py" \
-o "$compiler"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have reservations about downloading scripts sight unseen for execution on engineers' machines. Can we just copy the formatter directly into this repo, like we used to do with gyb?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated


for formatter in "$base/Utils/Debugger/Formatters"/*.py; do
type_name=$(basename "$formatter" .py)

# Find the corresponding Swift source file under Sources/
swift_file=$(find "$base/Sources" -name "${type_name}.swift" | head -1)
if [[ -z "$swift_file" ]]; then
echo "Warning: No Swift source file found for ${type_name}, skipping" >&2
continue
fi

# Derive the module name from the first path component under Sources/
rel_path="${swift_file#"$base/Sources/"}"
module=$(cut -d/ -f1 <<< "$rel_path")

# Output file sits next to the source file
target="$(dirname "$swift_file")/${type_name}+Formatter.swift"

echo "Compiling ${type_name} (module: ${module}) -> ${target}"

# Add header(license), and document this script's invocation.
cp "$base/Utils/Debugger/HEADER.swift" "$target"
echo "// Generated with: $0" "$@" >> "$target"
echo >> "$target"

python3 "$compiler" --compile \
--format swift \
--type-name "^${module}[.]${type_name}<.+>$" \
--append --output "$target" \
"$formatter"
done
Loading