[libc][hdrgen] Print __BEGIN_C_DECLS / __END_C_DECLS conditionally. (#188830)

Clean up the `%public_api` printer code slightly - get rid of explicit
`\n` and ensure we only print `__BEGIN_C_DECLS` and `__END_C_DECLS` if
the generated header actually contains functions or objects to declare.

I've noticed that after 27ba9e2a44 landed,
generated errno.h header has two blocks of `__BEGIN_C_DECLS` /
`__END_C_DECLS`: an empty one was generated automatically from
`%public_api` section that was intended to only add the `errno_t` type
declaration.
This commit is contained in:
Alexey Samsonov
2026-03-26 13:02:34 -07:00
committed by GitHub
parent 049700fcef
commit 968b6aaef1
4 changed files with 40 additions and 3 deletions

View File

@@ -280,9 +280,14 @@ class HeaderFile:
self.include_lines(self.template_file is None)
+ self.macro_lines()
+ self.enum_lines()
+ ["\n__BEGIN_C_DECLS\n"]
)
content.append("")
has_decls = self.functions or self.objects
if has_decls:
content.append("__BEGIN_C_DECLS")
content.append("")
# Emit function declarations.
current_guard = None
last_name = None
for function in sorted(self.functions):
@@ -317,10 +322,13 @@ class HeaderFile:
content.append(f"#endif // {current_guard}")
content.append("")
# Emit object declarations.
content.extend(str(object) for object in self.objects)
if self.objects:
content.append("")
content.append("__END_C_DECLS")
if has_decls:
content.append("__END_C_DECLS")
return "\n".join(content)

View File

@@ -0,0 +1,16 @@
//===-- Standard C header <macro_only.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_MACRO_ONLY_H
#define _LLVM_LIBC_MACRO_ONLY_H
#include "__llvm-libc-common.h"
#define MACRO_A 1
#endif // _LLVM_LIBC_MACRO_ONLY_H

View File

@@ -0,0 +1,6 @@
header: macro_only.h
standards:
- stdc
macros:
- macro_name: MACRO_A
macro_value: 1

View File

@@ -82,13 +82,20 @@ class TestHeaderGenIntegration(unittest.TestCase):
self.run_script(yaml_file, output_file)
self.compare_files(output_file, expected_output_file)
def test_generate_header(self):
def test_generate_proxy_header(self):
yaml_file = self.source_dir / "input/test_small.yaml"
expected_output_file = self.source_dir / "expected_output/test_small_proxy.h"
output_file = self.output_dir / "test_small.h"
self.run_script(yaml_file, output_file, switches=["--proxy"])
self.compare_files(output_file, expected_output_file)
def test_generate_macro_only_header(self):
yaml_file = self.source_dir / "input/macro_only.yaml"
expected_output_file = self.source_dir / "expected_output/macro_only.h"
output_file = self.output_dir / "macro_only.h"
self.run_script(yaml_file, output_file)
self.compare_files(output_file, expected_output_file)
def main():
parser = argparse.ArgumentParser(description="TestHeaderGenIntegration arguments")
parser.add_argument(