blob: a9905f3391606815404b6affae6e53ce3c2884b4 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Lexer and LexerToken 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:
Chris Lattner22eb9722006-06-18 05:43:12 +000022// 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/SourceBuffer.h"
31#include "clang/Basic/SourceLocation.h"
32#include "llvm/Config/alloca.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000033#include <cctype>
34#include <iostream>
35using namespace llvm;
36using namespace clang;
37
38static void InitCharacterInfo();
39
Chris Lattner4cca5ba2006-07-02 20:05:54 +000040Lexer::Lexer(const SourceBuffer *File, unsigned fileid, Preprocessor &pp,
41 const char *BufStart, const char *BufEnd)
Chris Lattner678c8802006-07-11 05:46:12 +000042 : BufferEnd(BufEnd ? BufEnd : File->getBufferEnd()),
Chris Lattner4cca5ba2006-07-02 20:05:54 +000043 InputFile(File), CurFileID(fileid), PP(pp), Features(PP.getLangOptions()) {
Chris Lattnerecfeafe2006-07-02 21:26:45 +000044 Is_PragmaLexer = false;
Chris Lattner4ec473f2006-07-03 05:16:05 +000045 IsMainFile = false;
Chris Lattner22eb9722006-06-18 05:43:12 +000046 InitCharacterInfo();
47
48 assert(BufferEnd[0] == 0 &&
49 "We assume that the input buffer has a null character at the end"
50 " to simplify lexing!");
Chris Lattner678c8802006-07-11 05:46:12 +000051
52 BufferPtr = BufStart ? BufStart : File->getBufferStart();
53
Chris Lattner22eb9722006-06-18 05:43:12 +000054 // Start of the file is a start of line.
55 IsAtStartOfLine = true;
56
57 // We are not after parsing a #.
58 ParsingPreprocessorDirective = false;
59
60 // We are not after parsing #include.
61 ParsingFilename = false;
Chris Lattner3ebcf4e2006-07-11 05:39:23 +000062
63 // We are not in raw mode. Raw mode disables diagnostics and interpretation
64 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
65 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
66 // or otherwise skipping over tokens.
67 LexingRawMode = false;
Chris Lattner22eb9722006-06-18 05:43:12 +000068}
69
Chris Lattnere3e81ea2006-07-03 01:13:26 +000070/// Stringify - Convert the specified string into a C string, with surrounding
71/// ""'s, and with escaped \ and " characters.
Chris Lattnerecc39e92006-07-15 05:23:31 +000072std::string Lexer::Stringify(const std::string &Str, bool Charify) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +000073 std::string Result = Str;
Chris Lattnerecc39e92006-07-15 05:23:31 +000074 char Quote = Charify ? '\'' : '"';
Chris Lattnere3e81ea2006-07-03 01:13:26 +000075 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
Chris Lattnerecc39e92006-07-15 05:23:31 +000076 if (Result[i] == '\\' || Result[i] == Quote) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +000077 Result.insert(Result.begin()+i, '\\');
78 ++i; ++e;
79 }
80 }
Chris Lattnerecc39e92006-07-15 05:23:31 +000081 return Result;
Chris Lattnere3e81ea2006-07-03 01:13:26 +000082}
83
Chris Lattner22eb9722006-06-18 05:43:12 +000084
Chris Lattner22eb9722006-06-18 05:43:12 +000085//===----------------------------------------------------------------------===//
86// Character information.
87//===----------------------------------------------------------------------===//
88
89static unsigned char CharInfo[256];
90
91enum {
92 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
93 CHAR_VERT_WS = 0x02, // '\r', '\n'
94 CHAR_LETTER = 0x04, // a-z,A-Z
95 CHAR_NUMBER = 0x08, // 0-9
96 CHAR_UNDER = 0x10, // _
97 CHAR_PERIOD = 0x20 // .
98};
99
100static void InitCharacterInfo() {
101 static bool isInited = false;
102 if (isInited) return;
103 isInited = true;
104
105 // Intiialize the CharInfo table.
106 // TODO: statically initialize this.
107 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
108 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
109 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
110
111 CharInfo[(int)'_'] = CHAR_UNDER;
112 for (unsigned i = 'a'; i <= 'z'; ++i)
113 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
114 for (unsigned i = '0'; i <= '9'; ++i)
115 CharInfo[i] = CHAR_NUMBER;
116}
117
118/// isIdentifierBody - Return true if this is the body character of an
119/// identifier, which is [a-zA-Z0-9_].
120static inline bool isIdentifierBody(unsigned char c) {
121 return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER);
122}
123
124/// isHorizontalWhitespace - Return true if this character is horizontal
125/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
126static inline bool isHorizontalWhitespace(unsigned char c) {
127 return CharInfo[c] & CHAR_HORZ_WS;
128}
129
130/// isWhitespace - Return true if this character is horizontal or vertical
131/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
132/// for '\0'.
133static inline bool isWhitespace(unsigned char c) {
134 return CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS);
135}
136
137/// isNumberBody - Return true if this is the body character of an
138/// preprocessing number, which is [a-zA-Z0-9_.].
139static inline bool isNumberBody(unsigned char c) {
140 return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD);
141}
142
Chris Lattnerd01e2912006-06-18 16:22:51 +0000143
Chris Lattner22eb9722006-06-18 05:43:12 +0000144//===----------------------------------------------------------------------===//
145// Diagnostics forwarding code.
146//===----------------------------------------------------------------------===//
147
148/// getSourceLocation - Return a source location identifier for the specified
149/// offset in the current file.
150SourceLocation Lexer::getSourceLocation(const char *Loc) const {
Chris Lattner8bbfe462006-07-02 22:27:49 +0000151 assert(Loc >= InputFile->getBufferStart() && Loc <= BufferEnd &&
Chris Lattner4cca5ba2006-07-02 20:05:54 +0000152 "Location out of range for this buffer!");
Chris Lattner8bbfe462006-07-02 22:27:49 +0000153 return SourceLocation(CurFileID, Loc-InputFile->getBufferStart());
Chris Lattner22eb9722006-06-18 05:43:12 +0000154}
155
156
157/// Diag - Forwarding function for diagnostics. This translate a source
158/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000159void Lexer::Diag(const char *Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000160 const std::string &Msg) const {
Chris Lattnercb283342006-06-18 06:48:37 +0000161 PP.Diag(getSourceLocation(Loc), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000162}
163
164//===----------------------------------------------------------------------===//
165// Trigraph and Escaped Newline Handling Code.
166//===----------------------------------------------------------------------===//
167
168/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
169/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
170static char GetTrigraphCharForLetter(char Letter) {
171 switch (Letter) {
172 default: return 0;
173 case '=': return '#';
174 case ')': return ']';
175 case '(': return '[';
176 case '!': return '|';
177 case '\'': return '^';
178 case '>': return '}';
179 case '/': return '\\';
180 case '<': return '{';
181 case '-': return '~';
182 }
183}
184
185/// DecodeTrigraphChar - If the specified character is a legal trigraph when
186/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
187/// return the result character. Finally, emit a warning about trigraph use
188/// whether trigraphs are enabled or not.
189static char DecodeTrigraphChar(const char *CP, Lexer *L) {
190 char Res = GetTrigraphCharForLetter(*CP);
191 if (Res && L) {
192 if (!L->getFeatures().Trigraphs) {
193 L->Diag(CP-2, diag::trigraph_ignored);
194 return 0;
195 } else {
196 L->Diag(CP-2, diag::trigraph_converted, std::string()+Res);
197 }
198 }
199 return Res;
200}
201
202/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
203/// get its size, and return it. This is tricky in several cases:
204/// 1. If currently at the start of a trigraph, we warn about the trigraph,
205/// then either return the trigraph (skipping 3 chars) or the '?',
206/// depending on whether trigraphs are enabled or not.
207/// 2. If this is an escaped newline (potentially with whitespace between
208/// the backslash and newline), implicitly skip the newline and return
209/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000210/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000211///
212/// This handles the slow/uncommon case of the getCharAndSize method. Here we
213/// know that we can accumulate into Size, and that we have already incremented
214/// Ptr by Size bytes.
215///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000216/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
217/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000218///
219char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
220 LexerToken *Tok) {
221 // If we have a slash, look for an escaped newline.
222 if (Ptr[0] == '\\') {
223 ++Size;
224 ++Ptr;
225Slash:
226 // Common case, backslash-char where the char is not whitespace.
227 if (!isWhitespace(Ptr[0])) return '\\';
228
229 // See if we have optional whitespace characters followed by a newline.
230 {
231 unsigned SizeTmp = 0;
232 do {
233 ++SizeTmp;
234 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
235 // Remember that this token needs to be cleaned.
236 if (Tok) Tok->SetFlag(LexerToken::NeedsCleaning);
237
238 // Warn if there was whitespace between the backslash and newline.
239 if (SizeTmp != 1 && Tok)
240 Diag(Ptr, diag::backslash_newline_space);
241
242 // If this is a \r\n or \n\r, skip the newlines.
243 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
244 Ptr[SizeTmp-1] != Ptr[SizeTmp])
245 ++SizeTmp;
246
247 // Found backslash<whitespace><newline>. Parse the char after it.
248 Size += SizeTmp;
249 Ptr += SizeTmp;
250 // Use slow version to accumulate a correct size field.
251 return getCharAndSizeSlow(Ptr, Size, Tok);
252 }
253 } while (isWhitespace(Ptr[SizeTmp]));
254 }
255
256 // Otherwise, this is not an escaped newline, just return the slash.
257 return '\\';
258 }
259
260 // If this is a trigraph, process it.
261 if (Ptr[0] == '?' && Ptr[1] == '?') {
262 // If this is actually a legal trigraph (not something like "??x"), emit
263 // a trigraph warning. If so, and if trigraphs are enabled, return it.
264 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
265 // Remember that this token needs to be cleaned.
266 if (Tok) Tok->SetFlag(LexerToken::NeedsCleaning);
267
268 Ptr += 3;
269 Size += 3;
270 if (C == '\\') goto Slash;
271 return C;
272 }
273 }
274
275 // If this is neither, return a single character.
276 ++Size;
277 return *Ptr;
278}
279
Chris Lattnerd01e2912006-06-18 16:22:51 +0000280
Chris Lattner22eb9722006-06-18 05:43:12 +0000281/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
282/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
283/// and that we have already incremented Ptr by Size bytes.
284///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000285/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
286/// be updated to match.
287char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000288 const LangOptions &Features) {
289 // If we have a slash, look for an escaped newline.
290 if (Ptr[0] == '\\') {
291 ++Size;
292 ++Ptr;
293Slash:
294 // Common case, backslash-char where the char is not whitespace.
295 if (!isWhitespace(Ptr[0])) return '\\';
296
297 // See if we have optional whitespace characters followed by a newline.
298 {
299 unsigned SizeTmp = 0;
300 do {
301 ++SizeTmp;
302 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
303
304 // If this is a \r\n or \n\r, skip the newlines.
305 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
306 Ptr[SizeTmp-1] != Ptr[SizeTmp])
307 ++SizeTmp;
308
309 // Found backslash<whitespace><newline>. Parse the char after it.
310 Size += SizeTmp;
311 Ptr += SizeTmp;
312
313 // Use slow version to accumulate a correct size field.
314 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
315 }
316 } while (isWhitespace(Ptr[SizeTmp]));
317 }
318
319 // Otherwise, this is not an escaped newline, just return the slash.
320 return '\\';
321 }
322
323 // If this is a trigraph, process it.
324 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
325 // If this is actually a legal trigraph (not something like "??x"), return
326 // it.
327 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
328 Ptr += 3;
329 Size += 3;
330 if (C == '\\') goto Slash;
331 return C;
332 }
333 }
334
335 // If this is neither, return a single character.
336 ++Size;
337 return *Ptr;
338}
339
Chris Lattner22eb9722006-06-18 05:43:12 +0000340//===----------------------------------------------------------------------===//
341// Helper methods for lexing.
342//===----------------------------------------------------------------------===//
343
Chris Lattnercb283342006-06-18 06:48:37 +0000344void Lexer::LexIdentifier(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000345 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
346 unsigned Size;
347 unsigned char C = *CurPtr++;
348 while (isIdentifierBody(C)) {
349 C = *CurPtr++;
350 }
351 --CurPtr; // Back up over the skipped character.
352
353 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
354 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000355 // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000356 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
357FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000358 const char *IdStart = BufferPtr;
Chris Lattnerd01e2912006-06-18 16:22:51 +0000359 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000360 Result.SetKind(tok::identifier);
361
Chris Lattnercefc7682006-07-08 08:28:12 +0000362 // Fill in Result.IdentifierInfo, looking up the identifier in the
363 // identifier table.
364 PP.LookUpIdentifierInfo(Result, IdStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000365
Chris Lattnerc5a00062006-06-18 16:41:01 +0000366 // Finally, now that we know we have an identifier, pass this off to the
367 // preprocessor, which may macro expand it or something.
Chris Lattner22eb9722006-06-18 05:43:12 +0000368 return PP.HandleIdentifier(Result);
369 }
370
371 // Otherwise, $,\,? in identifier found. Enter slower path.
372
373 C = getCharAndSize(CurPtr, Size);
374 while (1) {
375 if (C == '$') {
376 // If we hit a $ and they are not supported in identifiers, we are done.
377 if (!Features.DollarIdents) goto FinishIdentifier;
378
379 // Otherwise, emit a diagnostic and continue.
Chris Lattnercb283342006-06-18 06:48:37 +0000380 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000381 CurPtr = ConsumeChar(CurPtr, Size, Result);
382 C = getCharAndSize(CurPtr, Size);
383 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000384 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000385 // Found end of identifier.
386 goto FinishIdentifier;
387 }
388
389 // Otherwise, this character is good, consume it.
390 CurPtr = ConsumeChar(CurPtr, Size, Result);
391
392 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000393 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000394 CurPtr = ConsumeChar(CurPtr, Size, Result);
395 C = getCharAndSize(CurPtr, Size);
396 }
397 }
398}
399
400
401/// LexNumericConstant - Lex the remainer of a integer or floating point
402/// constant. From[-1] is the first character lexed. Return the end of the
403/// constant.
Chris Lattnercb283342006-06-18 06:48:37 +0000404void Lexer::LexNumericConstant(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000405 unsigned Size;
406 char C = getCharAndSize(CurPtr, Size);
407 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000408 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000409 CurPtr = ConsumeChar(CurPtr, Size, Result);
410 PrevCh = C;
411 C = getCharAndSize(CurPtr, Size);
412 }
413
414 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
415 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
416 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
417
418 // If we have a hex FP constant, continue.
419 if (Features.HexFloats &&
420 (C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p'))
421 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
422
423 Result.SetKind(tok::numeric_constant);
424
Chris Lattnerd01e2912006-06-18 16:22:51 +0000425 // Update the location of token as well as BufferPtr.
426 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000427}
428
429/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
430/// either " or L".
Chris Lattnercb283342006-06-18 06:48:37 +0000431void Lexer::LexStringLiteral(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000432 const char *NulCharacter = 0; // Does this string contain the \0 character?
433
434 char C = getAndAdvanceChar(CurPtr, Result);
435 while (C != '"') {
436 // Skip escaped characters.
437 if (C == '\\') {
438 // Skip the escaped character.
439 C = getAndAdvanceChar(CurPtr, Result);
440 } else if (C == '\n' || C == '\r' || // Newline.
441 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000442 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000443 BufferPtr = CurPtr-1;
444 return LexTokenInternal(Result);
445 } else if (C == 0) {
446 NulCharacter = CurPtr-1;
447 }
448 C = getAndAdvanceChar(CurPtr, Result);
449 }
450
Chris Lattnercb283342006-06-18 06:48:37 +0000451 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000452
453 Result.SetKind(tok::string_literal);
454
Chris Lattnerd01e2912006-06-18 16:22:51 +0000455 // Update the location of the token as well as the BufferPtr instance var.
456 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000457}
458
459/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
460/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattnercb283342006-06-18 06:48:37 +0000461void Lexer::LexAngledStringLiteral(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000462 const char *NulCharacter = 0; // Does this string contain the \0 character?
463
464 char C = getAndAdvanceChar(CurPtr, Result);
465 while (C != '>') {
466 // Skip escaped characters.
467 if (C == '\\') {
468 // Skip the escaped character.
469 C = getAndAdvanceChar(CurPtr, Result);
470 } else if (C == '\n' || C == '\r' || // Newline.
471 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000472 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000473 BufferPtr = CurPtr-1;
474 return LexTokenInternal(Result);
475 } else if (C == 0) {
476 NulCharacter = CurPtr-1;
477 }
478 C = getAndAdvanceChar(CurPtr, Result);
479 }
480
Chris Lattnercb283342006-06-18 06:48:37 +0000481 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000482
483 Result.SetKind(tok::angle_string_literal);
484
Chris Lattnerd01e2912006-06-18 16:22:51 +0000485 // Update the location of token as well as BufferPtr.
486 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000487}
488
489
490/// LexCharConstant - Lex the remainder of a character constant, after having
491/// lexed either ' or L'.
Chris Lattnercb283342006-06-18 06:48:37 +0000492void Lexer::LexCharConstant(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000493 const char *NulCharacter = 0; // Does this character contain the \0 character?
494
495 // Handle the common case of 'x' and '\y' efficiently.
496 char C = getAndAdvanceChar(CurPtr, Result);
497 if (C == '\'') {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000498 Diag(BufferPtr, diag::err_empty_character);
Chris Lattner22eb9722006-06-18 05:43:12 +0000499 BufferPtr = CurPtr;
500 return LexTokenInternal(Result);
501 } else if (C == '\\') {
502 // Skip the escaped character.
503 // FIXME: UCN's.
504 C = getAndAdvanceChar(CurPtr, Result);
505 }
506
507 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
508 ++CurPtr;
509 } else {
510 // Fall back on generic code for embedded nulls, newlines, wide chars.
511 do {
512 // Skip escaped characters.
513 if (C == '\\') {
514 // Skip the escaped character.
515 C = getAndAdvanceChar(CurPtr, Result);
516 } else if (C == '\n' || C == '\r' || // Newline.
517 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000518 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000519 BufferPtr = CurPtr-1;
520 return LexTokenInternal(Result);
521 } else if (C == 0) {
522 NulCharacter = CurPtr-1;
523 }
524 C = getAndAdvanceChar(CurPtr, Result);
525 } while (C != '\'');
526 }
527
Chris Lattnercb283342006-06-18 06:48:37 +0000528 if (NulCharacter) Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000529
530 Result.SetKind(tok::char_constant);
531
Chris Lattnerd01e2912006-06-18 16:22:51 +0000532 // Update the location of token as well as BufferPtr.
533 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000534}
535
536/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
537/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattnercb283342006-06-18 06:48:37 +0000538void Lexer::SkipWhitespace(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000539 // Whitespace - Skip it, then return the token after the whitespace.
540 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
541 while (1) {
542 // Skip horizontal whitespace very aggressively.
543 while (isHorizontalWhitespace(Char))
544 Char = *++CurPtr;
545
546 // Otherwise if we something other than whitespace, we're done.
547 if (Char != '\n' && Char != '\r')
548 break;
549
550 if (ParsingPreprocessorDirective) {
551 // End of preprocessor directive line, let LexTokenInternal handle this.
552 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000553 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000554 }
555
556 // ok, but handle newline.
557 // The returned token is at the start of the line.
558 Result.SetFlag(LexerToken::StartOfLine);
559 // No leading whitespace seen so far.
560 Result.ClearFlag(LexerToken::LeadingSpace);
561 Char = *++CurPtr;
562 }
563
564 // If this isn't immediately after a newline, there is leading space.
565 char PrevChar = CurPtr[-1];
566 if (PrevChar != '\n' && PrevChar != '\r')
567 Result.SetFlag(LexerToken::LeadingSpace);
568
569 // If the next token is obviously a // or /* */ comment, skip it efficiently
570 // too (without going through the big switch stmt).
571 if (Char == '/' && CurPtr[1] == '/') {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000572 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000573 return SkipBCPLComment(Result, CurPtr+1);
574 }
575 if (Char == '/' && CurPtr[1] == '*') {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000576 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000577 return SkipBlockComment(Result, CurPtr+2);
578 }
579 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000580}
581
582// SkipBCPLComment - We have just read the // characters from input. Skip until
583// we find the newline character thats terminate the comment. Then update
584/// BufferPtr and return.
Chris Lattnercb283342006-06-18 06:48:37 +0000585void Lexer::SkipBCPLComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000586 // If BCPL comments aren't explicitly enabled for this language, emit an
587 // extension warning.
588 if (!Features.BCPLComment) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000589 Diag(BufferPtr, diag::ext_bcpl_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000590
591 // Mark them enabled so we only emit one warning for this translation
592 // unit.
593 Features.BCPLComment = true;
594 }
595
596 // Scan over the body of the comment. The common case, when scanning, is that
597 // the comment contains normal ascii characters with nothing interesting in
598 // them. As such, optimize for this case with the inner loop.
599 char C;
600 do {
601 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +0000602 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
603 // If we find a \n character, scan backwards, checking to see if it's an
604 // escaped newline, like we do for block comments.
Chris Lattner22eb9722006-06-18 05:43:12 +0000605
606 // Skip over characters in the fast loop.
607 while (C != 0 && // Potentially EOF.
608 C != '\\' && // Potentially escaped newline.
609 C != '?' && // Potentially trigraph.
610 C != '\n' && C != '\r') // Newline or DOS-style newline.
611 C = *++CurPtr;
612
613 // If this is a newline, we're done.
614 if (C == '\n' || C == '\r')
615 break; // Found the newline? Break out!
616
617 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
618 // properly decode the character.
619 const char *OldPtr = CurPtr;
620 C = getAndAdvanceChar(CurPtr, Result);
621
622 // If we read multiple characters, and one of those characters was a \r or
623 // \n, then we had an escaped newline within the comment. Emit diagnostic.
624 if (CurPtr != OldPtr+1) {
625 for (; OldPtr != CurPtr; ++OldPtr)
626 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnercb283342006-06-18 06:48:37 +0000627 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
628 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000629 }
630 }
631
632 if (CurPtr == BufferEnd+1) goto FoundEOF;
633 } while (C != '\n' && C != '\r');
634
635 // Found and did not consume a newline.
636
637 // If we are inside a preprocessor directive and we see the end of line,
638 // return immediately, so that the lexer can return this as an EOM token.
639 if (ParsingPreprocessorDirective) {
640 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000641 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000642 }
643
644 // Otherwise, eat the \n character. We don't care if this is a \n\r or
645 // \r\n sequence.
646 ++CurPtr;
647
648 // The next returned token is at the start of the line.
649 Result.SetFlag(LexerToken::StartOfLine);
650 // No leading whitespace seen so far.
651 Result.ClearFlag(LexerToken::LeadingSpace);
652
653 // It is common for the tokens immediately after a // comment to be
654 // whitespace (indentation for the next line). Instead of going through the
655 // big switch, handle it efficiently now.
656 if (isWhitespace(*CurPtr)) {
657 Result.SetFlag(LexerToken::LeadingSpace);
658 return SkipWhitespace(Result, CurPtr+1);
659 }
660
661 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000662 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000663
664FoundEOF: // If we ran off the end of the buffer, return EOF.
665 BufferPtr = CurPtr-1;
Chris Lattnercb283342006-06-18 06:48:37 +0000666 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000667}
668
Chris Lattnercb283342006-06-18 06:48:37 +0000669/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
670/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner22eb9722006-06-18 05:43:12 +0000671/// diagnostic if so. We know that the is inside of a block comment.
Chris Lattner1f583052006-06-18 06:53:56 +0000672static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
673 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000674 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Chris Lattner22eb9722006-06-18 05:43:12 +0000675
676 // Back up off the newline.
677 --CurPtr;
678
679 // If this is a two-character newline sequence, skip the other character.
680 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
681 // \n\n or \r\r -> not escaped newline.
682 if (CurPtr[0] == CurPtr[1])
683 return false;
684 // \n\r or \r\n -> skip the newline.
685 --CurPtr;
686 }
687
688 // If we have horizontal whitespace, skip over it. We allow whitespace
689 // between the slash and newline.
690 bool HasSpace = false;
691 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
692 --CurPtr;
693 HasSpace = true;
694 }
695
696 // If we have a slash, we know this is an escaped newline.
697 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +0000698 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000699 } else {
700 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +0000701 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
702 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +0000703 return false;
Chris Lattnercb283342006-06-18 06:48:37 +0000704
705 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +0000706 CurPtr -= 2;
707
708 // If no trigraphs are enabled, warn that we ignored this trigraph and
709 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +0000710 if (!L->getFeatures().Trigraphs) {
711 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000712 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000713 }
Chris Lattner1f583052006-06-18 06:53:56 +0000714 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000715 }
716
717 // Warn about having an escaped newline between the */ characters.
Chris Lattner1f583052006-06-18 06:53:56 +0000718 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Chris Lattner22eb9722006-06-18 05:43:12 +0000719
720 // If there was space between the backslash and newline, warn about it.
Chris Lattner1f583052006-06-18 06:53:56 +0000721 if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space);
Chris Lattner22eb9722006-06-18 05:43:12 +0000722
Chris Lattnercb283342006-06-18 06:48:37 +0000723 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000724}
725
726/// SkipBlockComment - We have just read the /* characters from input. Read
727/// until we find the */ characters that terminate the comment. Note that we
728/// don't bother decoding trigraphs or escaped newlines in block comments,
729/// because they cannot cause the comment to end. The only thing that can
730/// happen is the comment could end with an escaped newline between the */ end
731/// of comment.
Chris Lattnercb283342006-06-18 06:48:37 +0000732void Lexer::SkipBlockComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000733 // Scan one character past where we should, looking for a '/' character. Once
734 // we find it, check to see if it was preceeded by a *. This common
735 // optimization helps people who like to put a lot of * characters in their
736 // comments.
737 unsigned char C = *CurPtr++;
738 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000739 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000740 BufferPtr = CurPtr-1;
Chris Lattnercb283342006-06-18 06:48:37 +0000741 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000742 }
743
744 while (1) {
745 // Skip over all non-interesting characters.
746 // TODO: Vectorize this. Note: memchr on Darwin is slower than this loop.
747 while (C != '/' && C != '\0')
748 C = *CurPtr++;
749
750 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000751 if (CurPtr[-2] == '*') // We found the final */. We're done!
752 break;
753
754 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +0000755 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000756 // We found the final */, though it had an escaped newline between the
757 // * and /. We're done!
758 break;
759 }
760 }
761 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
762 // If this is a /* inside of the comment, emit a warning. Don't do this
763 // if this is a /*/, which will end the comment. This misses cases with
764 // embedded escaped newlines, but oh well.
Chris Lattnercb283342006-06-18 06:48:37 +0000765 Diag(CurPtr-1, diag::nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000766 }
767 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000768 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000769 // Note: the user probably forgot a */. We could continue immediately
770 // after the /*, but this would involve lexing a lot of what really is the
771 // comment, which surely would confuse the parser.
772 BufferPtr = CurPtr-1;
Chris Lattnercb283342006-06-18 06:48:37 +0000773 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000774 }
775 C = *CurPtr++;
776 }
777
778 // It is common for the tokens immediately after a /**/ comment to be
779 // whitespace. Instead of going through the big switch, handle it
780 // efficiently now.
781 if (isHorizontalWhitespace(*CurPtr)) {
782 Result.SetFlag(LexerToken::LeadingSpace);
783 return SkipWhitespace(Result, CurPtr+1);
784 }
785
786 // Otherwise, just return so that the next character will be lexed as a token.
787 BufferPtr = CurPtr;
788 Result.SetFlag(LexerToken::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000789}
790
791//===----------------------------------------------------------------------===//
792// Primary Lexing Entry Points
793//===----------------------------------------------------------------------===//
794
795/// LexIncludeFilename - After the preprocessor has parsed a #include, lex and
796/// (potentially) macro expand the filename.
Chris Lattner269c2322006-06-25 06:23:00 +0000797std::string Lexer::LexIncludeFilename(LexerToken &FilenameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000798 assert(ParsingPreprocessorDirective &&
799 ParsingFilename == false &&
800 "Must be in a preprocessing directive!");
801
802 // We are now parsing a filename!
803 ParsingFilename = true;
804
Chris Lattner269c2322006-06-25 06:23:00 +0000805 // Lex the filename.
806 Lex(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000807
808 // We should have gotten the filename now.
809 ParsingFilename = false;
810
811 // No filename?
Chris Lattner269c2322006-06-25 06:23:00 +0000812 if (FilenameTok.getKind() == tok::eom) {
813 PP.Diag(FilenameTok, diag::err_pp_expects_filename);
814 return "";
Chris Lattnercb283342006-06-18 06:48:37 +0000815 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000816
Chris Lattner269c2322006-06-25 06:23:00 +0000817 // Get the text form of the filename.
818 std::string Filename = PP.getSpelling(FilenameTok);
819 assert(!Filename.empty() && "Can't have tokens with empty spellings!");
820
821 // Make sure the filename is <x> or "x".
822 if (Filename[0] == '<') {
823 if (Filename[Filename.size()-1] != '>') {
824 PP.Diag(FilenameTok, diag::err_pp_expects_filename);
825 FilenameTok.SetKind(tok::eom);
826 return "";
827 }
828 } else if (Filename[0] == '"') {
829 if (Filename[Filename.size()-1] != '"') {
830 PP.Diag(FilenameTok, diag::err_pp_expects_filename);
831 FilenameTok.SetKind(tok::eom);
832 return "";
833 }
834 } else {
835 PP.Diag(FilenameTok, diag::err_pp_expects_filename);
836 FilenameTok.SetKind(tok::eom);
837 return "";
Chris Lattner22eb9722006-06-18 05:43:12 +0000838 }
Chris Lattner269c2322006-06-25 06:23:00 +0000839
840 // Diagnose #include "" as invalid.
841 if (Filename.size() == 2) {
842 PP.Diag(FilenameTok, diag::err_pp_empty_filename);
843 FilenameTok.SetKind(tok::eom);
844 return "";
845 }
846
847 return Filename;
Chris Lattner22eb9722006-06-18 05:43:12 +0000848}
849
850/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
851/// uninterpreted string. This switches the lexer out of directive mode.
852std::string Lexer::ReadToEndOfLine() {
853 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
854 "Must be in a preprocessing directive!");
855 std::string Result;
856 LexerToken Tmp;
857
858 // CurPtr - Cache BufferPtr in an automatic variable.
859 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000860 while (1) {
861 char Char = getAndAdvanceChar(CurPtr, Tmp);
862 switch (Char) {
863 default:
864 Result += Char;
865 break;
866 case 0: // Null.
867 // Found end of file?
868 if (CurPtr-1 != BufferEnd) {
869 // Nope, normal character, continue.
870 Result += Char;
871 break;
872 }
873 // FALL THROUGH.
874 case '\r':
875 case '\n':
876 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
877 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
878 BufferPtr = CurPtr-1;
879
880 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +0000881 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000882 assert(Tmp.getKind() == tok::eom && "Unexpected token!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000883
884 // Finally, we're done, return the string we found.
885 return Result;
886 }
887 }
888}
889
890/// LexEndOfFile - CurPtr points to the end of this file. Handle this
891/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +0000892/// This returns true if Result contains a token, false if PP.Lex should be
893/// called again.
894bool Lexer::LexEndOfFile(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000895 // If we hit the end of the file while parsing a preprocessor directive,
896 // end the preprocessor directive first. The next token returned will
897 // then be the end of file.
898 if (ParsingPreprocessorDirective) {
899 // Done parsing the "line".
900 ParsingPreprocessorDirective = false;
901 Result.SetKind(tok::eom);
Chris Lattnerd01e2912006-06-18 16:22:51 +0000902 // Update the location of token as well as BufferPtr.
903 FormTokenWithChars(Result, CurPtr);
Chris Lattner2183a6e2006-07-18 06:36:12 +0000904 return true; // Have a token.
Chris Lattner22eb9722006-06-18 05:43:12 +0000905 }
906
Chris Lattner30a2fa12006-07-19 06:31:49 +0000907 // If we are in raw mode, return this event as an EOF token. Let the caller
908 // that put us in raw mode handle the event.
909 if (LexingRawMode) {
910 Result.StartToken();
911 BufferPtr = BufferEnd;
912 FormTokenWithChars(Result, BufferEnd);
913 Result.SetKind(tok::eof);
914 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000915 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000916
Chris Lattner30a2fa12006-07-19 06:31:49 +0000917 // Otherwise, issue diagnostics for unterminated #if and missing newline.
918
919 // If we are in a #if directive, emit an error.
920 while (!ConditionalStack.empty()) {
921 PP.Diag(ConditionalStack.back().IfLoc,
922 diag::err_pp_unterminated_conditional);
923 ConditionalStack.pop_back();
924 }
925
926 // If the file was empty or didn't end in a newline, issue a pedwarn.
927 if (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
928 Diag(BufferEnd, diag::ext_no_newline_eof);
929
Chris Lattner22eb9722006-06-18 05:43:12 +0000930 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +0000931
932 // Finally, let the preprocessor handle this.
Chris Lattner2183a6e2006-07-18 06:36:12 +0000933 return PP.HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000934}
935
Chris Lattner678c8802006-07-11 05:46:12 +0000936/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
937/// the specified lexer will return a tok::l_paren token, 0 if it is something
938/// else and 2 if there are no more tokens in the buffer controlled by the
939/// lexer.
940unsigned Lexer::isNextPPTokenLParen() {
941 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
942
943 // Switch to 'skipping' mode. This will ensure that we can lex a token
944 // without emitting diagnostics, disables macro expansion, and will cause EOF
945 // to return an EOF token instead of popping the include stack.
946 LexingRawMode = true;
947
948 // Save state that can be changed while lexing so that we can restore it.
949 const char *TmpBufferPtr = BufferPtr;
950
951 LexerToken Tok;
952 Tok.StartToken();
953 LexTokenInternal(Tok);
954
955 // Restore state that may have changed.
956 BufferPtr = TmpBufferPtr;
957
958 // Restore the lexer back to non-skipping mode.
959 LexingRawMode = false;
960
961 if (Tok.getKind() == tok::eof)
962 return 2;
963 return Tok.getKind() == tok::l_paren;
964}
965
Chris Lattner22eb9722006-06-18 05:43:12 +0000966
967/// LexTokenInternal - This implements a simple C family lexer. It is an
968/// extremely performance critical piece of code. This assumes that the buffer
969/// has a null character at the end of the file. Return true if an error
970/// occurred and compilation should terminate, false if normal. This returns a
971/// preprocessing token, not a normal token, as such, it is an internal
972/// interface. It assumes that the Flags of result have been cleared before
973/// calling this.
Chris Lattnercb283342006-06-18 06:48:37 +0000974void Lexer::LexTokenInternal(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000975LexNextToken:
976 // New token, can't need cleaning yet.
977 Result.ClearFlag(LexerToken::NeedsCleaning);
Chris Lattner27746e42006-07-05 00:07:54 +0000978 Result.SetIdentifierInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +0000979
980 // CurPtr - Cache BufferPtr in an automatic variable.
981 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000982
Chris Lattnereb54b592006-07-10 06:34:27 +0000983 // Small amounts of horizontal whitespace is very common between tokens.
984 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
985 ++CurPtr;
986 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
987 ++CurPtr;
988 BufferPtr = CurPtr;
989 Result.SetFlag(LexerToken::LeadingSpace);
990 }
991
Chris Lattner22eb9722006-06-18 05:43:12 +0000992 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
993
994 // Read a character, advancing over it.
995 char Char = getAndAdvanceChar(CurPtr, Result);
996 switch (Char) {
997 case 0: // Null.
998 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +0000999 if (CurPtr-1 == BufferEnd) {
1000 // Read the PP instance variable into an automatic variable, because
1001 // LexEndOfFile will often delete 'this'.
1002 Preprocessor &PPCache = PP;
1003 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1004 return; // Got a token to return.
1005 return PPCache.Lex(Result);
1006 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001007
Chris Lattnercb283342006-06-18 06:48:37 +00001008 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner22eb9722006-06-18 05:43:12 +00001009 Result.SetFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001010 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001011 goto LexNextToken; // GCC isn't tail call eliminating.
1012 case '\n':
1013 case '\r':
1014 // If we are inside a preprocessor directive and we see the end of line,
1015 // we know we are done with the directive, so return an EOM token.
1016 if (ParsingPreprocessorDirective) {
1017 // Done parsing the "line".
1018 ParsingPreprocessorDirective = false;
1019
1020 // Since we consumed a newline, we are back at the start of a line.
1021 IsAtStartOfLine = true;
1022
1023 Result.SetKind(tok::eom);
1024 break;
1025 }
1026 // The returned token is at the start of the line.
1027 Result.SetFlag(LexerToken::StartOfLine);
1028 // No leading whitespace seen so far.
1029 Result.ClearFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001030 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001031 goto LexNextToken; // GCC isn't tail call eliminating.
1032 case ' ':
1033 case '\t':
1034 case '\f':
1035 case '\v':
1036 Result.SetFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001037 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001038 goto LexNextToken; // GCC isn't tail call eliminating.
1039
1040 case 'L':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001041 // Notify MIOpt that we read a non-whitespace/non-comment token.
1042 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001043 Char = getCharAndSize(CurPtr, SizeTmp);
1044
1045 // Wide string literal.
1046 if (Char == '"')
1047 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1048
1049 // Wide character constant.
1050 if (Char == '\'')
1051 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1052 // FALL THROUGH, treating L like the start of an identifier.
1053
1054 // C99 6.4.2: Identifiers.
1055 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1056 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1057 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1058 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1059 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1060 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1061 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1062 case 'v': case 'w': case 'x': case 'y': case 'z':
1063 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001064 // Notify MIOpt that we read a non-whitespace/non-comment token.
1065 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001066 return LexIdentifier(Result, CurPtr);
1067
1068 // C99 6.4.4.1: Integer Constants.
1069 // C99 6.4.4.2: Floating Constants.
1070 case '0': case '1': case '2': case '3': case '4':
1071 case '5': case '6': case '7': case '8': case '9':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001072 // Notify MIOpt that we read a non-whitespace/non-comment token.
1073 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001074 return LexNumericConstant(Result, CurPtr);
1075
1076 // C99 6.4.4: Character Constants.
1077 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001078 // Notify MIOpt that we read a non-whitespace/non-comment token.
1079 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001080 return LexCharConstant(Result, CurPtr);
1081
1082 // C99 6.4.5: String Literals.
1083 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001084 // Notify MIOpt that we read a non-whitespace/non-comment token.
1085 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001086 return LexStringLiteral(Result, CurPtr);
1087
1088 // C99 6.4.6: Punctuators.
1089 case '?':
1090 Result.SetKind(tok::question);
1091 break;
1092 case '[':
1093 Result.SetKind(tok::l_square);
1094 break;
1095 case ']':
1096 Result.SetKind(tok::r_square);
1097 break;
1098 case '(':
1099 Result.SetKind(tok::l_paren);
1100 break;
1101 case ')':
1102 Result.SetKind(tok::r_paren);
1103 break;
1104 case '{':
1105 Result.SetKind(tok::l_brace);
1106 break;
1107 case '}':
1108 Result.SetKind(tok::r_brace);
1109 break;
1110 case '.':
1111 Char = getCharAndSize(CurPtr, SizeTmp);
1112 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001113 // Notify MIOpt that we read a non-whitespace/non-comment token.
1114 MIOpt.ReadToken();
1115
Chris Lattner22eb9722006-06-18 05:43:12 +00001116 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1117 } else if (Features.CPlusPlus && Char == '*') {
1118 Result.SetKind(tok::periodstar);
1119 CurPtr += SizeTmp;
1120 } else if (Char == '.' &&
1121 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
1122 Result.SetKind(tok::ellipsis);
1123 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1124 SizeTmp2, Result);
1125 } else {
1126 Result.SetKind(tok::period);
1127 }
1128 break;
1129 case '&':
1130 Char = getCharAndSize(CurPtr, SizeTmp);
1131 if (Char == '&') {
1132 Result.SetKind(tok::ampamp);
1133 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1134 } else if (Char == '=') {
1135 Result.SetKind(tok::ampequal);
1136 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1137 } else {
1138 Result.SetKind(tok::amp);
1139 }
1140 break;
1141 case '*':
1142 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
1143 Result.SetKind(tok::starequal);
1144 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1145 } else {
1146 Result.SetKind(tok::star);
1147 }
1148 break;
1149 case '+':
1150 Char = getCharAndSize(CurPtr, SizeTmp);
1151 if (Char == '+') {
1152 Result.SetKind(tok::plusplus);
1153 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1154 } else if (Char == '=') {
1155 Result.SetKind(tok::plusequal);
1156 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1157 } else {
1158 Result.SetKind(tok::plus);
1159 }
1160 break;
1161 case '-':
1162 Char = getCharAndSize(CurPtr, SizeTmp);
1163 if (Char == '-') {
1164 Result.SetKind(tok::minusminus);
1165 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1166 } else if (Char == '>' && Features.CPlusPlus &&
1167 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {
1168 Result.SetKind(tok::arrowstar); // C++ ->*
1169 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1170 SizeTmp2, Result);
1171 } else if (Char == '>') {
1172 Result.SetKind(tok::arrow);
1173 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1174 } else if (Char == '=') {
1175 Result.SetKind(tok::minusequal);
1176 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1177 } else {
1178 Result.SetKind(tok::minus);
1179 }
1180 break;
1181 case '~':
1182 Result.SetKind(tok::tilde);
1183 break;
1184 case '!':
1185 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
1186 Result.SetKind(tok::exclaimequal);
1187 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1188 } else {
1189 Result.SetKind(tok::exclaim);
1190 }
1191 break;
1192 case '/':
1193 // 6.4.9: Comments
1194 Char = getCharAndSize(CurPtr, SizeTmp);
1195 if (Char == '/') { // BCPL comment.
1196 Result.SetFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001197 SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result));
Chris Lattner22eb9722006-06-18 05:43:12 +00001198 goto LexNextToken; // GCC isn't tail call eliminating.
1199 } else if (Char == '*') { // /**/ comment.
1200 Result.SetFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001201 SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result));
Chris Lattner22eb9722006-06-18 05:43:12 +00001202 goto LexNextToken; // GCC isn't tail call eliminating.
1203 } else if (Char == '=') {
1204 Result.SetKind(tok::slashequal);
1205 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1206 } else {
1207 Result.SetKind(tok::slash);
1208 }
1209 break;
1210 case '%':
1211 Char = getCharAndSize(CurPtr, SizeTmp);
1212 if (Char == '=') {
1213 Result.SetKind(tok::percentequal);
1214 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1215 } else if (Features.Digraphs && Char == '>') {
1216 Result.SetKind(tok::r_brace); // '%>' -> '}'
1217 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1218 } else if (Features.Digraphs && Char == ':') {
1219 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001220 Char = getCharAndSize(CurPtr, SizeTmp);
1221 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001222 Result.SetKind(tok::hashhash); // '%:%:' -> '##'
1223 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1224 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001225 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
1226 Result.SetKind(tok::hashat);
1227 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1228 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner22eb9722006-06-18 05:43:12 +00001229 } else {
1230 Result.SetKind(tok::hash); // '%:' -> '#'
1231
1232 // We parsed a # character. If this occurs at the start of the line,
1233 // it's actually the start of a preprocessing directive. Callback to
1234 // the preprocessor to handle it.
1235 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001236 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001237 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001238 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001239
1240 // As an optimization, if the preprocessor didn't switch lexers, tail
1241 // recurse.
1242 if (PP.isCurrentLexer(this)) {
1243 // Start a new token. If this is a #include or something, the PP may
1244 // want us starting at the beginning of the line again. If so, set
1245 // the StartOfLine flag.
1246 if (IsAtStartOfLine) {
1247 Result.SetFlag(LexerToken::StartOfLine);
1248 IsAtStartOfLine = false;
1249 }
1250 goto LexNextToken; // GCC isn't tail call eliminating.
1251 }
1252
1253 return PP.Lex(Result);
1254 }
1255 }
1256 } else {
1257 Result.SetKind(tok::percent);
1258 }
1259 break;
1260 case '<':
1261 Char = getCharAndSize(CurPtr, SizeTmp);
1262 if (ParsingFilename) {
1263 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1264 } else if (Char == '<' &&
1265 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
1266 Result.SetKind(tok::lesslessequal);
1267 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1268 SizeTmp2, Result);
1269 } else if (Char == '<') {
1270 Result.SetKind(tok::lessless);
1271 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1272 } else if (Char == '=') {
1273 Result.SetKind(tok::lessequal);
1274 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1275 } else if (Features.Digraphs && Char == ':') {
1276 Result.SetKind(tok::l_square); // '<:' -> '['
1277 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1278 } else if (Features.Digraphs && Char == '>') {
1279 Result.SetKind(tok::l_brace); // '<%' -> '{'
1280 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1281 } else if (Features.CPPMinMax && Char == '?') { // <?
1282 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerd01e2912006-06-18 16:22:51 +00001283 Diag(BufferPtr, diag::min_max_deprecated);
Chris Lattner22eb9722006-06-18 05:43:12 +00001284
1285 if (getCharAndSize(CurPtr, SizeTmp) == '=') { // <?=
1286 Result.SetKind(tok::lessquestionequal);
1287 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1288 } else {
1289 Result.SetKind(tok::lessquestion);
1290 }
1291 } else {
1292 Result.SetKind(tok::less);
1293 }
1294 break;
1295 case '>':
1296 Char = getCharAndSize(CurPtr, SizeTmp);
1297 if (Char == '=') {
1298 Result.SetKind(tok::greaterequal);
1299 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1300 } else if (Char == '>' &&
1301 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
1302 Result.SetKind(tok::greatergreaterequal);
1303 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1304 SizeTmp2, Result);
1305 } else if (Char == '>') {
1306 Result.SetKind(tok::greatergreater);
1307 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1308 } else if (Features.CPPMinMax && Char == '?') {
1309 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerd01e2912006-06-18 16:22:51 +00001310 Diag(BufferPtr, diag::min_max_deprecated);
Chris Lattner22eb9722006-06-18 05:43:12 +00001311
1312 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
1313 Result.SetKind(tok::greaterquestionequal); // >?=
1314 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1315 } else {
1316 Result.SetKind(tok::greaterquestion); // >?
1317 }
1318 } else {
1319 Result.SetKind(tok::greater);
1320 }
1321 break;
1322 case '^':
1323 Char = getCharAndSize(CurPtr, SizeTmp);
1324 if (Char == '=') {
1325 Result.SetKind(tok::caretequal);
1326 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1327 } else {
1328 Result.SetKind(tok::caret);
1329 }
1330 break;
1331 case '|':
1332 Char = getCharAndSize(CurPtr, SizeTmp);
1333 if (Char == '=') {
1334 Result.SetKind(tok::pipeequal);
1335 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1336 } else if (Char == '|') {
1337 Result.SetKind(tok::pipepipe);
1338 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1339 } else {
1340 Result.SetKind(tok::pipe);
1341 }
1342 break;
1343 case ':':
1344 Char = getCharAndSize(CurPtr, SizeTmp);
1345 if (Features.Digraphs && Char == '>') {
1346 Result.SetKind(tok::r_square); // ':>' -> ']'
1347 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1348 } else if (Features.CPlusPlus && Char == ':') {
1349 Result.SetKind(tok::coloncolon);
1350 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1351 } else {
1352 Result.SetKind(tok::colon);
1353 }
1354 break;
1355 case ';':
1356 Result.SetKind(tok::semi);
1357 break;
1358 case '=':
1359 Char = getCharAndSize(CurPtr, SizeTmp);
1360 if (Char == '=') {
1361 Result.SetKind(tok::equalequal);
1362 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1363 } else {
1364 Result.SetKind(tok::equal);
1365 }
1366 break;
1367 case ',':
1368 Result.SetKind(tok::comma);
1369 break;
1370 case '#':
1371 Char = getCharAndSize(CurPtr, SizeTmp);
1372 if (Char == '#') {
1373 Result.SetKind(tok::hashhash);
1374 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001375 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
1376 Result.SetKind(tok::hashat);
1377 Diag(BufferPtr, diag::charize_microsoft_ext);
1378 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001379 } else {
1380 Result.SetKind(tok::hash);
1381 // We parsed a # character. If this occurs at the start of the line,
1382 // it's actually the start of a preprocessing directive. Callback to
1383 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00001384 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001385 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001386 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001387 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001388
1389 // As an optimization, if the preprocessor didn't switch lexers, tail
1390 // recurse.
1391 if (PP.isCurrentLexer(this)) {
1392 // Start a new token. If this is a #include or something, the PP may
1393 // want us starting at the beginning of the line again. If so, set
1394 // the StartOfLine flag.
1395 if (IsAtStartOfLine) {
1396 Result.SetFlag(LexerToken::StartOfLine);
1397 IsAtStartOfLine = false;
1398 }
1399 goto LexNextToken; // GCC isn't tail call eliminating.
1400 }
1401 return PP.Lex(Result);
1402 }
1403 }
1404 break;
1405
1406 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00001407 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00001408 // FALL THROUGH.
1409 default:
1410 // Objective C support.
1411 if (CurPtr[-1] == '@' && Features.ObjC1) {
1412 Result.SetKind(tok::at);
1413 break;
1414 } else if (CurPtr[-1] == '$' && Features.DollarIdents) {// $ in identifiers.
Chris Lattnercb283342006-06-18 06:48:37 +00001415 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001416 // Notify MIOpt that we read a non-whitespace/non-comment token.
1417 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001418 return LexIdentifier(Result, CurPtr);
1419 }
1420
Chris Lattner041bef82006-07-11 05:52:53 +00001421 Result.SetKind(tok::unknown);
1422 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001423 }
1424
Chris Lattner371ac8a2006-07-04 07:11:10 +00001425 // Notify MIOpt that we read a non-whitespace/non-comment token.
1426 MIOpt.ReadToken();
1427
Chris Lattnerd01e2912006-06-18 16:22:51 +00001428 // Update the location of token as well as BufferPtr.
1429 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001430}