[libc++][Android] Allow testing libc++ with clang-r536225 (#116149)
[llvm-project.git] / libc / newhdrgen / header.py
blobdf8ce613bd0f9951b5014d2b30c40cf2646e9f1c
1 # ====- HeaderFile Class for libc function headers -----------*- python -*--==#
3 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 # See https://llvm.org/LICENSE.txt for license information.
5 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 # ==-------------------------------------------------------------------------==#
10 class HeaderFile:
11 def __init__(self, name):
12 self.name = name
13 self.macros = []
14 self.types = []
15 self.enumerations = []
16 self.objects = []
17 self.functions = []
19 def add_macro(self, macro):
20 self.macros.append(macro)
22 def add_type(self, type_):
23 self.types.append(type_)
25 def add_enumeration(self, enumeration):
26 self.enumerations.append(enumeration)
28 def add_object(self, object):
29 self.objects.append(object)
31 def add_function(self, function):
32 self.functions.append(function)
34 def __str__(self):
35 content = [""]
37 for macro in self.macros:
38 content.append(f"{macro}\n")
40 for type_ in self.types:
41 content.append(f"{type_}")
43 if self.enumerations:
44 combined_enum_content = ",\n ".join(
45 str(enum) for enum in self.enumerations
47 content.append(f"\nenum {{\n {combined_enum_content},\n}};")
49 content.append("\n__BEGIN_C_DECLS\n")
51 current_guard = None
52 for function in self.functions:
53 if function.guard == None:
54 content.append(str(function) + " __NOEXCEPT;")
55 content.append("")
56 else:
57 if current_guard == None:
58 current_guard = function.guard
59 content.append(f"#ifdef {current_guard}")
60 content.append(str(function) + " __NOEXCEPT;")
61 content.append("")
62 elif current_guard == function.guard:
63 content.append(str(function) + " __NOEXCEPT;")
64 content.append("")
65 else:
66 content.pop()
67 content.append(f"#endif // {current_guard}")
68 content.append("")
69 current_guard = function.guard
70 content.append(f"#ifdef {current_guard}")
71 content.append(str(function) + " __NOEXCEPT;")
72 content.append("")
73 if current_guard != None:
74 content.pop()
75 content.append(f"#endif // {current_guard}")
76 content.append("")
78 for object in self.objects:
79 content.append(str(object))
80 if self.objects:
81 content.append("\n__END_C_DECLS")
82 else:
83 content.append("__END_C_DECLS")
85 return "\n".join(content)