Skip to content

Commit 3a7e1df

Browse files
committed
Update the code after code review comments.
Add missing include of `<optional>`, Add `struct text_chunks_with_parens_position`, Add asserts for pointer that are used (i->op, i->op_close), Add assertion for having '>' as a last character of template function, Use open_pos and close_pos for printing '(' and ')',
1 parent c6bf645 commit 3a7e1df

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

source/cppfront.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "sema.h"
1919
#include <iostream>
2020
#include <cstdio>
21-
21+
#include <optional>
2222

2323
namespace cpp2 {
2424

@@ -1734,7 +1734,13 @@ class cppfront
17341734
auto last_was_prefixed = false;
17351735
auto saw_dollar = false;
17361736

1737-
auto args = std::optional<std::vector<text_with_pos>>{};
1737+
struct text_chunks_with_parens_position {
1738+
std::vector<text_with_pos> text_chunks;
1739+
cpp2::source_position open_pos;
1740+
cpp2::source_position close_pos;
1741+
};
1742+
1743+
auto args = std::optional<text_chunks_with_parens_position>{};
17381744

17391745
auto print_to_string = [&](auto& i, auto... args) {
17401746
auto print = std::string{};
@@ -1780,12 +1786,15 @@ class cppfront
17801786
// expr_list is emited to args variable for future use
17811787
if (i->op->type() == lexeme::LeftParen) {
17821788

1783-
args.emplace();
1789+
assert(i->op);
1790+
assert(i->op_close);
1791+
auto local_args = text_chunks_with_parens_position{{}, i->op->position(), i->op_close->position()};
17841792

17851793
if (!i->expr_list->expressions.empty()) {
1786-
args.emplace(print_to_text_chunks(*i->expr_list));
1794+
local_args.text_chunks = print_to_text_chunks(*i->expr_list);
17871795
}
17881796

1797+
args.emplace(std::move(local_args));
17891798
}
17901799
// Going backwards if we found Dot and there is args variable
17911800
// it means that it should be handled by UFCS
@@ -1809,17 +1818,18 @@ class cppfront
18091818
// from obj.fun<int, long, double>(1,2) this CPP2_UFCS_TEMPLATE(fun, (<int,long, double>), obj, 1, 2)
18101819
auto split = funcname.find('<'); assert(split != std::string::npos);
18111820
funcname.insert(split, ", (");
1821+
assert(funcname.back() == '>');
18121822
funcname += ')';
18131823
}
18141824
// If there are no additional arguments, use the _0 version
1815-
if (args.value().empty()) {
1825+
if (args.value().text_chunks.empty()) {
18161826
ufcs_string += "_0";
18171827
}
18181828

18191829
prefix.emplace_back(ufcs_string + "(" + funcname + ", ", i->op->position() );
1820-
suffix.emplace_back(")", i->op->position() );
1821-
if (!args.value().empty()) {
1822-
for (auto&& e: args.value()) {
1830+
suffix.emplace_back(")", args.value().close_pos );
1831+
if (!args.value().text_chunks.empty()) {
1832+
for (auto&& e: args.value().text_chunks) {
18231833
suffix.push_back(e);
18241834
}
18251835
suffix.emplace_back(", ", i->op->position());
@@ -1876,12 +1886,12 @@ class cppfront
18761886

18771887
if (args) {
18781888
// if args are stored it means that this is function or method
1879-
// that is not handled by UFCS e.g. that has more than one template argument
1880-
suffix.emplace_back(")", n.position());
1881-
for (auto&& e: args.value()) {
1889+
// that is not handled by UFCS and args need to be printed
1890+
suffix.emplace_back(")", args.value().close_pos);
1891+
for (auto&& e: args.value().text_chunks) {
18821892
suffix.push_back(e);
18831893
}
1884-
suffix.emplace_back("(", n.position());
1894+
suffix.emplace_back("(", args.value().open_pos);
18851895
args.reset();
18861896
}
18871897

@@ -1937,11 +1947,11 @@ class cppfront
19371947
// if after printing core expression args is defined
19381948
// it means that the chaining started by function call
19391949
// we need to print its arguments
1940-
suffix.emplace_back(")", n.position());
1941-
for (auto&& e: args.value()) {
1950+
suffix.emplace_back(")", args.value().close_pos);
1951+
for (auto&& e: args.value().text_chunks) {
19421952
suffix.push_back(e);
19431953
}
1944-
suffix.emplace_back("(", n.position());
1954+
suffix.emplace_back("(", args.value().open_pos);
19451955
args.reset();
19461956
}
19471957

0 commit comments

Comments
 (0)