blob: 7f14e7a4a2df20301e8d990afea2df10169ed238 [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 Lattnercdf600e2009-01-16 07:00:02 +0000324 SourceLocation SpellingLoc = SourceMgr.getSpellingLoc(FileLoc);
Chris Lattnere805eac2009-01-19 06:46:35 +0000325 SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
Chris Lattnerfcc20e02009-01-26 20:15:46 +0000326 return SourceMgr.createInstantiationLoc(SpellingLoc, FileLoc, TokLen);
Chris Lattner4b009652007-07-25 00:24:17 +0000327}
328
329/// getSourceLocation - Return a source location identifier for the specified
330/// offset in the current file.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000331SourceLocation Lexer::getSourceLocation(const char *Loc,
332 unsigned TokLen) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000333 assert(Loc >= BufferStart && Loc <= BufferEnd &&
334 "Location out of range for this buffer!");
335
336 // In the normal case, we're just lexing from a simple file buffer, return
337 // the file id from FileLoc with the offset specified.
338 unsigned CharNo = Loc-BufferStart;
339 if (FileLoc.isFileID())
Chris Lattnere805eac2009-01-19 06:46:35 +0000340 return FileLoc.getFileLocWithOffset(CharNo);
Chris Lattner4b009652007-07-25 00:24:17 +0000341
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000342 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
343 // tokens are lexed from where the _Pragma was defined.
Chris Lattner342dccb2007-10-17 20:41:00 +0000344 assert(PP && "This doesn't work on raw lexers");
Chris Lattner27c0ced2009-01-26 00:43:02 +0000345 return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
Chris Lattner4b009652007-07-25 00:24:17 +0000346}
347
348/// Diag - Forwarding function for diagnostics. This translate a source
349/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner9943e982008-11-22 00:59:29 +0000350DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner0370d6b2008-11-18 07:59:24 +0000351 return PP->Diag(getSourceLocation(Loc), DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000352}
Chris Lattner4b009652007-07-25 00:24:17 +0000353
354//===----------------------------------------------------------------------===//
355// Trigraph and Escaped Newline Handling Code.
356//===----------------------------------------------------------------------===//
357
358/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
359/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
360static char GetTrigraphCharForLetter(char Letter) {
361 switch (Letter) {
362 default: return 0;
363 case '=': return '#';
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 }
373}
374
375/// DecodeTrigraphChar - If the specified character is a legal trigraph when
376/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
377/// return the result character. Finally, emit a warning about trigraph use
378/// whether trigraphs are enabled or not.
379static char DecodeTrigraphChar(const char *CP, Lexer *L) {
380 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner0370d6b2008-11-18 07:59:24 +0000381 if (!Res || !L) return Res;
382
383 if (!L->getFeatures().Trigraphs) {
Chris Lattnerf9c62772008-11-22 02:02:22 +0000384 if (!L->isLexingRawMode())
385 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner0370d6b2008-11-18 07:59:24 +0000386 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000387 }
Chris Lattner0370d6b2008-11-18 07:59:24 +0000388
Chris Lattnerf9c62772008-11-22 02:02:22 +0000389 if (!L->isLexingRawMode())
390 L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
Chris Lattner4b009652007-07-25 00:24:17 +0000391 return Res;
392}
393
394/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
395/// get its size, and return it. This is tricky in several cases:
396/// 1. If currently at the start of a trigraph, we warn about the trigraph,
397/// then either return the trigraph (skipping 3 chars) or the '?',
398/// depending on whether trigraphs are enabled or not.
399/// 2. If this is an escaped newline (potentially with whitespace between
400/// the backslash and newline), implicitly skip the newline and return
401/// the char after it.
402/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
403///
404/// This handles the slow/uncommon case of the getCharAndSize method. Here we
405/// know that we can accumulate into Size, and that we have already incremented
406/// Ptr by Size bytes.
407///
408/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
409/// be updated to match.
410///
411char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
412 Token *Tok) {
413 // If we have a slash, look for an escaped newline.
414 if (Ptr[0] == '\\') {
415 ++Size;
416 ++Ptr;
417Slash:
418 // Common case, backslash-char where the char is not whitespace.
419 if (!isWhitespace(Ptr[0])) return '\\';
420
421 // See if we have optional whitespace characters followed by a newline.
422 {
423 unsigned SizeTmp = 0;
424 do {
425 ++SizeTmp;
426 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
427 // Remember that this token needs to be cleaned.
428 if (Tok) Tok->setFlag(Token::NeedsCleaning);
429
430 // Warn if there was whitespace between the backslash and newline.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000431 if (SizeTmp != 1 && Tok && !isLexingRawMode())
Chris Lattner4b009652007-07-25 00:24:17 +0000432 Diag(Ptr, diag::backslash_newline_space);
433
434 // If this is a \r\n or \n\r, skip the newlines.
435 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
436 Ptr[SizeTmp-1] != Ptr[SizeTmp])
437 ++SizeTmp;
438
439 // Found backslash<whitespace><newline>. Parse the char after it.
440 Size += SizeTmp;
441 Ptr += SizeTmp;
442 // Use slow version to accumulate a correct size field.
443 return getCharAndSizeSlow(Ptr, Size, Tok);
444 }
445 } while (isWhitespace(Ptr[SizeTmp]));
446 }
447
448 // Otherwise, this is not an escaped newline, just return the slash.
449 return '\\';
450 }
451
452 // If this is a trigraph, process it.
453 if (Ptr[0] == '?' && Ptr[1] == '?') {
454 // If this is actually a legal trigraph (not something like "??x"), emit
455 // a trigraph warning. If so, and if trigraphs are enabled, return it.
456 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
457 // Remember that this token needs to be cleaned.
458 if (Tok) Tok->setFlag(Token::NeedsCleaning);
459
460 Ptr += 3;
461 Size += 3;
462 if (C == '\\') goto Slash;
463 return C;
464 }
465 }
466
467 // If this is neither, return a single character.
468 ++Size;
469 return *Ptr;
470}
471
472
473/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
474/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
475/// and that we have already incremented Ptr by Size bytes.
476///
477/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
478/// be updated to match.
479char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
480 const LangOptions &Features) {
481 // If we have a slash, look for an escaped newline.
482 if (Ptr[0] == '\\') {
483 ++Size;
484 ++Ptr;
485Slash:
486 // Common case, backslash-char where the char is not whitespace.
487 if (!isWhitespace(Ptr[0])) return '\\';
488
489 // See if we have optional whitespace characters followed by a newline.
490 {
491 unsigned SizeTmp = 0;
492 do {
493 ++SizeTmp;
494 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
495
496 // If this is a \r\n or \n\r, skip the newlines.
497 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
498 Ptr[SizeTmp-1] != Ptr[SizeTmp])
499 ++SizeTmp;
500
501 // Found backslash<whitespace><newline>. Parse the char after it.
502 Size += SizeTmp;
503 Ptr += SizeTmp;
504
505 // Use slow version to accumulate a correct size field.
506 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
507 }
508 } while (isWhitespace(Ptr[SizeTmp]));
509 }
510
511 // Otherwise, this is not an escaped newline, just return the slash.
512 return '\\';
513 }
514
515 // If this is a trigraph, process it.
516 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
517 // If this is actually a legal trigraph (not something like "??x"), return
518 // it.
519 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
520 Ptr += 3;
521 Size += 3;
522 if (C == '\\') goto Slash;
523 return C;
524 }
525 }
526
527 // If this is neither, return a single character.
528 ++Size;
529 return *Ptr;
530}
531
532//===----------------------------------------------------------------------===//
533// Helper methods for lexing.
534//===----------------------------------------------------------------------===//
535
536void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
537 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
538 unsigned Size;
539 unsigned char C = *CurPtr++;
540 while (isIdentifierBody(C)) {
541 C = *CurPtr++;
542 }
543 --CurPtr; // Back up over the skipped character.
544
545 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
546 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
547 // FIXME: UCNs.
548 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
549FinishIdentifier:
550 const char *IdStart = BufferPtr;
Chris Lattner0344cc72008-10-12 04:51:35 +0000551 FormTokenWithChars(Result, CurPtr, tok::identifier);
Chris Lattner4b009652007-07-25 00:24:17 +0000552
553 // If we are in raw mode, return this identifier raw. There is no need to
554 // look up identifier information or attempt to macro expand it.
555 if (LexingRawMode) return;
556
557 // Fill in Result.IdentifierInfo, looking up the identifier in the
558 // identifier table.
Chris Lattner2f93c3d2009-01-21 07:45:14 +0000559 IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
Chris Lattner4b009652007-07-25 00:24:17 +0000560
Chris Lattner9ac3dd92009-01-23 18:35:48 +0000561 // Change the kind of this identifier to the appropriate token kind, e.g.
562 // turning "for" into a keyword.
563 Result.setKind(II->getTokenID());
564
Chris Lattner4b009652007-07-25 00:24:17 +0000565 // Finally, now that we know we have an identifier, pass this off to the
566 // preprocessor, which may macro expand it or something.
Chris Lattner2f93c3d2009-01-21 07:45:14 +0000567 if (II->isHandleIdentifierCase())
Chris Lattner5b747d02009-01-21 07:43:11 +0000568 PP->HandleIdentifier(Result);
569 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000570 }
571
572 // Otherwise, $,\,? in identifier found. Enter slower path.
573
574 C = getCharAndSize(CurPtr, Size);
575 while (1) {
576 if (C == '$') {
577 // If we hit a $ and they are not supported in identifiers, we are done.
578 if (!Features.DollarIdents) goto FinishIdentifier;
579
580 // Otherwise, emit a diagnostic and continue.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000581 if (!isLexingRawMode())
582 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner4b009652007-07-25 00:24:17 +0000583 CurPtr = ConsumeChar(CurPtr, Size, Result);
584 C = getCharAndSize(CurPtr, Size);
585 continue;
586 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
587 // Found end of identifier.
588 goto FinishIdentifier;
589 }
590
591 // Otherwise, this character is good, consume it.
592 CurPtr = ConsumeChar(CurPtr, Size, Result);
593
594 C = getCharAndSize(CurPtr, Size);
595 while (isIdentifierBody(C)) { // FIXME: UCNs.
596 CurPtr = ConsumeChar(CurPtr, Size, Result);
597 C = getCharAndSize(CurPtr, Size);
598 }
599 }
600}
601
602
Nate Begeman937cac72008-04-14 02:26:39 +0000603/// LexNumericConstant - Lex the remainder of a integer or floating point
Chris Lattner4b009652007-07-25 00:24:17 +0000604/// constant. From[-1] is the first character lexed. Return the end of the
605/// constant.
606void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
607 unsigned Size;
608 char C = getCharAndSize(CurPtr, Size);
609 char PrevCh = 0;
610 while (isNumberBody(C)) { // FIXME: UCNs?
611 CurPtr = ConsumeChar(CurPtr, Size, Result);
612 PrevCh = C;
613 C = getCharAndSize(CurPtr, Size);
614 }
615
616 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
617 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
618 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
619
620 // If we have a hex FP constant, continue.
Chris Lattner9ba63822008-11-22 07:39:03 +0000621 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
622 (Features.HexFloats || !Features.NoExtensions))
Chris Lattner4b009652007-07-25 00:24:17 +0000623 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
624
Chris Lattner4b009652007-07-25 00:24:17 +0000625 // Update the location of token as well as BufferPtr.
Chris Lattner6ad1f502009-01-26 19:29:26 +0000626 const char *TokStart = BufferPtr;
Chris Lattner0344cc72008-10-12 04:51:35 +0000627 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Chris Lattner6ad1f502009-01-26 19:29:26 +0000628 Result.setLiteralData(TokStart);
Chris Lattner4b009652007-07-25 00:24:17 +0000629}
630
631/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
632/// either " or L".
Chris Lattner867a87b2008-10-12 04:05:48 +0000633void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Chris Lattner4b009652007-07-25 00:24:17 +0000634 const char *NulCharacter = 0; // Does this string contain the \0 character?
635
636 char C = getAndAdvanceChar(CurPtr, Result);
637 while (C != '"') {
638 // Skip escaped characters.
639 if (C == '\\') {
640 // Skip the escaped character.
641 C = getAndAdvanceChar(CurPtr, Result);
642 } else if (C == '\n' || C == '\r' || // Newline.
643 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000644 if (!isLexingRawMode())
645 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner0344cc72008-10-12 04:51:35 +0000646 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner4b009652007-07-25 00:24:17 +0000647 return;
648 } else if (C == 0) {
649 NulCharacter = CurPtr-1;
650 }
651 C = getAndAdvanceChar(CurPtr, Result);
652 }
653
654 // If a nul character existed in the string, warn about it.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000655 if (NulCharacter && !isLexingRawMode())
656 Diag(NulCharacter, diag::null_in_string);
Chris Lattner4b009652007-07-25 00:24:17 +0000657
Chris Lattner4b009652007-07-25 00:24:17 +0000658 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner6ad1f502009-01-26 19:29:26 +0000659 const char *TokStart = BufferPtr;
Chris Lattner0344cc72008-10-12 04:51:35 +0000660 FormTokenWithChars(Result, CurPtr,
661 Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner6ad1f502009-01-26 19:29:26 +0000662 Result.setLiteralData(TokStart);
Chris Lattner4b009652007-07-25 00:24:17 +0000663}
664
665/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
666/// after having lexed the '<' character. This is used for #include filenames.
667void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
668 const char *NulCharacter = 0; // Does this string contain the \0 character?
669
670 char C = getAndAdvanceChar(CurPtr, Result);
671 while (C != '>') {
672 // Skip escaped characters.
673 if (C == '\\') {
674 // Skip the escaped character.
675 C = getAndAdvanceChar(CurPtr, Result);
676 } else if (C == '\n' || C == '\r' || // Newline.
677 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000678 if (!isLexingRawMode())
679 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner0344cc72008-10-12 04:51:35 +0000680 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner4b009652007-07-25 00:24:17 +0000681 return;
682 } else if (C == 0) {
683 NulCharacter = CurPtr-1;
684 }
685 C = getAndAdvanceChar(CurPtr, Result);
686 }
687
688 // If a nul character existed in the string, warn about it.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000689 if (NulCharacter && !isLexingRawMode())
690 Diag(NulCharacter, diag::null_in_string);
Chris Lattner4b009652007-07-25 00:24:17 +0000691
Chris Lattner4b009652007-07-25 00:24:17 +0000692 // Update the location of token as well as BufferPtr.
Chris Lattner6ad1f502009-01-26 19:29:26 +0000693 const char *TokStart = BufferPtr;
Chris Lattner0344cc72008-10-12 04:51:35 +0000694 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Chris Lattner6ad1f502009-01-26 19:29:26 +0000695 Result.setLiteralData(TokStart);
Chris Lattner4b009652007-07-25 00:24:17 +0000696}
697
698
699/// LexCharConstant - Lex the remainder of a character constant, after having
700/// lexed either ' or L'.
701void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
702 const char *NulCharacter = 0; // Does this character contain the \0 character?
703
704 // Handle the common case of 'x' and '\y' efficiently.
705 char C = getAndAdvanceChar(CurPtr, Result);
706 if (C == '\'') {
Chris Lattnerf9c62772008-11-22 02:02:22 +0000707 if (!isLexingRawMode())
708 Diag(BufferPtr, diag::err_empty_character);
Chris Lattner0344cc72008-10-12 04:51:35 +0000709 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4b009652007-07-25 00:24:17 +0000710 return;
711 } else if (C == '\\') {
712 // Skip the escaped character.
713 // FIXME: UCN's.
714 C = getAndAdvanceChar(CurPtr, Result);
715 }
716
717 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
718 ++CurPtr;
719 } else {
720 // Fall back on generic code for embedded nulls, newlines, wide chars.
721 do {
722 // Skip escaped characters.
723 if (C == '\\') {
724 // Skip the escaped character.
725 C = getAndAdvanceChar(CurPtr, Result);
726 } else if (C == '\n' || C == '\r' || // Newline.
727 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000728 if (!isLexingRawMode())
729 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner0344cc72008-10-12 04:51:35 +0000730 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner4b009652007-07-25 00:24:17 +0000731 return;
732 } else if (C == 0) {
733 NulCharacter = CurPtr-1;
734 }
735 C = getAndAdvanceChar(CurPtr, Result);
736 } while (C != '\'');
737 }
738
Chris Lattnerf9c62772008-11-22 02:02:22 +0000739 if (NulCharacter && !isLexingRawMode())
740 Diag(NulCharacter, diag::null_in_char);
Chris Lattner4b009652007-07-25 00:24:17 +0000741
Chris Lattner4b009652007-07-25 00:24:17 +0000742 // Update the location of token as well as BufferPtr.
Chris Lattner6ad1f502009-01-26 19:29:26 +0000743 const char *TokStart = BufferPtr;
Chris Lattner0344cc72008-10-12 04:51:35 +0000744 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Chris Lattner6ad1f502009-01-26 19:29:26 +0000745 Result.setLiteralData(TokStart);
Chris Lattner4b009652007-07-25 00:24:17 +0000746}
747
748/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
749/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattner867a87b2008-10-12 04:05:48 +0000750///
751/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
752///
753bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Chris Lattner4b009652007-07-25 00:24:17 +0000754 // Whitespace - Skip it, then return the token after the whitespace.
755 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
756 while (1) {
757 // Skip horizontal whitespace very aggressively.
758 while (isHorizontalWhitespace(Char))
759 Char = *++CurPtr;
760
Daniel Dunbara2208392008-11-25 00:20:22 +0000761 // Otherwise if we have something other than whitespace, we're done.
Chris Lattner4b009652007-07-25 00:24:17 +0000762 if (Char != '\n' && Char != '\r')
763 break;
764
765 if (ParsingPreprocessorDirective) {
766 // End of preprocessor directive line, let LexTokenInternal handle this.
767 BufferPtr = CurPtr;
Chris Lattner867a87b2008-10-12 04:05:48 +0000768 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000769 }
770
771 // ok, but handle newline.
772 // The returned token is at the start of the line.
773 Result.setFlag(Token::StartOfLine);
774 // No leading whitespace seen so far.
775 Result.clearFlag(Token::LeadingSpace);
776 Char = *++CurPtr;
777 }
778
779 // If this isn't immediately after a newline, there is leading space.
780 char PrevChar = CurPtr[-1];
781 if (PrevChar != '\n' && PrevChar != '\r')
782 Result.setFlag(Token::LeadingSpace);
783
Chris Lattner867a87b2008-10-12 04:05:48 +0000784 // If the client wants us to return whitespace, return it now.
785 if (isKeepWhitespaceMode()) {
Chris Lattner0344cc72008-10-12 04:51:35 +0000786 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner867a87b2008-10-12 04:05:48 +0000787 return true;
788 }
789
Chris Lattner4b009652007-07-25 00:24:17 +0000790 BufferPtr = CurPtr;
Chris Lattner867a87b2008-10-12 04:05:48 +0000791 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000792}
793
794// SkipBCPLComment - We have just read the // characters from input. Skip until
795// we find the newline character thats terminate the comment. Then update
Chris Lattnerf03b00c2008-10-12 04:15:42 +0000796/// BufferPtr and return. If we're in KeepCommentMode, this will form the token
797/// and return true.
Chris Lattner4b009652007-07-25 00:24:17 +0000798bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
799 // If BCPL comments aren't explicitly enabled for this language, emit an
800 // extension warning.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000801 if (!Features.BCPLComment && !isLexingRawMode()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000802 Diag(BufferPtr, diag::ext_bcpl_comment);
803
804 // Mark them enabled so we only emit one warning for this translation
805 // unit.
806 Features.BCPLComment = true;
807 }
808
809 // Scan over the body of the comment. The common case, when scanning, is that
810 // the comment contains normal ascii characters with nothing interesting in
811 // them. As such, optimize for this case with the inner loop.
812 char C;
813 do {
814 C = *CurPtr;
815 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
816 // If we find a \n character, scan backwards, checking to see if it's an
817 // escaped newline, like we do for block comments.
818
819 // Skip over characters in the fast loop.
820 while (C != 0 && // Potentially EOF.
821 C != '\\' && // Potentially escaped newline.
822 C != '?' && // Potentially trigraph.
823 C != '\n' && C != '\r') // Newline or DOS-style newline.
824 C = *++CurPtr;
825
826 // If this is a newline, we're done.
827 if (C == '\n' || C == '\r')
828 break; // Found the newline? Break out!
829
830 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnerc3697802008-12-12 07:34:39 +0000831 // properly decode the character. Read it in raw mode to avoid emitting
832 // diagnostics about things like trigraphs. If we see an escaped newline,
833 // we'll handle it below.
Chris Lattner4b009652007-07-25 00:24:17 +0000834 const char *OldPtr = CurPtr;
Chris Lattnerc3697802008-12-12 07:34:39 +0000835 bool OldRawMode = isLexingRawMode();
836 LexingRawMode = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000837 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerc3697802008-12-12 07:34:39 +0000838 LexingRawMode = OldRawMode;
Chris Lattner4b009652007-07-25 00:24:17 +0000839
840 // If we read multiple characters, and one of those characters was a \r or
841 // \n, then we had an escaped newline within the comment. Emit diagnostic
842 // unless the next line is also a // comment.
843 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
844 for (; OldPtr != CurPtr; ++OldPtr)
845 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
846 // Okay, we found a // comment that ends in a newline, if the next
847 // line is also a // comment, but has spaces, don't emit a diagnostic.
848 if (isspace(C)) {
849 const char *ForwardPtr = CurPtr;
850 while (isspace(*ForwardPtr)) // Skip whitespace.
851 ++ForwardPtr;
852 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
853 break;
854 }
855
Chris Lattnerf9c62772008-11-22 02:02:22 +0000856 if (!isLexingRawMode())
857 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Chris Lattner4b009652007-07-25 00:24:17 +0000858 break;
859 }
860 }
861
862 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
863 } while (C != '\n' && C != '\r');
864
865 // Found but did not consume the newline.
866
867 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner170adb12008-10-12 03:22:02 +0000868 if (inKeepCommentMode())
Chris Lattner4b009652007-07-25 00:24:17 +0000869 return SaveBCPLComment(Result, CurPtr);
870
871 // If we are inside a preprocessor directive and we see the end of line,
872 // return immediately, so that the lexer can return this as an EOM token.
873 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
874 BufferPtr = CurPtr;
Chris Lattnerf03b00c2008-10-12 04:15:42 +0000875 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000876 }
877
878 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner43d38202008-10-12 00:23:07 +0000879 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattner867a87b2008-10-12 04:05:48 +0000880 // contribute to another token), it isn't needed for correctness. Note that
881 // this is ok even in KeepWhitespaceMode, because we would have returned the
882 /// comment above in that mode.
Chris Lattner4b009652007-07-25 00:24:17 +0000883 ++CurPtr;
884
885 // The next returned token is at the start of the line.
886 Result.setFlag(Token::StartOfLine);
887 // No leading whitespace seen so far.
888 Result.clearFlag(Token::LeadingSpace);
889 BufferPtr = CurPtr;
Chris Lattnerf03b00c2008-10-12 04:15:42 +0000890 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000891}
892
893/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
894/// an appropriate way and return it.
895bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner0344cc72008-10-12 04:51:35 +0000896 // If we're not in a preprocessor directive, just return the // comment
897 // directly.
898 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattner4b009652007-07-25 00:24:17 +0000899
Chris Lattner0344cc72008-10-12 04:51:35 +0000900 if (!ParsingPreprocessorDirective)
901 return true;
902
903 // If this BCPL-style comment is in a macro definition, transmogrify it into
904 // a C-style block comment.
905 std::string Spelling = PP->getSpelling(Result);
906 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
907 Spelling[1] = '*'; // Change prefix to "/*".
908 Spelling += "*/"; // add suffix.
909
910 Result.setKind(tok::comment);
Chris Lattner6ad1f502009-01-26 19:29:26 +0000911 PP->CreateString(&Spelling[0], Spelling.size(), Result,
912 Result.getLocation());
Chris Lattnerf03b00c2008-10-12 04:15:42 +0000913 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000914}
915
916/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
917/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattnerb3872872008-12-12 07:14:34 +0000918/// diagnostic if so. We know that the newline is inside of a block comment.
Chris Lattner4b009652007-07-25 00:24:17 +0000919static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
920 Lexer *L) {
921 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
922
923 // Back up off the newline.
924 --CurPtr;
925
926 // If this is a two-character newline sequence, skip the other character.
927 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
928 // \n\n or \r\r -> not escaped newline.
929 if (CurPtr[0] == CurPtr[1])
930 return false;
931 // \n\r or \r\n -> skip the newline.
932 --CurPtr;
933 }
934
935 // If we have horizontal whitespace, skip over it. We allow whitespace
936 // between the slash and newline.
937 bool HasSpace = false;
938 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
939 --CurPtr;
940 HasSpace = true;
941 }
942
943 // If we have a slash, we know this is an escaped newline.
944 if (*CurPtr == '\\') {
945 if (CurPtr[-1] != '*') return false;
946 } else {
947 // It isn't a slash, is it the ?? / trigraph?
948 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
949 CurPtr[-3] != '*')
950 return false;
951
952 // This is the trigraph ending the comment. Emit a stern warning!
953 CurPtr -= 2;
954
955 // If no trigraphs are enabled, warn that we ignored this trigraph and
956 // ignore this * character.
957 if (!L->getFeatures().Trigraphs) {
Chris Lattnerf9c62772008-11-22 02:02:22 +0000958 if (!L->isLexingRawMode())
959 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattner4b009652007-07-25 00:24:17 +0000960 return false;
961 }
Chris Lattnerf9c62772008-11-22 02:02:22 +0000962 if (!L->isLexingRawMode())
963 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner4b009652007-07-25 00:24:17 +0000964 }
965
966 // Warn about having an escaped newline between the */ characters.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000967 if (!L->isLexingRawMode())
968 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Chris Lattner4b009652007-07-25 00:24:17 +0000969
970 // If there was space between the backslash and newline, warn about it.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000971 if (HasSpace && !L->isLexingRawMode())
972 L->Diag(CurPtr, diag::backslash_newline_space);
Chris Lattner4b009652007-07-25 00:24:17 +0000973
974 return true;
975}
976
977#ifdef __SSE2__
978#include <emmintrin.h>
979#elif __ALTIVEC__
980#include <altivec.h>
981#undef bool
982#endif
983
984/// SkipBlockComment - We have just read the /* characters from input. Read
985/// until we find the */ characters that terminate the comment. Note that we
986/// don't bother decoding trigraphs or escaped newlines in block comments,
987/// because they cannot cause the comment to end. The only thing that can
988/// happen is the comment could end with an escaped newline between the */ end
989/// of comment.
Chris Lattnerf03b00c2008-10-12 04:15:42 +0000990///
991/// If KeepCommentMode is enabled, this forms a token from the comment and
992/// returns true.
Chris Lattner4b009652007-07-25 00:24:17 +0000993bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
994 // Scan one character past where we should, looking for a '/' character. Once
995 // we find it, check to see if it was preceeded by a *. This common
996 // optimization helps people who like to put a lot of * characters in their
997 // comments.
998
999 // The first character we get with newlines and trigraphs skipped to handle
1000 // the degenerate /*/ case below correctly if the * has an escaped newline
1001 // after it.
1002 unsigned CharSize;
1003 unsigned char C = getCharAndSize(CurPtr, CharSize);
1004 CurPtr += CharSize;
1005 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerf9c62772008-11-22 02:02:22 +00001006 if (!isLexingRawMode())
Chris Lattnere5eca952008-10-12 01:31:51 +00001007 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattnerd66f4542008-10-12 04:19:49 +00001008 --CurPtr;
1009
1010 // KeepWhitespaceMode should return this broken comment as a token. Since
1011 // it isn't a well formed comment, just return it as an 'unknown' token.
1012 if (isKeepWhitespaceMode()) {
Chris Lattner0344cc72008-10-12 04:51:35 +00001013 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd66f4542008-10-12 04:19:49 +00001014 return true;
1015 }
1016
1017 BufferPtr = CurPtr;
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001018 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001019 }
1020
1021 // Check to see if the first character after the '/*' is another /. If so,
1022 // then this slash does not end the block comment, it is part of it.
1023 if (C == '/')
1024 C = *CurPtr++;
1025
1026 while (1) {
1027 // Skip over all non-interesting characters until we find end of buffer or a
1028 // (probably ending) '/' character.
1029 if (CurPtr + 24 < BufferEnd) {
1030 // While not aligned to a 16-byte boundary.
1031 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1032 C = *CurPtr++;
1033
1034 if (C == '/') goto FoundSlash;
1035
1036#ifdef __SSE2__
1037 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1038 '/', '/', '/', '/', '/', '/', '/', '/');
1039 while (CurPtr+16 <= BufferEnd &&
1040 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1041 CurPtr += 16;
1042#elif __ALTIVEC__
1043 __vector unsigned char Slashes = {
1044 '/', '/', '/', '/', '/', '/', '/', '/',
1045 '/', '/', '/', '/', '/', '/', '/', '/'
1046 };
1047 while (CurPtr+16 <= BufferEnd &&
1048 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1049 CurPtr += 16;
1050#else
1051 // Scan for '/' quickly. Many block comments are very large.
1052 while (CurPtr[0] != '/' &&
1053 CurPtr[1] != '/' &&
1054 CurPtr[2] != '/' &&
1055 CurPtr[3] != '/' &&
1056 CurPtr+4 < BufferEnd) {
1057 CurPtr += 4;
1058 }
1059#endif
1060
1061 // It has to be one of the bytes scanned, increment to it and read one.
1062 C = *CurPtr++;
1063 }
1064
1065 // Loop to scan the remainder.
1066 while (C != '/' && C != '\0')
1067 C = *CurPtr++;
1068
1069 FoundSlash:
1070 if (C == '/') {
1071 if (CurPtr[-2] == '*') // We found the final */. We're done!
1072 break;
1073
1074 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
1075 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
1076 // We found the final */, though it had an escaped newline between the
1077 // * and /. We're done!
1078 break;
1079 }
1080 }
1081 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1082 // If this is a /* inside of the comment, emit a warning. Don't do this
1083 // if this is a /*/, which will end the comment. This misses cases with
1084 // embedded escaped newlines, but oh well.
Chris Lattnerf9c62772008-11-22 02:02:22 +00001085 if (!isLexingRawMode())
1086 Diag(CurPtr-1, diag::warn_nested_block_comment);
Chris Lattner4b009652007-07-25 00:24:17 +00001087 }
1088 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerf9c62772008-11-22 02:02:22 +00001089 if (!isLexingRawMode())
1090 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner4b009652007-07-25 00:24:17 +00001091 // Note: the user probably forgot a */. We could continue immediately
1092 // after the /*, but this would involve lexing a lot of what really is the
1093 // comment, which surely would confuse the parser.
Chris Lattnerd66f4542008-10-12 04:19:49 +00001094 --CurPtr;
1095
1096 // KeepWhitespaceMode should return this broken comment as a token. Since
1097 // it isn't a well formed comment, just return it as an 'unknown' token.
1098 if (isKeepWhitespaceMode()) {
Chris Lattner0344cc72008-10-12 04:51:35 +00001099 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd66f4542008-10-12 04:19:49 +00001100 return true;
1101 }
1102
1103 BufferPtr = CurPtr;
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001104 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001105 }
1106 C = *CurPtr++;
1107 }
1108
1109 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner170adb12008-10-12 03:22:02 +00001110 if (inKeepCommentMode()) {
Chris Lattner0344cc72008-10-12 04:51:35 +00001111 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001112 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001113 }
1114
1115 // It is common for the tokens immediately after a /**/ comment to be
1116 // whitespace. Instead of going through the big switch, handle it
Chris Lattner867a87b2008-10-12 04:05:48 +00001117 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1118 // have already returned above with the comment as a token.
Chris Lattner4b009652007-07-25 00:24:17 +00001119 if (isHorizontalWhitespace(*CurPtr)) {
1120 Result.setFlag(Token::LeadingSpace);
1121 SkipWhitespace(Result, CurPtr+1);
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001122 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001123 }
1124
1125 // Otherwise, just return so that the next character will be lexed as a token.
1126 BufferPtr = CurPtr;
1127 Result.setFlag(Token::LeadingSpace);
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001128 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001129}
1130
1131//===----------------------------------------------------------------------===//
1132// Primary Lexing Entry Points
1133//===----------------------------------------------------------------------===//
1134
Chris Lattner4b009652007-07-25 00:24:17 +00001135/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1136/// uninterpreted string. This switches the lexer out of directive mode.
1137std::string Lexer::ReadToEndOfLine() {
1138 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1139 "Must be in a preprocessing directive!");
1140 std::string Result;
1141 Token Tmp;
1142
1143 // CurPtr - Cache BufferPtr in an automatic variable.
1144 const char *CurPtr = BufferPtr;
1145 while (1) {
1146 char Char = getAndAdvanceChar(CurPtr, Tmp);
1147 switch (Char) {
1148 default:
1149 Result += Char;
1150 break;
1151 case 0: // Null.
1152 // Found end of file?
1153 if (CurPtr-1 != BufferEnd) {
1154 // Nope, normal character, continue.
1155 Result += Char;
1156 break;
1157 }
1158 // FALL THROUGH.
1159 case '\r':
1160 case '\n':
1161 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1162 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1163 BufferPtr = CurPtr-1;
1164
1165 // Next, lex the character, which should handle the EOM transition.
1166 Lex(Tmp);
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001167 assert(Tmp.is(tok::eom) && "Unexpected token!");
Chris Lattner4b009652007-07-25 00:24:17 +00001168
1169 // Finally, we're done, return the string we found.
1170 return Result;
1171 }
1172 }
1173}
1174
1175/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1176/// condition, reporting diagnostics and handling other edge cases as required.
1177/// This returns true if Result contains a token, false if PP.Lex should be
1178/// called again.
1179bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
1180 // If we hit the end of the file while parsing a preprocessor directive,
1181 // end the preprocessor directive first. The next token returned will
1182 // then be the end of file.
1183 if (ParsingPreprocessorDirective) {
1184 // Done parsing the "line".
1185 ParsingPreprocessorDirective = false;
Chris Lattner4b009652007-07-25 00:24:17 +00001186 // Update the location of token as well as BufferPtr.
Chris Lattner0344cc72008-10-12 04:51:35 +00001187 FormTokenWithChars(Result, CurPtr, tok::eom);
Chris Lattner4b009652007-07-25 00:24:17 +00001188
1189 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner1c1bed12008-10-12 03:27:19 +00001190 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner4b009652007-07-25 00:24:17 +00001191 return true; // Have a token.
1192 }
1193
1194 // If we are in raw mode, return this event as an EOF token. Let the caller
1195 // that put us in raw mode handle the event.
Chris Lattnerf9c62772008-11-22 02:02:22 +00001196 if (isLexingRawMode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001197 Result.startToken();
1198 BufferPtr = BufferEnd;
Chris Lattner0344cc72008-10-12 04:51:35 +00001199 FormTokenWithChars(Result, BufferEnd, tok::eof);
Chris Lattner4b009652007-07-25 00:24:17 +00001200 return true;
1201 }
1202
1203 // Otherwise, issue diagnostics for unterminated #if and missing newline.
1204
1205 // If we are in a #if directive, emit an error.
1206 while (!ConditionalStack.empty()) {
Chris Lattner8ef6cdc2008-11-22 06:22:39 +00001207 PP->Diag(ConditionalStack.back().IfLoc,
1208 diag::err_pp_unterminated_conditional);
Chris Lattner4b009652007-07-25 00:24:17 +00001209 ConditionalStack.pop_back();
1210 }
1211
Chris Lattner5c337fa2008-04-12 05:54:25 +00001212 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1213 // a pedwarn.
1214 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Chris Lattner4b009652007-07-25 00:24:17 +00001215 Diag(BufferEnd, diag::ext_no_newline_eof);
1216
1217 BufferPtr = CurPtr;
1218
1219 // Finally, let the preprocessor handle this.
Chris Lattner342dccb2007-10-17 20:41:00 +00001220 return PP->HandleEndOfFile(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001221}
1222
1223/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1224/// the specified lexer will return a tok::l_paren token, 0 if it is something
1225/// else and 2 if there are no more tokens in the buffer controlled by the
1226/// lexer.
1227unsigned Lexer::isNextPPTokenLParen() {
1228 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1229
1230 // Switch to 'skipping' mode. This will ensure that we can lex a token
1231 // without emitting diagnostics, disables macro expansion, and will cause EOF
1232 // to return an EOF token instead of popping the include stack.
1233 LexingRawMode = true;
1234
1235 // Save state that can be changed while lexing so that we can restore it.
1236 const char *TmpBufferPtr = BufferPtr;
1237
1238 Token Tok;
1239 Tok.startToken();
1240 LexTokenInternal(Tok);
1241
1242 // Restore state that may have changed.
1243 BufferPtr = TmpBufferPtr;
1244
1245 // Restore the lexer back to non-skipping mode.
1246 LexingRawMode = false;
1247
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001248 if (Tok.is(tok::eof))
Chris Lattner4b009652007-07-25 00:24:17 +00001249 return 2;
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001250 return Tok.is(tok::l_paren);
Chris Lattner4b009652007-07-25 00:24:17 +00001251}
1252
1253
1254/// LexTokenInternal - This implements a simple C family lexer. It is an
1255/// extremely performance critical piece of code. This assumes that the buffer
1256/// has a null character at the end of the file. Return true if an error
1257/// occurred and compilation should terminate, false if normal. This returns a
1258/// preprocessing token, not a normal token, as such, it is an internal
1259/// interface. It assumes that the Flags of result have been cleared before
1260/// calling this.
1261void Lexer::LexTokenInternal(Token &Result) {
1262LexNextToken:
1263 // New token, can't need cleaning yet.
1264 Result.clearFlag(Token::NeedsCleaning);
1265 Result.setIdentifierInfo(0);
1266
1267 // CurPtr - Cache BufferPtr in an automatic variable.
1268 const char *CurPtr = BufferPtr;
1269
1270 // Small amounts of horizontal whitespace is very common between tokens.
1271 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1272 ++CurPtr;
1273 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1274 ++CurPtr;
Chris Lattner867a87b2008-10-12 04:05:48 +00001275
1276 // If we are keeping whitespace and other tokens, just return what we just
1277 // skipped. The next lexer invocation will return the token after the
1278 // whitespace.
1279 if (isKeepWhitespaceMode()) {
Chris Lattner0344cc72008-10-12 04:51:35 +00001280 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner867a87b2008-10-12 04:05:48 +00001281 return;
1282 }
1283
Chris Lattner4b009652007-07-25 00:24:17 +00001284 BufferPtr = CurPtr;
1285 Result.setFlag(Token::LeadingSpace);
1286 }
1287
1288 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1289
1290 // Read a character, advancing over it.
1291 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001292 tok::TokenKind Kind;
1293
Chris Lattner4b009652007-07-25 00:24:17 +00001294 switch (Char) {
1295 case 0: // Null.
1296 // Found end of file?
1297 if (CurPtr-1 == BufferEnd) {
1298 // Read the PP instance variable into an automatic variable, because
1299 // LexEndOfFile will often delete 'this'.
Chris Lattner342dccb2007-10-17 20:41:00 +00001300 Preprocessor *PPCache = PP;
Chris Lattner4b009652007-07-25 00:24:17 +00001301 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1302 return; // Got a token to return.
Chris Lattner342dccb2007-10-17 20:41:00 +00001303 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1304 return PPCache->Lex(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001305 }
1306
Chris Lattnerf9c62772008-11-22 02:02:22 +00001307 if (!isLexingRawMode())
1308 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner4b009652007-07-25 00:24:17 +00001309 Result.setFlag(Token::LeadingSpace);
Chris Lattner867a87b2008-10-12 04:05:48 +00001310 if (SkipWhitespace(Result, CurPtr))
1311 return; // KeepWhitespaceMode
1312
Chris Lattner4b009652007-07-25 00:24:17 +00001313 goto LexNextToken; // GCC isn't tail call eliminating.
1314 case '\n':
1315 case '\r':
1316 // If we are inside a preprocessor directive and we see the end of line,
1317 // we know we are done with the directive, so return an EOM token.
1318 if (ParsingPreprocessorDirective) {
1319 // Done parsing the "line".
1320 ParsingPreprocessorDirective = false;
1321
1322 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner1c1bed12008-10-12 03:27:19 +00001323 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner4b009652007-07-25 00:24:17 +00001324
1325 // Since we consumed a newline, we are back at the start of a line.
1326 IsAtStartOfLine = true;
1327
Chris Lattner0344cc72008-10-12 04:51:35 +00001328 Kind = tok::eom;
Chris Lattner4b009652007-07-25 00:24:17 +00001329 break;
1330 }
1331 // The returned token is at the start of the line.
1332 Result.setFlag(Token::StartOfLine);
1333 // No leading whitespace seen so far.
1334 Result.clearFlag(Token::LeadingSpace);
Chris Lattner867a87b2008-10-12 04:05:48 +00001335
1336 if (SkipWhitespace(Result, CurPtr))
1337 return; // KeepWhitespaceMode
Chris Lattner4b009652007-07-25 00:24:17 +00001338 goto LexNextToken; // GCC isn't tail call eliminating.
1339 case ' ':
1340 case '\t':
1341 case '\f':
1342 case '\v':
1343 SkipHorizontalWhitespace:
1344 Result.setFlag(Token::LeadingSpace);
Chris Lattner867a87b2008-10-12 04:05:48 +00001345 if (SkipWhitespace(Result, CurPtr))
1346 return; // KeepWhitespaceMode
Chris Lattner4b009652007-07-25 00:24:17 +00001347
1348 SkipIgnoredUnits:
1349 CurPtr = BufferPtr;
1350
1351 // If the next token is obviously a // or /* */ comment, skip it efficiently
1352 // too (without going through the big switch stmt).
Chris Lattner43e455d2009-01-16 22:39:25 +00001353 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1354 Features.BCPLComment) {
Chris Lattner4b009652007-07-25 00:24:17 +00001355 SkipBCPLComment(Result, CurPtr+2);
1356 goto SkipIgnoredUnits;
Chris Lattner170adb12008-10-12 03:22:02 +00001357 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001358 SkipBlockComment(Result, CurPtr+2);
1359 goto SkipIgnoredUnits;
1360 } else if (isHorizontalWhitespace(*CurPtr)) {
1361 goto SkipHorizontalWhitespace;
1362 }
1363 goto LexNextToken; // GCC isn't tail call eliminating.
1364
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001365 // C99 6.4.4.1: Integer Constants.
1366 // C99 6.4.4.2: Floating Constants.
1367 case '0': case '1': case '2': case '3': case '4':
1368 case '5': case '6': case '7': case '8': case '9':
1369 // Notify MIOpt that we read a non-whitespace/non-comment token.
1370 MIOpt.ReadToken();
1371 return LexNumericConstant(Result, CurPtr);
1372
1373 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Chris Lattner4b009652007-07-25 00:24:17 +00001374 // Notify MIOpt that we read a non-whitespace/non-comment token.
1375 MIOpt.ReadToken();
1376 Char = getCharAndSize(CurPtr, SizeTmp);
1377
1378 // Wide string literal.
1379 if (Char == '"')
1380 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1381 true);
1382
1383 // Wide character constant.
1384 if (Char == '\'')
1385 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1386 // FALL THROUGH, treating L like the start of an identifier.
1387
1388 // C99 6.4.2: Identifiers.
1389 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1390 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1391 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1392 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1393 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1394 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1395 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1396 case 'v': case 'w': case 'x': case 'y': case 'z':
1397 case '_':
1398 // Notify MIOpt that we read a non-whitespace/non-comment token.
1399 MIOpt.ReadToken();
1400 return LexIdentifier(Result, CurPtr);
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001401
1402 case '$': // $ in identifiers.
1403 if (Features.DollarIdents) {
Chris Lattnerf9c62772008-11-22 02:02:22 +00001404 if (!isLexingRawMode())
1405 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001406 // Notify MIOpt that we read a non-whitespace/non-comment token.
1407 MIOpt.ReadToken();
1408 return LexIdentifier(Result, CurPtr);
1409 }
Chris Lattner4b009652007-07-25 00:24:17 +00001410
Chris Lattner0344cc72008-10-12 04:51:35 +00001411 Kind = tok::unknown;
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001412 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001413
1414 // C99 6.4.4: Character Constants.
1415 case '\'':
1416 // Notify MIOpt that we read a non-whitespace/non-comment token.
1417 MIOpt.ReadToken();
1418 return LexCharConstant(Result, CurPtr);
1419
1420 // C99 6.4.5: String Literals.
1421 case '"':
1422 // Notify MIOpt that we read a non-whitespace/non-comment token.
1423 MIOpt.ReadToken();
1424 return LexStringLiteral(Result, CurPtr, false);
1425
1426 // C99 6.4.6: Punctuators.
1427 case '?':
Chris Lattner0344cc72008-10-12 04:51:35 +00001428 Kind = tok::question;
Chris Lattner4b009652007-07-25 00:24:17 +00001429 break;
1430 case '[':
Chris Lattner0344cc72008-10-12 04:51:35 +00001431 Kind = tok::l_square;
Chris Lattner4b009652007-07-25 00:24:17 +00001432 break;
1433 case ']':
Chris Lattner0344cc72008-10-12 04:51:35 +00001434 Kind = tok::r_square;
Chris Lattner4b009652007-07-25 00:24:17 +00001435 break;
1436 case '(':
Chris Lattner0344cc72008-10-12 04:51:35 +00001437 Kind = tok::l_paren;
Chris Lattner4b009652007-07-25 00:24:17 +00001438 break;
1439 case ')':
Chris Lattner0344cc72008-10-12 04:51:35 +00001440 Kind = tok::r_paren;
Chris Lattner4b009652007-07-25 00:24:17 +00001441 break;
1442 case '{':
Chris Lattner0344cc72008-10-12 04:51:35 +00001443 Kind = tok::l_brace;
Chris Lattner4b009652007-07-25 00:24:17 +00001444 break;
1445 case '}':
Chris Lattner0344cc72008-10-12 04:51:35 +00001446 Kind = tok::r_brace;
Chris Lattner4b009652007-07-25 00:24:17 +00001447 break;
1448 case '.':
1449 Char = getCharAndSize(CurPtr, SizeTmp);
1450 if (Char >= '0' && Char <= '9') {
1451 // Notify MIOpt that we read a non-whitespace/non-comment token.
1452 MIOpt.ReadToken();
1453
1454 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1455 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001456 Kind = tok::periodstar;
Chris Lattner4b009652007-07-25 00:24:17 +00001457 CurPtr += SizeTmp;
1458 } else if (Char == '.' &&
1459 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001460 Kind = tok::ellipsis;
Chris Lattner4b009652007-07-25 00:24:17 +00001461 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1462 SizeTmp2, Result);
1463 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001464 Kind = tok::period;
Chris Lattner4b009652007-07-25 00:24:17 +00001465 }
1466 break;
1467 case '&':
1468 Char = getCharAndSize(CurPtr, SizeTmp);
1469 if (Char == '&') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001470 Kind = tok::ampamp;
Chris Lattner4b009652007-07-25 00:24:17 +00001471 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1472 } else if (Char == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001473 Kind = tok::ampequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001474 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1475 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001476 Kind = tok::amp;
Chris Lattner4b009652007-07-25 00:24:17 +00001477 }
1478 break;
1479 case '*':
1480 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001481 Kind = tok::starequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001482 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1483 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001484 Kind = tok::star;
Chris Lattner4b009652007-07-25 00:24:17 +00001485 }
1486 break;
1487 case '+':
1488 Char = getCharAndSize(CurPtr, SizeTmp);
1489 if (Char == '+') {
Chris Lattner4b009652007-07-25 00:24:17 +00001490 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001491 Kind = tok::plusplus;
Chris Lattner4b009652007-07-25 00:24:17 +00001492 } else if (Char == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001493 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001494 Kind = tok::plusequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001495 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001496 Kind = tok::plus;
Chris Lattner4b009652007-07-25 00:24:17 +00001497 }
1498 break;
1499 case '-':
1500 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattner0344cc72008-10-12 04:51:35 +00001501 if (Char == '-') { // --
Chris Lattner4b009652007-07-25 00:24:17 +00001502 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001503 Kind = tok::minusminus;
Chris Lattner4b009652007-07-25 00:24:17 +00001504 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattner0344cc72008-10-12 04:51:35 +00001505 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Chris Lattner4b009652007-07-25 00:24:17 +00001506 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1507 SizeTmp2, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001508 Kind = tok::arrowstar;
1509 } else if (Char == '>') { // ->
Chris Lattner4b009652007-07-25 00:24:17 +00001510 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001511 Kind = tok::arrow;
1512 } else if (Char == '=') { // -=
Chris Lattner4b009652007-07-25 00:24:17 +00001513 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001514 Kind = tok::minusequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001515 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001516 Kind = tok::minus;
Chris Lattner4b009652007-07-25 00:24:17 +00001517 }
1518 break;
1519 case '~':
Chris Lattner0344cc72008-10-12 04:51:35 +00001520 Kind = tok::tilde;
Chris Lattner4b009652007-07-25 00:24:17 +00001521 break;
1522 case '!':
1523 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001524 Kind = tok::exclaimequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001525 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1526 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001527 Kind = tok::exclaim;
Chris Lattner4b009652007-07-25 00:24:17 +00001528 }
1529 break;
1530 case '/':
1531 // 6.4.9: Comments
1532 Char = getCharAndSize(CurPtr, SizeTmp);
1533 if (Char == '/') { // BCPL comment.
Chris Lattner43e455d2009-01-16 22:39:25 +00001534 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
1535 // want to lex this as a comment. There is one problem with this though,
1536 // that in one particular corner case, this can change the behavior of the
1537 // resultant program. For example, In "foo //**/ bar", C89 would lex
1538 // this as "foo / bar" and langauges with BCPL comments would lex it as
1539 // "foo". Check to see if the character after the second slash is a '*'.
1540 // If so, we will lex that as a "/" instead of the start of a comment.
1541 if (Features.BCPLComment ||
1542 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
1543 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1544 return; // KeepCommentMode
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001545
Chris Lattner43e455d2009-01-16 22:39:25 +00001546 // It is common for the tokens immediately after a // comment to be
1547 // whitespace (indentation for the next line). Instead of going through
1548 // the big switch, handle it efficiently now.
1549 goto SkipIgnoredUnits;
1550 }
1551 }
1552
1553 if (Char == '*') { // /**/ comment.
Chris Lattner4b009652007-07-25 00:24:17 +00001554 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001555 return; // KeepCommentMode
1556 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner43e455d2009-01-16 22:39:25 +00001557 }
1558
1559 if (Char == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001560 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001561 Kind = tok::slashequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001562 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001563 Kind = tok::slash;
Chris Lattner4b009652007-07-25 00:24:17 +00001564 }
1565 break;
1566 case '%':
1567 Char = getCharAndSize(CurPtr, SizeTmp);
1568 if (Char == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001569 Kind = tok::percentequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001570 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1571 } else if (Features.Digraphs && Char == '>') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001572 Kind = tok::r_brace; // '%>' -> '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001573 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1574 } else if (Features.Digraphs && Char == ':') {
1575 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1576 Char = getCharAndSize(CurPtr, SizeTmp);
1577 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001578 Kind = tok::hashhash; // '%:%:' -> '##'
Chris Lattner4b009652007-07-25 00:24:17 +00001579 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1580 SizeTmp2, Result);
1581 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner4b009652007-07-25 00:24:17 +00001582 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerf9c62772008-11-22 02:02:22 +00001583 if (!isLexingRawMode())
1584 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner0344cc72008-10-12 04:51:35 +00001585 Kind = tok::hashat;
Chris Lattner4b009652007-07-25 00:24:17 +00001586 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001587 Kind = tok::hash; // '%:' -> '#'
Chris Lattner4b009652007-07-25 00:24:17 +00001588
1589 // We parsed a # character. If this occurs at the start of the line,
1590 // it's actually the start of a preprocessing directive. Callback to
1591 // the preprocessor to handle it.
1592 // FIXME: -fpreprocessed mode??
1593 if (Result.isAtStartOfLine() && !LexingRawMode) {
1594 BufferPtr = CurPtr;
Chris Lattner342dccb2007-10-17 20:41:00 +00001595 PP->HandleDirective(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001596
1597 // As an optimization, if the preprocessor didn't switch lexers, tail
1598 // recurse.
Chris Lattner342dccb2007-10-17 20:41:00 +00001599 if (PP->isCurrentLexer(this)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001600 // Start a new token. If this is a #include or something, the PP may
1601 // want us starting at the beginning of the line again. If so, set
1602 // the StartOfLine flag.
1603 if (IsAtStartOfLine) {
1604 Result.setFlag(Token::StartOfLine);
1605 IsAtStartOfLine = false;
1606 }
1607 goto LexNextToken; // GCC isn't tail call eliminating.
1608 }
1609
Chris Lattner342dccb2007-10-17 20:41:00 +00001610 return PP->Lex(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001611 }
1612 }
1613 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001614 Kind = tok::percent;
Chris Lattner4b009652007-07-25 00:24:17 +00001615 }
1616 break;
1617 case '<':
1618 Char = getCharAndSize(CurPtr, SizeTmp);
1619 if (ParsingFilename) {
1620 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1621 } else if (Char == '<' &&
1622 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001623 Kind = tok::lesslessequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001624 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1625 SizeTmp2, Result);
1626 } else if (Char == '<') {
Chris Lattner4b009652007-07-25 00:24:17 +00001627 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001628 Kind = tok::lessless;
Chris Lattner4b009652007-07-25 00:24:17 +00001629 } else if (Char == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001630 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001631 Kind = tok::lessequal;
1632 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Chris Lattner4b009652007-07-25 00:24:17 +00001633 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001634 Kind = tok::l_square;
1635 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Chris Lattner4b009652007-07-25 00:24:17 +00001636 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001637 Kind = tok::l_brace;
Chris Lattner4b009652007-07-25 00:24:17 +00001638 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001639 Kind = tok::less;
Chris Lattner4b009652007-07-25 00:24:17 +00001640 }
1641 break;
1642 case '>':
1643 Char = getCharAndSize(CurPtr, SizeTmp);
1644 if (Char == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001645 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001646 Kind = tok::greaterequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001647 } else if (Char == '>' &&
1648 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001649 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1650 SizeTmp2, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001651 Kind = tok::greatergreaterequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001652 } else if (Char == '>') {
Chris Lattner4b009652007-07-25 00:24:17 +00001653 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001654 Kind = tok::greatergreater;
Chris Lattner4b009652007-07-25 00:24:17 +00001655 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001656 Kind = tok::greater;
Chris Lattner4b009652007-07-25 00:24:17 +00001657 }
1658 break;
1659 case '^':
1660 Char = getCharAndSize(CurPtr, SizeTmp);
1661 if (Char == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001662 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001663 Kind = tok::caretequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001664 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001665 Kind = tok::caret;
Chris Lattner4b009652007-07-25 00:24:17 +00001666 }
1667 break;
1668 case '|':
1669 Char = getCharAndSize(CurPtr, SizeTmp);
1670 if (Char == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001671 Kind = tok::pipeequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001672 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1673 } else if (Char == '|') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001674 Kind = tok::pipepipe;
Chris Lattner4b009652007-07-25 00:24:17 +00001675 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1676 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001677 Kind = tok::pipe;
Chris Lattner4b009652007-07-25 00:24:17 +00001678 }
1679 break;
1680 case ':':
1681 Char = getCharAndSize(CurPtr, SizeTmp);
1682 if (Features.Digraphs && Char == '>') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001683 Kind = tok::r_square; // ':>' -> ']'
Chris Lattner4b009652007-07-25 00:24:17 +00001684 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1685 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001686 Kind = tok::coloncolon;
Chris Lattner4b009652007-07-25 00:24:17 +00001687 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1688 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001689 Kind = tok::colon;
Chris Lattner4b009652007-07-25 00:24:17 +00001690 }
1691 break;
1692 case ';':
Chris Lattner0344cc72008-10-12 04:51:35 +00001693 Kind = tok::semi;
Chris Lattner4b009652007-07-25 00:24:17 +00001694 break;
1695 case '=':
1696 Char = getCharAndSize(CurPtr, SizeTmp);
1697 if (Char == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001698 Kind = tok::equalequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001699 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1700 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001701 Kind = tok::equal;
Chris Lattner4b009652007-07-25 00:24:17 +00001702 }
1703 break;
1704 case ',':
Chris Lattner0344cc72008-10-12 04:51:35 +00001705 Kind = tok::comma;
Chris Lattner4b009652007-07-25 00:24:17 +00001706 break;
1707 case '#':
1708 Char = getCharAndSize(CurPtr, SizeTmp);
1709 if (Char == '#') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001710 Kind = tok::hashhash;
Chris Lattner4b009652007-07-25 00:24:17 +00001711 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1712 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner0344cc72008-10-12 04:51:35 +00001713 Kind = tok::hashat;
Chris Lattnerf9c62772008-11-22 02:02:22 +00001714 if (!isLexingRawMode())
1715 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner4b009652007-07-25 00:24:17 +00001716 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1717 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001718 Kind = tok::hash;
Chris Lattner4b009652007-07-25 00:24:17 +00001719 // We parsed a # character. If this occurs at the start of the line,
1720 // it's actually the start of a preprocessing directive. Callback to
1721 // the preprocessor to handle it.
1722 // FIXME: -fpreprocessed mode??
1723 if (Result.isAtStartOfLine() && !LexingRawMode) {
1724 BufferPtr = CurPtr;
Chris Lattner342dccb2007-10-17 20:41:00 +00001725 PP->HandleDirective(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001726
1727 // As an optimization, if the preprocessor didn't switch lexers, tail
1728 // recurse.
Chris Lattner342dccb2007-10-17 20:41:00 +00001729 if (PP->isCurrentLexer(this)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001730 // Start a new token. If this is a #include or something, the PP may
1731 // want us starting at the beginning of the line again. If so, set
1732 // the StartOfLine flag.
1733 if (IsAtStartOfLine) {
1734 Result.setFlag(Token::StartOfLine);
1735 IsAtStartOfLine = false;
1736 }
1737 goto LexNextToken; // GCC isn't tail call eliminating.
1738 }
Chris Lattner342dccb2007-10-17 20:41:00 +00001739 return PP->Lex(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001740 }
1741 }
1742 break;
1743
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001744 case '@':
1745 // Objective C support.
1746 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattner0344cc72008-10-12 04:51:35 +00001747 Kind = tok::at;
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001748 else
Chris Lattner0344cc72008-10-12 04:51:35 +00001749 Kind = tok::unknown;
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001750 break;
1751
Chris Lattner4b009652007-07-25 00:24:17 +00001752 case '\\':
1753 // FIXME: UCN's.
1754 // FALL THROUGH.
1755 default:
Chris Lattner0344cc72008-10-12 04:51:35 +00001756 Kind = tok::unknown;
Chris Lattner4b009652007-07-25 00:24:17 +00001757 break;
1758 }
1759
1760 // Notify MIOpt that we read a non-whitespace/non-comment token.
1761 MIOpt.ReadToken();
1762
1763 // Update the location of token as well as BufferPtr.
Chris Lattner0344cc72008-10-12 04:51:35 +00001764 FormTokenWithChars(Result, CurPtr, Kind);
Chris Lattner4b009652007-07-25 00:24:17 +00001765}