Skip to content

Commit 2038591

Browse files
committed
Squashed commit of the following:
commit 5e4c0ad82fd5adbfe4d6b7663569f7986e43736d Author: Herb Sutter <[email protected]> Date: Wed Jan 11 15:12:57 2023 -0800 Expand string literals just-in-time before their line is tokenized commit a2dc17576435c04d6cc508dd0cb31e0bf10c3af1 Author: Herb Sutter <[email protected]> Date: Wed Jan 11 10:28:01 2023 -0800 Move the string literal expansion into the lexer
1 parent 1269838 commit 2038591

File tree

6 files changed

+184
-165
lines changed

6 files changed

+184
-165
lines changed

regression-tests/test-results/mixed-inspect-templates.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ struct my_type {};
4848
std::cout << "inspected var : " + cpp2::to_string(fun(var)) << std::endl;
4949
std::cout << "inspected myt : " + cpp2::to_string(fun(myt)) << std::endl;
5050

51-
std::cout << "inspected vec : " + cpp2::to_string(fun2(vec)) << std::endl;
52-
std::cout << "inspected arr : " + cpp2::to_string(fun2(arr)) << std::endl;
53-
std::cout << "inspected var : " + cpp2::to_string(fun2(var)) << std::endl;
54-
std::cout << "inspected myt : " + cpp2::to_string(fun2(myt)) << std::endl;
51+
std::cout << "inspected vec : " + cpp2::to_string(fun2(std::move(vec))) << std::endl;
52+
std::cout << "inspected arr : " + cpp2::to_string(fun2(std::move(arr))) << std::endl;
53+
std::cout << "inspected var : " + cpp2::to_string(fun2(std::move(var))) << std::endl;
54+
std::cout << "inspected myt : " + cpp2::to_string(fun2(std::move(myt))) << std::endl;
5555
}
5656

regression-tests/test-results/mixed-string-interpolation.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,43 +21,43 @@ struct custom_struct_with_no_stringize_customization { } custom;
2121
std::cout << "a = " + cpp2::to_string(a) + ", b = " + cpp2::to_string(b) + "\n";
2222

2323
b = 42;
24-
std::cout << "a^2 + b = " + cpp2::to_string(a * a + b.value()) + "\n";
24+
std::cout << "a^2 + b = " + cpp2::to_string(a * std::move(a) + CPP2_UFCS_0(value, std::move(b))) + "\n";
2525

2626
std::string_view sv {"my string_view"};
27-
std::cout << "sv = " + cpp2::to_string(sv) + "\n";
27+
std::cout << "sv = " + cpp2::to_string(std::move(sv)) + "\n";
2828

2929
std::optional<std::string_view> osv {};
3030
std::cout << "osv = " + cpp2::to_string(osv) + "\n";
3131
osv = "string literal bound to optional string_view";
32-
std::cout << "osv = " + cpp2::to_string(osv) + "\n";
32+
std::cout << "osv = " + cpp2::to_string(std::move(osv)) + "\n";
3333

3434
std::variant<std::monostate,std::string,double> var {};
3535
std::cout << "var = " + cpp2::to_string(var) + "\n";
3636
var = "abracadabra";
3737
std::cout << "var = " + cpp2::to_string(var) + "\n";
3838
var = 2.71828;
39-
std::cout << "var = " + cpp2::to_string(var) + "\n";
39+
std::cout << "var = " + cpp2::to_string(std::move(var)) + "\n";
4040

4141
std::pair<int,double> mypair {12, 3.4};
42-
std::cout << "mypair = " + cpp2::to_string(mypair) + "\n";
42+
std::cout << "mypair = " + cpp2::to_string(std::move(mypair)) + "\n";
4343

4444
std::tuple<int> tup1 {12};
4545
std::tuple<int,double> tup2 {12, 3.4};
4646
std::tuple<int,double,std::string> tup3 {12, 3.4, "456"};
47-
std::cout << "tup1 = " + cpp2::to_string(tup1) + "\n";
48-
std::cout << "tup2 = " + cpp2::to_string(tup2) + "\n";
49-
std::cout << "tup3 = " + cpp2::to_string(tup3) + "\n";
47+
std::cout << "tup1 = " + cpp2::to_string(std::move(tup1)) + "\n";
48+
std::cout << "tup2 = " + cpp2::to_string(std::move(tup2)) + "\n";
49+
std::cout << "tup3 = " + cpp2::to_string(std::move(tup3)) + "\n";
5050

