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