blob: 3a473f77912bd6690bd51bc8455b64190b890f03 [file] [log] [blame]
alokp@chromium.org4e4b8072011-08-07 05:36:04 +00001//
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 Wallez054f7ed2016-09-20 17:15:59 -04007#include "compiler/preprocessor/Input.h"
alokp@chromium.org4e4b8072011-08-07 05:36:04 +00008
alokp@chromium.org98eec912012-05-01 10:04:08 +00009#include <algorithm>
alokp@chromium.org99b5c0c2012-05-17 20:44:52 +000010#include <cstring>
alokp@chromium.org98eec912012-05-01 10:04:08 +000011
Corentin Wallez054f7ed2016-09-20 17:15:59 -040012#include "common/debug.h"
13
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000014namespace pp
15{
16
alokp@chromium.org30a487c2012-05-02 17:30:46 +000017Input::Input() : mCount(0), mString(0)
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000018{
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000019}
20
Jamie Madillf832c9d2016-12-12 17:38:48 -050021Input::Input(size_t count, const char *const string[], const int length[])
22 : mCount(count), mString(string)
alokp@chromium.org98eec912012-05-01 10:04:08 +000023{
alokp@chromium.org30a487c2012-05-02 17:30:46 +000024 mLength.reserve(mCount);
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000025 for (size_t i = 0; i < mCount; ++i)
alokp@chromium.org98eec912012-05-01 10:04:08 +000026 {
alokp@chromium.org30a487c2012-05-02 17:30:46 +000027 int len = length ? length[i] : -1;
daniel@transgaming.coma16a55f2012-12-20 20:51:54 +000028 mLength.push_back(len < 0 ? std::strlen(mString[i]) : len);
alokp@chromium.org98eec912012-05-01 10:04:08 +000029 }
30}
31
Olli Etuaho26e355b2015-08-14 14:16:19 +030032const char *Input::skipChar()
33{
34 // This function should only be called when there is a character to skip.
Corentin Wallez054f7ed2016-09-20 17:15:59 -040035 ASSERT(mReadLoc.cIndex < mLength[mReadLoc.sIndex]);
Olli Etuaho26e355b2015-08-14 14:16:19 +030036 ++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
49size_t Input::read(char *buf, size_t maxSize, int *lineNo)
alokp@chromium.org98eec912012-05-01 10:04:08 +000050{
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000051 size_t nRead = 0;
Olli Etuaho26e355b2015-08-14 14:16:19 +030052 // 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 Wallezd78e33a2017-10-30 12:33:52 -040064 // Fake an EOF if the line number would overflow.
65 if (*lineNo == INT_MAX)
66 {
67 return 0;
68 }
Olli Etuaho26e355b2015-08-14 14:16:19 +030069 ++(*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 Wallezd78e33a2017-10-30 12:33:52 -040079 // Fake an EOF if the line number would overflow.
80 if (*lineNo == INT_MAX)
81 {
82 return 0;
83 }
Olli Etuaho26e355b2015-08-14 14:16:19 +030084 ++(*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.org98eec912012-05-01 10:04:08 +000097 {
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000098 size_t size = mLength[mReadLoc.sIndex] - mReadLoc.cIndex;
Jamie Madillf832c9d2016-12-12 17:38:48 -050099 size = std::min(size, maxSize);
Olli Etuaho26e355b2015-08-14 14:16:19 +0300100 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 Madillf832c9d2016-12-12 17:38:48 -0500107 size = i;
Olli Etuaho26e355b2015-08-14 14:16:19 +0300108 maxRead = nRead + size; // Stop reading right before the backslash.
109 }
110 }
daniel@transgaming.coma16a55f2012-12-20 20:51:54 +0000111 std::memcpy(buf + nRead, mString[mReadLoc.sIndex] + mReadLoc.cIndex, size);
alokp@chromium.org98eec912012-05-01 10:04:08 +0000112 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.org4e4b8072011-08-07 05:36:04 +0000125} // namespace pp