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