blob: c379f58b0c6c8c5b5812deb2ddcfc9d2a349416d [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.
97Lexer::Lexer(SourceLocation fileloc, Preprocessor &PP)
98// FIXME: This is really horrible and only needed for _Pragma lexers, split this
99// out of the main lexer path!
100: PreprocessorLexer(&PP,
101 PP.getSourceManager().getCanonicalFileID(
102 PP.getSourceManager().getSpellingLoc(fileloc))),
103 FileLoc(fileloc),
104 Features(PP.getLangOptions()) {
105
106 SourceManager &SourceMgr = PP.getSourceManager();
107 const llvm::MemoryBuffer *InputFile = SourceMgr.getBuffer(getFileID());
108
109 InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
110 InputFile->getBufferEnd());
111
112 // Default to keeping comments if the preprocessor wants them.
113 SetCommentRetentionState(PP.getCommentRetentionState());
114}
Chris Lattnerdbf388b2007-10-07 08:47:24 +0000115
Chris Lattner168ae2d2007-10-17 20:41:00 +0000116/// Lexer constructor - Create a new lexer object for the specified buffer
117/// with the specified preprocessor managing the lexing process. This lexer
118/// assumes that the associated file buffer and Preprocessor objects will
119/// outlive it, so it doesn't take ownership of either of them.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000120Lexer::Lexer(SourceLocation fileloc, Preprocessor &PP,
Chris Lattner22d91ca2009-01-17 06:55:17 +0000121 const char *BufPtr, const char *BufEnd)
Chris Lattner2b2453a2009-01-17 06:22:33 +0000122// FIXME: This is really horrible and only needed for _Pragma lexers, split this
123// out of the main lexer path!
124 : PreprocessorLexer(&PP,
125 PP.getSourceManager().getCanonicalFileID(
126 PP.getSourceManager().getSpellingLoc(fileloc))),
127 FileLoc(fileloc),
128 Features(PP.getLangOptions()) {
Chris Lattner25bdb512007-07-20 16:52:03 +0000129
Chris Lattner0770dab2009-01-17 07:56:59 +0000130 InitLexer(PP.getSourceManager().getBuffer(getFileID())->getBufferStart(),
131 BufPtr, BufEnd);
Reid Spencer5f016e22007-07-11 17:01:13 +0000132
Chris Lattnerd88dc482008-10-12 04:05:48 +0000133 // Default to keeping comments if the preprocessor wants them.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000134 SetCommentRetentionState(PP.getCommentRetentionState());
Reid Spencer5f016e22007-07-11 17:01:13 +0000135}
136
Chris Lattner168ae2d2007-10-17 20:41:00 +0000137/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner590f0cc2008-10-12 01:15:46 +0000138/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
139/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000140Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattnerde96c0f2009-01-17 07:42:27 +0000141 const char *BufStart, const char *BufPtr, const char *BufEnd)
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000142 : FileLoc(fileloc), Features(features) {
Chris Lattner22d91ca2009-01-17 06:55:17 +0000143
Chris Lattner22d91ca2009-01-17 06:55:17 +0000144 InitLexer(BufStart, BufPtr, BufEnd);
Chris Lattner168ae2d2007-10-17 20:41:00 +0000145
146 // We *are* in raw mode.
147 LexingRawMode = true;
Chris Lattner168ae2d2007-10-17 20:41:00 +0000148}
149
Chris Lattner025c3a62009-01-17 07:35:14 +0000150/// Lexer constructor - Create a new raw lexer object. This object is only
151/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
152/// range will outlive it, so it doesn't take ownership of it.
153Lexer::Lexer(FileID FID, const SourceManager &SM, const LangOptions &features)
154 : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
155 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
156
157 InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
158 FromFile->getBufferEnd());
159
160 // We *are* in raw mode.
161 LexingRawMode = true;
162}
163
Chris Lattner168ae2d2007-10-17 20:41:00 +0000164
Reid Spencer5f016e22007-07-11 17:01:13 +0000165/// Stringify - Convert the specified string into a C string, with surrounding
166/// ""'s, and with escaped \ and " characters.
167std::string Lexer::Stringify(const std::string &Str, bool Charify) {
168 std::string Result = Str;
169 char Quote = Charify ? '\'' : '"';
170 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
171 if (Result[i] == '\\' || Result[i] == Quote) {
172 Result.insert(Result.begin()+i, '\\');
173 ++i; ++e;
174 }
175 }
176 return Result;
177}
178
Chris Lattnerd8e30832007-07-24 06:57:14 +0000179/// Stringify - Convert the specified string into a C string by escaping '\'
180/// and " characters. This does not add surrounding ""'s to the string.
181void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
182 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
183 if (Str[i] == '\\' || Str[i] == '"') {
184 Str.insert(Str.begin()+i, '\\');
185 ++i; ++e;
186 }
187 }
188}
189
Reid Spencer5f016e22007-07-11 17:01:13 +0000190
Chris Lattner9a611942007-10-17 21:18:47 +0000191/// MeasureTokenLength - Relex the token at the specified location and return
192/// its length in bytes in the input file. If the token needs cleaning (e.g.
193/// includes a trigraph or an escaped newline) then this count includes bytes
194/// that are part of that.
195unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
196 const SourceManager &SM) {
197 // If this comes from a macro expansion, we really do want the macro name, not
198 // the token this macro expanded to.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000199 Loc = SM.getInstantiationLoc(Loc);
Chris Lattner9a611942007-10-17 21:18:47 +0000200
201 const char *StrData = SM.getCharacterData(Loc);
202
203 // TODO: this could be special cased for common tokens like identifiers, ')',
204 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
205 // all obviously single-char tokens. This could use
206 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
207 // something.
Chris Lattnerde96c0f2009-01-17 07:42:27 +0000208 std::pair<const char *,const char *> Buffer = SM.getBufferData(Loc);
Chris Lattner9a611942007-10-17 21:18:47 +0000209
210 // Create a langops struct and enable trigraphs. This is sufficient for
211 // measuring tokens.
212 LangOptions LangOpts;
213 LangOpts.Trigraphs = true;
214
215 // Create a lexer starting at the beginning of this token.
Chris Lattnerde96c0f2009-01-17 07:42:27 +0000216 Lexer TheLexer(Loc, LangOpts, Buffer.first, StrData, Buffer.second);
Chris Lattner9a611942007-10-17 21:18:47 +0000217 Token TheTok;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000218 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner9a611942007-10-17 21:18:47 +0000219 return TheTok.getLength();
220}
221
Reid Spencer5f016e22007-07-11 17:01:13 +0000222//===----------------------------------------------------------------------===//
223// Character information.
224//===----------------------------------------------------------------------===//
225
226static unsigned char CharInfo[256];
227
228enum {
229 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
230 CHAR_VERT_WS = 0x02, // '\r', '\n'
231 CHAR_LETTER = 0x04, // a-z,A-Z
232 CHAR_NUMBER = 0x08, // 0-9
233 CHAR_UNDER = 0x10, // _
234 CHAR_PERIOD = 0x20 // .
235};
236
237static void InitCharacterInfo() {
238 static bool isInited = false;
239 if (isInited) return;
240 isInited = true;
241
242 // Intiialize the CharInfo table.
243 // TODO: statically initialize this.
244 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
245 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
246 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
247
248 CharInfo[(int)'_'] = CHAR_UNDER;
249 CharInfo[(int)'.'] = CHAR_PERIOD;
250 for (unsigned i = 'a'; i <= 'z'; ++i)
251 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
252 for (unsigned i = '0'; i <= '9'; ++i)
253 CharInfo[i] = CHAR_NUMBER;
254}
255
256/// isIdentifierBody - Return true if this is the body character of an
257/// identifier, which is [a-zA-Z0-9_].
258static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000259 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000260}
261
262/// isHorizontalWhitespace - Return true if this character is horizontal
263/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
264static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000265 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000266}
267
268/// isWhitespace - Return true if this character is horizontal or vertical
269/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
270/// for '\0'.
271static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000272 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000273}
274
275/// isNumberBody - Return true if this is the body character of an
276/// preprocessing number, which is [a-zA-Z0-9_.].
277static inline bool isNumberBody(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000278 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
279 true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000280}
281
282
283//===----------------------------------------------------------------------===//
284// Diagnostics forwarding code.
285//===----------------------------------------------------------------------===//
286
Chris Lattner409a0362007-07-22 18:38:25 +0000287/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
288/// lexer buffer was all instantiated at a single point, perform the mapping.
289/// This is currently only used for _Pragma implementation, so it is the slow
290/// path of the hot getSourceLocation method. Do not allow it to be inlined.
291static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
292 SourceLocation FileLoc,
293 unsigned CharNo) DISABLE_INLINE;
294static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
295 SourceLocation FileLoc,
296 unsigned CharNo) {
297 // Otherwise, we're lexing "mapped tokens". This is used for things like
298 // _Pragma handling. Combine the instantiation location of FileLoc with the
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000299 // spelling location.
Chris Lattner409a0362007-07-22 18:38:25 +0000300 SourceManager &SourceMgr = PP.getSourceManager();
301
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000302 // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000303 // characters come from spelling(FileLoc)+Offset.
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000304 SourceLocation InstLoc = SourceMgr.getInstantiationLoc(FileLoc);
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000305 SourceLocation SpellingLoc = SourceMgr.getSpellingLoc(FileLoc);
306 SpellingLoc = SourceLocation::getFileLoc(SpellingLoc.getFileID(), CharNo);
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000307 return SourceMgr.getInstantiationLoc(SpellingLoc, InstLoc);
Chris Lattner409a0362007-07-22 18:38:25 +0000308}
309
Reid Spencer5f016e22007-07-11 17:01:13 +0000310/// getSourceLocation - Return a source location identifier for the specified
311/// offset in the current file.
312SourceLocation Lexer::getSourceLocation(const char *Loc) const {
Chris Lattner448cec42007-07-22 18:44:36 +0000313 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000314 "Location out of range for this buffer!");
Chris Lattner9dc1f532007-07-20 16:37:10 +0000315
316 // In the normal case, we're just lexing from a simple file buffer, return
317 // the file id from FileLoc with the offset specified.
Chris Lattner448cec42007-07-22 18:44:36 +0000318 unsigned CharNo = Loc-BufferStart;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000319 if (FileLoc.isFileID())
320 return SourceLocation::getFileLoc(FileLoc.getFileID(), CharNo);
321
Chris Lattner2b2453a2009-01-17 06:22:33 +0000322 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
323 // tokens are lexed from where the _Pragma was defined.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000324 assert(PP && "This doesn't work on raw lexers");
325 return GetMappedTokenLoc(*PP, FileLoc, CharNo);
Reid Spencer5f016e22007-07-11 17:01:13 +0000326}
327
Reid Spencer5f016e22007-07-11 17:01:13 +0000328/// Diag - Forwarding function for diagnostics. This translate a source
329/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000330DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner3692b092008-11-18 07:59:24 +0000331 return PP->Diag(getSourceLocation(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000332}
Reid Spencer5f016e22007-07-11 17:01:13 +0000333
334//===----------------------------------------------------------------------===//
335// Trigraph and Escaped Newline Handling Code.
336//===----------------------------------------------------------------------===//
337
338/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
339/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
340static char GetTrigraphCharForLetter(char Letter) {
341 switch (Letter) {
342 default: return 0;
343 case '=': return '#';
344 case ')': return ']';
345 case '(': return '[';
346 case '!': return '|';
347 case '\'': return '^';
348 case '>': return '}';
349 case '/': return '\\';
350 case '<': return '{';
351 case '-': return '~';
352 }
353}
354
355/// DecodeTrigraphChar - If the specified character is a legal trigraph when
356/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
357/// return the result character. Finally, emit a warning about trigraph use
358/// whether trigraphs are enabled or not.
359static char DecodeTrigraphChar(const char *CP, Lexer *L) {
360 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner3692b092008-11-18 07:59:24 +0000361 if (!Res || !L) return Res;
362
363 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +0000364 if (!L->isLexingRawMode())
365 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner3692b092008-11-18 07:59:24 +0000366 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000367 }
Chris Lattner3692b092008-11-18 07:59:24 +0000368
Chris Lattner74d15df2008-11-22 02:02:22 +0000369 if (!L->isLexingRawMode())
370 L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000371 return Res;
372}
373
374/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
375/// get its size, and return it. This is tricky in several cases:
376/// 1. If currently at the start of a trigraph, we warn about the trigraph,
377/// then either return the trigraph (skipping 3 chars) or the '?',
378/// depending on whether trigraphs are enabled or not.
379/// 2. If this is an escaped newline (potentially with whitespace between
380/// the backslash and newline), implicitly skip the newline and return
381/// the char after it.
382/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
383///
384/// This handles the slow/uncommon case of the getCharAndSize method. Here we
385/// know that we can accumulate into Size, and that we have already incremented
386/// Ptr by Size bytes.
387///
388/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
389/// be updated to match.
390///
391char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattnerd2177732007-07-20 16:59:19 +0000392 Token *Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000393 // If we have a slash, look for an escaped newline.
394 if (Ptr[0] == '\\') {
395 ++Size;
396 ++Ptr;
397Slash:
398 // Common case, backslash-char where the char is not whitespace.
399 if (!isWhitespace(Ptr[0])) return '\\';
400
401 // See if we have optional whitespace characters followed by a newline.
402 {
403 unsigned SizeTmp = 0;
404 do {
405 ++SizeTmp;
406 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
407 // Remember that this token needs to be cleaned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000408 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000409
410 // Warn if there was whitespace between the backslash and newline.
Chris Lattner74d15df2008-11-22 02:02:22 +0000411 if (SizeTmp != 1 && Tok && !isLexingRawMode())
Reid Spencer5f016e22007-07-11 17:01:13 +0000412 Diag(Ptr, diag::backslash_newline_space);
413
414 // If this is a \r\n or \n\r, skip the newlines.
415 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
416 Ptr[SizeTmp-1] != Ptr[SizeTmp])
417 ++SizeTmp;
418
419 // Found backslash<whitespace><newline>. Parse the char after it.
420 Size += SizeTmp;
421 Ptr += SizeTmp;
422 // Use slow version to accumulate a correct size field.
423 return getCharAndSizeSlow(Ptr, Size, Tok);
424 }
425 } while (isWhitespace(Ptr[SizeTmp]));
426 }
427
428 // Otherwise, this is not an escaped newline, just return the slash.
429 return '\\';
430 }
431
432 // If this is a trigraph, process it.
433 if (Ptr[0] == '?' && Ptr[1] == '?') {
434 // If this is actually a legal trigraph (not something like "??x"), emit
435 // a trigraph warning. If so, and if trigraphs are enabled, return it.
436 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
437 // Remember that this token needs to be cleaned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000438 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000439
440 Ptr += 3;
441 Size += 3;
442 if (C == '\\') goto Slash;
443 return C;
444 }
445 }
446
447 // If this is neither, return a single character.
448 ++Size;
449 return *Ptr;
450}
451
452
453/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
454/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
455/// and that we have already incremented Ptr by Size bytes.
456///
457/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
458/// be updated to match.
459char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
460 const LangOptions &Features) {
461 // If we have a slash, look for an escaped newline.
462 if (Ptr[0] == '\\') {
463 ++Size;
464 ++Ptr;
465Slash:
466 // Common case, backslash-char where the char is not whitespace.
467 if (!isWhitespace(Ptr[0])) return '\\';
468
469 // See if we have optional whitespace characters followed by a newline.
470 {
471 unsigned SizeTmp = 0;
472 do {
473 ++SizeTmp;
474 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
475
476 // If this is a \r\n or \n\r, skip the newlines.
477 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
478 Ptr[SizeTmp-1] != Ptr[SizeTmp])
479 ++SizeTmp;
480
481 // Found backslash<whitespace><newline>. Parse the char after it.
482 Size += SizeTmp;
483 Ptr += SizeTmp;
484
485 // Use slow version to accumulate a correct size field.
486 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
487 }
488 } while (isWhitespace(Ptr[SizeTmp]));
489 }
490
491 // Otherwise, this is not an escaped newline, just return the slash.
492 return '\\';
493 }
494
495 // If this is a trigraph, process it.
496 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
497 // If this is actually a legal trigraph (not something like "??x"), return
498 // it.
499 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
500 Ptr += 3;
501 Size += 3;
502 if (C == '\\') goto Slash;
503 return C;
504 }
505 }
506
507 // If this is neither, return a single character.
508 ++Size;
509 return *Ptr;
510}
511
512//===----------------------------------------------------------------------===//
513// Helper methods for lexing.
514//===----------------------------------------------------------------------===//
515
Chris Lattnerd2177732007-07-20 16:59:19 +0000516void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000517 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
518 unsigned Size;
519 unsigned char C = *CurPtr++;
520 while (isIdentifierBody(C)) {
521 C = *CurPtr++;
522 }
523 --CurPtr; // Back up over the skipped character.
524
525 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
526 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
527 // FIXME: UCNs.
528 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
529FinishIdentifier:
530 const char *IdStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000531 FormTokenWithChars(Result, CurPtr, tok::identifier);
Reid Spencer5f016e22007-07-11 17:01:13 +0000532
533 // If we are in raw mode, return this identifier raw. There is no need to
534 // look up identifier information or attempt to macro expand it.
535 if (LexingRawMode) return;
536
537 // Fill in Result.IdentifierInfo, looking up the identifier in the
538 // identifier table.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000539 PP->LookUpIdentifierInfo(Result, IdStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000540
541 // Finally, now that we know we have an identifier, pass this off to the
542 // preprocessor, which may macro expand it or something.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000543 return PP->HandleIdentifier(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 }
545
546 // Otherwise, $,\,? in identifier found. Enter slower path.
547
548 C = getCharAndSize(CurPtr, Size);
549 while (1) {
550 if (C == '$') {
551 // If we hit a $ and they are not supported in identifiers, we are done.
552 if (!Features.DollarIdents) goto FinishIdentifier;
553
554 // Otherwise, emit a diagnostic and continue.
Chris Lattner74d15df2008-11-22 02:02:22 +0000555 if (!isLexingRawMode())
556 Diag(CurPtr, diag::ext_dollar_in_identifier);
Reid Spencer5f016e22007-07-11 17:01:13 +0000557 CurPtr = ConsumeChar(CurPtr, Size, Result);
558 C = getCharAndSize(CurPtr, Size);
559 continue;
560 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
561 // Found end of identifier.
562 goto FinishIdentifier;
563 }
564
565 // Otherwise, this character is good, consume it.
566 CurPtr = ConsumeChar(CurPtr, Size, Result);
567
568 C = getCharAndSize(CurPtr, Size);
569 while (isIdentifierBody(C)) { // FIXME: UCNs.
570 CurPtr = ConsumeChar(CurPtr, Size, Result);
571 C = getCharAndSize(CurPtr, Size);
572 }
573 }
574}
575
576
Nate Begeman5253c7f2008-04-14 02:26:39 +0000577/// LexNumericConstant - Lex the remainder of a integer or floating point
Reid Spencer5f016e22007-07-11 17:01:13 +0000578/// constant. From[-1] is the first character lexed. Return the end of the
579/// constant.
Chris Lattnerd2177732007-07-20 16:59:19 +0000580void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 unsigned Size;
582 char C = getCharAndSize(CurPtr, Size);
583 char PrevCh = 0;
584 while (isNumberBody(C)) { // FIXME: UCNs?
585 CurPtr = ConsumeChar(CurPtr, Size, Result);
586 PrevCh = C;
587 C = getCharAndSize(CurPtr, Size);
588 }
589
590 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
591 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
592 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
593
594 // If we have a hex FP constant, continue.
Chris Lattner49842122008-11-22 07:39:03 +0000595 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
596 (Features.HexFloats || !Features.NoExtensions))
Reid Spencer5f016e22007-07-11 17:01:13 +0000597 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
598
Reid Spencer5f016e22007-07-11 17:01:13 +0000599 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +0000600 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Reid Spencer5f016e22007-07-11 17:01:13 +0000601}
602
603/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
604/// either " or L".
Chris Lattnerd88dc482008-10-12 04:05:48 +0000605void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 const char *NulCharacter = 0; // Does this string contain the \0 character?
607
608 char C = getAndAdvanceChar(CurPtr, Result);
609 while (C != '"') {
610 // Skip escaped characters.
611 if (C == '\\') {
612 // Skip the escaped character.
613 C = getAndAdvanceChar(CurPtr, Result);
614 } else if (C == '\n' || C == '\r' || // Newline.
615 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner74d15df2008-11-22 02:02:22 +0000616 if (!isLexingRawMode())
617 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000618 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000619 return;
620 } else if (C == 0) {
621 NulCharacter = CurPtr-1;
622 }
623 C = getAndAdvanceChar(CurPtr, Result);
624 }
625
626 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000627 if (NulCharacter && !isLexingRawMode())
628 Diag(NulCharacter, diag::null_in_string);
Reid Spencer5f016e22007-07-11 17:01:13 +0000629
Reid Spencer5f016e22007-07-11 17:01:13 +0000630 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner9e6293d2008-10-12 04:51:35 +0000631 FormTokenWithChars(Result, CurPtr,
632 Wide ? tok::wide_string_literal : tok::string_literal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000633}
634
635/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
636/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattnerd2177732007-07-20 16:59:19 +0000637void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000638 const char *NulCharacter = 0; // Does this string contain the \0 character?
639
640 char C = getAndAdvanceChar(CurPtr, Result);
641 while (C != '>') {
642 // Skip escaped characters.
643 if (C == '\\') {
644 // Skip the escaped character.
645 C = getAndAdvanceChar(CurPtr, Result);
646 } else if (C == '\n' || C == '\r' || // Newline.
647 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner74d15df2008-11-22 02:02:22 +0000648 if (!isLexingRawMode())
649 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000650 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 return;
652 } else if (C == 0) {
653 NulCharacter = CurPtr-1;
654 }
655 C = getAndAdvanceChar(CurPtr, Result);
656 }
657
658 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000659 if (NulCharacter && !isLexingRawMode())
660 Diag(NulCharacter, diag::null_in_string);
Reid Spencer5f016e22007-07-11 17:01:13 +0000661
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +0000663 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Reid Spencer5f016e22007-07-11 17:01:13 +0000664}
665
666
667/// LexCharConstant - Lex the remainder of a character constant, after having
668/// lexed either ' or L'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000669void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 const char *NulCharacter = 0; // Does this character contain the \0 character?
671
672 // Handle the common case of 'x' and '\y' efficiently.
673 char C = getAndAdvanceChar(CurPtr, Result);
674 if (C == '\'') {
Chris Lattner74d15df2008-11-22 02:02:22 +0000675 if (!isLexingRawMode())
676 Diag(BufferPtr, diag::err_empty_character);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000677 FormTokenWithChars(Result, CurPtr, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000678 return;
679 } else if (C == '\\') {
680 // Skip the escaped character.
681 // FIXME: UCN's.
682 C = getAndAdvanceChar(CurPtr, Result);
683 }
684
685 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
686 ++CurPtr;
687 } else {
688 // Fall back on generic code for embedded nulls, newlines, wide chars.
689 do {
690 // Skip escaped characters.
691 if (C == '\\') {
692 // Skip the escaped character.
693 C = getAndAdvanceChar(CurPtr, Result);
694 } else if (C == '\n' || C == '\r' || // Newline.
695 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner74d15df2008-11-22 02:02:22 +0000696 if (!isLexingRawMode())
697 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000698 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 return;
700 } else if (C == 0) {
701 NulCharacter = CurPtr-1;
702 }
703 C = getAndAdvanceChar(CurPtr, Result);
704 } while (C != '\'');
705 }
706
Chris Lattner74d15df2008-11-22 02:02:22 +0000707 if (NulCharacter && !isLexingRawMode())
708 Diag(NulCharacter, diag::null_in_char);
Reid Spencer5f016e22007-07-11 17:01:13 +0000709
Reid Spencer5f016e22007-07-11 17:01:13 +0000710 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +0000711 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Reid Spencer5f016e22007-07-11 17:01:13 +0000712}
713
714/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
715/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattnerd88dc482008-10-12 04:05:48 +0000716///
717/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
718///
719bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000720 // Whitespace - Skip it, then return the token after the whitespace.
721 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
722 while (1) {
723 // Skip horizontal whitespace very aggressively.
724 while (isHorizontalWhitespace(Char))
725 Char = *++CurPtr;
726
Daniel Dunbarddd3e8b2008-11-25 00:20:22 +0000727 // Otherwise if we have something other than whitespace, we're done.
Reid Spencer5f016e22007-07-11 17:01:13 +0000728 if (Char != '\n' && Char != '\r')
729 break;
730
731 if (ParsingPreprocessorDirective) {
732 // End of preprocessor directive line, let LexTokenInternal handle this.
733 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +0000734 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000735 }
736
737 // ok, but handle newline.
738 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +0000739 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +0000741 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 Char = *++CurPtr;
743 }
744
745 // If this isn't immediately after a newline, there is leading space.
746 char PrevChar = CurPtr[-1];
747 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattnerd2177732007-07-20 16:59:19 +0000748 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000749
Chris Lattnerd88dc482008-10-12 04:05:48 +0000750 // If the client wants us to return whitespace, return it now.
751 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +0000752 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +0000753 return true;
754 }
755
Reid Spencer5f016e22007-07-11 17:01:13 +0000756 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +0000757 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000758}
759
760// SkipBCPLComment - We have just read the // characters from input. Skip until
761// we find the newline character thats terminate the comment. Then update
Chris Lattner2d381892008-10-12 04:15:42 +0000762/// BufferPtr and return. If we're in KeepCommentMode, this will form the token
763/// and return true.
Chris Lattnerd2177732007-07-20 16:59:19 +0000764bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 // If BCPL comments aren't explicitly enabled for this language, emit an
766 // extension warning.
Chris Lattner74d15df2008-11-22 02:02:22 +0000767 if (!Features.BCPLComment && !isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000768 Diag(BufferPtr, diag::ext_bcpl_comment);
769
770 // Mark them enabled so we only emit one warning for this translation
771 // unit.
772 Features.BCPLComment = true;
773 }
774
775 // Scan over the body of the comment. The common case, when scanning, is that
776 // the comment contains normal ascii characters with nothing interesting in
777 // them. As such, optimize for this case with the inner loop.
778 char C;
779 do {
780 C = *CurPtr;
781 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
782 // If we find a \n character, scan backwards, checking to see if it's an
783 // escaped newline, like we do for block comments.
784
785 // Skip over characters in the fast loop.
786 while (C != 0 && // Potentially EOF.
787 C != '\\' && // Potentially escaped newline.
788 C != '?' && // Potentially trigraph.
789 C != '\n' && C != '\r') // Newline or DOS-style newline.
790 C = *++CurPtr;
791
792 // If this is a newline, we're done.
793 if (C == '\n' || C == '\r')
794 break; // Found the newline? Break out!
795
796 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000797 // properly decode the character. Read it in raw mode to avoid emitting
798 // diagnostics about things like trigraphs. If we see an escaped newline,
799 // we'll handle it below.
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 const char *OldPtr = CurPtr;
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000801 bool OldRawMode = isLexingRawMode();
802 LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000803 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000804 LexingRawMode = OldRawMode;
Reid Spencer5f016e22007-07-11 17:01:13 +0000805
806 // If we read multiple characters, and one of those characters was a \r or
807 // \n, then we had an escaped newline within the comment. Emit diagnostic
808 // unless the next line is also a // comment.
809 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
810 for (; OldPtr != CurPtr; ++OldPtr)
811 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
812 // Okay, we found a // comment that ends in a newline, if the next
813 // line is also a // comment, but has spaces, don't emit a diagnostic.
814 if (isspace(C)) {
815 const char *ForwardPtr = CurPtr;
816 while (isspace(*ForwardPtr)) // Skip whitespace.
817 ++ForwardPtr;
818 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
819 break;
820 }
821
Chris Lattner74d15df2008-11-22 02:02:22 +0000822 if (!isLexingRawMode())
823 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000824 break;
825 }
826 }
827
828 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
829 } while (C != '\n' && C != '\r');
830
831 // Found but did not consume the newline.
832
833 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +0000834 if (inKeepCommentMode())
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 return SaveBCPLComment(Result, CurPtr);
836
837 // If we are inside a preprocessor directive and we see the end of line,
838 // return immediately, so that the lexer can return this as an EOM token.
839 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
840 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +0000841 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000842 }
843
844 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner7a4f0042008-10-12 00:23:07 +0000845 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattnerd88dc482008-10-12 04:05:48 +0000846 // contribute to another token), it isn't needed for correctness. Note that
847 // this is ok even in KeepWhitespaceMode, because we would have returned the
848 /// comment above in that mode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000849 ++CurPtr;
850
851 // The next returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +0000852 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000853 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +0000854 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000855 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +0000856 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000857}
858
859/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
860/// an appropriate way and return it.
Chris Lattnerd2177732007-07-20 16:59:19 +0000861bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner9e6293d2008-10-12 04:51:35 +0000862 // If we're not in a preprocessor directive, just return the // comment
863 // directly.
864 FormTokenWithChars(Result, CurPtr, tok::comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000865
Chris Lattner9e6293d2008-10-12 04:51:35 +0000866 if (!ParsingPreprocessorDirective)
867 return true;
868
869 // If this BCPL-style comment is in a macro definition, transmogrify it into
870 // a C-style block comment.
871 std::string Spelling = PP->getSpelling(Result);
872 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
873 Spelling[1] = '*'; // Change prefix to "/*".
874 Spelling += "*/"; // add suffix.
875
876 Result.setKind(tok::comment);
877 Result.setLocation(PP->CreateString(&Spelling[0], Spelling.size(),
878 Result.getLocation()));
879 Result.setLength(Spelling.size());
Chris Lattner2d381892008-10-12 04:15:42 +0000880 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000881}
882
883/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
884/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner47a2b402008-12-12 07:14:34 +0000885/// diagnostic if so. We know that the newline is inside of a block comment.
Reid Spencer5f016e22007-07-11 17:01:13 +0000886static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
887 Lexer *L) {
888 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
889
890 // Back up off the newline.
891 --CurPtr;
892
893 // If this is a two-character newline sequence, skip the other character.
894 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
895 // \n\n or \r\r -> not escaped newline.
896 if (CurPtr[0] == CurPtr[1])
897 return false;
898 // \n\r or \r\n -> skip the newline.
899 --CurPtr;
900 }
901
902 // If we have horizontal whitespace, skip over it. We allow whitespace
903 // between the slash and newline.
904 bool HasSpace = false;
905 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
906 --CurPtr;
907 HasSpace = true;
908 }
909
910 // If we have a slash, we know this is an escaped newline.
911 if (*CurPtr == '\\') {
912 if (CurPtr[-1] != '*') return false;
913 } else {
914 // It isn't a slash, is it the ?? / trigraph?
915 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
916 CurPtr[-3] != '*')
917 return false;
918
919 // This is the trigraph ending the comment. Emit a stern warning!
920 CurPtr -= 2;
921
922 // If no trigraphs are enabled, warn that we ignored this trigraph and
923 // ignore this * character.
924 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +0000925 if (!L->isLexingRawMode())
926 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 return false;
928 }
Chris Lattner74d15df2008-11-22 02:02:22 +0000929 if (!L->isLexingRawMode())
930 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 }
932
933 // Warn about having an escaped newline between the */ characters.
Chris Lattner74d15df2008-11-22 02:02:22 +0000934 if (!L->isLexingRawMode())
935 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Reid Spencer5f016e22007-07-11 17:01:13 +0000936
937 // If there was space between the backslash and newline, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000938 if (HasSpace && !L->isLexingRawMode())
939 L->Diag(CurPtr, diag::backslash_newline_space);
Reid Spencer5f016e22007-07-11 17:01:13 +0000940
941 return true;
942}
943
944#ifdef __SSE2__
945#include <emmintrin.h>
946#elif __ALTIVEC__
947#include <altivec.h>
948#undef bool
949#endif
950
951/// SkipBlockComment - We have just read the /* characters from input. Read
952/// until we find the */ characters that terminate the comment. Note that we
953/// don't bother decoding trigraphs or escaped newlines in block comments,
954/// because they cannot cause the comment to end. The only thing that can
955/// happen is the comment could end with an escaped newline between the */ end
956/// of comment.
Chris Lattner2d381892008-10-12 04:15:42 +0000957///
958/// If KeepCommentMode is enabled, this forms a token from the comment and
959/// returns true.
Chris Lattnerd2177732007-07-20 16:59:19 +0000960bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000961 // Scan one character past where we should, looking for a '/' character. Once
962 // we find it, check to see if it was preceeded by a *. This common
963 // optimization helps people who like to put a lot of * characters in their
964 // comments.
Chris Lattner8146b682007-07-21 23:43:37 +0000965
966 // The first character we get with newlines and trigraphs skipped to handle
967 // the degenerate /*/ case below correctly if the * has an escaped newline
968 // after it.
969 unsigned CharSize;
970 unsigned char C = getCharAndSize(CurPtr, CharSize);
971 CurPtr += CharSize;
Reid Spencer5f016e22007-07-11 17:01:13 +0000972 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner74d15df2008-11-22 02:02:22 +0000973 if (!isLexingRawMode())
Chris Lattner0af57422008-10-12 01:31:51 +0000974 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner31f0eca2008-10-12 04:19:49 +0000975 --CurPtr;
976
977 // KeepWhitespaceMode should return this broken comment as a token. Since
978 // it isn't a well formed comment, just return it as an 'unknown' token.
979 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +0000980 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +0000981 return true;
982 }
983
984 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +0000985 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 }
987
Chris Lattner8146b682007-07-21 23:43:37 +0000988 // Check to see if the first character after the '/*' is another /. If so,
989 // then this slash does not end the block comment, it is part of it.
990 if (C == '/')
991 C = *CurPtr++;
992
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 while (1) {
994 // Skip over all non-interesting characters until we find end of buffer or a
995 // (probably ending) '/' character.
996 if (CurPtr + 24 < BufferEnd) {
997 // While not aligned to a 16-byte boundary.
998 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
999 C = *CurPtr++;
1000
1001 if (C == '/') goto FoundSlash;
1002
1003#ifdef __SSE2__
1004 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1005 '/', '/', '/', '/', '/', '/', '/', '/');
1006 while (CurPtr+16 <= BufferEnd &&
1007 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1008 CurPtr += 16;
1009#elif __ALTIVEC__
1010 __vector unsigned char Slashes = {
1011 '/', '/', '/', '/', '/', '/', '/', '/',
1012 '/', '/', '/', '/', '/', '/', '/', '/'
1013 };
1014 while (CurPtr+16 <= BufferEnd &&
1015 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1016 CurPtr += 16;
1017#else
1018 // Scan for '/' quickly. Many block comments are very large.
1019 while (CurPtr[0] != '/' &&
1020 CurPtr[1] != '/' &&
1021 CurPtr[2] != '/' &&
1022 CurPtr[3] != '/' &&
1023 CurPtr+4 < BufferEnd) {
1024 CurPtr += 4;
1025 }
1026#endif
1027
1028 // It has to be one of the bytes scanned, increment to it and read one.
1029 C = *CurPtr++;
1030 }
1031
1032 // Loop to scan the remainder.
1033 while (C != '/' && C != '\0')
1034 C = *CurPtr++;
1035
1036 FoundSlash:
1037 if (C == '/') {
1038 if (CurPtr[-2] == '*') // We found the final */. We're done!
1039 break;
1040
1041 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
1042 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
1043 // We found the final */, though it had an escaped newline between the
1044 // * and /. We're done!
1045 break;
1046 }
1047 }
1048 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1049 // If this is a /* inside of the comment, emit a warning. Don't do this
1050 // if this is a /*/, which will end the comment. This misses cases with
1051 // embedded escaped newlines, but oh well.
Chris Lattner74d15df2008-11-22 02:02:22 +00001052 if (!isLexingRawMode())
1053 Diag(CurPtr-1, diag::warn_nested_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 }
1055 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001056 if (!isLexingRawMode())
1057 Diag(BufferPtr, diag::err_unterminated_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 // Note: the user probably forgot a */. We could continue immediately
1059 // after the /*, but this would involve lexing a lot of what really is the
1060 // comment, which surely would confuse the parser.
Chris Lattner31f0eca2008-10-12 04:19:49 +00001061 --CurPtr;
1062
1063 // KeepWhitespaceMode should return this broken comment as a token. Since
1064 // it isn't a well formed comment, just return it as an 'unknown' token.
1065 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001066 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001067 return true;
1068 }
1069
1070 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001071 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001072 }
1073 C = *CurPtr++;
1074 }
1075
1076 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +00001077 if (inKeepCommentMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001078 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattner2d381892008-10-12 04:15:42 +00001079 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001080 }
1081
1082 // It is common for the tokens immediately after a /**/ comment to be
1083 // whitespace. Instead of going through the big switch, handle it
Chris Lattnerd88dc482008-10-12 04:05:48 +00001084 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1085 // have already returned above with the comment as a token.
Reid Spencer5f016e22007-07-11 17:01:13 +00001086 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001087 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001088 SkipWhitespace(Result, CurPtr+1);
Chris Lattner2d381892008-10-12 04:15:42 +00001089 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 }
1091
1092 // Otherwise, just return so that the next character will be lexed as a token.
1093 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001094 Result.setFlag(Token::LeadingSpace);
Chris Lattner2d381892008-10-12 04:15:42 +00001095 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001096}
1097
1098//===----------------------------------------------------------------------===//
1099// Primary Lexing Entry Points
1100//===----------------------------------------------------------------------===//
1101
Reid Spencer5f016e22007-07-11 17:01:13 +00001102/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1103/// uninterpreted string. This switches the lexer out of directive mode.
1104std::string Lexer::ReadToEndOfLine() {
1105 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1106 "Must be in a preprocessing directive!");
1107 std::string Result;
Chris Lattnerd2177732007-07-20 16:59:19 +00001108 Token Tmp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001109
1110 // CurPtr - Cache BufferPtr in an automatic variable.
1111 const char *CurPtr = BufferPtr;
1112 while (1) {
1113 char Char = getAndAdvanceChar(CurPtr, Tmp);
1114 switch (Char) {
1115 default:
1116 Result += Char;
1117 break;
1118 case 0: // Null.
1119 // Found end of file?
1120 if (CurPtr-1 != BufferEnd) {
1121 // Nope, normal character, continue.
1122 Result += Char;
1123 break;
1124 }
1125 // FALL THROUGH.
1126 case '\r':
1127 case '\n':
1128 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1129 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1130 BufferPtr = CurPtr-1;
1131
1132 // Next, lex the character, which should handle the EOM transition.
1133 Lex(Tmp);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001134 assert(Tmp.is(tok::eom) && "Unexpected token!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001135
1136 // Finally, we're done, return the string we found.
1137 return Result;
1138 }
1139 }
1140}
1141
1142/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1143/// condition, reporting diagnostics and handling other edge cases as required.
1144/// This returns true if Result contains a token, false if PP.Lex should be
1145/// called again.
Chris Lattnerd2177732007-07-20 16:59:19 +00001146bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 // If we hit the end of the file while parsing a preprocessor directive,
1148 // end the preprocessor directive first. The next token returned will
1149 // then be the end of file.
1150 if (ParsingPreprocessorDirective) {
1151 // Done parsing the "line".
1152 ParsingPreprocessorDirective = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00001154 FormTokenWithChars(Result, CurPtr, tok::eom);
Reid Spencer5f016e22007-07-11 17:01:13 +00001155
1156 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001157 SetCommentRetentionState(PP->getCommentRetentionState());
Reid Spencer5f016e22007-07-11 17:01:13 +00001158 return true; // Have a token.
1159 }
1160
1161 // If we are in raw mode, return this event as an EOF token. Let the caller
1162 // that put us in raw mode handle the event.
Chris Lattner74d15df2008-11-22 02:02:22 +00001163 if (isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 Result.startToken();
1165 BufferPtr = BufferEnd;
Chris Lattner9e6293d2008-10-12 04:51:35 +00001166 FormTokenWithChars(Result, BufferEnd, tok::eof);
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 return true;
1168 }
1169
1170 // Otherwise, issue diagnostics for unterminated #if and missing newline.
1171
1172 // If we are in a #if directive, emit an error.
1173 while (!ConditionalStack.empty()) {
Chris Lattner30c64762008-11-22 06:22:39 +00001174 PP->Diag(ConditionalStack.back().IfLoc,
1175 diag::err_pp_unterminated_conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 ConditionalStack.pop_back();
1177 }
1178
Chris Lattnerb25e5d72008-04-12 05:54:25 +00001179 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1180 // a pedwarn.
1181 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Reid Spencer5f016e22007-07-11 17:01:13 +00001182 Diag(BufferEnd, diag::ext_no_newline_eof);
1183
1184 BufferPtr = CurPtr;
1185
1186 // Finally, let the preprocessor handle this.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001187 return PP->HandleEndOfFile(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001188}
1189
1190/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1191/// the specified lexer will return a tok::l_paren token, 0 if it is something
1192/// else and 2 if there are no more tokens in the buffer controlled by the
1193/// lexer.
1194unsigned Lexer::isNextPPTokenLParen() {
1195 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1196
1197 // Switch to 'skipping' mode. This will ensure that we can lex a token
1198 // without emitting diagnostics, disables macro expansion, and will cause EOF
1199 // to return an EOF token instead of popping the include stack.
1200 LexingRawMode = true;
1201
1202 // Save state that can be changed while lexing so that we can restore it.
1203 const char *TmpBufferPtr = BufferPtr;
1204
Chris Lattnerd2177732007-07-20 16:59:19 +00001205 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 Tok.startToken();
1207 LexTokenInternal(Tok);
1208
1209 // Restore state that may have changed.
1210 BufferPtr = TmpBufferPtr;
1211
1212 // Restore the lexer back to non-skipping mode.
1213 LexingRawMode = false;
1214
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001215 if (Tok.is(tok::eof))
Reid Spencer5f016e22007-07-11 17:01:13 +00001216 return 2;
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001217 return Tok.is(tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001218}
1219
1220
1221/// LexTokenInternal - This implements a simple C family lexer. It is an
1222/// extremely performance critical piece of code. This assumes that the buffer
1223/// has a null character at the end of the file. Return true if an error
1224/// occurred and compilation should terminate, false if normal. This returns a
1225/// preprocessing token, not a normal token, as such, it is an internal
1226/// interface. It assumes that the Flags of result have been cleared before
1227/// calling this.
Chris Lattnerd2177732007-07-20 16:59:19 +00001228void Lexer::LexTokenInternal(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001229LexNextToken:
1230 // New token, can't need cleaning yet.
Chris Lattnerd2177732007-07-20 16:59:19 +00001231 Result.clearFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 Result.setIdentifierInfo(0);
1233
1234 // CurPtr - Cache BufferPtr in an automatic variable.
1235 const char *CurPtr = BufferPtr;
1236
1237 // Small amounts of horizontal whitespace is very common between tokens.
1238 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1239 ++CurPtr;
1240 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1241 ++CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +00001242
1243 // If we are keeping whitespace and other tokens, just return what we just
1244 // skipped. The next lexer invocation will return the token after the
1245 // whitespace.
1246 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001247 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001248 return;
1249 }
1250
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001252 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001253 }
1254
1255 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1256
1257 // Read a character, advancing over it.
1258 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001259 tok::TokenKind Kind;
1260
Reid Spencer5f016e22007-07-11 17:01:13 +00001261 switch (Char) {
1262 case 0: // Null.
1263 // Found end of file?
1264 if (CurPtr-1 == BufferEnd) {
1265 // Read the PP instance variable into an automatic variable, because
1266 // LexEndOfFile will often delete 'this'.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001267 Preprocessor *PPCache = PP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1269 return; // Got a token to return.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001270 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1271 return PPCache->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 }
1273
Chris Lattner74d15df2008-11-22 02:02:22 +00001274 if (!isLexingRawMode())
1275 Diag(CurPtr-1, diag::null_in_file);
Chris Lattnerd2177732007-07-20 16:59:19 +00001276 Result.setFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001277 if (SkipWhitespace(Result, CurPtr))
1278 return; // KeepWhitespaceMode
1279
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 goto LexNextToken; // GCC isn't tail call eliminating.
1281 case '\n':
1282 case '\r':
1283 // If we are inside a preprocessor directive and we see the end of line,
1284 // we know we are done with the directive, so return an EOM token.
1285 if (ParsingPreprocessorDirective) {
1286 // Done parsing the "line".
1287 ParsingPreprocessorDirective = false;
1288
1289 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001290 SetCommentRetentionState(PP->getCommentRetentionState());
Reid Spencer5f016e22007-07-11 17:01:13 +00001291
1292 // Since we consumed a newline, we are back at the start of a line.
1293 IsAtStartOfLine = true;
1294
Chris Lattner9e6293d2008-10-12 04:51:35 +00001295 Kind = tok::eom;
Reid Spencer5f016e22007-07-11 17:01:13 +00001296 break;
1297 }
1298 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +00001299 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001300 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +00001301 Result.clearFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001302
1303 if (SkipWhitespace(Result, CurPtr))
1304 return; // KeepWhitespaceMode
Reid Spencer5f016e22007-07-11 17:01:13 +00001305 goto LexNextToken; // GCC isn't tail call eliminating.
1306 case ' ':
1307 case '\t':
1308 case '\f':
1309 case '\v':
Chris Lattner8133cfc2007-07-22 06:29:05 +00001310 SkipHorizontalWhitespace:
Chris Lattnerd2177732007-07-20 16:59:19 +00001311 Result.setFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001312 if (SkipWhitespace(Result, CurPtr))
1313 return; // KeepWhitespaceMode
Chris Lattner8133cfc2007-07-22 06:29:05 +00001314
1315 SkipIgnoredUnits:
1316 CurPtr = BufferPtr;
1317
1318 // If the next token is obviously a // or /* */ comment, skip it efficiently
1319 // too (without going through the big switch stmt).
Chris Lattner8402c732009-01-16 22:39:25 +00001320 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1321 Features.BCPLComment) {
Chris Lattner8133cfc2007-07-22 06:29:05 +00001322 SkipBCPLComment(Result, CurPtr+2);
1323 goto SkipIgnoredUnits;
Chris Lattnerfa95a012008-10-12 03:22:02 +00001324 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner8133cfc2007-07-22 06:29:05 +00001325 SkipBlockComment(Result, CurPtr+2);
1326 goto SkipIgnoredUnits;
1327 } else if (isHorizontalWhitespace(*CurPtr)) {
1328 goto SkipHorizontalWhitespace;
1329 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001330 goto LexNextToken; // GCC isn't tail call eliminating.
1331
Chris Lattner3a570772008-01-03 17:58:54 +00001332 // C99 6.4.4.1: Integer Constants.
1333 // C99 6.4.4.2: Floating Constants.
1334 case '0': case '1': case '2': case '3': case '4':
1335 case '5': case '6': case '7': case '8': case '9':
1336 // Notify MIOpt that we read a non-whitespace/non-comment token.
1337 MIOpt.ReadToken();
1338 return LexNumericConstant(Result, CurPtr);
1339
1340 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 // Notify MIOpt that we read a non-whitespace/non-comment token.
1342 MIOpt.ReadToken();
1343 Char = getCharAndSize(CurPtr, SizeTmp);
1344
1345 // Wide string literal.
1346 if (Char == '"')
1347 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1348 true);
1349
1350 // Wide character constant.
1351 if (Char == '\'')
1352 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1353 // FALL THROUGH, treating L like the start of an identifier.
1354
1355 // C99 6.4.2: Identifiers.
1356 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1357 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1358 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1359 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1360 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1361 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1362 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1363 case 'v': case 'w': case 'x': case 'y': case 'z':
1364 case '_':
1365 // Notify MIOpt that we read a non-whitespace/non-comment token.
1366 MIOpt.ReadToken();
1367 return LexIdentifier(Result, CurPtr);
Chris Lattner3a570772008-01-03 17:58:54 +00001368
1369 case '$': // $ in identifiers.
1370 if (Features.DollarIdents) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001371 if (!isLexingRawMode())
1372 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner3a570772008-01-03 17:58:54 +00001373 // Notify MIOpt that we read a non-whitespace/non-comment token.
1374 MIOpt.ReadToken();
1375 return LexIdentifier(Result, CurPtr);
1376 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001377
Chris Lattner9e6293d2008-10-12 04:51:35 +00001378 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00001379 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001380
1381 // C99 6.4.4: Character Constants.
1382 case '\'':
1383 // Notify MIOpt that we read a non-whitespace/non-comment token.
1384 MIOpt.ReadToken();
1385 return LexCharConstant(Result, CurPtr);
1386
1387 // C99 6.4.5: String Literals.
1388 case '"':
1389 // Notify MIOpt that we read a non-whitespace/non-comment token.
1390 MIOpt.ReadToken();
1391 return LexStringLiteral(Result, CurPtr, false);
1392
1393 // C99 6.4.6: Punctuators.
1394 case '?':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001395 Kind = tok::question;
Reid Spencer5f016e22007-07-11 17:01:13 +00001396 break;
1397 case '[':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001398 Kind = tok::l_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001399 break;
1400 case ']':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001401 Kind = tok::r_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001402 break;
1403 case '(':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001404 Kind = tok::l_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001405 break;
1406 case ')':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001407 Kind = tok::r_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001408 break;
1409 case '{':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001410 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001411 break;
1412 case '}':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001413 Kind = tok::r_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001414 break;
1415 case '.':
1416 Char = getCharAndSize(CurPtr, SizeTmp);
1417 if (Char >= '0' && Char <= '9') {
1418 // Notify MIOpt that we read a non-whitespace/non-comment token.
1419 MIOpt.ReadToken();
1420
1421 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1422 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001423 Kind = tok::periodstar;
Reid Spencer5f016e22007-07-11 17:01:13 +00001424 CurPtr += SizeTmp;
1425 } else if (Char == '.' &&
1426 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001427 Kind = tok::ellipsis;
Reid Spencer5f016e22007-07-11 17:01:13 +00001428 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1429 SizeTmp2, Result);
1430 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001431 Kind = tok::period;
Reid Spencer5f016e22007-07-11 17:01:13 +00001432 }
1433 break;
1434 case '&':
1435 Char = getCharAndSize(CurPtr, SizeTmp);
1436 if (Char == '&') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001437 Kind = tok::ampamp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001438 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1439 } else if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001440 Kind = tok::ampequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001441 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1442 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001443 Kind = tok::amp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001444 }
1445 break;
1446 case '*':
1447 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001448 Kind = tok::starequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001449 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1450 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001451 Kind = tok::star;
Reid Spencer5f016e22007-07-11 17:01:13 +00001452 }
1453 break;
1454 case '+':
1455 Char = getCharAndSize(CurPtr, SizeTmp);
1456 if (Char == '+') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001457 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001458 Kind = tok::plusplus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001459 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001460 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001461 Kind = tok::plusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001462 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001463 Kind = tok::plus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001464 }
1465 break;
1466 case '-':
1467 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001468 if (Char == '-') { // --
Reid Spencer5f016e22007-07-11 17:01:13 +00001469 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001470 Kind = tok::minusminus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001471 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattner9e6293d2008-10-12 04:51:35 +00001472 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Reid Spencer5f016e22007-07-11 17:01:13 +00001473 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1474 SizeTmp2, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001475 Kind = tok::arrowstar;
1476 } else if (Char == '>') { // ->
Reid Spencer5f016e22007-07-11 17:01:13 +00001477 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001478 Kind = tok::arrow;
1479 } else if (Char == '=') { // -=
Reid Spencer5f016e22007-07-11 17:01:13 +00001480 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001481 Kind = tok::minusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001482 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001483 Kind = tok::minus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001484 }
1485 break;
1486 case '~':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001487 Kind = tok::tilde;
Reid Spencer5f016e22007-07-11 17:01:13 +00001488 break;
1489 case '!':
1490 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001491 Kind = tok::exclaimequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001492 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1493 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001494 Kind = tok::exclaim;
Reid Spencer5f016e22007-07-11 17:01:13 +00001495 }
1496 break;
1497 case '/':
1498 // 6.4.9: Comments
1499 Char = getCharAndSize(CurPtr, SizeTmp);
1500 if (Char == '/') { // BCPL comment.
Chris Lattner8402c732009-01-16 22:39:25 +00001501 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
1502 // want to lex this as a comment. There is one problem with this though,
1503 // that in one particular corner case, this can change the behavior of the
1504 // resultant program. For example, In "foo //**/ bar", C89 would lex
1505 // this as "foo / bar" and langauges with BCPL comments would lex it as
1506 // "foo". Check to see if the character after the second slash is a '*'.
1507 // If so, we will lex that as a "/" instead of the start of a comment.
1508 if (Features.BCPLComment ||
1509 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
1510 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1511 return; // KeepCommentMode
Chris Lattner2d381892008-10-12 04:15:42 +00001512
Chris Lattner8402c732009-01-16 22:39:25 +00001513 // It is common for the tokens immediately after a // comment to be
1514 // whitespace (indentation for the next line). Instead of going through
1515 // the big switch, handle it efficiently now.
1516 goto SkipIgnoredUnits;
1517 }
1518 }
1519
1520 if (Char == '*') { // /**/ comment.
Reid Spencer5f016e22007-07-11 17:01:13 +00001521 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner2d381892008-10-12 04:15:42 +00001522 return; // KeepCommentMode
1523 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner8402c732009-01-16 22:39:25 +00001524 }
1525
1526 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001527 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001528 Kind = tok::slashequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001529 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001530 Kind = tok::slash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001531 }
1532 break;
1533 case '%':
1534 Char = getCharAndSize(CurPtr, SizeTmp);
1535 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001536 Kind = tok::percentequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001537 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1538 } else if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001539 Kind = tok::r_brace; // '%>' -> '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001540 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1541 } else if (Features.Digraphs && Char == ':') {
1542 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1543 Char = getCharAndSize(CurPtr, SizeTmp);
1544 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001545 Kind = tok::hashhash; // '%:%:' -> '##'
Reid Spencer5f016e22007-07-11 17:01:13 +00001546 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1547 SizeTmp2, Result);
1548 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Reid Spencer5f016e22007-07-11 17:01:13 +00001549 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner74d15df2008-11-22 02:02:22 +00001550 if (!isLexingRawMode())
1551 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001552 Kind = tok::hashat;
Reid Spencer5f016e22007-07-11 17:01:13 +00001553 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001554 Kind = tok::hash; // '%:' -> '#'
Reid Spencer5f016e22007-07-11 17:01:13 +00001555
1556 // We parsed a # character. If this occurs at the start of the line,
1557 // it's actually the start of a preprocessing directive. Callback to
1558 // the preprocessor to handle it.
1559 // FIXME: -fpreprocessed mode??
1560 if (Result.isAtStartOfLine() && !LexingRawMode) {
1561 BufferPtr = CurPtr;
Chris Lattner168ae2d2007-10-17 20:41:00 +00001562 PP->HandleDirective(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001563
1564 // As an optimization, if the preprocessor didn't switch lexers, tail
1565 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001566 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001567 // Start a new token. If this is a #include or something, the PP may
1568 // want us starting at the beginning of the line again. If so, set
1569 // the StartOfLine flag.
1570 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001571 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001572 IsAtStartOfLine = false;
1573 }
1574 goto LexNextToken; // GCC isn't tail call eliminating.
1575 }
1576
Chris Lattner168ae2d2007-10-17 20:41:00 +00001577 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001578 }
1579 }
1580 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001581 Kind = tok::percent;
Reid Spencer5f016e22007-07-11 17:01:13 +00001582 }
1583 break;
1584 case '<':
1585 Char = getCharAndSize(CurPtr, SizeTmp);
1586 if (ParsingFilename) {
1587 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1588 } else if (Char == '<' &&
1589 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001590 Kind = tok::lesslessequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001591 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1592 SizeTmp2, Result);
1593 } else if (Char == '<') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001594 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001595 Kind = tok::lessless;
Reid Spencer5f016e22007-07-11 17:01:13 +00001596 } else 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::lessequal;
1599 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Reid Spencer5f016e22007-07-11 17:01:13 +00001600 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001601 Kind = tok::l_square;
1602 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Reid Spencer5f016e22007-07-11 17:01:13 +00001603 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001604 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001605 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001606 Kind = tok::less;
Reid Spencer5f016e22007-07-11 17:01:13 +00001607 }
1608 break;
1609 case '>':
1610 Char = getCharAndSize(CurPtr, SizeTmp);
1611 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::greaterequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001614 } else if (Char == '>' &&
1615 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1617 SizeTmp2, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001618 Kind = tok::greatergreaterequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001619 } else if (Char == '>') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001620 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001621 Kind = tok::greatergreater;
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001623 Kind = tok::greater;
Reid Spencer5f016e22007-07-11 17:01:13 +00001624 }
1625 break;
1626 case '^':
1627 Char = getCharAndSize(CurPtr, SizeTmp);
1628 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001629 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001630 Kind = tok::caretequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001631 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001632 Kind = tok::caret;
Reid Spencer5f016e22007-07-11 17:01:13 +00001633 }
1634 break;
1635 case '|':
1636 Char = getCharAndSize(CurPtr, SizeTmp);
1637 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001638 Kind = tok::pipeequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001639 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1640 } else if (Char == '|') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001641 Kind = tok::pipepipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00001642 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1643 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001644 Kind = tok::pipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00001645 }
1646 break;
1647 case ':':
1648 Char = getCharAndSize(CurPtr, SizeTmp);
1649 if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001650 Kind = tok::r_square; // ':>' -> ']'
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1652 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001653 Kind = tok::coloncolon;
Reid Spencer5f016e22007-07-11 17:01:13 +00001654 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1655 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001656 Kind = tok::colon;
Reid Spencer5f016e22007-07-11 17:01:13 +00001657 }
1658 break;
1659 case ';':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001660 Kind = tok::semi;
Reid Spencer5f016e22007-07-11 17:01:13 +00001661 break;
1662 case '=':
1663 Char = getCharAndSize(CurPtr, SizeTmp);
1664 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001665 Kind = tok::equalequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001666 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1667 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001668 Kind = tok::equal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001669 }
1670 break;
1671 case ',':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001672 Kind = tok::comma;
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 break;
1674 case '#':
1675 Char = getCharAndSize(CurPtr, SizeTmp);
1676 if (Char == '#') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001677 Kind = tok::hashhash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001678 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1679 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner9e6293d2008-10-12 04:51:35 +00001680 Kind = tok::hashat;
Chris Lattner74d15df2008-11-22 02:02:22 +00001681 if (!isLexingRawMode())
1682 Diag(BufferPtr, diag::charize_microsoft_ext);
Reid Spencer5f016e22007-07-11 17:01:13 +00001683 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1684 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001685 Kind = tok::hash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001686 // We parsed a # character. If this occurs at the start of the line,
1687 // it's actually the start of a preprocessing directive. Callback to
1688 // the preprocessor to handle it.
1689 // FIXME: -fpreprocessed mode??
1690 if (Result.isAtStartOfLine() && !LexingRawMode) {
1691 BufferPtr = CurPtr;
Chris Lattner168ae2d2007-10-17 20:41:00 +00001692 PP->HandleDirective(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001693
1694 // As an optimization, if the preprocessor didn't switch lexers, tail
1695 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001696 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001697 // Start a new token. If this is a #include or something, the PP may
1698 // want us starting at the beginning of the line again. If so, set
1699 // the StartOfLine flag.
1700 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001701 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001702 IsAtStartOfLine = false;
1703 }
1704 goto LexNextToken; // GCC isn't tail call eliminating.
1705 }
Chris Lattner168ae2d2007-10-17 20:41:00 +00001706 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001707 }
1708 }
1709 break;
1710
Chris Lattner3a570772008-01-03 17:58:54 +00001711 case '@':
1712 // Objective C support.
1713 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattner9e6293d2008-10-12 04:51:35 +00001714 Kind = tok::at;
Chris Lattner3a570772008-01-03 17:58:54 +00001715 else
Chris Lattner9e6293d2008-10-12 04:51:35 +00001716 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00001717 break;
1718
Reid Spencer5f016e22007-07-11 17:01:13 +00001719 case '\\':
1720 // FIXME: UCN's.
1721 // FALL THROUGH.
1722 default:
Chris Lattner9e6293d2008-10-12 04:51:35 +00001723 Kind = tok::unknown;
Reid Spencer5f016e22007-07-11 17:01:13 +00001724 break;
1725 }
1726
1727 // Notify MIOpt that we read a non-whitespace/non-comment token.
1728 MIOpt.ReadToken();
1729
1730 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00001731 FormTokenWithChars(Result, CurPtr, Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001732}