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