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