blob: 059afe2182201f3c4205c52f3489b1c645a04940 [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Lexer and Token interfaces.
11//
12//===----------------------------------------------------------------------===//
13//
14// TODO: GCC Diagnostics emitted by the lexer:
15// PEDWARN: (form feed|vertical tab) in preprocessing directive
16//
17// Universal characters, unicode, char mapping:
18// WARNING: `%.*s' is not in NFKC
19// WARNING: `%.*s' is not in NFC
20//
21// Other:
22// TODO: Options to support:
23// -fexec-charset,-fwide-exec-charset
24//
25//===----------------------------------------------------------------------===//
26
27#include "clang/Lex/Lexer.h"
28#include "clang/Lex/Preprocessor.h"
Chris Lattner545f39e2009-01-29 05:15:15 +000029#include "clang/Lex/LexDiagnostic.h"
Chris Lattner4b009652007-07-25 00:24:17 +000030#include "clang/Basic/SourceManager.h"
31#include "llvm/Support/Compiler.h"
32#include "llvm/Support/MemoryBuffer.h"
33#include <cctype>
34using namespace clang;
35
36static void InitCharacterInfo();
37
Chris Lattneraa9bdf12007-10-07 08:47:24 +000038//===----------------------------------------------------------------------===//
39// Token Class Implementation
40//===----------------------------------------------------------------------===//
41
42/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
43bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
Douglas Gregora7502582008-12-01 21:46:47 +000044 if (IdentifierInfo *II = getIdentifierInfo())
45 return II->getObjCKeywordID() == objcKey;
46 return false;
Chris Lattneraa9bdf12007-10-07 08:47:24 +000047}
48
49/// getObjCKeywordID - Return the ObjC keyword kind.
50tok::ObjCKeywordKind Token::getObjCKeywordID() const {
51 IdentifierInfo *specId = getIdentifierInfo();
52 return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
53}
54
Chris Lattner7208a4b2007-12-13 01:59:49 +000055
Chris Lattneraa9bdf12007-10-07 08:47:24 +000056//===----------------------------------------------------------------------===//
57// Lexer Class Implementation
58//===----------------------------------------------------------------------===//
59
Chris Lattner9bb53672009-01-17 06:55:17 +000060void Lexer::InitLexer(const char *BufStart, const char *BufPtr,
61 const char *BufEnd) {
62 InitCharacterInfo();
63
64 BufferStart = BufStart;
65 BufferPtr = BufPtr;
66 BufferEnd = BufEnd;
67
68 assert(BufEnd[0] == 0 &&
69 "We assume that the input buffer has a null character at the end"
70 " to simplify lexing!");
71
72 Is_PragmaLexer = false;
73
74 // Start of the file is a start of line.
75 IsAtStartOfLine = true;
76
77 // We are not after parsing a #.
78 ParsingPreprocessorDirective = false;
79
80 // We are not after parsing #include.
81 ParsingFilename = false;
82
83 // We are not in raw mode. Raw mode disables diagnostics and interpretation
84 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
85 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
86 // or otherwise skipping over tokens.
87 LexingRawMode = false;
88
89 // Default to not keeping comments.
90 ExtendedTokenMode = 0;
91}
92
Chris Lattner54edb962009-01-17 07:56:59 +000093/// Lexer constructor - Create a new lexer object for the specified buffer
94/// with the specified preprocessor managing the lexing process. This lexer
95/// assumes that the associated file buffer and Preprocessor objects will
96/// outlive it, so it doesn't take ownership of either of them.
Chris Lattner5df086f2009-01-17 08:03:42 +000097Lexer::Lexer(FileID FID, Preprocessor &PP)
98 : PreprocessorLexer(&PP, FID),
99 FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
100 Features(PP.getLangOptions()) {
Chris Lattner54edb962009-01-17 07:56:59 +0000101
Chris Lattner5df086f2009-01-17 08:03:42 +0000102 const llvm::MemoryBuffer *InputFile = PP.getSourceManager().getBuffer(FID);
Chris Lattner54edb962009-01-17 07:56:59 +0000103
104 InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
105 InputFile->getBufferEnd());
106
107 // Default to keeping comments if the preprocessor wants them.
108 SetCommentRetentionState(PP.getCommentRetentionState());
109}
Chris Lattneraa9bdf12007-10-07 08:47:24 +0000110
Chris Lattner342dccb2007-10-17 20:41:00 +0000111/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner0b5892e2008-10-12 01:15:46 +0000112/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
113/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner342dccb2007-10-17 20:41:00 +0000114Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattnerded7b322009-01-17 07:42:27 +0000115 const char *BufStart, const char *BufPtr, const char *BufEnd)
Chris Lattneref63fd52009-01-17 03:48:08 +0000116 : FileLoc(fileloc), Features(features) {
Chris Lattner9bb53672009-01-17 06:55:17 +0000117
Chris Lattner9bb53672009-01-17 06:55:17 +0000118 InitLexer(BufStart, BufPtr, BufEnd);
Chris Lattner342dccb2007-10-17 20:41:00 +0000119
120 // We *are* in raw mode.
121 LexingRawMode = true;
Chris Lattner342dccb2007-10-17 20:41:00 +0000122}
123
Chris Lattnerc7b23592009-01-17 07:35:14 +0000124/// Lexer constructor - Create a new raw lexer object. This object is only
125/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
126/// range will outlive it, so it doesn't take ownership of it.
127Lexer::Lexer(FileID FID, const SourceManager &SM, const LangOptions &features)
128 : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
129 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
130
131 InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
132 FromFile->getBufferEnd());
133
134 // We *are* in raw mode.
135 LexingRawMode = true;
136}
137
Chris Lattnercef77822009-01-17 08:27:52 +0000138/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
139/// _Pragma expansion. This has a variety of magic semantics that this method
140/// sets up. It returns a new'd Lexer that must be delete'd when done.
141///
142/// On entrance to this routine, TokStartLoc is a macro location which has a
143/// spelling loc that indicates the bytes to be lexed for the token and an
144/// instantiation location that indicates where all lexed tokens should be
145/// "expanded from".
146///
147/// FIXME: It would really be nice to make _Pragma just be a wrapper around a
148/// normal lexer that remaps tokens as they fly by. This would require making
149/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer
150/// interface that could handle this stuff. This would pull GetMappedTokenLoc
151/// out of the critical path of the lexer!
152///
Chris Lattnere805eac2009-01-19 06:46:35 +0000153Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000154 SourceLocation InstantiationLocStart,
155 SourceLocation InstantiationLocEnd,
Chris Lattnere805eac2009-01-19 06:46:35 +0000156 unsigned TokLen, Preprocessor &PP) {
Chris Lattnercef77822009-01-17 08:27:52 +0000157 SourceManager &SM = PP.getSourceManager();
Chris Lattnercef77822009-01-17 08:27:52 +0000158
159 // Create the lexer as if we were going to lex the file normally.
Chris Lattner1737bd52009-01-19 07:46:45 +0000160 FileID SpellingFID = SM.getFileID(SpellingLoc);
Chris Lattnere805eac2009-01-19 06:46:35 +0000161 Lexer *L = new Lexer(SpellingFID, PP);
Chris Lattnercef77822009-01-17 08:27:52 +0000162
163 // Now that the lexer is created, change the start/end locations so that we
164 // just lex the subsection of the file that we want. This is lexing from a
165 // scratch buffer.
166 const char *StrData = SM.getCharacterData(SpellingLoc);
167
168 L->BufferPtr = StrData;
169 L->BufferEnd = StrData+TokLen;
Chris Lattnerd6d3feb2009-03-08 08:08:45 +0000170 assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
Chris Lattnercef77822009-01-17 08:27:52 +0000171
172 // Set the SourceLocation with the remapping information. This ensures that
173 // GetMappedTokenLoc will remap the tokens as they are lexed.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000174 L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000175 InstantiationLocStart,
176 InstantiationLocEnd, TokLen);
Chris Lattnercef77822009-01-17 08:27:52 +0000177
178 // Ensure that the lexer thinks it is inside a directive, so that end \n will
179 // return an EOM token.
180 L->ParsingPreprocessorDirective = true;
181
182 // This lexer really is for _Pragma.
183 L->Is_PragmaLexer = true;
184 return L;
185}
186
Chris Lattner342dccb2007-10-17 20:41:00 +0000187
Chris Lattner4b009652007-07-25 00:24:17 +0000188/// Stringify - Convert the specified string into a C string, with surrounding
189/// ""'s, and with escaped \ and " characters.
190std::string Lexer::Stringify(const std::string &Str, bool Charify) {
191 std::string Result = Str;
192 char Quote = Charify ? '\'' : '"';
193 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
194 if (Result[i] == '\\' || Result[i] == Quote) {
195 Result.insert(Result.begin()+i, '\\');
196 ++i; ++e;
197 }
198 }
199 return Result;
200}
201
202/// Stringify - Convert the specified string into a C string by escaping '\'
203/// and " characters. This does not add surrounding ""'s to the string.
204void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
205 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
206 if (Str[i] == '\\' || Str[i] == '"') {
207 Str.insert(Str.begin()+i, '\\');
208 ++i; ++e;
209 }
210 }
211}
212
213
Chris Lattner761d76b2007-10-17 21:18:47 +0000214/// MeasureTokenLength - Relex the token at the specified location and return
215/// its length in bytes in the input file. If the token needs cleaning (e.g.
216/// includes a trigraph or an escaped newline) then this count includes bytes
217/// that are part of that.
218unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
219 const SourceManager &SM) {
Chris Lattner761d76b2007-10-17 21:18:47 +0000220 // TODO: this could be special cased for common tokens like identifiers, ')',
221 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
222 // all obviously single-char tokens. This could use
223 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
224 // something.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000225
226 // If this comes from a macro expansion, we really do want the macro name, not
227 // the token this macro expanded to.
Chris Lattner41ca1592009-01-26 22:24:27 +0000228 Loc = SM.getInstantiationLoc(Loc);
229 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
Chris Lattner003dfcb2009-01-17 08:30:10 +0000230 std::pair<const char *,const char *> Buffer = SM.getBufferData(LocInfo.first);
231 const char *StrData = Buffer.first+LocInfo.second;
232
Chris Lattner761d76b2007-10-17 21:18:47 +0000233 // Create a langops struct and enable trigraphs. This is sufficient for
234 // measuring tokens.
235 LangOptions LangOpts;
236 LangOpts.Trigraphs = true;
237
238 // Create a lexer starting at the beginning of this token.
Chris Lattnerded7b322009-01-17 07:42:27 +0000239 Lexer TheLexer(Loc, LangOpts, Buffer.first, StrData, Buffer.second);
Chris Lattner761d76b2007-10-17 21:18:47 +0000240 Token TheTok;
Chris Lattner0b5892e2008-10-12 01:15:46 +0000241 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner761d76b2007-10-17 21:18:47 +0000242 return TheTok.getLength();
243}
244
Chris Lattner4b009652007-07-25 00:24:17 +0000245//===----------------------------------------------------------------------===//
246// Character information.
247//===----------------------------------------------------------------------===//
248
249static unsigned char CharInfo[256];
250
251enum {
252 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
253 CHAR_VERT_WS = 0x02, // '\r', '\n'
254 CHAR_LETTER = 0x04, // a-z,A-Z
255 CHAR_NUMBER = 0x08, // 0-9
256 CHAR_UNDER = 0x10, // _
257 CHAR_PERIOD = 0x20 // .
258};
259
260static void InitCharacterInfo() {
261 static bool isInited = false;
262 if (isInited) return;
263 isInited = true;
264
265 // Intiialize the CharInfo table.
266 // TODO: statically initialize this.
267 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
268 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
269 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
270
271 CharInfo[(int)'_'] = CHAR_UNDER;
272 CharInfo[(int)'.'] = CHAR_PERIOD;
273 for (unsigned i = 'a'; i <= 'z'; ++i)
274 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
275 for (unsigned i = '0'; i <= '9'; ++i)
276 CharInfo[i] = CHAR_NUMBER;
277}
278
279/// isIdentifierBody - Return true if this is the body character of an
280/// identifier, which is [a-zA-Z0-9_].
281static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiserc62f6b92007-10-18 12:47:01 +0000282 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Chris Lattner4b009652007-07-25 00:24:17 +0000283}
284
285/// isHorizontalWhitespace - Return true if this character is horizontal
286/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
287static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiserc62f6b92007-10-18 12:47:01 +0000288 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Chris Lattner4b009652007-07-25 00:24:17 +0000289}
290
291/// isWhitespace - Return true if this character is horizontal or vertical
292/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
293/// for '\0'.
294static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiserc62f6b92007-10-18 12:47:01 +0000295 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Chris Lattner4b009652007-07-25 00:24:17 +0000296}
297
298/// isNumberBody - Return true if this is the body character of an
299/// preprocessing number, which is [a-zA-Z0-9_.].
300static inline bool isNumberBody(unsigned char c) {
Hartmut Kaiserc62f6b92007-10-18 12:47:01 +0000301 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
302 true : false;
Chris Lattner4b009652007-07-25 00:24:17 +0000303}
304
305
306//===----------------------------------------------------------------------===//
307// Diagnostics forwarding code.
308//===----------------------------------------------------------------------===//
309
310/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
311/// lexer buffer was all instantiated at a single point, perform the mapping.
312/// This is currently only used for _Pragma implementation, so it is the slow
313/// path of the hot getSourceLocation method. Do not allow it to be inlined.
314static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
315 SourceLocation FileLoc,
Chris Lattner27c0ced2009-01-26 00:43:02 +0000316 unsigned CharNo,
317 unsigned TokLen) DISABLE_INLINE;
Chris Lattner4b009652007-07-25 00:24:17 +0000318static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
319 SourceLocation FileLoc,
Chris Lattner27c0ced2009-01-26 00:43:02 +0000320 unsigned CharNo, unsigned TokLen) {
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000321 assert(FileLoc.isMacroID() && "Must be an instantiation");
322
Chris Lattner4b009652007-07-25 00:24:17 +0000323 // Otherwise, we're lexing "mapped tokens". This is used for things like
324 // _Pragma handling. Combine the instantiation location of FileLoc with the
Chris Lattnercdf600e2009-01-16 07:00:02 +0000325 // spelling location.
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000326 SourceManager &SM = PP.getSourceManager();
Chris Lattner4b009652007-07-25 00:24:17 +0000327
Chris Lattner18c8dc02009-01-16 07:36:28 +0000328 // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
Chris Lattnercdf600e2009-01-16 07:00:02 +0000329 // characters come from spelling(FileLoc)+Offset.
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000330 SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
Chris Lattnere805eac2009-01-19 06:46:35 +0000331 SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
Chris Lattnerbc7a3cb2009-02-15 20:52:18 +0000332
333 // Figure out the expansion loc range, which is the range covered by the
334 // original _Pragma(...) sequence.
335 std::pair<SourceLocation,SourceLocation> II =
336 SM.getImmediateInstantiationRange(FileLoc);
337
338 return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
Chris Lattner4b009652007-07-25 00:24:17 +0000339}
340
341/// getSourceLocation - Return a source location identifier for the specified
342/// offset in the current file.
Chris Lattner27c0ced2009-01-26 00:43:02 +0000343SourceLocation Lexer::getSourceLocation(const char *Loc,
344 unsigned TokLen) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000345 assert(Loc >= BufferStart && Loc <= BufferEnd &&
346 "Location out of range for this buffer!");
347
348 // In the normal case, we're just lexing from a simple file buffer, return
349 // the file id from FileLoc with the offset specified.
350 unsigned CharNo = Loc-BufferStart;
351 if (FileLoc.isFileID())
Chris Lattnere805eac2009-01-19 06:46:35 +0000352 return FileLoc.getFileLocWithOffset(CharNo);
Chris Lattner4b009652007-07-25 00:24:17 +0000353
Chris Lattnerf4f776a2009-01-17 06:22:33 +0000354 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
355 // tokens are lexed from where the _Pragma was defined.
Chris Lattner342dccb2007-10-17 20:41:00 +0000356 assert(PP && "This doesn't work on raw lexers");
Chris Lattner27c0ced2009-01-26 00:43:02 +0000357 return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
Chris Lattner4b009652007-07-25 00:24:17 +0000358}
359
360/// Diag - Forwarding function for diagnostics. This translate a source
361/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner9943e982008-11-22 00:59:29 +0000362DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner0370d6b2008-11-18 07:59:24 +0000363 return PP->Diag(getSourceLocation(Loc), DiagID);
Chris Lattner4b009652007-07-25 00:24:17 +0000364}
Chris Lattner4b009652007-07-25 00:24:17 +0000365
366//===----------------------------------------------------------------------===//
367// Trigraph and Escaped Newline Handling Code.
368//===----------------------------------------------------------------------===//
369
370/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
371/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
372static char GetTrigraphCharForLetter(char Letter) {
373 switch (Letter) {
374 default: return 0;
375 case '=': return '#';
376 case ')': return ']';
377 case '(': return '[';
378 case '!': return '|';
379 case '\'': return '^';
380 case '>': return '}';
381 case '/': return '\\';
382 case '<': return '{';
383 case '-': return '~';
384 }
385}
386
387/// DecodeTrigraphChar - If the specified character is a legal trigraph when
388/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
389/// return the result character. Finally, emit a warning about trigraph use
390/// whether trigraphs are enabled or not.
391static char DecodeTrigraphChar(const char *CP, Lexer *L) {
392 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner0370d6b2008-11-18 07:59:24 +0000393 if (!Res || !L) return Res;
394
395 if (!L->getFeatures().Trigraphs) {
Chris Lattnerf9c62772008-11-22 02:02:22 +0000396 if (!L->isLexingRawMode())
397 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner0370d6b2008-11-18 07:59:24 +0000398 return 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000399 }
Chris Lattner0370d6b2008-11-18 07:59:24 +0000400
Chris Lattnerf9c62772008-11-22 02:02:22 +0000401 if (!L->isLexingRawMode())
402 L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
Chris Lattner4b009652007-07-25 00:24:17 +0000403 return Res;
404}
405
406/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
407/// get its size, and return it. This is tricky in several cases:
408/// 1. If currently at the start of a trigraph, we warn about the trigraph,
409/// then either return the trigraph (skipping 3 chars) or the '?',
410/// depending on whether trigraphs are enabled or not.
411/// 2. If this is an escaped newline (potentially with whitespace between
412/// the backslash and newline), implicitly skip the newline and return
413/// the char after it.
414/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
415///
416/// This handles the slow/uncommon case of the getCharAndSize method. Here we
417/// know that we can accumulate into Size, and that we have already incremented
418/// Ptr by Size bytes.
419///
420/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
421/// be updated to match.
422///
423char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
424 Token *Tok) {
425 // If we have a slash, look for an escaped newline.
426 if (Ptr[0] == '\\') {
427 ++Size;
428 ++Ptr;
429Slash:
430 // Common case, backslash-char where the char is not whitespace.
431 if (!isWhitespace(Ptr[0])) return '\\';
432
433 // See if we have optional whitespace characters followed by a newline.
434 {
435 unsigned SizeTmp = 0;
436 do {
437 ++SizeTmp;
438 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
439 // Remember that this token needs to be cleaned.
440 if (Tok) Tok->setFlag(Token::NeedsCleaning);
441
442 // Warn if there was whitespace between the backslash and newline.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000443 if (SizeTmp != 1 && Tok && !isLexingRawMode())
Chris Lattner4b009652007-07-25 00:24:17 +0000444 Diag(Ptr, diag::backslash_newline_space);
445
446 // If this is a \r\n or \n\r, skip the newlines.
447 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
448 Ptr[SizeTmp-1] != Ptr[SizeTmp])
449 ++SizeTmp;
450
451 // Found backslash<whitespace><newline>. Parse the char after it.
452 Size += SizeTmp;
453 Ptr += SizeTmp;
454 // Use slow version to accumulate a correct size field.
455 return getCharAndSizeSlow(Ptr, Size, Tok);
456 }
457 } while (isWhitespace(Ptr[SizeTmp]));
458 }
459
460 // Otherwise, this is not an escaped newline, just return the slash.
461 return '\\';
462 }
463
464 // If this is a trigraph, process it.
465 if (Ptr[0] == '?' && Ptr[1] == '?') {
466 // If this is actually a legal trigraph (not something like "??x"), emit
467 // a trigraph warning. If so, and if trigraphs are enabled, return it.
468 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
469 // Remember that this token needs to be cleaned.
470 if (Tok) Tok->setFlag(Token::NeedsCleaning);
471
472 Ptr += 3;
473 Size += 3;
474 if (C == '\\') goto Slash;
475 return C;
476 }
477 }
478
479 // If this is neither, return a single character.
480 ++Size;
481 return *Ptr;
482}
483
484
485/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
486/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
487/// and that we have already incremented Ptr by Size bytes.
488///
489/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
490/// be updated to match.
491char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
492 const LangOptions &Features) {
493 // If we have a slash, look for an escaped newline.
494 if (Ptr[0] == '\\') {
495 ++Size;
496 ++Ptr;
497Slash:
498 // Common case, backslash-char where the char is not whitespace.
499 if (!isWhitespace(Ptr[0])) return '\\';
500
501 // See if we have optional whitespace characters followed by a newline.
502 {
503 unsigned SizeTmp = 0;
504 do {
505 ++SizeTmp;
506 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
507
508 // If this is a \r\n or \n\r, skip the newlines.
509 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
510 Ptr[SizeTmp-1] != Ptr[SizeTmp])
511 ++SizeTmp;
512
513 // Found backslash<whitespace><newline>. Parse the char after it.
514 Size += SizeTmp;
515 Ptr += SizeTmp;
516
517 // Use slow version to accumulate a correct size field.
518 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
519 }
520 } while (isWhitespace(Ptr[SizeTmp]));
521 }
522
523 // Otherwise, this is not an escaped newline, just return the slash.
524 return '\\';
525 }
526
527 // If this is a trigraph, process it.
528 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
529 // If this is actually a legal trigraph (not something like "??x"), return
530 // it.
531 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
532 Ptr += 3;
533 Size += 3;
534 if (C == '\\') goto Slash;
535 return C;
536 }
537 }
538
539 // If this is neither, return a single character.
540 ++Size;
541 return *Ptr;
542}
543
544//===----------------------------------------------------------------------===//
545// Helper methods for lexing.
546//===----------------------------------------------------------------------===//
547
548void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
549 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
550 unsigned Size;
551 unsigned char C = *CurPtr++;
552 while (isIdentifierBody(C)) {
553 C = *CurPtr++;
554 }
555 --CurPtr; // Back up over the skipped character.
556
557 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
558 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
559 // FIXME: UCNs.
560 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
561FinishIdentifier:
562 const char *IdStart = BufferPtr;
Chris Lattner0344cc72008-10-12 04:51:35 +0000563 FormTokenWithChars(Result, CurPtr, tok::identifier);
Chris Lattner4b009652007-07-25 00:24:17 +0000564
565 // If we are in raw mode, return this identifier raw. There is no need to
566 // look up identifier information or attempt to macro expand it.
567 if (LexingRawMode) return;
568
569 // Fill in Result.IdentifierInfo, looking up the identifier in the
570 // identifier table.
Chris Lattner2f93c3d2009-01-21 07:45:14 +0000571 IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
Chris Lattner4b009652007-07-25 00:24:17 +0000572
Chris Lattner9ac3dd92009-01-23 18:35:48 +0000573 // Change the kind of this identifier to the appropriate token kind, e.g.
574 // turning "for" into a keyword.
575 Result.setKind(II->getTokenID());
576
Chris Lattner4b009652007-07-25 00:24:17 +0000577 // Finally, now that we know we have an identifier, pass this off to the
578 // preprocessor, which may macro expand it or something.
Chris Lattner2f93c3d2009-01-21 07:45:14 +0000579 if (II->isHandleIdentifierCase())
Chris Lattner5b747d02009-01-21 07:43:11 +0000580 PP->HandleIdentifier(Result);
581 return;
Chris Lattner4b009652007-07-25 00:24:17 +0000582 }
583
584 // Otherwise, $,\,? in identifier found. Enter slower path.
585
586 C = getCharAndSize(CurPtr, Size);
587 while (1) {
588 if (C == '$') {
589 // If we hit a $ and they are not supported in identifiers, we are done.
590 if (!Features.DollarIdents) goto FinishIdentifier;
591
592 // Otherwise, emit a diagnostic and continue.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000593 if (!isLexingRawMode())
594 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner4b009652007-07-25 00:24:17 +0000595 CurPtr = ConsumeChar(CurPtr, Size, Result);
596 C = getCharAndSize(CurPtr, Size);
597 continue;
598 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
599 // Found end of identifier.
600 goto FinishIdentifier;
601 }
602
603 // Otherwise, this character is good, consume it.
604 CurPtr = ConsumeChar(CurPtr, Size, Result);
605
606 C = getCharAndSize(CurPtr, Size);
607 while (isIdentifierBody(C)) { // FIXME: UCNs.
608 CurPtr = ConsumeChar(CurPtr, Size, Result);
609 C = getCharAndSize(CurPtr, Size);
610 }
611 }
612}
613
614
Nate Begeman937cac72008-04-14 02:26:39 +0000615/// LexNumericConstant - Lex the remainder of a integer or floating point
Chris Lattner4b009652007-07-25 00:24:17 +0000616/// constant. From[-1] is the first character lexed. Return the end of the
617/// constant.
618void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
619 unsigned Size;
620 char C = getCharAndSize(CurPtr, Size);
621 char PrevCh = 0;
622 while (isNumberBody(C)) { // FIXME: UCNs?
623 CurPtr = ConsumeChar(CurPtr, Size, Result);
624 PrevCh = C;
625 C = getCharAndSize(CurPtr, Size);
626 }
627
628 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
629 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
630 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
631
632 // If we have a hex FP constant, continue.
Chris Lattner9ba63822008-11-22 07:39:03 +0000633 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
634 (Features.HexFloats || !Features.NoExtensions))
Chris Lattner4b009652007-07-25 00:24:17 +0000635 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
636
Chris Lattner4b009652007-07-25 00:24:17 +0000637 // Update the location of token as well as BufferPtr.
Chris Lattner6ad1f502009-01-26 19:29:26 +0000638 const char *TokStart = BufferPtr;
Chris Lattner0344cc72008-10-12 04:51:35 +0000639 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Chris Lattner6ad1f502009-01-26 19:29:26 +0000640 Result.setLiteralData(TokStart);
Chris Lattner4b009652007-07-25 00:24:17 +0000641}
642
643/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
644/// either " or L".
Chris Lattner867a87b2008-10-12 04:05:48 +0000645void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Chris Lattner4b009652007-07-25 00:24:17 +0000646 const char *NulCharacter = 0; // Does this string contain the \0 character?
647
648 char C = getAndAdvanceChar(CurPtr, Result);
649 while (C != '"') {
650 // Skip escaped characters.
651 if (C == '\\') {
652 // Skip the escaped character.
653 C = getAndAdvanceChar(CurPtr, Result);
654 } else if (C == '\n' || C == '\r' || // Newline.
655 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnere7299ca2009-03-18 21:10:12 +0000656 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattnerf9c62772008-11-22 02:02:22 +0000657 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner0344cc72008-10-12 04:51:35 +0000658 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner4b009652007-07-25 00:24:17 +0000659 return;
660 } else if (C == 0) {
661 NulCharacter = CurPtr-1;
662 }
663 C = getAndAdvanceChar(CurPtr, Result);
664 }
665
666 // If a nul character existed in the string, warn about it.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000667 if (NulCharacter && !isLexingRawMode())
668 Diag(NulCharacter, diag::null_in_string);
Chris Lattner4b009652007-07-25 00:24:17 +0000669
Chris Lattner4b009652007-07-25 00:24:17 +0000670 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner6ad1f502009-01-26 19:29:26 +0000671 const char *TokStart = BufferPtr;
Chris Lattner0344cc72008-10-12 04:51:35 +0000672 FormTokenWithChars(Result, CurPtr,
673 Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner6ad1f502009-01-26 19:29:26 +0000674 Result.setLiteralData(TokStart);
Chris Lattner4b009652007-07-25 00:24:17 +0000675}
676
677/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
678/// after having lexed the '<' character. This is used for #include filenames.
679void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
680 const char *NulCharacter = 0; // Does this string contain the \0 character?
681
682 char C = getAndAdvanceChar(CurPtr, Result);
683 while (C != '>') {
684 // Skip escaped characters.
685 if (C == '\\') {
686 // Skip the escaped character.
687 C = getAndAdvanceChar(CurPtr, Result);
688 } else if (C == '\n' || C == '\r' || // Newline.
689 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnere7299ca2009-03-18 21:10:12 +0000690 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattneradcfb6d2009-02-19 18:29:56 +0000691 Diag(BufferPtr, diag::err_unterminated_angled_string);
Chris Lattner0344cc72008-10-12 04:51:35 +0000692 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner4b009652007-07-25 00:24:17 +0000693 return;
694 } else if (C == 0) {
695 NulCharacter = CurPtr-1;
696 }
697 C = getAndAdvanceChar(CurPtr, Result);
698 }
699
700 // If a nul character existed in the string, warn about it.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000701 if (NulCharacter && !isLexingRawMode())
702 Diag(NulCharacter, diag::null_in_string);
Chris Lattner4b009652007-07-25 00:24:17 +0000703
Chris Lattner4b009652007-07-25 00:24:17 +0000704 // Update the location of token as well as BufferPtr.
Chris Lattner6ad1f502009-01-26 19:29:26 +0000705 const char *TokStart = BufferPtr;
Chris Lattner0344cc72008-10-12 04:51:35 +0000706 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Chris Lattner6ad1f502009-01-26 19:29:26 +0000707 Result.setLiteralData(TokStart);
Chris Lattner4b009652007-07-25 00:24:17 +0000708}
709
710
711/// LexCharConstant - Lex the remainder of a character constant, after having
712/// lexed either ' or L'.
713void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
714 const char *NulCharacter = 0; // Does this character contain the \0 character?
715
716 // Handle the common case of 'x' and '\y' efficiently.
717 char C = getAndAdvanceChar(CurPtr, Result);
718 if (C == '\'') {
Chris Lattnere7299ca2009-03-18 21:10:12 +0000719 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattnerf9c62772008-11-22 02:02:22 +0000720 Diag(BufferPtr, diag::err_empty_character);
Chris Lattner0344cc72008-10-12 04:51:35 +0000721 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4b009652007-07-25 00:24:17 +0000722 return;
723 } else if (C == '\\') {
724 // Skip the escaped character.
725 // FIXME: UCN's.
726 C = getAndAdvanceChar(CurPtr, Result);
727 }
728
729 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
730 ++CurPtr;
731 } else {
732 // Fall back on generic code for embedded nulls, newlines, wide chars.
733 do {
734 // Skip escaped characters.
735 if (C == '\\') {
736 // Skip the escaped character.
737 C = getAndAdvanceChar(CurPtr, Result);
738 } else if (C == '\n' || C == '\r' || // Newline.
739 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnere7299ca2009-03-18 21:10:12 +0000740 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattnerf9c62772008-11-22 02:02:22 +0000741 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner0344cc72008-10-12 04:51:35 +0000742 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner4b009652007-07-25 00:24:17 +0000743 return;
744 } else if (C == 0) {
745 NulCharacter = CurPtr-1;
746 }
747 C = getAndAdvanceChar(CurPtr, Result);
748 } while (C != '\'');
749 }
750
Chris Lattnerf9c62772008-11-22 02:02:22 +0000751 if (NulCharacter && !isLexingRawMode())
752 Diag(NulCharacter, diag::null_in_char);
Chris Lattner4b009652007-07-25 00:24:17 +0000753
Chris Lattner4b009652007-07-25 00:24:17 +0000754 // Update the location of token as well as BufferPtr.
Chris Lattner6ad1f502009-01-26 19:29:26 +0000755 const char *TokStart = BufferPtr;
Chris Lattner0344cc72008-10-12 04:51:35 +0000756 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Chris Lattner6ad1f502009-01-26 19:29:26 +0000757 Result.setLiteralData(TokStart);
Chris Lattner4b009652007-07-25 00:24:17 +0000758}
759
760/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
761/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattner867a87b2008-10-12 04:05:48 +0000762///
763/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
764///
765bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Chris Lattner4b009652007-07-25 00:24:17 +0000766 // Whitespace - Skip it, then return the token after the whitespace.
767 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
768 while (1) {
769 // Skip horizontal whitespace very aggressively.
770 while (isHorizontalWhitespace(Char))
771 Char = *++CurPtr;
772
Daniel Dunbara2208392008-11-25 00:20:22 +0000773 // Otherwise if we have something other than whitespace, we're done.
Chris Lattner4b009652007-07-25 00:24:17 +0000774 if (Char != '\n' && Char != '\r')
775 break;
776
777 if (ParsingPreprocessorDirective) {
778 // End of preprocessor directive line, let LexTokenInternal handle this.
779 BufferPtr = CurPtr;
Chris Lattner867a87b2008-10-12 04:05:48 +0000780 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000781 }
782
783 // ok, but handle newline.
784 // The returned token is at the start of the line.
785 Result.setFlag(Token::StartOfLine);
786 // No leading whitespace seen so far.
787 Result.clearFlag(Token::LeadingSpace);
788 Char = *++CurPtr;
789 }
790
791 // If this isn't immediately after a newline, there is leading space.
792 char PrevChar = CurPtr[-1];
793 if (PrevChar != '\n' && PrevChar != '\r')
794 Result.setFlag(Token::LeadingSpace);
795
Chris Lattner867a87b2008-10-12 04:05:48 +0000796 // If the client wants us to return whitespace, return it now.
797 if (isKeepWhitespaceMode()) {
Chris Lattner0344cc72008-10-12 04:51:35 +0000798 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner867a87b2008-10-12 04:05:48 +0000799 return true;
800 }
801
Chris Lattner4b009652007-07-25 00:24:17 +0000802 BufferPtr = CurPtr;
Chris Lattner867a87b2008-10-12 04:05:48 +0000803 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000804}
805
806// SkipBCPLComment - We have just read the // characters from input. Skip until
807// we find the newline character thats terminate the comment. Then update
Chris Lattnerf03b00c2008-10-12 04:15:42 +0000808/// BufferPtr and return. If we're in KeepCommentMode, this will form the token
809/// and return true.
Chris Lattner4b009652007-07-25 00:24:17 +0000810bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
811 // If BCPL comments aren't explicitly enabled for this language, emit an
812 // extension warning.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000813 if (!Features.BCPLComment && !isLexingRawMode()) {
Chris Lattner4b009652007-07-25 00:24:17 +0000814 Diag(BufferPtr, diag::ext_bcpl_comment);
815
816 // Mark them enabled so we only emit one warning for this translation
817 // unit.
818 Features.BCPLComment = true;
819 }
820
821 // Scan over the body of the comment. The common case, when scanning, is that
822 // the comment contains normal ascii characters with nothing interesting in
823 // them. As such, optimize for this case with the inner loop.
824 char C;
825 do {
826 C = *CurPtr;
827 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
828 // If we find a \n character, scan backwards, checking to see if it's an
829 // escaped newline, like we do for block comments.
830
831 // Skip over characters in the fast loop.
832 while (C != 0 && // Potentially EOF.
833 C != '\\' && // Potentially escaped newline.
834 C != '?' && // Potentially trigraph.
835 C != '\n' && C != '\r') // Newline or DOS-style newline.
836 C = *++CurPtr;
837
838 // If this is a newline, we're done.
839 if (C == '\n' || C == '\r')
840 break; // Found the newline? Break out!
841
842 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnerc3697802008-12-12 07:34:39 +0000843 // properly decode the character. Read it in raw mode to avoid emitting
844 // diagnostics about things like trigraphs. If we see an escaped newline,
845 // we'll handle it below.
Chris Lattner4b009652007-07-25 00:24:17 +0000846 const char *OldPtr = CurPtr;
Chris Lattnerc3697802008-12-12 07:34:39 +0000847 bool OldRawMode = isLexingRawMode();
848 LexingRawMode = true;
Chris Lattner4b009652007-07-25 00:24:17 +0000849 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerc3697802008-12-12 07:34:39 +0000850 LexingRawMode = OldRawMode;
Chris Lattner5edb39d2009-04-05 00:26:41 +0000851
852 // If the char that we finally got was a \n, then we must have had something
853 // like \<newline><newline>. We don't want to have consumed the second
854 // newline, we want CurPtr, to end up pointing to it down below.
855 if (C == '\n' || C == '\r') {
856 --CurPtr;
857 C = 'x'; // doesn't matter what this is.
858 }
Chris Lattner4b009652007-07-25 00:24:17 +0000859
860 // If we read multiple characters, and one of those characters was a \r or
861 // \n, then we had an escaped newline within the comment. Emit diagnostic
862 // unless the next line is also a // comment.
863 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
864 for (; OldPtr != CurPtr; ++OldPtr)
865 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
866 // Okay, we found a // comment that ends in a newline, if the next
867 // line is also a // comment, but has spaces, don't emit a diagnostic.
868 if (isspace(C)) {
869 const char *ForwardPtr = CurPtr;
870 while (isspace(*ForwardPtr)) // Skip whitespace.
871 ++ForwardPtr;
872 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
873 break;
874 }
875
Chris Lattnerf9c62772008-11-22 02:02:22 +0000876 if (!isLexingRawMode())
877 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Chris Lattner4b009652007-07-25 00:24:17 +0000878 break;
879 }
880 }
881
882 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
883 } while (C != '\n' && C != '\r');
884
885 // Found but did not consume the newline.
886
887 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner170adb12008-10-12 03:22:02 +0000888 if (inKeepCommentMode())
Chris Lattner4b009652007-07-25 00:24:17 +0000889 return SaveBCPLComment(Result, CurPtr);
890
891 // If we are inside a preprocessor directive and we see the end of line,
892 // return immediately, so that the lexer can return this as an EOM token.
893 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
894 BufferPtr = CurPtr;
Chris Lattnerf03b00c2008-10-12 04:15:42 +0000895 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000896 }
897
898 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner43d38202008-10-12 00:23:07 +0000899 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattner867a87b2008-10-12 04:05:48 +0000900 // contribute to another token), it isn't needed for correctness. Note that
901 // this is ok even in KeepWhitespaceMode, because we would have returned the
902 /// comment above in that mode.
Chris Lattner4b009652007-07-25 00:24:17 +0000903 ++CurPtr;
904
905 // The next returned token is at the start of the line.
906 Result.setFlag(Token::StartOfLine);
907 // No leading whitespace seen so far.
908 Result.clearFlag(Token::LeadingSpace);
909 BufferPtr = CurPtr;
Chris Lattnerf03b00c2008-10-12 04:15:42 +0000910 return false;
Chris Lattner4b009652007-07-25 00:24:17 +0000911}
912
913/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
914/// an appropriate way and return it.
915bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner0344cc72008-10-12 04:51:35 +0000916 // If we're not in a preprocessor directive, just return the // comment
917 // directly.
918 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattner4b009652007-07-25 00:24:17 +0000919
Chris Lattner0344cc72008-10-12 04:51:35 +0000920 if (!ParsingPreprocessorDirective)
921 return true;
922
923 // If this BCPL-style comment is in a macro definition, transmogrify it into
924 // a C-style block comment.
925 std::string Spelling = PP->getSpelling(Result);
926 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
927 Spelling[1] = '*'; // Change prefix to "/*".
928 Spelling += "*/"; // add suffix.
929
930 Result.setKind(tok::comment);
Chris Lattner6ad1f502009-01-26 19:29:26 +0000931 PP->CreateString(&Spelling[0], Spelling.size(), Result,
932 Result.getLocation());
Chris Lattnerf03b00c2008-10-12 04:15:42 +0000933 return true;
Chris Lattner4b009652007-07-25 00:24:17 +0000934}
935
936/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
937/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattnerb3872872008-12-12 07:14:34 +0000938/// diagnostic if so. We know that the newline is inside of a block comment.
Chris Lattner4b009652007-07-25 00:24:17 +0000939static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
940 Lexer *L) {
941 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
942
943 // Back up off the newline.
944 --CurPtr;
945
946 // If this is a two-character newline sequence, skip the other character.
947 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
948 // \n\n or \r\r -> not escaped newline.
949 if (CurPtr[0] == CurPtr[1])
950 return false;
951 // \n\r or \r\n -> skip the newline.
952 --CurPtr;
953 }
954
955 // If we have horizontal whitespace, skip over it. We allow whitespace
956 // between the slash and newline.
957 bool HasSpace = false;
958 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
959 --CurPtr;
960 HasSpace = true;
961 }
962
963 // If we have a slash, we know this is an escaped newline.
964 if (*CurPtr == '\\') {
965 if (CurPtr[-1] != '*') return false;
966 } else {
967 // It isn't a slash, is it the ?? / trigraph?
968 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
969 CurPtr[-3] != '*')
970 return false;
971
972 // This is the trigraph ending the comment. Emit a stern warning!
973 CurPtr -= 2;
974
975 // If no trigraphs are enabled, warn that we ignored this trigraph and
976 // ignore this * character.
977 if (!L->getFeatures().Trigraphs) {
Chris Lattnerf9c62772008-11-22 02:02:22 +0000978 if (!L->isLexingRawMode())
979 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattner4b009652007-07-25 00:24:17 +0000980 return false;
981 }
Chris Lattnerf9c62772008-11-22 02:02:22 +0000982 if (!L->isLexingRawMode())
983 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner4b009652007-07-25 00:24:17 +0000984 }
985
986 // Warn about having an escaped newline between the */ characters.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000987 if (!L->isLexingRawMode())
988 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Chris Lattner4b009652007-07-25 00:24:17 +0000989
990 // If there was space between the backslash and newline, warn about it.
Chris Lattnerf9c62772008-11-22 02:02:22 +0000991 if (HasSpace && !L->isLexingRawMode())
992 L->Diag(CurPtr, diag::backslash_newline_space);
Chris Lattner4b009652007-07-25 00:24:17 +0000993
994 return true;
995}
996
997#ifdef __SSE2__
998#include <emmintrin.h>
999#elif __ALTIVEC__
1000#include <altivec.h>
1001#undef bool
1002#endif
1003
1004/// SkipBlockComment - We have just read the /* characters from input. Read
1005/// until we find the */ characters that terminate the comment. Note that we
1006/// don't bother decoding trigraphs or escaped newlines in block comments,
1007/// because they cannot cause the comment to end. The only thing that can
1008/// happen is the comment could end with an escaped newline between the */ end
1009/// of comment.
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001010///
1011/// If KeepCommentMode is enabled, this forms a token from the comment and
1012/// returns true.
Chris Lattner4b009652007-07-25 00:24:17 +00001013bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
1014 // Scan one character past where we should, looking for a '/' character. Once
1015 // we find it, check to see if it was preceeded by a *. This common
1016 // optimization helps people who like to put a lot of * characters in their
1017 // comments.
1018
1019 // The first character we get with newlines and trigraphs skipped to handle
1020 // the degenerate /*/ case below correctly if the * has an escaped newline
1021 // after it.
1022 unsigned CharSize;
1023 unsigned char C = getCharAndSize(CurPtr, CharSize);
1024 CurPtr += CharSize;
1025 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerf9c62772008-11-22 02:02:22 +00001026 if (!isLexingRawMode())
Chris Lattnere5eca952008-10-12 01:31:51 +00001027 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattnerd66f4542008-10-12 04:19:49 +00001028 --CurPtr;
1029
1030 // KeepWhitespaceMode should return this broken comment as a token. Since
1031 // it isn't a well formed comment, just return it as an 'unknown' token.
1032 if (isKeepWhitespaceMode()) {
Chris Lattner0344cc72008-10-12 04:51:35 +00001033 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd66f4542008-10-12 04:19:49 +00001034 return true;
1035 }
1036
1037 BufferPtr = CurPtr;
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001038 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001039 }
1040
1041 // Check to see if the first character after the '/*' is another /. If so,
1042 // then this slash does not end the block comment, it is part of it.
1043 if (C == '/')
1044 C = *CurPtr++;
1045
1046 while (1) {
1047 // Skip over all non-interesting characters until we find end of buffer or a
1048 // (probably ending) '/' character.
1049 if (CurPtr + 24 < BufferEnd) {
1050 // While not aligned to a 16-byte boundary.
1051 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1052 C = *CurPtr++;
1053
1054 if (C == '/') goto FoundSlash;
1055
1056#ifdef __SSE2__
1057 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1058 '/', '/', '/', '/', '/', '/', '/', '/');
1059 while (CurPtr+16 <= BufferEnd &&
1060 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1061 CurPtr += 16;
1062#elif __ALTIVEC__
1063 __vector unsigned char Slashes = {
1064 '/', '/', '/', '/', '/', '/', '/', '/',
1065 '/', '/', '/', '/', '/', '/', '/', '/'
1066 };
1067 while (CurPtr+16 <= BufferEnd &&
1068 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1069 CurPtr += 16;
1070#else
1071 // Scan for '/' quickly. Many block comments are very large.
1072 while (CurPtr[0] != '/' &&
1073 CurPtr[1] != '/' &&
1074 CurPtr[2] != '/' &&
1075 CurPtr[3] != '/' &&
1076 CurPtr+4 < BufferEnd) {
1077 CurPtr += 4;
1078 }
1079#endif
1080
1081 // It has to be one of the bytes scanned, increment to it and read one.
1082 C = *CurPtr++;
1083 }
1084
1085 // Loop to scan the remainder.
1086 while (C != '/' && C != '\0')
1087 C = *CurPtr++;
1088
1089 FoundSlash:
1090 if (C == '/') {
1091 if (CurPtr[-2] == '*') // We found the final */. We're done!
1092 break;
1093
1094 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
1095 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
1096 // We found the final */, though it had an escaped newline between the
1097 // * and /. We're done!
1098 break;
1099 }
1100 }
1101 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1102 // If this is a /* inside of the comment, emit a warning. Don't do this
1103 // if this is a /*/, which will end the comment. This misses cases with
1104 // embedded escaped newlines, but oh well.
Chris Lattnerf9c62772008-11-22 02:02:22 +00001105 if (!isLexingRawMode())
1106 Diag(CurPtr-1, diag::warn_nested_block_comment);
Chris Lattner4b009652007-07-25 00:24:17 +00001107 }
1108 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerf9c62772008-11-22 02:02:22 +00001109 if (!isLexingRawMode())
1110 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner4b009652007-07-25 00:24:17 +00001111 // Note: the user probably forgot a */. We could continue immediately
1112 // after the /*, but this would involve lexing a lot of what really is the
1113 // comment, which surely would confuse the parser.
Chris Lattnerd66f4542008-10-12 04:19:49 +00001114 --CurPtr;
1115
1116 // KeepWhitespaceMode should return this broken comment as a token. Since
1117 // it isn't a well formed comment, just return it as an 'unknown' token.
1118 if (isKeepWhitespaceMode()) {
Chris Lattner0344cc72008-10-12 04:51:35 +00001119 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd66f4542008-10-12 04:19:49 +00001120 return true;
1121 }
1122
1123 BufferPtr = CurPtr;
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001124 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001125 }
1126 C = *CurPtr++;
1127 }
1128
1129 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner170adb12008-10-12 03:22:02 +00001130 if (inKeepCommentMode()) {
Chris Lattner0344cc72008-10-12 04:51:35 +00001131 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001132 return true;
Chris Lattner4b009652007-07-25 00:24:17 +00001133 }
1134
1135 // It is common for the tokens immediately after a /**/ comment to be
1136 // whitespace. Instead of going through the big switch, handle it
Chris Lattner867a87b2008-10-12 04:05:48 +00001137 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1138 // have already returned above with the comment as a token.
Chris Lattner4b009652007-07-25 00:24:17 +00001139 if (isHorizontalWhitespace(*CurPtr)) {
1140 Result.setFlag(Token::LeadingSpace);
1141 SkipWhitespace(Result, CurPtr+1);
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001142 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001143 }
1144
1145 // Otherwise, just return so that the next character will be lexed as a token.
1146 BufferPtr = CurPtr;
1147 Result.setFlag(Token::LeadingSpace);
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001148 return false;
Chris Lattner4b009652007-07-25 00:24:17 +00001149}
1150
1151//===----------------------------------------------------------------------===//
1152// Primary Lexing Entry Points
1153//===----------------------------------------------------------------------===//
1154
Chris Lattner4b009652007-07-25 00:24:17 +00001155/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1156/// uninterpreted string. This switches the lexer out of directive mode.
1157std::string Lexer::ReadToEndOfLine() {
1158 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1159 "Must be in a preprocessing directive!");
1160 std::string Result;
1161 Token Tmp;
1162
1163 // CurPtr - Cache BufferPtr in an automatic variable.
1164 const char *CurPtr = BufferPtr;
1165 while (1) {
1166 char Char = getAndAdvanceChar(CurPtr, Tmp);
1167 switch (Char) {
1168 default:
1169 Result += Char;
1170 break;
1171 case 0: // Null.
1172 // Found end of file?
1173 if (CurPtr-1 != BufferEnd) {
1174 // Nope, normal character, continue.
1175 Result += Char;
1176 break;
1177 }
1178 // FALL THROUGH.
1179 case '\r':
1180 case '\n':
1181 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1182 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1183 BufferPtr = CurPtr-1;
1184
1185 // Next, lex the character, which should handle the EOM transition.
1186 Lex(Tmp);
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001187 assert(Tmp.is(tok::eom) && "Unexpected token!");
Chris Lattner4b009652007-07-25 00:24:17 +00001188
1189 // Finally, we're done, return the string we found.
1190 return Result;
1191 }
1192 }
1193}
1194
1195/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1196/// condition, reporting diagnostics and handling other edge cases as required.
1197/// This returns true if Result contains a token, false if PP.Lex should be
1198/// called again.
1199bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
1200 // If we hit the end of the file while parsing a preprocessor directive,
1201 // end the preprocessor directive first. The next token returned will
1202 // then be the end of file.
1203 if (ParsingPreprocessorDirective) {
1204 // Done parsing the "line".
1205 ParsingPreprocessorDirective = false;
Chris Lattner4b009652007-07-25 00:24:17 +00001206 // Update the location of token as well as BufferPtr.
Chris Lattner0344cc72008-10-12 04:51:35 +00001207 FormTokenWithChars(Result, CurPtr, tok::eom);
Chris Lattner4b009652007-07-25 00:24:17 +00001208
1209 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner1c1bed12008-10-12 03:27:19 +00001210 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner4b009652007-07-25 00:24:17 +00001211 return true; // Have a token.
1212 }
1213
1214 // If we are in raw mode, return this event as an EOF token. Let the caller
1215 // that put us in raw mode handle the event.
Chris Lattnerf9c62772008-11-22 02:02:22 +00001216 if (isLexingRawMode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001217 Result.startToken();
1218 BufferPtr = BufferEnd;
Chris Lattner0344cc72008-10-12 04:51:35 +00001219 FormTokenWithChars(Result, BufferEnd, tok::eof);
Chris Lattner4b009652007-07-25 00:24:17 +00001220 return true;
1221 }
1222
1223 // Otherwise, issue diagnostics for unterminated #if and missing newline.
1224
1225 // If we are in a #if directive, emit an error.
1226 while (!ConditionalStack.empty()) {
Chris Lattner8ef6cdc2008-11-22 06:22:39 +00001227 PP->Diag(ConditionalStack.back().IfLoc,
1228 diag::err_pp_unterminated_conditional);
Chris Lattner4b009652007-07-25 00:24:17 +00001229 ConditionalStack.pop_back();
1230 }
1231
Chris Lattner5c337fa2008-04-12 05:54:25 +00001232 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1233 // a pedwarn.
1234 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Mike Stumpf3e739f2009-04-02 02:29:42 +00001235 Diag(BufferEnd, diag::ext_no_newline_eof)
1236 << CodeModificationHint::CreateInsertion(getSourceLocation(BufferEnd),
1237 "\n");
Chris Lattner4b009652007-07-25 00:24:17 +00001238
1239 BufferPtr = CurPtr;
1240
1241 // Finally, let the preprocessor handle this.
Chris Lattner342dccb2007-10-17 20:41:00 +00001242 return PP->HandleEndOfFile(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001243}
1244
1245/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1246/// the specified lexer will return a tok::l_paren token, 0 if it is something
1247/// else and 2 if there are no more tokens in the buffer controlled by the
1248/// lexer.
1249unsigned Lexer::isNextPPTokenLParen() {
1250 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1251
1252 // Switch to 'skipping' mode. This will ensure that we can lex a token
1253 // without emitting diagnostics, disables macro expansion, and will cause EOF
1254 // to return an EOF token instead of popping the include stack.
1255 LexingRawMode = true;
1256
1257 // Save state that can be changed while lexing so that we can restore it.
1258 const char *TmpBufferPtr = BufferPtr;
1259
1260 Token Tok;
1261 Tok.startToken();
1262 LexTokenInternal(Tok);
1263
1264 // Restore state that may have changed.
1265 BufferPtr = TmpBufferPtr;
1266
1267 // Restore the lexer back to non-skipping mode.
1268 LexingRawMode = false;
1269
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001270 if (Tok.is(tok::eof))
Chris Lattner4b009652007-07-25 00:24:17 +00001271 return 2;
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001272 return Tok.is(tok::l_paren);
Chris Lattner4b009652007-07-25 00:24:17 +00001273}
1274
1275
1276/// LexTokenInternal - This implements a simple C family lexer. It is an
1277/// extremely performance critical piece of code. This assumes that the buffer
1278/// has a null character at the end of the file. Return true if an error
1279/// occurred and compilation should terminate, false if normal. This returns a
1280/// preprocessing token, not a normal token, as such, it is an internal
1281/// interface. It assumes that the Flags of result have been cleared before
1282/// calling this.
1283void Lexer::LexTokenInternal(Token &Result) {
1284LexNextToken:
1285 // New token, can't need cleaning yet.
1286 Result.clearFlag(Token::NeedsCleaning);
1287 Result.setIdentifierInfo(0);
1288
1289 // CurPtr - Cache BufferPtr in an automatic variable.
1290 const char *CurPtr = BufferPtr;
1291
1292 // Small amounts of horizontal whitespace is very common between tokens.
1293 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1294 ++CurPtr;
1295 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1296 ++CurPtr;
Chris Lattner867a87b2008-10-12 04:05:48 +00001297
1298 // If we are keeping whitespace and other tokens, just return what we just
1299 // skipped. The next lexer invocation will return the token after the
1300 // whitespace.
1301 if (isKeepWhitespaceMode()) {
Chris Lattner0344cc72008-10-12 04:51:35 +00001302 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner867a87b2008-10-12 04:05:48 +00001303 return;
1304 }
1305
Chris Lattner4b009652007-07-25 00:24:17 +00001306 BufferPtr = CurPtr;
1307 Result.setFlag(Token::LeadingSpace);
1308 }
1309
1310 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1311
1312 // Read a character, advancing over it.
1313 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001314 tok::TokenKind Kind;
1315
Chris Lattner4b009652007-07-25 00:24:17 +00001316 switch (Char) {
1317 case 0: // Null.
1318 // Found end of file?
1319 if (CurPtr-1 == BufferEnd) {
1320 // Read the PP instance variable into an automatic variable, because
1321 // LexEndOfFile will often delete 'this'.
Chris Lattner342dccb2007-10-17 20:41:00 +00001322 Preprocessor *PPCache = PP;
Chris Lattner4b009652007-07-25 00:24:17 +00001323 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1324 return; // Got a token to return.
Chris Lattner342dccb2007-10-17 20:41:00 +00001325 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1326 return PPCache->Lex(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001327 }
1328
Chris Lattnerf9c62772008-11-22 02:02:22 +00001329 if (!isLexingRawMode())
1330 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner4b009652007-07-25 00:24:17 +00001331 Result.setFlag(Token::LeadingSpace);
Chris Lattner867a87b2008-10-12 04:05:48 +00001332 if (SkipWhitespace(Result, CurPtr))
1333 return; // KeepWhitespaceMode
1334
Chris Lattner4b009652007-07-25 00:24:17 +00001335 goto LexNextToken; // GCC isn't tail call eliminating.
1336 case '\n':
1337 case '\r':
1338 // If we are inside a preprocessor directive and we see the end of line,
1339 // we know we are done with the directive, so return an EOM token.
1340 if (ParsingPreprocessorDirective) {
1341 // Done parsing the "line".
1342 ParsingPreprocessorDirective = false;
1343
1344 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner1c1bed12008-10-12 03:27:19 +00001345 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner4b009652007-07-25 00:24:17 +00001346
1347 // Since we consumed a newline, we are back at the start of a line.
1348 IsAtStartOfLine = true;
1349
Chris Lattner0344cc72008-10-12 04:51:35 +00001350 Kind = tok::eom;
Chris Lattner4b009652007-07-25 00:24:17 +00001351 break;
1352 }
1353 // The returned token is at the start of the line.
1354 Result.setFlag(Token::StartOfLine);
1355 // No leading whitespace seen so far.
1356 Result.clearFlag(Token::LeadingSpace);
Chris Lattner867a87b2008-10-12 04:05:48 +00001357
1358 if (SkipWhitespace(Result, CurPtr))
1359 return; // KeepWhitespaceMode
Chris Lattner4b009652007-07-25 00:24:17 +00001360 goto LexNextToken; // GCC isn't tail call eliminating.
1361 case ' ':
1362 case '\t':
1363 case '\f':
1364 case '\v':
1365 SkipHorizontalWhitespace:
1366 Result.setFlag(Token::LeadingSpace);
Chris Lattner867a87b2008-10-12 04:05:48 +00001367 if (SkipWhitespace(Result, CurPtr))
1368 return; // KeepWhitespaceMode
Chris Lattner4b009652007-07-25 00:24:17 +00001369
1370 SkipIgnoredUnits:
1371 CurPtr = BufferPtr;
1372
1373 // If the next token is obviously a // or /* */ comment, skip it efficiently
1374 // too (without going through the big switch stmt).
Chris Lattner43e455d2009-01-16 22:39:25 +00001375 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1376 Features.BCPLComment) {
Chris Lattner4b009652007-07-25 00:24:17 +00001377 SkipBCPLComment(Result, CurPtr+2);
1378 goto SkipIgnoredUnits;
Chris Lattner170adb12008-10-12 03:22:02 +00001379 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner4b009652007-07-25 00:24:17 +00001380 SkipBlockComment(Result, CurPtr+2);
1381 goto SkipIgnoredUnits;
1382 } else if (isHorizontalWhitespace(*CurPtr)) {
1383 goto SkipHorizontalWhitespace;
1384 }
1385 goto LexNextToken; // GCC isn't tail call eliminating.
1386
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001387 // C99 6.4.4.1: Integer Constants.
1388 // C99 6.4.4.2: Floating Constants.
1389 case '0': case '1': case '2': case '3': case '4':
1390 case '5': case '6': case '7': case '8': case '9':
1391 // Notify MIOpt that we read a non-whitespace/non-comment token.
1392 MIOpt.ReadToken();
1393 return LexNumericConstant(Result, CurPtr);
1394
1395 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Chris Lattner4b009652007-07-25 00:24:17 +00001396 // Notify MIOpt that we read a non-whitespace/non-comment token.
1397 MIOpt.ReadToken();
1398 Char = getCharAndSize(CurPtr, SizeTmp);
1399
1400 // Wide string literal.
1401 if (Char == '"')
1402 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1403 true);
1404
1405 // Wide character constant.
1406 if (Char == '\'')
1407 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1408 // FALL THROUGH, treating L like the start of an identifier.
1409
1410 // C99 6.4.2: Identifiers.
1411 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1412 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1413 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1414 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1415 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1416 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1417 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1418 case 'v': case 'w': case 'x': case 'y': case 'z':
1419 case '_':
1420 // Notify MIOpt that we read a non-whitespace/non-comment token.
1421 MIOpt.ReadToken();
1422 return LexIdentifier(Result, CurPtr);
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001423
1424 case '$': // $ in identifiers.
1425 if (Features.DollarIdents) {
Chris Lattnerf9c62772008-11-22 02:02:22 +00001426 if (!isLexingRawMode())
1427 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001428 // Notify MIOpt that we read a non-whitespace/non-comment token.
1429 MIOpt.ReadToken();
1430 return LexIdentifier(Result, CurPtr);
1431 }
Chris Lattner4b009652007-07-25 00:24:17 +00001432
Chris Lattner0344cc72008-10-12 04:51:35 +00001433 Kind = tok::unknown;
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001434 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001435
1436 // C99 6.4.4: Character Constants.
1437 case '\'':
1438 // Notify MIOpt that we read a non-whitespace/non-comment token.
1439 MIOpt.ReadToken();
1440 return LexCharConstant(Result, CurPtr);
1441
1442 // C99 6.4.5: String Literals.
1443 case '"':
1444 // Notify MIOpt that we read a non-whitespace/non-comment token.
1445 MIOpt.ReadToken();
1446 return LexStringLiteral(Result, CurPtr, false);
1447
1448 // C99 6.4.6: Punctuators.
1449 case '?':
Chris Lattner0344cc72008-10-12 04:51:35 +00001450 Kind = tok::question;
Chris Lattner4b009652007-07-25 00:24:17 +00001451 break;
1452 case '[':
Chris Lattner0344cc72008-10-12 04:51:35 +00001453 Kind = tok::l_square;
Chris Lattner4b009652007-07-25 00:24:17 +00001454 break;
1455 case ']':
Chris Lattner0344cc72008-10-12 04:51:35 +00001456 Kind = tok::r_square;
Chris Lattner4b009652007-07-25 00:24:17 +00001457 break;
1458 case '(':
Chris Lattner0344cc72008-10-12 04:51:35 +00001459 Kind = tok::l_paren;
Chris Lattner4b009652007-07-25 00:24:17 +00001460 break;
1461 case ')':
Chris Lattner0344cc72008-10-12 04:51:35 +00001462 Kind = tok::r_paren;
Chris Lattner4b009652007-07-25 00:24:17 +00001463 break;
1464 case '{':
Chris Lattner0344cc72008-10-12 04:51:35 +00001465 Kind = tok::l_brace;
Chris Lattner4b009652007-07-25 00:24:17 +00001466 break;
1467 case '}':
Chris Lattner0344cc72008-10-12 04:51:35 +00001468 Kind = tok::r_brace;
Chris Lattner4b009652007-07-25 00:24:17 +00001469 break;
1470 case '.':
1471 Char = getCharAndSize(CurPtr, SizeTmp);
1472 if (Char >= '0' && Char <= '9') {
1473 // Notify MIOpt that we read a non-whitespace/non-comment token.
1474 MIOpt.ReadToken();
1475
1476 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1477 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001478 Kind = tok::periodstar;
Chris Lattner4b009652007-07-25 00:24:17 +00001479 CurPtr += SizeTmp;
1480 } else if (Char == '.' &&
1481 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001482 Kind = tok::ellipsis;
Chris Lattner4b009652007-07-25 00:24:17 +00001483 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1484 SizeTmp2, Result);
1485 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001486 Kind = tok::period;
Chris Lattner4b009652007-07-25 00:24:17 +00001487 }
1488 break;
1489 case '&':
1490 Char = getCharAndSize(CurPtr, SizeTmp);
1491 if (Char == '&') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001492 Kind = tok::ampamp;
Chris Lattner4b009652007-07-25 00:24:17 +00001493 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1494 } else if (Char == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001495 Kind = tok::ampequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001496 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1497 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001498 Kind = tok::amp;
Chris Lattner4b009652007-07-25 00:24:17 +00001499 }
1500 break;
1501 case '*':
1502 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001503 Kind = tok::starequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001504 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1505 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001506 Kind = tok::star;
Chris Lattner4b009652007-07-25 00:24:17 +00001507 }
1508 break;
1509 case '+':
1510 Char = getCharAndSize(CurPtr, SizeTmp);
1511 if (Char == '+') {
Chris Lattner4b009652007-07-25 00:24:17 +00001512 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001513 Kind = tok::plusplus;
Chris Lattner4b009652007-07-25 00:24:17 +00001514 } else if (Char == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001515 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001516 Kind = tok::plusequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001517 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001518 Kind = tok::plus;
Chris Lattner4b009652007-07-25 00:24:17 +00001519 }
1520 break;
1521 case '-':
1522 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattner0344cc72008-10-12 04:51:35 +00001523 if (Char == '-') { // --
Chris Lattner4b009652007-07-25 00:24:17 +00001524 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001525 Kind = tok::minusminus;
Chris Lattner4b009652007-07-25 00:24:17 +00001526 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattner0344cc72008-10-12 04:51:35 +00001527 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Chris Lattner4b009652007-07-25 00:24:17 +00001528 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1529 SizeTmp2, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001530 Kind = tok::arrowstar;
1531 } else if (Char == '>') { // ->
Chris Lattner4b009652007-07-25 00:24:17 +00001532 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001533 Kind = tok::arrow;
1534 } else if (Char == '=') { // -=
Chris Lattner4b009652007-07-25 00:24:17 +00001535 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001536 Kind = tok::minusequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001537 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001538 Kind = tok::minus;
Chris Lattner4b009652007-07-25 00:24:17 +00001539 }
1540 break;
1541 case '~':
Chris Lattner0344cc72008-10-12 04:51:35 +00001542 Kind = tok::tilde;
Chris Lattner4b009652007-07-25 00:24:17 +00001543 break;
1544 case '!':
1545 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001546 Kind = tok::exclaimequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001547 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1548 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001549 Kind = tok::exclaim;
Chris Lattner4b009652007-07-25 00:24:17 +00001550 }
1551 break;
1552 case '/':
1553 // 6.4.9: Comments
1554 Char = getCharAndSize(CurPtr, SizeTmp);
1555 if (Char == '/') { // BCPL comment.
Chris Lattner43e455d2009-01-16 22:39:25 +00001556 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
1557 // want to lex this as a comment. There is one problem with this though,
1558 // that in one particular corner case, this can change the behavior of the
1559 // resultant program. For example, In "foo //**/ bar", C89 would lex
1560 // this as "foo / bar" and langauges with BCPL comments would lex it as
1561 // "foo". Check to see if the character after the second slash is a '*'.
1562 // If so, we will lex that as a "/" instead of the start of a comment.
1563 if (Features.BCPLComment ||
1564 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
1565 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1566 return; // KeepCommentMode
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001567
Chris Lattner43e455d2009-01-16 22:39:25 +00001568 // It is common for the tokens immediately after a // comment to be
1569 // whitespace (indentation for the next line). Instead of going through
1570 // the big switch, handle it efficiently now.
1571 goto SkipIgnoredUnits;
1572 }
1573 }
1574
1575 if (Char == '*') { // /**/ comment.
Chris Lattner4b009652007-07-25 00:24:17 +00001576 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattnerf03b00c2008-10-12 04:15:42 +00001577 return; // KeepCommentMode
1578 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner43e455d2009-01-16 22:39:25 +00001579 }
1580
1581 if (Char == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001582 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001583 Kind = tok::slashequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001584 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001585 Kind = tok::slash;
Chris Lattner4b009652007-07-25 00:24:17 +00001586 }
1587 break;
1588 case '%':
1589 Char = getCharAndSize(CurPtr, SizeTmp);
1590 if (Char == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001591 Kind = tok::percentequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001592 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1593 } else if (Features.Digraphs && Char == '>') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001594 Kind = tok::r_brace; // '%>' -> '}'
Chris Lattner4b009652007-07-25 00:24:17 +00001595 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1596 } else if (Features.Digraphs && Char == ':') {
1597 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1598 Char = getCharAndSize(CurPtr, SizeTmp);
1599 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001600 Kind = tok::hashhash; // '%:%:' -> '##'
Chris Lattner4b009652007-07-25 00:24:17 +00001601 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1602 SizeTmp2, Result);
1603 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner4b009652007-07-25 00:24:17 +00001604 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerf9c62772008-11-22 02:02:22 +00001605 if (!isLexingRawMode())
1606 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner0344cc72008-10-12 04:51:35 +00001607 Kind = tok::hashat;
Chris Lattner5bf64722009-03-18 20:58:27 +00001608 } else { // '%:' -> '#'
Chris Lattner4b009652007-07-25 00:24:17 +00001609 // We parsed a # character. If this occurs at the start of the line,
1610 // it's actually the start of a preprocessing directive. Callback to
1611 // the preprocessor to handle it.
1612 // FIXME: -fpreprocessed mode??
1613 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner5bf64722009-03-18 20:58:27 +00001614 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner342dccb2007-10-17 20:41:00 +00001615 PP->HandleDirective(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001616
1617 // As an optimization, if the preprocessor didn't switch lexers, tail
1618 // recurse.
Chris Lattner342dccb2007-10-17 20:41:00 +00001619 if (PP->isCurrentLexer(this)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001620 // Start a new token. If this is a #include or something, the PP may
1621 // want us starting at the beginning of the line again. If so, set
1622 // the StartOfLine flag.
1623 if (IsAtStartOfLine) {
1624 Result.setFlag(Token::StartOfLine);
1625 IsAtStartOfLine = false;
1626 }
1627 goto LexNextToken; // GCC isn't tail call eliminating.
1628 }
1629
Chris Lattner342dccb2007-10-17 20:41:00 +00001630 return PP->Lex(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001631 }
Chris Lattner5bf64722009-03-18 20:58:27 +00001632
1633 Kind = tok::hash;
Chris Lattner4b009652007-07-25 00:24:17 +00001634 }
1635 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001636 Kind = tok::percent;
Chris Lattner4b009652007-07-25 00:24:17 +00001637 }
1638 break;
1639 case '<':
1640 Char = getCharAndSize(CurPtr, SizeTmp);
1641 if (ParsingFilename) {
1642 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1643 } else if (Char == '<' &&
1644 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001645 Kind = tok::lesslessequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001646 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1647 SizeTmp2, Result);
1648 } else if (Char == '<') {
Chris Lattner4b009652007-07-25 00:24:17 +00001649 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001650 Kind = tok::lessless;
Chris Lattner4b009652007-07-25 00:24:17 +00001651 } else if (Char == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001652 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001653 Kind = tok::lessequal;
1654 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Chris Lattner4b009652007-07-25 00:24:17 +00001655 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001656 Kind = tok::l_square;
1657 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Chris Lattner4b009652007-07-25 00:24:17 +00001658 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001659 Kind = tok::l_brace;
Chris Lattner4b009652007-07-25 00:24:17 +00001660 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001661 Kind = tok::less;
Chris Lattner4b009652007-07-25 00:24:17 +00001662 }
1663 break;
1664 case '>':
1665 Char = getCharAndSize(CurPtr, SizeTmp);
1666 if (Char == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001667 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001668 Kind = tok::greaterequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001669 } else if (Char == '>' &&
1670 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001671 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1672 SizeTmp2, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001673 Kind = tok::greatergreaterequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001674 } else if (Char == '>') {
Chris Lattner4b009652007-07-25 00:24:17 +00001675 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001676 Kind = tok::greatergreater;
Chris Lattner4b009652007-07-25 00:24:17 +00001677 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001678 Kind = tok::greater;
Chris Lattner4b009652007-07-25 00:24:17 +00001679 }
1680 break;
1681 case '^':
1682 Char = getCharAndSize(CurPtr, SizeTmp);
1683 if (Char == '=') {
Chris Lattner4b009652007-07-25 00:24:17 +00001684 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0344cc72008-10-12 04:51:35 +00001685 Kind = tok::caretequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001686 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001687 Kind = tok::caret;
Chris Lattner4b009652007-07-25 00:24:17 +00001688 }
1689 break;
1690 case '|':
1691 Char = getCharAndSize(CurPtr, SizeTmp);
1692 if (Char == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001693 Kind = tok::pipeequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001694 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1695 } else if (Char == '|') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001696 Kind = tok::pipepipe;
Chris Lattner4b009652007-07-25 00:24:17 +00001697 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1698 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001699 Kind = tok::pipe;
Chris Lattner4b009652007-07-25 00:24:17 +00001700 }
1701 break;
1702 case ':':
1703 Char = getCharAndSize(CurPtr, SizeTmp);
1704 if (Features.Digraphs && Char == '>') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001705 Kind = tok::r_square; // ':>' -> ']'
Chris Lattner4b009652007-07-25 00:24:17 +00001706 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1707 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001708 Kind = tok::coloncolon;
Chris Lattner4b009652007-07-25 00:24:17 +00001709 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1710 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001711 Kind = tok::colon;
Chris Lattner4b009652007-07-25 00:24:17 +00001712 }
1713 break;
1714 case ';':
Chris Lattner0344cc72008-10-12 04:51:35 +00001715 Kind = tok::semi;
Chris Lattner4b009652007-07-25 00:24:17 +00001716 break;
1717 case '=':
1718 Char = getCharAndSize(CurPtr, SizeTmp);
1719 if (Char == '=') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001720 Kind = tok::equalequal;
Chris Lattner4b009652007-07-25 00:24:17 +00001721 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1722 } else {
Chris Lattner0344cc72008-10-12 04:51:35 +00001723 Kind = tok::equal;
Chris Lattner4b009652007-07-25 00:24:17 +00001724 }
1725 break;
1726 case ',':
Chris Lattner0344cc72008-10-12 04:51:35 +00001727 Kind = tok::comma;
Chris Lattner4b009652007-07-25 00:24:17 +00001728 break;
1729 case '#':
1730 Char = getCharAndSize(CurPtr, SizeTmp);
1731 if (Char == '#') {
Chris Lattner0344cc72008-10-12 04:51:35 +00001732 Kind = tok::hashhash;
Chris Lattner4b009652007-07-25 00:24:17 +00001733 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1734 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner0344cc72008-10-12 04:51:35 +00001735 Kind = tok::hashat;
Chris Lattnerf9c62772008-11-22 02:02:22 +00001736 if (!isLexingRawMode())
1737 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner4b009652007-07-25 00:24:17 +00001738 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1739 } else {
Chris Lattner4b009652007-07-25 00:24:17 +00001740 // We parsed a # character. If this occurs at the start of the line,
1741 // it's actually the start of a preprocessing directive. Callback to
1742 // the preprocessor to handle it.
1743 // FIXME: -fpreprocessed mode??
1744 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner5bf64722009-03-18 20:58:27 +00001745 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner342dccb2007-10-17 20:41:00 +00001746 PP->HandleDirective(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001747
1748 // As an optimization, if the preprocessor didn't switch lexers, tail
1749 // recurse.
Chris Lattner342dccb2007-10-17 20:41:00 +00001750 if (PP->isCurrentLexer(this)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001751 // Start a new token. If this is a #include or something, the PP may
1752 // want us starting at the beginning of the line again. If so, set
1753 // the StartOfLine flag.
1754 if (IsAtStartOfLine) {
1755 Result.setFlag(Token::StartOfLine);
1756 IsAtStartOfLine = false;
1757 }
1758 goto LexNextToken; // GCC isn't tail call eliminating.
1759 }
Chris Lattner342dccb2007-10-17 20:41:00 +00001760 return PP->Lex(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001761 }
Chris Lattner5bf64722009-03-18 20:58:27 +00001762
1763 Kind = tok::hash;
Chris Lattner4b009652007-07-25 00:24:17 +00001764 }
1765 break;
1766
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001767 case '@':
1768 // Objective C support.
1769 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattner0344cc72008-10-12 04:51:35 +00001770 Kind = tok::at;
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001771 else
Chris Lattner0344cc72008-10-12 04:51:35 +00001772 Kind = tok::unknown;
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001773 break;
1774
Chris Lattner4b009652007-07-25 00:24:17 +00001775 case '\\':
1776 // FIXME: UCN's.
1777 // FALL THROUGH.
1778 default:
Chris Lattner0344cc72008-10-12 04:51:35 +00001779 Kind = tok::unknown;
Chris Lattner4b009652007-07-25 00:24:17 +00001780 break;
1781 }
1782
1783 // Notify MIOpt that we read a non-whitespace/non-comment token.
1784 MIOpt.ReadToken();
1785
1786 // Update the location of token as well as BufferPtr.
Chris Lattner0344cc72008-10-12 04:51:35 +00001787 FormTokenWithChars(Result, CurPtr, Kind);
Chris Lattner4b009652007-07-25 00:24:17 +00001788}