Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1 | //===--- Lexer.cpp - C Language Family Lexer ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Lexer and LexerToken interfaces. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
| 14 | // TODO: GCC Diagnostics emitted by the lexer: |
| 15 | // PEDWARN: (form feed|vertical tab) in preprocessing directive |
| 16 | // |
| 17 | // Universal characters, unicode, char mapping: |
| 18 | // WARNING: `%.*s' is not in NFKC |
| 19 | // WARNING: `%.*s' is not in NFC |
| 20 | // |
| 21 | // Other: |
| 22 | // ERROR : attempt to use poisoned \"%s\" |
| 23 | // |
| 24 | // TODO: Options to support: |
| 25 | // -fexec-charset,-fwide-exec-charset |
| 26 | // |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
| 29 | #include "clang/Lex/Lexer.h" |
| 30 | #include "clang/Lex/Preprocessor.h" |
| 31 | #include "clang/Basic/Diagnostic.h" |
| 32 | #include "clang/Basic/SourceBuffer.h" |
| 33 | #include "clang/Basic/SourceLocation.h" |
| 34 | #include "llvm/Config/alloca.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 35 | #include <cctype> |
| 36 | #include <iostream> |
| 37 | using namespace llvm; |
| 38 | using namespace clang; |
| 39 | |
| 40 | static void InitCharacterInfo(); |
| 41 | |
| 42 | Lexer::Lexer(const SourceBuffer *File, unsigned fileid, Preprocessor &pp) |
| 43 | : BufferPtr(File->getBufferStart()), BufferStart(BufferPtr), |
| 44 | BufferEnd(File->getBufferEnd()), InputFile(File), CurFileID(fileid), PP(pp), |
| 45 | Features(PP.getLangOptions()) { |
| 46 | InitCharacterInfo(); |
| 47 | |
| 48 | assert(BufferEnd[0] == 0 && |
| 49 | "We assume that the input buffer has a null character at the end" |
| 50 | " to simplify lexing!"); |
| 51 | |
| 52 | // Start of the file is a start of line. |
| 53 | IsAtStartOfLine = true; |
| 54 | |
| 55 | // We are not after parsing a #. |
| 56 | ParsingPreprocessorDirective = false; |
| 57 | |
| 58 | // We are not after parsing #include. |
| 59 | ParsingFilename = false; |
| 60 | } |
| 61 | |
| 62 | //===----------------------------------------------------------------------===// |
| 63 | // LexerToken implementation. |
| 64 | //===----------------------------------------------------------------------===// |
| 65 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 66 | //===----------------------------------------------------------------------===// |
| 67 | // Character information. |
| 68 | //===----------------------------------------------------------------------===// |
| 69 | |
| 70 | static unsigned char CharInfo[256]; |
| 71 | |
| 72 | enum { |
| 73 | CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0' |
| 74 | CHAR_VERT_WS = 0x02, // '\r', '\n' |
| 75 | CHAR_LETTER = 0x04, // a-z,A-Z |
| 76 | CHAR_NUMBER = 0x08, // 0-9 |
| 77 | CHAR_UNDER = 0x10, // _ |
| 78 | CHAR_PERIOD = 0x20 // . |
| 79 | }; |
| 80 | |
| 81 | static void InitCharacterInfo() { |
| 82 | static bool isInited = false; |
| 83 | if (isInited) return; |
| 84 | isInited = true; |
| 85 | |
| 86 | // Intiialize the CharInfo table. |
| 87 | // TODO: statically initialize this. |
| 88 | CharInfo[(int)' '] = CharInfo[(int)'\t'] = |
| 89 | CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS; |
| 90 | CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS; |
| 91 | |
| 92 | CharInfo[(int)'_'] = CHAR_UNDER; |
| 93 | for (unsigned i = 'a'; i <= 'z'; ++i) |
| 94 | CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER; |
| 95 | for (unsigned i = '0'; i <= '9'; ++i) |
| 96 | CharInfo[i] = CHAR_NUMBER; |
| 97 | } |
| 98 | |
| 99 | /// isIdentifierBody - Return true if this is the body character of an |
| 100 | /// identifier, which is [a-zA-Z0-9_]. |
| 101 | static inline bool isIdentifierBody(unsigned char c) { |
| 102 | return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER); |
| 103 | } |
| 104 | |
| 105 | /// isHorizontalWhitespace - Return true if this character is horizontal |
| 106 | /// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'. |
| 107 | static inline bool isHorizontalWhitespace(unsigned char c) { |
| 108 | return CharInfo[c] & CHAR_HORZ_WS; |
| 109 | } |
| 110 | |
| 111 | /// isWhitespace - Return true if this character is horizontal or vertical |
| 112 | /// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false |
| 113 | /// for '\0'. |
| 114 | static inline bool isWhitespace(unsigned char c) { |
| 115 | return CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS); |
| 116 | } |
| 117 | |
| 118 | /// isNumberBody - Return true if this is the body character of an |
| 119 | /// preprocessing number, which is [a-zA-Z0-9_.]. |
| 120 | static inline bool isNumberBody(unsigned char c) { |
| 121 | return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD); |
| 122 | } |
| 123 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 124 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 125 | //===----------------------------------------------------------------------===// |
| 126 | // Diagnostics forwarding code. |
| 127 | //===----------------------------------------------------------------------===// |
| 128 | |
| 129 | /// getSourceLocation - Return a source location identifier for the specified |
| 130 | /// offset in the current file. |
| 131 | SourceLocation Lexer::getSourceLocation(const char *Loc) const { |
| 132 | assert(Loc >= InputFile->getBufferStart() && Loc <= InputFile->getBufferEnd() |
| 133 | && "Location out of range for this buffer!"); |
| 134 | return SourceLocation(CurFileID, Loc-InputFile->getBufferStart()); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | /// Diag - Forwarding function for diagnostics. This translate a source |
| 139 | /// position in the current buffer into a SourceLocation object for rendering. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 140 | void Lexer::Diag(const char *Loc, unsigned DiagID, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 141 | const std::string &Msg) const { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 142 | PP.Diag(getSourceLocation(Loc), DiagID, Msg); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | //===----------------------------------------------------------------------===// |
| 146 | // Trigraph and Escaped Newline Handling Code. |
| 147 | //===----------------------------------------------------------------------===// |
| 148 | |
| 149 | /// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair, |
| 150 | /// return the decoded trigraph letter it corresponds to, or '\0' if nothing. |
| 151 | static char GetTrigraphCharForLetter(char Letter) { |
| 152 | switch (Letter) { |
| 153 | default: return 0; |
| 154 | case '=': return '#'; |
| 155 | case ')': return ']'; |
| 156 | case '(': return '['; |
| 157 | case '!': return '|'; |
| 158 | case '\'': return '^'; |
| 159 | case '>': return '}'; |
| 160 | case '/': return '\\'; |
| 161 | case '<': return '{'; |
| 162 | case '-': return '~'; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /// DecodeTrigraphChar - If the specified character is a legal trigraph when |
| 167 | /// prefixed with ??, emit a trigraph warning. If trigraphs are enabled, |
| 168 | /// return the result character. Finally, emit a warning about trigraph use |
| 169 | /// whether trigraphs are enabled or not. |
| 170 | static char DecodeTrigraphChar(const char *CP, Lexer *L) { |
| 171 | char Res = GetTrigraphCharForLetter(*CP); |
| 172 | if (Res && L) { |
| 173 | if (!L->getFeatures().Trigraphs) { |
| 174 | L->Diag(CP-2, diag::trigraph_ignored); |
| 175 | return 0; |
| 176 | } else { |
| 177 | L->Diag(CP-2, diag::trigraph_converted, std::string()+Res); |
| 178 | } |
| 179 | } |
| 180 | return Res; |
| 181 | } |
| 182 | |
| 183 | /// getCharAndSizeSlow - Peek a single 'character' from the specified buffer, |
| 184 | /// get its size, and return it. This is tricky in several cases: |
| 185 | /// 1. If currently at the start of a trigraph, we warn about the trigraph, |
| 186 | /// then either return the trigraph (skipping 3 chars) or the '?', |
| 187 | /// depending on whether trigraphs are enabled or not. |
| 188 | /// 2. If this is an escaped newline (potentially with whitespace between |
| 189 | /// the backslash and newline), implicitly skip the newline and return |
| 190 | /// the char after it. |
| 191 | /// 3. If this is a UCN, return it. FIXME: for C++? |
| 192 | /// |
| 193 | /// This handles the slow/uncommon case of the getCharAndSize method. Here we |
| 194 | /// know that we can accumulate into Size, and that we have already incremented |
| 195 | /// Ptr by Size bytes. |
| 196 | /// |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 197 | /// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should |
| 198 | /// be updated to match. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 199 | /// |
| 200 | char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size, |
| 201 | LexerToken *Tok) { |
| 202 | // If we have a slash, look for an escaped newline. |
| 203 | if (Ptr[0] == '\\') { |
| 204 | ++Size; |
| 205 | ++Ptr; |
| 206 | Slash: |
| 207 | // Common case, backslash-char where the char is not whitespace. |
| 208 | if (!isWhitespace(Ptr[0])) return '\\'; |
| 209 | |
| 210 | // See if we have optional whitespace characters followed by a newline. |
| 211 | { |
| 212 | unsigned SizeTmp = 0; |
| 213 | do { |
| 214 | ++SizeTmp; |
| 215 | if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') { |
| 216 | // Remember that this token needs to be cleaned. |
| 217 | if (Tok) Tok->SetFlag(LexerToken::NeedsCleaning); |
| 218 | |
| 219 | // Warn if there was whitespace between the backslash and newline. |
| 220 | if (SizeTmp != 1 && Tok) |
| 221 | Diag(Ptr, diag::backslash_newline_space); |
| 222 | |
| 223 | // If this is a \r\n or \n\r, skip the newlines. |
| 224 | if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') && |
| 225 | Ptr[SizeTmp-1] != Ptr[SizeTmp]) |
| 226 | ++SizeTmp; |
| 227 | |
| 228 | // Found backslash<whitespace><newline>. Parse the char after it. |
| 229 | Size += SizeTmp; |
| 230 | Ptr += SizeTmp; |
| 231 | // Use slow version to accumulate a correct size field. |
| 232 | return getCharAndSizeSlow(Ptr, Size, Tok); |
| 233 | } |
| 234 | } while (isWhitespace(Ptr[SizeTmp])); |
| 235 | } |
| 236 | |
| 237 | // Otherwise, this is not an escaped newline, just return the slash. |
| 238 | return '\\'; |
| 239 | } |
| 240 | |
| 241 | // If this is a trigraph, process it. |
| 242 | if (Ptr[0] == '?' && Ptr[1] == '?') { |
| 243 | // If this is actually a legal trigraph (not something like "??x"), emit |
| 244 | // a trigraph warning. If so, and if trigraphs are enabled, return it. |
| 245 | if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) { |
| 246 | // Remember that this token needs to be cleaned. |
| 247 | if (Tok) Tok->SetFlag(LexerToken::NeedsCleaning); |
| 248 | |
| 249 | Ptr += 3; |
| 250 | Size += 3; |
| 251 | if (C == '\\') goto Slash; |
| 252 | return C; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // If this is neither, return a single character. |
| 257 | ++Size; |
| 258 | return *Ptr; |
| 259 | } |
| 260 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 261 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 262 | /// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the |
| 263 | /// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size, |
| 264 | /// and that we have already incremented Ptr by Size bytes. |
| 265 | /// |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 266 | /// NOTE: When this method is updated, getCharAndSizeSlow (above) should |
| 267 | /// be updated to match. |
| 268 | char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size, |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 269 | const LangOptions &Features) { |
| 270 | // If we have a slash, look for an escaped newline. |
| 271 | if (Ptr[0] == '\\') { |
| 272 | ++Size; |
| 273 | ++Ptr; |
| 274 | Slash: |
| 275 | // Common case, backslash-char where the char is not whitespace. |
| 276 | if (!isWhitespace(Ptr[0])) return '\\'; |
| 277 | |
| 278 | // See if we have optional whitespace characters followed by a newline. |
| 279 | { |
| 280 | unsigned SizeTmp = 0; |
| 281 | do { |
| 282 | ++SizeTmp; |
| 283 | if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') { |
| 284 | |
| 285 | // If this is a \r\n or \n\r, skip the newlines. |
| 286 | if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') && |
| 287 | Ptr[SizeTmp-1] != Ptr[SizeTmp]) |
| 288 | ++SizeTmp; |
| 289 | |
| 290 | // Found backslash<whitespace><newline>. Parse the char after it. |
| 291 | Size += SizeTmp; |
| 292 | Ptr += SizeTmp; |
| 293 | |
| 294 | // Use slow version to accumulate a correct size field. |
| 295 | return getCharAndSizeSlowNoWarn(Ptr, Size, Features); |
| 296 | } |
| 297 | } while (isWhitespace(Ptr[SizeTmp])); |
| 298 | } |
| 299 | |
| 300 | // Otherwise, this is not an escaped newline, just return the slash. |
| 301 | return '\\'; |
| 302 | } |
| 303 | |
| 304 | // If this is a trigraph, process it. |
| 305 | if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') { |
| 306 | // If this is actually a legal trigraph (not something like "??x"), return |
| 307 | // it. |
| 308 | if (char C = GetTrigraphCharForLetter(Ptr[2])) { |
| 309 | Ptr += 3; |
| 310 | Size += 3; |
| 311 | if (C == '\\') goto Slash; |
| 312 | return C; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | // If this is neither, return a single character. |
| 317 | ++Size; |
| 318 | return *Ptr; |
| 319 | } |
| 320 | |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 321 | //===----------------------------------------------------------------------===// |
| 322 | // Helper methods for lexing. |
| 323 | //===----------------------------------------------------------------------===// |
| 324 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 325 | void Lexer::LexIdentifier(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 326 | // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$] |
| 327 | unsigned Size; |
| 328 | unsigned char C = *CurPtr++; |
| 329 | while (isIdentifierBody(C)) { |
| 330 | C = *CurPtr++; |
| 331 | } |
| 332 | --CurPtr; // Back up over the skipped character. |
| 333 | |
| 334 | // Fast path, no $,\,? in identifier found. '\' might be an escaped newline |
| 335 | // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN. |
| 336 | // FIXME: universal chars. |
| 337 | if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) { |
| 338 | FinishIdentifier: |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 339 | const char *IdStart = BufferPtr, *IdEnd = CurPtr; |
| 340 | FormTokenWithChars(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 341 | Result.SetKind(tok::identifier); |
| 342 | |
| 343 | // Look up this token, see if it is a macro, or if it is a language keyword. |
| 344 | const char *SpelledTokStart, *SpelledTokEnd; |
| 345 | if (!Result.needsCleaning()) { |
| 346 | // No cleaning needed, just use the characters from the lexed buffer. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 347 | SpelledTokStart = IdStart; |
| 348 | SpelledTokEnd = IdEnd; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 349 | } else { |
| 350 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
Chris Lattner | 33ce728 | 2006-06-18 07:35:33 +0000 | [diff] [blame] | 351 | char *TmpBuf = (char*)alloca(Result.getLength()); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 352 | unsigned Size = PP.getSpelling(Result, TmpBuf); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 353 | SpelledTokStart = TmpBuf; |
| 354 | SpelledTokEnd = TmpBuf+Size; |
| 355 | } |
| 356 | |
| 357 | Result.SetIdentifierInfo(PP.getIdentifierInfo(SpelledTokStart, |
| 358 | SpelledTokEnd)); |
| 359 | return PP.HandleIdentifier(Result); |
| 360 | } |
| 361 | |
| 362 | // Otherwise, $,\,? in identifier found. Enter slower path. |
| 363 | |
| 364 | C = getCharAndSize(CurPtr, Size); |
| 365 | while (1) { |
| 366 | if (C == '$') { |
| 367 | // If we hit a $ and they are not supported in identifiers, we are done. |
| 368 | if (!Features.DollarIdents) goto FinishIdentifier; |
| 369 | |
| 370 | // Otherwise, emit a diagnostic and continue. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 371 | Diag(CurPtr, diag::ext_dollar_in_identifier); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 372 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 373 | C = getCharAndSize(CurPtr, Size); |
| 374 | continue; |
| 375 | } else if (!isIdentifierBody(C)) { // FIXME: universal chars. |
| 376 | // Found end of identifier. |
| 377 | goto FinishIdentifier; |
| 378 | } |
| 379 | |
| 380 | // Otherwise, this character is good, consume it. |
| 381 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 382 | |
| 383 | C = getCharAndSize(CurPtr, Size); |
| 384 | while (isIdentifierBody(C)) { // FIXME: universal chars. |
| 385 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 386 | C = getCharAndSize(CurPtr, Size); |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | |
| 392 | /// LexNumericConstant - Lex the remainer of a integer or floating point |
| 393 | /// constant. From[-1] is the first character lexed. Return the end of the |
| 394 | /// constant. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 395 | void Lexer::LexNumericConstant(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 396 | unsigned Size; |
| 397 | char C = getCharAndSize(CurPtr, Size); |
| 398 | char PrevCh = 0; |
| 399 | while (isNumberBody(C)) { // FIXME: universal chars? |
| 400 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 401 | PrevCh = C; |
| 402 | C = getCharAndSize(CurPtr, Size); |
| 403 | } |
| 404 | |
| 405 | // If we fell out, check for a sign, due to 1e+12. If we have one, continue. |
| 406 | if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e')) |
| 407 | return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); |
| 408 | |
| 409 | // If we have a hex FP constant, continue. |
| 410 | if (Features.HexFloats && |
| 411 | (C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p')) |
| 412 | return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); |
| 413 | |
| 414 | Result.SetKind(tok::numeric_constant); |
| 415 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 416 | // Update the location of token as well as BufferPtr. |
| 417 | FormTokenWithChars(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | /// LexStringLiteral - Lex the remainder of a string literal, after having lexed |
| 421 | /// either " or L". |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 422 | void Lexer::LexStringLiteral(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 423 | const char *NulCharacter = 0; // Does this string contain the \0 character? |
| 424 | |
| 425 | char C = getAndAdvanceChar(CurPtr, Result); |
| 426 | while (C != '"') { |
| 427 | // Skip escaped characters. |
| 428 | if (C == '\\') { |
| 429 | // Skip the escaped character. |
| 430 | C = getAndAdvanceChar(CurPtr, Result); |
| 431 | } else if (C == '\n' || C == '\r' || // Newline. |
| 432 | (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 433 | Diag(BufferPtr, diag::err_unterminated_string); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 434 | BufferPtr = CurPtr-1; |
| 435 | return LexTokenInternal(Result); |
| 436 | } else if (C == 0) { |
| 437 | NulCharacter = CurPtr-1; |
| 438 | } |
| 439 | C = getAndAdvanceChar(CurPtr, Result); |
| 440 | } |
| 441 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 442 | if (NulCharacter) Diag(NulCharacter, diag::null_in_string); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 443 | |
| 444 | Result.SetKind(tok::string_literal); |
| 445 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 446 | // Update the location of the token as well as the BufferPtr instance var. |
| 447 | FormTokenWithChars(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | /// LexAngledStringLiteral - Lex the remainder of an angled string literal, |
| 451 | /// after having lexed the '<' character. This is used for #include filenames. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 452 | void Lexer::LexAngledStringLiteral(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 453 | const char *NulCharacter = 0; // Does this string contain the \0 character? |
| 454 | |
| 455 | char C = getAndAdvanceChar(CurPtr, Result); |
| 456 | while (C != '>') { |
| 457 | // Skip escaped characters. |
| 458 | if (C == '\\') { |
| 459 | // Skip the escaped character. |
| 460 | C = getAndAdvanceChar(CurPtr, Result); |
| 461 | } else if (C == '\n' || C == '\r' || // Newline. |
| 462 | (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 463 | Diag(BufferPtr, diag::err_unterminated_string); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 464 | BufferPtr = CurPtr-1; |
| 465 | return LexTokenInternal(Result); |
| 466 | } else if (C == 0) { |
| 467 | NulCharacter = CurPtr-1; |
| 468 | } |
| 469 | C = getAndAdvanceChar(CurPtr, Result); |
| 470 | } |
| 471 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 472 | if (NulCharacter) Diag(NulCharacter, diag::null_in_string); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 473 | |
| 474 | Result.SetKind(tok::angle_string_literal); |
| 475 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 476 | // Update the location of token as well as BufferPtr. |
| 477 | FormTokenWithChars(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | |
| 481 | /// LexCharConstant - Lex the remainder of a character constant, after having |
| 482 | /// lexed either ' or L'. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 483 | void Lexer::LexCharConstant(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 484 | const char *NulCharacter = 0; // Does this character contain the \0 character? |
| 485 | |
| 486 | // Handle the common case of 'x' and '\y' efficiently. |
| 487 | char C = getAndAdvanceChar(CurPtr, Result); |
| 488 | if (C == '\'') { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 489 | Diag(BufferPtr, diag::err_empty_character); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 490 | BufferPtr = CurPtr; |
| 491 | return LexTokenInternal(Result); |
| 492 | } else if (C == '\\') { |
| 493 | // Skip the escaped character. |
| 494 | // FIXME: UCN's. |
| 495 | C = getAndAdvanceChar(CurPtr, Result); |
| 496 | } |
| 497 | |
| 498 | if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') { |
| 499 | ++CurPtr; |
| 500 | } else { |
| 501 | // Fall back on generic code for embedded nulls, newlines, wide chars. |
| 502 | do { |
| 503 | // Skip escaped characters. |
| 504 | if (C == '\\') { |
| 505 | // Skip the escaped character. |
| 506 | C = getAndAdvanceChar(CurPtr, Result); |
| 507 | } else if (C == '\n' || C == '\r' || // Newline. |
| 508 | (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 509 | Diag(BufferPtr, diag::err_unterminated_char); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 510 | BufferPtr = CurPtr-1; |
| 511 | return LexTokenInternal(Result); |
| 512 | } else if (C == 0) { |
| 513 | NulCharacter = CurPtr-1; |
| 514 | } |
| 515 | C = getAndAdvanceChar(CurPtr, Result); |
| 516 | } while (C != '\''); |
| 517 | } |
| 518 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 519 | if (NulCharacter) Diag(NulCharacter, diag::null_in_char); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 520 | |
| 521 | Result.SetKind(tok::char_constant); |
| 522 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 523 | // Update the location of token as well as BufferPtr. |
| 524 | FormTokenWithChars(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /// SkipWhitespace - Efficiently skip over a series of whitespace characters. |
| 528 | /// Update BufferPtr to point to the next non-whitespace character and return. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 529 | void Lexer::SkipWhitespace(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 530 | // Whitespace - Skip it, then return the token after the whitespace. |
| 531 | unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently. |
| 532 | while (1) { |
| 533 | // Skip horizontal whitespace very aggressively. |
| 534 | while (isHorizontalWhitespace(Char)) |
| 535 | Char = *++CurPtr; |
| 536 | |
| 537 | // Otherwise if we something other than whitespace, we're done. |
| 538 | if (Char != '\n' && Char != '\r') |
| 539 | break; |
| 540 | |
| 541 | if (ParsingPreprocessorDirective) { |
| 542 | // End of preprocessor directive line, let LexTokenInternal handle this. |
| 543 | BufferPtr = CurPtr; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 544 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 545 | } |
| 546 | |
| 547 | // ok, but handle newline. |
| 548 | // The returned token is at the start of the line. |
| 549 | Result.SetFlag(LexerToken::StartOfLine); |
| 550 | // No leading whitespace seen so far. |
| 551 | Result.ClearFlag(LexerToken::LeadingSpace); |
| 552 | Char = *++CurPtr; |
| 553 | } |
| 554 | |
| 555 | // If this isn't immediately after a newline, there is leading space. |
| 556 | char PrevChar = CurPtr[-1]; |
| 557 | if (PrevChar != '\n' && PrevChar != '\r') |
| 558 | Result.SetFlag(LexerToken::LeadingSpace); |
| 559 | |
| 560 | // If the next token is obviously a // or /* */ comment, skip it efficiently |
| 561 | // too (without going through the big switch stmt). |
| 562 | if (Char == '/' && CurPtr[1] == '/') { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 563 | BufferPtr = CurPtr; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 564 | return SkipBCPLComment(Result, CurPtr+1); |
| 565 | } |
| 566 | if (Char == '/' && CurPtr[1] == '*') { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 567 | BufferPtr = CurPtr; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 568 | return SkipBlockComment(Result, CurPtr+2); |
| 569 | } |
| 570 | BufferPtr = CurPtr; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | // SkipBCPLComment - We have just read the // characters from input. Skip until |
| 574 | // we find the newline character thats terminate the comment. Then update |
| 575 | /// BufferPtr and return. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 576 | void Lexer::SkipBCPLComment(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 577 | // If BCPL comments aren't explicitly enabled for this language, emit an |
| 578 | // extension warning. |
| 579 | if (!Features.BCPLComment) { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 580 | Diag(BufferPtr, diag::ext_bcpl_comment); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 581 | |
| 582 | // Mark them enabled so we only emit one warning for this translation |
| 583 | // unit. |
| 584 | Features.BCPLComment = true; |
| 585 | } |
| 586 | |
| 587 | // Scan over the body of the comment. The common case, when scanning, is that |
| 588 | // the comment contains normal ascii characters with nothing interesting in |
| 589 | // them. As such, optimize for this case with the inner loop. |
| 590 | char C; |
| 591 | do { |
| 592 | C = *CurPtr; |
| 593 | // FIXME: just scan for a \n or \r character. If we find a \n character, |
| 594 | // scan backwards, checking to see if it's an escaped newline, like we do |
| 595 | // for block comments. |
| 596 | |
| 597 | // Skip over characters in the fast loop. |
| 598 | while (C != 0 && // Potentially EOF. |
| 599 | C != '\\' && // Potentially escaped newline. |
| 600 | C != '?' && // Potentially trigraph. |
| 601 | C != '\n' && C != '\r') // Newline or DOS-style newline. |
| 602 | C = *++CurPtr; |
| 603 | |
| 604 | // If this is a newline, we're done. |
| 605 | if (C == '\n' || C == '\r') |
| 606 | break; // Found the newline? Break out! |
| 607 | |
| 608 | // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to |
| 609 | // properly decode the character. |
| 610 | const char *OldPtr = CurPtr; |
| 611 | C = getAndAdvanceChar(CurPtr, Result); |
| 612 | |
| 613 | // If we read multiple characters, and one of those characters was a \r or |
| 614 | // \n, then we had an escaped newline within the comment. Emit diagnostic. |
| 615 | if (CurPtr != OldPtr+1) { |
| 616 | for (; OldPtr != CurPtr; ++OldPtr) |
| 617 | if (OldPtr[0] == '\n' || OldPtr[0] == '\r') { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 618 | Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment); |
| 619 | break; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 620 | } |
| 621 | } |
| 622 | |
| 623 | if (CurPtr == BufferEnd+1) goto FoundEOF; |
| 624 | } while (C != '\n' && C != '\r'); |
| 625 | |
| 626 | // Found and did not consume a newline. |
| 627 | |
| 628 | // If we are inside a preprocessor directive and we see the end of line, |
| 629 | // return immediately, so that the lexer can return this as an EOM token. |
| 630 | if (ParsingPreprocessorDirective) { |
| 631 | BufferPtr = CurPtr; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 632 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | // Otherwise, eat the \n character. We don't care if this is a \n\r or |
| 636 | // \r\n sequence. |
| 637 | ++CurPtr; |
| 638 | |
| 639 | // The next returned token is at the start of the line. |
| 640 | Result.SetFlag(LexerToken::StartOfLine); |
| 641 | // No leading whitespace seen so far. |
| 642 | Result.ClearFlag(LexerToken::LeadingSpace); |
| 643 | |
| 644 | // It is common for the tokens immediately after a // comment to be |
| 645 | // whitespace (indentation for the next line). Instead of going through the |
| 646 | // big switch, handle it efficiently now. |
| 647 | if (isWhitespace(*CurPtr)) { |
| 648 | Result.SetFlag(LexerToken::LeadingSpace); |
| 649 | return SkipWhitespace(Result, CurPtr+1); |
| 650 | } |
| 651 | |
| 652 | BufferPtr = CurPtr; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 653 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 654 | |
| 655 | FoundEOF: // If we ran off the end of the buffer, return EOF. |
| 656 | BufferPtr = CurPtr-1; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 657 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 660 | /// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline |
| 661 | /// character (either \n or \r) is part of an escaped newline sequence. Issue a |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 662 | /// diagnostic if so. We know that the is inside of a block comment. |
Chris Lattner | 1f58305 | 2006-06-18 06:53:56 +0000 | [diff] [blame] | 663 | static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr, |
| 664 | Lexer *L) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 665 | assert(CurPtr[0] == '\n' || CurPtr[0] == '\r'); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 666 | |
| 667 | // Back up off the newline. |
| 668 | --CurPtr; |
| 669 | |
| 670 | // If this is a two-character newline sequence, skip the other character. |
| 671 | if (CurPtr[0] == '\n' || CurPtr[0] == '\r') { |
| 672 | // \n\n or \r\r -> not escaped newline. |
| 673 | if (CurPtr[0] == CurPtr[1]) |
| 674 | return false; |
| 675 | // \n\r or \r\n -> skip the newline. |
| 676 | --CurPtr; |
| 677 | } |
| 678 | |
| 679 | // If we have horizontal whitespace, skip over it. We allow whitespace |
| 680 | // between the slash and newline. |
| 681 | bool HasSpace = false; |
| 682 | while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) { |
| 683 | --CurPtr; |
| 684 | HasSpace = true; |
| 685 | } |
| 686 | |
| 687 | // If we have a slash, we know this is an escaped newline. |
| 688 | if (*CurPtr == '\\') { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 689 | if (CurPtr[-1] != '*') return false; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 690 | } else { |
| 691 | // It isn't a slash, is it the ?? / trigraph? |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 692 | if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' || |
| 693 | CurPtr[-3] != '*') |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 694 | return false; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 695 | |
| 696 | // This is the trigraph ending the comment. Emit a stern warning! |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 697 | CurPtr -= 2; |
| 698 | |
| 699 | // If no trigraphs are enabled, warn that we ignored this trigraph and |
| 700 | // ignore this * character. |
Chris Lattner | 1f58305 | 2006-06-18 06:53:56 +0000 | [diff] [blame] | 701 | if (!L->getFeatures().Trigraphs) { |
| 702 | L->Diag(CurPtr, diag::trigraph_ignored_block_comment); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 703 | return false; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 704 | } |
Chris Lattner | 1f58305 | 2006-06-18 06:53:56 +0000 | [diff] [blame] | 705 | L->Diag(CurPtr, diag::trigraph_ends_block_comment); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 706 | } |
| 707 | |
| 708 | // Warn about having an escaped newline between the */ characters. |
Chris Lattner | 1f58305 | 2006-06-18 06:53:56 +0000 | [diff] [blame] | 709 | L->Diag(CurPtr, diag::escaped_newline_block_comment_end); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 710 | |
| 711 | // If there was space between the backslash and newline, warn about it. |
Chris Lattner | 1f58305 | 2006-06-18 06:53:56 +0000 | [diff] [blame] | 712 | if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 713 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 714 | return true; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | /// SkipBlockComment - We have just read the /* characters from input. Read |
| 718 | /// until we find the */ characters that terminate the comment. Note that we |
| 719 | /// don't bother decoding trigraphs or escaped newlines in block comments, |
| 720 | /// because they cannot cause the comment to end. The only thing that can |
| 721 | /// happen is the comment could end with an escaped newline between the */ end |
| 722 | /// of comment. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 723 | void Lexer::SkipBlockComment(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 724 | // Scan one character past where we should, looking for a '/' character. Once |
| 725 | // we find it, check to see if it was preceeded by a *. This common |
| 726 | // optimization helps people who like to put a lot of * characters in their |
| 727 | // comments. |
| 728 | unsigned char C = *CurPtr++; |
| 729 | if (C == 0 && CurPtr == BufferEnd+1) { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 730 | Diag(BufferPtr, diag::err_unterminated_block_comment); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 731 | BufferPtr = CurPtr-1; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 732 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | while (1) { |
| 736 | // Skip over all non-interesting characters. |
| 737 | // TODO: Vectorize this. Note: memchr on Darwin is slower than this loop. |
| 738 | while (C != '/' && C != '\0') |
| 739 | C = *CurPtr++; |
| 740 | |
| 741 | if (C == '/') { |
| 742 | char T; |
| 743 | if (CurPtr[-2] == '*') // We found the final */. We're done! |
| 744 | break; |
| 745 | |
| 746 | if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) { |
Chris Lattner | 1f58305 | 2006-06-18 06:53:56 +0000 | [diff] [blame] | 747 | if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 748 | // We found the final */, though it had an escaped newline between the |
| 749 | // * and /. We're done! |
| 750 | break; |
| 751 | } |
| 752 | } |
| 753 | if (CurPtr[0] == '*' && CurPtr[1] != '/') { |
| 754 | // If this is a /* inside of the comment, emit a warning. Don't do this |
| 755 | // if this is a /*/, which will end the comment. This misses cases with |
| 756 | // embedded escaped newlines, but oh well. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 757 | Diag(CurPtr-1, diag::nested_block_comment); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 758 | } |
| 759 | } else if (C == 0 && CurPtr == BufferEnd+1) { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 760 | Diag(BufferPtr, diag::err_unterminated_block_comment); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 761 | // Note: the user probably forgot a */. We could continue immediately |
| 762 | // after the /*, but this would involve lexing a lot of what really is the |
| 763 | // comment, which surely would confuse the parser. |
| 764 | BufferPtr = CurPtr-1; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 765 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 766 | } |
| 767 | C = *CurPtr++; |
| 768 | } |
| 769 | |
| 770 | // It is common for the tokens immediately after a /**/ comment to be |
| 771 | // whitespace. Instead of going through the big switch, handle it |
| 772 | // efficiently now. |
| 773 | if (isHorizontalWhitespace(*CurPtr)) { |
| 774 | Result.SetFlag(LexerToken::LeadingSpace); |
| 775 | return SkipWhitespace(Result, CurPtr+1); |
| 776 | } |
| 777 | |
| 778 | // Otherwise, just return so that the next character will be lexed as a token. |
| 779 | BufferPtr = CurPtr; |
| 780 | Result.SetFlag(LexerToken::LeadingSpace); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | //===----------------------------------------------------------------------===// |
| 784 | // Primary Lexing Entry Points |
| 785 | //===----------------------------------------------------------------------===// |
| 786 | |
| 787 | /// LexIncludeFilename - After the preprocessor has parsed a #include, lex and |
| 788 | /// (potentially) macro expand the filename. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 789 | void Lexer::LexIncludeFilename(LexerToken &Result) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 790 | assert(ParsingPreprocessorDirective && |
| 791 | ParsingFilename == false && |
| 792 | "Must be in a preprocessing directive!"); |
| 793 | |
| 794 | // We are now parsing a filename! |
| 795 | ParsingFilename = true; |
| 796 | |
| 797 | // There should be exactly two tokens here if everything is good: first the |
| 798 | // filename, then the EOM. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 799 | Lex(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 800 | |
| 801 | // We should have gotten the filename now. |
| 802 | ParsingFilename = false; |
| 803 | |
| 804 | // No filename? |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 805 | if (Result.getKind() == tok::eom) { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 806 | PP.Diag(Result, diag::err_pp_expects_filename); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 807 | return; |
| 808 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 809 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 810 | // Verify that there is nothing after the filename, other than EOM. Use the |
| 811 | // preprocessor to lex this in case lexing the filename entered a macro. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 812 | LexerToken EndTok; |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 813 | PP.Lex(EndTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 814 | |
| 815 | if (EndTok.getKind() != tok::eom) { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 816 | PP.Diag(EndTok, diag::ext_pp_extra_tokens_at_eol, "#include"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 817 | |
| 818 | // Lex until the end of the preprocessor directive line. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 819 | while (EndTok.getKind() != tok::eom) |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 820 | PP.Lex(EndTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 821 | |
| 822 | Result.SetKind(tok::eom); |
| 823 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 824 | } |
| 825 | |
| 826 | /// ReadToEndOfLine - Read the rest of the current preprocessor line as an |
| 827 | /// uninterpreted string. This switches the lexer out of directive mode. |
| 828 | std::string Lexer::ReadToEndOfLine() { |
| 829 | assert(ParsingPreprocessorDirective && ParsingFilename == false && |
| 830 | "Must be in a preprocessing directive!"); |
| 831 | std::string Result; |
| 832 | LexerToken Tmp; |
| 833 | |
| 834 | // CurPtr - Cache BufferPtr in an automatic variable. |
| 835 | const char *CurPtr = BufferPtr; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 836 | while (1) { |
| 837 | char Char = getAndAdvanceChar(CurPtr, Tmp); |
| 838 | switch (Char) { |
| 839 | default: |
| 840 | Result += Char; |
| 841 | break; |
| 842 | case 0: // Null. |
| 843 | // Found end of file? |
| 844 | if (CurPtr-1 != BufferEnd) { |
| 845 | // Nope, normal character, continue. |
| 846 | Result += Char; |
| 847 | break; |
| 848 | } |
| 849 | // FALL THROUGH. |
| 850 | case '\r': |
| 851 | case '\n': |
| 852 | // Okay, we found the end of the line. First, back up past the \0, \r, \n. |
| 853 | assert(CurPtr[-1] == Char && "Trigraphs for newline?"); |
| 854 | BufferPtr = CurPtr-1; |
| 855 | |
| 856 | // Next, lex the character, which should handle the EOM transition. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 857 | Lex(Tmp); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 858 | assert(Tmp.getKind() == tok::eom && "Unexpected token!"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 859 | |
| 860 | // Finally, we're done, return the string we found. |
| 861 | return Result; |
| 862 | } |
| 863 | } |
| 864 | } |
| 865 | |
| 866 | /// LexEndOfFile - CurPtr points to the end of this file. Handle this |
| 867 | /// condition, reporting diagnostics and handling other edge cases as required. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 868 | void Lexer::LexEndOfFile(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 869 | // If we hit the end of the file while parsing a preprocessor directive, |
| 870 | // end the preprocessor directive first. The next token returned will |
| 871 | // then be the end of file. |
| 872 | if (ParsingPreprocessorDirective) { |
| 873 | // Done parsing the "line". |
| 874 | ParsingPreprocessorDirective = false; |
| 875 | Result.SetKind(tok::eom); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 876 | // Update the location of token as well as BufferPtr. |
| 877 | FormTokenWithChars(Result, CurPtr); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 878 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | // If we are in a #if directive, emit an error. |
| 882 | while (!ConditionalStack.empty()) { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 883 | PP.Diag(ConditionalStack.back().IfLoc, |
| 884 | diag::err_pp_unterminated_conditional); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 885 | ConditionalStack.pop_back(); |
| 886 | } |
| 887 | |
| 888 | // If the file was empty or didn't end in a newline, issue a pedwarn. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 889 | if (CurPtr[-1] != '\n' && CurPtr[-1] != '\r') |
| 890 | Diag(BufferEnd, diag::ext_no_newline_eof); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 891 | |
| 892 | BufferPtr = CurPtr; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 893 | PP.HandleEndOfFile(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | |
| 897 | /// LexTokenInternal - This implements a simple C family lexer. It is an |
| 898 | /// extremely performance critical piece of code. This assumes that the buffer |
| 899 | /// has a null character at the end of the file. Return true if an error |
| 900 | /// occurred and compilation should terminate, false if normal. This returns a |
| 901 | /// preprocessing token, not a normal token, as such, it is an internal |
| 902 | /// interface. It assumes that the Flags of result have been cleared before |
| 903 | /// calling this. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 904 | void Lexer::LexTokenInternal(LexerToken &Result) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 905 | LexNextToken: |
| 906 | // New token, can't need cleaning yet. |
| 907 | Result.ClearFlag(LexerToken::NeedsCleaning); |
| 908 | |
| 909 | // CurPtr - Cache BufferPtr in an automatic variable. |
| 910 | const char *CurPtr = BufferPtr; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 911 | |
| 912 | unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below. |
| 913 | |
| 914 | // Read a character, advancing over it. |
| 915 | char Char = getAndAdvanceChar(CurPtr, Result); |
| 916 | switch (Char) { |
| 917 | case 0: // Null. |
| 918 | // Found end of file? |
| 919 | if (CurPtr-1 == BufferEnd) |
| 920 | return LexEndOfFile(Result, CurPtr-1); // Retreat back into the file. |
| 921 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 922 | Diag(CurPtr-1, diag::null_in_file); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 923 | Result.SetFlag(LexerToken::LeadingSpace); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 924 | SkipWhitespace(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 925 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 926 | case '\n': |
| 927 | case '\r': |
| 928 | // If we are inside a preprocessor directive and we see the end of line, |
| 929 | // we know we are done with the directive, so return an EOM token. |
| 930 | if (ParsingPreprocessorDirective) { |
| 931 | // Done parsing the "line". |
| 932 | ParsingPreprocessorDirective = false; |
| 933 | |
| 934 | // Since we consumed a newline, we are back at the start of a line. |
| 935 | IsAtStartOfLine = true; |
| 936 | |
| 937 | Result.SetKind(tok::eom); |
| 938 | break; |
| 939 | } |
| 940 | // The returned token is at the start of the line. |
| 941 | Result.SetFlag(LexerToken::StartOfLine); |
| 942 | // No leading whitespace seen so far. |
| 943 | Result.ClearFlag(LexerToken::LeadingSpace); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 944 | SkipWhitespace(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 945 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 946 | case ' ': |
| 947 | case '\t': |
| 948 | case '\f': |
| 949 | case '\v': |
| 950 | Result.SetFlag(LexerToken::LeadingSpace); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 951 | SkipWhitespace(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 952 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 953 | |
| 954 | case 'L': |
| 955 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 956 | |
| 957 | // Wide string literal. |
| 958 | if (Char == '"') |
| 959 | return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result)); |
| 960 | |
| 961 | // Wide character constant. |
| 962 | if (Char == '\'') |
| 963 | return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); |
| 964 | // FALL THROUGH, treating L like the start of an identifier. |
| 965 | |
| 966 | // C99 6.4.2: Identifiers. |
| 967 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': |
| 968 | case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N': |
| 969 | case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': |
| 970 | case 'V': case 'W': case 'X': case 'Y': case 'Z': |
| 971 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': |
| 972 | case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': |
| 973 | case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': |
| 974 | case 'v': case 'w': case 'x': case 'y': case 'z': |
| 975 | case '_': |
| 976 | return LexIdentifier(Result, CurPtr); |
| 977 | |
| 978 | // C99 6.4.4.1: Integer Constants. |
| 979 | // C99 6.4.4.2: Floating Constants. |
| 980 | case '0': case '1': case '2': case '3': case '4': |
| 981 | case '5': case '6': case '7': case '8': case '9': |
| 982 | return LexNumericConstant(Result, CurPtr); |
| 983 | |
| 984 | // C99 6.4.4: Character Constants. |
| 985 | case '\'': |
| 986 | return LexCharConstant(Result, CurPtr); |
| 987 | |
| 988 | // C99 6.4.5: String Literals. |
| 989 | case '"': |
| 990 | return LexStringLiteral(Result, CurPtr); |
| 991 | |
| 992 | // C99 6.4.6: Punctuators. |
| 993 | case '?': |
| 994 | Result.SetKind(tok::question); |
| 995 | break; |
| 996 | case '[': |
| 997 | Result.SetKind(tok::l_square); |
| 998 | break; |
| 999 | case ']': |
| 1000 | Result.SetKind(tok::r_square); |
| 1001 | break; |
| 1002 | case '(': |
| 1003 | Result.SetKind(tok::l_paren); |
| 1004 | break; |
| 1005 | case ')': |
| 1006 | Result.SetKind(tok::r_paren); |
| 1007 | break; |
| 1008 | case '{': |
| 1009 | Result.SetKind(tok::l_brace); |
| 1010 | break; |
| 1011 | case '}': |
| 1012 | Result.SetKind(tok::r_brace); |
| 1013 | break; |
| 1014 | case '.': |
| 1015 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1016 | if (Char >= '0' && Char <= '9') { |
| 1017 | return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); |
| 1018 | } else if (Features.CPlusPlus && Char == '*') { |
| 1019 | Result.SetKind(tok::periodstar); |
| 1020 | CurPtr += SizeTmp; |
| 1021 | } else if (Char == '.' && |
| 1022 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') { |
| 1023 | Result.SetKind(tok::ellipsis); |
| 1024 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 1025 | SizeTmp2, Result); |
| 1026 | } else { |
| 1027 | Result.SetKind(tok::period); |
| 1028 | } |
| 1029 | break; |
| 1030 | case '&': |
| 1031 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1032 | if (Char == '&') { |
| 1033 | Result.SetKind(tok::ampamp); |
| 1034 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1035 | } else if (Char == '=') { |
| 1036 | Result.SetKind(tok::ampequal); |
| 1037 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1038 | } else { |
| 1039 | Result.SetKind(tok::amp); |
| 1040 | } |
| 1041 | break; |
| 1042 | case '*': |
| 1043 | if (getCharAndSize(CurPtr, SizeTmp) == '=') { |
| 1044 | Result.SetKind(tok::starequal); |
| 1045 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1046 | } else { |
| 1047 | Result.SetKind(tok::star); |
| 1048 | } |
| 1049 | break; |
| 1050 | case '+': |
| 1051 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1052 | if (Char == '+') { |
| 1053 | Result.SetKind(tok::plusplus); |
| 1054 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1055 | } else if (Char == '=') { |
| 1056 | Result.SetKind(tok::plusequal); |
| 1057 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1058 | } else { |
| 1059 | Result.SetKind(tok::plus); |
| 1060 | } |
| 1061 | break; |
| 1062 | case '-': |
| 1063 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1064 | if (Char == '-') { |
| 1065 | Result.SetKind(tok::minusminus); |
| 1066 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1067 | } else if (Char == '>' && Features.CPlusPlus && |
| 1068 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { |
| 1069 | Result.SetKind(tok::arrowstar); // C++ ->* |
| 1070 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 1071 | SizeTmp2, Result); |
| 1072 | } else if (Char == '>') { |
| 1073 | Result.SetKind(tok::arrow); |
| 1074 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1075 | } else if (Char == '=') { |
| 1076 | Result.SetKind(tok::minusequal); |
| 1077 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1078 | } else { |
| 1079 | Result.SetKind(tok::minus); |
| 1080 | } |
| 1081 | break; |
| 1082 | case '~': |
| 1083 | Result.SetKind(tok::tilde); |
| 1084 | break; |
| 1085 | case '!': |
| 1086 | if (getCharAndSize(CurPtr, SizeTmp) == '=') { |
| 1087 | Result.SetKind(tok::exclaimequal); |
| 1088 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1089 | } else { |
| 1090 | Result.SetKind(tok::exclaim); |
| 1091 | } |
| 1092 | break; |
| 1093 | case '/': |
| 1094 | // 6.4.9: Comments |
| 1095 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1096 | if (Char == '/') { // BCPL comment. |
| 1097 | Result.SetFlag(LexerToken::LeadingSpace); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1098 | SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1099 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 1100 | } else if (Char == '*') { // /**/ comment. |
| 1101 | Result.SetFlag(LexerToken::LeadingSpace); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1102 | SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1103 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 1104 | } else if (Char == '=') { |
| 1105 | Result.SetKind(tok::slashequal); |
| 1106 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1107 | } else { |
| 1108 | Result.SetKind(tok::slash); |
| 1109 | } |
| 1110 | break; |
| 1111 | case '%': |
| 1112 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1113 | if (Char == '=') { |
| 1114 | Result.SetKind(tok::percentequal); |
| 1115 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1116 | } else if (Features.Digraphs && Char == '>') { |
| 1117 | Result.SetKind(tok::r_brace); // '%>' -> '}' |
| 1118 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1119 | } else if (Features.Digraphs && Char == ':') { |
| 1120 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1121 | if (getCharAndSize(CurPtr, SizeTmp) == '%' && |
| 1122 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') { |
| 1123 | Result.SetKind(tok::hashhash); // '%:%:' -> '##' |
| 1124 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 1125 | SizeTmp2, Result); |
| 1126 | } else { |
| 1127 | Result.SetKind(tok::hash); // '%:' -> '#' |
| 1128 | |
| 1129 | // We parsed a # character. If this occurs at the start of the line, |
| 1130 | // it's actually the start of a preprocessing directive. Callback to |
| 1131 | // the preprocessor to handle it. |
| 1132 | // FIXME: -fpreprocessed mode?? |
| 1133 | if (Result.isAtStartOfLine() && !PP.isSkipping()) { |
| 1134 | BufferPtr = CurPtr; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1135 | PP.HandleDirective(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1136 | |
| 1137 | // As an optimization, if the preprocessor didn't switch lexers, tail |
| 1138 | // recurse. |
| 1139 | if (PP.isCurrentLexer(this)) { |
| 1140 | // Start a new token. If this is a #include or something, the PP may |
| 1141 | // want us starting at the beginning of the line again. If so, set |
| 1142 | // the StartOfLine flag. |
| 1143 | if (IsAtStartOfLine) { |
| 1144 | Result.SetFlag(LexerToken::StartOfLine); |
| 1145 | IsAtStartOfLine = false; |
| 1146 | } |
| 1147 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 1148 | } |
| 1149 | |
| 1150 | return PP.Lex(Result); |
| 1151 | } |
| 1152 | } |
| 1153 | } else { |
| 1154 | Result.SetKind(tok::percent); |
| 1155 | } |
| 1156 | break; |
| 1157 | case '<': |
| 1158 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1159 | if (ParsingFilename) { |
| 1160 | return LexAngledStringLiteral(Result, CurPtr+SizeTmp); |
| 1161 | } else if (Char == '<' && |
| 1162 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') { |
| 1163 | Result.SetKind(tok::lesslessequal); |
| 1164 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 1165 | SizeTmp2, Result); |
| 1166 | } else if (Char == '<') { |
| 1167 | Result.SetKind(tok::lessless); |
| 1168 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1169 | } else if (Char == '=') { |
| 1170 | Result.SetKind(tok::lessequal); |
| 1171 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1172 | } else if (Features.Digraphs && Char == ':') { |
| 1173 | Result.SetKind(tok::l_square); // '<:' -> '[' |
| 1174 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1175 | } else if (Features.Digraphs && Char == '>') { |
| 1176 | Result.SetKind(tok::l_brace); // '<%' -> '{' |
| 1177 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1178 | } else if (Features.CPPMinMax && Char == '?') { // <? |
| 1179 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 1180 | Diag(BufferPtr, diag::min_max_deprecated); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1181 | |
| 1182 | if (getCharAndSize(CurPtr, SizeTmp) == '=') { // <?= |
| 1183 | Result.SetKind(tok::lessquestionequal); |
| 1184 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1185 | } else { |
| 1186 | Result.SetKind(tok::lessquestion); |
| 1187 | } |
| 1188 | } else { |
| 1189 | Result.SetKind(tok::less); |
| 1190 | } |
| 1191 | break; |
| 1192 | case '>': |
| 1193 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1194 | if (Char == '=') { |
| 1195 | Result.SetKind(tok::greaterequal); |
| 1196 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1197 | } else if (Char == '>' && |
| 1198 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') { |
| 1199 | Result.SetKind(tok::greatergreaterequal); |
| 1200 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 1201 | SizeTmp2, Result); |
| 1202 | } else if (Char == '>') { |
| 1203 | Result.SetKind(tok::greatergreater); |
| 1204 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1205 | } else if (Features.CPPMinMax && Char == '?') { |
| 1206 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 1207 | Diag(BufferPtr, diag::min_max_deprecated); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1208 | |
| 1209 | if (getCharAndSize(CurPtr, SizeTmp) == '=') { |
| 1210 | Result.SetKind(tok::greaterquestionequal); // >?= |
| 1211 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1212 | } else { |
| 1213 | Result.SetKind(tok::greaterquestion); // >? |
| 1214 | } |
| 1215 | } else { |
| 1216 | Result.SetKind(tok::greater); |
| 1217 | } |
| 1218 | break; |
| 1219 | case '^': |
| 1220 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1221 | if (Char == '=') { |
| 1222 | Result.SetKind(tok::caretequal); |
| 1223 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1224 | } else { |
| 1225 | Result.SetKind(tok::caret); |
| 1226 | } |
| 1227 | break; |
| 1228 | case '|': |
| 1229 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1230 | if (Char == '=') { |
| 1231 | Result.SetKind(tok::pipeequal); |
| 1232 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1233 | } else if (Char == '|') { |
| 1234 | Result.SetKind(tok::pipepipe); |
| 1235 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1236 | } else { |
| 1237 | Result.SetKind(tok::pipe); |
| 1238 | } |
| 1239 | break; |
| 1240 | case ':': |
| 1241 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1242 | if (Features.Digraphs && Char == '>') { |
| 1243 | Result.SetKind(tok::r_square); // ':>' -> ']' |
| 1244 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1245 | } else if (Features.CPlusPlus && Char == ':') { |
| 1246 | Result.SetKind(tok::coloncolon); |
| 1247 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1248 | } else { |
| 1249 | Result.SetKind(tok::colon); |
| 1250 | } |
| 1251 | break; |
| 1252 | case ';': |
| 1253 | Result.SetKind(tok::semi); |
| 1254 | break; |
| 1255 | case '=': |
| 1256 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1257 | if (Char == '=') { |
| 1258 | Result.SetKind(tok::equalequal); |
| 1259 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1260 | } else { |
| 1261 | Result.SetKind(tok::equal); |
| 1262 | } |
| 1263 | break; |
| 1264 | case ',': |
| 1265 | Result.SetKind(tok::comma); |
| 1266 | break; |
| 1267 | case '#': |
| 1268 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1269 | if (Char == '#') { |
| 1270 | Result.SetKind(tok::hashhash); |
| 1271 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1272 | } else { |
| 1273 | Result.SetKind(tok::hash); |
| 1274 | // We parsed a # character. If this occurs at the start of the line, |
| 1275 | // it's actually the start of a preprocessing directive. Callback to |
| 1276 | // the preprocessor to handle it. |
| 1277 | // FIXME: not in preprocessed mode?? |
| 1278 | if (Result.isAtStartOfLine() && !PP.isSkipping()) { |
| 1279 | BufferPtr = CurPtr; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1280 | PP.HandleDirective(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1281 | |
| 1282 | // As an optimization, if the preprocessor didn't switch lexers, tail |
| 1283 | // recurse. |
| 1284 | if (PP.isCurrentLexer(this)) { |
| 1285 | // Start a new token. If this is a #include or something, the PP may |
| 1286 | // want us starting at the beginning of the line again. If so, set |
| 1287 | // the StartOfLine flag. |
| 1288 | if (IsAtStartOfLine) { |
| 1289 | Result.SetFlag(LexerToken::StartOfLine); |
| 1290 | IsAtStartOfLine = false; |
| 1291 | } |
| 1292 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 1293 | } |
| 1294 | return PP.Lex(Result); |
| 1295 | } |
| 1296 | } |
| 1297 | break; |
| 1298 | |
| 1299 | case '\\': |
| 1300 | // FIXME: handle UCN's. |
| 1301 | // FALL THROUGH. |
| 1302 | default: |
| 1303 | // Objective C support. |
| 1304 | if (CurPtr[-1] == '@' && Features.ObjC1) { |
| 1305 | Result.SetKind(tok::at); |
| 1306 | break; |
| 1307 | } else if (CurPtr[-1] == '$' && Features.DollarIdents) {// $ in identifiers. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1308 | Diag(CurPtr-1, diag::ext_dollar_in_identifier); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1309 | return LexIdentifier(Result, CurPtr); |
| 1310 | } |
| 1311 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1312 | if (!PP.isSkipping()) Diag(CurPtr-1, diag::err_stray_character); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1313 | BufferPtr = CurPtr; |
| 1314 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 1315 | } |
| 1316 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame^] | 1317 | // Update the location of token as well as BufferPtr. |
| 1318 | FormTokenWithChars(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1319 | } |