https://github.com/HazardyKnusperkeks created 
https://github.com/llvm/llvm-project/pull/191502

Fixes #191295.

From 7dc71552ac9a48010208591cfd960f730fcb459d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <[email protected]>
Date: Fri, 10 Apr 2026 21:41:21 +0200
Subject: [PATCH] [clang-format] Detect language for file templates

Fixes #191295.
---
 clang/lib/Format/Format.cpp           | 6 ++++++
 clang/unittests/Format/FormatTest.cpp | 1 +
 2 files changed, 7 insertions(+)

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", ""));

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to