blob: 8acfe2aef6a2ce1e0d9f0c233bd0646fa192a2ca [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"
29#include "clang/Basic/Diagnostic.h"
30#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 {
Chris Lattnercb8e41c2007-10-09 18:02:16 +000044 return is(tok::identifier) &&
45 getIdentifierInfo()->getObjCKeywordID() == objcKey;
Chris Lattneraa9bdf12007-10-07 08:47:24 +000046}
47
48/// getObjCKeywordID - Return the ObjC keyword kind.
49tok::ObjCKeywordKind Token::getObjCKeywordID() const {
50 IdentifierInfo *specId = getIdentifierInfo();
51 return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
52}
53
Chris Lattner7208a4b2007-12-13 01:59:49 +000054
Chris Lattneraa9bdf12007-10-07 08:47:24 +000055//===----------------------------------------------------------------------===//
56// Lexer Class Implementation
57//===----------------------------------------------------------------------===//
58
59
Chris Lattner342dccb2007-10-17 20:41:00 +000060/// Lexer constructor - Create a new lexer object for the specified buffer
61/// with the specified preprocessor managing the lexing process. This lexer
62/// assumes that the associated file buffer and Preprocessor objects will
63/// outlive it, so it doesn't take ownership of either of them.
Chris Lattner4b009652007-07-25 00:24:17 +000064Lexer::Lexer(SourceLocation fileloc, Preprocessor &pp,
65 const char *BufStart, const char *BufEnd)
Chris Lattner342dccb2007-10-17 20:41:00 +000066 : FileLoc(fileloc), PP(&pp), Features(pp.getLangOptions()) {
Chris Lattner4b009652007-07-25 00:24:17 +000067
Chris Lattner342dccb2007-10-17 20:41:00 +000068 SourceManager &SourceMgr = PP->getSourceManager();
Chris Lattner4b009652007-07-25 00:24:17 +000069 unsigned InputFileID = SourceMgr.getPhysicalLoc(FileLoc).getFileID();
70 const llvm::MemoryBuffer *InputFile = SourceMgr.getBuffer(InputFileID);
71
72 Is_PragmaLexer = false;
Chris Lattner4b009652007-07-25 00:24:17 +000073 InitCharacterInfo();
74
75 // BufferStart must always be InputFile->getBufferStart().
76 BufferStart = InputFile->getBufferStart();
77
78 // BufferPtr and BufferEnd can start out somewhere inside the current buffer.
79 // If unspecified, they starts at the start/end of the buffer.
80 BufferPtr = BufStart ? BufStart : BufferStart;
81 BufferEnd = BufEnd ? BufEnd : InputFile->getBufferEnd();
82
83 assert(BufferEnd[0] == 0 &&
84 "We assume that the input buffer has a null character at the end"
85 " to simplify lexing!");
86
87 // Start of the file is a start of line.
88 IsAtStartOfLine = true;
89
90 // We are not after parsing a #.
91 ParsingPreprocessorDirective = false;
92
93 // We are not after parsing #include.
94 ParsingFilename = false;
95
96 // We are not in raw mode. Raw mode disables diagnostics and interpretation
97 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
98 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
99 // or otherwise skipping over tokens.
100 LexingRawMode = false;
101
102 // Default to keeping comments if requested.
Chris Lattner342dccb2007-10-17 20:41:00 +0000103 KeepCommentMode = PP->getCommentRetentionState();
Chris Lattner4b009652007-07-25 00:24:17 +0000104}
105
Chris Lattner342dccb2007-10-17 20:41:00 +0000106/// Lexer constructor - Create a new raw lexer object. This object is only
107/// suitable for calls to 'LexRawToken'. This lexer assumes that the
108/// associated file buffer will outlive it, so it doesn't take ownership of
109/// either of them.
110Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
111 const char *BufStart, const char *BufEnd)
112 : FileLoc(fileloc), PP(0), Features(features) {
113 Is_PragmaLexer = false;
114 InitCharacterInfo();
115
116 BufferStart = BufStart;
117 BufferPtr = BufStart;
118 BufferEnd = BufEnd;
119
120 assert(BufferEnd[0] == 0 &&
121 "We assume that the input buffer has a null character at the end"
122 " to simplify lexing!");
123
124 // Start of the file is a start of line.
125 IsAtStartOfLine = true;
126
127 // We are not after parsing a #.
128 ParsingPreprocessorDirective = false;
129
130 // We are not after parsing #include.
131 ParsingFilename = false;
132
133 // We *are* in raw mode.
134 LexingRawMode = true;
135
136 // Never keep comments in raw mode.
137 KeepCommentMode = false;
138}
139
140
Chris Lattner4b009652007-07-25 00:24:17 +0000141/// Stringify - Convert the specified string into a C string, with surrounding
142/// ""'s, and with escaped \ and " characters.
143std::string Lexer::Stringify(const std::string &Str, bool Charify) {
144 std::string Result = Str;
145 char Quote = Charify ? '\'' : '"';
146 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
147 if (Result[i] == '\\' || Result[i] == Quote) {
148 Result.insert(Result.begin()+i, '\\');
149 ++i; ++e;
150 }
151 }
152 return Result;
153}
154
155/// Stringify - Convert the specified string into a C string by escaping '\'
156/// and " characters. This does not add surrounding ""'s to the string.
157void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
158 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
159 if (Str[i] == '\\' || Str[i] == '"') {
160 Str.insert(Str.begin()+i, '\\');
161 ++i; ++e;
162 }
163 }
164}
165
166
Chris Lattner761d76b2007-10-17 21:18:47 +0000167/// MeasureTokenLength - Relex the token at the specified location and return
168/// its length in bytes in the input file. If the token needs cleaning (e.g.
169/// includes a trigraph or an escaped newline) then this count includes bytes
170/// that are part of that.
171unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
172 const SourceManager &SM) {
173 // If this comes from a macro expansion, we really do want the macro name, not
174 // the token this macro expanded to.
175 Loc = SM.getLogicalLoc(Loc);
176
177 const char *StrData = SM.getCharacterData(Loc);
178
179 // TODO: this could be special cased for common tokens like identifiers, ')',
180 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
181 // all obviously single-char tokens. This could use
182 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
183 // something.
184
185
186 const char *BufEnd = SM.getBufferData(Loc.getFileID()).second;
187
188 // Create a langops struct and enable trigraphs. This is sufficient for
189 // measuring tokens.
190 LangOptions LangOpts;
191 LangOpts.Trigraphs = true;
192
193 // Create a lexer starting at the beginning of this token.
194 Lexer TheLexer(Loc, LangOpts, StrData, BufEnd);
195 Token TheTok;
196 TheLexer.LexRawToken(TheTok);
197 return TheTok.getLength();
198}
199
Chris Lattner4b009652007-07-25 00:24:17 +0000200//===----------------------------------------------------------------------===//
201// Character information.
202//===----------------------------------------------------------------------===//
203
204static unsigned char CharInfo[256];
205
206enum {
207 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
208 CHAR_VERT_WS = 0x02, // '\r', '\n'
209 CHAR_LETTER = 0x04, // a-z,A-Z
210 CHAR_NUMBER = 0x08, // 0-9
211 CHAR_UNDER = 0x10, // _
212 CHAR_PERIOD = 0x20 // .
213};
214
215static void InitCharacterInfo() {
216 static bool isInited = false;
217 if (isInited) return;
218 isInited = true;
219
220 // Intiialize the CharInfo table.
221 // TODO: statically initialize this.
222 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
223 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
224 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
225
226 CharInfo[(int)'_'] = CHAR_UNDER;
227 CharInfo[(int)'.'] = CHAR_PERIOD;
228 for (unsigned i = 'a'; i <= 'z'; ++i)
229 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
230 for (unsigned i = '0'; i <= '9'; ++i)
231 CharInfo[i] = CHAR_NUMBER;
232}
233
234/// isIdentifierBody - Return true if this is the body character of an
235/// identifier, which is [a-zA-Z0-9_].
236static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiserc62f6b92007-10-18 12:47:01 +0000237 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Chris Lattner4b009652007-07-25 00:24:17 +0000238}
239
240/// isHorizontalWhitespace - Return true if this character is horizontal
241/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
242static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiserc62f6b92007-10-18 12:47:01 +0000243 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Chris Lattner4b009652007-07-25 00:24:17 +0000244}
245
246/// isWhitespace - Return true if this character is horizontal or vertical
247/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
248/// for '\0'.
249static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiserc62f6b92007-10-18 12:47:01 +0000250 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Chris Lattner4b009652007-07-25 00:24:17 +0000251}
252
253/// isNumberBody - Return true if this is the body character of an
254/// preprocessing number, which is [a-zA-Z0-9_.].
255static inline bool isNumberBody(unsigned char c) {
Hartmut Kaiserc62f6b92007-10-18 12:47:01 +0000256 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
257 true : false;
Chris Lattner4b009652007-07-25 00:24:17 +0000258}
259
260
261//===----------------------------------------------------------------------===//
262// Diagnostics forwarding code.
263//===----------------------------------------------------------------------===//
264
265/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
266/// lexer buffer was all instantiated at a single point, perform the mapping.
267/// This is currently only used for _Pragma implementation, so it is the slow
268/// path of the hot getSourceLocation method. Do not allow it to be inlined.
269static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
270 SourceLocation FileLoc,
271 unsigned CharNo) DISABLE_INLINE;
272static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
273 SourceLocation FileLoc,
274 unsigned CharNo) {
275 // Otherwise, we're lexing "mapped tokens". This is used for things like
276 // _Pragma handling. Combine the instantiation location of FileLoc with the
277 // physical location.
278 SourceManager &SourceMgr = PP.getSourceManager();
279
280 // Create a new SLoc which is expanded from logical(FileLoc) but whose
281 // characters come from phys(FileLoc)+Offset.
282 SourceLocation VirtLoc = SourceMgr.getLogicalLoc(FileLoc);
283 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(FileLoc);
284 PhysLoc = SourceLocation::getFileLoc(PhysLoc.getFileID(), CharNo);
285 return SourceMgr.getInstantiationLoc(PhysLoc, VirtLoc);
286}
287
288/// getSourceLocation - Return a source location identifier for the specified
289/// offset in the current file.
290SourceLocation Lexer::getSourceLocation(const char *Loc) const {
291 assert(Loc >= BufferStart && Loc <= BufferEnd &&
292 "Location out of range for this buffer!");
293
294 // In the normal case, we're just lexing from a simple file buffer, return
295 // the file id from FileLoc with the offset specified.
296 unsigned CharNo = Loc-BufferStart;
297 if (FileLoc.isFileID())
298 return SourceLocation::getFileLoc(FileLoc.getFileID(), CharNo);
299
Chris Lattner342dccb2007-10-17 20:41:00 +0000300 assert(PP && "This doesn't work on raw lexers");
301 return GetMappedTokenLoc(*PP, FileLoc, CharNo);
Chris Lattner4b009652007-07-25 00:24:17 +0000302}
303
304/// Diag - Forwarding function for diagnostics. This translate a source
305/// position in the current buffer into a SourceLocation object for rendering.
306void Lexer::Diag(const char *Loc, unsigned DiagID,
307 const std::string &Msg) const {
Chris Lattner4478db92007-11-30 22:53:43 +0000308 if (LexingRawMode && Diagnostic::isBuiltinNoteWarningOrExtension(DiagID))
Chris Lattner4b009652007-07-25 00:24:17 +0000309 return;
Chris Lattner342dccb2007-10-17 20:41:00 +0000310 PP->Diag(getSourceLocation(Loc), DiagID, Msg);
Chris Lattner4b009652007-07-25 00:24:17 +0000311}
312void Lexer::Diag(SourceLocation Loc, unsigned DiagID,
313 const std::string &Msg) const {
Chris Lattner4478db92007-11-30 22:53:43 +0000314 if (LexingRawMode && Diagnostic::isBuiltinNoteWarningOrExtension(DiagID))
Chris Lattner4b009652007-07-25 00:24:17 +0000315 return;
Chris Lattner342dccb2007-10-17 20:41:00 +0000316 PP->Diag(Loc, DiagID, Msg);
Chris Lattner4b009652007-07-25 00:24:17 +0000317}
318
319
320//===----------------------------------------------------------------------===//
321// Trigraph and Escaped Newline Handling Code.
322//===----------------------------------------------------------------------===//
323
324/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
325/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
326static char GetTrigraphCharForLetter(char Letter) {
327 switch (Letter) {
328 default: return 0;
329 case '=': return '#';
330 case ')': return ']';
331 case '(': return '[';
332 case '!': return '|';
333 case '\'': return '^';
334 case '>': return '}';
335 case '/': return '\\';
336 case '<': return '{';
337 case '-': return '~';
338 }
339}
340
341/// DecodeTrigraphChar - If the specified character is a legal trigraph when
342/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
343/// return the result character. Finally, emit a warning about trigraph use
344/// whether trigraphs are enabled or not.
345static char DecodeTrigraphChar(const char *CP, Lexer *L) {
346 char Res = GetTrigraphCharForLetter(*CP);
347 if (Res && L) {
348 if (!L->getFeatures().Trigraphs) {
349 L->Diag(CP-2, diag::trigraph_ignored);
350 return 0;
351 } else {
352 L->Diag(CP-2, diag::trigraph_converted, std::string()+Res);
353 }
354 }
355 return Res;
356}
357
358/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
359/// get its size, and return it. This is tricky in several cases:
360/// 1. If currently at the start of a trigraph, we warn about the trigraph,
361/// then either return the trigraph (skipping 3 chars) or the '?',
362/// depending on whether trigraphs are enabled or not.
363/// 2. If this is an escaped newline (potentially with whitespace between
364/// the backslash and newline), implicitly skip the newline and return
365/// the char after it.
366/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
367///
368/// This handles the slow/uncommon case of the getCharAndSize method. Here we
369/// know that we can accumulate into Size, and that we have already incremented
370/// Ptr by Size bytes.
371///
372/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
373/// be updated to match.
374///
375char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
376 Token *Tok) {
377 // If we have a slash, look for an escaped newline.
378 if (Ptr[0] == '\\') {
379 ++Size;
380 ++Ptr;
381Slash:
382 // Common case, backslash-char where the char is not whitespace.
383 if (!isWhitespace(Ptr[0])) return '\\';
384
385 // See if we have optional whitespace characters followed by a newline.
386 {
387 unsigned SizeTmp = 0;
388 do {
389 ++SizeTmp;
390 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
391 // Remember that this token needs to be cleaned.
392 if (Tok) Tok->setFlag(Token::NeedsCleaning);
393
394 // Warn if there was whitespace between the backslash and newline.
395 if (SizeTmp != 1 && Tok)
396 Diag(Ptr, diag::backslash_newline_space);
397
398 // If this is a \r\n or \n\r, skip the newlines.
399 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
400 Ptr[SizeTmp-1] != Ptr[SizeTmp])
401 ++SizeTmp;
402
403 // Found backslash<whitespace><newline>. Parse the char after it.
404 Size += SizeTmp;
405 Ptr += SizeTmp;
406 // Use slow version to accumulate a correct size field.
407 return getCharAndSizeSlow(Ptr, Size, Tok);
408 }
409 } while (isWhitespace(Ptr[SizeTmp]));
410 }
411
412 // Otherwise, this is not an escaped newline, just return the slash.
413 return '\\';
414 }
415
416 // If this is a trigraph, process it.
417 if (Ptr[0] == '?' && Ptr[1] == '?') {
418 // If this is actually a legal trigraph (not something like "??x"), emit
419 // a trigraph warning. If so, and if trigraphs are enabled, return it.
420 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
421 // Remember that this token needs to be cleaned.
422 if (Tok) Tok->setFlag(Token::NeedsCleaning);
423
424 Ptr += 3;
425 Size += 3;
426 if (C == '\\') goto Slash;
427 return C;
428 }
429 }
430
431 // If this is neither, return a single character.
432 ++Size;
433 return *Ptr;
434}
435
436
437/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
438/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
439/// and that we have already incremented Ptr by Size bytes.
440///
441/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
442/// be updated to match.
443char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
444 const LangOptions &Features) {
445 // If we have a slash, look for an escaped newline.
446 if (Ptr[0] == '\\') {
447 ++Size;
448 ++Ptr;
449Slash:
450 // Common case, backslash-char where the char is not whitespace.
451 if (!isWhitespace(Ptr[0])) return '\\';
452
453 // See if we have optional whitespace characters followed by a newline.
454 {
455 unsigned SizeTmp = 0;
456 do {
457 ++SizeTmp;
458 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
459
460 // If this is a \r\n or \n\r, skip the newlines.
461 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
462 Ptr[SizeTmp-1] != Ptr[SizeTmp])
463 ++SizeTmp;
464
465 // Found backslash<whitespace><newline>. Parse the char after it.
466 Size += SizeTmp;
467 Ptr += SizeTmp;
468
469 // Use slow version to accumulate a correct size field.
470 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
471 }
472 } while (isWhitespace(Ptr[SizeTmp]));
473 }
474
475 // Otherwise, this is not an escaped newline, just return the slash.
476 return '\\';
477 }
478
479 // If this is a trigraph, process it.
480 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
481 // If this is actually a legal trigraph (not something like "??x"), return
482 // it.
483 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
484 Ptr += 3;
485 Size += 3;
486 if (C == '\\') goto Slash;
487 return C;
488 }
489 }
490
491 // If this is neither, return a single character.
492 ++Size;
493 return *Ptr;
494}
495
496//===----------------------------------------------------------------------===//
497// Helper methods for lexing.
498//===----------------------------------------------------------------------===//
499
500void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
501 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
502 unsigned Size;
503 unsigned char C = *CurPtr++;
504 while (isIdentifierBody(C)) {
505 C = *CurPtr++;
506 }
507 --CurPtr; // Back up over the skipped character.
508
509 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
510 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
511 // FIXME: UCNs.
512 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
513FinishIdentifier:
514 const char *IdStart = BufferPtr;
515 FormTokenWithChars(Result, CurPtr);
516 Result.setKind(tok::identifier);
517
518 // If we are in raw mode, return this identifier raw. There is no need to
519 // look up identifier information or attempt to macro expand it.
520 if (LexingRawMode) return;
521
522 // Fill in Result.IdentifierInfo, looking up the identifier in the
523 // identifier table.
Chris Lattner342dccb2007-10-17 20:41:00 +0000524 PP->LookUpIdentifierInfo(Result, IdStart);
Chris Lattner4b009652007-07-25 00:24:17 +0000525
526 // Finally, now that we know we have an identifier, pass this off to the
527 // preprocessor, which may macro expand it or something.
Chris Lattner342dccb2007-10-17 20:41:00 +0000528 return PP->HandleIdentifier(Result);
Chris Lattner4b009652007-07-25 00:24:17 +0000529 }
530
531 // Otherwise, $,\,? in identifier found. Enter slower path.
532
533 C = getCharAndSize(CurPtr, Size);
534 while (1) {
535 if (C == '$') {
536 // If we hit a $ and they are not supported in identifiers, we are done.
537 if (!Features.DollarIdents) goto FinishIdentifier;
538
539 // Otherwise, emit a diagnostic and continue.
540 Diag(CurPtr, diag::ext_dollar_in_identifier);
541 CurPtr = ConsumeChar(CurPtr, Size, Result);
542 C = getCharAndSize(CurPtr, Size);
543 continue;
544 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
545 // Found end of identifier.
546 goto FinishIdentifier;
547 }
548
549 // Otherwise, this character is good, consume it.
550 CurPtr = ConsumeChar(CurPtr, Size, Result);
551
552 C = getCharAndSize(CurPtr, Size);
553 while (isIdentifierBody(C)) { // FIXME: UCNs.
554 CurPtr = ConsumeChar(CurPtr, Size, Result);
555 C = getCharAndSize(CurPtr, Size);
556 }
557 }
558}
559
560
Nate Begeman937cac72008-04-14 02:26:39 +0000561/// LexNumericConstant - Lex the remainder of a integer or floating point
Chris Lattner4b009652007-07-25 00:24:17 +0000562/// constant. From[-1] is the first character lexed. Return the end of the
563/// constant.
564void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
565 unsigned Size;
566 char C = getCharAndSize(CurPtr, Size);
567 char PrevCh = 0;
568 while (isNumberBody(C)) { // FIXME: UCNs?
569 CurPtr = ConsumeChar(CurPtr, Size, Result);
570 PrevCh = C;
571 C = getCharAndSize(CurPtr, Size);
572 }
573
574 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
575 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
576 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
577
578 // If we have a hex FP constant, continue.
579 if (Features.HexFloats &&
580 (C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p'))
581 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
582
583 Result.setKind(tok::numeric_constant);
584
585 // Update the location of token as well as BufferPtr.
586 FormTokenWithChars(Result, CurPtr);
587}
588
589/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
590/// either " or L".
591void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide){
592 const char *NulCharacter = 0; // Does this string contain the \0 character?
593
594 char C = getAndAdvanceChar(CurPtr, Result);
595 while (C != '"') {
596 // Skip escaped characters.
597 if (C == '\\') {
598 // Skip the escaped character.
599 C = getAndAdvanceChar(CurPtr, Result);
600 } else if (C == '\n' || C == '\r' || // Newline.
601 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
602 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
603 Result.setKind(tok::unknown);
604 FormTokenWithChars(Result, CurPtr-1);
605 return;
606 } else if (C == 0) {
607 NulCharacter = CurPtr-1;
608 }
609 C = getAndAdvanceChar(CurPtr, Result);
610 }
611
612 // If a nul character existed in the string, warn about it.
613 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
614
615 Result.setKind(Wide ? tok::wide_string_literal : tok::string_literal);
616
617 // Update the location of the token as well as the BufferPtr instance var.
618 FormTokenWithChars(Result, CurPtr);
619}
620
621/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
622/// after having lexed the '<' character. This is used for #include filenames.
623void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
624 const char *NulCharacter = 0; // Does this string contain the \0 character?
625
626 char C = getAndAdvanceChar(CurPtr, Result);
627 while (C != '>') {
628 // Skip escaped characters.
629 if (C == '\\') {
630 // Skip the escaped character.
631 C = getAndAdvanceChar(CurPtr, Result);
632 } else if (C == '\n' || C == '\r' || // Newline.
633 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
634 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
635 Result.setKind(tok::unknown);
636 FormTokenWithChars(Result, CurPtr-1);
637 return;
638 } else if (C == 0) {
639 NulCharacter = CurPtr-1;
640 }
641 C = getAndAdvanceChar(CurPtr, Result);
642 }
643
644 // If a nul character existed in the string, warn about it.
645 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
646
647 Result.setKind(tok::angle_string_literal);
648
649 // Update the location of token as well as BufferPtr.
650 FormTokenWithChars(Result, CurPtr);
651}
652
653
654/// LexCharConstant - Lex the remainder of a character constant, after having
655/// lexed either ' or L'.
656void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
657 const char *NulCharacter = 0; // Does this character contain the \0 character?
658
659 // Handle the common case of 'x' and '\y' efficiently.
660 char C = getAndAdvanceChar(CurPtr, Result);
661 if (C == '\'') {
662 if (!LexingRawMode) Diag(BufferPtr, diag::err_empty_character);
663 Result.setKind(tok::unknown);
664 FormTokenWithChars(Result, CurPtr);
665 return;
666 } else if (C == '\\') {
667 // Skip the escaped character.
668 // FIXME: UCN's.
669 C = getAndAdvanceChar(CurPtr, Result);
670 }
671
672 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
673 ++CurPtr;
674 } else {
675 // Fall back on generic code for embedded nulls, newlines, wide chars.
676 do {
677 // Skip escaped characters.
678 if (C == '\\') {
679 // Skip the escaped character.
680 C = getAndAdvanceChar(CurPtr, Result);
681 } else if (C == '\n' || C == '\r' || // Newline.
682 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
683 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_char);
684 Result.setKind(tok::unknown);
685 FormTokenWithChars(Result, CurPtr-1);
686 return;
687 } else if (C == 0) {
688 NulCharacter = CurPtr-1;
689 }
690 C = getAndAdvanceChar(CurPtr, Result);
691 } while (C != '\'');
692 }
693
694 if (NulCharacter) Diag(NulCharacter, diag::null_in_char);
695
696 Result.setKind(tok::char_constant);
697
698 // Update the location of token as well as BufferPtr.
699 FormTokenWithChars(Result, CurPtr);
700}
701
702/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
703/// Update BufferPtr to point to the next non-whitespace character and return.
704void Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
705 // Whitespace - Skip it, then return the token after the whitespace.
706 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
707 while (1) {
708 // Skip horizontal whitespace very aggressively.
709 while (isHorizontalWhitespace(Char))
710 Char = *++CurPtr;
711
712 // Otherwise if we something other than whitespace, we're done.
713 if (Char != '\n' && Char != '\r')
714 break;
715
716 if (ParsingPreprocessorDirective) {
717 // End of preprocessor directive line, let LexTokenInternal handle this.
718 BufferPtr = CurPtr;
719 return;
720 }
721
722 // ok, but handle newline.
723 // The returned token is at the start of the line.
724 Result.setFlag(Token::StartOfLine);
725 // No leading whitespace seen so far.
726 Result.clearFlag(Token::LeadingSpace);
727 Char = *++CurPtr;
728 }
729
730 // If this isn't immediately after a newline, there is leading space.
731 char PrevChar = CurPtr[-1];
732 if (PrevChar != '\n' && PrevChar != '\r')
733 Result.setFlag(Token::LeadingSpace);
734
735 BufferPtr = CurPtr;
736}
737
738// SkipBCPLComment - We have just read the // characters from input. Skip until
739// we find the newline character thats terminate the comment. Then update
740/// BufferPtr and return.
741bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
742 // If BCPL comments aren't explicitly enabled for this language, emit an
743 // extension warning.
744 if (!Features.BCPLComment) {
745 Diag(BufferPtr, diag::ext_bcpl_comment);
746
747 // Mark them enabled so we only emit one warning for this translation
748 // unit.
749 Features.BCPLComment = true;
750 }
751
752 // Scan over the body of the comment. The common case, when scanning, is that
753 // the comment contains normal ascii characters with nothing interesting in
754 // them. As such, optimize for this case with the inner loop.
755 char C;
756 do {
757 C = *CurPtr;
758 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
759 // If we find a \n character, scan backwards, checking to see if it's an
760 // escaped newline, like we do for block comments.
761
762 // Skip over characters in the fast loop.
763 while (C != 0 && // Potentially EOF.
764 C != '\\' && // Potentially escaped newline.
765 C != '?' && // Potentially trigraph.
766 C != '\n' && C != '\r') // Newline or DOS-style newline.
767 C = *++CurPtr;
768
769 // If this is a newline, we're done.
770 if (C == '\n' || C == '\r')
771 break; // Found the newline? Break out!
772
773 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
774 // properly decode the character.
775 const char *OldPtr = CurPtr;
776 C = getAndAdvanceChar(CurPtr, Result);
777
778 // If we read multiple characters, and one of those characters was a \r or
779 // \n, then we had an escaped newline within the comment. Emit diagnostic
780 // unless the next line is also a // comment.
781 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
782 for (; OldPtr != CurPtr; ++OldPtr)
783 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
784 // Okay, we found a // comment that ends in a newline, if the next
785 // line is also a // comment, but has spaces, don't emit a diagnostic.
786 if (isspace(C)) {
787 const char *ForwardPtr = CurPtr;
788 while (isspace(*ForwardPtr)) // Skip whitespace.
789 ++ForwardPtr;
790 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
791 break;
792 }
793
794 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
795 break;
796 }
797 }
798
799 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
800 } while (C != '\n' && C != '\r');
801
802 // Found but did not consume the newline.
803
804 // If we are returning comments as tokens, return this comment as a token.
805 if (KeepCommentMode)
806 return SaveBCPLComment(Result, CurPtr);
807
808 // If we are inside a preprocessor directive and we see the end of line,
809 // return immediately, so that the lexer can return this as an EOM token.
810 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
811 BufferPtr = CurPtr;
812 return true;
813 }
814
815 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner43d38202008-10-12 00:23:07 +0000816 // \r\n sequence. This is an efficiency hack (because we know the \n can't
817 // contribute to another token), it isn't needed for correctness.
Chris Lattner4b009652007-07-25 00:24:17 +0000818 ++CurPtr;
819
820 // The next returned token is at the start of the line.
821 Result.setFlag(Token::StartOfLine);
822 // No leading whitespace seen so far.
823 Result.clearFlag(Token::LeadingSpace);
824 BufferPtr = CurPtr;
825 return true;
826}
827
828/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
829/// an appropriate way and return it.
830bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
831 Result.setKind(tok::comment);
832 FormTokenWithChars(Result, CurPtr);
833
834 // If this BCPL-style comment is in a macro definition, transmogrify it into
835 // a C-style block comment.
836 if (ParsingPreprocessorDirective) {
Chris Lattner342dccb2007-10-17 20:41:00 +0000837 std::string Spelling = PP->getSpelling(Result);
Chris Lattner4b009652007-07-25 00:24:17 +0000838 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
839 Spelling[1] = '*'; // Change prefix to "/*".
840 Spelling += "*/"; // add suffix.
841
Chris Lattner342dccb2007-10-17 20:41:00 +0000842 Result.setLocation(PP->CreateString(&Spelling[0], Spelling.size(),
843 Result.getLocation()));
Chris Lattner4b009652007-07-25 00:24:17 +0000844 Result.setLength(Spelling.size());
845 }
846 return false;
847}
848
849/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
850/// character (either \n or \r) is part of an escaped newline sequence. Issue a
851/// diagnostic if so. We know that the is inside of a block comment.
852static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
853 Lexer *L) {
854 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
855
856 // Back up off the newline.
857 --CurPtr;
858
859 // If this is a two-character newline sequence, skip the other character.
860 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
861 // \n\n or \r\r -> not escaped newline.
862 if (CurPtr[0] == CurPtr[1])
863 return false;
864 // \n\r or \r\n -> skip the newline.
865 --CurPtr;
866 }
867
868 // If we have horizontal whitespace, skip over it. We allow whitespace
869 // between the slash and newline.
870 bool HasSpace = false;
871 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
872 --CurPtr;
873 HasSpace = true;
874 }
875
876 // If we have a slash, we know this is an escaped newline.
877 if (*CurPtr == '\\') {
878 if (CurPtr[-1] != '*') return false;
879 } else {
880 // It isn't a slash, is it the ?? / trigraph?
881 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
882 CurPtr[-3] != '*')
883 return false;
884
885 // This is the trigraph ending the comment. Emit a stern warning!
886 CurPtr -= 2;
887
888 // If no trigraphs are enabled, warn that we ignored this trigraph and
889 // ignore this * character.
890 if (!L->getFeatures().Trigraphs) {
891 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
892 return false;
893 }
894 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
895 }
896
897 // Warn about having an escaped newline between the */ characters.
898 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
899
900 // If there was space between the backslash and newline, warn about it.
901 if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space);
902
903 return true;
904}
905
906#ifdef __SSE2__
907#include <emmintrin.h>
908#elif __ALTIVEC__
909#include <altivec.h>
910#undef bool
911#endif
912
913/// SkipBlockComment - We have just read the /* characters from input. Read
914/// until we find the */ characters that terminate the comment. Note that we
915/// don't bother decoding trigraphs or escaped newlines in block comments,
916/// because they cannot cause the comment to end. The only thing that can
917/// happen is the comment could end with an escaped newline between the */ end
918/// of comment.
919bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
920 // Scan one character past where we should, looking for a '/' character. Once
921 // we find it, check to see if it was preceeded by a *. This common
922 // optimization helps people who like to put a lot of * characters in their
923 // comments.
924
925 // The first character we get with newlines and trigraphs skipped to handle
926 // the degenerate /*/ case below correctly if the * has an escaped newline
927 // after it.
928 unsigned CharSize;
929 unsigned char C = getCharAndSize(CurPtr, CharSize);
930 CurPtr += CharSize;
931 if (C == 0 && CurPtr == BufferEnd+1) {
932 Diag(BufferPtr, diag::err_unterminated_block_comment);
933 BufferPtr = CurPtr-1;
934 return true;
935 }
936
937 // Check to see if the first character after the '/*' is another /. If so,
938 // then this slash does not end the block comment, it is part of it.
939 if (C == '/')
940 C = *CurPtr++;
941
942 while (1) {
943 // Skip over all non-interesting characters until we find end of buffer or a
944 // (probably ending) '/' character.
945 if (CurPtr + 24 < BufferEnd) {
946 // While not aligned to a 16-byte boundary.
947 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
948 C = *CurPtr++;
949
950 if (C == '/') goto FoundSlash;
951
952#ifdef __SSE2__
953 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
954 '/', '/', '/', '/', '/', '/', '/', '/');
955 while (CurPtr+16 <= BufferEnd &&
956 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
957 CurPtr += 16;
958#elif __ALTIVEC__
959 __vector unsigned char Slashes = {
960 '/', '/', '/', '/', '/', '/', '/', '/',
961 '/', '/', '/', '/', '/', '/', '/', '/'
962 };
963 while (CurPtr+16 <= BufferEnd &&
964 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
965 CurPtr += 16;
966#else
967 // Scan for '/' quickly. Many block comments are very large.
968 while (CurPtr[0] != '/' &&
969 CurPtr[1] != '/' &&
970 CurPtr[2] != '/' &&
971 CurPtr[3] != '/' &&
972 CurPtr+4 < BufferEnd) {
973 CurPtr += 4;
974 }
975#endif
976
977 // It has to be one of the bytes scanned, increment to it and read one.
978 C = *CurPtr++;
979 }
980
981 // Loop to scan the remainder.
982 while (C != '/' && C != '\0')
983 C = *CurPtr++;
984
985 FoundSlash:
986 if (C == '/') {
987 if (CurPtr[-2] == '*') // We found the final */. We're done!
988 break;
989
990 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
991 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
992 // We found the final */, though it had an escaped newline between the
993 // * and /. We're done!
994 break;
995 }
996 }
997 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
998 // If this is a /* inside of the comment, emit a warning. Don't do this
999 // if this is a /*/, which will end the comment. This misses cases with
1000 // embedded escaped newlines, but oh well.
1001 Diag(CurPtr-1, diag::nested_block_comment);
1002 }
1003 } else if (C == 0 && CurPtr == BufferEnd+1) {
1004 Diag(BufferPtr, diag::err_unterminated_block_comment);
1005 // Note: the user probably forgot a */. We could continue immediately
1006 // after the /*, but this would involve lexing a lot of what really is the
1007 // comment, which surely would confuse the parser.
1008 BufferPtr = CurPtr-1;
1009 return true;
1010 }
1011 C = *CurPtr++;
1012 }
1013
1014 // If we are returning comments as tokens, return this comment as a token.
1015 if (KeepCommentMode) {
1016 Result.setKind(tok::comment);
1017 FormTokenWithChars(Result, CurPtr);
1018 return false;
1019 }
1020
1021 // It is common for the tokens immediately after a /**/ comment to be
1022 // whitespace. Instead of going through the big switch, handle it
1023 // efficiently now.
1024 if (isHorizontalWhitespace(*CurPtr)) {
1025 Result.setFlag(Token::LeadingSpace);
1026 SkipWhitespace(Result, CurPtr+1);
1027 return true;
1028 }
1029
1030 // Otherwise, just return so that the next character will be lexed as a token.
1031 BufferPtr = CurPtr;
1032 Result.setFlag(Token::LeadingSpace);
1033 return true;
1034}
1035
1036//===----------------------------------------------------------------------===//
1037// Primary Lexing Entry Points
1038//===----------------------------------------------------------------------===//
1039
1040/// LexIncludeFilename - After the preprocessor has parsed a #include, lex and
1041/// (potentially) macro expand the filename.
1042void Lexer::LexIncludeFilename(Token &FilenameTok) {
1043 assert(ParsingPreprocessorDirective &&
1044 ParsingFilename == false &&
1045 "Must be in a preprocessing directive!");
1046
1047 // We are now parsing a filename!
1048 ParsingFilename = true;
1049
1050 // Lex the filename.
1051 Lex(FilenameTok);
1052
1053 // We should have obtained the filename now.
1054 ParsingFilename = false;
1055
1056 // No filename?
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001057 if (FilenameTok.is(tok::eom))
Chris Lattner4b009652007-07-25 00:24:17 +00001058 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
1059}
1060
1061/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1062/// uninterpreted string. This switches the lexer out of directive mode.
1063std::string Lexer::ReadToEndOfLine() {
1064 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1065 "Must be in a preprocessing directive!");
1066 std::string Result;
1067 Token Tmp;
1068
1069 // CurPtr - Cache BufferPtr in an automatic variable.
1070 const char *CurPtr = BufferPtr;
1071 while (1) {
1072 char Char = getAndAdvanceChar(CurPtr, Tmp);
1073 switch (Char) {
1074 default:
1075 Result += Char;
1076 break;
1077 case 0: // Null.
1078 // Found end of file?
1079 if (CurPtr-1 != BufferEnd) {
1080 // Nope, normal character, continue.
1081 Result += Char;
1082 break;
1083 }
1084 // FALL THROUGH.
1085 case '\r':
1086 case '\n':
1087 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1088 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1089 BufferPtr = CurPtr-1;
1090
1091 // Next, lex the character, which should handle the EOM transition.
1092 Lex(Tmp);
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001093 assert(Tmp.is(tok::eom) && "Unexpected token!");
Chris Lattner4b009652007-07-25 00:24:17 +00001094
1095 // Finally, we're done, return the string we found.
1096 return Result;
1097 }
1098 }
1099}
1100
1101/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1102/// condition, reporting diagnostics and handling other edge cases as required.
1103/// This returns true if Result contains a token, false if PP.Lex should be
1104/// called again.
1105bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
1106 // If we hit the end of the file while parsing a preprocessor directive,
1107 // end the preprocessor directive first. The next token returned will
1108 // then be the end of file.
1109 if (ParsingPreprocessorDirective) {
1110 // Done parsing the "line".
1111 ParsingPreprocessorDirective = false;
1112 Result.setKind(tok::eom);
1113 // Update the location of token as well as BufferPtr.
1114 FormTokenWithChars(Result, CurPtr);
1115
1116 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner342dccb2007-10-17 20:41:00 +00001117 KeepCommentMode = PP->getCommentRetentionState();
Chris Lattner4b009652007-07-25 00:24:17 +00001118 return true; // Have a token.
1119 }
1120
1121 // If we are in raw mode, return this event as an EOF token. Let the caller
1122 // that put us in raw mode handle the event.
1123 if (LexingRawMode) {
1124 Result.startToken();
1125 BufferPtr = BufferEnd;
1126 FormTokenWithChars(Result, BufferEnd);
1127 Result.setKind(tok::eof);
1128 return true;
1129 }
1130
1131 // Otherwise, issue diagnostics for unterminated #if and missing newline.
1132
1133 // If we are in a #if directive, emit an error.
1134 while (!ConditionalStack.empty()) {
1135 Diag(ConditionalStack.back().IfLoc, diag::err_pp_unterminated_conditional);
1136 ConditionalStack.pop_back();
1137 }
1138
Chris Lattner5c337fa2008-04-12 05:54:25 +00001139 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1140 // a pedwarn.
1141 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Chris Lattner4b009652007-07-25 00:24:17 +00001142 Diag(BufferEnd, diag::ext_no_newline_eof);
1143
1144 BufferPtr = CurPtr;
1145
1146 // Finally, let the preprocessor handle this.
Chris Lattner342dccb2007-10-17 20:41:00 +00001147 return PP->HandleEndOfFile(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001148}
1149
1150/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1151/// the specified lexer will return a tok::l_paren token, 0 if it is something
1152/// else and 2 if there are no more tokens in the buffer controlled by the
1153/// lexer.
1154unsigned Lexer::isNextPPTokenLParen() {
1155 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1156
1157 // Switch to 'skipping' mode. This will ensure that we can lex a token
1158 // without emitting diagnostics, disables macro expansion, and will cause EOF
1159 // to return an EOF token instead of popping the include stack.
1160 LexingRawMode = true;
1161
1162 // Save state that can be changed while lexing so that we can restore it.
1163 const char *TmpBufferPtr = BufferPtr;
1164
1165 Token Tok;
1166 Tok.startToken();
1167 LexTokenInternal(Tok);
1168
1169 // Restore state that may have changed.
1170 BufferPtr = TmpBufferPtr;
1171
1172 // Restore the lexer back to non-skipping mode.
1173 LexingRawMode = false;
1174
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001175 if (Tok.is(tok::eof))
Chris Lattner4b009652007-07-25 00:24:17 +00001176 return 2;
Chris Lattnercb8e41c2007-10-09 18:02:16 +00001177 return Tok.is(tok::l_paren);
Chris Lattner4b009652007-07-25 00:24:17 +00001178}
1179
1180
1181/// LexTokenInternal - This implements a simple C family lexer. It is an
1182/// extremely performance critical piece of code. This assumes that the buffer
1183/// has a null character at the end of the file. Return true if an error
1184/// occurred and compilation should terminate, false if normal. This returns a
1185/// preprocessing token, not a normal token, as such, it is an internal
1186/// interface. It assumes that the Flags of result have been cleared before
1187/// calling this.
1188void Lexer::LexTokenInternal(Token &Result) {
1189LexNextToken:
1190 // New token, can't need cleaning yet.
1191 Result.clearFlag(Token::NeedsCleaning);
1192 Result.setIdentifierInfo(0);
1193
1194 // CurPtr - Cache BufferPtr in an automatic variable.
1195 const char *CurPtr = BufferPtr;
1196
1197 // Small amounts of horizontal whitespace is very common between tokens.
1198 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1199 ++CurPtr;
1200 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1201 ++CurPtr;
1202 BufferPtr = CurPtr;
1203 Result.setFlag(Token::LeadingSpace);
1204 }
1205
1206 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1207
1208 // Read a character, advancing over it.
1209 char Char = getAndAdvanceChar(CurPtr, Result);
1210 switch (Char) {
1211 case 0: // Null.
1212 // Found end of file?
1213 if (CurPtr-1 == BufferEnd) {
1214 // Read the PP instance variable into an automatic variable, because
1215 // LexEndOfFile will often delete 'this'.
Chris Lattner342dccb2007-10-17 20:41:00 +00001216 Preprocessor *PPCache = PP;
Chris Lattner4b009652007-07-25 00:24:17 +00001217 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1218 return; // Got a token to return.
Chris Lattner342dccb2007-10-17 20:41:00 +00001219 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1220 return PPCache->Lex(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001221 }
1222
1223 Diag(CurPtr-1, diag::null_in_file);
1224 Result.setFlag(Token::LeadingSpace);
1225 SkipWhitespace(Result, CurPtr);
1226 goto LexNextToken; // GCC isn't tail call eliminating.
1227 case '\n':
1228 case '\r':
1229 // If we are inside a preprocessor directive and we see the end of line,
1230 // we know we are done with the directive, so return an EOM token.
1231 if (ParsingPreprocessorDirective) {
1232 // Done parsing the "line".
1233 ParsingPreprocessorDirective = false;
1234
1235 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner342dccb2007-10-17 20:41:00 +00001236 KeepCommentMode = PP->getCommentRetentionState();
Chris Lattner4b009652007-07-25 00:24:17 +00001237
1238 // Since we consumed a newline, we are back at the start of a line.
1239 IsAtStartOfLine = true;
1240
1241 Result.setKind(tok::eom);
1242 break;
1243 }
1244 // The returned token is at the start of the line.
1245 Result.setFlag(Token::StartOfLine);
1246 // No leading whitespace seen so far.
1247 Result.clearFlag(Token::LeadingSpace);
1248 SkipWhitespace(Result, CurPtr);
1249 goto LexNextToken; // GCC isn't tail call eliminating.
1250 case ' ':
1251 case '\t':
1252 case '\f':
1253 case '\v':
1254 SkipHorizontalWhitespace:
1255 Result.setFlag(Token::LeadingSpace);
1256 SkipWhitespace(Result, CurPtr);
1257
1258 SkipIgnoredUnits:
1259 CurPtr = BufferPtr;
1260
1261 // If the next token is obviously a // or /* */ comment, skip it efficiently
1262 // too (without going through the big switch stmt).
1263 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !KeepCommentMode) {
1264 SkipBCPLComment(Result, CurPtr+2);
1265 goto SkipIgnoredUnits;
1266 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !KeepCommentMode) {
1267 SkipBlockComment(Result, CurPtr+2);
1268 goto SkipIgnoredUnits;
1269 } else if (isHorizontalWhitespace(*CurPtr)) {
1270 goto SkipHorizontalWhitespace;
1271 }
1272 goto LexNextToken; // GCC isn't tail call eliminating.
1273
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001274 // C99 6.4.4.1: Integer Constants.
1275 // C99 6.4.4.2: Floating Constants.
1276 case '0': case '1': case '2': case '3': case '4':
1277 case '5': case '6': case '7': case '8': case '9':
1278 // Notify MIOpt that we read a non-whitespace/non-comment token.
1279 MIOpt.ReadToken();
1280 return LexNumericConstant(Result, CurPtr);
1281
1282 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Chris Lattner4b009652007-07-25 00:24:17 +00001283 // Notify MIOpt that we read a non-whitespace/non-comment token.
1284 MIOpt.ReadToken();
1285 Char = getCharAndSize(CurPtr, SizeTmp);
1286
1287 // Wide string literal.
1288 if (Char == '"')
1289 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1290 true);
1291
1292 // Wide character constant.
1293 if (Char == '\'')
1294 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1295 // FALL THROUGH, treating L like the start of an identifier.
1296
1297 // C99 6.4.2: Identifiers.
1298 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1299 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1300 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1301 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1302 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1303 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1304 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1305 case 'v': case 'w': case 'x': case 'y': case 'z':
1306 case '_':
1307 // Notify MIOpt that we read a non-whitespace/non-comment token.
1308 MIOpt.ReadToken();
1309 return LexIdentifier(Result, CurPtr);
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001310
1311 case '$': // $ in identifiers.
1312 if (Features.DollarIdents) {
1313 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
1314 // Notify MIOpt that we read a non-whitespace/non-comment token.
1315 MIOpt.ReadToken();
1316 return LexIdentifier(Result, CurPtr);
1317 }
Chris Lattner4b009652007-07-25 00:24:17 +00001318
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001319 Result.setKind(tok::unknown);
1320 break;
Chris Lattner4b009652007-07-25 00:24:17 +00001321
1322 // C99 6.4.4: Character Constants.
1323 case '\'':
1324 // Notify MIOpt that we read a non-whitespace/non-comment token.
1325 MIOpt.ReadToken();
1326 return LexCharConstant(Result, CurPtr);
1327
1328 // C99 6.4.5: String Literals.
1329 case '"':
1330 // Notify MIOpt that we read a non-whitespace/non-comment token.
1331 MIOpt.ReadToken();
1332 return LexStringLiteral(Result, CurPtr, false);
1333
1334 // C99 6.4.6: Punctuators.
1335 case '?':
1336 Result.setKind(tok::question);
1337 break;
1338 case '[':
1339 Result.setKind(tok::l_square);
1340 break;
1341 case ']':
1342 Result.setKind(tok::r_square);
1343 break;
1344 case '(':
1345 Result.setKind(tok::l_paren);
1346 break;
1347 case ')':
1348 Result.setKind(tok::r_paren);
1349 break;
1350 case '{':
1351 Result.setKind(tok::l_brace);
1352 break;
1353 case '}':
1354 Result.setKind(tok::r_brace);
1355 break;
1356 case '.':
1357 Char = getCharAndSize(CurPtr, SizeTmp);
1358 if (Char >= '0' && Char <= '9') {
1359 // Notify MIOpt that we read a non-whitespace/non-comment token.
1360 MIOpt.ReadToken();
1361
1362 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1363 } else if (Features.CPlusPlus && Char == '*') {
1364 Result.setKind(tok::periodstar);
1365 CurPtr += SizeTmp;
1366 } else if (Char == '.' &&
1367 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
1368 Result.setKind(tok::ellipsis);
1369 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1370 SizeTmp2, Result);
1371 } else {
1372 Result.setKind(tok::period);
1373 }
1374 break;
1375 case '&':
1376 Char = getCharAndSize(CurPtr, SizeTmp);
1377 if (Char == '&') {
1378 Result.setKind(tok::ampamp);
1379 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1380 } else if (Char == '=') {
1381 Result.setKind(tok::ampequal);
1382 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1383 } else {
1384 Result.setKind(tok::amp);
1385 }
1386 break;
1387 case '*':
1388 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
1389 Result.setKind(tok::starequal);
1390 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1391 } else {
1392 Result.setKind(tok::star);
1393 }
1394 break;
1395 case '+':
1396 Char = getCharAndSize(CurPtr, SizeTmp);
1397 if (Char == '+') {
1398 Result.setKind(tok::plusplus);
1399 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1400 } else if (Char == '=') {
1401 Result.setKind(tok::plusequal);
1402 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1403 } else {
1404 Result.setKind(tok::plus);
1405 }
1406 break;
1407 case '-':
1408 Char = getCharAndSize(CurPtr, SizeTmp);
1409 if (Char == '-') {
1410 Result.setKind(tok::minusminus);
1411 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1412 } else if (Char == '>' && Features.CPlusPlus &&
1413 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {
1414 Result.setKind(tok::arrowstar); // C++ ->*
1415 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1416 SizeTmp2, Result);
1417 } else if (Char == '>') {
1418 Result.setKind(tok::arrow);
1419 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1420 } else if (Char == '=') {
1421 Result.setKind(tok::minusequal);
1422 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1423 } else {
1424 Result.setKind(tok::minus);
1425 }
1426 break;
1427 case '~':
1428 Result.setKind(tok::tilde);
1429 break;
1430 case '!':
1431 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
1432 Result.setKind(tok::exclaimequal);
1433 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1434 } else {
1435 Result.setKind(tok::exclaim);
1436 }
1437 break;
1438 case '/':
1439 // 6.4.9: Comments
1440 Char = getCharAndSize(CurPtr, SizeTmp);
1441 if (Char == '/') { // BCPL comment.
1442 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) {
1443 // It is common for the tokens immediately after a // comment to be
1444 // whitespace (indentation for the next line). Instead of going through
1445 // the big switch, handle it efficiently now.
1446 goto SkipIgnoredUnits;
1447 }
1448 return; // KeepCommentMode
1449 } else if (Char == '*') { // /**/ comment.
1450 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1451 goto LexNextToken; // GCC isn't tail call eliminating.
1452 return; // KeepCommentMode
1453 } else if (Char == '=') {
1454 Result.setKind(tok::slashequal);
1455 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1456 } else {
1457 Result.setKind(tok::slash);
1458 }
1459 break;
1460 case '%':
1461 Char = getCharAndSize(CurPtr, SizeTmp);
1462 if (Char == '=') {
1463 Result.setKind(tok::percentequal);
1464 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1465 } else if (Features.Digraphs && Char == '>') {
1466 Result.setKind(tok::r_brace); // '%>' -> '}'
1467 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1468 } else if (Features.Digraphs && Char == ':') {
1469 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1470 Char = getCharAndSize(CurPtr, SizeTmp);
1471 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
1472 Result.setKind(tok::hashhash); // '%:%:' -> '##'
1473 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1474 SizeTmp2, Result);
1475 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
1476 Result.setKind(tok::hashat);
1477 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1478 Diag(BufferPtr, diag::charize_microsoft_ext);
1479 } else {
1480 Result.setKind(tok::hash); // '%:' -> '#'
1481
1482 // We parsed a # character. If this occurs at the start of the line,
1483 // it's actually the start of a preprocessing directive. Callback to
1484 // the preprocessor to handle it.
1485 // FIXME: -fpreprocessed mode??
1486 if (Result.isAtStartOfLine() && !LexingRawMode) {
1487 BufferPtr = CurPtr;
Chris Lattner342dccb2007-10-17 20:41:00 +00001488 PP->HandleDirective(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001489
1490 // As an optimization, if the preprocessor didn't switch lexers, tail
1491 // recurse.
Chris Lattner342dccb2007-10-17 20:41:00 +00001492 if (PP->isCurrentLexer(this)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001493 // Start a new token. If this is a #include or something, the PP may
1494 // want us starting at the beginning of the line again. If so, set
1495 // the StartOfLine flag.
1496 if (IsAtStartOfLine) {
1497 Result.setFlag(Token::StartOfLine);
1498 IsAtStartOfLine = false;
1499 }
1500 goto LexNextToken; // GCC isn't tail call eliminating.
1501 }
1502
Chris Lattner342dccb2007-10-17 20:41:00 +00001503 return PP->Lex(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001504 }
1505 }
1506 } else {
1507 Result.setKind(tok::percent);
1508 }
1509 break;
1510 case '<':
1511 Char = getCharAndSize(CurPtr, SizeTmp);
1512 if (ParsingFilename) {
1513 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1514 } else if (Char == '<' &&
1515 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
1516 Result.setKind(tok::lesslessequal);
1517 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1518 SizeTmp2, Result);
1519 } else if (Char == '<') {
1520 Result.setKind(tok::lessless);
1521 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1522 } else if (Char == '=') {
1523 Result.setKind(tok::lessequal);
1524 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1525 } else if (Features.Digraphs && Char == ':') {
1526 Result.setKind(tok::l_square); // '<:' -> '['
1527 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner0297c762008-02-25 04:01:39 +00001528 } else if (Features.Digraphs && Char == '%') {
Chris Lattner4b009652007-07-25 00:24:17 +00001529 Result.setKind(tok::l_brace); // '<%' -> '{'
1530 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1531 } else {
1532 Result.setKind(tok::less);
1533 }
1534 break;
1535 case '>':
1536 Char = getCharAndSize(CurPtr, SizeTmp);
1537 if (Char == '=') {
1538 Result.setKind(tok::greaterequal);
1539 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1540 } else if (Char == '>' &&
1541 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
1542 Result.setKind(tok::greatergreaterequal);
1543 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1544 SizeTmp2, Result);
1545 } else if (Char == '>') {
1546 Result.setKind(tok::greatergreater);
1547 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1548 } else {
1549 Result.setKind(tok::greater);
1550 }
1551 break;
1552 case '^':
1553 Char = getCharAndSize(CurPtr, SizeTmp);
1554 if (Char == '=') {
1555 Result.setKind(tok::caretequal);
1556 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1557 } else {
1558 Result.setKind(tok::caret);
1559 }
1560 break;
1561 case '|':
1562 Char = getCharAndSize(CurPtr, SizeTmp);
1563 if (Char == '=') {
1564 Result.setKind(tok::pipeequal);
1565 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1566 } else if (Char == '|') {
1567 Result.setKind(tok::pipepipe);
1568 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1569 } else {
1570 Result.setKind(tok::pipe);
1571 }
1572 break;
1573 case ':':
1574 Char = getCharAndSize(CurPtr, SizeTmp);
1575 if (Features.Digraphs && Char == '>') {
1576 Result.setKind(tok::r_square); // ':>' -> ']'
1577 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1578 } else if (Features.CPlusPlus && Char == ':') {
1579 Result.setKind(tok::coloncolon);
1580 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1581 } else {
1582 Result.setKind(tok::colon);
1583 }
1584 break;
1585 case ';':
1586 Result.setKind(tok::semi);
1587 break;
1588 case '=':
1589 Char = getCharAndSize(CurPtr, SizeTmp);
1590 if (Char == '=') {
1591 Result.setKind(tok::equalequal);
1592 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1593 } else {
1594 Result.setKind(tok::equal);
1595 }
1596 break;
1597 case ',':
1598 Result.setKind(tok::comma);
1599 break;
1600 case '#':
1601 Char = getCharAndSize(CurPtr, SizeTmp);
1602 if (Char == '#') {
1603 Result.setKind(tok::hashhash);
1604 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1605 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
1606 Result.setKind(tok::hashat);
1607 Diag(BufferPtr, diag::charize_microsoft_ext);
1608 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1609 } else {
1610 Result.setKind(tok::hash);
1611 // We parsed a # character. If this occurs at the start of the line,
1612 // it's actually the start of a preprocessing directive. Callback to
1613 // the preprocessor to handle it.
1614 // FIXME: -fpreprocessed mode??
1615 if (Result.isAtStartOfLine() && !LexingRawMode) {
1616 BufferPtr = CurPtr;
Chris Lattner342dccb2007-10-17 20:41:00 +00001617 PP->HandleDirective(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001618
1619 // As an optimization, if the preprocessor didn't switch lexers, tail
1620 // recurse.
Chris Lattner342dccb2007-10-17 20:41:00 +00001621 if (PP->isCurrentLexer(this)) {
Chris Lattner4b009652007-07-25 00:24:17 +00001622 // Start a new token. If this is a #include or something, the PP may
1623 // want us starting at the beginning of the line again. If so, set
1624 // the StartOfLine flag.
1625 if (IsAtStartOfLine) {
1626 Result.setFlag(Token::StartOfLine);
1627 IsAtStartOfLine = false;
1628 }
1629 goto LexNextToken; // GCC isn't tail call eliminating.
1630 }
Chris Lattner342dccb2007-10-17 20:41:00 +00001631 return PP->Lex(Result);
Chris Lattner4b009652007-07-25 00:24:17 +00001632 }
1633 }
1634 break;
1635
Chris Lattner0ffadcc2008-01-03 17:58:54 +00001636 case '@':
1637 // Objective C support.
1638 if (CurPtr[-1] == '@' && Features.ObjC1)
1639 Result.setKind(tok::at);
1640 else
1641 Result.setKind(tok::unknown);
1642 break;
1643
Chris Lattner4b009652007-07-25 00:24:17 +00001644 case '\\':
1645 // FIXME: UCN's.
1646 // FALL THROUGH.
1647 default:
Chris Lattner4b009652007-07-25 00:24:17 +00001648 Result.setKind(tok::unknown);
1649 break;
1650 }
1651
1652 // Notify MIOpt that we read a non-whitespace/non-comment token.
1653 MIOpt.ReadToken();
1654
1655 // Update the location of token as well as BufferPtr.
1656 FormTokenWithChars(Result, CurPtr);
1657}