diff --git a/.github/workflows/build-cppfront.yaml b/.github/workflows/build-cppfront.yaml index 3c44d77b13..8d228b383a 100644 --- a/.github/workflows/build-cppfront.yaml +++ b/.github/workflows/build-cppfront.yaml @@ -9,7 +9,7 @@ jobs: - name: Compiler name & version run: cl.exe - name: Build - run: cl.exe source/cppfront.cpp -std:c++latest -MD -EHsc -experimental:module -W4 + run: cl.exe source/cppfront.cpp -std:c++latest -MD -EHsc -experimental:module -W4 -WX build-unix-like: strategy: fail-fast: false @@ -36,7 +36,7 @@ jobs: runs-on: ${{ matrix.runs-on }} env: CXX: ${{ matrix.compiler }} - CXXFLAGS: -std=${{ matrix.cxx-std }} -Wall -Wextra -Wold-style-cast -pthread + CXXFLAGS: -std=${{ matrix.cxx-std }} -Wall -Wextra -Wold-style-cast -Wpedantic -Werror -pthread steps: - uses: actions/checkout@v3 - name: Install compiler diff --git a/source/common.h b/source/common.h index fe0301c14b..67565e1b46 100644 --- a/source/common.h +++ b/source/common.h @@ -65,8 +65,8 @@ struct source_line -> int { return - std::find_if_not( text.begin(), text.end(), &isspace ) - - text.begin(); + unsafe_narrow(std::find_if_not( text.begin(), text.end(), &isspace ) + - text.begin()); } auto prefix() const @@ -334,7 +334,7 @@ auto is_nondigit(char c) isalpha(c) || c == '_' ; -}; +} //G identifier-start: //G nondigit @@ -379,7 +379,7 @@ auto starts_with_identifier(std::string_view s) return j; } return 0; -}; +} // Helper to allow one of the above or a digit separator @@ -759,7 +759,7 @@ class cmdline_processor auto length = std::ssize(name); if (opt_out) { length += 3; } // space to print "[-]" if (max_flag_length < length) { - max_flag_length = length; + max_flag_length = unsafe_narrow(length); } } struct register_flag { diff --git a/source/io.h b/source/io.h index cf016a3fd4..462f96a76d 100644 --- a/source/io.h +++ b/source/io.h @@ -457,7 +457,7 @@ class braces_tracker } auto current_depth() const -> int { - return std::ssize(open_braces); + return unsafe_narrow(std::ssize(open_braces)); } // --- Preprocessor matching functions - #if/#else/#endif @@ -610,7 +610,7 @@ auto process_cpp_line( return r; } in_raw_string_literal = false; - i = end_pos+raw_string_closing_seq.size()-1; + i = unsafe_narrow(end_pos+raw_string_closing_seq.size()-1); } else { r.all_comment_line = false; @@ -797,7 +797,7 @@ auto process_cpp2_line( if (in_char_literal) { errors.emplace_back( - source_position(lineno, ssize(line)), + source_position(lineno, unsafe_narrow(ssize(line))), std::string("line ended before character literal was terminated") ); } @@ -957,7 +957,7 @@ class source lines.back().text, in_comment, braces, - std::ssize(lines)-1, + unsafe_narrow(std::ssize(lines)-1), errors ) && in.getline(&buf[0], max_line_len) @@ -982,7 +982,7 @@ class source in_raw_string_literal, raw_string_closing_seq, braces, - std::ssize(lines) - 1 + unsafe_narrow(std::ssize(lines)-1) ); if (stats.all_comment_line) { lines.back().cat = source_line::category::comment; diff --git a/source/lex.h b/source/lex.h index 478fe3da8a..f08c3eedb1 100644 --- a/source/lex.h +++ b/source/lex.h @@ -199,7 +199,7 @@ auto _as(lexeme l) break;case lexeme::None: return "(NONE)"; break;default: return "INTERNAL-ERROR"; } -}; +} auto is_operator(lexeme l) @@ -286,13 +286,13 @@ class token pos.colno += offset; } - auto position() const -> source_position { return pos; } + auto position() const -> source_position { return pos; } - auto length () const -> int { return sv.size(); } + auto length () const -> int { return unsafe_narrow(sv.size()); } - auto type () const -> lexeme { return lex_type; } + auto type () const -> lexeme { return lex_type; } - auto set_type(lexeme l) -> void { lex_type = l; } + auto set_type(lexeme l) -> void { lex_type = l; } auto visit(auto& v, int depth) const -> void @@ -307,7 +307,7 @@ class token ) { sv.remove_prefix(prefix.size()); - pos.colno += prefix.size(); + pos.colno += unsafe_narrow(prefix.size()); } } @@ -816,7 +816,7 @@ auto lex_line( source_position(lineno, i + 1), type }); - i += num-1; + i += unsafe_narrow(num-1); merge_cpp1_multi_token_fundamental_type_names(); merge_operator_function_names(); @@ -1066,7 +1066,7 @@ auto lex_line( auto parts = expand_raw_string_literal(opening_seq, closing_seq, closing_strategy, part, errors, source_position(lineno, pos_to_replace + 1)); auto new_part = parts.generate(); mutable_line.replace( pos_to_replace, size_to_replace, new_part ); - i += std::ssize(new_part)-1; + i += unsafe_narrow(std::ssize(new_part)-1); if (parts.is_expanded()) { // raw string was expanded and we need to repeat the processing of this line @@ -1147,7 +1147,7 @@ auto lex_line( auto closing_strategy = end_pos == line.npos ? string_parts::no_ends : string_parts::on_the_end; auto size_to_replace = end_pos == line.npos ? std::ssize(line) - i : end_pos - i + std::ssize(rsm.closing_seq); - if (interpolate_raw_string(rsm.opening_seq, rsm.closing_seq, closing_strategy, part, i, size_to_replace ) ) { + if (interpolate_raw_string(rsm.opening_seq, rsm.closing_seq, closing_strategy, part, i, unsafe_narrow(size_to_replace) ) ) { continue; } } @@ -1164,7 +1164,7 @@ auto lex_line( raw_string_multiline.value().text += raw_string_multiline.value().closing_seq; // and position where multiline_raw_string ends (needed for reseting line parsing) - i = end_pos+std::ssize(raw_string_multiline.value().closing_seq)-1; + i = unsafe_narrow(end_pos+std::ssize(raw_string_multiline.value().closing_seq)-1); const auto& text = raw_string_multiline.value().should_interpolate ? raw_string_multiline.value().text.substr(1) : raw_string_multiline.value().text; multiline_raw_strings.emplace_back(multiline_raw_string{ text, {lineno, i} }); @@ -1379,7 +1379,9 @@ auto lex_line( opening_seq, closing_seq, string_parts::on_both_ends, - std::string_view(&line[paren_pos+1], closing_pos-paren_pos-1), i, closing_pos-i+std::ssize(closing_seq)) + std::string_view(&line[paren_pos+1], closing_pos-paren_pos-1), + i, + unsafe_narrow(closing_pos-i+std::ssize(closing_seq))) ) { continue; } @@ -1397,12 +1399,14 @@ auto lex_line( opening_seq, closing_seq, string_parts::on_the_beginning, - std::string_view(&line[paren_pos+1], std::ssize(line)-(paren_pos+1)), i, std::ssize(line)-i) + std::string_view(&line[paren_pos+1], std::ssize(line)-(paren_pos+1)), + i, + unsafe_narrow(std::ssize(line)-i)) ) { continue; } // skip entire raw string opening sequence R" - i = paren_pos; + i = unsafe_narrow(paren_pos); // if we are on the end of the line we need to add new line char if (i+1 == std::ssize(line)) { @@ -1594,7 +1598,7 @@ auto lex_line( } else { raw_string_multiline.emplace(raw_string{source_position{lineno, i}, opening_seq, opening_seq, closing_seq }); // skip entire raw string opening sequence R" - i = paren_pos; + i = unsafe_narrow(paren_pos); // if we are on the end of the line we need to add new line char if (i+1 == std::ssize(line)) { @@ -1842,7 +1846,7 @@ class tokens // Create new map entry for the section starting at this line, // and populate its tokens with the tokens in this section - auto lineno = std::distance(std::begin(lines), line); + auto lineno = unsafe_narrow(std::distance(std::begin(lines), line)); // If this is generated code, use negative line numbers to // inform and assist the printer diff --git a/source/parse.h b/source/parse.h index 3ac3555c48..df067fb49a 100644 --- a/source/parse.h +++ b/source/parse.h @@ -322,7 +322,7 @@ struct binary_expression_node auto terms_size() const -> int { - return std::ssize(terms); + return unsafe_narrow(std::ssize(terms)); } auto is_identifier() const @@ -2313,7 +2313,7 @@ struct function_type_node auto parameter_count() const -> int { - return std::ssize(parameters->parameters); + return unsafe_narrow(std::ssize(parameters->parameters)); } auto index_of_parameter_named(std::string_view s) const @@ -5785,7 +5785,7 @@ class parser } for (auto& e : expression_node::current_expressions) { - e->num_subexpressions += std::ssize(n->ops); + e->num_subexpressions += unsafe_narrow(std::ssize(n->ops)); } return n; diff --git a/source/sema.h b/source/sema.h index d339659cd2..6c96b70aaf 100644 --- a/source/sema.h +++ b/source/sema.h @@ -546,7 +546,7 @@ class sema //----------------------------------------------------------------------- // Function logic: For each entry in the table... // - for (auto sympos = std::ssize(symbols) - 1; sympos >= 0; --sympos) + for (auto sympos = unsafe_narrow(std::ssize(symbols) - 1); sympos >= 0; --sympos) { // If this is an uninitialized local variable, // ensure it is definitely initialized and tag those initializations diff --git a/source/to_cpp1.h b/source/to_cpp1.h index a40589d3a9..502d95c394 100644 --- a/source/to_cpp1.h +++ b/source/to_cpp1.h @@ -402,11 +402,11 @@ class positional_printer // Now also adjust the colno if (last_newline != std::string::npos) { // If we found a newline, it's the distance from the last newline to EOL - curr_pos.colno = s.length() - last_newline; + curr_pos.colno = unsafe_narrow(s.length() - last_newline); } else { // Else add the length of the string - curr_pos.colno += s.length(); + curr_pos.colno += unsafe_narrow(s.length()); } } } @@ -2777,7 +2777,7 @@ class cppfront return is_pointer_declaration(type_id_node->address_of, deref_cnt, addr_cnt + 1); } - int pointer_declarators_cnt = std::count_if(std::cbegin(type_id_node->pc_qualifiers), std::cend(type_id_node->pc_qualifiers), [](auto* q) { + auto pointer_declarators_cnt = std::count_if(std::cbegin(type_id_node->pc_qualifiers), std::cend(type_id_node->pc_qualifiers), [](auto* q) { return q->type() == lexeme::Multiply; }); @@ -2789,7 +2789,7 @@ class cppfront return is_pointer_declaration(type_id_node->suspicious_initialization, deref_cnt, addr_cnt); } - return (pointer_declarators_cnt + addr_cnt - deref_cnt) > 0; + return (unsafe_narrow(pointer_declarators_cnt) + addr_cnt - deref_cnt) > 0; } auto is_pointer_declaration(