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