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" |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 30 | #include "clang/Lex/CodeCompletionHandler.h" |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 31 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringSwitch.h" |
Chris Lattner | 409a036 | 2007-07-22 18:38:25 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MemoryBuffer.h" |
| 35 | #include <cctype> |
| 36 | using namespace clang; |
| 37 | |
Chris Lattner | a2bf105 | 2009-12-17 05:29:40 +0000 | [diff] [blame] | 38 | static void InitCharacterInfo(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | |
Chris Lattner | dbf388b | 2007-10-07 08:47:24 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | // Token Class Implementation |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | /// isObjCAtKeyword - Return true if we have an ObjC keyword identifier. |
Chris Lattner | dbf388b | 2007-10-07 08:47:24 +0000 | [diff] [blame] | 45 | bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const { |
Douglas Gregor | bec1c9d | 2008-12-01 21:46:47 +0000 | [diff] [blame] | 46 | if (IdentifierInfo *II = getIdentifierInfo()) |
| 47 | return II->getObjCKeywordID() == objcKey; |
| 48 | return false; |
Chris Lattner | dbf388b | 2007-10-07 08:47:24 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /// getObjCKeywordID - Return the ObjC keyword kind. |
| 52 | tok::ObjCKeywordKind Token::getObjCKeywordID() const { |
| 53 | IdentifierInfo *specId = getIdentifierInfo(); |
| 54 | return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword; |
| 55 | } |
| 56 | |
Chris Lattner | 53702cd | 2007-12-13 01:59:49 +0000 | [diff] [blame] | 57 | |
Chris Lattner | dbf388b | 2007-10-07 08:47:24 +0000 | [diff] [blame] | 58 | //===----------------------------------------------------------------------===// |
| 59 | // Lexer Class Implementation |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | void Lexer::InitLexer(const char *BufStart, const char *BufPtr, |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 63 | const char *BufEnd) { |
Chris Lattner | a2bf105 | 2009-12-17 05:29:40 +0000 | [diff] [blame] | 64 | InitCharacterInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 66 | BufferStart = BufStart; |
| 67 | BufferPtr = BufPtr; |
| 68 | BufferEnd = BufEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 70 | assert(BufEnd[0] == 0 && |
| 71 | "We assume that the input buffer has a null character at the end" |
| 72 | " to simplify lexing!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 74 | Is_PragmaLexer = false; |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 75 | IsInConflictMarker = false; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 77 | // Start of the file is a start of line. |
| 78 | IsAtStartOfLine = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 80 | // We are not after parsing a #. |
| 81 | ParsingPreprocessorDirective = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 83 | // We are not after parsing #include. |
| 84 | ParsingFilename = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 86 | // We are not in raw mode. Raw mode disables diagnostics and interpretation |
| 87 | // of tokens (e.g. identifiers, thus disabling macro expansion). It is used |
| 88 | // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block |
| 89 | // or otherwise skipping over tokens. |
| 90 | LexingRawMode = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 92 | // Default to not keeping comments. |
| 93 | ExtendedTokenMode = 0; |
| 94 | } |
| 95 | |
Chris Lattner | 0770dab | 2009-01-17 07:56:59 +0000 | [diff] [blame] | 96 | /// Lexer constructor - Create a new lexer object for the specified buffer |
| 97 | /// with the specified preprocessor managing the lexing process. This lexer |
| 98 | /// assumes that the associated file buffer and Preprocessor objects will |
| 99 | /// outlive it, so it doesn't take ownership of either of them. |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 100 | Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP) |
Chris Lattner | 88d3ac1 | 2009-01-17 08:03:42 +0000 | [diff] [blame] | 101 | : PreprocessorLexer(&PP, FID), |
| 102 | FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)), |
| 103 | Features(PP.getLangOptions()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +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()); |
| 110 | } |
Chris Lattner | dbf388b | 2007-10-07 08:47:24 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 112 | /// Lexer constructor - Create a new raw lexer object. This object is only |
Chris Lattner | 590f0cc | 2008-10-12 01:15:46 +0000 | [diff] [blame] | 113 | /// suitable for calls to 'LexRawToken'. This lexer assumes that the text |
| 114 | /// range will outlive it, so it doesn't take ownership of it. |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 115 | Lexer::Lexer(SourceLocation fileloc, const LangOptions &features, |
Chris Lattner | de96c0f | 2009-01-17 07:42:27 +0000 | [diff] [blame] | 116 | const char *BufStart, const char *BufPtr, const char *BufEnd) |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 117 | : FileLoc(fileloc), Features(features) { |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 119 | InitLexer(BufStart, BufPtr, BufEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 121 | // We *are* in raw mode. |
| 122 | LexingRawMode = true; |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 125 | /// Lexer constructor - Create a new raw lexer object. This object is only |
| 126 | /// suitable for calls to 'LexRawToken'. This lexer assumes that the text |
| 127 | /// range will outlive it, so it doesn't take ownership of it. |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 128 | Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile, |
| 129 | const SourceManager &SM, const LangOptions &features) |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 130 | : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) { |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 131 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(), |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 133 | FromFile->getBufferEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 135 | // We *are* in raw mode. |
| 136 | LexingRawMode = true; |
| 137 | } |
| 138 | |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 139 | /// Create_PragmaLexer: Lexer constructor - Create a new lexer object for |
| 140 | /// _Pragma expansion. This has a variety of magic semantics that this method |
| 141 | /// sets up. It returns a new'd Lexer that must be delete'd when done. |
| 142 | /// |
| 143 | /// On entrance to this routine, TokStartLoc is a macro location which has a |
| 144 | /// spelling loc that indicates the bytes to be lexed for the token and an |
| 145 | /// instantiation location that indicates where all lexed tokens should be |
| 146 | /// "expanded from". |
| 147 | /// |
| 148 | /// FIXME: It would really be nice to make _Pragma just be a wrapper around a |
| 149 | /// normal lexer that remaps tokens as they fly by. This would require making |
| 150 | /// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer |
| 151 | /// interface that could handle this stuff. This would pull GetMappedTokenLoc |
| 152 | /// out of the critical path of the lexer! |
| 153 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc, |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 155 | SourceLocation InstantiationLocStart, |
| 156 | SourceLocation InstantiationLocEnd, |
Chris Lattner | bcc2a67 | 2009-01-19 06:46:35 +0000 | [diff] [blame] | 157 | unsigned TokLen, Preprocessor &PP) { |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 158 | SourceManager &SM = PP.getSourceManager(); |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 159 | |
| 160 | // 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] | 161 | FileID SpellingFID = SM.getFileID(SpellingLoc); |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 162 | const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID); |
| 163 | Lexer *L = new Lexer(SpellingFID, InputFile, PP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 165 | // Now that the lexer is created, change the start/end locations so that we |
| 166 | // just lex the subsection of the file that we want. This is lexing from a |
| 167 | // scratch buffer. |
| 168 | const char *StrData = SM.getCharacterData(SpellingLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 170 | L->BufferPtr = StrData; |
| 171 | L->BufferEnd = StrData+TokLen; |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 172 | assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!"); |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 173 | |
| 174 | // Set the SourceLocation with the remapping information. This ensures that |
| 175 | // GetMappedTokenLoc will remap the tokens as they are lexed. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 176 | L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID), |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 177 | InstantiationLocStart, |
| 178 | InstantiationLocEnd, TokLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 180 | // Ensure that the lexer thinks it is inside a directive, so that end \n will |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 181 | // return an EOD token. |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 182 | L->ParsingPreprocessorDirective = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 184 | // This lexer really is for _Pragma. |
| 185 | L->Is_PragmaLexer = true; |
| 186 | return L; |
| 187 | } |
| 188 | |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 189 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | /// Stringify - Convert the specified string into a C string, with surrounding |
| 191 | /// ""'s, and with escaped \ and " characters. |
| 192 | std::string Lexer::Stringify(const std::string &Str, bool Charify) { |
| 193 | std::string Result = Str; |
| 194 | char Quote = Charify ? '\'' : '"'; |
| 195 | for (unsigned i = 0, e = Result.size(); i != e; ++i) { |
| 196 | if (Result[i] == '\\' || Result[i] == Quote) { |
| 197 | Result.insert(Result.begin()+i, '\\'); |
| 198 | ++i; ++e; |
| 199 | } |
| 200 | } |
| 201 | return Result; |
| 202 | } |
| 203 | |
Chris Lattner | d8e3083 | 2007-07-24 06:57:14 +0000 | [diff] [blame] | 204 | /// Stringify - Convert the specified string into a C string by escaping '\' |
| 205 | /// and " characters. This does not add surrounding ""'s to the string. |
| 206 | void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) { |
| 207 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 208 | if (Str[i] == '\\' || Str[i] == '"') { |
| 209 | Str.insert(Str.begin()+i, '\\'); |
| 210 | ++i; ++e; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
Chris Lattner | b060727 | 2010-11-17 07:26:20 +0000 | [diff] [blame] | 215 | //===----------------------------------------------------------------------===// |
| 216 | // Token Spelling |
| 217 | //===----------------------------------------------------------------------===// |
| 218 | |
| 219 | /// getSpelling() - Return the 'spelling' of this token. The spelling of a |
| 220 | /// token are the characters used to represent the token in the source file |
| 221 | /// after trigraph expansion and escaped-newline folding. In particular, this |
| 222 | /// wants to get the true, uncanonicalized, spelling of things like digraphs |
| 223 | /// UCNs, etc. |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 224 | llvm::StringRef Lexer::getSpelling(SourceLocation loc, |
| 225 | llvm::SmallVectorImpl<char> &buffer, |
| 226 | const SourceManager &SM, |
| 227 | const LangOptions &options, |
| 228 | bool *invalid) { |
| 229 | // Break down the source location. |
| 230 | std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc); |
| 231 | |
| 232 | // Try to the load the file buffer. |
| 233 | bool invalidTemp = false; |
| 234 | llvm::StringRef file = SM.getBufferData(locInfo.first, &invalidTemp); |
| 235 | if (invalidTemp) { |
| 236 | if (invalid) *invalid = true; |
| 237 | return llvm::StringRef(); |
| 238 | } |
| 239 | |
| 240 | const char *tokenBegin = file.data() + locInfo.second; |
| 241 | |
| 242 | // Lex from the start of the given location. |
| 243 | Lexer lexer(SM.getLocForStartOfFile(locInfo.first), options, |
| 244 | file.begin(), tokenBegin, file.end()); |
| 245 | Token token; |
| 246 | lexer.LexFromRawLexer(token); |
| 247 | |
| 248 | unsigned length = token.getLength(); |
| 249 | |
| 250 | // Common case: no need for cleaning. |
| 251 | if (!token.needsCleaning()) |
| 252 | return llvm::StringRef(tokenBegin, length); |
| 253 | |
| 254 | // Hard case, we need to relex the characters into the string. |
| 255 | buffer.clear(); |
| 256 | buffer.reserve(length); |
| 257 | |
| 258 | for (const char *ti = tokenBegin, *te = ti + length; ti != te; ) { |
| 259 | unsigned charSize; |
| 260 | buffer.push_back(Lexer::getCharAndSizeNoWarn(ti, charSize, options)); |
| 261 | ti += charSize; |
| 262 | } |
| 263 | |
| 264 | return llvm::StringRef(buffer.data(), buffer.size()); |
| 265 | } |
| 266 | |
| 267 | /// getSpelling() - Return the 'spelling' of this token. The spelling of a |
| 268 | /// token are the characters used to represent the token in the source file |
| 269 | /// after trigraph expansion and escaped-newline folding. In particular, this |
| 270 | /// wants to get the true, uncanonicalized, spelling of things like digraphs |
| 271 | /// UCNs, etc. |
Chris Lattner | b060727 | 2010-11-17 07:26:20 +0000 | [diff] [blame] | 272 | std::string Lexer::getSpelling(const Token &Tok, const SourceManager &SourceMgr, |
| 273 | const LangOptions &Features, bool *Invalid) { |
| 274 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
| 275 | |
| 276 | // If this token contains nothing interesting, return it directly. |
| 277 | bool CharDataInvalid = false; |
| 278 | const char* TokStart = SourceMgr.getCharacterData(Tok.getLocation(), |
| 279 | &CharDataInvalid); |
| 280 | if (Invalid) |
| 281 | *Invalid = CharDataInvalid; |
| 282 | if (CharDataInvalid) |
| 283 | return std::string(); |
| 284 | |
| 285 | if (!Tok.needsCleaning()) |
| 286 | return std::string(TokStart, TokStart+Tok.getLength()); |
| 287 | |
| 288 | std::string Result; |
| 289 | Result.reserve(Tok.getLength()); |
| 290 | |
| 291 | // Otherwise, hard case, relex the characters into the string. |
| 292 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 293 | Ptr != End; ) { |
| 294 | unsigned CharSize; |
| 295 | Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features)); |
| 296 | Ptr += CharSize; |
| 297 | } |
| 298 | assert(Result.size() != unsigned(Tok.getLength()) && |
| 299 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
| 300 | return Result; |
| 301 | } |
| 302 | |
| 303 | /// getSpelling - This method is used to get the spelling of a token into a |
| 304 | /// preallocated buffer, instead of as an std::string. The caller is required |
| 305 | /// to allocate enough space for the token, which is guaranteed to be at least |
| 306 | /// Tok.getLength() bytes long. The actual length of the token is returned. |
| 307 | /// |
| 308 | /// Note that this method may do two possible things: it may either fill in |
| 309 | /// the buffer specified with characters, or it may *change the input pointer* |
| 310 | /// to point to a constant buffer with the data already in it (avoiding a |
| 311 | /// copy). The caller is not allowed to modify the returned buffer pointer |
| 312 | /// if an internal buffer is returned. |
| 313 | unsigned Lexer::getSpelling(const Token &Tok, const char *&Buffer, |
| 314 | const SourceManager &SourceMgr, |
| 315 | const LangOptions &Features, bool *Invalid) { |
| 316 | assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 317 | |
| 318 | const char *TokStart = 0; |
| 319 | // NOTE: this has to be checked *before* testing for an IdentifierInfo. |
| 320 | if (Tok.is(tok::raw_identifier)) |
| 321 | TokStart = Tok.getRawIdentifierData(); |
| 322 | else if (const IdentifierInfo *II = Tok.getIdentifierInfo()) { |
| 323 | // Just return the string from the identifier table, which is very quick. |
Chris Lattner | b060727 | 2010-11-17 07:26:20 +0000 | [diff] [blame] | 324 | Buffer = II->getNameStart(); |
| 325 | return II->getLength(); |
| 326 | } |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 327 | |
| 328 | // NOTE: this can be checked even after testing for an IdentifierInfo. |
Chris Lattner | b060727 | 2010-11-17 07:26:20 +0000 | [diff] [blame] | 329 | if (Tok.isLiteral()) |
| 330 | TokStart = Tok.getLiteralData(); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 331 | |
Chris Lattner | b060727 | 2010-11-17 07:26:20 +0000 | [diff] [blame] | 332 | if (TokStart == 0) { |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 333 | // Compute the start of the token in the input lexer buffer. |
Chris Lattner | b060727 | 2010-11-17 07:26:20 +0000 | [diff] [blame] | 334 | bool CharDataInvalid = false; |
| 335 | TokStart = SourceMgr.getCharacterData(Tok.getLocation(), &CharDataInvalid); |
| 336 | if (Invalid) |
| 337 | *Invalid = CharDataInvalid; |
| 338 | if (CharDataInvalid) { |
| 339 | Buffer = ""; |
| 340 | return 0; |
| 341 | } |
| 342 | } |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 343 | |
Chris Lattner | b060727 | 2010-11-17 07:26:20 +0000 | [diff] [blame] | 344 | // If this token contains nothing interesting, return it directly. |
| 345 | if (!Tok.needsCleaning()) { |
| 346 | Buffer = TokStart; |
| 347 | return Tok.getLength(); |
| 348 | } |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 349 | |
Chris Lattner | b060727 | 2010-11-17 07:26:20 +0000 | [diff] [blame] | 350 | // Otherwise, hard case, relex the characters into the string. |
| 351 | char *OutBuf = const_cast<char*>(Buffer); |
| 352 | for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); |
| 353 | Ptr != End; ) { |
| 354 | unsigned CharSize; |
| 355 | *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features); |
| 356 | Ptr += CharSize; |
| 357 | } |
| 358 | assert(unsigned(OutBuf-Buffer) != Tok.getLength() && |
| 359 | "NeedsCleaning flag set on something that didn't need cleaning!"); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 360 | |
Chris Lattner | b060727 | 2010-11-17 07:26:20 +0000 | [diff] [blame] | 361 | return OutBuf-Buffer; |
| 362 | } |
| 363 | |
| 364 | |
| 365 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 366 | static bool isWhitespace(unsigned char c); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 367 | |
Chris Lattner | 9a61194 | 2007-10-17 21:18:47 +0000 | [diff] [blame] | 368 | /// MeasureTokenLength - Relex the token at the specified location and return |
| 369 | /// its length in bytes in the input file. If the token needs cleaning (e.g. |
| 370 | /// includes a trigraph or an escaped newline) then this count includes bytes |
| 371 | /// that are part of that. |
| 372 | unsigned Lexer::MeasureTokenLength(SourceLocation Loc, |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 373 | const SourceManager &SM, |
| 374 | const LangOptions &LangOpts) { |
Chris Lattner | 9a61194 | 2007-10-17 21:18:47 +0000 | [diff] [blame] | 375 | // TODO: this could be special cased for common tokens like identifiers, ')', |
| 376 | // 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] | 377 | // all obviously single-char tokens. This could use |
Chris Lattner | 9a61194 | 2007-10-17 21:18:47 +0000 | [diff] [blame] | 378 | // Lexer::isObviouslySimpleCharacter for example to handle identifiers or |
| 379 | // something. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 380 | |
| 381 | // If this comes from a macro expansion, we really do want the macro name, not |
| 382 | // the token this macro expanded to. |
Chris Lattner | 363fdc2 | 2009-01-26 22:24:27 +0000 | [diff] [blame] | 383 | Loc = SM.getInstantiationLoc(Loc); |
| 384 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 385 | bool Invalid = false; |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 386 | llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 387 | if (Invalid) |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 388 | return 0; |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 389 | |
| 390 | const char *StrData = Buffer.data()+LocInfo.second; |
Chris Lattner | 8350394 | 2009-01-17 08:30:10 +0000 | [diff] [blame] | 391 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 392 | if (isWhitespace(StrData[0])) |
| 393 | return 0; |
| 394 | |
Chris Lattner | 9a61194 | 2007-10-17 21:18:47 +0000 | [diff] [blame] | 395 | // Create a lexer starting at the beginning of this token. |
Sebastian Redl | c3526d8 | 2010-09-30 01:03:03 +0000 | [diff] [blame] | 396 | Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, |
| 397 | Buffer.begin(), StrData, Buffer.end()); |
Chris Lattner | 39de740 | 2009-10-14 15:04:18 +0000 | [diff] [blame] | 398 | TheLexer.SetCommentRetentionState(true); |
Chris Lattner | 9a61194 | 2007-10-17 21:18:47 +0000 | [diff] [blame] | 399 | Token TheTok; |
Chris Lattner | 590f0cc | 2008-10-12 01:15:46 +0000 | [diff] [blame] | 400 | TheLexer.LexFromRawLexer(TheTok); |
Chris Lattner | 9a61194 | 2007-10-17 21:18:47 +0000 | [diff] [blame] | 401 | return TheTok.getLength(); |
| 402 | } |
| 403 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 404 | SourceLocation Lexer::GetBeginningOfToken(SourceLocation Loc, |
| 405 | const SourceManager &SM, |
| 406 | const LangOptions &LangOpts) { |
| 407 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
Douglas Gregor | 3de8424 | 2011-01-31 22:42:36 +0000 | [diff] [blame] | 408 | if (LocInfo.first.isInvalid()) |
| 409 | return Loc; |
| 410 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 411 | bool Invalid = false; |
| 412 | llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); |
| 413 | if (Invalid) |
| 414 | return Loc; |
| 415 | |
| 416 | // Back up from the current location until we hit the beginning of a line |
| 417 | // (or the buffer). We'll relex from that point. |
| 418 | const char *BufStart = Buffer.data(); |
Douglas Gregor | 3de8424 | 2011-01-31 22:42:36 +0000 | [diff] [blame] | 419 | if (LocInfo.second >= Buffer.size()) |
| 420 | return Loc; |
| 421 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 422 | const char *StrData = BufStart+LocInfo.second; |
| 423 | if (StrData[0] == '\n' || StrData[0] == '\r') |
| 424 | return Loc; |
| 425 | |
| 426 | const char *LexStart = StrData; |
| 427 | while (LexStart != BufStart) { |
| 428 | if (LexStart[0] == '\n' || LexStart[0] == '\r') { |
| 429 | ++LexStart; |
| 430 | break; |
| 431 | } |
| 432 | |
| 433 | --LexStart; |
| 434 | } |
| 435 | |
| 436 | // Create a lexer starting at the beginning of this token. |
| 437 | SourceLocation LexerStartLoc = Loc.getFileLocWithOffset(-LocInfo.second); |
| 438 | Lexer TheLexer(LexerStartLoc, LangOpts, BufStart, LexStart, Buffer.end()); |
| 439 | TheLexer.SetCommentRetentionState(true); |
| 440 | |
| 441 | // Lex tokens until we find the token that contains the source location. |
| 442 | Token TheTok; |
| 443 | do { |
| 444 | TheLexer.LexFromRawLexer(TheTok); |
| 445 | |
| 446 | if (TheLexer.getBufferLocation() > StrData) { |
| 447 | // Lexing this token has taken the lexer past the source location we're |
| 448 | // looking for. If the current token encompasses our source location, |
| 449 | // return the beginning of that token. |
| 450 | if (TheLexer.getBufferLocation() - TheTok.getLength() <= StrData) |
| 451 | return TheTok.getLocation(); |
| 452 | |
| 453 | // We ended up skipping over the source location entirely, which means |
| 454 | // that it points into whitespace. We're done here. |
| 455 | break; |
| 456 | } |
| 457 | } while (TheTok.getKind() != tok::eof); |
| 458 | |
| 459 | // We've passed our source location; just return the original source location. |
| 460 | return Loc; |
| 461 | } |
| 462 | |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 463 | namespace { |
| 464 | enum PreambleDirectiveKind { |
| 465 | PDK_Skipped, |
| 466 | PDK_StartIf, |
| 467 | PDK_EndIf, |
| 468 | PDK_Unknown |
| 469 | }; |
| 470 | } |
| 471 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 472 | std::pair<unsigned, bool> |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 473 | Lexer::ComputePreamble(const llvm::MemoryBuffer *Buffer, unsigned MaxLines) { |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 474 | // Create a lexer starting at the beginning of the file. Note that we use a |
| 475 | // "fake" file source location at offset 1 so that the lexer will track our |
| 476 | // position within the file. |
| 477 | const unsigned StartOffset = 1; |
| 478 | SourceLocation StartLoc = SourceLocation::getFromRawEncoding(StartOffset); |
| 479 | LangOptions LangOpts; |
| 480 | Lexer TheLexer(StartLoc, LangOpts, Buffer->getBufferStart(), |
| 481 | Buffer->getBufferStart(), Buffer->getBufferEnd()); |
| 482 | |
| 483 | bool InPreprocessorDirective = false; |
| 484 | Token TheTok; |
| 485 | Token IfStartTok; |
| 486 | unsigned IfCount = 0; |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 487 | unsigned Line = 0; |
| 488 | |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 489 | do { |
| 490 | TheLexer.LexFromRawLexer(TheTok); |
| 491 | |
| 492 | if (InPreprocessorDirective) { |
| 493 | // If we've hit the end of the file, we're done. |
| 494 | if (TheTok.getKind() == tok::eof) { |
| 495 | InPreprocessorDirective = false; |
| 496 | break; |
| 497 | } |
| 498 | |
| 499 | // If we haven't hit the end of the preprocessor directive, skip this |
| 500 | // token. |
| 501 | if (!TheTok.isAtStartOfLine()) |
| 502 | continue; |
| 503 | |
| 504 | // We've passed the end of the preprocessor directive, and will look |
| 505 | // at this token again below. |
| 506 | InPreprocessorDirective = false; |
| 507 | } |
| 508 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 509 | // Keep track of the # of lines in the preamble. |
| 510 | if (TheTok.isAtStartOfLine()) { |
| 511 | ++Line; |
| 512 | |
| 513 | // If we were asked to limit the number of lines in the preamble, |
| 514 | // and we're about to exceed that limit, we're done. |
| 515 | if (MaxLines && Line >= MaxLines) |
| 516 | break; |
| 517 | } |
| 518 | |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 519 | // Comments are okay; skip over them. |
| 520 | if (TheTok.getKind() == tok::comment) |
| 521 | continue; |
| 522 | |
| 523 | if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) { |
| 524 | // This is the start of a preprocessor directive. |
| 525 | Token HashTok = TheTok; |
| 526 | InPreprocessorDirective = true; |
| 527 | |
| 528 | // Figure out which direective this is. Since we're lexing raw tokens, |
| 529 | // we don't have an identifier table available. Instead, just look at |
| 530 | // the raw identifier to recognize and categorize preprocessor directives. |
| 531 | TheLexer.LexFromRawLexer(TheTok); |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 532 | if (TheTok.getKind() == tok::raw_identifier && !TheTok.needsCleaning()) { |
| 533 | llvm::StringRef Keyword(TheTok.getRawIdentifierData(), |
| 534 | TheTok.getLength()); |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 535 | PreambleDirectiveKind PDK |
| 536 | = llvm::StringSwitch<PreambleDirectiveKind>(Keyword) |
| 537 | .Case("include", PDK_Skipped) |
| 538 | .Case("__include_macros", PDK_Skipped) |
| 539 | .Case("define", PDK_Skipped) |
| 540 | .Case("undef", PDK_Skipped) |
| 541 | .Case("line", PDK_Skipped) |
| 542 | .Case("error", PDK_Skipped) |
| 543 | .Case("pragma", PDK_Skipped) |
| 544 | .Case("import", PDK_Skipped) |
| 545 | .Case("include_next", PDK_Skipped) |
| 546 | .Case("warning", PDK_Skipped) |
| 547 | .Case("ident", PDK_Skipped) |
| 548 | .Case("sccs", PDK_Skipped) |
| 549 | .Case("assert", PDK_Skipped) |
| 550 | .Case("unassert", PDK_Skipped) |
| 551 | .Case("if", PDK_StartIf) |
| 552 | .Case("ifdef", PDK_StartIf) |
| 553 | .Case("ifndef", PDK_StartIf) |
| 554 | .Case("elif", PDK_Skipped) |
| 555 | .Case("else", PDK_Skipped) |
| 556 | .Case("endif", PDK_EndIf) |
| 557 | .Default(PDK_Unknown); |
| 558 | |
| 559 | switch (PDK) { |
| 560 | case PDK_Skipped: |
| 561 | continue; |
| 562 | |
| 563 | case PDK_StartIf: |
| 564 | if (IfCount == 0) |
| 565 | IfStartTok = HashTok; |
| 566 | |
| 567 | ++IfCount; |
| 568 | continue; |
| 569 | |
| 570 | case PDK_EndIf: |
| 571 | // Mismatched #endif. The preamble ends here. |
| 572 | if (IfCount == 0) |
| 573 | break; |
| 574 | |
| 575 | --IfCount; |
| 576 | continue; |
| 577 | |
| 578 | case PDK_Unknown: |
| 579 | // We don't know what this directive is; stop at the '#'. |
| 580 | break; |
| 581 | } |
| 582 | } |
| 583 | |
| 584 | // We only end up here if we didn't recognize the preprocessor |
| 585 | // directive or it was one that can't occur in the preamble at this |
| 586 | // point. Roll back the current token to the location of the '#'. |
| 587 | InPreprocessorDirective = false; |
| 588 | TheTok = HashTok; |
| 589 | } |
| 590 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 591 | // We hit a token that we don't recognize as being in the |
| 592 | // "preprocessing only" part of the file, so we're no longer in |
| 593 | // the preamble. |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 594 | break; |
| 595 | } while (true); |
| 596 | |
| 597 | SourceLocation End = IfCount? IfStartTok.getLocation() : TheTok.getLocation(); |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 598 | return std::make_pair(End.getRawEncoding() - StartLoc.getRawEncoding(), |
| 599 | IfCount? IfStartTok.isAtStartOfLine() |
| 600 | : TheTok.isAtStartOfLine()); |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 601 | } |
| 602 | |
Chris Lattner | 7ef5c27 | 2010-11-17 07:05:50 +0000 | [diff] [blame] | 603 | |
| 604 | /// AdvanceToTokenCharacter - Given a location that specifies the start of a |
| 605 | /// token, return a new location that specifies a character within the token. |
| 606 | SourceLocation Lexer::AdvanceToTokenCharacter(SourceLocation TokStart, |
| 607 | unsigned CharNo, |
| 608 | const SourceManager &SM, |
| 609 | const LangOptions &Features) { |
| 610 | // Figure out how many physical characters away the specified instantiation |
| 611 | // character is. This needs to take into consideration newlines and |
| 612 | // trigraphs. |
| 613 | bool Invalid = false; |
| 614 | const char *TokPtr = SM.getCharacterData(TokStart, &Invalid); |
| 615 | |
| 616 | // If they request the first char of the token, we're trivially done. |
| 617 | if (Invalid || (CharNo == 0 && Lexer::isObviouslySimpleCharacter(*TokPtr))) |
| 618 | return TokStart; |
| 619 | |
| 620 | unsigned PhysOffset = 0; |
| 621 | |
| 622 | // The usual case is that tokens don't contain anything interesting. Skip |
| 623 | // over the uninteresting characters. If a token only consists of simple |
| 624 | // chars, this method is extremely fast. |
| 625 | while (Lexer::isObviouslySimpleCharacter(*TokPtr)) { |
| 626 | if (CharNo == 0) |
| 627 | return TokStart.getFileLocWithOffset(PhysOffset); |
| 628 | ++TokPtr, --CharNo, ++PhysOffset; |
| 629 | } |
| 630 | |
| 631 | // If we have a character that may be a trigraph or escaped newline, use a |
| 632 | // lexer to parse it correctly. |
| 633 | for (; CharNo; --CharNo) { |
| 634 | unsigned Size; |
| 635 | Lexer::getCharAndSizeNoWarn(TokPtr, Size, Features); |
| 636 | TokPtr += Size; |
| 637 | PhysOffset += Size; |
| 638 | } |
| 639 | |
| 640 | // Final detail: if we end up on an escaped newline, we want to return the |
| 641 | // location of the actual byte of the token. For example foo\<newline>bar |
| 642 | // advanced by 3 should return the location of b, not of \\. One compounding |
| 643 | // detail of this is that the escape may be made by a trigraph. |
| 644 | if (!Lexer::isObviouslySimpleCharacter(*TokPtr)) |
| 645 | PhysOffset += Lexer::SkipEscapedNewLines(TokPtr)-TokPtr; |
| 646 | |
| 647 | return TokStart.getFileLocWithOffset(PhysOffset); |
| 648 | } |
| 649 | |
| 650 | /// \brief Computes the source location just past the end of the |
| 651 | /// token at this source location. |
| 652 | /// |
| 653 | /// This routine can be used to produce a source location that |
| 654 | /// points just past the end of the token referenced by \p Loc, and |
| 655 | /// is generally used when a diagnostic needs to point just after a |
| 656 | /// token where it expected something different that it received. If |
| 657 | /// the returned source location would not be meaningful (e.g., if |
| 658 | /// it points into a macro), this routine returns an invalid |
| 659 | /// source location. |
| 660 | /// |
| 661 | /// \param Offset an offset from the end of the token, where the source |
| 662 | /// location should refer to. The default offset (0) produces a source |
| 663 | /// location pointing just past the end of the token; an offset of 1 produces |
| 664 | /// a source location pointing to the last character in the token, etc. |
| 665 | SourceLocation Lexer::getLocForEndOfToken(SourceLocation Loc, unsigned Offset, |
| 666 | const SourceManager &SM, |
| 667 | const LangOptions &Features) { |
| 668 | if (Loc.isInvalid() || !Loc.isFileID()) |
| 669 | return SourceLocation(); |
| 670 | |
| 671 | unsigned Len = Lexer::MeasureTokenLength(Loc, SM, Features); |
| 672 | if (Len > Offset) |
| 673 | Len = Len - Offset; |
| 674 | else |
| 675 | return Loc; |
| 676 | |
| 677 | return AdvanceToTokenCharacter(Loc, Len, SM, Features); |
| 678 | } |
| 679 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 680 | //===----------------------------------------------------------------------===// |
| 681 | // Character information. |
| 682 | //===----------------------------------------------------------------------===// |
| 683 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 684 | enum { |
| 685 | CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0' |
| 686 | CHAR_VERT_WS = 0x02, // '\r', '\n' |
| 687 | CHAR_LETTER = 0x04, // a-z,A-Z |
| 688 | CHAR_NUMBER = 0x08, // 0-9 |
| 689 | CHAR_UNDER = 0x10, // _ |
| 690 | CHAR_PERIOD = 0x20 // . |
| 691 | }; |
| 692 | |
Chris Lattner | 03b9866 | 2009-07-07 17:09:54 +0000 | [diff] [blame] | 693 | // Statically initialize CharInfo table based on ASCII character set |
| 694 | // Reference: FreeBSD 7.2 /usr/share/misc/ascii |
Chris Lattner | a2bf105 | 2009-12-17 05:29:40 +0000 | [diff] [blame] | 695 | static const unsigned char CharInfo[256] = |
Chris Lattner | 03b9866 | 2009-07-07 17:09:54 +0000 | [diff] [blame] | 696 | { |
| 697 | // 0 NUL 1 SOH 2 STX 3 ETX |
| 698 | // 4 EOT 5 ENQ 6 ACK 7 BEL |
| 699 | 0 , 0 , 0 , 0 , |
| 700 | 0 , 0 , 0 , 0 , |
| 701 | // 8 BS 9 HT 10 NL 11 VT |
| 702 | //12 NP 13 CR 14 SO 15 SI |
| 703 | 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS, |
| 704 | CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 , |
| 705 | //16 DLE 17 DC1 18 DC2 19 DC3 |
| 706 | //20 DC4 21 NAK 22 SYN 23 ETB |
| 707 | 0 , 0 , 0 , 0 , |
| 708 | 0 , 0 , 0 , 0 , |
| 709 | //24 CAN 25 EM 26 SUB 27 ESC |
| 710 | //28 FS 29 GS 30 RS 31 US |
| 711 | 0 , 0 , 0 , 0 , |
| 712 | 0 , 0 , 0 , 0 , |
| 713 | //32 SP 33 ! 34 " 35 # |
| 714 | //36 $ 37 % 38 & 39 ' |
| 715 | CHAR_HORZ_WS, 0 , 0 , 0 , |
| 716 | 0 , 0 , 0 , 0 , |
| 717 | //40 ( 41 ) 42 * 43 + |
| 718 | //44 , 45 - 46 . 47 / |
| 719 | 0 , 0 , 0 , 0 , |
| 720 | 0 , 0 , CHAR_PERIOD , 0 , |
| 721 | //48 0 49 1 50 2 51 3 |
| 722 | //52 4 53 5 54 6 55 7 |
| 723 | CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , |
| 724 | CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , |
| 725 | //56 8 57 9 58 : 59 ; |
| 726 | //60 < 61 = 62 > 63 ? |
| 727 | CHAR_NUMBER , CHAR_NUMBER , 0 , 0 , |
| 728 | 0 , 0 , 0 , 0 , |
| 729 | //64 @ 65 A 66 B 67 C |
| 730 | //68 D 69 E 70 F 71 G |
| 731 | 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 732 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 733 | //72 H 73 I 74 J 75 K |
| 734 | //76 L 77 M 78 N 79 O |
| 735 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 736 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 737 | //80 P 81 Q 82 R 83 S |
| 738 | //84 T 85 U 86 V 87 W |
| 739 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 740 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 741 | //88 X 89 Y 90 Z 91 [ |
| 742 | //92 \ 93 ] 94 ^ 95 _ |
| 743 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 , |
| 744 | 0 , 0 , 0 , CHAR_UNDER , |
| 745 | //96 ` 97 a 98 b 99 c |
| 746 | //100 d 101 e 102 f 103 g |
| 747 | 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 748 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 749 | //104 h 105 i 106 j 107 k |
| 750 | //108 l 109 m 110 n 111 o |
| 751 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 752 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 753 | //112 p 113 q 114 r 115 s |
| 754 | //116 t 117 u 118 v 119 w |
| 755 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 756 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 757 | //120 x 121 y 122 z 123 { |
| 758 | //124 | 125 } 126 ~ 127 DEL |
| 759 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 , |
| 760 | 0 , 0 , 0 , 0 |
| 761 | }; |
| 762 | |
Chris Lattner | a2bf105 | 2009-12-17 05:29:40 +0000 | [diff] [blame] | 763 | static void InitCharacterInfo() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 764 | static bool isInited = false; |
| 765 | if (isInited) return; |
Chris Lattner | 03b9866 | 2009-07-07 17:09:54 +0000 | [diff] [blame] | 766 | // check the statically-initialized CharInfo table |
| 767 | assert(CHAR_HORZ_WS == CharInfo[(int)' ']); |
| 768 | assert(CHAR_HORZ_WS == CharInfo[(int)'\t']); |
| 769 | assert(CHAR_HORZ_WS == CharInfo[(int)'\f']); |
| 770 | assert(CHAR_HORZ_WS == CharInfo[(int)'\v']); |
| 771 | assert(CHAR_VERT_WS == CharInfo[(int)'\n']); |
| 772 | assert(CHAR_VERT_WS == CharInfo[(int)'\r']); |
| 773 | assert(CHAR_UNDER == CharInfo[(int)'_']); |
| 774 | assert(CHAR_PERIOD == CharInfo[(int)'.']); |
| 775 | for (unsigned i = 'a'; i <= 'z'; ++i) { |
| 776 | assert(CHAR_LETTER == CharInfo[i]); |
| 777 | assert(CHAR_LETTER == CharInfo[i+'A'-'a']); |
| 778 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 779 | for (unsigned i = '0'; i <= '9'; ++i) |
Chris Lattner | 03b9866 | 2009-07-07 17:09:54 +0000 | [diff] [blame] | 780 | assert(CHAR_NUMBER == CharInfo[i]); |
Steve Naroff | 7b68265 | 2009-12-08 16:38:12 +0000 | [diff] [blame] | 781 | |
Chris Lattner | 03b9866 | 2009-07-07 17:09:54 +0000 | [diff] [blame] | 782 | isInited = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 783 | } |
| 784 | |
Chris Lattner | 03b9866 | 2009-07-07 17:09:54 +0000 | [diff] [blame] | 785 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 786 | /// isIdentifierBody - Return true if this is the body character of an |
| 787 | /// identifier, which is [a-zA-Z0-9_]. |
| 788 | static inline bool isIdentifierBody(unsigned char c) { |
Hartmut Kaiser | 95c062b | 2007-10-18 12:47:01 +0000 | [diff] [blame] | 789 | return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | /// isHorizontalWhitespace - Return true if this character is horizontal |
| 793 | /// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'. |
| 794 | static inline bool isHorizontalWhitespace(unsigned char c) { |
Hartmut Kaiser | 95c062b | 2007-10-18 12:47:01 +0000 | [diff] [blame] | 795 | return (CharInfo[c] & CHAR_HORZ_WS) ? true : false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 796 | } |
| 797 | |
| 798 | /// isWhitespace - Return true if this character is horizontal or vertical |
| 799 | /// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false |
| 800 | /// for '\0'. |
| 801 | static inline bool isWhitespace(unsigned char c) { |
Hartmut Kaiser | 95c062b | 2007-10-18 12:47:01 +0000 | [diff] [blame] | 802 | return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | /// isNumberBody - Return true if this is the body character of an |
| 806 | /// preprocessing number, which is [a-zA-Z0-9_.]. |
| 807 | static inline bool isNumberBody(unsigned char c) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 808 | return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ? |
Hartmut Kaiser | 95c062b | 2007-10-18 12:47:01 +0000 | [diff] [blame] | 809 | true : false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | |
| 813 | //===----------------------------------------------------------------------===// |
| 814 | // Diagnostics forwarding code. |
| 815 | //===----------------------------------------------------------------------===// |
| 816 | |
Chris Lattner | 409a036 | 2007-07-22 18:38:25 +0000 | [diff] [blame] | 817 | /// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the |
| 818 | /// lexer buffer was all instantiated at a single point, perform the mapping. |
| 819 | /// This is currently only used for _Pragma implementation, so it is the slow |
| 820 | /// path of the hot getSourceLocation method. Do not allow it to be inlined. |
Chandler Carruth | 14bd965 | 2010-10-23 08:44:57 +0000 | [diff] [blame] | 821 | static LLVM_ATTRIBUTE_NOINLINE SourceLocation GetMappedTokenLoc( |
| 822 | Preprocessor &PP, SourceLocation FileLoc, unsigned CharNo, unsigned TokLen); |
Chris Lattner | 409a036 | 2007-07-22 18:38:25 +0000 | [diff] [blame] | 823 | static SourceLocation GetMappedTokenLoc(Preprocessor &PP, |
| 824 | SourceLocation FileLoc, |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 825 | unsigned CharNo, unsigned TokLen) { |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 826 | assert(FileLoc.isMacroID() && "Must be an instantiation"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 827 | |
Chris Lattner | 409a036 | 2007-07-22 18:38:25 +0000 | [diff] [blame] | 828 | // Otherwise, we're lexing "mapped tokens". This is used for things like |
| 829 | // _Pragma handling. Combine the instantiation location of FileLoc with the |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 830 | // spelling location. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 831 | SourceManager &SM = PP.getSourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 832 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 833 | // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 834 | // characters come from spelling(FileLoc)+Offset. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 835 | SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc); |
Chris Lattner | bcc2a67 | 2009-01-19 06:46:35 +0000 | [diff] [blame] | 836 | SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 837 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 838 | // Figure out the expansion loc range, which is the range covered by the |
| 839 | // original _Pragma(...) sequence. |
| 840 | std::pair<SourceLocation,SourceLocation> II = |
| 841 | SM.getImmediateInstantiationRange(FileLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 842 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 843 | return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen); |
Chris Lattner | 409a036 | 2007-07-22 18:38:25 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 846 | /// getSourceLocation - Return a source location identifier for the specified |
| 847 | /// offset in the current file. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 848 | SourceLocation Lexer::getSourceLocation(const char *Loc, |
| 849 | unsigned TokLen) const { |
Chris Lattner | 448cec4 | 2007-07-22 18:44:36 +0000 | [diff] [blame] | 850 | assert(Loc >= BufferStart && Loc <= BufferEnd && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 851 | "Location out of range for this buffer!"); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 852 | |
| 853 | // In the normal case, we're just lexing from a simple file buffer, return |
| 854 | // the file id from FileLoc with the offset specified. |
Chris Lattner | 448cec4 | 2007-07-22 18:44:36 +0000 | [diff] [blame] | 855 | unsigned CharNo = Loc-BufferStart; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 856 | if (FileLoc.isFileID()) |
Chris Lattner | bcc2a67 | 2009-01-19 06:46:35 +0000 | [diff] [blame] | 857 | return FileLoc.getFileLocWithOffset(CharNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 858 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 859 | // Otherwise, this is the _Pragma lexer case, which pretends that all of the |
| 860 | // tokens are lexed from where the _Pragma was defined. |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 861 | assert(PP && "This doesn't work on raw lexers"); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 862 | return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 865 | /// Diag - Forwarding function for diagnostics. This translate a source |
| 866 | /// position in the current buffer into a SourceLocation object for rendering. |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 867 | DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const { |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 868 | return PP->Diag(getSourceLocation(Loc), DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 869 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 870 | |
| 871 | //===----------------------------------------------------------------------===// |
| 872 | // Trigraph and Escaped Newline Handling Code. |
| 873 | //===----------------------------------------------------------------------===// |
| 874 | |
| 875 | /// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair, |
| 876 | /// return the decoded trigraph letter it corresponds to, or '\0' if nothing. |
| 877 | static char GetTrigraphCharForLetter(char Letter) { |
| 878 | switch (Letter) { |
| 879 | default: return 0; |
| 880 | case '=': return '#'; |
| 881 | case ')': return ']'; |
| 882 | case '(': return '['; |
| 883 | case '!': return '|'; |
| 884 | case '\'': return '^'; |
| 885 | case '>': return '}'; |
| 886 | case '/': return '\\'; |
| 887 | case '<': return '{'; |
| 888 | case '-': return '~'; |
| 889 | } |
| 890 | } |
| 891 | |
| 892 | /// DecodeTrigraphChar - If the specified character is a legal trigraph when |
| 893 | /// prefixed with ??, emit a trigraph warning. If trigraphs are enabled, |
| 894 | /// return the result character. Finally, emit a warning about trigraph use |
| 895 | /// whether trigraphs are enabled or not. |
| 896 | static char DecodeTrigraphChar(const char *CP, Lexer *L) { |
| 897 | char Res = GetTrigraphCharForLetter(*CP); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 898 | if (!Res || !L) return Res; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 899 | |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 900 | if (!L->getFeatures().Trigraphs) { |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 901 | if (!L->isLexingRawMode()) |
| 902 | L->Diag(CP-2, diag::trigraph_ignored); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 903 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 904 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 905 | |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 906 | if (!L->isLexingRawMode()) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 907 | L->Diag(CP-2, diag::trigraph_converted) << llvm::StringRef(&Res, 1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 908 | return Res; |
| 909 | } |
| 910 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 911 | /// getEscapedNewLineSize - Return the size of the specified escaped newline, |
| 912 | /// 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] | 913 | /// trigraph equivalent on entry to this function. |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 914 | unsigned Lexer::getEscapedNewLineSize(const char *Ptr) { |
| 915 | unsigned Size = 0; |
| 916 | while (isWhitespace(Ptr[Size])) { |
| 917 | ++Size; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 918 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 919 | if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r') |
| 920 | continue; |
| 921 | |
| 922 | // If this is a \r\n or \n\r, skip the other half. |
| 923 | if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') && |
| 924 | Ptr[Size-1] != Ptr[Size]) |
| 925 | ++Size; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 926 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 927 | return Size; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 928 | } |
| 929 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 930 | // Not an escaped newline, must be a \t or something else. |
| 931 | return 0; |
| 932 | } |
| 933 | |
Chris Lattner | 0337495 | 2009-04-18 22:27:02 +0000 | [diff] [blame] | 934 | /// SkipEscapedNewLines - If P points to an escaped newline (or a series of |
| 935 | /// them), skip over them and return the first non-escaped-newline found, |
| 936 | /// otherwise return P. |
| 937 | const char *Lexer::SkipEscapedNewLines(const char *P) { |
| 938 | while (1) { |
| 939 | const char *AfterEscape; |
| 940 | if (*P == '\\') { |
| 941 | AfterEscape = P+1; |
| 942 | } else if (*P == '?') { |
| 943 | // If not a trigraph for escape, bail out. |
| 944 | if (P[1] != '?' || P[2] != '/') |
| 945 | return P; |
| 946 | AfterEscape = P+3; |
| 947 | } else { |
| 948 | return P; |
| 949 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 950 | |
Chris Lattner | 0337495 | 2009-04-18 22:27:02 +0000 | [diff] [blame] | 951 | unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape); |
| 952 | if (NewLineSize == 0) return P; |
| 953 | P = AfterEscape+NewLineSize; |
| 954 | } |
| 955 | } |
| 956 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 957 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 958 | /// getCharAndSizeSlow - Peek a single 'character' from the specified buffer, |
| 959 | /// get its size, and return it. This is tricky in several cases: |
| 960 | /// 1. If currently at the start of a trigraph, we warn about the trigraph, |
| 961 | /// then either return the trigraph (skipping 3 chars) or the '?', |
| 962 | /// depending on whether trigraphs are enabled or not. |
| 963 | /// 2. If this is an escaped newline (potentially with whitespace between |
| 964 | /// the backslash and newline), implicitly skip the newline and return |
| 965 | /// the char after it. |
| 966 | /// 3. If this is a UCN, return it. FIXME: C++ UCN's? |
| 967 | /// |
| 968 | /// This handles the slow/uncommon case of the getCharAndSize method. Here we |
| 969 | /// know that we can accumulate into Size, and that we have already incremented |
| 970 | /// Ptr by Size bytes. |
| 971 | /// |
| 972 | /// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should |
| 973 | /// be updated to match. |
| 974 | /// |
| 975 | char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size, |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 976 | Token *Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 977 | // If we have a slash, look for an escaped newline. |
| 978 | if (Ptr[0] == '\\') { |
| 979 | ++Size; |
| 980 | ++Ptr; |
| 981 | Slash: |
| 982 | // Common case, backslash-char where the char is not whitespace. |
| 983 | if (!isWhitespace(Ptr[0])) return '\\'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 984 | |
Chris Lattner | 5636a3b | 2009-06-23 05:15:06 +0000 | [diff] [blame] | 985 | // See if we have optional whitespace characters between the slash and |
| 986 | // newline. |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 987 | if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { |
| 988 | // Remember that this token needs to be cleaned. |
| 989 | if (Tok) Tok->setFlag(Token::NeedsCleaning); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 990 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 991 | // Warn if there was whitespace between the backslash and newline. |
Chris Lattner | 5636a3b | 2009-06-23 05:15:06 +0000 | [diff] [blame] | 992 | if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode()) |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 993 | Diag(Ptr, diag::backslash_newline_space); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 994 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 995 | // Found backslash<whitespace><newline>. Parse the char after it. |
| 996 | Size += EscapedNewLineSize; |
| 997 | Ptr += EscapedNewLineSize; |
| 998 | // Use slow version to accumulate a correct size field. |
| 999 | return getCharAndSizeSlow(Ptr, Size, Tok); |
| 1000 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1001 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1002 | // Otherwise, this is not an escaped newline, just return the slash. |
| 1003 | return '\\'; |
| 1004 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1005 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1006 | // If this is a trigraph, process it. |
| 1007 | if (Ptr[0] == '?' && Ptr[1] == '?') { |
| 1008 | // If this is actually a legal trigraph (not something like "??x"), emit |
| 1009 | // a trigraph warning. If so, and if trigraphs are enabled, return it. |
| 1010 | if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) { |
| 1011 | // Remember that this token needs to be cleaned. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1012 | if (Tok) Tok->setFlag(Token::NeedsCleaning); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1013 | |
| 1014 | Ptr += 3; |
| 1015 | Size += 3; |
| 1016 | if (C == '\\') goto Slash; |
| 1017 | return C; |
| 1018 | } |
| 1019 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1020 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1021 | // If this is neither, return a single character. |
| 1022 | ++Size; |
| 1023 | return *Ptr; |
| 1024 | } |
| 1025 | |
| 1026 | |
| 1027 | /// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the |
| 1028 | /// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size, |
| 1029 | /// and that we have already incremented Ptr by Size bytes. |
| 1030 | /// |
| 1031 | /// NOTE: When this method is updated, getCharAndSizeSlow (above) should |
| 1032 | /// be updated to match. |
| 1033 | char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size, |
| 1034 | const LangOptions &Features) { |
| 1035 | // If we have a slash, look for an escaped newline. |
| 1036 | if (Ptr[0] == '\\') { |
| 1037 | ++Size; |
| 1038 | ++Ptr; |
| 1039 | Slash: |
| 1040 | // Common case, backslash-char where the char is not whitespace. |
| 1041 | if (!isWhitespace(Ptr[0])) return '\\'; |
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 | // See if we have optional whitespace characters followed by a newline. |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 1044 | if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { |
| 1045 | // Found backslash<whitespace><newline>. Parse the char after it. |
| 1046 | Size += EscapedNewLineSize; |
| 1047 | Ptr += EscapedNewLineSize; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1048 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 1049 | // Use slow version to accumulate a correct size field. |
| 1050 | return getCharAndSizeSlowNoWarn(Ptr, Size, Features); |
| 1051 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1052 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1053 | // Otherwise, this is not an escaped newline, just return the slash. |
| 1054 | return '\\'; |
| 1055 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1056 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1057 | // If this is a trigraph, process it. |
| 1058 | if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') { |
| 1059 | // If this is actually a legal trigraph (not something like "??x"), return |
| 1060 | // it. |
| 1061 | if (char C = GetTrigraphCharForLetter(Ptr[2])) { |
| 1062 | Ptr += 3; |
| 1063 | Size += 3; |
| 1064 | if (C == '\\') goto Slash; |
| 1065 | return C; |
| 1066 | } |
| 1067 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1068 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1069 | // If this is neither, return a single character. |
| 1070 | ++Size; |
| 1071 | return *Ptr; |
| 1072 | } |
| 1073 | |
| 1074 | //===----------------------------------------------------------------------===// |
| 1075 | // Helper methods for lexing. |
| 1076 | //===----------------------------------------------------------------------===// |
| 1077 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 1078 | /// \brief Routine that indiscriminately skips bytes in the source file. |
| 1079 | void Lexer::SkipBytes(unsigned Bytes, bool StartOfLine) { |
| 1080 | BufferPtr += Bytes; |
| 1081 | if (BufferPtr > BufferEnd) |
| 1082 | BufferPtr = BufferEnd; |
| 1083 | IsAtStartOfLine = StartOfLine; |
| 1084 | } |
| 1085 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1086 | void Lexer::LexIdentifier(Token &Result, const char *CurPtr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1087 | // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$] |
| 1088 | unsigned Size; |
| 1089 | unsigned char C = *CurPtr++; |
Chris Lattner | cd991db | 2010-01-11 02:38:50 +0000 | [diff] [blame] | 1090 | while (isIdentifierBody(C)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1091 | C = *CurPtr++; |
Chris Lattner | cd991db | 2010-01-11 02:38:50 +0000 | [diff] [blame] | 1092 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1093 | --CurPtr; // Back up over the skipped character. |
| 1094 | |
| 1095 | // Fast path, no $,\,? in identifier found. '\' might be an escaped newline |
| 1096 | // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN. |
| 1097 | // FIXME: UCNs. |
Chris Lattner | cd991db | 2010-01-11 02:38:50 +0000 | [diff] [blame] | 1098 | // |
| 1099 | // TODO: Could merge these checks into a CharInfo flag to make the comparison |
| 1100 | // cheaper |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1101 | if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) { |
| 1102 | FinishIdentifier: |
| 1103 | const char *IdStart = BufferPtr; |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 1104 | FormTokenWithChars(Result, CurPtr, tok::raw_identifier); |
| 1105 | Result.setRawIdentifierData(IdStart); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1106 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1107 | // If we are in raw mode, return this identifier raw. There is no need to |
| 1108 | // look up identifier information or attempt to macro expand it. |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 1109 | if (LexingRawMode) |
| 1110 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1111 | |
Abramo Bagnara | c4bf2b9 | 2010-12-22 08:23:18 +0000 | [diff] [blame] | 1112 | // Fill in Result.IdentifierInfo and update the token kind, |
| 1113 | // looking up the identifier in the identifier table. |
| 1114 | IdentifierInfo *II = PP->LookUpIdentifierInfo(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1115 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1116 | // Finally, now that we know we have an identifier, pass this off to the |
| 1117 | // preprocessor, which may macro expand it or something. |
Chris Lattner | d1186fa | 2009-01-21 07:45:14 +0000 | [diff] [blame] | 1118 | if (II->isHandleIdentifierCase()) |
Chris Lattner | 6a170eb | 2009-01-21 07:43:11 +0000 | [diff] [blame] | 1119 | PP->HandleIdentifier(Result); |
| 1120 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1121 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1122 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1123 | // Otherwise, $,\,? in identifier found. Enter slower path. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1124 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1125 | C = getCharAndSize(CurPtr, Size); |
| 1126 | while (1) { |
| 1127 | if (C == '$') { |
| 1128 | // If we hit a $ and they are not supported in identifiers, we are done. |
| 1129 | if (!Features.DollarIdents) goto FinishIdentifier; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1130 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1131 | // Otherwise, emit a diagnostic and continue. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1132 | if (!isLexingRawMode()) |
| 1133 | Diag(CurPtr, diag::ext_dollar_in_identifier); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1134 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 1135 | C = getCharAndSize(CurPtr, Size); |
| 1136 | continue; |
| 1137 | } else if (!isIdentifierBody(C)) { // FIXME: UCNs. |
| 1138 | // Found end of identifier. |
| 1139 | goto FinishIdentifier; |
| 1140 | } |
| 1141 | |
| 1142 | // Otherwise, this character is good, consume it. |
| 1143 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 1144 | |
| 1145 | C = getCharAndSize(CurPtr, Size); |
| 1146 | while (isIdentifierBody(C)) { // FIXME: UCNs. |
| 1147 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 1148 | C = getCharAndSize(CurPtr, Size); |
| 1149 | } |
| 1150 | } |
| 1151 | } |
| 1152 | |
Douglas Gregor | a75ec43 | 2010-08-30 14:50:47 +0000 | [diff] [blame] | 1153 | /// isHexaLiteral - Return true if Start points to a hex constant. |
Chris Lattner | 4a55100 | 2010-08-30 17:11:14 +0000 | [diff] [blame] | 1154 | /// in microsoft mode (where this is supposed to be several different tokens). |
Chris Lattner | 6ab55eb | 2010-08-31 16:42:00 +0000 | [diff] [blame] | 1155 | static bool isHexaLiteral(const char *Start, const LangOptions &Features) { |
| 1156 | unsigned Size; |
| 1157 | char C1 = Lexer::getCharAndSizeNoWarn(Start, Size, Features); |
| 1158 | if (C1 != '0') |
| 1159 | return false; |
| 1160 | char C2 = Lexer::getCharAndSizeNoWarn(Start + Size, Size, Features); |
| 1161 | return (C2 == 'x' || C2 == 'X'); |
Douglas Gregor | a75ec43 | 2010-08-30 14:50:47 +0000 | [diff] [blame] | 1162 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1163 | |
Nate Begeman | 5253c7f | 2008-04-14 02:26:39 +0000 | [diff] [blame] | 1164 | /// LexNumericConstant - Lex the remainder of a integer or floating point |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1165 | /// constant. From[-1] is the first character lexed. Return the end of the |
| 1166 | /// constant. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1167 | void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1168 | unsigned Size; |
| 1169 | char C = getCharAndSize(CurPtr, Size); |
| 1170 | char PrevCh = 0; |
| 1171 | while (isNumberBody(C)) { // FIXME: UCNs? |
| 1172 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 1173 | PrevCh = C; |
| 1174 | C = getCharAndSize(CurPtr, Size); |
| 1175 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1176 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1177 | // If we fell out, check for a sign, due to 1e+12. If we have one, continue. |
Chris Lattner | b2f4a20 | 2010-08-30 17:09:08 +0000 | [diff] [blame] | 1178 | if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e')) { |
| 1179 | // If we are in Microsoft mode, don't continue if the constant is hex. |
| 1180 | // For example, MSVC will accept the following as 3 tokens: 0x1234567e+1 |
Chris Lattner | 6ab55eb | 2010-08-31 16:42:00 +0000 | [diff] [blame] | 1181 | if (!Features.Microsoft || !isHexaLiteral(BufferPtr, Features)) |
Chris Lattner | b2f4a20 | 2010-08-30 17:09:08 +0000 | [diff] [blame] | 1182 | return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); |
| 1183 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1184 | |
| 1185 | // If we have a hex FP constant, continue. |
Sean Hunt | 8c72340 | 2010-01-10 23:37:56 +0000 | [diff] [blame] | 1186 | if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') && |
Chris Lattner | b2f4a20 | 2010-08-30 17:09:08 +0000 | [diff] [blame] | 1187 | !Features.CPlusPlus0x) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1188 | return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1189 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1190 | // Update the location of token as well as BufferPtr. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 1191 | const char *TokStart = BufferPtr; |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1192 | FormTokenWithChars(Result, CurPtr, tok::numeric_constant); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 1193 | Result.setLiteralData(TokStart); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | /// LexStringLiteral - Lex the remainder of a string literal, after having lexed |
| 1197 | /// either " or L". |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1198 | void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1199 | const char *NulCharacter = 0; // Does this string contain the \0 character? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1201 | char C = getAndAdvanceChar(CurPtr, Result); |
| 1202 | while (C != '"') { |
Chris Lattner | 571339c | 2010-05-30 23:27:38 +0000 | [diff] [blame] | 1203 | // Skip escaped characters. Escaped newlines will already be processed by |
| 1204 | // getAndAdvanceChar. |
| 1205 | if (C == '\\') |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1206 | C = getAndAdvanceChar(CurPtr, Result); |
Douglas Gregor | 33611e0 | 2010-05-30 22:59:50 +0000 | [diff] [blame] | 1207 | |
Chris Lattner | 571339c | 2010-05-30 23:27:38 +0000 | [diff] [blame] | 1208 | if (C == '\n' || C == '\r' || // Newline. |
Douglas Gregor | 33611e0 | 2010-05-30 22:59:50 +0000 | [diff] [blame] | 1209 | (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 1210 | if (C == 0 && PP && PP->isCodeCompletionFile(FileLoc)) |
| 1211 | PP->CodeCompleteNaturalLanguage(); |
| 1212 | else if (!isLexingRawMode() && !Features.AsmPreprocessor) |
Argyrios Kyrtzidis | ff1ed98 | 2011-02-15 23:45:31 +0000 | [diff] [blame] | 1213 | Diag(BufferPtr, diag::warn_unterminated_string); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1214 | FormTokenWithChars(Result, CurPtr-1, tok::unknown); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1215 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1216 | } |
Chris Lattner | 571339c | 2010-05-30 23:27:38 +0000 | [diff] [blame] | 1217 | |
| 1218 | if (C == 0) |
| 1219 | NulCharacter = CurPtr-1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1220 | C = getAndAdvanceChar(CurPtr, Result); |
| 1221 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1222 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1223 | // If a nul character existed in the string, warn about it. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1224 | if (NulCharacter && !isLexingRawMode()) |
| 1225 | Diag(NulCharacter, diag::null_in_string); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1226 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1227 | // 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] | 1228 | const char *TokStart = BufferPtr; |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1229 | FormTokenWithChars(Result, CurPtr, |
| 1230 | Wide ? tok::wide_string_literal : tok::string_literal); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 1231 | Result.setLiteralData(TokStart); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
| 1234 | /// LexAngledStringLiteral - Lex the remainder of an angled string literal, |
| 1235 | /// after having lexed the '<' character. This is used for #include filenames. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1236 | void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1237 | const char *NulCharacter = 0; // Does this string contain the \0 character? |
Chris Lattner | 9cb51ce | 2009-04-17 23:56:52 +0000 | [diff] [blame] | 1238 | const char *AfterLessPos = CurPtr; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1239 | char C = getAndAdvanceChar(CurPtr, Result); |
| 1240 | while (C != '>') { |
| 1241 | // Skip escaped characters. |
| 1242 | if (C == '\\') { |
| 1243 | // Skip the escaped character. |
| 1244 | C = getAndAdvanceChar(CurPtr, Result); |
| 1245 | } else if (C == '\n' || C == '\r' || // Newline. |
| 1246 | (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. |
Chris Lattner | 9cb51ce | 2009-04-17 23:56:52 +0000 | [diff] [blame] | 1247 | // If the filename is unterminated, then it must just be a lone < |
| 1248 | // character. Return this as such. |
| 1249 | FormTokenWithChars(Result, AfterLessPos, tok::less); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1250 | return; |
| 1251 | } else if (C == 0) { |
| 1252 | NulCharacter = CurPtr-1; |
| 1253 | } |
| 1254 | C = getAndAdvanceChar(CurPtr, Result); |
| 1255 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1256 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1257 | // If a nul character existed in the string, warn about it. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1258 | if (NulCharacter && !isLexingRawMode()) |
| 1259 | Diag(NulCharacter, diag::null_in_string); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1260 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1261 | // Update the location of token as well as BufferPtr. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 1262 | const char *TokStart = BufferPtr; |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1263 | FormTokenWithChars(Result, CurPtr, tok::angle_string_literal); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 1264 | Result.setLiteralData(TokStart); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1265 | } |
| 1266 | |
| 1267 | |
| 1268 | /// LexCharConstant - Lex the remainder of a character constant, after having |
| 1269 | /// lexed either ' or L'. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1270 | void Lexer::LexCharConstant(Token &Result, const char *CurPtr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1271 | const char *NulCharacter = 0; // Does this character contain the \0 character? |
| 1272 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1273 | char C = getAndAdvanceChar(CurPtr, Result); |
| 1274 | if (C == '\'') { |
Chris Lattner | 33ab3f6 | 2009-03-18 21:10:12 +0000 | [diff] [blame] | 1275 | if (!isLexingRawMode() && !Features.AsmPreprocessor) |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1276 | Diag(BufferPtr, diag::err_empty_character); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1277 | FormTokenWithChars(Result, CurPtr, tok::unknown); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1278 | return; |
Chris Lattner | d80f786 | 2010-07-07 23:24:27 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
| 1281 | while (C != '\'') { |
| 1282 | // Skip escaped characters. |
| 1283 | if (C == '\\') { |
| 1284 | // Skip the escaped character. |
| 1285 | // FIXME: UCN's |
| 1286 | C = getAndAdvanceChar(CurPtr, Result); |
| 1287 | } else if (C == '\n' || C == '\r' || // Newline. |
| 1288 | (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 1289 | if (C == 0 && PP && PP->isCodeCompletionFile(FileLoc)) |
| 1290 | PP->CodeCompleteNaturalLanguage(); |
| 1291 | else if (!isLexingRawMode() && !Features.AsmPreprocessor) |
Argyrios Kyrtzidis | ff1ed98 | 2011-02-15 23:45:31 +0000 | [diff] [blame] | 1292 | Diag(BufferPtr, diag::warn_unterminated_char); |
Chris Lattner | d80f786 | 2010-07-07 23:24:27 +0000 | [diff] [blame] | 1293 | FormTokenWithChars(Result, CurPtr-1, tok::unknown); |
| 1294 | return; |
| 1295 | } else if (C == 0) { |
| 1296 | NulCharacter = CurPtr-1; |
| 1297 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1298 | C = getAndAdvanceChar(CurPtr, Result); |
| 1299 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1300 | |
Chris Lattner | d80f786 | 2010-07-07 23:24:27 +0000 | [diff] [blame] | 1301 | // If a nul character existed in the character, warn about it. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1302 | if (NulCharacter && !isLexingRawMode()) |
| 1303 | Diag(NulCharacter, diag::null_in_char); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1304 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1305 | // Update the location of token as well as BufferPtr. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 1306 | const char *TokStart = BufferPtr; |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1307 | FormTokenWithChars(Result, CurPtr, tok::char_constant); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 1308 | Result.setLiteralData(TokStart); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | /// SkipWhitespace - Efficiently skip over a series of whitespace characters. |
| 1312 | /// Update BufferPtr to point to the next non-whitespace character and return. |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1313 | /// |
| 1314 | /// This method forms a token and returns true if KeepWhitespaceMode is enabled. |
| 1315 | /// |
| 1316 | bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1317 | // Whitespace - Skip it, then return the token after the whitespace. |
| 1318 | unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently. |
| 1319 | while (1) { |
| 1320 | // Skip horizontal whitespace very aggressively. |
| 1321 | while (isHorizontalWhitespace(Char)) |
| 1322 | Char = *++CurPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1323 | |
Daniel Dunbar | ddd3e8b | 2008-11-25 00:20:22 +0000 | [diff] [blame] | 1324 | // Otherwise if we have something other than whitespace, we're done. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1325 | if (Char != '\n' && Char != '\r') |
| 1326 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1327 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1328 | if (ParsingPreprocessorDirective) { |
| 1329 | // End of preprocessor directive line, let LexTokenInternal handle this. |
| 1330 | BufferPtr = CurPtr; |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1331 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1332 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1333 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1334 | // ok, but handle newline. |
| 1335 | // The returned token is at the start of the line. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1336 | Result.setFlag(Token::StartOfLine); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1337 | // No leading whitespace seen so far. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1338 | Result.clearFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1339 | Char = *++CurPtr; |
| 1340 | } |
| 1341 | |
| 1342 | // If this isn't immediately after a newline, there is leading space. |
| 1343 | char PrevChar = CurPtr[-1]; |
| 1344 | if (PrevChar != '\n' && PrevChar != '\r') |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1345 | Result.setFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1346 | |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1347 | // If the client wants us to return whitespace, return it now. |
| 1348 | if (isKeepWhitespaceMode()) { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1349 | FormTokenWithChars(Result, CurPtr, tok::unknown); |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1350 | return true; |
| 1351 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1352 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1353 | BufferPtr = CurPtr; |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1354 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1355 | } |
| 1356 | |
| 1357 | // SkipBCPLComment - We have just read the // characters from input. Skip until |
| 1358 | // we find the newline character thats terminate the comment. Then update |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 1359 | /// BufferPtr and return. |
| 1360 | /// |
| 1361 | /// If we're in KeepCommentMode or any CommentHandler has inserted |
| 1362 | /// some tokens, this will store the first token and return true. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1363 | bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1364 | // If BCPL comments aren't explicitly enabled for this language, emit an |
| 1365 | // extension warning. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1366 | if (!Features.BCPLComment && !isLexingRawMode()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1367 | Diag(BufferPtr, diag::ext_bcpl_comment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1368 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1369 | // Mark them enabled so we only emit one warning for this translation |
| 1370 | // unit. |
| 1371 | Features.BCPLComment = true; |
| 1372 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1373 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1374 | // Scan over the body of the comment. The common case, when scanning, is that |
| 1375 | // the comment contains normal ascii characters with nothing interesting in |
| 1376 | // them. As such, optimize for this case with the inner loop. |
| 1377 | char C; |
| 1378 | do { |
| 1379 | C = *CurPtr; |
| 1380 | // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character. |
| 1381 | // If we find a \n character, scan backwards, checking to see if it's an |
| 1382 | // escaped newline, like we do for block comments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1383 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1384 | // Skip over characters in the fast loop. |
| 1385 | while (C != 0 && // Potentially EOF. |
| 1386 | C != '\\' && // Potentially escaped newline. |
| 1387 | C != '?' && // Potentially trigraph. |
| 1388 | C != '\n' && C != '\r') // Newline or DOS-style newline. |
| 1389 | C = *++CurPtr; |
| 1390 | |
| 1391 | // If this is a newline, we're done. |
| 1392 | if (C == '\n' || C == '\r') |
| 1393 | break; // Found the newline? Break out! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1394 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1395 | // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to |
Chris Lattner | bc3e984 | 2008-12-12 07:34:39 +0000 | [diff] [blame] | 1396 | // properly decode the character. Read it in raw mode to avoid emitting |
| 1397 | // diagnostics about things like trigraphs. If we see an escaped newline, |
| 1398 | // we'll handle it below. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1399 | const char *OldPtr = CurPtr; |
Chris Lattner | bc3e984 | 2008-12-12 07:34:39 +0000 | [diff] [blame] | 1400 | bool OldRawMode = isLexingRawMode(); |
| 1401 | LexingRawMode = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1402 | C = getAndAdvanceChar(CurPtr, Result); |
Chris Lattner | bc3e984 | 2008-12-12 07:34:39 +0000 | [diff] [blame] | 1403 | LexingRawMode = OldRawMode; |
Chris Lattner | ead616c | 2009-04-05 00:26:41 +0000 | [diff] [blame] | 1404 | |
| 1405 | // If the char that we finally got was a \n, then we must have had something |
| 1406 | // like \<newline><newline>. We don't want to have consumed the second |
| 1407 | // newline, we want CurPtr, to end up pointing to it down below. |
| 1408 | if (C == '\n' || C == '\r') { |
| 1409 | --CurPtr; |
| 1410 | C = 'x'; // doesn't matter what this is. |
| 1411 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1413 | // If we read multiple characters, and one of those characters was a \r or |
| 1414 | // \n, then we had an escaped newline within the comment. Emit diagnostic |
| 1415 | // unless the next line is also a // comment. |
| 1416 | if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') { |
| 1417 | for (; OldPtr != CurPtr; ++OldPtr) |
| 1418 | if (OldPtr[0] == '\n' || OldPtr[0] == '\r') { |
| 1419 | // Okay, we found a // comment that ends in a newline, if the next |
| 1420 | // line is also a // comment, but has spaces, don't emit a diagnostic. |
| 1421 | if (isspace(C)) { |
| 1422 | const char *ForwardPtr = CurPtr; |
| 1423 | while (isspace(*ForwardPtr)) // Skip whitespace. |
| 1424 | ++ForwardPtr; |
| 1425 | if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/') |
| 1426 | break; |
| 1427 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1428 | |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1429 | if (!isLexingRawMode()) |
| 1430 | Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1431 | break; |
| 1432 | } |
| 1433 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1434 | |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 1435 | if (CurPtr == BufferEnd+1) { |
| 1436 | if (PP && PP->isCodeCompletionFile(FileLoc)) |
| 1437 | PP->CodeCompleteNaturalLanguage(); |
| 1438 | |
| 1439 | --CurPtr; |
| 1440 | break; |
| 1441 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1442 | } while (C != '\n' && C != '\r'); |
| 1443 | |
Chris Lattner | 3d0ad58 | 2010-02-03 21:06:21 +0000 | [diff] [blame] | 1444 | // Found but did not consume the newline. Notify comment handlers about the |
| 1445 | // comment unless we're in a #if 0 block. |
| 1446 | if (PP && !isLexingRawMode() && |
| 1447 | PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr), |
| 1448 | getSourceLocation(CurPtr)))) { |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 1449 | BufferPtr = CurPtr; |
| 1450 | return true; // A token has to be returned. |
| 1451 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1452 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1453 | // 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] | 1454 | if (inKeepCommentMode()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1455 | return SaveBCPLComment(Result, CurPtr); |
| 1456 | |
| 1457 | // If we are inside a preprocessor directive and we see the end of line, |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1458 | // return immediately, so that the lexer can return this as an EOD token. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1459 | if (ParsingPreprocessorDirective || CurPtr == BufferEnd) { |
| 1460 | BufferPtr = CurPtr; |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1461 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1462 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1463 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1464 | // 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] | 1465 | // \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] | 1466 | // contribute to another token), it isn't needed for correctness. Note that |
| 1467 | // this is ok even in KeepWhitespaceMode, because we would have returned the |
| 1468 | /// comment above in that mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1469 | ++CurPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1470 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1471 | // The next returned token is at the start of the line. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1472 | Result.setFlag(Token::StartOfLine); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1473 | // No leading whitespace seen so far. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1474 | Result.clearFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1475 | BufferPtr = CurPtr; |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1476 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | /// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in |
| 1480 | /// an appropriate way and return it. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1481 | bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1482 | // If we're not in a preprocessor directive, just return the // comment |
| 1483 | // directly. |
| 1484 | FormTokenWithChars(Result, CurPtr, tok::comment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1485 | |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1486 | if (!ParsingPreprocessorDirective) |
| 1487 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1488 | |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1489 | // If this BCPL-style comment is in a macro definition, transmogrify it into |
| 1490 | // a C-style block comment. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 1491 | bool Invalid = false; |
| 1492 | std::string Spelling = PP->getSpelling(Result, &Invalid); |
| 1493 | if (Invalid) |
| 1494 | return true; |
| 1495 | |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1496 | assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?"); |
| 1497 | Spelling[1] = '*'; // Change prefix to "/*". |
| 1498 | Spelling += "*/"; // add suffix. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1499 | |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1500 | Result.setKind(tok::comment); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 1501 | PP->CreateString(&Spelling[0], Spelling.size(), Result, |
| 1502 | Result.getLocation()); |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1503 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | /// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline |
| 1507 | /// 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] | 1508 | /// 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] | 1509 | static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1510 | Lexer *L) { |
| 1511 | assert(CurPtr[0] == '\n' || CurPtr[0] == '\r'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1512 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1513 | // Back up off the newline. |
| 1514 | --CurPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1515 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1516 | // If this is a two-character newline sequence, skip the other character. |
| 1517 | if (CurPtr[0] == '\n' || CurPtr[0] == '\r') { |
| 1518 | // \n\n or \r\r -> not escaped newline. |
| 1519 | if (CurPtr[0] == CurPtr[1]) |
| 1520 | return false; |
| 1521 | // \n\r or \r\n -> skip the newline. |
| 1522 | --CurPtr; |
| 1523 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1524 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1525 | // If we have horizontal whitespace, skip over it. We allow whitespace |
| 1526 | // between the slash and newline. |
| 1527 | bool HasSpace = false; |
| 1528 | while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) { |
| 1529 | --CurPtr; |
| 1530 | HasSpace = true; |
| 1531 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1532 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1533 | // If we have a slash, we know this is an escaped newline. |
| 1534 | if (*CurPtr == '\\') { |
| 1535 | if (CurPtr[-1] != '*') return false; |
| 1536 | } else { |
| 1537 | // It isn't a slash, is it the ?? / trigraph? |
| 1538 | if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' || |
| 1539 | CurPtr[-3] != '*') |
| 1540 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1541 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1542 | // This is the trigraph ending the comment. Emit a stern warning! |
| 1543 | CurPtr -= 2; |
| 1544 | |
| 1545 | // If no trigraphs are enabled, warn that we ignored this trigraph and |
| 1546 | // ignore this * character. |
| 1547 | if (!L->getFeatures().Trigraphs) { |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1548 | if (!L->isLexingRawMode()) |
| 1549 | L->Diag(CurPtr, diag::trigraph_ignored_block_comment); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1550 | return false; |
| 1551 | } |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1552 | if (!L->isLexingRawMode()) |
| 1553 | L->Diag(CurPtr, diag::trigraph_ends_block_comment); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1554 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1555 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1556 | // Warn about having an escaped newline between the */ characters. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1557 | if (!L->isLexingRawMode()) |
| 1558 | L->Diag(CurPtr, diag::escaped_newline_block_comment_end); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1559 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1560 | // If there was space between the backslash and newline, warn about it. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1561 | if (HasSpace && !L->isLexingRawMode()) |
| 1562 | L->Diag(CurPtr, diag::backslash_newline_space); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1563 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1564 | return true; |
| 1565 | } |
| 1566 | |
| 1567 | #ifdef __SSE2__ |
| 1568 | #include <emmintrin.h> |
| 1569 | #elif __ALTIVEC__ |
| 1570 | #include <altivec.h> |
| 1571 | #undef bool |
| 1572 | #endif |
| 1573 | |
| 1574 | /// SkipBlockComment - We have just read the /* characters from input. Read |
| 1575 | /// until we find the */ characters that terminate the comment. Note that we |
| 1576 | /// don't bother decoding trigraphs or escaped newlines in block comments, |
| 1577 | /// because they cannot cause the comment to end. The only thing that can |
| 1578 | /// happen is the comment could end with an escaped newline between the */ end |
| 1579 | /// of comment. |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1580 | /// |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 1581 | /// If we're in KeepCommentMode or any CommentHandler has inserted |
| 1582 | /// some tokens, this will store the first token and return true. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1583 | bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1584 | // Scan one character past where we should, looking for a '/' character. Once |
| 1585 | // we find it, check to see if it was preceeded by a *. This common |
| 1586 | // optimization helps people who like to put a lot of * characters in their |
| 1587 | // comments. |
Chris Lattner | 8146b68 | 2007-07-21 23:43:37 +0000 | [diff] [blame] | 1588 | |
| 1589 | // The first character we get with newlines and trigraphs skipped to handle |
| 1590 | // the degenerate /*/ case below correctly if the * has an escaped newline |
| 1591 | // after it. |
| 1592 | unsigned CharSize; |
| 1593 | unsigned char C = getCharAndSize(CurPtr, CharSize); |
| 1594 | CurPtr += CharSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1595 | if (C == 0 && CurPtr == BufferEnd+1) { |
Chris Lattner | 150fcd5 | 2010-05-16 19:54:05 +0000 | [diff] [blame] | 1596 | if (!isLexingRawMode() && |
| 1597 | !PP->isCodeCompletionFile(FileLoc)) |
Chris Lattner | 0af5742 | 2008-10-12 01:31:51 +0000 | [diff] [blame] | 1598 | Diag(BufferPtr, diag::err_unterminated_block_comment); |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1599 | --CurPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1600 | |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1601 | // KeepWhitespaceMode should return this broken comment as a token. Since |
| 1602 | // it isn't a well formed comment, just return it as an 'unknown' token. |
| 1603 | if (isKeepWhitespaceMode()) { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1604 | FormTokenWithChars(Result, CurPtr, tok::unknown); |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1605 | return true; |
| 1606 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1607 | |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1608 | BufferPtr = CurPtr; |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1609 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1610 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1611 | |
Chris Lattner | 8146b68 | 2007-07-21 23:43:37 +0000 | [diff] [blame] | 1612 | // Check to see if the first character after the '/*' is another /. If so, |
| 1613 | // then this slash does not end the block comment, it is part of it. |
| 1614 | if (C == '/') |
| 1615 | C = *CurPtr++; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1616 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1617 | while (1) { |
| 1618 | // Skip over all non-interesting characters until we find end of buffer or a |
| 1619 | // (probably ending) '/' character. |
| 1620 | if (CurPtr + 24 < BufferEnd) { |
| 1621 | // While not aligned to a 16-byte boundary. |
| 1622 | while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0) |
| 1623 | C = *CurPtr++; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1625 | if (C == '/') goto FoundSlash; |
| 1626 | |
| 1627 | #ifdef __SSE2__ |
| 1628 | __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/', |
| 1629 | '/', '/', '/', '/', '/', '/', '/', '/'); |
| 1630 | while (CurPtr+16 <= BufferEnd && |
| 1631 | _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0) |
| 1632 | CurPtr += 16; |
| 1633 | #elif __ALTIVEC__ |
| 1634 | __vector unsigned char Slashes = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1635 | '/', '/', '/', '/', '/', '/', '/', '/', |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1636 | '/', '/', '/', '/', '/', '/', '/', '/' |
| 1637 | }; |
| 1638 | while (CurPtr+16 <= BufferEnd && |
| 1639 | !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes)) |
| 1640 | CurPtr += 16; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | #else |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1642 | // Scan for '/' quickly. Many block comments are very large. |
| 1643 | while (CurPtr[0] != '/' && |
| 1644 | CurPtr[1] != '/' && |
| 1645 | CurPtr[2] != '/' && |
| 1646 | CurPtr[3] != '/' && |
| 1647 | CurPtr+4 < BufferEnd) { |
| 1648 | CurPtr += 4; |
| 1649 | } |
| 1650 | #endif |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1651 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1652 | // It has to be one of the bytes scanned, increment to it and read one. |
| 1653 | C = *CurPtr++; |
| 1654 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1655 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1656 | // Loop to scan the remainder. |
| 1657 | while (C != '/' && C != '\0') |
| 1658 | C = *CurPtr++; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1659 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1660 | FoundSlash: |
| 1661 | if (C == '/') { |
| 1662 | if (CurPtr[-2] == '*') // We found the final */. We're done! |
| 1663 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1664 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1665 | if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) { |
| 1666 | if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) { |
| 1667 | // We found the final */, though it had an escaped newline between the |
| 1668 | // * and /. We're done! |
| 1669 | break; |
| 1670 | } |
| 1671 | } |
| 1672 | if (CurPtr[0] == '*' && CurPtr[1] != '/') { |
| 1673 | // If this is a /* inside of the comment, emit a warning. Don't do this |
| 1674 | // if this is a /*/, which will end the comment. This misses cases with |
| 1675 | // embedded escaped newlines, but oh well. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1676 | if (!isLexingRawMode()) |
| 1677 | Diag(CurPtr-1, diag::warn_nested_block_comment); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1678 | } |
| 1679 | } else if (C == 0 && CurPtr == BufferEnd+1) { |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 1680 | if (PP && PP->isCodeCompletionFile(FileLoc)) |
| 1681 | PP->CodeCompleteNaturalLanguage(); |
| 1682 | else if (!isLexingRawMode()) |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1683 | Diag(BufferPtr, diag::err_unterminated_block_comment); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1684 | // Note: the user probably forgot a */. We could continue immediately |
| 1685 | // after the /*, but this would involve lexing a lot of what really is the |
| 1686 | // comment, which surely would confuse the parser. |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1687 | --CurPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1688 | |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1689 | // KeepWhitespaceMode should return this broken comment as a token. Since |
| 1690 | // it isn't a well formed comment, just return it as an 'unknown' token. |
| 1691 | if (isKeepWhitespaceMode()) { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1692 | FormTokenWithChars(Result, CurPtr, tok::unknown); |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1693 | return true; |
| 1694 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1695 | |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1696 | BufferPtr = CurPtr; |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1697 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1698 | } |
| 1699 | C = *CurPtr++; |
| 1700 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1701 | |
Chris Lattner | 3d0ad58 | 2010-02-03 21:06:21 +0000 | [diff] [blame] | 1702 | // Notify comment handlers about the comment unless we're in a #if 0 block. |
| 1703 | if (PP && !isLexingRawMode() && |
| 1704 | PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr), |
| 1705 | getSourceLocation(CurPtr)))) { |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 1706 | BufferPtr = CurPtr; |
| 1707 | return true; // A token has to be returned. |
| 1708 | } |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 1709 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1710 | // 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] | 1711 | if (inKeepCommentMode()) { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1712 | FormTokenWithChars(Result, CurPtr, tok::comment); |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1713 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | // It is common for the tokens immediately after a /**/ comment to be |
| 1717 | // whitespace. Instead of going through the big switch, handle it |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1718 | // efficiently now. This is safe even in KeepWhitespaceMode because we would |
| 1719 | // have already returned above with the comment as a token. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1720 | if (isHorizontalWhitespace(*CurPtr)) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1721 | Result.setFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1722 | SkipWhitespace(Result, CurPtr+1); |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1723 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1724 | } |
| 1725 | |
| 1726 | // Otherwise, just return so that the next character will be lexed as a token. |
| 1727 | BufferPtr = CurPtr; |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1728 | Result.setFlag(Token::LeadingSpace); |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1729 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1730 | } |
| 1731 | |
| 1732 | //===----------------------------------------------------------------------===// |
| 1733 | // Primary Lexing Entry Points |
| 1734 | //===----------------------------------------------------------------------===// |
| 1735 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1736 | /// ReadToEndOfLine - Read the rest of the current preprocessor line as an |
| 1737 | /// uninterpreted string. This switches the lexer out of directive mode. |
| 1738 | std::string Lexer::ReadToEndOfLine() { |
| 1739 | assert(ParsingPreprocessorDirective && ParsingFilename == false && |
| 1740 | "Must be in a preprocessing directive!"); |
| 1741 | std::string Result; |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1742 | Token Tmp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1743 | |
| 1744 | // CurPtr - Cache BufferPtr in an automatic variable. |
| 1745 | const char *CurPtr = BufferPtr; |
| 1746 | while (1) { |
| 1747 | char Char = getAndAdvanceChar(CurPtr, Tmp); |
| 1748 | switch (Char) { |
| 1749 | default: |
| 1750 | Result += Char; |
| 1751 | break; |
| 1752 | case 0: // Null. |
| 1753 | // Found end of file? |
| 1754 | if (CurPtr-1 != BufferEnd) { |
| 1755 | // Nope, normal character, continue. |
| 1756 | Result += Char; |
| 1757 | break; |
| 1758 | } |
| 1759 | // FALL THROUGH. |
| 1760 | case '\r': |
| 1761 | case '\n': |
| 1762 | // Okay, we found the end of the line. First, back up past the \0, \r, \n. |
| 1763 | assert(CurPtr[-1] == Char && "Trigraphs for newline?"); |
| 1764 | BufferPtr = CurPtr-1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1765 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1766 | // Next, lex the character, which should handle the EOD transition. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1767 | Lex(Tmp); |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 1768 | if (Tmp.is(tok::code_completion)) { |
| 1769 | if (PP && PP->getCodeCompletionHandler()) |
| 1770 | PP->getCodeCompletionHandler()->CodeCompleteNaturalLanguage(); |
| 1771 | Lex(Tmp); |
| 1772 | } |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1773 | assert(Tmp.is(tok::eod) && "Unexpected token!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1774 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1775 | // Finally, we're done, return the string we found. |
| 1776 | return Result; |
| 1777 | } |
| 1778 | } |
| 1779 | } |
| 1780 | |
| 1781 | /// LexEndOfFile - CurPtr points to the end of this file. Handle this |
| 1782 | /// condition, reporting diagnostics and handling other edge cases as required. |
| 1783 | /// This returns true if Result contains a token, false if PP.Lex should be |
| 1784 | /// called again. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1785 | bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) { |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 1786 | // Check if we are performing code completion. |
| 1787 | if (PP && PP->isCodeCompletionFile(FileLoc)) { |
| 1788 | // We're at the end of the file, but we've been asked to consider the |
| 1789 | // end of the file to be a code-completion token. Return the |
| 1790 | // code-completion token. |
| 1791 | Result.startToken(); |
| 1792 | FormTokenWithChars(Result, CurPtr, tok::code_completion); |
| 1793 | |
| 1794 | // Only do the eof -> code_completion translation once. |
| 1795 | PP->SetCodeCompletionPoint(0, 0, 0); |
| 1796 | |
| 1797 | // Silence any diagnostics that occur once we hit the code-completion point. |
| 1798 | PP->getDiagnostics().setSuppressAllDiagnostics(true); |
| 1799 | return true; |
| 1800 | } |
| 1801 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1802 | // If we hit the end of the file while parsing a preprocessor directive, |
| 1803 | // end the preprocessor directive first. The next token returned will |
| 1804 | // then be the end of file. |
| 1805 | if (ParsingPreprocessorDirective) { |
| 1806 | // Done parsing the "line". |
| 1807 | ParsingPreprocessorDirective = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1808 | // Update the location of token as well as BufferPtr. |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 1809 | FormTokenWithChars(Result, CurPtr, tok::eod); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1810 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1811 | // Restore comment saving mode, in case it was disabled for directive. |
Chris Lattner | f744d13 | 2008-10-12 03:27:19 +0000 | [diff] [blame] | 1812 | SetCommentRetentionState(PP->getCommentRetentionState()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1813 | return true; // Have a token. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1814 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1815 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1816 | // If we are in raw mode, return this event as an EOF token. Let the caller |
| 1817 | // that put us in raw mode handle the event. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1818 | if (isLexingRawMode()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1819 | Result.startToken(); |
| 1820 | BufferPtr = BufferEnd; |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1821 | FormTokenWithChars(Result, BufferEnd, tok::eof); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1822 | return true; |
| 1823 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1824 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 1825 | // Issue diagnostics for unterminated #if and missing newline. |
| 1826 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1827 | // If we are in a #if directive, emit an error. |
| 1828 | while (!ConditionalStack.empty()) { |
Douglas Gregor | 2d474ba | 2010-08-12 17:04:55 +0000 | [diff] [blame] | 1829 | if (!PP->isCodeCompletionFile(FileLoc)) |
| 1830 | PP->Diag(ConditionalStack.back().IfLoc, |
| 1831 | diag::err_pp_unterminated_conditional); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1832 | ConditionalStack.pop_back(); |
| 1833 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1834 | |
Chris Lattner | b25e5d7 | 2008-04-12 05:54:25 +0000 | [diff] [blame] | 1835 | // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue |
| 1836 | // a pedwarn. |
| 1837 | if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')) |
Mike Stump | 20d0ee5 | 2009-04-02 02:29:42 +0000 | [diff] [blame] | 1838 | Diag(BufferEnd, diag::ext_no_newline_eof) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1839 | << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1840 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1841 | BufferPtr = CurPtr; |
| 1842 | |
| 1843 | // Finally, let the preprocessor handle this. |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 1844 | return PP->HandleEndOfFile(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
| 1847 | /// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from |
| 1848 | /// the specified lexer will return a tok::l_paren token, 0 if it is something |
| 1849 | /// else and 2 if there are no more tokens in the buffer controlled by the |
| 1850 | /// lexer. |
| 1851 | unsigned Lexer::isNextPPTokenLParen() { |
| 1852 | assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1853 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1854 | // Switch to 'skipping' mode. This will ensure that we can lex a token |
| 1855 | // without emitting diagnostics, disables macro expansion, and will cause EOF |
| 1856 | // to return an EOF token instead of popping the include stack. |
| 1857 | LexingRawMode = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1858 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1859 | // Save state that can be changed while lexing so that we can restore it. |
| 1860 | const char *TmpBufferPtr = BufferPtr; |
Chris Lattner | a864cf7 | 2009-04-24 07:15:46 +0000 | [diff] [blame] | 1861 | bool inPPDirectiveMode = ParsingPreprocessorDirective; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1862 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1863 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1864 | Tok.startToken(); |
| 1865 | LexTokenInternal(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1866 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1867 | // Restore state that may have changed. |
| 1868 | BufferPtr = TmpBufferPtr; |
Chris Lattner | a864cf7 | 2009-04-24 07:15:46 +0000 | [diff] [blame] | 1869 | ParsingPreprocessorDirective = inPPDirectiveMode; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1870 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1871 | // Restore the lexer back to non-skipping mode. |
| 1872 | LexingRawMode = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 1874 | if (Tok.is(tok::eof)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1875 | return 2; |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 1876 | return Tok.is(tok::l_paren); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1877 | } |
| 1878 | |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 1879 | /// FindConflictEnd - Find the end of a version control conflict marker. |
| 1880 | static const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd) { |
| 1881 | llvm::StringRef RestOfBuffer(CurPtr+7, BufferEnd-CurPtr-7); |
| 1882 | size_t Pos = RestOfBuffer.find(">>>>>>>"); |
| 1883 | while (Pos != llvm::StringRef::npos) { |
| 1884 | // Must occur at start of line. |
| 1885 | if (RestOfBuffer[Pos-1] != '\r' && |
| 1886 | RestOfBuffer[Pos-1] != '\n') { |
| 1887 | RestOfBuffer = RestOfBuffer.substr(Pos+7); |
Chris Lattner | 3d48899 | 2010-05-17 20:27:25 +0000 | [diff] [blame] | 1888 | Pos = RestOfBuffer.find(">>>>>>>"); |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 1889 | continue; |
| 1890 | } |
| 1891 | return RestOfBuffer.data()+Pos; |
| 1892 | } |
| 1893 | return 0; |
| 1894 | } |
| 1895 | |
| 1896 | /// IsStartOfConflictMarker - If the specified pointer is the start of a version |
| 1897 | /// control conflict marker like '<<<<<<<', recognize it as such, emit an error |
| 1898 | /// and recover nicely. This returns true if it is a conflict marker and false |
| 1899 | /// if not. |
| 1900 | bool Lexer::IsStartOfConflictMarker(const char *CurPtr) { |
| 1901 | // Only a conflict marker if it starts at the beginning of a line. |
| 1902 | if (CurPtr != BufferStart && |
| 1903 | CurPtr[-1] != '\n' && CurPtr[-1] != '\r') |
| 1904 | return false; |
| 1905 | |
| 1906 | // Check to see if we have <<<<<<<. |
| 1907 | if (BufferEnd-CurPtr < 8 || |
| 1908 | llvm::StringRef(CurPtr, 7) != "<<<<<<<") |
| 1909 | return false; |
| 1910 | |
| 1911 | // If we have a situation where we don't care about conflict markers, ignore |
| 1912 | // it. |
| 1913 | if (IsInConflictMarker || isLexingRawMode()) |
| 1914 | return false; |
| 1915 | |
| 1916 | // Check to see if there is a >>>>>>> somewhere in the buffer at the start of |
| 1917 | // a line to terminate this conflict marker. |
Chris Lattner | 3d48899 | 2010-05-17 20:27:25 +0000 | [diff] [blame] | 1918 | if (FindConflictEnd(CurPtr, BufferEnd)) { |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 1919 | // We found a match. We are really in a conflict marker. |
| 1920 | // Diagnose this, and ignore to the end of line. |
| 1921 | Diag(CurPtr, diag::err_conflict_marker); |
| 1922 | IsInConflictMarker = true; |
| 1923 | |
| 1924 | // Skip ahead to the end of line. We know this exists because the |
| 1925 | // end-of-conflict marker starts with \r or \n. |
| 1926 | while (*CurPtr != '\r' && *CurPtr != '\n') { |
| 1927 | assert(CurPtr != BufferEnd && "Didn't find end of line"); |
| 1928 | ++CurPtr; |
| 1929 | } |
| 1930 | BufferPtr = CurPtr; |
| 1931 | return true; |
| 1932 | } |
| 1933 | |
| 1934 | // No end of conflict marker found. |
| 1935 | return false; |
| 1936 | } |
| 1937 | |
| 1938 | |
| 1939 | /// HandleEndOfConflictMarker - If this is a '=======' or '|||||||' or '>>>>>>>' |
| 1940 | /// marker, then it is the end of a conflict marker. Handle it by ignoring up |
| 1941 | /// until the end of the line. This returns true if it is a conflict marker and |
| 1942 | /// false if not. |
| 1943 | bool Lexer::HandleEndOfConflictMarker(const char *CurPtr) { |
| 1944 | // Only a conflict marker if it starts at the beginning of a line. |
| 1945 | if (CurPtr != BufferStart && |
| 1946 | CurPtr[-1] != '\n' && CurPtr[-1] != '\r') |
| 1947 | return false; |
| 1948 | |
| 1949 | // If we have a situation where we don't care about conflict markers, ignore |
| 1950 | // it. |
| 1951 | if (!IsInConflictMarker || isLexingRawMode()) |
| 1952 | return false; |
| 1953 | |
| 1954 | // Check to see if we have the marker (7 characters in a row). |
| 1955 | for (unsigned i = 1; i != 7; ++i) |
| 1956 | if (CurPtr[i] != CurPtr[0]) |
| 1957 | return false; |
| 1958 | |
| 1959 | // If we do have it, search for the end of the conflict marker. This could |
| 1960 | // fail if it got skipped with a '#if 0' or something. Note that CurPtr might |
| 1961 | // be the end of conflict marker. |
| 1962 | if (const char *End = FindConflictEnd(CurPtr, BufferEnd)) { |
| 1963 | CurPtr = End; |
| 1964 | |
| 1965 | // Skip ahead to the end of line. |
| 1966 | while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n') |
| 1967 | ++CurPtr; |
| 1968 | |
| 1969 | BufferPtr = CurPtr; |
| 1970 | |
| 1971 | // No longer in the conflict marker. |
| 1972 | IsInConflictMarker = false; |
| 1973 | return true; |
| 1974 | } |
| 1975 | |
| 1976 | return false; |
| 1977 | } |
| 1978 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1979 | |
| 1980 | /// LexTokenInternal - This implements a simple C family lexer. It is an |
| 1981 | /// extremely performance critical piece of code. This assumes that the buffer |
Chris Lattner | efb173d | 2009-07-07 05:05:42 +0000 | [diff] [blame] | 1982 | /// has a null character at the end of the file. This returns a preprocessing |
| 1983 | /// token, not a normal token, as such, it is an internal interface. It assumes |
| 1984 | /// that the Flags of result have been cleared before calling this. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1985 | void Lexer::LexTokenInternal(Token &Result) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1986 | LexNextToken: |
| 1987 | // New token, can't need cleaning yet. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1988 | Result.clearFlag(Token::NeedsCleaning); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1989 | Result.setIdentifierInfo(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1991 | // CurPtr - Cache BufferPtr in an automatic variable. |
| 1992 | const char *CurPtr = BufferPtr; |
| 1993 | |
| 1994 | // Small amounts of horizontal whitespace is very common between tokens. |
| 1995 | if ((*CurPtr == ' ') || (*CurPtr == '\t')) { |
| 1996 | ++CurPtr; |
| 1997 | while ((*CurPtr == ' ') || (*CurPtr == '\t')) |
| 1998 | ++CurPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1999 | |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 2000 | // If we are keeping whitespace and other tokens, just return what we just |
| 2001 | // skipped. The next lexer invocation will return the token after the |
| 2002 | // whitespace. |
| 2003 | if (isKeepWhitespaceMode()) { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2004 | FormTokenWithChars(Result, CurPtr, tok::unknown); |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 2005 | return; |
| 2006 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2007 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2008 | BufferPtr = CurPtr; |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 2009 | Result.setFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2010 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2011 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2012 | unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2013 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2014 | // Read a character, advancing over it. |
| 2015 | char Char = getAndAdvanceChar(CurPtr, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2016 | tok::TokenKind Kind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2018 | switch (Char) { |
| 2019 | case 0: // Null. |
| 2020 | // Found end of file? |
| 2021 | if (CurPtr-1 == BufferEnd) { |
| 2022 | // Read the PP instance variable into an automatic variable, because |
| 2023 | // LexEndOfFile will often delete 'this'. |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 2024 | Preprocessor *PPCache = PP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2025 | if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file. |
| 2026 | return; // Got a token to return. |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 2027 | assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); |
| 2028 | return PPCache->Lex(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2029 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2030 | |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 2031 | if (!isLexingRawMode()) |
| 2032 | Diag(CurPtr-1, diag::null_in_file); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 2033 | Result.setFlag(Token::LeadingSpace); |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 2034 | if (SkipWhitespace(Result, CurPtr)) |
| 2035 | return; // KeepWhitespaceMode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2036 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2037 | goto LexNextToken; // GCC isn't tail call eliminating. |
Chris Lattner | a2bf105 | 2009-12-17 05:29:40 +0000 | [diff] [blame] | 2038 | |
| 2039 | case 26: // DOS & CP/M EOF: "^Z". |
| 2040 | // If we're in Microsoft extensions mode, treat this as end of file. |
| 2041 | if (Features.Microsoft) { |
| 2042 | // Read the PP instance variable into an automatic variable, because |
| 2043 | // LexEndOfFile will often delete 'this'. |
| 2044 | Preprocessor *PPCache = PP; |
| 2045 | if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file. |
| 2046 | return; // Got a token to return. |
| 2047 | assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); |
| 2048 | return PPCache->Lex(Result); |
| 2049 | } |
| 2050 | // If Microsoft extensions are disabled, this is just random garbage. |
| 2051 | Kind = tok::unknown; |
| 2052 | break; |
| 2053 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2054 | case '\n': |
| 2055 | case '\r': |
| 2056 | // If we are inside a preprocessor directive and we see the end of line, |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 2057 | // we know we are done with the directive, so return an EOD token. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2058 | if (ParsingPreprocessorDirective) { |
| 2059 | // Done parsing the "line". |
| 2060 | ParsingPreprocessorDirective = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2061 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2062 | // Restore comment saving mode, in case it was disabled for directive. |
Chris Lattner | f744d13 | 2008-10-12 03:27:19 +0000 | [diff] [blame] | 2063 | SetCommentRetentionState(PP->getCommentRetentionState()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2064 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2065 | // Since we consumed a newline, we are back at the start of a line. |
| 2066 | IsAtStartOfLine = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | |
Peter Collingbourne | 8402155 | 2011-02-28 02:37:51 +0000 | [diff] [blame] | 2068 | Kind = tok::eod; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2069 | break; |
| 2070 | } |
| 2071 | // The returned token is at the start of the line. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 2072 | Result.setFlag(Token::StartOfLine); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2073 | // No leading whitespace seen so far. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 2074 | Result.clearFlag(Token::LeadingSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2075 | |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 2076 | if (SkipWhitespace(Result, CurPtr)) |
| 2077 | return; // KeepWhitespaceMode |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2078 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 2079 | case ' ': |
| 2080 | case '\t': |
| 2081 | case '\f': |
| 2082 | case '\v': |
Chris Lattner | 8133cfc | 2007-07-22 06:29:05 +0000 | [diff] [blame] | 2083 | SkipHorizontalWhitespace: |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 2084 | Result.setFlag(Token::LeadingSpace); |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 2085 | if (SkipWhitespace(Result, CurPtr)) |
| 2086 | return; // KeepWhitespaceMode |
Chris Lattner | 8133cfc | 2007-07-22 06:29:05 +0000 | [diff] [blame] | 2087 | |
| 2088 | SkipIgnoredUnits: |
| 2089 | CurPtr = BufferPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2090 | |
Chris Lattner | 8133cfc | 2007-07-22 06:29:05 +0000 | [diff] [blame] | 2091 | // If the next token is obviously a // or /* */ comment, skip it efficiently |
| 2092 | // too (without going through the big switch stmt). |
Chris Lattner | 8402c73 | 2009-01-16 22:39:25 +0000 | [diff] [blame] | 2093 | if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() && |
Daniel Dunbar | 2ed4228 | 2011-03-18 21:23:38 +0000 | [diff] [blame^] | 2094 | Features.BCPLComment && !Features.TraditionalCPP) { |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 2095 | if (SkipBCPLComment(Result, CurPtr+2)) |
| 2096 | return; // There is a token to return. |
Chris Lattner | 8133cfc | 2007-07-22 06:29:05 +0000 | [diff] [blame] | 2097 | goto SkipIgnoredUnits; |
Chris Lattner | fa95a01 | 2008-10-12 03:22:02 +0000 | [diff] [blame] | 2098 | } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) { |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 2099 | if (SkipBlockComment(Result, CurPtr+2)) |
| 2100 | return; // There is a token to return. |
Chris Lattner | 8133cfc | 2007-07-22 06:29:05 +0000 | [diff] [blame] | 2101 | goto SkipIgnoredUnits; |
| 2102 | } else if (isHorizontalWhitespace(*CurPtr)) { |
| 2103 | goto SkipHorizontalWhitespace; |
| 2104 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2105 | goto LexNextToken; // GCC isn't tail call eliminating. |
Chris Lattner | a2bf105 | 2009-12-17 05:29:40 +0000 | [diff] [blame] | 2106 | |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 2107 | // C99 6.4.4.1: Integer Constants. |
| 2108 | // C99 6.4.4.2: Floating Constants. |
| 2109 | case '0': case '1': case '2': case '3': case '4': |
| 2110 | case '5': case '6': case '7': case '8': case '9': |
| 2111 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 2112 | MIOpt.ReadToken(); |
| 2113 | return LexNumericConstant(Result, CurPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 2115 | case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz"). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2116 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 2117 | MIOpt.ReadToken(); |
| 2118 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2119 | |
| 2120 | // Wide string literal. |
| 2121 | if (Char == '"') |
| 2122 | return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), |
| 2123 | true); |
| 2124 | |
| 2125 | // Wide character constant. |
| 2126 | if (Char == '\'') |
| 2127 | return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); |
| 2128 | // FALL THROUGH, treating L like the start of an identifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2129 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2130 | // C99 6.4.2: Identifiers. |
| 2131 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': |
| 2132 | case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N': |
| 2133 | case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': |
| 2134 | case 'V': case 'W': case 'X': case 'Y': case 'Z': |
| 2135 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': |
| 2136 | case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': |
| 2137 | case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': |
| 2138 | case 'v': case 'w': case 'x': case 'y': case 'z': |
| 2139 | case '_': |
| 2140 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 2141 | MIOpt.ReadToken(); |
| 2142 | return LexIdentifier(Result, CurPtr); |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 2143 | |
| 2144 | case '$': // $ in identifiers. |
| 2145 | if (Features.DollarIdents) { |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 2146 | if (!isLexingRawMode()) |
| 2147 | Diag(CurPtr-1, diag::ext_dollar_in_identifier); |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 2148 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 2149 | MIOpt.ReadToken(); |
| 2150 | return LexIdentifier(Result, CurPtr); |
| 2151 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2152 | |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2153 | Kind = tok::unknown; |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 2154 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2155 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2156 | // C99 6.4.4: Character Constants. |
| 2157 | case '\'': |
| 2158 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 2159 | MIOpt.ReadToken(); |
| 2160 | return LexCharConstant(Result, CurPtr); |
| 2161 | |
| 2162 | // C99 6.4.5: String Literals. |
| 2163 | case '"': |
| 2164 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 2165 | MIOpt.ReadToken(); |
| 2166 | return LexStringLiteral(Result, CurPtr, false); |
| 2167 | |
| 2168 | // C99 6.4.6: Punctuators. |
| 2169 | case '?': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2170 | Kind = tok::question; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2171 | break; |
| 2172 | case '[': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2173 | Kind = tok::l_square; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2174 | break; |
| 2175 | case ']': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2176 | Kind = tok::r_square; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2177 | break; |
| 2178 | case '(': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2179 | Kind = tok::l_paren; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2180 | break; |
| 2181 | case ')': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2182 | Kind = tok::r_paren; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2183 | break; |
| 2184 | case '{': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2185 | Kind = tok::l_brace; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2186 | break; |
| 2187 | case '}': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2188 | Kind = tok::r_brace; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2189 | break; |
| 2190 | case '.': |
| 2191 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2192 | if (Char >= '0' && Char <= '9') { |
| 2193 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 2194 | MIOpt.ReadToken(); |
| 2195 | |
| 2196 | return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); |
| 2197 | } else if (Features.CPlusPlus && Char == '*') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2198 | Kind = tok::periodstar; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2199 | CurPtr += SizeTmp; |
| 2200 | } else if (Char == '.' && |
| 2201 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2202 | Kind = tok::ellipsis; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2203 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 2204 | SizeTmp2, Result); |
| 2205 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2206 | Kind = tok::period; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2207 | } |
| 2208 | break; |
| 2209 | case '&': |
| 2210 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2211 | if (Char == '&') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2212 | Kind = tok::ampamp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2213 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2214 | } else if (Char == '=') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2215 | Kind = tok::ampequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2216 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2217 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2218 | Kind = tok::amp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2219 | } |
| 2220 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2221 | case '*': |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2222 | if (getCharAndSize(CurPtr, SizeTmp) == '=') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2223 | Kind = tok::starequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2224 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2225 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2226 | Kind = tok::star; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2227 | } |
| 2228 | break; |
| 2229 | case '+': |
| 2230 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2231 | if (Char == '+') { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2232 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2233 | Kind = tok::plusplus; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2234 | } else if (Char == '=') { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2235 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2236 | Kind = tok::plusequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2237 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2238 | Kind = tok::plus; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2239 | } |
| 2240 | break; |
| 2241 | case '-': |
| 2242 | Char = getCharAndSize(CurPtr, SizeTmp); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2243 | if (Char == '-') { // -- |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2244 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2245 | Kind = tok::minusminus; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2246 | } else if (Char == '>' && Features.CPlusPlus && |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2247 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->* |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2248 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 2249 | SizeTmp2, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2250 | Kind = tok::arrowstar; |
| 2251 | } else if (Char == '>') { // -> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2252 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2253 | Kind = tok::arrow; |
| 2254 | } else if (Char == '=') { // -= |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2255 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2256 | Kind = tok::minusequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2257 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2258 | Kind = tok::minus; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2259 | } |
| 2260 | break; |
| 2261 | case '~': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2262 | Kind = tok::tilde; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2263 | break; |
| 2264 | case '!': |
| 2265 | if (getCharAndSize(CurPtr, SizeTmp) == '=') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2266 | Kind = tok::exclaimequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2267 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2268 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2269 | Kind = tok::exclaim; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2270 | } |
| 2271 | break; |
| 2272 | case '/': |
| 2273 | // 6.4.9: Comments |
| 2274 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2275 | if (Char == '/') { // BCPL comment. |
Chris Lattner | 8402c73 | 2009-01-16 22:39:25 +0000 | [diff] [blame] | 2276 | // Even if BCPL comments are disabled (e.g. in C89 mode), we generally |
| 2277 | // want to lex this as a comment. There is one problem with this though, |
| 2278 | // that in one particular corner case, this can change the behavior of the |
| 2279 | // resultant program. For example, In "foo //**/ bar", C89 would lex |
| 2280 | // this as "foo / bar" and langauges with BCPL comments would lex it as |
| 2281 | // "foo". Check to see if the character after the second slash is a '*'. |
| 2282 | // If so, we will lex that as a "/" instead of the start of a comment. |
Daniel Dunbar | 2ed4228 | 2011-03-18 21:23:38 +0000 | [diff] [blame^] | 2283 | // However, we never do this in -traditional-cpp mode. |
| 2284 | if ((Features.BCPLComment || |
| 2285 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') && |
| 2286 | !Features.TraditionalCPP) { |
Chris Lattner | 8402c73 | 2009-01-16 22:39:25 +0000 | [diff] [blame] | 2287 | if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 2288 | return; // There is a token to return. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2289 | |
Chris Lattner | 8402c73 | 2009-01-16 22:39:25 +0000 | [diff] [blame] | 2290 | // It is common for the tokens immediately after a // comment to be |
| 2291 | // whitespace (indentation for the next line). Instead of going through |
| 2292 | // the big switch, handle it efficiently now. |
| 2293 | goto SkipIgnoredUnits; |
| 2294 | } |
| 2295 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2296 | |
Chris Lattner | 8402c73 | 2009-01-16 22:39:25 +0000 | [diff] [blame] | 2297 | if (Char == '*') { // /**/ comment. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2298 | if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 2299 | return; // There is a token to return. |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 2300 | goto LexNextToken; // GCC isn't tail call eliminating. |
Chris Lattner | 8402c73 | 2009-01-16 22:39:25 +0000 | [diff] [blame] | 2301 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2302 | |
Chris Lattner | 8402c73 | 2009-01-16 22:39:25 +0000 | [diff] [blame] | 2303 | if (Char == '=') { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2304 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2305 | Kind = tok::slashequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2306 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2307 | Kind = tok::slash; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2308 | } |
| 2309 | break; |
| 2310 | case '%': |
| 2311 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2312 | if (Char == '=') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2313 | Kind = tok::percentequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2314 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2315 | } else if (Features.Digraphs && Char == '>') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2316 | Kind = tok::r_brace; // '%>' -> '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2317 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2318 | } else if (Features.Digraphs && Char == ':') { |
| 2319 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2320 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2321 | if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2322 | Kind = tok::hashhash; // '%:%:' -> '##' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2323 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 2324 | SizeTmp2, Result); |
| 2325 | } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2326 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 2327 | if (!isLexingRawMode()) |
| 2328 | Diag(BufferPtr, diag::charize_microsoft_ext); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2329 | Kind = tok::hashat; |
Chris Lattner | e91e932 | 2009-03-18 20:58:27 +0000 | [diff] [blame] | 2330 | } else { // '%:' -> '#' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2331 | // We parsed a # character. If this occurs at the start of the line, |
| 2332 | // it's actually the start of a preprocessing directive. Callback to |
| 2333 | // the preprocessor to handle it. |
| 2334 | // FIXME: -fpreprocessed mode?? |
Chris Lattner | 766703b | 2009-05-13 06:10:29 +0000 | [diff] [blame] | 2335 | if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) { |
Chris Lattner | e91e932 | 2009-03-18 20:58:27 +0000 | [diff] [blame] | 2336 | FormTokenWithChars(Result, CurPtr, tok::hash); |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 2337 | PP->HandleDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2338 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2339 | // As an optimization, if the preprocessor didn't switch lexers, tail |
| 2340 | // recurse. |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 2341 | if (PP->isCurrentLexer(this)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2342 | // Start a new token. If this is a #include or something, the PP may |
| 2343 | // want us starting at the beginning of the line again. If so, set |
Chris Lattner | 515f43f | 2010-04-12 23:04:41 +0000 | [diff] [blame] | 2344 | // the StartOfLine flag and clear LeadingSpace. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2345 | if (IsAtStartOfLine) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 2346 | Result.setFlag(Token::StartOfLine); |
Chris Lattner | 515f43f | 2010-04-12 23:04:41 +0000 | [diff] [blame] | 2347 | Result.clearFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2348 | IsAtStartOfLine = false; |
| 2349 | } |
| 2350 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 2351 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2352 | |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 2353 | return PP->Lex(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2354 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2355 | |
Chris Lattner | e91e932 | 2009-03-18 20:58:27 +0000 | [diff] [blame] | 2356 | Kind = tok::hash; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2357 | } |
| 2358 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2359 | Kind = tok::percent; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2360 | } |
| 2361 | break; |
| 2362 | case '<': |
| 2363 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2364 | if (ParsingFilename) { |
Chris Lattner | 9cb51ce | 2009-04-17 23:56:52 +0000 | [diff] [blame] | 2365 | return LexAngledStringLiteral(Result, CurPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2366 | } else if (Char == '<') { |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 2367 | char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2); |
| 2368 | if (After == '=') { |
| 2369 | Kind = tok::lesslessequal; |
| 2370 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 2371 | SizeTmp2, Result); |
| 2372 | } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) { |
| 2373 | // If this is actually a '<<<<<<<' version control conflict marker, |
| 2374 | // recognize it as such and recover nicely. |
| 2375 | goto LexNextToken; |
Peter Collingbourne | 1b791d6 | 2011-02-09 21:08:21 +0000 | [diff] [blame] | 2376 | } else if (Features.CUDA && After == '<') { |
| 2377 | Kind = tok::lesslessless; |
| 2378 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 2379 | SizeTmp2, Result); |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 2380 | } else { |
| 2381 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2382 | Kind = tok::lessless; |
| 2383 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2384 | } else if (Char == '=') { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2385 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2386 | Kind = tok::lessequal; |
| 2387 | } else if (Features.Digraphs && Char == ':') { // '<:' -> '[' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2388 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2389 | Kind = tok::l_square; |
| 2390 | } else if (Features.Digraphs && Char == '%') { // '<%' -> '{' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2391 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2392 | Kind = tok::l_brace; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2393 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2394 | Kind = tok::less; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2395 | } |
| 2396 | break; |
| 2397 | case '>': |
| 2398 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2399 | if (Char == '=') { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2400 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2401 | Kind = tok::greaterequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2402 | } else if (Char == '>') { |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 2403 | char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2); |
| 2404 | if (After == '=') { |
| 2405 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 2406 | SizeTmp2, Result); |
| 2407 | Kind = tok::greatergreaterequal; |
| 2408 | } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) { |
| 2409 | // If this is '>>>>>>>' and we're in a conflict marker, ignore it. |
| 2410 | goto LexNextToken; |
Peter Collingbourne | 1b791d6 | 2011-02-09 21:08:21 +0000 | [diff] [blame] | 2411 | } else if (Features.CUDA && After == '>') { |
| 2412 | Kind = tok::greatergreatergreater; |
| 2413 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 2414 | SizeTmp2, Result); |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 2415 | } else { |
| 2416 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2417 | Kind = tok::greatergreater; |
| 2418 | } |
| 2419 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2420 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2421 | Kind = tok::greater; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2422 | } |
| 2423 | break; |
| 2424 | case '^': |
| 2425 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2426 | if (Char == '=') { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2427 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2428 | Kind = tok::caretequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2429 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2430 | Kind = tok::caret; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2431 | } |
| 2432 | break; |
| 2433 | case '|': |
| 2434 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2435 | if (Char == '=') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2436 | Kind = tok::pipeequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2437 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2438 | } else if (Char == '|') { |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 2439 | // If this is '|||||||' and we're in a conflict marker, ignore it. |
| 2440 | if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1)) |
| 2441 | goto LexNextToken; |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2442 | Kind = tok::pipepipe; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2443 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2444 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2445 | Kind = tok::pipe; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2446 | } |
| 2447 | break; |
| 2448 | case ':': |
| 2449 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2450 | if (Features.Digraphs && Char == '>') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2451 | Kind = tok::r_square; // ':>' -> ']' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2452 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2453 | } else if (Features.CPlusPlus && Char == ':') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2454 | Kind = tok::coloncolon; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2455 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2456 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2457 | Kind = tok::colon; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2458 | } |
| 2459 | break; |
| 2460 | case ';': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2461 | Kind = tok::semi; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2462 | break; |
| 2463 | case '=': |
| 2464 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2465 | if (Char == '=') { |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 2466 | // If this is '=======' and we're in a conflict marker, ignore it. |
| 2467 | if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1)) |
| 2468 | goto LexNextToken; |
| 2469 | |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2470 | Kind = tok::equalequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2471 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2472 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2473 | Kind = tok::equal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2474 | } |
| 2475 | break; |
| 2476 | case ',': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2477 | Kind = tok::comma; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2478 | break; |
| 2479 | case '#': |
| 2480 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2481 | if (Char == '#') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2482 | Kind = tok::hashhash; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2483 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2484 | } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2485 | Kind = tok::hashat; |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 2486 | if (!isLexingRawMode()) |
| 2487 | Diag(BufferPtr, diag::charize_microsoft_ext); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2488 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2489 | } else { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2490 | // We parsed a # character. If this occurs at the start of the line, |
| 2491 | // it's actually the start of a preprocessing directive. Callback to |
| 2492 | // the preprocessor to handle it. |
| 2493 | // FIXME: -fpreprocessed mode?? |
Chris Lattner | 766703b | 2009-05-13 06:10:29 +0000 | [diff] [blame] | 2494 | if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) { |
Chris Lattner | e91e932 | 2009-03-18 20:58:27 +0000 | [diff] [blame] | 2495 | FormTokenWithChars(Result, CurPtr, tok::hash); |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 2496 | PP->HandleDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2497 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2498 | // As an optimization, if the preprocessor didn't switch lexers, tail |
| 2499 | // recurse. |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 2500 | if (PP->isCurrentLexer(this)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2501 | // Start a new token. If this is a #include or something, the PP may |
| 2502 | // want us starting at the beginning of the line again. If so, set |
Chris Lattner | 515f43f | 2010-04-12 23:04:41 +0000 | [diff] [blame] | 2503 | // the StartOfLine flag and clear LeadingSpace. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2504 | if (IsAtStartOfLine) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 2505 | Result.setFlag(Token::StartOfLine); |
Chris Lattner | 515f43f | 2010-04-12 23:04:41 +0000 | [diff] [blame] | 2506 | Result.clearFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2507 | IsAtStartOfLine = false; |
| 2508 | } |
| 2509 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 2510 | } |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 2511 | return PP->Lex(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2512 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2513 | |
Chris Lattner | e91e932 | 2009-03-18 20:58:27 +0000 | [diff] [blame] | 2514 | Kind = tok::hash; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2515 | } |
| 2516 | break; |
| 2517 | |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 2518 | case '@': |
| 2519 | // Objective C support. |
| 2520 | if (CurPtr[-1] == '@' && Features.ObjC1) |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2521 | Kind = tok::at; |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 2522 | else |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2523 | Kind = tok::unknown; |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 2524 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2525 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2526 | case '\\': |
| 2527 | // FIXME: UCN's. |
| 2528 | // FALL THROUGH. |
| 2529 | default: |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2530 | Kind = tok::unknown; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2531 | break; |
| 2532 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2533 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2534 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 2535 | MIOpt.ReadToken(); |
| 2536 | |
| 2537 | // Update the location of token as well as BufferPtr. |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2538 | FormTokenWithChars(Result, CurPtr, Kind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2539 | } |