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