[clang] [clang-format] Handle multi-section config with InheritParentConfig (PR #224816)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Sep 19 00:05:27 PDT 2026
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/224816
Fixes #217085
>From 8ce4d7c7f71e12bfe24a304c6f7b5ff6c21179ea Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Sat, 19 Sep 2026 00:04:04 -0700
Subject: [PATCH] [clang-format] Handle multi-section config with
InheritParentConfig
Fixes #217085
---
clang/lib/Format/Format.cpp | 30 ++++++++++++---------
clang/test/Format/inherit-parent-config.cpp | 21 +++++++++++++++
2 files changed, 38 insertions(+), 13 deletions(-)
create mode 100644 clang/test/Format/inherit-parent-config.cpp
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 4c78c1dbe9f80f..a60301d78520b7 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 00000000000000..9b70b8cabc6d8e
--- /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";
More information about the cfe-commits
mailing list