/src/llvm-project/clang/lib/Lex/LiteralSupport.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===--- LiteralSupport.cpp - Code to parse and process literals ----------===// |
2 | | // |
3 | | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | | // See https://llvm.org/LICENSE.txt for license information. |
5 | | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | | // |
7 | | //===----------------------------------------------------------------------===// |
8 | | // |
9 | | // This file implements the NumericLiteralParser, CharLiteralParser, and |
10 | | // StringLiteralParser interfaces. |
11 | | // |
12 | | //===----------------------------------------------------------------------===// |
13 | | |
14 | | #include "clang/Lex/LiteralSupport.h" |
15 | | #include "clang/Basic/CharInfo.h" |
16 | | #include "clang/Basic/LangOptions.h" |
17 | | #include "clang/Basic/SourceLocation.h" |
18 | | #include "clang/Basic/TargetInfo.h" |
19 | | #include "clang/Lex/LexDiagnostic.h" |
20 | | #include "clang/Lex/Lexer.h" |
21 | | #include "clang/Lex/Preprocessor.h" |
22 | | #include "clang/Lex/Token.h" |
23 | | #include "llvm/ADT/APInt.h" |
24 | | #include "llvm/ADT/SmallVector.h" |
25 | | #include "llvm/ADT/StringExtras.h" |
26 | | #include "llvm/ADT/StringSwitch.h" |
27 | | #include "llvm/Support/ConvertUTF.h" |
28 | | #include "llvm/Support/Error.h" |
29 | | #include "llvm/Support/ErrorHandling.h" |
30 | | #include "llvm/Support/Unicode.h" |
31 | | #include <algorithm> |
32 | | #include <cassert> |
33 | | #include <cstddef> |
34 | | #include <cstdint> |
35 | | #include <cstring> |
36 | | #include <string> |
37 | | |
38 | | using namespace clang; |
39 | | |
40 | 139 | static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target) { |
41 | 139 | switch (kind) { |
42 | 0 | default: llvm_unreachable("Unknown token type!"); |
43 | 0 | case tok::char_constant: |
44 | 139 | case tok::string_literal: |
45 | 139 | case tok::utf8_char_constant: |
46 | 139 | case tok::utf8_string_literal: |
47 | 139 | return Target.getCharWidth(); |
48 | 0 | case tok::wide_char_constant: |
49 | 0 | case tok::wide_string_literal: |
50 | 0 | return Target.getWCharWidth(); |
51 | 0 | case tok::utf16_char_constant: |
52 | 0 | case tok::utf16_string_literal: |
53 | 0 | return Target.getChar16Width(); |
54 | 0 | case tok::utf32_char_constant: |
55 | 0 | case tok::utf32_string_literal: |
56 | 0 | return Target.getChar32Width(); |
57 | 139 | } |
58 | 139 | } |
59 | | |
60 | 0 | static unsigned getEncodingPrefixLen(tok::TokenKind kind) { |
61 | 0 | switch (kind) { |
62 | 0 | default: |
63 | 0 | llvm_unreachable("Unknown token type!"); |
64 | 0 | case tok::char_constant: |
65 | 0 | case tok::string_literal: |
66 | 0 | return 0; |
67 | 0 | case tok::utf8_char_constant: |
68 | 0 | case tok::utf8_string_literal: |
69 | 0 | return 2; |
70 | 0 | case tok::wide_char_constant: |
71 | 0 | case tok::wide_string_literal: |
72 | 0 | case tok::utf16_char_constant: |
73 | 0 | case tok::utf16_string_literal: |
74 | 0 | case tok::utf32_char_constant: |
75 | 0 | case tok::utf32_string_literal: |
76 | 0 | return 1; |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | | static CharSourceRange MakeCharSourceRange(const LangOptions &Features, |
81 | | FullSourceLoc TokLoc, |
82 | | const char *TokBegin, |
83 | | const char *TokRangeBegin, |
84 | 17 | const char *TokRangeEnd) { |
85 | 17 | SourceLocation Begin = |
86 | 17 | Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin, |
87 | 17 | TokLoc.getManager(), Features); |
88 | 17 | SourceLocation End = |
89 | 17 | Lexer::AdvanceToTokenCharacter(Begin, TokRangeEnd - TokRangeBegin, |
90 | 17 | TokLoc.getManager(), Features); |
91 | 17 | return CharSourceRange::getCharRange(Begin, End); |
92 | 17 | } |
93 | | |
94 | | /// Produce a diagnostic highlighting some portion of a literal. |
95 | | /// |
96 | | /// Emits the diagnostic \p DiagID, highlighting the range of characters from |
97 | | /// \p TokRangeBegin (inclusive) to \p TokRangeEnd (exclusive), which must be |
98 | | /// a substring of a spelling buffer for the token beginning at \p TokBegin. |
99 | | static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, |
100 | | const LangOptions &Features, FullSourceLoc TokLoc, |
101 | | const char *TokBegin, const char *TokRangeBegin, |
102 | 1 | const char *TokRangeEnd, unsigned DiagID) { |
103 | 1 | SourceLocation Begin = |
104 | 1 | Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin, |
105 | 1 | TokLoc.getManager(), Features); |
106 | 1 | return Diags->Report(Begin, DiagID) << |
107 | 1 | MakeCharSourceRange(Features, TokLoc, TokBegin, TokRangeBegin, TokRangeEnd); |
108 | 1 | } |
109 | | |
110 | 0 | static bool IsEscapeValidInUnevaluatedStringLiteral(char Escape) { |
111 | 0 | switch (Escape) { |
112 | 0 | case '\'': |
113 | 0 | case '"': |
114 | 0 | case '?': |
115 | 0 | case '\\': |
116 | 0 | case 'a': |
117 | 0 | case 'b': |
118 | 0 | case 'f': |
119 | 0 | case 'n': |
120 | 0 | case 'r': |
121 | 0 | case 't': |
122 | 0 | case 'v': |
123 | 0 | return true; |
124 | 0 | } |
125 | 0 | return false; |
126 | 0 | } |
127 | | |
128 | | /// ProcessCharEscape - Parse a standard C escape sequence, which can occur in |
129 | | /// either a character or a string literal. |
130 | | static unsigned ProcessCharEscape(const char *ThisTokBegin, |
131 | | const char *&ThisTokBuf, |
132 | | const char *ThisTokEnd, bool &HadError, |
133 | | FullSourceLoc Loc, unsigned CharWidth, |
134 | | DiagnosticsEngine *Diags, |
135 | | const LangOptions &Features, |
136 | 0 | StringLiteralEvalMethod EvalMethod) { |
137 | 0 | const char *EscapeBegin = ThisTokBuf; |
138 | 0 | bool Delimited = false; |
139 | 0 | bool EndDelimiterFound = false; |
140 | | |
141 | | // Skip the '\' char. |
142 | 0 | ++ThisTokBuf; |
143 | | |
144 | | // We know that this character can't be off the end of the buffer, because |
145 | | // that would have been \", which would not have been the end of string. |
146 | 0 | unsigned ResultChar = *ThisTokBuf++; |
147 | 0 | char Escape = ResultChar; |
148 | 0 | switch (ResultChar) { |
149 | | // These map to themselves. |
150 | 0 | case '\\': case '\'': case '"': case '?': break; |
151 | | |
152 | | // These have fixed mappings. |
153 | 0 | case 'a': |
154 | | // TODO: K&R: the meaning of '\\a' is different in traditional C |
155 | 0 | ResultChar = 7; |
156 | 0 | break; |
157 | 0 | case 'b': |
158 | 0 | ResultChar = 8; |
159 | 0 | break; |
160 | 0 | case 'e': |
161 | 0 | if (Diags) |
162 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
163 | 0 | diag::ext_nonstandard_escape) << "e"; |
164 | 0 | ResultChar = 27; |
165 | 0 | break; |
166 | 0 | case 'E': |
167 | 0 | if (Diags) |
168 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
169 | 0 | diag::ext_nonstandard_escape) << "E"; |
170 | 0 | ResultChar = 27; |
171 | 0 | break; |
172 | 0 | case 'f': |
173 | 0 | ResultChar = 12; |
174 | 0 | break; |
175 | 0 | case 'n': |
176 | 0 | ResultChar = 10; |
177 | 0 | break; |
178 | 0 | case 'r': |
179 | 0 | ResultChar = 13; |
180 | 0 | break; |
181 | 0 | case 't': |
182 | 0 | ResultChar = 9; |
183 | 0 | break; |
184 | 0 | case 'v': |
185 | 0 | ResultChar = 11; |
186 | 0 | break; |
187 | 0 | case 'x': { // Hex escape. |
188 | 0 | ResultChar = 0; |
189 | 0 | if (ThisTokBuf != ThisTokEnd && *ThisTokBuf == '{') { |
190 | 0 | Delimited = true; |
191 | 0 | ThisTokBuf++; |
192 | 0 | if (*ThisTokBuf == '}') { |
193 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
194 | 0 | diag::err_delimited_escape_empty); |
195 | 0 | return ResultChar; |
196 | 0 | } |
197 | 0 | } else if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) { |
198 | 0 | if (Diags) |
199 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
200 | 0 | diag::err_hex_escape_no_digits) << "x"; |
201 | 0 | return ResultChar; |
202 | 0 | } |
203 | | |
204 | | // Hex escapes are a maximal series of hex digits. |
205 | 0 | bool Overflow = false; |
206 | 0 | for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) { |
207 | 0 | if (Delimited && *ThisTokBuf == '}') { |
208 | 0 | ThisTokBuf++; |
209 | 0 | EndDelimiterFound = true; |
210 | 0 | break; |
211 | 0 | } |
212 | 0 | int CharVal = llvm::hexDigitValue(*ThisTokBuf); |
213 | 0 | if (CharVal == -1) { |
214 | | // Non delimited hex escape sequences stop at the first non-hex digit. |
215 | 0 | if (!Delimited) |
216 | 0 | break; |
217 | 0 | HadError = true; |
218 | 0 | if (Diags) |
219 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
220 | 0 | diag::err_delimited_escape_invalid) |
221 | 0 | << StringRef(ThisTokBuf, 1); |
222 | 0 | continue; |
223 | 0 | } |
224 | | // About to shift out a digit? |
225 | 0 | if (ResultChar & 0xF0000000) |
226 | 0 | Overflow = true; |
227 | 0 | ResultChar <<= 4; |
228 | 0 | ResultChar |= CharVal; |
229 | 0 | } |
230 | | // See if any bits will be truncated when evaluated as a character. |
231 | 0 | if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { |
232 | 0 | Overflow = true; |
233 | 0 | ResultChar &= ~0U >> (32-CharWidth); |
234 | 0 | } |
235 | | |
236 | | // Check for overflow. |
237 | 0 | if (!HadError && Overflow) { // Too many digits to fit in |
238 | 0 | HadError = true; |
239 | 0 | if (Diags) |
240 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
241 | 0 | diag::err_escape_too_large) |
242 | 0 | << 0; |
243 | 0 | } |
244 | 0 | break; |
245 | 0 | } |
246 | 0 | case '0': case '1': case '2': case '3': |
247 | 0 | case '4': case '5': case '6': case '7': { |
248 | | // Octal escapes. |
249 | 0 | --ThisTokBuf; |
250 | 0 | ResultChar = 0; |
251 | | |
252 | | // Octal escapes are a series of octal digits with maximum length 3. |
253 | | // "\0123" is a two digit sequence equal to "\012" "3". |
254 | 0 | unsigned NumDigits = 0; |
255 | 0 | do { |
256 | 0 | ResultChar <<= 3; |
257 | 0 | ResultChar |= *ThisTokBuf++ - '0'; |
258 | 0 | ++NumDigits; |
259 | 0 | } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 && |
260 | 0 | ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7'); |
261 | | |
262 | | // Check for overflow. Reject '\777', but not L'\777'. |
263 | 0 | if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { |
264 | 0 | if (Diags) |
265 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
266 | 0 | diag::err_escape_too_large) << 1; |
267 | 0 | ResultChar &= ~0U >> (32-CharWidth); |
268 | 0 | } |
269 | 0 | break; |
270 | 0 | } |
271 | 0 | case 'o': { |
272 | 0 | bool Overflow = false; |
273 | 0 | if (ThisTokBuf == ThisTokEnd || *ThisTokBuf != '{') { |
274 | 0 | HadError = true; |
275 | 0 | if (Diags) |
276 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
277 | 0 | diag::err_delimited_escape_missing_brace) |
278 | 0 | << "o"; |
279 | |
|
280 | 0 | break; |
281 | 0 | } |
282 | 0 | ResultChar = 0; |
283 | 0 | Delimited = true; |
284 | 0 | ++ThisTokBuf; |
285 | 0 | if (*ThisTokBuf == '}') { |
286 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
287 | 0 | diag::err_delimited_escape_empty); |
288 | 0 | return ResultChar; |
289 | 0 | } |
290 | | |
291 | 0 | while (ThisTokBuf != ThisTokEnd) { |
292 | 0 | if (*ThisTokBuf == '}') { |
293 | 0 | EndDelimiterFound = true; |
294 | 0 | ThisTokBuf++; |
295 | 0 | break; |
296 | 0 | } |
297 | 0 | if (*ThisTokBuf < '0' || *ThisTokBuf > '7') { |
298 | 0 | HadError = true; |
299 | 0 | if (Diags) |
300 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
301 | 0 | diag::err_delimited_escape_invalid) |
302 | 0 | << StringRef(ThisTokBuf, 1); |
303 | 0 | ThisTokBuf++; |
304 | 0 | continue; |
305 | 0 | } |
306 | | // Check if one of the top three bits is set before shifting them out. |
307 | 0 | if (ResultChar & 0xE0000000) |
308 | 0 | Overflow = true; |
309 | |
|
310 | 0 | ResultChar <<= 3; |
311 | 0 | ResultChar |= *ThisTokBuf++ - '0'; |
312 | 0 | } |
313 | | // Check for overflow. Reject '\777', but not L'\777'. |
314 | 0 | if (!HadError && |
315 | 0 | (Overflow || (CharWidth != 32 && (ResultChar >> CharWidth) != 0))) { |
316 | 0 | HadError = true; |
317 | 0 | if (Diags) |
318 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
319 | 0 | diag::err_escape_too_large) |
320 | 0 | << 1; |
321 | 0 | ResultChar &= ~0U >> (32 - CharWidth); |
322 | 0 | } |
323 | 0 | break; |
324 | 0 | } |
325 | | // Otherwise, these are not valid escapes. |
326 | 0 | case '(': case '{': case '[': case '%': |
327 | | // GCC accepts these as extensions. We warn about them as such though. |
328 | 0 | if (Diags) |
329 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
330 | 0 | diag::ext_nonstandard_escape) |
331 | 0 | << std::string(1, ResultChar); |
332 | 0 | break; |
333 | 0 | default: |
334 | 0 | if (!Diags) |
335 | 0 | break; |
336 | | |
337 | 0 | if (isPrintable(ResultChar)) |
338 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
339 | 0 | diag::ext_unknown_escape) |
340 | 0 | << std::string(1, ResultChar); |
341 | 0 | else |
342 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
343 | 0 | diag::ext_unknown_escape) |
344 | 0 | << "x" + llvm::utohexstr(ResultChar); |
345 | 0 | break; |
346 | 0 | } |
347 | | |
348 | 0 | if (Delimited && Diags) { |
349 | 0 | if (!EndDelimiterFound) |
350 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
351 | 0 | diag::err_expected) |
352 | 0 | << tok::r_brace; |
353 | 0 | else if (!HadError) { |
354 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
355 | 0 | Features.CPlusPlus23 ? diag::warn_cxx23_delimited_escape_sequence |
356 | 0 | : diag::ext_delimited_escape_sequence) |
357 | 0 | << /*delimited*/ 0 << (Features.CPlusPlus ? 1 : 0); |
358 | 0 | } |
359 | 0 | } |
360 | |
|
361 | 0 | if (EvalMethod == StringLiteralEvalMethod::Unevaluated && |
362 | 0 | !IsEscapeValidInUnevaluatedStringLiteral(Escape)) { |
363 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf, |
364 | 0 | diag::err_unevaluated_string_invalid_escape_sequence) |
365 | 0 | << StringRef(EscapeBegin, ThisTokBuf - EscapeBegin); |
366 | 0 | HadError = true; |
367 | 0 | } |
368 | |
|
369 | 0 | return ResultChar; |
370 | 0 | } |
371 | | |
372 | | static void appendCodePoint(unsigned Codepoint, |
373 | 127k | llvm::SmallVectorImpl<char> &Str) { |
374 | 127k | char ResultBuf[4]; |
375 | 127k | char *ResultPtr = ResultBuf; |
376 | 127k | if (llvm::ConvertCodePointToUTF8(Codepoint, ResultPtr)) |
377 | 125k | Str.append(ResultBuf, ResultPtr); |
378 | 127k | } |
379 | | |
380 | 13.6k | void clang::expandUCNs(SmallVectorImpl<char> &Buf, StringRef Input) { |
381 | 3.66M | for (StringRef::iterator I = Input.begin(), E = Input.end(); I != E; ++I) { |
382 | 3.65M | if (*I != '\\') { |
383 | 3.52M | Buf.push_back(*I); |
384 | 3.52M | continue; |
385 | 3.52M | } |
386 | | |
387 | 127k | ++I; |
388 | 127k | char Kind = *I; |
389 | 127k | ++I; |
390 | | |
391 | 127k | assert(Kind == 'u' || Kind == 'U' || Kind == 'N'); |
392 | 0 | uint32_t CodePoint = 0; |
393 | | |
394 | 127k | if (Kind == 'u' && *I == '{') { |
395 | 10.6k | for (++I; *I != '}'; ++I) { |
396 | 8.49k | unsigned Value = llvm::hexDigitValue(*I); |
397 | 8.49k | assert(Value != -1U); |
398 | 0 | CodePoint <<= 4; |
399 | 8.49k | CodePoint += Value; |
400 | 8.49k | } |
401 | 2.15k | appendCodePoint(CodePoint, Buf); |
402 | 2.15k | continue; |
403 | 2.15k | } |
404 | | |
405 | 124k | if (Kind == 'N') { |
406 | 13.6k | assert(*I == '{'); |
407 | 0 | ++I; |
408 | 13.6k | auto Delim = std::find(I, Input.end(), '}'); |
409 | 13.6k | assert(Delim != Input.end()); |
410 | 0 | StringRef Name(I, std::distance(I, Delim)); |
411 | 13.6k | std::optional<llvm::sys::unicode::LooseMatchingResult> Res = |
412 | 13.6k | llvm::sys::unicode::nameToCodepointLooseMatching(Name); |
413 | 13.6k | assert(Res && "could not find a codepoint that was previously found"); |
414 | 0 | CodePoint = Res->CodePoint; |
415 | 13.6k | assert(CodePoint != 0xFFFFFFFF); |
416 | 0 | appendCodePoint(CodePoint, Buf); |
417 | 13.6k | I = Delim; |
418 | 13.6k | continue; |
419 | 13.6k | } |
420 | | |
421 | 111k | unsigned NumHexDigits; |
422 | 111k | if (Kind == 'u') |
423 | 110k | NumHexDigits = 4; |
424 | 361 | else |
425 | 361 | NumHexDigits = 8; |
426 | | |
427 | 111k | assert(I + NumHexDigits <= E); |
428 | | |
429 | 557k | for (; NumHexDigits != 0; ++I, --NumHexDigits) { |
430 | 446k | unsigned Value = llvm::hexDigitValue(*I); |
431 | 446k | assert(Value != -1U); |
432 | | |
433 | 0 | CodePoint <<= 4; |
434 | 446k | CodePoint += Value; |
435 | 446k | } |
436 | | |
437 | 111k | appendCodePoint(CodePoint, Buf); |
438 | 111k | --I; |
439 | 111k | } |
440 | 13.6k | } |
441 | | |
442 | | bool clang::isFunctionLocalStringLiteralMacro(tok::TokenKind K, |
443 | 1 | const LangOptions &LO) { |
444 | 1 | return LO.MicrosoftExt && |
445 | 1 | (K == tok::kw___FUNCTION__ || K == tok::kw_L__FUNCTION__ || |
446 | 0 | K == tok::kw___FUNCSIG__ || K == tok::kw_L__FUNCSIG__ || |
447 | 0 | K == tok::kw___FUNCDNAME__); |
448 | 1 | } |
449 | | |
450 | 2 | bool clang::tokenIsLikeStringLiteral(const Token &Tok, const LangOptions &LO) { |
451 | 2 | return tok::isStringLiteral(Tok.getKind()) || |
452 | 2 | isFunctionLocalStringLiteralMacro(Tok.getKind(), LO); |
453 | 2 | } |
454 | | |
455 | | static bool ProcessNumericUCNEscape(const char *ThisTokBegin, |
456 | | const char *&ThisTokBuf, |
457 | | const char *ThisTokEnd, uint32_t &UcnVal, |
458 | | unsigned short &UcnLen, bool &Delimited, |
459 | | FullSourceLoc Loc, DiagnosticsEngine *Diags, |
460 | | const LangOptions &Features, |
461 | 0 | bool in_char_string_literal = false) { |
462 | 0 | const char *UcnBegin = ThisTokBuf; |
463 | 0 | bool HasError = false; |
464 | 0 | bool EndDelimiterFound = false; |
465 | | |
466 | | // Skip the '\u' char's. |
467 | 0 | ThisTokBuf += 2; |
468 | 0 | Delimited = false; |
469 | 0 | if (UcnBegin[1] == 'u' && in_char_string_literal && |
470 | 0 | ThisTokBuf != ThisTokEnd && *ThisTokBuf == '{') { |
471 | 0 | Delimited = true; |
472 | 0 | ThisTokBuf++; |
473 | 0 | } else if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) { |
474 | 0 | if (Diags) |
475 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
476 | 0 | diag::err_hex_escape_no_digits) |
477 | 0 | << StringRef(&ThisTokBuf[-1], 1); |
478 | 0 | return false; |
479 | 0 | } |
480 | 0 | UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8); |
481 | |
|
482 | 0 | bool Overflow = false; |
483 | 0 | unsigned short Count = 0; |
484 | 0 | for (; ThisTokBuf != ThisTokEnd && (Delimited || Count != UcnLen); |
485 | 0 | ++ThisTokBuf) { |
486 | 0 | if (Delimited && *ThisTokBuf == '}') { |
487 | 0 | ++ThisTokBuf; |
488 | 0 | EndDelimiterFound = true; |
489 | 0 | break; |
490 | 0 | } |
491 | 0 | int CharVal = llvm::hexDigitValue(*ThisTokBuf); |
492 | 0 | if (CharVal == -1) { |
493 | 0 | HasError = true; |
494 | 0 | if (!Delimited) |
495 | 0 | break; |
496 | 0 | if (Diags) { |
497 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
498 | 0 | diag::err_delimited_escape_invalid) |
499 | 0 | << StringRef(ThisTokBuf, 1); |
500 | 0 | } |
501 | 0 | Count++; |
502 | 0 | continue; |
503 | 0 | } |
504 | 0 | if (UcnVal & 0xF0000000) { |
505 | 0 | Overflow = true; |
506 | 0 | continue; |
507 | 0 | } |
508 | 0 | UcnVal <<= 4; |
509 | 0 | UcnVal |= CharVal; |
510 | 0 | Count++; |
511 | 0 | } |
512 | |
|
513 | 0 | if (Overflow) { |
514 | 0 | if (Diags) |
515 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
516 | 0 | diag::err_escape_too_large) |
517 | 0 | << 0; |
518 | 0 | return false; |
519 | 0 | } |
520 | | |
521 | 0 | if (Delimited && !EndDelimiterFound) { |
522 | 0 | if (Diags) { |
523 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
524 | 0 | diag::err_expected) |
525 | 0 | << tok::r_brace; |
526 | 0 | } |
527 | 0 | return false; |
528 | 0 | } |
529 | | |
530 | | // If we didn't consume the proper number of digits, there is a problem. |
531 | 0 | if (Count == 0 || (!Delimited && Count != UcnLen)) { |
532 | 0 | if (Diags) |
533 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
534 | 0 | Delimited ? diag::err_delimited_escape_empty |
535 | 0 | : diag::err_ucn_escape_incomplete); |
536 | 0 | return false; |
537 | 0 | } |
538 | 0 | return !HasError; |
539 | 0 | } |
540 | | |
541 | | static void DiagnoseInvalidUnicodeCharacterName( |
542 | | DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc Loc, |
543 | | const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, |
544 | 0 | llvm::StringRef Name) { |
545 | |
|
546 | 0 | Diag(Diags, Features, Loc, TokBegin, TokRangeBegin, TokRangeEnd, |
547 | 0 | diag::err_invalid_ucn_name) |
548 | 0 | << Name; |
549 | |
|
550 | 0 | namespace u = llvm::sys::unicode; |
551 | |
|
552 | 0 | std::optional<u::LooseMatchingResult> Res = |
553 | 0 | u::nameToCodepointLooseMatching(Name); |
554 | 0 | if (Res) { |
555 | 0 | Diag(Diags, Features, Loc, TokBegin, TokRangeBegin, TokRangeEnd, |
556 | 0 | diag::note_invalid_ucn_name_loose_matching) |
557 | 0 | << FixItHint::CreateReplacement( |
558 | 0 | MakeCharSourceRange(Features, Loc, TokBegin, TokRangeBegin, |
559 | 0 | TokRangeEnd), |
560 | 0 | Res->Name); |
561 | 0 | return; |
562 | 0 | } |
563 | | |
564 | 0 | unsigned Distance = 0; |
565 | 0 | SmallVector<u::MatchForCodepointName> Matches = |
566 | 0 | u::nearestMatchesForCodepointName(Name, 5); |
567 | 0 | assert(!Matches.empty() && "No unicode characters found"); |
568 | | |
569 | 0 | for (const auto &Match : Matches) { |
570 | 0 | if (Distance == 0) |
571 | 0 | Distance = Match.Distance; |
572 | 0 | if (std::max(Distance, Match.Distance) - |
573 | 0 | std::min(Distance, Match.Distance) > |
574 | 0 | 3) |
575 | 0 | break; |
576 | 0 | Distance = Match.Distance; |
577 | |
|
578 | 0 | std::string Str; |
579 | 0 | llvm::UTF32 V = Match.Value; |
580 | 0 | bool Converted = |
581 | 0 | llvm::convertUTF32ToUTF8String(llvm::ArrayRef<llvm::UTF32>(&V, 1), Str); |
582 | 0 | (void)Converted; |
583 | 0 | assert(Converted && "Found a match wich is not a unicode character"); |
584 | | |
585 | 0 | Diag(Diags, Features, Loc, TokBegin, TokRangeBegin, TokRangeEnd, |
586 | 0 | diag::note_invalid_ucn_name_candidate) |
587 | 0 | << Match.Name << llvm::utohexstr(Match.Value) |
588 | 0 | << Str // FIXME: Fix the rendering of non printable characters |
589 | 0 | << FixItHint::CreateReplacement( |
590 | 0 | MakeCharSourceRange(Features, Loc, TokBegin, TokRangeBegin, |
591 | 0 | TokRangeEnd), |
592 | 0 | Match.Name); |
593 | 0 | } |
594 | 0 | } |
595 | | |
596 | | static bool ProcessNamedUCNEscape(const char *ThisTokBegin, |
597 | | const char *&ThisTokBuf, |
598 | | const char *ThisTokEnd, uint32_t &UcnVal, |
599 | | unsigned short &UcnLen, FullSourceLoc Loc, |
600 | | DiagnosticsEngine *Diags, |
601 | 0 | const LangOptions &Features) { |
602 | 0 | const char *UcnBegin = ThisTokBuf; |
603 | 0 | assert(UcnBegin[0] == '\\' && UcnBegin[1] == 'N'); |
604 | 0 | ThisTokBuf += 2; |
605 | 0 | if (ThisTokBuf == ThisTokEnd || *ThisTokBuf != '{') { |
606 | 0 | if (Diags) { |
607 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
608 | 0 | diag::err_delimited_escape_missing_brace) |
609 | 0 | << StringRef(&ThisTokBuf[-1], 1); |
610 | 0 | } |
611 | 0 | return false; |
612 | 0 | } |
613 | 0 | ThisTokBuf++; |
614 | 0 | const char *ClosingBrace = std::find_if(ThisTokBuf, ThisTokEnd, [](char C) { |
615 | 0 | return C == '}' || isVerticalWhitespace(C); |
616 | 0 | }); |
617 | 0 | bool Incomplete = ClosingBrace == ThisTokEnd; |
618 | 0 | bool Empty = ClosingBrace == ThisTokBuf; |
619 | 0 | if (Incomplete || Empty) { |
620 | 0 | if (Diags) { |
621 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
622 | 0 | Incomplete ? diag::err_ucn_escape_incomplete |
623 | 0 | : diag::err_delimited_escape_empty) |
624 | 0 | << StringRef(&UcnBegin[1], 1); |
625 | 0 | } |
626 | 0 | ThisTokBuf = ClosingBrace == ThisTokEnd ? ClosingBrace : ClosingBrace + 1; |
627 | 0 | return false; |
628 | 0 | } |
629 | 0 | StringRef Name(ThisTokBuf, ClosingBrace - ThisTokBuf); |
630 | 0 | ThisTokBuf = ClosingBrace + 1; |
631 | 0 | std::optional<char32_t> Res = llvm::sys::unicode::nameToCodepointStrict(Name); |
632 | 0 | if (!Res) { |
633 | 0 | if (Diags) |
634 | 0 | DiagnoseInvalidUnicodeCharacterName(Diags, Features, Loc, ThisTokBegin, |
635 | 0 | &UcnBegin[3], ClosingBrace, Name); |
636 | 0 | return false; |
637 | 0 | } |
638 | 0 | UcnVal = *Res; |
639 | 0 | UcnLen = UcnVal > 0xFFFF ? 8 : 4; |
640 | 0 | return true; |
641 | 0 | } |
642 | | |
643 | | /// ProcessUCNEscape - Read the Universal Character Name, check constraints and |
644 | | /// return the UTF32. |
645 | | static bool ProcessUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, |
646 | | const char *ThisTokEnd, uint32_t &UcnVal, |
647 | | unsigned short &UcnLen, FullSourceLoc Loc, |
648 | | DiagnosticsEngine *Diags, |
649 | | const LangOptions &Features, |
650 | 0 | bool in_char_string_literal = false) { |
651 | |
|
652 | 0 | bool HasError; |
653 | 0 | const char *UcnBegin = ThisTokBuf; |
654 | 0 | bool IsDelimitedEscapeSequence = false; |
655 | 0 | bool IsNamedEscapeSequence = false; |
656 | 0 | if (ThisTokBuf[1] == 'N') { |
657 | 0 | IsNamedEscapeSequence = true; |
658 | 0 | HasError = !ProcessNamedUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, |
659 | 0 | UcnVal, UcnLen, Loc, Diags, Features); |
660 | 0 | } else { |
661 | 0 | HasError = |
662 | 0 | !ProcessNumericUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, |
663 | 0 | UcnLen, IsDelimitedEscapeSequence, Loc, Diags, |
664 | 0 | Features, in_char_string_literal); |
665 | 0 | } |
666 | 0 | if (HasError) |
667 | 0 | return false; |
668 | | |
669 | | // Check UCN constraints (C99 6.4.3p2) [C++11 lex.charset p2] |
670 | 0 | if ((0xD800 <= UcnVal && UcnVal <= 0xDFFF) || // surrogate codepoints |
671 | 0 | UcnVal > 0x10FFFF) { // maximum legal UTF32 value |
672 | 0 | if (Diags) |
673 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
674 | 0 | diag::err_ucn_escape_invalid); |
675 | 0 | return false; |
676 | 0 | } |
677 | | |
678 | | // C23 and C++11 allow UCNs that refer to control characters |
679 | | // and basic source characters inside character and string literals |
680 | 0 | if (UcnVal < 0xa0 && |
681 | | // $, @, ` are allowed in all language modes |
682 | 0 | (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60)) { |
683 | 0 | bool IsError = |
684 | 0 | (!(Features.CPlusPlus11 || Features.C23) || !in_char_string_literal); |
685 | 0 | if (Diags) { |
686 | 0 | char BasicSCSChar = UcnVal; |
687 | 0 | if (UcnVal >= 0x20 && UcnVal < 0x7f) |
688 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
689 | 0 | IsError ? diag::err_ucn_escape_basic_scs |
690 | 0 | : Features.CPlusPlus |
691 | 0 | ? diag::warn_cxx98_compat_literal_ucn_escape_basic_scs |
692 | 0 | : diag::warn_c23_compat_literal_ucn_escape_basic_scs) |
693 | 0 | << StringRef(&BasicSCSChar, 1); |
694 | 0 | else |
695 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
696 | 0 | IsError ? diag::err_ucn_control_character |
697 | 0 | : Features.CPlusPlus |
698 | 0 | ? diag::warn_cxx98_compat_literal_ucn_control_character |
699 | 0 | : diag::warn_c23_compat_literal_ucn_control_character); |
700 | 0 | } |
701 | 0 | if (IsError) |
702 | 0 | return false; |
703 | 0 | } |
704 | | |
705 | 0 | if (!Features.CPlusPlus && !Features.C99 && Diags) |
706 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
707 | 0 | diag::warn_ucn_not_valid_in_c89_literal); |
708 | |
|
709 | 0 | if ((IsDelimitedEscapeSequence || IsNamedEscapeSequence) && Diags) |
710 | 0 | Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf, |
711 | 0 | Features.CPlusPlus23 ? diag::warn_cxx23_delimited_escape_sequence |
712 | 0 | : diag::ext_delimited_escape_sequence) |
713 | 0 | << (IsNamedEscapeSequence ? 1 : 0) << (Features.CPlusPlus ? 1 : 0); |
714 | |
|
715 | 0 | return true; |
716 | 0 | } |
717 | | |
718 | | /// MeasureUCNEscape - Determine the number of bytes within the resulting string |
719 | | /// which this UCN will occupy. |
720 | | static int MeasureUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, |
721 | | const char *ThisTokEnd, unsigned CharByteWidth, |
722 | 0 | const LangOptions &Features, bool &HadError) { |
723 | | // UTF-32: 4 bytes per escape. |
724 | 0 | if (CharByteWidth == 4) |
725 | 0 | return 4; |
726 | | |
727 | 0 | uint32_t UcnVal = 0; |
728 | 0 | unsigned short UcnLen = 0; |
729 | 0 | FullSourceLoc Loc; |
730 | |
|
731 | 0 | if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, |
732 | 0 | UcnLen, Loc, nullptr, Features, true)) { |
733 | 0 | HadError = true; |
734 | 0 | return 0; |
735 | 0 | } |
736 | | |
737 | | // UTF-16: 2 bytes for BMP, 4 bytes otherwise. |
738 | 0 | if (CharByteWidth == 2) |
739 | 0 | return UcnVal <= 0xFFFF ? 2 : 4; |
740 | | |
741 | | // UTF-8. |
742 | 0 | if (UcnVal < 0x80) |
743 | 0 | return 1; |
744 | 0 | if (UcnVal < 0x800) |
745 | 0 | return 2; |
746 | 0 | if (UcnVal < 0x10000) |
747 | 0 | return 3; |
748 | 0 | return 4; |
749 | 0 | } |
750 | | |
751 | | /// EncodeUCNEscape - Read the Universal Character Name, check constraints and |
752 | | /// convert the UTF32 to UTF8 or UTF16. This is a subroutine of |
753 | | /// StringLiteralParser. When we decide to implement UCN's for identifiers, |
754 | | /// we will likely rework our support for UCN's. |
755 | | static void EncodeUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf, |
756 | | const char *ThisTokEnd, |
757 | | char *&ResultBuf, bool &HadError, |
758 | | FullSourceLoc Loc, unsigned CharByteWidth, |
759 | | DiagnosticsEngine *Diags, |
760 | 0 | const LangOptions &Features) { |
761 | 0 | typedef uint32_t UTF32; |
762 | 0 | UTF32 UcnVal = 0; |
763 | 0 | unsigned short UcnLen = 0; |
764 | 0 | if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, UcnLen, |
765 | 0 | Loc, Diags, Features, true)) { |
766 | 0 | HadError = true; |
767 | 0 | return; |
768 | 0 | } |
769 | | |
770 | 0 | assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth == 4) && |
771 | 0 | "only character widths of 1, 2, or 4 bytes supported"); |
772 | | |
773 | 0 | (void)UcnLen; |
774 | 0 | assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported"); |
775 | | |
776 | 0 | if (CharByteWidth == 4) { |
777 | | // FIXME: Make the type of the result buffer correct instead of |
778 | | // using reinterpret_cast. |
779 | 0 | llvm::UTF32 *ResultPtr = reinterpret_cast<llvm::UTF32*>(ResultBuf); |
780 | 0 | *ResultPtr = UcnVal; |
781 | 0 | ResultBuf += 4; |
782 | 0 | return; |
783 | 0 | } |
784 | | |
785 | 0 | if (CharByteWidth == 2) { |
786 | | // FIXME: Make the type of the result buffer correct instead of |
787 | | // using reinterpret_cast. |
788 | 0 | llvm::UTF16 *ResultPtr = reinterpret_cast<llvm::UTF16*>(ResultBuf); |
789 | |
|
790 | 0 | if (UcnVal <= (UTF32)0xFFFF) { |
791 | 0 | *ResultPtr = UcnVal; |
792 | 0 | ResultBuf += 2; |
793 | 0 | return; |
794 | 0 | } |
795 | | |
796 | | // Convert to UTF16. |
797 | 0 | UcnVal -= 0x10000; |
798 | 0 | *ResultPtr = 0xD800 + (UcnVal >> 10); |
799 | 0 | *(ResultPtr+1) = 0xDC00 + (UcnVal & 0x3FF); |
800 | 0 | ResultBuf += 4; |
801 | 0 | return; |
802 | 0 | } |
803 | | |
804 | 0 | assert(CharByteWidth == 1 && "UTF-8 encoding is only for 1 byte characters"); |
805 | | |
806 | | // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8. |
807 | | // The conversion below was inspired by: |
808 | | // http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c |
809 | | // First, we determine how many bytes the result will require. |
810 | 0 | typedef uint8_t UTF8; |
811 | |
|
812 | 0 | unsigned short bytesToWrite = 0; |
813 | 0 | if (UcnVal < (UTF32)0x80) |
814 | 0 | bytesToWrite = 1; |
815 | 0 | else if (UcnVal < (UTF32)0x800) |
816 | 0 | bytesToWrite = 2; |
817 | 0 | else if (UcnVal < (UTF32)0x10000) |
818 | 0 | bytesToWrite = 3; |
819 | 0 | else |
820 | 0 | bytesToWrite = 4; |
821 | |
|
822 | 0 | const unsigned byteMask = 0xBF; |
823 | 0 | const unsigned byteMark = 0x80; |
824 | | |
825 | | // Once the bits are split out into bytes of UTF8, this is a mask OR-ed |
826 | | // into the first byte, depending on how many bytes follow. |
827 | 0 | static const UTF8 firstByteMark[5] = { |
828 | 0 | 0x00, 0x00, 0xC0, 0xE0, 0xF0 |
829 | 0 | }; |
830 | | // Finally, we write the bytes into ResultBuf. |
831 | 0 | ResultBuf += bytesToWrite; |
832 | 0 | switch (bytesToWrite) { // note: everything falls through. |
833 | 0 | case 4: |
834 | 0 | *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6; |
835 | 0 | [[fallthrough]]; |
836 | 0 | case 3: |
837 | 0 | *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6; |
838 | 0 | [[fallthrough]]; |
839 | 0 | case 2: |
840 | 0 | *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6; |
841 | 0 | [[fallthrough]]; |
842 | 0 | case 1: |
843 | 0 | *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]); |
844 | 0 | } |
845 | | // Update the buffer. |
846 | 0 | ResultBuf += bytesToWrite; |
847 | 0 | } |
848 | | |
849 | | /// integer-constant: [C99 6.4.4.1] |
850 | | /// decimal-constant integer-suffix |
851 | | /// octal-constant integer-suffix |
852 | | /// hexadecimal-constant integer-suffix |
853 | | /// binary-literal integer-suffix [GNU, C++1y] |
854 | | /// user-defined-integer-literal: [C++11 lex.ext] |
855 | | /// decimal-literal ud-suffix |
856 | | /// octal-literal ud-suffix |
857 | | /// hexadecimal-literal ud-suffix |
858 | | /// binary-literal ud-suffix [GNU, C++1y] |
859 | | /// decimal-constant: |
860 | | /// nonzero-digit |
861 | | /// decimal-constant digit |
862 | | /// octal-constant: |
863 | | /// 0 |
864 | | /// octal-constant octal-digit |
865 | | /// hexadecimal-constant: |
866 | | /// hexadecimal-prefix hexadecimal-digit |
867 | | /// hexadecimal-constant hexadecimal-digit |
868 | | /// hexadecimal-prefix: one of |
869 | | /// 0x 0X |
870 | | /// binary-literal: |
871 | | /// 0b binary-digit |
872 | | /// 0B binary-digit |
873 | | /// binary-literal binary-digit |
874 | | /// integer-suffix: |
875 | | /// unsigned-suffix [long-suffix] |
876 | | /// unsigned-suffix [long-long-suffix] |
877 | | /// long-suffix [unsigned-suffix] |
878 | | /// long-long-suffix [unsigned-sufix] |
879 | | /// nonzero-digit: |
880 | | /// 1 2 3 4 5 6 7 8 9 |
881 | | /// octal-digit: |
882 | | /// 0 1 2 3 4 5 6 7 |
883 | | /// hexadecimal-digit: |
884 | | /// 0 1 2 3 4 5 6 7 8 9 |
885 | | /// a b c d e f |
886 | | /// A B C D E F |
887 | | /// binary-digit: |
888 | | /// 0 |
889 | | /// 1 |
890 | | /// unsigned-suffix: one of |
891 | | /// u U |
892 | | /// long-suffix: one of |
893 | | /// l L |
894 | | /// long-long-suffix: one of |
895 | | /// ll LL |
896 | | /// |
897 | | /// floating-constant: [C99 6.4.4.2] |
898 | | /// TODO: add rules... |
899 | | /// |
900 | | NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling, |
901 | | SourceLocation TokLoc, |
902 | | const SourceManager &SM, |
903 | | const LangOptions &LangOpts, |
904 | | const TargetInfo &Target, |
905 | | DiagnosticsEngine &Diags) |
906 | | : SM(SM), LangOpts(LangOpts), Diags(Diags), |
907 | 33 | ThisTokBegin(TokSpelling.begin()), ThisTokEnd(TokSpelling.end()) { |
908 | | |
909 | 33 | s = DigitsBegin = ThisTokBegin; |
910 | 33 | saw_exponent = false; |
911 | 33 | saw_period = false; |
912 | 33 | saw_ud_suffix = false; |
913 | 33 | saw_fixed_point_suffix = false; |
914 | 33 | isLong = false; |
915 | 33 | isUnsigned = false; |
916 | 33 | isLongLong = false; |
917 | 33 | isSizeT = false; |
918 | 33 | isHalf = false; |
919 | 33 | isFloat = false; |
920 | 33 | isImaginary = false; |
921 | 33 | isFloat16 = false; |
922 | 33 | isFloat128 = false; |
923 | 33 | MicrosoftInteger = 0; |
924 | 33 | isFract = false; |
925 | 33 | isAccum = false; |
926 | 33 | hadError = false; |
927 | 33 | isBitInt = false; |
928 | | |
929 | | // This routine assumes that the range begin/end matches the regex for integer |
930 | | // and FP constants (specifically, the 'pp-number' regex), and assumes that |
931 | | // the byte at "*end" is both valid and not part of the regex. Because of |
932 | | // this, it doesn't have to check for 'overscan' in various places. |
933 | | // Note: For HLSL, the end token is allowed to be '.' which would be in the |
934 | | // 'pp-number' regex. This is required to support vector swizzles on numeric |
935 | | // constants (i.e. 1.xx or 1.5f.rrr). |
936 | 33 | if (isPreprocessingNumberBody(*ThisTokEnd) && |
937 | 33 | !(LangOpts.HLSL && *ThisTokEnd == '.')) { |
938 | 0 | Diags.Report(TokLoc, diag::err_lexing_numeric); |
939 | 0 | hadError = true; |
940 | 0 | return; |
941 | 0 | } |
942 | | |
943 | 33 | if (*s == '0') { // parse radix |
944 | 2 | ParseNumberStartingWithZero(TokLoc); |
945 | 2 | if (hadError) |
946 | 1 | return; |
947 | 31 | } else { // the first digit is non-zero |
948 | 31 | radix = 10; |
949 | 31 | s = SkipDigits(s); |
950 | 31 | if (s == ThisTokEnd) { |
951 | | // Done. |
952 | 29 | } else { |
953 | 29 | ParseDecimalOrOctalCommon(TokLoc); |
954 | 29 | if (hadError) |
955 | 4 | return; |
956 | 29 | } |
957 | 31 | } |
958 | | |
959 | 28 | SuffixBegin = s; |
960 | 28 | checkSeparator(TokLoc, s, CSK_AfterDigits); |
961 | | |
962 | | // Initial scan to lookahead for fixed point suffix. |
963 | 28 | if (LangOpts.FixedPoint) { |
964 | 0 | for (const char *c = s; c != ThisTokEnd; ++c) { |
965 | 0 | if (*c == 'r' || *c == 'k' || *c == 'R' || *c == 'K') { |
966 | 0 | saw_fixed_point_suffix = true; |
967 | 0 | break; |
968 | 0 | } |
969 | 0 | } |
970 | 0 | } |
971 | | |
972 | | // Parse the suffix. At this point we can classify whether we have an FP or |
973 | | // integer constant. |
974 | 28 | bool isFixedPointConstant = isFixedPointLiteral(); |
975 | 28 | bool isFPConstant = isFloatingLiteral(); |
976 | 28 | bool HasSize = false; |
977 | | |
978 | | // Loop over all of the characters of the suffix. If we see something bad, |
979 | | // we break out of the loop. |
980 | 31 | for (; s != ThisTokEnd; ++s) { |
981 | 25 | switch (*s) { |
982 | 0 | case 'R': |
983 | 0 | case 'r': |
984 | 0 | if (!LangOpts.FixedPoint) |
985 | 0 | break; |
986 | 0 | if (isFract || isAccum) break; |
987 | 0 | if (!(saw_period || saw_exponent)) break; |
988 | 0 | isFract = true; |
989 | 0 | continue; |
990 | 0 | case 'K': |
991 | 1 | case 'k': |
992 | 1 | if (!LangOpts.FixedPoint) |
993 | 1 | break; |
994 | 0 | if (isFract || isAccum) break; |
995 | 0 | if (!(saw_period || saw_exponent)) break; |
996 | 0 | isAccum = true; |
997 | 0 | continue; |
998 | 0 | case 'h': // FP Suffix for "half". |
999 | 1 | case 'H': |
1000 | | // OpenCL Extension v1.2 s9.5 - h or H suffix for half type. |
1001 | 1 | if (!(LangOpts.Half || LangOpts.FixedPoint)) |
1002 | 1 | break; |
1003 | 0 | if (isIntegerLiteral()) break; // Error for integer constant. |
1004 | 0 | if (HasSize) |
1005 | 0 | break; |
1006 | 0 | HasSize = true; |
1007 | 0 | isHalf = true; |
1008 | 0 | continue; // Success. |
1009 | 0 | case 'f': // FP Suffix for "float" |
1010 | 0 | case 'F': |
1011 | 0 | if (!isFPConstant) break; // Error for integer constant. |
1012 | 0 | if (HasSize) |
1013 | 0 | break; |
1014 | 0 | HasSize = true; |
1015 | | |
1016 | | // CUDA host and device may have different _Float16 support, therefore |
1017 | | // allows f16 literals to avoid false alarm. |
1018 | | // When we compile for OpenMP target offloading on NVPTX, f16 suffix |
1019 | | // should also be supported. |
1020 | | // ToDo: more precise check for CUDA. |
1021 | | // TODO: AMDGPU might also support it in the future. |
1022 | 0 | if ((Target.hasFloat16Type() || LangOpts.CUDA || |
1023 | 0 | (LangOpts.OpenMPIsTargetDevice && Target.getTriple().isNVPTX())) && |
1024 | 0 | s + 2 < ThisTokEnd && s[1] == '1' && s[2] == '6') { |
1025 | 0 | s += 2; // success, eat up 2 characters. |
1026 | 0 | isFloat16 = true; |
1027 | 0 | continue; |
1028 | 0 | } |
1029 | | |
1030 | 0 | isFloat = true; |
1031 | 0 | continue; // Success. |
1032 | 0 | case 'q': // FP Suffix for "__float128" |
1033 | 1 | case 'Q': |
1034 | 1 | if (!isFPConstant) break; // Error for integer constant. |
1035 | 0 | if (HasSize) |
1036 | 0 | break; |
1037 | 0 | HasSize = true; |
1038 | 0 | isFloat128 = true; |
1039 | 0 | continue; // Success. |
1040 | 0 | case 'u': |
1041 | 0 | case 'U': |
1042 | 0 | if (isFPConstant) break; // Error for floating constant. |
1043 | 0 | if (isUnsigned) break; // Cannot be repeated. |
1044 | 0 | isUnsigned = true; |
1045 | 0 | continue; // Success. |
1046 | 1 | case 'l': |
1047 | 1 | case 'L': |
1048 | 1 | if (HasSize) |
1049 | 0 | break; |
1050 | 1 | HasSize = true; |
1051 | | |
1052 | | // Check for long long. The L's need to be adjacent and the same case. |
1053 | 1 | if (s[1] == s[0]) { |
1054 | 0 | assert(s + 1 < ThisTokEnd && "didn't maximally munch?"); |
1055 | 0 | if (isFPConstant) break; // long long invalid for floats. |
1056 | 0 | isLongLong = true; |
1057 | 0 | ++s; // Eat both of them. |
1058 | 1 | } else { |
1059 | 1 | isLong = true; |
1060 | 1 | } |
1061 | 1 | continue; // Success. |
1062 | 1 | case 'z': |
1063 | 2 | case 'Z': |
1064 | 2 | if (isFPConstant) |
1065 | 0 | break; // Invalid for floats. |
1066 | 2 | if (HasSize) |
1067 | 0 | break; |
1068 | 2 | HasSize = true; |
1069 | 2 | isSizeT = true; |
1070 | 2 | continue; |
1071 | 0 | case 'i': |
1072 | 0 | case 'I': |
1073 | 0 | if (LangOpts.MicrosoftExt && !isFPConstant) { |
1074 | | // Allow i8, i16, i32, and i64. First, look ahead and check if |
1075 | | // suffixes are Microsoft integers and not the imaginary unit. |
1076 | 0 | uint8_t Bits = 0; |
1077 | 0 | size_t ToSkip = 0; |
1078 | 0 | switch (s[1]) { |
1079 | 0 | case '8': // i8 suffix |
1080 | 0 | Bits = 8; |
1081 | 0 | ToSkip = 2; |
1082 | 0 | break; |
1083 | 0 | case '1': |
1084 | 0 | if (s[2] == '6') { // i16 suffix |
1085 | 0 | Bits = 16; |
1086 | 0 | ToSkip = 3; |
1087 | 0 | } |
1088 | 0 | break; |
1089 | 0 | case '3': |
1090 | 0 | if (s[2] == '2') { // i32 suffix |
1091 | 0 | Bits = 32; |
1092 | 0 | ToSkip = 3; |
1093 | 0 | } |
1094 | 0 | break; |
1095 | 0 | case '6': |
1096 | 0 | if (s[2] == '4') { // i64 suffix |
1097 | 0 | Bits = 64; |
1098 | 0 | ToSkip = 3; |
1099 | 0 | } |
1100 | 0 | break; |
1101 | 0 | default: |
1102 | 0 | break; |
1103 | 0 | } |
1104 | 0 | if (Bits) { |
1105 | 0 | if (HasSize) |
1106 | 0 | break; |
1107 | 0 | HasSize = true; |
1108 | 0 | MicrosoftInteger = Bits; |
1109 | 0 | s += ToSkip; |
1110 | 0 | assert(s <= ThisTokEnd && "didn't maximally munch?"); |
1111 | 0 | break; |
1112 | 0 | } |
1113 | 0 | } |
1114 | 0 | [[fallthrough]]; |
1115 | 0 | case 'j': |
1116 | 0 | case 'J': |
1117 | 0 | if (isImaginary) break; // Cannot be repeated. |
1118 | 0 | isImaginary = true; |
1119 | 0 | continue; // Success. |
1120 | 0 | case 'w': |
1121 | 0 | case 'W': |
1122 | 0 | if (isFPConstant) |
1123 | 0 | break; // Invalid for floats. |
1124 | 0 | if (HasSize) |
1125 | 0 | break; // Invalid if we already have a size for the literal. |
1126 | | |
1127 | | // wb and WB are allowed, but a mixture of cases like Wb or wB is not. We |
1128 | | // explicitly do not support the suffix in C++ as an extension because a |
1129 | | // library-based UDL that resolves to a library type may be more |
1130 | | // appropriate there. |
1131 | 0 | if (!LangOpts.CPlusPlus && ((s[0] == 'w' && s[1] == 'b') || |
1132 | 0 | (s[0] == 'W' && s[1] == 'B'))) { |
1133 | 0 | isBitInt = true; |
1134 | 0 | HasSize = true; |
1135 | 0 | ++s; // Skip both characters (2nd char skipped on continue). |
1136 | 0 | continue; // Success. |
1137 | 0 | } |
1138 | 25 | } |
1139 | | // If we reached here, there was an error or a ud-suffix. |
1140 | 22 | break; |
1141 | 25 | } |
1142 | | |
1143 | | // "i", "if", and "il" are user-defined suffixes in C++1y. |
1144 | 28 | if (s != ThisTokEnd || isImaginary) { |
1145 | | // FIXME: Don't bother expanding UCNs if !tok.hasUCN(). |
1146 | 22 | expandUCNs(UDSuffixBuf, StringRef(SuffixBegin, ThisTokEnd - SuffixBegin)); |
1147 | 22 | if (isValidUDSuffix(LangOpts, UDSuffixBuf)) { |
1148 | 1 | if (!isImaginary) { |
1149 | | // Any suffix pieces we might have parsed are actually part of the |
1150 | | // ud-suffix. |
1151 | 1 | isLong = false; |
1152 | 1 | isUnsigned = false; |
1153 | 1 | isLongLong = false; |
1154 | 1 | isSizeT = false; |
1155 | 1 | isFloat = false; |
1156 | 1 | isFloat16 = false; |
1157 | 1 | isHalf = false; |
1158 | 1 | isImaginary = false; |
1159 | 1 | isBitInt = false; |
1160 | 1 | MicrosoftInteger = 0; |
1161 | 1 | saw_fixed_point_suffix = false; |
1162 | 1 | isFract = false; |
1163 | 1 | isAccum = false; |
1164 | 1 | } |
1165 | | |
1166 | 1 | saw_ud_suffix = true; |
1167 | 1 | return; |
1168 | 1 | } |
1169 | | |
1170 | 21 | if (s != ThisTokEnd) { |
1171 | | // Report an error if there are any. |
1172 | 21 | Diags.Report(Lexer::AdvanceToTokenCharacter( |
1173 | 21 | TokLoc, SuffixBegin - ThisTokBegin, SM, LangOpts), |
1174 | 21 | diag::err_invalid_suffix_constant) |
1175 | 21 | << StringRef(SuffixBegin, ThisTokEnd - SuffixBegin) |
1176 | 21 | << (isFixedPointConstant ? 2 : isFPConstant); |
1177 | 21 | hadError = true; |
1178 | 21 | } |
1179 | 21 | } |
1180 | | |
1181 | 27 | if (!hadError && saw_fixed_point_suffix) { |
1182 | 0 | assert(isFract || isAccum); |
1183 | 0 | } |
1184 | 27 | } |
1185 | | |
1186 | | /// ParseDecimalOrOctalCommon - This method is called for decimal or octal |
1187 | | /// numbers. It issues an error for illegal digits, and handles floating point |
1188 | | /// parsing. If it detects a floating point number, the radix is set to 10. |
1189 | 31 | void NumericLiteralParser::ParseDecimalOrOctalCommon(SourceLocation TokLoc){ |
1190 | 31 | assert((radix == 8 || radix == 10) && "Unexpected radix"); |
1191 | | |
1192 | | // If we have a hex digit other than 'e' (which denotes a FP exponent) then |
1193 | | // the code is using an incorrect base. |
1194 | 31 | if (isHexDigit(*s) && *s != 'e' && *s != 'E' && |
1195 | 31 | !isValidUDSuffix(LangOpts, StringRef(s, ThisTokEnd - s))) { |
1196 | 5 | Diags.Report( |
1197 | 5 | Lexer::AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin, SM, LangOpts), |
1198 | 5 | diag::err_invalid_digit) |
1199 | 5 | << StringRef(s, 1) << (radix == 8 ? 1 : 0); |
1200 | 5 | hadError = true; |
1201 | 5 | return; |
1202 | 5 | } |
1203 | | |
1204 | 26 | if (*s == '.') { |
1205 | 1 | checkSeparator(TokLoc, s, CSK_AfterDigits); |
1206 | 1 | s++; |
1207 | 1 | radix = 10; |
1208 | 1 | saw_period = true; |
1209 | 1 | checkSeparator(TokLoc, s, CSK_BeforeDigits); |
1210 | 1 | s = SkipDigits(s); // Skip suffix. |
1211 | 1 | } |
1212 | 26 | if (*s == 'e' || *s == 'E') { // exponent |
1213 | 0 | checkSeparator(TokLoc, s, CSK_AfterDigits); |
1214 | 0 | const char *Exponent = s; |
1215 | 0 | s++; |
1216 | 0 | radix = 10; |
1217 | 0 | saw_exponent = true; |
1218 | 0 | if (s != ThisTokEnd && (*s == '+' || *s == '-')) s++; // sign |
1219 | 0 | const char *first_non_digit = SkipDigits(s); |
1220 | 0 | if (containsDigits(s, first_non_digit)) { |
1221 | 0 | checkSeparator(TokLoc, s, CSK_BeforeDigits); |
1222 | 0 | s = first_non_digit; |
1223 | 0 | } else { |
1224 | 0 | if (!hadError) { |
1225 | 0 | Diags.Report(Lexer::AdvanceToTokenCharacter( |
1226 | 0 | TokLoc, Exponent - ThisTokBegin, SM, LangOpts), |
1227 | 0 | diag::err_exponent_has_no_digits); |
1228 | 0 | hadError = true; |
1229 | 0 | } |
1230 | 0 | return; |
1231 | 0 | } |
1232 | 0 | } |
1233 | 26 | } |
1234 | | |
1235 | | /// Determine whether a suffix is a valid ud-suffix. We avoid treating reserved |
1236 | | /// suffixes as ud-suffixes, because the diagnostic experience is better if we |
1237 | | /// treat it as an invalid suffix. |
1238 | | bool NumericLiteralParser::isValidUDSuffix(const LangOptions &LangOpts, |
1239 | 112k | StringRef Suffix) { |
1240 | 112k | if (!LangOpts.CPlusPlus11 || Suffix.empty()) |
1241 | 7 | return false; |
1242 | | |
1243 | | // By C++11 [lex.ext]p10, ud-suffixes starting with an '_' are always valid. |
1244 | 112k | if (Suffix[0] == '_') |
1245 | 1 | return true; |
1246 | | |
1247 | | // In C++11, there are no library suffixes. |
1248 | 112k | if (!LangOpts.CPlusPlus14) |
1249 | 0 | return false; |
1250 | | |
1251 | | // In C++14, "s", "h", "min", "ms", "us", and "ns" are used in the library. |
1252 | | // Per tweaked N3660, "il", "i", and "if" are also used in the library. |
1253 | | // In C++2a "d" and "y" are used in the library. |
1254 | 112k | return llvm::StringSwitch<bool>(Suffix) |
1255 | 112k | .Cases("h", "min", "s", true) |
1256 | 112k | .Cases("ms", "us", "ns", true) |
1257 | 112k | .Cases("il", "i", "if", true) |
1258 | 112k | .Cases("d", "y", LangOpts.CPlusPlus20) |
1259 | 112k | .Default(false); |
1260 | 112k | } |
1261 | | |
1262 | | void NumericLiteralParser::checkSeparator(SourceLocation TokLoc, |
1263 | | const char *Pos, |
1264 | 30 | CheckSeparatorKind IsAfterDigits) { |
1265 | 30 | if (IsAfterDigits == CSK_AfterDigits) { |
1266 | 29 | if (Pos == ThisTokBegin) |
1267 | 1 | return; |
1268 | 28 | --Pos; |
1269 | 28 | } else if (Pos == ThisTokEnd) |
1270 | 0 | return; |
1271 | | |
1272 | 29 | if (isDigitSeparator(*Pos)) { |
1273 | 0 | Diags.Report(Lexer::AdvanceToTokenCharacter(TokLoc, Pos - ThisTokBegin, SM, |
1274 | 0 | LangOpts), |
1275 | 0 | diag::err_digit_separator_not_between_digits) |
1276 | 0 | << IsAfterDigits; |
1277 | 0 | hadError = true; |
1278 | 0 | } |
1279 | 29 | } |
1280 | | |
1281 | | /// ParseNumberStartingWithZero - This method is called when the first character |
1282 | | /// of the number is found to be a zero. This means it is either an octal |
1283 | | /// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or |
1284 | | /// a floating point number (01239.123e4). Eat the prefix, determining the |
1285 | | /// radix etc. |
1286 | 2 | void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) { |
1287 | 2 | assert(s[0] == '0' && "Invalid method call"); |
1288 | 0 | s++; |
1289 | | |
1290 | 2 | int c1 = s[0]; |
1291 | | |
1292 | | // Handle a hex number like 0x1234. |
1293 | 2 | if ((c1 == 'x' || c1 == 'X') && (isHexDigit(s[1]) || s[1] == '.')) { |
1294 | 0 | s++; |
1295 | 0 | assert(s < ThisTokEnd && "didn't maximally munch?"); |
1296 | 0 | radix = 16; |
1297 | 0 | DigitsBegin = s; |
1298 | 0 | s = SkipHexDigits(s); |
1299 | 0 | bool HasSignificandDigits = containsDigits(DigitsBegin, s); |
1300 | 0 | if (s == ThisTokEnd) { |
1301 | | // Done. |
1302 | 0 | } else if (*s == '.') { |
1303 | 0 | s++; |
1304 | 0 | saw_period = true; |
1305 | 0 | const char *floatDigitsBegin = s; |
1306 | 0 | s = SkipHexDigits(s); |
1307 | 0 | if (containsDigits(floatDigitsBegin, s)) |
1308 | 0 | HasSignificandDigits = true; |
1309 | 0 | if (HasSignificandDigits) |
1310 | 0 | checkSeparator(TokLoc, floatDigitsBegin, CSK_BeforeDigits); |
1311 | 0 | } |
1312 | |
|
1313 | 0 | if (!HasSignificandDigits) { |
1314 | 0 | Diags.Report(Lexer::AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin, SM, |
1315 | 0 | LangOpts), |
1316 | 0 | diag::err_hex_constant_requires) |
1317 | 0 | << LangOpts.CPlusPlus << 1; |
1318 | 0 | hadError = true; |
1319 | 0 | return; |
1320 | 0 | } |
1321 | | |
1322 | | // A binary exponent can appear with or with a '.'. If dotted, the |
1323 | | // binary exponent is required. |
1324 | 0 | if (*s == 'p' || *s == 'P') { |
1325 | 0 | checkSeparator(TokLoc, s, CSK_AfterDigits); |
1326 | 0 | const char *Exponent = s; |
1327 | 0 | s++; |
1328 | 0 | saw_exponent = true; |
1329 | 0 | if (s != ThisTokEnd && (*s == '+' || *s == '-')) s++; // sign |
1330 | 0 | const char *first_non_digit = SkipDigits(s); |
1331 | 0 | if (!containsDigits(s, first_non_digit)) { |
1332 | 0 | if (!hadError) { |
1333 | 0 | Diags.Report(Lexer::AdvanceToTokenCharacter( |
1334 | 0 | TokLoc, Exponent - ThisTokBegin, SM, LangOpts), |
1335 | 0 | diag::err_exponent_has_no_digits); |
1336 | 0 | hadError = true; |
1337 | 0 | } |
1338 | 0 | return; |
1339 | 0 | } |
1340 | 0 | checkSeparator(TokLoc, s, CSK_BeforeDigits); |
1341 | 0 | s = first_non_digit; |
1342 | |
|
1343 | 0 | if (!LangOpts.HexFloats) |
1344 | 0 | Diags.Report(TokLoc, LangOpts.CPlusPlus |
1345 | 0 | ? diag::ext_hex_literal_invalid |
1346 | 0 | : diag::ext_hex_constant_invalid); |
1347 | 0 | else if (LangOpts.CPlusPlus17) |
1348 | 0 | Diags.Report(TokLoc, diag::warn_cxx17_hex_literal); |
1349 | 0 | } else if (saw_period) { |
1350 | 0 | Diags.Report(Lexer::AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin, SM, |
1351 | 0 | LangOpts), |
1352 | 0 | diag::err_hex_constant_requires) |
1353 | 0 | << LangOpts.CPlusPlus << 0; |
1354 | 0 | hadError = true; |
1355 | 0 | } |
1356 | 0 | return; |
1357 | 0 | } |
1358 | | |
1359 | | // Handle simple binary numbers 0b01010 |
1360 | 2 | if ((c1 == 'b' || c1 == 'B') && (s[1] == '0' || s[1] == '1')) { |
1361 | | // 0b101010 is a C++1y / GCC extension. |
1362 | 0 | Diags.Report(TokLoc, LangOpts.CPlusPlus14 |
1363 | 0 | ? diag::warn_cxx11_compat_binary_literal |
1364 | 0 | : LangOpts.CPlusPlus ? diag::ext_binary_literal_cxx14 |
1365 | 0 | : diag::ext_binary_literal); |
1366 | 0 | ++s; |
1367 | 0 | assert(s < ThisTokEnd && "didn't maximally munch?"); |
1368 | 0 | radix = 2; |
1369 | 0 | DigitsBegin = s; |
1370 | 0 | s = SkipBinaryDigits(s); |
1371 | 0 | if (s == ThisTokEnd) { |
1372 | | // Done. |
1373 | 0 | } else if (isHexDigit(*s) && |
1374 | 0 | !isValidUDSuffix(LangOpts, StringRef(s, ThisTokEnd - s))) { |
1375 | 0 | Diags.Report(Lexer::AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin, SM, |
1376 | 0 | LangOpts), |
1377 | 0 | diag::err_invalid_digit) |
1378 | 0 | << StringRef(s, 1) << 2; |
1379 | 0 | hadError = true; |
1380 | 0 | } |
1381 | | // Other suffixes will be diagnosed by the caller. |
1382 | 0 | return; |
1383 | 0 | } |
1384 | | |
1385 | | // For now, the radix is set to 8. If we discover that we have a |
1386 | | // floating point constant, the radix will change to 10. Octal floating |
1387 | | // point constants are not permitted (only decimal and hexadecimal). |
1388 | 2 | radix = 8; |
1389 | 2 | const char *PossibleNewDigitStart = s; |
1390 | 2 | s = SkipOctalDigits(s); |
1391 | | // When the value is 0 followed by a suffix (like 0wb), we want to leave 0 |
1392 | | // as the start of the digits. So if skipping octal digits does not skip |
1393 | | // anything, we leave the digit start where it was. |
1394 | 2 | if (s != PossibleNewDigitStart) |
1395 | 0 | DigitsBegin = PossibleNewDigitStart; |
1396 | | |
1397 | 2 | if (s == ThisTokEnd) |
1398 | 0 | return; // Done, simple octal number like 01234 |
1399 | | |
1400 | | // If we have some other non-octal digit that *is* a decimal digit, see if |
1401 | | // this is part of a floating point number like 094.123 or 09e1. |
1402 | 2 | if (isDigit(*s)) { |
1403 | 0 | const char *EndDecimal = SkipDigits(s); |
1404 | 0 | if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') { |
1405 | 0 | s = EndDecimal; |
1406 | 0 | radix = 10; |
1407 | 0 | } |
1408 | 0 | } |
1409 | | |
1410 | 2 | ParseDecimalOrOctalCommon(TokLoc); |
1411 | 2 | } |
1412 | | |
1413 | 5 | static bool alwaysFitsInto64Bits(unsigned Radix, unsigned NumDigits) { |
1414 | 5 | switch (Radix) { |
1415 | 0 | case 2: |
1416 | 0 | return NumDigits <= 64; |
1417 | 0 | case 8: |
1418 | 0 | return NumDigits <= 64 / 3; // Digits are groups of 3 bits. |
1419 | 5 | case 10: |
1420 | 5 | return NumDigits <= 19; // floor(log10(2^64)) |
1421 | 0 | case 16: |
1422 | 0 | return NumDigits <= 64 / 4; // Digits are groups of 4 bits. |
1423 | 0 | default: |
1424 | 0 | llvm_unreachable("impossible Radix"); |
1425 | 5 | } |
1426 | 5 | } |
1427 | | |
1428 | | /// GetIntegerValue - Convert this numeric literal value to an APInt that |
1429 | | /// matches Val's input width. If there is an overflow, set Val to the low bits |
1430 | | /// of the result and return true. Otherwise, return false. |
1431 | 5 | bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) { |
1432 | | // Fast path: Compute a conservative bound on the maximum number of |
1433 | | // bits per digit in this radix. If we can't possibly overflow a |
1434 | | // uint64 based on that bound then do the simple conversion to |
1435 | | // integer. This avoids the expensive overflow checking below, and |
1436 | | // handles the common cases that matter (small decimal integers and |
1437 | | // hex/octal values which don't overflow). |
1438 | 5 | const unsigned NumDigits = SuffixBegin - DigitsBegin; |
1439 | 5 | if (alwaysFitsInto64Bits(radix, NumDigits)) { |
1440 | 5 | uint64_t N = 0; |
1441 | 13 | for (const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr) |
1442 | 8 | if (!isDigitSeparator(*Ptr)) |
1443 | 8 | N = N * radix + llvm::hexDigitValue(*Ptr); |
1444 | | |
1445 | | // This will truncate the value to Val's input width. Simply check |
1446 | | // for overflow by comparing. |
1447 | 5 | Val = N; |
1448 | 5 | return Val.getZExtValue() != N; |
1449 | 5 | } |
1450 | | |
1451 | 0 | Val = 0; |
1452 | 0 | const char *Ptr = DigitsBegin; |
1453 | |
|
1454 | 0 | llvm::APInt RadixVal(Val.getBitWidth(), radix); |
1455 | 0 | llvm::APInt CharVal(Val.getBitWidth(), 0); |
1456 | 0 | llvm::APInt OldVal = Val; |
1457 | |
|
1458 | 0 | bool OverflowOccurred = false; |
1459 | 0 | while (Ptr < SuffixBegin) { |
1460 | 0 | if (isDigitSeparator(*Ptr)) { |
1461 | 0 | ++Ptr; |
1462 | 0 | continue; |
1463 | 0 | } |
1464 | | |
1465 | 0 | unsigned C = llvm::hexDigitValue(*Ptr++); |
1466 | | |
1467 | | // If this letter is out of bound for this radix, reject it. |
1468 | 0 | assert(C < radix && "NumericLiteralParser ctor should have rejected this"); |
1469 | | |
1470 | 0 | CharVal = C; |
1471 | | |
1472 | | // Add the digit to the value in the appropriate radix. If adding in digits |
1473 | | // made the value smaller, then this overflowed. |
1474 | 0 | OldVal = Val; |
1475 | | |
1476 | | // Multiply by radix, did overflow occur on the multiply? |
1477 | 0 | Val *= RadixVal; |
1478 | 0 | OverflowOccurred |= Val.udiv(RadixVal) != OldVal; |
1479 | | |
1480 | | // Add value, did overflow occur on the value? |
1481 | | // (a + b) ult b <=> overflow |
1482 | 0 | Val += CharVal; |
1483 | 0 | OverflowOccurred |= Val.ult(CharVal); |
1484 | 0 | } |
1485 | 0 | return OverflowOccurred; |
1486 | 5 | } |
1487 | | |
1488 | | llvm::APFloat::opStatus |
1489 | 1 | NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) { |
1490 | 1 | using llvm::APFloat; |
1491 | | |
1492 | 1 | unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin); |
1493 | | |
1494 | 1 | llvm::SmallString<16> Buffer; |
1495 | 1 | StringRef Str(ThisTokBegin, n); |
1496 | 1 | if (Str.contains('\'')) { |
1497 | 0 | Buffer.reserve(n); |
1498 | 0 | std::remove_copy_if(Str.begin(), Str.end(), std::back_inserter(Buffer), |
1499 | 0 | &isDigitSeparator); |
1500 | 0 | Str = Buffer; |
1501 | 0 | } |
1502 | | |
1503 | 1 | auto StatusOrErr = |
1504 | 1 | Result.convertFromString(Str, APFloat::rmNearestTiesToEven); |
1505 | 1 | assert(StatusOrErr && "Invalid floating point representation"); |
1506 | 1 | return !errorToBool(StatusOrErr.takeError()) ? *StatusOrErr |
1507 | 1 | : APFloat::opInvalidOp; |
1508 | 1 | } |
1509 | | |
1510 | 0 | static inline bool IsExponentPart(char c) { |
1511 | 0 | return c == 'p' || c == 'P' || c == 'e' || c == 'E'; |
1512 | 0 | } |
1513 | | |
1514 | 0 | bool NumericLiteralParser::GetFixedPointValue(llvm::APInt &StoreVal, unsigned Scale) { |
1515 | 0 | assert(radix == 16 || radix == 10); |
1516 | | |
1517 | | // Find how many digits are needed to store the whole literal. |
1518 | 0 | unsigned NumDigits = SuffixBegin - DigitsBegin; |
1519 | 0 | if (saw_period) --NumDigits; |
1520 | | |
1521 | | // Initial scan of the exponent if it exists |
1522 | 0 | bool ExpOverflowOccurred = false; |
1523 | 0 | bool NegativeExponent = false; |
1524 | 0 | const char *ExponentBegin; |
1525 | 0 | uint64_t Exponent = 0; |
1526 | 0 | int64_t BaseShift = 0; |
1527 | 0 | if (saw_exponent) { |
1528 | 0 | const char *Ptr = DigitsBegin; |
1529 | |
|
1530 | 0 | while (!IsExponentPart(*Ptr)) ++Ptr; |
1531 | 0 | ExponentBegin = Ptr; |
1532 | 0 | ++Ptr; |
1533 | 0 | NegativeExponent = *Ptr == '-'; |
1534 | 0 | if (NegativeExponent) ++Ptr; |
1535 | |
|
1536 | 0 | unsigned NumExpDigits = SuffixBegin - Ptr; |
1537 | 0 | if (alwaysFitsInto64Bits(radix, NumExpDigits)) { |
1538 | 0 | llvm::StringRef ExpStr(Ptr, NumExpDigits); |
1539 | 0 | llvm::APInt ExpInt(/*numBits=*/64, ExpStr, /*radix=*/10); |
1540 | 0 | Exponent = ExpInt.getZExtValue(); |
1541 | 0 | } else { |
1542 | 0 | ExpOverflowOccurred = true; |
1543 | 0 | } |
1544 | |
|
1545 | 0 | if (NegativeExponent) BaseShift -= Exponent; |
1546 | 0 | else BaseShift += Exponent; |
1547 | 0 | } |
1548 | | |
1549 | | // Number of bits needed for decimal literal is |
1550 | | // ceil(NumDigits * log2(10)) Integral part |
1551 | | // + Scale Fractional part |
1552 | | // + ceil(Exponent * log2(10)) Exponent |
1553 | | // -------------------------------------------------- |
1554 | | // ceil((NumDigits + Exponent) * log2(10)) + Scale |
1555 | | // |
1556 | | // But for simplicity in handling integers, we can round up log2(10) to 4, |
1557 | | // making: |
1558 | | // 4 * (NumDigits + Exponent) + Scale |
1559 | | // |
1560 | | // Number of digits needed for hexadecimal literal is |
1561 | | // 4 * NumDigits Integral part |
1562 | | // + Scale Fractional part |
1563 | | // + Exponent Exponent |
1564 | | // -------------------------------------------------- |
1565 | | // (4 * NumDigits) + Scale + Exponent |
1566 | 0 | uint64_t NumBitsNeeded; |
1567 | 0 | if (radix == 10) |
1568 | 0 | NumBitsNeeded = 4 * (NumDigits + Exponent) + Scale; |
1569 | 0 | else |
1570 | 0 | NumBitsNeeded = 4 * NumDigits + Exponent + Scale; |
1571 | |
|
1572 | 0 | if (NumBitsNeeded > std::numeric_limits<unsigned>::max()) |
1573 | 0 | ExpOverflowOccurred = true; |
1574 | 0 | llvm::APInt Val(static_cast<unsigned>(NumBitsNeeded), 0, /*isSigned=*/false); |
1575 | |
|
1576 | 0 | bool FoundDecimal = false; |
1577 | |
|
1578 | 0 | int64_t FractBaseShift = 0; |
1579 | 0 | const char *End = saw_exponent ? ExponentBegin : SuffixBegin; |
1580 | 0 | for (const char *Ptr = DigitsBegin; Ptr < End; ++Ptr) { |
1581 | 0 | if (*Ptr == '.') { |
1582 | 0 | FoundDecimal = true; |
1583 | 0 | continue; |
1584 | 0 | } |
1585 | | |
1586 | | // Normal reading of an integer |
1587 | 0 | unsigned C = llvm::hexDigitValue(*Ptr); |
1588 | 0 | assert(C < radix && "NumericLiteralParser ctor should have rejected this"); |
1589 | | |
1590 | 0 | Val *= radix; |
1591 | 0 | Val += C; |
1592 | |
|
1593 | 0 | if (FoundDecimal) |
1594 | | // Keep track of how much we will need to adjust this value by from the |
1595 | | // number of digits past the radix point. |
1596 | 0 | --FractBaseShift; |
1597 | 0 | } |
1598 | | |
1599 | | // For a radix of 16, we will be multiplying by 2 instead of 16. |
1600 | 0 | if (radix == 16) FractBaseShift *= 4; |
1601 | 0 | BaseShift += FractBaseShift; |
1602 | |
|
1603 | 0 | Val <<= Scale; |
1604 | |
|
1605 | 0 | uint64_t Base = (radix == 16) ? 2 : 10; |
1606 | 0 | if (BaseShift > 0) { |
1607 | 0 | for (int64_t i = 0; i < BaseShift; ++i) { |
1608 | 0 | Val *= Base; |
1609 | 0 | } |
1610 | 0 | } else if (BaseShift < 0) { |
1611 | 0 | for (int64_t i = BaseShift; i < 0 && !Val.isZero(); ++i) |
1612 | 0 | Val = Val.udiv(Base); |
1613 | 0 | } |
1614 | |
|
1615 | 0 | bool IntOverflowOccurred = false; |
1616 | 0 | auto MaxVal = llvm::APInt::getMaxValue(StoreVal.getBitWidth()); |
1617 | 0 | if (Val.getBitWidth() > StoreVal.getBitWidth()) { |
1618 | 0 | IntOverflowOccurred |= Val.ugt(MaxVal.zext(Val.getBitWidth())); |
1619 | 0 | StoreVal = Val.trunc(StoreVal.getBitWidth()); |
1620 | 0 | } else if (Val.getBitWidth() < StoreVal.getBitWidth()) { |
1621 | 0 | IntOverflowOccurred |= Val.zext(MaxVal.getBitWidth()).ugt(MaxVal); |
1622 | 0 | StoreVal = Val.zext(StoreVal.getBitWidth()); |
1623 | 0 | } else { |
1624 | 0 | StoreVal = Val; |
1625 | 0 | } |
1626 | |
|
1627 | 0 | return IntOverflowOccurred || ExpOverflowOccurred; |
1628 | 0 | } |
1629 | | |
1630 | | /// \verbatim |
1631 | | /// user-defined-character-literal: [C++11 lex.ext] |
1632 | | /// character-literal ud-suffix |
1633 | | /// ud-suffix: |
1634 | | /// identifier |
1635 | | /// character-literal: [C++11 lex.ccon] |
1636 | | /// ' c-char-sequence ' |
1637 | | /// u' c-char-sequence ' |
1638 | | /// U' c-char-sequence ' |
1639 | | /// L' c-char-sequence ' |
1640 | | /// u8' c-char-sequence ' [C++1z lex.ccon] |
1641 | | /// c-char-sequence: |
1642 | | /// c-char |
1643 | | /// c-char-sequence c-char |
1644 | | /// c-char: |
1645 | | /// any member of the source character set except the single-quote ', |
1646 | | /// backslash \, or new-line character |
1647 | | /// escape-sequence |
1648 | | /// universal-character-name |
1649 | | /// escape-sequence: |
1650 | | /// simple-escape-sequence |
1651 | | /// octal-escape-sequence |
1652 | | /// hexadecimal-escape-sequence |
1653 | | /// simple-escape-sequence: |
1654 | | /// one of \' \" \? \\ \a \b \f \n \r \t \v |
1655 | | /// octal-escape-sequence: |
1656 | | /// \ octal-digit |
1657 | | /// \ octal-digit octal-digit |
1658 | | /// \ octal-digit octal-digit octal-digit |
1659 | | /// hexadecimal-escape-sequence: |
1660 | | /// \x hexadecimal-digit |
1661 | | /// hexadecimal-escape-sequence hexadecimal-digit |
1662 | | /// universal-character-name: [C++11 lex.charset] |
1663 | | /// \u hex-quad |
1664 | | /// \U hex-quad hex-quad |
1665 | | /// hex-quad: |
1666 | | /// hex-digit hex-digit hex-digit hex-digit |
1667 | | /// \endverbatim |
1668 | | /// |
1669 | | CharLiteralParser::CharLiteralParser(const char *begin, const char *end, |
1670 | | SourceLocation Loc, Preprocessor &PP, |
1671 | 3 | tok::TokenKind kind) { |
1672 | | // At this point we know that the character matches the regex "(L|u|U)?'.*'". |
1673 | 3 | HadError = false; |
1674 | | |
1675 | 3 | Kind = kind; |
1676 | | |
1677 | 3 | const char *TokBegin = begin; |
1678 | | |
1679 | | // Skip over wide character determinant. |
1680 | 3 | if (Kind != tok::char_constant) |
1681 | 0 | ++begin; |
1682 | 3 | if (Kind == tok::utf8_char_constant) |
1683 | 0 | ++begin; |
1684 | | |
1685 | | // Skip over the entry quote. |
1686 | 3 | if (begin[0] != '\'') { |
1687 | 0 | PP.Diag(Loc, diag::err_lexing_char); |
1688 | 0 | HadError = true; |
1689 | 0 | return; |
1690 | 0 | } |
1691 | | |
1692 | 3 | ++begin; |
1693 | | |
1694 | | // Remove an optional ud-suffix. |
1695 | 3 | if (end[-1] != '\'') { |
1696 | 1 | const char *UDSuffixEnd = end; |
1697 | 2 | do { |
1698 | 2 | --end; |
1699 | 2 | } while (end[-1] != '\''); |
1700 | | // FIXME: Don't bother with this if !tok.hasUCN(). |
1701 | 1 | expandUCNs(UDSuffixBuf, StringRef(end, UDSuffixEnd - end)); |
1702 | 1 | UDSuffixOffset = end - TokBegin; |
1703 | 1 | } |
1704 | | |
1705 | | // Trim the ending quote. |
1706 | 3 | assert(end != begin && "Invalid token lexed"); |
1707 | 0 | --end; |
1708 | | |
1709 | | // FIXME: The "Value" is an uint64_t so we can handle char literals of |
1710 | | // up to 64-bits. |
1711 | | // FIXME: This extensively assumes that 'char' is 8-bits. |
1712 | 3 | assert(PP.getTargetInfo().getCharWidth() == 8 && |
1713 | 3 | "Assumes char is 8 bits"); |
1714 | 0 | assert(PP.getTargetInfo().getIntWidth() <= 64 && |
1715 | 3 | (PP.getTargetInfo().getIntWidth() & 7) == 0 && |
1716 | 3 | "Assumes sizeof(int) on target is <= 64 and a multiple of char"); |
1717 | 0 | assert(PP.getTargetInfo().getWCharWidth() <= 64 && |
1718 | 3 | "Assumes sizeof(wchar) on target is <= 64"); |
1719 | | |
1720 | 0 | SmallVector<uint32_t, 4> codepoint_buffer; |
1721 | 3 | codepoint_buffer.resize(end - begin); |
1722 | 3 | uint32_t *buffer_begin = &codepoint_buffer.front(); |
1723 | 3 | uint32_t *buffer_end = buffer_begin + codepoint_buffer.size(); |
1724 | | |
1725 | | // Unicode escapes representing characters that cannot be correctly |
1726 | | // represented in a single code unit are disallowed in character literals |
1727 | | // by this implementation. |
1728 | 3 | uint32_t largest_character_for_kind; |
1729 | 3 | if (tok::wide_char_constant == Kind) { |
1730 | 0 | largest_character_for_kind = |
1731 | 0 | 0xFFFFFFFFu >> (32-PP.getTargetInfo().getWCharWidth()); |
1732 | 3 | } else if (tok::utf8_char_constant == Kind) { |
1733 | 0 | largest_character_for_kind = 0x7F; |
1734 | 3 | } else if (tok::utf16_char_constant == Kind) { |
1735 | 0 | largest_character_for_kind = 0xFFFF; |
1736 | 3 | } else if (tok::utf32_char_constant == Kind) { |
1737 | 0 | largest_character_for_kind = 0x10FFFF; |
1738 | 3 | } else { |
1739 | 3 | largest_character_for_kind = 0x7Fu; |
1740 | 3 | } |
1741 | | |
1742 | 6 | while (begin != end) { |
1743 | | // Is this a span of non-escape characters? |
1744 | 3 | if (begin[0] != '\\') { |
1745 | 3 | char const *start = begin; |
1746 | 261 | do { |
1747 | 261 | ++begin; |
1748 | 261 | } while (begin != end && *begin != '\\'); |
1749 | | |
1750 | 3 | char const *tmp_in_start = start; |
1751 | 3 | uint32_t *tmp_out_start = buffer_begin; |
1752 | 3 | llvm::ConversionResult res = |
1753 | 3 | llvm::ConvertUTF8toUTF32(reinterpret_cast<llvm::UTF8 const **>(&start), |
1754 | 3 | reinterpret_cast<llvm::UTF8 const *>(begin), |
1755 | 3 | &buffer_begin, buffer_end, llvm::strictConversion); |
1756 | 3 | if (res != llvm::conversionOK) { |
1757 | | // If we see bad encoding for unprefixed character literals, warn and |
1758 | | // simply copy the byte values, for compatibility with gcc and |
1759 | | // older versions of clang. |
1760 | 3 | bool NoErrorOnBadEncoding = isOrdinary(); |
1761 | 3 | unsigned Msg = diag::err_bad_character_encoding; |
1762 | 3 | if (NoErrorOnBadEncoding) |
1763 | 3 | Msg = diag::warn_bad_character_encoding; |
1764 | 3 | PP.Diag(Loc, Msg); |
1765 | 3 | if (NoErrorOnBadEncoding) { |
1766 | 3 | start = tmp_in_start; |
1767 | 3 | buffer_begin = tmp_out_start; |
1768 | 264 | for (; start != begin; ++start, ++buffer_begin) |
1769 | 261 | *buffer_begin = static_cast<uint8_t>(*start); |
1770 | 3 | } else { |
1771 | 0 | HadError = true; |
1772 | 0 | } |
1773 | 3 | } else { |
1774 | 0 | for (; tmp_out_start < buffer_begin; ++tmp_out_start) { |
1775 | 0 | if (*tmp_out_start > largest_character_for_kind) { |
1776 | 0 | HadError = true; |
1777 | 0 | PP.Diag(Loc, diag::err_character_too_large); |
1778 | 0 | } |
1779 | 0 | } |
1780 | 0 | } |
1781 | | |
1782 | 3 | continue; |
1783 | 3 | } |
1784 | | // Is this a Universal Character Name escape? |
1785 | 0 | if (begin[1] == 'u' || begin[1] == 'U' || begin[1] == 'N') { |
1786 | 0 | unsigned short UcnLen = 0; |
1787 | 0 | if (!ProcessUCNEscape(TokBegin, begin, end, *buffer_begin, UcnLen, |
1788 | 0 | FullSourceLoc(Loc, PP.getSourceManager()), |
1789 | 0 | &PP.getDiagnostics(), PP.getLangOpts(), true)) { |
1790 | 0 | HadError = true; |
1791 | 0 | } else if (*buffer_begin > largest_character_for_kind) { |
1792 | 0 | HadError = true; |
1793 | 0 | PP.Diag(Loc, diag::err_character_too_large); |
1794 | 0 | } |
1795 | |
|
1796 | 0 | ++buffer_begin; |
1797 | 0 | continue; |
1798 | 0 | } |
1799 | 0 | unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo()); |
1800 | 0 | uint64_t result = |
1801 | 0 | ProcessCharEscape(TokBegin, begin, end, HadError, |
1802 | 0 | FullSourceLoc(Loc, PP.getSourceManager()), CharWidth, |
1803 | 0 | &PP.getDiagnostics(), PP.getLangOpts(), |
1804 | 0 | StringLiteralEvalMethod::Evaluated); |
1805 | 0 | *buffer_begin++ = result; |
1806 | 0 | } |
1807 | | |
1808 | 3 | unsigned NumCharsSoFar = buffer_begin - &codepoint_buffer.front(); |
1809 | | |
1810 | 3 | if (NumCharsSoFar > 1) { |
1811 | 3 | if (isOrdinary() && NumCharsSoFar == 4) |
1812 | 0 | PP.Diag(Loc, diag::warn_four_char_character_literal); |
1813 | 3 | else if (isOrdinary()) |
1814 | 3 | PP.Diag(Loc, diag::warn_multichar_character_literal); |
1815 | 0 | else { |
1816 | 0 | PP.Diag(Loc, diag::err_multichar_character_literal) << (isWide() ? 0 : 1); |
1817 | 0 | HadError = true; |
1818 | 0 | } |
1819 | 3 | IsMultiChar = true; |
1820 | 3 | } else { |
1821 | 0 | IsMultiChar = false; |
1822 | 0 | } |
1823 | | |
1824 | 3 | llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0); |
1825 | | |
1826 | | // Narrow character literals act as though their value is concatenated |
1827 | | // in this implementation, but warn on overflow. |
1828 | 3 | bool multi_char_too_long = false; |
1829 | 3 | if (isOrdinary() && isMultiChar()) { |
1830 | 3 | LitVal = 0; |
1831 | 264 | for (size_t i = 0; i < NumCharsSoFar; ++i) { |
1832 | | // check for enough leading zeros to shift into |
1833 | 261 | multi_char_too_long |= (LitVal.countl_zero() < 8); |
1834 | 261 | LitVal <<= 8; |
1835 | 261 | LitVal = LitVal + (codepoint_buffer[i] & 0xFF); |
1836 | 261 | } |
1837 | 3 | } else if (NumCharsSoFar > 0) { |
1838 | | // otherwise just take the last character |
1839 | 0 | LitVal = buffer_begin[-1]; |
1840 | 0 | } |
1841 | | |
1842 | 3 | if (!HadError && multi_char_too_long) { |
1843 | 3 | PP.Diag(Loc, diag::warn_char_constant_too_large); |
1844 | 3 | } |
1845 | | |
1846 | | // Transfer the value from APInt to uint64_t |
1847 | 3 | Value = LitVal.getZExtValue(); |
1848 | | |
1849 | | // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1") |
1850 | | // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple |
1851 | | // character constants are not sign extended in the this implementation: |
1852 | | // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC. |
1853 | 3 | if (isOrdinary() && NumCharsSoFar == 1 && (Value & 128) && |
1854 | 3 | PP.getLangOpts().CharIsSigned) |
1855 | 0 | Value = (signed char)Value; |
1856 | 3 | } |
1857 | | |
1858 | | /// \verbatim |
1859 | | /// string-literal: [C++0x lex.string] |
1860 | | /// encoding-prefix " [s-char-sequence] " |
1861 | | /// encoding-prefix R raw-string |
1862 | | /// encoding-prefix: |
1863 | | /// u8 |
1864 | | /// u |
1865 | | /// U |
1866 | | /// L |
1867 | | /// s-char-sequence: |
1868 | | /// s-char |
1869 | | /// s-char-sequence s-char |
1870 | | /// s-char: |
1871 | | /// any member of the source character set except the double-quote ", |
1872 | | /// backslash \, or new-line character |
1873 | | /// escape-sequence |
1874 | | /// universal-character-name |
1875 | | /// raw-string: |
1876 | | /// " d-char-sequence ( r-char-sequence ) d-char-sequence " |
1877 | | /// r-char-sequence: |
1878 | | /// r-char |
1879 | | /// r-char-sequence r-char |
1880 | | /// r-char: |
1881 | | /// any member of the source character set, except a right parenthesis ) |
1882 | | /// followed by the initial d-char-sequence (which may be empty) |
1883 | | /// followed by a double quote ". |
1884 | | /// d-char-sequence: |
1885 | | /// d-char |
1886 | | /// d-char-sequence d-char |
1887 | | /// d-char: |
1888 | | /// any member of the basic source character set except: |
1889 | | /// space, the left parenthesis (, the right parenthesis ), |
1890 | | /// the backslash \, and the control characters representing horizontal |
1891 | | /// tab, vertical tab, form feed, and newline. |
1892 | | /// escape-sequence: [C++0x lex.ccon] |
1893 | | /// simple-escape-sequence |
1894 | | /// octal-escape-sequence |
1895 | | /// hexadecimal-escape-sequence |
1896 | | /// simple-escape-sequence: |
1897 | | /// one of \' \" \? \\ \a \b \f \n \r \t \v |
1898 | | /// octal-escape-sequence: |
1899 | | /// \ octal-digit |
1900 | | /// \ octal-digit octal-digit |
1901 | | /// \ octal-digit octal-digit octal-digit |
1902 | | /// hexadecimal-escape-sequence: |
1903 | | /// \x hexadecimal-digit |
1904 | | /// hexadecimal-escape-sequence hexadecimal-digit |
1905 | | /// universal-character-name: |
1906 | | /// \u hex-quad |
1907 | | /// \U hex-quad hex-quad |
1908 | | /// hex-quad: |
1909 | | /// hex-digit hex-digit hex-digit hex-digit |
1910 | | /// \endverbatim |
1911 | | /// |
1912 | | StringLiteralParser::StringLiteralParser(ArrayRef<Token> StringToks, |
1913 | | Preprocessor &PP, |
1914 | | StringLiteralEvalMethod EvalMethod) |
1915 | | : SM(PP.getSourceManager()), Features(PP.getLangOpts()), |
1916 | | Target(PP.getTargetInfo()), Diags(&PP.getDiagnostics()), |
1917 | | MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown), |
1918 | | ResultPtr(ResultBuf.data()), EvalMethod(EvalMethod), hadError(false), |
1919 | 139 | Pascal(false) { |
1920 | 139 | init(StringToks); |
1921 | 139 | } |
1922 | | |
1923 | 139 | void StringLiteralParser::init(ArrayRef<Token> StringToks){ |
1924 | | // The literal token may have come from an invalid source location (e.g. due |
1925 | | // to a PCH error), in which case the token length will be 0. |
1926 | 139 | if (StringToks.empty() || StringToks[0].getLength() < 2) |
1927 | 0 | return DiagnoseLexingError(SourceLocation()); |
1928 | | |
1929 | | // Scan all of the string portions, remember the max individual token length, |
1930 | | // computing a bound on the concatenated string length, and see whether any |
1931 | | // piece is a wide-string. If any of the string portions is a wide-string |
1932 | | // literal, the result is a wide-string literal [C99 6.4.5p4]. |
1933 | 139 | assert(!StringToks.empty() && "expected at least one token"); |
1934 | 0 | MaxTokenLength = StringToks[0].getLength(); |
1935 | 139 | assert(StringToks[0].getLength() >= 2 && "literal token is invalid!"); |
1936 | 0 | SizeBound = StringToks[0].getLength() - 2; // -2 for "". |
1937 | 139 | hadError = false; |
1938 | | |
1939 | | // Determines the kind of string from the prefix |
1940 | 139 | Kind = tok::string_literal; |
1941 | | |
1942 | | /// (C99 5.1.1.2p1). The common case is only one string fragment. |
1943 | 139 | for (const Token &Tok : StringToks) { |
1944 | 139 | if (Tok.getLength() < 2) |
1945 | 0 | return DiagnoseLexingError(Tok.getLocation()); |
1946 | | |
1947 | | // The string could be shorter than this if it needs cleaning, but this is a |
1948 | | // reasonable bound, which is all we need. |
1949 | 139 | assert(Tok.getLength() >= 2 && "literal token is invalid!"); |
1950 | 0 | SizeBound += Tok.getLength() - 2; // -2 for "". |
1951 | | |
1952 | | // Remember maximum string piece length. |
1953 | 139 | if (Tok.getLength() > MaxTokenLength) |
1954 | 0 | MaxTokenLength = Tok.getLength(); |
1955 | | |
1956 | | // Remember if we see any wide or utf-8/16/32 strings. |
1957 | | // Also check for illegal concatenations. |
1958 | 139 | if (isUnevaluated() && Tok.getKind() != tok::string_literal) { |
1959 | 0 | if (Diags) { |
1960 | 0 | SourceLocation PrefixEndLoc = Lexer::AdvanceToTokenCharacter( |
1961 | 0 | Tok.getLocation(), getEncodingPrefixLen(Tok.getKind()), SM, |
1962 | 0 | Features); |
1963 | 0 | CharSourceRange Range = |
1964 | 0 | CharSourceRange::getCharRange({Tok.getLocation(), PrefixEndLoc}); |
1965 | 0 | StringRef Prefix(SM.getCharacterData(Tok.getLocation()), |
1966 | 0 | getEncodingPrefixLen(Tok.getKind())); |
1967 | 0 | Diags->Report(Tok.getLocation(), |
1968 | 0 | Features.CPlusPlus26 |
1969 | 0 | ? diag::err_unevaluated_string_prefix |
1970 | 0 | : diag::warn_unevaluated_string_prefix) |
1971 | 0 | << Prefix << Features.CPlusPlus << FixItHint::CreateRemoval(Range); |
1972 | 0 | } |
1973 | 0 | if (Features.CPlusPlus26) |
1974 | 0 | hadError = true; |
1975 | 139 | } else if (Tok.isNot(Kind) && Tok.isNot(tok::string_literal)) { |
1976 | 0 | if (isOrdinary()) { |
1977 | 0 | Kind = Tok.getKind(); |
1978 | 0 | } else { |
1979 | 0 | if (Diags) |
1980 | 0 | Diags->Report(Tok.getLocation(), diag::err_unsupported_string_concat); |
1981 | 0 | hadError = true; |
1982 | 0 | } |
1983 | 0 | } |
1984 | 139 | } |
1985 | | |
1986 | | // Include space for the null terminator. |
1987 | 139 | ++SizeBound; |
1988 | | |
1989 | | // TODO: K&R warning: "traditional C rejects string constant concatenation" |
1990 | | |
1991 | | // Get the width in bytes of char/wchar_t/char16_t/char32_t |
1992 | 139 | CharByteWidth = getCharWidth(Kind, Target); |
1993 | 139 | assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple"); |
1994 | 0 | CharByteWidth /= 8; |
1995 | | |
1996 | | // The output buffer size needs to be large enough to hold wide characters. |
1997 | | // This is a worst-case assumption which basically corresponds to L"" "long". |
1998 | 139 | SizeBound *= CharByteWidth; |
1999 | | |
2000 | | // Size the temporary buffer to hold the result string data. |
2001 | 139 | ResultBuf.resize(SizeBound); |
2002 | | |
2003 | | // Likewise, but for each string piece. |
2004 | 139 | SmallString<512> TokenBuf; |
2005 | 139 | TokenBuf.resize(MaxTokenLength); |
2006 | | |
2007 | | // Loop over all the strings, getting their spelling, and expanding them to |
2008 | | // wide strings as appropriate. |
2009 | 139 | ResultPtr = &ResultBuf[0]; // Next byte to fill in. |
2010 | | |
2011 | 139 | Pascal = false; |
2012 | | |
2013 | 139 | SourceLocation UDSuffixTokLoc; |
2014 | | |
2015 | 278 | for (unsigned i = 0, e = StringToks.size(); i != e; ++i) { |
2016 | 139 | const char *ThisTokBuf = &TokenBuf[0]; |
2017 | | // Get the spelling of the token, which eliminates trigraphs, etc. We know |
2018 | | // that ThisTokBuf points to a buffer that is big enough for the whole token |
2019 | | // and 'spelled' tokens can only shrink. |
2020 | 139 | bool StringInvalid = false; |
2021 | 139 | unsigned ThisTokLen = |
2022 | 139 | Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features, |
2023 | 139 | &StringInvalid); |
2024 | 139 | if (StringInvalid) |
2025 | 0 | return DiagnoseLexingError(StringToks[i].getLocation()); |
2026 | | |
2027 | 139 | const char *ThisTokBegin = ThisTokBuf; |
2028 | 139 | const char *ThisTokEnd = ThisTokBuf+ThisTokLen; |
2029 | | |
2030 | | // Remove an optional ud-suffix. |
2031 | 139 | if (ThisTokEnd[-1] != '"') { |
2032 | 0 | const char *UDSuffixEnd = ThisTokEnd; |
2033 | 0 | do { |
2034 | 0 | --ThisTokEnd; |
2035 | 0 | } while (ThisTokEnd[-1] != '"'); |
2036 | |
|
2037 | 0 | StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd); |
2038 | |
|
2039 | 0 | if (UDSuffixBuf.empty()) { |
2040 | 0 | if (StringToks[i].hasUCN()) |
2041 | 0 | expandUCNs(UDSuffixBuf, UDSuffix); |
2042 | 0 | else |
2043 | 0 | UDSuffixBuf.assign(UDSuffix); |
2044 | 0 | UDSuffixToken = i; |
2045 | 0 | UDSuffixOffset = ThisTokEnd - ThisTokBuf; |
2046 | 0 | UDSuffixTokLoc = StringToks[i].getLocation(); |
2047 | 0 | } else { |
2048 | 0 | SmallString<32> ExpandedUDSuffix; |
2049 | 0 | if (StringToks[i].hasUCN()) { |
2050 | 0 | expandUCNs(ExpandedUDSuffix, UDSuffix); |
2051 | 0 | UDSuffix = ExpandedUDSuffix; |
2052 | 0 | } |
2053 | | |
2054 | | // C++11 [lex.ext]p8: At the end of phase 6, if a string literal is the |
2055 | | // result of a concatenation involving at least one user-defined-string- |
2056 | | // literal, all the participating user-defined-string-literals shall |
2057 | | // have the same ud-suffix. |
2058 | 0 | bool UnevaluatedStringHasUDL = isUnevaluated() && !UDSuffix.empty(); |
2059 | 0 | if (UDSuffixBuf != UDSuffix || UnevaluatedStringHasUDL) { |
2060 | 0 | if (Diags) { |
2061 | 0 | SourceLocation TokLoc = StringToks[i].getLocation(); |
2062 | 0 | if (UnevaluatedStringHasUDL) { |
2063 | 0 | Diags->Report(TokLoc, diag::err_unevaluated_string_udl) |
2064 | 0 | << SourceRange(TokLoc, TokLoc); |
2065 | 0 | } else { |
2066 | 0 | Diags->Report(TokLoc, diag::err_string_concat_mixed_suffix) |
2067 | 0 | << UDSuffixBuf << UDSuffix |
2068 | 0 | << SourceRange(UDSuffixTokLoc, UDSuffixTokLoc); |
2069 | 0 | } |
2070 | 0 | } |
2071 | 0 | hadError = true; |
2072 | 0 | } |
2073 | 0 | } |
2074 | 0 | } |
2075 | | |
2076 | | // Strip the end quote. |
2077 | 139 | --ThisTokEnd; |
2078 | | |
2079 | | // TODO: Input character set mapping support. |
2080 | | |
2081 | | // Skip marker for wide or unicode strings. |
2082 | 139 | if (ThisTokBuf[0] == 'L' || ThisTokBuf[0] == 'u' || ThisTokBuf[0] == 'U') { |
2083 | 0 | ++ThisTokBuf; |
2084 | | // Skip 8 of u8 marker for utf8 strings. |
2085 | 0 | if (ThisTokBuf[0] == '8') |
2086 | 0 | ++ThisTokBuf; |
2087 | 0 | } |
2088 | | |
2089 | | // Check for raw string |
2090 | 139 | if (ThisTokBuf[0] == 'R') { |
2091 | 0 | if (ThisTokBuf[1] != '"') { |
2092 | | // The file may have come from PCH and then changed after loading the |
2093 | | // PCH; Fail gracefully. |
2094 | 0 | return DiagnoseLexingError(StringToks[i].getLocation()); |
2095 | 0 | } |
2096 | 0 | ThisTokBuf += 2; // skip R" |
2097 | | |
2098 | | // C++11 [lex.string]p2: A `d-char-sequence` shall consist of at most 16 |
2099 | | // characters. |
2100 | 0 | constexpr unsigned MaxRawStrDelimLen = 16; |
2101 | |
|
2102 | 0 | const char *Prefix = ThisTokBuf; |
2103 | 0 | while (static_cast<unsigned>(ThisTokBuf - Prefix) < MaxRawStrDelimLen && |
2104 | 0 | ThisTokBuf[0] != '(') |
2105 | 0 | ++ThisTokBuf; |
2106 | 0 | if (ThisTokBuf[0] != '(') |
2107 | 0 | return DiagnoseLexingError(StringToks[i].getLocation()); |
2108 | 0 | ++ThisTokBuf; // skip '(' |
2109 | | |
2110 | | // Remove same number of characters from the end |
2111 | 0 | ThisTokEnd -= ThisTokBuf - Prefix; |
2112 | 0 | if (ThisTokEnd < ThisTokBuf) |
2113 | 0 | return DiagnoseLexingError(StringToks[i].getLocation()); |
2114 | | |
2115 | | // C++14 [lex.string]p4: A source-file new-line in a raw string literal |
2116 | | // results in a new-line in the resulting execution string-literal. |
2117 | 0 | StringRef RemainingTokenSpan(ThisTokBuf, ThisTokEnd - ThisTokBuf); |
2118 | 0 | while (!RemainingTokenSpan.empty()) { |
2119 | | // Split the string literal on \r\n boundaries. |
2120 | 0 | size_t CRLFPos = RemainingTokenSpan.find("\r\n"); |
2121 | 0 | StringRef BeforeCRLF = RemainingTokenSpan.substr(0, CRLFPos); |
2122 | 0 | StringRef AfterCRLF = RemainingTokenSpan.substr(CRLFPos); |
2123 | | |
2124 | | // Copy everything before the \r\n sequence into the string literal. |
2125 | 0 | if (CopyStringFragment(StringToks[i], ThisTokBegin, BeforeCRLF)) |
2126 | 0 | hadError = true; |
2127 | | |
2128 | | // Point into the \n inside the \r\n sequence and operate on the |
2129 | | // remaining portion of the literal. |
2130 | 0 | RemainingTokenSpan = AfterCRLF.substr(1); |
2131 | 0 | } |
2132 | 139 | } else { |
2133 | 139 | if (ThisTokBuf[0] != '"') { |
2134 | | // The file may have come from PCH and then changed after loading the |
2135 | | // PCH; Fail gracefully. |
2136 | 0 | return DiagnoseLexingError(StringToks[i].getLocation()); |
2137 | 0 | } |
2138 | 139 | ++ThisTokBuf; // skip " |
2139 | | |
2140 | | // Check if this is a pascal string |
2141 | 139 | if (!isUnevaluated() && Features.PascalStrings && |
2142 | 139 | ThisTokBuf + 1 != ThisTokEnd && ThisTokBuf[0] == '\\' && |
2143 | 139 | ThisTokBuf[1] == 'p') { |
2144 | | |
2145 | | // If the \p sequence is found in the first token, we have a pascal string |
2146 | | // Otherwise, if we already have a pascal string, ignore the first \p |
2147 | 0 | if (i == 0) { |
2148 | 0 | ++ThisTokBuf; |
2149 | 0 | Pascal = true; |
2150 | 0 | } else if (Pascal) |
2151 | 0 | ThisTokBuf += 2; |
2152 | 0 | } |
2153 | | |
2154 | 278 | while (ThisTokBuf != ThisTokEnd) { |
2155 | | // Is this a span of non-escape characters? |
2156 | 139 | if (ThisTokBuf[0] != '\\') { |
2157 | 139 | const char *InStart = ThisTokBuf; |
2158 | 1.59k | do { |
2159 | 1.59k | ++ThisTokBuf; |
2160 | 1.59k | } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\'); |
2161 | | |
2162 | | // Copy the character span over. |
2163 | 139 | if (CopyStringFragment(StringToks[i], ThisTokBegin, |
2164 | 139 | StringRef(InStart, ThisTokBuf - InStart))) |
2165 | 0 | hadError = true; |
2166 | 139 | continue; |
2167 | 139 | } |
2168 | | // Is this a Universal Character Name escape? |
2169 | 0 | if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U' || |
2170 | 0 | ThisTokBuf[1] == 'N') { |
2171 | 0 | EncodeUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, |
2172 | 0 | ResultPtr, hadError, |
2173 | 0 | FullSourceLoc(StringToks[i].getLocation(), SM), |
2174 | 0 | CharByteWidth, Diags, Features); |
2175 | 0 | continue; |
2176 | 0 | } |
2177 | | // Otherwise, this is a non-UCN escape character. Process it. |
2178 | 0 | unsigned ResultChar = |
2179 | 0 | ProcessCharEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, hadError, |
2180 | 0 | FullSourceLoc(StringToks[i].getLocation(), SM), |
2181 | 0 | CharByteWidth * 8, Diags, Features, EvalMethod); |
2182 | |
|
2183 | 0 | if (CharByteWidth == 4) { |
2184 | | // FIXME: Make the type of the result buffer correct instead of |
2185 | | // using reinterpret_cast. |
2186 | 0 | llvm::UTF32 *ResultWidePtr = reinterpret_cast<llvm::UTF32*>(ResultPtr); |
2187 | 0 | *ResultWidePtr = ResultChar; |
2188 | 0 | ResultPtr += 4; |
2189 | 0 | } else if (CharByteWidth == 2) { |
2190 | | // FIXME: Make the type of the result buffer correct instead of |
2191 | | // using reinterpret_cast. |
2192 | 0 | llvm::UTF16 *ResultWidePtr = reinterpret_cast<llvm::UTF16*>(ResultPtr); |
2193 | 0 | *ResultWidePtr = ResultChar & 0xFFFF; |
2194 | 0 | ResultPtr += 2; |
2195 | 0 | } else { |
2196 | 0 | assert(CharByteWidth == 1 && "Unexpected char width"); |
2197 | 0 | *ResultPtr++ = ResultChar & 0xFF; |
2198 | 0 | } |
2199 | 0 | } |
2200 | 139 | } |
2201 | 139 | } |
2202 | | |
2203 | 139 | assert((!Pascal || !isUnevaluated()) && |
2204 | 139 | "Pascal string in unevaluated context"); |
2205 | 139 | if (Pascal) { |
2206 | 0 | if (CharByteWidth == 4) { |
2207 | | // FIXME: Make the type of the result buffer correct instead of |
2208 | | // using reinterpret_cast. |
2209 | 0 | llvm::UTF32 *ResultWidePtr = reinterpret_cast<llvm::UTF32*>(ResultBuf.data()); |
2210 | 0 | ResultWidePtr[0] = GetNumStringChars() - 1; |
2211 | 0 | } else if (CharByteWidth == 2) { |
2212 | | // FIXME: Make the type of the result buffer correct instead of |
2213 | | // using reinterpret_cast. |
2214 | 0 | llvm::UTF16 *ResultWidePtr = reinterpret_cast<llvm::UTF16*>(ResultBuf.data()); |
2215 | 0 | ResultWidePtr[0] = GetNumStringChars() - 1; |
2216 | 0 | } else { |
2217 | 0 | assert(CharByteWidth == 1 && "Unexpected char width"); |
2218 | 0 | ResultBuf[0] = GetNumStringChars() - 1; |
2219 | 0 | } |
2220 | | |
2221 | | // Verify that pascal strings aren't too large. |
2222 | 0 | if (GetStringLength() > 256) { |
2223 | 0 | if (Diags) |
2224 | 0 | Diags->Report(StringToks.front().getLocation(), |
2225 | 0 | diag::err_pascal_string_too_long) |
2226 | 0 | << SourceRange(StringToks.front().getLocation(), |
2227 | 0 | StringToks.back().getLocation()); |
2228 | 0 | hadError = true; |
2229 | 0 | return; |
2230 | 0 | } |
2231 | 139 | } else if (Diags) { |
2232 | | // Complain if this string literal has too many characters. |
2233 | 139 | unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509; |
2234 | | |
2235 | 139 | if (GetNumStringChars() > MaxChars) |
2236 | 0 | Diags->Report(StringToks.front().getLocation(), |
2237 | 0 | diag::ext_string_too_long) |
2238 | 0 | << GetNumStringChars() << MaxChars |
2239 | 0 | << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0) |
2240 | 0 | << SourceRange(StringToks.front().getLocation(), |
2241 | 0 | StringToks.back().getLocation()); |
2242 | 139 | } |
2243 | 139 | } |
2244 | | |
2245 | 18 | static const char *resyncUTF8(const char *Err, const char *End) { |
2246 | 18 | if (Err == End) |
2247 | 0 | return End; |
2248 | 18 | End = Err + std::min<unsigned>(llvm::getNumBytesForUTF8(*Err), End-Err); |
2249 | 18 | while (++Err != End && (*Err & 0xC0) == 0x80) |
2250 | 0 | ; |
2251 | 18 | return Err; |
2252 | 18 | } |
2253 | | |
2254 | | /// This function copies from Fragment, which is a sequence of bytes |
2255 | | /// within Tok's contents (which begin at TokBegin) into ResultPtr. |
2256 | | /// Performs widening for multi-byte characters. |
2257 | | bool StringLiteralParser::CopyStringFragment(const Token &Tok, |
2258 | | const char *TokBegin, |
2259 | 139 | StringRef Fragment) { |
2260 | 139 | const llvm::UTF8 *ErrorPtrTmp; |
2261 | 139 | if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp)) |
2262 | 138 | return false; |
2263 | | |
2264 | | // If we see bad encoding for unprefixed string literals, warn and |
2265 | | // simply copy the byte values, for compatibility with gcc and older |
2266 | | // versions of clang. |
2267 | 1 | bool NoErrorOnBadEncoding = isOrdinary(); |
2268 | 1 | if (NoErrorOnBadEncoding) { |
2269 | 1 | memcpy(ResultPtr, Fragment.data(), Fragment.size()); |
2270 | 1 | ResultPtr += Fragment.size(); |
2271 | 1 | } |
2272 | | |
2273 | 1 | if (Diags) { |
2274 | 1 | const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp); |
2275 | | |
2276 | 1 | FullSourceLoc SourceLoc(Tok.getLocation(), SM); |
2277 | 1 | const DiagnosticBuilder &Builder = |
2278 | 1 | Diag(Diags, Features, SourceLoc, TokBegin, |
2279 | 1 | ErrorPtr, resyncUTF8(ErrorPtr, Fragment.end()), |
2280 | 1 | NoErrorOnBadEncoding ? diag::warn_bad_string_encoding |
2281 | 1 | : diag::err_bad_string_encoding); |
2282 | | |
2283 | 1 | const char *NextStart = resyncUTF8(ErrorPtr, Fragment.end()); |
2284 | 1 | StringRef NextFragment(NextStart, Fragment.end()-NextStart); |
2285 | | |
2286 | | // Decode into a dummy buffer. |
2287 | 1 | SmallString<512> Dummy; |
2288 | 1 | Dummy.reserve(Fragment.size() * CharByteWidth); |
2289 | 1 | char *Ptr = Dummy.data(); |
2290 | | |
2291 | 17 | while (!ConvertUTF8toWide(CharByteWidth, NextFragment, Ptr, ErrorPtrTmp)) { |
2292 | 16 | const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp); |
2293 | 16 | NextStart = resyncUTF8(ErrorPtr, Fragment.end()); |
2294 | 16 | Builder << MakeCharSourceRange(Features, SourceLoc, TokBegin, |
2295 | 16 | ErrorPtr, NextStart); |
2296 | 16 | NextFragment = StringRef(NextStart, Fragment.end()-NextStart); |
2297 | 16 | } |
2298 | 1 | } |
2299 | 1 | return !NoErrorOnBadEncoding; |
2300 | 139 | } |
2301 | | |
2302 | 0 | void StringLiteralParser::DiagnoseLexingError(SourceLocation Loc) { |
2303 | 0 | hadError = true; |
2304 | 0 | if (Diags) |
2305 | 0 | Diags->Report(Loc, diag::err_lexing_string); |
2306 | 0 | } |
2307 | | |
2308 | | /// getOffsetOfStringByte - This function returns the offset of the |
2309 | | /// specified byte of the string data represented by Token. This handles |
2310 | | /// advancing over escape sequences in the string. |
2311 | | unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok, |
2312 | 0 | unsigned ByteNo) const { |
2313 | | // Get the spelling of the token. |
2314 | 0 | SmallString<32> SpellingBuffer; |
2315 | 0 | SpellingBuffer.resize(Tok.getLength()); |
2316 | |
|
2317 | 0 | bool StringInvalid = false; |
2318 | 0 | const char *SpellingPtr = &SpellingBuffer[0]; |
2319 | 0 | unsigned TokLen = Lexer::getSpelling(Tok, SpellingPtr, SM, Features, |
2320 | 0 | &StringInvalid); |
2321 | 0 | if (StringInvalid) |
2322 | 0 | return 0; |
2323 | | |
2324 | 0 | const char *SpellingStart = SpellingPtr; |
2325 | 0 | const char *SpellingEnd = SpellingPtr+TokLen; |
2326 | | |
2327 | | // Handle UTF-8 strings just like narrow strings. |
2328 | 0 | if (SpellingPtr[0] == 'u' && SpellingPtr[1] == '8') |
2329 | 0 | SpellingPtr += 2; |
2330 | |
|
2331 | 0 | assert(SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' && |
2332 | 0 | SpellingPtr[0] != 'U' && "Doesn't handle wide or utf strings yet"); |
2333 | | |
2334 | | // For raw string literals, this is easy. |
2335 | 0 | if (SpellingPtr[0] == 'R') { |
2336 | 0 | assert(SpellingPtr[1] == '"' && "Should be a raw string literal!"); |
2337 | | // Skip 'R"'. |
2338 | 0 | SpellingPtr += 2; |
2339 | 0 | while (*SpellingPtr != '(') { |
2340 | 0 | ++SpellingPtr; |
2341 | 0 | assert(SpellingPtr < SpellingEnd && "Missing ( for raw string literal"); |
2342 | 0 | } |
2343 | | // Skip '('. |
2344 | 0 | ++SpellingPtr; |
2345 | 0 | return SpellingPtr - SpellingStart + ByteNo; |
2346 | 0 | } |
2347 | | |
2348 | | // Skip over the leading quote |
2349 | 0 | assert(SpellingPtr[0] == '"' && "Should be a string literal!"); |
2350 | 0 | ++SpellingPtr; |
2351 | | |
2352 | | // Skip over bytes until we find the offset we're looking for. |
2353 | 0 | while (ByteNo) { |
2354 | 0 | assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!"); |
2355 | | |
2356 | | // Step over non-escapes simply. |
2357 | 0 | if (*SpellingPtr != '\\') { |
2358 | 0 | ++SpellingPtr; |
2359 | 0 | --ByteNo; |
2360 | 0 | continue; |
2361 | 0 | } |
2362 | | |
2363 | | // Otherwise, this is an escape character. Advance over it. |
2364 | 0 | bool HadError = false; |
2365 | 0 | if (SpellingPtr[1] == 'u' || SpellingPtr[1] == 'U' || |
2366 | 0 | SpellingPtr[1] == 'N') { |
2367 | 0 | const char *EscapePtr = SpellingPtr; |
2368 | 0 | unsigned Len = MeasureUCNEscape(SpellingStart, SpellingPtr, SpellingEnd, |
2369 | 0 | 1, Features, HadError); |
2370 | 0 | if (Len > ByteNo) { |
2371 | | // ByteNo is somewhere within the escape sequence. |
2372 | 0 | SpellingPtr = EscapePtr; |
2373 | 0 | break; |
2374 | 0 | } |
2375 | 0 | ByteNo -= Len; |
2376 | 0 | } else { |
2377 | 0 | ProcessCharEscape(SpellingStart, SpellingPtr, SpellingEnd, HadError, |
2378 | 0 | FullSourceLoc(Tok.getLocation(), SM), CharByteWidth * 8, |
2379 | 0 | Diags, Features, StringLiteralEvalMethod::Evaluated); |
2380 | 0 | --ByteNo; |
2381 | 0 | } |
2382 | 0 | assert(!HadError && "This method isn't valid on erroneous strings"); |
2383 | 0 | } |
2384 | |
|
2385 | 0 | return SpellingPtr-SpellingStart; |
2386 | 0 | } |
2387 | | |
2388 | | /// Determine whether a suffix is a valid ud-suffix. We avoid treating reserved |
2389 | | /// suffixes as ud-suffixes, because the diagnostic experience is better if we |
2390 | | /// treat it as an invalid suffix. |
2391 | | bool StringLiteralParser::isValidUDSuffix(const LangOptions &LangOpts, |
2392 | 112k | StringRef Suffix) { |
2393 | 112k | return NumericLiteralParser::isValidUDSuffix(LangOpts, Suffix) || |
2394 | 112k | Suffix == "sv"; |
2395 | 112k | } |