Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion include/cpp2util.h
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,7 @@ inline auto split_string_list(std::string_view str)
std::vector<std::string_view> ret;

auto is_id_char = [](char c) {
return std::isalnum(c) || c == '_';
return std::isalnum(static_cast<unsigned char>(c)) || c == '_';
};

auto pos = decltype(std::ssize(str)){ 0 };
Expand Down
7 changes: 7 additions & 0 deletions regression-tests/pure2-utf8-byte-order-mark.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// A UTF-8 byte order mark starts this file. cppfront drops it rather than
// reading its bytes as source (they used to fail a debug CRT assertion)

main: () =
{
std::cout << "The byte order mark was skipped\n";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The byte order mark was skipped
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pure2-utf8-byte-order-mark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
The byte order mark was skipped
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pure2-utf8-byte-order-mark.cpp
30 changes: 30 additions & 0 deletions regression-tests/test-results/pure2-utf8-byte-order-mark.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

#define CPP2_INCLUDE_STD Yes

//=== Cpp2 type declarations ====================================================


#include "cpp2util.h"

#line 1 "pure2-utf8-byte-order-mark.cpp2"


//=== Cpp2 type definitions and function declarations ===========================

#line 1 "pure2-utf8-byte-order-mark.cpp2"
// A UTF-8 byte order mark starts this file. cppfront drops it rather than
// reading its bytes as source (they used to fail a debug CRT assertion)

#line 4 "pure2-utf8-byte-order-mark.cpp2"
auto main() -> int;

//=== Cpp2 function definitions =================================================

#line 1 "pure2-utf8-byte-order-mark.cpp2"

#line 4 "pure2-utf8-byte-order-mark.cpp2"
auto main() -> int
{
std::cout << "The byte order mark was skipped\n";
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pure2-utf8-byte-order-mark.cpp2... ok (all Cpp2, passes safety checks)

28 changes: 20 additions & 8 deletions source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ struct source_line
-> int
{
return
unchecked_narrow<int>(std::find_if_not( text.begin(), text.end(), &isspace )
unchecked_narrow<int>(std::find_if_not( text.begin(), text.end(),
[](char c) { return isspace(static_cast<unsigned char>(c)); } )
- text.begin());
}

Expand Down Expand Up @@ -279,14 +280,25 @@ auto is_binary_digit(char c)
;
}

// The <cctype> classifiers (isspace, isdigit, ...) take an int that must be
// EOF or an unsigned char's value. A plain char above 127, such as a byte of
// UTF-8 text or a byte order mark, is negative where char is signed (MSVC),
// which is undefined behavior and a debug CRT assertion. Pass chars through this.
auto as_ctype(char c)
-> int
{
return static_cast<unsigned char>(c);
}


//G digit: one of
//G binary-digit
//G one of '2' '3' '4' '5' '6' '7' '8' '9'
//G
auto is_digit(char c)
-> bool
{
return isdigit(c);
return isdigit(as_ctype(c));
}

//G hexadecimal-digit:
Expand All @@ -296,7 +308,7 @@ auto is_digit(char c)
auto is_hexadecimal_digit(char c)
-> bool
{
return isxdigit(c);
return isxdigit(as_ctype(c));
}

//G nondigit:
Expand All @@ -308,7 +320,7 @@ auto is_nondigit(char c)
-> bool
{
return
isalpha(c)
isalpha(as_ctype(c))
|| c == '_'
;
}
Expand Down Expand Up @@ -509,7 +521,7 @@ auto to_upper_and_underbar(std::string_view s)
{
auto ret = std::string{s};
for (char& c : ret) {
if (std::isalnum(c)) { c = to_upper(c); }
if (std::isalnum(as_ctype(c))) { c = to_upper(c); }
else { c = '_'; }
}
return ret;
Expand All @@ -523,9 +535,9 @@ auto is_empty_or_a_decimal_number(std::string_view s)
if (size == 0) { return true; }

auto i = 0;
while (i < size && isspace(s[i]) ) { ++i; }
while (i < size && isdigit(s[i]) ) { ++i; }
while (i < size && isspace(s[i]) ) { ++i; }
while (i < size && isspace(as_ctype(s[i])) ) { ++i; }
while (i < size && isdigit(as_ctype(s[i])) ) { ++i; }
while (i < size && isspace(as_ctype(s[i])) ) { ++i; }
return i == size;
}

Expand Down
21 changes: 16 additions & 5 deletions source/io.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ auto move_next(
while (
i < ssize(line)
&& line[i]
&& p(line[i])
&& p(as_ctype(line[i])) // p is usually a <cctype> classifier
)
{
++i;
Expand Down Expand Up @@ -194,7 +194,7 @@ auto starts_with_operator(std::string_view s)
// skip any spaces
while (
j < std::ssize(s)
&& isspace(s[j])
&& isspace(as_ctype(s[j]))
)
{
++j;
Expand Down Expand Up @@ -284,7 +284,7 @@ auto starts_with_identifier_colon(std::string const& line)
// see if it's an access-specifier
auto s = std::string_view( &line[i], std::ssize(line) - i );
auto j = 0;
assert (!isspace(s[j]));
assert (!isspace(as_ctype(s[j])));
if (s.starts_with("public")) {
j += 6;
}
Expand All @@ -296,7 +296,7 @@ auto starts_with_identifier_colon(std::string const& line)
}
while (
j < std::ssize(s)
&& isspace(s[j])
&& isspace(as_ctype(s[j]))
)
{
++j;
Expand Down Expand Up @@ -602,7 +602,7 @@ auto process_cpp_line(

// Process this source character
//
if (!isspace(line[i])) {
if (!isspace(as_ctype(line[i]))) {
r.empty_line = false;
}

Expand Down Expand Up @@ -929,8 +929,19 @@ class source
}
};

auto first_line = true;
while (in.getline(&buf[0], max_line_len)) {

// Drop a UTF-8 byte order mark, which editors such as Visual Studio
// may write at the start of a file; it isn't part of the source
if (std::exchange(first_line, false)) {
auto const text = std::string_view(&buf[0]);
if (text.starts_with("\xEF\xBB\xBF")) {
// Shift the rest of the line left, with its terminating null
std::copy(&buf[3], &buf[0] + text.size() + 1, &buf[0]);
}
}

// Handle preprocessor source separately, they're outside the language
//
if (auto pre = is_preprocessor(buf, true);
Expand Down
6 changes: 3 additions & 3 deletions source/lex.h
Original file line number Diff line number Diff line change
Expand Up @@ -1619,7 +1619,7 @@ auto lex_line(
line[i] == 'n'
&& peek1 == 'o'
&& peek2 == 't'
&& isspace(peek3)
&& isspace(as_ctype(peek3))
)
{
store(3, lexeme::Not);
Expand Down Expand Up @@ -1860,7 +1860,7 @@ auto lex_line(
else if (auto j = starts_with_identifier({&line[i], std::size(line)-i}))
{
if (
!isspace(peek(-1))
!isspace(as_ctype(peek(-1)))
&& !tokens.empty()
&& is_literal(tokens.back().type())
)
Expand Down Expand Up @@ -1892,7 +1892,7 @@ auto lex_line(

// Anything else should be whitespace
//
else if (!isspace(line[i])) {
else if (!isspace(as_ctype(line[i]))) {
errors.emplace_back(
source_position(lineno, i),
std::string("unexpected text '") + line[i] + "'",
Expand Down
2 changes: 1 addition & 1 deletion source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -10028,7 +10028,7 @@ class parser
{
if (
curr() == "nullptr"
|| isdigit(std::string_view(curr())[0])
|| isdigit(as_ctype(std::string_view(curr())[0]))
|| (
curr() == "("
&& peek(1)
Expand Down
4 changes: 2 additions & 2 deletions source/to_cpp1.h
Original file line number Diff line number Diff line change
Expand Up @@ -1466,7 +1466,7 @@ class cppfront
flag_cpp2_only
&& line.cat != source_line::category::comment
&& line.cat != source_line::category::import
&& std::any_of(line.text.begin(), line.text.end(), [](auto c) { return !std::isspace(c); })
&& std::any_of(line.text.begin(), line.text.end(), [](char c) { return !std::isspace(as_ctype(c)); })
)
{
if (line.cat == source_line::category::preprocessor) {
Expand Down Expand Up @@ -2209,7 +2209,7 @@ class cppfront
!statement.empty()
&& (
statement.back() == ';'
|| isspace(statement.back())
|| isspace(as_ctype(statement.back()))
)
)
{
Expand Down