Skip to content

Commit aaab6af

Browse files
committed
2 parents 7fe06da + 6ff481d commit aaab6af

File tree

7 files changed

+39
-35
lines changed

7 files changed

+39
-35
lines changed

.github/workflows/build-cppfront.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99
- name: Compiler name & version
1010
run: cl.exe
1111
- name: Build
12-
run: cl.exe source/cppfront.cpp -std:c++latest -MD -EHsc -experimental:module -W4
12+
run: cl.exe source/cppfront.cpp -std:c++latest -MD -EHsc -experimental:module -W4 -WX
1313
build-unix-like:
1414
strategy:
1515
fail-fast: false
@@ -36,7 +36,7 @@ jobs:
3636
runs-on: ${{ matrix.runs-on }}
3737
env:
3838
CXX: ${{ matrix.compiler }}
39-
CXXFLAGS: -std=${{ matrix.cxx-std }} -Wall -Wextra -Wold-style-cast -pthread
39+
CXXFLAGS: -std=${{ matrix.cxx-std }} -Wall -Wextra -Wold-style-cast -Wpedantic -Werror -pthread
4040
steps:
4141
- uses: actions/checkout@v3
4242
- name: Install compiler

source/common.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ struct source_line
6565
-> int
6666
{
6767
return
68-
std::find_if_not( text.begin(), text.end(), &isspace )
69-
- text.begin();
68+
unsafe_narrow<int>(std::find_if_not( text.begin(), text.end(), &isspace )
69+
- text.begin());
7070
}
7171

7272
auto prefix() const
@@ -334,7 +334,7 @@ auto is_nondigit(char c)
334334
isalpha(c)
335335
|| c == '_'
336336
;
337-
};
337+
}
338338

339339
//G identifier-start:
340340
//G nondigit
@@ -379,7 +379,7 @@ auto starts_with_identifier(std::string_view s)
379379
return j;
380380
}
381381
return 0;
382-
};
382+
}
383383

384384

385385
// Helper to allow one of the above or a digit separator
@@ -759,7 +759,7 @@ class cmdline_processor
759759
auto length = std::ssize(name);
760760
if (opt_out) { length += 3; } // space to print "[-]"
761761
if (max_flag_length < length) {
762-
max_flag_length = length;
762+
max_flag_length = unsafe_narrow<int>(length);
763763
}
764764
}
765765
struct register_flag {

source/io.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ class braces_tracker
457457
}
458458

459459
auto current_depth() const -> int {
460-
return std::ssize(open_braces);
460+
return unsafe_narrow<int>(std::ssize(open_braces));
461461
}
462462

463463
// --- Preprocessor matching functions - #if/#else/#endif
@@ -610,7 +610,7 @@ auto process_cpp_line(
610610
return r;
611611
}
612612
in_raw_string_literal = false;
613-
i = end_pos+raw_string_closing_seq.size()-1;
613+
i = unsafe_narrow<int>(end_pos+raw_string_closing_seq.size()-1);
614614
}
615615
else {
616616
r.all_comment_line = false;
@@ -797,7 +797,7 @@ auto process_cpp2_line(
797797

798798
if (in_char_literal) {
799799
errors.emplace_back(
800-
source_position(lineno, ssize(line)),
800+
source_position(lineno, unsafe_narrow<colno_t>(ssize(line))),
801801
std::string("line ended before character literal was terminated")
802802
);
803803
}
@@ -957,7 +957,7 @@ class source
957957
lines.back().text,
958958
in_comment,
959959
braces,
960-
std::ssize(lines)-1,
960+
unsafe_narrow<lineno_t>(std::ssize(lines)-1),
961961
errors
962962
)
963963
&& in.getline(&buf[0], max_line_len)
@@ -982,7 +982,7 @@ class source
982982
in_raw_string_literal,
983983
raw_string_closing_seq,
984984
braces,
985-
std::ssize(lines) - 1
985+
unsafe_narrow<lineno_t>(std::ssize(lines)-1)
986986
);
987987
if (stats.all_comment_line) {
988988
lines.back().cat = source_line::category::comment;

source/lex.h

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ auto _as(lexeme l)
199199
break;case lexeme::None: return "(NONE)";
200200
break;default: return "INTERNAL-ERROR";
201201
}
202-
};
202+
}
203203

204204

205205
auto is_operator(lexeme l)
@@ -286,13 +286,13 @@ class token
286286
pos.colno += offset;
287287
}
288288

