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