[libc] Make hdrgen emit correct relative paths for llvm-libc-types (#127150)

This makes hdrgen emit `#include "..."` lines using the correct
relative path (number of `../` steps, if any) for the containing
header, and includes this in the sorted block of `#include` lines
also used for the macro headers.
This commit is contained in:
Roland McGrath
2025-02-14 11:01:26 -08:00
committed by GitHub
parent 1199bbb396
commit d2dd8021fd
7 changed files with 77 additions and 16 deletions

View File

@@ -6,7 +6,7 @@
#
# ==-------------------------------------------------------------------------==#
from pathlib import PurePath
from pathlib import PurePosixPath
class HeaderFile:
@@ -37,26 +37,33 @@ class HeaderFile:
def includes(self):
return sorted(
{
PurePath("llvm-libc-macros") / macro.header
PurePosixPath("llvm-libc-macros") / macro.header
for macro in self.macros
if macro.header is not None
}
| {
PurePosixPath("llvm-libc-types") / f"{typ.type_name}.h"
for typ in self.types
}
)
def public_api(self):
header_dir = PurePath(self.name).parent
# Python 3.12 has .relative_to(dir, walk_up=True) for this.
path_prefix = PurePosixPath("../" * (len(PurePosixPath(self.name).parents) - 1))
def relpath(file):
return path_prefix / file
content = [
f'#include "{file.relative_to(header_dir)}"' for file in self.includes()
] + [""]
f"#include {file}"
for file in sorted(f'"{relpath(file)!s}"' for file in self.includes())
]
for macro in self.macros:
# When there is nothing to define, the Macro object converts to str
# as an empty string. Don't emit a blank line for those cases.
if str(macro):
content.append(f"{macro}\n")
for type_ in self.types:
content.append(f"{type_}")
content.extend(["", f"{macro}"])
if self.enumerations:
combined_enum_content = ",\n ".join(

View File

@@ -0,0 +1,23 @@
//===-- C standard library header subdir/test.h --------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===--------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SUBDIR_TEST_H
#define LLVM_LIBC_SUBDIR_TEST_H
#include "../__llvm-libc-common.h"
#include "../llvm-libc-types/type_a.h"
#include "../llvm-libc-types/type_b.h"
__BEGIN_C_DECLS
type_a func(type_b) __NOEXCEPT;
__END_C_DECLS
#endif // LLVM_LIBC_SUBDIR_TEST_H

View File

@@ -15,6 +15,8 @@
#include "llvm-libc-macros/test_more-macros.h"
#include "llvm-libc-macros/test_small-macros.h"
#include "llvm-libc-types/type_a.h"
#include "llvm-libc-types/type_b.h"
#define MACRO_A 1
@@ -22,9 +24,6 @@
#define MACRO_C
#include <llvm-libc-types/type_a.h>
#include <llvm-libc-types/type_b.h>
enum {
enum_a = value_1,
enum_b = value_2,

View File

@@ -0,0 +1,16 @@
//===-- C standard library header subdir/test.h --------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===--------------------------------------------------------------------===//
#ifndef LLVM_LIBC_SUBDIR_TEST_H
#define LLVM_LIBC_SUBDIR_TEST_H
#include "../__llvm-libc-common.h"
%%public_api()
#endif // LLVM_LIBC_SUBDIR_TEST_H

View File

@@ -0,0 +1,12 @@
header: subdir/test.h
header_template: test.h.def
types:
- type_name: type_a
- type_name: type_b
functions:
- name: func
return_type: type_a
arguments:
- type: type_b
standards:
- stdc

View File

@@ -12,7 +12,7 @@ class TestHeaderGenIntegration(unittest.TestCase):
self.main_script = self.source_dir.parent / "main.py"
self.maxDiff = 80 * 100
def run_script(self, yaml_file, output_file, entry_points):
def run_script(self, yaml_file, output_file, entry_points=[]):
command = [
"python3",
str(self.main_script),
@@ -52,6 +52,13 @@ class TestHeaderGenIntegration(unittest.TestCase):
self.compare_files(output_file, expected_output_file)
def test_generate_subdir_header(self):
yaml_file = self.source_dir / "input" / "subdir" / "test.yaml"
expected_output_file = self.source_dir / "expected_output" / "subdir" / "test.h"
output_file = self.output_dir / "subdir" / "test.h"
self.run_script(yaml_file, output_file)
self.compare_files(output_file, expected_output_file)
def main():
parser = argparse.ArgumentParser(description="TestHeaderGenIntegration arguments")

View File

@@ -10,6 +10,3 @@
class Type:
def __init__(self, type_name):
self.type_name = type_name
def __str__(self):
return f"#include <llvm-libc-types/{self.type_name}.h>"