Skip to content

Commit 9eaeaab

Browse files
author
tzijnge
committed
#19 Use clang-format
* Some CppFile updates
1 parent 5b5eda4 commit 9eaeaab

7 files changed

Lines changed: 47 additions & 42 deletions

File tree

src/lrpc/codegen/common.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@
44
from lrpc.core import LrpcVar
55

66

7-
def lrpc_var_includes(var: LrpcVar) -> set[str]:
8-
includes = set()
7+
def lrpc_var_includes(var: LrpcVar) -> set[tuple[str, bool]]:
8+
includes: set[tuple[str, bool]] = set()
99
if var.base_type_is_integral():
10-
includes.add("<cstdint>")
10+
includes.add(("<cstdint>", True))
1111

1212
if var.base_type_is_custom():
13-
includes.add(f'"{var.base_type()}.hpp"')
13+
includes.add((f'"{var.base_type()}.hpp"', True))
1414

1515
if var.base_type_is_string() or var.is_array() or var.is_optional():
16-
includes.add('"lrpccore/LrpcTypes.hpp"')
16+
includes.add(('"lrpccore/LrpcTypes.hpp"', True))
1717

1818
return includes
1919

src/lrpc/codegen/cppfile/cpp_file.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010

1111

1212
class CppFile:
13+
IWYU_EXPORT = " // IWYU pragma: export"
14+
1315
def __init__(self, filename: str, writer: TextIO | None = None, indent: str = " ") -> None:
1416
self._indent = indent
1517
self._level = 0
@@ -26,11 +28,18 @@ def __call__(self, text: str) -> None:
2628
def newline(self) -> None:
2729
self._file.write("\n")
2830

31+
def pragma_once(self) -> None:
32+
self.write("#pragma once")
33+
34+
def include(self, path: str, *, iwyu_export: bool = False) -> None:
35+
suffix = self.IWYU_EXPORT if iwyu_export else ""
36+
self.write(f"#include {path}{suffix}")
37+
2938
def label(self, text: str) -> None:
3039
self._file.write(self._indent * (self._level - 1) + text + ":\n")
3140

3241
@contextmanager
33-
def block(self, text: str, postfix: str = "") -> Generator[None, None, None]:
42+
def block(self, text: str, postfix: str = "", *, trailing_newline: bool = False) -> Generator[None, None, None]:
3443
self.write(text)
3544
self._file.write(self._indent * self._level + "{\n")
3645
self._level += 1
@@ -39,6 +48,8 @@ def block(self, text: str, postfix: str = "") -> Generator[None, None, None]:
3948
finally:
4049
self._level -= 1
4150
self._file.write(self._indent * self._level + "}" + postfix + "\n")
51+
if trailing_newline:
52+
self._file.write("\n")
4253

4354
def close(self) -> None:
4455
if self._owns_file:

