Skip to content

Commit d054c7e

Browse files
committed
Add test utility 'extract'
See https://lists.llvm.org/pipermail/llvm-dev/2020-July/143373.html "[llvm-dev] Multiple documents in one test file" for some discussions. `extract part filename` splits the input file into multiple parts separated by regex `^(.|//)--- ` and extract the specified part to stdout or the output file (if specified). Use case A (organizing input of different formats (e.g. linker script+assembly) in one file). ``` // RUN: extract lds %s -o %t.lds // RUN: extract asm %s -o %t.s // RUN: llvm-mc %t.s -o %t.o // RUN: ld.lld -T %t.lds %t.o -o %t This is sometimes better than the %S/Inputs/ approach because the user can see the auxiliary files immediately and don't have to open another file. ``` Use case B (for utilities which don't have built-in input splitting feature): ``` // RUN: extract case1 %s | llc | FileCheck %s --check-prefix=CASE1 // RUN: extract case2 %s | llc | FileCheck %s --check-prefix=CASE2 Combing tests prudently can improve readability. This is sometimes better than having multiple test files. ``` Since this is a new utility, there is no git history concerns for UpperCase variable names. I use lowerCase variable names like mlir/lld. Reviewed By: jhenderson Differential Revision: https://reviews.llvm.org/D83834
1 parent 8131e19 commit d054c7e

File tree

17 files changed

+276
-36
lines changed

17 files changed

+276
-36
lines changed

