llvm / llvm/llvm-project

[clang-format] Clang bug with formatting enum

Open
#223,011 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

The error was first introduced at https://github.com/llvm/llvm-project/pull/194154.

With a simple reproducer at:

```c++
enum Flags {
FLAG_WIN_NO_EXECUTE =
1 << 21, // Windows only. Marks the file with a deny ACE that prevents
// opening the file with EXECUTE access. Cannot be used with
// FILE_WIN_EXECUTE flag. See also PreventExecuteMapping.
};
```

When executing the clang formatter, this becomes:

```c++
// Minimal reproducer for clang-format enum line-breaking regression.
enum Flags {
FLAG_WIN_NO_EXECUTE = 1
<< 21, // Windows only. Marks the file with a deny ACE that prevents
// opening the file with EXECUTE access. Cannot be used with
// FILE_WIN_EXECUTE flag. See also PreventExecuteMapping.
};
```

Seems like the issue is at:

```c++
// clang/lib/Format/TokenAnnotator.cpp
if (!Scopes.empty() && Scopes.back() == ST_Enum)
Tok->setFinalizedType(TT_EnumEqual);
```

Because this is checking if there should be a value after = within the context of Enum.

Contributor guide

Open the contributing guide

Research direction

Reproduce the enum formatting regression from the issue, then inspect clang/lib/Format/TokenAnnotator.cpp at the shown ST_Enum and TT_EnumEqual logic. Verify the formatter's output for the provided C++ example and trace how the enum assignment and shift expression are classified. Done means the example formats correctly without moving the shift operator onto a separate line.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
74/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.