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