Skip to content

Commit e3bd43b

Browse files
authored
[libc] added grouping of guarded functions in header generation (llvm#98532)
Instead of #ifdef guards for each individual function, #ifdef and #endif will surround all functions that have the same guard.
1 parent e8734e4 commit e3bd43b

File tree

6 files changed

+119
-14
lines changed

6 files changed

+119
-14
lines changed

libc/newhdrgen/class_implementation/classes/function.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,4 @@ def __str__(self):
2929
result = f"{self.return_type} {self.name}({arguments_str}) __NOEXCEPT;"
3030
else:
3131
result = f"{attributes_str} {self.return_type} {self.name}({arguments_str}) __NOEXCEPT;"
32-
if self.guard:
33-
result = f"#ifdef {self.guard}\n{result}\n#endif // {self.guard}"
3432
return result

libc/newhdrgen/header.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,33 @@ def __str__(self):
5757

5858
content.append("\n__BEGIN_C_DECLS\n")
5959

60+
current_guard = None
6061
for function in self.functions:
61-
content.append(str(function))
62-
content.append("")
62+
if function.guard == None:
63+
content.append(str(function))
64+
content.append("")
65+
else:
66+
if current_guard == None:
67+
current_guard = function.guard
68+
content.append(f"#ifdef {current_guard}")
69+
content.append(str(function))
70+
content.append("")
71+
elif current_guard == function.guard:
72+
content.append(str(function))
73+
content.append("")
74+
else:
75+
content.pop()
76+
content.append(f"#endif // {current_guard}")
77+
content.append("")
78+
current_guard = function.guard
79+
content.append(f"#ifdef {current_guard}")
80+
content.append(str(function))
81+
content.append("")
82+
if current_guard != None:
83+
content.pop()
84+
content.append(f"#endif // {current_guard}")
85+
content.append("")
86+
6387
for object in self.objects:
6488
content.append(str(object))
6589
if self.objects:

libc/newhdrgen/tests/expected_output/test_header.h

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#define LLVM_LIBC_TEST_SMALL_H
1111

1212
#include "__llvm-libc-common.h"
13+
#include "llvm-libc-macros/float16-macros.h"
1314
#include "llvm-libc-macros/test_small-macros.h"
15+
#include "llvm-libc-types/float128.h"
1416

1517
#define MACRO_A 1
1618

@@ -26,13 +28,21 @@ enum {
2628

2729
__BEGIN_C_DECLS
2830

29-
#ifdef FUNC_A_16
3031
CONST_FUNC_A void func_a() __NOEXCEPT;
31-
#endif // FUNC_A_16
3232

33-
#ifdef FUNC_B_16
34-
CONST_FUNC_B int func_b(int, float) __NOEXCEPT;
35-
#endif // FUNC_B_16
33+
#ifdef LIBC_TYPES_HAS_FLOAT128
34+
float128 func_b() __NOEXCEPT;
35+
#endif // LIBC_TYPES_HAS_FLOAT128
36+
37+
#ifdef LIBC_TYPES_HAS_FLOAT16
38+
_Float16 func_c(int, float) __NOEXCEPT;
39+
40+
_Float16 func_d(int, float) __NOEXCEPT;
41+
#endif // LIBC_TYPES_HAS_FLOAT16
42+
43+
#ifdef LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128
44+
_Float16 func_e(float128) __NOEXCEPT;
45+
#endif // LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128
3646

3747
extern obj object_1;
3848
extern obj object_2;

libc/newhdrgen/tests/input/test_small.h.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
#define LLVM_LIBC_TEST_SMALL_H
1111

1212
#include "__llvm-libc-common.h"
13+
#include "llvm-libc-macros/float16-macros.h"
1314
#include "llvm-libc-macros/test_small-macros.h"
15+
#include "llvm-libc-types/float128.h"
1416

1517
%%public_api()
1618

libc/newhdrgen/tests/input/test_small.yaml

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,35 @@ functions:
2323
arguments: []
2424
standards:
2525
- stdc
26-
guard: FUNC_A_16
2726
attributes:
2827
- CONST_FUNC_A
2928
- name: func_b
30-
return_type: int
29+
return_type: float128
30+
arguments: []
31+
standards:
32+
- stdc
33+
guard: LIBC_TYPES_HAS_FLOAT128
34+
- name: func_c
35+
return_type: _Float16
3136
arguments:
3237
- type: int
3338
- type: float
3439
standards:
3540
- stdc
36-
guard: FUNC_B_16
37-
attributes:
38-
- CONST_FUNC_B
41+
guard: LIBC_TYPES_HAS_FLOAT16
42+
- name: func_d
43+
return_type: _Float16
44+
arguments:
45+
- type: int
46+
- type: float
47+
standards:
48+
- stdc
49+
guard: LIBC_TYPES_HAS_FLOAT16
50+
- name: func_e
51+
return_type: _Float16
52+
arguments:
53+
- type: float128
54+
standards:
55+
- stdc
56+
guard: LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128
57+
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
//===-- C standard library header test_small-------------------------------===//
2+
//
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
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_TEST_SMALL_H
10+
#define LLVM_LIBC_TEST_SMALL_H
11+
12+
#include "__llvm-libc-common.h"
13+
#include "llvm-libc-macros/float16-macros.h"
14+
#include "llvm-libc-macros/test_small-macros.h"
15+
#include "llvm-libc-types/float128.h"
16+
17+
#define MACRO_A 1
18+
19+
#define MACRO_B 2
20+
21+
#include <llvm-libc-types/type_a.h>
22+
#include <llvm-libc-types/type_b.h>
23+
24+
enum {
25+
enum_a = value_1,
26+
enum_b = value_2,
27+
};
28+
29+
__BEGIN_C_DECLS
30+
31+
CONST_FUNC_A void func_a() __NOEXCEPT;
32+
33+
#ifdef LIBC_TYPES_HAS_FLOAT128
34+
float128 func_b() __NOEXCEPT;
35+
#endif // LIBC_TYPES_HAS_FLOAT128
36+
37+
#ifdef LIBC_TYPES_HAS_FLOAT16
38+
_Float16 func_c(int, float) __NOEXCEPT;
39+
40+
_Float16 func_d(int, float) __NOEXCEPT;
41+
#endif // LIBC_TYPES_HAS_FLOAT16
42+
43+
#ifdef LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128
44+
_Float16 func_e(float128) __NOEXCEPT;
45+
#endif // LIBC_TYPES_HAS_FLOAT16_AND_FLOAT128
46+
47+
extern obj object_1;
48+
extern obj object_2;
49+
50+
__END_C_DECLS
51+
52+
#endif // LLVM_LIBC_TEST_SMALL_H

0 commit comments

Comments
 (0)