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: |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 22 | // TODO: Options to support: |
| 23 | // -fexec-charset,-fwide-exec-charset |
| 24 | // |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | #include "clang/Lex/Lexer.h" |
| 28 | #include "clang/Lex/Preprocessor.h" |
| 29 | #include "clang/Basic/Diagnostic.h" |
| 30 | #include "clang/Basic/SourceBuffer.h" |
| 31 | #include "clang/Basic/SourceLocation.h" |
| 32 | #include "llvm/Config/alloca.h" |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 33 | #include <cctype> |
| 34 | #include <iostream> |
| 35 | using namespace llvm; |
| 36 | using namespace clang; |
| 37 | |
| 38 | static void InitCharacterInfo(); |
| 39 | |
Chris Lattner | 4cca5ba | 2006-07-02 20:05:54 +0000 | [diff] [blame] | 40 | Lexer::Lexer(const SourceBuffer *File, unsigned fileid, Preprocessor &pp, |
| 41 | const char *BufStart, const char *BufEnd) |
| 42 | : BufferPtr(BufStart ? BufStart : File->getBufferStart()), |
Chris Lattner | 4cca5ba | 2006-07-02 20:05:54 +0000 | [diff] [blame] | 43 | BufferEnd(BufEnd ? BufEnd : File->getBufferEnd()), |
| 44 | InputFile(File), CurFileID(fileid), PP(pp), Features(PP.getLangOptions()) { |
Chris Lattner | ecfeafe | 2006-07-02 21:26:45 +0000 | [diff] [blame] | 45 | Is_PragmaLexer = false; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 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 { |
Chris Lattner | 8bbfe46 | 2006-07-02 22:27:49 +0000 | [diff] [blame] | 132 | assert(Loc >= InputFile->getBufferStart() && Loc <= BufferEnd && |
Chris Lattner | 4cca5ba | 2006-07-02 20:05:54 +0000 | [diff] [blame] | 133 | "Location out of range for this buffer!"); |
Chris Lattner | 8bbfe46 | 2006-07-02 22:27:49 +0000 | [diff] [blame] | 134 | return SourceLocation(CurFileID, Loc-InputFile->getBufferStart()); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 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. |
Chris Lattner | 505c547 | 2006-07-03 00:55:48 +0000 | [diff] [blame^] | 191 | /// 3. If this is a UCN, return it. FIXME: C++ UCN's? |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 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. |
Chris Lattner | 505c547 | 2006-07-03 00:55:48 +0000 | [diff] [blame^] | 336 | // FIXME: UCNs. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 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. |
Chris Lattner | c5a0006 | 2006-06-18 16:41:01 +0000 | [diff] [blame] | 344 | IdentifierTokenInfo *II; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 345 | if (!Result.needsCleaning()) { |
| 346 | // No cleaning needed, just use the characters from the lexed buffer. |
Chris Lattner | c5a0006 | 2006-06-18 16:41:01 +0000 | [diff] [blame] | 347 | II = PP.getIdentifierInfo(IdStart, IdEnd); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 348 | } else { |
| 349 | // Cleaning needed, alloca a buffer, clean into it, then use the buffer. |
Chris Lattner | 33ce728 | 2006-06-18 07:35:33 +0000 | [diff] [blame] | 350 | char *TmpBuf = (char*)alloca(Result.getLength()); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 351 | unsigned Size = PP.getSpelling(Result, TmpBuf); |
Chris Lattner | c5a0006 | 2006-06-18 16:41:01 +0000 | [diff] [blame] | 352 | II = PP.getIdentifierInfo(TmpBuf, TmpBuf+Size); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 353 | } |
Chris Lattner | c5a0006 | 2006-06-18 16:41:01 +0000 | [diff] [blame] | 354 | Result.SetIdentifierInfo(II); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 355 | |
Chris Lattner | c5a0006 | 2006-06-18 16:41:01 +0000 | [diff] [blame] | 356 | // Finally, now that we know we have an identifier, pass this off to the |
| 357 | // preprocessor, which may macro expand it or something. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 358 | return PP.HandleIdentifier(Result); |
| 359 | } |
| 360 | |
| 361 | // Otherwise, $,\,? in identifier found. Enter slower path. |
| 362 | |
| 363 | C = getCharAndSize(CurPtr, Size); |
| 364 | while (1) { |
| 365 | if (C == '$') { |
| 366 | // If we hit a $ and they are not supported in identifiers, we are done. |
| 367 | if (!Features.DollarIdents) goto FinishIdentifier; |
| 368 | |
| 369 | // Otherwise, emit a diagnostic and continue. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 370 | Diag(CurPtr, diag::ext_dollar_in_identifier); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 371 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 372 | C = getCharAndSize(CurPtr, Size); |
| 373 | continue; |
Chris Lattner | 505c547 | 2006-07-03 00:55:48 +0000 | [diff] [blame^] | 374 | } else if (!isIdentifierBody(C)) { // FIXME: UCNs. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 375 | // Found end of identifier. |
| 376 | goto FinishIdentifier; |
| 377 | } |
| 378 | |
| 379 | // Otherwise, this character is good, consume it. |
| 380 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 381 | |
| 382 | C = getCharAndSize(CurPtr, Size); |
Chris Lattner | 505c547 | 2006-07-03 00:55:48 +0000 | [diff] [blame^] | 383 | while (isIdentifierBody(C)) { // FIXME: UCNs. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 384 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 385 | C = getCharAndSize(CurPtr, Size); |
| 386 | } |
| 387 | } |
| 388 | } |
| 389 | |
| 390 | |
| 391 | /// LexNumericConstant - Lex the remainer of a integer or floating point |
| 392 | /// constant. From[-1] is the first character lexed. Return the end of the |
| 393 | /// constant. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 394 | void Lexer::LexNumericConstant(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 395 | unsigned Size; |
| 396 | char C = getCharAndSize(CurPtr, Size); |
| 397 | char PrevCh = 0; |
Chris Lattner | 505c547 | 2006-07-03 00:55:48 +0000 | [diff] [blame^] | 398 | while (isNumberBody(C)) { // FIXME: UCNs? |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 399 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 400 | PrevCh = C; |
| 401 | C = getCharAndSize(CurPtr, Size); |
| 402 | } |
| 403 | |
| 404 | // If we fell out, check for a sign, due to 1e+12. If we have one, continue. |
| 405 | if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e')) |
| 406 | return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); |
| 407 | |
| 408 | // If we have a hex FP constant, continue. |
| 409 | if (Features.HexFloats && |
| 410 | (C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p')) |
| 411 | return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); |
| 412 | |
| 413 | Result.SetKind(tok::numeric_constant); |
| 414 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 415 | // Update the location of token as well as BufferPtr. |
| 416 | FormTokenWithChars(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | /// LexStringLiteral - Lex the remainder of a string literal, after having lexed |
| 420 | /// either " or L". |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 421 | void Lexer::LexStringLiteral(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 422 | const char *NulCharacter = 0; // Does this string contain the \0 character? |
| 423 | |
| 424 | char C = getAndAdvanceChar(CurPtr, Result); |
| 425 | while (C != '"') { |
| 426 | // Skip escaped characters. |
| 427 | if (C == '\\') { |
| 428 | // Skip the escaped character. |
| 429 | C = getAndAdvanceChar(CurPtr, Result); |
| 430 | } else if (C == '\n' || C == '\r' || // Newline. |
| 431 | (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 432 | Diag(BufferPtr, diag::err_unterminated_string); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 433 | BufferPtr = CurPtr-1; |
| 434 | return LexTokenInternal(Result); |
| 435 | } else if (C == 0) { |
| 436 | NulCharacter = CurPtr-1; |
| 437 | } |
| 438 | C = getAndAdvanceChar(CurPtr, Result); |
| 439 | } |
| 440 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 441 | if (NulCharacter) Diag(NulCharacter, diag::null_in_string); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 442 | |
| 443 | Result.SetKind(tok::string_literal); |
| 444 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 445 | // Update the location of the token as well as the BufferPtr instance var. |
| 446 | FormTokenWithChars(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 447 | } |
| 448 | |
| 449 | /// LexAngledStringLiteral - Lex the remainder of an angled string literal, |
| 450 | /// after having lexed the '<' character. This is used for #include filenames. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 451 | void Lexer::LexAngledStringLiteral(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 452 | const char *NulCharacter = 0; // Does this string contain the \0 character? |
| 453 | |
| 454 | char C = getAndAdvanceChar(CurPtr, Result); |
| 455 | while (C != '>') { |
| 456 | // Skip escaped characters. |
| 457 | if (C == '\\') { |
| 458 | // Skip the escaped character. |
| 459 | C = getAndAdvanceChar(CurPtr, Result); |
| 460 | } else if (C == '\n' || C == '\r' || // Newline. |
| 461 | (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 462 | Diag(BufferPtr, diag::err_unterminated_string); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 463 | BufferPtr = CurPtr-1; |
| 464 | return LexTokenInternal(Result); |
| 465 | } else if (C == 0) { |
| 466 | NulCharacter = CurPtr-1; |
| 467 | } |
| 468 | C = getAndAdvanceChar(CurPtr, Result); |
| 469 | } |
| 470 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 471 | if (NulCharacter) Diag(NulCharacter, diag::null_in_string); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 472 | |
| 473 | Result.SetKind(tok::angle_string_literal); |
| 474 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 475 | // Update the location of token as well as BufferPtr. |
| 476 | FormTokenWithChars(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | |
| 480 | /// LexCharConstant - Lex the remainder of a character constant, after having |
| 481 | /// lexed either ' or L'. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 482 | void Lexer::LexCharConstant(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 483 | const char *NulCharacter = 0; // Does this character contain the \0 character? |
| 484 | |
| 485 | // Handle the common case of 'x' and '\y' efficiently. |
| 486 | char C = getAndAdvanceChar(CurPtr, Result); |
| 487 | if (C == '\'') { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 488 | Diag(BufferPtr, diag::err_empty_character); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 489 | BufferPtr = CurPtr; |
| 490 | return LexTokenInternal(Result); |
| 491 | } else if (C == '\\') { |
| 492 | // Skip the escaped character. |
| 493 | // FIXME: UCN's. |
| 494 | C = getAndAdvanceChar(CurPtr, Result); |
| 495 | } |
| 496 | |
| 497 | if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') { |
| 498 | ++CurPtr; |
| 499 | } else { |
| 500 | // Fall back on generic code for embedded nulls, newlines, wide chars. |
| 501 | do { |
| 502 | // Skip escaped characters. |
| 503 | if (C == '\\') { |
| 504 | // Skip the escaped character. |
| 505 | C = getAndAdvanceChar(CurPtr, Result); |
| 506 | } else if (C == '\n' || C == '\r' || // Newline. |
| 507 | (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 508 | Diag(BufferPtr, diag::err_unterminated_char); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 509 | BufferPtr = CurPtr-1; |
| 510 | return LexTokenInternal(Result); |
| 511 | } else if (C == 0) { |
| 512 | NulCharacter = CurPtr-1; |
| 513 | } |
| 514 | C = getAndAdvanceChar(CurPtr, Result); |
| 515 | } while (C != '\''); |
| 516 | } |
| 517 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 518 | if (NulCharacter) Diag(NulCharacter, diag::null_in_char); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 519 | |
| 520 | Result.SetKind(tok::char_constant); |
| 521 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 522 | // Update the location of token as well as BufferPtr. |
| 523 | FormTokenWithChars(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | /// SkipWhitespace - Efficiently skip over a series of whitespace characters. |
| 527 | /// Update BufferPtr to point to the next non-whitespace character and return. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 528 | void Lexer::SkipWhitespace(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 529 | // Whitespace - Skip it, then return the token after the whitespace. |
| 530 | unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently. |
| 531 | while (1) { |
| 532 | // Skip horizontal whitespace very aggressively. |
| 533 | while (isHorizontalWhitespace(Char)) |
| 534 | Char = *++CurPtr; |
| 535 | |
| 536 | // Otherwise if we something other than whitespace, we're done. |
| 537 | if (Char != '\n' && Char != '\r') |
| 538 | break; |
| 539 | |
| 540 | if (ParsingPreprocessorDirective) { |
| 541 | // End of preprocessor directive line, let LexTokenInternal handle this. |
| 542 | BufferPtr = CurPtr; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 543 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | // ok, but handle newline. |
| 547 | // The returned token is at the start of the line. |
| 548 | Result.SetFlag(LexerToken::StartOfLine); |
| 549 | // No leading whitespace seen so far. |
| 550 | Result.ClearFlag(LexerToken::LeadingSpace); |
| 551 | Char = *++CurPtr; |
| 552 | } |
| 553 | |
| 554 | // If this isn't immediately after a newline, there is leading space. |
| 555 | char PrevChar = CurPtr[-1]; |
| 556 | if (PrevChar != '\n' && PrevChar != '\r') |
| 557 | Result.SetFlag(LexerToken::LeadingSpace); |
| 558 | |
| 559 | // If the next token is obviously a // or /* */ comment, skip it efficiently |
| 560 | // too (without going through the big switch stmt). |
| 561 | if (Char == '/' && CurPtr[1] == '/') { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 562 | BufferPtr = CurPtr; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 563 | return SkipBCPLComment(Result, CurPtr+1); |
| 564 | } |
| 565 | if (Char == '/' && CurPtr[1] == '*') { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 566 | BufferPtr = CurPtr; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 567 | return SkipBlockComment(Result, CurPtr+2); |
| 568 | } |
| 569 | BufferPtr = CurPtr; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | // SkipBCPLComment - We have just read the // characters from input. Skip until |
| 573 | // we find the newline character thats terminate the comment. Then update |
| 574 | /// BufferPtr and return. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 575 | void Lexer::SkipBCPLComment(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 576 | // If BCPL comments aren't explicitly enabled for this language, emit an |
| 577 | // extension warning. |
| 578 | if (!Features.BCPLComment) { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 579 | Diag(BufferPtr, diag::ext_bcpl_comment); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 580 | |
| 581 | // Mark them enabled so we only emit one warning for this translation |
| 582 | // unit. |
| 583 | Features.BCPLComment = true; |
| 584 | } |
| 585 | |
| 586 | // Scan over the body of the comment. The common case, when scanning, is that |
| 587 | // the comment contains normal ascii characters with nothing interesting in |
| 588 | // them. As such, optimize for this case with the inner loop. |
| 589 | char C; |
| 590 | do { |
| 591 | C = *CurPtr; |
Chris Lattner | 505c547 | 2006-07-03 00:55:48 +0000 | [diff] [blame^] | 592 | // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character. |
| 593 | // If we find a \n character, scan backwards, checking to see if it's an |
| 594 | // escaped newline, like we do for block comments. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 595 | |
| 596 | // Skip over characters in the fast loop. |
| 597 | while (C != 0 && // Potentially EOF. |
| 598 | C != '\\' && // Potentially escaped newline. |
| 599 | C != '?' && // Potentially trigraph. |
| 600 | C != '\n' && C != '\r') // Newline or DOS-style newline. |
| 601 | C = *++CurPtr; |
| 602 | |
| 603 | // If this is a newline, we're done. |
| 604 | if (C == '\n' || C == '\r') |
| 605 | break; // Found the newline? Break out! |
| 606 | |
| 607 | // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to |
| 608 | // properly decode the character. |
| 609 | const char *OldPtr = CurPtr; |
| 610 | C = getAndAdvanceChar(CurPtr, Result); |
| 611 | |
| 612 | // If we read multiple characters, and one of those characters was a \r or |
| 613 | // \n, then we had an escaped newline within the comment. Emit diagnostic. |
| 614 | if (CurPtr != OldPtr+1) { |
| 615 | for (; OldPtr != CurPtr; ++OldPtr) |
| 616 | if (OldPtr[0] == '\n' || OldPtr[0] == '\r') { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 617 | Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment); |
| 618 | break; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
| 622 | if (CurPtr == BufferEnd+1) goto FoundEOF; |
| 623 | } while (C != '\n' && C != '\r'); |
| 624 | |
| 625 | // Found and did not consume a newline. |
| 626 | |
| 627 | // If we are inside a preprocessor directive and we see the end of line, |
| 628 | // return immediately, so that the lexer can return this as an EOM token. |
| 629 | if (ParsingPreprocessorDirective) { |
| 630 | BufferPtr = CurPtr; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 631 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 632 | } |
| 633 | |
| 634 | // Otherwise, eat the \n character. We don't care if this is a \n\r or |
| 635 | // \r\n sequence. |
| 636 | ++CurPtr; |
| 637 | |
| 638 | // The next returned token is at the start of the line. |
| 639 | Result.SetFlag(LexerToken::StartOfLine); |
| 640 | // No leading whitespace seen so far. |
| 641 | Result.ClearFlag(LexerToken::LeadingSpace); |
| 642 | |
| 643 | // It is common for the tokens immediately after a // comment to be |
| 644 | // whitespace (indentation for the next line). Instead of going through the |
| 645 | // big switch, handle it efficiently now. |
| 646 | if (isWhitespace(*CurPtr)) { |
| 647 | Result.SetFlag(LexerToken::LeadingSpace); |
| 648 | return SkipWhitespace(Result, CurPtr+1); |
| 649 | } |
| 650 | |
| 651 | BufferPtr = CurPtr; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 652 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 653 | |
| 654 | FoundEOF: // If we ran off the end of the buffer, return EOF. |
| 655 | BufferPtr = CurPtr-1; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 656 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 659 | /// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline |
| 660 | /// 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] | 661 | /// 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] | 662 | static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr, |
| 663 | Lexer *L) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 664 | assert(CurPtr[0] == '\n' || CurPtr[0] == '\r'); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 665 | |
| 666 | // Back up off the newline. |
| 667 | --CurPtr; |
| 668 | |
| 669 | // If this is a two-character newline sequence, skip the other character. |
| 670 | if (CurPtr[0] == '\n' || CurPtr[0] == '\r') { |
| 671 | // \n\n or \r\r -> not escaped newline. |
| 672 | if (CurPtr[0] == CurPtr[1]) |
| 673 | return false; |
| 674 | // \n\r or \r\n -> skip the newline. |
| 675 | --CurPtr; |
| 676 | } |
| 677 | |
| 678 | // If we have horizontal whitespace, skip over it. We allow whitespace |
| 679 | // between the slash and newline. |
| 680 | bool HasSpace = false; |
| 681 | while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) { |
| 682 | --CurPtr; |
| 683 | HasSpace = true; |
| 684 | } |
| 685 | |
| 686 | // If we have a slash, we know this is an escaped newline. |
| 687 | if (*CurPtr == '\\') { |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 688 | if (CurPtr[-1] != '*') return false; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 689 | } else { |
| 690 | // It isn't a slash, is it the ?? / trigraph? |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 691 | if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' || |
| 692 | CurPtr[-3] != '*') |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 693 | return false; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 694 | |
| 695 | // This is the trigraph ending the comment. Emit a stern warning! |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 696 | CurPtr -= 2; |
| 697 | |
| 698 | // If no trigraphs are enabled, warn that we ignored this trigraph and |
| 699 | // ignore this * character. |
Chris Lattner | 1f58305 | 2006-06-18 06:53:56 +0000 | [diff] [blame] | 700 | if (!L->getFeatures().Trigraphs) { |
| 701 | L->Diag(CurPtr, diag::trigraph_ignored_block_comment); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 702 | return false; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 703 | } |
Chris Lattner | 1f58305 | 2006-06-18 06:53:56 +0000 | [diff] [blame] | 704 | L->Diag(CurPtr, diag::trigraph_ends_block_comment); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 705 | } |
| 706 | |
| 707 | // Warn about having an escaped newline between the */ characters. |
Chris Lattner | 1f58305 | 2006-06-18 06:53:56 +0000 | [diff] [blame] | 708 | L->Diag(CurPtr, diag::escaped_newline_block_comment_end); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 709 | |
| 710 | // If there was space between the backslash and newline, warn about it. |
Chris Lattner | 1f58305 | 2006-06-18 06:53:56 +0000 | [diff] [blame] | 711 | if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 712 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 713 | return true; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 714 | } |
| 715 | |
| 716 | /// SkipBlockComment - We have just read the /* characters from input. Read |
| 717 | /// until we find the */ characters that terminate the comment. Note that we |
| 718 | /// don't bother decoding trigraphs or escaped newlines in block comments, |
| 719 | /// because they cannot cause the comment to end. The only thing that can |
| 720 | /// happen is the comment could end with an escaped newline between the */ end |
| 721 | /// of comment. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 722 | void Lexer::SkipBlockComment(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 723 | // Scan one character past where we should, looking for a '/' character. Once |
| 724 | // we find it, check to see if it was preceeded by a *. This common |
| 725 | // optimization helps people who like to put a lot of * characters in their |
| 726 | // comments. |
| 727 | unsigned char C = *CurPtr++; |
| 728 | if (C == 0 && CurPtr == BufferEnd+1) { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 729 | Diag(BufferPtr, diag::err_unterminated_block_comment); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 730 | BufferPtr = CurPtr-1; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 731 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 732 | } |
| 733 | |
| 734 | while (1) { |
| 735 | // Skip over all non-interesting characters. |
| 736 | // TODO: Vectorize this. Note: memchr on Darwin is slower than this loop. |
| 737 | while (C != '/' && C != '\0') |
| 738 | C = *CurPtr++; |
| 739 | |
| 740 | if (C == '/') { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 741 | if (CurPtr[-2] == '*') // We found the final */. We're done! |
| 742 | break; |
| 743 | |
| 744 | if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) { |
Chris Lattner | 1f58305 | 2006-06-18 06:53:56 +0000 | [diff] [blame] | 745 | if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 746 | // We found the final */, though it had an escaped newline between the |
| 747 | // * and /. We're done! |
| 748 | break; |
| 749 | } |
| 750 | } |
| 751 | if (CurPtr[0] == '*' && CurPtr[1] != '/') { |
| 752 | // If this is a /* inside of the comment, emit a warning. Don't do this |
| 753 | // if this is a /*/, which will end the comment. This misses cases with |
| 754 | // embedded escaped newlines, but oh well. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 755 | Diag(CurPtr-1, diag::nested_block_comment); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 756 | } |
| 757 | } else if (C == 0 && CurPtr == BufferEnd+1) { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 758 | Diag(BufferPtr, diag::err_unterminated_block_comment); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 759 | // Note: the user probably forgot a */. We could continue immediately |
| 760 | // after the /*, but this would involve lexing a lot of what really is the |
| 761 | // comment, which surely would confuse the parser. |
| 762 | BufferPtr = CurPtr-1; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 763 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 764 | } |
| 765 | C = *CurPtr++; |
| 766 | } |
| 767 | |
| 768 | // It is common for the tokens immediately after a /**/ comment to be |
| 769 | // whitespace. Instead of going through the big switch, handle it |
| 770 | // efficiently now. |
| 771 | if (isHorizontalWhitespace(*CurPtr)) { |
| 772 | Result.SetFlag(LexerToken::LeadingSpace); |
| 773 | return SkipWhitespace(Result, CurPtr+1); |
| 774 | } |
| 775 | |
| 776 | // Otherwise, just return so that the next character will be lexed as a token. |
| 777 | BufferPtr = CurPtr; |
| 778 | Result.SetFlag(LexerToken::LeadingSpace); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 779 | } |
| 780 | |
| 781 | //===----------------------------------------------------------------------===// |
| 782 | // Primary Lexing Entry Points |
| 783 | //===----------------------------------------------------------------------===// |
| 784 | |
| 785 | /// LexIncludeFilename - After the preprocessor has parsed a #include, lex and |
| 786 | /// (potentially) macro expand the filename. |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 787 | std::string Lexer::LexIncludeFilename(LexerToken &FilenameTok) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 788 | assert(ParsingPreprocessorDirective && |
| 789 | ParsingFilename == false && |
| 790 | "Must be in a preprocessing directive!"); |
| 791 | |
| 792 | // We are now parsing a filename! |
| 793 | ParsingFilename = true; |
| 794 | |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 795 | // Lex the filename. |
| 796 | Lex(FilenameTok); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 797 | |
| 798 | // We should have gotten the filename now. |
| 799 | ParsingFilename = false; |
| 800 | |
| 801 | // No filename? |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 802 | if (FilenameTok.getKind() == tok::eom) { |
| 803 | PP.Diag(FilenameTok, diag::err_pp_expects_filename); |
| 804 | return ""; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 805 | } |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 806 | |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 807 | // Get the text form of the filename. |
| 808 | std::string Filename = PP.getSpelling(FilenameTok); |
| 809 | assert(!Filename.empty() && "Can't have tokens with empty spellings!"); |
| 810 | |
| 811 | // Make sure the filename is <x> or "x". |
| 812 | if (Filename[0] == '<') { |
| 813 | if (Filename[Filename.size()-1] != '>') { |
| 814 | PP.Diag(FilenameTok, diag::err_pp_expects_filename); |
| 815 | FilenameTok.SetKind(tok::eom); |
| 816 | return ""; |
| 817 | } |
| 818 | } else if (Filename[0] == '"') { |
| 819 | if (Filename[Filename.size()-1] != '"') { |
| 820 | PP.Diag(FilenameTok, diag::err_pp_expects_filename); |
| 821 | FilenameTok.SetKind(tok::eom); |
| 822 | return ""; |
| 823 | } |
| 824 | } else { |
| 825 | PP.Diag(FilenameTok, diag::err_pp_expects_filename); |
| 826 | FilenameTok.SetKind(tok::eom); |
| 827 | return ""; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 828 | } |
Chris Lattner | 269c232 | 2006-06-25 06:23:00 +0000 | [diff] [blame] | 829 | |
| 830 | // Diagnose #include "" as invalid. |
| 831 | if (Filename.size() == 2) { |
| 832 | PP.Diag(FilenameTok, diag::err_pp_empty_filename); |
| 833 | FilenameTok.SetKind(tok::eom); |
| 834 | return ""; |
| 835 | } |
| 836 | |
| 837 | return Filename; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /// ReadToEndOfLine - Read the rest of the current preprocessor line as an |
| 841 | /// uninterpreted string. This switches the lexer out of directive mode. |
| 842 | std::string Lexer::ReadToEndOfLine() { |
| 843 | assert(ParsingPreprocessorDirective && ParsingFilename == false && |
| 844 | "Must be in a preprocessing directive!"); |
| 845 | std::string Result; |
| 846 | LexerToken Tmp; |
| 847 | |
| 848 | // CurPtr - Cache BufferPtr in an automatic variable. |
| 849 | const char *CurPtr = BufferPtr; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 850 | while (1) { |
| 851 | char Char = getAndAdvanceChar(CurPtr, Tmp); |
| 852 | switch (Char) { |
| 853 | default: |
| 854 | Result += Char; |
| 855 | break; |
| 856 | case 0: // Null. |
| 857 | // Found end of file? |
| 858 | if (CurPtr-1 != BufferEnd) { |
| 859 | // Nope, normal character, continue. |
| 860 | Result += Char; |
| 861 | break; |
| 862 | } |
| 863 | // FALL THROUGH. |
| 864 | case '\r': |
| 865 | case '\n': |
| 866 | // Okay, we found the end of the line. First, back up past the \0, \r, \n. |
| 867 | assert(CurPtr[-1] == Char && "Trigraphs for newline?"); |
| 868 | BufferPtr = CurPtr-1; |
| 869 | |
| 870 | // Next, lex the character, which should handle the EOM transition. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 871 | Lex(Tmp); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 872 | assert(Tmp.getKind() == tok::eom && "Unexpected token!"); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 873 | |
| 874 | // Finally, we're done, return the string we found. |
| 875 | return Result; |
| 876 | } |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | /// LexEndOfFile - CurPtr points to the end of this file. Handle this |
| 881 | /// condition, reporting diagnostics and handling other edge cases as required. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 882 | void Lexer::LexEndOfFile(LexerToken &Result, const char *CurPtr) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 883 | // If we hit the end of the file while parsing a preprocessor directive, |
| 884 | // end the preprocessor directive first. The next token returned will |
| 885 | // then be the end of file. |
| 886 | if (ParsingPreprocessorDirective) { |
| 887 | // Done parsing the "line". |
| 888 | ParsingPreprocessorDirective = false; |
| 889 | Result.SetKind(tok::eom); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 890 | // Update the location of token as well as BufferPtr. |
| 891 | FormTokenWithChars(Result, CurPtr); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 892 | return; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | // If we are in a #if directive, emit an error. |
| 896 | while (!ConditionalStack.empty()) { |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 897 | PP.Diag(ConditionalStack.back().IfLoc, |
| 898 | diag::err_pp_unterminated_conditional); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 899 | ConditionalStack.pop_back(); |
| 900 | } |
| 901 | |
| 902 | // 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] | 903 | if (CurPtr[-1] != '\n' && CurPtr[-1] != '\r') |
| 904 | Diag(BufferEnd, diag::ext_no_newline_eof); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 905 | |
| 906 | BufferPtr = CurPtr; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 907 | PP.HandleEndOfFile(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | |
| 911 | /// LexTokenInternal - This implements a simple C family lexer. It is an |
| 912 | /// extremely performance critical piece of code. This assumes that the buffer |
| 913 | /// has a null character at the end of the file. Return true if an error |
| 914 | /// occurred and compilation should terminate, false if normal. This returns a |
| 915 | /// preprocessing token, not a normal token, as such, it is an internal |
| 916 | /// interface. It assumes that the Flags of result have been cleared before |
| 917 | /// calling this. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 918 | void Lexer::LexTokenInternal(LexerToken &Result) { |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 919 | LexNextToken: |
| 920 | // New token, can't need cleaning yet. |
| 921 | Result.ClearFlag(LexerToken::NeedsCleaning); |
| 922 | |
| 923 | // CurPtr - Cache BufferPtr in an automatic variable. |
| 924 | const char *CurPtr = BufferPtr; |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 925 | |
| 926 | unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below. |
| 927 | |
| 928 | // Read a character, advancing over it. |
| 929 | char Char = getAndAdvanceChar(CurPtr, Result); |
| 930 | switch (Char) { |
| 931 | case 0: // Null. |
| 932 | // Found end of file? |
| 933 | if (CurPtr-1 == BufferEnd) |
| 934 | return LexEndOfFile(Result, CurPtr-1); // Retreat back into the file. |
| 935 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 936 | Diag(CurPtr-1, diag::null_in_file); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 937 | Result.SetFlag(LexerToken::LeadingSpace); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 938 | SkipWhitespace(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 939 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 940 | case '\n': |
| 941 | case '\r': |
| 942 | // If we are inside a preprocessor directive and we see the end of line, |
| 943 | // we know we are done with the directive, so return an EOM token. |
| 944 | if (ParsingPreprocessorDirective) { |
| 945 | // Done parsing the "line". |
| 946 | ParsingPreprocessorDirective = false; |
| 947 | |
| 948 | // Since we consumed a newline, we are back at the start of a line. |
| 949 | IsAtStartOfLine = true; |
| 950 | |
| 951 | Result.SetKind(tok::eom); |
| 952 | break; |
| 953 | } |
| 954 | // The returned token is at the start of the line. |
| 955 | Result.SetFlag(LexerToken::StartOfLine); |
| 956 | // No leading whitespace seen so far. |
| 957 | Result.ClearFlag(LexerToken::LeadingSpace); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 958 | SkipWhitespace(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 959 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 960 | case ' ': |
| 961 | case '\t': |
| 962 | case '\f': |
| 963 | case '\v': |
| 964 | Result.SetFlag(LexerToken::LeadingSpace); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 965 | SkipWhitespace(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 966 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 967 | |
| 968 | case 'L': |
| 969 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 970 | |
| 971 | // Wide string literal. |
| 972 | if (Char == '"') |
| 973 | return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result)); |
| 974 | |
| 975 | // Wide character constant. |
| 976 | if (Char == '\'') |
| 977 | return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); |
| 978 | // FALL THROUGH, treating L like the start of an identifier. |
| 979 | |
| 980 | // C99 6.4.2: Identifiers. |
| 981 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': |
| 982 | case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N': |
| 983 | case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': |
| 984 | case 'V': case 'W': case 'X': case 'Y': case 'Z': |
| 985 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': |
| 986 | case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': |
| 987 | case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': |
| 988 | case 'v': case 'w': case 'x': case 'y': case 'z': |
| 989 | case '_': |
| 990 | return LexIdentifier(Result, CurPtr); |
| 991 | |
| 992 | // C99 6.4.4.1: Integer Constants. |
| 993 | // C99 6.4.4.2: Floating Constants. |
| 994 | case '0': case '1': case '2': case '3': case '4': |
| 995 | case '5': case '6': case '7': case '8': case '9': |
| 996 | return LexNumericConstant(Result, CurPtr); |
| 997 | |
| 998 | // C99 6.4.4: Character Constants. |
| 999 | case '\'': |
| 1000 | return LexCharConstant(Result, CurPtr); |
| 1001 | |
| 1002 | // C99 6.4.5: String Literals. |
| 1003 | case '"': |
| 1004 | return LexStringLiteral(Result, CurPtr); |
| 1005 | |
| 1006 | // C99 6.4.6: Punctuators. |
| 1007 | case '?': |
| 1008 | Result.SetKind(tok::question); |
| 1009 | break; |
| 1010 | case '[': |
| 1011 | Result.SetKind(tok::l_square); |
| 1012 | break; |
| 1013 | case ']': |
| 1014 | Result.SetKind(tok::r_square); |
| 1015 | break; |
| 1016 | case '(': |
| 1017 | Result.SetKind(tok::l_paren); |
| 1018 | break; |
| 1019 | case ')': |
| 1020 | Result.SetKind(tok::r_paren); |
| 1021 | break; |
| 1022 | case '{': |
| 1023 | Result.SetKind(tok::l_brace); |
| 1024 | break; |
| 1025 | case '}': |
| 1026 | Result.SetKind(tok::r_brace); |
| 1027 | break; |
| 1028 | case '.': |
| 1029 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1030 | if (Char >= '0' && Char <= '9') { |
| 1031 | return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); |
| 1032 | } else if (Features.CPlusPlus && Char == '*') { |
| 1033 | Result.SetKind(tok::periodstar); |
| 1034 | CurPtr += SizeTmp; |
| 1035 | } else if (Char == '.' && |
| 1036 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') { |
| 1037 | Result.SetKind(tok::ellipsis); |
| 1038 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 1039 | SizeTmp2, Result); |
| 1040 | } else { |
| 1041 | Result.SetKind(tok::period); |
| 1042 | } |
| 1043 | break; |
| 1044 | case '&': |
| 1045 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1046 | if (Char == '&') { |
| 1047 | Result.SetKind(tok::ampamp); |
| 1048 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1049 | } else if (Char == '=') { |
| 1050 | Result.SetKind(tok::ampequal); |
| 1051 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1052 | } else { |
| 1053 | Result.SetKind(tok::amp); |
| 1054 | } |
| 1055 | break; |
| 1056 | case '*': |
| 1057 | if (getCharAndSize(CurPtr, SizeTmp) == '=') { |
| 1058 | Result.SetKind(tok::starequal); |
| 1059 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1060 | } else { |
| 1061 | Result.SetKind(tok::star); |
| 1062 | } |
| 1063 | break; |
| 1064 | case '+': |
| 1065 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1066 | if (Char == '+') { |
| 1067 | Result.SetKind(tok::plusplus); |
| 1068 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1069 | } else if (Char == '=') { |
| 1070 | Result.SetKind(tok::plusequal); |
| 1071 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1072 | } else { |
| 1073 | Result.SetKind(tok::plus); |
| 1074 | } |
| 1075 | break; |
| 1076 | case '-': |
| 1077 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1078 | if (Char == '-') { |
| 1079 | Result.SetKind(tok::minusminus); |
| 1080 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1081 | } else if (Char == '>' && Features.CPlusPlus && |
| 1082 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { |
| 1083 | Result.SetKind(tok::arrowstar); // C++ ->* |
| 1084 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 1085 | SizeTmp2, Result); |
| 1086 | } else if (Char == '>') { |
| 1087 | Result.SetKind(tok::arrow); |
| 1088 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1089 | } else if (Char == '=') { |
| 1090 | Result.SetKind(tok::minusequal); |
| 1091 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1092 | } else { |
| 1093 | Result.SetKind(tok::minus); |
| 1094 | } |
| 1095 | break; |
| 1096 | case '~': |
| 1097 | Result.SetKind(tok::tilde); |
| 1098 | break; |
| 1099 | case '!': |
| 1100 | if (getCharAndSize(CurPtr, SizeTmp) == '=') { |
| 1101 | Result.SetKind(tok::exclaimequal); |
| 1102 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1103 | } else { |
| 1104 | Result.SetKind(tok::exclaim); |
| 1105 | } |
| 1106 | break; |
| 1107 | case '/': |
| 1108 | // 6.4.9: Comments |
| 1109 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1110 | if (Char == '/') { // BCPL comment. |
| 1111 | Result.SetFlag(LexerToken::LeadingSpace); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1112 | SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1113 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 1114 | } else if (Char == '*') { // /**/ comment. |
| 1115 | Result.SetFlag(LexerToken::LeadingSpace); |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1116 | SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1117 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 1118 | } else if (Char == '=') { |
| 1119 | Result.SetKind(tok::slashequal); |
| 1120 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1121 | } else { |
| 1122 | Result.SetKind(tok::slash); |
| 1123 | } |
| 1124 | break; |
| 1125 | case '%': |
| 1126 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1127 | if (Char == '=') { |
| 1128 | Result.SetKind(tok::percentequal); |
| 1129 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1130 | } else if (Features.Digraphs && Char == '>') { |
| 1131 | Result.SetKind(tok::r_brace); // '%>' -> '}' |
| 1132 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1133 | } else if (Features.Digraphs && Char == ':') { |
| 1134 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1135 | if (getCharAndSize(CurPtr, SizeTmp) == '%' && |
| 1136 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') { |
| 1137 | Result.SetKind(tok::hashhash); // '%:%:' -> '##' |
| 1138 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 1139 | SizeTmp2, Result); |
| 1140 | } else { |
| 1141 | Result.SetKind(tok::hash); // '%:' -> '#' |
| 1142 | |
| 1143 | // We parsed a # character. If this occurs at the start of the line, |
| 1144 | // it's actually the start of a preprocessing directive. Callback to |
| 1145 | // the preprocessor to handle it. |
| 1146 | // FIXME: -fpreprocessed mode?? |
| 1147 | if (Result.isAtStartOfLine() && !PP.isSkipping()) { |
| 1148 | BufferPtr = CurPtr; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1149 | PP.HandleDirective(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1150 | |
| 1151 | // As an optimization, if the preprocessor didn't switch lexers, tail |
| 1152 | // recurse. |
| 1153 | if (PP.isCurrentLexer(this)) { |
| 1154 | // Start a new token. If this is a #include or something, the PP may |
| 1155 | // want us starting at the beginning of the line again. If so, set |
| 1156 | // the StartOfLine flag. |
| 1157 | if (IsAtStartOfLine) { |
| 1158 | Result.SetFlag(LexerToken::StartOfLine); |
| 1159 | IsAtStartOfLine = false; |
| 1160 | } |
| 1161 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 1162 | } |
| 1163 | |
| 1164 | return PP.Lex(Result); |
| 1165 | } |
| 1166 | } |
| 1167 | } else { |
| 1168 | Result.SetKind(tok::percent); |
| 1169 | } |
| 1170 | break; |
| 1171 | case '<': |
| 1172 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1173 | if (ParsingFilename) { |
| 1174 | return LexAngledStringLiteral(Result, CurPtr+SizeTmp); |
| 1175 | } else if (Char == '<' && |
| 1176 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') { |
| 1177 | Result.SetKind(tok::lesslessequal); |
| 1178 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 1179 | SizeTmp2, Result); |
| 1180 | } else if (Char == '<') { |
| 1181 | Result.SetKind(tok::lessless); |
| 1182 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1183 | } else if (Char == '=') { |
| 1184 | Result.SetKind(tok::lessequal); |
| 1185 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1186 | } else if (Features.Digraphs && Char == ':') { |
| 1187 | Result.SetKind(tok::l_square); // '<:' -> '[' |
| 1188 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1189 | } else if (Features.Digraphs && Char == '>') { |
| 1190 | Result.SetKind(tok::l_brace); // '<%' -> '{' |
| 1191 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1192 | } else if (Features.CPPMinMax && Char == '?') { // <? |
| 1193 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 1194 | Diag(BufferPtr, diag::min_max_deprecated); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1195 | |
| 1196 | if (getCharAndSize(CurPtr, SizeTmp) == '=') { // <?= |
| 1197 | Result.SetKind(tok::lessquestionequal); |
| 1198 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1199 | } else { |
| 1200 | Result.SetKind(tok::lessquestion); |
| 1201 | } |
| 1202 | } else { |
| 1203 | Result.SetKind(tok::less); |
| 1204 | } |
| 1205 | break; |
| 1206 | case '>': |
| 1207 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1208 | if (Char == '=') { |
| 1209 | Result.SetKind(tok::greaterequal); |
| 1210 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1211 | } else if (Char == '>' && |
| 1212 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') { |
| 1213 | Result.SetKind(tok::greatergreaterequal); |
| 1214 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 1215 | SizeTmp2, Result); |
| 1216 | } else if (Char == '>') { |
| 1217 | Result.SetKind(tok::greatergreater); |
| 1218 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1219 | } else if (Features.CPPMinMax && Char == '?') { |
| 1220 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 1221 | Diag(BufferPtr, diag::min_max_deprecated); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1222 | |
| 1223 | if (getCharAndSize(CurPtr, SizeTmp) == '=') { |
| 1224 | Result.SetKind(tok::greaterquestionequal); // >?= |
| 1225 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1226 | } else { |
| 1227 | Result.SetKind(tok::greaterquestion); // >? |
| 1228 | } |
| 1229 | } else { |
| 1230 | Result.SetKind(tok::greater); |
| 1231 | } |
| 1232 | break; |
| 1233 | case '^': |
| 1234 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1235 | if (Char == '=') { |
| 1236 | Result.SetKind(tok::caretequal); |
| 1237 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1238 | } else { |
| 1239 | Result.SetKind(tok::caret); |
| 1240 | } |
| 1241 | break; |
| 1242 | case '|': |
| 1243 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1244 | if (Char == '=') { |
| 1245 | Result.SetKind(tok::pipeequal); |
| 1246 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1247 | } else if (Char == '|') { |
| 1248 | Result.SetKind(tok::pipepipe); |
| 1249 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1250 | } else { |
| 1251 | Result.SetKind(tok::pipe); |
| 1252 | } |
| 1253 | break; |
| 1254 | case ':': |
| 1255 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1256 | if (Features.Digraphs && Char == '>') { |
| 1257 | Result.SetKind(tok::r_square); // ':>' -> ']' |
| 1258 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1259 | } else if (Features.CPlusPlus && Char == ':') { |
| 1260 | Result.SetKind(tok::coloncolon); |
| 1261 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1262 | } else { |
| 1263 | Result.SetKind(tok::colon); |
| 1264 | } |
| 1265 | break; |
| 1266 | case ';': |
| 1267 | Result.SetKind(tok::semi); |
| 1268 | break; |
| 1269 | case '=': |
| 1270 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1271 | if (Char == '=') { |
| 1272 | Result.SetKind(tok::equalequal); |
| 1273 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1274 | } else { |
| 1275 | Result.SetKind(tok::equal); |
| 1276 | } |
| 1277 | break; |
| 1278 | case ',': |
| 1279 | Result.SetKind(tok::comma); |
| 1280 | break; |
| 1281 | case '#': |
| 1282 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1283 | if (Char == '#') { |
| 1284 | Result.SetKind(tok::hashhash); |
| 1285 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1286 | } else { |
| 1287 | Result.SetKind(tok::hash); |
| 1288 | // We parsed a # character. If this occurs at the start of the line, |
| 1289 | // it's actually the start of a preprocessing directive. Callback to |
| 1290 | // the preprocessor to handle it. |
Chris Lattner | 505c547 | 2006-07-03 00:55:48 +0000 | [diff] [blame^] | 1291 | // FIXME: -fpreprocessed mode?? |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1292 | if (Result.isAtStartOfLine() && !PP.isSkipping()) { |
| 1293 | BufferPtr = CurPtr; |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1294 | PP.HandleDirective(Result); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1295 | |
| 1296 | // As an optimization, if the preprocessor didn't switch lexers, tail |
| 1297 | // recurse. |
| 1298 | if (PP.isCurrentLexer(this)) { |
| 1299 | // Start a new token. If this is a #include or something, the PP may |
| 1300 | // want us starting at the beginning of the line again. If so, set |
| 1301 | // the StartOfLine flag. |
| 1302 | if (IsAtStartOfLine) { |
| 1303 | Result.SetFlag(LexerToken::StartOfLine); |
| 1304 | IsAtStartOfLine = false; |
| 1305 | } |
| 1306 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 1307 | } |
| 1308 | return PP.Lex(Result); |
| 1309 | } |
| 1310 | } |
| 1311 | break; |
| 1312 | |
| 1313 | case '\\': |
Chris Lattner | 505c547 | 2006-07-03 00:55:48 +0000 | [diff] [blame^] | 1314 | // FIXME: UCN's. |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1315 | // FALL THROUGH. |
| 1316 | default: |
| 1317 | // Objective C support. |
| 1318 | if (CurPtr[-1] == '@' && Features.ObjC1) { |
| 1319 | Result.SetKind(tok::at); |
| 1320 | break; |
| 1321 | } else if (CurPtr[-1] == '$' && Features.DollarIdents) {// $ in identifiers. |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1322 | Diag(CurPtr-1, diag::ext_dollar_in_identifier); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1323 | return LexIdentifier(Result, CurPtr); |
| 1324 | } |
| 1325 | |
Chris Lattner | cb28334 | 2006-06-18 06:48:37 +0000 | [diff] [blame] | 1326 | if (!PP.isSkipping()) Diag(CurPtr-1, diag::err_stray_character); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1327 | BufferPtr = CurPtr; |
| 1328 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 1329 | } |
| 1330 | |
Chris Lattner | d01e291 | 2006-06-18 16:22:51 +0000 | [diff] [blame] | 1331 | // Update the location of token as well as BufferPtr. |
| 1332 | FormTokenWithChars(Result, CurPtr); |
Chris Lattner | 22eb972 | 2006-06-18 05:43:12 +0000 | [diff] [blame] | 1333 | } |