blob: e6030ad1d4586e5c8689fd9cdd63f34b5780c44a [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerd2177732007-07-20 16:59:19 +000010// This file implements the Lexer and Token interfaces.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
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"
Chris Lattner9dc1f532007-07-20 16:37:10 +000030#include "clang/Basic/SourceManager.h"
Chris Lattner409a0362007-07-22 18:38:25 +000031#include "llvm/Support/Compiler.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000032#include "llvm/Support/MemoryBuffer.h"
33#include <cctype>
34using namespace clang;
35
36static void InitCharacterInfo();
37
Chris Lattnerdbf388b2007-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 Gregorbec1c9d2008-12-01 21:46:47 +000044 if (IdentifierInfo *II = getIdentifierInfo())
45 return II->getObjCKeywordID() == objcKey;
46 return false;
Chris Lattnerdbf388b2007-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 Lattner53702cd2007-12-13 01:59:49 +000055
Chris Lattnerdbf388b2007-10-07 08:47:24 +000056//===----------------------------------------------------------------------===//
57// Lexer Class Implementation
58//===----------------------------------------------------------------------===//
59
60
Chris Lattner168ae2d2007-10-17 20:41:00 +000061/// Lexer constructor - Create a new lexer object for the specified buffer
62/// with the specified preprocessor managing the lexing process. This lexer
63/// assumes that the associated file buffer and Preprocessor objects will
64/// outlive it, so it doesn't take ownership of either of them.
Chris Lattner25bdb512007-07-20 16:52:03 +000065Lexer::Lexer(SourceLocation fileloc, Preprocessor &pp,
66 const char *BufStart, const char *BufEnd)
Ted Kremenek41938c82008-11-19 21:57:25 +000067 : PreprocessorLexer(&pp, fileloc), FileLoc(fileloc),
68 Features(pp.getLangOptions()) {
Chris Lattner25bdb512007-07-20 16:52:03 +000069
Chris Lattner168ae2d2007-10-17 20:41:00 +000070 SourceManager &SourceMgr = PP->getSourceManager();
Chris Lattner448cec42007-07-22 18:44:36 +000071 unsigned InputFileID = SourceMgr.getPhysicalLoc(FileLoc).getFileID();
72 const llvm::MemoryBuffer *InputFile = SourceMgr.getBuffer(InputFileID);
Chris Lattner25bdb512007-07-20 16:52:03 +000073
Reid Spencer5f016e22007-07-11 17:01:13 +000074 Is_PragmaLexer = false;
Reid Spencer5f016e22007-07-11 17:01:13 +000075 InitCharacterInfo();
Chris Lattner448cec42007-07-22 18:44:36 +000076
77 // BufferStart must always be InputFile->getBufferStart().
78 BufferStart = InputFile->getBufferStart();
79
80 // BufferPtr and BufferEnd can start out somewhere inside the current buffer.
81 // If unspecified, they starts at the start/end of the buffer.
82 BufferPtr = BufStart ? BufStart : BufferStart;
Chris Lattner25bdb512007-07-20 16:52:03 +000083 BufferEnd = BufEnd ? BufEnd : InputFile->getBufferEnd();
84
Reid Spencer5f016e22007-07-11 17:01:13 +000085 assert(BufferEnd[0] == 0 &&
86 "We assume that the input buffer has a null character at the end"
87 " to simplify lexing!");
Chris Lattner25bdb512007-07-20 16:52:03 +000088
Reid Spencer5f016e22007-07-11 17:01:13 +000089 // Start of the file is a start of line.
90 IsAtStartOfLine = true;
91
92 // We are not after parsing a #.
93 ParsingPreprocessorDirective = false;
94
95 // We are not after parsing #include.
96 ParsingFilename = false;
97
98 // We are not in raw mode. Raw mode disables diagnostics and interpretation
99 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
100 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
101 // or otherwise skipping over tokens.
102 LexingRawMode = false;
103
Chris Lattnerd88dc482008-10-12 04:05:48 +0000104 // Default to keeping comments if the preprocessor wants them.
105 ExtendedTokenMode = 0;
Chris Lattnerf744d132008-10-12 03:27:19 +0000106 SetCommentRetentionState(PP->getCommentRetentionState());
Reid Spencer5f016e22007-07-11 17:01:13 +0000107}
108
Chris Lattner168ae2d2007-10-17 20:41:00 +0000109/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner590f0cc2008-10-12 01:15:46 +0000110/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
111/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000112Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattner590f0cc2008-10-12 01:15:46 +0000113 const char *BufStart, const char *BufEnd,
114 const llvm::MemoryBuffer *FromFile)
Ted Kremenek41938c82008-11-19 21:57:25 +0000115 : PreprocessorLexer(), FileLoc(fileloc),
116 Features(features) {
117
Chris Lattner168ae2d2007-10-17 20:41:00 +0000118 Is_PragmaLexer = false;
119 InitCharacterInfo();
120
Chris Lattner8527b712008-10-12 01:23:27 +0000121 // If a MemoryBuffer was specified, use its start as BufferStart. This affects
122 // the source location objects produced by this lexer.
Chris Lattner590f0cc2008-10-12 01:15:46 +0000123 BufferStart = FromFile ? FromFile->getBufferStart() : BufStart;
Chris Lattner168ae2d2007-10-17 20:41:00 +0000124 BufferPtr = BufStart;
125 BufferEnd = BufEnd;
126
127 assert(BufferEnd[0] == 0 &&
128 "We assume that the input buffer has a null character at the end"
129 " to simplify lexing!");
130
131 // Start of the file is a start of line.
132 IsAtStartOfLine = true;
133
134 // We are not after parsing a #.
135 ParsingPreprocessorDirective = false;
136
137 // We are not after parsing #include.
138 ParsingFilename = false;
139
140 // We *are* in raw mode.
141 LexingRawMode = true;
142
Chris Lattnera2c7ad92008-10-12 01:34:51 +0000143 // Default to not keeping comments in raw mode.
Chris Lattnerd88dc482008-10-12 04:05:48 +0000144 ExtendedTokenMode = 0;
Chris Lattner168ae2d2007-10-17 20:41:00 +0000145}
146
147
Reid Spencer5f016e22007-07-11 17:01:13 +0000148/// Stringify - Convert the specified string into a C string, with surrounding
149/// ""'s, and with escaped \ and " characters.
150std::string Lexer::Stringify(const std::string &Str, bool Charify) {
151 std::string Result = Str;
152 char Quote = Charify ? '\'' : '"';
153 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
154 if (Result[i] == '\\' || Result[i] == Quote) {
155 Result.insert(Result.begin()+i, '\\');
156 ++i; ++e;
157 }
158 }
159 return Result;
160}
161
Chris Lattnerd8e30832007-07-24 06:57:14 +0000162/// Stringify - Convert the specified string into a C string by escaping '\'
163/// and " characters. This does not add surrounding ""'s to the string.
164void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
165 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
166 if (Str[i] == '\\' || Str[i] == '"') {
167 Str.insert(Str.begin()+i, '\\');
168 ++i; ++e;
169 }
170 }
171}
172
Reid Spencer5f016e22007-07-11 17:01:13 +0000173
Chris Lattner9a611942007-10-17 21:18:47 +0000174/// MeasureTokenLength - Relex the token at the specified location and return
175/// its length in bytes in the input file. If the token needs cleaning (e.g.
176/// includes a trigraph or an escaped newline) then this count includes bytes
177/// that are part of that.
178unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
179 const SourceManager &SM) {
180 // If this comes from a macro expansion, we really do want the macro name, not
181 // the token this macro expanded to.
182 Loc = SM.getLogicalLoc(Loc);
183
184 const char *StrData = SM.getCharacterData(Loc);
185
186 // TODO: this could be special cased for common tokens like identifiers, ')',
187 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
188 // all obviously single-char tokens. This could use
189 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
190 // something.
191
192
193 const char *BufEnd = SM.getBufferData(Loc.getFileID()).second;
194
195 // Create a langops struct and enable trigraphs. This is sufficient for
196 // measuring tokens.
197 LangOptions LangOpts;
198 LangOpts.Trigraphs = true;
199
200 // Create a lexer starting at the beginning of this token.
201 Lexer TheLexer(Loc, LangOpts, StrData, BufEnd);
202 Token TheTok;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000203 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner9a611942007-10-17 21:18:47 +0000204 return TheTok.getLength();
205}
206
Reid Spencer5f016e22007-07-11 17:01:13 +0000207//===----------------------------------------------------------------------===//
208// Character information.
209//===----------------------------------------------------------------------===//
210
211static unsigned char CharInfo[256];
212
213enum {
214 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
215 CHAR_VERT_WS = 0x02, // '\r', '\n'
216 CHAR_LETTER = 0x04, // a-z,A-Z
217 CHAR_NUMBER = 0x08, // 0-9
218 CHAR_UNDER = 0x10, // _
219 CHAR_PERIOD = 0x20 // .
220};
221
222static void InitCharacterInfo() {
223 static bool isInited = false;
224 if (isInited) return;
225 isInited = true;
226
227 // Intiialize the CharInfo table.
228 // TODO: statically initialize this.
229 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
230 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
231 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
232
233 CharInfo[(int)'_'] = CHAR_UNDER;
234 CharInfo[(int)'.'] = CHAR_PERIOD;
235 for (unsigned i = 'a'; i <= 'z'; ++i)
236 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
237 for (unsigned i = '0'; i <= '9'; ++i)
238 CharInfo[i] = CHAR_NUMBER;
239}
240
241/// isIdentifierBody - Return true if this is the body character of an
242/// identifier, which is [a-zA-Z0-9_].
243static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000244 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000245}
246
247/// isHorizontalWhitespace - Return true if this character is horizontal
248/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
249static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000250 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000251}
252
253/// isWhitespace - Return true if this character is horizontal or vertical
254/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
255/// for '\0'.
256static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000257 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000258}
259
260/// isNumberBody - Return true if this is the body character of an
261/// preprocessing number, which is [a-zA-Z0-9_.].
262static inline bool isNumberBody(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000263 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
264 true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000265}
266
267
268//===----------------------------------------------------------------------===//
269// Diagnostics forwarding code.
270//===----------------------------------------------------------------------===//
271
Chris Lattner409a0362007-07-22 18:38:25 +0000272/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
273/// lexer buffer was all instantiated at a single point, perform the mapping.
274/// This is currently only used for _Pragma implementation, so it is the slow
275/// path of the hot getSourceLocation method. Do not allow it to be inlined.
276static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
277 SourceLocation FileLoc,
278 unsigned CharNo) DISABLE_INLINE;
279static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
280 SourceLocation FileLoc,
281 unsigned CharNo) {
282 // Otherwise, we're lexing "mapped tokens". This is used for things like
283 // _Pragma handling. Combine the instantiation location of FileLoc with the
284 // physical location.
285 SourceManager &SourceMgr = PP.getSourceManager();
286
287 // Create a new SLoc which is expanded from logical(FileLoc) but whose
288 // characters come from phys(FileLoc)+Offset.
289 SourceLocation VirtLoc = SourceMgr.getLogicalLoc(FileLoc);
290 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(FileLoc);
291 PhysLoc = SourceLocation::getFileLoc(PhysLoc.getFileID(), CharNo);
292 return SourceMgr.getInstantiationLoc(PhysLoc, VirtLoc);
293}
294
Reid Spencer5f016e22007-07-11 17:01:13 +0000295/// getSourceLocation - Return a source location identifier for the specified
296/// offset in the current file.
297SourceLocation Lexer::getSourceLocation(const char *Loc) const {
Chris Lattner448cec42007-07-22 18:44:36 +0000298 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000299 "Location out of range for this buffer!");
Chris Lattner9dc1f532007-07-20 16:37:10 +0000300
301 // In the normal case, we're just lexing from a simple file buffer, return
302 // the file id from FileLoc with the offset specified.
Chris Lattner448cec42007-07-22 18:44:36 +0000303 unsigned CharNo = Loc-BufferStart;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000304 if (FileLoc.isFileID())
305 return SourceLocation::getFileLoc(FileLoc.getFileID(), CharNo);
306
Chris Lattner168ae2d2007-10-17 20:41:00 +0000307 assert(PP && "This doesn't work on raw lexers");
308 return GetMappedTokenLoc(*PP, FileLoc, CharNo);
Reid Spencer5f016e22007-07-11 17:01:13 +0000309}
310
Reid Spencer5f016e22007-07-11 17:01:13 +0000311/// Diag - Forwarding function for diagnostics. This translate a source
312/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000313DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner3692b092008-11-18 07:59:24 +0000314 return PP->Diag(getSourceLocation(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000315}
Reid Spencer5f016e22007-07-11 17:01:13 +0000316
317//===----------------------------------------------------------------------===//
318// Trigraph and Escaped Newline Handling Code.
319//===----------------------------------------------------------------------===//
320
321/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
322/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
323static char GetTrigraphCharForLetter(char Letter) {
324 switch (Letter) {
325 default: return 0;
326 case '=': return '#';
327 case ')': return ']';
328 case '(': return '[';
329 case '!': return '|';
330 case '\'': return '^';
331 case '>': return '}';
332 case '/': return '\\';
333 case '<': return '{';
334 case '-': return '~';
335 }
336}
337
338/// DecodeTrigraphChar - If the specified character is a legal trigraph when
339/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
340/// return the result character. Finally, emit a warning about trigraph use
341/// whether trigraphs are enabled or not.
342static char DecodeTrigraphChar(const char *CP, Lexer *L) {
343 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner3692b092008-11-18 07:59:24 +0000344 if (!Res || !L) return Res;
345
346 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +0000347 if (!L->isLexingRawMode())
348 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner3692b092008-11-18 07:59:24 +0000349 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000350 }
Chris Lattner3692b092008-11-18 07:59:24 +0000351
Chris Lattner74d15df2008-11-22 02:02:22 +0000352 if (!L->isLexingRawMode())
353 L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000354 return Res;
355}
356
357/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
358/// get its size, and return it. This is tricky in several cases:
359/// 1. If currently at the start of a trigraph, we warn about the trigraph,
360/// then either return the trigraph (skipping 3 chars) or the '?',
361/// depending on whether trigraphs are enabled or not.
362/// 2. If this is an escaped newline (potentially with whitespace between
363/// the backslash and newline), implicitly skip the newline and return
364/// the char after it.
365/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
366///
367/// This handles the slow/uncommon case of the getCharAndSize method. Here we
368/// know that we can accumulate into Size, and that we have already incremented
369/// Ptr by Size bytes.
370///
371/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
372/// be updated to match.
373///
374char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattnerd2177732007-07-20 16:59:19 +0000375 Token *Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 // If we have a slash, look for an escaped newline.
377 if (Ptr[0] == '\\') {
378 ++Size;
379 ++Ptr;
380Slash:
381 // Common case, backslash-char where the char is not whitespace.
382 if (!isWhitespace(Ptr[0])) return '\\';
383
384 // See if we have optional whitespace characters followed by a newline.
385 {
386 unsigned SizeTmp = 0;
387 do {
388 ++SizeTmp;
389 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
390 // Remember that this token needs to be cleaned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000391 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000392
393 // Warn if there was whitespace between the backslash and newline.
Chris Lattner74d15df2008-11-22 02:02:22 +0000394 if (SizeTmp != 1 && Tok && !isLexingRawMode())
Reid Spencer5f016e22007-07-11 17:01:13 +0000395 Diag(Ptr, diag::backslash_newline_space);
396
397 // If this is a \r\n or \n\r, skip the newlines.
398 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
399 Ptr[SizeTmp-1] != Ptr[SizeTmp])
400 ++SizeTmp;
401
402 // Found backslash<whitespace><newline>. Parse the char after it.
403 Size += SizeTmp;
404 Ptr += SizeTmp;
405 // Use slow version to accumulate a correct size field.
406 return getCharAndSizeSlow(Ptr, Size, Tok);
407 }
408 } while (isWhitespace(Ptr[SizeTmp]));
409 }
410
411 // Otherwise, this is not an escaped newline, just return the slash.
412 return '\\';
413 }
414
415 // If this is a trigraph, process it.
416 if (Ptr[0] == '?' && Ptr[1] == '?') {
417 // If this is actually a legal trigraph (not something like "??x"), emit
418 // a trigraph warning. If so, and if trigraphs are enabled, return it.
419 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
420 // Remember that this token needs to be cleaned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000421 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000422
423 Ptr += 3;
424 Size += 3;
425 if (C == '\\') goto Slash;
426 return C;
427 }
428 }
429
430 // If this is neither, return a single character.
431 ++Size;
432 return *Ptr;
433}
434
435
436/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
437/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
438/// and that we have already incremented Ptr by Size bytes.
439///
440/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
441/// be updated to match.
442char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
443 const LangOptions &Features) {
444 // If we have a slash, look for an escaped newline.
445 if (Ptr[0] == '\\') {
446 ++Size;
447 ++Ptr;
448Slash:
449 // Common case, backslash-char where the char is not whitespace.
450 if (!isWhitespace(Ptr[0])) return '\\';
451
452 // See if we have optional whitespace characters followed by a newline.
453 {
454 unsigned SizeTmp = 0;
455 do {
456 ++SizeTmp;
457 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
458
459 // If this is a \r\n or \n\r, skip the newlines.
460 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
461 Ptr[SizeTmp-1] != Ptr[SizeTmp])
462 ++SizeTmp;
463
464 // Found backslash<whitespace><newline>. Parse the char after it.
465 Size += SizeTmp;
466 Ptr += SizeTmp;
467
468 // Use slow version to accumulate a correct size field.
469 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
470 }
471 } while (isWhitespace(Ptr[SizeTmp]));
472 }
473
474 // Otherwise, this is not an escaped newline, just return the slash.
475 return '\\';
476 }
477
478 // If this is a trigraph, process it.
479 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
480 // If this is actually a legal trigraph (not something like "??x"), return
481 // it.
482 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
483 Ptr += 3;
484 Size += 3;
485 if (C == '\\') goto Slash;
486 return C;
487 }
488 }
489
490 // If this is neither, return a single character.
491 ++Size;
492 return *Ptr;
493}
494
495//===----------------------------------------------------------------------===//
496// Helper methods for lexing.
497//===----------------------------------------------------------------------===//
498
Chris Lattnerd2177732007-07-20 16:59:19 +0000499void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000500 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
501 unsigned Size;
502 unsigned char C = *CurPtr++;
503 while (isIdentifierBody(C)) {
504 C = *CurPtr++;
505 }
506 --CurPtr; // Back up over the skipped character.
507
508 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
509 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
510 // FIXME: UCNs.
511 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
512FinishIdentifier:
513 const char *IdStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000514 FormTokenWithChars(Result, CurPtr, tok::identifier);
Reid Spencer5f016e22007-07-11 17:01:13 +0000515
516 // If we are in raw mode, return this identifier raw. There is no need to
517 // look up identifier information or attempt to macro expand it.
518 if (LexingRawMode) return;
519
520 // Fill in Result.IdentifierInfo, looking up the identifier in the
521 // identifier table.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000522 PP->LookUpIdentifierInfo(Result, IdStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000523
524 // Finally, now that we know we have an identifier, pass this off to the
525 // preprocessor, which may macro expand it or something.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000526 return PP->HandleIdentifier(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000527 }
528
529 // Otherwise, $,\,? in identifier found. Enter slower path.
530
531 C = getCharAndSize(CurPtr, Size);
532 while (1) {
533 if (C == '$') {
534 // If we hit a $ and they are not supported in identifiers, we are done.
535 if (!Features.DollarIdents) goto FinishIdentifier;
536
537 // Otherwise, emit a diagnostic and continue.
Chris Lattner74d15df2008-11-22 02:02:22 +0000538 if (!isLexingRawMode())
539 Diag(CurPtr, diag::ext_dollar_in_identifier);
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 CurPtr = ConsumeChar(CurPtr, Size, Result);
541 C = getCharAndSize(CurPtr, Size);
542 continue;
543 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
544 // Found end of identifier.
545 goto FinishIdentifier;
546 }
547
548 // Otherwise, this character is good, consume it.
549 CurPtr = ConsumeChar(CurPtr, Size, Result);
550
551 C = getCharAndSize(CurPtr, Size);
552 while (isIdentifierBody(C)) { // FIXME: UCNs.
553 CurPtr = ConsumeChar(CurPtr, Size, Result);
554 C = getCharAndSize(CurPtr, Size);
555 }
556 }
557}
558
559
Nate Begeman5253c7f2008-04-14 02:26:39 +0000560/// LexNumericConstant - Lex the remainder of a integer or floating point
Reid Spencer5f016e22007-07-11 17:01:13 +0000561/// constant. From[-1] is the first character lexed. Return the end of the
562/// constant.
Chris Lattnerd2177732007-07-20 16:59:19 +0000563void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000564 unsigned Size;
565 char C = getCharAndSize(CurPtr, Size);
566 char PrevCh = 0;
567 while (isNumberBody(C)) { // FIXME: UCNs?
568 CurPtr = ConsumeChar(CurPtr, Size, Result);
569 PrevCh = C;
570 C = getCharAndSize(CurPtr, Size);
571 }
572
573 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
574 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
575 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
576
577 // If we have a hex FP constant, continue.
Chris Lattner49842122008-11-22 07:39:03 +0000578 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
579 (Features.HexFloats || !Features.NoExtensions))
Reid Spencer5f016e22007-07-11 17:01:13 +0000580 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
581
Reid Spencer5f016e22007-07-11 17:01:13 +0000582 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +0000583 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Reid Spencer5f016e22007-07-11 17:01:13 +0000584}
585
586/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
587/// either " or L".
Chris Lattnerd88dc482008-10-12 04:05:48 +0000588void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000589 const char *NulCharacter = 0; // Does this string contain the \0 character?
590
591 char C = getAndAdvanceChar(CurPtr, Result);
592 while (C != '"') {
593 // Skip escaped characters.
594 if (C == '\\') {
595 // Skip the escaped character.
596 C = getAndAdvanceChar(CurPtr, Result);
597 } else if (C == '\n' || C == '\r' || // Newline.
598 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner74d15df2008-11-22 02:02:22 +0000599 if (!isLexingRawMode())
600 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000601 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000602 return;
603 } else if (C == 0) {
604 NulCharacter = CurPtr-1;
605 }
606 C = getAndAdvanceChar(CurPtr, Result);
607 }
608
609 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000610 if (NulCharacter && !isLexingRawMode())
611 Diag(NulCharacter, diag::null_in_string);
Reid Spencer5f016e22007-07-11 17:01:13 +0000612
Reid Spencer5f016e22007-07-11 17:01:13 +0000613 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner9e6293d2008-10-12 04:51:35 +0000614 FormTokenWithChars(Result, CurPtr,
615 Wide ? tok::wide_string_literal : tok::string_literal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000616}
617
618/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
619/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattnerd2177732007-07-20 16:59:19 +0000620void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000621 const char *NulCharacter = 0; // Does this string contain the \0 character?
622
623 char C = getAndAdvanceChar(CurPtr, Result);
624 while (C != '>') {
625 // Skip escaped characters.
626 if (C == '\\') {
627 // Skip the escaped character.
628 C = getAndAdvanceChar(CurPtr, Result);
629 } else if (C == '\n' || C == '\r' || // Newline.
630 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner74d15df2008-11-22 02:02:22 +0000631 if (!isLexingRawMode())
632 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000633 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 return;
635 } else if (C == 0) {
636 NulCharacter = CurPtr-1;
637 }
638 C = getAndAdvanceChar(CurPtr, Result);
639 }
640
641 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000642 if (NulCharacter && !isLexingRawMode())
643 Diag(NulCharacter, diag::null_in_string);
Reid Spencer5f016e22007-07-11 17:01:13 +0000644
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +0000646 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000647}
648
649
650/// LexCharConstant - Lex the remainder of a character constant, after having
651/// lexed either ' or L'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000652void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000653 const char *NulCharacter = 0; // Does this character contain the \0 character?
654
655 // Handle the common case of 'x' and '\y' efficiently.
656 char C = getAndAdvanceChar(CurPtr, Result);
657 if (C == '\'') {
Chris Lattner74d15df2008-11-22 02:02:22 +0000658 if (!isLexingRawMode())
659 Diag(BufferPtr, diag::err_empty_character);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000660 FormTokenWithChars(Result, CurPtr, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 return;
662 } else if (C == '\\') {
663 // Skip the escaped character.
664 // FIXME: UCN's.
665 C = getAndAdvanceChar(CurPtr, Result);
666 }
667
668 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
669 ++CurPtr;
670 } else {
671 // Fall back on generic code for embedded nulls, newlines, wide chars.
672 do {
673 // Skip escaped characters.
674 if (C == '\\') {
675 // Skip the escaped character.
676 C = getAndAdvanceChar(CurPtr, Result);
677 } else if (C == '\n' || C == '\r' || // Newline.
678 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner74d15df2008-11-22 02:02:22 +0000679 if (!isLexingRawMode())
680 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000681 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 return;
683 } else if (C == 0) {
684 NulCharacter = CurPtr-1;
685 }
686 C = getAndAdvanceChar(CurPtr, Result);
687 } while (C != '\'');
688 }
689
Chris Lattner74d15df2008-11-22 02:02:22 +0000690 if (NulCharacter && !isLexingRawMode())
691 Diag(NulCharacter, diag::null_in_char);
Reid Spencer5f016e22007-07-11 17:01:13 +0000692
Reid Spencer5f016e22007-07-11 17:01:13 +0000693 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +0000694 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Reid Spencer5f016e22007-07-11 17:01:13 +0000695}
696
697/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
698/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattnerd88dc482008-10-12 04:05:48 +0000699///
700/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
701///
702bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 // Whitespace - Skip it, then return the token after the whitespace.
704 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
705 while (1) {
706 // Skip horizontal whitespace very aggressively.
707 while (isHorizontalWhitespace(Char))
708 Char = *++CurPtr;
709
Daniel Dunbarddd3e8b2008-11-25 00:20:22 +0000710 // Otherwise if we have something other than whitespace, we're done.
Reid Spencer5f016e22007-07-11 17:01:13 +0000711 if (Char != '\n' && Char != '\r')
712 break;
713
714 if (ParsingPreprocessorDirective) {
715 // End of preprocessor directive line, let LexTokenInternal handle this.
716 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +0000717 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000718 }
719
720 // ok, but handle newline.
721 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +0000722 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000723 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +0000724 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 Char = *++CurPtr;
726 }
727
728 // If this isn't immediately after a newline, there is leading space.
729 char PrevChar = CurPtr[-1];
730 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattnerd2177732007-07-20 16:59:19 +0000731 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000732
Chris Lattnerd88dc482008-10-12 04:05:48 +0000733 // If the client wants us to return whitespace, return it now.
734 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +0000735 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +0000736 return true;
737 }
738
Reid Spencer5f016e22007-07-11 17:01:13 +0000739 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +0000740 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000741}
742
743// SkipBCPLComment - We have just read the // characters from input. Skip until
744// we find the newline character thats terminate the comment. Then update
Chris Lattner2d381892008-10-12 04:15:42 +0000745/// BufferPtr and return. If we're in KeepCommentMode, this will form the token
746/// and return true.
Chris Lattnerd2177732007-07-20 16:59:19 +0000747bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000748 // If BCPL comments aren't explicitly enabled for this language, emit an
749 // extension warning.
Chris Lattner74d15df2008-11-22 02:02:22 +0000750 if (!Features.BCPLComment && !isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 Diag(BufferPtr, diag::ext_bcpl_comment);
752
753 // Mark them enabled so we only emit one warning for this translation
754 // unit.
755 Features.BCPLComment = true;
756 }
757
758 // Scan over the body of the comment. The common case, when scanning, is that
759 // the comment contains normal ascii characters with nothing interesting in
760 // them. As such, optimize for this case with the inner loop.
761 char C;
762 do {
763 C = *CurPtr;
764 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
765 // If we find a \n character, scan backwards, checking to see if it's an
766 // escaped newline, like we do for block comments.
767
768 // Skip over characters in the fast loop.
769 while (C != 0 && // Potentially EOF.
770 C != '\\' && // Potentially escaped newline.
771 C != '?' && // Potentially trigraph.
772 C != '\n' && C != '\r') // Newline or DOS-style newline.
773 C = *++CurPtr;
774
775 // If this is a newline, we're done.
776 if (C == '\n' || C == '\r')
777 break; // Found the newline? Break out!
778
779 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000780 // properly decode the character. Read it in raw mode to avoid emitting
781 // diagnostics about things like trigraphs. If we see an escaped newline,
782 // we'll handle it below.
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 const char *OldPtr = CurPtr;
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000784 bool OldRawMode = isLexingRawMode();
785 LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000786 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000787 LexingRawMode = OldRawMode;
Reid Spencer5f016e22007-07-11 17:01:13 +0000788
789 // If we read multiple characters, and one of those characters was a \r or
790 // \n, then we had an escaped newline within the comment. Emit diagnostic
791 // unless the next line is also a // comment.
792 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
793 for (; OldPtr != CurPtr; ++OldPtr)
794 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
795 // Okay, we found a // comment that ends in a newline, if the next
796 // line is also a // comment, but has spaces, don't emit a diagnostic.
797 if (isspace(C)) {
798 const char *ForwardPtr = CurPtr;
799 while (isspace(*ForwardPtr)) // Skip whitespace.
800 ++ForwardPtr;
801 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
802 break;
803 }
804
Chris Lattner74d15df2008-11-22 02:02:22 +0000805 if (!isLexingRawMode())
806 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 break;
808 }
809 }
810
811 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
812 } while (C != '\n' && C != '\r');
813
814 // Found but did not consume the newline.
815
816 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +0000817 if (inKeepCommentMode())
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 return SaveBCPLComment(Result, CurPtr);
819
820 // If we are inside a preprocessor directive and we see the end of line,
821 // return immediately, so that the lexer can return this as an EOM token.
822 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
823 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +0000824 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 }
826
827 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner7a4f0042008-10-12 00:23:07 +0000828 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattnerd88dc482008-10-12 04:05:48 +0000829 // contribute to another token), it isn't needed for correctness. Note that
830 // this is ok even in KeepWhitespaceMode, because we would have returned the
831 /// comment above in that mode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000832 ++CurPtr;
833
834 // The next returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +0000835 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +0000837 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000838 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +0000839 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000840}
841
842/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
843/// an appropriate way and return it.
Chris Lattnerd2177732007-07-20 16:59:19 +0000844bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner9e6293d2008-10-12 04:51:35 +0000845 // If we're not in a preprocessor directive, just return the // comment
846 // directly.
847 FormTokenWithChars(Result, CurPtr, tok::comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000848
Chris Lattner9e6293d2008-10-12 04:51:35 +0000849 if (!ParsingPreprocessorDirective)
850 return true;
851
852 // If this BCPL-style comment is in a macro definition, transmogrify it into
853 // a C-style block comment.
854 std::string Spelling = PP->getSpelling(Result);
855 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
856 Spelling[1] = '*'; // Change prefix to "/*".
857 Spelling += "*/"; // add suffix.
858
859 Result.setKind(tok::comment);
860 Result.setLocation(PP->CreateString(&Spelling[0], Spelling.size(),
861 Result.getLocation()));
862 Result.setLength(Spelling.size());
Chris Lattner2d381892008-10-12 04:15:42 +0000863 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000864}
865
866/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
867/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner47a2b402008-12-12 07:14:34 +0000868/// diagnostic if so. We know that the newline is inside of a block comment.
Reid Spencer5f016e22007-07-11 17:01:13 +0000869static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
870 Lexer *L) {
871 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
872
873 // Back up off the newline.
874 --CurPtr;
875
876 // If this is a two-character newline sequence, skip the other character.
877 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
878 // \n\n or \r\r -> not escaped newline.
879 if (CurPtr[0] == CurPtr[1])
880 return false;
881 // \n\r or \r\n -> skip the newline.
882 --CurPtr;
883 }
884
885 // If we have horizontal whitespace, skip over it. We allow whitespace
886 // between the slash and newline.
887 bool HasSpace = false;
888 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
889 --CurPtr;
890 HasSpace = true;
891 }
892
893 // If we have a slash, we know this is an escaped newline.
894 if (*CurPtr == '\\') {
895 if (CurPtr[-1] != '*') return false;
896 } else {
897 // It isn't a slash, is it the ?? / trigraph?
898 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
899 CurPtr[-3] != '*')
900 return false;
901
902 // This is the trigraph ending the comment. Emit a stern warning!
903 CurPtr -= 2;
904
905 // If no trigraphs are enabled, warn that we ignored this trigraph and
906 // ignore this * character.
907 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +0000908 if (!L->isLexingRawMode())
909 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 return false;
911 }
Chris Lattner74d15df2008-11-22 02:02:22 +0000912 if (!L->isLexingRawMode())
913 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000914 }
915
916 // Warn about having an escaped newline between the */ characters.
Chris Lattner74d15df2008-11-22 02:02:22 +0000917 if (!L->isLexingRawMode())
918 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Reid Spencer5f016e22007-07-11 17:01:13 +0000919
920 // If there was space between the backslash and newline, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000921 if (HasSpace && !L->isLexingRawMode())
922 L->Diag(CurPtr, diag::backslash_newline_space);
Reid Spencer5f016e22007-07-11 17:01:13 +0000923
924 return true;
925}
926
927#ifdef __SSE2__
928#include <emmintrin.h>
929#elif __ALTIVEC__
930#include <altivec.h>
931#undef bool
932#endif
933
934/// SkipBlockComment - We have just read the /* characters from input. Read
935/// until we find the */ characters that terminate the comment. Note that we
936/// don't bother decoding trigraphs or escaped newlines in block comments,
937/// because they cannot cause the comment to end. The only thing that can
938/// happen is the comment could end with an escaped newline between the */ end
939/// of comment.
Chris Lattner2d381892008-10-12 04:15:42 +0000940///
941/// If KeepCommentMode is enabled, this forms a token from the comment and
942/// returns true.
Chris Lattnerd2177732007-07-20 16:59:19 +0000943bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000944 // Scan one character past where we should, looking for a '/' character. Once
945 // we find it, check to see if it was preceeded by a *. This common
946 // optimization helps people who like to put a lot of * characters in their
947 // comments.
Chris Lattner8146b682007-07-21 23:43:37 +0000948
949 // The first character we get with newlines and trigraphs skipped to handle
950 // the degenerate /*/ case below correctly if the * has an escaped newline
951 // after it.
952 unsigned CharSize;
953 unsigned char C = getCharAndSize(CurPtr, CharSize);
954 CurPtr += CharSize;
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner74d15df2008-11-22 02:02:22 +0000956 if (!isLexingRawMode())
Chris Lattner0af57422008-10-12 01:31:51 +0000957 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner31f0eca2008-10-12 04:19:49 +0000958 --CurPtr;
959
960 // KeepWhitespaceMode should return this broken comment as a token. Since
961 // it isn't a well formed comment, just return it as an 'unknown' token.
962 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +0000963 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +0000964 return true;
965 }
966
967 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +0000968 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 }
970
Chris Lattner8146b682007-07-21 23:43:37 +0000971 // Check to see if the first character after the '/*' is another /. If so,
972 // then this slash does not end the block comment, it is part of it.
973 if (C == '/')
974 C = *CurPtr++;
975
Reid Spencer5f016e22007-07-11 17:01:13 +0000976 while (1) {
977 // Skip over all non-interesting characters until we find end of buffer or a
978 // (probably ending) '/' character.
979 if (CurPtr + 24 < BufferEnd) {
980 // While not aligned to a 16-byte boundary.
981 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
982 C = *CurPtr++;
983
984 if (C == '/') goto FoundSlash;
985
986#ifdef __SSE2__
987 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
988 '/', '/', '/', '/', '/', '/', '/', '/');
989 while (CurPtr+16 <= BufferEnd &&
990 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
991 CurPtr += 16;
992#elif __ALTIVEC__
993 __vector unsigned char Slashes = {
994 '/', '/', '/', '/', '/', '/', '/', '/',
995 '/', '/', '/', '/', '/', '/', '/', '/'
996 };
997 while (CurPtr+16 <= BufferEnd &&
998 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
999 CurPtr += 16;
1000#else
1001 // Scan for '/' quickly. Many block comments are very large.
1002 while (CurPtr[0] != '/' &&
1003 CurPtr[1] != '/' &&
1004 CurPtr[2] != '/' &&
1005 CurPtr[3] != '/' &&
1006 CurPtr+4 < BufferEnd) {
1007 CurPtr += 4;
1008 }
1009#endif
1010
1011 // It has to be one of the bytes scanned, increment to it and read one.
1012 C = *CurPtr++;
1013 }
1014
1015 // Loop to scan the remainder.
1016 while (C != '/' && C != '\0')
1017 C = *CurPtr++;
1018
1019 FoundSlash:
1020 if (C == '/') {
1021 if (CurPtr[-2] == '*') // We found the final */. We're done!
1022 break;
1023
1024 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
1025 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
1026 // We found the final */, though it had an escaped newline between the
1027 // * and /. We're done!
1028 break;
1029 }
1030 }
1031 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1032 // If this is a /* inside of the comment, emit a warning. Don't do this
1033 // if this is a /*/, which will end the comment. This misses cases with
1034 // embedded escaped newlines, but oh well.
Chris Lattner74d15df2008-11-22 02:02:22 +00001035 if (!isLexingRawMode())
1036 Diag(CurPtr-1, diag::warn_nested_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 }
1038 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001039 if (!isLexingRawMode())
1040 Diag(BufferPtr, diag::err_unterminated_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 // Note: the user probably forgot a */. We could continue immediately
1042 // after the /*, but this would involve lexing a lot of what really is the
1043 // comment, which surely would confuse the parser.
Chris Lattner31f0eca2008-10-12 04:19:49 +00001044 --CurPtr;
1045
1046 // KeepWhitespaceMode should return this broken comment as a token. Since
1047 // it isn't a well formed comment, just return it as an 'unknown' token.
1048 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001049 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001050 return true;
1051 }
1052
1053 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001054 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001055 }
1056 C = *CurPtr++;
1057 }
1058
1059 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +00001060 if (inKeepCommentMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001061 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattner2d381892008-10-12 04:15:42 +00001062 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001063 }
1064
1065 // It is common for the tokens immediately after a /**/ comment to be
1066 // whitespace. Instead of going through the big switch, handle it
Chris Lattnerd88dc482008-10-12 04:05:48 +00001067 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1068 // have already returned above with the comment as a token.
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001070 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 SkipWhitespace(Result, CurPtr+1);
Chris Lattner2d381892008-10-12 04:15:42 +00001072 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001073 }
1074
1075 // Otherwise, just return so that the next character will be lexed as a token.
1076 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001077 Result.setFlag(Token::LeadingSpace);
Chris Lattner2d381892008-10-12 04:15:42 +00001078 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001079}
1080
1081//===----------------------------------------------------------------------===//
1082// Primary Lexing Entry Points
1083//===----------------------------------------------------------------------===//
1084
Reid Spencer5f016e22007-07-11 17:01:13 +00001085/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1086/// uninterpreted string. This switches the lexer out of directive mode.
1087std::string Lexer::ReadToEndOfLine() {
1088 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1089 "Must be in a preprocessing directive!");
1090 std::string Result;
Chris Lattnerd2177732007-07-20 16:59:19 +00001091 Token Tmp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001092
1093 // CurPtr - Cache BufferPtr in an automatic variable.
1094 const char *CurPtr = BufferPtr;
1095 while (1) {
1096 char Char = getAndAdvanceChar(CurPtr, Tmp);
1097 switch (Char) {
1098 default:
1099 Result += Char;
1100 break;
1101 case 0: // Null.
1102 // Found end of file?
1103 if (CurPtr-1 != BufferEnd) {
1104 // Nope, normal character, continue.
1105 Result += Char;
1106 break;
1107 }
1108 // FALL THROUGH.
1109 case '\r':
1110 case '\n':
1111 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1112 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1113 BufferPtr = CurPtr-1;
1114
1115 // Next, lex the character, which should handle the EOM transition.
1116 Lex(Tmp);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001117 assert(Tmp.is(tok::eom) && "Unexpected token!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001118
1119 // Finally, we're done, return the string we found.
1120 return Result;
1121 }
1122 }
1123}
1124
1125/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1126/// condition, reporting diagnostics and handling other edge cases as required.
1127/// This returns true if Result contains a token, false if PP.Lex should be
1128/// called again.
Chris Lattnerd2177732007-07-20 16:59:19 +00001129bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 // If we hit the end of the file while parsing a preprocessor directive,
1131 // end the preprocessor directive first. The next token returned will
1132 // then be the end of file.
1133 if (ParsingPreprocessorDirective) {
1134 // Done parsing the "line".
1135 ParsingPreprocessorDirective = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00001137 FormTokenWithChars(Result, CurPtr, tok::eom);
Reid Spencer5f016e22007-07-11 17:01:13 +00001138
1139 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001140 SetCommentRetentionState(PP->getCommentRetentionState());
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 return true; // Have a token.
1142 }
1143
1144 // If we are in raw mode, return this event as an EOF token. Let the caller
1145 // that put us in raw mode handle the event.
Chris Lattner74d15df2008-11-22 02:02:22 +00001146 if (isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 Result.startToken();
1148 BufferPtr = BufferEnd;
Chris Lattner9e6293d2008-10-12 04:51:35 +00001149 FormTokenWithChars(Result, BufferEnd, tok::eof);
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 return true;
1151 }
1152
1153 // Otherwise, issue diagnostics for unterminated #if and missing newline.
1154
1155 // If we are in a #if directive, emit an error.
1156 while (!ConditionalStack.empty()) {
Chris Lattner30c64762008-11-22 06:22:39 +00001157 PP->Diag(ConditionalStack.back().IfLoc,
1158 diag::err_pp_unterminated_conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +00001159 ConditionalStack.pop_back();
1160 }
1161
Chris Lattnerb25e5d72008-04-12 05:54:25 +00001162 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1163 // a pedwarn.
1164 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 Diag(BufferEnd, diag::ext_no_newline_eof);
1166
1167 BufferPtr = CurPtr;
1168
1169 // Finally, let the preprocessor handle this.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001170 return PP->HandleEndOfFile(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001171}
1172
1173/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1174/// the specified lexer will return a tok::l_paren token, 0 if it is something
1175/// else and 2 if there are no more tokens in the buffer controlled by the
1176/// lexer.
1177unsigned Lexer::isNextPPTokenLParen() {
1178 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1179
1180 // Switch to 'skipping' mode. This will ensure that we can lex a token
1181 // without emitting diagnostics, disables macro expansion, and will cause EOF
1182 // to return an EOF token instead of popping the include stack.
1183 LexingRawMode = true;
1184
1185 // Save state that can be changed while lexing so that we can restore it.
1186 const char *TmpBufferPtr = BufferPtr;
1187
Chris Lattnerd2177732007-07-20 16:59:19 +00001188 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 Tok.startToken();
1190 LexTokenInternal(Tok);
1191
1192 // Restore state that may have changed.
1193 BufferPtr = TmpBufferPtr;
1194
1195 // Restore the lexer back to non-skipping mode.
1196 LexingRawMode = false;
1197
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001198 if (Tok.is(tok::eof))
Reid Spencer5f016e22007-07-11 17:01:13 +00001199 return 2;
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001200 return Tok.is(tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001201}
1202
1203
1204/// LexTokenInternal - This implements a simple C family lexer. It is an
1205/// extremely performance critical piece of code. This assumes that the buffer
1206/// has a null character at the end of the file. Return true if an error
1207/// occurred and compilation should terminate, false if normal. This returns a
1208/// preprocessing token, not a normal token, as such, it is an internal
1209/// interface. It assumes that the Flags of result have been cleared before
1210/// calling this.
Chris Lattnerd2177732007-07-20 16:59:19 +00001211void Lexer::LexTokenInternal(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001212LexNextToken:
1213 // New token, can't need cleaning yet.
Chris Lattnerd2177732007-07-20 16:59:19 +00001214 Result.clearFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 Result.setIdentifierInfo(0);
1216
1217 // CurPtr - Cache BufferPtr in an automatic variable.
1218 const char *CurPtr = BufferPtr;
1219
1220 // Small amounts of horizontal whitespace is very common between tokens.
1221 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1222 ++CurPtr;
1223 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1224 ++CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +00001225
1226 // If we are keeping whitespace and other tokens, just return what we just
1227 // skipped. The next lexer invocation will return the token after the
1228 // whitespace.
1229 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001230 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001231 return;
1232 }
1233
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001235 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 }
1237
1238 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1239
1240 // Read a character, advancing over it.
1241 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001242 tok::TokenKind Kind;
1243
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 switch (Char) {
1245 case 0: // Null.
1246 // Found end of file?
1247 if (CurPtr-1 == BufferEnd) {
1248 // Read the PP instance variable into an automatic variable, because
1249 // LexEndOfFile will often delete 'this'.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001250 Preprocessor *PPCache = PP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1252 return; // Got a token to return.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001253 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1254 return PPCache->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001255 }
1256
Chris Lattner74d15df2008-11-22 02:02:22 +00001257 if (!isLexingRawMode())
1258 Diag(CurPtr-1, diag::null_in_file);
Chris Lattnerd2177732007-07-20 16:59:19 +00001259 Result.setFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001260 if (SkipWhitespace(Result, CurPtr))
1261 return; // KeepWhitespaceMode
1262
Reid Spencer5f016e22007-07-11 17:01:13 +00001263 goto LexNextToken; // GCC isn't tail call eliminating.
1264 case '\n':
1265 case '\r':
1266 // If we are inside a preprocessor directive and we see the end of line,
1267 // we know we are done with the directive, so return an EOM token.
1268 if (ParsingPreprocessorDirective) {
1269 // Done parsing the "line".
1270 ParsingPreprocessorDirective = false;
1271
1272 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001273 SetCommentRetentionState(PP->getCommentRetentionState());
Reid Spencer5f016e22007-07-11 17:01:13 +00001274
1275 // Since we consumed a newline, we are back at the start of a line.
1276 IsAtStartOfLine = true;
1277
Chris Lattner9e6293d2008-10-12 04:51:35 +00001278 Kind = tok::eom;
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 break;
1280 }
1281 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +00001282 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001283 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +00001284 Result.clearFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001285
1286 if (SkipWhitespace(Result, CurPtr))
1287 return; // KeepWhitespaceMode
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 goto LexNextToken; // GCC isn't tail call eliminating.
1289 case ' ':
1290 case '\t':
1291 case '\f':
1292 case '\v':
Chris Lattner8133cfc2007-07-22 06:29:05 +00001293 SkipHorizontalWhitespace:
Chris Lattnerd2177732007-07-20 16:59:19 +00001294 Result.setFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001295 if (SkipWhitespace(Result, CurPtr))
1296 return; // KeepWhitespaceMode
Chris Lattner8133cfc2007-07-22 06:29:05 +00001297
1298 SkipIgnoredUnits:
1299 CurPtr = BufferPtr;
1300
1301 // If the next token is obviously a // or /* */ comment, skip it efficiently
1302 // too (without going through the big switch stmt).
Chris Lattnerfa95a012008-10-12 03:22:02 +00001303 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode()) {
Chris Lattner8133cfc2007-07-22 06:29:05 +00001304 SkipBCPLComment(Result, CurPtr+2);
1305 goto SkipIgnoredUnits;
Chris Lattnerfa95a012008-10-12 03:22:02 +00001306 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner8133cfc2007-07-22 06:29:05 +00001307 SkipBlockComment(Result, CurPtr+2);
1308 goto SkipIgnoredUnits;
1309 } else if (isHorizontalWhitespace(*CurPtr)) {
1310 goto SkipHorizontalWhitespace;
1311 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 goto LexNextToken; // GCC isn't tail call eliminating.
1313
Chris Lattner3a570772008-01-03 17:58:54 +00001314 // C99 6.4.4.1: Integer Constants.
1315 // C99 6.4.4.2: Floating Constants.
1316 case '0': case '1': case '2': case '3': case '4':
1317 case '5': case '6': case '7': case '8': case '9':
1318 // Notify MIOpt that we read a non-whitespace/non-comment token.
1319 MIOpt.ReadToken();
1320 return LexNumericConstant(Result, CurPtr);
1321
1322 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Reid Spencer5f016e22007-07-11 17:01:13 +00001323 // Notify MIOpt that we read a non-whitespace/non-comment token.
1324 MIOpt.ReadToken();
1325 Char = getCharAndSize(CurPtr, SizeTmp);
1326
1327 // Wide string literal.
1328 if (Char == '"')
1329 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1330 true);
1331
1332 // Wide character constant.
1333 if (Char == '\'')
1334 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1335 // FALL THROUGH, treating L like the start of an identifier.
1336
1337 // C99 6.4.2: Identifiers.
1338 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1339 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1340 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1341 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1342 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1343 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1344 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1345 case 'v': case 'w': case 'x': case 'y': case 'z':
1346 case '_':
1347 // Notify MIOpt that we read a non-whitespace/non-comment token.
1348 MIOpt.ReadToken();
1349 return LexIdentifier(Result, CurPtr);
Chris Lattner3a570772008-01-03 17:58:54 +00001350
1351 case '$': // $ in identifiers.
1352 if (Features.DollarIdents) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001353 if (!isLexingRawMode())
1354 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner3a570772008-01-03 17:58:54 +00001355 // Notify MIOpt that we read a non-whitespace/non-comment token.
1356 MIOpt.ReadToken();
1357 return LexIdentifier(Result, CurPtr);
1358 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001359
Chris Lattner9e6293d2008-10-12 04:51:35 +00001360 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00001361 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001362
1363 // C99 6.4.4: Character Constants.
1364 case '\'':
1365 // Notify MIOpt that we read a non-whitespace/non-comment token.
1366 MIOpt.ReadToken();
1367 return LexCharConstant(Result, CurPtr);
1368
1369 // C99 6.4.5: String Literals.
1370 case '"':
1371 // Notify MIOpt that we read a non-whitespace/non-comment token.
1372 MIOpt.ReadToken();
1373 return LexStringLiteral(Result, CurPtr, false);
1374
1375 // C99 6.4.6: Punctuators.
1376 case '?':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001377 Kind = tok::question;
Reid Spencer5f016e22007-07-11 17:01:13 +00001378 break;
1379 case '[':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001380 Kind = tok::l_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001381 break;
1382 case ']':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001383 Kind = tok::r_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001384 break;
1385 case '(':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001386 Kind = tok::l_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001387 break;
1388 case ')':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001389 Kind = tok::r_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001390 break;
1391 case '{':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001392 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001393 break;
1394 case '}':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001395 Kind = tok::r_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001396 break;
1397 case '.':
1398 Char = getCharAndSize(CurPtr, SizeTmp);
1399 if (Char >= '0' && Char <= '9') {
1400 // Notify MIOpt that we read a non-whitespace/non-comment token.
1401 MIOpt.ReadToken();
1402
1403 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1404 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001405 Kind = tok::periodstar;
Reid Spencer5f016e22007-07-11 17:01:13 +00001406 CurPtr += SizeTmp;
1407 } else if (Char == '.' &&
1408 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001409 Kind = tok::ellipsis;
Reid Spencer5f016e22007-07-11 17:01:13 +00001410 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1411 SizeTmp2, Result);
1412 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001413 Kind = tok::period;
Reid Spencer5f016e22007-07-11 17:01:13 +00001414 }
1415 break;
1416 case '&':
1417 Char = getCharAndSize(CurPtr, SizeTmp);
1418 if (Char == '&') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001419 Kind = tok::ampamp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001420 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1421 } else if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001422 Kind = tok::ampequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001423 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1424 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001425 Kind = tok::amp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001426 }
1427 break;
1428 case '*':
1429 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001430 Kind = tok::starequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001431 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1432 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001433 Kind = tok::star;
Reid Spencer5f016e22007-07-11 17:01:13 +00001434 }
1435 break;
1436 case '+':
1437 Char = getCharAndSize(CurPtr, SizeTmp);
1438 if (Char == '+') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001439 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001440 Kind = tok::plusplus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001441 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001442 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001443 Kind = tok::plusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001444 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001445 Kind = tok::plus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001446 }
1447 break;
1448 case '-':
1449 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001450 if (Char == '-') { // --
Reid Spencer5f016e22007-07-11 17:01:13 +00001451 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001452 Kind = tok::minusminus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001453 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattner9e6293d2008-10-12 04:51:35 +00001454 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Reid Spencer5f016e22007-07-11 17:01:13 +00001455 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1456 SizeTmp2, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001457 Kind = tok::arrowstar;
1458 } else if (Char == '>') { // ->
Reid Spencer5f016e22007-07-11 17:01:13 +00001459 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001460 Kind = tok::arrow;
1461 } else if (Char == '=') { // -=
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001463 Kind = tok::minusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001464 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001465 Kind = tok::minus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001466 }
1467 break;
1468 case '~':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001469 Kind = tok::tilde;
Reid Spencer5f016e22007-07-11 17:01:13 +00001470 break;
1471 case '!':
1472 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001473 Kind = tok::exclaimequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001474 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1475 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001476 Kind = tok::exclaim;
Reid Spencer5f016e22007-07-11 17:01:13 +00001477 }
1478 break;
1479 case '/':
1480 // 6.4.9: Comments
1481 Char = getCharAndSize(CurPtr, SizeTmp);
1482 if (Char == '/') { // BCPL comment.
Chris Lattner2d381892008-10-12 04:15:42 +00001483 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1484 return; // KeepCommentMode
1485
1486 // It is common for the tokens immediately after a // comment to be
1487 // whitespace (indentation for the next line). Instead of going through
1488 // the big switch, handle it efficiently now.
1489 goto SkipIgnoredUnits;
Reid Spencer5f016e22007-07-11 17:01:13 +00001490 } else if (Char == '*') { // /**/ comment.
1491 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner2d381892008-10-12 04:15:42 +00001492 return; // KeepCommentMode
1493 goto LexNextToken; // GCC isn't tail call eliminating.
Reid Spencer5f016e22007-07-11 17:01:13 +00001494 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001495 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001496 Kind = tok::slashequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001497 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001498 Kind = tok::slash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001499 }
1500 break;
1501 case '%':
1502 Char = getCharAndSize(CurPtr, SizeTmp);
1503 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001504 Kind = tok::percentequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001505 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1506 } else if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001507 Kind = tok::r_brace; // '%>' -> '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001508 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1509 } else if (Features.Digraphs && Char == ':') {
1510 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1511 Char = getCharAndSize(CurPtr, SizeTmp);
1512 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001513 Kind = tok::hashhash; // '%:%:' -> '##'
Reid Spencer5f016e22007-07-11 17:01:13 +00001514 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1515 SizeTmp2, Result);
1516 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Reid Spencer5f016e22007-07-11 17:01:13 +00001517 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner74d15df2008-11-22 02:02:22 +00001518 if (!isLexingRawMode())
1519 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001520 Kind = tok::hashat;
Reid Spencer5f016e22007-07-11 17:01:13 +00001521 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001522 Kind = tok::hash; // '%:' -> '#'
Reid Spencer5f016e22007-07-11 17:01:13 +00001523
1524 // We parsed a # character. If this occurs at the start of the line,
1525 // it's actually the start of a preprocessing directive. Callback to
1526 // the preprocessor to handle it.
1527 // FIXME: -fpreprocessed mode??
1528 if (Result.isAtStartOfLine() && !LexingRawMode) {
1529 BufferPtr = CurPtr;
Chris Lattner168ae2d2007-10-17 20:41:00 +00001530 PP->HandleDirective(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001531
1532 // As an optimization, if the preprocessor didn't switch lexers, tail
1533 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001534 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001535 // Start a new token. If this is a #include or something, the PP may
1536 // want us starting at the beginning of the line again. If so, set
1537 // the StartOfLine flag.
1538 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001539 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001540 IsAtStartOfLine = false;
1541 }
1542 goto LexNextToken; // GCC isn't tail call eliminating.
1543 }
1544
Chris Lattner168ae2d2007-10-17 20:41:00 +00001545 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001546 }
1547 }
1548 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001549 Kind = tok::percent;
Reid Spencer5f016e22007-07-11 17:01:13 +00001550 }
1551 break;
1552 case '<':
1553 Char = getCharAndSize(CurPtr, SizeTmp);
1554 if (ParsingFilename) {
1555 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1556 } else if (Char == '<' &&
1557 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001558 Kind = tok::lesslessequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001559 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1560 SizeTmp2, Result);
1561 } else if (Char == '<') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001562 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001563 Kind = tok::lessless;
Reid Spencer5f016e22007-07-11 17:01:13 +00001564 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001566 Kind = tok::lessequal;
1567 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Reid Spencer5f016e22007-07-11 17:01:13 +00001568 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001569 Kind = tok::l_square;
1570 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Reid Spencer5f016e22007-07-11 17:01:13 +00001571 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001572 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001573 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001574 Kind = tok::less;
Reid Spencer5f016e22007-07-11 17:01:13 +00001575 }
1576 break;
1577 case '>':
1578 Char = getCharAndSize(CurPtr, SizeTmp);
1579 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001580 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001581 Kind = tok::greaterequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001582 } else if (Char == '>' &&
1583 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001584 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1585 SizeTmp2, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001586 Kind = tok::greatergreaterequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001587 } else if (Char == '>') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001588 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001589 Kind = tok::greatergreater;
Reid Spencer5f016e22007-07-11 17:01:13 +00001590 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001591 Kind = tok::greater;
Reid Spencer5f016e22007-07-11 17:01:13 +00001592 }
1593 break;
1594 case '^':
1595 Char = getCharAndSize(CurPtr, SizeTmp);
1596 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001597 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001598 Kind = tok::caretequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001599 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001600 Kind = tok::caret;
Reid Spencer5f016e22007-07-11 17:01:13 +00001601 }
1602 break;
1603 case '|':
1604 Char = getCharAndSize(CurPtr, SizeTmp);
1605 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001606 Kind = tok::pipeequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001607 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1608 } else if (Char == '|') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001609 Kind = tok::pipepipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00001610 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1611 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001612 Kind = tok::pipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00001613 }
1614 break;
1615 case ':':
1616 Char = getCharAndSize(CurPtr, SizeTmp);
1617 if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001618 Kind = tok::r_square; // ':>' -> ']'
Reid Spencer5f016e22007-07-11 17:01:13 +00001619 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1620 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001621 Kind = tok::coloncolon;
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1623 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001624 Kind = tok::colon;
Reid Spencer5f016e22007-07-11 17:01:13 +00001625 }
1626 break;
1627 case ';':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001628 Kind = tok::semi;
Reid Spencer5f016e22007-07-11 17:01:13 +00001629 break;
1630 case '=':
1631 Char = getCharAndSize(CurPtr, SizeTmp);
1632 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001633 Kind = tok::equalequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001634 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1635 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001636 Kind = tok::equal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001637 }
1638 break;
1639 case ',':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001640 Kind = tok::comma;
Reid Spencer5f016e22007-07-11 17:01:13 +00001641 break;
1642 case '#':
1643 Char = getCharAndSize(CurPtr, SizeTmp);
1644 if (Char == '#') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001645 Kind = tok::hashhash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001646 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1647 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner9e6293d2008-10-12 04:51:35 +00001648 Kind = tok::hashat;
Chris Lattner74d15df2008-11-22 02:02:22 +00001649 if (!isLexingRawMode())
1650 Diag(BufferPtr, diag::charize_microsoft_ext);
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1652 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001653 Kind = tok::hash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001654 // We parsed a # character. If this occurs at the start of the line,
1655 // it's actually the start of a preprocessing directive. Callback to
1656 // the preprocessor to handle it.
1657 // FIXME: -fpreprocessed mode??
1658 if (Result.isAtStartOfLine() && !LexingRawMode) {
1659 BufferPtr = CurPtr;
Chris Lattner168ae2d2007-10-17 20:41:00 +00001660 PP->HandleDirective(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001661
1662 // As an optimization, if the preprocessor didn't switch lexers, tail
1663 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001664 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001665 // Start a new token. If this is a #include or something, the PP may
1666 // want us starting at the beginning of the line again. If so, set
1667 // the StartOfLine flag.
1668 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001669 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001670 IsAtStartOfLine = false;
1671 }
1672 goto LexNextToken; // GCC isn't tail call eliminating.
1673 }
Chris Lattner168ae2d2007-10-17 20:41:00 +00001674 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001675 }
1676 }
1677 break;
1678
Chris Lattner3a570772008-01-03 17:58:54 +00001679 case '@':
1680 // Objective C support.
1681 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattner9e6293d2008-10-12 04:51:35 +00001682 Kind = tok::at;
Chris Lattner3a570772008-01-03 17:58:54 +00001683 else
Chris Lattner9e6293d2008-10-12 04:51:35 +00001684 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00001685 break;
1686
Reid Spencer5f016e22007-07-11 17:01:13 +00001687 case '\\':
1688 // FIXME: UCN's.
1689 // FALL THROUGH.
1690 default:
Chris Lattner9e6293d2008-10-12 04:51:35 +00001691 Kind = tok::unknown;
Reid Spencer5f016e22007-07-11 17:01:13 +00001692 break;
1693 }
1694
1695 // Notify MIOpt that we read a non-whitespace/non-comment token.
1696 MIOpt.ReadToken();
1697
1698 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00001699 FormTokenWithChars(Result, CurPtr, Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001700}