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