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