LiteralSupport.cpp revision 6ea623823f8532670480425b573f35115404b4a0
15f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===--- LiteralSupport.cpp - Code to parse and process literals ----------===// 25f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 35f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// The LLVM Compiler Infrastructure 45f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 55f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file is distributed under the University of Illinois Open Source 65f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// License. See LICENSE.TXT for details. 75f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 85f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 95f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// This file implements the NumericLiteralParser, CharLiteralParser, and 115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// StringLiteralParser interfaces. 125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer// 135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer//===----------------------------------------------------------------------===// 145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 155f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/LiteralSupport.h" 165f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Lex/Preprocessor.h" 175f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/Diagnostic.h" 185f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer#include "clang/Basic/SourceManager.h" 19464175bba1318bef7905122e9fda20cff926df78Chris Lattner#include "clang/Basic/TargetInfo.h" 20464175bba1318bef7905122e9fda20cff926df78Chris Lattner#include "llvm/ADT/StringExtras.h" 21464175bba1318bef7905122e9fda20cff926df78Chris Lattnerusing namespace clang; 225f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 235f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// HexDigitValue - Return the value of the specified hex digit, or -1 if it's 245f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// not valid. 255f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic int HexDigitValue(char C) { 265f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C >= '0' && C <= '9') return C-'0'; 275f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C >= 'a' && C <= 'f') return C-'a'+10; 285f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (C >= 'A' && C <= 'F') return C-'A'+10; 295f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer return -1; 305f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer} 315f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 325f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// ProcessCharEscape - Parse a standard C escape sequence, which can occur in 335f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// either a character or a string literal. 345f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencerstatic unsigned ProcessCharEscape(const char *&ThisTokBuf, 355f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer const char *ThisTokEnd, bool &HadError, 365f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer SourceLocation Loc, bool IsWide, 375f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Preprocessor &PP) { 38464175bba1318bef7905122e9fda20cff926df78Chris Lattner // Skip the '\' char. 395f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ++ThisTokBuf; 405f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 415f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // We know that this character can't be off the end of the buffer, because 425f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // that would have been \", which would not have been the end of string. 435f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned ResultChar = *ThisTokBuf++; 445f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer switch (ResultChar) { 455f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // These map to themselves. 465f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '\\': case '\'': case '"': case '?': break; 475f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // These have fixed mappings. 495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'a': 505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // TODO: K&R: the meaning of '\\a' is different in traditional C 515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ResultChar = 7; 525f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 535f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'b': 545f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ResultChar = 8; 555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'e': 575f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer PP.Diag(Loc, diag::ext_nonstandard_escape, "e"); 585f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ResultChar = 27; 595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 60d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner case 'f': 61464175bba1318bef7905122e9fda20cff926df78Chris Lattner ResultChar = 12; 62464175bba1318bef7905122e9fda20cff926df78Chris Lattner break; 63464175bba1318bef7905122e9fda20cff926df78Chris Lattner case 'n': 64d2d2a11a91d7ddf468bfb70f66362d24806ed601Chris Lattner ResultChar = 10; 655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'r': 675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ResultChar = 13; 685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 't': 705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ResultChar = 9; 715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'v': 735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ResultChar = 11; 745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 765f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer //case 'u': case 'U': // FIXME: UCNs. 775f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case 'x': { // Hex escape. 785f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ResultChar = 0; 795f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) { 805f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer PP.Diag(Loc, diag::err_hex_escape_no_digits); 815f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer HadError = 1; 8273322924127c873c13101b705dd823f5539ffa5fSteve Naroff break; 8373322924127c873c13101b705dd823f5539ffa5fSteve Naroff } 8473322924127c873c13101b705dd823f5539ffa5fSteve Naroff 8573322924127c873c13101b705dd823f5539ffa5fSteve Naroff // Hex escapes are a maximal series of hex digits. 8673322924127c873c13101b705dd823f5539ffa5fSteve Naroff bool Overflow = false; 8773322924127c873c13101b705dd823f5539ffa5fSteve Naroff for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) { 8873322924127c873c13101b705dd823f5539ffa5fSteve Naroff int CharVal = HexDigitValue(ThisTokBuf[0]); 895f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CharVal == -1) break; 905f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Overflow |= (ResultChar & 0xF0000000) ? true : false; // About to shift out a digit? 915f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ResultChar <<= 4; 925f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ResultChar |= CharVal; 935f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 945f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 955f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // See if any bits will be truncated when evaluated as a character. 965f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer unsigned CharWidth = PP.getTargetInfo().getCharWidth(IsWide); 975f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 985f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { 995f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer Overflow = true; 1005f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ResultChar &= ~0U >> (32-CharWidth); 1015f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1025f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 103d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff // Check for overflow. 104d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff if (Overflow) // Too many digits to fit in 105d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff PP.Diag(Loc, diag::warn_hex_escape_too_large); 106d1861fd633d5096a00777c918eb8575ea7162fe7Steve Naroff break; 1075f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1085f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '0': case '1': case '2': case '3': 1095f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer case '4': case '5': case '6': case '7': { 1105f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Octal escapes. 1115f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer --ThisTokBuf; 1125f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer ResultChar = 0; 1135f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1145f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer // Octal escapes are a series of octal digits with maximum length 3. 1158b9023ba35a86838789e2c9034a6128728c547aaChris Lattner // "\0123" is a two digit sequence equal to "\012" "3". 1168b9023ba35a86838789e2c9034a6128728c547aaChris Lattner unsigned NumDigits = 0; 1178b9023ba35a86838789e2c9034a6128728c547aaChris Lattner do { 1188b9023ba35a86838789e2c9034a6128728c547aaChris Lattner ResultChar <<= 3; 119464175bba1318bef7905122e9fda20cff926df78Chris Lattner ResultChar |= *ThisTokBuf++ - '0'; 120464175bba1318bef7905122e9fda20cff926df78Chris Lattner ++NumDigits; 121464175bba1318bef7905122e9fda20cff926df78Chris Lattner } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 && 122464175bba1318bef7905122e9fda20cff926df78Chris Lattner ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7'); 123464175bba1318bef7905122e9fda20cff926df78Chris Lattner 124464175bba1318bef7905122e9fda20cff926df78Chris Lattner // Check for overflow. Reject '\777', but not L'\777'. 125464175bba1318bef7905122e9fda20cff926df78Chris Lattner unsigned CharWidth = PP.getTargetInfo().getCharWidth(IsWide); 126464175bba1318bef7905122e9fda20cff926df78Chris Lattner 127464175bba1318bef7905122e9fda20cff926df78Chris Lattner if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { 128464175bba1318bef7905122e9fda20cff926df78Chris Lattner PP.Diag(Loc, diag::warn_octal_escape_too_large); 129464175bba1318bef7905122e9fda20cff926df78Chris Lattner ResultChar &= ~0U >> (32-CharWidth); 130464175bba1318bef7905122e9fda20cff926df78Chris Lattner } 131464175bba1318bef7905122e9fda20cff926df78Chris Lattner break; 132464175bba1318bef7905122e9fda20cff926df78Chris Lattner } 133464175bba1318bef7905122e9fda20cff926df78Chris Lattner 134464175bba1318bef7905122e9fda20cff926df78Chris Lattner // Otherwise, these are not valid escapes. 135464175bba1318bef7905122e9fda20cff926df78Chris Lattner case '(': case '{': case '[': case '%': 136464175bba1318bef7905122e9fda20cff926df78Chris Lattner // GCC accepts these as extensions. We warn about them as such though. 137464175bba1318bef7905122e9fda20cff926df78Chris Lattner if (!PP.getLangOptions().NoExtensions) { 138464175bba1318bef7905122e9fda20cff926df78Chris Lattner PP.Diag(Loc, diag::ext_nonstandard_escape, 139464175bba1318bef7905122e9fda20cff926df78Chris Lattner std::string()+(char)ResultChar); 140464175bba1318bef7905122e9fda20cff926df78Chris Lattner break; 141464175bba1318bef7905122e9fda20cff926df78Chris Lattner } 142464175bba1318bef7905122e9fda20cff926df78Chris Lattner // FALL THROUGH. 143464175bba1318bef7905122e9fda20cff926df78Chris Lattner default: 144464175bba1318bef7905122e9fda20cff926df78Chris Lattner if (isgraph(ThisTokBuf[0])) { 145464175bba1318bef7905122e9fda20cff926df78Chris Lattner PP.Diag(Loc, diag::ext_unknown_escape, std::string()+(char)ResultChar); 146464175bba1318bef7905122e9fda20cff926df78Chris Lattner } else { 147464175bba1318bef7905122e9fda20cff926df78Chris Lattner PP.Diag(Loc, diag::ext_unknown_escape, "x"+llvm::utohexstr(ResultChar)); 1485f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1495f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer break; 1505f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer } 1515f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 152464175bba1318bef7905122e9fda20cff926df78Chris Lattner return ResultChar; 153464175bba1318bef7905122e9fda20cff926df78Chris Lattner} 154464175bba1318bef7905122e9fda20cff926df78Chris Lattner 1555f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 1565f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer 157464175bba1318bef7905122e9fda20cff926df78Chris Lattner 158464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// integer-constant: [C99 6.4.4.1] 1595f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// decimal-constant integer-suffix 1605f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// octal-constant integer-suffix 161464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// hexadecimal-constant integer-suffix 162464175bba1318bef7905122e9fda20cff926df78Chris Lattner/// decimal-constant: 1635f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// nonzero-digit 1645f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// decimal-constant digit 1655f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// octal-constant: 1665f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 0 1675f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// octal-constant octal-digit 1685f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// hexadecimal-constant: 1695f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// hexadecimal-prefix hexadecimal-digit 1705f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// hexadecimal-constant hexadecimal-digit 1715f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// hexadecimal-prefix: one of 1725f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// 0x 0X 1735f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// integer-suffix: 1745f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// unsigned-suffix [long-suffix] 1755f016e2cb5d11daeb237544de1c5d59f20fe1a6eReid Spencer/// unsigned-suffix [long-long-suffix] 176/// long-suffix [unsigned-suffix] 177/// long-long-suffix [unsigned-sufix] 178/// nonzero-digit: 179/// 1 2 3 4 5 6 7 8 9 180/// octal-digit: 181/// 0 1 2 3 4 5 6 7 182/// hexadecimal-digit: 183/// 0 1 2 3 4 5 6 7 8 9 184/// a b c d e f 185/// A B C D E F 186/// unsigned-suffix: one of 187/// u U 188/// long-suffix: one of 189/// l L 190/// long-long-suffix: one of 191/// ll LL 192/// 193/// floating-constant: [C99 6.4.4.2] 194/// TODO: add rules... 195/// 196NumericLiteralParser:: 197NumericLiteralParser(const char *begin, const char *end, 198 SourceLocation TokLoc, Preprocessor &pp) 199 : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) { 200 s = DigitsBegin = begin; 201 saw_exponent = false; 202 saw_period = false; 203 isLong = false; 204 isUnsigned = false; 205 isLongLong = false; 206 isFloat = false; 207 isImaginary = false; 208 hadError = false; 209 210 if (*s == '0') { // parse radix 211 ParseNumberStartingWithZero(TokLoc); 212 if (hadError) 213 return; 214 } else { // the first digit is non-zero 215 radix = 10; 216 s = SkipDigits(s); 217 if (s == ThisTokEnd) { 218 // Done. 219 } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) { 220 Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin), 221 diag::err_invalid_decimal_digit, std::string(s, s+1)); 222 return; 223 } else if (*s == '.') { 224 s++; 225 saw_period = true; 226 s = SkipDigits(s); 227 } 228 if (*s == 'e' || *s == 'E') { // exponent 229 const char *Exponent = s; 230 s++; 231 saw_exponent = true; 232 if (*s == '+' || *s == '-') s++; // sign 233 const char *first_non_digit = SkipDigits(s); 234 if (first_non_digit != s) { 235 s = first_non_digit; 236 } else { 237 Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin), 238 diag::err_exponent_has_no_digits); 239 return; 240 } 241 } 242 } 243 244 SuffixBegin = s; 245 246 // Parse the suffix. At this point we can classify whether we have an FP or 247 // integer constant. 248 bool isFPConstant = isFloatingLiteral(); 249 250 // Loop over all of the characters of the suffix. If we see something bad, 251 // we break out of the loop. 252 for (; s != ThisTokEnd; ++s) { 253 switch (*s) { 254 case 'f': // FP Suffix for "float" 255 case 'F': 256 if (!isFPConstant) break; // Error for integer constant. 257 if (isFloat || isLong) break; // FF, LF invalid. 258 isFloat = true; 259 continue; // Success. 260 case 'u': 261 case 'U': 262 if (isFPConstant) break; // Error for floating constant. 263 if (isUnsigned) break; // Cannot be repeated. 264 isUnsigned = true; 265 continue; // Success. 266 case 'l': 267 case 'L': 268 if (isLong || isLongLong) break; // Cannot be repeated. 269 if (isFloat) break; // LF invalid. 270 271 // Check for long long. The L's need to be adjacent and the same case. 272 if (s+1 != ThisTokEnd && s[1] == s[0]) { 273 if (isFPConstant) break; // long long invalid for floats. 274 isLongLong = true; 275 ++s; // Eat both of them. 276 } else { 277 isLong = true; 278 } 279 continue; // Success. 280 case 'i': 281 if (PP.getLangOptions().Microsoft) { 282 // Allow i8, i16, i32, i64, and i128. 283 if (++s == ThisTokEnd) break; 284 switch (*s) { 285 case '8': 286 s++; // i8 suffix 287 break; 288 case '1': 289 if (++s == ThisTokEnd) break; 290 if (*s == '6') s++; // i16 suffix 291 else if (*s == '2') { 292 if (++s == ThisTokEnd) break; 293 if (*s == '8') s++; // i128 suffix 294 } 295 break; 296 case '3': 297 if (++s == ThisTokEnd) break; 298 if (*s == '2') s++; // i32 suffix 299 break; 300 case '6': 301 if (++s == ThisTokEnd) break; 302 if (*s == '4') s++; // i64 suffix 303 break; 304 default: 305 break; 306 } 307 break; 308 } 309 // fall through. 310 case 'I': 311 case 'j': 312 case 'J': 313 if (isImaginary) break; // Cannot be repeated. 314 PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin), 315 diag::ext_imaginary_constant); 316 isImaginary = true; 317 continue; // Success. 318 } 319 // If we reached here, there was an error. 320 break; 321 } 322 323 // Report an error if there are any. 324 if (s != ThisTokEnd) { 325 Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin), 326 isFPConstant ? diag::err_invalid_suffix_float_constant : 327 diag::err_invalid_suffix_integer_constant, 328 std::string(SuffixBegin, ThisTokEnd)); 329 return; 330 } 331} 332 333/// ParseNumberStartingWithZero - This method is called when the first character 334/// of the number is found to be a zero. This means it is either an octal 335/// number (like '04') or a hex number ('0x123a') a binary number ('0b1010') or 336/// a floating point number (01239.123e4). Eat the prefix, determining the 337/// radix etc. 338void NumericLiteralParser::ParseNumberStartingWithZero(SourceLocation TokLoc) { 339 assert(s[0] == '0' && "Invalid method call"); 340 s++; 341 342 // Handle a hex number like 0x1234. 343 if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) { 344 s++; 345 radix = 16; 346 DigitsBegin = s; 347 s = SkipHexDigits(s); 348 if (s == ThisTokEnd) { 349 // Done. 350 } else if (*s == '.') { 351 s++; 352 saw_period = true; 353 s = SkipHexDigits(s); 354 } 355 // A binary exponent can appear with or with a '.'. If dotted, the 356 // binary exponent is required. 357 if (*s == 'p' || *s == 'P') { 358 const char *Exponent = s; 359 s++; 360 saw_exponent = true; 361 if (*s == '+' || *s == '-') s++; // sign 362 const char *first_non_digit = SkipDigits(s); 363 if (first_non_digit == s) { 364 Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin), 365 diag::err_exponent_has_no_digits); 366 return; 367 } 368 s = first_non_digit; 369 370 if (!PP.getLangOptions().HexFloats) 371 Diag(TokLoc, diag::ext_hexconstant_invalid); 372 } else if (saw_period) { 373 Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin), 374 diag::err_hexconstant_requires_exponent); 375 } 376 return; 377 } 378 379 // Handle simple binary numbers 0b01010 380 if (*s == 'b' || *s == 'B') { 381 // 0b101010 is a GCC extension. 382 PP.Diag(TokLoc, diag::ext_binary_literal); 383 ++s; 384 radix = 2; 385 DigitsBegin = s; 386 s = SkipBinaryDigits(s); 387 if (s == ThisTokEnd) { 388 // Done. 389 } else if (isxdigit(*s)) { 390 Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin), 391 diag::err_invalid_binary_digit, std::string(s, s+1)); 392 } 393 // Other suffixes will be diagnosed by the caller. 394 return; 395 } 396 397 // For now, the radix is set to 8. If we discover that we have a 398 // floating point constant, the radix will change to 10. Octal floating 399 // point constants are not permitted (only decimal and hexadecimal). 400 radix = 8; 401 DigitsBegin = s; 402 s = SkipOctalDigits(s); 403 if (s == ThisTokEnd) 404 return; // Done, simple octal number like 01234 405 406 // If we have some other non-octal digit that *is* a decimal digit, see if 407 // this is part of a floating point number like 094.123 or 09e1. 408 if (isdigit(*s)) { 409 const char *EndDecimal = SkipDigits(s); 410 if (EndDecimal[0] == '.' || EndDecimal[0] == 'e' || EndDecimal[0] == 'E') { 411 s = EndDecimal; 412 radix = 10; 413 } 414 } 415 416 // If we have a hex digit other than 'e' (which denotes a FP exponent) then 417 // the code is using an incorrect base. 418 if (isxdigit(*s) && *s != 'e' && *s != 'E') { 419 Diag(PP.AdvanceToTokenCharacter(TokLoc, s-ThisTokBegin), 420 diag::err_invalid_octal_digit, std::string(s, s+1)); 421 return; 422 } 423 424 if (*s == '.') { 425 s++; 426 radix = 10; 427 saw_period = true; 428 s = SkipDigits(s); // Skip suffix. 429 } 430 if (*s == 'e' || *s == 'E') { // exponent 431 const char *Exponent = s; 432 s++; 433 radix = 10; 434 saw_exponent = true; 435 if (*s == '+' || *s == '-') s++; // sign 436 const char *first_non_digit = SkipDigits(s); 437 if (first_non_digit != s) { 438 s = first_non_digit; 439 } else { 440 Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-ThisTokBegin), 441 diag::err_exponent_has_no_digits); 442 return; 443 } 444 } 445} 446 447 448/// GetIntegerValue - Convert this numeric literal value to an APInt that 449/// matches Val's input width. If there is an overflow, set Val to the low bits 450/// of the result and return true. Otherwise, return false. 451bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) { 452 Val = 0; 453 s = DigitsBegin; 454 455 llvm::APInt RadixVal(Val.getBitWidth(), radix); 456 llvm::APInt CharVal(Val.getBitWidth(), 0); 457 llvm::APInt OldVal = Val; 458 459 bool OverflowOccurred = false; 460 while (s < SuffixBegin) { 461 unsigned C = HexDigitValue(*s++); 462 463 // If this letter is out of bound for this radix, reject it. 464 assert(C < radix && "NumericLiteralParser ctor should have rejected this"); 465 466 CharVal = C; 467 468 // Add the digit to the value in the appropriate radix. If adding in digits 469 // made the value smaller, then this overflowed. 470 OldVal = Val; 471 472 // Multiply by radix, did overflow occur on the multiply? 473 Val *= RadixVal; 474 OverflowOccurred |= Val.udiv(RadixVal) != OldVal; 475 476 OldVal = Val; 477 // Add value, did overflow occur on the value? 478 Val += CharVal; 479 OverflowOccurred |= Val.ult(OldVal); 480 OverflowOccurred |= Val.ult(CharVal); 481 } 482 return OverflowOccurred; 483} 484 485llvm::APFloat NumericLiteralParser:: 486GetFloatValue(const llvm::fltSemantics &Format, bool* isExact) { 487 using llvm::APFloat; 488 489 llvm::SmallVector<char,256> floatChars; 490 for (unsigned i = 0, n = ThisTokEnd-ThisTokBegin; i != n; ++i) 491 floatChars.push_back(ThisTokBegin[i]); 492 493 floatChars.push_back('\0'); 494 495 APFloat V (Format, APFloat::fcZero, false); 496 APFloat::opStatus status; 497 498 status = V.convertFromString(&floatChars[0],APFloat::rmNearestTiesToEven); 499 500 if (isExact) 501 *isExact = status == APFloat::opOK; 502 503 return V; 504} 505 506void NumericLiteralParser::Diag(SourceLocation Loc, unsigned DiagID, 507 const std::string &M) { 508 PP.Diag(Loc, DiagID, M); 509 hadError = true; 510} 511 512 513CharLiteralParser::CharLiteralParser(const char *begin, const char *end, 514 SourceLocation Loc, Preprocessor &PP) { 515 // At this point we know that the character matches the regex "L?'.*'". 516 HadError = false; 517 Value = 0; 518 519 // Determine if this is a wide character. 520 IsWide = begin[0] == 'L'; 521 if (IsWide) ++begin; 522 523 // Skip over the entry quote. 524 assert(begin[0] == '\'' && "Invalid token lexed"); 525 ++begin; 526 527 // FIXME: This assumes that 'int' is 32-bits in overflow calculation, and the 528 // size of "value". 529 assert(PP.getTargetInfo().getIntWidth() == 32 && 530 "Assumes sizeof(int) == 4 for now"); 531 // FIXME: This assumes that wchar_t is 32-bits for now. 532 assert(PP.getTargetInfo().getWCharWidth() == 32 && 533 "Assumes sizeof(wchar_t) == 4 for now"); 534 // FIXME: This extensively assumes that 'char' is 8-bits. 535 assert(PP.getTargetInfo().getCharWidth() == 8 && 536 "Assumes char is 8 bits"); 537 538 bool isFirstChar = true; 539 bool isMultiChar = false; 540 while (begin[0] != '\'') { 541 unsigned ResultChar; 542 if (begin[0] != '\\') // If this is a normal character, consume it. 543 ResultChar = *begin++; 544 else // Otherwise, this is an escape character. 545 ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP); 546 547 // If this is a multi-character constant (e.g. 'abc'), handle it. These are 548 // implementation defined (C99 6.4.4.4p10). 549 if (!isFirstChar) { 550 // If this is the second character being processed, do special handling. 551 if (!isMultiChar) { 552 isMultiChar = true; 553 554 // Warn about discarding the top bits for multi-char wide-character 555 // constants (L'abcd'). 556 if (IsWide) 557 PP.Diag(Loc, diag::warn_extraneous_wide_char_constant); 558 } 559 560 if (IsWide) { 561 // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'. 562 Value = 0; 563 } else { 564 // Narrow character literals act as though their value is concatenated 565 // in this implementation. 566 if (((Value << 8) >> 8) != Value) 567 PP.Diag(Loc, diag::warn_char_constant_too_large); 568 Value <<= 8; 569 } 570 } 571 572 Value += ResultChar; 573 isFirstChar = false; 574 } 575 576 // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1") 577 // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple 578 // character constants are not sign extended in the this implementation: 579 // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC. 580 if (!IsWide && !isMultiChar && (Value & 128) && 581 PP.getTargetInfo().isCharSigned()) 582 Value = (signed char)Value; 583} 584 585 586/// string-literal: [C99 6.4.5] 587/// " [s-char-sequence] " 588/// L" [s-char-sequence] " 589/// s-char-sequence: 590/// s-char 591/// s-char-sequence s-char 592/// s-char: 593/// any source character except the double quote ", 594/// backslash \, or newline character 595/// escape-character 596/// universal-character-name 597/// escape-character: [C99 6.4.4.4] 598/// \ escape-code 599/// universal-character-name 600/// escape-code: 601/// character-escape-code 602/// octal-escape-code 603/// hex-escape-code 604/// character-escape-code: one of 605/// n t b r f v a 606/// \ ' " ? 607/// octal-escape-code: 608/// octal-digit 609/// octal-digit octal-digit 610/// octal-digit octal-digit octal-digit 611/// hex-escape-code: 612/// x hex-digit 613/// hex-escape-code hex-digit 614/// universal-character-name: 615/// \u hex-quad 616/// \U hex-quad hex-quad 617/// hex-quad: 618/// hex-digit hex-digit hex-digit hex-digit 619/// 620StringLiteralParser:: 621StringLiteralParser(const Token *StringToks, unsigned NumStringToks, 622 Preprocessor &pp, TargetInfo &t) 623 : PP(pp), Target(t) { 624 // Scan all of the string portions, remember the max individual token length, 625 // computing a bound on the concatenated string length, and see whether any 626 // piece is a wide-string. If any of the string portions is a wide-string 627 // literal, the result is a wide-string literal [C99 6.4.5p4]. 628 MaxTokenLength = StringToks[0].getLength(); 629 SizeBound = StringToks[0].getLength()-2; // -2 for "". 630 AnyWide = StringToks[0].is(tok::wide_string_literal); 631 632 hadError = false; 633 634 // Implement Translation Phase #6: concatenation of string literals 635 /// (C99 5.1.1.2p1). The common case is only one string fragment. 636 for (unsigned i = 1; i != NumStringToks; ++i) { 637 // The string could be shorter than this if it needs cleaning, but this is a 638 // reasonable bound, which is all we need. 639 SizeBound += StringToks[i].getLength()-2; // -2 for "". 640 641 // Remember maximum string piece length. 642 if (StringToks[i].getLength() > MaxTokenLength) 643 MaxTokenLength = StringToks[i].getLength(); 644 645 // Remember if we see any wide strings. 646 AnyWide |= StringToks[i].is(tok::wide_string_literal); 647 } 648 649 650 // Include space for the null terminator. 651 ++SizeBound; 652 653 // TODO: K&R warning: "traditional C rejects string constant concatenation" 654 655 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not 656 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true. 657 wchar_tByteWidth = ~0U; 658 if (AnyWide) { 659 wchar_tByteWidth = Target.getWCharWidth(); 660 assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!"); 661 wchar_tByteWidth /= 8; 662 } 663 664 // The output buffer size needs to be large enough to hold wide characters. 665 // This is a worst-case assumption which basically corresponds to L"" "long". 666 if (AnyWide) 667 SizeBound *= wchar_tByteWidth; 668 669 // Size the temporary buffer to hold the result string data. 670 ResultBuf.resize(SizeBound); 671 672 // Likewise, but for each string piece. 673 llvm::SmallString<512> TokenBuf; 674 TokenBuf.resize(MaxTokenLength); 675 676 // Loop over all the strings, getting their spelling, and expanding them to 677 // wide strings as appropriate. 678 ResultPtr = &ResultBuf[0]; // Next byte to fill in. 679 680 Pascal = false; 681 682 for (unsigned i = 0, e = NumStringToks; i != e; ++i) { 683 const char *ThisTokBuf = &TokenBuf[0]; 684 // Get the spelling of the token, which eliminates trigraphs, etc. We know 685 // that ThisTokBuf points to a buffer that is big enough for the whole token 686 // and 'spelled' tokens can only shrink. 687 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf); 688 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote. 689 690 // TODO: Input character set mapping support. 691 692 // Skip L marker for wide strings. 693 bool ThisIsWide = false; 694 if (ThisTokBuf[0] == 'L') { 695 ++ThisTokBuf; 696 ThisIsWide = true; 697 } 698 699 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?"); 700 ++ThisTokBuf; 701 702 // Check if this is a pascal string 703 if (pp.getLangOptions().PascalStrings && ThisTokBuf + 1 != ThisTokEnd && 704 ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') { 705 706 // If the \p sequence is found in the first token, we have a pascal string 707 // Otherwise, if we already have a pascal string, ignore the first \p 708 if (i == 0) { 709 ++ThisTokBuf; 710 Pascal = true; 711 } else if (Pascal) 712 ThisTokBuf += 2; 713 } 714 715 while (ThisTokBuf != ThisTokEnd) { 716 // Is this a span of non-escape characters? 717 if (ThisTokBuf[0] != '\\') { 718 const char *InStart = ThisTokBuf; 719 do { 720 ++ThisTokBuf; 721 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\'); 722 723 // Copy the character span over. 724 unsigned Len = ThisTokBuf-InStart; 725 if (!AnyWide) { 726 memcpy(ResultPtr, InStart, Len); 727 ResultPtr += Len; 728 } else { 729 // Note: our internal rep of wide char tokens is always little-endian. 730 for (; Len; --Len, ++InStart) { 731 *ResultPtr++ = InStart[0]; 732 // Add zeros at the end. 733 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) 734 *ResultPtr++ = 0; 735 } 736 } 737 continue; 738 } 739 740 // Otherwise, this is an escape character. Process it. 741 unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError, 742 StringToks[i].getLocation(), 743 ThisIsWide, PP); 744 745 // Note: our internal rep of wide char tokens is always little-endian. 746 *ResultPtr++ = ResultChar & 0xFF; 747 748 if (AnyWide) { 749 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) 750 *ResultPtr++ = ResultChar >> i*8; 751 } 752 } 753 } 754 755 // Add zero terminator. 756 *ResultPtr = 0; 757 if (AnyWide) { 758 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) 759 *ResultPtr++ = 0; 760 } 761 762 if (Pascal) 763 ResultBuf[0] = ResultPtr-&ResultBuf[0]-1; 764} 765