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