[clang] [clang-format] Ignore timestamps in clang-format-diff.py file headers (PR #240061)

via cfe-commits cfe-commits at lists.llvm.org
Sat Nov 7 14:58:11 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-format

Author: Thejesh (thejesh23)

<details>
<summary>Changes</summary>

The file name on `+++` lines now ends at the first tab, so headers from `diff -u` and `svn diff` that carry a timestamp work. This adds a lit test and a `%clang_format_diff` substitution.

Fixes #<!-- -->240059

Assisted-by: Claude Code (claude-opus-5-5). I reviewed the change. I ran the new test locally: it fails before the change and passes after it. Normal `git diff` input, including file names with spaces, still works.


---
Full diff: https://github.com/llvm/llvm-project/pull/240061.diff


4 Files Affected:

- (added) clang/test/Format/Inputs/clang-format-diff-timestamp.diff (+4) 
- (added) clang/test/Format/clang-format-diff.cpp (+8) 
- (modified) clang/test/lit.cfg.py (+13) 
- (modified) clang/tools/clang-format/clang-format-diff.py (+2-1) 


``````````diff
diff --git a/clang/test/Format/Inputs/clang-format-diff-timestamp.diff b/clang/test/Format/Inputs/clang-format-diff-timestamp.diff
new file mode 100644
index 00000000000000..b884192b6a437a
--- /dev/null
+++ b/clang/test/Format/Inputs/clang-format-diff-timestamp.diff
@@ -0,0 +1,4 @@
+--- a/test.cpp	2026-01-01 00:00:00.000000000 +0000
++++ b/test.cpp	2026-01-01 00:00:00.000000000 +0000
+@@ -0,0 +1 @@
++int  b;
diff --git a/clang/test/Format/clang-format-diff.cpp b/clang/test/Format/clang-format-diff.cpp
new file mode 100644
index 00000000000000..caeb429cad381b
--- /dev/null
+++ b/clang/test/Format/clang-format-diff.cpp
@@ -0,0 +1,8 @@
+// RUN: rm -rf %t.dir && mkdir -p %t.dir
+// RUN: echo 'int  b;' > %t.dir/test.cpp
+
+// Header lines from diff and svn may carry a tab-separated timestamp.
+// RUN: cd %t.dir && not %clang_format_diff -p1 -style=LLVM \
+// RUN:   < %S/Inputs/clang-format-diff-timestamp.diff | FileCheck %s
+// CHECK: -int  b;
+// CHECK-NEXT: +int b;
diff --git a/clang/test/lit.cfg.py b/clang/test/lit.cfg.py
index bd1dac8044f73b..43ba39ac5e845a 100644
--- a/clang/test/lit.cfg.py
+++ b/clang/test/lit.cfg.py
@@ -295,6 +295,19 @@ def have_host_clang_repl_thread_local():
     )
 )
 
+config.substitutions.append(
+    (
+        "%clang_format_diff",
+        '"%s" %s'
+        % (
+            config.python_executable,
+            os.path.join(
+                config.clang_src_dir, "tools", "clang-format", "clang-format-diff.py"
+            ),
+        ),
+    )
+)
+
 config.substitutions.append(
     (
         "%deps-to-rsp",
diff --git a/clang/tools/clang-format/clang-format-diff.py b/clang/tools/clang-format/clang-format-diff.py
index 3059982ba231f1..5a9478a06ace08 100755
--- a/clang/tools/clang-format/clang-format-diff.py
+++ b/clang/tools/clang-format/clang-format-diff.py
@@ -102,7 +102,8 @@ def main():
     filename = None
     lines_by_file = {}
     for line in sys.stdin:
-        match = re.search(r"^\+\+\+\ (.*?/){%s}(.+)" % args.p, line.rstrip())
+        # Stop at a tab, after which diff and svn may append a timestamp.
+        match = re.search(r"^\+\+\+\ (.*?/){%s}([^\t]+)" % args.p, line.rstrip())
         if match:
             filename = match.group(2)
         if filename is None:

``````````

</details>


https://github.com/llvm/llvm-project/pull/240061


More information about the cfe-commits mailing list