Skip to content

Commit c745c54

Browse files
[LLVM] Prefer octal to hex for printf (#157884)
Hex escapes of the form \xff are not universally supported in the printf implementations on the platforms that LLVM runs on (although they apparently are in the shell builtins). Octal escapes are required to be supported by POSIX. This patch converts all hex escapes to octal escapes for compatibility reasons. This came up when trying to turn on lit's internal shell by default for llvm/. We started using /usr/bin/printf instead of the shell builtin on MacOS, which does not support hex escapes. I used the following python script to automate most of the conversion with a few manual touchups needed: ```py import sys def process_line(to_process: str): output = "" i = 0 while i < len(to_process): if to_process[i:i+2] == '\\x': hex_string = to_process[i+2:i+4] number = int(hex_string, 16) output += "\\" octal_string = oct(number)[2:] if len(octal_string) == 1: octal_string = "00" + octal_string elif len(octal_string) == 2: octal_string = "0" + octal_string assert(len(octal_string) == 3) output += octal_string i += 4 else: output += to_process[i] i += 1 return output with open(sys.argv[1]) as input_file: lines = input_file.readlines() for i, _ in enumerate(lines): lines[i] = process_line(lines[i]) with open(sys.argv[1], 'w') as output_file: output_file.writelines(lines) ```
1 parent 55e99ef commit c745c54

File tree

14 files changed

+67
-67
lines changed

14 files changed

+67
-67
lines changed

llvm/test/tools/llvm-cgdata/empty.test

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ RUN: llvm-cgdata --convert %t_emptyheader.cgdata --format text | count 0
2929
# uint64_t OutlinedHashTreeOffset;
3030
# uint64_t StableFunctionMapOffset;
3131
# }
32-
RUN: printf '\xffcgdata\x81' > %t_header.cgdata
33-
RUN: printf '\x04\x00\x00\x00' >> %t_header.cgdata
34-
RUN: printf '\x00\x00\x00\x00' >> %t_header.cgdata
35-
RUN: printf '\x20\x00\x00\x00\x00\x00\x00\x00' >> %t_header.cgdata
36-
RUN: printf '\x20\x00\x00\x00\x00\x00\x00\x00' >> %t_header.cgdata
32+
RUN: printf '\377cgdata\201' > %t_header.cgdata
33+
RUN: printf '\004\000\000\000' >> %t_header.cgdata
34+
RUN: printf '\000\000\000\000' >> %t_header.cgdata
35+
RUN: printf '\040\000\000\000\000\000\000\000' >> %t_header.cgdata
36+
RUN: printf '\040\000\000\000\000\000\000\000' >> %t_header.cgdata
3737
RUN: diff %t_header.cgdata %t_emptyheader.cgdata

llvm/test/tools/llvm-cgdata/error.test

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,29 @@ RUN: not llvm-cgdata --show %t_empty.cgdata 2>&1 | FileCheck %s --check-prefix=E
1313
EMPTY: {{.}}cgdata: empty codegen data
1414

1515
# Not a magic.
16-
RUN: printf '\xff' > %t_malformed.cgdata
16+
RUN: printf '\377' > %t_malformed.cgdata
1717
RUN: not llvm-cgdata --show %t_malformed.cgdata 2>&1 | FileCheck %s --check-prefix=MALFORMED
1818
MALFORMED: {{.}}cgdata: malformed codegen data
1919

2020
# The minimum header size is 24.
21-
RUN: printf '\xffcgdata\x81' > %t_corrupt.cgdata
21+
RUN: printf '\377cgdata\201' > %t_corrupt.cgdata
2222
RUN: not llvm-cgdata --show %t_corrupt.cgdata 2>&1 | FileCheck %s --check-prefix=CORRUPT
2323
CORRUPT: {{.}}cgdata: invalid codegen data (file header is corrupt)
2424

2525
# The current version 4 while the header says 5.
26-
RUN: printf '\xffcgdata\x81' > %t_version.cgdata
27-
RUN: printf '\x05\x00\x00\x00' >> %t_version.cgdata
28-
RUN: printf '\x00\x00\x00\x00' >> %t_version.cgdata
29-
RUN: printf '\x20\x00\x00\x00\x00\x00\x00\x00' >> %t_version.cgdata
30-
RUN: printf '\x20\x00\x00\x00\x00\x00\x00\x00' >> %t_version.cgdata
26+
RUN: printf '\377cgdata\201' > %t_version.cgdata
27+
RUN: printf '\005\000\000\000' >> %t_version.cgdata
28+
RUN: printf '\000\000\000\000' >> %t_version.cgdata
29+
RUN: printf '\040\000\000\000\000\000\000\000' >> %t_version.cgdata
30+
RUN: printf '\040\000\000\000\000\000\000\000' >> %t_version.cgdata
3131
RUN: not llvm-cgdata --show %t_version.cgdata 2>&1 | FileCheck %s --check-prefix=BAD_VERSION
3232
BAD_VERSION: {{.}}cgdata: unsupported codegen data version
3333

3434
# Header says an outlined hash tree, but the file ends after the header.
35-
RUN: printf '\xffcgdata\x81' > %t_eof.cgdata
36-
RUN: printf '\x02\x00\x00\x00' >> %t_eof.cgdata
37-
RUN: printf '\x01\x00\x00\x00' >> %t_eof.cgdata
38-
RUN: printf '\x20\x00\x00\x00\x00\x00\x00\x00' >> %t_eof.cgdata
39-
RUN: printf '\x20\x00\x00\x00\x00\x00\x00\x00' >> %t_eof.cgdata
35+
RUN: printf '\377cgdata\201' > %t_eof.cgdata
36+
RUN: printf '\002\000\000\000' >> %t_eof.cgdata
37+
RUN: printf '\001\000\000\000' >> %t_eof.cgdata
38+
RUN: printf '\040\000\000\000\000\000\000\000' >> %t_eof.cgdata
39+
RUN: printf '\040\000\000\000\000\000\000\000' >> %t_eof.cgdata
4040
RUN: not llvm-cgdata --show %t_eof.cgdata 2>&1 | FileCheck %s --check-prefix=EOF
4141
EOF: {{.}}cgdata: end of File

llvm/test/tools/llvm-gsymutil/X86/elf-dwarf.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
# RUN: yaml2obj %s -o %t
66
# RUN: llvm-gsymutil --convert %t -o %t.gsym 2>&1 | FileCheck %s --check-prefix=CONVERT
77
# RUN: llvm-gsymutil --address=0x400391 --address=0x4004cd %t.gsym 2>&1 | FileCheck %s --check-prefix=ADDR
8-
# RUN: echo -e "0x400391 %/t.gsym\n0x4004cd %/t.gsym" | llvm-gsymutil --addresses-from-stdin 2>&1 | FileCheck %s --check-prefix=ADDRI --dump-input=always
8+
# RUN: printf "0x400391 %/t.gsym\n0x4004cd %/t.gsym" | llvm-gsymutil --addresses-from-stdin 2>&1 | FileCheck %s --check-prefix=ADDRI --dump-input=always
99
# RUN: llvm-gsymutil --address=0x400391 --address=0x4004cd --verbose %t.gsym 2>&1 | FileCheck %s --check-prefix=ADDRV --dump-input=always
1010
# RUN: llvm-gsymutil --address=0x400391 --address=0x4004cd --verbose %t.gsym 2>&1 | FileCheck %s --check-prefix=ADDRV --dump-input=always
1111
# RUN: llvm-gsymutil %t.gsym 2>&1 | FileCheck %s --check-prefix=DUMP

llvm/test/tools/llvm-objcopy/ELF/add-invalid-note.test

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
## Add [namesz, descsz, type, name, desc] for a build id.
44

55
## Notes should be padded to 8 bytes.
6-
# RUN: printf "\x04\x00\x00\x00" > %t-miss-padding-note.bin
7-
# RUN: printf "\x07\x00\x00\x00" >> %t-miss-padding-note.bin
8-
# RUN: printf "\x03\x00\x00\x00" >> %t-miss-padding-note.bin
9-
# RUN: printf "GNU\x00" >> %t-miss-padding-note.bin
10-
# RUN: printf "\x0c\x0d\x0e" >> %t-miss-padding-note.bin
6+
# RUN: printf "\004\000\000\000" > %t-miss-padding-note.bin
7+
# RUN: printf "\007\000\000\000" >> %t-miss-padding-note.bin
8+
# RUN: printf "\003\000\000\000" >> %t-miss-padding-note.bin
9+
# RUN: printf "GNU\000" >> %t-miss-padding-note.bin
10+
# RUN: printf "\014\015\016" >> %t-miss-padding-note.bin
1111

1212
## The namesz field bit is incorrect.
13-
# RUN: printf "\x08\x00\x00\x00" > %t-invalid-size-note.bin
14-
# RUN: printf "\x07\x00\x00\x00" >> %t-invalid-size-note.bin
15-
# RUN: printf "\x03\x00\x00\x00" >> %t-invalid-size-note.bin
16-
# RUN: printf "GNU\x00" >> %t-invalid-size-note.bin
17-
# RUN: printf "\x0c\x0d\x0e\x00" >> %t-invalid-size-note.bin
13+
# RUN: printf "\010\000\000\000" > %t-invalid-size-note.bin
14+
# RUN: printf "\007\000\000\000" >> %t-invalid-size-note.bin
15+
# RUN: printf "\003\000\000\000" >> %t-invalid-size-note.bin
16+
# RUN: printf "GNU\000" >> %t-invalid-size-note.bin
17+
# RUN: printf "\014\015\016\000" >> %t-invalid-size-note.bin
1818

1919
## Missing type field.
20-
# RUN: printf "\x08\x00\x00\x00" > %t-short-note.bin
21-
# RUN: printf "\x07\x00\x00\x00" >> %t-short-note.bin
20+
# RUN: printf "\010\000\000\000" > %t-short-note.bin
21+
# RUN: printf "\007\000\000\000" >> %t-short-note.bin
2222

2323
# RUN: yaml2obj %s -o %t.o
2424

@@ -33,11 +33,11 @@
3333
# CHECK-SHORT: .note.short data must be either empty or at least 12 bytes long
3434

3535
## Test compatibility with .note.gnu.property, which has 8-byte alignment.
36-
# RUN: printf "\x04\x00\x00\x00\x40\x00\x00\x00\x05\x00\x00\x00\x47\x4e\x55\x00" > %t-note-gnu-property.bin
37-
# RUN: printf "\x02\x00\x00\xc0\x04\x00\x00\x00\x03\x00\x00\x00\x00\x00\x00\x00" >> %t-note-gnu-property.bin
38-
# RUN: printf "\x01\x80\x01\xc0\x04\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00" >> %t-note-gnu-property.bin
39-
# RUN: printf "\x01\x00\x01\xc0\x04\x00\x00\x00\x0b\x00\x00\x00\x00\x00\x00\x00" >> %t-note-gnu-property.bin
40-
# RUN: printf "\x02\x00\x01\xc0\x04\x00\x00\x00\x01\x00\x00\x00\x00\x00\x00\x00" >> %t-note-gnu-property.bin
36+
# RUN: printf "\004\000\000\000\100\000\000\000\005\000\000\000\107\116\125\000" > %t-note-gnu-property.bin
37+
# RUN: printf "\002\000\000\300\004\000\000\000\003\000\000\000\000\000\000\000" >> %t-note-gnu-property.bin
38+
# RUN: printf "\001\200\001\300\004\000\000\000\001\000\000\000\000\000\000\000" >> %t-note-gnu-property.bin
39+
# RUN: printf "\001\000\001\300\004\000\000\000\013\000\000\000\000\000\000\000" >> %t-note-gnu-property.bin
40+
# RUN: printf "\002\000\001\300\004\000\000\000\001\000\000\000\000\000\000\000" >> %t-note-gnu-property.bin
4141
# RUN: llvm-objcopy --add-section=.note.gnu.property=%t-note-gnu-property.bin %t.o %t-with-note-gnu-property.o
4242

4343
## Test that verification can be disabled.

llvm/test/tools/llvm-profdata/binary-ids-padding.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ RUN: printf '\0\0\0\0\0\0\0\0' >> %t.profraw
6969

7070
RUN: printf '\067\265\035\031\112\165\023\344' >> %t.profraw
7171
RUN: printf '\02\0\0\0\0\0\0\0' >> %t.profraw
72-
RUN: printf '\xc8\xff\3\0\1\0\0\0' >> %t.profraw
72+
RUN: printf '\310\377\3\0\1\0\0\0' >> %t.profraw
7373
RUN: printf '\0\0\0\0\0\0\0\0' >> %t.profraw
7474
RUN: printf '\0\0\0\0\0\0\0\0' >> %t.profraw
7575
RUN: printf '\0\0\0\0\0\0\0\0' >> %t.profraw

llvm/test/tools/llvm-profdata/raw-32-bits-be.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ RUN: printf '\0\0\0\3' >> %t
3131

3232
RUN: printf '\344\023\165\112\031\035\265\067' >> %t
3333
RUN: printf '\0\0\0\0\0\0\0\2' >> %t
34-
RUN: printf '\0\xff\xff\xd8' >> %t
35-
RUN: printf '\2\xff\xff\xd3' >> %t
34+
RUN: printf '\0\377\377\330' >> %t
35+
RUN: printf '\2\377\377\323' >> %t
3636
RUN: printf '\0\0\0\0' >> %t
3737
RUN: printf '\0\0\0\0' >> %t
3838
RUN: printf '\0\0\0\2' >> %t

llvm/test/tools/llvm-profdata/raw-32-bits-le.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ RUN: printf '\3\0\0\0' >> %t
3030

3131
RUN: printf '\067\265\035\031\112\165\023\344' >> %t
3232
RUN: printf '\02\0\0\0\0\0\0\0' >> %t
33-
RUN: printf '\xd8\xff\xff\0' >> %t
34-
RUN: printf '\xd3\xff\xff\2' >> %t
33+
RUN: printf '\330\377\377\0' >> %t
34+
RUN: printf '\323\377\377\2' >> %t
3535
RUN: printf '\0\0\0\0' >> %t
3636
RUN: printf '\0\0\0\0' >> %t
3737
RUN: printf '\2\0\0\0' >> %t

llvm/test/tools/llvm-profdata/raw-64-bits-be.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ RUN: printf '\0\0\0\3' >> %t
3030

3131
RUN: printf '\344\023\165\112\031\035\265\067' >> %t
3232
RUN: printf '\0\0\0\0\0\0\0\02' >> %t
33-
RUN: printf '\0\0\0\1\0\3\xff\xc8' >> %t
34-
RUN: printf '\0\0\0\3\0\3\xff\xc3' >> %t
33+
RUN: printf '\0\0\0\1\0\3\377\310' >> %t
34+
RUN: printf '\0\0\0\3\0\3\377\303' >> %t
3535
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
3636
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
3737
RUN: printf '\0\0\0\02' >> %t

llvm/test/tools/llvm-profdata/raw-64-bits-le.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ RUN: printf '\3\0\0\0' >> %t
3030

3131
RUN: printf '\067\265\035\031\112\165\023\344' >> %t
3232
RUN: printf '\02\0\0\0\0\0\0\0' >> %t
33-
RUN: printf '\xc8\xff\3\0\1\0\0\0' >> %t
34-
RUN: printf '\xc3\xff\3\0\3\0\0\0' >> %t
33+
RUN: printf '\310\377\3\0\1\0\0\0' >> %t
34+
RUN: printf '\303\377\3\0\3\0\0\0' >> %t
3535
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
3636
RUN: printf '\0\0\0\0\0\0\0\0' >> %t
3737
RUN: printf '\02\0\0\0' >> %t
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## Show that llvm-strings can handle a negative signed char value (i.e. > 0x7f).
22
## Such characters should form string delimiters like other unprintable ones.
33

4-
# RUN: echo -e "z\0\x80\0a\0" | llvm-strings --bytes 1 - | FileCheck %s
4+
# RUN: printf "z\0\200\0a\0" | llvm-strings --bytes 1 - | FileCheck %s
55
# CHECK: z{{$}}
66
# CHECK-NEXT: {{^}}a

0 commit comments

Comments
 (0)