Commit 3b040eb8 by Jamie Madill

Revert "Add full support for line continuation in the preprocessor"

Warning in the Linux/Mac builders: In file included from ../../third_party/angle/src/tests/preprocessor_tests/input_test.cpp:7: In file included from ../../third_party/angle/src/tests/preprocessor_tests/PreprocessorTest.h:7: ../../testing/gtest/include/gtest/gtest.h:1392:16: error: comparison of integers of different signs: 'const int' and 'const unsigned long' [-Werror,-Wsign-compare] if (expected == actual) { ~~~~~~~~ ^ ~~~~~~ ../../testing/gtest/include/gtest/gtest.h:1422:12: note: in instantiation of function template specialization 'testing::internal::CmpHelperEQ<int, unsigned long>' requested here return CmpHelperEQ(expected_expression, actual_expression, expected, ^ ../../third_party/angle/src/tests/preprocessor_tests/input_test.cpp:171:5: note: in instantiation of function template specialization 'testing::internal::EqHelper<false>::Compare<int, unsigned long>' requested here EXPECT_EQ(3, input.read(buf, maxSize, &lineNo)); ^ BUG=angleproject:1125 This reverts commit c1157d19. Change-Id: Ic6fa286d190b006cccc5154d86e21ecc03175763 Reviewed-on: https://chromium-review.googlesource.com/294080Reviewed-by: 's avatarJamie Madill <jmadill@chromium.org> Tested-by: 's avatarJamie Madill <jmadill@chromium.org>
parent c1157d19
...@@ -29,75 +29,13 @@ Input::Input(size_t count, const char *const string[], const int length[]) : ...@@ -29,75 +29,13 @@ Input::Input(size_t count, const char *const string[], const int length[]) :
} }
} }
const char *Input::skipChar() size_t Input::read(char *buf, size_t maxSize)
{
// This function should only be called when there is a character to skip.
assert(mReadLoc.cIndex < mLength[mReadLoc.sIndex]);
++mReadLoc.cIndex;
if (mReadLoc.cIndex == mLength[mReadLoc.sIndex])
{
++mReadLoc.sIndex;
mReadLoc.cIndex = 0;
}
if (mReadLoc.sIndex >= mCount)
{
return nullptr;
}
return mString[mReadLoc.sIndex] + mReadLoc.cIndex;
}
size_t Input::read(char *buf, size_t maxSize, int *lineNo)
{ {
size_t nRead = 0; size_t nRead = 0;
// The previous call to read might have stopped copying the string when encountering a line while ((nRead < maxSize) && (mReadLoc.sIndex < mCount))
// continuation. Check for this possibility first.
if (mReadLoc.sIndex < mCount && maxSize > 0)
{
const char *c = mString[mReadLoc.sIndex] + mReadLoc.cIndex;
if ((*c) == '\\')
{
c = skipChar();
if (c != nullptr && (*c) == '\n')
{
// Line continuation of backslash + newline.
skipChar();
++(*lineNo);
}
else if (c != nullptr && (*c) == '\r')
{
// Line continuation. Could be backslash + '\r\n' or just backslash + '\r'.
c = skipChar();
if (c != nullptr && (*c) == '\n')
{
skipChar();
}
++(*lineNo);
}
else
{
// Not line continuation, so write the skipped backslash to buf.
*buf = '\\';
++nRead;
}
}
}
size_t maxRead = maxSize;
while ((nRead < maxRead) && (mReadLoc.sIndex < mCount))
{ {
size_t size = mLength[mReadLoc.sIndex] - mReadLoc.cIndex; size_t size = mLength[mReadLoc.sIndex] - mReadLoc.cIndex;
size = std::min(size, maxSize); size = std::min(size, maxSize);
for (size_t i = 0; i < size; ++i)
{
// Stop if a possible line continuation is encountered.
// It will be processed on the next call on input, which skips it
// and increments line number if necessary.
if (*(mString[mReadLoc.sIndex] + mReadLoc.cIndex + i) == '\\')
{
size = i;
maxRead = nRead + size; // Stop reading right before the backslash.
}
}
std::memcpy(buf + nRead, mString[mReadLoc.sIndex] + mReadLoc.cIndex, size); std::memcpy(buf + nRead, mString[mReadLoc.sIndex] + mReadLoc.cIndex, size);
nRead += size; nRead += size;
mReadLoc.cIndex += size; mReadLoc.cIndex += size;
......
...@@ -33,7 +33,7 @@ class Input ...@@ -33,7 +33,7 @@ class Input
return mLength[index]; return mLength[index];
} }
size_t read(char *buf, size_t maxSize, int *lineNo); size_t read(char *buf, size_t maxSize);
struct Location struct Location
{ {
...@@ -49,10 +49,6 @@ class Input ...@@ -49,10 +49,6 @@ class Input
const Location &readLoc() const { return mReadLoc; } const Location &readLoc() const { return mReadLoc; }
private: private:
// Skip a character and return the next character after the one that was skipped.
// Return nullptr if data runs out.
const char *skipChar();
// Input. // Input.
size_t mCount; size_t mCount;
const char * const *mString; const char * const *mString;
......
...@@ -594,7 +594,7 @@ typedef pp::SourceLocation YYLTYPE; ...@@ -594,7 +594,7 @@ typedef pp::SourceLocation YYLTYPE;
} while(0); } while(0);
#define YY_INPUT(buf, result, maxSize) \ #define YY_INPUT(buf, result, maxSize) \
result = yyextra->input.read(buf, maxSize, &yylineno); result = yyextra->input.read(buf, maxSize);
#define INITIAL 0 #define INITIAL 0
#define COMMENT 1 #define COMMENT 1
......
...@@ -77,7 +77,7 @@ typedef pp::SourceLocation YYLTYPE; ...@@ -77,7 +77,7 @@ typedef pp::SourceLocation YYLTYPE;
} while(0); } while(0);
#define YY_INPUT(buf, result, maxSize) \ #define YY_INPUT(buf, result, maxSize) \
result = yyextra->input.read(buf, maxSize, &yylineno); result = yyextra->input.read(buf, maxSize);
%} %}
......
...@@ -30,8 +30,7 @@ TEST(InputTest, DefaultConstructor) ...@@ -30,8 +30,7 @@ TEST(InputTest, DefaultConstructor)
{ {
pp::Input input; pp::Input input;
EXPECT_EQ(0u, input.count()); EXPECT_EQ(0u, input.count());
int lineNo = 0; EXPECT_EQ(0u, input.read(NULL, 1));
EXPECT_EQ(0u, input.read(NULL, 1, &lineNo));
} }
TEST(InputTest, NullLength) TEST(InputTest, NullLength)
...@@ -73,35 +72,34 @@ TEST(InputTest, ReadSingleString) ...@@ -73,35 +72,34 @@ TEST(InputTest, ReadSingleString)
char buf[4] = {'\0', '\0', '\0', '\0'}; char buf[4] = {'\0', '\0', '\0', '\0'};
int maxSize = 1; int maxSize = 1;
int lineNo = 0;
pp::Input input1(count, str, NULL); pp::Input input1(count, str, NULL);
EXPECT_EQ(1u, input1.read(buf, maxSize, &lineNo)); EXPECT_EQ(1u, input1.read(buf, maxSize));
EXPECT_EQ('f', buf[0]); EXPECT_EQ('f', buf[0]);
EXPECT_EQ(1u, input1.read(buf, maxSize, &lineNo)); EXPECT_EQ(1u, input1.read(buf, maxSize));
EXPECT_EQ('o', buf[0]); EXPECT_EQ('o', buf[0]);
EXPECT_EQ(1u, input1.read(buf, maxSize, &lineNo)); EXPECT_EQ(1u, input1.read(buf, maxSize));
EXPECT_EQ('o', buf[0]); EXPECT_EQ('o', buf[0]);
EXPECT_EQ(0u, input1.read(buf, maxSize, &lineNo)); EXPECT_EQ(0u, input1.read(buf, maxSize));
maxSize = 2; maxSize = 2;
pp::Input input2(count, str, NULL); pp::Input input2(count, str, NULL);
EXPECT_EQ(2u, input2.read(buf, maxSize, &lineNo)); EXPECT_EQ(2u, input2.read(buf, maxSize));
EXPECT_STREQ("fo", buf); EXPECT_STREQ("fo", buf);
EXPECT_EQ(1u, input2.read(buf, maxSize, &lineNo)); EXPECT_EQ(1u, input2.read(buf, maxSize));
EXPECT_EQ('o', buf[0]); EXPECT_EQ('o', buf[0]);
EXPECT_EQ(0u, input2.read(buf, maxSize, &lineNo)); EXPECT_EQ(0u, input2.read(buf, maxSize));
maxSize = 3; maxSize = 3;
pp::Input input3(count, str, NULL); pp::Input input3(count, str, NULL);
EXPECT_EQ(3u, input3.read(buf, maxSize, &lineNo)); EXPECT_EQ(3u, input3.read(buf, maxSize));
EXPECT_STREQ("foo", buf); EXPECT_STREQ("foo", buf);
EXPECT_EQ(0u, input3.read(buf, maxSize, &lineNo)); EXPECT_EQ(0u, input3.read(buf, maxSize));
maxSize = 4; maxSize = 4;
pp::Input input4(count, str, NULL); pp::Input input4(count, str, NULL);
EXPECT_EQ(3u, input4.read(buf, maxSize, &lineNo)); EXPECT_EQ(3u, input4.read(buf, maxSize));
EXPECT_STREQ("foo", buf); EXPECT_STREQ("foo", buf);
EXPECT_EQ(0u, input4.read(buf, maxSize, &lineNo)); EXPECT_EQ(0u, input4.read(buf, maxSize));
} }
TEST(InputTest, ReadMultipleStrings) TEST(InputTest, ReadMultipleStrings)
...@@ -111,35 +109,34 @@ TEST(InputTest, ReadMultipleStrings) ...@@ -111,35 +109,34 @@ TEST(InputTest, ReadMultipleStrings)
char buf[4] = {'\0', '\0', '\0', '\0'}; char buf[4] = {'\0', '\0', '\0', '\0'};
int maxSize = 1; int maxSize = 1;
int lineNo = 0;
pp::Input input1(count, str, NULL); pp::Input input1(count, str, NULL);
EXPECT_EQ(1u, input1.read(buf, maxSize, &lineNo)); EXPECT_EQ(1u, input1.read(buf, maxSize));
EXPECT_EQ('f', buf[0]); EXPECT_EQ('f', buf[0]);
EXPECT_EQ(1u, input1.read(buf, maxSize, &lineNo)); EXPECT_EQ(1u, input1.read(buf, maxSize));
EXPECT_EQ('o', buf[0]); EXPECT_EQ('o', buf[0]);
EXPECT_EQ(1u, input1.read(buf, maxSize, &lineNo)); EXPECT_EQ(1u, input1.read(buf, maxSize));
EXPECT_EQ('o', buf[0]); EXPECT_EQ('o', buf[0]);
EXPECT_EQ(0u, input1.read(buf, maxSize, &lineNo)); EXPECT_EQ(0u, input1.read(buf, maxSize));
maxSize = 2; maxSize = 2;
pp::Input input2(count, str, NULL); pp::Input input2(count, str, NULL);
EXPECT_EQ(2u, input2.read(buf, maxSize, &lineNo)); EXPECT_EQ(2u, input2.read(buf, maxSize));
EXPECT_STREQ("fo", buf); EXPECT_STREQ("fo", buf);
EXPECT_EQ(1u, input2.read(buf, maxSize, &lineNo)); EXPECT_EQ(1u, input2.read(buf, maxSize));
EXPECT_EQ('o', buf[0]); EXPECT_EQ('o', buf[0]);
EXPECT_EQ(0u, input2.read(buf, maxSize, &lineNo)); EXPECT_EQ(0u, input2.read(buf, maxSize));
maxSize = 3; maxSize = 3;
pp::Input input3(count, str, NULL); pp::Input input3(count, str, NULL);
EXPECT_EQ(3u, input3.read(buf, maxSize, &lineNo)); EXPECT_EQ(3u, input3.read(buf, maxSize));
EXPECT_STREQ("foo", buf); EXPECT_STREQ("foo", buf);
EXPECT_EQ(0u, input3.read(buf, maxSize, &lineNo)); EXPECT_EQ(0u, input3.read(buf, maxSize));
maxSize = 4; maxSize = 4;
pp::Input input4(count, str, NULL); pp::Input input4(count, str, NULL);
EXPECT_EQ(3u, input4.read(buf, maxSize, &lineNo)); EXPECT_EQ(3u, input4.read(buf, maxSize));
EXPECT_STREQ("foo", buf); EXPECT_STREQ("foo", buf);
EXPECT_EQ(0u, input4.read(buf, maxSize, &lineNo)); EXPECT_EQ(0u, input4.read(buf, maxSize));
} }
TEST(InputTest, ReadStringsWithLength) TEST(InputTest, ReadStringsWithLength)
...@@ -151,28 +148,9 @@ TEST(InputTest, ReadStringsWithLength) ...@@ -151,28 +148,9 @@ TEST(InputTest, ReadStringsWithLength)
int length[] = {2, 3}; int length[] = {2, 3};
char buf[6] = {'\0', '\0', '\0', '\0', '\0', '\0'}; char buf[6] = {'\0', '\0', '\0', '\0', '\0', '\0'};
size_t maxSize = 5; size_t maxSize = 5;
int lineNo = 0;
pp::Input input(count, str, length); pp::Input input(count, str, length);
EXPECT_EQ(maxSize, input.read(buf, maxSize, &lineNo)); EXPECT_EQ(maxSize, input.read(buf, maxSize));
EXPECT_STREQ("fobar", buf); EXPECT_STREQ("fobar", buf);
} }
TEST(InputTest, ReadStringsWithLineContinuation)
{
int count = 2;
const char* str[] = {"foo\\", "\nba\\\r\nr"};
int length[] = {4, 7};
char buf[11] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'};
size_t maxSize = 11;
int lineNo = 0;
pp::Input input(count, str, length);
EXPECT_EQ(3, input.read(buf, maxSize, &lineNo));
EXPECT_EQ(lineNo, 0);
EXPECT_EQ(2, input.read(buf + 3, maxSize - 3, &lineNo));
EXPECT_EQ(lineNo, 1);
EXPECT_EQ(1, input.read(buf + 5, maxSize - 5, &lineNo));
EXPECT_EQ(lineNo, 2);
EXPECT_STREQ("foobar", buf);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment