Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- LiteralSupport.cpp - Code to parse and process literals ----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the NumericLiteralParser, CharLiteralParser, and |
| 11 | // StringLiteralParser interfaces. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Lex/LiteralSupport.h" |
| 16 | #include "clang/Lex/Preprocessor.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 17 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 136f93a | 2007-07-16 06:55:01 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceManager.h" |
| 19 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
| 21 | using namespace clang; |
| 22 | |
| 23 | /// HexDigitValue - Return the value of the specified hex digit, or -1 if it's |
| 24 | /// not valid. |
| 25 | static int HexDigitValue(char C) { |
| 26 | if (C >= '0' && C <= '9') return C-'0'; |
| 27 | if (C >= 'a' && C <= 'f') return C-'a'+10; |
| 28 | if (C >= 'A' && C <= 'F') return C-'A'+10; |
| 29 | return -1; |
| 30 | } |
| 31 | |
| 32 | /// ProcessCharEscape - Parse a standard C escape sequence, which can occur in |
| 33 | /// either a character or a string literal. |
| 34 | static unsigned ProcessCharEscape(const char *&ThisTokBuf, |
| 35 | const char *ThisTokEnd, bool &HadError, |
| 36 | SourceLocation Loc, bool IsWide, |
| 37 | Preprocessor &PP) { |
| 38 | // Skip the '\' char. |
| 39 | ++ThisTokBuf; |
| 40 | |
| 41 | // We know that this character can't be off the end of the buffer, because |
| 42 | // that would have been \", which would not have been the end of string. |
| 43 | unsigned ResultChar = *ThisTokBuf++; |
| 44 | switch (ResultChar) { |
| 45 | // These map to themselves. |
| 46 | case '\\': case '\'': case '"': case '?': break; |
| 47 | |
| 48 | // These have fixed mappings. |
| 49 | case 'a': |
| 50 | // TODO: K&R: the meaning of '\\a' is different in traditional C |
| 51 | ResultChar = 7; |
| 52 | break; |
| 53 | case 'b': |
| 54 | ResultChar = 8; |
| 55 | break; |
| 56 | case 'e': |
| 57 | PP.Diag(Loc, diag::ext_nonstandard_escape, "e"); |
| 58 | ResultChar = 27; |
| 59 | break; |
| 60 | case 'f': |
| 61 | ResultChar = 12; |
| 62 | break; |
| 63 | case 'n': |
| 64 | ResultChar = 10; |
| 65 | break; |
| 66 | case 'r': |
| 67 | ResultChar = 13; |
| 68 | break; |
| 69 | case 't': |
| 70 | ResultChar = 9; |
| 71 | break; |
| 72 | case 'v': |
| 73 | ResultChar = 11; |
| 74 | break; |
| 75 | |
| 76 | //case 'u': case 'U': // FIXME: UCNs. |
| 77 | case 'x': { // Hex escape. |
| 78 | ResultChar = 0; |
| 79 | if (ThisTokBuf == ThisTokEnd || !isxdigit(*ThisTokBuf)) { |
| 80 | PP.Diag(Loc, diag::err_hex_escape_no_digits); |
| 81 | HadError = 1; |
| 82 | break; |
| 83 | } |
| 84 | |
| 85 | // Hex escapes are a maximal series of hex digits. |
| 86 | bool Overflow = false; |
| 87 | for (; ThisTokBuf != ThisTokEnd; ++ThisTokBuf) { |
| 88 | int CharVal = HexDigitValue(ThisTokBuf[0]); |
| 89 | if (CharVal == -1) break; |
Chris Lattner | b812814 | 2007-09-03 18:28:41 +0000 | [diff] [blame] | 90 | Overflow |= (ResultChar & 0xF0000000) ? true : false; // About to shift out a digit? |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | ResultChar <<= 4; |
| 92 | ResultChar |= CharVal; |
| 93 | } |
| 94 | |
| 95 | // See if any bits will be truncated when evaluated as a character. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 96 | unsigned CharWidth = PP.getTargetInfo().getCharWidth(IsWide); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 97 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { |
| 99 | Overflow = true; |
| 100 | ResultChar &= ~0U >> (32-CharWidth); |
| 101 | } |
| 102 | |
| 103 | // Check for overflow. |
| 104 | if (Overflow) // Too many digits to fit in |
| 105 | PP.Diag(Loc, diag::warn_hex_escape_too_large); |
| 106 | break; |
| 107 | } |
| 108 | case '0': case '1': case '2': case '3': |
| 109 | case '4': case '5': case '6': case '7': { |
| 110 | // Octal escapes. |
| 111 | --ThisTokBuf; |
| 112 | ResultChar = 0; |
| 113 | |
| 114 | // Octal escapes are a series of octal digits with maximum length 3. |
| 115 | // "\0123" is a two digit sequence equal to "\012" "3". |
| 116 | unsigned NumDigits = 0; |
| 117 | do { |
| 118 | ResultChar <<= 3; |
| 119 | ResultChar |= *ThisTokBuf++ - '0'; |
| 120 | ++NumDigits; |
| 121 | } while (ThisTokBuf != ThisTokEnd && NumDigits < 3 && |
| 122 | ThisTokBuf[0] >= '0' && ThisTokBuf[0] <= '7'); |
| 123 | |
| 124 | // Check for overflow. Reject '\777', but not L'\777'. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 125 | unsigned CharWidth = PP.getTargetInfo().getCharWidth(IsWide); |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 126 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 127 | if (CharWidth != 32 && (ResultChar >> CharWidth) != 0) { |
| 128 | PP.Diag(Loc, diag::warn_octal_escape_too_large); |
| 129 | ResultChar &= ~0U >> (32-CharWidth); |
| 130 | } |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | // Otherwise, these are not valid escapes. |
| 135 | case '(': case '{': case '[': case '%': |
| 136 | // GCC accepts these as extensions. We warn about them as such though. |
| 137 | if (!PP.getLangOptions().NoExtensions) { |
| 138 | PP.Diag(Loc, diag::ext_nonstandard_escape, |
| 139 | std::string()+(char)ResultChar); |
| 140 | break; |
| 141 | } |
| 142 | // FALL THROUGH. |
| 143 | default: |
| 144 | if (isgraph(ThisTokBuf[0])) { |
| 145 | PP.Diag(Loc, diag::ext_unknown_escape, std::string()+(char)ResultChar); |
| 146 | } else { |
| 147 | PP.Diag(Loc, diag::ext_unknown_escape, "x"+llvm::utohexstr(ResultChar)); |
| 148 | } |
| 149 | break; |
| 150 | } |
| 151 | |
| 152 | return ResultChar; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | /// integer-constant: [C99 6.4.4.1] |
| 159 | /// decimal-constant integer-suffix |
| 160 | /// octal-constant integer-suffix |
| 161 | /// hexadecimal-constant integer-suffix |
| 162 | /// decimal-constant: |
| 163 | /// nonzero-digit |
| 164 | /// decimal-constant digit |
| 165 | /// octal-constant: |
| 166 | /// 0 |
| 167 | /// octal-constant octal-digit |
| 168 | /// hexadecimal-constant: |
| 169 | /// hexadecimal-prefix hexadecimal-digit |
| 170 | /// hexadecimal-constant hexadecimal-digit |
| 171 | /// hexadecimal-prefix: one of |
| 172 | /// 0x 0X |
| 173 | /// integer-suffix: |
| 174 | /// unsigned-suffix [long-suffix] |
| 175 | /// 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 | /// |
| 196 | |
| 197 | NumericLiteralParser:: |
| 198 | NumericLiteralParser(const char *begin, const char *end, |
| 199 | SourceLocation TokLoc, Preprocessor &pp) |
| 200 | : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) { |
| 201 | s = DigitsBegin = begin; |
| 202 | saw_exponent = false; |
| 203 | saw_period = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 204 | isLong = false; |
| 205 | isUnsigned = false; |
| 206 | isLongLong = false; |
Chris Lattner | 6e400c2 | 2007-08-26 03:29:23 +0000 | [diff] [blame] | 207 | isFloat = false; |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 208 | isImaginary = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 209 | hadError = false; |
| 210 | |
| 211 | if (*s == '0') { // parse radix |
| 212 | s++; |
| 213 | if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) { |
| 214 | s++; |
| 215 | radix = 16; |
| 216 | DigitsBegin = s; |
| 217 | s = SkipHexDigits(s); |
| 218 | if (s == ThisTokEnd) { |
| 219 | // Done. |
| 220 | } else if (*s == '.') { |
| 221 | s++; |
| 222 | saw_period = true; |
| 223 | s = SkipHexDigits(s); |
| 224 | } |
| 225 | // A binary exponent can appear with or with a '.'. If dotted, the |
| 226 | // binary exponent is required. |
Chris Lattner | 70f66ab | 2008-04-20 18:47:55 +0000 | [diff] [blame] | 227 | if ((*s == 'p' || *s == 'P') && PP.getLangOptions().HexFloats) { |
| 228 | const char *Exponent = s; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 229 | s++; |
| 230 | saw_exponent = true; |
| 231 | if (*s == '+' || *s == '-') s++; // sign |
| 232 | const char *first_non_digit = SkipDigits(s); |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 233 | if (first_non_digit != s) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 234 | s = first_non_digit; |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 235 | } else { |
Chris Lattner | 70f66ab | 2008-04-20 18:47:55 +0000 | [diff] [blame] | 236 | Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin), |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 237 | diag::err_exponent_has_no_digits); |
| 238 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 239 | } |
| 240 | } else if (saw_period) { |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 241 | Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin), |
| 242 | diag::err_hexconstant_requires_exponent); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 243 | return; |
| 244 | } |
| 245 | } else if (*s == 'b' || *s == 'B') { |
| 246 | // 0b101010 is a GCC extension. |
| 247 | ++s; |
| 248 | radix = 2; |
| 249 | DigitsBegin = s; |
| 250 | s = SkipBinaryDigits(s); |
| 251 | if (s == ThisTokEnd) { |
| 252 | // Done. |
| 253 | } else if (isxdigit(*s)) { |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 254 | Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin), |
| 255 | diag::err_invalid_binary_digit, std::string(s, s+1)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 256 | return; |
| 257 | } |
| 258 | PP.Diag(TokLoc, diag::ext_binary_literal); |
| 259 | } else { |
| 260 | // For now, the radix is set to 8. If we discover that we have a |
| 261 | // floating point constant, the radix will change to 10. Octal floating |
| 262 | // point constants are not permitted (only decimal and hexadecimal). |
| 263 | radix = 8; |
| 264 | DigitsBegin = s; |
| 265 | s = SkipOctalDigits(s); |
| 266 | if (s == ThisTokEnd) { |
| 267 | // Done. |
Christopher Lamb | 016765e | 2007-11-29 06:06:27 +0000 | [diff] [blame] | 268 | } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) { |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 269 | Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin), |
| 270 | diag::err_invalid_octal_digit, std::string(s, s+1)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 271 | return; |
| 272 | } else if (*s == '.') { |
| 273 | s++; |
| 274 | radix = 10; |
| 275 | saw_period = true; |
| 276 | s = SkipDigits(s); |
| 277 | } |
| 278 | if (*s == 'e' || *s == 'E') { // exponent |
Chris Lattner | 70f66ab | 2008-04-20 18:47:55 +0000 | [diff] [blame] | 279 | const char *Exponent = s; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | s++; |
| 281 | radix = 10; |
| 282 | saw_exponent = true; |
| 283 | if (*s == '+' || *s == '-') s++; // sign |
| 284 | const char *first_non_digit = SkipDigits(s); |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 285 | if (first_non_digit != s) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 286 | s = first_non_digit; |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 287 | } else { |
Chris Lattner | 70f66ab | 2008-04-20 18:47:55 +0000 | [diff] [blame] | 288 | Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin), |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 289 | diag::err_exponent_has_no_digits); |
| 290 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | } |
| 294 | } else { // the first digit is non-zero |
| 295 | radix = 10; |
| 296 | s = SkipDigits(s); |
| 297 | if (s == ThisTokEnd) { |
| 298 | // Done. |
Christopher Lamb | 016765e | 2007-11-29 06:06:27 +0000 | [diff] [blame] | 299 | } else if (isxdigit(*s) && !(*s == 'e' || *s == 'E')) { |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 300 | Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin), |
| 301 | diag::err_invalid_decimal_digit, std::string(s, s+1)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 302 | return; |
| 303 | } else if (*s == '.') { |
| 304 | s++; |
| 305 | saw_period = true; |
| 306 | s = SkipDigits(s); |
| 307 | } |
| 308 | if (*s == 'e' || *s == 'E') { // exponent |
Chris Lattner | 70f66ab | 2008-04-20 18:47:55 +0000 | [diff] [blame] | 309 | const char *Exponent = s; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 310 | s++; |
| 311 | saw_exponent = true; |
| 312 | if (*s == '+' || *s == '-') s++; // sign |
| 313 | const char *first_non_digit = SkipDigits(s); |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 314 | if (first_non_digit != s) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 315 | s = first_non_digit; |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 316 | } else { |
Chris Lattner | 70f66ab | 2008-04-20 18:47:55 +0000 | [diff] [blame] | 317 | Diag(PP.AdvanceToTokenCharacter(TokLoc, Exponent-begin), |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 318 | diag::err_exponent_has_no_digits); |
| 319 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 320 | } |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | SuffixBegin = s; |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 325 | |
| 326 | // Parse the suffix. At this point we can classify whether we have an FP or |
| 327 | // integer constant. |
| 328 | bool isFPConstant = isFloatingLiteral(); |
| 329 | |
| 330 | // Loop over all of the characters of the suffix. If we see something bad, |
| 331 | // we break out of the loop. |
| 332 | for (; s != ThisTokEnd; ++s) { |
| 333 | switch (*s) { |
| 334 | case 'f': // FP Suffix for "float" |
| 335 | case 'F': |
| 336 | if (!isFPConstant) break; // Error for integer constant. |
Chris Lattner | 6e400c2 | 2007-08-26 03:29:23 +0000 | [diff] [blame] | 337 | if (isFloat || isLong) break; // FF, LF invalid. |
| 338 | isFloat = true; |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 339 | continue; // Success. |
| 340 | case 'u': |
| 341 | case 'U': |
| 342 | if (isFPConstant) break; // Error for floating constant. |
| 343 | if (isUnsigned) break; // Cannot be repeated. |
| 344 | isUnsigned = true; |
| 345 | continue; // Success. |
| 346 | case 'l': |
| 347 | case 'L': |
| 348 | if (isLong || isLongLong) break; // Cannot be repeated. |
Chris Lattner | 6e400c2 | 2007-08-26 03:29:23 +0000 | [diff] [blame] | 349 | if (isFloat) break; // LF invalid. |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 350 | |
| 351 | // Check for long long. The L's need to be adjacent and the same case. |
| 352 | if (s+1 != ThisTokEnd && s[1] == s[0]) { |
| 353 | if (isFPConstant) break; // long long invalid for floats. |
| 354 | isLongLong = true; |
| 355 | ++s; // Eat both of them. |
| 356 | } else { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 357 | isLong = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 358 | } |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 359 | continue; // Success. |
| 360 | case 'i': |
Steve Naroff | 0c29b22 | 2008-04-04 21:02:54 +0000 | [diff] [blame] | 361 | if (PP.getLangOptions().Microsoft) { |
| 362 | // Allow i8, i16, i32, i64, and i128. |
| 363 | if (++s == ThisTokEnd) break; |
| 364 | switch (*s) { |
| 365 | case '8': |
| 366 | s++; // i8 suffix |
| 367 | break; |
| 368 | case '1': |
| 369 | if (++s == ThisTokEnd) break; |
| 370 | if (*s == '6') s++; // i16 suffix |
| 371 | else if (*s == '2') { |
| 372 | if (++s == ThisTokEnd) break; |
| 373 | if (*s == '8') s++; // i128 suffix |
| 374 | } |
| 375 | break; |
| 376 | case '3': |
| 377 | if (++s == ThisTokEnd) break; |
| 378 | if (*s == '2') s++; // i32 suffix |
| 379 | break; |
| 380 | case '6': |
| 381 | if (++s == ThisTokEnd) break; |
| 382 | if (*s == '4') s++; // i64 suffix |
| 383 | break; |
| 384 | default: |
| 385 | break; |
| 386 | } |
| 387 | break; |
| 388 | } |
| 389 | // fall through. |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 390 | case 'I': |
| 391 | case 'j': |
| 392 | case 'J': |
| 393 | if (isImaginary) break; // Cannot be repeated. |
| 394 | PP.Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin), |
| 395 | diag::ext_imaginary_constant); |
| 396 | isImaginary = true; |
| 397 | continue; // Success. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 398 | } |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 399 | // If we reached here, there was an error. |
| 400 | break; |
| 401 | } |
| 402 | |
| 403 | // Report an error if there are any. |
| 404 | if (s != ThisTokEnd) { |
Chris Lattner | 0b7f69d | 2008-04-20 18:41:46 +0000 | [diff] [blame] | 405 | Diag(PP.AdvanceToTokenCharacter(TokLoc, s-begin), |
| 406 | isFPConstant ? diag::err_invalid_suffix_float_constant : |
| 407 | diag::err_invalid_suffix_integer_constant, |
Chris Lattner | 506b8de | 2007-08-26 01:58:14 +0000 | [diff] [blame] | 408 | std::string(SuffixBegin, ThisTokEnd)); |
| 409 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 410 | } |
| 411 | } |
| 412 | |
| 413 | /// GetIntegerValue - Convert this numeric literal value to an APInt that |
| 414 | /// matches Val's input width. If there is an overflow, set Val to the low bits |
| 415 | /// of the result and return true. Otherwise, return false. |
| 416 | bool NumericLiteralParser::GetIntegerValue(llvm::APInt &Val) { |
| 417 | Val = 0; |
| 418 | s = DigitsBegin; |
| 419 | |
| 420 | llvm::APInt RadixVal(Val.getBitWidth(), radix); |
| 421 | llvm::APInt CharVal(Val.getBitWidth(), 0); |
| 422 | llvm::APInt OldVal = Val; |
| 423 | |
| 424 | bool OverflowOccurred = false; |
| 425 | while (s < SuffixBegin) { |
| 426 | unsigned C = HexDigitValue(*s++); |
| 427 | |
| 428 | // If this letter is out of bound for this radix, reject it. |
| 429 | assert(C < radix && "NumericLiteralParser ctor should have rejected this"); |
| 430 | |
| 431 | CharVal = C; |
| 432 | |
| 433 | // Add the digit to the value in the appropriate radix. If adding in digits |
| 434 | // made the value smaller, then this overflowed. |
| 435 | OldVal = Val; |
| 436 | |
| 437 | // Multiply by radix, did overflow occur on the multiply? |
| 438 | Val *= RadixVal; |
| 439 | OverflowOccurred |= Val.udiv(RadixVal) != OldVal; |
| 440 | |
| 441 | OldVal = Val; |
| 442 | // Add value, did overflow occur on the value? |
| 443 | Val += CharVal; |
| 444 | OverflowOccurred |= Val.ult(OldVal); |
| 445 | OverflowOccurred |= Val.ult(CharVal); |
| 446 | } |
| 447 | return OverflowOccurred; |
| 448 | } |
| 449 | |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 450 | llvm::APFloat NumericLiteralParser:: |
Ted Kremenek | 427d5af | 2007-11-26 23:12:30 +0000 | [diff] [blame] | 451 | GetFloatValue(const llvm::fltSemantics &Format, bool* isExact) { |
| 452 | using llvm::APFloat; |
| 453 | |
Ted Kremenek | 32e61bf | 2007-11-29 00:54:29 +0000 | [diff] [blame] | 454 | llvm::SmallVector<char,256> floatChars; |
| 455 | for (unsigned i = 0, n = ThisTokEnd-ThisTokBegin; i != n; ++i) |
| 456 | floatChars.push_back(ThisTokBegin[i]); |
| 457 | |
| 458 | floatChars.push_back('\0'); |
| 459 | |
Ted Kremenek | 427d5af | 2007-11-26 23:12:30 +0000 | [diff] [blame] | 460 | APFloat V (Format, APFloat::fcZero, false); |
Ted Kremenek | 427d5af | 2007-11-26 23:12:30 +0000 | [diff] [blame] | 461 | APFloat::opStatus status; |
Ted Kremenek | 32e61bf | 2007-11-29 00:54:29 +0000 | [diff] [blame] | 462 | |
| 463 | status = V.convertFromString(&floatChars[0],APFloat::rmNearestTiesToEven); |
Ted Kremenek | 427d5af | 2007-11-26 23:12:30 +0000 | [diff] [blame] | 464 | |
| 465 | if (isExact) |
| 466 | *isExact = status == APFloat::opOK; |
| 467 | |
| 468 | return V; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void NumericLiteralParser::Diag(SourceLocation Loc, unsigned DiagID, |
| 472 | const std::string &M) { |
| 473 | PP.Diag(Loc, DiagID, M); |
| 474 | hadError = true; |
| 475 | } |
| 476 | |
| 477 | |
| 478 | CharLiteralParser::CharLiteralParser(const char *begin, const char *end, |
| 479 | SourceLocation Loc, Preprocessor &PP) { |
| 480 | // At this point we know that the character matches the regex "L?'.*'". |
| 481 | HadError = false; |
| 482 | Value = 0; |
| 483 | |
| 484 | // Determine if this is a wide character. |
| 485 | IsWide = begin[0] == 'L'; |
| 486 | if (IsWide) ++begin; |
| 487 | |
| 488 | // Skip over the entry quote. |
| 489 | assert(begin[0] == '\'' && "Invalid token lexed"); |
| 490 | ++begin; |
| 491 | |
| 492 | // FIXME: This assumes that 'int' is 32-bits in overflow calculation, and the |
| 493 | // size of "value". |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 494 | assert(PP.getTargetInfo().getIntWidth() == 32 && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 495 | "Assumes sizeof(int) == 4 for now"); |
| 496 | // FIXME: This assumes that wchar_t is 32-bits for now. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 497 | assert(PP.getTargetInfo().getWCharWidth() == 32 && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 498 | "Assumes sizeof(wchar_t) == 4 for now"); |
| 499 | // FIXME: This extensively assumes that 'char' is 8-bits. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 500 | assert(PP.getTargetInfo().getCharWidth() == 8 && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 501 | "Assumes char is 8 bits"); |
| 502 | |
| 503 | bool isFirstChar = true; |
| 504 | bool isMultiChar = false; |
| 505 | while (begin[0] != '\'') { |
| 506 | unsigned ResultChar; |
| 507 | if (begin[0] != '\\') // If this is a normal character, consume it. |
| 508 | ResultChar = *begin++; |
| 509 | else // Otherwise, this is an escape character. |
| 510 | ResultChar = ProcessCharEscape(begin, end, HadError, Loc, IsWide, PP); |
| 511 | |
| 512 | // If this is a multi-character constant (e.g. 'abc'), handle it. These are |
| 513 | // implementation defined (C99 6.4.4.4p10). |
| 514 | if (!isFirstChar) { |
| 515 | // If this is the second character being processed, do special handling. |
| 516 | if (!isMultiChar) { |
| 517 | isMultiChar = true; |
| 518 | |
| 519 | // Warn about discarding the top bits for multi-char wide-character |
| 520 | // constants (L'abcd'). |
| 521 | if (IsWide) |
| 522 | PP.Diag(Loc, diag::warn_extraneous_wide_char_constant); |
| 523 | } |
| 524 | |
| 525 | if (IsWide) { |
| 526 | // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'. |
| 527 | Value = 0; |
| 528 | } else { |
| 529 | // Narrow character literals act as though their value is concatenated |
| 530 | // in this implementation. |
| 531 | if (((Value << 8) >> 8) != Value) |
| 532 | PP.Diag(Loc, diag::warn_char_constant_too_large); |
| 533 | Value <<= 8; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | Value += ResultChar; |
| 538 | isFirstChar = false; |
| 539 | } |
| 540 | |
| 541 | // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1") |
| 542 | // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple |
| 543 | // character constants are not sign extended in the this implementation: |
| 544 | // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC. |
| 545 | if (!IsWide && !isMultiChar && (Value & 128) && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 546 | PP.getTargetInfo().isCharSigned()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 547 | Value = (signed char)Value; |
| 548 | } |
| 549 | |
| 550 | |
| 551 | /// string-literal: [C99 6.4.5] |
| 552 | /// " [s-char-sequence] " |
| 553 | /// L" [s-char-sequence] " |
| 554 | /// s-char-sequence: |
| 555 | /// s-char |
| 556 | /// s-char-sequence s-char |
| 557 | /// s-char: |
| 558 | /// any source character except the double quote ", |
| 559 | /// backslash \, or newline character |
| 560 | /// escape-character |
| 561 | /// universal-character-name |
| 562 | /// escape-character: [C99 6.4.4.4] |
| 563 | /// \ escape-code |
| 564 | /// universal-character-name |
| 565 | /// escape-code: |
| 566 | /// character-escape-code |
| 567 | /// octal-escape-code |
| 568 | /// hex-escape-code |
| 569 | /// character-escape-code: one of |
| 570 | /// n t b r f v a |
| 571 | /// \ ' " ? |
| 572 | /// octal-escape-code: |
| 573 | /// octal-digit |
| 574 | /// octal-digit octal-digit |
| 575 | /// octal-digit octal-digit octal-digit |
| 576 | /// hex-escape-code: |
| 577 | /// x hex-digit |
| 578 | /// hex-escape-code hex-digit |
| 579 | /// universal-character-name: |
| 580 | /// \u hex-quad |
| 581 | /// \U hex-quad hex-quad |
| 582 | /// hex-quad: |
| 583 | /// hex-digit hex-digit hex-digit hex-digit |
| 584 | /// |
| 585 | StringLiteralParser:: |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 586 | StringLiteralParser(const Token *StringToks, unsigned NumStringToks, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 587 | Preprocessor &pp, TargetInfo &t) |
| 588 | : PP(pp), Target(t) { |
| 589 | // Scan all of the string portions, remember the max individual token length, |
| 590 | // computing a bound on the concatenated string length, and see whether any |
| 591 | // piece is a wide-string. If any of the string portions is a wide-string |
| 592 | // literal, the result is a wide-string literal [C99 6.4.5p4]. |
| 593 | MaxTokenLength = StringToks[0].getLength(); |
| 594 | SizeBound = StringToks[0].getLength()-2; // -2 for "". |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 595 | AnyWide = StringToks[0].is(tok::wide_string_literal); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 596 | |
| 597 | hadError = false; |
| 598 | |
| 599 | // Implement Translation Phase #6: concatenation of string literals |
| 600 | /// (C99 5.1.1.2p1). The common case is only one string fragment. |
| 601 | for (unsigned i = 1; i != NumStringToks; ++i) { |
| 602 | // The string could be shorter than this if it needs cleaning, but this is a |
| 603 | // reasonable bound, which is all we need. |
| 604 | SizeBound += StringToks[i].getLength()-2; // -2 for "". |
| 605 | |
| 606 | // Remember maximum string piece length. |
| 607 | if (StringToks[i].getLength() > MaxTokenLength) |
| 608 | MaxTokenLength = StringToks[i].getLength(); |
| 609 | |
| 610 | // Remember if we see any wide strings. |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 611 | AnyWide |= StringToks[i].is(tok::wide_string_literal); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | |
| 615 | // Include space for the null terminator. |
| 616 | ++SizeBound; |
| 617 | |
| 618 | // TODO: K&R warning: "traditional C rejects string constant concatenation" |
| 619 | |
| 620 | // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not |
| 621 | // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true. |
| 622 | wchar_tByteWidth = ~0U; |
| 623 | if (AnyWide) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 624 | wchar_tByteWidth = Target.getWCharWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 625 | assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!"); |
| 626 | wchar_tByteWidth /= 8; |
| 627 | } |
| 628 | |
| 629 | // The output buffer size needs to be large enough to hold wide characters. |
| 630 | // This is a worst-case assumption which basically corresponds to L"" "long". |
| 631 | if (AnyWide) |
| 632 | SizeBound *= wchar_tByteWidth; |
| 633 | |
| 634 | // Size the temporary buffer to hold the result string data. |
| 635 | ResultBuf.resize(SizeBound); |
| 636 | |
| 637 | // Likewise, but for each string piece. |
| 638 | llvm::SmallString<512> TokenBuf; |
| 639 | TokenBuf.resize(MaxTokenLength); |
| 640 | |
| 641 | // Loop over all the strings, getting their spelling, and expanding them to |
| 642 | // wide strings as appropriate. |
| 643 | ResultPtr = &ResultBuf[0]; // Next byte to fill in. |
| 644 | |
Anders Carlsson | ee98ac5 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 645 | Pascal = false; |
| 646 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 647 | for (unsigned i = 0, e = NumStringToks; i != e; ++i) { |
| 648 | const char *ThisTokBuf = &TokenBuf[0]; |
| 649 | // Get the spelling of the token, which eliminates trigraphs, etc. We know |
| 650 | // that ThisTokBuf points to a buffer that is big enough for the whole token |
| 651 | // and 'spelled' tokens can only shrink. |
| 652 | unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf); |
| 653 | const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote. |
| 654 | |
| 655 | // TODO: Input character set mapping support. |
| 656 | |
| 657 | // Skip L marker for wide strings. |
| 658 | bool ThisIsWide = false; |
| 659 | if (ThisTokBuf[0] == 'L') { |
| 660 | ++ThisTokBuf; |
| 661 | ThisIsWide = true; |
| 662 | } |
| 663 | |
| 664 | assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?"); |
| 665 | ++ThisTokBuf; |
| 666 | |
Anders Carlsson | ee98ac5 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 667 | // Check if this is a pascal string |
| 668 | if (pp.getLangOptions().PascalStrings && ThisTokBuf + 1 != ThisTokEnd && |
| 669 | ThisTokBuf[0] == '\\' && ThisTokBuf[1] == 'p') { |
| 670 | |
| 671 | // If the \p sequence is found in the first token, we have a pascal string |
| 672 | // Otherwise, if we already have a pascal string, ignore the first \p |
| 673 | if (i == 0) { |
| 674 | ++ThisTokBuf; |
| 675 | Pascal = true; |
| 676 | } else if (Pascal) |
| 677 | ThisTokBuf += 2; |
| 678 | } |
| 679 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 680 | while (ThisTokBuf != ThisTokEnd) { |
| 681 | // Is this a span of non-escape characters? |
| 682 | if (ThisTokBuf[0] != '\\') { |
| 683 | const char *InStart = ThisTokBuf; |
| 684 | do { |
| 685 | ++ThisTokBuf; |
| 686 | } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\'); |
| 687 | |
| 688 | // Copy the character span over. |
| 689 | unsigned Len = ThisTokBuf-InStart; |
| 690 | if (!AnyWide) { |
| 691 | memcpy(ResultPtr, InStart, Len); |
| 692 | ResultPtr += Len; |
| 693 | } else { |
| 694 | // Note: our internal rep of wide char tokens is always little-endian. |
| 695 | for (; Len; --Len, ++InStart) { |
| 696 | *ResultPtr++ = InStart[0]; |
| 697 | // Add zeros at the end. |
| 698 | for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) |
| 699 | *ResultPtr++ = 0; |
| 700 | } |
| 701 | } |
| 702 | continue; |
| 703 | } |
| 704 | |
| 705 | // Otherwise, this is an escape character. Process it. |
| 706 | unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError, |
| 707 | StringToks[i].getLocation(), |
| 708 | ThisIsWide, PP); |
| 709 | |
| 710 | // Note: our internal rep of wide char tokens is always little-endian. |
| 711 | *ResultPtr++ = ResultChar & 0xFF; |
| 712 | |
| 713 | if (AnyWide) { |
| 714 | for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) |
| 715 | *ResultPtr++ = ResultChar >> i*8; |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 | |
| 720 | // Add zero terminator. |
| 721 | *ResultPtr = 0; |
| 722 | if (AnyWide) { |
| 723 | for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) |
| 724 | *ResultPtr++ = 0; |
| 725 | } |
Anders Carlsson | ee98ac5 | 2007-10-15 02:50:23 +0000 | [diff] [blame] | 726 | |
| 727 | if (Pascal) |
| 728 | ResultBuf[0] = ResultPtr-&ResultBuf[0]-1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 729 | } |