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