llvm / llvm/llvm-project

[clang-format] Incorrectly formats values using UTF8 characters

Open
#179,891 6 comments 0 reactions 0 assignees View on GitHub
clang-format question
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

If I define a `char` or `uint8_t` array using UTF characters (e.g. `𠜎`) then ClangFormat incorrectly formats it by adding an unexpected newline.

Example (when configured for line-length = 100) and tab = 8 spaces:

```c
/* 8 + 65 characters gets formatted to next line */
const uint8_t new_data_a[] = "𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎";
/* 8 + 65 characters does not get formatted to next line */
const uint8_t new_data_b[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
/* 8 + 87 characters does not get formatted to next line */
const uint8_t new_data_c[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
```

The line

```c
const uint8_t new_data_a[] = "𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎";
```

gets incorrectly/unexpected formatted as
```c
const uint8_t new_data_a[] =
"𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎𠜎";
```

**Logs**

N/A

**System information**

```
clangd version 21.1.6
Features: linux
Platform: x86_64-pc-linux-gnu
```

Editor/LSP plugin:
https://marketplace.visualstudio.com/items?itemName=llvm-vs-code-extensions.vscode-clangd but also happens when running `clang-format` outside of the editor/extension

Operating system:
Linux

Originally created as https://github.com/clangd/clangd/issues/2582

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.