Skip to content

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

Description

@JohnC32

Given files:

./_clang-format
./code/_clang-format
./code/raw_string_with_cpp_content.cpp
  • ./_clang-format contains:
---
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 0
...
  • ./code/_clang-format contains:
---
BasedOnStyle: InheritParentConfig
---
Language: Cpp
ColumnLimit: 100
...
  • ./code/raw_string_with_cpp_content.cpp contains:
#include <string>
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:

#include <string>
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:

#include <string>
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.

Metadata

Metadata

Assignees

Labels

Type

Projects

Status
Needs Fix

Relationships

None yet

Development

No branches or pull requests

Issue actions