Skip to content

Enable debugging with LLDB (Xcode, Qt Creator, etc) #744

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <iostream>
#include <cstdio>
#include <optional>
#include <filesystem>

namespace cpp2 {

Expand Down Expand Up @@ -68,6 +69,14 @@ static cmdline_processor::register_flag cmd_noline(
[]{ flag_clean_cpp1 = true; }
);

static auto flag_absolute_line_directives = false;
static cmdline_processor::register_flag cmd_absolute_line_directives(
9,
"absolute-line-directives",
"Emit absolute paths in #line directives",
[] { flag_absolute_line_directives = true; }
);

static auto flag_import_std = false;
static cmdline_processor::register_flag cmd_import_std(
0,
Expand Down Expand Up @@ -222,6 +231,11 @@ class positional_printer
bool printed_extra = false;
char last_printed_char = {};

struct line_directive_info {
lineno_t out_pos_line; // output lineno where the line directive was printed
lineno_t line; // the lineno in the line directive
} prev_line_directive = {};

struct req_act_info {
colno_t requested;
colno_t offset;
Expand Down Expand Up @@ -401,6 +415,15 @@ class positional_printer
return;
}

// Don't print duplicate line directives on subsequent lines
if (
prev_line_directive.out_pos_line == curr_pos.lineno
&& prev_line_directive.line == line
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have an example of what was being output that is being suppressed? I know that there are some places where we need to output the same directive multiple times due to intermediate code. It's not 100% clear to me that this will only eliminate truly duplicate line numbers.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That code prevents duplicate line directives like this:

#line 11 "C:\\path\\main.cpp2"
#line 11 "C:\\path\\main.cpp2"
auto main() -> int{
 print_menu(2023);
}

But it still allows the same line directive line number to be reused elsewhere, just not immediately on subsequent lines.

This is still allowed:

#line 11 "C:\\path\\main.cpp2"
auto main() -> int;

//=== Cpp2 function definitions =================================================

#line 11 "C:\\path\\main.cpp2"
auto main() -> int{
 print_menu(2023);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks for the confirmation.

)
{
return;
}

// Otherwise, implement the request
prev_line_info = { curr_pos.lineno, { } };
ensure_at_start_of_new_line();
Expand All @@ -411,6 +434,7 @@ class positional_printer
*out << "#line " << line << " " << std::quoted(cpp2_filename) << "\n";
}
just_printed_line_directive = true;
prev_line_directive = { curr_pos.lineno, line };
}

// Catch up with comment/blank lines
Expand Down Expand Up @@ -620,7 +644,9 @@ class positional_printer
)
-> void
{
cpp2_filename = cpp2_filename_;
cpp2_filename = (flag_absolute_line_directives) ?
std::filesystem::absolute(std::filesystem::path(cpp2_filename_)).string() :
cpp2_filename_;
assert(
!is_open()
&& !pcomments
Expand Down Expand Up @@ -5598,6 +5624,12 @@ class cppfront
)
)
{
// Print a line directive before every function definition
// (needed to enable debugging with lldb).
if (n.initializer) {
printer.print_extra("");
}

auto is_streaming_operator = [](std::string_view sv) {
return
sv == "operator<<"
Expand Down Expand Up @@ -5929,7 +5961,6 @@ class cppfront
// Else emit the definition
else if (n.initializer)
{

if (func->returns.index() == function_type_node::list) {
auto& r = std::get<function_type_node::list>(func->returns);
function_returns.emplace_back(r.get());
Expand Down