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