Skip to content

Commit 0c806cf

Browse files
committed
Merge remote-tracking branch 'upstream/master'
2 parents 5874fc5 + 7129b11 commit 0c806cf

3 files changed

Lines changed: 33 additions & 3 deletions

File tree

simplecpp.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,20 @@ void simplecpp::TokenList::readfile(std::istream &istr, const std::string &filen
401401
if (ch < ' ' && ch != '\t' && ch != '\n' && ch != '\r')
402402
ch = ' ';
403403

404+
if (ch >= 0x80) {
405+
if (outputList) {
406+
simplecpp::Output err(files);
407+
err.type = simplecpp::Output::UNHANDLED_CHAR_ERROR;
408+
err.location = location;
409+
std::ostringstream s;
410+
s << (int)ch;
411+
err.msg = "The code contains unhandled character(s) (character code=" + s.str() + "). Neither unicode nor extended ascii is supported.";
412+
outputList->push_back(err);
413+
}
414+
clear();
415+
return;
416+
}
417+
404418
if (ch == '\n') {
405419
if (cback() && cback()->op == '\\') {
406420
if (location.col > cback()->location.col + 1U)
@@ -1220,6 +1234,7 @@ namespace simplecpp {
12201234
}
12211235
if (!sameline(nametoken, argtok)) {
12221236
endToken = argtok ? argtok->previous : argtok;
1237+
valueToken = NULL;
12231238
return false;
12241239
}
12251240
valueToken = argtok ? argtok->next : NULL;
@@ -1286,8 +1301,8 @@ namespace simplecpp {
12861301
} else {
12871302
if (!expandArg(tokens, tok, tok->location, macros, expandedmacros, parametertokens)) {
12881303
bool expanded = false;
1289-
if (macros.find(tok->str) != macros.end() && expandedmacros.find(tok->str) == expandedmacros.end()) {
1290-
const std::map<TokenString, Macro>::const_iterator it = macros.find(tok->str);
1304+
const std::map<TokenString, Macro>::const_iterator it = macros.find(tok->str);
1305+
if (it != macros.end() && expandedmacros.find(tok->str) == expandedmacros.end()) {
12911306
const Macro &m = it->second;
12921307
if (!m.functionLike()) {
12931308
m.expand(tokens, tok, macros, files);

simplecpp.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ namespace simplecpp {
164164
MISSING_HEADER,
165165
INCLUDE_NESTED_TOO_DEEPLY,
166166
SYNTAX_ERROR,
167-
PORTABILITY_BACKSLASH
167+
PORTABILITY_BACKSLASH,
168+
UNHANDLED_CHAR_ERROR
168169
} type;
169170
Location location;
170171
std::string msg;

test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ static std::string toString(const simplecpp::OutputList &outputList)
9090
case simplecpp::Output::Type::PORTABILITY_BACKSLASH:
9191
ostr << "portability_backslash,";
9292
break;
93+
case simplecpp::Output::Type::UNHANDLED_CHAR_ERROR:
94+
ostr << "unhandled_char_error,";
9395
}
9496

9597
ostr << output.msg << '\n';
@@ -1086,6 +1088,17 @@ static void readfile_cpp14_number()
10861088
ASSERT_EQUALS("A = 12345 ;", readfile("A = 12\'345;"));
10871089
}
10881090

1091+
static void readfile_unhandled_chars()
1092+
{
1093+
simplecpp::OutputList outputList;
1094+
readfile("// 你好世界", -1, &outputList);
1095+
ASSERT_EQUALS("", toString(outputList));
1096+
readfile("s=\"你好世界\"", -1, &outputList);
1097+
ASSERT_EQUALS("", toString(outputList));
1098+
readfile("int 你好世界=0;", -1, &outputList);
1099+
ASSERT_EQUALS("file0,1,unhandled_char_error,The code contains unhandled character(s) (character code=228). Neither unicode nor extended ascii is supported.\n", toString(outputList));
1100+
}
1101+
10891102
static void stringify1()
10901103
{
10911104
const char code_c[] = "#include \"A.h\"\n"
@@ -1443,6 +1456,7 @@ int main(int argc, char **argv)
14431456
TEST_CASE(readfile_string);
14441457
TEST_CASE(readfile_rawstring);
14451458
TEST_CASE(readfile_cpp14_number);
1459+
TEST_CASE(readfile_unhandled_chars);
14461460

14471461
TEST_CASE(stringify1);
14481462

0 commit comments

Comments
 (0)