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