19
19
#include < iostream>
20
20
#include < cstdio>
21
21
#include < optional>
22
+ #include < filesystem>
22
23
23
24
namespace cpp2 {
24
25
@@ -68,6 +69,14 @@ static cmdline_processor::register_flag cmd_noline(
68
69
[]{ flag_clean_cpp1 = true ; }
69
70
);
70
71
72
+ static auto flag_absolute_line_directives = false ;
73
+ static cmdline_processor::register_flag cmd_absolute_line_directives (
74
+ 9 ,
75
+ " absolute-line-directives" ,
76
+ " Emit absolute paths in #line directives" ,
77
+ [] { flag_absolute_line_directives = true ; }
78
+ );
79
+
71
80
static auto flag_import_std = false ;
72
81
static cmdline_processor::register_flag cmd_import_std (
73
82
0 ,
@@ -222,6 +231,11 @@ class positional_printer
222
231
bool printed_extra = false ;
223
232
char last_printed_char = {};
224
233
234
+ struct line_directive_info {
235
+ lineno_t out_pos_line; // output lineno where the line directive was printed
236
+ lineno_t line; // the lineno in the line directive
237
+ } prev_line_directive = {};
238
+
225
239
struct req_act_info {
226
240
colno_t requested;
227
241
colno_t offset;
@@ -401,6 +415,15 @@ class positional_printer
401
415
return ;
402
416
}
403
417
418
+ // Don't print duplicate line directives on subsequent lines
419
+ if (
420
+ prev_line_directive.out_pos_line == curr_pos.lineno
421
+ && prev_line_directive.line == line
422
+ )
423
+ {
424
+ return ;
425
+ }
426
+
404
427
// Otherwise, implement the request
405
428
prev_line_info = { curr_pos.lineno , { } };
406
429
ensure_at_start_of_new_line ();
@@ -411,6 +434,7 @@ class positional_printer
411
434
*out << " #line " << line << " " << std::quoted (cpp2_filename) << " \n " ;
412
435
}
413
436
just_printed_line_directive = true ;
437
+ prev_line_directive = { curr_pos.lineno , line };
414
438
}
415
439
416
440
// Catch up with comment/blank lines
@@ -620,7 +644,9 @@ class positional_printer
620
644
)
621
645
-> void
622
646
{
623
- cpp2_filename = cpp2_filename_;
647
+ cpp2_filename = (flag_absolute_line_directives) ?
648
+ std::filesystem::absolute (std::filesystem::path (cpp2_filename_)).string () :
649
+ cpp2_filename_;
624
650
assert (
625
651
!is_open ()
626
652
&& !pcomments
@@ -5601,6 +5627,12 @@ class cppfront
5601
5627
)
5602
5628
)
5603
5629
{
5630
+ // Print a line directive before every function definition
5631
+ // (needed to enable debugging with lldb).
5632
+ if (n.initializer ) {
5633
+ printer.print_extra (" " );
5634
+ }
5635
+
5604
5636
auto is_streaming_operator = [](std::string_view sv) {
5605
5637
return
5606
5638
sv == " operator<<"
@@ -5932,7 +5964,6 @@ class cppfront
5932
5964
// Else emit the definition
5933
5965
else if (n.initializer )
5934
5966
{
5935
-
5936
5967
if (func->returns .index () == function_type_node::list) {
5937
5968
auto & r = std::get<function_type_node::list>(func->returns );
5938
5969
function_returns.emplace_back (r.get ());
0 commit comments