289-
auto position() const -> source_position { return pos; }
289+
auto position() const -> source_position { return pos; }
290290

291-
auto length () const -> int { return sv.size(); }
291+
auto length () const -> int { return unsafe_narrow<int>(sv.size()); }
292292

293-
auto type () const -> lexeme { return lex_type; }
293+
auto type () const -> lexeme { return lex_type; }
294294

295-
auto set_type(lexeme l) -> void { lex_type = l; }
295+
auto set_type(lexeme l) -> void { lex_type = l; }
296296

297297
auto visit(auto& v, int depth) const
298298
-> void
@@ -307,7 +307,7 @@ class token
307307
)
308308
{
309309
sv.remove_prefix(prefix.size());
310-
pos.colno += prefix.size();
310+
pos.colno += unsafe_narrow<colno_t>(prefix.size());
311311
}
312312
}
313313

@@ -816,7 +816,7 @@ auto lex_line(
816816
source_position(lineno, i + 1),
817817
type
818818
});
819-
i += num-1;
819+
i += unsafe_narrow<int>(num-1);
820820

821821
merge_cpp1_multi_token_fundamental_type_names();
822822
merge_operator_function_names();
@@ -1066,7 +1066,7 @@ auto lex_line(
10661066
auto parts = expand_raw_string_literal(opening_seq, closing_seq, closing_strategy, part, errors, source_position(lineno, pos_to_replace + 1));
10671067
auto new_part = parts.generate();
10681068
mutable_line.replace( pos_to_replace, size_to_replace, new_part );
1069-
i += std::ssize(new_part)-1;
1069+
i += unsafe_narrow<colno_t>(std::ssize(new_part)-1);
10701070

10711071
if (parts.is_expanded()) {
10721072
// raw string was expanded and we need to repeat the processing of this line
@@ -1147,7 +1147,7 @@ auto lex_line(
11471147
auto closing_strategy = end_pos == line.npos ? string_parts::no_ends : string_parts::on_the_end;
11481148
auto size_to_replace = end_pos == line.npos ? std::ssize(line) - i : end_pos - i + std::ssize(rsm.closing_seq);
11491149

1150-
if (interpolate_raw_string(rsm.opening_seq, rsm.closing_seq, closing_strategy, part, i, size_to_replace ) ) {
1150+
if (interpolate_raw_string(rsm.opening_seq, rsm.closing_seq, closing_strategy, part, i, unsafe_narrow<int>(size_to_replace) ) ) {
11511151
continue;
11521152
}
11531153
}
@@ -1164,7 +1164,7 @@ auto lex_line(
11641164
raw_string_multiline.value().text += raw_string_multiline.value().closing_seq;
11651165

11661166
// and position where multiline_raw_string ends (needed for reseting line parsing)
1167-
i = end_pos+std::ssize(raw_string_multiline.value().closing_seq)-1;
1167+
i = unsafe_narrow<colno_t>(end_pos+std::ssize(raw_string_multiline.value().closing_seq)-1);
11681168

11691169
const auto& text = raw_string_multiline.value().should_interpolate ? raw_string_multiline.value().text.substr(1) : raw_string_multiline.value().text;
11701170
multiline_raw_strings.emplace_back(multiline_raw_string{ text, {lineno, i} });
@@ -1379,7 +1379,9 @@ auto lex_line(
13791379
opening_seq,
13801380
closing_seq,
13811381
string_parts::on_both_ends,
1382-
std::string_view(&line[paren_pos+1], closing_pos-paren_pos-1), i, closing_pos-i+std::ssize(closing_seq))
1382+
std::string_view(&line[paren_pos+1], closing_pos-paren_pos-1),
1383+
i,
1384+
unsafe_narrow<int>(closing_pos-i+std::ssize(closing_seq)))
13831385
) {
13841386
continue;
13851387
}
@@ -1397,12 +1399,14 @@ auto lex_line(
13971399
opening_seq,
13981400
closing_seq,
13991401
string_parts::on_the_beginning,
1400-
std::string_view(&line[paren_pos+1], std::ssize(line)-(paren_pos+1)), i, std::ssize(line)-i)
1402+
std::string_view(&line[paren_pos+1], std::ssize(line)-(paren_pos+1)),
1403+
i,
1404+
unsafe_narrow<int>(std::ssize(line)-i))
14011405
) {
14021406
continue;
14031407
}
14041408
// skip entire raw string opening sequence R"
1405-
i = paren_pos;
1409+
i = unsafe_narrow<int>(paren_pos);
14061410

