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