[clang] [clang-format] Handle multi-section config with InheritParentConfig (PR #224816)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 19 00:06:09 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: owenca (owenca)
<details>
<summary>Changes</summary>
Fixes #<!-- -->217085
---
Full diff: https://github.com/llvm/llvm-project/pull/224816.diff
2 Files Affected:
- (modified) clang/lib/Format/Format.cpp (+17-13)
- (added) clang/test/Format/inherit-parent-config.cpp (+21)
``````````diff
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 4c78c1dbe9f80..a60301d78520b 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -2568,30 +2568,34 @@ std::error_code parseConfiguration(llvm::MemoryBufferRef Config,
CPos = I;
}
+ auto &Style0 = Styles[0];
+ const bool HasDefaultStyle = Style0.Language == FormatStyle::LK_None;
+
// If Language is not found, use the default style if there is one. Otherwise,
// use the C style for C++ .h files and for backward compatibility, the C++
// style for .c files.
if (LanguagePos < 0) {
- if (Styles[0].Language == FormatStyle::LK_None) // Default style.
+ if (HasDefaultStyle) {
LanguagePos = 0;
- else if (IsDotHFile && Language == FormatStyle::LK_Cpp)
- LanguagePos = CPos;
- else if (!IsDotHFile && Language == FormatStyle::LK_C)
- LanguagePos = CppPos;
- if (LanguagePos < 0)
- return make_error_code(ParseError::Unsuitable);
+ Style0.Language = Language;
+ } else {
+ if (IsDotHFile && Language == FormatStyle::LK_Cpp)
+ LanguagePos = CPos;
+ else if (!IsDotHFile && Language == FormatStyle::LK_C)
+ LanguagePos = CppPos;
+ if (LanguagePos < 0)
+ return make_error_code(ParseError::Unsuitable);
+ Language = Styles[LanguagePos].Language;
+ }
}
for (const auto &S : llvm::reverse(llvm::drop_begin(Styles)))
Style->StyleSet.Add(S);
- *Style = Styles[LanguagePos];
+ if (!HasDefaultStyle || LanguagePos == 0)
+ Style->StyleSet.Add(Style0);
- if (LanguagePos == 0) {
- if (Style->Language == FormatStyle::LK_None) // Default style.
- Style->Language = Language;
- Style->StyleSet.Add(*Style);
- }
+ *Style = *Style->StyleSet.Get(Language);
if (Style->InsertTrailingCommas != FormatStyle::TCS_None &&
(Style->PackArguments.BinPack == FormatStyle::BPAS_BinPack ||
diff --git a/clang/test/Format/inherit-parent-config.cpp b/clang/test/Format/inherit-parent-config.cpp
new file mode 100644
index 0000000000000..9b70b8cabc6d8
--- /dev/null
+++ b/clang/test/Format/inherit-parent-config.cpp
@@ -0,0 +1,21 @@
+// RUN: rm -rf %t.dir
+// RUN: mkdir -p %t.dir/code
+
+// RUN: cd %t.dir
+// RUN: echo "BasedOnStyle: Google" > .clang-format
+// RUN: echo "IndentWidth: 4" >> .clang-format
+
+// RUN: cd code
+// RUN: echo "BasedOnStyle: InheritParentConfig" > .clang-format
+// RUN: echo "---" >> .clang-format
+// RUN: echo "Language: Cpp" >> .clang-format
+
+// RUN: clang-format -style=file:.clang-format %s \
+// RUN: | FileCheck %s --strict-whitespace
+// CHECK: {{^ {8}//}}
+
+s = R"CPP(
+ void foo() {
+ // "IndentWidth: 4" applies here, resulting in 8 leading spaces.
+ }
+)CPP";
``````````
</details>
https://github.com/llvm/llvm-project/pull/224816
More information about the cfe-commits
mailing list