Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Lexer.cpp - C Language Family Lexer ------------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 10 | // This file implements the Lexer and Token interfaces. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | // |
| 14 | // TODO: GCC Diagnostics emitted by the lexer: |
| 15 | // PEDWARN: (form feed|vertical tab) in preprocessing directive |
| 16 | // |
| 17 | // Universal characters, unicode, char mapping: |
| 18 | // WARNING: `%.*s' is not in NFKC |
| 19 | // WARNING: `%.*s' is not in NFC |
| 20 | // |
| 21 | // Other: |
| 22 | // TODO: Options to support: |
| 23 | // -fexec-charset,-fwide-exec-charset |
| 24 | // |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | |
| 27 | #include "clang/Lex/Lexer.h" |
| 28 | #include "clang/Lex/Preprocessor.h" |
Chris Lattner | 500d329 | 2009-01-29 05:15:15 +0000 | [diff] [blame] | 29 | #include "clang/Lex/LexDiagnostic.h" |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 30 | #include "clang/Lex/CodeCompletionHandler.h" |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 31 | #include "clang/Basic/SourceManager.h" |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/StringSwitch.h" |
Chris Lattner | 409a036 | 2007-07-22 18:38:25 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Compiler.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | #include "llvm/Support/MemoryBuffer.h" |
| 35 | #include <cctype> |
| 36 | using namespace clang; |
| 37 | |
Chris Lattner | a2bf105 | 2009-12-17 05:29:40 +0000 | [diff] [blame] | 38 | static void InitCharacterInfo(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | |
Chris Lattner | dbf388b | 2007-10-07 08:47:24 +0000 | [diff] [blame] | 40 | //===----------------------------------------------------------------------===// |
| 41 | // Token Class Implementation |
| 42 | //===----------------------------------------------------------------------===// |
| 43 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | /// isObjCAtKeyword - Return true if we have an ObjC keyword identifier. |
Chris Lattner | dbf388b | 2007-10-07 08:47:24 +0000 | [diff] [blame] | 45 | bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const { |
Douglas Gregor | bec1c9d | 2008-12-01 21:46:47 +0000 | [diff] [blame] | 46 | if (IdentifierInfo *II = getIdentifierInfo()) |
| 47 | return II->getObjCKeywordID() == objcKey; |
| 48 | return false; |
Chris Lattner | dbf388b | 2007-10-07 08:47:24 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | /// getObjCKeywordID - Return the ObjC keyword kind. |
| 52 | tok::ObjCKeywordKind Token::getObjCKeywordID() const { |
| 53 | IdentifierInfo *specId = getIdentifierInfo(); |
| 54 | return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword; |
| 55 | } |
| 56 | |
Chris Lattner | 53702cd | 2007-12-13 01:59:49 +0000 | [diff] [blame] | 57 | |
Chris Lattner | dbf388b | 2007-10-07 08:47:24 +0000 | [diff] [blame] | 58 | //===----------------------------------------------------------------------===// |
| 59 | // Lexer Class Implementation |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 62 | void Lexer::InitLexer(const char *BufStart, const char *BufPtr, |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 63 | const char *BufEnd) { |
Chris Lattner | a2bf105 | 2009-12-17 05:29:40 +0000 | [diff] [blame] | 64 | InitCharacterInfo(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 65 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 66 | BufferStart = BufStart; |
| 67 | BufferPtr = BufPtr; |
| 68 | BufferEnd = BufEnd; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 69 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 70 | assert(BufEnd[0] == 0 && |
| 71 | "We assume that the input buffer has a null character at the end" |
| 72 | " to simplify lexing!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 74 | Is_PragmaLexer = false; |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 75 | IsInConflictMarker = false; |
Douglas Gregor | 81b747b | 2009-09-17 21:32:03 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 77 | // Start of the file is a start of line. |
| 78 | IsAtStartOfLine = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 79 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 80 | // We are not after parsing a #. |
| 81 | ParsingPreprocessorDirective = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 82 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 83 | // We are not after parsing #include. |
| 84 | ParsingFilename = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 86 | // We are not in raw mode. Raw mode disables diagnostics and interpretation |
| 87 | // of tokens (e.g. identifiers, thus disabling macro expansion). It is used |
| 88 | // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block |
| 89 | // or otherwise skipping over tokens. |
| 90 | LexingRawMode = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 92 | // Default to not keeping comments. |
| 93 | ExtendedTokenMode = 0; |
| 94 | } |
| 95 | |
Chris Lattner | 0770dab | 2009-01-17 07:56:59 +0000 | [diff] [blame] | 96 | /// Lexer constructor - Create a new lexer object for the specified buffer |
| 97 | /// with the specified preprocessor managing the lexing process. This lexer |
| 98 | /// assumes that the associated file buffer and Preprocessor objects will |
| 99 | /// outlive it, so it doesn't take ownership of either of them. |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 100 | Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP) |
Chris Lattner | 88d3ac1 | 2009-01-17 08:03:42 +0000 | [diff] [blame] | 101 | : PreprocessorLexer(&PP, FID), |
| 102 | FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)), |
| 103 | Features(PP.getLangOptions()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 0770dab | 2009-01-17 07:56:59 +0000 | [diff] [blame] | 105 | InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(), |
| 106 | InputFile->getBufferEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 0770dab | 2009-01-17 07:56:59 +0000 | [diff] [blame] | 108 | // Default to keeping comments if the preprocessor wants them. |
| 109 | SetCommentRetentionState(PP.getCommentRetentionState()); |
| 110 | } |
Chris Lattner | dbf388b | 2007-10-07 08:47:24 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 112 | /// Lexer constructor - Create a new raw lexer object. This object is only |
Chris Lattner | 590f0cc | 2008-10-12 01:15:46 +0000 | [diff] [blame] | 113 | /// suitable for calls to 'LexRawToken'. This lexer assumes that the text |
| 114 | /// range will outlive it, so it doesn't take ownership of it. |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 115 | Lexer::Lexer(SourceLocation fileloc, const LangOptions &features, |
Chris Lattner | de96c0f | 2009-01-17 07:42:27 +0000 | [diff] [blame] | 116 | const char *BufStart, const char *BufPtr, const char *BufEnd) |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 117 | : FileLoc(fileloc), Features(features) { |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 22d91ca | 2009-01-17 06:55:17 +0000 | [diff] [blame] | 119 | InitLexer(BufStart, BufPtr, BufEnd); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 120 | |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 121 | // We *are* in raw mode. |
| 122 | LexingRawMode = true; |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 125 | /// Lexer constructor - Create a new raw lexer object. This object is only |
| 126 | /// suitable for calls to 'LexRawToken'. This lexer assumes that the text |
| 127 | /// range will outlive it, so it doesn't take ownership of it. |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 128 | Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile, |
| 129 | const SourceManager &SM, const LangOptions &features) |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 130 | : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) { |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 131 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 132 | InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(), |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 133 | FromFile->getBufferEnd()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 134 | |
Chris Lattner | 025c3a6 | 2009-01-17 07:35:14 +0000 | [diff] [blame] | 135 | // We *are* in raw mode. |
| 136 | LexingRawMode = true; |
| 137 | } |
| 138 | |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 139 | /// Create_PragmaLexer: Lexer constructor - Create a new lexer object for |
| 140 | /// _Pragma expansion. This has a variety of magic semantics that this method |
| 141 | /// sets up. It returns a new'd Lexer that must be delete'd when done. |
| 142 | /// |
| 143 | /// On entrance to this routine, TokStartLoc is a macro location which has a |
| 144 | /// spelling loc that indicates the bytes to be lexed for the token and an |
| 145 | /// instantiation location that indicates where all lexed tokens should be |
| 146 | /// "expanded from". |
| 147 | /// |
| 148 | /// FIXME: It would really be nice to make _Pragma just be a wrapper around a |
| 149 | /// normal lexer that remaps tokens as they fly by. This would require making |
| 150 | /// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer |
| 151 | /// interface that could handle this stuff. This would pull GetMappedTokenLoc |
| 152 | /// out of the critical path of the lexer! |
| 153 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc, |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 155 | SourceLocation InstantiationLocStart, |
| 156 | SourceLocation InstantiationLocEnd, |
Chris Lattner | bcc2a67 | 2009-01-19 06:46:35 +0000 | [diff] [blame] | 157 | unsigned TokLen, Preprocessor &PP) { |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 158 | SourceManager &SM = PP.getSourceManager(); |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 159 | |
| 160 | // Create the lexer as if we were going to lex the file normally. |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 161 | FileID SpellingFID = SM.getFileID(SpellingLoc); |
Chris Lattner | 6e29014 | 2009-11-30 04:18:44 +0000 | [diff] [blame] | 162 | const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID); |
| 163 | Lexer *L = new Lexer(SpellingFID, InputFile, PP); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 165 | // Now that the lexer is created, change the start/end locations so that we |
| 166 | // just lex the subsection of the file that we want. This is lexing from a |
| 167 | // scratch buffer. |
| 168 | const char *StrData = SM.getCharacterData(SpellingLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 169 | |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 170 | L->BufferPtr = StrData; |
| 171 | L->BufferEnd = StrData+TokLen; |
Chris Lattner | 1fa4953 | 2009-03-08 08:08:45 +0000 | [diff] [blame] | 172 | assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!"); |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 173 | |
| 174 | // Set the SourceLocation with the remapping information. This ensures that |
| 175 | // GetMappedTokenLoc will remap the tokens as they are lexed. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 176 | L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID), |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 177 | InstantiationLocStart, |
| 178 | InstantiationLocEnd, TokLen); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 179 | |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 180 | // Ensure that the lexer thinks it is inside a directive, so that end \n will |
| 181 | // return an EOM token. |
| 182 | L->ParsingPreprocessorDirective = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Chris Lattner | 42e00d1 | 2009-01-17 08:27:52 +0000 | [diff] [blame] | 184 | // This lexer really is for _Pragma. |
| 185 | L->Is_PragmaLexer = true; |
| 186 | return L; |
| 187 | } |
| 188 | |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 189 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | /// Stringify - Convert the specified string into a C string, with surrounding |
| 191 | /// ""'s, and with escaped \ and " characters. |
| 192 | std::string Lexer::Stringify(const std::string &Str, bool Charify) { |
| 193 | std::string Result = Str; |
| 194 | char Quote = Charify ? '\'' : '"'; |
| 195 | for (unsigned i = 0, e = Result.size(); i != e; ++i) { |
| 196 | if (Result[i] == '\\' || Result[i] == Quote) { |
| 197 | Result.insert(Result.begin()+i, '\\'); |
| 198 | ++i; ++e; |
| 199 | } |
| 200 | } |
| 201 | return Result; |
| 202 | } |
| 203 | |
Chris Lattner | d8e3083 | 2007-07-24 06:57:14 +0000 | [diff] [blame] | 204 | /// Stringify - Convert the specified string into a C string by escaping '\' |
| 205 | /// and " characters. This does not add surrounding ""'s to the string. |
| 206 | void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) { |
| 207 | for (unsigned i = 0, e = Str.size(); i != e; ++i) { |
| 208 | if (Str[i] == '\\' || Str[i] == '"') { |
| 209 | Str.insert(Str.begin()+i, '\\'); |
| 210 | ++i; ++e; |
| 211 | } |
| 212 | } |
| 213 | } |
| 214 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 215 | static bool isWhitespace(unsigned char c); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | |
Chris Lattner | 9a61194 | 2007-10-17 21:18:47 +0000 | [diff] [blame] | 217 | /// MeasureTokenLength - Relex the token at the specified location and return |
| 218 | /// its length in bytes in the input file. If the token needs cleaning (e.g. |
| 219 | /// includes a trigraph or an escaped newline) then this count includes bytes |
| 220 | /// that are part of that. |
| 221 | unsigned Lexer::MeasureTokenLength(SourceLocation Loc, |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 222 | const SourceManager &SM, |
| 223 | const LangOptions &LangOpts) { |
Chris Lattner | 9a61194 | 2007-10-17 21:18:47 +0000 | [diff] [blame] | 224 | // TODO: this could be special cased for common tokens like identifiers, ')', |
| 225 | // etc to make this faster, if it mattered. Just look at StrData[0] to handle |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | // all obviously single-char tokens. This could use |
Chris Lattner | 9a61194 | 2007-10-17 21:18:47 +0000 | [diff] [blame] | 227 | // Lexer::isObviouslySimpleCharacter for example to handle identifiers or |
| 228 | // something. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 229 | |
| 230 | // If this comes from a macro expansion, we really do want the macro name, not |
| 231 | // the token this macro expanded to. |
Chris Lattner | 363fdc2 | 2009-01-26 22:24:27 +0000 | [diff] [blame] | 232 | Loc = SM.getInstantiationLoc(Loc); |
| 233 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 234 | bool Invalid = false; |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 235 | llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 236 | if (Invalid) |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 237 | return 0; |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 238 | |
| 239 | const char *StrData = Buffer.data()+LocInfo.second; |
Chris Lattner | 8350394 | 2009-01-17 08:30:10 +0000 | [diff] [blame] | 240 | |
Douglas Gregor | 33e9abd | 2010-01-22 19:49:59 +0000 | [diff] [blame] | 241 | if (isWhitespace(StrData[0])) |
| 242 | return 0; |
| 243 | |
Chris Lattner | 9a61194 | 2007-10-17 21:18:47 +0000 | [diff] [blame] | 244 | // Create a lexer starting at the beginning of this token. |
Sebastian Redl | c3526d8 | 2010-09-30 01:03:03 +0000 | [diff] [blame] | 245 | Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, |
| 246 | Buffer.begin(), StrData, Buffer.end()); |
Chris Lattner | 39de740 | 2009-10-14 15:04:18 +0000 | [diff] [blame] | 247 | TheLexer.SetCommentRetentionState(true); |
Chris Lattner | 9a61194 | 2007-10-17 21:18:47 +0000 | [diff] [blame] | 248 | Token TheTok; |
Chris Lattner | 590f0cc | 2008-10-12 01:15:46 +0000 | [diff] [blame] | 249 | TheLexer.LexFromRawLexer(TheTok); |
Chris Lattner | 9a61194 | 2007-10-17 21:18:47 +0000 | [diff] [blame] | 250 | return TheTok.getLength(); |
| 251 | } |
| 252 | |
Douglas Gregor | a8e5c5b | 2010-07-22 20:22:31 +0000 | [diff] [blame] | 253 | SourceLocation Lexer::GetBeginningOfToken(SourceLocation Loc, |
| 254 | const SourceManager &SM, |
| 255 | const LangOptions &LangOpts) { |
| 256 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
| 257 | bool Invalid = false; |
| 258 | llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); |
| 259 | if (Invalid) |
| 260 | return Loc; |
| 261 | |
| 262 | // Back up from the current location until we hit the beginning of a line |
| 263 | // (or the buffer). We'll relex from that point. |
| 264 | const char *BufStart = Buffer.data(); |
| 265 | const char *StrData = BufStart+LocInfo.second; |
| 266 | if (StrData[0] == '\n' || StrData[0] == '\r') |
| 267 | return Loc; |
| 268 | |
| 269 | const char *LexStart = StrData; |
| 270 | while (LexStart != BufStart) { |
| 271 | if (LexStart[0] == '\n' || LexStart[0] == '\r') { |
| 272 | ++LexStart; |
| 273 | break; |
| 274 | } |
| 275 | |
| 276 | --LexStart; |
| 277 | } |
| 278 | |
| 279 | // Create a lexer starting at the beginning of this token. |
| 280 | SourceLocation LexerStartLoc = Loc.getFileLocWithOffset(-LocInfo.second); |
| 281 | Lexer TheLexer(LexerStartLoc, LangOpts, BufStart, LexStart, Buffer.end()); |
| 282 | TheLexer.SetCommentRetentionState(true); |
| 283 | |
| 284 | // Lex tokens until we find the token that contains the source location. |
| 285 | Token TheTok; |
| 286 | do { |
| 287 | TheLexer.LexFromRawLexer(TheTok); |
| 288 | |
| 289 | if (TheLexer.getBufferLocation() > StrData) { |
| 290 | // Lexing this token has taken the lexer past the source location we're |
| 291 | // looking for. If the current token encompasses our source location, |
| 292 | // return the beginning of that token. |
| 293 | if (TheLexer.getBufferLocation() - TheTok.getLength() <= StrData) |
| 294 | return TheTok.getLocation(); |
| 295 | |
| 296 | // We ended up skipping over the source location entirely, which means |
| 297 | // that it points into whitespace. We're done here. |
| 298 | break; |
| 299 | } |
| 300 | } while (TheTok.getKind() != tok::eof); |
| 301 | |
| 302 | // We've passed our source location; just return the original source location. |
| 303 | return Loc; |
| 304 | } |
| 305 | |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 306 | namespace { |
| 307 | enum PreambleDirectiveKind { |
| 308 | PDK_Skipped, |
| 309 | PDK_StartIf, |
| 310 | PDK_EndIf, |
| 311 | PDK_Unknown |
| 312 | }; |
| 313 | } |
| 314 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 315 | std::pair<unsigned, bool> |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 316 | Lexer::ComputePreamble(const llvm::MemoryBuffer *Buffer, unsigned MaxLines) { |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 317 | // Create a lexer starting at the beginning of the file. Note that we use a |
| 318 | // "fake" file source location at offset 1 so that the lexer will track our |
| 319 | // position within the file. |
| 320 | const unsigned StartOffset = 1; |
| 321 | SourceLocation StartLoc = SourceLocation::getFromRawEncoding(StartOffset); |
| 322 | LangOptions LangOpts; |
| 323 | Lexer TheLexer(StartLoc, LangOpts, Buffer->getBufferStart(), |
| 324 | Buffer->getBufferStart(), Buffer->getBufferEnd()); |
| 325 | |
| 326 | bool InPreprocessorDirective = false; |
| 327 | Token TheTok; |
| 328 | Token IfStartTok; |
| 329 | unsigned IfCount = 0; |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 330 | unsigned Line = 0; |
| 331 | |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 332 | do { |
| 333 | TheLexer.LexFromRawLexer(TheTok); |
| 334 | |
| 335 | if (InPreprocessorDirective) { |
| 336 | // If we've hit the end of the file, we're done. |
| 337 | if (TheTok.getKind() == tok::eof) { |
| 338 | InPreprocessorDirective = false; |
| 339 | break; |
| 340 | } |
| 341 | |
| 342 | // If we haven't hit the end of the preprocessor directive, skip this |
| 343 | // token. |
| 344 | if (!TheTok.isAtStartOfLine()) |
| 345 | continue; |
| 346 | |
| 347 | // We've passed the end of the preprocessor directive, and will look |
| 348 | // at this token again below. |
| 349 | InPreprocessorDirective = false; |
| 350 | } |
| 351 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 352 | // Keep track of the # of lines in the preamble. |
| 353 | if (TheTok.isAtStartOfLine()) { |
| 354 | ++Line; |
| 355 | |
| 356 | // If we were asked to limit the number of lines in the preamble, |
| 357 | // and we're about to exceed that limit, we're done. |
| 358 | if (MaxLines && Line >= MaxLines) |
| 359 | break; |
| 360 | } |
| 361 | |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 362 | // Comments are okay; skip over them. |
| 363 | if (TheTok.getKind() == tok::comment) |
| 364 | continue; |
| 365 | |
| 366 | if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) { |
| 367 | // This is the start of a preprocessor directive. |
| 368 | Token HashTok = TheTok; |
| 369 | InPreprocessorDirective = true; |
| 370 | |
| 371 | // Figure out which direective this is. Since we're lexing raw tokens, |
| 372 | // we don't have an identifier table available. Instead, just look at |
| 373 | // the raw identifier to recognize and categorize preprocessor directives. |
| 374 | TheLexer.LexFromRawLexer(TheTok); |
| 375 | if (TheTok.getKind() == tok::identifier && !TheTok.needsCleaning()) { |
| 376 | const char *IdStart = Buffer->getBufferStart() |
| 377 | + TheTok.getLocation().getRawEncoding() - 1; |
| 378 | llvm::StringRef Keyword(IdStart, TheTok.getLength()); |
| 379 | PreambleDirectiveKind PDK |
| 380 | = llvm::StringSwitch<PreambleDirectiveKind>(Keyword) |
| 381 | .Case("include", PDK_Skipped) |
| 382 | .Case("__include_macros", PDK_Skipped) |
| 383 | .Case("define", PDK_Skipped) |
| 384 | .Case("undef", PDK_Skipped) |
| 385 | .Case("line", PDK_Skipped) |
| 386 | .Case("error", PDK_Skipped) |
| 387 | .Case("pragma", PDK_Skipped) |
| 388 | .Case("import", PDK_Skipped) |
| 389 | .Case("include_next", PDK_Skipped) |
| 390 | .Case("warning", PDK_Skipped) |
| 391 | .Case("ident", PDK_Skipped) |
| 392 | .Case("sccs", PDK_Skipped) |
| 393 | .Case("assert", PDK_Skipped) |
| 394 | .Case("unassert", PDK_Skipped) |
| 395 | .Case("if", PDK_StartIf) |
| 396 | .Case("ifdef", PDK_StartIf) |
| 397 | .Case("ifndef", PDK_StartIf) |
| 398 | .Case("elif", PDK_Skipped) |
| 399 | .Case("else", PDK_Skipped) |
| 400 | .Case("endif", PDK_EndIf) |
| 401 | .Default(PDK_Unknown); |
| 402 | |
| 403 | switch (PDK) { |
| 404 | case PDK_Skipped: |
| 405 | continue; |
| 406 | |
| 407 | case PDK_StartIf: |
| 408 | if (IfCount == 0) |
| 409 | IfStartTok = HashTok; |
| 410 | |
| 411 | ++IfCount; |
| 412 | continue; |
| 413 | |
| 414 | case PDK_EndIf: |
| 415 | // Mismatched #endif. The preamble ends here. |
| 416 | if (IfCount == 0) |
| 417 | break; |
| 418 | |
| 419 | --IfCount; |
| 420 | continue; |
| 421 | |
| 422 | case PDK_Unknown: |
| 423 | // We don't know what this directive is; stop at the '#'. |
| 424 | break; |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | // We only end up here if we didn't recognize the preprocessor |
| 429 | // directive or it was one that can't occur in the preamble at this |
| 430 | // point. Roll back the current token to the location of the '#'. |
| 431 | InPreprocessorDirective = false; |
| 432 | TheTok = HashTok; |
| 433 | } |
| 434 | |
Douglas Gregor | df95a13 | 2010-08-09 20:45:32 +0000 | [diff] [blame] | 435 | // We hit a token that we don't recognize as being in the |
| 436 | // "preprocessing only" part of the file, so we're no longer in |
| 437 | // the preamble. |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 438 | break; |
| 439 | } while (true); |
| 440 | |
| 441 | SourceLocation End = IfCount? IfStartTok.getLocation() : TheTok.getLocation(); |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 442 | return std::make_pair(End.getRawEncoding() - StartLoc.getRawEncoding(), |
| 443 | IfCount? IfStartTok.isAtStartOfLine() |
| 444 | : TheTok.isAtStartOfLine()); |
Douglas Gregor | f033f1d | 2010-07-20 20:18:03 +0000 | [diff] [blame] | 445 | } |
| 446 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 447 | //===----------------------------------------------------------------------===// |
| 448 | // Character information. |
| 449 | //===----------------------------------------------------------------------===// |
| 450 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 451 | enum { |
| 452 | CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0' |
| 453 | CHAR_VERT_WS = 0x02, // '\r', '\n' |
| 454 | CHAR_LETTER = 0x04, // a-z,A-Z |
| 455 | CHAR_NUMBER = 0x08, // 0-9 |
| 456 | CHAR_UNDER = 0x10, // _ |
| 457 | CHAR_PERIOD = 0x20 // . |
| 458 | }; |
| 459 | |
Chris Lattner | 03b9866 | 2009-07-07 17:09:54 +0000 | [diff] [blame] | 460 | // Statically initialize CharInfo table based on ASCII character set |
| 461 | // Reference: FreeBSD 7.2 /usr/share/misc/ascii |
Chris Lattner | a2bf105 | 2009-12-17 05:29:40 +0000 | [diff] [blame] | 462 | static const unsigned char CharInfo[256] = |
Chris Lattner | 03b9866 | 2009-07-07 17:09:54 +0000 | [diff] [blame] | 463 | { |
| 464 | // 0 NUL 1 SOH 2 STX 3 ETX |
| 465 | // 4 EOT 5 ENQ 6 ACK 7 BEL |
| 466 | 0 , 0 , 0 , 0 , |
| 467 | 0 , 0 , 0 , 0 , |
| 468 | // 8 BS 9 HT 10 NL 11 VT |
| 469 | //12 NP 13 CR 14 SO 15 SI |
| 470 | 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS, |
| 471 | CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 , |
| 472 | //16 DLE 17 DC1 18 DC2 19 DC3 |
| 473 | //20 DC4 21 NAK 22 SYN 23 ETB |
| 474 | 0 , 0 , 0 , 0 , |
| 475 | 0 , 0 , 0 , 0 , |
| 476 | //24 CAN 25 EM 26 SUB 27 ESC |
| 477 | //28 FS 29 GS 30 RS 31 US |
| 478 | 0 , 0 , 0 , 0 , |
| 479 | 0 , 0 , 0 , 0 , |
| 480 | //32 SP 33 ! 34 " 35 # |
| 481 | //36 $ 37 % 38 & 39 ' |
| 482 | CHAR_HORZ_WS, 0 , 0 , 0 , |
| 483 | 0 , 0 , 0 , 0 , |
| 484 | //40 ( 41 ) 42 * 43 + |
| 485 | //44 , 45 - 46 . 47 / |
| 486 | 0 , 0 , 0 , 0 , |
| 487 | 0 , 0 , CHAR_PERIOD , 0 , |
| 488 | //48 0 49 1 50 2 51 3 |
| 489 | //52 4 53 5 54 6 55 7 |
| 490 | CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , |
| 491 | CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , |
| 492 | //56 8 57 9 58 : 59 ; |
| 493 | //60 < 61 = 62 > 63 ? |
| 494 | CHAR_NUMBER , CHAR_NUMBER , 0 , 0 , |
| 495 | 0 , 0 , 0 , 0 , |
| 496 | //64 @ 65 A 66 B 67 C |
| 497 | //68 D 69 E 70 F 71 G |
| 498 | 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 499 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 500 | //72 H 73 I 74 J 75 K |
| 501 | //76 L 77 M 78 N 79 O |
| 502 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 503 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 504 | //80 P 81 Q 82 R 83 S |
| 505 | //84 T 85 U 86 V 87 W |
| 506 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 507 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 508 | //88 X 89 Y 90 Z 91 [ |
| 509 | //92 \ 93 ] 94 ^ 95 _ |
| 510 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 , |
| 511 | 0 , 0 , 0 , CHAR_UNDER , |
| 512 | //96 ` 97 a 98 b 99 c |
| 513 | //100 d 101 e 102 f 103 g |
| 514 | 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 515 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 516 | //104 h 105 i 106 j 107 k |
| 517 | //108 l 109 m 110 n 111 o |
| 518 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 519 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 520 | //112 p 113 q 114 r 115 s |
| 521 | //116 t 117 u 118 v 119 w |
| 522 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 523 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , |
| 524 | //120 x 121 y 122 z 123 { |
| 525 | //124 | 125 } 126 ~ 127 DEL |
| 526 | CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 , |
| 527 | 0 , 0 , 0 , 0 |
| 528 | }; |
| 529 | |
Chris Lattner | a2bf105 | 2009-12-17 05:29:40 +0000 | [diff] [blame] | 530 | static void InitCharacterInfo() { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 531 | static bool isInited = false; |
| 532 | if (isInited) return; |
Chris Lattner | 03b9866 | 2009-07-07 17:09:54 +0000 | [diff] [blame] | 533 | // check the statically-initialized CharInfo table |
| 534 | assert(CHAR_HORZ_WS == CharInfo[(int)' ']); |
| 535 | assert(CHAR_HORZ_WS == CharInfo[(int)'\t']); |
| 536 | assert(CHAR_HORZ_WS == CharInfo[(int)'\f']); |
| 537 | assert(CHAR_HORZ_WS == CharInfo[(int)'\v']); |
| 538 | assert(CHAR_VERT_WS == CharInfo[(int)'\n']); |
| 539 | assert(CHAR_VERT_WS == CharInfo[(int)'\r']); |
| 540 | assert(CHAR_UNDER == CharInfo[(int)'_']); |
| 541 | assert(CHAR_PERIOD == CharInfo[(int)'.']); |
| 542 | for (unsigned i = 'a'; i <= 'z'; ++i) { |
| 543 | assert(CHAR_LETTER == CharInfo[i]); |
| 544 | assert(CHAR_LETTER == CharInfo[i+'A'-'a']); |
| 545 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 546 | for (unsigned i = '0'; i <= '9'; ++i) |
Chris Lattner | 03b9866 | 2009-07-07 17:09:54 +0000 | [diff] [blame] | 547 | assert(CHAR_NUMBER == CharInfo[i]); |
Steve Naroff | 7b68265 | 2009-12-08 16:38:12 +0000 | [diff] [blame] | 548 | |
Chris Lattner | 03b9866 | 2009-07-07 17:09:54 +0000 | [diff] [blame] | 549 | isInited = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Chris Lattner | 03b9866 | 2009-07-07 17:09:54 +0000 | [diff] [blame] | 552 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 553 | /// isIdentifierBody - Return true if this is the body character of an |
| 554 | /// identifier, which is [a-zA-Z0-9_]. |
| 555 | static inline bool isIdentifierBody(unsigned char c) { |
Hartmut Kaiser | 95c062b | 2007-10-18 12:47:01 +0000 | [diff] [blame] | 556 | return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 557 | } |
| 558 | |
| 559 | /// isHorizontalWhitespace - Return true if this character is horizontal |
| 560 | /// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'. |
| 561 | static inline bool isHorizontalWhitespace(unsigned char c) { |
Hartmut Kaiser | 95c062b | 2007-10-18 12:47:01 +0000 | [diff] [blame] | 562 | return (CharInfo[c] & CHAR_HORZ_WS) ? true : false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | /// isWhitespace - Return true if this character is horizontal or vertical |
| 566 | /// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false |
| 567 | /// for '\0'. |
| 568 | static inline bool isWhitespace(unsigned char c) { |
Hartmut Kaiser | 95c062b | 2007-10-18 12:47:01 +0000 | [diff] [blame] | 569 | return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | /// isNumberBody - Return true if this is the body character of an |
| 573 | /// preprocessing number, which is [a-zA-Z0-9_.]. |
| 574 | static inline bool isNumberBody(unsigned char c) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 575 | return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ? |
Hartmut Kaiser | 95c062b | 2007-10-18 12:47:01 +0000 | [diff] [blame] | 576 | true : false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | |
| 580 | //===----------------------------------------------------------------------===// |
| 581 | // Diagnostics forwarding code. |
| 582 | //===----------------------------------------------------------------------===// |
| 583 | |
Chris Lattner | 409a036 | 2007-07-22 18:38:25 +0000 | [diff] [blame] | 584 | /// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the |
| 585 | /// lexer buffer was all instantiated at a single point, perform the mapping. |
| 586 | /// This is currently only used for _Pragma implementation, so it is the slow |
| 587 | /// path of the hot getSourceLocation method. Do not allow it to be inlined. |
Benjamin Kramer | c997eb4 | 2009-11-14 16:36:57 +0000 | [diff] [blame] | 588 | static DISABLE_INLINE SourceLocation GetMappedTokenLoc(Preprocessor &PP, |
| 589 | SourceLocation FileLoc, |
| 590 | unsigned CharNo, |
| 591 | unsigned TokLen); |
Chris Lattner | 409a036 | 2007-07-22 18:38:25 +0000 | [diff] [blame] | 592 | static SourceLocation GetMappedTokenLoc(Preprocessor &PP, |
| 593 | SourceLocation FileLoc, |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 594 | unsigned CharNo, unsigned TokLen) { |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 595 | assert(FileLoc.isMacroID() && "Must be an instantiation"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 409a036 | 2007-07-22 18:38:25 +0000 | [diff] [blame] | 597 | // Otherwise, we're lexing "mapped tokens". This is used for things like |
| 598 | // _Pragma handling. Combine the instantiation location of FileLoc with the |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 599 | // spelling location. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 600 | SourceManager &SM = PP.getSourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 602 | // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 603 | // characters come from spelling(FileLoc)+Offset. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 604 | SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc); |
Chris Lattner | bcc2a67 | 2009-01-19 06:46:35 +0000 | [diff] [blame] | 605 | SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 606 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 607 | // Figure out the expansion loc range, which is the range covered by the |
| 608 | // original _Pragma(...) sequence. |
| 609 | std::pair<SourceLocation,SourceLocation> II = |
| 610 | SM.getImmediateInstantiationRange(FileLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 611 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 612 | return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen); |
Chris Lattner | 409a036 | 2007-07-22 18:38:25 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 615 | /// getSourceLocation - Return a source location identifier for the specified |
| 616 | /// offset in the current file. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 617 | SourceLocation Lexer::getSourceLocation(const char *Loc, |
| 618 | unsigned TokLen) const { |
Chris Lattner | 448cec4 | 2007-07-22 18:44:36 +0000 | [diff] [blame] | 619 | assert(Loc >= BufferStart && Loc <= BufferEnd && |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 620 | "Location out of range for this buffer!"); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 621 | |
| 622 | // In the normal case, we're just lexing from a simple file buffer, return |
| 623 | // the file id from FileLoc with the offset specified. |
Chris Lattner | 448cec4 | 2007-07-22 18:44:36 +0000 | [diff] [blame] | 624 | unsigned CharNo = Loc-BufferStart; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 625 | if (FileLoc.isFileID()) |
Chris Lattner | bcc2a67 | 2009-01-19 06:46:35 +0000 | [diff] [blame] | 626 | return FileLoc.getFileLocWithOffset(CharNo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 627 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 628 | // Otherwise, this is the _Pragma lexer case, which pretends that all of the |
| 629 | // tokens are lexed from where the _Pragma was defined. |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 630 | assert(PP && "This doesn't work on raw lexers"); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 631 | return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 632 | } |
| 633 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 634 | /// Diag - Forwarding function for diagnostics. This translate a source |
| 635 | /// position in the current buffer into a SourceLocation object for rendering. |
Chris Lattner | 3cbfe2c | 2008-11-22 00:59:29 +0000 | [diff] [blame] | 636 | DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const { |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 637 | return PP->Diag(getSourceLocation(Loc), DiagID); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 638 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 639 | |
| 640 | //===----------------------------------------------------------------------===// |
| 641 | // Trigraph and Escaped Newline Handling Code. |
| 642 | //===----------------------------------------------------------------------===// |
| 643 | |
| 644 | /// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair, |
| 645 | /// return the decoded trigraph letter it corresponds to, or '\0' if nothing. |
| 646 | static char GetTrigraphCharForLetter(char Letter) { |
| 647 | switch (Letter) { |
| 648 | default: return 0; |
| 649 | case '=': return '#'; |
| 650 | case ')': return ']'; |
| 651 | case '(': return '['; |
| 652 | case '!': return '|'; |
| 653 | case '\'': return '^'; |
| 654 | case '>': return '}'; |
| 655 | case '/': return '\\'; |
| 656 | case '<': return '{'; |
| 657 | case '-': return '~'; |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | /// DecodeTrigraphChar - If the specified character is a legal trigraph when |
| 662 | /// prefixed with ??, emit a trigraph warning. If trigraphs are enabled, |
| 663 | /// return the result character. Finally, emit a warning about trigraph use |
| 664 | /// whether trigraphs are enabled or not. |
| 665 | static char DecodeTrigraphChar(const char *CP, Lexer *L) { |
| 666 | char Res = GetTrigraphCharForLetter(*CP); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 667 | if (!Res || !L) return Res; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 668 | |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 669 | if (!L->getFeatures().Trigraphs) { |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 670 | if (!L->isLexingRawMode()) |
| 671 | L->Diag(CP-2, diag::trigraph_ignored); |
Chris Lattner | 3692b09 | 2008-11-18 07:59:24 +0000 | [diff] [blame] | 672 | return 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 673 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 674 | |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 675 | if (!L->isLexingRawMode()) |
Benjamin Kramer | 476d8b8 | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 676 | L->Diag(CP-2, diag::trigraph_converted) << llvm::StringRef(&Res, 1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 677 | return Res; |
| 678 | } |
| 679 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 680 | /// getEscapedNewLineSize - Return the size of the specified escaped newline, |
| 681 | /// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 682 | /// trigraph equivalent on entry to this function. |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 683 | unsigned Lexer::getEscapedNewLineSize(const char *Ptr) { |
| 684 | unsigned Size = 0; |
| 685 | while (isWhitespace(Ptr[Size])) { |
| 686 | ++Size; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 687 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 688 | if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r') |
| 689 | continue; |
| 690 | |
| 691 | // If this is a \r\n or \n\r, skip the other half. |
| 692 | if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') && |
| 693 | Ptr[Size-1] != Ptr[Size]) |
| 694 | ++Size; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 695 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 696 | return Size; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 697 | } |
| 698 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 699 | // Not an escaped newline, must be a \t or something else. |
| 700 | return 0; |
| 701 | } |
| 702 | |
Chris Lattner | 0337495 | 2009-04-18 22:27:02 +0000 | [diff] [blame] | 703 | /// SkipEscapedNewLines - If P points to an escaped newline (or a series of |
| 704 | /// them), skip over them and return the first non-escaped-newline found, |
| 705 | /// otherwise return P. |
| 706 | const char *Lexer::SkipEscapedNewLines(const char *P) { |
| 707 | while (1) { |
| 708 | const char *AfterEscape; |
| 709 | if (*P == '\\') { |
| 710 | AfterEscape = P+1; |
| 711 | } else if (*P == '?') { |
| 712 | // If not a trigraph for escape, bail out. |
| 713 | if (P[1] != '?' || P[2] != '/') |
| 714 | return P; |
| 715 | AfterEscape = P+3; |
| 716 | } else { |
| 717 | return P; |
| 718 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 719 | |
Chris Lattner | 0337495 | 2009-04-18 22:27:02 +0000 | [diff] [blame] | 720 | unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape); |
| 721 | if (NewLineSize == 0) return P; |
| 722 | P = AfterEscape+NewLineSize; |
| 723 | } |
| 724 | } |
| 725 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 726 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 727 | /// getCharAndSizeSlow - Peek a single 'character' from the specified buffer, |
| 728 | /// get its size, and return it. This is tricky in several cases: |
| 729 | /// 1. If currently at the start of a trigraph, we warn about the trigraph, |
| 730 | /// then either return the trigraph (skipping 3 chars) or the '?', |
| 731 | /// depending on whether trigraphs are enabled or not. |
| 732 | /// 2. If this is an escaped newline (potentially with whitespace between |
| 733 | /// the backslash and newline), implicitly skip the newline and return |
| 734 | /// the char after it. |
| 735 | /// 3. If this is a UCN, return it. FIXME: C++ UCN's? |
| 736 | /// |
| 737 | /// This handles the slow/uncommon case of the getCharAndSize method. Here we |
| 738 | /// know that we can accumulate into Size, and that we have already incremented |
| 739 | /// Ptr by Size bytes. |
| 740 | /// |
| 741 | /// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should |
| 742 | /// be updated to match. |
| 743 | /// |
| 744 | char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size, |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 745 | Token *Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 746 | // If we have a slash, look for an escaped newline. |
| 747 | if (Ptr[0] == '\\') { |
| 748 | ++Size; |
| 749 | ++Ptr; |
| 750 | Slash: |
| 751 | // Common case, backslash-char where the char is not whitespace. |
| 752 | if (!isWhitespace(Ptr[0])) return '\\'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 753 | |
Chris Lattner | 5636a3b | 2009-06-23 05:15:06 +0000 | [diff] [blame] | 754 | // See if we have optional whitespace characters between the slash and |
| 755 | // newline. |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 756 | if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { |
| 757 | // Remember that this token needs to be cleaned. |
| 758 | if (Tok) Tok->setFlag(Token::NeedsCleaning); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 759 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 760 | // Warn if there was whitespace between the backslash and newline. |
Chris Lattner | 5636a3b | 2009-06-23 05:15:06 +0000 | [diff] [blame] | 761 | if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode()) |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 762 | Diag(Ptr, diag::backslash_newline_space); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 763 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 764 | // Found backslash<whitespace><newline>. Parse the char after it. |
| 765 | Size += EscapedNewLineSize; |
| 766 | Ptr += EscapedNewLineSize; |
| 767 | // Use slow version to accumulate a correct size field. |
| 768 | return getCharAndSizeSlow(Ptr, Size, Tok); |
| 769 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 770 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 771 | // Otherwise, this is not an escaped newline, just return the slash. |
| 772 | return '\\'; |
| 773 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 774 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 775 | // If this is a trigraph, process it. |
| 776 | if (Ptr[0] == '?' && Ptr[1] == '?') { |
| 777 | // If this is actually a legal trigraph (not something like "??x"), emit |
| 778 | // a trigraph warning. If so, and if trigraphs are enabled, return it. |
| 779 | if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) { |
| 780 | // Remember that this token needs to be cleaned. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 781 | if (Tok) Tok->setFlag(Token::NeedsCleaning); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 782 | |
| 783 | Ptr += 3; |
| 784 | Size += 3; |
| 785 | if (C == '\\') goto Slash; |
| 786 | return C; |
| 787 | } |
| 788 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 789 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 790 | // If this is neither, return a single character. |
| 791 | ++Size; |
| 792 | return *Ptr; |
| 793 | } |
| 794 | |
| 795 | |
| 796 | /// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the |
| 797 | /// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size, |
| 798 | /// and that we have already incremented Ptr by Size bytes. |
| 799 | /// |
| 800 | /// NOTE: When this method is updated, getCharAndSizeSlow (above) should |
| 801 | /// be updated to match. |
| 802 | char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size, |
| 803 | const LangOptions &Features) { |
| 804 | // If we have a slash, look for an escaped newline. |
| 805 | if (Ptr[0] == '\\') { |
| 806 | ++Size; |
| 807 | ++Ptr; |
| 808 | Slash: |
| 809 | // Common case, backslash-char where the char is not whitespace. |
| 810 | if (!isWhitespace(Ptr[0])) return '\\'; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 811 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 812 | // See if we have optional whitespace characters followed by a newline. |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 813 | if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { |
| 814 | // Found backslash<whitespace><newline>. Parse the char after it. |
| 815 | Size += EscapedNewLineSize; |
| 816 | Ptr += EscapedNewLineSize; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 817 | |
Chris Lattner | 24f0e48 | 2009-04-18 22:05:41 +0000 | [diff] [blame] | 818 | // Use slow version to accumulate a correct size field. |
| 819 | return getCharAndSizeSlowNoWarn(Ptr, Size, Features); |
| 820 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 821 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 822 | // Otherwise, this is not an escaped newline, just return the slash. |
| 823 | return '\\'; |
| 824 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 825 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 826 | // If this is a trigraph, process it. |
| 827 | if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') { |
| 828 | // If this is actually a legal trigraph (not something like "??x"), return |
| 829 | // it. |
| 830 | if (char C = GetTrigraphCharForLetter(Ptr[2])) { |
| 831 | Ptr += 3; |
| 832 | Size += 3; |
| 833 | if (C == '\\') goto Slash; |
| 834 | return C; |
| 835 | } |
| 836 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 837 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 838 | // If this is neither, return a single character. |
| 839 | ++Size; |
| 840 | return *Ptr; |
| 841 | } |
| 842 | |
| 843 | //===----------------------------------------------------------------------===// |
| 844 | // Helper methods for lexing. |
| 845 | //===----------------------------------------------------------------------===// |
| 846 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 847 | /// \brief Routine that indiscriminately skips bytes in the source file. |
| 848 | void Lexer::SkipBytes(unsigned Bytes, bool StartOfLine) { |
| 849 | BufferPtr += Bytes; |
| 850 | if (BufferPtr > BufferEnd) |
| 851 | BufferPtr = BufferEnd; |
| 852 | IsAtStartOfLine = StartOfLine; |
| 853 | } |
| 854 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 855 | void Lexer::LexIdentifier(Token &Result, const char *CurPtr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 856 | // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$] |
| 857 | unsigned Size; |
| 858 | unsigned char C = *CurPtr++; |
Chris Lattner | cd991db | 2010-01-11 02:38:50 +0000 | [diff] [blame] | 859 | while (isIdentifierBody(C)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 860 | C = *CurPtr++; |
Chris Lattner | cd991db | 2010-01-11 02:38:50 +0000 | [diff] [blame] | 861 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 862 | --CurPtr; // Back up over the skipped character. |
| 863 | |
| 864 | // Fast path, no $,\,? in identifier found. '\' might be an escaped newline |
| 865 | // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN. |
| 866 | // FIXME: UCNs. |
Chris Lattner | cd991db | 2010-01-11 02:38:50 +0000 | [diff] [blame] | 867 | // |
| 868 | // TODO: Could merge these checks into a CharInfo flag to make the comparison |
| 869 | // cheaper |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 870 | if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) { |
| 871 | FinishIdentifier: |
| 872 | const char *IdStart = BufferPtr; |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 873 | FormTokenWithChars(Result, CurPtr, tok::identifier); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 874 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 875 | // If we are in raw mode, return this identifier raw. There is no need to |
| 876 | // look up identifier information or attempt to macro expand it. |
| 877 | if (LexingRawMode) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 878 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 879 | // Fill in Result.IdentifierInfo, looking up the identifier in the |
| 880 | // identifier table. |
Chris Lattner | d1186fa | 2009-01-21 07:45:14 +0000 | [diff] [blame] | 881 | IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 882 | |
Chris Lattner | 863c486 | 2009-01-23 18:35:48 +0000 | [diff] [blame] | 883 | // Change the kind of this identifier to the appropriate token kind, e.g. |
| 884 | // turning "for" into a keyword. |
| 885 | Result.setKind(II->getTokenID()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 886 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 887 | // Finally, now that we know we have an identifier, pass this off to the |
| 888 | // preprocessor, which may macro expand it or something. |
Chris Lattner | d1186fa | 2009-01-21 07:45:14 +0000 | [diff] [blame] | 889 | if (II->isHandleIdentifierCase()) |
Chris Lattner | 6a170eb | 2009-01-21 07:43:11 +0000 | [diff] [blame] | 890 | PP->HandleIdentifier(Result); |
| 891 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 892 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 893 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 894 | // Otherwise, $,\,? in identifier found. Enter slower path. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 895 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 896 | C = getCharAndSize(CurPtr, Size); |
| 897 | while (1) { |
| 898 | if (C == '$') { |
| 899 | // If we hit a $ and they are not supported in identifiers, we are done. |
| 900 | if (!Features.DollarIdents) goto FinishIdentifier; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 901 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 902 | // Otherwise, emit a diagnostic and continue. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 903 | if (!isLexingRawMode()) |
| 904 | Diag(CurPtr, diag::ext_dollar_in_identifier); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 905 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 906 | C = getCharAndSize(CurPtr, Size); |
| 907 | continue; |
| 908 | } else if (!isIdentifierBody(C)) { // FIXME: UCNs. |
| 909 | // Found end of identifier. |
| 910 | goto FinishIdentifier; |
| 911 | } |
| 912 | |
| 913 | // Otherwise, this character is good, consume it. |
| 914 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 915 | |
| 916 | C = getCharAndSize(CurPtr, Size); |
| 917 | while (isIdentifierBody(C)) { // FIXME: UCNs. |
| 918 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 919 | C = getCharAndSize(CurPtr, Size); |
| 920 | } |
| 921 | } |
| 922 | } |
| 923 | |
Douglas Gregor | a75ec43 | 2010-08-30 14:50:47 +0000 | [diff] [blame] | 924 | /// isHexaLiteral - Return true if Start points to a hex constant. |
Chris Lattner | 4a55100 | 2010-08-30 17:11:14 +0000 | [diff] [blame] | 925 | /// in microsoft mode (where this is supposed to be several different tokens). |
Chris Lattner | 6ab55eb | 2010-08-31 16:42:00 +0000 | [diff] [blame] | 926 | static bool isHexaLiteral(const char *Start, const LangOptions &Features) { |
| 927 | unsigned Size; |
| 928 | char C1 = Lexer::getCharAndSizeNoWarn(Start, Size, Features); |
| 929 | if (C1 != '0') |
| 930 | return false; |
| 931 | char C2 = Lexer::getCharAndSizeNoWarn(Start + Size, Size, Features); |
| 932 | return (C2 == 'x' || C2 == 'X'); |
Douglas Gregor | a75ec43 | 2010-08-30 14:50:47 +0000 | [diff] [blame] | 933 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 934 | |
Nate Begeman | 5253c7f | 2008-04-14 02:26:39 +0000 | [diff] [blame] | 935 | /// LexNumericConstant - Lex the remainder of a integer or floating point |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 936 | /// constant. From[-1] is the first character lexed. Return the end of the |
| 937 | /// constant. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 938 | void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 939 | unsigned Size; |
| 940 | char C = getCharAndSize(CurPtr, Size); |
| 941 | char PrevCh = 0; |
| 942 | while (isNumberBody(C)) { // FIXME: UCNs? |
| 943 | CurPtr = ConsumeChar(CurPtr, Size, Result); |
| 944 | PrevCh = C; |
| 945 | C = getCharAndSize(CurPtr, Size); |
| 946 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 947 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 948 | // If we fell out, check for a sign, due to 1e+12. If we have one, continue. |
Chris Lattner | b2f4a20 | 2010-08-30 17:09:08 +0000 | [diff] [blame] | 949 | if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e')) { |
| 950 | // If we are in Microsoft mode, don't continue if the constant is hex. |
| 951 | // For example, MSVC will accept the following as 3 tokens: 0x1234567e+1 |
Chris Lattner | 6ab55eb | 2010-08-31 16:42:00 +0000 | [diff] [blame] | 952 | if (!Features.Microsoft || !isHexaLiteral(BufferPtr, Features)) |
Chris Lattner | b2f4a20 | 2010-08-30 17:09:08 +0000 | [diff] [blame] | 953 | return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); |
| 954 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 955 | |
| 956 | // If we have a hex FP constant, continue. |
Sean Hunt | 8c72340 | 2010-01-10 23:37:56 +0000 | [diff] [blame] | 957 | if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') && |
Chris Lattner | b2f4a20 | 2010-08-30 17:09:08 +0000 | [diff] [blame] | 958 | !Features.CPlusPlus0x) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 959 | return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 960 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 961 | // Update the location of token as well as BufferPtr. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 962 | const char *TokStart = BufferPtr; |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 963 | FormTokenWithChars(Result, CurPtr, tok::numeric_constant); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 964 | Result.setLiteralData(TokStart); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 965 | } |
| 966 | |
| 967 | /// LexStringLiteral - Lex the remainder of a string literal, after having lexed |
| 968 | /// either " or L". |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 969 | void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 970 | const char *NulCharacter = 0; // Does this string contain the \0 character? |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 971 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 972 | char C = getAndAdvanceChar(CurPtr, Result); |
| 973 | while (C != '"') { |
Chris Lattner | 571339c | 2010-05-30 23:27:38 +0000 | [diff] [blame] | 974 | // Skip escaped characters. Escaped newlines will already be processed by |
| 975 | // getAndAdvanceChar. |
| 976 | if (C == '\\') |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 977 | C = getAndAdvanceChar(CurPtr, Result); |
Douglas Gregor | 33611e0 | 2010-05-30 22:59:50 +0000 | [diff] [blame] | 978 | |
Chris Lattner | 571339c | 2010-05-30 23:27:38 +0000 | [diff] [blame] | 979 | if (C == '\n' || C == '\r' || // Newline. |
Douglas Gregor | 33611e0 | 2010-05-30 22:59:50 +0000 | [diff] [blame] | 980 | (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 981 | if (C == 0 && PP && PP->isCodeCompletionFile(FileLoc)) |
| 982 | PP->CodeCompleteNaturalLanguage(); |
| 983 | else if (!isLexingRawMode() && !Features.AsmPreprocessor) |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 984 | Diag(BufferPtr, diag::err_unterminated_string); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 985 | FormTokenWithChars(Result, CurPtr-1, tok::unknown); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 986 | return; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 987 | } |
Chris Lattner | 571339c | 2010-05-30 23:27:38 +0000 | [diff] [blame] | 988 | |
| 989 | if (C == 0) |
| 990 | NulCharacter = CurPtr-1; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 991 | C = getAndAdvanceChar(CurPtr, Result); |
| 992 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 993 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 994 | // If a nul character existed in the string, warn about it. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 995 | if (NulCharacter && !isLexingRawMode()) |
| 996 | Diag(NulCharacter, diag::null_in_string); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 997 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 998 | // Update the location of the token as well as the BufferPtr instance var. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 999 | const char *TokStart = BufferPtr; |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1000 | FormTokenWithChars(Result, CurPtr, |
| 1001 | Wide ? tok::wide_string_literal : tok::string_literal); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 1002 | Result.setLiteralData(TokStart); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1003 | } |
| 1004 | |
| 1005 | /// LexAngledStringLiteral - Lex the remainder of an angled string literal, |
| 1006 | /// after having lexed the '<' character. This is used for #include filenames. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1007 | void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1008 | const char *NulCharacter = 0; // Does this string contain the \0 character? |
Chris Lattner | 9cb51ce | 2009-04-17 23:56:52 +0000 | [diff] [blame] | 1009 | const char *AfterLessPos = CurPtr; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1010 | char C = getAndAdvanceChar(CurPtr, Result); |
| 1011 | while (C != '>') { |
| 1012 | // Skip escaped characters. |
| 1013 | if (C == '\\') { |
| 1014 | // Skip the escaped character. |
| 1015 | C = getAndAdvanceChar(CurPtr, Result); |
| 1016 | } else if (C == '\n' || C == '\r' || // Newline. |
| 1017 | (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. |
Chris Lattner | 9cb51ce | 2009-04-17 23:56:52 +0000 | [diff] [blame] | 1018 | // If the filename is unterminated, then it must just be a lone < |
| 1019 | // character. Return this as such. |
| 1020 | FormTokenWithChars(Result, AfterLessPos, tok::less); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1021 | return; |
| 1022 | } else if (C == 0) { |
| 1023 | NulCharacter = CurPtr-1; |
| 1024 | } |
| 1025 | C = getAndAdvanceChar(CurPtr, Result); |
| 1026 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1027 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1028 | // If a nul character existed in the string, warn about it. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1029 | if (NulCharacter && !isLexingRawMode()) |
| 1030 | Diag(NulCharacter, diag::null_in_string); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1031 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1032 | // Update the location of token as well as BufferPtr. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 1033 | const char *TokStart = BufferPtr; |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1034 | FormTokenWithChars(Result, CurPtr, tok::angle_string_literal); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 1035 | Result.setLiteralData(TokStart); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
| 1038 | |
| 1039 | /// LexCharConstant - Lex the remainder of a character constant, after having |
| 1040 | /// lexed either ' or L'. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1041 | void Lexer::LexCharConstant(Token &Result, const char *CurPtr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1042 | const char *NulCharacter = 0; // Does this character contain the \0 character? |
| 1043 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1044 | char C = getAndAdvanceChar(CurPtr, Result); |
| 1045 | if (C == '\'') { |
Chris Lattner | 33ab3f6 | 2009-03-18 21:10:12 +0000 | [diff] [blame] | 1046 | if (!isLexingRawMode() && !Features.AsmPreprocessor) |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1047 | Diag(BufferPtr, diag::err_empty_character); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1048 | FormTokenWithChars(Result, CurPtr, tok::unknown); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1049 | return; |
Chris Lattner | d80f786 | 2010-07-07 23:24:27 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | while (C != '\'') { |
| 1053 | // Skip escaped characters. |
| 1054 | if (C == '\\') { |
| 1055 | // Skip the escaped character. |
| 1056 | // FIXME: UCN's |
| 1057 | C = getAndAdvanceChar(CurPtr, Result); |
| 1058 | } else if (C == '\n' || C == '\r' || // Newline. |
| 1059 | (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 1060 | if (C == 0 && PP && PP->isCodeCompletionFile(FileLoc)) |
| 1061 | PP->CodeCompleteNaturalLanguage(); |
| 1062 | else if (!isLexingRawMode() && !Features.AsmPreprocessor) |
Chris Lattner | d80f786 | 2010-07-07 23:24:27 +0000 | [diff] [blame] | 1063 | Diag(BufferPtr, diag::err_unterminated_char); |
| 1064 | FormTokenWithChars(Result, CurPtr-1, tok::unknown); |
| 1065 | return; |
| 1066 | } else if (C == 0) { |
| 1067 | NulCharacter = CurPtr-1; |
| 1068 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1069 | C = getAndAdvanceChar(CurPtr, Result); |
| 1070 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1071 | |
Chris Lattner | d80f786 | 2010-07-07 23:24:27 +0000 | [diff] [blame] | 1072 | // If a nul character existed in the character, warn about it. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1073 | if (NulCharacter && !isLexingRawMode()) |
| 1074 | Diag(NulCharacter, diag::null_in_char); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1075 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1076 | // Update the location of token as well as BufferPtr. |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 1077 | const char *TokStart = BufferPtr; |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1078 | FormTokenWithChars(Result, CurPtr, tok::char_constant); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 1079 | Result.setLiteralData(TokStart); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1080 | } |
| 1081 | |
| 1082 | /// SkipWhitespace - Efficiently skip over a series of whitespace characters. |
| 1083 | /// Update BufferPtr to point to the next non-whitespace character and return. |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1084 | /// |
| 1085 | /// This method forms a token and returns true if KeepWhitespaceMode is enabled. |
| 1086 | /// |
| 1087 | bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1088 | // Whitespace - Skip it, then return the token after the whitespace. |
| 1089 | unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently. |
| 1090 | while (1) { |
| 1091 | // Skip horizontal whitespace very aggressively. |
| 1092 | while (isHorizontalWhitespace(Char)) |
| 1093 | Char = *++CurPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1094 | |
Daniel Dunbar | ddd3e8b | 2008-11-25 00:20:22 +0000 | [diff] [blame] | 1095 | // Otherwise if we have something other than whitespace, we're done. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1096 | if (Char != '\n' && Char != '\r') |
| 1097 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1099 | if (ParsingPreprocessorDirective) { |
| 1100 | // End of preprocessor directive line, let LexTokenInternal handle this. |
| 1101 | BufferPtr = CurPtr; |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1102 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1103 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1104 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1105 | // ok, but handle newline. |
| 1106 | // The returned token is at the start of the line. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1107 | Result.setFlag(Token::StartOfLine); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1108 | // No leading whitespace seen so far. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1109 | Result.clearFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1110 | Char = *++CurPtr; |
| 1111 | } |
| 1112 | |
| 1113 | // If this isn't immediately after a newline, there is leading space. |
| 1114 | char PrevChar = CurPtr[-1]; |
| 1115 | if (PrevChar != '\n' && PrevChar != '\r') |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1116 | Result.setFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1117 | |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1118 | // If the client wants us to return whitespace, return it now. |
| 1119 | if (isKeepWhitespaceMode()) { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1120 | FormTokenWithChars(Result, CurPtr, tok::unknown); |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1121 | return true; |
| 1122 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1123 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1124 | BufferPtr = CurPtr; |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1125 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1126 | } |
| 1127 | |
| 1128 | // SkipBCPLComment - We have just read the // characters from input. Skip until |
| 1129 | // we find the newline character thats terminate the comment. Then update |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 1130 | /// BufferPtr and return. |
| 1131 | /// |
| 1132 | /// If we're in KeepCommentMode or any CommentHandler has inserted |
| 1133 | /// some tokens, this will store the first token and return true. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1134 | bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1135 | // If BCPL comments aren't explicitly enabled for this language, emit an |
| 1136 | // extension warning. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1137 | if (!Features.BCPLComment && !isLexingRawMode()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1138 | Diag(BufferPtr, diag::ext_bcpl_comment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1139 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1140 | // Mark them enabled so we only emit one warning for this translation |
| 1141 | // unit. |
| 1142 | Features.BCPLComment = true; |
| 1143 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1144 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1145 | // Scan over the body of the comment. The common case, when scanning, is that |
| 1146 | // the comment contains normal ascii characters with nothing interesting in |
| 1147 | // them. As such, optimize for this case with the inner loop. |
| 1148 | char C; |
| 1149 | do { |
| 1150 | C = *CurPtr; |
| 1151 | // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character. |
| 1152 | // If we find a \n character, scan backwards, checking to see if it's an |
| 1153 | // escaped newline, like we do for block comments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1154 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1155 | // Skip over characters in the fast loop. |
| 1156 | while (C != 0 && // Potentially EOF. |
| 1157 | C != '\\' && // Potentially escaped newline. |
| 1158 | C != '?' && // Potentially trigraph. |
| 1159 | C != '\n' && C != '\r') // Newline or DOS-style newline. |
| 1160 | C = *++CurPtr; |
| 1161 | |
| 1162 | // If this is a newline, we're done. |
| 1163 | if (C == '\n' || C == '\r') |
| 1164 | break; // Found the newline? Break out! |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1165 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1166 | // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to |
Chris Lattner | bc3e984 | 2008-12-12 07:34:39 +0000 | [diff] [blame] | 1167 | // properly decode the character. Read it in raw mode to avoid emitting |
| 1168 | // diagnostics about things like trigraphs. If we see an escaped newline, |
| 1169 | // we'll handle it below. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1170 | const char *OldPtr = CurPtr; |
Chris Lattner | bc3e984 | 2008-12-12 07:34:39 +0000 | [diff] [blame] | 1171 | bool OldRawMode = isLexingRawMode(); |
| 1172 | LexingRawMode = true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1173 | C = getAndAdvanceChar(CurPtr, Result); |
Chris Lattner | bc3e984 | 2008-12-12 07:34:39 +0000 | [diff] [blame] | 1174 | LexingRawMode = OldRawMode; |
Chris Lattner | ead616c | 2009-04-05 00:26:41 +0000 | [diff] [blame] | 1175 | |
| 1176 | // If the char that we finally got was a \n, then we must have had something |
| 1177 | // like \<newline><newline>. We don't want to have consumed the second |
| 1178 | // newline, we want CurPtr, to end up pointing to it down below. |
| 1179 | if (C == '\n' || C == '\r') { |
| 1180 | --CurPtr; |
| 1181 | C = 'x'; // doesn't matter what this is. |
| 1182 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1183 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1184 | // If we read multiple characters, and one of those characters was a \r or |
| 1185 | // \n, then we had an escaped newline within the comment. Emit diagnostic |
| 1186 | // unless the next line is also a // comment. |
| 1187 | if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') { |
| 1188 | for (; OldPtr != CurPtr; ++OldPtr) |
| 1189 | if (OldPtr[0] == '\n' || OldPtr[0] == '\r') { |
| 1190 | // Okay, we found a // comment that ends in a newline, if the next |
| 1191 | // line is also a // comment, but has spaces, don't emit a diagnostic. |
| 1192 | if (isspace(C)) { |
| 1193 | const char *ForwardPtr = CurPtr; |
| 1194 | while (isspace(*ForwardPtr)) // Skip whitespace. |
| 1195 | ++ForwardPtr; |
| 1196 | if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/') |
| 1197 | break; |
| 1198 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1199 | |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1200 | if (!isLexingRawMode()) |
| 1201 | Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1202 | break; |
| 1203 | } |
| 1204 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1205 | |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 1206 | if (CurPtr == BufferEnd+1) { |
| 1207 | if (PP && PP->isCodeCompletionFile(FileLoc)) |
| 1208 | PP->CodeCompleteNaturalLanguage(); |
| 1209 | |
| 1210 | --CurPtr; |
| 1211 | break; |
| 1212 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1213 | } while (C != '\n' && C != '\r'); |
| 1214 | |
Chris Lattner | 3d0ad58 | 2010-02-03 21:06:21 +0000 | [diff] [blame] | 1215 | // Found but did not consume the newline. Notify comment handlers about the |
| 1216 | // comment unless we're in a #if 0 block. |
| 1217 | if (PP && !isLexingRawMode() && |
| 1218 | PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr), |
| 1219 | getSourceLocation(CurPtr)))) { |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 1220 | BufferPtr = CurPtr; |
| 1221 | return true; // A token has to be returned. |
| 1222 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1223 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1224 | // If we are returning comments as tokens, return this comment as a token. |
Chris Lattner | fa95a01 | 2008-10-12 03:22:02 +0000 | [diff] [blame] | 1225 | if (inKeepCommentMode()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1226 | return SaveBCPLComment(Result, CurPtr); |
| 1227 | |
| 1228 | // If we are inside a preprocessor directive and we see the end of line, |
| 1229 | // return immediately, so that the lexer can return this as an EOM token. |
| 1230 | if (ParsingPreprocessorDirective || CurPtr == BufferEnd) { |
| 1231 | BufferPtr = CurPtr; |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1232 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1233 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1234 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1235 | // Otherwise, eat the \n character. We don't care if this is a \n\r or |
Chris Lattner | 7a4f004 | 2008-10-12 00:23:07 +0000 | [diff] [blame] | 1236 | // \r\n sequence. This is an efficiency hack (because we know the \n can't |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1237 | // contribute to another token), it isn't needed for correctness. Note that |
| 1238 | // this is ok even in KeepWhitespaceMode, because we would have returned the |
| 1239 | /// comment above in that mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1240 | ++CurPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1241 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1242 | // The next returned token is at the start of the line. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1243 | Result.setFlag(Token::StartOfLine); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1244 | // No leading whitespace seen so far. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1245 | Result.clearFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1246 | BufferPtr = CurPtr; |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1247 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1248 | } |
| 1249 | |
| 1250 | /// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in |
| 1251 | /// an appropriate way and return it. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1252 | bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1253 | // If we're not in a preprocessor directive, just return the // comment |
| 1254 | // directly. |
| 1255 | FormTokenWithChars(Result, CurPtr, tok::comment); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1256 | |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1257 | if (!ParsingPreprocessorDirective) |
| 1258 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1260 | // If this BCPL-style comment is in a macro definition, transmogrify it into |
| 1261 | // a C-style block comment. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 1262 | bool Invalid = false; |
| 1263 | std::string Spelling = PP->getSpelling(Result, &Invalid); |
| 1264 | if (Invalid) |
| 1265 | return true; |
| 1266 | |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1267 | assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?"); |
| 1268 | Spelling[1] = '*'; // Change prefix to "/*". |
| 1269 | Spelling += "*/"; // add suffix. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1270 | |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1271 | Result.setKind(tok::comment); |
Chris Lattner | 47246be | 2009-01-26 19:29:26 +0000 | [diff] [blame] | 1272 | PP->CreateString(&Spelling[0], Spelling.size(), Result, |
| 1273 | Result.getLocation()); |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1274 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | /// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline |
| 1278 | /// character (either \n or \r) is part of an escaped newline sequence. Issue a |
Chris Lattner | 47a2b40 | 2008-12-12 07:14:34 +0000 | [diff] [blame] | 1279 | /// diagnostic if so. We know that the newline is inside of a block comment. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1280 | static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1281 | Lexer *L) { |
| 1282 | assert(CurPtr[0] == '\n' || CurPtr[0] == '\r'); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1283 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1284 | // Back up off the newline. |
| 1285 | --CurPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1286 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1287 | // If this is a two-character newline sequence, skip the other character. |
| 1288 | if (CurPtr[0] == '\n' || CurPtr[0] == '\r') { |
| 1289 | // \n\n or \r\r -> not escaped newline. |
| 1290 | if (CurPtr[0] == CurPtr[1]) |
| 1291 | return false; |
| 1292 | // \n\r or \r\n -> skip the newline. |
| 1293 | --CurPtr; |
| 1294 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1295 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1296 | // If we have horizontal whitespace, skip over it. We allow whitespace |
| 1297 | // between the slash and newline. |
| 1298 | bool HasSpace = false; |
| 1299 | while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) { |
| 1300 | --CurPtr; |
| 1301 | HasSpace = true; |
| 1302 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1303 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1304 | // If we have a slash, we know this is an escaped newline. |
| 1305 | if (*CurPtr == '\\') { |
| 1306 | if (CurPtr[-1] != '*') return false; |
| 1307 | } else { |
| 1308 | // It isn't a slash, is it the ?? / trigraph? |
| 1309 | if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' || |
| 1310 | CurPtr[-3] != '*') |
| 1311 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1312 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1313 | // This is the trigraph ending the comment. Emit a stern warning! |
| 1314 | CurPtr -= 2; |
| 1315 | |
| 1316 | // If no trigraphs are enabled, warn that we ignored this trigraph and |
| 1317 | // ignore this * character. |
| 1318 | if (!L->getFeatures().Trigraphs) { |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1319 | if (!L->isLexingRawMode()) |
| 1320 | L->Diag(CurPtr, diag::trigraph_ignored_block_comment); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1321 | return false; |
| 1322 | } |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1323 | if (!L->isLexingRawMode()) |
| 1324 | L->Diag(CurPtr, diag::trigraph_ends_block_comment); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1325 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1326 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1327 | // Warn about having an escaped newline between the */ characters. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1328 | if (!L->isLexingRawMode()) |
| 1329 | L->Diag(CurPtr, diag::escaped_newline_block_comment_end); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1330 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1331 | // If there was space between the backslash and newline, warn about it. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1332 | if (HasSpace && !L->isLexingRawMode()) |
| 1333 | L->Diag(CurPtr, diag::backslash_newline_space); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1334 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1335 | return true; |
| 1336 | } |
| 1337 | |
| 1338 | #ifdef __SSE2__ |
| 1339 | #include <emmintrin.h> |
| 1340 | #elif __ALTIVEC__ |
| 1341 | #include <altivec.h> |
| 1342 | #undef bool |
| 1343 | #endif |
| 1344 | |
| 1345 | /// SkipBlockComment - We have just read the /* characters from input. Read |
| 1346 | /// until we find the */ characters that terminate the comment. Note that we |
| 1347 | /// don't bother decoding trigraphs or escaped newlines in block comments, |
| 1348 | /// because they cannot cause the comment to end. The only thing that can |
| 1349 | /// happen is the comment could end with an escaped newline between the */ end |
| 1350 | /// of comment. |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1351 | /// |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 1352 | /// If we're in KeepCommentMode or any CommentHandler has inserted |
| 1353 | /// some tokens, this will store the first token and return true. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1354 | bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1355 | // Scan one character past where we should, looking for a '/' character. Once |
| 1356 | // we find it, check to see if it was preceeded by a *. This common |
| 1357 | // optimization helps people who like to put a lot of * characters in their |
| 1358 | // comments. |
Chris Lattner | 8146b68 | 2007-07-21 23:43:37 +0000 | [diff] [blame] | 1359 | |
| 1360 | // The first character we get with newlines and trigraphs skipped to handle |
| 1361 | // the degenerate /*/ case below correctly if the * has an escaped newline |
| 1362 | // after it. |
| 1363 | unsigned CharSize; |
| 1364 | unsigned char C = getCharAndSize(CurPtr, CharSize); |
| 1365 | CurPtr += CharSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1366 | if (C == 0 && CurPtr == BufferEnd+1) { |
Chris Lattner | 150fcd5 | 2010-05-16 19:54:05 +0000 | [diff] [blame] | 1367 | if (!isLexingRawMode() && |
| 1368 | !PP->isCodeCompletionFile(FileLoc)) |
Chris Lattner | 0af5742 | 2008-10-12 01:31:51 +0000 | [diff] [blame] | 1369 | Diag(BufferPtr, diag::err_unterminated_block_comment); |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1370 | --CurPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1371 | |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1372 | // KeepWhitespaceMode should return this broken comment as a token. Since |
| 1373 | // it isn't a well formed comment, just return it as an 'unknown' token. |
| 1374 | if (isKeepWhitespaceMode()) { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1375 | FormTokenWithChars(Result, CurPtr, tok::unknown); |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1376 | return true; |
| 1377 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1378 | |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1379 | BufferPtr = CurPtr; |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1380 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1381 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1382 | |
Chris Lattner | 8146b68 | 2007-07-21 23:43:37 +0000 | [diff] [blame] | 1383 | // Check to see if the first character after the '/*' is another /. If so, |
| 1384 | // then this slash does not end the block comment, it is part of it. |
| 1385 | if (C == '/') |
| 1386 | C = *CurPtr++; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1387 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1388 | while (1) { |
| 1389 | // Skip over all non-interesting characters until we find end of buffer or a |
| 1390 | // (probably ending) '/' character. |
| 1391 | if (CurPtr + 24 < BufferEnd) { |
| 1392 | // While not aligned to a 16-byte boundary. |
| 1393 | while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0) |
| 1394 | C = *CurPtr++; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1395 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1396 | if (C == '/') goto FoundSlash; |
| 1397 | |
| 1398 | #ifdef __SSE2__ |
| 1399 | __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/', |
| 1400 | '/', '/', '/', '/', '/', '/', '/', '/'); |
| 1401 | while (CurPtr+16 <= BufferEnd && |
| 1402 | _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0) |
| 1403 | CurPtr += 16; |
| 1404 | #elif __ALTIVEC__ |
| 1405 | __vector unsigned char Slashes = { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1406 | '/', '/', '/', '/', '/', '/', '/', '/', |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1407 | '/', '/', '/', '/', '/', '/', '/', '/' |
| 1408 | }; |
| 1409 | while (CurPtr+16 <= BufferEnd && |
| 1410 | !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes)) |
| 1411 | CurPtr += 16; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | #else |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1413 | // Scan for '/' quickly. Many block comments are very large. |
| 1414 | while (CurPtr[0] != '/' && |
| 1415 | CurPtr[1] != '/' && |
| 1416 | CurPtr[2] != '/' && |
| 1417 | CurPtr[3] != '/' && |
| 1418 | CurPtr+4 < BufferEnd) { |
| 1419 | CurPtr += 4; |
| 1420 | } |
| 1421 | #endif |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1422 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1423 | // It has to be one of the bytes scanned, increment to it and read one. |
| 1424 | C = *CurPtr++; |
| 1425 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1426 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1427 | // Loop to scan the remainder. |
| 1428 | while (C != '/' && C != '\0') |
| 1429 | C = *CurPtr++; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1430 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1431 | FoundSlash: |
| 1432 | if (C == '/') { |
| 1433 | if (CurPtr[-2] == '*') // We found the final */. We're done! |
| 1434 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1435 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1436 | if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) { |
| 1437 | if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) { |
| 1438 | // We found the final */, though it had an escaped newline between the |
| 1439 | // * and /. We're done! |
| 1440 | break; |
| 1441 | } |
| 1442 | } |
| 1443 | if (CurPtr[0] == '*' && CurPtr[1] != '/') { |
| 1444 | // If this is a /* inside of the comment, emit a warning. Don't do this |
| 1445 | // if this is a /*/, which will end the comment. This misses cases with |
| 1446 | // embedded escaped newlines, but oh well. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1447 | if (!isLexingRawMode()) |
| 1448 | Diag(CurPtr-1, diag::warn_nested_block_comment); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1449 | } |
| 1450 | } else if (C == 0 && CurPtr == BufferEnd+1) { |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 1451 | if (PP && PP->isCodeCompletionFile(FileLoc)) |
| 1452 | PP->CodeCompleteNaturalLanguage(); |
| 1453 | else if (!isLexingRawMode()) |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1454 | Diag(BufferPtr, diag::err_unterminated_block_comment); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1455 | // Note: the user probably forgot a */. We could continue immediately |
| 1456 | // after the /*, but this would involve lexing a lot of what really is the |
| 1457 | // comment, which surely would confuse the parser. |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1458 | --CurPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1459 | |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1460 | // KeepWhitespaceMode should return this broken comment as a token. Since |
| 1461 | // it isn't a well formed comment, just return it as an 'unknown' token. |
| 1462 | if (isKeepWhitespaceMode()) { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1463 | FormTokenWithChars(Result, CurPtr, tok::unknown); |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1464 | return true; |
| 1465 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1466 | |
Chris Lattner | 31f0eca | 2008-10-12 04:19:49 +0000 | [diff] [blame] | 1467 | BufferPtr = CurPtr; |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1468 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1469 | } |
| 1470 | C = *CurPtr++; |
| 1471 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1472 | |
Chris Lattner | 3d0ad58 | 2010-02-03 21:06:21 +0000 | [diff] [blame] | 1473 | // Notify comment handlers about the comment unless we're in a #if 0 block. |
| 1474 | if (PP && !isLexingRawMode() && |
| 1475 | PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr), |
| 1476 | getSourceLocation(CurPtr)))) { |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 1477 | BufferPtr = CurPtr; |
| 1478 | return true; // A token has to be returned. |
| 1479 | } |
Douglas Gregor | 2e22253 | 2009-07-02 17:08:52 +0000 | [diff] [blame] | 1480 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1481 | // If we are returning comments as tokens, return this comment as a token. |
Chris Lattner | fa95a01 | 2008-10-12 03:22:02 +0000 | [diff] [blame] | 1482 | if (inKeepCommentMode()) { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1483 | FormTokenWithChars(Result, CurPtr, tok::comment); |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1484 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | // It is common for the tokens immediately after a /**/ comment to be |
| 1488 | // whitespace. Instead of going through the big switch, handle it |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1489 | // efficiently now. This is safe even in KeepWhitespaceMode because we would |
| 1490 | // have already returned above with the comment as a token. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1491 | if (isHorizontalWhitespace(*CurPtr)) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1492 | Result.setFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1493 | SkipWhitespace(Result, CurPtr+1); |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1494 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | // Otherwise, just return so that the next character will be lexed as a token. |
| 1498 | BufferPtr = CurPtr; |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1499 | Result.setFlag(Token::LeadingSpace); |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 1500 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | //===----------------------------------------------------------------------===// |
| 1504 | // Primary Lexing Entry Points |
| 1505 | //===----------------------------------------------------------------------===// |
| 1506 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1507 | /// ReadToEndOfLine - Read the rest of the current preprocessor line as an |
| 1508 | /// uninterpreted string. This switches the lexer out of directive mode. |
| 1509 | std::string Lexer::ReadToEndOfLine() { |
| 1510 | assert(ParsingPreprocessorDirective && ParsingFilename == false && |
| 1511 | "Must be in a preprocessing directive!"); |
| 1512 | std::string Result; |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1513 | Token Tmp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1514 | |
| 1515 | // CurPtr - Cache BufferPtr in an automatic variable. |
| 1516 | const char *CurPtr = BufferPtr; |
| 1517 | while (1) { |
| 1518 | char Char = getAndAdvanceChar(CurPtr, Tmp); |
| 1519 | switch (Char) { |
| 1520 | default: |
| 1521 | Result += Char; |
| 1522 | break; |
| 1523 | case 0: // Null. |
| 1524 | // Found end of file? |
| 1525 | if (CurPtr-1 != BufferEnd) { |
| 1526 | // Nope, normal character, continue. |
| 1527 | Result += Char; |
| 1528 | break; |
| 1529 | } |
| 1530 | // FALL THROUGH. |
| 1531 | case '\r': |
| 1532 | case '\n': |
| 1533 | // Okay, we found the end of the line. First, back up past the \0, \r, \n. |
| 1534 | assert(CurPtr[-1] == Char && "Trigraphs for newline?"); |
| 1535 | BufferPtr = CurPtr-1; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1536 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1537 | // Next, lex the character, which should handle the EOM transition. |
| 1538 | Lex(Tmp); |
Douglas Gregor | 55817af | 2010-08-25 17:04:25 +0000 | [diff] [blame] | 1539 | if (Tmp.is(tok::code_completion)) { |
| 1540 | if (PP && PP->getCodeCompletionHandler()) |
| 1541 | PP->getCodeCompletionHandler()->CodeCompleteNaturalLanguage(); |
| 1542 | Lex(Tmp); |
| 1543 | } |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 1544 | assert(Tmp.is(tok::eom) && "Unexpected token!"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1545 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1546 | // Finally, we're done, return the string we found. |
| 1547 | return Result; |
| 1548 | } |
| 1549 | } |
| 1550 | } |
| 1551 | |
| 1552 | /// LexEndOfFile - CurPtr points to the end of this file. Handle this |
| 1553 | /// condition, reporting diagnostics and handling other edge cases as required. |
| 1554 | /// This returns true if Result contains a token, false if PP.Lex should be |
| 1555 | /// called again. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1556 | bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) { |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 1557 | // Check if we are performing code completion. |
| 1558 | if (PP && PP->isCodeCompletionFile(FileLoc)) { |
| 1559 | // We're at the end of the file, but we've been asked to consider the |
| 1560 | // end of the file to be a code-completion token. Return the |
| 1561 | // code-completion token. |
| 1562 | Result.startToken(); |
| 1563 | FormTokenWithChars(Result, CurPtr, tok::code_completion); |
| 1564 | |
| 1565 | // Only do the eof -> code_completion translation once. |
| 1566 | PP->SetCodeCompletionPoint(0, 0, 0); |
| 1567 | |
| 1568 | // Silence any diagnostics that occur once we hit the code-completion point. |
| 1569 | PP->getDiagnostics().setSuppressAllDiagnostics(true); |
| 1570 | return true; |
| 1571 | } |
| 1572 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1573 | // If we hit the end of the file while parsing a preprocessor directive, |
| 1574 | // end the preprocessor directive first. The next token returned will |
| 1575 | // then be the end of file. |
| 1576 | if (ParsingPreprocessorDirective) { |
| 1577 | // Done parsing the "line". |
| 1578 | ParsingPreprocessorDirective = false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1579 | // Update the location of token as well as BufferPtr. |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1580 | FormTokenWithChars(Result, CurPtr, tok::eom); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1581 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1582 | // Restore comment saving mode, in case it was disabled for directive. |
Chris Lattner | f744d13 | 2008-10-12 03:27:19 +0000 | [diff] [blame] | 1583 | SetCommentRetentionState(PP->getCommentRetentionState()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1584 | return true; // Have a token. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1585 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1586 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1587 | // If we are in raw mode, return this event as an EOF token. Let the caller |
| 1588 | // that put us in raw mode handle the event. |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1589 | if (isLexingRawMode()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1590 | Result.startToken(); |
| 1591 | BufferPtr = BufferEnd; |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1592 | FormTokenWithChars(Result, BufferEnd, tok::eof); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1593 | return true; |
| 1594 | } |
Douglas Gregor | 86d9a52 | 2009-09-21 16:56:56 +0000 | [diff] [blame] | 1595 | |
Douglas Gregor | f44e854 | 2010-08-24 19:08:16 +0000 | [diff] [blame] | 1596 | // Issue diagnostics for unterminated #if and missing newline. |
| 1597 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1598 | // If we are in a #if directive, emit an error. |
| 1599 | while (!ConditionalStack.empty()) { |
Douglas Gregor | 2d474ba | 2010-08-12 17:04:55 +0000 | [diff] [blame] | 1600 | if (!PP->isCodeCompletionFile(FileLoc)) |
| 1601 | PP->Diag(ConditionalStack.back().IfLoc, |
| 1602 | diag::err_pp_unterminated_conditional); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1603 | ConditionalStack.pop_back(); |
| 1604 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1605 | |
Chris Lattner | b25e5d7 | 2008-04-12 05:54:25 +0000 | [diff] [blame] | 1606 | // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue |
| 1607 | // a pedwarn. |
| 1608 | if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')) |
Mike Stump | 20d0ee5 | 2009-04-02 02:29:42 +0000 | [diff] [blame] | 1609 | Diag(BufferEnd, diag::ext_no_newline_eof) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 1610 | << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1611 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1612 | BufferPtr = CurPtr; |
| 1613 | |
| 1614 | // Finally, let the preprocessor handle this. |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 1615 | return PP->HandleEndOfFile(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | /// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from |
| 1619 | /// the specified lexer will return a tok::l_paren token, 0 if it is something |
| 1620 | /// else and 2 if there are no more tokens in the buffer controlled by the |
| 1621 | /// lexer. |
| 1622 | unsigned Lexer::isNextPPTokenLParen() { |
| 1623 | assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1624 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1625 | // Switch to 'skipping' mode. This will ensure that we can lex a token |
| 1626 | // without emitting diagnostics, disables macro expansion, and will cause EOF |
| 1627 | // to return an EOF token instead of popping the include stack. |
| 1628 | LexingRawMode = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1629 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1630 | // Save state that can be changed while lexing so that we can restore it. |
| 1631 | const char *TmpBufferPtr = BufferPtr; |
Chris Lattner | a864cf7 | 2009-04-24 07:15:46 +0000 | [diff] [blame] | 1632 | bool inPPDirectiveMode = ParsingPreprocessorDirective; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1633 | |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1634 | Token Tok; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1635 | Tok.startToken(); |
| 1636 | LexTokenInternal(Tok); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1638 | // Restore state that may have changed. |
| 1639 | BufferPtr = TmpBufferPtr; |
Chris Lattner | a864cf7 | 2009-04-24 07:15:46 +0000 | [diff] [blame] | 1640 | ParsingPreprocessorDirective = inPPDirectiveMode; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1641 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1642 | // Restore the lexer back to non-skipping mode. |
| 1643 | LexingRawMode = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1644 | |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 1645 | if (Tok.is(tok::eof)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1646 | return 2; |
Chris Lattner | 22f6bbc | 2007-10-09 18:02:16 +0000 | [diff] [blame] | 1647 | return Tok.is(tok::l_paren); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 1650 | /// FindConflictEnd - Find the end of a version control conflict marker. |
| 1651 | static const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd) { |
| 1652 | llvm::StringRef RestOfBuffer(CurPtr+7, BufferEnd-CurPtr-7); |
| 1653 | size_t Pos = RestOfBuffer.find(">>>>>>>"); |
| 1654 | while (Pos != llvm::StringRef::npos) { |
| 1655 | // Must occur at start of line. |
| 1656 | if (RestOfBuffer[Pos-1] != '\r' && |
| 1657 | RestOfBuffer[Pos-1] != '\n') { |
| 1658 | RestOfBuffer = RestOfBuffer.substr(Pos+7); |
Chris Lattner | 3d48899 | 2010-05-17 20:27:25 +0000 | [diff] [blame] | 1659 | Pos = RestOfBuffer.find(">>>>>>>"); |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 1660 | continue; |
| 1661 | } |
| 1662 | return RestOfBuffer.data()+Pos; |
| 1663 | } |
| 1664 | return 0; |
| 1665 | } |
| 1666 | |
| 1667 | /// IsStartOfConflictMarker - If the specified pointer is the start of a version |
| 1668 | /// control conflict marker like '<<<<<<<', recognize it as such, emit an error |
| 1669 | /// and recover nicely. This returns true if it is a conflict marker and false |
| 1670 | /// if not. |
| 1671 | bool Lexer::IsStartOfConflictMarker(const char *CurPtr) { |
| 1672 | // Only a conflict marker if it starts at the beginning of a line. |
| 1673 | if (CurPtr != BufferStart && |
| 1674 | CurPtr[-1] != '\n' && CurPtr[-1] != '\r') |
| 1675 | return false; |
| 1676 | |
| 1677 | // Check to see if we have <<<<<<<. |
| 1678 | if (BufferEnd-CurPtr < 8 || |
| 1679 | llvm::StringRef(CurPtr, 7) != "<<<<<<<") |
| 1680 | return false; |
| 1681 | |
| 1682 | // If we have a situation where we don't care about conflict markers, ignore |
| 1683 | // it. |
| 1684 | if (IsInConflictMarker || isLexingRawMode()) |
| 1685 | return false; |
| 1686 | |
| 1687 | // Check to see if there is a >>>>>>> somewhere in the buffer at the start of |
| 1688 | // a line to terminate this conflict marker. |
Chris Lattner | 3d48899 | 2010-05-17 20:27:25 +0000 | [diff] [blame] | 1689 | if (FindConflictEnd(CurPtr, BufferEnd)) { |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 1690 | // We found a match. We are really in a conflict marker. |
| 1691 | // Diagnose this, and ignore to the end of line. |
| 1692 | Diag(CurPtr, diag::err_conflict_marker); |
| 1693 | IsInConflictMarker = true; |
| 1694 | |
| 1695 | // Skip ahead to the end of line. We know this exists because the |
| 1696 | // end-of-conflict marker starts with \r or \n. |
| 1697 | while (*CurPtr != '\r' && *CurPtr != '\n') { |
| 1698 | assert(CurPtr != BufferEnd && "Didn't find end of line"); |
| 1699 | ++CurPtr; |
| 1700 | } |
| 1701 | BufferPtr = CurPtr; |
| 1702 | return true; |
| 1703 | } |
| 1704 | |
| 1705 | // No end of conflict marker found. |
| 1706 | return false; |
| 1707 | } |
| 1708 | |
| 1709 | |
| 1710 | /// HandleEndOfConflictMarker - If this is a '=======' or '|||||||' or '>>>>>>>' |
| 1711 | /// marker, then it is the end of a conflict marker. Handle it by ignoring up |
| 1712 | /// until the end of the line. This returns true if it is a conflict marker and |
| 1713 | /// false if not. |
| 1714 | bool Lexer::HandleEndOfConflictMarker(const char *CurPtr) { |
| 1715 | // Only a conflict marker if it starts at the beginning of a line. |
| 1716 | if (CurPtr != BufferStart && |
| 1717 | CurPtr[-1] != '\n' && CurPtr[-1] != '\r') |
| 1718 | return false; |
| 1719 | |
| 1720 | // If we have a situation where we don't care about conflict markers, ignore |
| 1721 | // it. |
| 1722 | if (!IsInConflictMarker || isLexingRawMode()) |
| 1723 | return false; |
| 1724 | |
| 1725 | // Check to see if we have the marker (7 characters in a row). |
| 1726 | for (unsigned i = 1; i != 7; ++i) |
| 1727 | if (CurPtr[i] != CurPtr[0]) |
| 1728 | return false; |
| 1729 | |
| 1730 | // If we do have it, search for the end of the conflict marker. This could |
| 1731 | // fail if it got skipped with a '#if 0' or something. Note that CurPtr might |
| 1732 | // be the end of conflict marker. |
| 1733 | if (const char *End = FindConflictEnd(CurPtr, BufferEnd)) { |
| 1734 | CurPtr = End; |
| 1735 | |
| 1736 | // Skip ahead to the end of line. |
| 1737 | while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n') |
| 1738 | ++CurPtr; |
| 1739 | |
| 1740 | BufferPtr = CurPtr; |
| 1741 | |
| 1742 | // No longer in the conflict marker. |
| 1743 | IsInConflictMarker = false; |
| 1744 | return true; |
| 1745 | } |
| 1746 | |
| 1747 | return false; |
| 1748 | } |
| 1749 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1750 | |
| 1751 | /// LexTokenInternal - This implements a simple C family lexer. It is an |
| 1752 | /// extremely performance critical piece of code. This assumes that the buffer |
Chris Lattner | efb173d | 2009-07-07 05:05:42 +0000 | [diff] [blame] | 1753 | /// has a null character at the end of the file. This returns a preprocessing |
| 1754 | /// token, not a normal token, as such, it is an internal interface. It assumes |
| 1755 | /// that the Flags of result have been cleared before calling this. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1756 | void Lexer::LexTokenInternal(Token &Result) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1757 | LexNextToken: |
| 1758 | // New token, can't need cleaning yet. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1759 | Result.clearFlag(Token::NeedsCleaning); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1760 | Result.setIdentifierInfo(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1761 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1762 | // CurPtr - Cache BufferPtr in an automatic variable. |
| 1763 | const char *CurPtr = BufferPtr; |
| 1764 | |
| 1765 | // Small amounts of horizontal whitespace is very common between tokens. |
| 1766 | if ((*CurPtr == ' ') || (*CurPtr == '\t')) { |
| 1767 | ++CurPtr; |
| 1768 | while ((*CurPtr == ' ') || (*CurPtr == '\t')) |
| 1769 | ++CurPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1770 | |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1771 | // If we are keeping whitespace and other tokens, just return what we just |
| 1772 | // skipped. The next lexer invocation will return the token after the |
| 1773 | // whitespace. |
| 1774 | if (isKeepWhitespaceMode()) { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1775 | FormTokenWithChars(Result, CurPtr, tok::unknown); |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1776 | return; |
| 1777 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1778 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1779 | BufferPtr = CurPtr; |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1780 | Result.setFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1781 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1782 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1783 | unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1785 | // Read a character, advancing over it. |
| 1786 | char Char = getAndAdvanceChar(CurPtr, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1787 | tok::TokenKind Kind; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1788 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1789 | switch (Char) { |
| 1790 | case 0: // Null. |
| 1791 | // Found end of file? |
| 1792 | if (CurPtr-1 == BufferEnd) { |
| 1793 | // Read the PP instance variable into an automatic variable, because |
| 1794 | // LexEndOfFile will often delete 'this'. |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 1795 | Preprocessor *PPCache = PP; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1796 | if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file. |
| 1797 | return; // Got a token to return. |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 1798 | assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); |
| 1799 | return PPCache->Lex(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1800 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1801 | |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1802 | if (!isLexingRawMode()) |
| 1803 | Diag(CurPtr-1, diag::null_in_file); |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1804 | Result.setFlag(Token::LeadingSpace); |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1805 | if (SkipWhitespace(Result, CurPtr)) |
| 1806 | return; // KeepWhitespaceMode |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1807 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1808 | goto LexNextToken; // GCC isn't tail call eliminating. |
Chris Lattner | a2bf105 | 2009-12-17 05:29:40 +0000 | [diff] [blame] | 1809 | |
| 1810 | case 26: // DOS & CP/M EOF: "^Z". |
| 1811 | // If we're in Microsoft extensions mode, treat this as end of file. |
| 1812 | if (Features.Microsoft) { |
| 1813 | // Read the PP instance variable into an automatic variable, because |
| 1814 | // LexEndOfFile will often delete 'this'. |
| 1815 | Preprocessor *PPCache = PP; |
| 1816 | if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file. |
| 1817 | return; // Got a token to return. |
| 1818 | assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); |
| 1819 | return PPCache->Lex(Result); |
| 1820 | } |
| 1821 | // If Microsoft extensions are disabled, this is just random garbage. |
| 1822 | Kind = tok::unknown; |
| 1823 | break; |
| 1824 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1825 | case '\n': |
| 1826 | case '\r': |
| 1827 | // If we are inside a preprocessor directive and we see the end of line, |
| 1828 | // we know we are done with the directive, so return an EOM token. |
| 1829 | if (ParsingPreprocessorDirective) { |
| 1830 | // Done parsing the "line". |
| 1831 | ParsingPreprocessorDirective = false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1832 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1833 | // Restore comment saving mode, in case it was disabled for directive. |
Chris Lattner | f744d13 | 2008-10-12 03:27:19 +0000 | [diff] [blame] | 1834 | SetCommentRetentionState(PP->getCommentRetentionState()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1835 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1836 | // Since we consumed a newline, we are back at the start of a line. |
| 1837 | IsAtStartOfLine = true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1839 | Kind = tok::eom; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1840 | break; |
| 1841 | } |
| 1842 | // The returned token is at the start of the line. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1843 | Result.setFlag(Token::StartOfLine); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1844 | // No leading whitespace seen so far. |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1845 | Result.clearFlag(Token::LeadingSpace); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1846 | |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1847 | if (SkipWhitespace(Result, CurPtr)) |
| 1848 | return; // KeepWhitespaceMode |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1849 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 1850 | case ' ': |
| 1851 | case '\t': |
| 1852 | case '\f': |
| 1853 | case '\v': |
Chris Lattner | 8133cfc | 2007-07-22 06:29:05 +0000 | [diff] [blame] | 1854 | SkipHorizontalWhitespace: |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 1855 | Result.setFlag(Token::LeadingSpace); |
Chris Lattner | d88dc48 | 2008-10-12 04:05:48 +0000 | [diff] [blame] | 1856 | if (SkipWhitespace(Result, CurPtr)) |
| 1857 | return; // KeepWhitespaceMode |
Chris Lattner | 8133cfc | 2007-07-22 06:29:05 +0000 | [diff] [blame] | 1858 | |
| 1859 | SkipIgnoredUnits: |
| 1860 | CurPtr = BufferPtr; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1861 | |
Chris Lattner | 8133cfc | 2007-07-22 06:29:05 +0000 | [diff] [blame] | 1862 | // If the next token is obviously a // or /* */ comment, skip it efficiently |
| 1863 | // too (without going through the big switch stmt). |
Chris Lattner | 8402c73 | 2009-01-16 22:39:25 +0000 | [diff] [blame] | 1864 | if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() && |
| 1865 | Features.BCPLComment) { |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 1866 | if (SkipBCPLComment(Result, CurPtr+2)) |
| 1867 | return; // There is a token to return. |
Chris Lattner | 8133cfc | 2007-07-22 06:29:05 +0000 | [diff] [blame] | 1868 | goto SkipIgnoredUnits; |
Chris Lattner | fa95a01 | 2008-10-12 03:22:02 +0000 | [diff] [blame] | 1869 | } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) { |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 1870 | if (SkipBlockComment(Result, CurPtr+2)) |
| 1871 | return; // There is a token to return. |
Chris Lattner | 8133cfc | 2007-07-22 06:29:05 +0000 | [diff] [blame] | 1872 | goto SkipIgnoredUnits; |
| 1873 | } else if (isHorizontalWhitespace(*CurPtr)) { |
| 1874 | goto SkipHorizontalWhitespace; |
| 1875 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1876 | goto LexNextToken; // GCC isn't tail call eliminating. |
Chris Lattner | a2bf105 | 2009-12-17 05:29:40 +0000 | [diff] [blame] | 1877 | |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 1878 | // C99 6.4.4.1: Integer Constants. |
| 1879 | // C99 6.4.4.2: Floating Constants. |
| 1880 | case '0': case '1': case '2': case '3': case '4': |
| 1881 | case '5': case '6': case '7': case '8': case '9': |
| 1882 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 1883 | MIOpt.ReadToken(); |
| 1884 | return LexNumericConstant(Result, CurPtr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1885 | |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 1886 | case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz"). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1887 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 1888 | MIOpt.ReadToken(); |
| 1889 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1890 | |
| 1891 | // Wide string literal. |
| 1892 | if (Char == '"') |
| 1893 | return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), |
| 1894 | true); |
| 1895 | |
| 1896 | // Wide character constant. |
| 1897 | if (Char == '\'') |
| 1898 | return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); |
| 1899 | // FALL THROUGH, treating L like the start of an identifier. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1900 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1901 | // C99 6.4.2: Identifiers. |
| 1902 | case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': |
| 1903 | case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N': |
| 1904 | case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U': |
| 1905 | case 'V': case 'W': case 'X': case 'Y': case 'Z': |
| 1906 | case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': |
| 1907 | case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': |
| 1908 | case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u': |
| 1909 | case 'v': case 'w': case 'x': case 'y': case 'z': |
| 1910 | case '_': |
| 1911 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 1912 | MIOpt.ReadToken(); |
| 1913 | return LexIdentifier(Result, CurPtr); |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 1914 | |
| 1915 | case '$': // $ in identifiers. |
| 1916 | if (Features.DollarIdents) { |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 1917 | if (!isLexingRawMode()) |
| 1918 | Diag(CurPtr-1, diag::ext_dollar_in_identifier); |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 1919 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 1920 | MIOpt.ReadToken(); |
| 1921 | return LexIdentifier(Result, CurPtr); |
| 1922 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1924 | Kind = tok::unknown; |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 1925 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1926 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1927 | // C99 6.4.4: Character Constants. |
| 1928 | case '\'': |
| 1929 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 1930 | MIOpt.ReadToken(); |
| 1931 | return LexCharConstant(Result, CurPtr); |
| 1932 | |
| 1933 | // C99 6.4.5: String Literals. |
| 1934 | case '"': |
| 1935 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 1936 | MIOpt.ReadToken(); |
| 1937 | return LexStringLiteral(Result, CurPtr, false); |
| 1938 | |
| 1939 | // C99 6.4.6: Punctuators. |
| 1940 | case '?': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1941 | Kind = tok::question; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1942 | break; |
| 1943 | case '[': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1944 | Kind = tok::l_square; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1945 | break; |
| 1946 | case ']': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1947 | Kind = tok::r_square; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1948 | break; |
| 1949 | case '(': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1950 | Kind = tok::l_paren; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1951 | break; |
| 1952 | case ')': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1953 | Kind = tok::r_paren; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1954 | break; |
| 1955 | case '{': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1956 | Kind = tok::l_brace; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1957 | break; |
| 1958 | case '}': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1959 | Kind = tok::r_brace; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1960 | break; |
| 1961 | case '.': |
| 1962 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1963 | if (Char >= '0' && Char <= '9') { |
| 1964 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 1965 | MIOpt.ReadToken(); |
| 1966 | |
| 1967 | return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); |
| 1968 | } else if (Features.CPlusPlus && Char == '*') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1969 | Kind = tok::periodstar; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1970 | CurPtr += SizeTmp; |
| 1971 | } else if (Char == '.' && |
| 1972 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1973 | Kind = tok::ellipsis; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1974 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 1975 | SizeTmp2, Result); |
| 1976 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1977 | Kind = tok::period; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1978 | } |
| 1979 | break; |
| 1980 | case '&': |
| 1981 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 1982 | if (Char == '&') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1983 | Kind = tok::ampamp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1984 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1985 | } else if (Char == '=') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1986 | Kind = tok::ampequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1987 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1988 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1989 | Kind = tok::amp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1990 | } |
| 1991 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1992 | case '*': |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1993 | if (getCharAndSize(CurPtr, SizeTmp) == '=') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1994 | Kind = tok::starequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1995 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 1996 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 1997 | Kind = tok::star; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1998 | } |
| 1999 | break; |
| 2000 | case '+': |
| 2001 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2002 | if (Char == '+') { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2003 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2004 | Kind = tok::plusplus; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2005 | } else if (Char == '=') { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2006 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2007 | Kind = tok::plusequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2008 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2009 | Kind = tok::plus; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2010 | } |
| 2011 | break; |
| 2012 | case '-': |
| 2013 | Char = getCharAndSize(CurPtr, SizeTmp); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2014 | if (Char == '-') { // -- |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2015 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2016 | Kind = tok::minusminus; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2017 | } else if (Char == '>' && Features.CPlusPlus && |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2018 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->* |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2019 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 2020 | SizeTmp2, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2021 | Kind = tok::arrowstar; |
| 2022 | } else if (Char == '>') { // -> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2023 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2024 | Kind = tok::arrow; |
| 2025 | } else if (Char == '=') { // -= |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2026 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2027 | Kind = tok::minusequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2028 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2029 | Kind = tok::minus; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2030 | } |
| 2031 | break; |
| 2032 | case '~': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2033 | Kind = tok::tilde; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2034 | break; |
| 2035 | case '!': |
| 2036 | if (getCharAndSize(CurPtr, SizeTmp) == '=') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2037 | Kind = tok::exclaimequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2038 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2039 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2040 | Kind = tok::exclaim; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2041 | } |
| 2042 | break; |
| 2043 | case '/': |
| 2044 | // 6.4.9: Comments |
| 2045 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2046 | if (Char == '/') { // BCPL comment. |
Chris Lattner | 8402c73 | 2009-01-16 22:39:25 +0000 | [diff] [blame] | 2047 | // Even if BCPL comments are disabled (e.g. in C89 mode), we generally |
| 2048 | // want to lex this as a comment. There is one problem with this though, |
| 2049 | // that in one particular corner case, this can change the behavior of the |
| 2050 | // resultant program. For example, In "foo //**/ bar", C89 would lex |
| 2051 | // this as "foo / bar" and langauges with BCPL comments would lex it as |
| 2052 | // "foo". Check to see if the character after the second slash is a '*'. |
| 2053 | // If so, we will lex that as a "/" instead of the start of a comment. |
| 2054 | if (Features.BCPLComment || |
| 2055 | getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') { |
| 2056 | if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 2057 | return; // There is a token to return. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2058 | |
Chris Lattner | 8402c73 | 2009-01-16 22:39:25 +0000 | [diff] [blame] | 2059 | // It is common for the tokens immediately after a // comment to be |
| 2060 | // whitespace (indentation for the next line). Instead of going through |
| 2061 | // the big switch, handle it efficiently now. |
| 2062 | goto SkipIgnoredUnits; |
| 2063 | } |
| 2064 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2065 | |
Chris Lattner | 8402c73 | 2009-01-16 22:39:25 +0000 | [diff] [blame] | 2066 | if (Char == '*') { // /**/ comment. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2067 | if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) |
Chris Lattner | 046c227 | 2010-01-18 22:35:47 +0000 | [diff] [blame] | 2068 | return; // There is a token to return. |
Chris Lattner | 2d38189 | 2008-10-12 04:15:42 +0000 | [diff] [blame] | 2069 | goto LexNextToken; // GCC isn't tail call eliminating. |
Chris Lattner | 8402c73 | 2009-01-16 22:39:25 +0000 | [diff] [blame] | 2070 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2071 | |
Chris Lattner | 8402c73 | 2009-01-16 22:39:25 +0000 | [diff] [blame] | 2072 | if (Char == '=') { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2073 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2074 | Kind = tok::slashequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2075 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2076 | Kind = tok::slash; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2077 | } |
| 2078 | break; |
| 2079 | case '%': |
| 2080 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2081 | if (Char == '=') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2082 | Kind = tok::percentequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2083 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2084 | } else if (Features.Digraphs && Char == '>') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2085 | Kind = tok::r_brace; // '%>' -> '}' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2086 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2087 | } else if (Features.Digraphs && Char == ':') { |
| 2088 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2089 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2090 | if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2091 | Kind = tok::hashhash; // '%:%:' -> '##' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2092 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 2093 | SizeTmp2, Result); |
| 2094 | } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2095 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 2096 | if (!isLexingRawMode()) |
| 2097 | Diag(BufferPtr, diag::charize_microsoft_ext); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2098 | Kind = tok::hashat; |
Chris Lattner | e91e932 | 2009-03-18 20:58:27 +0000 | [diff] [blame] | 2099 | } else { // '%:' -> '#' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2100 | // We parsed a # character. If this occurs at the start of the line, |
| 2101 | // it's actually the start of a preprocessing directive. Callback to |
| 2102 | // the preprocessor to handle it. |
| 2103 | // FIXME: -fpreprocessed mode?? |
Chris Lattner | 766703b | 2009-05-13 06:10:29 +0000 | [diff] [blame] | 2104 | if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) { |
Chris Lattner | e91e932 | 2009-03-18 20:58:27 +0000 | [diff] [blame] | 2105 | FormTokenWithChars(Result, CurPtr, tok::hash); |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 2106 | PP->HandleDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2107 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2108 | // As an optimization, if the preprocessor didn't switch lexers, tail |
| 2109 | // recurse. |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 2110 | if (PP->isCurrentLexer(this)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2111 | // Start a new token. If this is a #include or something, the PP may |
| 2112 | // want us starting at the beginning of the line again. If so, set |
Chris Lattner | 515f43f | 2010-04-12 23:04:41 +0000 | [diff] [blame] | 2113 | // the StartOfLine flag and clear LeadingSpace. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2114 | if (IsAtStartOfLine) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 2115 | Result.setFlag(Token::StartOfLine); |
Chris Lattner | 515f43f | 2010-04-12 23:04:41 +0000 | [diff] [blame] | 2116 | Result.clearFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2117 | IsAtStartOfLine = false; |
| 2118 | } |
| 2119 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 2120 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2121 | |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 2122 | return PP->Lex(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2123 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2124 | |
Chris Lattner | e91e932 | 2009-03-18 20:58:27 +0000 | [diff] [blame] | 2125 | Kind = tok::hash; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2126 | } |
| 2127 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2128 | Kind = tok::percent; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2129 | } |
| 2130 | break; |
| 2131 | case '<': |
| 2132 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2133 | if (ParsingFilename) { |
Chris Lattner | 9cb51ce | 2009-04-17 23:56:52 +0000 | [diff] [blame] | 2134 | return LexAngledStringLiteral(Result, CurPtr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2135 | } else if (Char == '<') { |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 2136 | char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2); |
| 2137 | if (After == '=') { |
| 2138 | Kind = tok::lesslessequal; |
| 2139 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 2140 | SizeTmp2, Result); |
| 2141 | } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) { |
| 2142 | // If this is actually a '<<<<<<<' version control conflict marker, |
| 2143 | // recognize it as such and recover nicely. |
| 2144 | goto LexNextToken; |
| 2145 | } else { |
| 2146 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2147 | Kind = tok::lessless; |
| 2148 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2149 | } else if (Char == '=') { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2150 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2151 | Kind = tok::lessequal; |
| 2152 | } else if (Features.Digraphs && Char == ':') { // '<:' -> '[' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2153 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2154 | Kind = tok::l_square; |
| 2155 | } else if (Features.Digraphs && Char == '%') { // '<%' -> '{' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2156 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2157 | Kind = tok::l_brace; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2158 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2159 | Kind = tok::less; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2160 | } |
| 2161 | break; |
| 2162 | case '>': |
| 2163 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2164 | if (Char == '=') { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2165 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2166 | Kind = tok::greaterequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2167 | } else if (Char == '>') { |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 2168 | char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2); |
| 2169 | if (After == '=') { |
| 2170 | CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), |
| 2171 | SizeTmp2, Result); |
| 2172 | Kind = tok::greatergreaterequal; |
| 2173 | } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) { |
| 2174 | // If this is '>>>>>>>' and we're in a conflict marker, ignore it. |
| 2175 | goto LexNextToken; |
| 2176 | } else { |
| 2177 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2178 | Kind = tok::greatergreater; |
| 2179 | } |
| 2180 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2181 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2182 | Kind = tok::greater; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2183 | } |
| 2184 | break; |
| 2185 | case '^': |
| 2186 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2187 | if (Char == '=') { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2188 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2189 | Kind = tok::caretequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2190 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2191 | Kind = tok::caret; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2192 | } |
| 2193 | break; |
| 2194 | case '|': |
| 2195 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2196 | if (Char == '=') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2197 | Kind = tok::pipeequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2198 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2199 | } else if (Char == '|') { |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 2200 | // If this is '|||||||' and we're in a conflict marker, ignore it. |
| 2201 | if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1)) |
| 2202 | goto LexNextToken; |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2203 | Kind = tok::pipepipe; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2204 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2205 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2206 | Kind = tok::pipe; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2207 | } |
| 2208 | break; |
| 2209 | case ':': |
| 2210 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2211 | if (Features.Digraphs && Char == '>') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2212 | Kind = tok::r_square; // ':>' -> ']' |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2213 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2214 | } else if (Features.CPlusPlus && Char == ':') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2215 | Kind = tok::coloncolon; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2216 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2217 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2218 | Kind = tok::colon; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2219 | } |
| 2220 | break; |
| 2221 | case ';': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2222 | Kind = tok::semi; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2223 | break; |
| 2224 | case '=': |
| 2225 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2226 | if (Char == '=') { |
Chris Lattner | 34f349d | 2009-12-14 06:16:57 +0000 | [diff] [blame] | 2227 | // If this is '=======' and we're in a conflict marker, ignore it. |
| 2228 | if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1)) |
| 2229 | goto LexNextToken; |
| 2230 | |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2231 | Kind = tok::equalequal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2232 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2233 | } else { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2234 | Kind = tok::equal; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2235 | } |
| 2236 | break; |
| 2237 | case ',': |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2238 | Kind = tok::comma; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2239 | break; |
| 2240 | case '#': |
| 2241 | Char = getCharAndSize(CurPtr, SizeTmp); |
| 2242 | if (Char == '#') { |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2243 | Kind = tok::hashhash; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2244 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2245 | } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2246 | Kind = tok::hashat; |
Chris Lattner | 74d15df | 2008-11-22 02:02:22 +0000 | [diff] [blame] | 2247 | if (!isLexingRawMode()) |
| 2248 | Diag(BufferPtr, diag::charize_microsoft_ext); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2249 | CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); |
| 2250 | } else { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2251 | // We parsed a # character. If this occurs at the start of the line, |
| 2252 | // it's actually the start of a preprocessing directive. Callback to |
| 2253 | // the preprocessor to handle it. |
| 2254 | // FIXME: -fpreprocessed mode?? |
Chris Lattner | 766703b | 2009-05-13 06:10:29 +0000 | [diff] [blame] | 2255 | if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) { |
Chris Lattner | e91e932 | 2009-03-18 20:58:27 +0000 | [diff] [blame] | 2256 | FormTokenWithChars(Result, CurPtr, tok::hash); |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 2257 | PP->HandleDirective(Result); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2258 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2259 | // As an optimization, if the preprocessor didn't switch lexers, tail |
| 2260 | // recurse. |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 2261 | if (PP->isCurrentLexer(this)) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2262 | // Start a new token. If this is a #include or something, the PP may |
| 2263 | // want us starting at the beginning of the line again. If so, set |
Chris Lattner | 515f43f | 2010-04-12 23:04:41 +0000 | [diff] [blame] | 2264 | // the StartOfLine flag and clear LeadingSpace. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2265 | if (IsAtStartOfLine) { |
Chris Lattner | d217773 | 2007-07-20 16:59:19 +0000 | [diff] [blame] | 2266 | Result.setFlag(Token::StartOfLine); |
Chris Lattner | 515f43f | 2010-04-12 23:04:41 +0000 | [diff] [blame] | 2267 | Result.clearFlag(Token::LeadingSpace); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2268 | IsAtStartOfLine = false; |
| 2269 | } |
| 2270 | goto LexNextToken; // GCC isn't tail call eliminating. |
| 2271 | } |
Chris Lattner | 168ae2d | 2007-10-17 20:41:00 +0000 | [diff] [blame] | 2272 | return PP->Lex(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2273 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2274 | |
Chris Lattner | e91e932 | 2009-03-18 20:58:27 +0000 | [diff] [blame] | 2275 | Kind = tok::hash; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2276 | } |
| 2277 | break; |
| 2278 | |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 2279 | case '@': |
| 2280 | // Objective C support. |
| 2281 | if (CurPtr[-1] == '@' && Features.ObjC1) |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2282 | Kind = tok::at; |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 2283 | else |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2284 | Kind = tok::unknown; |
Chris Lattner | 3a57077 | 2008-01-03 17:58:54 +0000 | [diff] [blame] | 2285 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2286 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2287 | case '\\': |
| 2288 | // FIXME: UCN's. |
| 2289 | // FALL THROUGH. |
| 2290 | default: |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2291 | Kind = tok::unknown; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2292 | break; |
| 2293 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2294 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2295 | // Notify MIOpt that we read a non-whitespace/non-comment token. |
| 2296 | MIOpt.ReadToken(); |
| 2297 | |
| 2298 | // Update the location of token as well as BufferPtr. |
Chris Lattner | 9e6293d | 2008-10-12 04:51:35 +0000 | [diff] [blame] | 2299 | FormTokenWithChars(Result, CurPtr, Kind); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2300 | } |