[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:57:23 PST 2026
https://github.com/thejesh23 created https://github.com/llvm/llvm-project/pull/240061
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.
>From b94e4d33a01d6c79e386d58e51fbd49fa7c65901 Mon Sep 17 00:00:00 2001
From: Thejesh <thejesh36 at gmail.com>
Date: Sat, 7 Nov 2026 14:56:47 -0800
Subject: [PATCH] [clang-format] Ignore timestamps in clang-format-diff.py file
headers
`diff -u` and `svn diff` put a tab and a timestamp or label after the
file name on `+++` lines. clang-format-diff.py kept that text as part of
the file name, so the -iregex filter never matched and nothing was
formatted, including for the svn command shown in the script's help.
Stop the file name at the first tab and add a lit test.
Assisted-by: Claude Code (claude-opus-5-5)
---
.../Format/Inputs/clang-format-diff-timestamp.diff | 4 ++++
clang/test/Format/clang-format-diff.cpp | 8 ++++++++
clang/test/lit.cfg.py | 13 +++++++++++++
clang/tools/clang-format/clang-format-diff.py | 3 ++-
4 files changed, 27 insertions(+), 1 deletion(-)
create mode 100644 clang/test/Format/Inputs/clang-format-diff-timestamp.diff
create mode 100644 clang/test/Format/clang-format-diff.cpp
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:
More information about the cfe-commits
mailing list