Skip to content

Commit 777f5b1

Browse files
bluetarpmediazaucy
authored andcommitted
Enable debugging with LLDB (Xcode, Qt Creator, etc) (hsutter#744)
* Print line directives before function definitions Line directive filenames now have absolute paths. These changes are required to enable debugging with LLDB. * Add command line option to enable absolute paths in line directives (defaults to off)
1 parent be0bab7 commit 777f5b1

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

source/cppfront.cpp

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <iostream>
2020
#include <cstdio>
2121
#include <optional>
22+
#include <filesystem>
2223

2324
namespace cpp2 {
2425

@@ -68,6 +69,14 @@ static cmdline_processor::register_flag cmd_noline(
6869
[]{ flag_clean_cpp1 = true; }
6970
);
7071

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+
7180
static auto flag_import_std = false;
7281
static cmdline_processor::register_flag cmd_import_std(
7382
0,
@@ -222,6 +231,11 @@ class positional_printer
222231
bool printed_extra = false;
223232
char last_printed_char = {};
224233

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+
225239
struct req_act_info {
226240
colno_t requested;
227241
colno_t offset;
@@ -401,6 +415,15 @@ class positional_printer
401415
return;
402416
}
403417

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+
404427
// Otherwise, implement the request
405428
prev_line_info = { curr_pos.lineno, { } };
406429
ensure_at_start_of_new_line();
@@ -411,6 +434,7 @@ class positional_printer
411434
*out << "#line " << line << " " << std::quoted(cpp2_filename) << "\n";
412435
}
413436
just_printed_line_directive = true;
437+
prev_line_directive = { curr_pos.lineno, line };
414438
}
415439

416440
// Catch up with comment/blank lines
@@ -620,7 +644,9 @@ class positional_printer
620644
)
621645
-> void
622646
{
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_;
624650
assert(
625651
!is_open()
626652
&& !pcomments
@@ -5601,6 +5627,12 @@ class cppfront
56015627
)
56025628
)
56035629
{
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+
56045636
auto is_streaming_operator = [](std::string_view sv) {
56055637
return
56065638
sv == "operator<<"
@@ -5932,7 +5964,6 @@ class cppfront
59325964
// Else emit the definition
59335965
else if (n.initializer)
59345966
{
5935-
59365967
if (func->returns.index() == function_type_node::list) {
59375968
auto& r = std::get<function_type_node::list>(func->returns);
59385969
function_returns.emplace_back(r.get());

0 commit comments

Comments
 (0)