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 | |
Geoff Lang | 197d529 | 2018-04-25 14:29:00 -0400 | [diff] [blame^] | 14 | namespace angle |
| 15 | { |
| 16 | |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 17 | namespace pp |
| 18 | { |
| 19 | |
alokp@chromium.org | 30a487c | 2012-05-02 17:30:46 +0000 | [diff] [blame] | 20 | Input::Input() : mCount(0), mString(0) |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 21 | { |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 22 | } |
| 23 | |
Jamie Madill | acf2f3a | 2017-11-21 19:22:44 -0500 | [diff] [blame] | 24 | Input::~Input() |
| 25 | { |
| 26 | } |
| 27 | |
Jamie Madill | f832c9d | 2016-12-12 17:38:48 -0500 | [diff] [blame] | 28 | Input::Input(size_t count, const char *const string[], const int length[]) |
| 29 | : mCount(count), mString(string) |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 30 | { |
alokp@chromium.org | 30a487c | 2012-05-02 17:30:46 +0000 | [diff] [blame] | 31 | mLength.reserve(mCount); |
shannon.woods@transgaming.com | d64b3da | 2013-02-28 23:19:26 +0000 | [diff] [blame] | 32 | for (size_t i = 0; i < mCount; ++i) |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 33 | { |
alokp@chromium.org | 30a487c | 2012-05-02 17:30:46 +0000 | [diff] [blame] | 34 | int len = length ? length[i] : -1; |
daniel@transgaming.com | a16a55f | 2012-12-20 20:51:54 +0000 | [diff] [blame] | 35 | mLength.push_back(len < 0 ? std::strlen(mString[i]) : len); |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
Olli Etuaho | 26e355b | 2015-08-14 14:16:19 +0300 | [diff] [blame] | 39 | const char *Input::skipChar() |
| 40 | { |
| 41 | // 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] | 42 | ASSERT(mReadLoc.cIndex < mLength[mReadLoc.sIndex]); |
Olli Etuaho | 26e355b | 2015-08-14 14:16:19 +0300 | [diff] [blame] | 43 | ++mReadLoc.cIndex; |
| 44 | if (mReadLoc.cIndex == mLength[mReadLoc.sIndex]) |
| 45 | { |
| 46 | ++mReadLoc.sIndex; |
| 47 | mReadLoc.cIndex = 0; |
| 48 | } |
| 49 | if (mReadLoc.sIndex >= mCount) |
| 50 | { |
| 51 | return nullptr; |
| 52 | } |
| 53 | return mString[mReadLoc.sIndex] + mReadLoc.cIndex; |
| 54 | } |
| 55 | |
| 56 | size_t Input::read(char *buf, size_t maxSize, int *lineNo) |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 57 | { |
shannon.woods@transgaming.com | d64b3da | 2013-02-28 23:19:26 +0000 | [diff] [blame] | 58 | size_t nRead = 0; |
Olli Etuaho | 26e355b | 2015-08-14 14:16:19 +0300 | [diff] [blame] | 59 | // The previous call to read might have stopped copying the string when encountering a line |
| 60 | // continuation. Check for this possibility first. |
| 61 | if (mReadLoc.sIndex < mCount && maxSize > 0) |
| 62 | { |
| 63 | const char *c = mString[mReadLoc.sIndex] + mReadLoc.cIndex; |
| 64 | if ((*c) == '\\') |
| 65 | { |
| 66 | c = skipChar(); |
| 67 | if (c != nullptr && (*c) == '\n') |
| 68 | { |
| 69 | // Line continuation of backslash + newline. |
| 70 | skipChar(); |
Corentin Wallez | d78e33a | 2017-10-30 12:33:52 -0400 | [diff] [blame] | 71 | // Fake an EOF if the line number would overflow. |
| 72 | if (*lineNo == INT_MAX) |
| 73 | { |
| 74 | return 0; |
| 75 | } |
Olli Etuaho | 26e355b | 2015-08-14 14:16:19 +0300 | [diff] [blame] | 76 | ++(*lineNo); |
| 77 | } |
| 78 | else if (c != nullptr && (*c) == '\r') |
| 79 | { |
| 80 | // Line continuation. Could be backslash + '\r\n' or just backslash + '\r'. |
| 81 | c = skipChar(); |
| 82 | if (c != nullptr && (*c) == '\n') |
| 83 | { |
| 84 | skipChar(); |
| 85 | } |
Corentin Wallez | d78e33a | 2017-10-30 12:33:52 -0400 | [diff] [blame] | 86 | // Fake an EOF if the line number would overflow. |
| 87 | if (*lineNo == INT_MAX) |
| 88 | { |
| 89 | return 0; |
| 90 | } |
Olli Etuaho | 26e355b | 2015-08-14 14:16:19 +0300 | [diff] [blame] | 91 | ++(*lineNo); |
| 92 | } |
| 93 | else |
| 94 | { |
| 95 | // Not line continuation, so write the skipped backslash to buf. |
| 96 | *buf = '\\'; |
| 97 | ++nRead; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | size_t maxRead = maxSize; |
| 103 | while ((nRead < maxRead) && (mReadLoc.sIndex < mCount)) |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 104 | { |
shannon.woods@transgaming.com | d64b3da | 2013-02-28 23:19:26 +0000 | [diff] [blame] | 105 | size_t size = mLength[mReadLoc.sIndex] - mReadLoc.cIndex; |
Jamie Madill | f832c9d | 2016-12-12 17:38:48 -0500 | [diff] [blame] | 106 | size = std::min(size, maxSize); |
Olli Etuaho | 26e355b | 2015-08-14 14:16:19 +0300 | [diff] [blame] | 107 | for (size_t i = 0; i < size; ++i) |
| 108 | { |
| 109 | // Stop if a possible line continuation is encountered. |
| 110 | // It will be processed on the next call on input, which skips it |
| 111 | // and increments line number if necessary. |
| 112 | if (*(mString[mReadLoc.sIndex] + mReadLoc.cIndex + i) == '\\') |
| 113 | { |
Jamie Madill | f832c9d | 2016-12-12 17:38:48 -0500 | [diff] [blame] | 114 | size = i; |
Olli Etuaho | 26e355b | 2015-08-14 14:16:19 +0300 | [diff] [blame] | 115 | maxRead = nRead + size; // Stop reading right before the backslash. |
| 116 | } |
| 117 | } |
daniel@transgaming.com | a16a55f | 2012-12-20 20:51:54 +0000 | [diff] [blame] | 118 | std::memcpy(buf + nRead, mString[mReadLoc.sIndex] + mReadLoc.cIndex, size); |
alokp@chromium.org | 98eec91 | 2012-05-01 10:04:08 +0000 | [diff] [blame] | 119 | nRead += size; |
| 120 | mReadLoc.cIndex += size; |
| 121 | |
| 122 | // Advance string if we reached the end of current string. |
| 123 | if (mReadLoc.cIndex == mLength[mReadLoc.sIndex]) |
| 124 | { |
| 125 | ++mReadLoc.sIndex; |
| 126 | mReadLoc.cIndex = 0; |
| 127 | } |
| 128 | } |
| 129 | return nRead; |
| 130 | } |
| 131 | |
alokp@chromium.org | 4e4b807 | 2011-08-07 05:36:04 +0000 | [diff] [blame] | 132 | } // namespace pp |
Geoff Lang | 197d529 | 2018-04-25 14:29:00 -0400 | [diff] [blame^] | 133 | |
| 134 | } // namespace angle |