5151
std::pair<std::string_view,std::optional<std::string>> p {"first", std::nullopt};
52-
std::cout << "p = " + cpp2::to_string(p) + "\n";
52+
std::cout << "p = " + cpp2::to_string(std::move(p)) + "\n";
5353

5454
std::tuple<double,std::optional<std::pair<std::string_view,int>>,std::optional<std::tuple<int,int,int>>> t {3.14, std::nullopt, std::nullopt};
55-
std::cout << "t = " + cpp2::to_string(t) + "\n";
55+
std::cout << "t = " + cpp2::to_string(std::move(t)) + "\n";
5656

5757
std::variant<int,std::string,std::pair<int,double>> vv {};
5858
std::cout << "vv = " + cpp2::to_string(vv) + "\n";
5959
vv = std::make_pair(1, 2.3);
60-
std::cout << "vv = " + cpp2::to_string(vv) + "\n";
60+
std::cout << "vv = " + cpp2::to_string(std::move(vv)) + "\n";
6161

6262
std::cout << "custom = " + cpp2::to_string(custom) + "\n";
6363
}

regression-tests/test-results/pure2-interpolation.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212

1313
[[nodiscard]] auto main() -> int{
1414
auto x {0};
15-
std::cout << "g" + cpp2::to_string(x) + "g" + cpp2::to_string(x) + "g" << "\n";
16-
std::cout << cpp2::to_string(x) + "g" + cpp2::to_string(x) + "g" << "\n";
17-
std::cout << cpp2::to_string(x) + "g" + cpp2::to_string(x) << "\n";
18-
std::cout << cpp2::to_string(x) + cpp2::to_string(x) << "\n";
19-
std::cout << "\"" + cpp2::to_string(x) + "\"" << "\n";
20-
std::cout << "\"" + cpp2::to_string(x) << "\n";
15+
std::cout << "g" + cpp2::to_string(x) + "g" + cpp2::to_string(x) + "g" << "\n";
16+
std::cout << cpp2::to_string(x) + "g" + cpp2::to_string(x) + "g" << "\n";
17+
std::cout << cpp2::to_string(x) + "g" + cpp2::to_string(x) << "\n";
18+
std::cout << cpp2::to_string(x) + cpp2::to_string(x) << "\n";
19+
std::cout << "\"" + cpp2::to_string(x) + "\"" << "\n";
20+
std::cout << "\"" + cpp2::to_string(x) << "\n";
2121
std::cout << "\"" << "\n";
2222
std::cout << "" << "\n";
2323
std::cout << "pl(ug$h" << "\n";
24-
std::cout << cpp2::to_string(x) + "pl(ug$h" << "\n";
24+
std::cout << cpp2::to_string(std::move(x)) + "pl(ug$h" << "\n";
2525
}
2626

source/cppfront.cpp

Lines changed: 3 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,135 +1001,6 @@ class cppfront
10011001
}
10021002

10031003

