blob: 9e8d1aa74076e42b8b36d1a0f43dbf82d75c1b37 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Lexer and Token interfaces.
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"
29#include "clang/Basic/Diagnostic.h"
30#include "clang/Basic/SourceManager.h"
31#include "llvm/Support/Compiler.h"
32#include "llvm/Support/MemoryBuffer.h"
33#include <cctype>
34using namespace clang;
35
36static void InitCharacterInfo();
37
Chris Lattneraa9bdf12007-10-07 08:47:24 +000038//===----------------------------------------------------------------------===//
39// Token Class Implementation
40//===----------------------------------------------------------------------===//
41
42/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
43bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
Douglas Gregora7502582008-12-01 21:46:47 +000044 if (IdentifierInfo *II = getIdentifierInfo())
45 return II->getObjCKeywordID() == objcKey;
46 return false;
Chris Lattneraa9bdf12007-10-07 08:47:24 +000047}
48
49/// getObjCKeywordID - Return the ObjC keyword kind.
50tok::ObjCKeywordKind Token::getObjCKeywordID() const {
51 IdentifierInfo *specId = getIdentifierInfo();
52 return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
53}
54
Chris Lattner7208a4b2007-12-13 01:59:49 +000055
Chris Lattneraa9bdf12007-10-07 08:47:24 +000056//===----------------------------------------------------------------------===//
57// Lexer Class Implementation
58//===----------------------------------------------------------------------===//
59
Chris Lattner9bb53672009-01-17 06:55:17 +000060void Lexer::InitLexer(const char *BufStart, const char *BufPtr,
61 const char *BufEnd) {
62 InitCharacterInfo();
63
64 BufferStart = BufStart;
65 BufferPtr = BufPtr;
66 BufferEnd = BufEnd;
67
68 assert(BufEnd[0] == 0 &&
69 "We assume that the input buffer has a null character at the end"
70 " to simplify lexing!");
71
72 Is_PragmaLexer = false;
73
74 // Start of the file is a start of line.
75 IsAtStartOfLine = true;
76
77 // We are not after parsing a #.
78 ParsingPreprocessorDirective = false;
79
80 // We are not after parsing #include.
81 ParsingFilename = false;
82
83 // We are not in raw mode. Raw mode disables diagnostics and interpretation
84 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
85 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
86 // or otherwise skipping over tokens.
87 LexingRawMode = false;
88
89 // Default to not keeping comments.
90 ExtendedTokenMode = 0;
91}
92
Chris Lattner54edb962009-01-17 07:56:59 +000093/// Lexer constructor - Create a new lexer object for the specified buffer
94/// with the specified preprocessor managing the lexing process. This lexer
95/// assumes that the associated file buffer and Preprocessor objects will
96/// outlive it, so it doesn't take ownership of either of them.
Chris Lattner5df086f2009-01-17 08:03:42 +000097Lexer::Lexer(FileID FID, Preprocessor &PP)
98 : PreprocessorLexer(&PP, FID),
99 FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
100 Features(PP.getLangOptions()) {
Chris Lattner54edb962009-01-17 07:56:59 +0000101
Chris Lattner5df086f2009-01-17 08:03:42 +0000102 const llvm::MemoryBuffer *InputFile = PP.getSourceManager().getBuffer(FID);
Chris Lattner54edb962009-01-17 07:56:59 +0000103
104 InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
105 InputFile->getBufferEnd());
106
107 // Default to keeping comments if the preprocessor wants them.
108 SetCommentRetentionState(PP.getCommentRetentionState());
109}
Chris Lattneraa9bdf12007-10-07 08:47:24 +0000110
Chris Lattner342dccb2007-10-17 20:41:00 +0000111/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner0b5892e2008-10-12 01:15:46 +0000112/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
113/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner342dccb2007-10-17 20:41:00 +0000114Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattnerded7b322009-01-17 07:42:27 +0000115 const char *BufStart, const char *BufPtr, const char *BufEnd)
Chris Lattneref63fd52009-01-17 03:48:08 +0000116 : FileLoc(fileloc), Features(features) {
Chris Lattner9bb53672009-01-17 06:55:17 +0000117
Chris Lattner9bb53672009-01-17 06:55:17 +0000118 InitLexer(BufStart, BufPtr, BufEnd);
Chris Lattner342dccb2007-10-17 20:41:00 +0000119
120 // We *are* in raw mode.
121 LexingRawMode = true;
Chris Lattner342dccb2007-10-17 20:41:00 +0000122}
123
Chris Lattnerc7b23592009-01-17 07:35:14 +0000124/// Lexer constructor - Create a new raw lexer object. This object is only
125/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
126/// range will outlive it, so it doesn't take ownership of it.
127Lexer::Lexer(FileID FID, const SourceManager &SM, const LangOptions &features)
128 : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
129 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
130
131 InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
132 FromFile->getBufferEnd());
133
134 // We *are* in raw mode.
135 LexingRawMode = true;
136}
137
Chris Lattnercef77822009-01-17 08:27:52 +0000138/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
139/// _Pragma expansion. This has a variety of magic semantics that this method
140/// sets up. It returns a new'd Lexer that must be delete'd when done.
141///
142/// On entrance to this routine, TokStartLoc is a macro location which has a
143/// spelling loc that indicates the bytes to be lexed for the token and an
144/// instantiation location that indicates where all lexed tokens should be
145/// "expanded from".
146///
147/// FIXME: It would really be nice to make _Pragma just be a wrapper around a
148/// normal lexer that remaps tokens as they fly by. This would require making
149/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer
150/// interface that could handle this stuff. This would pull GetMappedTokenLoc
151/// out of the critical path of the lexer!
152///
Chris Lattnere805eac2009-01-19 06:46:35 +0000153Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
154 SourceLocation InstantiationLoc,
155 unsigned TokLen, Preprocessor &PP) {
Chris Lattnercef77822009-01-17 08:27:52 +0000156 SourceManager &SM = PP.getSourceManager();
Chris Lattnercef77822009-01-17 08:27:52 +0000157
158 // Create the lexer as if we were going to lex the file normally.
Chris Lattner1737bd52009-01-19 07:46:45 +0000159 FileID SpellingFID = SM.getFileID(SpellingLoc);
Chris Lattnere805eac2009-01-19 06:46:35 +0000160 Lexer *L = new Lexer(SpellingFID, PP);
Chris Lattnercef77822009-01-17 08:27:52 +0000161
162 // Now that the lexer is created, change the start/end locations so that we
163 // just lex the subsection of the file that we want. This is lexing from a
164 // scratch buffer.
165 const char *StrData = SM.getCharacterData(SpellingLoc);
166
167 L->BufferPtr = StrData;
168 L->BufferEnd = StrData+TokLen;
169
170 // Set the SourceLocation with the remapping information. This ensures that
171 // GetMappedTokenLoc will remap the tokens as they are lexed.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000172 L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
173 InstantiationLoc, TokLen);
Chris Lattnercef77822009-01-17 08:27:52 +0000174
175 // Ensure that the lexer thinks it is inside a directive, so that end \n will
176 // return an EOM token.
177 L->ParsingPreprocessorDirective = true;
178
179 // This lexer really is for _Pragma.
180 L->Is_PragmaLexer = true;
181 return L;
182}
183
Chris Lattner342dccb2007-10-17 20:41:00 +0000184
Chris Lattner4b009652007-07-25 00:24:17 +0000185/// Stringify - Convert the specified string into a C string, with surrounding
186/// ""'s, and with escaped \ and " characters.
187std::string Lexer::Stringify(const std::string &Str, bool Charify) {
188 std::string Result = Str;
189 char Quote = Charify ? '\'' : '"';
190 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
191 if (Result[i] == '\\' || Result[i] == Quote) {
192 Result.insert(Result.begin()+i, '\\');
193 ++i; ++e;
194 }
195 }
196 return Result;
197}
198
199/// Stringify - Convert the specified string into a C string by escaping '\'
200/// and " characters. This does not add surrounding ""'s to the string.
201void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
202 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
203 if (Str[i] == '\\' || Str[i] == '"') {
204 Str.insert(Str.begin()+i, '\\');
205 ++i; ++e;
206 }
207 }
208}
209
210
Chris Lattner761d76b2007-10-17 21:18:47 +0000211/// MeasureTokenLength - Relex the token at the specified location and return
212/// its length in bytes in the input file. If the token needs cleaning (e.g.
213/// includes a trigraph or an escaped newline) then this count includes bytes
214/// that are part of that.
215unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
216 const SourceManager &SM) {
Chris Lattner761d76b2007-10-17 21:18:47 +0000217 // TODO: this could be special cased for common tokens like identifiers, ')',
218 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
219 // all obviously single-char tokens. This could use
220 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
221 // something.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000222
223 // If this comes from a macro expansion, we really do want the macro name, not
224 // the token this macro expanded to.
225 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedInstantiationLoc(Loc);
Chris Lattner003dfcb2009-01-17 08:30:10 +0000226 std::pair<const char *,const char *> Buffer = SM.getBufferData(LocInfo.first);
227 const char *StrData = Buffer.first+LocInfo.second;
228
Chris Lattner761d76b2007-10-17 21:18:47 +0000229 // Create a langops struct and enable trigraphs. This is sufficient for
230 // measuring tokens.
231 LangOptions LangOpts;
232 LangOpts.Trigraphs = true;
233
234 // Create a lexer starting at the beginning of this token.
Chris Lattnerded7b322009-01-17 07:42:27 +0000235 Lexer TheLexer(Loc, LangOpts, Buffer.first, StrData, Buffer.second);
Chris Lattner761d76b2007-10-17 21:18:47 +0000236 Token TheTok;
Chris Lattner0b5892e2008-10-12 01:15:46 +0000237 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner761d76b2007-10-17 21:18:47 +0000238 return TheTok.getLength();
239}
240
Chris Lattner4b009652007-07-25 00:24:17 +0000241//===----------------------------------------------------------------------===//
242// Character information.
243//===----------------------------------------------------------------------===//
244
245static unsigned char CharInfo[256];
246
247enum {
248 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
249 CHAR_VERT_WS = 0x02, // '\r', '\n'
250 CHAR_LETTER = 0x04, // a-z,A-Z
251 CHAR_NUMBER = 0x08, // 0-9
252 CHAR_UNDER = 0x10, // _
253 CHAR_PERIOD = 0x20 // .
254};
255
256static void InitCharacterInfo() {
257 static bool isInited = false;
258 if (isInited) return;
259 isInited = true;
260
261 // Intiialize the CharInfo table.
262 // TODO: statically initialize this.
263 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
264 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
265 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
266
267 CharInfo[(int)'_'] = CHAR_UNDER;
268 CharInfo[(int)'.'] = CHAR_PERIOD;
269 for (unsigned i = 'a'; i <= 'z'; ++i)
270 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
271 for (unsigned i = '0'; i <= '9'; ++i)
272 CharInfo[i] = CHAR_NUMBER;
273}
274
275/// isIdentifierBody - Return true if this is the body character of an
276/// identifier, which is [a-zA-Z0-9_].
277static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiserc62f6b92007-10-18 12:47:01 +0000278 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Chris Lattner4b009652007-07-25 00:24:17 +0000279}
280
281/// isHorizontalWhitespace - Return true if this character is horizontal
282/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
283static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiserc62f6b92007-10-18 12:47:01 +0000284 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Chris Lattner4b009652007-07-25 00:24:17 +0000285}
286
287/// isWhitespace - Return true if this character is horizontal or vertical
288/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
289/// for '\0'.
290static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiserc62f6b92007-10-18 12:47:01 +0000291 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Chris Lattner4b009652007-07-25 00:24:17 +0000292}
293
294/// isNumberBody - Return true if this is the body character of an
295/// preprocessing number, which is [a-zA-Z0-9_.].
296static inline bool isNumberBody(unsigned char c) {
Hartmut Kaiserc62f6b92007-10-18 12:47:01 +0000297 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
298 true : false;
Chris Lattner4b009652007-07-25 00:24:17 +0000299}
300
301
302//===----------------------------------------------------------------------===//
303// Diagnostics forwarding code.
304//===----------------------------------------------------------------------===//
305
306/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
307/// lexer buffer was all instantiated at a single point, perform the mapping.
308/// This is currently only used for _Pragma implementation, so it is the slow
309/// path of the hot getSourceLocation method. Do not allow it to be inlined.
310static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
311 SourceLocation FileLoc,
Chris Lattner27c0ced2009-01-26 00:43:02 +0000312 unsigned CharNo,
313 unsigned TokLen) DISABLE_INLINE;
Chris Lattner4b009652007-07-25 00:24:17 +0000314static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
315 SourceLocation FileLoc,
Chris Lattner27c0ced2009-01-26 00:43:02 +0000316 unsigned CharNo, unsigned TokLen) {
Chris Lattner4b009652007-07-25 00:24:17 +0000317 // Otherwise, we're lexing "mapped tokens". This is used for things like
318 // _Pragma handling. Combine the instantiation location of FileLoc with the
Chris Lattnercdf600e2009-01-16 07:00:02 +0000319 // spelling location.
Chris Lattner4b009652007-07-25 00:24:17 +0000320 SourceManager &SourceMgr = PP.getSourceManager();
321
Chris Lattner18c8dc02009-01-16 07:36:28 +0000322 // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
Chris Lattnercdf600e2009-01-16 07:00:02 +0000323 // characters come from spelling(FileLoc)+Offset.
Chris Lattner18c8dc02009-01-16 07:36:28 +0000324 SourceLocation InstLoc = SourceMgr.getInstantiationLoc(FileLoc);
Chris Lattnercdf600e2009-01-16 07:00:02 +0000325 SourceLocation SpellingLoc = SourceMgr.getSpellingLoc(FileLoc);
Chris Lattnere805eac2009-01-19 06:46:35 +0000326 SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
Chris Lattner27c0ced2009-01-26 00:43:02 +0000327 return SourceMgr.createInstantiationLoc(SpellingLoc, InstLoc, TokLen);
Chris Lattner4b009652007-07-25 00:24:17 +0000328}
329
330/// getSourceLocation - Return a source location identifier for the specified
331/// offset in the current file.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000332SourceLocation Lexer::getSourceLocation(const char *Loc,
333 unsigned TokLen) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000334 assert(Loc >= BufferStart && Loc <= BufferEnd &&
335 "Location out of range for this buffer!");
336
337 // In the normal case, we're just lexing from a simple file buffer, return
338 // the file id from FileLoc with the offset specified.
339 unsigned CharNo = Loc-BufferStart;
340 if (FileLoc.isFileID())
Chris Lattnere805eac2009-01-19 06:46:35 +0000341 return FileLoc.getFileLocWithOffset(CharNo);
Chris Lattner4b009652007-07-25 00:24:17 +0000342
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000343 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
344 // tokens are lexed from where the _Pragma was defined.
Chris Lattner342dccb2007-10-17 20:41:00 +0000345 assert(PP && "This doesn't work on raw lexers");
Chris Lattner27c0ced2009-01-26 00:43:02 +0000346 return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
Chris Lattner4b009652007-07-25 00:24:17 +0000347}
348
349/// Diag - Forwarding function for diagnostics. This translate a source
350/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner9943e982008-11-22 00:59:29 +0000351DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner0370d6b2008-11-18 07:59:24 +0000352 return PP->Diag(getSourceLocation(Loc), DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000353}
Chris Lattner4b009652007-07-25 00:24:17 +0000354
355//===----------------------------------------------------------------------===//
356// Trigraph and Escaped Newline Handling Code.
357//===----------------------------------------------------------------------===//
358
359/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
360/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
361static char GetTrigraphCharForLetter(char Letter) {
362 switch (Letter) {
363 default: return 0;
364 case '=': return '#';
365 case ')': return ']';
366 case '(': return '[';
367 case '!': return '|';
368 case '\'': return '^';
369 case '>': return '}';
370 case '/': return '\\';
371 case '<': return '{';
372 case '-': return '~';
373 }
374}
375
376/// DecodeTrigraphChar - If the specified character is a legal trigraph when
377/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
378/// return the result character. Finally, emit a warning about trigraph use
379/// whether trigraphs are enabled or not.
380static char DecodeTrigraphChar(const char *CP, Lexer *L) {
381 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner0370d6b2008-11-18 07:59:24 +0000382 if (!Res || !L) return Res;
383
384 if (!L->getFeatures().Trigraphs) {
Chris Lattnerf9c62772008-11-22 02:02:22 +0000385 if (!L->isLexingRawMode())
386 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner0370d6b2008-11-18 07:59:24 +0000387 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000388 }
Chris Lattner0370d6b2008-11-18 07:59:24 +0000389
Chris Lattnerf9c62772008-11-22 02:02:22 +0000390 if (!L->isLexingRawMode())
391 L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
Chris Lattner4b009652007-07-25 00:24:17 +0000392 return Res;
393}
394
395/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
396/// get its size, and return it. This is tricky in several cases:
397/// 1. If currently at the start of a trigraph, we warn about the trigraph,
398/// then either return the trigraph (skipping 3 chars) or the '?',
399/// depending on whether trigraphs are enabled or not.
400/// 2. If this is an escaped newline (potentially with whitespace between
401/// the backslash and newline), implicitly skip the newline and return
402/// the char after it.
403/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
404///
405/// This handles the slow/uncommon case of the getCharAndSize method. Here we
406/// know that we can accumulate into Size, and that we have already incremented
407/// Ptr by Size bytes.
408///
409/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
410/// be updated to match.
411///
412char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
413 Token *Tok) {
414 // If we have a slash, look for an escaped newline.
415 if (Ptr[0] == '\\') {
416 ++Size;
417 ++Ptr;
418Slash:
419 // Common case, backslash-char where the char is not whitespace.
420 if (!isWhitespace(Ptr[0])) return '\\';
421
422 // See if we have optional whitespace characters followed by a newline.
423 {
424 unsigned SizeTmp = 0;
425 do {
426 ++SizeTmp;
427 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
428 // Remember that this token needs to be cleaned.
429 if (Tok) Tok->setFlag(Token::NeedsCleaning);
430
431 // Warn if there was whitespace between the backslash and newline.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000432 if (SizeTmp != 1 && Tok && !isLexingRawMode())
Chris Lattner4b009652007-07-25 00:24:17 +0000433 Diag(Ptr, diag::backslash_newline_space);
434
435 // If this is a \r\n or \n\r, skip the newlines.
436 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
437 Ptr[SizeTmp-1] != Ptr[SizeTmp])
438 ++SizeTmp;
439
440 // Found backslash<whitespace><newline>. Parse the char after it.
441 Size += SizeTmp;
442 Ptr += SizeTmp;
443 // Use slow version to accumulate a correct size field.
444 return getCharAndSizeSlow(Ptr, Size, Tok);
445 }
446 } while (isWhitespace(Ptr[SizeTmp]));
447 }
448
449 // Otherwise, this is not an escaped newline, just return the slash.
450 return '\\';
451 }
452
453 // If this is a trigraph, process it.
454 if (Ptr[0] == '?' && Ptr[1] == '?') {
455 // If this is actually a legal trigraph (not something like "??x"), emit
456 // a trigraph warning. If so, and if trigraphs are enabled, return it.
457 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
458 // Remember that this token needs to be cleaned.
459 if (Tok) Tok->setFlag(Token::NeedsCleaning);
460
461 Ptr += 3;
462 Size += 3;
463 if (C == '\\') goto Slash;
464 return C;
465 }
466 }
467
468 // If this is neither, return a single character.
469 ++Size;
470 return *Ptr;
471}
472
473
474/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
475/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
476/// and that we have already incremented Ptr by Size bytes.
477///
478/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
479/// be updated to match.
480char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
481 const LangOptions &Features) {
482 // If we have a slash, look for an escaped newline.
483 if (Ptr[0] == '\\') {
484 ++Size;
485 ++Ptr;
486Slash:
487 // Common case, backslash-char where the char is not whitespace.
488 if (!isWhitespace(Ptr[0])) return '\\';
489
490 // See if we have optional whitespace characters followed by a newline.
491 {
492 unsigned SizeTmp = 0;
493 do {
494 ++SizeTmp;
495 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
496
497 // If this is a \r\n or \n\r, skip the newlines.
498 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
499 Ptr[SizeTmp-1] != Ptr[SizeTmp])
500 ++SizeTmp;
501
502 // Found backslash<whitespace><newline>. Parse the char after it.
503 Size += SizeTmp;
504 Ptr += SizeTmp;
505
506 // Use slow version to accumulate a correct size field.
507 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
508 }
509 } while (isWhitespace(Ptr[SizeTmp]));
510 }
511
512 // Otherwise, this is not an escaped newline, just return the slash.
513 return '\\';
514 }
515
516 // If this is a trigraph, process it.
517 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
518 // If this is actually a legal trigraph (not something like "??x"), return
519 // it.
520 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
521 Ptr += 3;
522 Size += 3;
523 if (C == '\\') goto Slash;
524 return C;
525 }
526 }
527
528 // If this is neither, return a single character.
529 ++Size;
530 return *Ptr;
531}
532
533//===----------------------------------------------------------------------===//
534// Helper methods for lexing.
535//===----------------------------------------------------------------------===//
536
537void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
538 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
539 unsigned Size;
540 unsigned char C = *CurPtr++;
541 while (isIdentifierBody(C)) {
542 C = *CurPtr++;
543 }
544 --CurPtr; // Back up over the skipped character.
545
546 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
547 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
548 // FIXME: UCNs.
549 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
550FinishIdentifier:
551 const char *IdStart = BufferPtr;
Chris Lattner0344cc72008-10-12 04:51:35 +0000552 FormTokenWithChars(Result, CurPtr, tok::identifier);
Chris Lattner4b009652007-07-25 00:24:17 +0000553
554 // If we are in raw mode, return this identifier raw. There is no need to
555 // look up identifier information or attempt to macro expand it.
556 if (LexingRawMode) return;
557
558 // Fill in Result.IdentifierInfo, looking up the identifier in the
559 // identifier table.
Chris Lattner2f93c3d2009-01-21 07:45:14 +0000560 IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
Chris Lattner4b009652007-07-25 00:24:17 +0000561
Chris Lattner9ac3dd92009-01-23 18:35:48 +0000562 // Change the kind of this identifier to the appropriate token kind, e.g.
563 // turning "for" into a keyword.
564 Result.setKind(II->getTokenID());
565
Chris Lattner4b009652007-07-25 00:24:17 +0000566 // Finally, now that we know we have an identifier, pass this off to the
567 // preprocessor, which may macro expand it or something.
Chris Lattner2f93c3d2009-01-21 07:45:14 +0000568 if (II->isHandleIdentifierCase())
Chris Lattner5b747d02009-01-21 07:43:11 +0000569 PP->HandleIdentifier(Result);
570 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000571 }
572
573 // Otherwise, $,\,? in identifier found. Enter slower path.
574
575 C = getCharAndSize(CurPtr, Size);
576 while (1) {
577 if (C == '$') {
578 // If we hit a $ and they are not supported in identifiers, we are done.
579 if (!Features.DollarIdents) goto FinishIdentifier;
580
581 // Otherwise, emit a diagnostic and continue.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000582 if (!isLexingRawMode())
583 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner4b009652007-07-25 00:24:17 +0000584 CurPtr = ConsumeChar(CurPtr, Size, Result);
585 C = getCharAndSize(CurPtr, Size);
586 continue;
587 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
588 // Found end of identifier.
589 goto FinishIdentifier;
590 }
591
592 // Otherwise, this character is good, consume it.
593 CurPtr = ConsumeChar(CurPtr, Size, Result);
594
595 C = getCharAndSize(CurPtr, Size);
596 while (isIdentifierBody(C)) { // FIXME: UCNs.
597 CurPtr = ConsumeChar(CurPtr, Size, Result);
598 C = getCharAndSize(CurPtr, Size);
599 }
600 }
601}
602
603
Nate Begeman937cac72008-04-14 02:26:39 +0000604/// LexNumericConstant - Lex the remainder of a integer or floating point
Chris Lattner4b009652007-07-25 00:24:17 +0000605/// constant. From[-1] is the first character lexed. Return the end of the
606/// constant.
607void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
608 unsigned Size;
609 char C = getCharAndSize(CurPtr, Size);
610 char PrevCh = 0;
611 while (isNumberBody(C)) { // FIXME: UCNs?
612 CurPtr = ConsumeChar(CurPtr, Size, Result);
613 PrevCh = C;
614 C = getCharAndSize(CurPtr, Size);
615 }
616
617 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
618 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
619 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
620
621 // If we have a hex FP constant, continue.
Chris Lattner9ba63822008-11-22 07:39:03 +0000622 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
623 (Features.HexFloats || !Features.NoExtensions))
Chris Lattner4b009652007-07-25 00:24:17 +0000624 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
625
Chris Lattner4b009652007-07-25 00:24:17 +0000626 // Update the location of token as well as BufferPtr.
Chris Lattner0344cc72008-10-12 04:51:35 +0000627 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Chris Lattner4b009652007-07-25 00:24:17 +0000628}
629
630/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
631/// either " or L".
Chris Lattner867a87b2008-10-12 04:05:48 +0000632void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Chris Lattner4b009652007-07-25 00:24:17 +0000633 const char *NulCharacter = 0; // Does this string contain the \0 character?
634
635 char C = getAndAdvanceChar(CurPtr, Result);
636 while (C != '"') {
637 // Skip escaped characters.
638 if (C == '\\') {
639 // Skip the escaped character.
640 C = getAndAdvanceChar(CurPtr, Result);
641 } else if (C == '\n' || C == '\r' || // Newline.
642 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000643 if (!isLexingRawMode())
644 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner0344cc72008-10-12 04:51:35 +0000645 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner4b009652007-07-25 00:24:17 +0000646 return;
647 } else if (C == 0) {
648 NulCharacter = CurPtr-1;
649 }
650 C = getAndAdvanceChar(CurPtr, Result);
651 }
652
653 // If a nul character existed in the string, warn about it.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000654 if (NulCharacter && !isLexingRawMode())
655 Diag(NulCharacter, diag::null_in_string);
Chris Lattner4b009652007-07-25 00:24:17 +0000656
Chris Lattner4b009652007-07-25 00:24:17 +0000657 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner0344cc72008-10-12 04:51:35 +0000658 FormTokenWithChars(Result, CurPtr,
659 Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner4b009652007-07-25 00:24:17 +0000660}
661
662/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
663/// after having lexed the '<' character. This is used for #include filenames.
664void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
665 const char *NulCharacter = 0; // Does this string contain the \0 character?
666
667 char C = getAndAdvanceChar(CurPtr, Result);
668 while (C != '>') {
669 // Skip escaped characters.
670 if (C == '\\') {
671 // Skip the escaped character.
672 C = getAndAdvanceChar(CurPtr, Result);
673 } else if (C == '\n' || C == '\r' || // Newline.
674 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000675 if (!isLexingRawMode())
676 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner0344cc72008-10-12 04:51:35 +0000677 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner4b009652007-07-25 00:24:17 +0000678 return;
679 } else if (C == 0) {
680 NulCharacter = CurPtr-1;
681 }
682 C = getAndAdvanceChar(CurPtr, Result);
683 }
684
685 // If a nul character existed in the string, warn about it.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000686 if (NulCharacter && !isLexingRawMode())
687 Diag(NulCharacter, diag::null_in_string);
Chris Lattner4b009652007-07-25 00:24:17 +0000688
Chris Lattner4b009652007-07-25 00:24:17 +0000689 // Update the location of token as well as BufferPtr.
Chris Lattner0344cc72008-10-12 04:51:35 +0000690 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Chris Lattner4b009652007-07-25 00:24:17 +0000691}
692
693
694/// LexCharConstant - Lex the remainder of a character constant, after having
695/// lexed either ' or L'.
696void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
697 const char *NulCharacter = 0; // Does this character contain the \0 character?
698
699 // Handle the common case of 'x' and '\y' efficiently.
700 char C = getAndAdvanceChar(CurPtr, Result);
701 if (C == '\'') {
Chris Lattnerf9c62772008-11-22 02:02:22 +0000702 if (!isLexingRawMode())
703 Diag(BufferPtr, diag::err_empty_character);
Chris Lattner0344cc72008-10-12 04:51:35 +0000704 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4b009652007-07-25 00:24:17 +0000705 return;
706 } else if (C == '\\') {
707 // Skip the escaped character.
708 // FIXME: UCN's.
709 C = getAndAdvanceChar(CurPtr, Result);
710 }
711
712 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
713 ++CurPtr;
714 } else {
715 // Fall back on generic code for embedded nulls, newlines, wide chars.
716 do {
717 // Skip escaped characters.
718 if (C == '\\') {
719 // Skip the escaped character.
720 C = getAndAdvanceChar(CurPtr, Result);
721 } else if (C == '\n' || C == '\r' || // Newline.
722 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000723 if (!isLexingRawMode())
724 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner0344cc72008-10-12 04:51:35 +0000725 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner4b009652007-07-25 00:24:17 +0000726 return;
727 } else if (C == 0) {
728 NulCharacter = CurPtr-1;
729 }
730 C = getAndAdvanceChar(CurPtr, Result);
731 } while (C != '\'');
732 }
733
Chris Lattnerf9c62772008-11-22 02:02:22 +0000734 if (NulCharacter && !isLexingRawMode())
735 Diag(NulCharacter, diag::null_in_char);
Chris Lattner4b009652007-07-25 00:24:17 +0000736
Chris Lattner4b009652007-07-25 00:24:17 +0000737 // Update the location of token as well as BufferPtr.
Chris Lattner0344cc72008-10-12 04:51:35 +0000738 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Chris Lattner4b009652007-07-25 00:24:17 +0000739}
740
741/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
742/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattner867a87b2008-10-12 04:05:48 +0000743///
744/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
745///
746bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Chris Lattner4b009652007-07-25 00:24:17 +0000747 // Whitespace - Skip it, then return the token after the whitespace.
748 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
749 while (1) {
750 // Skip horizontal whitespace very aggressively.
751 while (isHorizontalWhitespace(Char))
752 Char = *++CurPtr;
753
Daniel Dunbara2208392008-11-25 00:20:22 +0000754 // Otherwise if we have something other than whitespace, we're done.
Chris Lattner4b009652007-07-25 00:24:17 +0000755 if (Char != '\n' && Char != '\r')
756 break;
757
758 if (ParsingPreprocessorDirective) {
759 // End of preprocessor directive line, let LexTokenInternal handle this.
760 BufferPtr = CurPtr;
Chris Lattner867a87b2008-10-12 04:05:48 +0000761 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000762 }
763
764 // ok, but handle newline.
765 // The returned token is at the start of the line.
766 Result.setFlag(Token::StartOfLine);
767 // No leading whitespace seen so far.
768 Result.clearFlag(Token::LeadingSpace);
769 Char = *++CurPtr;
770 }
771
772 // If this isn't immediately after a newline, there is leading space.
773 char PrevChar = CurPtr[-1];
774 if (PrevChar != '\n' && PrevChar != '\r')
775 Result.setFlag(Token::LeadingSpace);
776
Chris Lattner867a87b2008-10-12 04:05:48 +0000777 // If the client wants us to return whitespace, return it now.
778 if (isKeepWhitespaceMode()) {
Chris Lattner0344cc72008-10-12 04:51:35 +0000779 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner867a87b2008-10-12 04:05:48 +0000780 return true;
781 }
782
Chris Lattner4b009652007-07-25 00:24:17 +0000783 BufferPtr = CurPtr;
Chris Lattner867a87b2008-10-12 04:05:48 +0000784 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000785}
786
787// SkipBCPLComment - We have just read the // characters from input. Skip until
788// we find the newline character thats terminate the comment. Then update
Chris Lattnerf03b00c2008-10-12 04:15:42 +0000789/// BufferPtr and return. If we're in KeepCommentMode, this will form the token
790/// and return true.
Chris Lattner4b009652007-07-25 00:24:17 +0000791bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
792 // If BCPL comments aren't explicitly enabled for this language, emit an
793 // extension warning.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000794 if (!Features.BCPLComment && !isLexingRawMode()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000795 Diag(BufferPtr, diag::ext_bcpl_comment);
796
797 // Mark them enabled so we only emit one warning for this translation
798 // unit.
799 Features.BCPLComment = true;
800 }
801
802 // Scan over the body of the comment. The common case, when scanning, is that
803 // the comment contains normal ascii characters with nothing interesting in
804 // them. As such, optimize for this case with the inner loop.
805 char C;
806 do {
807 C = *CurPtr;
808 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
809 // If we find a \n character, scan backwards, checking to see if it's an
810 // escaped newline, like we do for block comments.
811
812 // Skip over characters in the fast loop.
813 while (C != 0 && // Potentially EOF.
814 C != '\\' && // Potentially escaped newline.
815 C != '?' && // Potentially trigraph.
816 C != '\n' && C != '\r') // Newline or DOS-style newline.
817 C = *++CurPtr;
818
819 // If this is a newline, we're done.
820 if (C == '\n' || C == '\r')
821 break; // Found the newline? Break out!
822
823 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnerc3697802008-12-12 07:34:39 +0000824 // properly decode the character. Read it in raw mode to avoid emitting
825 // diagnostics about things like trigraphs. If we see an escaped newline,
826 // we'll handle it below.
Chris Lattner4b009652007-07-25 00:24:17 +0000827 const char *OldPtr = CurPtr;
Chris Lattnerc3697802008-12-12 07:34:39 +0000828 bool OldRawMode = isLexingRawMode();
829 LexingRawMode = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000830 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerc3697802008-12-12 07:34:39 +0000831 LexingRawMode = OldRawMode;
Chris Lattner4b009652007-07-25 00:24:17 +0000832
833 // If we read multiple characters, and one of those characters was a \r or
834 // \n, then we had an escaped newline within the comment. Emit diagnostic
835 // unless the next line is also a // comment.
836 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
837 for (; OldPtr != CurPtr; ++OldPtr)
838 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
839 // Okay, we found a // comment that ends in a newline, if the next
840 // line is also a // comment, but has spaces, don't emit a diagnostic.
841 if (isspace(C)) {
842 const char *ForwardPtr = CurPtr;
843 while (isspace(*ForwardPtr)) // Skip whitespace.
844 ++ForwardPtr;
845 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
846 break;
847 }
848
Chris Lattnerf9c62772008-11-22 02:02:22 +0000849 if (!isLexingRawMode())
850 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Chris Lattner4b009652007-07-25 00:24:17 +0000851 break;
852 }
853 }
854
855 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
856 } while (C != '\n' && C != '\r');
857
858 // Found but did not consume the newline.
859
860 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner170adb12008-10-12 03:22:02 +0000861 if (inKeepCommentMode())
Chris Lattner4b009652007-07-25 00:24:17 +0000862 return SaveBCPLComment(Result, CurPtr);
863
864 // If we are inside a preprocessor directive and we see the end of line,
865 // return immediately, so that the lexer can return this as an EOM token.
866 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
867 BufferPtr = CurPtr;
Chris Lattnerf03b00c2008-10-12 04:15:42 +0000868 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000869 }
870
871 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner43d38202008-10-12 00:23:07 +0000872 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattner867a87b2008-10-12 04:05:48 +0000873 // contribute to another token), it isn't needed for correctness. Note that
874 // this is ok even in KeepWhitespaceMode, because we would have returned the
875 /// comment above in that mode.
Chris Lattner4b009652007-07-25 00:24:17 +0000876 ++CurPtr;
877
878 // The next returned token is at the start of the line.
879 Result.setFlag(Token::StartOfLine);
880 // No leading whitespace seen so far.
881 Result.clearFlag(Token::LeadingSpace);
882 BufferPtr = CurPtr;
Chris Lattnerf03b00c2008-10-12 04:15:42 +0000883 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000884}
885
886/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
887/// an appropriate way and return it.
888bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner0344cc72008-10-12 04:51:35 +0000889 // If we're not in a preprocessor directive, just return the // comment
890 // directly.
891 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattner4b009652007-07-25 00:24:17 +0000892
Chris Lattner0344cc72008-10-12 04:51:35 +0000893 if (!ParsingPreprocessorDirective)
894 return true;
895
896 // If this BCPL-style comment is in a macro definition, transmogrify it into
897 // a C-style block comment.
898 std::string Spelling = PP->getSpelling(Result);
899 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
900 Spelling[1] = '*'; // Change prefix to "/*".
901 Spelling += "*/"; // add suffix.
902
903 Result.setKind(tok::comment);
904 Result.setLocation(PP->CreateString(&Spelling[0], Spelling.size(),
905 Result.getLocation()));
906 Result.setLength(Spelling.size());
Chris Lattnerf03b00c2008-10-12 04:15:42 +0000907 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000908}
909
910/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
911/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattnerb3872872008-12-12 07:14:34 +0000912/// diagnostic if so. We know that the newline is inside of a block comment.
Chris Lattner4b009652007-07-25 00:24:17 +0000913static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
914 Lexer *L) {
915 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
916
917 // Back up off the newline.
918 --CurPtr;
919
920 // If this is a two-character newline sequence, skip the other character.
921 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
922 // \n\n or \r\r -> not escaped newline.
923 if (CurPtr[0] == CurPtr[1])
924 return false;
925 // \n\r or \r\n -> skip the newline.
926 --CurPtr;
927 }
928
929 // If we have horizontal whitespace, skip over it. We allow whitespace
930 // between the slash and newline.
931 bool HasSpace = false;
932 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
933 --CurPtr;
934 HasSpace = true;
935 }
936
937 // If we have a slash, we know this is an escaped newline.
938 if (*CurPtr == '\\') {
939 if (CurPtr[-1] != '*') return false;
940 } else {
941 // It isn't a slash, is it the ?? / trigraph?
942 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
943 CurPtr[-3] != '*')
944 return false;
945
946 // This is the trigraph ending the comment. Emit a stern warning!
947 CurPtr -= 2;
948
949 // If no trigraphs are enabled, warn that we ignored this trigraph and
950 // ignore this * character.
951 if (!L->getFeatures().Trigraphs) {
Chris Lattnerf9c62772008-11-22 02:02:22 +0000952 if (!L->isLexingRawMode())
953 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattner4b009652007-07-25 00:24:17 +0000954 return false;
955 }
Chris Lattnerf9c62772008-11-22 02:02:22 +0000956 if (!L->isLexingRawMode())
957 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner4b009652007-07-25 00:24:17 +0000958 }
959
960 // Warn about having an escaped newline between the */ characters.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000961 if (!L->isLexingRawMode())
962 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Chris Lattner4b009652007-07-25 00:24:17 +0000963
964 // If there was space between the backslash and newline, warn about it.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000965 if (HasSpace && !L->isLexingRawMode())
966 L->Diag(CurPtr, diag::backslash_newline_space);
Chris Lattner4b009652007-07-25 00:24:17 +0000967
968 return true;
969}
970
971#ifdef __SSE2__
972#include <emmintrin.h>
973#elif __ALTIVEC__
974#include <altivec.h>
975#undef bool
976#endif
977
978/// SkipBlockComment - We have just read the /* characters from input. Read
979/// until we find the */ characters that terminate the comment. Note that we
980/// don't bother decoding trigraphs or escaped newlines in block comments,
981/// because they cannot cause the comment to end. The only thing that can
982/// happen is the comment could end with an escaped newline between the */ end
983/// of comment.
Chris Lattnerf03b00c2008-10-12 04:15:42 +0000984///
985/// If KeepCommentMode is enabled, this forms a token from the comment and
986/// returns true.
Chris Lattner4b009652007-07-25 00:24:17 +0000987bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
988 // Scan one character past where we should, looking for a '/' character. Once
989 // we find it, check to see if it was preceeded by a *. This common
990 // optimization helps people who like to put a lot of * characters in their
991 // comments.
992
993 // The first character we get with newlines and trigraphs skipped to handle
994 // the degenerate /*/ case below correctly if the * has an escaped newline
995 // after it.
996 unsigned CharSize;
997 unsigned char C = getCharAndSize(CurPtr, CharSize);
998 CurPtr += CharSize;
999 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerf9c62772008-11-22 02:02:22 +00001000 if (!isLexingRawMode())
Chris Lattnere5eca952008-10-12 01:31:51 +00001001 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattnerd66f4542008-10-12 04:19:49 +00001002 --CurPtr;
1003
1004 // KeepWhitespaceMode should return this broken comment as a token. Since
1005 // it isn't a well formed comment, just return it as an 'unknown' token.
1006 if (isKeepWhitespaceMode()) {
Chris Lattner0344cc72008-10-12 04:51:35 +00001007 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd66f4542008-10-12 04:19:49 +00001008 return true;
1009 }
1010
1011 BufferPtr = CurPtr;
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001012 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001013 }
1014
1015 // Check to see if the first character after the '/*' is another /. If so,
1016 // then this slash does not end the block comment, it is part of it.
1017 if (C == '/')
1018 C = *CurPtr++;
1019
1020 while (1) {
1021 // Skip over all non-interesting characters until we find end of buffer or a
1022 // (probably ending) '/' character.
1023 if (CurPtr + 24 < BufferEnd) {
1024 // While not aligned to a 16-byte boundary.
1025 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1026 C = *CurPtr++;
1027
1028 if (C == '/') goto FoundSlash;
1029
1030#ifdef __SSE2__
1031 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1032 '/', '/', '/', '/', '/', '/', '/', '/');
1033 while (CurPtr+16 <= BufferEnd &&
1034 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1035 CurPtr += 16;
1036#elif __ALTIVEC__
1037 __vector unsigned char Slashes = {
1038 '/', '/', '/', '/', '/', '/', '/', '/',
1039 '/', '/', '/', '/', '/', '/', '/', '/'
1040 };
1041 while (CurPtr+16 <= BufferEnd &&
1042 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1043 CurPtr += 16;
1044#else
1045 // Scan for '/' quickly. Many block comments are very large.
1046 while (CurPtr[0] != '/' &&
1047 CurPtr[1] != '/' &&
1048 CurPtr[2] != '/' &&
1049 CurPtr[3] != '/' &&
1050 CurPtr+4 < BufferEnd) {
1051 CurPtr += 4;
1052 }
1053#endif
1054
1055 // It has to be one of the bytes scanned, increment to it and read one.
1056 C = *CurPtr++;
1057 }
1058
1059 // Loop to scan the remainder.
1060 while (C != '/' && C != '\0')
1061 C = *CurPtr++;
1062
1063 FoundSlash:
1064 if (C == '/') {
1065 if (CurPtr[-2] == '*') // We found the final */. We're done!
1066 break;
1067
1068 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
1069 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
1070 // We found the final */, though it had an escaped newline between the
1071 // * and /. We're done!
1072 break;
1073 }
1074 }
1075 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1076 // If this is a /* inside of the comment, emit a warning. Don't do this
1077 // if this is a /*/, which will end the comment. This misses cases with
1078 // embedded escaped newlines, but oh well.
Chris Lattnerf9c62772008-11-22 02:02:22 +00001079 if (!isLexingRawMode())
1080 Diag(CurPtr-1, diag::warn_nested_block_comment);
Chris Lattner4b009652007-07-25 00:24:17 +00001081 }
1082 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerf9c62772008-11-22 02:02:22 +00001083 if (!isLexingRawMode())
1084 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner4b009652007-07-25 00:24:17 +00001085 // Note: the user probably forgot a */. We could continue immediately
1086 // after the /*, but this would involve lexing a lot of what really is the
1087 // comment, which surely would confuse the parser.
Chris Lattnerd66f4542008-10-12 04:19:49 +00001088 --CurPtr;
1089
1090 // KeepWhitespaceMode should return this broken comment as a token. Since
1091 // it isn't a well formed comment, just return it as an 'unknown' token.
1092 if (isKeepWhitespaceMode()) {
Chris Lattner0344cc72008-10-12 04:51:35 +00001093 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd66f4542008-10-12 04:19:49 +00001094 return true;
1095 }
1096
1097 BufferPtr = CurPtr;
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001098 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001099 }
1100 C = *CurPtr++;
1101 }
1102
1103 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner170adb12008-10-12 03:22:02 +00001104 if (inKeepCommentMode()) {
Chris Lattner0344cc72008-10-12 04:51:35 +00001105 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001106 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001107 }
1108
1109 // It is common for the tokens immediately after a /**/ comment to be
1110 // whitespace. Instead of going through the big switch, handle it
Chris Lattner867a87b2008-10-12 04:05:48 +00001111 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1112 // have already returned above with the comment as a token.
Chris Lattner4b009652007-07-25 00:24:17 +00001113 if (isHorizontalWhitespace(*CurPtr)) {
1114 Result.setFlag(Token::LeadingSpace);
1115 SkipWhitespace(Result, CurPtr+1);
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001116 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001117 }
1118
1119 // Otherwise, just return so that the next character will be lexed as a token.
1120 BufferPtr = CurPtr;
1121 Result.setFlag(Token::LeadingSpace);
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001122 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001123}
1124
1125//===----------------------------------------------------------------------===//
1126// Primary Lexing Entry Points
1127//===----------------------------------------------------------------------===//
1128
Chris Lattner4b009652007-07-25 00:24:17 +00001129/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1130/// uninterpreted string. This switches the lexer out of directive mode.
1131std::string Lexer::ReadToEndOfLine() {
1132 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1133 "Must be in a preprocessing directive!");
1134 std::string Result;
1135 Token Tmp;
1136
1137 // CurPtr - Cache BufferPtr in an automatic variable.
1138 const char *CurPtr = BufferPtr;
1139 while (1) {
1140 char Char = getAndAdvanceChar(CurPtr, Tmp);
1141 switch (Char) {
1142 default:
1143 Result += Char;
1144 break;
1145 case 0: // Null.
1146 // Found end of file?
1147 if (CurPtr-1 != BufferEnd) {
1148 // Nope, normal character, continue.
1149 Result += Char;
1150 break;
1151 }
1152 // FALL THROUGH.
1153 case '\r':
1154 case '\n':
1155 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1156 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1157 BufferPtr = CurPtr-1;
1158
1159 // Next, lex the character, which should handle the EOM transition.
1160 Lex(Tmp);
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001161 assert(Tmp.is(tok::eom) && "Unexpected token!");
Chris Lattner4b009652007-07-25 00:24:17 +00001162
1163 // Finally, we're done, return the string we found.
1164 return Result;
1165 }
1166 }
1167}
1168
1169/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1170/// condition, reporting diagnostics and handling other edge cases as required.
1171/// This returns true if Result contains a token, false if PP.Lex should be
1172/// called again.
1173bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
1174 // If we hit the end of the file while parsing a preprocessor directive,
1175 // end the preprocessor directive first. The next token returned will
1176 // then be the end of file.
1177 if (ParsingPreprocessorDirective) {
1178 // Done parsing the "line".
1179 ParsingPreprocessorDirective = false;
Chris Lattner4b009652007-07-25 00:24:17 +00001180 // Update the location of token as well as BufferPtr.
Chris Lattner0344cc72008-10-12 04:51:35 +00001181 FormTokenWithChars(Result, CurPtr, tok::eom);
Chris Lattner4b009652007-07-25 00:24:17 +00001182
1183 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner1c1bed12008-10-12 03:27:19 +00001184 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner4b009652007-07-25 00:24:17 +00001185 return true; // Have a token.
1186 }
1187
1188 // If we are in raw mode, return this event as an EOF token. Let the caller
1189 // that put us in raw mode handle the event.
Chris Lattnerf9c62772008-11-22 02:02:22 +00001190 if (isLexingRawMode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001191 Result.startToken();
1192 BufferPtr = BufferEnd;
Chris Lattner0344cc72008-10-12 04:51:35 +00001193 FormTokenWithChars(Result, BufferEnd, tok::eof);
Chris Lattner4b009652007-07-25 00:24:17 +00001194 return true;
1195 }
1196
1197 // Otherwise, issue diagnostics for unterminated #if and missing newline.
1198
1199 // If we are in a #if directive, emit an error.
1200 while (!ConditionalStack.empty()) {
Chris Lattner8ef6cdc2008-11-22 06:22:39 +00001201 PP->Diag(ConditionalStack.back().IfLoc,
1202 diag::err_pp_unterminated_conditional);
Chris Lattner4b009652007-07-25 00:24:17 +00001203 ConditionalStack.pop_back();
1204 }
1205
Chris Lattner5c337fa2008-04-12 05:54:25 +00001206 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1207 // a pedwarn.
1208 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Chris Lattner4b009652007-07-25 00:24:17 +00001209 Diag(BufferEnd, diag::ext_no_newline_eof);
1210
1211 BufferPtr = CurPtr;
1212
1213 // Finally, let the preprocessor handle this.
Chris Lattner342dccb2007-10-17 20:41:00 +00001214 return PP->HandleEndOfFile(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001215}
1216
1217/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1218/// the specified lexer will return a tok::l_paren token, 0 if it is something
1219/// else and 2 if there are no more tokens in the buffer controlled by the
1220/// lexer.
1221unsigned Lexer::isNextPPTokenLParen() {
1222 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1223
1224 // Switch to 'skipping' mode. This will ensure that we can lex a token
1225 // without emitting diagnostics, disables macro expansion, and will cause EOF
1226 // to return an EOF token instead of popping the include stack.
1227 LexingRawMode = true;
1228
1229 // Save state that can be changed while lexing so that we can restore it.
1230 const char *TmpBufferPtr = BufferPtr;
1231
1232 Token Tok;
1233 Tok.startToken();
1234 LexTokenInternal(Tok);
1235
1236 // Restore state that may have changed.
1237 BufferPtr = TmpBufferPtr;
1238
1239 // Restore the lexer back to non-skipping mode.
1240 LexingRawMode = false;
1241
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001242 if (Tok.is(tok::eof))
Chris Lattner4b009652007-07-25 00:24:17 +00001243 return 2;
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001244 return Tok.is(tok::l_paren);
Chris Lattner4b009652007-07-25 00:24:17 +00001245}
1246
1247
1248/// LexTokenInternal - This implements a simple C family lexer. It is an
1249/// extremely performance critical piece of code. This assumes that the buffer
1250/// has a null character at the end of the file. Return true if an error
1251/// occurred and compilation should terminate, false if normal. This returns a
1252/// preprocessing token, not a normal token, as such, it is an internal
1253/// interface. It assumes that the Flags of result have been cleared before
1254/// calling this.
1255void Lexer::LexTokenInternal(Token &Result) {
1256LexNextToken:
1257 // New token, can't need cleaning yet.
1258 Result.clearFlag(Token::NeedsCleaning);
1259 Result.setIdentifierInfo(0);
1260
1261 // CurPtr - Cache BufferPtr in an automatic variable.
1262 const char *CurPtr = BufferPtr;
1263
1264 // Small amounts of horizontal whitespace is very common between tokens.
1265 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1266 ++CurPtr;
1267 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1268 ++CurPtr;
Chris Lattner867a87b2008-10-12 04:05:48 +00001269
1270 // If we are keeping whitespace and other tokens, just return what we just
1271 // skipped. The next lexer invocation will return the token after the
1272 // whitespace.
1273 if (isKeepWhitespaceMode()) {
Chris Lattner0344cc72008-10-12 04:51:35 +00001274 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner867a87b2008-10-12 04:05:48 +00001275 return;
1276 }
1277
Chris Lattner4b009652007-07-25 00:24:17 +00001278 BufferPtr = CurPtr;
1279 Result.setFlag(Token::LeadingSpace);
1280 }
1281
1282 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1283
1284 // Read a character, advancing over it.
1285 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001286 tok::TokenKind Kind;
1287
Chris Lattner4b009652007-07-25 00:24:17 +00001288 switch (Char) {
1289 case 0: // Null.
1290 // Found end of file?
1291 if (CurPtr-1 == BufferEnd) {
1292 // Read the PP instance variable into an automatic variable, because
1293 // LexEndOfFile will often delete 'this'.
Chris Lattner342dccb2007-10-17 20:41:00 +00001294 Preprocessor *PPCache = PP;
Chris Lattner4b009652007-07-25 00:24:17 +00001295 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1296 return; // Got a token to return.
Chris Lattner342dccb2007-10-17 20:41:00 +00001297 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1298 return PPCache->Lex(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001299 }
1300
Chris Lattnerf9c62772008-11-22 02:02:22 +00001301 if (!isLexingRawMode())
1302 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner4b009652007-07-25 00:24:17 +00001303 Result.setFlag(Token::LeadingSpace);
Chris Lattner867a87b2008-10-12 04:05:48 +00001304 if (SkipWhitespace(Result, CurPtr))
1305 return; // KeepWhitespaceMode
1306
Chris Lattner4b009652007-07-25 00:24:17 +00001307 goto LexNextToken; // GCC isn't tail call eliminating.
1308 case '\n':
1309 case '\r':
1310 // If we are inside a preprocessor directive and we see the end of line,
1311 // we know we are done with the directive, so return an EOM token.
1312 if (ParsingPreprocessorDirective) {
1313 // Done parsing the "line".
1314 ParsingPreprocessorDirective = false;
1315
1316 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner1c1bed12008-10-12 03:27:19 +00001317 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner4b009652007-07-25 00:24:17 +00001318
1319 // Since we consumed a newline, we are back at the start of a line.
1320 IsAtStartOfLine = true;
1321
Chris Lattner0344cc72008-10-12 04:51:35 +00001322 Kind = tok::eom;
Chris Lattner4b009652007-07-25 00:24:17 +00001323 break;
1324 }
1325 // The returned token is at the start of the line.
1326 Result.setFlag(Token::StartOfLine);
1327 // No leading whitespace seen so far.
1328 Result.clearFlag(Token::LeadingSpace);
Chris Lattner867a87b2008-10-12 04:05:48 +00001329
1330 if (SkipWhitespace(Result, CurPtr))
1331 return; // KeepWhitespaceMode
Chris Lattner4b009652007-07-25 00:24:17 +00001332 goto LexNextToken; // GCC isn't tail call eliminating.
1333 case ' ':
1334 case '\t':
1335 case '\f':
1336 case '\v':
1337 SkipHorizontalWhitespace:
1338 Result.setFlag(Token::LeadingSpace);
Chris Lattner867a87b2008-10-12 04:05:48 +00001339 if (SkipWhitespace(Result, CurPtr))
1340 return; // KeepWhitespaceMode
Chris Lattner4b009652007-07-25 00:24:17 +00001341
1342 SkipIgnoredUnits:
1343 CurPtr = BufferPtr;
1344
1345 // If the next token is obviously a // or /* */ comment, skip it efficiently
1346 // too (without going through the big switch stmt).
Chris Lattner43e455d2009-01-16 22:39:25 +00001347 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1348 Features.BCPLComment) {
Chris Lattner4b009652007-07-25 00:24:17 +00001349 SkipBCPLComment(Result, CurPtr+2);
1350 goto SkipIgnoredUnits;
Chris Lattner170adb12008-10-12 03:22:02 +00001351 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001352 SkipBlockComment(Result, CurPtr+2);
1353 goto SkipIgnoredUnits;
1354 } else if (isHorizontalWhitespace(*CurPtr)) {
1355 goto SkipHorizontalWhitespace;
1356 }
1357 goto LexNextToken; // GCC isn't tail call eliminating.
1358
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001359 // C99 6.4.4.1: Integer Constants.
1360 // C99 6.4.4.2: Floating Constants.
1361 case '0': case '1': case '2': case '3': case '4':
1362 case '5': case '6': case '7': case '8': case '9':
1363 // Notify MIOpt that we read a non-whitespace/non-comment token.
1364 MIOpt.ReadToken();
1365 return LexNumericConstant(Result, CurPtr);
1366
1367 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Chris Lattner4b009652007-07-25 00:24:17 +00001368 // Notify MIOpt that we read a non-whitespace/non-comment token.
1369 MIOpt.ReadToken();
1370 Char = getCharAndSize(CurPtr, SizeTmp);
1371
1372 // Wide string literal.
1373 if (Char == '"')
1374 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1375 true);
1376
1377 // Wide character constant.
1378 if (Char == '\'')
1379 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1380 // FALL THROUGH, treating L like the start of an identifier.
1381
1382 // C99 6.4.2: Identifiers.
1383 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1384 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1385 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1386 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1387 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1388 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1389 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1390 case 'v': case 'w': case 'x': case 'y': case 'z':
1391 case '_':
1392 // Notify MIOpt that we read a non-whitespace/non-comment token.
1393 MIOpt.ReadToken();
1394 return LexIdentifier(Result, CurPtr);
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001395
1396 case '$': // $ in identifiers.
1397 if (Features.DollarIdents) {
Chris Lattnerf9c62772008-11-22 02:02:22 +00001398 if (!isLexingRawMode())
1399 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001400 // Notify MIOpt that we read a non-whitespace/non-comment token.
1401 MIOpt.ReadToken();
1402 return LexIdentifier(Result, CurPtr);
1403 }
Chris Lattner4b009652007-07-25 00:24:17 +00001404
Chris Lattner0344cc72008-10-12 04:51:35 +00001405 Kind = tok::unknown;
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001406 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001407
1408 // C99 6.4.4: Character Constants.
1409 case '\'':
1410 // Notify MIOpt that we read a non-whitespace/non-comment token.
1411 MIOpt.ReadToken();
1412 return LexCharConstant(Result, CurPtr);
1413
1414 // C99 6.4.5: String Literals.
1415 case '"':
1416 // Notify MIOpt that we read a non-whitespace/non-comment token.
1417 MIOpt.ReadToken();
1418 return LexStringLiteral(Result, CurPtr, false);
1419
1420 // C99 6.4.6: Punctuators.
1421 case '?':
Chris Lattner0344cc72008-10-12 04:51:35 +00001422 Kind = tok::question;
Chris Lattner4b009652007-07-25 00:24:17 +00001423 break;
1424 case '[':
Chris Lattner0344cc72008-10-12 04:51:35 +00001425 Kind = tok::l_square;
Chris Lattner4b009652007-07-25 00:24:17 +00001426 break;
1427 case ']':
Chris Lattner0344cc72008-10-12 04:51:35 +00001428 Kind = tok::r_square;
Chris Lattner4b009652007-07-25 00:24:17 +00001429 break;
1430 case '(':
Chris Lattner0344cc72008-10-12 04:51:35 +00001431 Kind = tok::l_paren;
Chris Lattner4b009652007-07-25 00:24:17 +00001432 break;
1433 case ')':
Chris Lattner0344cc72008-10-12 04:51:35 +00001434 Kind = tok::r_paren;
Chris Lattner4b009652007-07-25 00:24:17 +00001435 break;
1436 case '{':
Chris Lattner0344cc72008-10-12 04:51:35 +00001437 Kind = tok::l_brace;
Chris Lattner4b009652007-07-25 00:24:17 +00001438 break;
1439 case '}':
Chris Lattner0344cc72008-10-12 04:51:35 +00001440 Kind = tok::r_brace;
Chris Lattner4b009652007-07-25 00:24:17 +00001441 break;
1442 case '.':
1443 Char = getCharAndSize(CurPtr, SizeTmp);
1444 if (Char >= '0' && Char <= '9') {
1445 // Notify MIOpt that we read a non-whitespace/non-comment token.
1446 MIOpt.ReadToken();
1447
1448 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1449 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001450 Kind = tok::periodstar;
Chris Lattner4b009652007-07-25 00:24:17 +00001451 CurPtr += SizeTmp;
1452 } else if (Char == '.' &&
1453 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001454 Kind = tok::ellipsis;
Chris Lattner4b009652007-07-25 00:24:17 +00001455 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1456 SizeTmp2, Result);
1457 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001458 Kind = tok::period;
Chris Lattner4b009652007-07-25 00:24:17 +00001459 }
1460 break;
1461 case '&':
1462 Char = getCharAndSize(CurPtr, SizeTmp);
1463 if (Char == '&') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001464 Kind = tok::ampamp;
Chris Lattner4b009652007-07-25 00:24:17 +00001465 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1466 } else if (Char == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001467 Kind = tok::ampequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001468 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1469 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001470 Kind = tok::amp;
Chris Lattner4b009652007-07-25 00:24:17 +00001471 }
1472 break;
1473 case '*':
1474 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001475 Kind = tok::starequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001476 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1477 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001478 Kind = tok::star;
Chris Lattner4b009652007-07-25 00:24:17 +00001479 }
1480 break;
1481 case '+':
1482 Char = getCharAndSize(CurPtr, SizeTmp);
1483 if (Char == '+') {
Chris Lattner4b009652007-07-25 00:24:17 +00001484 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001485 Kind = tok::plusplus;
Chris Lattner4b009652007-07-25 00:24:17 +00001486 } else if (Char == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001487 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001488 Kind = tok::plusequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001489 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001490 Kind = tok::plus;
Chris Lattner4b009652007-07-25 00:24:17 +00001491 }
1492 break;
1493 case '-':
1494 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattner0344cc72008-10-12 04:51:35 +00001495 if (Char == '-') { // --
Chris Lattner4b009652007-07-25 00:24:17 +00001496 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001497 Kind = tok::minusminus;
Chris Lattner4b009652007-07-25 00:24:17 +00001498 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattner0344cc72008-10-12 04:51:35 +00001499 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Chris Lattner4b009652007-07-25 00:24:17 +00001500 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1501 SizeTmp2, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001502 Kind = tok::arrowstar;
1503 } else if (Char == '>') { // ->
Chris Lattner4b009652007-07-25 00:24:17 +00001504 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001505 Kind = tok::arrow;
1506 } else if (Char == '=') { // -=
Chris Lattner4b009652007-07-25 00:24:17 +00001507 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001508 Kind = tok::minusequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001509 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001510 Kind = tok::minus;
Chris Lattner4b009652007-07-25 00:24:17 +00001511 }
1512 break;
1513 case '~':
Chris Lattner0344cc72008-10-12 04:51:35 +00001514 Kind = tok::tilde;
Chris Lattner4b009652007-07-25 00:24:17 +00001515 break;
1516 case '!':
1517 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001518 Kind = tok::exclaimequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001519 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1520 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001521 Kind = tok::exclaim;
Chris Lattner4b009652007-07-25 00:24:17 +00001522 }
1523 break;
1524 case '/':
1525 // 6.4.9: Comments
1526 Char = getCharAndSize(CurPtr, SizeTmp);
1527 if (Char == '/') { // BCPL comment.
Chris Lattner43e455d2009-01-16 22:39:25 +00001528 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
1529 // want to lex this as a comment. There is one problem with this though,
1530 // that in one particular corner case, this can change the behavior of the
1531 // resultant program. For example, In "foo //**/ bar", C89 would lex
1532 // this as "foo / bar" and langauges with BCPL comments would lex it as
1533 // "foo". Check to see if the character after the second slash is a '*'.
1534 // If so, we will lex that as a "/" instead of the start of a comment.
1535 if (Features.BCPLComment ||
1536 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
1537 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1538 return; // KeepCommentMode
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001539
Chris Lattner43e455d2009-01-16 22:39:25 +00001540 // It is common for the tokens immediately after a // comment to be
1541 // whitespace (indentation for the next line). Instead of going through
1542 // the big switch, handle it efficiently now.
1543 goto SkipIgnoredUnits;
1544 }
1545 }
1546
1547 if (Char == '*') { // /**/ comment.
Chris Lattner4b009652007-07-25 00:24:17 +00001548 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001549 return; // KeepCommentMode
1550 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner43e455d2009-01-16 22:39:25 +00001551 }
1552
1553 if (Char == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001554 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001555 Kind = tok::slashequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001556 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001557 Kind = tok::slash;
Chris Lattner4b009652007-07-25 00:24:17 +00001558 }
1559 break;
1560 case '%':
1561 Char = getCharAndSize(CurPtr, SizeTmp);
1562 if (Char == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001563 Kind = tok::percentequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001564 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1565 } else if (Features.Digraphs && Char == '>') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001566 Kind = tok::r_brace; // '%>' -> '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001567 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1568 } else if (Features.Digraphs && Char == ':') {
1569 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1570 Char = getCharAndSize(CurPtr, SizeTmp);
1571 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001572 Kind = tok::hashhash; // '%:%:' -> '##'
Chris Lattner4b009652007-07-25 00:24:17 +00001573 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1574 SizeTmp2, Result);
1575 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner4b009652007-07-25 00:24:17 +00001576 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerf9c62772008-11-22 02:02:22 +00001577 if (!isLexingRawMode())
1578 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner0344cc72008-10-12 04:51:35 +00001579 Kind = tok::hashat;
Chris Lattner4b009652007-07-25 00:24:17 +00001580 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001581 Kind = tok::hash; // '%:' -> '#'
Chris Lattner4b009652007-07-25 00:24:17 +00001582
1583 // We parsed a # character. If this occurs at the start of the line,
1584 // it's actually the start of a preprocessing directive. Callback to
1585 // the preprocessor to handle it.
1586 // FIXME: -fpreprocessed mode??
1587 if (Result.isAtStartOfLine() && !LexingRawMode) {
1588 BufferPtr = CurPtr;
Chris Lattner342dccb2007-10-17 20:41:00 +00001589 PP->HandleDirective(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001590
1591 // As an optimization, if the preprocessor didn't switch lexers, tail
1592 // recurse.
Chris Lattner342dccb2007-10-17 20:41:00 +00001593 if (PP->isCurrentLexer(this)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001594 // Start a new token. If this is a #include or something, the PP may
1595 // want us starting at the beginning of the line again. If so, set
1596 // the StartOfLine flag.
1597 if (IsAtStartOfLine) {
1598 Result.setFlag(Token::StartOfLine);
1599 IsAtStartOfLine = false;
1600 }
1601 goto LexNextToken; // GCC isn't tail call eliminating.
1602 }
1603
Chris Lattner342dccb2007-10-17 20:41:00 +00001604 return PP->Lex(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001605 }
1606 }
1607 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001608 Kind = tok::percent;
Chris Lattner4b009652007-07-25 00:24:17 +00001609 }
1610 break;
1611 case '<':
1612 Char = getCharAndSize(CurPtr, SizeTmp);
1613 if (ParsingFilename) {
1614 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1615 } else if (Char == '<' &&
1616 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001617 Kind = tok::lesslessequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001618 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1619 SizeTmp2, Result);
1620 } else if (Char == '<') {
Chris Lattner4b009652007-07-25 00:24:17 +00001621 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001622 Kind = tok::lessless;
Chris Lattner4b009652007-07-25 00:24:17 +00001623 } else if (Char == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001624 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001625 Kind = tok::lessequal;
1626 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Chris Lattner4b009652007-07-25 00:24:17 +00001627 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001628 Kind = tok::l_square;
1629 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Chris Lattner4b009652007-07-25 00:24:17 +00001630 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001631 Kind = tok::l_brace;
Chris Lattner4b009652007-07-25 00:24:17 +00001632 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001633 Kind = tok::less;
Chris Lattner4b009652007-07-25 00:24:17 +00001634 }
1635 break;
1636 case '>':
1637 Char = getCharAndSize(CurPtr, SizeTmp);
1638 if (Char == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001639 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001640 Kind = tok::greaterequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001641 } else if (Char == '>' &&
1642 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001643 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1644 SizeTmp2, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001645 Kind = tok::greatergreaterequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001646 } else if (Char == '>') {
Chris Lattner4b009652007-07-25 00:24:17 +00001647 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001648 Kind = tok::greatergreater;
Chris Lattner4b009652007-07-25 00:24:17 +00001649 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001650 Kind = tok::greater;
Chris Lattner4b009652007-07-25 00:24:17 +00001651 }
1652 break;
1653 case '^':
1654 Char = getCharAndSize(CurPtr, SizeTmp);
1655 if (Char == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001656 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001657 Kind = tok::caretequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001658 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001659 Kind = tok::caret;
Chris Lattner4b009652007-07-25 00:24:17 +00001660 }
1661 break;
1662 case '|':
1663 Char = getCharAndSize(CurPtr, SizeTmp);
1664 if (Char == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001665 Kind = tok::pipeequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001666 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1667 } else if (Char == '|') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001668 Kind = tok::pipepipe;
Chris Lattner4b009652007-07-25 00:24:17 +00001669 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1670 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001671 Kind = tok::pipe;
Chris Lattner4b009652007-07-25 00:24:17 +00001672 }
1673 break;
1674 case ':':
1675 Char = getCharAndSize(CurPtr, SizeTmp);
1676 if (Features.Digraphs && Char == '>') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001677 Kind = tok::r_square; // ':>' -> ']'
Chris Lattner4b009652007-07-25 00:24:17 +00001678 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1679 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001680 Kind = tok::coloncolon;
Chris Lattner4b009652007-07-25 00:24:17 +00001681 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1682 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001683 Kind = tok::colon;
Chris Lattner4b009652007-07-25 00:24:17 +00001684 }
1685 break;
1686 case ';':
Chris Lattner0344cc72008-10-12 04:51:35 +00001687 Kind = tok::semi;
Chris Lattner4b009652007-07-25 00:24:17 +00001688 break;
1689 case '=':
1690 Char = getCharAndSize(CurPtr, SizeTmp);
1691 if (Char == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001692 Kind = tok::equalequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001693 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1694 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001695 Kind = tok::equal;
Chris Lattner4b009652007-07-25 00:24:17 +00001696 }
1697 break;
1698 case ',':
Chris Lattner0344cc72008-10-12 04:51:35 +00001699 Kind = tok::comma;
Chris Lattner4b009652007-07-25 00:24:17 +00001700 break;
1701 case '#':
1702 Char = getCharAndSize(CurPtr, SizeTmp);
1703 if (Char == '#') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001704 Kind = tok::hashhash;
Chris Lattner4b009652007-07-25 00:24:17 +00001705 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1706 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner0344cc72008-10-12 04:51:35 +00001707 Kind = tok::hashat;
Chris Lattnerf9c62772008-11-22 02:02:22 +00001708 if (!isLexingRawMode())
1709 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner4b009652007-07-25 00:24:17 +00001710 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1711 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001712 Kind = tok::hash;
Chris Lattner4b009652007-07-25 00:24:17 +00001713 // We parsed a # character. If this occurs at the start of the line,
1714 // it's actually the start of a preprocessing directive. Callback to
1715 // the preprocessor to handle it.
1716 // FIXME: -fpreprocessed mode??
1717 if (Result.isAtStartOfLine() && !LexingRawMode) {
1718 BufferPtr = CurPtr;
Chris Lattner342dccb2007-10-17 20:41:00 +00001719 PP->HandleDirective(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001720
1721 // As an optimization, if the preprocessor didn't switch lexers, tail
1722 // recurse.
Chris Lattner342dccb2007-10-17 20:41:00 +00001723 if (PP->isCurrentLexer(this)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001724 // Start a new token. If this is a #include or something, the PP may
1725 // want us starting at the beginning of the line again. If so, set
1726 // the StartOfLine flag.
1727 if (IsAtStartOfLine) {
1728 Result.setFlag(Token::StartOfLine);
1729 IsAtStartOfLine = false;
1730 }
1731 goto LexNextToken; // GCC isn't tail call eliminating.
1732 }
Chris Lattner342dccb2007-10-17 20:41:00 +00001733 return PP->Lex(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001734 }
1735 }
1736 break;
1737
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001738 case '@':
1739 // Objective C support.
1740 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattner0344cc72008-10-12 04:51:35 +00001741 Kind = tok::at;
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001742 else
Chris Lattner0344cc72008-10-12 04:51:35 +00001743 Kind = tok::unknown;
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001744 break;
1745
Chris Lattner4b009652007-07-25 00:24:17 +00001746 case '\\':
1747 // FIXME: UCN's.
1748 // FALL THROUGH.
1749 default:
Chris Lattner0344cc72008-10-12 04:51:35 +00001750 Kind = tok::unknown;
Chris Lattner4b009652007-07-25 00:24:17 +00001751 break;
1752 }
1753
1754 // Notify MIOpt that we read a non-whitespace/non-comment token.
1755 MIOpt.ReadToken();
1756
1757 // Update the location of token as well as BufferPtr.
Chris Lattner0344cc72008-10-12 04:51:35 +00001758 FormTokenWithChars(Result, CurPtr, Kind);
Chris Lattner4b009652007-07-25 00:24:17 +00001759}