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