[llvm] [lit] Add support to print relative paths when outputting test names (PR #154317)

Björn Pettersson via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 19 05:10:28 PDT 2025


https://github.com/bjope created https://github.com/llvm/llvm-project/pull/154317

This patch adds a -r|--relative-paths option to llvm-lit, which when enabled will print test case names using paths relative to the current working directory. The legacy default without that option is that test cases are identified using a path relative to the test suite.

Examples:
  > llvm-lit test/Transforms/ADCE/willreturn.ll
  PASS: LLVM :: Transforms/ADCE/willreturn.ll

  > llvm-lit -r test/Transforms/ADCE/willreturn.ll
  PASS: LLVM :: test/Transforms/ADCE/willreturn.ll

The option is currently only impacting how tests are identified in console output. Not other kinds of reports.

Background to this patch was the discussion here:
  https://discourse.llvm.org/t/test-case-paths-in-llvm-lit-output-are-lacking-the-location-of-the-test-dir-itself/87973

>From 153193b0bd9dcdcd1bc1e6ab65a2e3e066afd5c2 Mon Sep 17 00:00:00 2001
From: Bjorn Pettersson <bjorn.a.pettersson at ericsson.com>
Date: Tue, 19 Aug 2025 13:38:06 +0200
Subject: [PATCH] [lit] Add support to print relative paths when outputting
 test names

This patch adds a -r|--relative-paths option to llvm-lit, which when
enabled will print test case names using paths relative to the
current working directory. The legacy default without that option
is that test cases are identified using a path relative to the
test suite.

Examples:
  > llvm-lit test/Transforms/ADCE/willreturn.ll
  PASS: LLVM :: Transforms/ADCE/willreturn.ll

  > llvm-lit -r test/Transforms/ADCE/willreturn.ll
  PASS: LLVM :: test/Transforms/ADCE/willreturn.ll

The option is currently only impacting how tests are identified in
console output. Not other kinds of reports.

Background to this patch was the discussion here:
  https://discourse.llvm.org/t/test-case-paths-in-llvm-lit-output-are-lacking-the-location-of-the-test-dir-itself/87973
---
 llvm/utils/lit/lit/Test.py                       |  6 ++++++
 llvm/utils/lit/lit/cl_arguments.py               |  7 +++++++
 llvm/utils/lit/lit/display.py                    |  6 +++---
 llvm/utils/lit/lit/main.py                       |  5 +++--
 .../lit/tests/Inputs/print-relative-path/lit.cfg |  7 +++++++
 .../tests/Inputs/print-relative-path/test.txt    |  1 +
 .../tests/Inputs/print-relative-path/test2.txt   |  2 ++
 llvm/utils/lit/tests/print-relative-path.py      | 16 ++++++++++++++++
 8 files changed, 45 insertions(+), 5 deletions(-)
 create mode 100644 llvm/utils/lit/tests/Inputs/print-relative-path/lit.cfg
 create mode 100644 llvm/utils/lit/tests/Inputs/print-relative-path/test.txt
 create mode 100644 llvm/utils/lit/tests/Inputs/print-relative-path/test2.txt
 create mode 100644 llvm/utils/lit/tests/print-relative-path.py

diff --git a/llvm/utils/lit/lit/Test.py b/llvm/utils/lit/lit/Test.py
index 7290977724170..da5de3bfe836c 100644
--- a/llvm/utils/lit/lit/Test.py
+++ b/llvm/utils/lit/lit/Test.py
@@ -307,6 +307,12 @@ def isFailure(self):
     def getFullName(self):
         return self.suite.config.name + " :: " + "/".join(self.path_in_suite)
 
+    def getFullNameRelPath(self, printRelativePaths):
+        if printRelativePaths:
+            return (self.suite.config.name + " :: " +
+                    os.path.relpath(self.getFilePath()))
+        return self.getFullName()
+
     def getFilePath(self):
         if self.file_path:
             return self.file_path
diff --git a/llvm/utils/lit/lit/cl_arguments.py b/llvm/utils/lit/lit/cl_arguments.py
index 8f9211ee3f538..29c6cf3e9e1c9 100644
--- a/llvm/utils/lit/lit/cl_arguments.py
+++ b/llvm/utils/lit/lit/cl_arguments.py
@@ -91,6 +91,13 @@ def parse_args():
         help="Enable -v, but for all tests not just failed tests.",
         action="store_true",
     )