1004-
//-----------------------------------------------------------------------
1005-
//
1006-
// A StringLiteral could include captures
1007-
//
1008-
auto expand_string_literal(token const& n) -> std::string
1009-
{
1010-
assert(n.type() == lexeme::StringLiteral);
1011-
1012-
auto text = std::string_view(n);
1013-
auto length = n.length();
1014-
assert (std::ssize(text) == length);
1015-
1016-
assert(length >= 2);
1017-
assert(text.back() == '"');
1018-
1019-
auto pos = 0;
1020-
auto ret = std::string{}; // the return string we're going to build
1021-
1022-
// Skip prefix to first non-" character
1023-
while (pos < length && text[pos] != '"') {
1024-
++pos;
1025-
}
1026-
assert(pos < length && text[pos] == '"');
1027-
++pos;
1028-
auto current_start = pos; // the current offset before which the string has been into ret
1029-
auto first = true;
1030-
1031-
auto add_plus = [&]{
1032-
if (!first) {
1033-
ret += " + ";
1034-
}
1035-
first = false;
1036-
};
1037-
1038-
// Now we're on the first character of the string itself
1039-
for ( ; pos < length && (text[pos] != '"' || text[pos-1] == '\\'); ++pos )
1040-
{
1041-
// Find the next )$
1042-
if (text[pos] == '$' && text[pos-1] == ')')
1043-
{
1044-
// Scan back to find the matching (
1045-
auto paren_depth = 1;
1046-
auto open = pos - 2;
1047-
1048-
// "next" in the string is the "last" one encountered in the backwards scan
1049-
auto last_nonwhitespace = '\0';
1050-
1051-
for( ; text[open] != '"' || (open > current_start && text[open-1] != '\\'); --open)
1052-
{
1053-
if (text[open] == ')') {
1054-
++paren_depth;
1055-
}
1056-
else if (text[open] == '(') {
1057-
--paren_depth;
1058-
if (paren_depth == 0) {
1059-
break;
1060-
}
1061-
}
1062-
else if (
1063-
(text[open] == '+' && text[open - 1] == '+') ||
1064-
(text[open] == '-' && text[open - 1] == '-')
1065-
)
1066-
{
1067-
errors.emplace_back(
1068-
source_position( n.position().lineno, n.position().colno + pos ),
1069-
"a string interpolation expression may not contain ++ or --"
1070-
);
1071-
return {};
1072-
}
1073-
else if (
1074-
(text[open] == '*' || text[open] == '&' || text[open] == '~') &&
1075-
!isspace(text[open - 1]) &&
1076-
!isalnum(last_nonwhitespace) && last_nonwhitespace != '('
1077-
)
1078-
{
1079-
errors.emplace_back(
1080-
source_position( n.position().lineno, n.position().colno + pos ),
1081-
"a string interpolation expression may not contain unary & * or ~"
1082-
);
1083-
return {};
1084-
}
1085-
1086-
if (!std::isspace(text[open])) {
1087-
last_nonwhitespace = text[open];
1088-
}
1089-
}
1090-
if (text[open] == '"')
1091-
{
1092-
errors.emplace_back(
1093-
source_position( n.position().lineno, n.position().colno + pos ),
1094-
"no matching ( for string interpolation ending in )$"
1095-
);
1096-
return {};
1097-
}
1098-
assert (text[open] == '(');
1099-
1100-
// 'open' is now at the matching (
1101-
1102-
// Put the next non-empty non-interpolated chunk straight into ret
1103-
if (open != current_start) {
1104-
add_plus();
1105-
ret += '"';
1106-
ret += text.substr(current_start, open - current_start);
1107-
ret += '"';
1108-
}
1109-
1110-
// Then put interpolated chunk into ret
1111-
add_plus();
1112-
ret += "cpp2::to_string";
1113-
ret += text.substr(open, pos - open);
1114-
1115-
current_start = pos+1;
1116-
}
1117-
}
1118-
1119-
// Now we should be on the the final " closing the string
1120-
assert(pos == length-1 && text[pos] == '"');
1121-
1122-
// Put the final non-interpolated chunk straight into ret
1123-
if (first || current_start < std::ssize(text)-1) {
1124-
add_plus();
1125-
ret += '"';
1126-
ret += text.substr(current_start);
1127-
}
1128-
1129-
return ret;
1130-
}
1131-
1132-
11331004
//-----------------------------------------------------------------------
11341005
//
11351006
auto emit(token const& n, bool is_qualified = false, source_position pos = {}) -> void
@@ -1147,9 +1018,9 @@ class cppfront
11471018
if (n == "new") {
11481019
printer.print_cpp2("cpp2_new", pos);
11491020
}
1150-
else if (n.type() == lexeme::StringLiteral) {
1151-
printer.print_cpp2( expand_string_literal(n), pos );
1152-
}
1021+
//else if (n.type() == lexeme::StringLiteral) {
1022+
// printer.print_cpp2( expand_string_literal(n, errors), pos );
1023+
//}
11531024
else {
11541025
printer.print_cpp2(n, pos);
11551026
}

0 commit comments

Comments
 (0)