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