Skip to content

Commit 0d1544b

Browse files
committed
Get rid of a few C-style casts that crept in, be -Wold-style-cast clean, closes #287
1 parent fda45aa commit 0d1544b

8 files changed

+69
-18
lines changed

include/cpp2util.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1433,7 +1433,7 @@ inline auto to_string(std::tuple<Ts...> const& t) -> std::string
14331433
//
14341434
struct args_t : std::vector<std::string_view>
14351435
{
1436-
args_t(int c, char const* const* v) : vector{(size_t)c}, argc{c}, argv{v} {}
1436+
args_t(int c, char const* const* v) : vector{static_cast<size_t>(c)}, argc{c}, argv{v} {}
14371437

14381438
int argc = 0;
14391439
char const* const* argv = nullptr;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
this is a string
2+
raw string without interpolation
3+
this is raw string literal
4+
5+
that can last for multiple
6+
7+
lines
8+
this is raw string literal
9+
that can last for multiple
10+
lines
11+
42 R"(this can be added too)"
12+
calculations like m["one"] + m["two"] = 3 also works
13+
at the beginning of the line!!!
14+
15+
16+
4242
17+
1.2.0.42

regression-tests/test-results/gcc-10/pure2-raw-string-literal-and-interpolation.cpp.output

Whitespace-only changes.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
this is a string
2+
raw string without interpolation
3+
this is raw string literal
4+
5+
that can last for multiple
6+
7+
lines
8+
this is raw string literal
9+
that can last for multiple
10+
lines
11+
42 R"(this can be added too)"
12+
calculations like m["one"] + m["two"] = 3 also works
13+
at the beginning of the line!!!
14+
15+
16+
4242
17+
1.2.0.42
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pure2-raw-string-literal-and-interpolation.cpp

source/common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ auto to_upper(char c)
449449
// C toupper is only not-UB in [0,127] and returns the wrong type,
450450
// so wrap the range check and the type cast here in one place...
451451
// note the 126 (not 127) is intentional to avoid a GCC warning
452-
if (0 <= c && c <= 126) { return (char)std::toupper(c); }
452+
if (0 <= c && c <= 126) { return static_cast<char>(std::toupper(c)); }
453453
// else
454454
return c;
455455
}

source/cppfront.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,20 @@ class positional_printer
221221
bool enable_indent_heuristic = true;
222222

223223
// Modal information
224-
bool declarations_only = true; // print only declarations in the first pass
224+
enum phases {
225+
phase0_type_decls = 0,
226+
phase1_type_defs_func_decls = 1,
227+
phase2_func_defs = 2
228+
};
229+
phases phase = phase1_type_defs_func_decls;
230+
231+
auto inc_phase() -> void {
232+
switch (phase) {
233+
break;case phase0_type_decls : phase = phase1_type_defs_func_decls;
234+
break;case phase1_type_defs_func_decls: phase = phase2_func_defs;
235+
break;default : assert(!"ICE: invalid lowering phase");
236+
}
237+
}
225238

226239
std::vector<std::string*> emit_string_targets; // option to emit to string instead of out file
227240
std::vector<std::vector<text_with_pos>*> emit_text_chunks_targets; // similar for vector<text_pos>
@@ -334,7 +347,7 @@ class positional_printer
334347
auto& comments = *pcomments;
335348

336349
// Don't emit comments while in the first declarations-only pass
337-
if (declarations_only) {
350+
if (phase < phase2_func_defs) {
338351
return;
339352
}
340353

@@ -565,7 +578,7 @@ class positional_printer
565578
// of the Cpp2 section, and not printed in thedeclarations-only pass
566579
if (
567580
!last_was_cpp2
568-
&& declarations_only
581+
&& phase < phase2_func_defs
569582
)
570583
{
571584
print ("\n");
@@ -755,19 +768,16 @@ class positional_printer
755768
// Modal state control functions
756769
//
757770

758-
// In the first pass we will print only declarations (the default)
759-
// For the second pass this function enables printing definitions
760-
//
761-
auto enable_definitions()
771+
auto next_phase()
762772
-> void
763773
{
764-
declarations_only = false;
774+
inc_phase();
765775
}
766776

767777
auto doing_declarations_only() const
768778
-> bool
769779
{
770-
return declarations_only;
780+
return phase < phase2_func_defs;
771781
}
772782

773783
// Provide an option to store to a given string instead, which is
@@ -1213,7 +1223,7 @@ class cppfront
12131223
//
12141224
printer.print_extra( hpp_includes );
12151225

1216-
printer.enable_definitions();
1226+
printer.next_phase();
12171227

12181228
for (auto& section : tokens.get_map())
12191229
{
@@ -3128,7 +3138,7 @@ class cppfront
31283138
&& rhs_tok
31293139
&& (
31303140
*rhs_tok == "nullptr"
3131-
|| is_digit(((std::string_view)*rhs_tok)[0])
3141+
|| is_digit((rhs_tok->as_string_view())[0])
31323142
)
31333143
)
31343144
{
@@ -4665,7 +4675,7 @@ class cppfront
46654675
loc += (">");
46664676
}
46674677
loc += " ";
4668-
loc += ((std::string_view)*decl.name());
4678+
loc += decl.name()->as_string_view();
46694679
if (decl.initializer)
46704680
{
46714681
std::string init;

source/lex.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,10 +243,16 @@ class token
243243
{
244244
}
245245

246-
operator std::string_view() const
246+
auto as_string_view() const
247+
-> std::string_view
247248
{
248249
assert (start);
249-
return {start, (unsigned)count};
250+
return {start, static_cast<unsigned>(count)};
251+
}
252+
253+
operator std::string_view() const
254+
{
255+
return as_string_view();
250256
}
251257

252258
auto operator== (token const& t) const
@@ -264,7 +270,7 @@ class token
264270
auto to_string( bool text_only = false ) const
265271
-> std::string
266272
{
267-
auto text = std::string{start, (unsigned)count};
273+
auto text = std::string{start, static_cast<unsigned>(count)};
268274
if (text_only) {
269275
return text;
270276
}
@@ -897,7 +903,7 @@ auto lex_line(
897903
|| !is_identifier_continue(line[i+m[0].length()]) // non-identifier char
898904
)
899905
{
900-
return (int)(m[0].length());
906+
return static_cast<int>(m[0].length());
901907
}
902908
}
903909
return 0;

0 commit comments

Comments
 (0)