blob: e6c3056547b7ce99ee651610ae63321958918d15 [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
Chris Lattner22d91ca2009-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 Lattner0770dab2009-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 Lattner88d3ac12009-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 Lattner0770dab2009-01-17 07:56:59 +0000101
Chris Lattner88d3ac12009-01-17 08:03:42 +0000102 const llvm::MemoryBuffer *InputFile = PP.getSourceManager().getBuffer(FID);
Chris Lattner0770dab2009-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 Lattnerdbf388b2007-10-07 08:47:24 +0000110
Chris Lattner168ae2d2007-10-17 20:41:00 +0000111/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner590f0cc2008-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 Lattner168ae2d2007-10-17 20:41:00 +0000114Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattnerde96c0f2009-01-17 07:42:27 +0000115 const char *BufStart, const char *BufPtr, const char *BufEnd)
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000116 : FileLoc(fileloc), Features(features) {
Chris Lattner22d91ca2009-01-17 06:55:17 +0000117
Chris Lattner22d91ca2009-01-17 06:55:17 +0000118 InitLexer(BufStart, BufPtr, BufEnd);
Chris Lattner168ae2d2007-10-17 20:41:00 +0000119
120 // We *are* in raw mode.
121 LexingRawMode = true;
Chris Lattner168ae2d2007-10-17 20:41:00 +0000122}
123
Chris Lattner025c3a62009-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 Lattner42e00d12009-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///
153Lexer *Lexer::Create_PragmaLexer(SourceLocation TokStartLoc, unsigned TokLen,
154 Preprocessor &PP) {
155 SourceManager &SM = PP.getSourceManager();
156 SourceLocation SpellingLoc = SM.getSpellingLoc(TokStartLoc);
157
158 // Create the lexer as if we were going to lex the file normally.
159 Lexer *L = new Lexer(SM.getCanonicalFileID(SpellingLoc), PP);
160
161 // Now that the lexer is created, change the start/end locations so that we
162 // just lex the subsection of the file that we want. This is lexing from a
163 // scratch buffer.
164 const char *StrData = SM.getCharacterData(SpellingLoc);
165
166 L->BufferPtr = StrData;
167 L->BufferEnd = StrData+TokLen;
168
169 // Set the SourceLocation with the remapping information. This ensures that
170 // GetMappedTokenLoc will remap the tokens as they are lexed.
171 L->FileLoc = TokStartLoc;
172
173 // Ensure that the lexer thinks it is inside a directive, so that end \n will
174 // return an EOM token.
175 L->ParsingPreprocessorDirective = true;
176
177 // This lexer really is for _Pragma.
178 L->Is_PragmaLexer = true;
179 return L;
180}
181
Chris Lattner168ae2d2007-10-17 20:41:00 +0000182
Reid Spencer5f016e22007-07-11 17:01:13 +0000183/// Stringify - Convert the specified string into a C string, with surrounding
184/// ""'s, and with escaped \ and " characters.
185std::string Lexer::Stringify(const std::string &Str, bool Charify) {
186 std::string Result = Str;
187 char Quote = Charify ? '\'' : '"';
188 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
189 if (Result[i] == '\\' || Result[i] == Quote) {
190 Result.insert(Result.begin()+i, '\\');
191 ++i; ++e;
192 }
193 }
194 return Result;
195}
196
Chris Lattnerd8e30832007-07-24 06:57:14 +0000197/// Stringify - Convert the specified string into a C string by escaping '\'
198/// and " characters. This does not add surrounding ""'s to the string.
199void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
200 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
201 if (Str[i] == '\\' || Str[i] == '"') {
202 Str.insert(Str.begin()+i, '\\');
203 ++i; ++e;
204 }
205 }
206}
207
Reid Spencer5f016e22007-07-11 17:01:13 +0000208
Chris Lattner9a611942007-10-17 21:18:47 +0000209/// MeasureTokenLength - Relex the token at the specified location and return
210/// its length in bytes in the input file. If the token needs cleaning (e.g.
211/// includes a trigraph or an escaped newline) then this count includes bytes
212/// that are part of that.
213unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
214 const SourceManager &SM) {
215 // If this comes from a macro expansion, we really do want the macro name, not
216 // the token this macro expanded to.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000217 Loc = SM.getInstantiationLoc(Loc);
Chris Lattner9a611942007-10-17 21:18:47 +0000218
219 const char *StrData = SM.getCharacterData(Loc);
220
221 // TODO: this could be special cased for common tokens like identifiers, ')',
222 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
223 // all obviously single-char tokens. This could use
224 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
225 // something.
Chris Lattnerde96c0f2009-01-17 07:42:27 +0000226 std::pair<const char *,const char *> Buffer = SM.getBufferData(Loc);
Chris Lattner9a611942007-10-17 21:18:47 +0000227
228 // Create a langops struct and enable trigraphs. This is sufficient for
229 // measuring tokens.
230 LangOptions LangOpts;
231 LangOpts.Trigraphs = true;
232
233 // Create a lexer starting at the beginning of this token.
Chris Lattnerde96c0f2009-01-17 07:42:27 +0000234 Lexer TheLexer(Loc, LangOpts, Buffer.first, StrData, Buffer.second);
Chris Lattner9a611942007-10-17 21:18:47 +0000235 Token TheTok;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000236 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner9a611942007-10-17 21:18:47 +0000237 return TheTok.getLength();
238}
239
Reid Spencer5f016e22007-07-11 17:01:13 +0000240//===----------------------------------------------------------------------===//
241// Character information.
242//===----------------------------------------------------------------------===//
243
244static unsigned char CharInfo[256];
245
246enum {
247 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
248 CHAR_VERT_WS = 0x02, // '\r', '\n'
249 CHAR_LETTER = 0x04, // a-z,A-Z
250 CHAR_NUMBER = 0x08, // 0-9
251 CHAR_UNDER = 0x10, // _
252 CHAR_PERIOD = 0x20 // .
253};
254
255static void InitCharacterInfo() {
256 static bool isInited = false;
257 if (isInited) return;
258 isInited = true;
259
260 // Intiialize the CharInfo table.
261 // TODO: statically initialize this.
262 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
263 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
264 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
265
266 CharInfo[(int)'_'] = CHAR_UNDER;
267 CharInfo[(int)'.'] = CHAR_PERIOD;
268 for (unsigned i = 'a'; i <= 'z'; ++i)
269 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
270 for (unsigned i = '0'; i <= '9'; ++i)
271 CharInfo[i] = CHAR_NUMBER;
272}
273
274/// isIdentifierBody - Return true if this is the body character of an
275/// identifier, which is [a-zA-Z0-9_].
276static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000277 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000278}
279
280/// isHorizontalWhitespace - Return true if this character is horizontal
281/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
282static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000283 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000284}
285
286/// isWhitespace - Return true if this character is horizontal or vertical
287/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
288/// for '\0'.
289static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000290 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000291}
292
293/// isNumberBody - Return true if this is the body character of an
294/// preprocessing number, which is [a-zA-Z0-9_.].
295static inline bool isNumberBody(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000296 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
297 true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000298}
299
300
301//===----------------------------------------------------------------------===//
302// Diagnostics forwarding code.
303//===----------------------------------------------------------------------===//
304
Chris Lattner409a0362007-07-22 18:38:25 +0000305/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
306/// lexer buffer was all instantiated at a single point, perform the mapping.
307/// This is currently only used for _Pragma implementation, so it is the slow
308/// path of the hot getSourceLocation method. Do not allow it to be inlined.
309static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
310 SourceLocation FileLoc,
311 unsigned CharNo) DISABLE_INLINE;
312static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
313 SourceLocation FileLoc,
314 unsigned CharNo) {
315 // Otherwise, we're lexing "mapped tokens". This is used for things like
316 // _Pragma handling. Combine the instantiation location of FileLoc with the
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000317 // spelling location.
Chris Lattner409a0362007-07-22 18:38:25 +0000318 SourceManager &SourceMgr = PP.getSourceManager();
319
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000320 // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000321 // characters come from spelling(FileLoc)+Offset.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000322 SourceLocation InstLoc = SourceMgr.getInstantiationLoc(FileLoc);
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000323 SourceLocation SpellingLoc = SourceMgr.getSpellingLoc(FileLoc);
324 SpellingLoc = SourceLocation::getFileLoc(SpellingLoc.getFileID(), CharNo);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000325 return SourceMgr.getInstantiationLoc(SpellingLoc, InstLoc);
Chris Lattner409a0362007-07-22 18:38:25 +0000326}
327
Reid Spencer5f016e22007-07-11 17:01:13 +0000328/// getSourceLocation - Return a source location identifier for the specified
329/// offset in the current file.
330SourceLocation Lexer::getSourceLocation(const char *Loc) const {
Chris Lattner448cec42007-07-22 18:44:36 +0000331 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000332 "Location out of range for this buffer!");
Chris Lattner9dc1f532007-07-20 16:37:10 +0000333
334 // In the normal case, we're just lexing from a simple file buffer, return
335 // the file id from FileLoc with the offset specified.
Chris Lattner448cec42007-07-22 18:44:36 +0000336 unsigned CharNo = Loc-BufferStart;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000337 if (FileLoc.isFileID())
338 return SourceLocation::getFileLoc(FileLoc.getFileID(), CharNo);
339
Chris Lattner2b2453a2009-01-17 06:22:33 +0000340 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
341 // tokens are lexed from where the _Pragma was defined.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000342 assert(PP && "This doesn't work on raw lexers");
343 return GetMappedTokenLoc(*PP, FileLoc, CharNo);
Reid Spencer5f016e22007-07-11 17:01:13 +0000344}
345
Reid Spencer5f016e22007-07-11 17:01:13 +0000346/// Diag - Forwarding function for diagnostics. This translate a source
347/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000348DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner3692b092008-11-18 07:59:24 +0000349 return PP->Diag(getSourceLocation(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000350}
Reid Spencer5f016e22007-07-11 17:01:13 +0000351
352//===----------------------------------------------------------------------===//
353// Trigraph and Escaped Newline Handling Code.
354//===----------------------------------------------------------------------===//
355
356/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
357/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
358static char GetTrigraphCharForLetter(char Letter) {
359 switch (Letter) {
360 default: return 0;
361 case '=': return '#';
362 case ')': return ']';
363 case '(': return '[';
364 case '!': return '|';
365 case '\'': return '^';
366 case '>': return '}';
367 case '/': return '\\';
368 case '<': return '{';
369 case '-': return '~';
370 }
371}
372
373/// DecodeTrigraphChar - If the specified character is a legal trigraph when
374/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
375/// return the result character. Finally, emit a warning about trigraph use
376/// whether trigraphs are enabled or not.
377static char DecodeTrigraphChar(const char *CP, Lexer *L) {
378 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner3692b092008-11-18 07:59:24 +0000379 if (!Res || !L) return Res;
380
381 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +0000382 if (!L->isLexingRawMode())
383 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner3692b092008-11-18 07:59:24 +0000384 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000385 }
Chris Lattner3692b092008-11-18 07:59:24 +0000386
Chris Lattner74d15df2008-11-22 02:02:22 +0000387 if (!L->isLexingRawMode())
388 L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000389 return Res;
390}
391
392/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
393/// get its size, and return it. This is tricky in several cases:
394/// 1. If currently at the start of a trigraph, we warn about the trigraph,
395/// then either return the trigraph (skipping 3 chars) or the '?',
396/// depending on whether trigraphs are enabled or not.
397/// 2. If this is an escaped newline (potentially with whitespace between
398/// the backslash and newline), implicitly skip the newline and return
399/// the char after it.
400/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
401///
402/// This handles the slow/uncommon case of the getCharAndSize method. Here we
403/// know that we can accumulate into Size, and that we have already incremented
404/// Ptr by Size bytes.
405///
406/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
407/// be updated to match.
408///
409char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattnerd2177732007-07-20 16:59:19 +0000410 Token *Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000411 // If we have a slash, look for an escaped newline.
412 if (Ptr[0] == '\\') {
413 ++Size;
414 ++Ptr;
415Slash:
416 // Common case, backslash-char where the char is not whitespace.
417 if (!isWhitespace(Ptr[0])) return '\\';
418
419 // See if we have optional whitespace characters followed by a newline.
420 {
421 unsigned SizeTmp = 0;
422 do {
423 ++SizeTmp;
424 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
425 // Remember that this token needs to be cleaned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000426 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000427
428 // Warn if there was whitespace between the backslash and newline.
Chris Lattner74d15df2008-11-22 02:02:22 +0000429 if (SizeTmp != 1 && Tok && !isLexingRawMode())
Reid Spencer5f016e22007-07-11 17:01:13 +0000430 Diag(Ptr, diag::backslash_newline_space);
431
432 // If this is a \r\n or \n\r, skip the newlines.
433 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
434 Ptr[SizeTmp-1] != Ptr[SizeTmp])
435 ++SizeTmp;
436
437 // Found backslash<whitespace><newline>. Parse the char after it.
438 Size += SizeTmp;
439 Ptr += SizeTmp;
440 // Use slow version to accumulate a correct size field.
441 return getCharAndSizeSlow(Ptr, Size, Tok);
442 }
443 } while (isWhitespace(Ptr[SizeTmp]));
444 }
445
446 // Otherwise, this is not an escaped newline, just return the slash.
447 return '\\';
448 }
449
450 // If this is a trigraph, process it.
451 if (Ptr[0] == '?' && Ptr[1] == '?') {
452 // If this is actually a legal trigraph (not something like "??x"), emit
453 // a trigraph warning. If so, and if trigraphs are enabled, return it.
454 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
455 // Remember that this token needs to be cleaned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000456 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000457
458 Ptr += 3;
459 Size += 3;
460 if (C == '\\') goto Slash;
461 return C;
462 }
463 }
464
465 // If this is neither, return a single character.
466 ++Size;
467 return *Ptr;
468}
469
470
471/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
472/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
473/// and that we have already incremented Ptr by Size bytes.
474///
475/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
476/// be updated to match.
477char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
478 const LangOptions &Features) {
479 // If we have a slash, look for an escaped newline.
480 if (Ptr[0] == '\\') {
481 ++Size;
482 ++Ptr;
483Slash:
484 // Common case, backslash-char where the char is not whitespace.
485 if (!isWhitespace(Ptr[0])) return '\\';
486
487 // See if we have optional whitespace characters followed by a newline.
488 {
489 unsigned SizeTmp = 0;
490 do {
491 ++SizeTmp;
492 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
493
494 // If this is a \r\n or \n\r, skip the newlines.
495 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
496 Ptr[SizeTmp-1] != Ptr[SizeTmp])
497 ++SizeTmp;
498
499 // Found backslash<whitespace><newline>. Parse the char after it.
500 Size += SizeTmp;
501 Ptr += SizeTmp;
502
503 // Use slow version to accumulate a correct size field.
504 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
505 }
506 } while (isWhitespace(Ptr[SizeTmp]));
507 }
508
509 // Otherwise, this is not an escaped newline, just return the slash.
510 return '\\';
511 }
512
513 // If this is a trigraph, process it.
514 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
515 // If this is actually a legal trigraph (not something like "??x"), return
516 // it.
517 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
518 Ptr += 3;
519 Size += 3;
520 if (C == '\\') goto Slash;
521 return C;
522 }
523 }
524
525 // If this is neither, return a single character.
526 ++Size;
527 return *Ptr;
528}
529
530//===----------------------------------------------------------------------===//
531// Helper methods for lexing.
532//===----------------------------------------------------------------------===//
533
Chris Lattnerd2177732007-07-20 16:59:19 +0000534void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000535 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
536 unsigned Size;
537 unsigned char C = *CurPtr++;
538 while (isIdentifierBody(C)) {
539 C = *CurPtr++;
540 }
541 --CurPtr; // Back up over the skipped character.
542
543 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
544 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
545 // FIXME: UCNs.
546 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
547FinishIdentifier:
548 const char *IdStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000549 FormTokenWithChars(Result, CurPtr, tok::identifier);
Reid Spencer5f016e22007-07-11 17:01:13 +0000550
551 // If we are in raw mode, return this identifier raw. There is no need to
552 // look up identifier information or attempt to macro expand it.
553 if (LexingRawMode) return;
554
555 // Fill in Result.IdentifierInfo, looking up the identifier in the
556 // identifier table.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000557 PP->LookUpIdentifierInfo(Result, IdStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000558
559 // Finally, now that we know we have an identifier, pass this off to the
560 // preprocessor, which may macro expand it or something.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000561 return PP->HandleIdentifier(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000562 }
563
564 // Otherwise, $,\,? in identifier found. Enter slower path.
565
566 C = getCharAndSize(CurPtr, Size);
567 while (1) {
568 if (C == '$') {
569 // If we hit a $ and they are not supported in identifiers, we are done.
570 if (!Features.DollarIdents) goto FinishIdentifier;
571
572 // Otherwise, emit a diagnostic and continue.
Chris Lattner74d15df2008-11-22 02:02:22 +0000573 if (!isLexingRawMode())
574 Diag(CurPtr, diag::ext_dollar_in_identifier);
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 CurPtr = ConsumeChar(CurPtr, Size, Result);
576 C = getCharAndSize(CurPtr, Size);
577 continue;
578 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
579 // Found end of identifier.
580 goto FinishIdentifier;
581 }
582
583 // Otherwise, this character is good, consume it.
584 CurPtr = ConsumeChar(CurPtr, Size, Result);
585
586 C = getCharAndSize(CurPtr, Size);
587 while (isIdentifierBody(C)) { // FIXME: UCNs.
588 CurPtr = ConsumeChar(CurPtr, Size, Result);
589 C = getCharAndSize(CurPtr, Size);
590 }
591 }
592}
593
594
Nate Begeman5253c7f2008-04-14 02:26:39 +0000595/// LexNumericConstant - Lex the remainder of a integer or floating point
Reid Spencer5f016e22007-07-11 17:01:13 +0000596/// constant. From[-1] is the first character lexed. Return the end of the
597/// constant.
Chris Lattnerd2177732007-07-20 16:59:19 +0000598void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 unsigned Size;
600 char C = getCharAndSize(CurPtr, Size);
601 char PrevCh = 0;
602 while (isNumberBody(C)) { // FIXME: UCNs?
603 CurPtr = ConsumeChar(CurPtr, Size, Result);
604 PrevCh = C;
605 C = getCharAndSize(CurPtr, Size);
606 }
607
608 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
609 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
610 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
611
612 // If we have a hex FP constant, continue.
Chris Lattner49842122008-11-22 07:39:03 +0000613 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
614 (Features.HexFloats || !Features.NoExtensions))
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
616
Reid Spencer5f016e22007-07-11 17:01:13 +0000617 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +0000618 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Reid Spencer5f016e22007-07-11 17:01:13 +0000619}
620
621/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
622/// either " or L".
Chris Lattnerd88dc482008-10-12 04:05:48 +0000623void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 const char *NulCharacter = 0; // Does this string contain the \0 character?
625
626 char C = getAndAdvanceChar(CurPtr, Result);
627 while (C != '"') {
628 // Skip escaped characters.
629 if (C == '\\') {
630 // Skip the escaped character.
631 C = getAndAdvanceChar(CurPtr, Result);
632 } else if (C == '\n' || C == '\r' || // Newline.
633 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner74d15df2008-11-22 02:02:22 +0000634 if (!isLexingRawMode())
635 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000636 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000637 return;
638 } else if (C == 0) {
639 NulCharacter = CurPtr-1;
640 }
641 C = getAndAdvanceChar(CurPtr, Result);
642 }
643
644 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000645 if (NulCharacter && !isLexingRawMode())
646 Diag(NulCharacter, diag::null_in_string);
Reid Spencer5f016e22007-07-11 17:01:13 +0000647
Reid Spencer5f016e22007-07-11 17:01:13 +0000648 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner9e6293d2008-10-12 04:51:35 +0000649 FormTokenWithChars(Result, CurPtr,
650 Wide ? tok::wide_string_literal : tok::string_literal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000651}
652
653/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
654/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattnerd2177732007-07-20 16:59:19 +0000655void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000656 const char *NulCharacter = 0; // Does this string contain the \0 character?
657
658 char C = getAndAdvanceChar(CurPtr, Result);
659 while (C != '>') {
660 // Skip escaped characters.
661 if (C == '\\') {
662 // Skip the escaped character.
663 C = getAndAdvanceChar(CurPtr, Result);
664 } else if (C == '\n' || C == '\r' || // Newline.
665 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner74d15df2008-11-22 02:02:22 +0000666 if (!isLexingRawMode())
667 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000668 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 return;
670 } else if (C == 0) {
671 NulCharacter = CurPtr-1;
672 }
673 C = getAndAdvanceChar(CurPtr, Result);
674 }
675
676 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000677 if (NulCharacter && !isLexingRawMode())
678 Diag(NulCharacter, diag::null_in_string);
Reid Spencer5f016e22007-07-11 17:01:13 +0000679
Reid Spencer5f016e22007-07-11 17:01:13 +0000680 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +0000681 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000682}
683
684
685/// LexCharConstant - Lex the remainder of a character constant, after having
686/// lexed either ' or L'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000687void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 const char *NulCharacter = 0; // Does this character contain the \0 character?
689
690 // Handle the common case of 'x' and '\y' efficiently.
691 char C = getAndAdvanceChar(CurPtr, Result);
692 if (C == '\'') {
Chris Lattner74d15df2008-11-22 02:02:22 +0000693 if (!isLexingRawMode())
694 Diag(BufferPtr, diag::err_empty_character);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000695 FormTokenWithChars(Result, CurPtr, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000696 return;
697 } else if (C == '\\') {
698 // Skip the escaped character.
699 // FIXME: UCN's.
700 C = getAndAdvanceChar(CurPtr, Result);
701 }
702
703 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
704 ++CurPtr;
705 } else {
706 // Fall back on generic code for embedded nulls, newlines, wide chars.
707 do {
708 // Skip escaped characters.
709 if (C == '\\') {
710 // Skip the escaped character.
711 C = getAndAdvanceChar(CurPtr, Result);
712 } else if (C == '\n' || C == '\r' || // Newline.
713 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner74d15df2008-11-22 02:02:22 +0000714 if (!isLexingRawMode())
715 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000716 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000717 return;
718 } else if (C == 0) {
719 NulCharacter = CurPtr-1;
720 }
721 C = getAndAdvanceChar(CurPtr, Result);
722 } while (C != '\'');
723 }
724
Chris Lattner74d15df2008-11-22 02:02:22 +0000725 if (NulCharacter && !isLexingRawMode())
726 Diag(NulCharacter, diag::null_in_char);
Reid Spencer5f016e22007-07-11 17:01:13 +0000727
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +0000729 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Reid Spencer5f016e22007-07-11 17:01:13 +0000730}
731
732/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
733/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattnerd88dc482008-10-12 04:05:48 +0000734///
735/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
736///
737bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000738 // Whitespace - Skip it, then return the token after the whitespace.
739 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
740 while (1) {
741 // Skip horizontal whitespace very aggressively.
742 while (isHorizontalWhitespace(Char))
743 Char = *++CurPtr;
744
Daniel Dunbarddd3e8b2008-11-25 00:20:22 +0000745 // Otherwise if we have something other than whitespace, we're done.
Reid Spencer5f016e22007-07-11 17:01:13 +0000746 if (Char != '\n' && Char != '\r')
747 break;
748
749 if (ParsingPreprocessorDirective) {
750 // End of preprocessor directive line, let LexTokenInternal handle this.
751 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +0000752 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 }
754
755 // ok, but handle newline.
756 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +0000757 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +0000759 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000760 Char = *++CurPtr;
761 }
762
763 // If this isn't immediately after a newline, there is leading space.
764 char PrevChar = CurPtr[-1];
765 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattnerd2177732007-07-20 16:59:19 +0000766 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000767
Chris Lattnerd88dc482008-10-12 04:05:48 +0000768 // If the client wants us to return whitespace, return it now.
769 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +0000770 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +0000771 return true;
772 }
773
Reid Spencer5f016e22007-07-11 17:01:13 +0000774 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +0000775 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000776}
777
778// SkipBCPLComment - We have just read the // characters from input. Skip until
779// we find the newline character thats terminate the comment. Then update
Chris Lattner2d381892008-10-12 04:15:42 +0000780/// BufferPtr and return. If we're in KeepCommentMode, this will form the token
781/// and return true.
Chris Lattnerd2177732007-07-20 16:59:19 +0000782bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000783 // If BCPL comments aren't explicitly enabled for this language, emit an
784 // extension warning.
Chris Lattner74d15df2008-11-22 02:02:22 +0000785 if (!Features.BCPLComment && !isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000786 Diag(BufferPtr, diag::ext_bcpl_comment);
787
788 // Mark them enabled so we only emit one warning for this translation
789 // unit.
790 Features.BCPLComment = true;
791 }
792
793 // Scan over the body of the comment. The common case, when scanning, is that
794 // the comment contains normal ascii characters with nothing interesting in
795 // them. As such, optimize for this case with the inner loop.
796 char C;
797 do {
798 C = *CurPtr;
799 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
800 // If we find a \n character, scan backwards, checking to see if it's an
801 // escaped newline, like we do for block comments.
802
803 // Skip over characters in the fast loop.
804 while (C != 0 && // Potentially EOF.
805 C != '\\' && // Potentially escaped newline.
806 C != '?' && // Potentially trigraph.
807 C != '\n' && C != '\r') // Newline or DOS-style newline.
808 C = *++CurPtr;
809
810 // If this is a newline, we're done.
811 if (C == '\n' || C == '\r')
812 break; // Found the newline? Break out!
813
814 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000815 // properly decode the character. Read it in raw mode to avoid emitting
816 // diagnostics about things like trigraphs. If we see an escaped newline,
817 // we'll handle it below.
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 const char *OldPtr = CurPtr;
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000819 bool OldRawMode = isLexingRawMode();
820 LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000822 LexingRawMode = OldRawMode;
Reid Spencer5f016e22007-07-11 17:01:13 +0000823
824 // If we read multiple characters, and one of those characters was a \r or
825 // \n, then we had an escaped newline within the comment. Emit diagnostic
826 // unless the next line is also a // comment.
827 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
828 for (; OldPtr != CurPtr; ++OldPtr)
829 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
830 // Okay, we found a // comment that ends in a newline, if the next
831 // line is also a // comment, but has spaces, don't emit a diagnostic.
832 if (isspace(C)) {
833 const char *ForwardPtr = CurPtr;
834 while (isspace(*ForwardPtr)) // Skip whitespace.
835 ++ForwardPtr;
836 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
837 break;
838 }
839
Chris Lattner74d15df2008-11-22 02:02:22 +0000840 if (!isLexingRawMode())
841 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 break;
843 }
844 }
845
846 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
847 } while (C != '\n' && C != '\r');
848
849 // Found but did not consume the newline.
850
851 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +0000852 if (inKeepCommentMode())
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 return SaveBCPLComment(Result, CurPtr);
854
855 // If we are inside a preprocessor directive and we see the end of line,
856 // return immediately, so that the lexer can return this as an EOM token.
857 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
858 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +0000859 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000860 }
861
862 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner7a4f0042008-10-12 00:23:07 +0000863 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattnerd88dc482008-10-12 04:05:48 +0000864 // contribute to another token), it isn't needed for correctness. Note that
865 // this is ok even in KeepWhitespaceMode, because we would have returned the
866 /// comment above in that mode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000867 ++CurPtr;
868
869 // The next returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +0000870 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +0000872 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +0000874 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000875}
876
877/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
878/// an appropriate way and return it.
Chris Lattnerd2177732007-07-20 16:59:19 +0000879bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner9e6293d2008-10-12 04:51:35 +0000880 // If we're not in a preprocessor directive, just return the // comment
881 // directly.
882 FormTokenWithChars(Result, CurPtr, tok::comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000883
Chris Lattner9e6293d2008-10-12 04:51:35 +0000884 if (!ParsingPreprocessorDirective)
885 return true;
886
887 // If this BCPL-style comment is in a macro definition, transmogrify it into
888 // a C-style block comment.
889 std::string Spelling = PP->getSpelling(Result);
890 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
891 Spelling[1] = '*'; // Change prefix to "/*".
892 Spelling += "*/"; // add suffix.
893
894 Result.setKind(tok::comment);
895 Result.setLocation(PP->CreateString(&Spelling[0], Spelling.size(),
896 Result.getLocation()));
897 Result.setLength(Spelling.size());
Chris Lattner2d381892008-10-12 04:15:42 +0000898 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000899}
900
901/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
902/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner47a2b402008-12-12 07:14:34 +0000903/// diagnostic if so. We know that the newline is inside of a block comment.
Reid Spencer5f016e22007-07-11 17:01:13 +0000904static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
905 Lexer *L) {
906 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
907
908 // Back up off the newline.
909 --CurPtr;
910
911 // If this is a two-character newline sequence, skip the other character.
912 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
913 // \n\n or \r\r -> not escaped newline.
914 if (CurPtr[0] == CurPtr[1])
915 return false;
916 // \n\r or \r\n -> skip the newline.
917 --CurPtr;
918 }
919
920 // If we have horizontal whitespace, skip over it. We allow whitespace
921 // between the slash and newline.
922 bool HasSpace = false;
923 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
924 --CurPtr;
925 HasSpace = true;
926 }
927
928 // If we have a slash, we know this is an escaped newline.
929 if (*CurPtr == '\\') {
930 if (CurPtr[-1] != '*') return false;
931 } else {
932 // It isn't a slash, is it the ?? / trigraph?
933 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
934 CurPtr[-3] != '*')
935 return false;
936
937 // This is the trigraph ending the comment. Emit a stern warning!
938 CurPtr -= 2;
939
940 // If no trigraphs are enabled, warn that we ignored this trigraph and
941 // ignore this * character.
942 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +0000943 if (!L->isLexingRawMode())
944 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 return false;
946 }
Chris Lattner74d15df2008-11-22 02:02:22 +0000947 if (!L->isLexingRawMode())
948 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000949 }
950
951 // Warn about having an escaped newline between the */ characters.
Chris Lattner74d15df2008-11-22 02:02:22 +0000952 if (!L->isLexingRawMode())
953 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Reid Spencer5f016e22007-07-11 17:01:13 +0000954
955 // If there was space between the backslash and newline, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000956 if (HasSpace && !L->isLexingRawMode())
957 L->Diag(CurPtr, diag::backslash_newline_space);
Reid Spencer5f016e22007-07-11 17:01:13 +0000958
959 return true;
960}
961
962#ifdef __SSE2__
963#include <emmintrin.h>
964#elif __ALTIVEC__
965#include <altivec.h>
966#undef bool
967#endif
968
969/// SkipBlockComment - We have just read the /* characters from input. Read
970/// until we find the */ characters that terminate the comment. Note that we
971/// don't bother decoding trigraphs or escaped newlines in block comments,
972/// because they cannot cause the comment to end. The only thing that can
973/// happen is the comment could end with an escaped newline between the */ end
974/// of comment.
Chris Lattner2d381892008-10-12 04:15:42 +0000975///
976/// If KeepCommentMode is enabled, this forms a token from the comment and
977/// returns true.
Chris Lattnerd2177732007-07-20 16:59:19 +0000978bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000979 // Scan one character past where we should, looking for a '/' character. Once
980 // we find it, check to see if it was preceeded by a *. This common
981 // optimization helps people who like to put a lot of * characters in their
982 // comments.
Chris Lattner8146b682007-07-21 23:43:37 +0000983
984 // The first character we get with newlines and trigraphs skipped to handle
985 // the degenerate /*/ case below correctly if the * has an escaped newline
986 // after it.
987 unsigned CharSize;
988 unsigned char C = getCharAndSize(CurPtr, CharSize);
989 CurPtr += CharSize;
Reid Spencer5f016e22007-07-11 17:01:13 +0000990 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner74d15df2008-11-22 02:02:22 +0000991 if (!isLexingRawMode())
Chris Lattner0af57422008-10-12 01:31:51 +0000992 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner31f0eca2008-10-12 04:19:49 +0000993 --CurPtr;
994
995 // KeepWhitespaceMode should return this broken comment as a token. Since
996 // it isn't a well formed comment, just return it as an 'unknown' token.
997 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +0000998 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +0000999 return true;
1000 }
1001
1002 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001003 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001004 }
1005
Chris Lattner8146b682007-07-21 23:43:37 +00001006 // Check to see if the first character after the '/*' is another /. If so,
1007 // then this slash does not end the block comment, it is part of it.
1008 if (C == '/')
1009 C = *CurPtr++;
1010
Reid Spencer5f016e22007-07-11 17:01:13 +00001011 while (1) {
1012 // Skip over all non-interesting characters until we find end of buffer or a
1013 // (probably ending) '/' character.
1014 if (CurPtr + 24 < BufferEnd) {
1015 // While not aligned to a 16-byte boundary.
1016 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1017 C = *CurPtr++;
1018
1019 if (C == '/') goto FoundSlash;
1020
1021#ifdef __SSE2__
1022 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1023 '/', '/', '/', '/', '/', '/', '/', '/');
1024 while (CurPtr+16 <= BufferEnd &&
1025 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1026 CurPtr += 16;
1027#elif __ALTIVEC__
1028 __vector unsigned char Slashes = {
1029 '/', '/', '/', '/', '/', '/', '/', '/',
1030 '/', '/', '/', '/', '/', '/', '/', '/'
1031 };
1032 while (CurPtr+16 <= BufferEnd &&
1033 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1034 CurPtr += 16;
1035#else
1036 // Scan for '/' quickly. Many block comments are very large.
1037 while (CurPtr[0] != '/' &&
1038 CurPtr[1] != '/' &&
1039 CurPtr[2] != '/' &&
1040 CurPtr[3] != '/' &&
1041 CurPtr+4 < BufferEnd) {
1042 CurPtr += 4;
1043 }
1044#endif
1045
1046 // It has to be one of the bytes scanned, increment to it and read one.
1047 C = *CurPtr++;
1048 }
1049
1050 // Loop to scan the remainder.
1051 while (C != '/' && C != '\0')
1052 C = *CurPtr++;
1053
1054 FoundSlash:
1055 if (C == '/') {
1056 if (CurPtr[-2] == '*') // We found the final */. We're done!
1057 break;
1058
1059 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
1060 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
1061 // We found the final */, though it had an escaped newline between the
1062 // * and /. We're done!
1063 break;
1064 }
1065 }
1066 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1067 // If this is a /* inside of the comment, emit a warning. Don't do this
1068 // if this is a /*/, which will end the comment. This misses cases with
1069 // embedded escaped newlines, but oh well.
Chris Lattner74d15df2008-11-22 02:02:22 +00001070 if (!isLexingRawMode())
1071 Diag(CurPtr-1, diag::warn_nested_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 }
1073 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001074 if (!isLexingRawMode())
1075 Diag(BufferPtr, diag::err_unterminated_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 // Note: the user probably forgot a */. We could continue immediately
1077 // after the /*, but this would involve lexing a lot of what really is the
1078 // comment, which surely would confuse the parser.
Chris Lattner31f0eca2008-10-12 04:19:49 +00001079 --CurPtr;
1080
1081 // KeepWhitespaceMode should return this broken comment as a token. Since
1082 // it isn't a well formed comment, just return it as an 'unknown' token.
1083 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001084 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001085 return true;
1086 }
1087
1088 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001089 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 }
1091 C = *CurPtr++;
1092 }
1093
1094 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +00001095 if (inKeepCommentMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001096 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattner2d381892008-10-12 04:15:42 +00001097 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001098 }
1099
1100 // It is common for the tokens immediately after a /**/ comment to be
1101 // whitespace. Instead of going through the big switch, handle it
Chris Lattnerd88dc482008-10-12 04:05:48 +00001102 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1103 // have already returned above with the comment as a token.
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001105 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 SkipWhitespace(Result, CurPtr+1);
Chris Lattner2d381892008-10-12 04:15:42 +00001107 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001108 }
1109
1110 // Otherwise, just return so that the next character will be lexed as a token.
1111 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001112 Result.setFlag(Token::LeadingSpace);
Chris Lattner2d381892008-10-12 04:15:42 +00001113 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001114}
1115
1116//===----------------------------------------------------------------------===//
1117// Primary Lexing Entry Points
1118//===----------------------------------------------------------------------===//
1119
Reid Spencer5f016e22007-07-11 17:01:13 +00001120/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1121/// uninterpreted string. This switches the lexer out of directive mode.
1122std::string Lexer::ReadToEndOfLine() {
1123 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1124 "Must be in a preprocessing directive!");
1125 std::string Result;
Chris Lattnerd2177732007-07-20 16:59:19 +00001126 Token Tmp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001127
1128 // CurPtr - Cache BufferPtr in an automatic variable.
1129 const char *CurPtr = BufferPtr;
1130 while (1) {
1131 char Char = getAndAdvanceChar(CurPtr, Tmp);
1132 switch (Char) {
1133 default:
1134 Result += Char;
1135 break;
1136 case 0: // Null.
1137 // Found end of file?
1138 if (CurPtr-1 != BufferEnd) {
1139 // Nope, normal character, continue.
1140 Result += Char;
1141 break;
1142 }
1143 // FALL THROUGH.
1144 case '\r':
1145 case '\n':
1146 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1147 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1148 BufferPtr = CurPtr-1;
1149
1150 // Next, lex the character, which should handle the EOM transition.
1151 Lex(Tmp);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001152 assert(Tmp.is(tok::eom) && "Unexpected token!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001153
1154 // Finally, we're done, return the string we found.
1155 return Result;
1156 }
1157 }
1158}
1159
1160/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1161/// condition, reporting diagnostics and handling other edge cases as required.
1162/// This returns true if Result contains a token, false if PP.Lex should be
1163/// called again.
Chris Lattnerd2177732007-07-20 16:59:19 +00001164bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001165 // If we hit the end of the file while parsing a preprocessor directive,
1166 // end the preprocessor directive first. The next token returned will
1167 // then be the end of file.
1168 if (ParsingPreprocessorDirective) {
1169 // Done parsing the "line".
1170 ParsingPreprocessorDirective = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001171 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00001172 FormTokenWithChars(Result, CurPtr, tok::eom);
Reid Spencer5f016e22007-07-11 17:01:13 +00001173
1174 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001175 SetCommentRetentionState(PP->getCommentRetentionState());
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 return true; // Have a token.
1177 }
1178
1179 // If we are in raw mode, return this event as an EOF token. Let the caller
1180 // that put us in raw mode handle the event.
Chris Lattner74d15df2008-11-22 02:02:22 +00001181 if (isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 Result.startToken();
1183 BufferPtr = BufferEnd;
Chris Lattner9e6293d2008-10-12 04:51:35 +00001184 FormTokenWithChars(Result, BufferEnd, tok::eof);
Reid Spencer5f016e22007-07-11 17:01:13 +00001185 return true;
1186 }
1187
1188 // Otherwise, issue diagnostics for unterminated #if and missing newline.
1189
1190 // If we are in a #if directive, emit an error.
1191 while (!ConditionalStack.empty()) {
Chris Lattner30c64762008-11-22 06:22:39 +00001192 PP->Diag(ConditionalStack.back().IfLoc,
1193 diag::err_pp_unterminated_conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +00001194 ConditionalStack.pop_back();
1195 }
1196
Chris Lattnerb25e5d72008-04-12 05:54:25 +00001197 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1198 // a pedwarn.
1199 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 Diag(BufferEnd, diag::ext_no_newline_eof);
1201
1202 BufferPtr = CurPtr;
1203
1204 // Finally, let the preprocessor handle this.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001205 return PP->HandleEndOfFile(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001206}
1207
1208/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1209/// the specified lexer will return a tok::l_paren token, 0 if it is something
1210/// else and 2 if there are no more tokens in the buffer controlled by the
1211/// lexer.
1212unsigned Lexer::isNextPPTokenLParen() {
1213 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1214
1215 // Switch to 'skipping' mode. This will ensure that we can lex a token
1216 // without emitting diagnostics, disables macro expansion, and will cause EOF
1217 // to return an EOF token instead of popping the include stack.
1218 LexingRawMode = true;
1219
1220 // Save state that can be changed while lexing so that we can restore it.
1221 const char *TmpBufferPtr = BufferPtr;
1222
Chris Lattnerd2177732007-07-20 16:59:19 +00001223 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 Tok.startToken();
1225 LexTokenInternal(Tok);
1226
1227 // Restore state that may have changed.
1228 BufferPtr = TmpBufferPtr;
1229
1230 // Restore the lexer back to non-skipping mode.
1231 LexingRawMode = false;
1232
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001233 if (Tok.is(tok::eof))
Reid Spencer5f016e22007-07-11 17:01:13 +00001234 return 2;
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001235 return Tok.is(tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001236}
1237
1238
1239/// LexTokenInternal - This implements a simple C family lexer. It is an
1240/// extremely performance critical piece of code. This assumes that the buffer
1241/// has a null character at the end of the file. Return true if an error
1242/// occurred and compilation should terminate, false if normal. This returns a
1243/// preprocessing token, not a normal token, as such, it is an internal
1244/// interface. It assumes that the Flags of result have been cleared before
1245/// calling this.
Chris Lattnerd2177732007-07-20 16:59:19 +00001246void Lexer::LexTokenInternal(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001247LexNextToken:
1248 // New token, can't need cleaning yet.
Chris Lattnerd2177732007-07-20 16:59:19 +00001249 Result.clearFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 Result.setIdentifierInfo(0);
1251
1252 // CurPtr - Cache BufferPtr in an automatic variable.
1253 const char *CurPtr = BufferPtr;
1254
1255 // Small amounts of horizontal whitespace is very common between tokens.
1256 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1257 ++CurPtr;
1258 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1259 ++CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +00001260
1261 // If we are keeping whitespace and other tokens, just return what we just
1262 // skipped. The next lexer invocation will return the token after the
1263 // whitespace.
1264 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001265 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001266 return;
1267 }
1268
Reid Spencer5f016e22007-07-11 17:01:13 +00001269 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001270 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001271 }
1272
1273 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1274
1275 // Read a character, advancing over it.
1276 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001277 tok::TokenKind Kind;
1278
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 switch (Char) {
1280 case 0: // Null.
1281 // Found end of file?
1282 if (CurPtr-1 == BufferEnd) {
1283 // Read the PP instance variable into an automatic variable, because
1284 // LexEndOfFile will often delete 'this'.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001285 Preprocessor *PPCache = PP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001286 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1287 return; // Got a token to return.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001288 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1289 return PPCache->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001290 }
1291
Chris Lattner74d15df2008-11-22 02:02:22 +00001292 if (!isLexingRawMode())
1293 Diag(CurPtr-1, diag::null_in_file);
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
1297
Reid Spencer5f016e22007-07-11 17:01:13 +00001298 goto LexNextToken; // GCC isn't tail call eliminating.
1299 case '\n':
1300 case '\r':
1301 // If we are inside a preprocessor directive and we see the end of line,
1302 // we know we are done with the directive, so return an EOM token.
1303 if (ParsingPreprocessorDirective) {
1304 // Done parsing the "line".
1305 ParsingPreprocessorDirective = false;
1306
1307 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001308 SetCommentRetentionState(PP->getCommentRetentionState());
Reid Spencer5f016e22007-07-11 17:01:13 +00001309
1310 // Since we consumed a newline, we are back at the start of a line.
1311 IsAtStartOfLine = true;
1312
Chris Lattner9e6293d2008-10-12 04:51:35 +00001313 Kind = tok::eom;
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 break;
1315 }
1316 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +00001317 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +00001319 Result.clearFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001320
1321 if (SkipWhitespace(Result, CurPtr))
1322 return; // KeepWhitespaceMode
Reid Spencer5f016e22007-07-11 17:01:13 +00001323 goto LexNextToken; // GCC isn't tail call eliminating.
1324 case ' ':
1325 case '\t':
1326 case '\f':
1327 case '\v':
Chris Lattner8133cfc2007-07-22 06:29:05 +00001328 SkipHorizontalWhitespace:
Chris Lattnerd2177732007-07-20 16:59:19 +00001329 Result.setFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001330 if (SkipWhitespace(Result, CurPtr))
1331 return; // KeepWhitespaceMode
Chris Lattner8133cfc2007-07-22 06:29:05 +00001332
1333 SkipIgnoredUnits:
1334 CurPtr = BufferPtr;
1335
1336 // If the next token is obviously a // or /* */ comment, skip it efficiently
1337 // too (without going through the big switch stmt).
Chris Lattner8402c732009-01-16 22:39:25 +00001338 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1339 Features.BCPLComment) {
Chris Lattner8133cfc2007-07-22 06:29:05 +00001340 SkipBCPLComment(Result, CurPtr+2);
1341 goto SkipIgnoredUnits;
Chris Lattnerfa95a012008-10-12 03:22:02 +00001342 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner8133cfc2007-07-22 06:29:05 +00001343 SkipBlockComment(Result, CurPtr+2);
1344 goto SkipIgnoredUnits;
1345 } else if (isHorizontalWhitespace(*CurPtr)) {
1346 goto SkipHorizontalWhitespace;
1347 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001348 goto LexNextToken; // GCC isn't tail call eliminating.
1349
Chris Lattner3a570772008-01-03 17:58:54 +00001350 // C99 6.4.4.1: Integer Constants.
1351 // C99 6.4.4.2: Floating Constants.
1352 case '0': case '1': case '2': case '3': case '4':
1353 case '5': case '6': case '7': case '8': case '9':
1354 // Notify MIOpt that we read a non-whitespace/non-comment token.
1355 MIOpt.ReadToken();
1356 return LexNumericConstant(Result, CurPtr);
1357
1358 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Reid Spencer5f016e22007-07-11 17:01:13 +00001359 // Notify MIOpt that we read a non-whitespace/non-comment token.
1360 MIOpt.ReadToken();
1361 Char = getCharAndSize(CurPtr, SizeTmp);
1362
1363 // Wide string literal.
1364 if (Char == '"')
1365 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1366 true);
1367
1368 // Wide character constant.
1369 if (Char == '\'')
1370 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1371 // FALL THROUGH, treating L like the start of an identifier.
1372
1373 // C99 6.4.2: Identifiers.
1374 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1375 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1376 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1377 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1378 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1379 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1380 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1381 case 'v': case 'w': case 'x': case 'y': case 'z':
1382 case '_':
1383 // Notify MIOpt that we read a non-whitespace/non-comment token.
1384 MIOpt.ReadToken();
1385 return LexIdentifier(Result, CurPtr);
Chris Lattner3a570772008-01-03 17:58:54 +00001386
1387 case '$': // $ in identifiers.
1388 if (Features.DollarIdents) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001389 if (!isLexingRawMode())
1390 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner3a570772008-01-03 17:58:54 +00001391 // Notify MIOpt that we read a non-whitespace/non-comment token.
1392 MIOpt.ReadToken();
1393 return LexIdentifier(Result, CurPtr);
1394 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001395
Chris Lattner9e6293d2008-10-12 04:51:35 +00001396 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00001397 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001398
1399 // C99 6.4.4: Character Constants.
1400 case '\'':
1401 // Notify MIOpt that we read a non-whitespace/non-comment token.
1402 MIOpt.ReadToken();
1403 return LexCharConstant(Result, CurPtr);
1404
1405 // C99 6.4.5: String Literals.
1406 case '"':
1407 // Notify MIOpt that we read a non-whitespace/non-comment token.
1408 MIOpt.ReadToken();
1409 return LexStringLiteral(Result, CurPtr, false);
1410
1411 // C99 6.4.6: Punctuators.
1412 case '?':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001413 Kind = tok::question;
Reid Spencer5f016e22007-07-11 17:01:13 +00001414 break;
1415 case '[':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001416 Kind = tok::l_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001417 break;
1418 case ']':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001419 Kind = tok::r_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001420 break;
1421 case '(':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001422 Kind = tok::l_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001423 break;
1424 case ')':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001425 Kind = tok::r_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001426 break;
1427 case '{':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001428 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001429 break;
1430 case '}':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001431 Kind = tok::r_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001432 break;
1433 case '.':
1434 Char = getCharAndSize(CurPtr, SizeTmp);
1435 if (Char >= '0' && Char <= '9') {
1436 // Notify MIOpt that we read a non-whitespace/non-comment token.
1437 MIOpt.ReadToken();
1438
1439 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1440 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001441 Kind = tok::periodstar;
Reid Spencer5f016e22007-07-11 17:01:13 +00001442 CurPtr += SizeTmp;
1443 } else if (Char == '.' &&
1444 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001445 Kind = tok::ellipsis;
Reid Spencer5f016e22007-07-11 17:01:13 +00001446 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1447 SizeTmp2, Result);
1448 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001449 Kind = tok::period;
Reid Spencer5f016e22007-07-11 17:01:13 +00001450 }
1451 break;
1452 case '&':
1453 Char = getCharAndSize(CurPtr, SizeTmp);
1454 if (Char == '&') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001455 Kind = tok::ampamp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001456 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1457 } else if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001458 Kind = tok::ampequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001459 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1460 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001461 Kind = tok::amp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 }
1463 break;
1464 case '*':
1465 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001466 Kind = tok::starequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001467 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1468 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001469 Kind = tok::star;
Reid Spencer5f016e22007-07-11 17:01:13 +00001470 }
1471 break;
1472 case '+':
1473 Char = getCharAndSize(CurPtr, SizeTmp);
1474 if (Char == '+') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001475 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001476 Kind = tok::plusplus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001477 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001478 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001479 Kind = tok::plusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001480 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001481 Kind = tok::plus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001482 }
1483 break;
1484 case '-':
1485 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001486 if (Char == '-') { // --
Reid Spencer5f016e22007-07-11 17:01:13 +00001487 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001488 Kind = tok::minusminus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001489 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattner9e6293d2008-10-12 04:51:35 +00001490 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Reid Spencer5f016e22007-07-11 17:01:13 +00001491 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1492 SizeTmp2, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001493 Kind = tok::arrowstar;
1494 } 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::arrow;
1497 } else if (Char == '=') { // -=
Reid Spencer5f016e22007-07-11 17:01:13 +00001498 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001499 Kind = tok::minusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001500 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001501 Kind = tok::minus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001502 }
1503 break;
1504 case '~':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001505 Kind = tok::tilde;
Reid Spencer5f016e22007-07-11 17:01:13 +00001506 break;
1507 case '!':
1508 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001509 Kind = tok::exclaimequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001510 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1511 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001512 Kind = tok::exclaim;
Reid Spencer5f016e22007-07-11 17:01:13 +00001513 }
1514 break;
1515 case '/':
1516 // 6.4.9: Comments
1517 Char = getCharAndSize(CurPtr, SizeTmp);
1518 if (Char == '/') { // BCPL comment.
Chris Lattner8402c732009-01-16 22:39:25 +00001519 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
1520 // want to lex this as a comment. There is one problem with this though,
1521 // that in one particular corner case, this can change the behavior of the
1522 // resultant program. For example, In "foo //**/ bar", C89 would lex
1523 // this as "foo / bar" and langauges with BCPL comments would lex it as
1524 // "foo". Check to see if the character after the second slash is a '*'.
1525 // If so, we will lex that as a "/" instead of the start of a comment.
1526 if (Features.BCPLComment ||
1527 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
1528 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1529 return; // KeepCommentMode
Chris Lattner2d381892008-10-12 04:15:42 +00001530
Chris Lattner8402c732009-01-16 22:39:25 +00001531 // It is common for the tokens immediately after a // comment to be
1532 // whitespace (indentation for the next line). Instead of going through
1533 // the big switch, handle it efficiently now.
1534 goto SkipIgnoredUnits;
1535 }
1536 }
1537
1538 if (Char == '*') { // /**/ comment.
Reid Spencer5f016e22007-07-11 17:01:13 +00001539 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner2d381892008-10-12 04:15:42 +00001540 return; // KeepCommentMode
1541 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner8402c732009-01-16 22:39:25 +00001542 }
1543
1544 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001545 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001546 Kind = tok::slashequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001547 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001548 Kind = tok::slash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001549 }
1550 break;
1551 case '%':
1552 Char = getCharAndSize(CurPtr, SizeTmp);
1553 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001554 Kind = tok::percentequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001555 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1556 } else if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001557 Kind = tok::r_brace; // '%>' -> '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001558 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1559 } else if (Features.Digraphs && Char == ':') {
1560 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1561 Char = getCharAndSize(CurPtr, SizeTmp);
1562 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001563 Kind = tok::hashhash; // '%:%:' -> '##'
Reid Spencer5f016e22007-07-11 17:01:13 +00001564 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1565 SizeTmp2, Result);
1566 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Reid Spencer5f016e22007-07-11 17:01:13 +00001567 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner74d15df2008-11-22 02:02:22 +00001568 if (!isLexingRawMode())
1569 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001570 Kind = tok::hashat;
Reid Spencer5f016e22007-07-11 17:01:13 +00001571 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001572 Kind = tok::hash; // '%:' -> '#'
Reid Spencer5f016e22007-07-11 17:01:13 +00001573
1574 // We parsed a # character. If this occurs at the start of the line,
1575 // it's actually the start of a preprocessing directive. Callback to
1576 // the preprocessor to handle it.
1577 // FIXME: -fpreprocessed mode??
1578 if (Result.isAtStartOfLine() && !LexingRawMode) {
1579 BufferPtr = CurPtr;
Chris Lattner168ae2d2007-10-17 20:41:00 +00001580 PP->HandleDirective(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001581
1582 // As an optimization, if the preprocessor didn't switch lexers, tail
1583 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001584 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001585 // Start a new token. If this is a #include or something, the PP may
1586 // want us starting at the beginning of the line again. If so, set
1587 // the StartOfLine flag.
1588 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001589 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001590 IsAtStartOfLine = false;
1591 }
1592 goto LexNextToken; // GCC isn't tail call eliminating.
1593 }
1594
Chris Lattner168ae2d2007-10-17 20:41:00 +00001595 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001596 }
1597 }
1598 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001599 Kind = tok::percent;
Reid Spencer5f016e22007-07-11 17:01:13 +00001600 }
1601 break;
1602 case '<':
1603 Char = getCharAndSize(CurPtr, SizeTmp);
1604 if (ParsingFilename) {
1605 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1606 } else if (Char == '<' &&
1607 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001608 Kind = tok::lesslessequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001609 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1610 SizeTmp2, Result);
1611 } else if (Char == '<') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001612 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001613 Kind = tok::lessless;
Reid Spencer5f016e22007-07-11 17:01:13 +00001614 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001615 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001616 Kind = tok::lessequal;
1617 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Reid Spencer5f016e22007-07-11 17:01:13 +00001618 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001619 Kind = tok::l_square;
1620 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Reid Spencer5f016e22007-07-11 17:01:13 +00001621 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001622 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001623 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001624 Kind = tok::less;
Reid Spencer5f016e22007-07-11 17:01:13 +00001625 }
1626 break;
1627 case '>':
1628 Char = getCharAndSize(CurPtr, SizeTmp);
1629 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001630 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001631 Kind = tok::greaterequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001632 } else if (Char == '>' &&
1633 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001634 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1635 SizeTmp2, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001636 Kind = tok::greatergreaterequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001637 } else if (Char == '>') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001638 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001639 Kind = tok::greatergreater;
Reid Spencer5f016e22007-07-11 17:01:13 +00001640 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001641 Kind = tok::greater;
Reid Spencer5f016e22007-07-11 17:01:13 +00001642 }
1643 break;
1644 case '^':
1645 Char = getCharAndSize(CurPtr, SizeTmp);
1646 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001648 Kind = tok::caretequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001649 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001650 Kind = tok::caret;
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 }
1652 break;
1653 case '|':
1654 Char = getCharAndSize(CurPtr, SizeTmp);
1655 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001656 Kind = tok::pipeequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1658 } else if (Char == '|') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001659 Kind = tok::pipepipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00001660 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1661 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001662 Kind = tok::pipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00001663 }
1664 break;
1665 case ':':
1666 Char = getCharAndSize(CurPtr, SizeTmp);
1667 if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001668 Kind = tok::r_square; // ':>' -> ']'
Reid Spencer5f016e22007-07-11 17:01:13 +00001669 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1670 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001671 Kind = tok::coloncolon;
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1673 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001674 Kind = tok::colon;
Reid Spencer5f016e22007-07-11 17:01:13 +00001675 }
1676 break;
1677 case ';':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001678 Kind = tok::semi;
Reid Spencer5f016e22007-07-11 17:01:13 +00001679 break;
1680 case '=':
1681 Char = getCharAndSize(CurPtr, SizeTmp);
1682 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001683 Kind = tok::equalequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001684 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1685 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001686 Kind = tok::equal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001687 }
1688 break;
1689 case ',':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001690 Kind = tok::comma;
Reid Spencer5f016e22007-07-11 17:01:13 +00001691 break;
1692 case '#':
1693 Char = getCharAndSize(CurPtr, SizeTmp);
1694 if (Char == '#') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001695 Kind = tok::hashhash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001696 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1697 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner9e6293d2008-10-12 04:51:35 +00001698 Kind = tok::hashat;
Chris Lattner74d15df2008-11-22 02:02:22 +00001699 if (!isLexingRawMode())
1700 Diag(BufferPtr, diag::charize_microsoft_ext);
Reid Spencer5f016e22007-07-11 17:01:13 +00001701 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1702 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001703 Kind = tok::hash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 // We parsed a # character. If this occurs at the start of the line,
1705 // it's actually the start of a preprocessing directive. Callback to
1706 // the preprocessor to handle it.
1707 // FIXME: -fpreprocessed mode??
1708 if (Result.isAtStartOfLine() && !LexingRawMode) {
1709 BufferPtr = CurPtr;
Chris Lattner168ae2d2007-10-17 20:41:00 +00001710 PP->HandleDirective(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001711
1712 // As an optimization, if the preprocessor didn't switch lexers, tail
1713 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001714 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001715 // Start a new token. If this is a #include or something, the PP may
1716 // want us starting at the beginning of the line again. If so, set
1717 // the StartOfLine flag.
1718 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001719 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001720 IsAtStartOfLine = false;
1721 }
1722 goto LexNextToken; // GCC isn't tail call eliminating.
1723 }
Chris Lattner168ae2d2007-10-17 20:41:00 +00001724 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001725 }
1726 }
1727 break;
1728
Chris Lattner3a570772008-01-03 17:58:54 +00001729 case '@':
1730 // Objective C support.
1731 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattner9e6293d2008-10-12 04:51:35 +00001732 Kind = tok::at;
Chris Lattner3a570772008-01-03 17:58:54 +00001733 else
Chris Lattner9e6293d2008-10-12 04:51:35 +00001734 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00001735 break;
1736
Reid Spencer5f016e22007-07-11 17:01:13 +00001737 case '\\':
1738 // FIXME: UCN's.
1739 // FALL THROUGH.
1740 default:
Chris Lattner9e6293d2008-10-12 04:51:35 +00001741 Kind = tok::unknown;
Reid Spencer5f016e22007-07-11 17:01:13 +00001742 break;
1743 }
1744
1745 // Notify MIOpt that we read a non-whitespace/non-comment token.
1746 MIOpt.ReadToken();
1747
1748 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00001749 FormTokenWithChars(Result, CurPtr, Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001750}