llvm / llvm/llvm-project

clang-format 22 BasedOnStyle InheritParentConfig regression for C++ raw strings with CPP content

Open
#217,085 4 comments 0 reactions 1 assignee Assigned to @owenca View on GitHub
clang-format confirmed regression:22
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Given files:

```
./_clang-format
./code/_clang-format
./code/raw_string_with_cpp_content.cpp
```

* `./_clang-format` contains:

```yaml
---
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 0
...
```

* `./code/_clang-format` contains:

```yaml
---
BasedOnStyle: InheritParentConfig
---
Language: Cpp
ColumnLimit: 100
...
```

* `./code/raw_string_with_cpp_content.cpp` contains:

```c++
#include
extern std::string foo(void);

std::string foo(void) {
std::string s = R"CPP(
int foo(int x) {
int y = x *2;
int c = 0;
while (y > 0) {
if (y > 10) {
y -= 2;
} else {
y--;
}
c++;
}
return c;
}
)CPP";
return s;
}

```

Running clang-format 21.1 (I used 21.1.1 via `/path/to/llvm-21.1/clang-format -style=file code/raw_string_with_cpp_content.cpp >code/out.21.1.run.cpp`) yields:

```c++
#include
extern std::string foo(void);

std::string foo(void) {
std::string s = R"CPP(
int foo(int x) {
int y = x * 2;
int c = 0;
while (y > 0) {
if (y > 10) {
y -= 2;
} else {
y--;
}
c++;
}
return c;
}
)CPP";
return s;
}

```

Running clang-format 22.1 (I used 22.1.4 via `/path/to/llvm-22.1/clang-format -style=file code/raw_string_with_cpp_content.cpp >code/out.22.1.run.cpp`) yields:

```c++
#include
extern std::string foo(void);

std::string foo(void) {
std::string s = R"CPP(
int foo(int x) {
int y = x * 2;
int c = 0;
while (y > 0) {
if (y > 10) {
y -= 2;
} else {
y--;
}
c++;
}
return c;
}
)CPP";
return s;
}
```

Notice that the "IndentWidth: 4" is no longer honored with clang-format 22.1. This problem
occurs when using "BasedOnStyle: InheritParentConfig" as shown. If I merge the two _clang-format
files into one, the we get the expected results.

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.