Line data Source code
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 :
7 : #include "compiler/preprocessor/Input.h"
8 :
9 : #include <algorithm>
10 : #include <cstring>
11 :
12 : #include "common/debug.h"
13 :
14 : namespace pp
15 : {
16 :
17 0 : Input::Input() : mCount(0), mString(0)
18 : {
19 0 : }
20 :
21 0 : Input::Input(size_t count, const char *const string[], const int length[]) :
22 : mCount(count),
23 0 : mString(string)
24 : {
25 0 : mLength.reserve(mCount);
26 0 : for (size_t i = 0; i < mCount; ++i)
27 : {
28 0 : int len = length ? length[i] : -1;
29 0 : mLength.push_back(len < 0 ? std::strlen(mString[i]) : len);
30 : }
31 0 : }
32 :
33 0 : const char *Input::skipChar()
34 : {
35 : // This function should only be called when there is a character to skip.
36 0 : ASSERT(mReadLoc.cIndex < mLength[mReadLoc.sIndex]);
37 0 : ++mReadLoc.cIndex;
38 0 : if (mReadLoc.cIndex == mLength[mReadLoc.sIndex])
39 : {
40 0 : ++mReadLoc.sIndex;
41 0 : mReadLoc.cIndex = 0;
42 : }
43 0 : if (mReadLoc.sIndex >= mCount)
44 : {
45 0 : return nullptr;
46 : }
47 0 : return mString[mReadLoc.sIndex] + mReadLoc.cIndex;
48 : }
49 :
50 0 : size_t Input::read(char *buf, size_t maxSize, int *lineNo)
51 : {
52 0 : size_t nRead = 0;
53 : // The previous call to read might have stopped copying the string when encountering a line
54 : // continuation. Check for this possibility first.
55 0 : if (mReadLoc.sIndex < mCount && maxSize > 0)
56 : {
57 0 : const char *c = mString[mReadLoc.sIndex] + mReadLoc.cIndex;
58 0 : if ((*c) == '\\')
59 : {
60 0 : c = skipChar();
61 0 : if (c != nullptr && (*c) == '\n')
62 : {
63 : // Line continuation of backslash + newline.
64 0 : skipChar();
65 0 : ++(*lineNo);
66 : }
67 0 : else if (c != nullptr && (*c) == '\r')
68 : {
69 : // Line continuation. Could be backslash + '\r\n' or just backslash + '\r'.
70 0 : c = skipChar();
71 0 : if (c != nullptr && (*c) == '\n')
72 : {
73 0 : skipChar();
74 : }
75 0 : ++(*lineNo);
76 : }
77 : else
78 : {
79 : // Not line continuation, so write the skipped backslash to buf.
80 0 : *buf = '\\';
81 0 : ++nRead;
82 : }
83 : }
84 : }
85 :
86 0 : size_t maxRead = maxSize;
87 0 : while ((nRead < maxRead) && (mReadLoc.sIndex < mCount))
88 : {
89 0 : size_t size = mLength[mReadLoc.sIndex] - mReadLoc.cIndex;
90 0 : size = std::min(size, maxSize);
91 0 : for (size_t i = 0; i < size; ++i)
92 : {
93 : // Stop if a possible line continuation is encountered.
94 : // It will be processed on the next call on input, which skips it
95 : // and increments line number if necessary.
96 0 : if (*(mString[mReadLoc.sIndex] + mReadLoc.cIndex + i) == '\\')
97 : {
98 0 : size = i;
99 0 : maxRead = nRead + size; // Stop reading right before the backslash.
100 : }
101 : }
102 0 : std::memcpy(buf + nRead, mString[mReadLoc.sIndex] + mReadLoc.cIndex, size);
103 0 : nRead += size;
104 0 : mReadLoc.cIndex += size;
105 :
106 : // Advance string if we reached the end of current string.
107 0 : if (mReadLoc.cIndex == mLength[mReadLoc.sIndex])
108 : {
109 0 : ++mReadLoc.sIndex;
110 0 : mReadLoc.cIndex = 0;
111 : }
112 : }
113 0 : return nRead;
114 : }
115 :
116 : } // namespace pp
117 :
|