llvm / llvm/llvm-project

[clang-format] Feature Request: Per-type alignment for AlignArrayOfStructures

Open
#179,792 1 comment 0 reactions 0 assignees View on GitHub
clang-format
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Summary

Add support for per-type column alignment in AlignArrayOfStructures, allowing different alignment strategies for numeric vs. string types.

Current Behavior

AlignArrayOfStructures currently supports three options: Left, Right, and None. The chosen alignment applies uniformly to all columns regardless of data type.

`AlignArrayOfStructures: Right`

Results in:

```c++
struct Entry data[] = {
{ 1, "foo", 3.14},
{123, "hello", 2.718},
{ 7, "ab", 0.0},
};
```

Notice that strings like "foo" and "hello" are right-aligned, which is unconventional and harder to read for text data.

Desired Behavior

Allow per-type alignment configuration. For example:

```yaml
AlignArrayOfStructures:
Numeric: Right
String: Left
Default: Left # fallback for other types
```

This would produce:

```c++
struct Entry data[] = {
{ 1, "foo", 3.14},
{123, "hello", 2.718},
{ 7, "ab", 0.0},
};
```

Numbers are right-aligned (easier to compare magnitudes)
Strings are left-aligned (conventional text alignment)
Rationale

Readability: Right-alignment is natural for numbers (like spreadsheets), while left-alignment is conventional for text. Mixed data benefits from mixed alignment.

Industry convention: Spreadsheets, tables in documents, and most data display tools use right-alignment for numbers and left-alignment for text.

Current workaround is cumbersome: Users must disable clang-format entirely for these sections using // clang-format off, which defeats the purpose of automated formatting.

Proposed Configuration Options

Option A: Structured YAML (preferred)

AlignArrayOfStructures:
Enabled: true
Numeric: Right
String: Left
Pointer: Left
Default: Left

Option B: Simple combined values

AlignArrayOfStructures: RightNumericLeftString

Backward Compatibility

The existing values (Left, Right, None) should continue to work as they do today, applying uniform alignment to all columns.

Environment

clang-format version: 21.x
Operating System: macOS / Linux
Additional Context

This enhancement would provide more flexible and readable output for common patterns like lookup tables, configuration arrays, and test data fixtures, which often contain mixed numeric and string data.

Contributor guide

Open the contributing guide

Research direction

No source files or tests are named. Start by locating the clang-format AlignArrayOfStructures option and its existing Left, Right, and None handling, then inspect the related formatting tests. Done means type-specific numeric, string, pointer, and fallback alignment is defined, implemented, tested, and existing values remain backward compatible.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.