lld/test/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ configure_lit_site_cfg(
3434
set(LLD_TEST_DEPS lld)
3535
if (NOT LLD_BUILT_STANDALONE)
3636
list(APPEND LLD_TEST_DEPS
37-
FileCheck count llc llvm-ar llvm-as llvm-bcanalyzer llvm-config llvm-cvtres
37+
FileCheck count extract llc llvm-ar llvm-as llvm-bcanalyzer llvm-config llvm-cvtres
3838
llvm-dis llvm-dwarfdump llvm-lib llvm-lipo llvm-mc llvm-nm llvm-objcopy
3939
llvm-objdump llvm-pdbutil llvm-readelf llvm-readobj llvm-strip not obj2yaml
4040
opt yaml2obj

lld/test/ELF/linkerscript/noload.s

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
11
# REQUIRES: x86
2-
# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o
3-
# RUN: echo "SECTIONS { \
4-
# RUN: .data_noload_a (NOLOAD) : { *(.data_noload_a) } \
5-
# RUN: .data_noload_b (0x10000) (NOLOAD) : { *(.data_noload_b) } \
6-
# RUN: .no_input_sec_noload (NOLOAD) : { . += 1; } \
7-
# RUN: .text (0x20000) : { *(.text) } };" > %t.script
8-
# RUN: ld.lld -o %t --script %t.script %t.o
2+
# RUN: extract asm %s -o %t.s && extract lds %s -o %t.lds
3+
# RUN: llvm-mc -filetype=obj -triple=x86_64 %t.s -o %t.o
4+
# RUN: ld.lld -o %t --script %t.lds %t.o
95
# RUN: llvm-readelf -S -l %t | FileCheck %s
106

117
# CHECK: Name Type Address Off Size
@@ -16,6 +12,7 @@
1612
# CHECK: Type Offset VirtAddr PhysAddr
1713
# CHECK-NEXT: LOAD 0x001000 0x0000000000020000 0x0000000000020000
1814

15+
#--- asm
1916
.section .text,"ax",@progbits
2017
nop
2118

@@ -24,3 +21,11 @@
2421

2522
.section .data_noload_b,"aw",@progbits
2623
.zero 4096
24+
25+
#--- lds
26+
SECTIONS {
27+
.data_noload_a (NOLOAD) : { *(.data_noload_a) }
28+
.data_noload_b (0x10000) (NOLOAD) : { *(.data_noload_b) }
29+
.no_input_sec_noload (NOLOAD) : { . += 1; }
30+
.text (0x20000) : { *(.text) }
31+
}

lld/test/lit.cfg.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
llvm_config.use_lld()
4040

4141
tool_patterns = [
42-
'llc', 'llvm-as', 'llvm-mc', 'llvm-nm', 'llvm-objdump', 'llvm-pdbutil',
43-
'llvm-dwarfdump', 'llvm-readelf', 'llvm-readobj', 'obj2yaml', 'yaml2obj',
44-
'opt', 'llvm-dis']
42+
'extract', 'llc', 'llvm-as', 'llvm-mc', 'llvm-nm', 'llvm-objdump',
43+
'llvm-pdbutil', 'llvm-dwarfdump', 'llvm-readelf', 'llvm-readobj',
44+
'obj2yaml', 'yaml2obj', 'opt', 'llvm-dis']
4545

4646
llvm_config.add_tool_substitutions(tool_patterns)
4747

@@ -87,7 +87,7 @@
8787
# Indirectly check if the mt.exe Microsoft utility exists by searching for
8888
# cvtres, which always accompanies it. Alternatively, check if we can use
8989
# libxml2 to merge manifests.
90-
if (lit.util.which('cvtres', config.environment['PATH']) or
90+
if (lit.util.which('cvtres', config.environment['PATH']) or
9191
config.llvm_libxml2_enabled):
9292
config.available_features.add('manifest_tool')
9393

llvm/docs/TestingGuide.rst

+21-2
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,27 @@ adding your code there instead of creating a new file.
271271
Extra files
272272
-----------
273273

274-
If your test requires extra files besides the file containing the ``RUN:``
275-
lines, the idiomatic place to put them is in a subdirectory ``Inputs``.
274+
If your test requires extra files besides the file containing the ``RUN:`` lines
275+
and the extra files are small, consider specifying them in the same file and
276+
using ``extract`` to extract them. For example,
277+
278+
.. code-block:: llvm
279+
280+
; RUN: extract b %s -o %tb.ll
281+
; RUN: extract a %s | llvm-link - %tb.ll -S | FileCheck %s
282+
283+
; CHECK: ...
284+
285+
;--- a
286+
...
287+
;--- b
288+
...
289+
290+
The parts are separated by the regex ``^(.|//)--- <part>``. By default the
291+
extracted content has leading empty lines to preserve line numbers. Specify
292+
``--no-leading-lines`` to drop leading lines.
293+
294+
If the extra files are large, the idiomatic place to put them is in a subdirectory ``Inputs``.
276295
You can then refer to the extra files as ``%S/Inputs/foo.bar``.
277296

278297
For example, consider ``test/Linker/ident.ll``. The directory structure is

llvm/test/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ set(LLVM_TEST_DEPENDS
5252
UnitTests
5353
bugpoint
5454
count
55+
extract
5556
llc
5657
lli
5758
lli-child-target

llvm/test/lit.cfg.py

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def get_asan_rtlib():
130130
config.llvm_locstats_used = os.path.exists(llvm_locstats_tool)
131131

132132
tools = [
133+
ToolSubst('%extract', FindTool('extract')),
133134
ToolSubst('%lli', FindTool('lli'), post='.', extra_args=lli_args),
134135
ToolSubst('%llc_dwarf', FindTool('llc'), extra_args=llc_args),
135136
ToolSubst('%go', config.go_executable, unresolved='ignore'),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
2+
3+
4+
aa
5+
; BB-NOT: {{.}}
6+
; BB: {{^}}bb{{$}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
3+
4+
5+
6+
7+
8+
bb
9+
10+
// CC: // Comments are preserved.

llvm/test/tools/extract/basic.test

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# AA-NOT: {{.}}
2+
# AA: {{^}}aa{{$}}
3+
#--- aa
4+
aa
5+
; BB-NOT: {{.}}
6+
; BB: {{^}}bb{{$}}
7+
;--- bb
8+
bb
9+
10+
// CC: // Comments are preserved.
11+
//--- cc
12+
cc
13+
// Comments are preserved.
14+
;--- dup
15+
;--- dup
16+
17+
# RUN: extract aa %s | diff %S/Inputs/basic-aa.txt -
18+
# RUN: extract bb - < %s | diff %S/Inputs/basic-bb.txt -
19+
# RUN: extract cc %s -o %t
20+
# RUN: FileCheck %s --check-prefix=CC < %t
21+
22+
# RUN: not %extract aa 2>&1 | FileCheck %s --check-prefix=NO_INPUT
23+
24+
# NO_INPUT: extract: error: input filename is not specified
25+
26+
# RUN: not %extract dup %s 2>&1 | FileCheck %s --check-prefix=DUP
27+
28+
# DUP: extract: error: {{.*}}.test: ';--- dup' occurs more than once
29+
30+
# RUN: not %extract not_exist %s 2>&1 | FileCheck %s --check-prefix=NOT_EXIST
31+
32+
# NOT_EXIST: extract: error: {{.*}}.test: ';--- not_exist' was not found

llvm/test/tools/extract/help.test

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
RUN: extract --help 2>&1 | FileCheck --implicit-check-not='General Options:' %s
2+
CHECK: OVERVIEW: Split input {{.*}}
3+
CHECK: Generic Options:
4+
CHECK: extract Options:
5+
CHECK: -o
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
## With --no-leading-lines, don't add leading lines (which is used to preserve line numbers).
2+
3+
# RUN: extract --no-leading-lines input %s -o %t
4+
# RUN: count 1 < %t
5+
# RUN: FileCheck %s < %t
6+
7+
# CHECK: input
8+
9+
#--- input
10+
input

llvm/test/tools/gold/X86/multiple-sections.ll

+9-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
; RUN: echo ".text.tin" > %t_order_lto.txt
2-
; RUN: echo ".text._start" >> %t_order_lto.txt
3-
; RUN: echo ".text.pat" >> %t_order_lto.txt
4-
; RUN: llvm-as %s -o %t.o
1+
; RUN: extract order %s -o %t.order
2+
; RUN: extract ir %s | llvm-as -o %t.o
53
; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext \
64
; RUN: -m elf_x86_64 -o %t.exe %t.o \
7-
; RUN: --section-ordering-file=%t_order_lto.txt
5+
; RUN: --section-ordering-file=%t.order
86
; RUN: llvm-readelf -s %t.exe | FileCheck %s
97

108
; Check that the order of the sections is tin -> _start -> pat.
@@ -13,6 +11,12 @@
1311
; CHECK: 00000000004000b0 1 FUNC LOCAL DEFAULT 1 tin
1412
; CHECK: 00000000004000c0 15 FUNC GLOBAL DEFAULT 1 _start
1513

14+
;--- order
15+
.text.tin
16+
.text._start
17+
.text.pat
18+
19+
;--- ir
1620
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
1721
target triple = "x86_64-unknown-linux-gnu"
1822

llvm/test/tools/llvm-objcopy/ELF/strip-symbol.test

+12-7
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
1-
# RUN: yaml2obj %s -o %t
1+
# RUN: extract yaml %s | yaml2obj - -o %t
22
# RUN: llvm-objcopy --strip-symbol baz -N bar %t %t2
33
# RUN: llvm-readobj --symbols --sections %t2 | FileCheck %s
44
# RUN: llvm-strip --strip-symbol baz -N bar %t -o %t3
55
# RUN: cmp %t2 %t3
66
# RUN: llvm-strip --regex --strip-symbol '^b.*' -N bar %t -o %t4
77
# RUN: cmp %t3 %t4
8-
# RUN: echo " bar # bar" > %t-list.txt
9-
# RUN: echo " baz # baz" >> %t-list.txt
10-
# RUN: echo " # no symbol" >> %t-list.txt
11-
# RUN: llvm-objcopy --strip-symbols %t-list.txt %t %t5
8+
# RUN: extract list1 %s -o %t-list.txt && llvm-objcopy --strip-symbols %t-list.txt %t %t5
129
# RUN: cmp %t3 %t5
13-
# RUN: echo "b.* # bar & baz" > %t-list2.txt
14-
# RUN: llvm-objcopy --regex --strip-symbols %t-list2.txt %t %t6
10+
# RUN: extract list2 %s -o %t-list2.txt && llvm-objcopy --regex --strip-symbols %t-list2.txt %t %t6
1511
# RUN: cmp %t3 %t6
1612

13+
#--- list1
14+
bar # bar
15+
baz # baz
16+
# no symbol
17+
18+
#--- list2
19+
b.* # bar & baz
20+
21+
#--- yaml
1722
!ELF
1823
FileHeader:
1924
Class: ELFCLASS64

llvm/test/tools/llvm-strings/radix.test

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
## Show that llvm-strings can handle the -t/--radix switch properly.
22

3-
RUN: echo one > %t
4-
RUN: echo two >> %t
5-
RUN: echo three >> %t
6-
RUN: echo four >> %t
7-
RUN: echo five >> %t
8-
RUN: echo six >> %t
9-
RUN: echo seven >> %t
10-
RUN: echo eight >> %t
11-
RUN: echo nine >> %t
12-
RUN: echo ten >> %t
3+
RUN: extract --no-leading-lines input %s -o %t
4+
#--- input
5+
one
6+
two
7+
three
8+
four
9+
five
10+
six
11+
seven
12+
eight
13+
nine
14+
ten
15+
#--- end
1316

1417
RUN: llvm-strings %t | FileCheck %s -check-prefix CHECK-NONE --implicit-check-not={{.}}
1518
RUN: llvm-strings -t d %t | FileCheck %s -check-prefix CHECK-DEC --strict-whitespace --implicit-check-not={{.}}

llvm/tools/extract/.clang-tidy

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# Almost identical to the top-level .clang-tidy, except that {Member,Parameter,Variable}Case use camelBack.
2+
Checks: '-*,clang-diagnostic-*,llvm-*,misc-*,-misc-unused-parameters,-misc-non-private-member-variables-in-classes,readability-identifier-naming'
3+
CheckOptions:
4+
- key: readability-identifier-naming.ClassCase
5+
value: CamelCase
6+
- key: readability-identifier-naming.EnumCase
7+
value: CamelCase
8+
- key: readability-identifier-naming.FunctionCase
9+
value: camelBack
10+
- key: readability-identifier-naming.MemberCase
11+
value: camelBack
12+
- key: readability-identifier-naming.ParameterCase
13+
value: camelBack
14+
- key: readability-identifier-naming.UnionCase
15+
value: CamelCase
16+
- key: readability-identifier-naming.VariableCase
17+
value: camelBack
18+
- key: readability-identifier-naming.IgnoreMainLikeFunctions
19+
value: 1

llvm/tools/extract/CMakeLists.txt

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
set(LLVM_LINK_COMPONENTS
2+
Support
3+
)
4+
5+
add_llvm_tool(extract
6+
extract.cpp
7+
)

0 commit comments

Comments
 (0)