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