blob: efbd84c879e2a5a32b227c2e8ef6ccc25e838144 [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"
Chris Lattner500d3292009-01-29 05:15:15 +000029#include "clang/Lex/LexDiagnostic.h"
Chris Lattner9dc1f532007-07-20 16:37:10 +000030#include "clang/Basic/SourceManager.h"
Chris Lattner409a0362007-07-22 18:38:25 +000031#include "llvm/Support/Compiler.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000032#include "llvm/Support/MemoryBuffer.h"
33#include <cctype>
34using namespace clang;
35
36static void InitCharacterInfo();
37
Chris Lattnerdbf388b2007-10-07 08:47:24 +000038//===----------------------------------------------------------------------===//
39// Token Class Implementation
40//===----------------------------------------------------------------------===//
41
42/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
43bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
Douglas Gregorbec1c9d2008-12-01 21:46:47 +000044 if (IdentifierInfo *II = getIdentifierInfo())
45 return II->getObjCKeywordID() == objcKey;
46 return false;
Chris Lattnerdbf388b2007-10-07 08:47:24 +000047}
48
49/// getObjCKeywordID - Return the ObjC keyword kind.
50tok::ObjCKeywordKind Token::getObjCKeywordID() const {
51 IdentifierInfo *specId = getIdentifierInfo();
52 return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
53}
54
Chris Lattner53702cd2007-12-13 01:59:49 +000055
Chris Lattnerdbf388b2007-10-07 08:47:24 +000056//===----------------------------------------------------------------------===//
57// Lexer Class Implementation
58//===----------------------------------------------------------------------===//
59
Chris Lattner22d91ca2009-01-17 06:55:17 +000060void Lexer::InitLexer(const char *BufStart, const char *BufPtr,
61 const char *BufEnd) {
62 InitCharacterInfo();
63
64 BufferStart = BufStart;
65 BufferPtr = BufPtr;
66 BufferEnd = BufEnd;
67
68 assert(BufEnd[0] == 0 &&
69 "We assume that the input buffer has a null character at the end"
70 " to simplify lexing!");
71
72 Is_PragmaLexer = false;
73
74 // Start of the file is a start of line.
75 IsAtStartOfLine = true;
76
77 // We are not after parsing a #.
78 ParsingPreprocessorDirective = false;
79
80 // We are not after parsing #include.
81 ParsingFilename = false;
82
83 // We are not in raw mode. Raw mode disables diagnostics and interpretation
84 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
85 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
86 // or otherwise skipping over tokens.
87 LexingRawMode = false;
88
89 // Default to not keeping comments.
90 ExtendedTokenMode = 0;
91}
92
Chris Lattner0770dab2009-01-17 07:56:59 +000093/// Lexer constructor - Create a new lexer object for the specified buffer
94/// with the specified preprocessor managing the lexing process. This lexer
95/// assumes that the associated file buffer and Preprocessor objects will
96/// outlive it, so it doesn't take ownership of either of them.
Chris Lattner88d3ac12009-01-17 08:03:42 +000097Lexer::Lexer(FileID FID, Preprocessor &PP)
98 : PreprocessorLexer(&PP, FID),
99 FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
100 Features(PP.getLangOptions()) {
Chris Lattner0770dab2009-01-17 07:56:59 +0000101
Chris Lattner88d3ac12009-01-17 08:03:42 +0000102 const llvm::MemoryBuffer *InputFile = PP.getSourceManager().getBuffer(FID);
Chris Lattner0770dab2009-01-17 07:56:59 +0000103
104 InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
105 InputFile->getBufferEnd());
106
107 // Default to keeping comments if the preprocessor wants them.
108 SetCommentRetentionState(PP.getCommentRetentionState());
109}
Chris Lattnerdbf388b2007-10-07 08:47:24 +0000110
Chris Lattner168ae2d2007-10-17 20:41:00 +0000111/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner590f0cc2008-10-12 01:15:46 +0000112/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
113/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000114Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattnerde96c0f2009-01-17 07:42:27 +0000115 const char *BufStart, const char *BufPtr, const char *BufEnd)
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000116 : FileLoc(fileloc), Features(features) {
Chris Lattner22d91ca2009-01-17 06:55:17 +0000117
Chris Lattner22d91ca2009-01-17 06:55:17 +0000118 InitLexer(BufStart, BufPtr, BufEnd);
Chris Lattner168ae2d2007-10-17 20:41:00 +0000119
120 // We *are* in raw mode.
121 LexingRawMode = true;
Chris Lattner168ae2d2007-10-17 20:41:00 +0000122}
123
Chris Lattner025c3a62009-01-17 07:35:14 +0000124/// Lexer constructor - Create a new raw lexer object. This object is only
125/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
126/// range will outlive it, so it doesn't take ownership of it.
127Lexer::Lexer(FileID FID, const SourceManager &SM, const LangOptions &features)
128 : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
129 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
130
131 InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
132 FromFile->getBufferEnd());
133
134 // We *are* in raw mode.
135 LexingRawMode = true;
136}
137
Chris Lattner42e00d12009-01-17 08:27:52 +0000138/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
139/// _Pragma expansion. This has a variety of magic semantics that this method
140/// sets up. It returns a new'd Lexer that must be delete'd when done.
141///
142/// On entrance to this routine, TokStartLoc is a macro location which has a
143/// spelling loc that indicates the bytes to be lexed for the token and an
144/// instantiation location that indicates where all lexed tokens should be
145/// "expanded from".
146///
147/// FIXME: It would really be nice to make _Pragma just be a wrapper around a
148/// normal lexer that remaps tokens as they fly by. This would require making
149/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer
150/// interface that could handle this stuff. This would pull GetMappedTokenLoc
151/// out of the critical path of the lexer!
152///
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000153Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000154 SourceLocation InstantiationLocStart,
155 SourceLocation InstantiationLocEnd,
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000156 unsigned TokLen, Preprocessor &PP) {
Chris Lattner42e00d12009-01-17 08:27:52 +0000157 SourceManager &SM = PP.getSourceManager();
Chris Lattner42e00d12009-01-17 08:27:52 +0000158
159 // Create the lexer as if we were going to lex the file normally.
Chris Lattnera11d6172009-01-19 07:46:45 +0000160 FileID SpellingFID = SM.getFileID(SpellingLoc);
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000161 Lexer *L = new Lexer(SpellingFID, PP);
Chris Lattner42e00d12009-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;
170
171 // Set the SourceLocation with the remapping information. This ensures that
172 // GetMappedTokenLoc will remap the tokens as they are lexed.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000173 L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
Chris Lattnere7fb4842009-02-15 20:52:18 +0000174 InstantiationLocStart,
175 InstantiationLocEnd, TokLen);
Chris Lattner42e00d12009-01-17 08:27:52 +0000176
177 // Ensure that the lexer thinks it is inside a directive, so that end \n will
178 // return an EOM token.
179 L->ParsingPreprocessorDirective = true;
180
181 // This lexer really is for _Pragma.
182 L->Is_PragmaLexer = true;
183 return L;
184}
185
Chris Lattner168ae2d2007-10-17 20:41:00 +0000186
Reid Spencer5f016e22007-07-11 17:01:13 +0000187/// Stringify - Convert the specified string into a C string, with surrounding
188/// ""'s, and with escaped \ and " characters.
189std::string Lexer::Stringify(const std::string &Str, bool Charify) {
190 std::string Result = Str;
191 char Quote = Charify ? '\'' : '"';
192 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
193 if (Result[i] == '\\' || Result[i] == Quote) {
194 Result.insert(Result.begin()+i, '\\');
195 ++i; ++e;
196 }
197 }
198 return Result;
199}
200
Chris Lattnerd8e30832007-07-24 06:57:14 +0000201/// Stringify - Convert the specified string into a C string by escaping '\'
202/// and " characters. This does not add surrounding ""'s to the string.
203void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
204 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
205 if (Str[i] == '\\' || Str[i] == '"') {
206 Str.insert(Str.begin()+i, '\\');
207 ++i; ++e;
208 }
209 }
210}
211
Reid Spencer5f016e22007-07-11 17:01:13 +0000212
Chris Lattner9a611942007-10-17 21:18:47 +0000213/// MeasureTokenLength - Relex the token at the specified location and return
214/// its length in bytes in the input file. If the token needs cleaning (e.g.
215/// includes a trigraph or an escaped newline) then this count includes bytes
216/// that are part of that.
217unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
218 const SourceManager &SM) {
Chris Lattner9a611942007-10-17 21:18:47 +0000219 // TODO: this could be special cased for common tokens like identifiers, ')',
220 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
221 // all obviously single-char tokens. This could use
222 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
223 // something.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000224
225 // If this comes from a macro expansion, we really do want the macro name, not
226 // the token this macro expanded to.
Chris Lattner363fdc22009-01-26 22:24:27 +0000227 Loc = SM.getInstantiationLoc(Loc);
228 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
Chris Lattner83503942009-01-17 08:30:10 +0000229 std::pair<const char *,const char *> Buffer = SM.getBufferData(LocInfo.first);
230 const char *StrData = Buffer.first+LocInfo.second;
231
Chris Lattner9a611942007-10-17 21:18:47 +0000232 // Create a langops struct and enable trigraphs. This is sufficient for
233 // measuring tokens.
234 LangOptions LangOpts;
235 LangOpts.Trigraphs = true;
236
237 // Create a lexer starting at the beginning of this token.
Chris Lattnerde96c0f2009-01-17 07:42:27 +0000238 Lexer TheLexer(Loc, LangOpts, Buffer.first, StrData, Buffer.second);
Chris Lattner9a611942007-10-17 21:18:47 +0000239 Token TheTok;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000240 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner9a611942007-10-17 21:18:47 +0000241 return TheTok.getLength();
242}
243
Reid Spencer5f016e22007-07-11 17:01:13 +0000244//===----------------------------------------------------------------------===//
245// Character information.
246//===----------------------------------------------------------------------===//
247
248static unsigned char CharInfo[256];
249
250enum {
251 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
252 CHAR_VERT_WS = 0x02, // '\r', '\n'
253 CHAR_LETTER = 0x04, // a-z,A-Z
254 CHAR_NUMBER = 0x08, // 0-9
255 CHAR_UNDER = 0x10, // _
256 CHAR_PERIOD = 0x20 // .
257};
258
259static void InitCharacterInfo() {
260 static bool isInited = false;
261 if (isInited) return;
262 isInited = true;
263
264 // Intiialize the CharInfo table.
265 // TODO: statically initialize this.
266 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
267 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
268 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
269
270 CharInfo[(int)'_'] = CHAR_UNDER;
271 CharInfo[(int)'.'] = CHAR_PERIOD;
272 for (unsigned i = 'a'; i <= 'z'; ++i)
273 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
274 for (unsigned i = '0'; i <= '9'; ++i)
275 CharInfo[i] = CHAR_NUMBER;
276}
277
278/// isIdentifierBody - Return true if this is the body character of an
279/// identifier, which is [a-zA-Z0-9_].
280static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000281 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000282}
283
284/// isHorizontalWhitespace - Return true if this character is horizontal
285/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
286static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000287 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000288}
289
290/// isWhitespace - Return true if this character is horizontal or vertical
291/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
292/// for '\0'.
293static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000294 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000295}
296
297/// isNumberBody - Return true if this is the body character of an
298/// preprocessing number, which is [a-zA-Z0-9_.].
299static inline bool isNumberBody(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000300 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
301 true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000302}
303
304
305//===----------------------------------------------------------------------===//
306// Diagnostics forwarding code.
307//===----------------------------------------------------------------------===//
308
Chris Lattner409a0362007-07-22 18:38:25 +0000309/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
310/// lexer buffer was all instantiated at a single point, perform the mapping.
311/// This is currently only used for _Pragma implementation, so it is the slow
312/// path of the hot getSourceLocation method. Do not allow it to be inlined.
313static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
314 SourceLocation FileLoc,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000315 unsigned CharNo,
316 unsigned TokLen) DISABLE_INLINE;
Chris Lattner409a0362007-07-22 18:38:25 +0000317static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
318 SourceLocation FileLoc,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000319 unsigned CharNo, unsigned TokLen) {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000320 assert(FileLoc.isMacroID() && "Must be an instantiation");
321
Chris Lattner409a0362007-07-22 18:38:25 +0000322 // Otherwise, we're lexing "mapped tokens". This is used for things like
323 // _Pragma handling. Combine the instantiation location of FileLoc with the
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000324 // spelling location.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000325 SourceManager &SM = PP.getSourceManager();
Chris Lattner409a0362007-07-22 18:38:25 +0000326
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000327 // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000328 // characters come from spelling(FileLoc)+Offset.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000329 SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000330 SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
Chris Lattnere7fb4842009-02-15 20:52:18 +0000331
332 // Figure out the expansion loc range, which is the range covered by the
333 // original _Pragma(...) sequence.
334 std::pair<SourceLocation,SourceLocation> II =
335 SM.getImmediateInstantiationRange(FileLoc);
336
337 return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
Chris Lattner409a0362007-07-22 18:38:25 +0000338}
339
Reid Spencer5f016e22007-07-11 17:01:13 +0000340/// getSourceLocation - Return a source location identifier for the specified
341/// offset in the current file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000342SourceLocation Lexer::getSourceLocation(const char *Loc,
343 unsigned TokLen) const {
Chris Lattner448cec42007-07-22 18:44:36 +0000344 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 "Location out of range for this buffer!");
Chris Lattner9dc1f532007-07-20 16:37:10 +0000346
347 // In the normal case, we're just lexing from a simple file buffer, return
348 // the file id from FileLoc with the offset specified.
Chris Lattner448cec42007-07-22 18:44:36 +0000349 unsigned CharNo = Loc-BufferStart;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000350 if (FileLoc.isFileID())
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000351 return FileLoc.getFileLocWithOffset(CharNo);
Chris Lattner9dc1f532007-07-20 16:37:10 +0000352
Chris Lattner2b2453a2009-01-17 06:22:33 +0000353 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
354 // tokens are lexed from where the _Pragma was defined.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000355 assert(PP && "This doesn't work on raw lexers");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000356 return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
Reid Spencer5f016e22007-07-11 17:01:13 +0000357}
358
Reid Spencer5f016e22007-07-11 17:01:13 +0000359/// Diag - Forwarding function for diagnostics. This translate a source
360/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000361DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner3692b092008-11-18 07:59:24 +0000362 return PP->Diag(getSourceLocation(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000363}
Reid Spencer5f016e22007-07-11 17:01:13 +0000364
365//===----------------------------------------------------------------------===//
366// Trigraph and Escaped Newline Handling Code.
367//===----------------------------------------------------------------------===//
368
369/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
370/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
371static char GetTrigraphCharForLetter(char Letter) {
372 switch (Letter) {
373 default: return 0;
374 case '=': return '#';
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 }
384}
385
386/// DecodeTrigraphChar - If the specified character is a legal trigraph when
387/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
388/// return the result character. Finally, emit a warning about trigraph use
389/// whether trigraphs are enabled or not.
390static char DecodeTrigraphChar(const char *CP, Lexer *L) {
391 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner3692b092008-11-18 07:59:24 +0000392 if (!Res || !L) return Res;
393
394 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +0000395 if (!L->isLexingRawMode())
396 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner3692b092008-11-18 07:59:24 +0000397 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 }
Chris Lattner3692b092008-11-18 07:59:24 +0000399
Chris Lattner74d15df2008-11-22 02:02:22 +0000400 if (!L->isLexingRawMode())
401 L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000402 return Res;
403}
404
405/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
406/// get its size, and return it. This is tricky in several cases:
407/// 1. If currently at the start of a trigraph, we warn about the trigraph,
408/// then either return the trigraph (skipping 3 chars) or the '?',
409/// depending on whether trigraphs are enabled or not.
410/// 2. If this is an escaped newline (potentially with whitespace between
411/// the backslash and newline), implicitly skip the newline and return
412/// the char after it.
413/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
414///
415/// This handles the slow/uncommon case of the getCharAndSize method. Here we
416/// know that we can accumulate into Size, and that we have already incremented
417/// Ptr by Size bytes.
418///
419/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
420/// be updated to match.
421///
422char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattnerd2177732007-07-20 16:59:19 +0000423 Token *Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000424 // If we have a slash, look for an escaped newline.
425 if (Ptr[0] == '\\') {
426 ++Size;
427 ++Ptr;
428Slash:
429 // Common case, backslash-char where the char is not whitespace.
430 if (!isWhitespace(Ptr[0])) return '\\';
431
432 // See if we have optional whitespace characters followed by a newline.
433 {
434 unsigned SizeTmp = 0;
435 do {
436 ++SizeTmp;
437 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
438 // Remember that this token needs to be cleaned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000439 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000440
441 // Warn if there was whitespace between the backslash and newline.
Chris Lattner74d15df2008-11-22 02:02:22 +0000442 if (SizeTmp != 1 && Tok && !isLexingRawMode())
Reid Spencer5f016e22007-07-11 17:01:13 +0000443 Diag(Ptr, diag::backslash_newline_space);
444
445 // If this is a \r\n or \n\r, skip the newlines.
446 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
447 Ptr[SizeTmp-1] != Ptr[SizeTmp])
448 ++SizeTmp;
449
450 // Found backslash<whitespace><newline>. Parse the char after it.
451 Size += SizeTmp;
452 Ptr += SizeTmp;
453 // Use slow version to accumulate a correct size field.
454 return getCharAndSizeSlow(Ptr, Size, Tok);
455 }
456 } while (isWhitespace(Ptr[SizeTmp]));
457 }
458
459 // Otherwise, this is not an escaped newline, just return the slash.
460 return '\\';
461 }
462
463 // If this is a trigraph, process it.
464 if (Ptr[0] == '?' && Ptr[1] == '?') {
465 // If this is actually a legal trigraph (not something like "??x"), emit
466 // a trigraph warning. If so, and if trigraphs are enabled, return it.
467 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
468 // Remember that this token needs to be cleaned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000469 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000470
471 Ptr += 3;
472 Size += 3;
473 if (C == '\\') goto Slash;
474 return C;
475 }
476 }
477
478 // If this is neither, return a single character.
479 ++Size;
480 return *Ptr;
481}
482
483
484/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
485/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
486/// and that we have already incremented Ptr by Size bytes.
487///
488/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
489/// be updated to match.
490char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
491 const LangOptions &Features) {
492 // If we have a slash, look for an escaped newline.
493 if (Ptr[0] == '\\') {
494 ++Size;
495 ++Ptr;
496Slash:
497 // Common case, backslash-char where the char is not whitespace.
498 if (!isWhitespace(Ptr[0])) return '\\';
499
500 // See if we have optional whitespace characters followed by a newline.
501 {
502 unsigned SizeTmp = 0;
503 do {
504 ++SizeTmp;
505 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
506
507 // If this is a \r\n or \n\r, skip the newlines.
508 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
509 Ptr[SizeTmp-1] != Ptr[SizeTmp])
510 ++SizeTmp;
511
512 // Found backslash<whitespace><newline>. Parse the char after it.
513 Size += SizeTmp;
514 Ptr += SizeTmp;
515
516 // Use slow version to accumulate a correct size field.
517 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
518 }
519 } while (isWhitespace(Ptr[SizeTmp]));
520 }
521
522 // Otherwise, this is not an escaped newline, just return the slash.
523 return '\\';
524 }
525
526 // If this is a trigraph, process it.
527 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
528 // If this is actually a legal trigraph (not something like "??x"), return
529 // it.
530 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
531 Ptr += 3;
532 Size += 3;
533 if (C == '\\') goto Slash;
534 return C;
535 }
536 }
537
538 // If this is neither, return a single character.
539 ++Size;
540 return *Ptr;
541}
542
543//===----------------------------------------------------------------------===//
544// Helper methods for lexing.
545//===----------------------------------------------------------------------===//
546
Chris Lattnerd2177732007-07-20 16:59:19 +0000547void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000548 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
549 unsigned Size;
550 unsigned char C = *CurPtr++;
551 while (isIdentifierBody(C)) {
552 C = *CurPtr++;
553 }
554 --CurPtr; // Back up over the skipped character.
555
556 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
557 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
558 // FIXME: UCNs.
559 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
560FinishIdentifier:
561 const char *IdStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000562 FormTokenWithChars(Result, CurPtr, tok::identifier);
Reid Spencer5f016e22007-07-11 17:01:13 +0000563
564 // If we are in raw mode, return this identifier raw. There is no need to
565 // look up identifier information or attempt to macro expand it.
566 if (LexingRawMode) return;
567
568 // Fill in Result.IdentifierInfo, looking up the identifier in the
569 // identifier table.
Chris Lattnerd1186fa2009-01-21 07:45:14 +0000570 IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000571
Chris Lattner863c4862009-01-23 18:35:48 +0000572 // Change the kind of this identifier to the appropriate token kind, e.g.
573 // turning "for" into a keyword.
574 Result.setKind(II->getTokenID());
575
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 // Finally, now that we know we have an identifier, pass this off to the
577 // preprocessor, which may macro expand it or something.
Chris Lattnerd1186fa2009-01-21 07:45:14 +0000578 if (II->isHandleIdentifierCase())
Chris Lattner6a170eb2009-01-21 07:43:11 +0000579 PP->HandleIdentifier(Result);
580 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000581 }
582
583 // Otherwise, $,\,? in identifier found. Enter slower path.
584
585 C = getCharAndSize(CurPtr, Size);
586 while (1) {
587 if (C == '$') {
588 // If we hit a $ and they are not supported in identifiers, we are done.
589 if (!Features.DollarIdents) goto FinishIdentifier;
590
591 // Otherwise, emit a diagnostic and continue.
Chris Lattner74d15df2008-11-22 02:02:22 +0000592 if (!isLexingRawMode())
593 Diag(CurPtr, diag::ext_dollar_in_identifier);
Reid Spencer5f016e22007-07-11 17:01:13 +0000594 CurPtr = ConsumeChar(CurPtr, Size, Result);
595 C = getCharAndSize(CurPtr, Size);
596 continue;
597 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
598 // Found end of identifier.
599 goto FinishIdentifier;
600 }
601
602 // Otherwise, this character is good, consume it.
603 CurPtr = ConsumeChar(CurPtr, Size, Result);
604
605 C = getCharAndSize(CurPtr, Size);
606 while (isIdentifierBody(C)) { // FIXME: UCNs.
607 CurPtr = ConsumeChar(CurPtr, Size, Result);
608 C = getCharAndSize(CurPtr, Size);
609 }
610 }
611}
612
613
Nate Begeman5253c7f2008-04-14 02:26:39 +0000614/// LexNumericConstant - Lex the remainder of a integer or floating point
Reid Spencer5f016e22007-07-11 17:01:13 +0000615/// constant. From[-1] is the first character lexed. Return the end of the
616/// constant.
Chris Lattnerd2177732007-07-20 16:59:19 +0000617void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000618 unsigned Size;
619 char C = getCharAndSize(CurPtr, Size);
620 char PrevCh = 0;
621 while (isNumberBody(C)) { // FIXME: UCNs?
622 CurPtr = ConsumeChar(CurPtr, Size, Result);
623 PrevCh = C;
624 C = getCharAndSize(CurPtr, Size);
625 }
626
627 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
628 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
629 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
630
631 // If we have a hex FP constant, continue.
Chris Lattner49842122008-11-22 07:39:03 +0000632 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
633 (Features.HexFloats || !Features.NoExtensions))
Reid Spencer5f016e22007-07-11 17:01:13 +0000634 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
635
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 // Update the location of token as well as BufferPtr.
Chris Lattner47246be2009-01-26 19:29:26 +0000637 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000638 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Chris Lattner47246be2009-01-26 19:29:26 +0000639 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000640}
641
642/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
643/// either " or L".
Chris Lattnerd88dc482008-10-12 04:05:48 +0000644void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000645 const char *NulCharacter = 0; // Does this string contain the \0 character?
646
647 char C = getAndAdvanceChar(CurPtr, Result);
648 while (C != '"') {
649 // Skip escaped characters.
650 if (C == '\\') {
651 // Skip the escaped character.
652 C = getAndAdvanceChar(CurPtr, Result);
653 } else if (C == '\n' || C == '\r' || // Newline.
654 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner74d15df2008-11-22 02:02:22 +0000655 if (!isLexingRawMode())
656 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000657 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 return;
659 } else if (C == 0) {
660 NulCharacter = CurPtr-1;
661 }
662 C = getAndAdvanceChar(CurPtr, Result);
663 }
664
665 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000666 if (NulCharacter && !isLexingRawMode())
667 Diag(NulCharacter, diag::null_in_string);
Reid Spencer5f016e22007-07-11 17:01:13 +0000668
Reid Spencer5f016e22007-07-11 17:01:13 +0000669 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner47246be2009-01-26 19:29:26 +0000670 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000671 FormTokenWithChars(Result, CurPtr,
672 Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +0000673 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000674}
675
676/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
677/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattnerd2177732007-07-20 16:59:19 +0000678void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 const char *NulCharacter = 0; // Does this string contain the \0 character?
680
681 char C = getAndAdvanceChar(CurPtr, Result);
682 while (C != '>') {
683 // Skip escaped characters.
684 if (C == '\\') {
685 // Skip the escaped character.
686 C = getAndAdvanceChar(CurPtr, Result);
687 } else if (C == '\n' || C == '\r' || // Newline.
688 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner74d15df2008-11-22 02:02:22 +0000689 if (!isLexingRawMode())
Chris Lattnerb66158c2009-02-19 18:29:56 +0000690 Diag(BufferPtr, diag::err_unterminated_angled_string);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000691 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 return;
693 } else if (C == 0) {
694 NulCharacter = CurPtr-1;
695 }
696 C = getAndAdvanceChar(CurPtr, Result);
697 }
698
699 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000700 if (NulCharacter && !isLexingRawMode())
701 Diag(NulCharacter, diag::null_in_string);
Reid Spencer5f016e22007-07-11 17:01:13 +0000702
Reid Spencer5f016e22007-07-11 17:01:13 +0000703 // Update the location of token as well as BufferPtr.
Chris Lattner47246be2009-01-26 19:29:26 +0000704 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000705 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +0000706 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000707}
708
709
710/// LexCharConstant - Lex the remainder of a character constant, after having
711/// lexed either ' or L'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000712void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000713 const char *NulCharacter = 0; // Does this character contain the \0 character?
714
715 // Handle the common case of 'x' and '\y' efficiently.
716 char C = getAndAdvanceChar(CurPtr, Result);
717 if (C == '\'') {
Chris Lattner74d15df2008-11-22 02:02:22 +0000718 if (!isLexingRawMode())
719 Diag(BufferPtr, diag::err_empty_character);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000720 FormTokenWithChars(Result, CurPtr, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 return;
722 } else if (C == '\\') {
723 // Skip the escaped character.
724 // FIXME: UCN's.
725 C = getAndAdvanceChar(CurPtr, Result);
726 }
727
728 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
729 ++CurPtr;
730 } else {
731 // Fall back on generic code for embedded nulls, newlines, wide chars.
732 do {
733 // Skip escaped characters.
734 if (C == '\\') {
735 // Skip the escaped character.
736 C = getAndAdvanceChar(CurPtr, Result);
737 } else if (C == '\n' || C == '\r' || // Newline.
738 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner74d15df2008-11-22 02:02:22 +0000739 if (!isLexingRawMode())
740 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000741 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 return;
743 } else if (C == 0) {
744 NulCharacter = CurPtr-1;
745 }
746 C = getAndAdvanceChar(CurPtr, Result);
747 } while (C != '\'');
748 }
749
Chris Lattner74d15df2008-11-22 02:02:22 +0000750 if (NulCharacter && !isLexingRawMode())
751 Diag(NulCharacter, diag::null_in_char);
Reid Spencer5f016e22007-07-11 17:01:13 +0000752
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 // Update the location of token as well as BufferPtr.
Chris Lattner47246be2009-01-26 19:29:26 +0000754 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000755 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Chris Lattner47246be2009-01-26 19:29:26 +0000756 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000757}
758
759/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
760/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattnerd88dc482008-10-12 04:05:48 +0000761///
762/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
763///
764bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 // Whitespace - Skip it, then return the token after the whitespace.
766 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
767 while (1) {
768 // Skip horizontal whitespace very aggressively.
769 while (isHorizontalWhitespace(Char))
770 Char = *++CurPtr;
771
Daniel Dunbarddd3e8b2008-11-25 00:20:22 +0000772 // Otherwise if we have something other than whitespace, we're done.
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 if (Char != '\n' && Char != '\r')
774 break;
775
776 if (ParsingPreprocessorDirective) {
777 // End of preprocessor directive line, let LexTokenInternal handle this.
778 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +0000779 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000780 }
781
782 // ok, but handle newline.
783 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +0000784 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +0000786 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 Char = *++CurPtr;
788 }
789
790 // If this isn't immediately after a newline, there is leading space.
791 char PrevChar = CurPtr[-1];
792 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattnerd2177732007-07-20 16:59:19 +0000793 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000794
Chris Lattnerd88dc482008-10-12 04:05:48 +0000795 // If the client wants us to return whitespace, return it now.
796 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +0000797 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +0000798 return true;
799 }
800
Reid Spencer5f016e22007-07-11 17:01:13 +0000801 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +0000802 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000803}
804
805// SkipBCPLComment - We have just read the // characters from input. Skip until
806// we find the newline character thats terminate the comment. Then update
Chris Lattner2d381892008-10-12 04:15:42 +0000807/// BufferPtr and return. If we're in KeepCommentMode, this will form the token
808/// and return true.
Chris Lattnerd2177732007-07-20 16:59:19 +0000809bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 // If BCPL comments aren't explicitly enabled for this language, emit an
811 // extension warning.
Chris Lattner74d15df2008-11-22 02:02:22 +0000812 if (!Features.BCPLComment && !isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000813 Diag(BufferPtr, diag::ext_bcpl_comment);
814
815 // Mark them enabled so we only emit one warning for this translation
816 // unit.
817 Features.BCPLComment = true;
818 }
819
820 // Scan over the body of the comment. The common case, when scanning, is that
821 // the comment contains normal ascii characters with nothing interesting in
822 // them. As such, optimize for this case with the inner loop.
823 char C;
824 do {
825 C = *CurPtr;
826 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
827 // If we find a \n character, scan backwards, checking to see if it's an
828 // escaped newline, like we do for block comments.
829
830 // Skip over characters in the fast loop.
831 while (C != 0 && // Potentially EOF.
832 C != '\\' && // Potentially escaped newline.
833 C != '?' && // Potentially trigraph.
834 C != '\n' && C != '\r') // Newline or DOS-style newline.
835 C = *++CurPtr;
836
837 // If this is a newline, we're done.
838 if (C == '\n' || C == '\r')
839 break; // Found the newline? Break out!
840
841 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000842 // properly decode the character. Read it in raw mode to avoid emitting
843 // diagnostics about things like trigraphs. If we see an escaped newline,
844 // we'll handle it below.
Reid Spencer5f016e22007-07-11 17:01:13 +0000845 const char *OldPtr = CurPtr;
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000846 bool OldRawMode = isLexingRawMode();
847 LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000848 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000849 LexingRawMode = OldRawMode;
Reid Spencer5f016e22007-07-11 17:01:13 +0000850
851 // If we read multiple characters, and one of those characters was a \r or
852 // \n, then we had an escaped newline within the comment. Emit diagnostic
853 // unless the next line is also a // comment.
854 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
855 for (; OldPtr != CurPtr; ++OldPtr)
856 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
857 // Okay, we found a // comment that ends in a newline, if the next
858 // line is also a // comment, but has spaces, don't emit a diagnostic.
859 if (isspace(C)) {
860 const char *ForwardPtr = CurPtr;
861 while (isspace(*ForwardPtr)) // Skip whitespace.
862 ++ForwardPtr;
863 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
864 break;
865 }
866
Chris Lattner74d15df2008-11-22 02:02:22 +0000867 if (!isLexingRawMode())
868 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 break;
870 }
871 }
872
873 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
874 } while (C != '\n' && C != '\r');
875
876 // Found but did not consume the newline.
877
878 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +0000879 if (inKeepCommentMode())
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 return SaveBCPLComment(Result, CurPtr);
881
882 // If we are inside a preprocessor directive and we see the end of line,
883 // return immediately, so that the lexer can return this as an EOM token.
884 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
885 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +0000886 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000887 }
888
889 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner7a4f0042008-10-12 00:23:07 +0000890 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattnerd88dc482008-10-12 04:05:48 +0000891 // contribute to another token), it isn't needed for correctness. Note that
892 // this is ok even in KeepWhitespaceMode, because we would have returned the
893 /// comment above in that mode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 ++CurPtr;
895
896 // The next returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +0000897 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +0000899 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000900 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +0000901 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000902}
903
904/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
905/// an appropriate way and return it.
Chris Lattnerd2177732007-07-20 16:59:19 +0000906bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner9e6293d2008-10-12 04:51:35 +0000907 // If we're not in a preprocessor directive, just return the // comment
908 // directly.
909 FormTokenWithChars(Result, CurPtr, tok::comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000910
Chris Lattner9e6293d2008-10-12 04:51:35 +0000911 if (!ParsingPreprocessorDirective)
912 return true;
913
914 // If this BCPL-style comment is in a macro definition, transmogrify it into
915 // a C-style block comment.
916 std::string Spelling = PP->getSpelling(Result);
917 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
918 Spelling[1] = '*'; // Change prefix to "/*".
919 Spelling += "*/"; // add suffix.
920
921 Result.setKind(tok::comment);
Chris Lattner47246be2009-01-26 19:29:26 +0000922 PP->CreateString(&Spelling[0], Spelling.size(), Result,
923 Result.getLocation());
Chris Lattner2d381892008-10-12 04:15:42 +0000924 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000925}
926
927/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
928/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner47a2b402008-12-12 07:14:34 +0000929/// diagnostic if so. We know that the newline is inside of a block comment.
Reid Spencer5f016e22007-07-11 17:01:13 +0000930static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
931 Lexer *L) {
932 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
933
934 // Back up off the newline.
935 --CurPtr;
936
937 // If this is a two-character newline sequence, skip the other character.
938 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
939 // \n\n or \r\r -> not escaped newline.
940 if (CurPtr[0] == CurPtr[1])
941 return false;
942 // \n\r or \r\n -> skip the newline.
943 --CurPtr;
944 }
945
946 // If we have horizontal whitespace, skip over it. We allow whitespace
947 // between the slash and newline.
948 bool HasSpace = false;
949 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
950 --CurPtr;
951 HasSpace = true;
952 }
953
954 // If we have a slash, we know this is an escaped newline.
955 if (*CurPtr == '\\') {
956 if (CurPtr[-1] != '*') return false;
957 } else {
958 // It isn't a slash, is it the ?? / trigraph?
959 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
960 CurPtr[-3] != '*')
961 return false;
962
963 // This is the trigraph ending the comment. Emit a stern warning!
964 CurPtr -= 2;
965
966 // If no trigraphs are enabled, warn that we ignored this trigraph and
967 // ignore this * character.
968 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +0000969 if (!L->isLexingRawMode())
970 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000971 return false;
972 }
Chris Lattner74d15df2008-11-22 02:02:22 +0000973 if (!L->isLexingRawMode())
974 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000975 }
976
977 // Warn about having an escaped newline between the */ characters.
Chris Lattner74d15df2008-11-22 02:02:22 +0000978 if (!L->isLexingRawMode())
979 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Reid Spencer5f016e22007-07-11 17:01:13 +0000980
981 // If there was space between the backslash and newline, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000982 if (HasSpace && !L->isLexingRawMode())
983 L->Diag(CurPtr, diag::backslash_newline_space);
Reid Spencer5f016e22007-07-11 17:01:13 +0000984
985 return true;
986}
987
988#ifdef __SSE2__
989#include <emmintrin.h>
990#elif __ALTIVEC__
991#include <altivec.h>
992#undef bool
993#endif
994
995/// SkipBlockComment - We have just read the /* characters from input. Read
996/// until we find the */ characters that terminate the comment. Note that we
997/// don't bother decoding trigraphs or escaped newlines in block comments,
998/// because they cannot cause the comment to end. The only thing that can
999/// happen is the comment could end with an escaped newline between the */ end
1000/// of comment.
Chris Lattner2d381892008-10-12 04:15:42 +00001001///
1002/// If KeepCommentMode is enabled, this forms a token from the comment and
1003/// returns true.
Chris Lattnerd2177732007-07-20 16:59:19 +00001004bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 // Scan one character past where we should, looking for a '/' character. Once
1006 // we find it, check to see if it was preceeded by a *. This common
1007 // optimization helps people who like to put a lot of * characters in their
1008 // comments.
Chris Lattner8146b682007-07-21 23:43:37 +00001009
1010 // The first character we get with newlines and trigraphs skipped to handle
1011 // the degenerate /*/ case below correctly if the * has an escaped newline
1012 // after it.
1013 unsigned CharSize;
1014 unsigned char C = getCharAndSize(CurPtr, CharSize);
1015 CurPtr += CharSize;
Reid Spencer5f016e22007-07-11 17:01:13 +00001016 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001017 if (!isLexingRawMode())
Chris Lattner0af57422008-10-12 01:31:51 +00001018 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001019 --CurPtr;
1020
1021 // KeepWhitespaceMode should return this broken comment as a token. Since
1022 // it isn't a well formed comment, just return it as an 'unknown' token.
1023 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001024 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001025 return true;
1026 }
1027
1028 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001029 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001030 }
1031
Chris Lattner8146b682007-07-21 23:43:37 +00001032 // Check to see if the first character after the '/*' is another /. If so,
1033 // then this slash does not end the block comment, it is part of it.
1034 if (C == '/')
1035 C = *CurPtr++;
1036
Reid Spencer5f016e22007-07-11 17:01:13 +00001037 while (1) {
1038 // Skip over all non-interesting characters until we find end of buffer or a
1039 // (probably ending) '/' character.
1040 if (CurPtr + 24 < BufferEnd) {
1041 // While not aligned to a 16-byte boundary.
1042 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1043 C = *CurPtr++;
1044
1045 if (C == '/') goto FoundSlash;
1046
1047#ifdef __SSE2__
1048 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1049 '/', '/', '/', '/', '/', '/', '/', '/');
1050 while (CurPtr+16 <= BufferEnd &&
1051 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1052 CurPtr += 16;
1053#elif __ALTIVEC__
1054 __vector unsigned char Slashes = {
1055 '/', '/', '/', '/', '/', '/', '/', '/',
1056 '/', '/', '/', '/', '/', '/', '/', '/'
1057 };
1058 while (CurPtr+16 <= BufferEnd &&
1059 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1060 CurPtr += 16;
1061#else
1062 // Scan for '/' quickly. Many block comments are very large.
1063 while (CurPtr[0] != '/' &&
1064 CurPtr[1] != '/' &&
1065 CurPtr[2] != '/' &&
1066 CurPtr[3] != '/' &&
1067 CurPtr+4 < BufferEnd) {
1068 CurPtr += 4;
1069 }
1070#endif
1071
1072 // It has to be one of the bytes scanned, increment to it and read one.
1073 C = *CurPtr++;
1074 }
1075
1076 // Loop to scan the remainder.
1077 while (C != '/' && C != '\0')
1078 C = *CurPtr++;
1079
1080 FoundSlash:
1081 if (C == '/') {
1082 if (CurPtr[-2] == '*') // We found the final */. We're done!
1083 break;
1084
1085 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
1086 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
1087 // We found the final */, though it had an escaped newline between the
1088 // * and /. We're done!
1089 break;
1090 }
1091 }
1092 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1093 // If this is a /* inside of the comment, emit a warning. Don't do this
1094 // if this is a /*/, which will end the comment. This misses cases with
1095 // embedded escaped newlines, but oh well.
Chris Lattner74d15df2008-11-22 02:02:22 +00001096 if (!isLexingRawMode())
1097 Diag(CurPtr-1, diag::warn_nested_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001098 }
1099 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001100 if (!isLexingRawMode())
1101 Diag(BufferPtr, diag::err_unterminated_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001102 // Note: the user probably forgot a */. We could continue immediately
1103 // after the /*, but this would involve lexing a lot of what really is the
1104 // comment, which surely would confuse the parser.
Chris Lattner31f0eca2008-10-12 04:19:49 +00001105 --CurPtr;
1106
1107 // KeepWhitespaceMode should return this broken comment as a token. Since
1108 // it isn't a well formed comment, just return it as an 'unknown' token.
1109 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001110 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001111 return true;
1112 }
1113
1114 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001115 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001116 }
1117 C = *CurPtr++;
1118 }
1119
1120 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +00001121 if (inKeepCommentMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001122 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattner2d381892008-10-12 04:15:42 +00001123 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001124 }
1125
1126 // It is common for the tokens immediately after a /**/ comment to be
1127 // whitespace. Instead of going through the big switch, handle it
Chris Lattnerd88dc482008-10-12 04:05:48 +00001128 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1129 // have already returned above with the comment as a token.
Reid Spencer5f016e22007-07-11 17:01:13 +00001130 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001131 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001132 SkipWhitespace(Result, CurPtr+1);
Chris Lattner2d381892008-10-12 04:15:42 +00001133 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 }
1135
1136 // Otherwise, just return so that the next character will be lexed as a token.
1137 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001138 Result.setFlag(Token::LeadingSpace);
Chris Lattner2d381892008-10-12 04:15:42 +00001139 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001140}
1141
1142//===----------------------------------------------------------------------===//
1143// Primary Lexing Entry Points
1144//===----------------------------------------------------------------------===//
1145
Reid Spencer5f016e22007-07-11 17:01:13 +00001146/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1147/// uninterpreted string. This switches the lexer out of directive mode.
1148std::string Lexer::ReadToEndOfLine() {
1149 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1150 "Must be in a preprocessing directive!");
1151 std::string Result;
Chris Lattnerd2177732007-07-20 16:59:19 +00001152 Token Tmp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001153
1154 // CurPtr - Cache BufferPtr in an automatic variable.
1155 const char *CurPtr = BufferPtr;
1156 while (1) {
1157 char Char = getAndAdvanceChar(CurPtr, Tmp);
1158 switch (Char) {
1159 default:
1160 Result += Char;
1161 break;
1162 case 0: // Null.
1163 // Found end of file?
1164 if (CurPtr-1 != BufferEnd) {
1165 // Nope, normal character, continue.
1166 Result += Char;
1167 break;
1168 }
1169 // FALL THROUGH.
1170 case '\r':
1171 case '\n':
1172 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1173 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1174 BufferPtr = CurPtr-1;
1175
1176 // Next, lex the character, which should handle the EOM transition.
1177 Lex(Tmp);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001178 assert(Tmp.is(tok::eom) && "Unexpected token!");
Reid Spencer5f016e22007-07-11 17:01:13 +00001179
1180 // Finally, we're done, return the string we found.
1181 return Result;
1182 }
1183 }
1184}
1185
1186/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1187/// condition, reporting diagnostics and handling other edge cases as required.
1188/// This returns true if Result contains a token, false if PP.Lex should be
1189/// called again.
Chris Lattnerd2177732007-07-20 16:59:19 +00001190bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001191 // If we hit the end of the file while parsing a preprocessor directive,
1192 // end the preprocessor directive first. The next token returned will
1193 // then be the end of file.
1194 if (ParsingPreprocessorDirective) {
1195 // Done parsing the "line".
1196 ParsingPreprocessorDirective = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001197 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00001198 FormTokenWithChars(Result, CurPtr, tok::eom);
Reid Spencer5f016e22007-07-11 17:01:13 +00001199
1200 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001201 SetCommentRetentionState(PP->getCommentRetentionState());
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 return true; // Have a token.
1203 }
1204
1205 // If we are in raw mode, return this event as an EOF token. Let the caller
1206 // that put us in raw mode handle the event.
Chris Lattner74d15df2008-11-22 02:02:22 +00001207 if (isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001208 Result.startToken();
1209 BufferPtr = BufferEnd;
Chris Lattner9e6293d2008-10-12 04:51:35 +00001210 FormTokenWithChars(Result, BufferEnd, tok::eof);
Reid Spencer5f016e22007-07-11 17:01:13 +00001211 return true;
1212 }
1213
1214 // Otherwise, issue diagnostics for unterminated #if and missing newline.
1215
1216 // If we are in a #if directive, emit an error.
1217 while (!ConditionalStack.empty()) {
Chris Lattner30c64762008-11-22 06:22:39 +00001218 PP->Diag(ConditionalStack.back().IfLoc,
1219 diag::err_pp_unterminated_conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 ConditionalStack.pop_back();
1221 }
1222
Chris Lattnerb25e5d72008-04-12 05:54:25 +00001223 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1224 // a pedwarn.
1225 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Reid Spencer5f016e22007-07-11 17:01:13 +00001226 Diag(BufferEnd, diag::ext_no_newline_eof);
1227
1228 BufferPtr = CurPtr;
1229
1230 // Finally, let the preprocessor handle this.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001231 return PP->HandleEndOfFile(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001232}
1233
1234/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1235/// the specified lexer will return a tok::l_paren token, 0 if it is something
1236/// else and 2 if there are no more tokens in the buffer controlled by the
1237/// lexer.
1238unsigned Lexer::isNextPPTokenLParen() {
1239 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1240
1241 // Switch to 'skipping' mode. This will ensure that we can lex a token
1242 // without emitting diagnostics, disables macro expansion, and will cause EOF
1243 // to return an EOF token instead of popping the include stack.
1244 LexingRawMode = true;
1245
1246 // Save state that can be changed while lexing so that we can restore it.
1247 const char *TmpBufferPtr = BufferPtr;
1248
Chris Lattnerd2177732007-07-20 16:59:19 +00001249 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001250 Tok.startToken();
1251 LexTokenInternal(Tok);
1252
1253 // Restore state that may have changed.
1254 BufferPtr = TmpBufferPtr;
1255
1256 // Restore the lexer back to non-skipping mode.
1257 LexingRawMode = false;
1258
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001259 if (Tok.is(tok::eof))
Reid Spencer5f016e22007-07-11 17:01:13 +00001260 return 2;
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001261 return Tok.is(tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001262}
1263
1264
1265/// LexTokenInternal - This implements a simple C family lexer. It is an
1266/// extremely performance critical piece of code. This assumes that the buffer
1267/// has a null character at the end of the file. Return true if an error
1268/// occurred and compilation should terminate, false if normal. This returns a
1269/// preprocessing token, not a normal token, as such, it is an internal
1270/// interface. It assumes that the Flags of result have been cleared before
1271/// calling this.
Chris Lattnerd2177732007-07-20 16:59:19 +00001272void Lexer::LexTokenInternal(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001273LexNextToken:
1274 // New token, can't need cleaning yet.
Chris Lattnerd2177732007-07-20 16:59:19 +00001275 Result.clearFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +00001276 Result.setIdentifierInfo(0);
1277
1278 // CurPtr - Cache BufferPtr in an automatic variable.
1279 const char *CurPtr = BufferPtr;
1280
1281 // Small amounts of horizontal whitespace is very common between tokens.
1282 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1283 ++CurPtr;
1284 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1285 ++CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +00001286
1287 // If we are keeping whitespace and other tokens, just return what we just
1288 // skipped. The next lexer invocation will return the token after the
1289 // whitespace.
1290 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001291 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001292 return;
1293 }
1294
Reid Spencer5f016e22007-07-11 17:01:13 +00001295 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001296 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001297 }
1298
1299 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1300
1301 // Read a character, advancing over it.
1302 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001303 tok::TokenKind Kind;
1304
Reid Spencer5f016e22007-07-11 17:01:13 +00001305 switch (Char) {
1306 case 0: // Null.
1307 // Found end of file?
1308 if (CurPtr-1 == BufferEnd) {
1309 // Read the PP instance variable into an automatic variable, because
1310 // LexEndOfFile will often delete 'this'.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001311 Preprocessor *PPCache = PP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1313 return; // Got a token to return.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001314 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1315 return PPCache->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001316 }
1317
Chris Lattner74d15df2008-11-22 02:02:22 +00001318 if (!isLexingRawMode())
1319 Diag(CurPtr-1, diag::null_in_file);
Chris Lattnerd2177732007-07-20 16:59:19 +00001320 Result.setFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001321 if (SkipWhitespace(Result, CurPtr))
1322 return; // KeepWhitespaceMode
1323
Reid Spencer5f016e22007-07-11 17:01:13 +00001324 goto LexNextToken; // GCC isn't tail call eliminating.
1325 case '\n':
1326 case '\r':
1327 // If we are inside a preprocessor directive and we see the end of line,
1328 // we know we are done with the directive, so return an EOM token.
1329 if (ParsingPreprocessorDirective) {
1330 // Done parsing the "line".
1331 ParsingPreprocessorDirective = false;
1332
1333 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001334 SetCommentRetentionState(PP->getCommentRetentionState());
Reid Spencer5f016e22007-07-11 17:01:13 +00001335
1336 // Since we consumed a newline, we are back at the start of a line.
1337 IsAtStartOfLine = true;
1338
Chris Lattner9e6293d2008-10-12 04:51:35 +00001339 Kind = tok::eom;
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 break;
1341 }
1342 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +00001343 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001344 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +00001345 Result.clearFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001346
1347 if (SkipWhitespace(Result, CurPtr))
1348 return; // KeepWhitespaceMode
Reid Spencer5f016e22007-07-11 17:01:13 +00001349 goto LexNextToken; // GCC isn't tail call eliminating.
1350 case ' ':
1351 case '\t':
1352 case '\f':
1353 case '\v':
Chris Lattner8133cfc2007-07-22 06:29:05 +00001354 SkipHorizontalWhitespace:
Chris Lattnerd2177732007-07-20 16:59:19 +00001355 Result.setFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001356 if (SkipWhitespace(Result, CurPtr))
1357 return; // KeepWhitespaceMode
Chris Lattner8133cfc2007-07-22 06:29:05 +00001358
1359 SkipIgnoredUnits:
1360 CurPtr = BufferPtr;
1361
1362 // If the next token is obviously a // or /* */ comment, skip it efficiently
1363 // too (without going through the big switch stmt).
Chris Lattner8402c732009-01-16 22:39:25 +00001364 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1365 Features.BCPLComment) {
Chris Lattner8133cfc2007-07-22 06:29:05 +00001366 SkipBCPLComment(Result, CurPtr+2);
1367 goto SkipIgnoredUnits;
Chris Lattnerfa95a012008-10-12 03:22:02 +00001368 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner8133cfc2007-07-22 06:29:05 +00001369 SkipBlockComment(Result, CurPtr+2);
1370 goto SkipIgnoredUnits;
1371 } else if (isHorizontalWhitespace(*CurPtr)) {
1372 goto SkipHorizontalWhitespace;
1373 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 goto LexNextToken; // GCC isn't tail call eliminating.
1375
Chris Lattner3a570772008-01-03 17:58:54 +00001376 // C99 6.4.4.1: Integer Constants.
1377 // C99 6.4.4.2: Floating Constants.
1378 case '0': case '1': case '2': case '3': case '4':
1379 case '5': case '6': case '7': case '8': case '9':
1380 // Notify MIOpt that we read a non-whitespace/non-comment token.
1381 MIOpt.ReadToken();
1382 return LexNumericConstant(Result, CurPtr);
1383
1384 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Reid Spencer5f016e22007-07-11 17:01:13 +00001385 // Notify MIOpt that we read a non-whitespace/non-comment token.
1386 MIOpt.ReadToken();
1387 Char = getCharAndSize(CurPtr, SizeTmp);
1388
1389 // Wide string literal.
1390 if (Char == '"')
1391 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1392 true);
1393
1394 // Wide character constant.
1395 if (Char == '\'')
1396 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1397 // FALL THROUGH, treating L like the start of an identifier.
1398
1399 // C99 6.4.2: Identifiers.
1400 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1401 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1402 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1403 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1404 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1405 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1406 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1407 case 'v': case 'w': case 'x': case 'y': case 'z':
1408 case '_':
1409 // Notify MIOpt that we read a non-whitespace/non-comment token.
1410 MIOpt.ReadToken();
1411 return LexIdentifier(Result, CurPtr);
Chris Lattner3a570772008-01-03 17:58:54 +00001412
1413 case '$': // $ in identifiers.
1414 if (Features.DollarIdents) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001415 if (!isLexingRawMode())
1416 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner3a570772008-01-03 17:58:54 +00001417 // Notify MIOpt that we read a non-whitespace/non-comment token.
1418 MIOpt.ReadToken();
1419 return LexIdentifier(Result, CurPtr);
1420 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001421
Chris Lattner9e6293d2008-10-12 04:51:35 +00001422 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00001423 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00001424
1425 // C99 6.4.4: Character Constants.
1426 case '\'':
1427 // Notify MIOpt that we read a non-whitespace/non-comment token.
1428 MIOpt.ReadToken();
1429 return LexCharConstant(Result, CurPtr);
1430
1431 // C99 6.4.5: String Literals.
1432 case '"':
1433 // Notify MIOpt that we read a non-whitespace/non-comment token.
1434 MIOpt.ReadToken();
1435 return LexStringLiteral(Result, CurPtr, false);
1436
1437 // C99 6.4.6: Punctuators.
1438 case '?':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001439 Kind = tok::question;
Reid Spencer5f016e22007-07-11 17:01:13 +00001440 break;
1441 case '[':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001442 Kind = tok::l_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001443 break;
1444 case ']':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001445 Kind = tok::r_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001446 break;
1447 case '(':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001448 Kind = tok::l_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001449 break;
1450 case ')':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001451 Kind = tok::r_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001452 break;
1453 case '{':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001454 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001455 break;
1456 case '}':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001457 Kind = tok::r_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001458 break;
1459 case '.':
1460 Char = getCharAndSize(CurPtr, SizeTmp);
1461 if (Char >= '0' && Char <= '9') {
1462 // Notify MIOpt that we read a non-whitespace/non-comment token.
1463 MIOpt.ReadToken();
1464
1465 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1466 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001467 Kind = tok::periodstar;
Reid Spencer5f016e22007-07-11 17:01:13 +00001468 CurPtr += SizeTmp;
1469 } else if (Char == '.' &&
1470 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001471 Kind = tok::ellipsis;
Reid Spencer5f016e22007-07-11 17:01:13 +00001472 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1473 SizeTmp2, Result);
1474 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001475 Kind = tok::period;
Reid Spencer5f016e22007-07-11 17:01:13 +00001476 }
1477 break;
1478 case '&':
1479 Char = getCharAndSize(CurPtr, SizeTmp);
1480 if (Char == '&') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001481 Kind = tok::ampamp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001482 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1483 } else if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001484 Kind = tok::ampequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001485 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1486 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001487 Kind = tok::amp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001488 }
1489 break;
1490 case '*':
1491 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001492 Kind = tok::starequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001493 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1494 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001495 Kind = tok::star;
Reid Spencer5f016e22007-07-11 17:01:13 +00001496 }
1497 break;
1498 case '+':
1499 Char = getCharAndSize(CurPtr, SizeTmp);
1500 if (Char == '+') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001501 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001502 Kind = tok::plusplus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001503 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001504 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001505 Kind = tok::plusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001506 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001507 Kind = tok::plus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001508 }
1509 break;
1510 case '-':
1511 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001512 if (Char == '-') { // --
Reid Spencer5f016e22007-07-11 17:01:13 +00001513 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001514 Kind = tok::minusminus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001515 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattner9e6293d2008-10-12 04:51:35 +00001516 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Reid Spencer5f016e22007-07-11 17:01:13 +00001517 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1518 SizeTmp2, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001519 Kind = tok::arrowstar;
1520 } else if (Char == '>') { // ->
Reid Spencer5f016e22007-07-11 17:01:13 +00001521 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001522 Kind = tok::arrow;
1523 } else if (Char == '=') { // -=
Reid Spencer5f016e22007-07-11 17:01:13 +00001524 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001525 Kind = tok::minusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001526 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001527 Kind = tok::minus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001528 }
1529 break;
1530 case '~':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001531 Kind = tok::tilde;
Reid Spencer5f016e22007-07-11 17:01:13 +00001532 break;
1533 case '!':
1534 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001535 Kind = tok::exclaimequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001536 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1537 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001538 Kind = tok::exclaim;
Reid Spencer5f016e22007-07-11 17:01:13 +00001539 }
1540 break;
1541 case '/':
1542 // 6.4.9: Comments
1543 Char = getCharAndSize(CurPtr, SizeTmp);
1544 if (Char == '/') { // BCPL comment.
Chris Lattner8402c732009-01-16 22:39:25 +00001545 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
1546 // want to lex this as a comment. There is one problem with this though,
1547 // that in one particular corner case, this can change the behavior of the
1548 // resultant program. For example, In "foo //**/ bar", C89 would lex
1549 // this as "foo / bar" and langauges with BCPL comments would lex it as
1550 // "foo". Check to see if the character after the second slash is a '*'.
1551 // If so, we will lex that as a "/" instead of the start of a comment.
1552 if (Features.BCPLComment ||
1553 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
1554 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1555 return; // KeepCommentMode
Chris Lattner2d381892008-10-12 04:15:42 +00001556
Chris Lattner8402c732009-01-16 22:39:25 +00001557 // It is common for the tokens immediately after a // comment to be
1558 // whitespace (indentation for the next line). Instead of going through
1559 // the big switch, handle it efficiently now.
1560 goto SkipIgnoredUnits;
1561 }
1562 }
1563
1564 if (Char == '*') { // /**/ comment.
Reid Spencer5f016e22007-07-11 17:01:13 +00001565 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner2d381892008-10-12 04:15:42 +00001566 return; // KeepCommentMode
1567 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner8402c732009-01-16 22:39:25 +00001568 }
1569
1570 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001571 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001572 Kind = tok::slashequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001573 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001574 Kind = tok::slash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001575 }
1576 break;
1577 case '%':
1578 Char = getCharAndSize(CurPtr, SizeTmp);
1579 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001580 Kind = tok::percentequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001581 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1582 } else if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001583 Kind = tok::r_brace; // '%>' -> '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001584 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1585 } else if (Features.Digraphs && Char == ':') {
1586 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1587 Char = getCharAndSize(CurPtr, SizeTmp);
1588 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001589 Kind = tok::hashhash; // '%:%:' -> '##'
Reid Spencer5f016e22007-07-11 17:01:13 +00001590 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1591 SizeTmp2, Result);
1592 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Reid Spencer5f016e22007-07-11 17:01:13 +00001593 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner74d15df2008-11-22 02:02:22 +00001594 if (!isLexingRawMode())
1595 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001596 Kind = tok::hashat;
Reid Spencer5f016e22007-07-11 17:01:13 +00001597 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001598 Kind = tok::hash; // '%:' -> '#'
Reid Spencer5f016e22007-07-11 17:01:13 +00001599
1600 // We parsed a # character. If this occurs at the start of the line,
1601 // it's actually the start of a preprocessing directive. Callback to
1602 // the preprocessor to handle it.
1603 // FIXME: -fpreprocessed mode??
1604 if (Result.isAtStartOfLine() && !LexingRawMode) {
1605 BufferPtr = CurPtr;
Chris Lattner168ae2d2007-10-17 20:41:00 +00001606 PP->HandleDirective(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001607
1608 // As an optimization, if the preprocessor didn't switch lexers, tail
1609 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001610 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001611 // Start a new token. If this is a #include or something, the PP may
1612 // want us starting at the beginning of the line again. If so, set
1613 // the StartOfLine flag.
1614 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001615 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 IsAtStartOfLine = false;
1617 }
1618 goto LexNextToken; // GCC isn't tail call eliminating.
1619 }
1620
Chris Lattner168ae2d2007-10-17 20:41:00 +00001621 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001622 }
1623 }
1624 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001625 Kind = tok::percent;
Reid Spencer5f016e22007-07-11 17:01:13 +00001626 }
1627 break;
1628 case '<':
1629 Char = getCharAndSize(CurPtr, SizeTmp);
1630 if (ParsingFilename) {
1631 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1632 } else if (Char == '<' &&
1633 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001634 Kind = tok::lesslessequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001635 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1636 SizeTmp2, Result);
1637 } else if (Char == '<') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001638 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001639 Kind = tok::lessless;
Reid Spencer5f016e22007-07-11 17:01:13 +00001640 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001641 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001642 Kind = tok::lessequal;
1643 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Reid Spencer5f016e22007-07-11 17:01:13 +00001644 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001645 Kind = tok::l_square;
1646 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001648 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001649 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001650 Kind = tok::less;
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 }
1652 break;
1653 case '>':
1654 Char = getCharAndSize(CurPtr, SizeTmp);
1655 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001656 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001657 Kind = tok::greaterequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001658 } else if (Char == '>' &&
1659 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001660 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1661 SizeTmp2, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001662 Kind = tok::greatergreaterequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001663 } else if (Char == '>') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001664 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001665 Kind = tok::greatergreater;
Reid Spencer5f016e22007-07-11 17:01:13 +00001666 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001667 Kind = tok::greater;
Reid Spencer5f016e22007-07-11 17:01:13 +00001668 }
1669 break;
1670 case '^':
1671 Char = getCharAndSize(CurPtr, SizeTmp);
1672 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001674 Kind = tok::caretequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001675 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001676 Kind = tok::caret;
Reid Spencer5f016e22007-07-11 17:01:13 +00001677 }
1678 break;
1679 case '|':
1680 Char = getCharAndSize(CurPtr, SizeTmp);
1681 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001682 Kind = tok::pipeequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001683 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1684 } else if (Char == '|') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001685 Kind = tok::pipepipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00001686 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1687 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001688 Kind = tok::pipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00001689 }
1690 break;
1691 case ':':
1692 Char = getCharAndSize(CurPtr, SizeTmp);
1693 if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001694 Kind = tok::r_square; // ':>' -> ']'
Reid Spencer5f016e22007-07-11 17:01:13 +00001695 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1696 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001697 Kind = tok::coloncolon;
Reid Spencer5f016e22007-07-11 17:01:13 +00001698 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1699 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001700 Kind = tok::colon;
Reid Spencer5f016e22007-07-11 17:01:13 +00001701 }
1702 break;
1703 case ';':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001704 Kind = tok::semi;
Reid Spencer5f016e22007-07-11 17:01:13 +00001705 break;
1706 case '=':
1707 Char = getCharAndSize(CurPtr, SizeTmp);
1708 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001709 Kind = tok::equalequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001710 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1711 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001712 Kind = tok::equal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001713 }
1714 break;
1715 case ',':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001716 Kind = tok::comma;
Reid Spencer5f016e22007-07-11 17:01:13 +00001717 break;
1718 case '#':
1719 Char = getCharAndSize(CurPtr, SizeTmp);
1720 if (Char == '#') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001721 Kind = tok::hashhash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001722 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1723 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner9e6293d2008-10-12 04:51:35 +00001724 Kind = tok::hashat;
Chris Lattner74d15df2008-11-22 02:02:22 +00001725 if (!isLexingRawMode())
1726 Diag(BufferPtr, diag::charize_microsoft_ext);
Reid Spencer5f016e22007-07-11 17:01:13 +00001727 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1728 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001729 Kind = tok::hash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001730 // We parsed a # character. If this occurs at the start of the line,
1731 // it's actually the start of a preprocessing directive. Callback to
1732 // the preprocessor to handle it.
1733 // FIXME: -fpreprocessed mode??
1734 if (Result.isAtStartOfLine() && !LexingRawMode) {
1735 BufferPtr = CurPtr;
Chris Lattner168ae2d2007-10-17 20:41:00 +00001736 PP->HandleDirective(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001737
1738 // As an optimization, if the preprocessor didn't switch lexers, tail
1739 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001740 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001741 // Start a new token. If this is a #include or something, the PP may
1742 // want us starting at the beginning of the line again. If so, set
1743 // the StartOfLine flag.
1744 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001745 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001746 IsAtStartOfLine = false;
1747 }
1748 goto LexNextToken; // GCC isn't tail call eliminating.
1749 }
Chris Lattner168ae2d2007-10-17 20:41:00 +00001750 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001751 }
1752 }
1753 break;
1754
Chris Lattner3a570772008-01-03 17:58:54 +00001755 case '@':
1756 // Objective C support.
1757 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattner9e6293d2008-10-12 04:51:35 +00001758 Kind = tok::at;
Chris Lattner3a570772008-01-03 17:58:54 +00001759 else
Chris Lattner9e6293d2008-10-12 04:51:35 +00001760 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00001761 break;
1762
Reid Spencer5f016e22007-07-11 17:01:13 +00001763 case '\\':
1764 // FIXME: UCN's.
1765 // FALL THROUGH.
1766 default:
Chris Lattner9e6293d2008-10-12 04:51:35 +00001767 Kind = tok::unknown;
Reid Spencer5f016e22007-07-11 17:01:13 +00001768 break;
1769 }
1770
1771 // Notify MIOpt that we read a non-whitespace/non-comment token.
1772 MIOpt.ReadToken();
1773
1774 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00001775 FormTokenWithChars(Result, CurPtr, Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001776}