[clang] [clang-format] Add 'cl' to enable OpenCL kernel file formatting (PR #134529)

Wenju He via cfe-commits cfe-commits at lists.llvm.org
Tue Apr 8 00:48:58 PDT 2025


https://github.com/wenju-he updated https://github.com/llvm/llvm-project/pull/134529

>From ac389b8b92fbb77c8884515d8f7293b4af17dfa5 Mon Sep 17 00:00:00 2001
From: Wenju He <wenju.he at intel.com>
Date: Sun, 6 Apr 2025 18:30:42 +0800
Subject: [PATCH 1/4] [clang-format] Add 'cl' to enable OpenCL kernel file
 formatting

---
 clang/tools/clang-format/git-clang-format | 1 +
 1 file changed, 1 insertion(+)

diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index 85eff4761e289..ba324b14ab80d 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -126,6 +126,7 @@ def main():
             "pb.txt",
             "textproto",
             "asciipb",  # TextProto
+            "cl", # OpenCL
         ]
     )
 

>From 88c11747fcc8db1921dfd8f73c9330c662f7fd91 Mon Sep 17 00:00:00 2001
From: Wenju He <wenju.he at intel.com>
Date: Mon, 7 Apr 2025 16:17:39 -0700
Subject: [PATCH 2/4] return C language format style for OpenCL

---
 clang/lib/Format/Format.cpp                   | 5 +++++
 clang/test/Format/dump-config-opencl-stdin.cl | 7 +++++++
 clang/test/Format/lit.local.cfg               | 3 ++-
 clang/unittests/Format/FormatTest.cpp         | 3 +++
 4 files changed, 17 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/Format/dump-config-opencl-stdin.cl

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 226d39f635676..0565d6d46eb32 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -4094,6 +4094,9 @@ static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
       FileName.ends_with_insensitive(".vh")) {
     return FormatStyle::LK_Verilog;
   }
+  // OpenCL is based on C99 and C11.
+  if (FileName.ends_with(".cl"))
+    return FormatStyle::LK_C;
   return FormatStyle::LK_Cpp;
 }
 
@@ -4121,6 +4124,8 @@ static FormatStyle::LanguageKind getLanguageByComment(const Environment &Env) {
       return FormatStyle::LK_Cpp;
     if (Text == "ObjC")
       return FormatStyle::LK_ObjC;
+    if (Text == "OpenCL")
+      return FormatStyle::LK_C;
   }
 
   return FormatStyle::LK_None;
diff --git a/clang/test/Format/dump-config-opencl-stdin.cl b/clang/test/Format/dump-config-opencl-stdin.cl
new file mode 100644
index 0000000000000..d02a3fb287a42
--- /dev/null
+++ b/clang/test/Format/dump-config-opencl-stdin.cl
@@ -0,0 +1,7 @@
+// RUN: clang-format -assume-filename=foo.cl -dump-config | FileCheck %s
+
+// RUN: clang-format -dump-config - < %s | FileCheck %s
+
+// CHECK: Language: C
+
+void foo() {}
diff --git a/clang/test/Format/lit.local.cfg b/clang/test/Format/lit.local.cfg
index b060c79226cbd..3717ee0dac577 100644
--- a/clang/test/Format/lit.local.cfg
+++ b/clang/test/Format/lit.local.cfg
@@ -20,7 +20,8 @@ config.suffixes = [
     ".textpb",
     ".asciipb",
     ".td",
-    ".test"
+    ".test",
+    ".cl"
 ]
 
 # AIX 'diff' command doesn't support --strip-trailing-cr, but the internal
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 69c9ee1d1dcb2..146ec9e0a1616 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25187,6 +25187,9 @@ TEST_F(FormatTest, GetLanguageByComment) {
   EXPECT_EQ(FormatStyle::LK_ObjC,
             guessLanguage("foo.h", "// clang-format Language: ObjC\n"
                                    "int i;"));
+  EXPECT_EQ(FormatStyle::LK_C,
+            guessLanguage("foo.h", "// clang-format Language: OpenCL\n"
+                                   "int i;"));
 }
 
 TEST_F(FormatTest, TypenameMacros) {

>From 69825a4bd73df7bdfaf21c52880ed1441c1d4d6b Mon Sep 17 00:00:00 2001
From: Wenju He <wenju.he at intel.com>
Date: Mon, 7 Apr 2025 23:48:38 -0700
Subject: [PATCH 3/4] Revert "return C language format style for OpenCL"

This reverts commit 88c11747fcc8db1921dfd8f73c9330c662f7fd91.
---
 clang/lib/Format/Format.cpp                   | 5 -----
 clang/test/Format/dump-config-opencl-stdin.cl | 7 -------
 clang/test/Format/lit.local.cfg               | 3 +--
 clang/unittests/Format/FormatTest.cpp         | 3 ---
 4 files changed, 1 insertion(+), 17 deletions(-)
 delete mode 100644 clang/test/Format/dump-config-opencl-stdin.cl

diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 0565d6d46eb32..226d39f635676 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -4094,9 +4094,6 @@ static FormatStyle::LanguageKind getLanguageByFileName(StringRef FileName) {
       FileName.ends_with_insensitive(".vh")) {
     return FormatStyle::LK_Verilog;
   }
-  // OpenCL is based on C99 and C11.
-  if (FileName.ends_with(".cl"))
-    return FormatStyle::LK_C;
   return FormatStyle::LK_Cpp;
 }
 
