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