14071411
// if we are on the end of the line we need to add new line char
14081412
if (i+1 == std::ssize(line)) {
@@ -1594,7 +1598,7 @@ auto lex_line(
15941598
} else {
15951599
raw_string_multiline.emplace(raw_string{source_position{lineno, i}, opening_seq, opening_seq, closing_seq });
15961600
// skip entire raw string opening sequence R"
1597-
i = paren_pos;
1601+
i = unsafe_narrow<int>(paren_pos);
15981602

15991603
// if we are on the end of the line we need to add new line char
16001604
if (i+1 == std::ssize(line)) {
@@ -1842,7 +1846,7 @@ class tokens
18421846

18431847
// Create new map entry for the section starting at this line,
18441848
// and populate its tokens with the tokens in this section
1845-
auto lineno = std::distance(std::begin(lines), line);
1849+
auto lineno = unsafe_narrow<lineno_t>(std::distance(std::begin(lines), line));
18461850

18471851
// If this is generated code, use negative line numbers to
18481852
// inform and assist the printer

source/parse.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,7 @@ struct binary_expression_node
322322
auto terms_size() const
323323
-> int
324324
{
325-
return std::ssize(terms);
325+
return unsafe_narrow<int>(std::ssize(terms));
326326
}
327327

328328
auto is_identifier() const
@@ -2313,7 +2313,7 @@ struct function_type_node
23132313
auto parameter_count() const
23142314
-> int
23152315
{
2316-
return std::ssize(parameters->parameters);
2316+
return unsafe_narrow<int>(std::ssize(parameters->parameters));
23172317
}
23182318

23192319
auto index_of_parameter_named(std::string_view s) const
@@ -5785,7 +5785,7 @@ class parser
57855785
}
57865786

57875787
for (auto& e : expression_node::current_expressions) {
5788-
e->num_subexpressions += std::ssize(n->ops);
5788+
e->num_subexpressions += unsafe_narrow<int>(std::ssize(n->ops));
57895789
}
57905790

57915791
return n;

source/sema.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ class sema
546546
//-----------------------------------------------------------------------
547547
// Function logic: For each entry in the table...
548548
//
549-
for (auto sympos = std::ssize(symbols) - 1; sympos >= 0; --sympos)
549+
for (auto sympos = unsafe_narrow<int>(std::ssize(symbols) - 1); sympos >= 0; --sympos)
550550
{
551551
// If this is an uninitialized local variable,
552552
// ensure it is definitely initialized and tag those initializations

source/to_cpp1.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -402,11 +402,11 @@ class positional_printer
402402
// Now also adjust the colno
403403
if (last_newline != std::string::npos) {
404404
// If we found a newline, it's the distance from the last newline to EOL
405-
curr_pos.colno = s.length() - last_newline;
405+
curr_pos.colno = unsafe_narrow<colno_t>(s.length() - last_newline);
406406
}
407407
else {
408408
// Else add the length of the string
409-
curr_pos.colno += s.length();
409+
curr_pos.colno += unsafe_narrow<colno_t>(s.length());
410410
}
411411
}
412412
}
@@ -2777,7 +2777,7 @@ class cppfront
27772777
return is_pointer_declaration(type_id_node->address_of, deref_cnt, addr_cnt + 1);
27782778
}
27792779

2780-
int pointer_declarators_cnt = std::count_if(std::cbegin(type_id_node->pc_qualifiers), std::cend(type_id_node->pc_qualifiers), [](auto* q) {
2780+
auto pointer_declarators_cnt = std::count_if(std::cbegin(type_id_node->pc_qualifiers), std::cend(type_id_node->pc_qualifiers), [](auto* q) {
27812781
return q->type() == lexeme::Multiply;
27822782
});
27832783

@@ -2789,7 +2789,7 @@ class cppfront
27892789
return is_pointer_declaration(type_id_node->suspicious_initialization, deref_cnt, addr_cnt);
27902790
}
27912791

2792-
return (pointer_declarators_cnt + addr_cnt - deref_cnt) > 0;
2792+
return (unsafe_narrow<int>(pointer_declarators_cnt) + addr_cnt - deref_cnt) > 0;
27932793
}
27942794

27952795
auto is_pointer_declaration(

0 commit comments

Comments
 (0)