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