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