Skip to content

Commit a24a754

Browse files
authored
[libc][hdrgen] Sort identifiers with leading underscores specially (llvm#165745)
This makes the sorting behavior more uniform: functions and macros are always sorted (separately), not only when merging. This changes the sort order used for functions and other things sorted by their symbol names. Symbols are sorted alphabetically without regard to leading underscores, and then for identifiers that differ only in the number of leading underscores, the fewer underscores the earlier in the sort order. For the functions declared in a generated header, adjacent names with and without underscores will be grouped together without blank lines. This is implemented by factoring the name field, equality, and sorting support out of the various entity classes into a new common superclass (hdrgen.Symbol). This uncovered YAML's requirement to quote the string "NULL" to avoid pyyaml parsing it as None (equivalent to Javascript null) rather than a string.
1 parent 25afea7 commit a24a754

File tree

17 files changed

+129
-80
lines changed

17 files changed

+129
-80
lines changed

libc/include/locale.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
header: locale.h
22
header_template: locale.h.def
33
macros:
4-
- macro_name: NULL
4+
- macro_name: "NULL"
55
macro_header: null-macro.h
66
types:
77
- type_name: locale_t

libc/include/stdio.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
header: stdio.h
22
header_template: stdio.h.def
33
macros:
4-
- macro_name: NULL
4+
- macro_name: "NULL"
55
macro_header: null-macro.h
66
- macro_name: stdout
77
macro_value: stdout

libc/include/stdlib.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ standards:
55
merge_yaml_files:
66
- stdlib-malloc.yaml
77
macros:
8-
- macro_name: NULL
8+
- macro_name: "NULL"
99
macro_header: null-macro.h
1010
types:
1111
- type_name: __atexithandler_t

libc/include/string.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ header: string.h
22
standards:
33
- stdc
44
macros:
5-
- macro_name: NULL
5+
- macro_name: "NULL"
66
macro_header: null-macro.h
77
types:
88
- type_name: locale_t

libc/include/time.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
header: time.h
22
header_template: time.h.def
33
macros:
4-
- macro_name: NULL
4+
- macro_name: "NULL"
55
macro_header: null-macro.h
66
types:
77
- type_name: struct_timeval

libc/include/wchar.yaml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
header: wchar.h
22
header_template: wchar.h.def
33
macros:
4-
- macro_name: NULL
4+
- macro_name: "NULL"
55
macro_header: null-macro.h
66
types:
77
- type_name: FILE
@@ -188,8 +188,8 @@ functions:
188188
standards:
189189
- stdc
190190
return_type: wchar_t *
191-
arguments:
192-
- type: wchar_t *__restrict
191+
arguments:
192+
- type: wchar_t *__restrict
193193
- type: const wchar_t *__restrict
194194
- type: size_t
195195
- name: wmemmove
@@ -212,7 +212,7 @@ functions:
212212
standards:
213213
- stdc
214214
return_type: wchar_t *
215-
arguments:
215+
arguments:
216216
- type: wchar_t *__restrict
217217
- type: const wchar_t *__restrict
218218
- name: wcslcat

libc/utils/hdrgen/hdrgen/enumeration.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,14 @@
66
#
77
# ==-------------------------------------------------------------------------==#
88

9-
from functools import total_ordering
9+
from hdrgen.symbol import Symbol
1010

1111

12-
@total_ordering
13-
class Enumeration:
12+
class Enumeration(Symbol):
1413
def __init__(self, name, value):
15-
self.name = name
14+
super().__init__(name)
1615
self.value = value
1716

18-
def __eq__(self, other):
19-
return self.name == other.name
20-
21-
def __lt__(self, other):
22-
return self.name < other.name
23-
24-
def __hash__(self):
25-
return self.name.__hash__()
26-
2717
def __str__(self):
2818
if self.value != None:
2919
return f"{self.name} = {self.value}"

libc/utils/hdrgen/hdrgen/function.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# ==-------------------------------------------------------------------------==#
88

99
import re
10-
from functools import total_ordering
10+
from hdrgen.symbol import Symbol
1111
from hdrgen.type import Type
1212

1313

@@ -37,14 +37,13 @@
3737
NONIDENTIFIER = re.compile("[^a-zA-Z0-9_]+")
3838

3939

40-
@total_ordering
41-
class Function:
40+
class Function(Symbol):
4241
def __init__(
4342
self, return_type, name, arguments, standards, guard=None, attributes=[]
4443
):
44+
super().__init__(name)
4545
assert return_type
4646
self.return_type = return_type
47-
self.name = name
4847
self.arguments = [
4948
arg if isinstance(arg, str) else arg["type"] for arg in arguments
5049
]
@@ -53,15 +52,6 @@ def __init__(
5352
self.guard = guard
5453
self.attributes = attributes or []
5554

56-
def __eq__(self, other):
57-
return self.name == other.name
58-
59-
def __lt__(self, other):
60-
return self.name < other.name
61-
62-
def __hash__(self):
63-
return self.name.__hash__()
64-
6555
def signature_types(self):
6656
def collapse(type_string):
6757
assert type_string

libc/utils/hdrgen/hdrgen/header.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,8 @@ def includes(self):
147147
}
148148
| {
149149
COMPILER_HEADER_TYPES.get(
150-
typ.type_name,
151-
PurePosixPath("llvm-libc-types") / f"{typ.type_name}.h",
150+
typ.name,
151+
PurePosixPath("llvm-libc-types") / f"{typ.name}.h",
152152
)
153153
for typ in self.all_types()
154154
}
@@ -227,7 +227,7 @@ def relpath(file):
227227
)
228228
]
229229

230-
for macro in self.macros:
230+
for macro in sorted(self.macros):
231231
# When there is nothing to define, the Macro object converts to str
232232
# as an empty string. Don't emit a blank line for those cases.
233233
if str(macro):
@@ -242,7 +242,12 @@ def relpath(file):
242242
content.append("\n__BEGIN_C_DECLS\n")
243243

244244
current_guard = None
245-
for function in self.functions:
245+
last_name = None
246+
for function in sorted(self.functions):
247+
# If the last function's name was the same after underscores,
248+
# elide the blank line between the declarations.
249+
if last_name == function.name_without_underscores():
250+
content.pop()
246251
if function.guard == None and current_guard == None:
247252
content.append(str(function) + " __NOEXCEPT;")
248253
content.append("")
@@ -264,6 +269,7 @@ def relpath(file):
264269
content.append(f"#ifdef {current_guard}")
265270
content.append(str(function) + " __NOEXCEPT;")
266271
content.append("")
272+
last_name = function.name_without_underscores()
267273
if current_guard != None:
268274
content.pop()
269275
content.append(f"#endif // {current_guard}")

libc/utils/hdrgen/hdrgen/macro.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,25 +6,15 @@
66
#
77
# ==-------------------------------------------------------------------------==#
88

9-
from functools import total_ordering
9+
from hdrgen.symbol import Symbol
1010

1111

12-
@total_ordering
13-
class Macro:
12+
class Macro(Symbol):
1413
def __init__(self, name, value=None, header=None):
15-
self.name = name
14+
super().__init__(name)
1615
self.value = value
1716
self.header = header
1817

19-
def __eq__(self, other):
20-
return self.name == other.name
21-
22-
def __lt__(self, other):
23-
return self.name < other.name
24-
25-
def __hash__(self):
26-
return self.name.__hash__()
27-
2818
def __str__(self):
2919
if self.header != None:
3020
return ""

0 commit comments

Comments
 (0)