alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2011 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Corentin Wallez | 054f7ed | 2016-09-20 17:15:59 -0400 | [diff] [blame] | 7 | #include "compiler/preprocessor/Input.h" |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 8 | |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 9 | #include <algorithm> |
alokp@chromium.org | 99b5c0c | 2012-05-17 20:44:52 +0000 | [diff] [blame] | 10 | #include <cstring> |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 11 | |
Corentin Wallez | 054f7ed | 2016-09-20 17:15:59 -0400 | [diff] [blame] | 12 | #include "common/debug.h" |
| 13 | |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 14 | namespace pp |
| 15 | { |
| 16 | |
alokp@chromium.org | 30a487c | 2012-05-02 17:30:46 +0000 | [diff] [blame] | 17 | Input::Input() : mCount(0), mString(0) |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 18 | { |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 19 | } |
| 20 | |
Jamie Madill | f832c9d | 2016-12-12 17:38:48 -0500 | [diff] [blame] | 21 | Input::Input(size_t count, const char *const string[], const int length[]) |
| 22 | : mCount(count), mString(string) |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 23 | { |
alokp@chromium.org | 30a487c | 2012-05-02 17:30:46 +0000 | [diff] [blame] | 24 | mLength.reserve(mCount); |
shannon.woods@transgaming.com | d64b3da | 2013-02-28 23:19:26 +0000 | [diff] [blame] | 25 | for (size_t i = 0; i < mCount; ++i) |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 26 | { |
alokp@chromium.org | 30a487c | 2012-05-02 17:30:46 +0000 | [diff] [blame] | 27 | int len = length ? length[i] : -1; |
daniel@transgaming.com | a16a55f | 2012-12-20 20:51:54 +0000 | [diff] [blame] | 28 | mLength.push_back(len < 0 ? std::strlen(mString[i]) : len); |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 29 | } |
| 30 | } |
| 31 | |
Olli Etuaho | 26e355b | 2015-08-14 14:16:19 +0300 | [diff] [blame] | 32 | const char *Input::skipChar() |
| 33 | { |
| 34 | // This function should only be called when there is a character to skip. |
Corentin Wallez | 054f7ed | 2016-09-20 17:15:59 -0400 | [diff] [blame] | 35 | ASSERT(mReadLoc.cIndex < mLength[mReadLoc.sIndex]); |
Olli Etuaho | 26e355b | 2015-08-14 14:16:19 +0300 | [diff] [blame] | 36 | ++mReadLoc.cIndex; |
| 37 | if (mReadLoc.cIndex == mLength[mReadLoc.sIndex]) |
| 38 | { |
| 39 | ++mReadLoc.sIndex; |
| 40 | mReadLoc.cIndex = 0; |
| 41 | } |
| 42 | if (mReadLoc.sIndex >= mCount) |
| 43 | { |
| 44 | return nullptr; |
| 45 | } |
| 46 | return mString[mReadLoc.sIndex] + mReadLoc.cIndex; |
| 47 | } |
| 48 | |
| 49 | size_t Input::read(char *buf, size_t maxSize, int *lineNo) |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 50 | { |
shannon.woods@transgaming.com | d64b3da | 2013-02-28 23:19:26 +0000 | [diff] [blame] | 51 | size_t nRead = 0; |
Olli Etuaho | 26e355b | 2015-08-14 14:16:19 +0300 | [diff] [blame] | 52 | // The previous call to read might have stopped copying the string when encountering a line |
| 53 | // continuation. Check for this possibility first. |
| 54 | if (mReadLoc.sIndex < mCount && maxSize > 0) |
| 55 | { |
| 56 | const char *c = mString[mReadLoc.sIndex] + mReadLoc.cIndex; |
| 57 | if ((*c) == '\\') |
| 58 | { |
| 59 | c = skipChar(); |
| 60 | if (c != nullptr && (*c) == '\n') |
| 61 | { |
| 62 | // Line continuation of backslash + newline. |
| 63 | skipChar(); |
Corentin Wallez | d78e33a | 2017-10-30 12:33:52 -0400 | [diff] [blame] | 64 | // Fake an EOF if the line number would overflow. |
| 65 | if (*lineNo == INT_MAX) |
| 66 | { |
| 67 | return 0; |
| 68 | } |
Olli Etuaho | 26e355b | 2015-08-14 14:16:19 +0300 | [diff] [blame] | 69 | ++(*lineNo); |
| 70 | } |
| 71 | else if (c != nullptr && (*c) == '\r') |
| 72 | { |
| 73 | // Line continuation. Could be backslash + '\r\n' or just backslash + '\r'. |
| 74 | c = skipChar(); |
| 75 | if (c != nullptr && (*c) == '\n') |
| 76 | { |
| 77 | skipChar(); |
| 78 | } |
Corentin Wallez | d78e33a | 2017-10-30 12:33:52 -0400 | [diff] [blame] | 79 | // Fake an EOF if the line number would overflow. |
| 80 | if (*lineNo == INT_MAX) |
| 81 | { |
| 82 | return 0; |
| 83 | } |
Olli Etuaho | 26e355b | 2015-08-14 14:16:19 +0300 | [diff] [blame] | 84 | ++(*lineNo); |
| 85 | } |
| 86 | else |
| 87 | { |
| 88 | // Not line continuation, so write the skipped backslash to buf. |
| 89 | *buf = '\\'; |
| 90 | ++nRead; |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | size_t maxRead = maxSize; |
| 96 | while ((nRead < maxRead) && (mReadLoc.sIndex < mCount)) |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 97 | { |
shannon.woods@transgaming.com | d64b3da | 2013-02-28 23:19:26 +0000 | [diff] [blame] | 98 | size_t size = mLength[mReadLoc.sIndex] - mReadLoc.cIndex; |
Jamie Madill | f832c9d | 2016-12-12 17:38:48 -0500 | [diff] [blame] | 99 | size = std::min(size, maxSize); |
Olli Etuaho | 26e355b | 2015-08-14 14:16:19 +0300 | [diff] [blame] | 100 | for (size_t i = 0; i < size; ++i) |
| 101 | { |
| 102 | // Stop if a possible line continuation is encountered. |
| 103 | // It will be processed on the next call on input, which skips it |
| 104 | // and increments line number if necessary. |
| 105 | if (*(mString[mReadLoc.sIndex] + mReadLoc.cIndex + i) == '\\') |
| 106 | { |
Jamie Madill | f832c9d | 2016-12-12 17:38:48 -0500 | [diff] [blame] | 107 | size = i; |
Olli Etuaho | 26e355b | 2015-08-14 14:16:19 +0300 | [diff] [blame] | 108 | maxRead = nRead + size; // Stop reading right before the backslash. |
| 109 | } |
| 110 | } |
daniel@transgaming.com | a16a55f | 2012-12-20 20:51:54 +0000 | [diff] [blame] | 111 | std::memcpy(buf + nRead, mString[mReadLoc.sIndex] + mReadLoc.cIndex, size); |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 112 | nRead += size; |
| 113 | mReadLoc.cIndex += size; |
| 114 | |
| 115 | // Advance string if we reached the end of current string. |
| 116 | if (mReadLoc.cIndex == mLength[mReadLoc.sIndex]) |
| 117 | { |
| 118 | ++mReadLoc.sIndex; |
| 119 | mReadLoc.cIndex = 0; |
| 120 | } |
| 121 | } |
| 122 | return nRead; |
| 123 | } |
| 124 | |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 125 | } // namespace pp |