Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 1 | //===--- LiteralSupport.cpp - Code to parse and process literals ----------===// |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Steve Naroff and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 10 | // This file implements the NumericLiteralParser, CharLiteralParser, and |
| 11 | // StringLiteralParser interfaces. |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Lex/LiteralSupport.h" |
| 16 | #include "clang/Lex/Preprocessor.h" |
| 17 | #include "clang/Basic/TargetInfo.h" |
| 18 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/APInt.h" |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/StringExtras.h" |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | using namespace clang; |
| 23 | |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 24 | /// HexDigitValue - Return the value of the specified hex digit, or -1 if it's |
| 25 | /// not valid. |
| 26 | static int HexDigitValue(char C) { |
| 27 | if (C >= '0' && C <= '9') return C-'0'; |
| 28 | if (C >= 'a' && C <= 'f') return C-'a'+10; |
| 29 | if (C >= 'A' && C <= 'F') return C-'A'+10; |
| 30 | return -1; |
| 31 | } |
| 32 | |
| 33 | /// ProcessCharEscape - Parse a standard C escape sequence, which can occur in |
| 34 | /// either a character or a string literal. |
| 35 | static unsigned ProcessCharEscape(const char *&ThisTokBuf, |
| 36 | const char *ThisTokEnd, bool &HadError, |
| 37 | SourceLocation Loc, 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 | if (ThisTokBuf == ThisTokEnd || |
| 79 | (ResultChar = HexDigitValue(*ThisTokBuf)) == ~0U) { |
| 80 | PP.Diag(Loc, diag::err_hex_escape_no_digits); |
| 81 | HadError = 1; |
| 82 | ResultChar = 0; |
| 83 | break; |
| 84 | } |
| 85 | ++ThisTokBuf; // Consumed one hex digit. |
| 86 | |
| 87 | // FIXME: warn_hex_escape_too_large. '\x12345' |
| 88 | assert(0 && "hex escape: unimp!"); |
| 89 | break; |
| 90 | case '0': case '1': case '2': case '3': |
| 91 | case '4': case '5': case '6': case '7': |
| 92 | // Octal escapes. |
| 93 | // FIXME: warn_octal_escape_too_large. '\012345' |
| 94 | assert(0 && "octal escape: unimp!"); |
| 95 | break; |
| 96 | |
| 97 | // Otherwise, these are not valid escapes. |
| 98 | case '(': case '{': case '[': case '%': |
| 99 | // GCC accepts these as extensions. We warn about them as such though. |
| 100 | if (!PP.getLangOptions().NoExtensions) { |
| 101 | PP.Diag(Loc, diag::ext_nonstandard_escape, |
| 102 | std::string()+(char)ResultChar); |
| 103 | break; |
| 104 | } |
| 105 | // FALL THROUGH. |
| 106 | default: |
| 107 | if (isgraph(ThisTokBuf[0])) { |
| 108 | PP.Diag(Loc, diag::ext_unknown_escape, std::string()+(char)ResultChar); |
| 109 | } else { |
| 110 | PP.Diag(Loc, diag::ext_unknown_escape, "x"+utohexstr(ResultChar)); |
| 111 | } |
| 112 | break; |
| 113 | } |
| 114 | |
| 115 | return ResultChar; |
| 116 | } |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 121 | /// integer-constant: [C99 6.4.4.1] |
| 122 | /// decimal-constant integer-suffix |
| 123 | /// octal-constant integer-suffix |
| 124 | /// hexadecimal-constant integer-suffix |
| 125 | /// decimal-constant: |
| 126 | /// nonzero-digit |
| 127 | /// decimal-constant digit |
| 128 | /// octal-constant: |
| 129 | /// 0 |
| 130 | /// octal-constant octal-digit |
| 131 | /// hexadecimal-constant: |
| 132 | /// hexadecimal-prefix hexadecimal-digit |
| 133 | /// hexadecimal-constant hexadecimal-digit |
| 134 | /// hexadecimal-prefix: one of |
| 135 | /// 0x 0X |
| 136 | /// integer-suffix: |
| 137 | /// unsigned-suffix [long-suffix] |
| 138 | /// unsigned-suffix [long-long-suffix] |
| 139 | /// long-suffix [unsigned-suffix] |
| 140 | /// long-long-suffix [unsigned-sufix] |
| 141 | /// nonzero-digit: |
| 142 | /// 1 2 3 4 5 6 7 8 9 |
| 143 | /// octal-digit: |
| 144 | /// 0 1 2 3 4 5 6 7 |
| 145 | /// hexadecimal-digit: |
| 146 | /// 0 1 2 3 4 5 6 7 8 9 |
| 147 | /// a b c d e f |
| 148 | /// A B C D E F |
| 149 | /// unsigned-suffix: one of |
| 150 | /// u U |
| 151 | /// long-suffix: one of |
| 152 | /// l L |
| 153 | /// long-long-suffix: one of |
| 154 | /// ll LL |
| 155 | /// |
| 156 | /// floating-constant: [C99 6.4.4.2] |
| 157 | /// TODO: add rules... |
| 158 | /// |
| 159 | |
| 160 | NumericLiteralParser:: |
| 161 | NumericLiteralParser(const char *begin, const char *end, |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 162 | SourceLocation TokLoc, Preprocessor &pp) |
| 163 | : PP(pp), ThisTokBegin(begin), ThisTokEnd(end) { |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 164 | s = DigitsBegin = begin; |
| 165 | saw_exponent = false; |
| 166 | saw_period = false; |
| 167 | saw_float_suffix = false; |
| 168 | isLong = false; |
| 169 | isUnsigned = false; |
| 170 | isLongLong = false; |
| 171 | hadError = false; |
| 172 | |
| 173 | if (*s == '0') { // parse radix |
| 174 | s++; |
| 175 | if ((*s == 'x' || *s == 'X') && (isxdigit(s[1]) || s[1] == '.')) { |
| 176 | s++; |
| 177 | radix = 16; |
| 178 | DigitsBegin = s; |
| 179 | s = SkipHexDigits(s); |
| 180 | if (s == ThisTokEnd) { |
| 181 | } else if (*s == '.') { |
| 182 | s++; |
| 183 | saw_period = true; |
| 184 | s = SkipHexDigits(s); |
| 185 | } |
| 186 | // A binary exponent can appear with or with a '.'. If dotted, the |
| 187 | // binary exponent is required. |
| 188 | if (*s == 'p' || *s == 'P') { |
| 189 | s++; |
| 190 | saw_exponent = true; |
| 191 | if (*s == '+' || *s == '-') s++; // sign |
| 192 | const char *first_non_digit = SkipDigits(s); |
| 193 | if (first_non_digit == s) { |
| 194 | Diag(TokLoc, diag::err_exponent_has_no_digits); |
| 195 | return; |
| 196 | } else { |
| 197 | s = first_non_digit; |
| 198 | } |
| 199 | } else if (saw_period) { |
| 200 | Diag(TokLoc, diag::err_hexconstant_requires_exponent); |
| 201 | return; |
| 202 | } |
| 203 | } else { |
| 204 | // For now, the radix is set to 8. If we discover that we have a |
| 205 | // floating point constant, the radix will change to 10. Octal floating |
| 206 | // point constants are not permitted (only decimal and hexadecimal). |
| 207 | radix = 8; |
| 208 | DigitsBegin = s; |
| 209 | s = SkipOctalDigits(s); |
| 210 | if (s == ThisTokEnd) { |
| 211 | } else if (*s == '.') { |
| 212 | s++; |
| 213 | radix = 10; |
| 214 | saw_period = true; |
| 215 | s = SkipDigits(s); |
| 216 | } |
| 217 | if (*s == 'e' || *s == 'E') { // exponent |
| 218 | s++; |
| 219 | radix = 10; |
| 220 | saw_exponent = true; |
| 221 | if (*s == '+' || *s == '-') s++; // sign |
| 222 | const char *first_non_digit = SkipDigits(s); |
| 223 | if (first_non_digit == s) { |
| 224 | Diag(TokLoc, diag::err_exponent_has_no_digits); |
| 225 | return; |
| 226 | } else { |
| 227 | s = first_non_digit; |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | } else { // the first digit is non-zero |
| 232 | radix = 10; |
| 233 | s = SkipDigits(s); |
| 234 | if (s == ThisTokEnd) { |
| 235 | } else if (*s == '.') { |
| 236 | s++; |
| 237 | saw_period = true; |
| 238 | s = SkipDigits(s); |
| 239 | } |
| 240 | if (*s == 'e' || *s == 'E') { // exponent |
| 241 | s++; |
| 242 | saw_exponent = true; |
| 243 | if (*s == '+' || *s == '-') s++; // sign |
| 244 | const char *first_non_digit = SkipDigits(s); |
| 245 | if (first_non_digit == s) { |
| 246 | Diag(TokLoc, diag::err_exponent_has_no_digits); |
| 247 | return; |
| 248 | } else { |
| 249 | s = first_non_digit; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | SuffixBegin = s; |
| 255 | |
| 256 | if (saw_period || saw_exponent) { |
| 257 | if (s < ThisTokEnd) { // parse size suffix (float, long double) |
| 258 | if (*s == 'f' || *s == 'F') { |
| 259 | saw_float_suffix = true; |
| 260 | s++; |
| 261 | } else if (*s == 'l' || *s == 'L') { |
| 262 | isLong = true; |
| 263 | s++; |
| 264 | } |
| 265 | if (s != ThisTokEnd) { |
| 266 | Diag(TokLoc, diag::err_invalid_suffix_float_constant, |
| 267 | std::string(SuffixBegin, ThisTokEnd)); |
| 268 | return; |
| 269 | } |
| 270 | } |
| 271 | } else { |
| 272 | if (s < ThisTokEnd) { |
| 273 | // parse int suffix - they can appear in any order ("ul", "lu", "llu"). |
| 274 | if (*s == 'u' || *s == 'U') { |
| 275 | s++; |
| 276 | isUnsigned = true; // unsigned |
| 277 | |
| 278 | if ((s < ThisTokEnd) && (*s == 'l' || *s == 'L')) { |
| 279 | s++; |
| 280 | // handle "long long" type - l's need to be adjacent and same case. |
| 281 | if ((s < ThisTokEnd) && (*s == *(s-1))) { |
| 282 | isLongLong = true; // unsigned long long |
| 283 | s++; |
| 284 | } else { |
| 285 | isLong = true; // unsigned long |
| 286 | } |
| 287 | } |
| 288 | } else if (*s == 'l' || *s == 'L') { |
| 289 | s++; |
| 290 | // handle "long long" types - l's need to be adjacent and same case. |
| 291 | if ((s < ThisTokEnd) && (*s == *(s-1))) { |
| 292 | s++; |
| 293 | if ((s < ThisTokEnd) && (*s == 'u' || *s == 'U')) { |
| 294 | isUnsigned = true; // unsigned long long |
| 295 | s++; |
| 296 | } else { |
| 297 | isLongLong = true; // long long |
| 298 | } |
| 299 | } else { // handle "long" types |
| 300 | if ((s < ThisTokEnd) && (*s == 'u' || *s == 'U')) { |
| 301 | isUnsigned = true; // unsigned long |
| 302 | s++; |
| 303 | } else { |
| 304 | isLong = true; // long |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | if (s != ThisTokEnd) { |
| 309 | Diag(TokLoc, diag::err_invalid_suffix_integer_constant, |
| 310 | std::string(SuffixBegin, ThisTokEnd)); |
| 311 | return; |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | } |
| 316 | |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 317 | bool NumericLiteralParser::GetIntegerValue(uintmax_t &val) { |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 318 | uintmax_t max_value = UINTMAX_MAX / radix; |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 319 | unsigned max_digit = UINTMAX_MAX % radix; |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 320 | |
| 321 | val = 0; |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 322 | s = DigitsBegin; |
| 323 | while (s < SuffixBegin) { |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 324 | unsigned C = HexDigitValue(*s++); |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 325 | |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 326 | if (val > max_value || (val == max_value && C > max_digit)) { |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 327 | return false; // Overflow! |
| 328 | } else { |
| 329 | val *= radix; |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 330 | val += C; |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 331 | } |
| 332 | } |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | bool NumericLiteralParser::GetIntegerValue(int &val) { |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 337 | intmax_t max_value = INT_MAX / radix; |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 338 | unsigned max_digit = INT_MAX % radix; |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 339 | |
| 340 | val = 0; |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 341 | s = DigitsBegin; |
| 342 | while (s < SuffixBegin) { |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 343 | unsigned C = HexDigitValue(*s++); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 344 | |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 345 | if (val > max_value || (val == max_value && C > max_digit)) { |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 346 | return false; // Overflow! |
| 347 | } else { |
| 348 | val *= radix; |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 349 | val += C; |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | return true; |
| 353 | } |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 355 | /// GetIntegerValue - Convert this numeric literal value to an APInt that |
Chris Lattner | 871b4e1 | 2007-04-04 06:36:34 +0000 | [diff] [blame] | 356 | /// matches Val's input width. If there is an overflow, set Val to the low bits |
| 357 | /// of the result and return true. Otherwise, return false. |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 358 | bool NumericLiteralParser::GetIntegerValue(APInt &Val) { |
| 359 | Val = 0; |
| 360 | s = DigitsBegin; |
| 361 | |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 362 | APInt RadixVal(Val.getBitWidth(), radix); |
| 363 | APInt CharVal(Val.getBitWidth(), 0); |
| 364 | APInt OldVal = Val; |
Chris Lattner | 871b4e1 | 2007-04-04 06:36:34 +0000 | [diff] [blame] | 365 | |
| 366 | bool OverflowOccurred = false; |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 367 | while (s < SuffixBegin) { |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 368 | unsigned C = HexDigitValue(*s++); |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 369 | |
| 370 | // If this letter is out of bound for this radix, reject it. |
Chris Lattner | 531efa4 | 2007-04-04 06:49:26 +0000 | [diff] [blame] | 371 | assert(C < radix && "NumericLiteralParser ctor should have rejected this"); |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 372 | |
| 373 | CharVal = C; |
| 374 | |
Chris Lattner | 871b4e1 | 2007-04-04 06:36:34 +0000 | [diff] [blame] | 375 | // Add the digit to the value in the appropriate radix. If adding in digits |
| 376 | // made the value smaller, then this overflowed. |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 377 | OldVal = Val; |
Chris Lattner | 871b4e1 | 2007-04-04 06:36:34 +0000 | [diff] [blame] | 378 | |
| 379 | // Multiply by radix, did overflow occur on the multiply? |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 380 | Val *= RadixVal; |
Chris Lattner | 871b4e1 | 2007-04-04 06:36:34 +0000 | [diff] [blame] | 381 | OverflowOccurred |= Val.udiv(RadixVal) != OldVal; |
| 382 | |
| 383 | OldVal = Val; |
| 384 | // Add value, did overflow occur on the value? |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 385 | Val += CharVal; |
Chris Lattner | 871b4e1 | 2007-04-04 06:36:34 +0000 | [diff] [blame] | 386 | OverflowOccurred |= Val.ult(OldVal); |
| 387 | OverflowOccurred |= Val.ult(CharVal); |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 388 | } |
Chris Lattner | 871b4e1 | 2007-04-04 06:36:34 +0000 | [diff] [blame] | 389 | return OverflowOccurred; |
Chris Lattner | 5b743d3 | 2007-04-04 05:52:58 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 393 | void NumericLiteralParser::Diag(SourceLocation Loc, unsigned DiagID, |
| 394 | const std::string &M) { |
| 395 | PP.Diag(Loc, DiagID, M); |
| 396 | hadError = true; |
| 397 | } |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 398 | |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 399 | |
| 400 | CharLiteralParser::CharLiteralParser(const char *begin, const char *end, |
| 401 | SourceLocation Loc, Preprocessor &PP) { |
| 402 | // At this point we know that the character matches the regex "L?'.*'". |
| 403 | HadError = false; |
| 404 | Value = 0; |
| 405 | |
| 406 | // Determine if this is a wide character. |
| 407 | IsWide = begin[0] == 'L'; |
| 408 | if (IsWide) ++begin; |
| 409 | |
| 410 | // Skip over the entry quote. |
| 411 | assert(begin[0] == '\'' && "Invalid token lexed"); |
| 412 | ++begin; |
| 413 | |
| 414 | // FIXME: This assumes that 'int' is 32-bits in overflow calculation, and the |
| 415 | // size of "value". |
| 416 | assert(PP.getTargetInfo().getIntWidth(Loc) == 32 && |
| 417 | "Assumes sizeof(int) == 4 for now"); |
| 418 | // FIXME: This assumes that wchar_t is 32-bits for now. |
| 419 | assert(PP.getTargetInfo().getWCharWidth(Loc) == 32 && |
| 420 | "Assumes sizeof(wchar_t) == 4 for now"); |
| 421 | // FIXME: This extensively assumes that 'char' is 8-bits. |
| 422 | assert(PP.getTargetInfo().getCharWidth(Loc) == 8 && |
| 423 | "Assumes char is 8 bits"); |
| 424 | |
| 425 | bool isFirstChar = true; |
| 426 | bool isMultiChar = false; |
| 427 | while (begin[0] != '\'') { |
| 428 | unsigned ResultChar; |
| 429 | if (begin[0] != '\\') // If this is a normal character, consume it. |
| 430 | ResultChar = *begin++; |
| 431 | else // Otherwise, this is an escape character. |
| 432 | ResultChar = ProcessCharEscape(begin, end, HadError, Loc, PP); |
| 433 | |
| 434 | // If this is a multi-character constant (e.g. 'abc'), handle it. These are |
| 435 | // implementation defined (C99 6.4.4.4p10). |
| 436 | if (!isFirstChar) { |
| 437 | // If this is the second character being processed, do special handling. |
| 438 | if (!isMultiChar) { |
| 439 | isMultiChar = true; |
| 440 | |
| 441 | // Warn about discarding the top bits for multi-char wide-character |
| 442 | // constants (L'abcd'). |
| 443 | if (IsWide) |
| 444 | PP.Diag(Loc, diag::warn_extraneous_wide_char_constant); |
| 445 | } |
| 446 | |
| 447 | if (IsWide) { |
| 448 | // Emulate GCC's (unintentional?) behavior: L'ab' -> L'b'. |
| 449 | Value = 0; |
| 450 | } else { |
| 451 | // Narrow character literals act as though their value is concatenated |
| 452 | // in this implementation. |
| 453 | if (((Value << 8) >> 8) != Value) |
| 454 | PP.Diag(Loc, diag::warn_char_constant_too_large); |
| 455 | Value <<= 8; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | Value += ResultChar; |
| 460 | isFirstChar = false; |
| 461 | } |
| 462 | |
| 463 | // If this is a single narrow character, sign extend it (e.g. '\xFF' is "-1") |
| 464 | // if 'char' is signed for this target (C99 6.4.4.4p10). Note that multiple |
| 465 | // character constants are not sign extended in the this implementation: |
| 466 | // '\xFF\xFF' = 65536 and '\x0\xFF' = 255, which matches GCC. |
| 467 | if (!IsWide && !isMultiChar && (Value & 128) && |
| 468 | PP.getTargetInfo().isCharSigned(Loc)) |
| 469 | Value = (signed char)Value; |
| 470 | } |
| 471 | |
| 472 | |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 473 | /// string-literal: [C99 6.4.5] |
| 474 | /// " [s-char-sequence] " |
| 475 | /// L" [s-char-sequence] " |
| 476 | /// s-char-sequence: |
| 477 | /// s-char |
| 478 | /// s-char-sequence s-char |
| 479 | /// s-char: |
| 480 | /// any source character except the double quote ", |
| 481 | /// backslash \, or newline character |
| 482 | /// escape-character |
| 483 | /// universal-character-name |
| 484 | /// escape-character: [C99 6.4.4.4] |
| 485 | /// \ escape-code |
| 486 | /// universal-character-name |
| 487 | /// escape-code: |
| 488 | /// character-escape-code |
| 489 | /// octal-escape-code |
| 490 | /// hex-escape-code |
| 491 | /// character-escape-code: one of |
| 492 | /// n t b r f v a |
| 493 | /// \ ' " ? |
| 494 | /// octal-escape-code: |
| 495 | /// octal-digit |
| 496 | /// octal-digit octal-digit |
| 497 | /// octal-digit octal-digit octal-digit |
| 498 | /// hex-escape-code: |
| 499 | /// x hex-digit |
| 500 | /// hex-escape-code hex-digit |
| 501 | /// universal-character-name: |
| 502 | /// \u hex-quad |
| 503 | /// \U hex-quad hex-quad |
| 504 | /// hex-quad: |
| 505 | /// hex-digit hex-digit hex-digit hex-digit |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 506 | /// |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 507 | StringLiteralParser:: |
| 508 | StringLiteralParser(const LexerToken *StringToks, unsigned NumStringToks, |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 509 | Preprocessor &pp, TargetInfo &t) |
| 510 | : PP(pp), Target(t) { |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 511 | // Scan all of the string portions, remember the max individual token length, |
| 512 | // computing a bound on the concatenated string length, and see whether any |
| 513 | // piece is a wide-string. If any of the string portions is a wide-string |
| 514 | // literal, the result is a wide-string literal [C99 6.4.5p4]. |
| 515 | MaxTokenLength = StringToks[0].getLength(); |
| 516 | SizeBound = StringToks[0].getLength()-2; // -2 for "". |
| 517 | AnyWide = StringToks[0].getKind() == tok::wide_string_literal; |
| 518 | |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 519 | hadError = false; |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 520 | |
| 521 | // Implement Translation Phase #6: concatenation of string literals |
| 522 | /// (C99 5.1.1.2p1). The common case is only one string fragment. |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 523 | for (unsigned i = 1; i != NumStringToks; ++i) { |
| 524 | // The string could be shorter than this if it needs cleaning, but this is a |
| 525 | // reasonable bound, which is all we need. |
| 526 | SizeBound += StringToks[i].getLength()-2; // -2 for "". |
| 527 | |
| 528 | // Remember maximum string piece length. |
| 529 | if (StringToks[i].getLength() > MaxTokenLength) |
| 530 | MaxTokenLength = StringToks[i].getLength(); |
| 531 | |
| 532 | // Remember if we see any wide strings. |
| 533 | AnyWide |= StringToks[i].getKind() == tok::wide_string_literal; |
| 534 | } |
| 535 | |
| 536 | |
| 537 | // Include space for the null terminator. |
| 538 | ++SizeBound; |
| 539 | |
| 540 | // TODO: K&R warning: "traditional C rejects string constant concatenation" |
| 541 | |
| 542 | // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not |
| 543 | // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true. |
| 544 | wchar_tByteWidth = ~0U; |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 545 | if (AnyWide) { |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 546 | wchar_tByteWidth = Target.getWCharWidth(StringToks[0].getLocation()); |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 547 | assert((wchar_tByteWidth & 7) == 0 && "Assumes wchar_t is byte multiple!"); |
| 548 | wchar_tByteWidth /= 8; |
| 549 | } |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 550 | |
| 551 | // The output buffer size needs to be large enough to hold wide characters. |
| 552 | // This is a worst-case assumption which basically corresponds to L"" "long". |
| 553 | if (AnyWide) |
| 554 | SizeBound *= wchar_tByteWidth; |
| 555 | |
| 556 | // Size the temporary buffer to hold the result string data. |
| 557 | ResultBuf.resize(SizeBound); |
| 558 | |
| 559 | // Likewise, but for each string piece. |
| 560 | SmallString<512> TokenBuf; |
| 561 | TokenBuf.resize(MaxTokenLength); |
| 562 | |
| 563 | // Loop over all the strings, getting their spelling, and expanding them to |
| 564 | // wide strings as appropriate. |
| 565 | ResultPtr = &ResultBuf[0]; // Next byte to fill in. |
| 566 | |
| 567 | for (unsigned i = 0, e = NumStringToks; i != e; ++i) { |
| 568 | const char *ThisTokBuf = &TokenBuf[0]; |
| 569 | // Get the spelling of the token, which eliminates trigraphs, etc. We know |
| 570 | // that ThisTokBuf points to a buffer that is big enough for the whole token |
| 571 | // and 'spelled' tokens can only shrink. |
| 572 | unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf); |
| 573 | const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote. |
| 574 | |
| 575 | // TODO: Input character set mapping support. |
| 576 | |
| 577 | // Skip L marker for wide strings. |
| 578 | if (ThisTokBuf[0] == 'L') ++ThisTokBuf; |
| 579 | |
| 580 | assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?"); |
| 581 | ++ThisTokBuf; |
| 582 | |
| 583 | while (ThisTokBuf != ThisTokEnd) { |
| 584 | // Is this a span of non-escape characters? |
| 585 | if (ThisTokBuf[0] != '\\') { |
| 586 | const char *InStart = ThisTokBuf; |
| 587 | do { |
| 588 | ++ThisTokBuf; |
| 589 | } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\'); |
| 590 | |
| 591 | // Copy the character span over. |
| 592 | unsigned Len = ThisTokBuf-InStart; |
| 593 | if (!AnyWide) { |
| 594 | memcpy(ResultPtr, InStart, Len); |
| 595 | ResultPtr += Len; |
| 596 | } else { |
| 597 | // Note: our internal rep of wide char tokens is always little-endian. |
| 598 | for (; Len; --Len, ++InStart) { |
| 599 | *ResultPtr++ = InStart[0]; |
| 600 | // Add zeros at the end. |
| 601 | for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) |
| 602 | *ResultPtr++ = 0; |
| 603 | } |
| 604 | } |
| 605 | continue; |
| 606 | } |
| 607 | |
Chris Lattner | 2f5add6 | 2007-04-05 06:57:15 +0000 | [diff] [blame] | 608 | // Otherwise, this is an escape character. Process it. |
| 609 | unsigned ResultChar = ProcessCharEscape(ThisTokBuf, ThisTokEnd, hadError, |
| 610 | StringToks[i].getLocation(), PP); |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 611 | |
| 612 | // Note: our internal rep of wide char tokens is always little-endian. |
| 613 | *ResultPtr++ = ResultChar & 0xFF; |
| 614 | |
| 615 | if (AnyWide) { |
| 616 | for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) |
| 617 | *ResultPtr++ = ResultChar >> i*8; |
| 618 | } |
| 619 | } |
| 620 | } |
| 621 | |
| 622 | // Add zero terminator. |
| 623 | *ResultPtr = 0; |
| 624 | if (AnyWide) { |
| 625 | for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) |
| 626 | *ResultPtr++ = 0; |
| 627 | } |
| 628 | } |