@@ -4124,8 +4121,6 @@ static FormatStyle::LanguageKind getLanguageByComment(const Environment &Env) {
       return FormatStyle::LK_Cpp;
     if (Text == "ObjC")
       return FormatStyle::LK_ObjC;
-    if (Text == "OpenCL")
-      return FormatStyle::LK_C;
   }
 
   return FormatStyle::LK_None;
diff --git a/clang/test/Format/dump-config-opencl-stdin.cl b/clang/test/Format/dump-config-opencl-stdin.cl
deleted file mode 100644
index d02a3fb287a42..0000000000000
--- a/clang/test/Format/dump-config-opencl-stdin.cl
+++ /dev/null
@@ -1,7 +0,0 @@
-// RUN: clang-format -assume-filename=foo.cl -dump-config | FileCheck %s
-
-// RUN: clang-format -dump-config - < %s | FileCheck %s
-
-// CHECK: Language: C
-
-void foo() {}
diff --git a/clang/test/Format/lit.local.cfg b/clang/test/Format/lit.local.cfg
index 3717ee0dac577..b060c79226cbd 100644
--- a/clang/test/Format/lit.local.cfg
+++ b/clang/test/Format/lit.local.cfg
@@ -20,8 +20,7 @@ config.suffixes = [
     ".textpb",
     ".asciipb",
     ".td",
-    ".test",
-    ".cl"
+    ".test"
 ]
 
 # AIX 'diff' command doesn't support --strip-trailing-cr, but the internal
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 146ec9e0a1616..69c9ee1d1dcb2 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -25187,9 +25187,6 @@ TEST_F(FormatTest, GetLanguageByComment) {
   EXPECT_EQ(FormatStyle::LK_ObjC,
             guessLanguage("foo.h", "// clang-format Language: ObjC\n"
                                    "int i;"));
-  EXPECT_EQ(FormatStyle::LK_C,
-            guessLanguage("foo.h", "// clang-format Language: OpenCL\n"
-                                   "int i;"));
 }
 
 TEST_F(FormatTest, TypenameMacros) {

>From 1c75586b5473644e6b3bf01afa2661f0af921fb8 Mon Sep 17 00:00:00 2001
From: Wenju He <wenju.he at intel.com>
Date: Tue, 8 Apr 2025 00:48:45 -0700
Subject: [PATCH 4/4] move after line 108, add clcpp as well

---
 clang/tools/clang-format/git-clang-format | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/tools/clang-format/git-clang-format b/clang/tools/clang-format/git-clang-format
index ba324b14ab80d..b81077a3f5ab9 100755
--- a/clang/tools/clang-format/git-clang-format
+++ b/clang/tools/clang-format/git-clang-format
@@ -106,6 +106,7 @@ def main():
             "c++m",  # C++ Modules
             "cu",
             "cuh",  # CUDA
+            "cl",  # OpenCL
             # Other languages that clang-format supports
             "proto",
             "protodevel",  # Protocol Buffers
@@ -126,7 +127,6 @@ def main():
             "pb.txt",
             "textproto",
             "asciipb",  # TextProto
-            "cl", # OpenCL
         ]
     )
 



More information about the cfe-commits mailing list