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