blob: 0f2327b823d8889918b59b73451add78e4907b8d [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 Madillacf2f3a2017-11-21 19:22:44 -050021Input::~Input()
22{
23}
24
Jamie Madillf832c9d2016-12-12 17:38:48 -050025Input::Input(size_t count, const char *const string[], const int length[])
26 : mCount(count), mString(string)
alokp@chromium.org98eec912012-05-01 10:04:08 +000027{
alokp@chromium.org30a487c2012-05-02 17:30:46 +000028 mLength.reserve(mCount);
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000029 for (size_t i = 0; i < mCount; ++i)
alokp@chromium.org98eec912012-05-01 10:04:08 +000030 {
alokp@chromium.org30a487c2012-05-02 17:30:46 +000031 int len = length ? length[i] : -1;
daniel@transgaming.coma16a55f2012-12-20 20:51:54 +000032 mLength.push_back(len < 0 ? std::strlen(mString[i]) : len);
alokp@chromium.org98eec912012-05-01 10:04:08 +000033 }
34}
35
Olli Etuaho26e355b2015-08-14 14:16:19 +030036const char *Input::skipChar()
37{
38 // This function should only be called when there is a character to skip.
Corentin Wallez054f7ed2016-09-20 17:15:59 -040039 ASSERT(mReadLoc.cIndex < mLength[mReadLoc.sIndex]);
Olli Etuaho26e355b2015-08-14 14:16:19 +030040 ++mReadLoc.cIndex;
41 if (mReadLoc.cIndex == mLength[mReadLoc.sIndex])
42 {
43 ++mReadLoc.sIndex;
44 mReadLoc.cIndex = 0;
45 }
46 if (mReadLoc.sIndex >= mCount)
47 {
48 return nullptr;
49 }
50 return mString[mReadLoc.sIndex] + mReadLoc.cIndex;
51}
52
53size_t Input::read(char *buf, size_t maxSize, int *lineNo)
alokp@chromium.org98eec912012-05-01 10:04:08 +000054{
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +000055 size_t nRead = 0;
Olli Etuaho26e355b2015-08-14 14:16:19 +030056 // The previous call to read might have stopped copying the string when encountering a line
57 // continuation. Check for this possibility first.
58 if (mReadLoc.sIndex < mCount && maxSize > 0)
59 {
60 const char *c = mString[mReadLoc.sIndex] + mReadLoc.cIndex;
61 if ((*c) == '\\')
62 {
63 c = skipChar();
64 if (c != nullptr && (*c) == '\n')
65 {
66 // Line continuation of backslash + newline.
67 skipChar();
Corentin Wallezd78e33a2017-10-30 12:33:52 -040068 // Fake an EOF if the line number would overflow.
69 if (*lineNo == INT_MAX)
70 {
71 return 0;
72 }
Olli Etuaho26e355b2015-08-14 14:16:19 +030073 ++(*lineNo);
74 }
75 else if (c != nullptr && (*c) == '\r')
76 {
77 // Line continuation. Could be backslash + '\r\n' or just backslash + '\r'.
78 c = skipChar();
79 if (c != nullptr && (*c) == '\n')
80 {
81 skipChar();
82 }
Corentin Wallezd78e33a2017-10-30 12:33:52 -040083 // Fake an EOF if the line number would overflow.
84 if (*lineNo == INT_MAX)
85 {
86 return 0;
87 }
Olli Etuaho26e355b2015-08-14 14:16:19 +030088 ++(*lineNo);
89 }
90 else
91 {
92 // Not line continuation, so write the skipped backslash to buf.
93 *buf = '\\';
94 ++nRead;
95 }
96 }
97 }
98
99 size_t maxRead = maxSize;
100 while ((nRead < maxRead) && (mReadLoc.sIndex < mCount))
alokp@chromium.org98eec912012-05-01 10:04:08 +0000101 {
shannon.woods@transgaming.comd64b3da2013-02-28 23:19:26 +0000102 size_t size = mLength[mReadLoc.sIndex] - mReadLoc.cIndex;
Jamie Madillf832c9d2016-12-12 17:38:48 -0500103 size = std::min(size, maxSize);
Olli Etuaho26e355b2015-08-14 14:16:19 +0300104 for (size_t i = 0; i < size; ++i)
105 {
106 // Stop if a possible line continuation is encountered.
107 // It will be processed on the next call on input, which skips it
108 // and increments line number if necessary.
109 if (*(mString[mReadLoc.sIndex] + mReadLoc.cIndex + i) == '\\')
110 {
Jamie Madillf832c9d2016-12-12 17:38:48 -0500111 size = i;
Olli Etuaho26e355b2015-08-14 14:16:19 +0300112 maxRead = nRead + size; // Stop reading right before the backslash.
113 }
114 }
daniel@transgaming.coma16a55f2012-12-20 20:51:54 +0000115 std::memcpy(buf + nRead, mString[mReadLoc.sIndex] + mReadLoc.cIndex, size);
alokp@chromium.org98eec912012-05-01 10:04:08 +0000116 nRead += size;
117 mReadLoc.cIndex += size;
118
119 // Advance string if we reached the end of current string.
120 if (mReadLoc.cIndex == mLength[mReadLoc.sIndex])
121 {
122 ++mReadLoc.sIndex;
123 mReadLoc.cIndex = 0;
124 }
125 }
126 return nRead;
127}
128
alokp@chromium.org4e4b8072011-08-07 05:36:04 +0000129} // namespace pp