blob: 8d1cac5386be124a2668078c6904c1a2b7e0a0fc [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
Geoff Lang197d5292018-04-25 14:29:00 -040014namespace angle
15{
16
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000017namespace pp
18{
19
Jamie Madillb980c562018-11-27 11:34:27 -050020Input::Input() : mCount(0), mString(0) {}
alokp@chromium.org4e4b8072011-08-07 05:36:04 +000021
Jamie Madillb980c562018-11-27 11:34:27 -050022Input::~Input() {}
Jamie Madillacf2f3a2017-11-21 19:22:44 -050023
Jamie Madillf832c9d2016-12-12 17:38:48 -050024Input::Input(size_t count, const char *const string[], const int length[])
25 : mCount(count), mString(string)
alokp@chromium.org98eec912012-05-01 10:04:08 +000026{
alokp@chromium.org30a487c2012-05-02 17:30:46 +000027 mLength.reserve(mCount);
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000028 for (size_t i = 0; i < mCount; ++i)
alokp@chromium.org98eec912012-05-01 10:04:08 +000029 {
alokp@chromium.org30a487c2012-05-02 17:30:46 +000030 int len = length ? length[i] : -1;
daniel@transgaming.coma16a55f2012-12-20 20:51:54 +000031 mLength.push_back(len < 0 ? std::strlen(mString[i]) : len);
alokp@chromium.org98eec912012-05-01 10:04:08 +000032 }
33}
34
Olli Etuaho26e355b2015-08-14 14:16:19 +030035const char *Input::skipChar()
36{
37 // This function should only be called when there is a character to skip.
Corentin Wallez054f7ed2016-09-20 17:15:59 -040038 ASSERT(mReadLoc.cIndex < mLength[mReadLoc.sIndex]);
Olli Etuaho26e355b2015-08-14 14:16:19 +030039 ++mReadLoc.cIndex;
40 if (mReadLoc.cIndex == mLength[mReadLoc.sIndex])
41 {
42 ++mReadLoc.sIndex;
43 mReadLoc.cIndex = 0;
44 }
45 if (mReadLoc.sIndex >= mCount)
46 {
47 return nullptr;
48 }
49 return mString[mReadLoc.sIndex] + mReadLoc.cIndex;
50}
51
52size_t Input::read(char *buf, size_t maxSize, int *lineNo)
alokp@chromium.org98eec912012-05-01 10:04:08 +000053{
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000054 size_t nRead = 0;
Olli Etuaho26e355b2015-08-14 14:16:19 +030055 // The previous call to read might have stopped copying the string when encountering a line
56 // continuation. Check for this possibility first.
57 if (mReadLoc.sIndex < mCount && maxSize > 0)
58 {
59 const char *c = mString[mReadLoc.sIndex] + mReadLoc.cIndex;
60 if ((*c) == '\\')
61 {
62 c = skipChar();
63 if (c != nullptr && (*c) == '\n')
64 {
65 // Line continuation of backslash + newline.
66 skipChar();
Corentin Wallezd78e33a2017-10-30 12:33:52 -040067 // Fake an EOF if the line number would overflow.
68 if (*lineNo == INT_MAX)
69 {
70 return 0;
71 }
Olli Etuaho26e355b2015-08-14 14:16:19 +030072 ++(*lineNo);
73 }
74 else if (c != nullptr && (*c) == '\r')
75 {
76 // Line continuation. Could be backslash + '\r\n' or just backslash + '\r'.
77 c = skipChar();
78 if (c != nullptr && (*c) == '\n')
79 {
80 skipChar();
81 }
Corentin Wallezd78e33a2017-10-30 12:33:52 -040082 // Fake an EOF if the line number would overflow.
83 if (*lineNo == INT_MAX)
84 {
85 return 0;
86 }
Olli Etuaho26e355b2015-08-14 14:16:19 +030087 ++(*lineNo);
88 }
89 else
90 {
91 // Not line continuation, so write the skipped backslash to buf.
92 *buf = '\\';
93 ++nRead;
94 }
95 }
96 }
97
98 size_t maxRead = maxSize;
99 while ((nRead < maxRead) && (mReadLoc.sIndex < mCount))
alokp@chromium.org98eec912012-05-01 10:04:08 +0000100 {
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000101 size_t size = mLength[mReadLoc.sIndex] - mReadLoc.cIndex;
Jamie Madillf832c9d2016-12-12 17:38:48 -0500102 size = std::min(size, maxSize);
Olli Etuaho26e355b2015-08-14 14:16:19 +0300103 for (size_t i = 0; i < size; ++i)
104 {
105 // Stop if a possible line continuation is encountered.
106 // It will be processed on the next call on input, which skips it
107 // and increments line number if necessary.
108 if (*(mString[mReadLoc.sIndex] + mReadLoc.cIndex + i) == '\\')
109 {
Jamie Madillf832c9d2016-12-12 17:38:48 -0500110 size = i;
Olli Etuaho26e355b2015-08-14 14:16:19 +0300111 maxRead = nRead + size; // Stop reading right before the backslash.
112 }
113 }
daniel@transgaming.coma16a55f2012-12-20 20:51:54 +0000114 std::memcpy(buf + nRead, mString[mReadLoc.sIndex] + mReadLoc.cIndex, size);
alokp@chromium.org98eec912012-05-01 10:04:08 +0000115 nRead += size;
116 mReadLoc.cIndex += size;
117
118 // Advance string if we reached the end of current string.
119 if (mReadLoc.cIndex == mLength[mReadLoc.sIndex])
120 {
121 ++mReadLoc.sIndex;
122 mReadLoc.cIndex = 0;
123 }
124 }
125 return nRead;
126}
127
alokp@chromium.org4e4b8072011-08-07 05:36:04 +0000128} // namespace pp
Geoff Lang197d5292018-04-25 14:29:00 -0400129
130} // namespace angle