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