+    format_group.add_argument(
+        "-r",
+        "--relative-paths",
+        dest="printRelativePaths",
+        help="Print paths relative to CWD",
+        action="store_true",
+    )
     format_group.add_argument(
         "-o",
         "--output",
diff --git a/llvm/utils/lit/lit/display.py b/llvm/utils/lit/lit/display.py
index 3a224f734bf71..7303ceabdfc60 100644
--- a/llvm/utils/lit/lit/display.py
+++ b/llvm/utils/lit/lit/display.py
@@ -116,7 +116,7 @@ def clear(self, interrupted):
 
     def print_result(self, test):
         # Show the test result line.
-        test_name = test.getFullName()
+        test_name = test.getFullNameRelPath(self.opts.printRelativePaths)
 
         extra_info = ""
         if test.result.attempts > 1:
@@ -137,7 +137,7 @@ def print_result(self, test):
         if (test.isFailure() and self.opts.showOutput) or self.opts.showAllOutput:
             if test.isFailure():
                 print(
-                    "%s TEST '%s' FAILED %s" % ("*" * 20, test.getFullName(), "*" * 20)
+                    "%s TEST '%s' FAILED %s" % ("*" * 20, test_name, "*" * 20)
                 )
             out = test.result.output
             # Encode/decode so that, when using Python 3.6.5 in Windows 10,
@@ -159,7 +159,7 @@ def print_result(self, test):
 
         # Report test metrics, if present.
         if test.result.metrics:
-            print("%s TEST '%s' RESULTS %s" % ("*" * 10, test.getFullName(), "*" * 10))
+            print("%s TEST '%s' RESULTS %s" % ("*" * 10, test_name, "*" * 10))
             items = sorted(test.result.metrics.items())
             for metric_name, value in items:
                 print("%s: %s " % (metric_name, value.format()))
diff --git a/llvm/utils/lit/lit/main.py b/llvm/utils/lit/lit/main.py
index 5255e2c5e1b51..7f52e194089b1 100755
--- a/llvm/utils/lit/lit/main.py
+++ b/llvm/utils/lit/lit/main.py
@@ -329,12 +329,13 @@ def print_results(tests, elapsed, opts):
             sorted(tests_by_code[code], key=lambda t: t.getFullName()),
             code,
             opts.shown_codes,
+            opts.printRelativePaths
         )
 
     print_summary(total_tests, tests_by_code, opts.quiet, elapsed)
 
 
-def print_group(tests, code, shown_codes):
+def print_group(tests, code, shown_codes, printRelativePaths):
     if not tests:
         return
     if not code.isFailure and code not in shown_codes:
@@ -342,7 +343,7 @@ def print_group(tests, code, shown_codes):
     print("*" * 20)
     print("{} Tests ({}):".format(code.label, len(tests)))
     for test in tests:
-        print("  %s" % test.getFullName())
+        print("  %s" % test.getFullNameRelPath(printRelativePaths))
     sys.stdout.write("\n")
 
 
diff --git a/llvm/utils/lit/tests/Inputs/print-relative-path/lit.cfg b/llvm/utils/lit/tests/Inputs/print-relative-path/lit.cfg
new file mode 100644
index 0000000000000..2cf5104e124dc
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/print-relative-path/lit.cfg
@@ -0,0 +1,7 @@
+import lit.formats
+
+config.name = "print-relative-path"
+config.suffixes = [".txt"]
+config.test_format = lit.formats.ShTest()
+config.test_source_root = None
+config.test_exec_root = None
diff --git a/llvm/utils/lit/tests/Inputs/print-relative-path/test.txt b/llvm/utils/lit/tests/Inputs/print-relative-path/test.txt
new file mode 100644
index 0000000000000..0ddee88ac0bf5
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/print-relative-path/test.txt
@@ -0,0 +1 @@
+# RUN: echo %var
diff --git a/llvm/utils/lit/tests/Inputs/print-relative-path/test2.txt b/llvm/utils/lit/tests/Inputs/print-relative-path/test2.txt
new file mode 100644
index 0000000000000..3c0fa612b5fb1
--- /dev/null
+++ b/llvm/utils/lit/tests/Inputs/print-relative-path/test2.txt
@@ -0,0 +1,2 @@
+# RUN: false
+
diff --git a/llvm/utils/lit/tests/print-relative-path.py b/llvm/utils/lit/tests/print-relative-path.py
new file mode 100644
index 0000000000000..27f2b2b34a2d8
--- /dev/null
+++ b/llvm/utils/lit/tests/print-relative-path.py
@@ -0,0 +1,16 @@
+# RUN: not %{lit} %{inputs}/print-relative-path | FileCheck --check-prefix=CHECK-DEFAULT %s
+# RUN: not %{lit} -r %{inputs}/print-relative-path | FileCheck --check-prefix=CHECK-RELATIVE %s
+# RUN: not %{lit} --relative-paths %{inputs}/print-relative-path | FileCheck --check-prefix=CHECK-RELATIVE %s
+
+
+# CHECK-DEFAULT: PASS: print-relative-path :: test.txt (1 of 2)
+# CHECK-DEFAULT-NEXT: FAIL: print-relative-path :: test2.txt (2 of 2)
+# CHECK-DEFAULT-NEXT: ********************
+# CHECK-DEFAULT-NEXT: Failed Tests (1):
+# CHECK-DEFAULT-NEXT:  print-relative-path :: test2.txt
+
+# CHECK-RELATIVE: PASS: print-relative-path :: Inputs/print-relative-path/test.txt (1 of 2)
+# CHECK-RELATIVE-NEXT: FAIL: print-relative-path :: Inputs/print-relative-path/test2.txt (2 of 2)
+# CHECK-RELATIVE-NEXT: ********************
+# CHECK-RELATIVE-NEXT: Failed Tests (1):
+# CHECK-RELATIVE-NEXT:  print-relative-path :: Inputs/print-relative-path/test2.txt



More information about the llvm-commits mailing list