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