Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/hsutter/cppfront
Browse files Browse the repository at this point in the history
  • Loading branch information
hsutter committed Dec 29, 2023
2 parents 7fe06da + 6ff481d commit aaab6af
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 35 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-cppfront.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
10 changes: 5 additions & 5 deletions source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ struct source_line
-> int
{
return
std::find_if_not( text.begin(), text.end(), &isspace )
- text.begin();
unsafe_narrow<int>(std::find_if_not( text.begin(), text.end(), &isspace )
- text.begin());
}

auto prefix() const
Expand Down Expand Up @@ -334,7 +334,7 @@ auto is_nondigit(char c)
isalpha(c)
|| c == '_'
;
};
}

//G identifier-start:
//G nondigit
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<int>(length);
}
}
struct register_flag {
Expand Down
10 changes: 5 additions & 5 deletions source/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ class braces_tracker
}

auto current_depth() const -> int {
return std::ssize(open_braces);
return unsafe_narrow<int>(std::ssize(open_braces));
}

// --- Preprocessor matching functions - #if/#else/#endif
Expand Down Expand Up @@ -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<int>(end_pos+raw_string_closing_seq.size()-1);
}
else {
r.all_comment_line = false;
Expand Down Expand Up @@ -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<colno_t>(ssize(line))),
std::string("line ended before character literal was terminated")
);
}
Expand Down Expand Up @@ -957,7 +957,7 @@ class source
lines.back().text,
in_comment,
braces,
std::ssize(lines)-1,
unsafe_narrow<lineno_t>(std::ssize(lines)-1),
errors
)
&& in.getline(&buf[0], max_line_len)
Expand All @@ -982,7 +982,7 @@ class source
in_raw_string_literal,
raw_string_closing_seq,
braces,
std::ssize(lines) - 1
unsafe_narrow<lineno_t>(std::ssize(lines)-1)
);
if (stats.all_comment_line) {
lines.back().cat = source_line::category::comment;
Expand Down
34 changes: 19 additions & 15 deletions source/lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ auto _as(lexeme l)
break;case lexeme::None: return "(NONE)";
break;default: return "INTERNAL-ERROR";
}
};
}


auto is_operator(lexeme l)
Expand Down Expand Up @@ -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<int>(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
Expand All @@ -307,7 +307,7 @@ class token
)
{
sv.remove_prefix(prefix.size());
pos.colno += prefix.size();
pos.colno += unsafe_narrow<colno_t>(prefix.size());
}
}

Expand Down Expand Up @@ -816,7 +816,7 @@ auto lex_line(
source_position(lineno, i + 1),
type
});
i += num-1;
i += unsafe_narrow<int>(num-1);

merge_cpp1_multi_token_fundamental_type_names();
merge_operator_function_names();
Expand Down Expand Up @@ -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<colno_t>(std::ssize(new_part)-1);

if (parts.is_expanded()) {
// raw string was expanded and we need to repeat the processing of this line
Expand Down Expand Up @@ -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<int>(size_to_replace) ) ) {
continue;
}
}
Expand All @@ -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<colno_t>(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} });
Expand Down Expand Up @@ -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<int>(closing_pos-i+std::ssize(closing_seq)))
) {
continue;
}
Expand All @@ -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<int>(std::ssize(line)-i))
) {
continue;
}
// skip entire raw string opening sequence R"
i = paren_pos;
i = unsafe_narrow<int>(paren_pos);

// if we are on the end of the line we need to add new line char
if (i+1 == std::ssize(line)) {
Expand Down Expand Up @@ -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<int>(paren_pos);

// if we are on the end of the line we need to add new line char
if (i+1 == std::ssize(line)) {
Expand Down Expand Up @@ -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<lineno_t>(std::distance(std::begin(lines), line));

// If this is generated code, use negative line numbers to
// inform and assist the printer
Expand Down
6 changes: 3 additions & 3 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ struct binary_expression_node
auto terms_size() const
-> int
{
return std::ssize(terms);
return unsafe_narrow<int>(std::ssize(terms));
}

auto is_identifier() const
Expand Down Expand Up @@ -2313,7 +2313,7 @@ struct function_type_node
auto parameter_count() const
-> int
{
return std::ssize(parameters->parameters);
return unsafe_narrow<int>(std::ssize(parameters->parameters));
}

auto index_of_parameter_named(std::string_view s) const
Expand Down Expand Up @@ -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<int>(std::ssize(n->ops));
}

return n;
Expand Down
2 changes: 1 addition & 1 deletion source/sema.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<int>(std::ssize(symbols) - 1); sympos >= 0; --sympos)
{
// If this is an uninitialized local variable,
// ensure it is definitely initialized and tag those initializations
Expand Down
8 changes: 4 additions & 4 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<colno_t>(s.length() - last_newline);
}
else {
// Else add the length of the string
curr_pos.colno += s.length();
curr_pos.colno += unsafe_narrow<colno_t>(s.length());
}
}
}
Expand Down Expand Up @@ -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;
});

Expand All @@ -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<int>(pointer_declarators_cnt) + addr_cnt - deref_cnt) > 0;
}

auto is_pointer_declaration(
Expand Down

0 comments on commit aaab6af

Please sign in to comment.