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