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