Debian Codegraph @

LspCpp/third_party/asio/tsify.pl

raw
1 #!/usr/bin/perl -w
2
3 use strict;
4 use File::Path;
5 # use Switch;
6
7 our $output_dir = "tsified";
8
9 sub print_line
10 {
11 my ($output, $line, $from, $lineno) = @_;
12
13 # Warn if the resulting line is >80 characters wide.
14 if (length($line) > 80)
15 {
16 if ($from =~ /\.[chi]pp$/)
17 {
18 print("Warning: $from:$lineno: output >80 characters wide.\n");
19 }
20 }
21
22 # Write the output.
23 print($output $line . "\n");
24 }
25
26 sub source_contains_asio_thread_usage
27 {
28 my ($from) = @_;
29
30 # Open the input file.
31 open(my $input, "<$from") or die("Can't open $from for reading");
32
33 # Check file for use of asio::thread.
34 while (my $line = <$input>)
35 {
36 chomp($line);
37 if ($line =~ /asio::thread/)
38 {
39 close($input);
40 return 1;
41 }
42 elsif ($line =~ /^ *thread /)
43 {
44 close($input);
45 return 1;
46 }
47 }
48
49 close($input);
50 return 0;
51 }
52
53 sub source_contains_asio_include
54 {
55 my ($from) = @_;
56
57 # Open the input file.
58 open(my $input, "<$from") or die("Can't open $from for reading");
59
60 # Check file for inclusion of asio.hpp.
61 while (my $line = <$input>)
62 {
63 chomp($line);
64 if ($line =~ /# *include [<"]asio\.hpp[>"]/)
65 {
66 close($input);
67 return 1;
68 }
69 }
70
71 close($input);
72 return 0;
73 }
74
75 sub copy_source_file
76 {
77 my ($from, $to) = @_;
78
79 # Ensure the output directory exists.
80 my $dir = $to;
81 $dir =~ s/[^\/]*$//;
82 mkpath($dir);
83
84 # First determine whether the file makes any use of asio::thread.
85 my $uses_asio_thread = source_contains_asio_thread_usage($from);
86
87 my $includes_asio = source_contains_asio_include($from);
88
89 my $is_asio_hpp = 0;
90 $is_asio_hpp = 1 if ($from =~ /asio\.hpp/);
91
92 my $needs_doc_link = 0;
93 $needs_doc_link = 1 if ($is_asio_hpp);
94
95 my $is_config_hpp = 0;
96 $is_config_hpp = 1 if ($from =~ /asio\/detail\/config\.hpp/);
97
98 my $is_error_hpp = 0;
99 $is_error_hpp = 1 if ($from =~ /asio\/error\.hpp/);
100
101 my $is_impl_src_hpp = 0;
102 $is_impl_src_hpp = 1 if ($from =~ /asio\/impl\/src\.hpp/);
103
104 my $is_test = 0;
105 $is_test = 1 if ($from =~ /tests\/unit/);
106
107 # Open the files.
108 open(my $input, "<$from") or die("Can't open $from for reading");
109 open(my $output, ">$to") or die("Can't open $to for writing");
110
111 # State for stripping out deprecated, extension, and old services code.
112 my $deprecated_state = 0;
113 my $extension_state = 0;
114 my $old_services_state = 0;
115
116 # State for simplifying namespaces in examples.
117 my $code_snippet_state = 0;
118
119 # Copy the content.
120 my $lineno = 1;
121 while (my $line = <$input>)
122 {
123 chomp($line);
124
125 # Strip out deprecated code.
126 if ($deprecated_state == 0)
127 {
128 if ($line =~ /#\s*if\s*defined\(ASIO_NO_DEPRECATED\)/)
129 {
130 $deprecated_state = 1;
131 next;
132 }
133 elsif ($line =~ /#\s*if\s*!defined\(ASIO_NO_DEPRECATED\)/)
134 {
135 $deprecated_state = -1;
136 next;
137 }
138 }
139 elsif ($deprecated_state == 1)
140 {
141 if ($line =~ /#\s*else\s*\/\/\s*!*defined\(ASIO_NO_DEPRECATED\)/)
142 {
143 $deprecated_state = -1;
144 next;
145 }
146 elsif ($line =~ /#\s*endif\s*\/\/\s*!*defined\(ASIO_NO_DEPRECATED\)/)
147 {
148 $deprecated_state = 0;
149 next;
150 }
151 else
152 {
153 $line =~ s/^# /#/;
154 }
155 }
156 elsif ($deprecated_state == -1)
157 {
158 if ($line =~ /#\s*else\s*\/\/\s*!*defined\(ASIO_NO_DEPRECATED\)/)
159 {
160 $deprecated_state = 1;
161 }
162 elsif ($line =~ /#\s*endif\s*\/\/\s*!*defined\(ASIO_NO_DEPRECATED\)/)
163 {
164 $deprecated_state = -2;
165 }
166 next;
167 }
168 elsif ($deprecated_state == -2)
169 {
170 $deprecated_state = 0;
171 next if ($line eq "");
172 }
173
174 # Strip out code for extensions.
175 if ($extension_state == 0)
176 {
177 if ($line =~ /#\s*if\s*!defined\(ASIO_NO_EXTENSIONS\)\s*$/)
178 {
179 $extension_state = -1;
180 next;
181 }
182 }
183 elsif ($extension_state == -1)
184 {
185 $extension_state = -2 if ($line =~ /#\s*endif\s*\/\/\s*!defined\(ASIO_NO_EXTENSIONS\)\s*$/);
186 next;
187 }
188 elsif ($extension_state == -2)
189 {
190 $extension_state = 0;
191 next if ($line eq "");
192 }
193
194 # Strip out code for old services.
195 if ($old_services_state == 0)
196 {
197 if ($line =~ /#\s*if\s*defined\(ASIO_ENABLE_OLD_SERVICES\)/)
198 {
199 $old_services_state = -1;
200 next;
201 }
202 elsif ($line =~ /#\s*if\s*!defined\(ASIO_ENABLE_OLD_SERVICES\)/)
203 {
204 $old_services_state = 1;
205 next;
206 }
207 }
208 elsif ($old_services_state == 1)
209 {
210 if ($line =~ /#\s*else\s*\/\/\s*!*defined\(ASIO_ENABLE_OLD_SERVICES\)/)
211 {
212 $old_services_state = -1;
213 next;
214 }
215 elsif ($line =~ /#\s*endif\s*\/\/\s*!*defined\(ASIO_ENABLE_OLD_SERVICES\)/)
216 {
217 $old_services_state = 0;
218 next;
219 }
220 else
221 {
222 $line =~ s/^# /#/;
223 }
224 }
225 elsif ($old_services_state == -1)
226 {
227 if ($line =~ /#\s*else\s*\/\/\s*!*defined\(ASIO_ENABLE_OLD_SERVICES\)/)
228 {
229 $old_services_state = 1;
230 }
231 elsif ($line =~ /#\s*endif\s*\/\/\s*!*defined\(ASIO_ENABLE_OLD_SERVICES\)/)
232 {
233 $old_services_state = -2;
234 }
235 next;
236 }
237 elsif ($old_services_state == -2)
238 {
239 $old_services_state = 0;
240 next if ($line eq "");
241 }
242
243 # Keep track of whether we are in an example.
244 if ($code_snippet_state == 0)
245 {
246 if ($line =~ /\@code/)
247 {
248 $code_snippet_state = 1;
249 }
250 }
251
252 # Unconditional replacements.
253 $line =~ s/[\\@]ref boost_bind/std::bind()/g;
254 if ($from =~ /.*\.txt$/)
255 {
256 $line =~ s/[\\@]ref async_read/std::experimental::net::v1::async_read()/g;
257 $line =~ s/[\\@]ref async_write/std::experimental::net::v1::async_write()/g;
258 }
259 if ($line =~ /asio_detail_posix_thread_function/)
260 {
261 $line =~ s/asio_detail_posix_thread_function/networking_ts_detail_posix_thread_function/g;
262 }
263 if ($line =~ /asio_signal_handler/)
264 {
265 $line =~ s/asio_signal_handler/networking_ts_signal_handler/g;
266 }
267 if ($line =~ /ASIO_/ && !($line =~ /NET_TS_/))
268 {
269 $line =~ s/ASIO_/NET_TS_/g;
270 }
271 if ($line =~ /asio_handler_/)
272 {
273 $line =~ s/asio_handler_/networking_ts_handler_/g;
274 }
275 if ($line =~ /asio_true_handler_/)
276 {
277 $line =~ s/asio_true_handler_/networking_ts_true_handler_/g;
278 }
279
280 # Lines skipped in asio/impl/src.hpp.
281 if ($is_impl_src_hpp)
282 {
283 next if
284 (
285 $line =~ /serial_port/
286 or $line =~ /\/local\//
287 or $line =~ /\/generic\//
288 or $line =~ /signal_set/
289 );
290 }
291
292 # Conditional replacements.
293 if ($line =~ /^(.* *)namespace asio \{/)
294 {
295 print_line($output, $1 . "namespace std {", $from, $lineno);
296 print_line($output, $1 . "namespace experimental {", $from, $lineno);
297 print_line($output, $1 . "namespace net {", $from, $lineno);
298 print_line($output, $1 . "inline namespace v1 {", $from, $lineno);
299 }
300 elsif ($line =~ /^(.* *)} \/\/ namespace asio$/)
301 {
302 print_line($output, $1 . "} // inline namespace v1", $from, $lineno);
303 print_line($output, $1 . "} // namespace net", $from, $lineno);
304 print_line($output, $1 . "} // namespace experimental", $from, $lineno);
305 print_line($output, $1 . "} // namespace std", $from, $lineno);
306 }
307 elsif ($line =~ /^(# *include )[<"](asio\.hpp)[>"]$/)
308 {
309 print_line($output, $1 . "<net>", $from, $lineno);
310 if ($uses_asio_thread)
311 {
312 print_line($output, $1 . "<thread>", $from, $lineno) if (!$is_test);
313 $uses_asio_thread = 0;
314 }
315 }
316 elsif ($line =~ /^(# *include )[<"]boost\/.*[>"].*$/)
317 {
318 if (!$includes_asio && $uses_asio_thread)
319 {
320 print_line($output, $1 . "<thread>", $from, $lineno) if (!$is_test);
321 $uses_asio_thread = 0;
322 }
323 print_line($output, $line, $from, $lineno);
324 }
325 elsif ($line =~ /^(# *include )[<"]asio\/thread\.hpp[>"]/)
326 {
327 if ($is_test)
328 {
329 print_line($output, $1 . "<experimental/__net_ts/detail/thread.hpp>", $from, $lineno);
330 }
331 else
332 {
333 # Line is removed.
334 }
335 }
336 elsif ($line =~ /(# *include )[<"]asio\/error_code\.hpp[>"]/)
337 {
338 if ($is_asio_hpp)
339 {
340 # Line is removed.
341 }
342 else
343 {
344 print_line($output, $1 . "<cerrno>", $from, $lineno) if ($is_error_hpp);
345 print_line($output, $1 . "<system_error>", $from, $lineno);
346 }
347 }
348 elsif ($line =~ /# *include [<"]asio\/impl\/error_code\.[hi]pp[>"]/)
349 {
350 # Line is removed.
351 }
352 elsif ($line =~ /(# *include )[<"]asio\/system_error\.hpp[>"]/)
353 {
354 if ($is_asio_hpp)
355 {
356 # Line is removed.
357 }
358 else
359 {
360 print_line($output, $1 . "<system_error>", $from, $lineno);
361 }
362 }
363 elsif ($line =~ /(^.*# *include )[<"]asio\/([^>"]*)[>"](.*)$/)
364 {
365 print_line($output, $1 . "<experimental/__net_ts/" . $2 . ">" . $3, $from, $lineno);
366 }
367 elsif ($line =~ /#.*defined\(.*ASIO_HAS_STD_SYSTEM_ERROR\)$/)
368 {
369 # Line is removed.
370 }
371 elsif ($line =~ /asio::thread\b/)
372 {
373 if ($is_test)
374 {
375 $line =~ s/asio::thread/std::experimental::net::v1::detail::thread/g;
376 }
377 else
378 {
379 $line =~ s/asio::thread/std::thread/g;
380 }
381 print_line($output, $line, $from, $lineno);
382 }
383 elsif ($line =~ /^( *)thread( .*)$/)
384 {
385 if ($is_test)
386 {
387 print_line($output, $1 . "std::experimental::net::v1::detail::thread" . $2, $from, $lineno);
388 }
389 else
390 {
391 print_line($output, $1 . "std::thread" . $2, $from, $lineno);
392 }
393 }
394 elsif ($line =~ /asio::/ && !($line =~ /boost::asio::/))
395 {
396 $line =~ s/asio::error_code/std::error_code/g;
397 $line =~ s/asio::error_category/std::error_category/g;
398 $line =~ s/asio::system_category/std::system_category/g;
399 $line =~ s/asio::system_error/std::system_error/g;
400 $line =~ s/asio::/std::experimental::net::v1::/g if $code_snippet_state == 0;
401 $line =~ s/asio::/std::experimental::net::/g if $code_snippet_state == 1;
402 print_line($output, $line, $from, $lineno);
403 }
404 elsif ($line =~ /using namespace asio/)
405 {
406 $line =~ s/using namespace asio/using namespace std::experimental::net::v1/g;
407 print_line($output, $line, $from, $lineno);
408 }
409 elsif ($line =~ /[\\@]ref boost_bind/)
410 {
411 $line =~ s/[\\@]ref boost_bind/std::bind()/g;
412 print_line($output, $line, $from, $lineno);
413 }
414 elsif ($line =~ /define.*DETAIL_CONFIG_HPP/)
415 {
416 print_line($output, $line, $from, $lineno);
417 print_line($output, "", $from, $lineno);
418 print_line($output, "#define NET_TS_STANDALONE 1", $from, $lineno);
419 }
420 else
421 {
422 print_line($output, $line, $from, $lineno);
423 }
424
425 # Keep track of whether we are in an example.
426 if ($code_snippet_state == 1)
427 {
428 if ($line =~ /\@endcode/)
429 {
430 $code_snippet_state = 0;
431 }
432 }
433
434 ++$lineno;
435 }
436
437 # Ok, we're done.
438 close($input);
439 close($output);
440 }
441
442 sub find_include_files
443 {
444 my @root_includes = (
445 "asio/ts/buffer.hpp",
446 "asio/ts/executor.hpp",
447 "asio/ts/internet.hpp",
448 "asio/ts/io_context.hpp",
449 "asio/ts/net.hpp",
450 "asio/ts/netfwd.hpp",
451 "asio/ts/socket.hpp",
452 "asio/ts/timer.hpp");
453
454 my @excluded_includes = (
455 "asio/basic_deadline_timer.hpp",
456 "asio/basic_streambuf.hpp",
457 "asio/basic_streambuf_fwd.hpp",
458 "asio/datagram_socket_service.hpp",
459 "asio/deadline_timer.hpp",
460 "asio/deadline_timer_service.hpp",
461 "asio/io_context_strand.hpp",
462 "asio/detail/strand_service.hpp",
463 "asio/detail/impl/strand_service.hpp",
464 "asio/detail/impl/strand_service.ipp",
465 "asio/socket_acceptor_service.hpp",
466 "asio/seq_packet_socket_service.hpp",
467 "asio/stream_socket_service.hpp",
468 "asio/time_traits.hpp",
469 "asio/waitable_timer_service.hpp");
470
471 my @include_files = ();
472 my %known_includes = ();
473
474 foreach my $include (@root_includes)
475 {
476 $known_includes{$include} = 1;
477 push(@include_files, "include/" . $include);
478 }
479
480 foreach my $include (@excluded_includes)
481 {
482 $known_includes{$include} = 1;
483 }
484
485 my @unprocessed_includes = sort keys %known_includes;
486 while (scalar(@unprocessed_includes) > 0)
487 {
488 my $include = pop(@unprocessed_includes);
489
490 open(my $input, "<include/$include") or die("Can't open include/$include for reading");
491 while (my $line = <$input>)
492 {
493 chomp($line);
494 if ($line =~ /^\s*#\s*include\s*"(asio\/[^"]*)"/)
495 {
496 if (not defined($known_includes{$1}))
497 {
498 $known_includes{$1} = 1;
499 push(@include_files, "include/" . $1);
500 push(@unprocessed_includes, $1);
501 }
502 }
503 }
504 close($input);
505 }
506
507 return @include_files;
508 }
509
510 sub copy_include_files
511 {
512 my @files = find_include_files();
513 push(@files, "include/asio/impl/src.hpp");
514
515 foreach my $file (@files)
516 {
517 if ($file ne "include/asio/thread.hpp"
518 and $file ne "include/asio/error_code.hpp"
519 and $file ne "include/asio/system_error.hpp"
520 and $file ne "include/asio/impl/error_code.hpp"
521 and $file ne "include/asio/impl/error_code.ipp")
522 {
523 my $from = $file;
524 my $to = $file;
525 $to =~ s/^include\/asio\//$output_dir\/include\/experimental\/__net_ts\//;
526 copy_source_file($from, $to);
527 }
528 }
529 }
530
531 sub create_top_level_includes
532 {
533 my @includes = (
534 "buffer",
535 "executor",
536 "internet",
537 "io_context",
538 "net",
539 "netfwd",
540 "socket",
541 "timer");
542
543 our $output_dir;
544 mkpath("$output_dir/include/experimental");
545 foreach my $include (@includes)
546 {
547 open(my $output, ">$output_dir/include/experimental/$include")
548 or die("Can't open $output_dir/include/experimental/$include for writing");
549
550 my $guard = "NET_TS_" . uc($include) . "_INCLUDED";
551
552 print($output "//\n");
553 print($output "// experimental/$include\n");
554 print($output "// " . '~' x length("experimental/" . $include) . "\n");
555 print($output "//\n");
556 print($output "// Copyright (c) 2003-2018 Christopher M. Kohlhoff (chris at kohlhoff dot com)\n");
557 print($output "//\n");
558 print($output "// Distributed under the Boost Software License, Version 1.0. (See accompanying\n");
559 print($output "// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)\n");
560 print($output "//\n");
561 print($output "\n");
562 print($output "#ifndef $guard\n");
563 print($output "#define $guard\n");
564 print($output "\n");
565 print($output "#include <experimental/__net_ts/ts/$include.hpp>\n");
566 print($output "\n");
567 print($output "#endif // $guard\n");
568
569 close($output);
570 }
571 }
572
573 copy_include_files();
574 create_top_level_includes();