src/lrpc/codegen/meta_constants_writer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ def write_constants(
2828
self._compressed_definition = compressed_definition
2929
self._definition_stream_chunk_size = definition_stream_chunk_size
3030

31-
self._file.write("#pragma once")
32-
self._file.write("#include <cstdint>")
33-
self._file.write('#include "lrpccore/LrpcTypes.hpp"')
31+
self._file.pragma_once()
32+
self._file.include("<cstdint>")
33+
self._file.include('"lrpccore/LrpcTypes.hpp"')
3434

3535
self._file.newline()
3636
optionally_in_namespace(self._file, self._write_constants, namespace)

src/lrpc/codegen/meta_service_file_writer.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ def __init__(self, file: CppFile) -> None:
3939
self._file = file
4040

4141
def write_service(self, namespace: str | None = None) -> None:
42-
self._file.write("#pragma once")
43-
self._file.write('#include "LrpcMeta_shim.hpp"')
44-
self._file.write('#include "LrpcMeta_constants.hpp"')
42+
self._file.pragma_once()
43+
self._file.include('"LrpcMeta_shim.hpp"')
44+
self._file.include('"LrpcMeta_constants.hpp"')
4545

4646
self._file.newline()
4747
optionally_in_namespace(self._file, self._write_service_class, namespace)

src/lrpc/codegen/server_include.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@ def visit_lrpc_def(self, lrpc_def: LrpcDef) -> None:
2222
self._file = CppFile(f"{self._output}/{lrpc_def.name()}.hpp")
2323

2424
write_file_banner(self._file)
25-
self._file.write("#pragma once")
26-
self._file.write('#include "lrpccore/LrpcTypes.hpp" // IWYU pragma: export')
27-
self._file.write('#include "lrpccore/Server.hpp" // IWYU pragma: export')
25+
self._file.pragma_once()
26+
self._file.include('"lrpccore/LrpcTypes.hpp"', iwyu_export=True)
27+
self._file.include('"lrpccore/Server.hpp"', iwyu_export=True)
2828
if len(lrpc_def.constants()) != 0:
29-
self._file.write(f'#include "{lrpc_def.name()}_Constants.hpp" // IWYU pragma: export')
30-
self._file.write('#include "LrpcMeta_constants.hpp" // IWYU pragma: export')
31-
self._file.write('#include "LrpcMeta_service.hpp" // IWYU pragma: export')
29+
self._file.include(f'"{lrpc_def.name()}_Constants.hpp"', iwyu_export=True)
30+
self._file.include('"LrpcMeta_constants.hpp"', iwyu_export=True)
31+
self._file.include('"LrpcMeta_service.hpp"', iwyu_export=True)
3232

3333
def visit_rpc_settings(self, settings: RpcSettings) -> None:
3434
rx = settings.rx_buffer_size()
@@ -40,8 +40,8 @@ def visit_rpc_settings(self, settings: RpcSettings) -> None:
4040

4141
def visit_lrpc_service(self, service: LrpcService) -> None:
4242
if service.name() != "LrpcMeta":
43-
self._file.write(f'#include "{service.name()}_includes.hpp" // IWYU pragma: export')
44-
self._file.write(f'#include "{service.name()}_shim.hpp" // IWYU pragma: export')
43+
self._file.include(f'"{service.name()}_includes.hpp"', iwyu_export=True)
44+
self._file.include(f'"{service.name()}_shim.hpp"', iwyu_export=True)
4545

4646
def visit_lrpc_def_end(self) -> None:
4747
self._file.newline()

src/lrpc/codegen/service_include.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ class ServiceIncludeVisitor(LrpcVisitor):
1010
def __init__(self, output: Path) -> None:
1111
self._output = output
1212
self._file: CppFile
13-
self._includes: set[str] = set()
13+
self._includes: set[tuple[str, bool]] = set()
1414

1515
def _create_service_include(self, output: Path, service_name: str) -> None:
1616
self._includes = set()
1717

1818
self._file = CppFile(f"{output}/{service_name}_includes.hpp")
1919
write_file_banner(self._file)
20-
self._file.write("#pragma once")
20+
self._file.pragma_once()
2121

2222
def visit_lrpc_service(self, service: LrpcService) -> None:
2323
self._create_service_include(self._output, service.name())
2424

2525
def visit_lrpc_service_end(self) -> None:
26-
for i in sorted(self._includes):
27-
self._file.write(f"#include {i} // IWYU pragma: export")
26+
for path, iwyu_export in sorted(self._includes):
27+
self._file.include(path, iwyu_export=iwyu_export)
2828

2929
def visit_lrpc_function(self, function: LrpcFun) -> None:
3030
if function.number_returns() > 1:
31-
self._includes.add("<tuple>")
31+
self._includes.add(("<tuple>", True))
3232

3333
def visit_lrpc_function_return(self, ret: LrpcVar) -> None:
3434
self._includes.update(lrpc_var_includes(ret))

src/lrpc/codegen/service_shim.py

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -133,25 +133,23 @@ def _write_server_stream_stop_request_shims(self, server_streams: list[LrpcStrea
133133
self._file.write("// Server stream start/stop shims")
134134

135135
for stream in server_streams:
136-
with self._file.block(f"void {stream.name()}_start_stop_shim(Reader& reader)"):
136+
with self._file.block(
137+
f"void {stream.name()}_start_stop_shim(Reader& reader)", trailing_newline=True,
138+
):
137139
self._file.write("const auto start = reader.read_unchecked<bool>();")
138140
with self._file.block("if (start)"):
139141
self._file.write(f"{stream.name()}();")
140142
with self._file.block("else"):
141143
self._file.write(f"{stream.name()}_stop();")
142144

143-
self._file.newline()
144-
145145
def _write_client_stream_stop_requests(self, client_streams: list[LrpcStream]) -> None:
146146
if len(client_streams) != 0:
147147
self._file.write("// Client stream stop requests")
148148

149149
for stream in client_streams:
150-
with self._file.block(f"void {stream.name()}_requestStop()"):
150+
with self._file.block(f"void {stream.name()}_requestStop()", trailing_newline=True):
151151
self._file(f"requestStop({stream.id()});")
152152

153-
self._file.newline()
154-
155153
def _write_shim_array(
156154
self,
157155
functions: list[LrpcFun],
@@ -161,16 +159,16 @@ def _write_shim_array(
161159
max_function_or_stream_id = self._max_function_or_stream_id(functions, client_streams, server_streams)
162160

163161
self._file.write(f"using ShimType = void ({self._class_name()}::*)(Reader &);")
164-
with self._file.block("void missingFunction_shim(Reader& reader)"):
162+
with self._file.block("void missingFunction_shim(Reader& reader)", trailing_newline=True):
165163
self._file.write("const auto data = reader.data();")
166164
self._file.write("const auto functionOrStreamId = static_cast<uint8_t>(data.at(2));")
167165
self._file.write("server().error(LrpcMetaError::UnknownFunctionOrStream, id(), functionOrStreamId);")
168-
self._file.newline()
169166

170167
with self._file.block("static ShimType shim(const size_t functionId)"):
171168
with self._file.block(
172169
f"static constexpr lrpc::array<ShimType, {max_function_or_stream_id + 1}> shims",
173170
";",
171+
trailing_newline=True,
174172
):
175173
function_info = {function.id(): function.name() for function in functions}
176174
client_stream_info = {stream.id(): stream.name() for stream in client_streams}
@@ -183,13 +181,9 @@ def _write_shim_array(
183181
name = server_stream_info.get(fid, name)
184182
self._file(f"&{self._class_name()}::{name}_shim,")
185183

186-
self._file.newline()
187-
188-
with self._file.block("if (functionId >= shims.size())"):
184+
with self._file.block("if (functionId >= shims.size())", trailing_newline=True):
189185
self._file.write(f"return &{self._class_name()}::missingFunction_shim;")
190186

191-
self._file.newline()
192-
193187
self._file.write("return shims.at(functionId);")
194188

195189
@staticmethod
@@ -219,12 +213,12 @@ def _max_function_or_stream_id(
219213
return max(server_stream_ids + client_stream_ids + function_ids)
220214

221215
def _write_include_guard(self) -> None:
222-
self._file("#pragma once")
216+
self._file.pragma_once()
223217

224218
def _write_includes(self) -> None:
225-
self._file('#include "lrpccore/Service.hpp"')
226-
self._file('#include "lrpccore/EtlRwExtensions.hpp"')
227-
self._file(f'#include "{self._service.name()}_includes.hpp"')
219+
self._file.include('"lrpccore/Service.hpp"')
220+
self._file.include('"lrpccore/EtlRwExtensions.hpp"')
221+
self._file.include(f'"{self._service.name()}_includes.hpp"')
228222

229223
self._file.newline()
230224

0 commit comments

Comments
 (0)