diff --git a/include/cpp2util.h b/include/cpp2util.h index 21764dd81..4f7a61cfc 100644 --- a/include/cpp2util.h +++ b/include/cpp2util.h @@ -612,7 +612,7 @@ inline auto split_string_list(std::string_view str) std::vector ret; auto is_id_char = [](char c) { - return std::isalnum(c) || c == '_'; + return std::isalnum(static_cast(c)) || c == '_'; }; auto pos = decltype(std::ssize(str)){ 0 }; diff --git a/regression-tests/pure2-utf8-byte-order-mark.cpp2 b/regression-tests/pure2-utf8-byte-order-mark.cpp2 new file mode 100644 index 000000000..d3b3e0124 --- /dev/null +++ b/regression-tests/pure2-utf8-byte-order-mark.cpp2 @@ -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"; +} diff --git a/regression-tests/test-results/msvc-2022-c++20/pure2-utf8-byte-order-mark.cpp.execution b/regression-tests/test-results/msvc-2022-c++20/pure2-utf8-byte-order-mark.cpp.execution new file mode 100644 index 000000000..b0c9ccd09 --- /dev/null +++ b/regression-tests/test-results/msvc-2022-c++20/pure2-utf8-byte-order-mark.cpp.execution @@ -0,0 +1 @@ +The byte order mark was skipped diff --git a/regression-tests/test-results/msvc-2022-c++20/pure2-utf8-byte-order-mark.cpp.output b/regression-tests/test-results/msvc-2022-c++20/pure2-utf8-byte-order-mark.cpp.output new file mode 100644 index 000000000..c78599578 --- /dev/null +++ b/regression-tests/test-results/msvc-2022-c++20/pure2-utf8-byte-order-mark.cpp.output @@ -0,0 +1 @@ +pure2-utf8-byte-order-mark.cpp diff --git a/regression-tests/test-results/msvc-2022-c++latest/pure2-utf8-byte-order-mark.cpp.execution b/regression-tests/test-results/msvc-2022-c++latest/pure2-utf8-byte-order-mark.cpp.execution new file mode 100644 index 000000000..b0c9ccd09 --- /dev/null +++ b/regression-tests/test-results/msvc-2022-c++latest/pure2-utf8-byte-order-mark.cpp.execution @@ -0,0 +1 @@ +The byte order mark was skipped diff --git a/regression-tests/test-results/msvc-2022-c++latest/pure2-utf8-byte-order-mark.cpp.output b/regression-tests/test-results/msvc-2022-c++latest/pure2-utf8-byte-order-mark.cpp.output new file mode 100644 index 000000000..c78599578 --- /dev/null +++ b/regression-tests/test-results/msvc-2022-c++latest/pure2-utf8-byte-order-mark.cpp.output @@ -0,0 +1 @@ +pure2-utf8-byte-order-mark.cpp diff --git a/regression-tests/test-results/pure2-utf8-byte-order-mark.cpp b/regression-tests/test-results/pure2-utf8-byte-order-mark.cpp new file mode 100644 index 000000000..8029ee6a7 --- /dev/null +++ b/regression-tests/test-results/pure2-utf8-byte-order-mark.cpp @@ -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"; +} + diff --git a/regression-tests/test-results/pure2-utf8-byte-order-mark.cpp2.output b/regression-tests/test-results/pure2-utf8-byte-order-mark.cpp2.output new file mode 100644 index 000000000..b8a964041 --- /dev/null +++ b/regression-tests/test-results/pure2-utf8-byte-order-mark.cpp2.output @@ -0,0 +1,2 @@ +pure2-utf8-byte-order-mark.cpp2... ok (all Cpp2, passes safety checks) + diff --git a/source/common.h b/source/common.h index 2c822d49e..53d241345 100644 --- a/source/common.h +++ b/source/common.h @@ -77,7 +77,8 @@ struct source_line -> int { return - unchecked_narrow(std::find_if_not( text.begin(), text.end(), &isspace ) + unchecked_narrow(std::find_if_not( text.begin(), text.end(), + [](char c) { return isspace(static_cast(c)); } ) - text.begin()); } @@ -279,6 +280,17 @@ auto is_binary_digit(char c) ; } +// The 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(c); +} + + //G digit: one of //G binary-digit //G one of '2' '3' '4' '5' '6' '7' '8' '9' @@ -286,7 +298,7 @@ auto is_binary_digit(char c) auto is_digit(char c) -> bool { - return isdigit(c); + return isdigit(as_ctype(c)); } //G hexadecimal-digit: @@ -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: @@ -308,7 +320,7 @@ auto is_nondigit(char c) -> bool { return - isalpha(c) + isalpha(as_ctype(c)) || c == '_' ; } @@ -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; @@ -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; } diff --git a/source/io.h b/source/io.h index 886663108..109c7639b 100644 --- a/source/io.h +++ b/source/io.h @@ -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 classifier ) { ++i; @@ -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; @@ -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; } @@ -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; @@ -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; } @@ -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); diff --git a/source/lex.h b/source/lex.h index 98c0c708b..7b9d39e55 100644 --- a/source/lex.h +++ b/source/lex.h @@ -1619,7 +1619,7 @@ auto lex_line( line[i] == 'n' && peek1 == 'o' && peek2 == 't' - && isspace(peek3) + && isspace(as_ctype(peek3)) ) { store(3, lexeme::Not); @@ -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()) ) @@ -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] + "'", diff --git a/source/parse.h b/source/parse.h index 4e341aebf..0ddb85419 100644 --- a/source/parse.h +++ b/source/parse.h @@ -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) diff --git a/source/to_cpp1.h b/source/to_cpp1.h index 9cc5b669d..57b210545 100644 --- a/source/to_cpp1.h +++ b/source/to_cpp1.h @@ -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) { @@ -2209,7 +2209,7 @@ class cppfront !statement.empty() && ( statement.back() == ';' - || isspace(statement.back()) + || isspace(as_ctype(statement.back())) ) ) {