diff --git a/lib/preprocessor.cpp b/lib/preprocessor.cpp index 49707298e..5b0761cf1 100644 --- a/lib/preprocessor.cpp +++ b/lib/preprocessor.cpp @@ -153,9 +153,11 @@ std::string Preprocessor::read(std::istream &istr, const std::string &filename) if (ch == '\\') { unsigned char chNext; + unsigned int spaces = 0; + #ifdef __GNUC__ // gcc-compatibility: ignore spaces - for (;;) { + for (;; spaces++) { chNext = (unsigned char)istr.peek(); if (chNext != '\n' && chNext != '\r' && (std::isspace(chNext) || std::iscntrl(chNext))) { @@ -174,7 +176,7 @@ std::string Preprocessor::read(std::istream &istr, const std::string &filename) ++newlines; (void)readChar(istr,bom); // Skip the "" } else - code << "\\"; + code << "\\" << (spaces?" ":""); } else { code << char(ch); diff --git a/test/testpreprocessor.cpp b/test/testpreprocessor.cpp index 82590c090..e88278f29 100644 --- a/test/testpreprocessor.cpp +++ b/test/testpreprocessor.cpp @@ -68,6 +68,7 @@ private: TEST_CASE(readCode1); TEST_CASE(readCode2); // #4308 - convert C++11 raw string to plain old C string TEST_CASE(readCode3); + TEST_CASE(readCode4); // #4351 - escaped whitespace in gcc // reading utf-16 file TEST_CASE(utf16); @@ -315,6 +316,15 @@ private: ASSERT_EQUALS("func(#errorname)", codestr); } + void readCode4() { + const char code[] = "char c = '\\ ';"; + Settings settings; + Preprocessor preprocessor(&settings, this); + std::istringstream istr(code); + ASSERT_EQUALS("char c = '\\ ';", preprocessor.read(istr,"test.c")); + ASSERT_EQUALS("", errout.str()); + } + void utf16() { Settings settings;