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