xref: /minix3/external/bsd/llvm/dist/clang/lib/Lex/LiteralSupport.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- LiteralSupport.cpp - Code to parse and process literals ----------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements the NumericLiteralParser, CharLiteralParser, and
11f4a2713aSLionel Sambuc // StringLiteralParser interfaces.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc #include "clang/Lex/LiteralSupport.h"
16f4a2713aSLionel Sambuc #include "clang/Basic/CharInfo.h"
17f4a2713aSLionel Sambuc #include "clang/Basic/TargetInfo.h"
18f4a2713aSLionel Sambuc #include "clang/Lex/LexDiagnostic.h"
19f4a2713aSLionel Sambuc #include "clang/Lex/Preprocessor.h"
20f4a2713aSLionel Sambuc #include "llvm/ADT/StringExtras.h"
21f4a2713aSLionel Sambuc #include "llvm/Support/ConvertUTF.h"
22f4a2713aSLionel Sambuc #include "llvm/Support/ErrorHandling.h"
23f4a2713aSLionel Sambuc 
24f4a2713aSLionel Sambuc using namespace clang;
25f4a2713aSLionel Sambuc 
getCharWidth(tok::TokenKind kind,const TargetInfo & Target)26f4a2713aSLionel Sambuc static unsigned getCharWidth(tok::TokenKind kind, const TargetInfo &Target) {
27f4a2713aSLionel Sambuc   switch (kind) {
28f4a2713aSLionel Sambuc   default: llvm_unreachable("Unknown token type!");
29f4a2713aSLionel Sambuc   case tok::char_constant:
30f4a2713aSLionel Sambuc   case tok::string_literal:
31*0a6a1f1dSLionel Sambuc   case tok::utf8_char_constant:
32f4a2713aSLionel Sambuc   case tok::utf8_string_literal:
33f4a2713aSLionel Sambuc     return Target.getCharWidth();
34f4a2713aSLionel Sambuc   case tok::wide_char_constant:
35f4a2713aSLionel Sambuc   case tok::wide_string_literal:
36f4a2713aSLionel Sambuc     return Target.getWCharWidth();
37f4a2713aSLionel Sambuc   case tok::utf16_char_constant:
38f4a2713aSLionel Sambuc   case tok::utf16_string_literal:
39f4a2713aSLionel Sambuc     return Target.getChar16Width();
40f4a2713aSLionel Sambuc   case tok::utf32_char_constant:
41f4a2713aSLionel Sambuc   case tok::utf32_string_literal:
42f4a2713aSLionel Sambuc     return Target.getChar32Width();
43f4a2713aSLionel Sambuc   }
44f4a2713aSLionel Sambuc }
45f4a2713aSLionel Sambuc 
MakeCharSourceRange(const LangOptions & Features,FullSourceLoc TokLoc,const char * TokBegin,const char * TokRangeBegin,const char * TokRangeEnd)46f4a2713aSLionel Sambuc static CharSourceRange MakeCharSourceRange(const LangOptions &Features,
47f4a2713aSLionel Sambuc                                            FullSourceLoc TokLoc,
48f4a2713aSLionel Sambuc                                            const char *TokBegin,
49f4a2713aSLionel Sambuc                                            const char *TokRangeBegin,
50f4a2713aSLionel Sambuc                                            const char *TokRangeEnd) {
51f4a2713aSLionel Sambuc   SourceLocation Begin =
52f4a2713aSLionel Sambuc     Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin,
53f4a2713aSLionel Sambuc                                    TokLoc.getManager(), Features);
54f4a2713aSLionel Sambuc   SourceLocation End =
55f4a2713aSLionel Sambuc     Lexer::AdvanceToTokenCharacter(Begin, TokRangeEnd - TokRangeBegin,
56f4a2713aSLionel Sambuc                                    TokLoc.getManager(), Features);
57f4a2713aSLionel Sambuc   return CharSourceRange::getCharRange(Begin, End);
58f4a2713aSLionel Sambuc }
59f4a2713aSLionel Sambuc 
60f4a2713aSLionel Sambuc /// \brief Produce a diagnostic highlighting some portion of a literal.
61f4a2713aSLionel Sambuc ///
62f4a2713aSLionel Sambuc /// Emits the diagnostic \p DiagID, highlighting the range of characters from
63f4a2713aSLionel Sambuc /// \p TokRangeBegin (inclusive) to \p TokRangeEnd (exclusive), which must be
64f4a2713aSLionel Sambuc /// a substring of a spelling buffer for the token beginning at \p TokBegin.
Diag(DiagnosticsEngine * Diags,const LangOptions & Features,FullSourceLoc TokLoc,const char * TokBegin,const char * TokRangeBegin,const char * TokRangeEnd,unsigned DiagID)65f4a2713aSLionel Sambuc static DiagnosticBuilder Diag(DiagnosticsEngine *Diags,
66f4a2713aSLionel Sambuc                               const LangOptions &Features, FullSourceLoc TokLoc,
67f4a2713aSLionel Sambuc                               const char *TokBegin, const char *TokRangeBegin,
68f4a2713aSLionel Sambuc                               const char *TokRangeEnd, unsigned DiagID) {
69f4a2713aSLionel Sambuc   SourceLocation Begin =
70f4a2713aSLionel Sambuc     Lexer::AdvanceToTokenCharacter(TokLoc, TokRangeBegin - TokBegin,
71f4a2713aSLionel Sambuc                                    TokLoc.getManager(), Features);
72f4a2713aSLionel Sambuc   return Diags->Report(Begin, DiagID) <<
73f4a2713aSLionel Sambuc     MakeCharSourceRange(Features, TokLoc, TokBegin, TokRangeBegin, TokRangeEnd);
74f4a2713aSLionel Sambuc }
75f4a2713aSLionel Sambuc 
76f4a2713aSLionel Sambuc /// ProcessCharEscape - Parse a standard C escape sequence, which can occur in
77f4a2713aSLionel Sambuc /// either a character or a string literal.
ProcessCharEscape(const char * ThisTokBegin,const char * & ThisTokBuf,const char * ThisTokEnd,bool & HadError,FullSourceLoc Loc,unsigned CharWidth,DiagnosticsEngine * Diags,const LangOptions & Features)78f4a2713aSLionel Sambuc static unsigned ProcessCharEscape(const char *ThisTokBegin,
79f4a2713aSLionel Sambuc                                   const char *&ThisTokBuf,
80f4a2713aSLionel Sambuc                                   const char *ThisTokEnd, bool &HadError,
81f4a2713aSLionel Sambuc                                   FullSourceLoc Loc, unsigned CharWidth,
82f4a2713aSLionel Sambuc                                   DiagnosticsEngine *Diags,
83f4a2713aSLionel Sambuc                                   const LangOptions &Features) {
84f4a2713aSLionel Sambuc   const char *EscapeBegin = ThisTokBuf;
85f4a2713aSLionel Sambuc 
86f4a2713aSLionel Sambuc   // Skip the '\' char.
87f4a2713aSLionel Sambuc   ++ThisTokBuf;
88f4a2713aSLionel Sambuc 
89f4a2713aSLionel Sambuc   // We know that this character can't be off the end of the buffer, because
90f4a2713aSLionel Sambuc   // that would have been \", which would not have been the end of string.
91f4a2713aSLionel Sambuc   unsigned ResultChar = *ThisTokBuf++;
92f4a2713aSLionel Sambuc   switch (ResultChar) {
93f4a2713aSLionel Sambuc   // These map to themselves.
94f4a2713aSLionel Sambuc   case '\\': case '\'': case '"': case '?': break;
95f4a2713aSLionel Sambuc 
96f4a2713aSLionel Sambuc     // These have fixed mappings.
97f4a2713aSLionel Sambuc   case 'a':
98f4a2713aSLionel Sambuc     // TODO: K&R: the meaning of '\\a' is different in traditional C
99f4a2713aSLionel Sambuc     ResultChar = 7;
100f4a2713aSLionel Sambuc     break;
101f4a2713aSLionel Sambuc   case 'b':
102f4a2713aSLionel Sambuc     ResultChar = 8;
103f4a2713aSLionel Sambuc     break;
104f4a2713aSLionel Sambuc   case 'e':
105f4a2713aSLionel Sambuc     if (Diags)
106f4a2713aSLionel Sambuc       Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
107f4a2713aSLionel Sambuc            diag::ext_nonstandard_escape) << "e";
108f4a2713aSLionel Sambuc     ResultChar = 27;
109f4a2713aSLionel Sambuc     break;
110f4a2713aSLionel Sambuc   case 'E':
111f4a2713aSLionel Sambuc     if (Diags)
112f4a2713aSLionel Sambuc       Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
113f4a2713aSLionel Sambuc            diag::ext_nonstandard_escape) << "E";
114f4a2713aSLionel Sambuc     ResultChar = 27;
115f4a2713aSLionel Sambuc     break;
116f4a2713aSLionel Sambuc   case 'f':
117f4a2713aSLionel Sambuc     ResultChar = 12;
118f4a2713aSLionel Sambuc     break;
119f4a2713aSLionel Sambuc   case 'n':
120f4a2713aSLionel Sambuc     ResultChar = 10;
121f4a2713aSLionel Sambuc     break;
122f4a2713aSLionel Sambuc   case 'r':
123f4a2713aSLionel Sambuc     ResultChar = 13;
124f4a2713aSLionel Sambuc     break;
125f4a2713aSLionel Sambuc   case 't':
126f4a2713aSLionel Sambuc     ResultChar = 9;
127f4a2713aSLionel Sambuc     break;
128f4a2713aSLionel Sambuc   case 'v':
129f4a2713aSLionel Sambuc     ResultChar = 11;
130f4a2713aSLionel Sambuc     break;
131f4a2713aSLionel Sambuc   case 'x': { // Hex escape.
132f4a2713aSLionel Sambuc     ResultChar = 0;
133f4a2713aSLionel Sambuc     if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) {
134f4a2713aSLionel Sambuc       if (Diags)
135f4a2713aSLionel Sambuc         Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
136f4a2713aSLionel Sambuc              diag::err_hex_escape_no_digits) << "x";
137f4a2713aSLionel Sambuc       HadError = 1;
138f4a2713aSLionel Sambuc       break;
139f4a2713aSLionel Sambuc     }
140f4a2713aSLionel Sambuc 
141f4a2713aSLionel Sambuc     // Hex escapes are a maximal series of hex digits.
142f4a2713aSLionel Sambuc     bool Overflow = false;
143f4a2713aSLionel Sambuc     for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) {
144f4a2713aSLionel Sambuc       int CharVal = llvm::hexDigitValue(ThisTokBuf[0]);
145f4a2713aSLionel Sambuc       if (CharVal == -1) break;
146f4a2713aSLionel Sambuc       // About to shift out a digit?
147f4a2713aSLionel Sambuc       Overflow |= (ResultChar & 0xF0000000) ? true : false;
148f4a2713aSLionel Sambuc       ResultChar <<= 4;
149f4a2713aSLionel Sambuc       ResultChar |= CharVal;
150f4a2713aSLionel Sambuc     }
151f4a2713aSLionel Sambuc 
152f4a2713aSLionel Sambuc     // See if any bits will be truncated when evaluated as a character.
153f4a2713aSLionel Sambuc     if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
154f4a2713aSLionel Sambuc       Overflow = true;
155f4a2713aSLionel Sambuc       ResultChar &= ~0U >> (32-CharWidth);
156f4a2713aSLionel Sambuc     }
157f4a2713aSLionel Sambuc 
158f4a2713aSLionel Sambuc     // Check for overflow.
159f4a2713aSLionel Sambuc     if (Overflow && Diags)   // Too many digits to fit in
160f4a2713aSLionel Sambuc       Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
161f4a2713aSLionel Sambuc            diag::err_hex_escape_too_large);
162f4a2713aSLionel Sambuc     break;
163f4a2713aSLionel Sambuc   }
164f4a2713aSLionel Sambuc   case '0': case '1': case '2': case '3':
165f4a2713aSLionel Sambuc   case '4': case '5': case '6': case '7': {
166f4a2713aSLionel Sambuc     // Octal escapes.
167f4a2713aSLionel Sambuc     --ThisTokBuf;
168f4a2713aSLionel Sambuc     ResultChar = 0;
169f4a2713aSLionel Sambuc 
170f4a2713aSLionel Sambuc     // Octal escapes are a series of octal digits with maximum length 3.
171f4a2713aSLionel Sambuc     // "\0123" is a two digit sequence equal to "\012" "3".
172f4a2713aSLionel Sambuc     unsigned NumDigits = 0;
173f4a2713aSLionel Sambuc     do {
174f4a2713aSLionel Sambuc       ResultChar <<= 3;
175f4a2713aSLionel Sambuc       ResultChar |= *ThisTokBuf++ - '0';
176f4a2713aSLionel Sambuc       ++NumDigits;
177f4a2713aSLionel Sambuc     } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 &&
178f4a2713aSLionel Sambuc              ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7');
179f4a2713aSLionel Sambuc 
180f4a2713aSLionel Sambuc     // Check for overflow.  Reject '\777', but not L'\777'.
181f4a2713aSLionel Sambuc     if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) {
182f4a2713aSLionel Sambuc       if (Diags)
183f4a2713aSLionel Sambuc         Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
184f4a2713aSLionel Sambuc              diag::err_octal_escape_too_large);
185f4a2713aSLionel Sambuc       ResultChar &= ~0U >> (32-CharWidth);
186f4a2713aSLionel Sambuc     }
187f4a2713aSLionel Sambuc     break;
188f4a2713aSLionel Sambuc   }
189f4a2713aSLionel Sambuc 
190f4a2713aSLionel Sambuc     // Otherwise, these are not valid escapes.
191f4a2713aSLionel Sambuc   case '(': case '{': case '[': case '%':
192f4a2713aSLionel Sambuc     // GCC accepts these as extensions.  We warn about them as such though.
193f4a2713aSLionel Sambuc     if (Diags)
194f4a2713aSLionel Sambuc       Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
195f4a2713aSLionel Sambuc            diag::ext_nonstandard_escape)
196f4a2713aSLionel Sambuc         << std::string(1, ResultChar);
197f4a2713aSLionel Sambuc     break;
198f4a2713aSLionel Sambuc   default:
199*0a6a1f1dSLionel Sambuc     if (!Diags)
200f4a2713aSLionel Sambuc       break;
201f4a2713aSLionel Sambuc 
202f4a2713aSLionel Sambuc     if (isPrintable(ResultChar))
203f4a2713aSLionel Sambuc       Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
204f4a2713aSLionel Sambuc            diag::ext_unknown_escape)
205f4a2713aSLionel Sambuc         << std::string(1, ResultChar);
206f4a2713aSLionel Sambuc     else
207f4a2713aSLionel Sambuc       Diag(Diags, Features, Loc, ThisTokBegin, EscapeBegin, ThisTokBuf,
208f4a2713aSLionel Sambuc            diag::ext_unknown_escape)
209f4a2713aSLionel Sambuc         << "x" + llvm::utohexstr(ResultChar);
210f4a2713aSLionel Sambuc     break;
211f4a2713aSLionel Sambuc   }
212f4a2713aSLionel Sambuc 
213f4a2713aSLionel Sambuc   return ResultChar;
214f4a2713aSLionel Sambuc }
215f4a2713aSLionel Sambuc 
appendCodePoint(unsigned Codepoint,llvm::SmallVectorImpl<char> & Str)216*0a6a1f1dSLionel Sambuc static void appendCodePoint(unsigned Codepoint,
217*0a6a1f1dSLionel Sambuc                             llvm::SmallVectorImpl<char> &Str) {
218*0a6a1f1dSLionel Sambuc   char ResultBuf[4];
219*0a6a1f1dSLionel Sambuc   char *ResultPtr = ResultBuf;
220*0a6a1f1dSLionel Sambuc   bool Res = llvm::ConvertCodePointToUTF8(Codepoint, ResultPtr);
221*0a6a1f1dSLionel Sambuc   (void)Res;
222*0a6a1f1dSLionel Sambuc   assert(Res && "Unexpected conversion failure");
223*0a6a1f1dSLionel Sambuc   Str.append(ResultBuf, ResultPtr);
224*0a6a1f1dSLionel Sambuc }
225*0a6a1f1dSLionel Sambuc 
expandUCNs(SmallVectorImpl<char> & Buf,StringRef Input)226*0a6a1f1dSLionel Sambuc void clang::expandUCNs(SmallVectorImpl<char> &Buf, StringRef Input) {
227*0a6a1f1dSLionel Sambuc   for (StringRef::iterator I = Input.begin(), E = Input.end(); I != E; ++I) {
228*0a6a1f1dSLionel Sambuc     if (*I != '\\') {
229*0a6a1f1dSLionel Sambuc       Buf.push_back(*I);
230*0a6a1f1dSLionel Sambuc       continue;
231*0a6a1f1dSLionel Sambuc     }
232*0a6a1f1dSLionel Sambuc 
233*0a6a1f1dSLionel Sambuc     ++I;
234*0a6a1f1dSLionel Sambuc     assert(*I == 'u' || *I == 'U');
235*0a6a1f1dSLionel Sambuc 
236*0a6a1f1dSLionel Sambuc     unsigned NumHexDigits;
237*0a6a1f1dSLionel Sambuc     if (*I == 'u')
238*0a6a1f1dSLionel Sambuc       NumHexDigits = 4;
239*0a6a1f1dSLionel Sambuc     else
240*0a6a1f1dSLionel Sambuc       NumHexDigits = 8;
241*0a6a1f1dSLionel Sambuc 
242*0a6a1f1dSLionel Sambuc     assert(I + NumHexDigits <= E);
243*0a6a1f1dSLionel Sambuc 
244*0a6a1f1dSLionel Sambuc     uint32_t CodePoint = 0;
245*0a6a1f1dSLionel Sambuc     for (++I; NumHexDigits != 0; ++I, --NumHexDigits) {
246*0a6a1f1dSLionel Sambuc       unsigned Value = llvm::hexDigitValue(*I);
247*0a6a1f1dSLionel Sambuc       assert(Value != -1U);
248*0a6a1f1dSLionel Sambuc 
249*0a6a1f1dSLionel Sambuc       CodePoint <<= 4;
250*0a6a1f1dSLionel Sambuc       CodePoint += Value;
251*0a6a1f1dSLionel Sambuc     }
252*0a6a1f1dSLionel Sambuc 
253*0a6a1f1dSLionel Sambuc     appendCodePoint(CodePoint, Buf);
254*0a6a1f1dSLionel Sambuc     --I;
255*0a6a1f1dSLionel Sambuc   }
256*0a6a1f1dSLionel Sambuc }
257*0a6a1f1dSLionel Sambuc 
258f4a2713aSLionel Sambuc /// ProcessUCNEscape - Read the Universal Character Name, check constraints and
259f4a2713aSLionel Sambuc /// return the UTF32.
ProcessUCNEscape(const char * ThisTokBegin,const char * & ThisTokBuf,const char * ThisTokEnd,uint32_t & UcnVal,unsigned short & UcnLen,FullSourceLoc Loc,DiagnosticsEngine * Diags,const LangOptions & Features,bool in_char_string_literal=false)260f4a2713aSLionel Sambuc static bool ProcessUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
261f4a2713aSLionel Sambuc                              const char *ThisTokEnd,
262f4a2713aSLionel Sambuc                              uint32_t &UcnVal, unsigned short &UcnLen,
263f4a2713aSLionel Sambuc                              FullSourceLoc Loc, DiagnosticsEngine *Diags,
264f4a2713aSLionel Sambuc                              const LangOptions &Features,
265f4a2713aSLionel Sambuc                              bool in_char_string_literal = false) {
266f4a2713aSLionel Sambuc   const char *UcnBegin = ThisTokBuf;
267f4a2713aSLionel Sambuc 
268f4a2713aSLionel Sambuc   // Skip the '\u' char's.
269f4a2713aSLionel Sambuc   ThisTokBuf += 2;
270f4a2713aSLionel Sambuc 
271f4a2713aSLionel Sambuc   if (ThisTokBuf == ThisTokEnd || !isHexDigit(*ThisTokBuf)) {
272f4a2713aSLionel Sambuc     if (Diags)
273f4a2713aSLionel Sambuc       Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
274f4a2713aSLionel Sambuc            diag::err_hex_escape_no_digits) << StringRef(&ThisTokBuf[-1], 1);
275f4a2713aSLionel Sambuc     return false;
276f4a2713aSLionel Sambuc   }
277f4a2713aSLionel Sambuc   UcnLen = (ThisTokBuf[-1] == 'u' ? 4 : 8);
278f4a2713aSLionel Sambuc   unsigned short UcnLenSave = UcnLen;
279f4a2713aSLionel Sambuc   for (; ThisTokBuf != ThisTokEnd && UcnLenSave; ++ThisTokBuf, UcnLenSave--) {
280f4a2713aSLionel Sambuc     int CharVal = llvm::hexDigitValue(ThisTokBuf[0]);
281f4a2713aSLionel Sambuc     if (CharVal == -1) break;
282f4a2713aSLionel Sambuc     UcnVal <<= 4;
283f4a2713aSLionel Sambuc     UcnVal |= CharVal;
284f4a2713aSLionel Sambuc   }
285f4a2713aSLionel Sambuc   // If we didn't consume the proper number of digits, there is a problem.
286f4a2713aSLionel Sambuc   if (UcnLenSave) {
287f4a2713aSLionel Sambuc     if (Diags)
288f4a2713aSLionel Sambuc       Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
289f4a2713aSLionel Sambuc            diag::err_ucn_escape_incomplete);
290f4a2713aSLionel Sambuc     return false;
291f4a2713aSLionel Sambuc   }
292f4a2713aSLionel Sambuc 
293f4a2713aSLionel Sambuc   // Check UCN constraints (C99 6.4.3p2) [C++11 lex.charset p2]
294f4a2713aSLionel Sambuc   if ((0xD800 <= UcnVal && UcnVal <= 0xDFFF) || // surrogate codepoints
295f4a2713aSLionel Sambuc       UcnVal > 0x10FFFF) {                      // maximum legal UTF32 value
296f4a2713aSLionel Sambuc     if (Diags)
297f4a2713aSLionel Sambuc       Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
298f4a2713aSLionel Sambuc            diag::err_ucn_escape_invalid);
299f4a2713aSLionel Sambuc     return false;
300f4a2713aSLionel Sambuc   }
301f4a2713aSLionel Sambuc 
302f4a2713aSLionel Sambuc   // C++11 allows UCNs that refer to control characters and basic source
303f4a2713aSLionel Sambuc   // characters inside character and string literals
304f4a2713aSLionel Sambuc   if (UcnVal < 0xa0 &&
305f4a2713aSLionel Sambuc       (UcnVal != 0x24 && UcnVal != 0x40 && UcnVal != 0x60)) {  // $, @, `
306f4a2713aSLionel Sambuc     bool IsError = (!Features.CPlusPlus11 || !in_char_string_literal);
307f4a2713aSLionel Sambuc     if (Diags) {
308f4a2713aSLionel Sambuc       char BasicSCSChar = UcnVal;
309f4a2713aSLionel Sambuc       if (UcnVal >= 0x20 && UcnVal < 0x7f)
310f4a2713aSLionel Sambuc         Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
311f4a2713aSLionel Sambuc              IsError ? diag::err_ucn_escape_basic_scs :
312f4a2713aSLionel Sambuc                        diag::warn_cxx98_compat_literal_ucn_escape_basic_scs)
313f4a2713aSLionel Sambuc             << StringRef(&BasicSCSChar, 1);
314f4a2713aSLionel Sambuc       else
315f4a2713aSLionel Sambuc         Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
316f4a2713aSLionel Sambuc              IsError ? diag::err_ucn_control_character :
317f4a2713aSLionel Sambuc                        diag::warn_cxx98_compat_literal_ucn_control_character);
318f4a2713aSLionel Sambuc     }
319f4a2713aSLionel Sambuc     if (IsError)
320f4a2713aSLionel Sambuc       return false;
321f4a2713aSLionel Sambuc   }
322f4a2713aSLionel Sambuc 
323f4a2713aSLionel Sambuc   if (!Features.CPlusPlus && !Features.C99 && Diags)
324f4a2713aSLionel Sambuc     Diag(Diags, Features, Loc, ThisTokBegin, UcnBegin, ThisTokBuf,
325f4a2713aSLionel Sambuc          diag::warn_ucn_not_valid_in_c89_literal);
326f4a2713aSLionel Sambuc 
327f4a2713aSLionel Sambuc   return true;
328f4a2713aSLionel Sambuc }
329f4a2713aSLionel Sambuc 
330f4a2713aSLionel Sambuc /// MeasureUCNEscape - Determine the number of bytes within the resulting string
331f4a2713aSLionel Sambuc /// which this UCN will occupy.
MeasureUCNEscape(const char * ThisTokBegin,const char * & ThisTokBuf,const char * ThisTokEnd,unsigned CharByteWidth,const LangOptions & Features,bool & HadError)332f4a2713aSLionel Sambuc static int MeasureUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
333f4a2713aSLionel Sambuc                             const char *ThisTokEnd, unsigned CharByteWidth,
334f4a2713aSLionel Sambuc                             const LangOptions &Features, bool &HadError) {
335f4a2713aSLionel Sambuc   // UTF-32: 4 bytes per escape.
336f4a2713aSLionel Sambuc   if (CharByteWidth == 4)
337f4a2713aSLionel Sambuc     return 4;
338f4a2713aSLionel Sambuc 
339f4a2713aSLionel Sambuc   uint32_t UcnVal = 0;
340f4a2713aSLionel Sambuc   unsigned short UcnLen = 0;
341f4a2713aSLionel Sambuc   FullSourceLoc Loc;
342f4a2713aSLionel Sambuc 
343f4a2713aSLionel Sambuc   if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal,
344*0a6a1f1dSLionel Sambuc                         UcnLen, Loc, nullptr, Features, true)) {
345f4a2713aSLionel Sambuc     HadError = true;
346f4a2713aSLionel Sambuc     return 0;
347f4a2713aSLionel Sambuc   }
348f4a2713aSLionel Sambuc 
349f4a2713aSLionel Sambuc   // UTF-16: 2 bytes for BMP, 4 bytes otherwise.
350f4a2713aSLionel Sambuc   if (CharByteWidth == 2)
351f4a2713aSLionel Sambuc     return UcnVal <= 0xFFFF ? 2 : 4;
352f4a2713aSLionel Sambuc 
353f4a2713aSLionel Sambuc   // UTF-8.
354f4a2713aSLionel Sambuc   if (UcnVal < 0x80)
355f4a2713aSLionel Sambuc     return 1;
356f4a2713aSLionel Sambuc   if (UcnVal < 0x800)
357f4a2713aSLionel Sambuc     return 2;
358f4a2713aSLionel Sambuc   if (UcnVal < 0x10000)
359f4a2713aSLionel Sambuc     return 3;
360f4a2713aSLionel Sambuc   return 4;
361f4a2713aSLionel Sambuc }
362f4a2713aSLionel Sambuc 
363f4a2713aSLionel Sambuc /// EncodeUCNEscape - Read the Universal Character Name, check constraints and
364f4a2713aSLionel Sambuc /// convert the UTF32 to UTF8 or UTF16. This is a subroutine of
365f4a2713aSLionel Sambuc /// StringLiteralParser. When we decide to implement UCN's for identifiers,
366f4a2713aSLionel Sambuc /// we will likely rework our support for UCN's.
EncodeUCNEscape(const char * ThisTokBegin,const char * & ThisTokBuf,const char * ThisTokEnd,char * & ResultBuf,bool & HadError,FullSourceLoc Loc,unsigned CharByteWidth,DiagnosticsEngine * Diags,const LangOptions & Features)367f4a2713aSLionel Sambuc static void EncodeUCNEscape(const char *ThisTokBegin, const char *&ThisTokBuf,
368f4a2713aSLionel Sambuc                             const char *ThisTokEnd,
369f4a2713aSLionel Sambuc                             char *&ResultBuf, bool &HadError,
370f4a2713aSLionel Sambuc                             FullSourceLoc Loc, unsigned CharByteWidth,
371f4a2713aSLionel Sambuc                             DiagnosticsEngine *Diags,
372f4a2713aSLionel Sambuc                             const LangOptions &Features) {
373f4a2713aSLionel Sambuc   typedef uint32_t UTF32;
374f4a2713aSLionel Sambuc   UTF32 UcnVal = 0;
375f4a2713aSLionel Sambuc   unsigned short UcnLen = 0;
376f4a2713aSLionel Sambuc   if (!ProcessUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, UcnVal, UcnLen,
377f4a2713aSLionel Sambuc                         Loc, Diags, Features, true)) {
378f4a2713aSLionel Sambuc     HadError = true;
379f4a2713aSLionel Sambuc     return;
380f4a2713aSLionel Sambuc   }
381f4a2713aSLionel Sambuc 
382f4a2713aSLionel Sambuc   assert((CharByteWidth == 1 || CharByteWidth == 2 || CharByteWidth == 4) &&
383f4a2713aSLionel Sambuc          "only character widths of 1, 2, or 4 bytes supported");
384f4a2713aSLionel Sambuc 
385f4a2713aSLionel Sambuc   (void)UcnLen;
386f4a2713aSLionel Sambuc   assert((UcnLen== 4 || UcnLen== 8) && "only ucn length of 4 or 8 supported");
387f4a2713aSLionel Sambuc 
388f4a2713aSLionel Sambuc   if (CharByteWidth == 4) {
389f4a2713aSLionel Sambuc     // FIXME: Make the type of the result buffer correct instead of
390f4a2713aSLionel Sambuc     // using reinterpret_cast.
391f4a2713aSLionel Sambuc     UTF32 *ResultPtr = reinterpret_cast<UTF32*>(ResultBuf);
392f4a2713aSLionel Sambuc     *ResultPtr = UcnVal;
393f4a2713aSLionel Sambuc     ResultBuf += 4;
394f4a2713aSLionel Sambuc     return;
395f4a2713aSLionel Sambuc   }
396f4a2713aSLionel Sambuc 
397f4a2713aSLionel Sambuc   if (CharByteWidth == 2) {
398f4a2713aSLionel Sambuc     // FIXME: Make the type of the result buffer correct instead of
399f4a2713aSLionel Sambuc     // using reinterpret_cast.
400f4a2713aSLionel Sambuc     UTF16 *ResultPtr = reinterpret_cast<UTF16*>(ResultBuf);
401f4a2713aSLionel Sambuc 
402f4a2713aSLionel Sambuc     if (UcnVal <= (UTF32)0xFFFF) {
403f4a2713aSLionel Sambuc       *ResultPtr = UcnVal;
404f4a2713aSLionel Sambuc       ResultBuf += 2;
405f4a2713aSLionel Sambuc       return;
406f4a2713aSLionel Sambuc     }
407f4a2713aSLionel Sambuc 
408f4a2713aSLionel Sambuc     // Convert to UTF16.
409f4a2713aSLionel Sambuc     UcnVal -= 0x10000;
410f4a2713aSLionel Sambuc     *ResultPtr     = 0xD800 + (UcnVal >> 10);
411f4a2713aSLionel Sambuc     *(ResultPtr+1) = 0xDC00 + (UcnVal & 0x3FF);
412f4a2713aSLionel Sambuc     ResultBuf += 4;
413f4a2713aSLionel Sambuc     return;
414f4a2713aSLionel Sambuc   }
415f4a2713aSLionel Sambuc 
416f4a2713aSLionel Sambuc   assert(CharByteWidth == 1 && "UTF-8 encoding is only for 1 byte characters");
417f4a2713aSLionel Sambuc 
418f4a2713aSLionel Sambuc   // Now that we've parsed/checked the UCN, we convert from UTF32->UTF8.
419f4a2713aSLionel Sambuc   // The conversion below was inspired by:
420f4a2713aSLionel Sambuc   //   http://www.unicode.org/Public/PROGRAMS/CVTUTF/ConvertUTF.c
421f4a2713aSLionel Sambuc   // First, we determine how many bytes the result will require.
422f4a2713aSLionel Sambuc   typedef uint8_t UTF8;
423f4a2713aSLionel Sambuc 
424f4a2713aSLionel Sambuc   unsigned short bytesToWrite = 0;
425f4a2713aSLionel Sambuc   if (UcnVal < (UTF32)0x80)
426f4a2713aSLionel Sambuc     bytesToWrite = 1;
427f4a2713aSLionel Sambuc   else if (UcnVal < (UTF32)0x800)
428f4a2713aSLionel Sambuc     bytesToWrite = 2;
429f4a2713aSLionel Sambuc   else if (UcnVal < (UTF32)0x10000)
430f4a2713aSLionel Sambuc     bytesToWrite = 3;
431f4a2713aSLionel Sambuc   else
432f4a2713aSLionel Sambuc     bytesToWrite = 4;
433f4a2713aSLionel Sambuc 
434f4a2713aSLionel Sambuc   const unsigned byteMask = 0xBF;
435f4a2713aSLionel Sambuc   const unsigned byteMark = 0x80;
436f4a2713aSLionel Sambuc 
437f4a2713aSLionel Sambuc   // Once the bits are split out into bytes of UTF8, this is a mask OR-ed
438f4a2713aSLionel Sambuc   // into the first byte, depending on how many bytes follow.
439f4a2713aSLionel Sambuc   static const UTF8 firstByteMark[5] = {
440f4a2713aSLionel Sambuc     0x00, 0x00, 0xC0, 0xE0, 0xF0
441f4a2713aSLionel Sambuc   };
442f4a2713aSLionel Sambuc   // Finally, we write the bytes into ResultBuf.
443f4a2713aSLionel Sambuc   ResultBuf += bytesToWrite;
444f4a2713aSLionel Sambuc   switch (bytesToWrite) { // note: everything falls through.
445f4a2713aSLionel Sambuc   case 4: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
446f4a2713aSLionel Sambuc   case 3: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
447f4a2713aSLionel Sambuc   case 2: *--ResultBuf = (UTF8)((UcnVal | byteMark) & byteMask); UcnVal >>= 6;
448f4a2713aSLionel Sambuc   case 1: *--ResultBuf = (UTF8) (UcnVal | firstByteMark[bytesToWrite]);
449f4a2713aSLionel Sambuc   }
450f4a2713aSLionel Sambuc   // Update the buffer.
451f4a2713aSLionel Sambuc   ResultBuf += bytesToWrite;
452f4a2713aSLionel Sambuc }
453f4a2713aSLionel Sambuc 
454f4a2713aSLionel Sambuc 
455f4a2713aSLionel Sambuc ///       integer-constant: [C99 6.4.4.1]
456f4a2713aSLionel Sambuc ///         decimal-constant integer-suffix
457f4a2713aSLionel Sambuc ///         octal-constant integer-suffix
458f4a2713aSLionel Sambuc ///         hexadecimal-constant integer-suffix
459f4a2713aSLionel Sambuc ///         binary-literal integer-suffix [GNU, C++1y]
460f4a2713aSLionel Sambuc ///       user-defined-integer-literal: [C++11 lex.ext]
461f4a2713aSLionel Sambuc ///         decimal-literal ud-suffix
462f4a2713aSLionel Sambuc ///         octal-literal ud-suffix
463f4a2713aSLionel Sambuc ///         hexadecimal-literal ud-suffix
464f4a2713aSLionel Sambuc ///         binary-literal ud-suffix [GNU, C++1y]
465f4a2713aSLionel Sambuc ///       decimal-constant:
466f4a2713aSLionel Sambuc ///         nonzero-digit
467f4a2713aSLionel Sambuc ///         decimal-constant digit
468f4a2713aSLionel Sambuc ///       octal-constant:
469f4a2713aSLionel Sambuc ///         0
470f4a2713aSLionel Sambuc ///         octal-constant octal-digit
471f4a2713aSLionel Sambuc ///       hexadecimal-constant:
472f4a2713aSLionel Sambuc ///         hexadecimal-prefix hexadecimal-digit
473f4a2713aSLionel Sambuc ///         hexadecimal-constant hexadecimal-digit
474f4a2713aSLionel Sambuc ///       hexadecimal-prefix: one of
475f4a2713aSLionel Sambuc ///         0x 0X
476f4a2713aSLionel Sambuc ///       binary-literal:
477f4a2713aSLionel Sambuc ///         0b binary-digit
478f4a2713aSLionel Sambuc ///         0B binary-digit
479f4a2713aSLionel Sambuc ///         binary-literal binary-digit
480f4a2713aSLionel Sambuc ///       integer-suffix:
481f4a2713aSLionel Sambuc ///         unsigned-suffix [long-suffix]
482f4a2713aSLionel Sambuc ///         unsigned-suffix [long-long-suffix]
483f4a2713aSLionel Sambuc ///         long-suffix [unsigned-suffix]
484f4a2713aSLionel Sambuc ///         long-long-suffix [unsigned-sufix]
485f4a2713aSLionel Sambuc ///       nonzero-digit:
486f4a2713aSLionel Sambuc ///         1 2 3 4 5 6 7 8 9
487f4a2713aSLionel Sambuc ///       octal-digit:
488f4a2713aSLionel Sambuc ///         0 1 2 3 4 5 6 7
489f4a2713aSLionel Sambuc ///       hexadecimal-digit:
490f4a2713aSLionel Sambuc ///         0 1 2 3 4 5 6 7 8 9
491f4a2713aSLionel Sambuc ///         a b c d e f
492f4a2713aSLionel Sambuc ///         A B C D E F
493f4a2713aSLionel Sambuc ///       binary-digit:
494f4a2713aSLionel Sambuc ///         0
495f4a2713aSLionel Sambuc ///         1
496f4a2713aSLionel Sambuc ///       unsigned-suffix: one of
497f4a2713aSLionel Sambuc ///         u U
498f4a2713aSLionel Sambuc ///       long-suffix: one of
499f4a2713aSLionel Sambuc ///         l L
500f4a2713aSLionel Sambuc ///       long-long-suffix: one of
501f4a2713aSLionel Sambuc ///         ll LL
502f4a2713aSLionel Sambuc ///
503f4a2713aSLionel Sambuc ///       floating-constant: [C99 6.4.4.2]
504f4a2713aSLionel Sambuc ///         TODO: add rules...
505f4a2713aSLionel Sambuc ///
NumericLiteralParser(StringRef TokSpelling,SourceLocation TokLoc,Preprocessor & PP)506f4a2713aSLionel Sambuc NumericLiteralParser::NumericLiteralParser(StringRef TokSpelling,
507f4a2713aSLionel Sambuc                                            SourceLocation TokLoc,
508f4a2713aSLionel Sambuc                                            Preprocessor &PP)
509f4a2713aSLionel Sambuc   : PP(PP), ThisTokBegin(TokSpelling.begin()), ThisTokEnd(TokSpelling.end()) {
510f4a2713aSLionel Sambuc 
511f4a2713aSLionel Sambuc   // This routine assumes that the range begin/end matches the regex for integer
512f4a2713aSLionel Sambuc   // and FP constants (specifically, the 'pp-number' regex), and assumes that
513f4a2713aSLionel Sambuc   // the byte at "*end" is both valid and not part of the regex.  Because of
514f4a2713aSLionel Sambuc   // this, it doesn't have to check for 'overscan' in various places.
515f4a2713aSLionel Sambuc   assert(!isPreprocessingNumberBody(*ThisTokEnd) && "didn't maximally munch?");
516f4a2713aSLionel Sambuc 
517f4a2713aSLionel Sambuc   s = DigitsBegin = ThisTokBegin;
518f4a2713aSLionel Sambuc   saw_exponent = false;
519f4a2713aSLionel Sambuc   saw_period = false;
520f4a2713aSLionel Sambuc   saw_ud_suffix = false;
521f4a2713aSLionel Sambuc   isLong = false;
522f4a2713aSLionel Sambuc   isUnsigned = false;
523f4a2713aSLionel Sambuc   isLongLong = false;
524f4a2713aSLionel Sambuc   isFloat = false;
525f4a2713aSLionel Sambuc   isImaginary = false;
526*0a6a1f1dSLionel Sambuc   MicrosoftInteger = 0;
527f4a2713aSLionel Sambuc   hadError = false;
528f4a2713aSLionel Sambuc 
529f4a2713aSLionel Sambuc   if (*s == '0') { // parse radix
530f4a2713aSLionel Sambuc     ParseNumberStartingWithZero(TokLoc);
531f4a2713aSLionel Sambuc     if (hadError)
532f4a2713aSLionel Sambuc       return;
533f4a2713aSLionel Sambuc   } else { // the first digit is non-zero
534f4a2713aSLionel Sambuc     radix = 10;
535f4a2713aSLionel Sambuc     s = SkipDigits(s);
536f4a2713aSLionel Sambuc     if (s == ThisTokEnd) {
537f4a2713aSLionel Sambuc       // Done.
538f4a2713aSLionel Sambuc     } else if (isHexDigit(*s) && !(*s == 'e' || *s == 'E')) {
539f4a2713aSLionel Sambuc       PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin),
540f4a2713aSLionel Sambuc               diag::err_invalid_decimal_digit) << StringRef(s, 1);
541f4a2713aSLionel Sambuc       hadError = true;
542f4a2713aSLionel Sambuc       return;
543f4a2713aSLionel Sambuc     } else if (*s == '.') {
544f4a2713aSLionel Sambuc       checkSeparator(TokLoc, s, CSK_AfterDigits);
545f4a2713aSLionel Sambuc       s++;
546f4a2713aSLionel Sambuc       saw_period = true;
547f4a2713aSLionel Sambuc       checkSeparator(TokLoc, s, CSK_BeforeDigits);
548f4a2713aSLionel Sambuc       s = SkipDigits(s);
549f4a2713aSLionel Sambuc     }
550f4a2713aSLionel Sambuc     if ((*s == 'e' || *s == 'E')) { // exponent
551f4a2713aSLionel Sambuc       checkSeparator(TokLoc, s, CSK_AfterDigits);
552f4a2713aSLionel Sambuc       const char *Exponent = s;
553f4a2713aSLionel Sambuc       s++;
554f4a2713aSLionel Sambuc       saw_exponent = true;
555f4a2713aSLionel Sambuc       if (*s == '+' || *s == '-')  s++; // sign
556f4a2713aSLionel Sambuc       checkSeparator(TokLoc, s, CSK_BeforeDigits);
557f4a2713aSLionel Sambuc       const char *first_non_digit = SkipDigits(s);
558f4a2713aSLionel Sambuc       if (first_non_digit != s) {
559f4a2713aSLionel Sambuc         s = first_non_digit;
560f4a2713aSLionel Sambuc       } else {
561f4a2713aSLionel Sambuc         PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent - ThisTokBegin),
562f4a2713aSLionel Sambuc                 diag::err_exponent_has_no_digits);
563f4a2713aSLionel Sambuc         hadError = true;
564f4a2713aSLionel Sambuc         return;
565f4a2713aSLionel Sambuc       }
566f4a2713aSLionel Sambuc     }
567f4a2713aSLionel Sambuc   }
568f4a2713aSLionel Sambuc 
569f4a2713aSLionel Sambuc   SuffixBegin = s;
570f4a2713aSLionel Sambuc   checkSeparator(TokLoc, s, CSK_AfterDigits);
571f4a2713aSLionel Sambuc 
572f4a2713aSLionel Sambuc   // Parse the suffix.  At this point we can classify whether we have an FP or
573f4a2713aSLionel Sambuc   // integer constant.
574f4a2713aSLionel Sambuc   bool isFPConstant = isFloatingLiteral();
575*0a6a1f1dSLionel Sambuc   const char *ImaginarySuffixLoc = nullptr;
576f4a2713aSLionel Sambuc 
577f4a2713aSLionel Sambuc   // Loop over all of the characters of the suffix.  If we see something bad,
578f4a2713aSLionel Sambuc   // we break out of the loop.
579f4a2713aSLionel Sambuc   for (; s != ThisTokEnd; ++s) {
580f4a2713aSLionel Sambuc     switch (*s) {
581f4a2713aSLionel Sambuc     case 'f':      // FP Suffix for "float"
582f4a2713aSLionel Sambuc     case 'F':
583f4a2713aSLionel Sambuc       if (!isFPConstant) break;  // Error for integer constant.
584f4a2713aSLionel Sambuc       if (isFloat || isLong) break; // FF, LF invalid.
585f4a2713aSLionel Sambuc       isFloat = true;
586f4a2713aSLionel Sambuc       continue;  // Success.
587f4a2713aSLionel Sambuc     case 'u':
588f4a2713aSLionel Sambuc     case 'U':
589f4a2713aSLionel Sambuc       if (isFPConstant) break;  // Error for floating constant.
590f4a2713aSLionel Sambuc       if (isUnsigned) break;    // Cannot be repeated.
591f4a2713aSLionel Sambuc       isUnsigned = true;
592f4a2713aSLionel Sambuc       continue;  // Success.
593f4a2713aSLionel Sambuc     case 'l':
594f4a2713aSLionel Sambuc     case 'L':
595f4a2713aSLionel Sambuc       if (isLong || isLongLong) break;  // Cannot be repeated.
596f4a2713aSLionel Sambuc       if (isFloat) break;               // LF invalid.
597f4a2713aSLionel Sambuc 
598f4a2713aSLionel Sambuc       // Check for long long.  The L's need to be adjacent and the same case.
599f4a2713aSLionel Sambuc       if (s+1 != ThisTokEnd && s[1] == s[0]) {
600f4a2713aSLionel Sambuc         if (isFPConstant) break;        // long long invalid for floats.
601f4a2713aSLionel Sambuc         isLongLong = true;
602f4a2713aSLionel Sambuc         ++s;  // Eat both of them.
603f4a2713aSLionel Sambuc       } else {
604f4a2713aSLionel Sambuc         isLong = true;
605f4a2713aSLionel Sambuc       }
606f4a2713aSLionel Sambuc       continue;  // Success.
607f4a2713aSLionel Sambuc     case 'i':
608f4a2713aSLionel Sambuc     case 'I':
609f4a2713aSLionel Sambuc       if (PP.getLangOpts().MicrosoftExt) {
610*0a6a1f1dSLionel Sambuc         if (isLong || isLongLong || MicrosoftInteger)
611*0a6a1f1dSLionel Sambuc           break;
612f4a2713aSLionel Sambuc 
613f4a2713aSLionel Sambuc         // Allow i8, i16, i32, i64, and i128.
614f4a2713aSLionel Sambuc         if (s + 1 != ThisTokEnd) {
615f4a2713aSLionel Sambuc           switch (s[1]) {
616f4a2713aSLionel Sambuc             case '8':
617*0a6a1f1dSLionel Sambuc               if (isFPConstant) break;
618f4a2713aSLionel Sambuc               s += 2; // i8 suffix
619*0a6a1f1dSLionel Sambuc               MicrosoftInteger = 8;
620f4a2713aSLionel Sambuc               break;
621f4a2713aSLionel Sambuc             case '1':
622*0a6a1f1dSLionel Sambuc               if (isFPConstant) break;
623f4a2713aSLionel Sambuc               if (s + 2 == ThisTokEnd) break;
624f4a2713aSLionel Sambuc               if (s[2] == '6') {
625f4a2713aSLionel Sambuc                 s += 3; // i16 suffix
626*0a6a1f1dSLionel Sambuc                 MicrosoftInteger = 16;
627f4a2713aSLionel Sambuc               }
628f4a2713aSLionel Sambuc               else if (s[2] == '2') {
629f4a2713aSLionel Sambuc                 if (s + 3 == ThisTokEnd) break;
630f4a2713aSLionel Sambuc                 if (s[3] == '8') {
631f4a2713aSLionel Sambuc                   s += 4; // i128 suffix
632*0a6a1f1dSLionel Sambuc                   MicrosoftInteger = 128;
633f4a2713aSLionel Sambuc                 }
634f4a2713aSLionel Sambuc               }
635f4a2713aSLionel Sambuc               break;
636f4a2713aSLionel Sambuc             case '3':
637*0a6a1f1dSLionel Sambuc               if (isFPConstant) break;
638f4a2713aSLionel Sambuc               if (s + 2 == ThisTokEnd) break;
639f4a2713aSLionel Sambuc               if (s[2] == '2') {
640f4a2713aSLionel Sambuc                 s += 3; // i32 suffix
641*0a6a1f1dSLionel Sambuc                 MicrosoftInteger = 32;
642f4a2713aSLionel Sambuc               }
643f4a2713aSLionel Sambuc               break;
644f4a2713aSLionel Sambuc             case '6':
645*0a6a1f1dSLionel Sambuc               if (isFPConstant) break;
646f4a2713aSLionel Sambuc               if (s + 2 == ThisTokEnd) break;
647f4a2713aSLionel Sambuc               if (s[2] == '4') {
648f4a2713aSLionel Sambuc                 s += 3; // i64 suffix
649*0a6a1f1dSLionel Sambuc                 MicrosoftInteger = 64;
650f4a2713aSLionel Sambuc               }
651f4a2713aSLionel Sambuc               break;
652f4a2713aSLionel Sambuc             default:
653f4a2713aSLionel Sambuc               break;
654f4a2713aSLionel Sambuc           }
655*0a6a1f1dSLionel Sambuc           if (MicrosoftInteger)
656f4a2713aSLionel Sambuc             break;
657f4a2713aSLionel Sambuc         }
658f4a2713aSLionel Sambuc       }
659f4a2713aSLionel Sambuc       // "i", "if", and "il" are user-defined suffixes in C++1y.
660*0a6a1f1dSLionel Sambuc       if (PP.getLangOpts().CPlusPlus14 && *s == 'i')
661f4a2713aSLionel Sambuc         break;
662f4a2713aSLionel Sambuc       // fall through.
663f4a2713aSLionel Sambuc     case 'j':
664f4a2713aSLionel Sambuc     case 'J':
665f4a2713aSLionel Sambuc       if (isImaginary) break;   // Cannot be repeated.
666f4a2713aSLionel Sambuc       isImaginary = true;
667f4a2713aSLionel Sambuc       ImaginarySuffixLoc = s;
668f4a2713aSLionel Sambuc       continue;  // Success.
669f4a2713aSLionel Sambuc     }
670f4a2713aSLionel Sambuc     // If we reached here, there was an error or a ud-suffix.
671f4a2713aSLionel Sambuc     break;
672f4a2713aSLionel Sambuc   }
673f4a2713aSLionel Sambuc 
674f4a2713aSLionel Sambuc   if (s != ThisTokEnd) {
675*0a6a1f1dSLionel Sambuc     // FIXME: Don't bother expanding UCNs if !tok.hasUCN().
676*0a6a1f1dSLionel Sambuc     expandUCNs(UDSuffixBuf, StringRef(SuffixBegin, ThisTokEnd - SuffixBegin));
677*0a6a1f1dSLionel Sambuc     if (isValidUDSuffix(PP.getLangOpts(), UDSuffixBuf)) {
678f4a2713aSLionel Sambuc       // Any suffix pieces we might have parsed are actually part of the
679f4a2713aSLionel Sambuc       // ud-suffix.
680f4a2713aSLionel Sambuc       isLong = false;
681f4a2713aSLionel Sambuc       isUnsigned = false;
682f4a2713aSLionel Sambuc       isLongLong = false;
683f4a2713aSLionel Sambuc       isFloat = false;
684f4a2713aSLionel Sambuc       isImaginary = false;
685*0a6a1f1dSLionel Sambuc       MicrosoftInteger = 0;
686f4a2713aSLionel Sambuc 
687f4a2713aSLionel Sambuc       saw_ud_suffix = true;
688f4a2713aSLionel Sambuc       return;
689f4a2713aSLionel Sambuc     }
690f4a2713aSLionel Sambuc 
691f4a2713aSLionel Sambuc     // Report an error if there are any.
692f4a2713aSLionel Sambuc     PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, SuffixBegin - ThisTokBegin),
693f4a2713aSLionel Sambuc             isFPConstant ? diag::err_invalid_suffix_float_constant :
694f4a2713aSLionel Sambuc                            diag::err_invalid_suffix_integer_constant)
695f4a2713aSLionel Sambuc       << StringRef(SuffixBegin, ThisTokEnd-SuffixBegin);
696f4a2713aSLionel Sambuc     hadError = true;
697f4a2713aSLionel Sambuc     return;
698f4a2713aSLionel Sambuc   }
699f4a2713aSLionel Sambuc 
700f4a2713aSLionel Sambuc   if (isImaginary) {
701f4a2713aSLionel Sambuc     PP.Diag(PP.AdvanceToTokenCharacter(TokLoc,
702f4a2713aSLionel Sambuc                                        ImaginarySuffixLoc - ThisTokBegin),
703f4a2713aSLionel Sambuc             diag::ext_imaginary_constant);
704f4a2713aSLionel Sambuc   }
705f4a2713aSLionel Sambuc }
706f4a2713aSLionel Sambuc 
707f4a2713aSLionel Sambuc /// Determine whether a suffix is a valid ud-suffix. We avoid treating reserved
708f4a2713aSLionel Sambuc /// suffixes as ud-suffixes, because the diagnostic experience is better if we
709f4a2713aSLionel Sambuc /// treat it as an invalid suffix.
isValidUDSuffix(const LangOptions & LangOpts,StringRef Suffix)710f4a2713aSLionel Sambuc bool NumericLiteralParser::isValidUDSuffix(const LangOptions &LangOpts,
711f4a2713aSLionel Sambuc                                            StringRef Suffix) {
712f4a2713aSLionel Sambuc   if (!LangOpts.CPlusPlus11 || Suffix.empty())
713f4a2713aSLionel Sambuc     return false;
714f4a2713aSLionel Sambuc 
715f4a2713aSLionel Sambuc   // By C++11 [lex.ext]p10, ud-suffixes starting with an '_' are always valid.
716f4a2713aSLionel Sambuc   if (Suffix[0] == '_')
717f4a2713aSLionel Sambuc     return true;
718f4a2713aSLionel Sambuc 
719f4a2713aSLionel Sambuc   // In C++11, there are no library suffixes.
720*0a6a1f1dSLionel Sambuc   if (!LangOpts.CPlusPlus14)
721f4a2713aSLionel Sambuc     return false;
722f4a2713aSLionel Sambuc 
723f4a2713aSLionel Sambuc   // In C++1y, "s", "h", "min", "ms", "us", and "ns" are used in the library.
724f4a2713aSLionel Sambuc   // Per tweaked N3660, "il", "i", and "if" are also used in the library.
725f4a2713aSLionel Sambuc   return llvm::StringSwitch<bool>(Suffix)
726f4a2713aSLionel Sambuc       .Cases("h", "min", "s", true)
727f4a2713aSLionel Sambuc       .Cases("ms", "us", "ns", true)
728f4a2713aSLionel Sambuc       .Cases("il", "i", "if", true)
729f4a2713aSLionel Sambuc       .Default(false);
730f4a2713aSLionel Sambuc }
731f4a2713aSLionel Sambuc 
checkSeparator(SourceLocation TokLoc,const char * Pos,CheckSeparatorKind IsAfterDigits)732f4a2713aSLionel Sambuc void NumericLiteralParser::checkSeparator(SourceLocation TokLoc,
733f4a2713aSLionel Sambuc                                           const char *Pos,
734f4a2713aSLionel Sambuc                                           CheckSeparatorKind IsAfterDigits) {
735f4a2713aSLionel Sambuc   if (IsAfterDigits == CSK_AfterDigits) {
736f4a2713aSLionel Sambuc     if (Pos == ThisTokBegin)
737f4a2713aSLionel Sambuc       return;
738f4a2713aSLionel Sambuc     --Pos;
739f4a2713aSLionel Sambuc   } else if (Pos == ThisTokEnd)
740f4a2713aSLionel Sambuc     return;
741f4a2713aSLionel Sambuc 
742f4a2713aSLionel Sambuc   if (isDigitSeparator(*Pos))
743f4a2713aSLionel Sambuc     PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Pos - ThisTokBegin),
744f4a2713aSLionel Sambuc             diag::err_digit_separator_not_between_digits)
745f4a2713aSLionel Sambuc       << IsAfterDigits;
746f4a2713aSLionel Sambuc }
747f4a2713aSLionel Sambuc 
748f4a2713aSLionel Sambuc /// ParseNumberStartingWithZero - This method is called when the first character
749f4a2713aSLionel Sambuc /// of the number is found to be a zero.  This means it is either an octal
750f4a2713aSLionel Sambuc /// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or
751f4a2713aSLionel Sambuc /// a floating point number (01239.123e4).  Eat the prefix, determining the
752f4a2713aSLionel Sambuc /// radix etc.
ParseNumberStartingWithZero(SourceLocation TokLoc)753f4a2713aSLionel Sambuc void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) {
754f4a2713aSLionel Sambuc   assert(s[0] == '0' && "Invalid method call");
755f4a2713aSLionel Sambuc   s++;
756f4a2713aSLionel Sambuc 
757f4a2713aSLionel Sambuc   int c1 = s[0];
758f4a2713aSLionel Sambuc   int c2 = s[1];
759f4a2713aSLionel Sambuc 
760f4a2713aSLionel Sambuc   // Handle a hex number like 0x1234.
761f4a2713aSLionel Sambuc   if ((c1 == 'x' || c1 == 'X') && (isHexDigit(c2) || c2 == '.')) {
762f4a2713aSLionel Sambuc     s++;
763f4a2713aSLionel Sambuc     radix = 16;
764f4a2713aSLionel Sambuc     DigitsBegin = s;
765f4a2713aSLionel Sambuc     s = SkipHexDigits(s);
766f4a2713aSLionel Sambuc     bool noSignificand = (s == DigitsBegin);
767f4a2713aSLionel Sambuc     if (s == ThisTokEnd) {
768f4a2713aSLionel Sambuc       // Done.
769f4a2713aSLionel Sambuc     } else if (*s == '.') {
770f4a2713aSLionel Sambuc       s++;
771f4a2713aSLionel Sambuc       saw_period = true;
772f4a2713aSLionel Sambuc       const char *floatDigitsBegin = s;
773*0a6a1f1dSLionel Sambuc       checkSeparator(TokLoc, s, CSK_BeforeDigits);
774f4a2713aSLionel Sambuc       s = SkipHexDigits(s);
775f4a2713aSLionel Sambuc       noSignificand &= (floatDigitsBegin == s);
776f4a2713aSLionel Sambuc     }
777f4a2713aSLionel Sambuc 
778f4a2713aSLionel Sambuc     if (noSignificand) {
779f4a2713aSLionel Sambuc       PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s - ThisTokBegin),
780f4a2713aSLionel Sambuc         diag::err_hexconstant_requires_digits);
781f4a2713aSLionel Sambuc       hadError = true;
782f4a2713aSLionel Sambuc       return;
783f4a2713aSLionel Sambuc     }
784f4a2713aSLionel Sambuc 
785f4a2713aSLionel Sambuc     // A binary exponent can appear with or with a '.'. If dotted, the
786f4a2713aSLionel Sambuc     // binary exponent is required.
787f4a2713aSLionel Sambuc     if (*s == 'p' || *s == 'P') {
788*0a6a1f1dSLionel Sambuc       checkSeparator(TokLoc, s, CSK_AfterDigits);
789f4a2713aSLionel Sambuc       const char *Exponent = s;
790f4a2713aSLionel Sambuc       s++;
791f4a2713aSLionel Sambuc       saw_exponent = true;
792f4a2713aSLionel Sambuc       if (*s == '+' || *s == '-')  s++; // sign
793f4a2713aSLionel Sambuc       const char *first_non_digit = SkipDigits(s);
794f4a2713aSLionel Sambuc       if (first_non_digit == s) {
795f4a2713aSLionel Sambuc         PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
796f4a2713aSLionel Sambuc                 diag::err_exponent_has_no_digits);
797f4a2713aSLionel Sambuc         hadError = true;
798f4a2713aSLionel Sambuc         return;
799f4a2713aSLionel Sambuc       }
800*0a6a1f1dSLionel Sambuc       checkSeparator(TokLoc, s, CSK_BeforeDigits);
801f4a2713aSLionel Sambuc       s = first_non_digit;
802f4a2713aSLionel Sambuc 
803f4a2713aSLionel Sambuc       if (!PP.getLangOpts().HexFloats)
804f4a2713aSLionel Sambuc         PP.Diag(TokLoc, diag::ext_hexconstant_invalid);
805f4a2713aSLionel Sambuc     } else if (saw_period) {
806f4a2713aSLionel Sambuc       PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
807f4a2713aSLionel Sambuc               diag::err_hexconstant_requires_exponent);
808f4a2713aSLionel Sambuc       hadError = true;
809f4a2713aSLionel Sambuc     }
810f4a2713aSLionel Sambuc     return;
811f4a2713aSLionel Sambuc   }
812f4a2713aSLionel Sambuc 
813f4a2713aSLionel Sambuc   // Handle simple binary numbers 0b01010
814f4a2713aSLionel Sambuc   if ((c1 == 'b' || c1 == 'B') && (c2 == '0' || c2 == '1')) {
815f4a2713aSLionel Sambuc     // 0b101010 is a C++1y / GCC extension.
816f4a2713aSLionel Sambuc     PP.Diag(TokLoc,
817*0a6a1f1dSLionel Sambuc             PP.getLangOpts().CPlusPlus14
818f4a2713aSLionel Sambuc               ? diag::warn_cxx11_compat_binary_literal
819f4a2713aSLionel Sambuc               : PP.getLangOpts().CPlusPlus
820*0a6a1f1dSLionel Sambuc                 ? diag::ext_binary_literal_cxx14
821f4a2713aSLionel Sambuc                 : diag::ext_binary_literal);
822f4a2713aSLionel Sambuc     ++s;
823f4a2713aSLionel Sambuc     radix = 2;
824f4a2713aSLionel Sambuc     DigitsBegin = s;
825f4a2713aSLionel Sambuc     s = SkipBinaryDigits(s);
826f4a2713aSLionel Sambuc     if (s == ThisTokEnd) {
827f4a2713aSLionel Sambuc       // Done.
828f4a2713aSLionel Sambuc     } else if (isHexDigit(*s)) {
829f4a2713aSLionel Sambuc       PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
830f4a2713aSLionel Sambuc               diag::err_invalid_binary_digit) << StringRef(s, 1);
831f4a2713aSLionel Sambuc       hadError = true;
832f4a2713aSLionel Sambuc     }
833f4a2713aSLionel Sambuc     // Other suffixes will be diagnosed by the caller.
834f4a2713aSLionel Sambuc     return;
835f4a2713aSLionel Sambuc   }
836f4a2713aSLionel Sambuc 
837f4a2713aSLionel Sambuc   // For now, the radix is set to 8. If we discover that we have a
838f4a2713aSLionel Sambuc   // floating point constant, the radix will change to 10. Octal floating
839f4a2713aSLionel Sambuc   // point constants are not permitted (only decimal and hexadecimal).
840f4a2713aSLionel Sambuc   radix = 8;
841f4a2713aSLionel Sambuc   DigitsBegin = s;
842f4a2713aSLionel Sambuc   s = SkipOctalDigits(s);
843f4a2713aSLionel Sambuc   if (s == ThisTokEnd)
844f4a2713aSLionel Sambuc     return; // Done, simple octal number like 01234
845f4a2713aSLionel Sambuc 
846f4a2713aSLionel Sambuc   // If we have some other non-octal digit that *is* a decimal digit, see if
847f4a2713aSLionel Sambuc   // this is part of a floating point number like 094.123 or 09e1.
848f4a2713aSLionel Sambuc   if (isDigit(*s)) {
849f4a2713aSLionel Sambuc     const char *EndDecimal = SkipDigits(s);
850f4a2713aSLionel Sambuc     if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') {
851f4a2713aSLionel Sambuc       s = EndDecimal;
852f4a2713aSLionel Sambuc       radix = 10;
853f4a2713aSLionel Sambuc     }
854f4a2713aSLionel Sambuc   }
855f4a2713aSLionel Sambuc 
856f4a2713aSLionel Sambuc   // If we have a hex digit other than 'e' (which denotes a FP exponent) then
857f4a2713aSLionel Sambuc   // the code is using an incorrect base.
858f4a2713aSLionel Sambuc   if (isHexDigit(*s) && *s != 'e' && *s != 'E') {
859f4a2713aSLionel Sambuc     PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin),
860f4a2713aSLionel Sambuc             diag::err_invalid_octal_digit) << StringRef(s, 1);
861f4a2713aSLionel Sambuc     hadError = true;
862f4a2713aSLionel Sambuc     return;
863f4a2713aSLionel Sambuc   }
864f4a2713aSLionel Sambuc 
865f4a2713aSLionel Sambuc   if (*s == '.') {
866f4a2713aSLionel Sambuc     s++;
867f4a2713aSLionel Sambuc     radix = 10;
868f4a2713aSLionel Sambuc     saw_period = true;
869*0a6a1f1dSLionel Sambuc     checkSeparator(TokLoc, s, CSK_BeforeDigits);
870f4a2713aSLionel Sambuc     s = SkipDigits(s); // Skip suffix.
871f4a2713aSLionel Sambuc   }
872f4a2713aSLionel Sambuc   if (*s == 'e' || *s == 'E') { // exponent
873*0a6a1f1dSLionel Sambuc     checkSeparator(TokLoc, s, CSK_AfterDigits);
874f4a2713aSLionel Sambuc     const char *Exponent = s;
875f4a2713aSLionel Sambuc     s++;
876f4a2713aSLionel Sambuc     radix = 10;
877f4a2713aSLionel Sambuc     saw_exponent = true;
878f4a2713aSLionel Sambuc     if (*s == '+' || *s == '-')  s++; // sign
879f4a2713aSLionel Sambuc     const char *first_non_digit = SkipDigits(s);
880f4a2713aSLionel Sambuc     if (first_non_digit != s) {
881*0a6a1f1dSLionel Sambuc       checkSeparator(TokLoc, s, CSK_BeforeDigits);
882f4a2713aSLionel Sambuc       s = first_non_digit;
883f4a2713aSLionel Sambuc     } else {
884f4a2713aSLionel Sambuc       PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin),
885f4a2713aSLionel Sambuc               diag::err_exponent_has_no_digits);
886f4a2713aSLionel Sambuc       hadError = true;
887f4a2713aSLionel Sambuc       return;
888f4a2713aSLionel Sambuc     }
889f4a2713aSLionel Sambuc   }
890f4a2713aSLionel Sambuc }
891f4a2713aSLionel Sambuc 
alwaysFitsInto64Bits(unsigned Radix,unsigned NumDigits)892f4a2713aSLionel Sambuc static bool alwaysFitsInto64Bits(unsigned Radix, unsigned NumDigits) {
893f4a2713aSLionel Sambuc   switch (Radix) {
894f4a2713aSLionel Sambuc   case 2:
895f4a2713aSLionel Sambuc     return NumDigits <= 64;
896f4a2713aSLionel Sambuc   case 8:
897f4a2713aSLionel Sambuc     return NumDigits <= 64 / 3; // Digits are groups of 3 bits.
898f4a2713aSLionel Sambuc   case 10:
899f4a2713aSLionel Sambuc     return NumDigits <= 19; // floor(log10(2^64))
900f4a2713aSLionel Sambuc   case 16:
901f4a2713aSLionel Sambuc     return NumDigits <= 64 / 4; // Digits are groups of 4 bits.
902f4a2713aSLionel Sambuc   default:
903f4a2713aSLionel Sambuc     llvm_unreachable("impossible Radix");
904f4a2713aSLionel Sambuc   }
905f4a2713aSLionel Sambuc }
906f4a2713aSLionel Sambuc 
907f4a2713aSLionel Sambuc /// GetIntegerValue - Convert this numeric literal value to an APInt that
908f4a2713aSLionel Sambuc /// matches Val's input width.  If there is an overflow, set Val to the low bits
909f4a2713aSLionel Sambuc /// of the result and return true.  Otherwise, return false.
GetIntegerValue(llvm::APInt & Val)910f4a2713aSLionel Sambuc bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) {
911f4a2713aSLionel Sambuc   // Fast path: Compute a conservative bound on the maximum number of
912f4a2713aSLionel Sambuc   // bits per digit in this radix. If we can't possibly overflow a
913f4a2713aSLionel Sambuc   // uint64 based on that bound then do the simple conversion to
914f4a2713aSLionel Sambuc   // integer. This avoids the expensive overflow checking below, and
915f4a2713aSLionel Sambuc   // handles the common cases that matter (small decimal integers and
916f4a2713aSLionel Sambuc   // hex/octal values which don't overflow).
917f4a2713aSLionel Sambuc   const unsigned NumDigits = SuffixBegin - DigitsBegin;
918f4a2713aSLionel Sambuc   if (alwaysFitsInto64Bits(radix, NumDigits)) {
919f4a2713aSLionel Sambuc     uint64_t N = 0;
920f4a2713aSLionel Sambuc     for (const char *Ptr = DigitsBegin; Ptr != SuffixBegin; ++Ptr)
921f4a2713aSLionel Sambuc       if (!isDigitSeparator(*Ptr))
922f4a2713aSLionel Sambuc         N = N * radix + llvm::hexDigitValue(*Ptr);
923f4a2713aSLionel Sambuc 
924f4a2713aSLionel Sambuc     // This will truncate the value to Val's input width. Simply check
925f4a2713aSLionel Sambuc     // for overflow by comparing.
926f4a2713aSLionel Sambuc     Val = N;
927f4a2713aSLionel Sambuc     return Val.getZExtValue() != N;
928f4a2713aSLionel Sambuc   }
929f4a2713aSLionel Sambuc 
930f4a2713aSLionel Sambuc   Val = 0;
931f4a2713aSLionel Sambuc   const char *Ptr = DigitsBegin;
932f4a2713aSLionel Sambuc 
933f4a2713aSLionel Sambuc   llvm::APInt RadixVal(Val.getBitWidth(), radix);
934f4a2713aSLionel Sambuc   llvm::APInt CharVal(Val.getBitWidth(), 0);
935f4a2713aSLionel Sambuc   llvm::APInt OldVal = Val;
936f4a2713aSLionel Sambuc 
937f4a2713aSLionel Sambuc   bool OverflowOccurred = false;
938f4a2713aSLionel Sambuc   while (Ptr < SuffixBegin) {
939f4a2713aSLionel Sambuc     if (isDigitSeparator(*Ptr)) {
940f4a2713aSLionel Sambuc       ++Ptr;
941f4a2713aSLionel Sambuc       continue;
942f4a2713aSLionel Sambuc     }
943f4a2713aSLionel Sambuc 
944f4a2713aSLionel Sambuc     unsigned C = llvm::hexDigitValue(*Ptr++);
945f4a2713aSLionel Sambuc 
946f4a2713aSLionel Sambuc     // If this letter is out of bound for this radix, reject it.
947f4a2713aSLionel Sambuc     assert(C < radix && "NumericLiteralParser ctor should have rejected this");
948f4a2713aSLionel Sambuc 
949f4a2713aSLionel Sambuc     CharVal = C;
950f4a2713aSLionel Sambuc 
951f4a2713aSLionel Sambuc     // Add the digit to the value in the appropriate radix.  If adding in digits
952f4a2713aSLionel Sambuc     // made the value smaller, then this overflowed.
953f4a2713aSLionel Sambuc     OldVal = Val;
954f4a2713aSLionel Sambuc 
955f4a2713aSLionel Sambuc     // Multiply by radix, did overflow occur on the multiply?
956f4a2713aSLionel Sambuc     Val *= RadixVal;
957f4a2713aSLionel Sambuc     OverflowOccurred |= Val.udiv(RadixVal) != OldVal;
958f4a2713aSLionel Sambuc 
959f4a2713aSLionel Sambuc     // Add value, did overflow occur on the value?
960f4a2713aSLionel Sambuc     //   (a + b) ult b  <=> overflow
961f4a2713aSLionel Sambuc     Val += CharVal;
962f4a2713aSLionel Sambuc     OverflowOccurred |= Val.ult(CharVal);
963f4a2713aSLionel Sambuc   }
964f4a2713aSLionel Sambuc   return OverflowOccurred;
965f4a2713aSLionel Sambuc }
966f4a2713aSLionel Sambuc 
967f4a2713aSLionel Sambuc llvm::APFloat::opStatus
GetFloatValue(llvm::APFloat & Result)968f4a2713aSLionel Sambuc NumericLiteralParser::GetFloatValue(llvm::APFloat &Result) {
969f4a2713aSLionel Sambuc   using llvm::APFloat;
970f4a2713aSLionel Sambuc 
971f4a2713aSLionel Sambuc   unsigned n = std::min(SuffixBegin - ThisTokBegin, ThisTokEnd - ThisTokBegin);
972f4a2713aSLionel Sambuc 
973f4a2713aSLionel Sambuc   llvm::SmallString<16> Buffer;
974f4a2713aSLionel Sambuc   StringRef Str(ThisTokBegin, n);
975f4a2713aSLionel Sambuc   if (Str.find('\'') != StringRef::npos) {
976f4a2713aSLionel Sambuc     Buffer.reserve(n);
977f4a2713aSLionel Sambuc     std::remove_copy_if(Str.begin(), Str.end(), std::back_inserter(Buffer),
978f4a2713aSLionel Sambuc                         &isDigitSeparator);
979f4a2713aSLionel Sambuc     Str = Buffer;
980f4a2713aSLionel Sambuc   }
981f4a2713aSLionel Sambuc 
982f4a2713aSLionel Sambuc   return Result.convertFromString(Str, APFloat::rmNearestTiesToEven);
983f4a2713aSLionel Sambuc }
984f4a2713aSLionel Sambuc 
985f4a2713aSLionel Sambuc 
986f4a2713aSLionel Sambuc /// \verbatim
987f4a2713aSLionel Sambuc ///       user-defined-character-literal: [C++11 lex.ext]
988f4a2713aSLionel Sambuc ///         character-literal ud-suffix
989f4a2713aSLionel Sambuc ///       ud-suffix:
990f4a2713aSLionel Sambuc ///         identifier
991f4a2713aSLionel Sambuc ///       character-literal: [C++11 lex.ccon]
992f4a2713aSLionel Sambuc ///         ' c-char-sequence '
993f4a2713aSLionel Sambuc ///         u' c-char-sequence '
994f4a2713aSLionel Sambuc ///         U' c-char-sequence '
995f4a2713aSLionel Sambuc ///         L' c-char-sequence '
996f4a2713aSLionel Sambuc ///       c-char-sequence:
997f4a2713aSLionel Sambuc ///         c-char
998f4a2713aSLionel Sambuc ///         c-char-sequence c-char
999f4a2713aSLionel Sambuc ///       c-char:
1000f4a2713aSLionel Sambuc ///         any member of the source character set except the single-quote ',
1001f4a2713aSLionel Sambuc ///           backslash \, or new-line character
1002f4a2713aSLionel Sambuc ///         escape-sequence
1003f4a2713aSLionel Sambuc ///         universal-character-name
1004f4a2713aSLionel Sambuc ///       escape-sequence:
1005f4a2713aSLionel Sambuc ///         simple-escape-sequence
1006f4a2713aSLionel Sambuc ///         octal-escape-sequence
1007f4a2713aSLionel Sambuc ///         hexadecimal-escape-sequence
1008f4a2713aSLionel Sambuc ///       simple-escape-sequence:
1009f4a2713aSLionel Sambuc ///         one of \' \" \? \\ \a \b \f \n \r \t \v
1010f4a2713aSLionel Sambuc ///       octal-escape-sequence:
1011f4a2713aSLionel Sambuc ///         \ octal-digit
1012f4a2713aSLionel Sambuc ///         \ octal-digit octal-digit
1013f4a2713aSLionel Sambuc ///         \ octal-digit octal-digit octal-digit
1014f4a2713aSLionel Sambuc ///       hexadecimal-escape-sequence:
1015f4a2713aSLionel Sambuc ///         \x hexadecimal-digit
1016f4a2713aSLionel Sambuc ///         hexadecimal-escape-sequence hexadecimal-digit
1017f4a2713aSLionel Sambuc ///       universal-character-name: [C++11 lex.charset]
1018f4a2713aSLionel Sambuc ///         \u hex-quad
1019f4a2713aSLionel Sambuc ///         \U hex-quad hex-quad
1020f4a2713aSLionel Sambuc ///       hex-quad:
1021f4a2713aSLionel Sambuc ///         hex-digit hex-digit hex-digit hex-digit
1022f4a2713aSLionel Sambuc /// \endverbatim
1023f4a2713aSLionel Sambuc ///
CharLiteralParser(const char * begin,const char * end,SourceLocation Loc,Preprocessor & PP,tok::TokenKind kind)1024f4a2713aSLionel Sambuc CharLiteralParser::CharLiteralParser(const char *begin, const char *end,
1025f4a2713aSLionel Sambuc                                      SourceLocation Loc, Preprocessor &PP,
1026f4a2713aSLionel Sambuc                                      tok::TokenKind kind) {
1027f4a2713aSLionel Sambuc   // At this point we know that the character matches the regex "(L|u|U)?'.*'".
1028f4a2713aSLionel Sambuc   HadError = false;
1029f4a2713aSLionel Sambuc 
1030f4a2713aSLionel Sambuc   Kind = kind;
1031f4a2713aSLionel Sambuc 
1032f4a2713aSLionel Sambuc   const char *TokBegin = begin;
1033f4a2713aSLionel Sambuc 
1034f4a2713aSLionel Sambuc   // Skip over wide character determinant.
1035*0a6a1f1dSLionel Sambuc   if (Kind != tok::char_constant)
1036f4a2713aSLionel Sambuc     ++begin;
1037*0a6a1f1dSLionel Sambuc   if (Kind == tok::utf8_char_constant)
1038*0a6a1f1dSLionel Sambuc     ++begin;
1039f4a2713aSLionel Sambuc 
1040f4a2713aSLionel Sambuc   // Skip over the entry quote.
1041f4a2713aSLionel Sambuc   assert(begin[0] == '\'' && "Invalid token lexed");
1042f4a2713aSLionel Sambuc   ++begin;
1043f4a2713aSLionel Sambuc 
1044f4a2713aSLionel Sambuc   // Remove an optional ud-suffix.
1045f4a2713aSLionel Sambuc   if (end[-1] != '\'') {
1046f4a2713aSLionel Sambuc     const char *UDSuffixEnd = end;
1047f4a2713aSLionel Sambuc     do {
1048f4a2713aSLionel Sambuc       --end;
1049f4a2713aSLionel Sambuc     } while (end[-1] != '\'');
1050*0a6a1f1dSLionel Sambuc     // FIXME: Don't bother with this if !tok.hasUCN().
1051*0a6a1f1dSLionel Sambuc     expandUCNs(UDSuffixBuf, StringRef(end, UDSuffixEnd - end));
1052f4a2713aSLionel Sambuc     UDSuffixOffset = end - TokBegin;
1053f4a2713aSLionel Sambuc   }
1054f4a2713aSLionel Sambuc 
1055f4a2713aSLionel Sambuc   // Trim the ending quote.
1056f4a2713aSLionel Sambuc   assert(end != begin && "Invalid token lexed");
1057f4a2713aSLionel Sambuc   --end;
1058f4a2713aSLionel Sambuc 
1059f4a2713aSLionel Sambuc   // FIXME: The "Value" is an uint64_t so we can handle char literals of
1060f4a2713aSLionel Sambuc   // up to 64-bits.
1061f4a2713aSLionel Sambuc   // FIXME: This extensively assumes that 'char' is 8-bits.
1062f4a2713aSLionel Sambuc   assert(PP.getTargetInfo().getCharWidth() == 8 &&
1063f4a2713aSLionel Sambuc          "Assumes char is 8 bits");
1064f4a2713aSLionel Sambuc   assert(PP.getTargetInfo().getIntWidth() <= 64 &&
1065f4a2713aSLionel Sambuc          (PP.getTargetInfo().getIntWidth() & 7) == 0 &&
1066f4a2713aSLionel Sambuc          "Assumes sizeof(int) on target is <= 64 and a multiple of char");
1067f4a2713aSLionel Sambuc   assert(PP.getTargetInfo().getWCharWidth() <= 64 &&
1068f4a2713aSLionel Sambuc          "Assumes sizeof(wchar) on target is <= 64");
1069f4a2713aSLionel Sambuc 
1070f4a2713aSLionel Sambuc   SmallVector<uint32_t, 4> codepoint_buffer;
1071f4a2713aSLionel Sambuc   codepoint_buffer.resize(end - begin);
1072f4a2713aSLionel Sambuc   uint32_t *buffer_begin = &codepoint_buffer.front();
1073f4a2713aSLionel Sambuc   uint32_t *buffer_end = buffer_begin + codepoint_buffer.size();
1074f4a2713aSLionel Sambuc 
1075f4a2713aSLionel Sambuc   // Unicode escapes representing characters that cannot be correctly
1076f4a2713aSLionel Sambuc   // represented in a single code unit are disallowed in character literals
1077f4a2713aSLionel Sambuc   // by this implementation.
1078f4a2713aSLionel Sambuc   uint32_t largest_character_for_kind;
1079f4a2713aSLionel Sambuc   if (tok::wide_char_constant == Kind) {
1080f4a2713aSLionel Sambuc     largest_character_for_kind =
1081f4a2713aSLionel Sambuc         0xFFFFFFFFu >> (32-PP.getTargetInfo().getWCharWidth());
1082*0a6a1f1dSLionel Sambuc   } else if (tok::utf8_char_constant == Kind) {
1083*0a6a1f1dSLionel Sambuc     largest_character_for_kind = 0x7F;
1084f4a2713aSLionel Sambuc   } else if (tok::utf16_char_constant == Kind) {
1085f4a2713aSLionel Sambuc     largest_character_for_kind = 0xFFFF;
1086f4a2713aSLionel Sambuc   } else if (tok::utf32_char_constant == Kind) {
1087f4a2713aSLionel Sambuc     largest_character_for_kind = 0x10FFFF;
1088f4a2713aSLionel Sambuc   } else {
1089f4a2713aSLionel Sambuc     largest_character_for_kind = 0x7Fu;
1090f4a2713aSLionel Sambuc   }
1091f4a2713aSLionel Sambuc 
1092f4a2713aSLionel Sambuc   while (begin != end) {
1093f4a2713aSLionel Sambuc     // Is this a span of non-escape characters?
1094f4a2713aSLionel Sambuc     if (begin[0] != '\\') {
1095f4a2713aSLionel Sambuc       char const *start = begin;
1096f4a2713aSLionel Sambuc       do {
1097f4a2713aSLionel Sambuc         ++begin;
1098f4a2713aSLionel Sambuc       } while (begin != end && *begin != '\\');
1099f4a2713aSLionel Sambuc 
1100f4a2713aSLionel Sambuc       char const *tmp_in_start = start;
1101f4a2713aSLionel Sambuc       uint32_t *tmp_out_start = buffer_begin;
1102f4a2713aSLionel Sambuc       ConversionResult res =
1103f4a2713aSLionel Sambuc           ConvertUTF8toUTF32(reinterpret_cast<UTF8 const **>(&start),
1104f4a2713aSLionel Sambuc                              reinterpret_cast<UTF8 const *>(begin),
1105f4a2713aSLionel Sambuc                              &buffer_begin, buffer_end, strictConversion);
1106f4a2713aSLionel Sambuc       if (res != conversionOK) {
1107f4a2713aSLionel Sambuc         // If we see bad encoding for unprefixed character literals, warn and
1108f4a2713aSLionel Sambuc         // simply copy the byte values, for compatibility with gcc and
1109f4a2713aSLionel Sambuc         // older versions of clang.
1110f4a2713aSLionel Sambuc         bool NoErrorOnBadEncoding = isAscii();
1111f4a2713aSLionel Sambuc         unsigned Msg = diag::err_bad_character_encoding;
1112f4a2713aSLionel Sambuc         if (NoErrorOnBadEncoding)
1113f4a2713aSLionel Sambuc           Msg = diag::warn_bad_character_encoding;
1114f4a2713aSLionel Sambuc         PP.Diag(Loc, Msg);
1115f4a2713aSLionel Sambuc         if (NoErrorOnBadEncoding) {
1116f4a2713aSLionel Sambuc           start = tmp_in_start;
1117f4a2713aSLionel Sambuc           buffer_begin = tmp_out_start;
1118f4a2713aSLionel Sambuc           for (; start != begin; ++start, ++buffer_begin)
1119f4a2713aSLionel Sambuc             *buffer_begin = static_cast<uint8_t>(*start);
1120f4a2713aSLionel Sambuc         } else {
1121f4a2713aSLionel Sambuc           HadError = true;
1122f4a2713aSLionel Sambuc         }
1123f4a2713aSLionel Sambuc       } else {
1124f4a2713aSLionel Sambuc         for (; tmp_out_start < buffer_begin; ++tmp_out_start) {
1125f4a2713aSLionel Sambuc           if (*tmp_out_start > largest_character_for_kind) {
1126f4a2713aSLionel Sambuc             HadError = true;
1127f4a2713aSLionel Sambuc             PP.Diag(Loc, diag::err_character_too_large);
1128f4a2713aSLionel Sambuc           }
1129f4a2713aSLionel Sambuc         }
1130f4a2713aSLionel Sambuc       }
1131f4a2713aSLionel Sambuc 
1132f4a2713aSLionel Sambuc       continue;
1133f4a2713aSLionel Sambuc     }
1134f4a2713aSLionel Sambuc     // Is this a Universal Character Name escape?
1135f4a2713aSLionel Sambuc     if (begin[1] == 'u' || begin[1] == 'U') {
1136f4a2713aSLionel Sambuc       unsigned short UcnLen = 0;
1137f4a2713aSLionel Sambuc       if (!ProcessUCNEscape(TokBegin, begin, end, *buffer_begin, UcnLen,
1138f4a2713aSLionel Sambuc                             FullSourceLoc(Loc, PP.getSourceManager()),
1139f4a2713aSLionel Sambuc                             &PP.getDiagnostics(), PP.getLangOpts(), true)) {
1140f4a2713aSLionel Sambuc         HadError = true;
1141f4a2713aSLionel Sambuc       } else if (*buffer_begin > largest_character_for_kind) {
1142f4a2713aSLionel Sambuc         HadError = true;
1143f4a2713aSLionel Sambuc         PP.Diag(Loc, diag::err_character_too_large);
1144f4a2713aSLionel Sambuc       }
1145f4a2713aSLionel Sambuc 
1146f4a2713aSLionel Sambuc       ++buffer_begin;
1147f4a2713aSLionel Sambuc       continue;
1148f4a2713aSLionel Sambuc     }
1149f4a2713aSLionel Sambuc     unsigned CharWidth = getCharWidth(Kind, PP.getTargetInfo());
1150f4a2713aSLionel Sambuc     uint64_t result =
1151f4a2713aSLionel Sambuc       ProcessCharEscape(TokBegin, begin, end, HadError,
1152f4a2713aSLionel Sambuc                         FullSourceLoc(Loc,PP.getSourceManager()),
1153f4a2713aSLionel Sambuc                         CharWidth, &PP.getDiagnostics(), PP.getLangOpts());
1154f4a2713aSLionel Sambuc     *buffer_begin++ = result;
1155f4a2713aSLionel Sambuc   }
1156f4a2713aSLionel Sambuc 
1157f4a2713aSLionel Sambuc   unsigned NumCharsSoFar = buffer_begin - &codepoint_buffer.front();
1158f4a2713aSLionel Sambuc 
1159f4a2713aSLionel Sambuc   if (NumCharsSoFar > 1) {
1160f4a2713aSLionel Sambuc     if (isWide())
1161f4a2713aSLionel Sambuc       PP.Diag(Loc, diag::warn_extraneous_char_constant);
1162f4a2713aSLionel Sambuc     else if (isAscii() && NumCharsSoFar == 4)
1163f4a2713aSLionel Sambuc       PP.Diag(Loc, diag::ext_four_char_character_literal);
1164f4a2713aSLionel Sambuc     else if (isAscii())
1165f4a2713aSLionel Sambuc       PP.Diag(Loc, diag::ext_multichar_character_literal);
1166f4a2713aSLionel Sambuc     else
1167f4a2713aSLionel Sambuc       PP.Diag(Loc, diag::err_multichar_utf_character_literal);
1168f4a2713aSLionel Sambuc     IsMultiChar = true;
1169f4a2713aSLionel Sambuc   } else {
1170f4a2713aSLionel Sambuc     IsMultiChar = false;
1171f4a2713aSLionel Sambuc   }
1172f4a2713aSLionel Sambuc 
1173f4a2713aSLionel Sambuc   llvm::APInt LitVal(PP.getTargetInfo().getIntWidth(), 0);
1174f4a2713aSLionel Sambuc 
1175f4a2713aSLionel Sambuc   // Narrow character literals act as though their value is concatenated
1176f4a2713aSLionel Sambuc   // in this implementation, but warn on overflow.
1177f4a2713aSLionel Sambuc   bool multi_char_too_long = false;
1178f4a2713aSLionel Sambuc   if (isAscii() && isMultiChar()) {
1179f4a2713aSLionel Sambuc     LitVal = 0;
1180f4a2713aSLionel Sambuc     for (size_t i = 0; i < NumCharsSoFar; ++i) {
1181f4a2713aSLionel Sambuc       // check for enough leading zeros to shift into
1182f4a2713aSLionel Sambuc       multi_char_too_long |= (LitVal.countLeadingZeros() < 8);
1183f4a2713aSLionel Sambuc       LitVal <<= 8;
1184f4a2713aSLionel Sambuc       LitVal = LitVal + (codepoint_buffer[i] & 0xFF);
1185f4a2713aSLionel Sambuc     }
1186f4a2713aSLionel Sambuc   } else if (NumCharsSoFar > 0) {
1187f4a2713aSLionel Sambuc     // otherwise just take the last character
1188f4a2713aSLionel Sambuc     LitVal = buffer_begin[-1];
1189f4a2713aSLionel Sambuc   }
1190f4a2713aSLionel Sambuc 
1191f4a2713aSLionel Sambuc   if (!HadError && multi_char_too_long) {
1192f4a2713aSLionel Sambuc     PP.Diag(Loc, diag::warn_char_constant_too_large);
1193f4a2713aSLionel Sambuc   }
1194f4a2713aSLionel Sambuc 
1195f4a2713aSLionel Sambuc   // Transfer the value from APInt to uint64_t
1196f4a2713aSLionel Sambuc   Value = LitVal.getZExtValue();
1197f4a2713aSLionel Sambuc 
1198f4a2713aSLionel Sambuc   // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1")
1199f4a2713aSLionel Sambuc   // if 'char' is signed for this target (C99 6.4.4.4p10).  Note that multiple
1200f4a2713aSLionel Sambuc   // character constants are not sign extended in the this implementation:
1201f4a2713aSLionel Sambuc   // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC.
1202f4a2713aSLionel Sambuc   if (isAscii() && NumCharsSoFar == 1 && (Value & 128) &&
1203f4a2713aSLionel Sambuc       PP.getLangOpts().CharIsSigned)
1204f4a2713aSLionel Sambuc     Value = (signed char)Value;
1205f4a2713aSLionel Sambuc }
1206f4a2713aSLionel Sambuc 
1207f4a2713aSLionel Sambuc /// \verbatim
1208f4a2713aSLionel Sambuc ///       string-literal: [C++0x lex.string]
1209f4a2713aSLionel Sambuc ///         encoding-prefix " [s-char-sequence] "
1210f4a2713aSLionel Sambuc ///         encoding-prefix R raw-string
1211f4a2713aSLionel Sambuc ///       encoding-prefix:
1212f4a2713aSLionel Sambuc ///         u8
1213f4a2713aSLionel Sambuc ///         u
1214f4a2713aSLionel Sambuc ///         U
1215f4a2713aSLionel Sambuc ///         L
1216f4a2713aSLionel Sambuc ///       s-char-sequence:
1217f4a2713aSLionel Sambuc ///         s-char
1218f4a2713aSLionel Sambuc ///         s-char-sequence s-char
1219f4a2713aSLionel Sambuc ///       s-char:
1220f4a2713aSLionel Sambuc ///         any member of the source character set except the double-quote ",
1221f4a2713aSLionel Sambuc ///           backslash \, or new-line character
1222f4a2713aSLionel Sambuc ///         escape-sequence
1223f4a2713aSLionel Sambuc ///         universal-character-name
1224f4a2713aSLionel Sambuc ///       raw-string:
1225f4a2713aSLionel Sambuc ///         " d-char-sequence ( r-char-sequence ) d-char-sequence "
1226f4a2713aSLionel Sambuc ///       r-char-sequence:
1227f4a2713aSLionel Sambuc ///         r-char
1228f4a2713aSLionel Sambuc ///         r-char-sequence r-char
1229f4a2713aSLionel Sambuc ///       r-char:
1230f4a2713aSLionel Sambuc ///         any member of the source character set, except a right parenthesis )
1231f4a2713aSLionel Sambuc ///           followed by the initial d-char-sequence (which may be empty)
1232f4a2713aSLionel Sambuc ///           followed by a double quote ".
1233f4a2713aSLionel Sambuc ///       d-char-sequence:
1234f4a2713aSLionel Sambuc ///         d-char
1235f4a2713aSLionel Sambuc ///         d-char-sequence d-char
1236f4a2713aSLionel Sambuc ///       d-char:
1237f4a2713aSLionel Sambuc ///         any member of the basic source character set except:
1238f4a2713aSLionel Sambuc ///           space, the left parenthesis (, the right parenthesis ),
1239f4a2713aSLionel Sambuc ///           the backslash \, and the control characters representing horizontal
1240f4a2713aSLionel Sambuc ///           tab, vertical tab, form feed, and newline.
1241f4a2713aSLionel Sambuc ///       escape-sequence: [C++0x lex.ccon]
1242f4a2713aSLionel Sambuc ///         simple-escape-sequence
1243f4a2713aSLionel Sambuc ///         octal-escape-sequence
1244f4a2713aSLionel Sambuc ///         hexadecimal-escape-sequence
1245f4a2713aSLionel Sambuc ///       simple-escape-sequence:
1246f4a2713aSLionel Sambuc ///         one of \' \" \? \\ \a \b \f \n \r \t \v
1247f4a2713aSLionel Sambuc ///       octal-escape-sequence:
1248f4a2713aSLionel Sambuc ///         \ octal-digit
1249f4a2713aSLionel Sambuc ///         \ octal-digit octal-digit
1250f4a2713aSLionel Sambuc ///         \ octal-digit octal-digit octal-digit
1251f4a2713aSLionel Sambuc ///       hexadecimal-escape-sequence:
1252f4a2713aSLionel Sambuc ///         \x hexadecimal-digit
1253f4a2713aSLionel Sambuc ///         hexadecimal-escape-sequence hexadecimal-digit
1254f4a2713aSLionel Sambuc ///       universal-character-name:
1255f4a2713aSLionel Sambuc ///         \u hex-quad
1256f4a2713aSLionel Sambuc ///         \U hex-quad hex-quad
1257f4a2713aSLionel Sambuc ///       hex-quad:
1258f4a2713aSLionel Sambuc ///         hex-digit hex-digit hex-digit hex-digit
1259f4a2713aSLionel Sambuc /// \endverbatim
1260f4a2713aSLionel Sambuc ///
1261f4a2713aSLionel Sambuc StringLiteralParser::
StringLiteralParser(ArrayRef<Token> StringToks,Preprocessor & PP,bool Complain)1262*0a6a1f1dSLionel Sambuc StringLiteralParser(ArrayRef<Token> StringToks,
1263f4a2713aSLionel Sambuc                     Preprocessor &PP, bool Complain)
1264f4a2713aSLionel Sambuc   : SM(PP.getSourceManager()), Features(PP.getLangOpts()),
1265*0a6a1f1dSLionel Sambuc     Target(PP.getTargetInfo()), Diags(Complain ? &PP.getDiagnostics() :nullptr),
1266f4a2713aSLionel Sambuc     MaxTokenLength(0), SizeBound(0), CharByteWidth(0), Kind(tok::unknown),
1267f4a2713aSLionel Sambuc     ResultPtr(ResultBuf.data()), hadError(false), Pascal(false) {
1268*0a6a1f1dSLionel Sambuc   init(StringToks);
1269f4a2713aSLionel Sambuc }
1270f4a2713aSLionel Sambuc 
init(ArrayRef<Token> StringToks)1271*0a6a1f1dSLionel Sambuc void StringLiteralParser::init(ArrayRef<Token> StringToks){
1272f4a2713aSLionel Sambuc   // The literal token may have come from an invalid source location (e.g. due
1273f4a2713aSLionel Sambuc   // to a PCH error), in which case the token length will be 0.
1274*0a6a1f1dSLionel Sambuc   if (StringToks.empty() || StringToks[0].getLength() < 2)
1275f4a2713aSLionel Sambuc     return DiagnoseLexingError(SourceLocation());
1276f4a2713aSLionel Sambuc 
1277f4a2713aSLionel Sambuc   // Scan all of the string portions, remember the max individual token length,
1278f4a2713aSLionel Sambuc   // computing a bound on the concatenated string length, and see whether any
1279f4a2713aSLionel Sambuc   // piece is a wide-string.  If any of the string portions is a wide-string
1280f4a2713aSLionel Sambuc   // literal, the result is a wide-string literal [C99 6.4.5p4].
1281*0a6a1f1dSLionel Sambuc   assert(!StringToks.empty() && "expected at least one token");
1282f4a2713aSLionel Sambuc   MaxTokenLength = StringToks[0].getLength();
1283f4a2713aSLionel Sambuc   assert(StringToks[0].getLength() >= 2 && "literal token is invalid!");
1284f4a2713aSLionel Sambuc   SizeBound = StringToks[0].getLength()-2;  // -2 for "".
1285f4a2713aSLionel Sambuc   Kind = StringToks[0].getKind();
1286f4a2713aSLionel Sambuc 
1287f4a2713aSLionel Sambuc   hadError = false;
1288f4a2713aSLionel Sambuc 
1289f4a2713aSLionel Sambuc   // Implement Translation Phase #6: concatenation of string literals
1290f4a2713aSLionel Sambuc   /// (C99 5.1.1.2p1).  The common case is only one string fragment.
1291*0a6a1f1dSLionel Sambuc   for (unsigned i = 1; i != StringToks.size(); ++i) {
1292f4a2713aSLionel Sambuc     if (StringToks[i].getLength() < 2)
1293f4a2713aSLionel Sambuc       return DiagnoseLexingError(StringToks[i].getLocation());
1294f4a2713aSLionel Sambuc 
1295f4a2713aSLionel Sambuc     // The string could be shorter than this if it needs cleaning, but this is a
1296f4a2713aSLionel Sambuc     // reasonable bound, which is all we need.
1297f4a2713aSLionel Sambuc     assert(StringToks[i].getLength() >= 2 && "literal token is invalid!");
1298f4a2713aSLionel Sambuc     SizeBound += StringToks[i].getLength()-2;  // -2 for "".
1299f4a2713aSLionel Sambuc 
1300f4a2713aSLionel Sambuc     // Remember maximum string piece length.
1301f4a2713aSLionel Sambuc     if (StringToks[i].getLength() > MaxTokenLength)
1302f4a2713aSLionel Sambuc       MaxTokenLength = StringToks[i].getLength();
1303f4a2713aSLionel Sambuc 
1304f4a2713aSLionel Sambuc     // Remember if we see any wide or utf-8/16/32 strings.
1305f4a2713aSLionel Sambuc     // Also check for illegal concatenations.
1306f4a2713aSLionel Sambuc     if (StringToks[i].isNot(Kind) && StringToks[i].isNot(tok::string_literal)) {
1307f4a2713aSLionel Sambuc       if (isAscii()) {
1308f4a2713aSLionel Sambuc         Kind = StringToks[i].getKind();
1309f4a2713aSLionel Sambuc       } else {
1310f4a2713aSLionel Sambuc         if (Diags)
1311f4a2713aSLionel Sambuc           Diags->Report(StringToks[i].getLocation(),
1312f4a2713aSLionel Sambuc                         diag::err_unsupported_string_concat);
1313f4a2713aSLionel Sambuc         hadError = true;
1314f4a2713aSLionel Sambuc       }
1315f4a2713aSLionel Sambuc     }
1316f4a2713aSLionel Sambuc   }
1317f4a2713aSLionel Sambuc 
1318f4a2713aSLionel Sambuc   // Include space for the null terminator.
1319f4a2713aSLionel Sambuc   ++SizeBound;
1320f4a2713aSLionel Sambuc 
1321f4a2713aSLionel Sambuc   // TODO: K&R warning: "traditional C rejects string constant concatenation"
1322f4a2713aSLionel Sambuc 
1323f4a2713aSLionel Sambuc   // Get the width in bytes of char/wchar_t/char16_t/char32_t
1324f4a2713aSLionel Sambuc   CharByteWidth = getCharWidth(Kind, Target);
1325f4a2713aSLionel Sambuc   assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple");
1326f4a2713aSLionel Sambuc   CharByteWidth /= 8;
1327f4a2713aSLionel Sambuc 
1328f4a2713aSLionel Sambuc   // The output buffer size needs to be large enough to hold wide characters.
1329f4a2713aSLionel Sambuc   // This is a worst-case assumption which basically corresponds to L"" "long".
1330f4a2713aSLionel Sambuc   SizeBound *= CharByteWidth;
1331f4a2713aSLionel Sambuc 
1332f4a2713aSLionel Sambuc   // Size the temporary buffer to hold the result string data.
1333f4a2713aSLionel Sambuc   ResultBuf.resize(SizeBound);
1334f4a2713aSLionel Sambuc 
1335f4a2713aSLionel Sambuc   // Likewise, but for each string piece.
1336f4a2713aSLionel Sambuc   SmallString<512> TokenBuf;
1337f4a2713aSLionel Sambuc   TokenBuf.resize(MaxTokenLength);
1338f4a2713aSLionel Sambuc 
1339f4a2713aSLionel Sambuc   // Loop over all the strings, getting their spelling, and expanding them to
1340f4a2713aSLionel Sambuc   // wide strings as appropriate.
1341f4a2713aSLionel Sambuc   ResultPtr = &ResultBuf[0];   // Next byte to fill in.
1342f4a2713aSLionel Sambuc 
1343f4a2713aSLionel Sambuc   Pascal = false;
1344f4a2713aSLionel Sambuc 
1345f4a2713aSLionel Sambuc   SourceLocation UDSuffixTokLoc;
1346f4a2713aSLionel Sambuc 
1347*0a6a1f1dSLionel Sambuc   for (unsigned i = 0, e = StringToks.size(); i != e; ++i) {
1348f4a2713aSLionel Sambuc     const char *ThisTokBuf = &TokenBuf[0];
1349f4a2713aSLionel Sambuc     // Get the spelling of the token, which eliminates trigraphs, etc.  We know
1350f4a2713aSLionel Sambuc     // that ThisTokBuf points to a buffer that is big enough for the whole token
1351f4a2713aSLionel Sambuc     // and 'spelled' tokens can only shrink.
1352f4a2713aSLionel Sambuc     bool StringInvalid = false;
1353f4a2713aSLionel Sambuc     unsigned ThisTokLen =
1354f4a2713aSLionel Sambuc       Lexer::getSpelling(StringToks[i], ThisTokBuf, SM, Features,
1355f4a2713aSLionel Sambuc                          &StringInvalid);
1356f4a2713aSLionel Sambuc     if (StringInvalid)
1357f4a2713aSLionel Sambuc       return DiagnoseLexingError(StringToks[i].getLocation());
1358f4a2713aSLionel Sambuc 
1359f4a2713aSLionel Sambuc     const char *ThisTokBegin = ThisTokBuf;
1360f4a2713aSLionel Sambuc     const char *ThisTokEnd = ThisTokBuf+ThisTokLen;
1361f4a2713aSLionel Sambuc 
1362f4a2713aSLionel Sambuc     // Remove an optional ud-suffix.
1363f4a2713aSLionel Sambuc     if (ThisTokEnd[-1] != '"') {
1364f4a2713aSLionel Sambuc       const char *UDSuffixEnd = ThisTokEnd;
1365f4a2713aSLionel Sambuc       do {
1366f4a2713aSLionel Sambuc         --ThisTokEnd;
1367f4a2713aSLionel Sambuc       } while (ThisTokEnd[-1] != '"');
1368f4a2713aSLionel Sambuc 
1369f4a2713aSLionel Sambuc       StringRef UDSuffix(ThisTokEnd, UDSuffixEnd - ThisTokEnd);
1370f4a2713aSLionel Sambuc 
1371f4a2713aSLionel Sambuc       if (UDSuffixBuf.empty()) {
1372*0a6a1f1dSLionel Sambuc         if (StringToks[i].hasUCN())
1373*0a6a1f1dSLionel Sambuc           expandUCNs(UDSuffixBuf, UDSuffix);
1374*0a6a1f1dSLionel Sambuc         else
1375f4a2713aSLionel Sambuc           UDSuffixBuf.assign(UDSuffix);
1376f4a2713aSLionel Sambuc         UDSuffixToken = i;
1377f4a2713aSLionel Sambuc         UDSuffixOffset = ThisTokEnd - ThisTokBuf;
1378f4a2713aSLionel Sambuc         UDSuffixTokLoc = StringToks[i].getLocation();
1379*0a6a1f1dSLionel Sambuc       } else {
1380*0a6a1f1dSLionel Sambuc         SmallString<32> ExpandedUDSuffix;
1381*0a6a1f1dSLionel Sambuc         if (StringToks[i].hasUCN()) {
1382*0a6a1f1dSLionel Sambuc           expandUCNs(ExpandedUDSuffix, UDSuffix);
1383*0a6a1f1dSLionel Sambuc           UDSuffix = ExpandedUDSuffix;
1384*0a6a1f1dSLionel Sambuc         }
1385*0a6a1f1dSLionel Sambuc 
1386f4a2713aSLionel Sambuc         // C++11 [lex.ext]p8: At the end of phase 6, if a string literal is the
1387f4a2713aSLionel Sambuc         // result of a concatenation involving at least one user-defined-string-
1388f4a2713aSLionel Sambuc         // literal, all the participating user-defined-string-literals shall
1389f4a2713aSLionel Sambuc         // have the same ud-suffix.
1390*0a6a1f1dSLionel Sambuc         if (UDSuffixBuf != UDSuffix) {
1391f4a2713aSLionel Sambuc           if (Diags) {
1392f4a2713aSLionel Sambuc             SourceLocation TokLoc = StringToks[i].getLocation();
1393f4a2713aSLionel Sambuc             Diags->Report(TokLoc, diag::err_string_concat_mixed_suffix)
1394f4a2713aSLionel Sambuc               << UDSuffixBuf << UDSuffix
1395f4a2713aSLionel Sambuc               << SourceRange(UDSuffixTokLoc, UDSuffixTokLoc)
1396f4a2713aSLionel Sambuc               << SourceRange(TokLoc, TokLoc);
1397f4a2713aSLionel Sambuc           }
1398f4a2713aSLionel Sambuc           hadError = true;
1399f4a2713aSLionel Sambuc         }
1400f4a2713aSLionel Sambuc       }
1401*0a6a1f1dSLionel Sambuc     }
1402f4a2713aSLionel Sambuc 
1403f4a2713aSLionel Sambuc     // Strip the end quote.
1404f4a2713aSLionel Sambuc     --ThisTokEnd;
1405f4a2713aSLionel Sambuc 
1406f4a2713aSLionel Sambuc     // TODO: Input character set mapping support.
1407f4a2713aSLionel Sambuc 
1408f4a2713aSLionel Sambuc     // Skip marker for wide or unicode strings.
1409f4a2713aSLionel Sambuc     if (ThisTokBuf[0] == 'L' || ThisTokBuf[0] == 'u' || ThisTokBuf[0] == 'U') {
1410f4a2713aSLionel Sambuc       ++ThisTokBuf;
1411f4a2713aSLionel Sambuc       // Skip 8 of u8 marker for utf8 strings.
1412f4a2713aSLionel Sambuc       if (ThisTokBuf[0] == '8')
1413f4a2713aSLionel Sambuc         ++ThisTokBuf;
1414f4a2713aSLionel Sambuc     }
1415f4a2713aSLionel Sambuc 
1416f4a2713aSLionel Sambuc     // Check for raw string
1417f4a2713aSLionel Sambuc     if (ThisTokBuf[0] == 'R') {
1418f4a2713aSLionel Sambuc       ThisTokBuf += 2; // skip R"
1419f4a2713aSLionel Sambuc 
1420f4a2713aSLionel Sambuc       const char *Prefix = ThisTokBuf;
1421f4a2713aSLionel Sambuc       while (ThisTokBuf[0] != '(')
1422f4a2713aSLionel Sambuc         ++ThisTokBuf;
1423f4a2713aSLionel Sambuc       ++ThisTokBuf; // skip '('
1424f4a2713aSLionel Sambuc 
1425f4a2713aSLionel Sambuc       // Remove same number of characters from the end
1426f4a2713aSLionel Sambuc       ThisTokEnd -= ThisTokBuf - Prefix;
1427f4a2713aSLionel Sambuc       assert(ThisTokEnd >= ThisTokBuf && "malformed raw string literal");
1428f4a2713aSLionel Sambuc 
1429f4a2713aSLionel Sambuc       // Copy the string over
1430f4a2713aSLionel Sambuc       if (CopyStringFragment(StringToks[i], ThisTokBegin,
1431f4a2713aSLionel Sambuc                              StringRef(ThisTokBuf, ThisTokEnd - ThisTokBuf)))
1432f4a2713aSLionel Sambuc         hadError = true;
1433f4a2713aSLionel Sambuc     } else {
1434f4a2713aSLionel Sambuc       if (ThisTokBuf[0] != '"') {
1435f4a2713aSLionel Sambuc         // The file may have come from PCH and then changed after loading the
1436f4a2713aSLionel Sambuc         // PCH; Fail gracefully.
1437f4a2713aSLionel Sambuc         return DiagnoseLexingError(StringToks[i].getLocation());
1438f4a2713aSLionel Sambuc       }
1439f4a2713aSLionel Sambuc       ++ThisTokBuf; // skip "
1440f4a2713aSLionel Sambuc 
1441f4a2713aSLionel Sambuc       // Check if this is a pascal string
1442f4a2713aSLionel Sambuc       if (Features.PascalStrings && ThisTokBuf + 1 != ThisTokEnd &&
1443f4a2713aSLionel Sambuc           ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') {
1444f4a2713aSLionel Sambuc 
1445f4a2713aSLionel Sambuc         // If the \p sequence is found in the first token, we have a pascal string
1446f4a2713aSLionel Sambuc         // Otherwise, if we already have a pascal string, ignore the first \p
1447f4a2713aSLionel Sambuc         if (i == 0) {
1448f4a2713aSLionel Sambuc           ++ThisTokBuf;
1449f4a2713aSLionel Sambuc           Pascal = true;
1450f4a2713aSLionel Sambuc         } else if (Pascal)
1451f4a2713aSLionel Sambuc           ThisTokBuf += 2;
1452f4a2713aSLionel Sambuc       }
1453f4a2713aSLionel Sambuc 
1454f4a2713aSLionel Sambuc       while (ThisTokBuf != ThisTokEnd) {
1455f4a2713aSLionel Sambuc         // Is this a span of non-escape characters?
1456f4a2713aSLionel Sambuc         if (ThisTokBuf[0] != '\\') {
1457f4a2713aSLionel Sambuc           const char *InStart = ThisTokBuf;
1458f4a2713aSLionel Sambuc           do {
1459f4a2713aSLionel Sambuc             ++ThisTokBuf;
1460f4a2713aSLionel Sambuc           } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
1461f4a2713aSLionel Sambuc 
1462f4a2713aSLionel Sambuc           // Copy the character span over.
1463f4a2713aSLionel Sambuc           if (CopyStringFragment(StringToks[i], ThisTokBegin,
1464f4a2713aSLionel Sambuc                                  StringRef(InStart, ThisTokBuf - InStart)))
1465f4a2713aSLionel Sambuc             hadError = true;
1466f4a2713aSLionel Sambuc           continue;
1467f4a2713aSLionel Sambuc         }
1468f4a2713aSLionel Sambuc         // Is this a Universal Character Name escape?
1469f4a2713aSLionel Sambuc         if (ThisTokBuf[1] == 'u' || ThisTokBuf[1] == 'U') {
1470f4a2713aSLionel Sambuc           EncodeUCNEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd,
1471f4a2713aSLionel Sambuc                           ResultPtr, hadError,
1472f4a2713aSLionel Sambuc                           FullSourceLoc(StringToks[i].getLocation(), SM),
1473f4a2713aSLionel Sambuc                           CharByteWidth, Diags, Features);
1474f4a2713aSLionel Sambuc           continue;
1475f4a2713aSLionel Sambuc         }
1476f4a2713aSLionel Sambuc         // Otherwise, this is a non-UCN escape character.  Process it.
1477f4a2713aSLionel Sambuc         unsigned ResultChar =
1478f4a2713aSLionel Sambuc           ProcessCharEscape(ThisTokBegin, ThisTokBuf, ThisTokEnd, hadError,
1479f4a2713aSLionel Sambuc                             FullSourceLoc(StringToks[i].getLocation(), SM),
1480f4a2713aSLionel Sambuc                             CharByteWidth*8, Diags, Features);
1481f4a2713aSLionel Sambuc 
1482f4a2713aSLionel Sambuc         if (CharByteWidth == 4) {
1483f4a2713aSLionel Sambuc           // FIXME: Make the type of the result buffer correct instead of
1484f4a2713aSLionel Sambuc           // using reinterpret_cast.
1485f4a2713aSLionel Sambuc           UTF32 *ResultWidePtr = reinterpret_cast<UTF32*>(ResultPtr);
1486f4a2713aSLionel Sambuc           *ResultWidePtr = ResultChar;
1487f4a2713aSLionel Sambuc           ResultPtr += 4;
1488f4a2713aSLionel Sambuc         } else if (CharByteWidth == 2) {
1489f4a2713aSLionel Sambuc           // FIXME: Make the type of the result buffer correct instead of
1490f4a2713aSLionel Sambuc           // using reinterpret_cast.
1491f4a2713aSLionel Sambuc           UTF16 *ResultWidePtr = reinterpret_cast<UTF16*>(ResultPtr);
1492f4a2713aSLionel Sambuc           *ResultWidePtr = ResultChar & 0xFFFF;
1493f4a2713aSLionel Sambuc           ResultPtr += 2;
1494f4a2713aSLionel Sambuc         } else {
1495f4a2713aSLionel Sambuc           assert(CharByteWidth == 1 && "Unexpected char width");
1496f4a2713aSLionel Sambuc           *ResultPtr++ = ResultChar & 0xFF;
1497f4a2713aSLionel Sambuc         }
1498f4a2713aSLionel Sambuc       }
1499f4a2713aSLionel Sambuc     }
1500f4a2713aSLionel Sambuc   }
1501f4a2713aSLionel Sambuc 
1502f4a2713aSLionel Sambuc   if (Pascal) {
1503f4a2713aSLionel Sambuc     if (CharByteWidth == 4) {
1504f4a2713aSLionel Sambuc       // FIXME: Make the type of the result buffer correct instead of
1505f4a2713aSLionel Sambuc       // using reinterpret_cast.
1506f4a2713aSLionel Sambuc       UTF32 *ResultWidePtr = reinterpret_cast<UTF32*>(ResultBuf.data());
1507f4a2713aSLionel Sambuc       ResultWidePtr[0] = GetNumStringChars() - 1;
1508f4a2713aSLionel Sambuc     } else if (CharByteWidth == 2) {
1509f4a2713aSLionel Sambuc       // FIXME: Make the type of the result buffer correct instead of
1510f4a2713aSLionel Sambuc       // using reinterpret_cast.
1511f4a2713aSLionel Sambuc       UTF16 *ResultWidePtr = reinterpret_cast<UTF16*>(ResultBuf.data());
1512f4a2713aSLionel Sambuc       ResultWidePtr[0] = GetNumStringChars() - 1;
1513f4a2713aSLionel Sambuc     } else {
1514f4a2713aSLionel Sambuc       assert(CharByteWidth == 1 && "Unexpected char width");
1515f4a2713aSLionel Sambuc       ResultBuf[0] = GetNumStringChars() - 1;
1516f4a2713aSLionel Sambuc     }
1517f4a2713aSLionel Sambuc 
1518f4a2713aSLionel Sambuc     // Verify that pascal strings aren't too large.
1519f4a2713aSLionel Sambuc     if (GetStringLength() > 256) {
1520f4a2713aSLionel Sambuc       if (Diags)
1521*0a6a1f1dSLionel Sambuc         Diags->Report(StringToks.front().getLocation(),
1522f4a2713aSLionel Sambuc                       diag::err_pascal_string_too_long)
1523*0a6a1f1dSLionel Sambuc           << SourceRange(StringToks.front().getLocation(),
1524*0a6a1f1dSLionel Sambuc                          StringToks.back().getLocation());
1525f4a2713aSLionel Sambuc       hadError = true;
1526f4a2713aSLionel Sambuc       return;
1527f4a2713aSLionel Sambuc     }
1528f4a2713aSLionel Sambuc   } else if (Diags) {
1529f4a2713aSLionel Sambuc     // Complain if this string literal has too many characters.
1530f4a2713aSLionel Sambuc     unsigned MaxChars = Features.CPlusPlus? 65536 : Features.C99 ? 4095 : 509;
1531f4a2713aSLionel Sambuc 
1532f4a2713aSLionel Sambuc     if (GetNumStringChars() > MaxChars)
1533*0a6a1f1dSLionel Sambuc       Diags->Report(StringToks.front().getLocation(),
1534f4a2713aSLionel Sambuc                     diag::ext_string_too_long)
1535f4a2713aSLionel Sambuc         << GetNumStringChars() << MaxChars
1536f4a2713aSLionel Sambuc         << (Features.CPlusPlus ? 2 : Features.C99 ? 1 : 0)
1537*0a6a1f1dSLionel Sambuc         << SourceRange(StringToks.front().getLocation(),
1538*0a6a1f1dSLionel Sambuc                        StringToks.back().getLocation());
1539f4a2713aSLionel Sambuc   }
1540f4a2713aSLionel Sambuc }
1541f4a2713aSLionel Sambuc 
resyncUTF8(const char * Err,const char * End)1542f4a2713aSLionel Sambuc static const char *resyncUTF8(const char *Err, const char *End) {
1543f4a2713aSLionel Sambuc   if (Err == End)
1544f4a2713aSLionel Sambuc     return End;
1545f4a2713aSLionel Sambuc   End = Err + std::min<unsigned>(getNumBytesForUTF8(*Err), End-Err);
1546f4a2713aSLionel Sambuc   while (++Err != End && (*Err & 0xC0) == 0x80)
1547f4a2713aSLionel Sambuc     ;
1548f4a2713aSLionel Sambuc   return Err;
1549f4a2713aSLionel Sambuc }
1550f4a2713aSLionel Sambuc 
1551f4a2713aSLionel Sambuc /// \brief This function copies from Fragment, which is a sequence of bytes
1552f4a2713aSLionel Sambuc /// within Tok's contents (which begin at TokBegin) into ResultPtr.
1553f4a2713aSLionel Sambuc /// Performs widening for multi-byte characters.
CopyStringFragment(const Token & Tok,const char * TokBegin,StringRef Fragment)1554f4a2713aSLionel Sambuc bool StringLiteralParser::CopyStringFragment(const Token &Tok,
1555f4a2713aSLionel Sambuc                                              const char *TokBegin,
1556f4a2713aSLionel Sambuc                                              StringRef Fragment) {
1557f4a2713aSLionel Sambuc   const UTF8 *ErrorPtrTmp;
1558f4a2713aSLionel Sambuc   if (ConvertUTF8toWide(CharByteWidth, Fragment, ResultPtr, ErrorPtrTmp))
1559f4a2713aSLionel Sambuc     return false;
1560f4a2713aSLionel Sambuc 
1561f4a2713aSLionel Sambuc   // If we see bad encoding for unprefixed string literals, warn and
1562f4a2713aSLionel Sambuc   // simply copy the byte values, for compatibility with gcc and older
1563f4a2713aSLionel Sambuc   // versions of clang.
1564f4a2713aSLionel Sambuc   bool NoErrorOnBadEncoding = isAscii();
1565f4a2713aSLionel Sambuc   if (NoErrorOnBadEncoding) {
1566f4a2713aSLionel Sambuc     memcpy(ResultPtr, Fragment.data(), Fragment.size());
1567f4a2713aSLionel Sambuc     ResultPtr += Fragment.size();
1568f4a2713aSLionel Sambuc   }
1569f4a2713aSLionel Sambuc 
1570f4a2713aSLionel Sambuc   if (Diags) {
1571f4a2713aSLionel Sambuc     const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp);
1572f4a2713aSLionel Sambuc 
1573f4a2713aSLionel Sambuc     FullSourceLoc SourceLoc(Tok.getLocation(), SM);
1574f4a2713aSLionel Sambuc     const DiagnosticBuilder &Builder =
1575f4a2713aSLionel Sambuc       Diag(Diags, Features, SourceLoc, TokBegin,
1576f4a2713aSLionel Sambuc            ErrorPtr, resyncUTF8(ErrorPtr, Fragment.end()),
1577f4a2713aSLionel Sambuc            NoErrorOnBadEncoding ? diag::warn_bad_string_encoding
1578f4a2713aSLionel Sambuc                                 : diag::err_bad_string_encoding);
1579f4a2713aSLionel Sambuc 
1580f4a2713aSLionel Sambuc     const char *NextStart = resyncUTF8(ErrorPtr, Fragment.end());
1581f4a2713aSLionel Sambuc     StringRef NextFragment(NextStart, Fragment.end()-NextStart);
1582f4a2713aSLionel Sambuc 
1583f4a2713aSLionel Sambuc     // Decode into a dummy buffer.
1584f4a2713aSLionel Sambuc     SmallString<512> Dummy;
1585f4a2713aSLionel Sambuc     Dummy.reserve(Fragment.size() * CharByteWidth);
1586f4a2713aSLionel Sambuc     char *Ptr = Dummy.data();
1587f4a2713aSLionel Sambuc 
1588*0a6a1f1dSLionel Sambuc     while (!ConvertUTF8toWide(CharByteWidth, NextFragment, Ptr, ErrorPtrTmp)) {
1589f4a2713aSLionel Sambuc       const char *ErrorPtr = reinterpret_cast<const char *>(ErrorPtrTmp);
1590f4a2713aSLionel Sambuc       NextStart = resyncUTF8(ErrorPtr, Fragment.end());
1591f4a2713aSLionel Sambuc       Builder << MakeCharSourceRange(Features, SourceLoc, TokBegin,
1592f4a2713aSLionel Sambuc                                      ErrorPtr, NextStart);
1593f4a2713aSLionel Sambuc       NextFragment = StringRef(NextStart, Fragment.end()-NextStart);
1594f4a2713aSLionel Sambuc     }
1595f4a2713aSLionel Sambuc   }
1596f4a2713aSLionel Sambuc   return !NoErrorOnBadEncoding;
1597f4a2713aSLionel Sambuc }
1598f4a2713aSLionel Sambuc 
DiagnoseLexingError(SourceLocation Loc)1599f4a2713aSLionel Sambuc void StringLiteralParser::DiagnoseLexingError(SourceLocation Loc) {
1600f4a2713aSLionel Sambuc   hadError = true;
1601f4a2713aSLionel Sambuc   if (Diags)
1602f4a2713aSLionel Sambuc     Diags->Report(Loc, diag::err_lexing_string);
1603f4a2713aSLionel Sambuc }
1604f4a2713aSLionel Sambuc 
1605f4a2713aSLionel Sambuc /// getOffsetOfStringByte - This function returns the offset of the
1606f4a2713aSLionel Sambuc /// specified byte of the string data represented by Token.  This handles
1607f4a2713aSLionel Sambuc /// advancing over escape sequences in the string.
getOffsetOfStringByte(const Token & Tok,unsigned ByteNo) const1608f4a2713aSLionel Sambuc unsigned StringLiteralParser::getOffsetOfStringByte(const Token &Tok,
1609f4a2713aSLionel Sambuc                                                     unsigned ByteNo) const {
1610f4a2713aSLionel Sambuc   // Get the spelling of the token.
1611f4a2713aSLionel Sambuc   SmallString<32> SpellingBuffer;
1612f4a2713aSLionel Sambuc   SpellingBuffer.resize(Tok.getLength());
1613f4a2713aSLionel Sambuc 
1614f4a2713aSLionel Sambuc   bool StringInvalid = false;
1615f4a2713aSLionel Sambuc   const char *SpellingPtr = &SpellingBuffer[0];
1616f4a2713aSLionel Sambuc   unsigned TokLen = Lexer::getSpelling(Tok, SpellingPtr, SM, Features,
1617f4a2713aSLionel Sambuc                                        &StringInvalid);
1618f4a2713aSLionel Sambuc   if (StringInvalid)
1619f4a2713aSLionel Sambuc     return 0;
1620f4a2713aSLionel Sambuc 
1621f4a2713aSLionel Sambuc   const char *SpellingStart = SpellingPtr;
1622f4a2713aSLionel Sambuc   const char *SpellingEnd = SpellingPtr+TokLen;
1623f4a2713aSLionel Sambuc 
1624f4a2713aSLionel Sambuc   // Handle UTF-8 strings just like narrow strings.
1625f4a2713aSLionel Sambuc   if (SpellingPtr[0] == 'u' && SpellingPtr[1] == '8')
1626f4a2713aSLionel Sambuc     SpellingPtr += 2;
1627f4a2713aSLionel Sambuc 
1628f4a2713aSLionel Sambuc   assert(SpellingPtr[0] != 'L' && SpellingPtr[0] != 'u' &&
1629f4a2713aSLionel Sambuc          SpellingPtr[0] != 'U' && "Doesn't handle wide or utf strings yet");
1630f4a2713aSLionel Sambuc 
1631f4a2713aSLionel Sambuc   // For raw string literals, this is easy.
1632f4a2713aSLionel Sambuc   if (SpellingPtr[0] == 'R') {
1633f4a2713aSLionel Sambuc     assert(SpellingPtr[1] == '"' && "Should be a raw string literal!");
1634f4a2713aSLionel Sambuc     // Skip 'R"'.
1635f4a2713aSLionel Sambuc     SpellingPtr += 2;
1636f4a2713aSLionel Sambuc     while (*SpellingPtr != '(') {
1637f4a2713aSLionel Sambuc       ++SpellingPtr;
1638f4a2713aSLionel Sambuc       assert(SpellingPtr < SpellingEnd && "Missing ( for raw string literal");
1639f4a2713aSLionel Sambuc     }
1640f4a2713aSLionel Sambuc     // Skip '('.
1641f4a2713aSLionel Sambuc     ++SpellingPtr;
1642f4a2713aSLionel Sambuc     return SpellingPtr - SpellingStart + ByteNo;
1643f4a2713aSLionel Sambuc   }
1644f4a2713aSLionel Sambuc 
1645f4a2713aSLionel Sambuc   // Skip over the leading quote
1646f4a2713aSLionel Sambuc   assert(SpellingPtr[0] == '"' && "Should be a string literal!");
1647f4a2713aSLionel Sambuc   ++SpellingPtr;
1648f4a2713aSLionel Sambuc 
1649f4a2713aSLionel Sambuc   // Skip over bytes until we find the offset we're looking for.
1650f4a2713aSLionel Sambuc   while (ByteNo) {
1651f4a2713aSLionel Sambuc     assert(SpellingPtr < SpellingEnd && "Didn't find byte offset!");
1652f4a2713aSLionel Sambuc 
1653f4a2713aSLionel Sambuc     // Step over non-escapes simply.
1654f4a2713aSLionel Sambuc     if (*SpellingPtr != '\\') {
1655f4a2713aSLionel Sambuc       ++SpellingPtr;
1656f4a2713aSLionel Sambuc       --ByteNo;
1657f4a2713aSLionel Sambuc       continue;
1658f4a2713aSLionel Sambuc     }
1659f4a2713aSLionel Sambuc 
1660f4a2713aSLionel Sambuc     // Otherwise, this is an escape character.  Advance over it.
1661f4a2713aSLionel Sambuc     bool HadError = false;
1662f4a2713aSLionel Sambuc     if (SpellingPtr[1] == 'u' || SpellingPtr[1] == 'U') {
1663f4a2713aSLionel Sambuc       const char *EscapePtr = SpellingPtr;
1664f4a2713aSLionel Sambuc       unsigned Len = MeasureUCNEscape(SpellingStart, SpellingPtr, SpellingEnd,
1665f4a2713aSLionel Sambuc                                       1, Features, HadError);
1666f4a2713aSLionel Sambuc       if (Len > ByteNo) {
1667f4a2713aSLionel Sambuc         // ByteNo is somewhere within the escape sequence.
1668f4a2713aSLionel Sambuc         SpellingPtr = EscapePtr;
1669f4a2713aSLionel Sambuc         break;
1670f4a2713aSLionel Sambuc       }
1671f4a2713aSLionel Sambuc       ByteNo -= Len;
1672f4a2713aSLionel Sambuc     } else {
1673f4a2713aSLionel Sambuc       ProcessCharEscape(SpellingStart, SpellingPtr, SpellingEnd, HadError,
1674f4a2713aSLionel Sambuc                         FullSourceLoc(Tok.getLocation(), SM),
1675f4a2713aSLionel Sambuc                         CharByteWidth*8, Diags, Features);
1676f4a2713aSLionel Sambuc       --ByteNo;
1677f4a2713aSLionel Sambuc     }
1678f4a2713aSLionel Sambuc     assert(!HadError && "This method isn't valid on erroneous strings");
1679f4a2713aSLionel Sambuc   }
1680f4a2713aSLionel Sambuc 
1681f4a2713aSLionel Sambuc   return SpellingPtr-SpellingStart;
1682f4a2713aSLionel Sambuc }
1683