[clang] [clang-format] Detect language for file templates (PR #191502)
via cfe-commits
cfe-commits at lists.llvm.org
Fri Apr 10 12:46:20 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Björn Schäpers (HazardyKnusperkeks)
<details>
<summary>Changes</summary>
Fixes #<!-- -->191295.
---
Full diff: https://github.com/llvm/llvm-project/pull/191502.diff
2 Files Affected:
- (modified) clang/lib/Format/Format.cpp (+6)
- (modified) clang/unittests/Format/FormatTest.cpp (+1)
``````````diff
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 42190604b3881..0d0fdd81bc842 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -4428,6 +4428,12 @@ const char *StyleOptionHelpDescription =
" --style=\"{BasedOnStyle: llvm, IndentWidth: 8}\"";
static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
+ constexpr std::array TemplateSuffixes{StringRef{".in"},
+ StringRef{".tempalte"}};
+ for (auto Suffix : TemplateSuffixes)
+ if (FileName.ends_with(Suffix))
+ FileName = FileName.drop_back(Suffix.size());
+
if (FileName.ends_with(".c"))
return FormatStyle::LK_C;
if (FileName.ends_with(".java"))
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index abe546542b4af..6591ec88af745 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -22354,6 +22354,7 @@ TEST_F(FormatTest, StructuredBindings) {
TEST_F(FormatTest, FileAndCode) {
EXPECT_EQ(FormatStyle::LK_C, guessLanguage("foo.c", ""));
+ EXPECT_EQ(FormatStyle::LK_C, guessLanguage("foo.c.in", ""));
EXPECT_EQ(FormatStyle::LK_Cpp, guessLanguage("foo.cc", ""));
EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.m", ""));
EXPECT_EQ(FormatStyle::LK_ObjC, guessLanguage("foo.mm", ""));
``````````
</details>
https://github.com/llvm/llvm-project/pull/191502
More information about the cfe-commits
mailing list