blob: 417d1d8d376cbd5e1b2798a2012faecb2b5f4493 [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 Lattner538d7f32006-07-20 04:31:52 +0000161 if (LexingRawMode && Diagnostic::isNoteWarningOrExtension(DiagID))
162 return;
Chris Lattnercb283342006-06-18 06:48:37 +0000163 PP.Diag(getSourceLocation(Loc), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000164}
Chris Lattner538d7f32006-07-20 04:31:52 +0000165void Lexer::Diag(SourceLocation Loc, unsigned DiagID,
166 const std::string &Msg) const {
167 if (LexingRawMode && Diagnostic::isNoteWarningOrExtension(DiagID))
168 return;
169 PP.Diag(Loc, DiagID, Msg);
170}
171
Chris Lattner22eb9722006-06-18 05:43:12 +0000172
173//===----------------------------------------------------------------------===//
174// Trigraph and Escaped Newline Handling Code.
175//===----------------------------------------------------------------------===//
176
177/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
178/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
179static char GetTrigraphCharForLetter(char Letter) {
180 switch (Letter) {
181 default: return 0;
182 case '=': return '#';
183 case ')': return ']';
184 case '(': return '[';
185 case '!': return '|';
186 case '\'': return '^';
187 case '>': return '}';
188 case '/': return '\\';
189 case '<': return '{';
190 case '-': return '~';
191 }
192}
193
194/// DecodeTrigraphChar - If the specified character is a legal trigraph when
195/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
196/// return the result character. Finally, emit a warning about trigraph use
197/// whether trigraphs are enabled or not.
198static char DecodeTrigraphChar(const char *CP, Lexer *L) {
199 char Res = GetTrigraphCharForLetter(*CP);
200 if (Res && L) {
201 if (!L->getFeatures().Trigraphs) {
202 L->Diag(CP-2, diag::trigraph_ignored);
203 return 0;
204 } else {
205 L->Diag(CP-2, diag::trigraph_converted, std::string()+Res);
206 }
207 }
208 return Res;
209}
210
211/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
212/// get its size, and return it. This is tricky in several cases:
213/// 1. If currently at the start of a trigraph, we warn about the trigraph,
214/// then either return the trigraph (skipping 3 chars) or the '?',
215/// depending on whether trigraphs are enabled or not.
216/// 2. If this is an escaped newline (potentially with whitespace between
217/// the backslash and newline), implicitly skip the newline and return
218/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000219/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000220///
221/// This handles the slow/uncommon case of the getCharAndSize method. Here we
222/// know that we can accumulate into Size, and that we have already incremented
223/// Ptr by Size bytes.
224///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000225/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
226/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000227///
228char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
229 LexerToken *Tok) {
230 // If we have a slash, look for an escaped newline.
231 if (Ptr[0] == '\\') {
232 ++Size;
233 ++Ptr;
234Slash:
235 // Common case, backslash-char where the char is not whitespace.
236 if (!isWhitespace(Ptr[0])) return '\\';
237
238 // See if we have optional whitespace characters followed by a newline.
239 {
240 unsigned SizeTmp = 0;
241 do {
242 ++SizeTmp;
243 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
244 // Remember that this token needs to be cleaned.
245 if (Tok) Tok->SetFlag(LexerToken::NeedsCleaning);
246
247 // Warn if there was whitespace between the backslash and newline.
248 if (SizeTmp != 1 && Tok)
249 Diag(Ptr, diag::backslash_newline_space);
250
251 // If this is a \r\n or \n\r, skip the newlines.
252 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
253 Ptr[SizeTmp-1] != Ptr[SizeTmp])
254 ++SizeTmp;
255
256 // Found backslash<whitespace><newline>. Parse the char after it.
257 Size += SizeTmp;
258 Ptr += SizeTmp;
259 // Use slow version to accumulate a correct size field.
260 return getCharAndSizeSlow(Ptr, Size, Tok);
261 }
262 } while (isWhitespace(Ptr[SizeTmp]));
263 }
264
265 // Otherwise, this is not an escaped newline, just return the slash.
266 return '\\';
267 }
268
269 // If this is a trigraph, process it.
270 if (Ptr[0] == '?' && Ptr[1] == '?') {
271 // If this is actually a legal trigraph (not something like "??x"), emit
272 // a trigraph warning. If so, and if trigraphs are enabled, return it.
273 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
274 // Remember that this token needs to be cleaned.
275 if (Tok) Tok->SetFlag(LexerToken::NeedsCleaning);
276
277 Ptr += 3;
278 Size += 3;
279 if (C == '\\') goto Slash;
280 return C;
281 }
282 }
283
284 // If this is neither, return a single character.
285 ++Size;
286 return *Ptr;
287}
288
Chris Lattnerd01e2912006-06-18 16:22:51 +0000289
Chris Lattner22eb9722006-06-18 05:43:12 +0000290/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
291/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
292/// and that we have already incremented Ptr by Size bytes.
293///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000294/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
295/// be updated to match.
296char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000297 const LangOptions &Features) {
298 // If we have a slash, look for an escaped newline.
299 if (Ptr[0] == '\\') {
300 ++Size;
301 ++Ptr;
302Slash:
303 // Common case, backslash-char where the char is not whitespace.
304 if (!isWhitespace(Ptr[0])) return '\\';
305
306 // See if we have optional whitespace characters followed by a newline.
307 {
308 unsigned SizeTmp = 0;
309 do {
310 ++SizeTmp;
311 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
312
313 // If this is a \r\n or \n\r, skip the newlines.
314 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
315 Ptr[SizeTmp-1] != Ptr[SizeTmp])
316 ++SizeTmp;
317
318 // Found backslash<whitespace><newline>. Parse the char after it.
319 Size += SizeTmp;
320 Ptr += SizeTmp;
321
322 // Use slow version to accumulate a correct size field.
323 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
324 }
325 } while (isWhitespace(Ptr[SizeTmp]));
326 }
327
328 // Otherwise, this is not an escaped newline, just return the slash.
329 return '\\';
330 }
331
332 // If this is a trigraph, process it.
333 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
334 // If this is actually a legal trigraph (not something like "??x"), return
335 // it.
336 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
337 Ptr += 3;
338 Size += 3;
339 if (C == '\\') goto Slash;
340 return C;
341 }
342 }
343
344 // If this is neither, return a single character.
345 ++Size;
346 return *Ptr;
347}
348
Chris Lattner22eb9722006-06-18 05:43:12 +0000349//===----------------------------------------------------------------------===//
350// Helper methods for lexing.
351//===----------------------------------------------------------------------===//
352
Chris Lattnercb283342006-06-18 06:48:37 +0000353void Lexer::LexIdentifier(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000354 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
355 unsigned Size;
356 unsigned char C = *CurPtr++;
357 while (isIdentifierBody(C)) {
358 C = *CurPtr++;
359 }
360 --CurPtr; // Back up over the skipped character.
361
362 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
363 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000364 // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000365 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
366FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000367 const char *IdStart = BufferPtr;
Chris Lattnerd01e2912006-06-18 16:22:51 +0000368 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000369 Result.SetKind(tok::identifier);
370
Chris Lattner0f1f5052006-07-20 04:16:23 +0000371 // If we are in raw mode, return this identifier raw. There is no need to
372 // look up identifier information or attempt to macro expand it.
373 if (LexingRawMode) return;
374
Chris Lattnercefc7682006-07-08 08:28:12 +0000375 // Fill in Result.IdentifierInfo, looking up the identifier in the
376 // identifier table.
377 PP.LookUpIdentifierInfo(Result, IdStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000378
Chris Lattnerc5a00062006-06-18 16:41:01 +0000379 // Finally, now that we know we have an identifier, pass this off to the
380 // preprocessor, which may macro expand it or something.
Chris Lattner22eb9722006-06-18 05:43:12 +0000381 return PP.HandleIdentifier(Result);
382 }
383
384 // Otherwise, $,\,? in identifier found. Enter slower path.
385
386 C = getCharAndSize(CurPtr, Size);
387 while (1) {
388 if (C == '$') {
389 // If we hit a $ and they are not supported in identifiers, we are done.
390 if (!Features.DollarIdents) goto FinishIdentifier;
391
392 // Otherwise, emit a diagnostic and continue.
Chris Lattnercb283342006-06-18 06:48:37 +0000393 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000394 CurPtr = ConsumeChar(CurPtr, Size, Result);
395 C = getCharAndSize(CurPtr, Size);
396 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000397 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000398 // Found end of identifier.
399 goto FinishIdentifier;
400 }
401
402 // Otherwise, this character is good, consume it.
403 CurPtr = ConsumeChar(CurPtr, Size, Result);
404
405 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000406 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000407 CurPtr = ConsumeChar(CurPtr, Size, Result);
408 C = getCharAndSize(CurPtr, Size);
409 }
410 }
411}
412
413
414/// LexNumericConstant - Lex the remainer of a integer or floating point
415/// constant. From[-1] is the first character lexed. Return the end of the
416/// constant.
Chris Lattnercb283342006-06-18 06:48:37 +0000417void Lexer::LexNumericConstant(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000418 unsigned Size;
419 char C = getCharAndSize(CurPtr, Size);
420 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000421 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000422 CurPtr = ConsumeChar(CurPtr, Size, Result);
423 PrevCh = C;
424 C = getCharAndSize(CurPtr, Size);
425 }
426
427 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
428 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
429 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
430
431 // If we have a hex FP constant, continue.
432 if (Features.HexFloats &&
433 (C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p'))
434 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
435
436 Result.SetKind(tok::numeric_constant);
437
Chris Lattnerd01e2912006-06-18 16:22:51 +0000438 // Update the location of token as well as BufferPtr.
439 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000440}
441
442/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
443/// either " or L".
Chris Lattnercb283342006-06-18 06:48:37 +0000444void Lexer::LexStringLiteral(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000445 const char *NulCharacter = 0; // Does this string contain the \0 character?
446
447 char C = getAndAdvanceChar(CurPtr, Result);
448 while (C != '"') {
449 // Skip escaped characters.
450 if (C == '\\') {
451 // Skip the escaped character.
452 C = getAndAdvanceChar(CurPtr, Result);
453 } else if (C == '\n' || C == '\r' || // Newline.
454 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000455 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner5a78a022006-07-20 06:02:19 +0000456 Result.SetKind(tok::unknown);
457 FormTokenWithChars(Result, CurPtr-1);
458 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000459 } else if (C == 0) {
460 NulCharacter = CurPtr-1;
461 }
462 C = getAndAdvanceChar(CurPtr, Result);
463 }
464
Chris Lattner5a78a022006-07-20 06:02:19 +0000465 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000466 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000467
468 Result.SetKind(tok::string_literal);
469
Chris Lattnerd01e2912006-06-18 16:22:51 +0000470 // Update the location of the token as well as the BufferPtr instance var.
471 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000472}
473
474/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
475/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattnercb283342006-06-18 06:48:37 +0000476void Lexer::LexAngledStringLiteral(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000477 const char *NulCharacter = 0; // Does this string contain the \0 character?
478
479 char C = getAndAdvanceChar(CurPtr, Result);
480 while (C != '>') {
481 // Skip escaped characters.
482 if (C == '\\') {
483 // Skip the escaped character.
484 C = getAndAdvanceChar(CurPtr, Result);
485 } else if (C == '\n' || C == '\r' || // Newline.
486 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000487 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner5a78a022006-07-20 06:02:19 +0000488 Result.SetKind(tok::unknown);
489 FormTokenWithChars(Result, CurPtr-1);
490 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000491 } else if (C == 0) {
492 NulCharacter = CurPtr-1;
493 }
494 C = getAndAdvanceChar(CurPtr, Result);
495 }
496
Chris Lattner5a78a022006-07-20 06:02:19 +0000497 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000498 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000499
500 Result.SetKind(tok::angle_string_literal);
501
Chris Lattnerd01e2912006-06-18 16:22:51 +0000502 // Update the location of token as well as BufferPtr.
503 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000504}
505
506
507/// LexCharConstant - Lex the remainder of a character constant, after having
508/// lexed either ' or L'.
Chris Lattnercb283342006-06-18 06:48:37 +0000509void Lexer::LexCharConstant(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000510 const char *NulCharacter = 0; // Does this character contain the \0 character?
511
512 // Handle the common case of 'x' and '\y' efficiently.
513 char C = getAndAdvanceChar(CurPtr, Result);
514 if (C == '\'') {
Chris Lattnera5f4c882006-07-20 06:08:47 +0000515 if (!LexingRawMode) Diag(BufferPtr, diag::err_empty_character);
Chris Lattner5a78a022006-07-20 06:02:19 +0000516 Result.SetKind(tok::unknown);
517 FormTokenWithChars(Result, CurPtr);
518 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000519 } else if (C == '\\') {
520 // Skip the escaped character.
521 // FIXME: UCN's.
522 C = getAndAdvanceChar(CurPtr, Result);
523 }
524
525 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
526 ++CurPtr;
527 } else {
528 // Fall back on generic code for embedded nulls, newlines, wide chars.
529 do {
530 // Skip escaped characters.
531 if (C == '\\') {
532 // Skip the escaped character.
533 C = getAndAdvanceChar(CurPtr, Result);
534 } else if (C == '\n' || C == '\r' || // Newline.
535 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000536 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner5a78a022006-07-20 06:02:19 +0000537 Result.SetKind(tok::unknown);
538 FormTokenWithChars(Result, CurPtr-1);
539 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000540 } else if (C == 0) {
541 NulCharacter = CurPtr-1;
542 }
543 C = getAndAdvanceChar(CurPtr, Result);
544 } while (C != '\'');
545 }
546
Chris Lattnercb283342006-06-18 06:48:37 +0000547 if (NulCharacter) Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000548
549 Result.SetKind(tok::char_constant);
550
Chris Lattnerd01e2912006-06-18 16:22:51 +0000551 // Update the location of token as well as BufferPtr.
552 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000553}
554
555/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
556/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattnercb283342006-06-18 06:48:37 +0000557void Lexer::SkipWhitespace(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000558 // Whitespace - Skip it, then return the token after the whitespace.
559 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
560 while (1) {
561 // Skip horizontal whitespace very aggressively.
562 while (isHorizontalWhitespace(Char))
563 Char = *++CurPtr;
564
565 // Otherwise if we something other than whitespace, we're done.
566 if (Char != '\n' && Char != '\r')
567 break;
568
569 if (ParsingPreprocessorDirective) {
570 // End of preprocessor directive line, let LexTokenInternal handle this.
571 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000572 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000573 }
574
575 // ok, but handle newline.
576 // The returned token is at the start of the line.
577 Result.SetFlag(LexerToken::StartOfLine);
578 // No leading whitespace seen so far.
579 Result.ClearFlag(LexerToken::LeadingSpace);
580 Char = *++CurPtr;
581 }
582
583 // If this isn't immediately after a newline, there is leading space.
584 char PrevChar = CurPtr[-1];
585 if (PrevChar != '\n' && PrevChar != '\r')
586 Result.SetFlag(LexerToken::LeadingSpace);
587
588 // If the next token is obviously a // or /* */ comment, skip it efficiently
589 // too (without going through the big switch stmt).
590 if (Char == '/' && CurPtr[1] == '/') {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000591 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000592 return SkipBCPLComment(Result, CurPtr+1);
593 }
594 if (Char == '/' && CurPtr[1] == '*') {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000595 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000596 return SkipBlockComment(Result, CurPtr+2);
597 }
598 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000599}
600
601// SkipBCPLComment - We have just read the // characters from input. Skip until
602// we find the newline character thats terminate the comment. Then update
603/// BufferPtr and return.
Chris Lattnercb283342006-06-18 06:48:37 +0000604void Lexer::SkipBCPLComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000605 // If BCPL comments aren't explicitly enabled for this language, emit an
606 // extension warning.
607 if (!Features.BCPLComment) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000608 Diag(BufferPtr, diag::ext_bcpl_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000609
610 // Mark them enabled so we only emit one warning for this translation
611 // unit.
612 Features.BCPLComment = true;
613 }
614
615 // Scan over the body of the comment. The common case, when scanning, is that
616 // the comment contains normal ascii characters with nothing interesting in
617 // them. As such, optimize for this case with the inner loop.
618 char C;
619 do {
620 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +0000621 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
622 // If we find a \n character, scan backwards, checking to see if it's an
623 // escaped newline, like we do for block comments.
Chris Lattner22eb9722006-06-18 05:43:12 +0000624
625 // Skip over characters in the fast loop.
626 while (C != 0 && // Potentially EOF.
627 C != '\\' && // Potentially escaped newline.
628 C != '?' && // Potentially trigraph.
629 C != '\n' && C != '\r') // Newline or DOS-style newline.
630 C = *++CurPtr;
631
632 // If this is a newline, we're done.
633 if (C == '\n' || C == '\r')
634 break; // Found the newline? Break out!
635
636 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
637 // properly decode the character.
638 const char *OldPtr = CurPtr;
639 C = getAndAdvanceChar(CurPtr, Result);
640
641 // If we read multiple characters, and one of those characters was a \r or
642 // \n, then we had an escaped newline within the comment. Emit diagnostic.
643 if (CurPtr != OldPtr+1) {
644 for (; OldPtr != CurPtr; ++OldPtr)
645 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnercb283342006-06-18 06:48:37 +0000646 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
647 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000648 }
649 }
650
651 if (CurPtr == BufferEnd+1) goto FoundEOF;
652 } while (C != '\n' && C != '\r');
653
654 // Found and did not consume a newline.
655
656 // If we are inside a preprocessor directive and we see the end of line,
657 // return immediately, so that the lexer can return this as an EOM token.
658 if (ParsingPreprocessorDirective) {
659 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000660 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000661 }
662
663 // Otherwise, eat the \n character. We don't care if this is a \n\r or
664 // \r\n sequence.
665 ++CurPtr;
666
667 // The next returned token is at the start of the line.
668 Result.SetFlag(LexerToken::StartOfLine);
669 // No leading whitespace seen so far.
670 Result.ClearFlag(LexerToken::LeadingSpace);
671
672 // It is common for the tokens immediately after a // comment to be
673 // whitespace (indentation for the next line). Instead of going through the
674 // big switch, handle it efficiently now.
675 if (isWhitespace(*CurPtr)) {
676 Result.SetFlag(LexerToken::LeadingSpace);
677 return SkipWhitespace(Result, CurPtr+1);
678 }
679
680 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000681 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000682
683FoundEOF: // If we ran off the end of the buffer, return EOF.
684 BufferPtr = CurPtr-1;
Chris Lattnercb283342006-06-18 06:48:37 +0000685 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000686}
687
Chris Lattnercb283342006-06-18 06:48:37 +0000688/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
689/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner22eb9722006-06-18 05:43:12 +0000690/// diagnostic if so. We know that the is inside of a block comment.
Chris Lattner1f583052006-06-18 06:53:56 +0000691static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
692 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000693 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Chris Lattner22eb9722006-06-18 05:43:12 +0000694
695 // Back up off the newline.
696 --CurPtr;
697
698 // If this is a two-character newline sequence, skip the other character.
699 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
700 // \n\n or \r\r -> not escaped newline.
701 if (CurPtr[0] == CurPtr[1])
702 return false;
703 // \n\r or \r\n -> skip the newline.
704 --CurPtr;
705 }
706
707 // If we have horizontal whitespace, skip over it. We allow whitespace
708 // between the slash and newline.
709 bool HasSpace = false;
710 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
711 --CurPtr;
712 HasSpace = true;
713 }
714
715 // If we have a slash, we know this is an escaped newline.
716 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +0000717 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000718 } else {
719 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +0000720 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
721 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +0000722 return false;
Chris Lattnercb283342006-06-18 06:48:37 +0000723
724 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +0000725 CurPtr -= 2;
726
727 // If no trigraphs are enabled, warn that we ignored this trigraph and
728 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +0000729 if (!L->getFeatures().Trigraphs) {
730 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000731 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000732 }
Chris Lattner1f583052006-06-18 06:53:56 +0000733 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000734 }
735
736 // Warn about having an escaped newline between the */ characters.
Chris Lattner1f583052006-06-18 06:53:56 +0000737 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Chris Lattner22eb9722006-06-18 05:43:12 +0000738
739 // If there was space between the backslash and newline, warn about it.
Chris Lattner1f583052006-06-18 06:53:56 +0000740 if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space);
Chris Lattner22eb9722006-06-18 05:43:12 +0000741
Chris Lattnercb283342006-06-18 06:48:37 +0000742 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000743}
744
745/// SkipBlockComment - We have just read the /* characters from input. Read
746/// until we find the */ characters that terminate the comment. Note that we
747/// don't bother decoding trigraphs or escaped newlines in block comments,
748/// because they cannot cause the comment to end. The only thing that can
749/// happen is the comment could end with an escaped newline between the */ end
750/// of comment.
Chris Lattnercb283342006-06-18 06:48:37 +0000751void Lexer::SkipBlockComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000752 // Scan one character past where we should, looking for a '/' character. Once
753 // we find it, check to see if it was preceeded by a *. This common
754 // optimization helps people who like to put a lot of * characters in their
755 // comments.
756 unsigned char C = *CurPtr++;
757 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000758 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000759 BufferPtr = CurPtr-1;
Chris Lattnercb283342006-06-18 06:48:37 +0000760 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000761 }
762
763 while (1) {
764 // Skip over all non-interesting characters.
765 // TODO: Vectorize this. Note: memchr on Darwin is slower than this loop.
766 while (C != '/' && C != '\0')
767 C = *CurPtr++;
768
769 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000770 if (CurPtr[-2] == '*') // We found the final */. We're done!
771 break;
772
773 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +0000774 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000775 // We found the final */, though it had an escaped newline between the
776 // * and /. We're done!
777 break;
778 }
779 }
780 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
781 // If this is a /* inside of the comment, emit a warning. Don't do this
782 // if this is a /*/, which will end the comment. This misses cases with
783 // embedded escaped newlines, but oh well.
Chris Lattnercb283342006-06-18 06:48:37 +0000784 Diag(CurPtr-1, diag::nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000785 }
786 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000787 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000788 // Note: the user probably forgot a */. We could continue immediately
789 // after the /*, but this would involve lexing a lot of what really is the
790 // comment, which surely would confuse the parser.
791 BufferPtr = CurPtr-1;
Chris Lattnercb283342006-06-18 06:48:37 +0000792 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000793 }
794 C = *CurPtr++;
795 }
796
797 // It is common for the tokens immediately after a /**/ comment to be
798 // whitespace. Instead of going through the big switch, handle it
799 // efficiently now.
800 if (isHorizontalWhitespace(*CurPtr)) {
801 Result.SetFlag(LexerToken::LeadingSpace);
802 return SkipWhitespace(Result, CurPtr+1);
803 }
804
805 // Otherwise, just return so that the next character will be lexed as a token.
806 BufferPtr = CurPtr;
807 Result.SetFlag(LexerToken::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000808}
809
810//===----------------------------------------------------------------------===//
811// Primary Lexing Entry Points
812//===----------------------------------------------------------------------===//
813
814/// LexIncludeFilename - After the preprocessor has parsed a #include, lex and
815/// (potentially) macro expand the filename.
Chris Lattner269c2322006-06-25 06:23:00 +0000816std::string Lexer::LexIncludeFilename(LexerToken &FilenameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000817 assert(ParsingPreprocessorDirective &&
818 ParsingFilename == false &&
819 "Must be in a preprocessing directive!");
820
821 // We are now parsing a filename!
822 ParsingFilename = true;
823
Chris Lattner269c2322006-06-25 06:23:00 +0000824 // Lex the filename.
825 Lex(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000826
827 // We should have gotten the filename now.
828 ParsingFilename = false;
829
830 // No filename?
Chris Lattner269c2322006-06-25 06:23:00 +0000831 if (FilenameTok.getKind() == tok::eom) {
Chris Lattner538d7f32006-07-20 04:31:52 +0000832 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner269c2322006-06-25 06:23:00 +0000833 return "";
Chris Lattnercb283342006-06-18 06:48:37 +0000834 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000835
Chris Lattner269c2322006-06-25 06:23:00 +0000836 // Get the text form of the filename.
837 std::string Filename = PP.getSpelling(FilenameTok);
838 assert(!Filename.empty() && "Can't have tokens with empty spellings!");
839
840 // Make sure the filename is <x> or "x".
841 if (Filename[0] == '<') {
842 if (Filename[Filename.size()-1] != '>') {
Chris Lattner538d7f32006-07-20 04:31:52 +0000843 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner269c2322006-06-25 06:23:00 +0000844 FilenameTok.SetKind(tok::eom);
845 return "";
846 }
847 } else if (Filename[0] == '"') {
848 if (Filename[Filename.size()-1] != '"') {
Chris Lattner538d7f32006-07-20 04:31:52 +0000849 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner269c2322006-06-25 06:23:00 +0000850 FilenameTok.SetKind(tok::eom);
851 return "";
852 }
853 } else {
Chris Lattner538d7f32006-07-20 04:31:52 +0000854 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner269c2322006-06-25 06:23:00 +0000855 FilenameTok.SetKind(tok::eom);
856 return "";
Chris Lattner22eb9722006-06-18 05:43:12 +0000857 }
Chris Lattner269c2322006-06-25 06:23:00 +0000858
859 // Diagnose #include "" as invalid.
860 if (Filename.size() == 2) {
Chris Lattner538d7f32006-07-20 04:31:52 +0000861 Diag(FilenameTok.getLocation(), diag::err_pp_empty_filename);
Chris Lattner269c2322006-06-25 06:23:00 +0000862 FilenameTok.SetKind(tok::eom);
863 return "";
864 }
865
866 return Filename;
Chris Lattner22eb9722006-06-18 05:43:12 +0000867}
868
869/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
870/// uninterpreted string. This switches the lexer out of directive mode.
871std::string Lexer::ReadToEndOfLine() {
872 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
873 "Must be in a preprocessing directive!");
874 std::string Result;
875 LexerToken Tmp;
876
877 // CurPtr - Cache BufferPtr in an automatic variable.
878 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000879 while (1) {
880 char Char = getAndAdvanceChar(CurPtr, Tmp);
881 switch (Char) {
882 default:
883 Result += Char;
884 break;
885 case 0: // Null.
886 // Found end of file?
887 if (CurPtr-1 != BufferEnd) {
888 // Nope, normal character, continue.
889 Result += Char;
890 break;
891 }
892 // FALL THROUGH.
893 case '\r':
894 case '\n':
895 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
896 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
897 BufferPtr = CurPtr-1;
898
899 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +0000900 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000901 assert(Tmp.getKind() == tok::eom && "Unexpected token!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000902
903 // Finally, we're done, return the string we found.
904 return Result;
905 }
906 }
907}
908
909/// LexEndOfFile - CurPtr points to the end of this file. Handle this
910/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +0000911/// This returns true if Result contains a token, false if PP.Lex should be
912/// called again.
913bool Lexer::LexEndOfFile(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000914 // If we hit the end of the file while parsing a preprocessor directive,
915 // end the preprocessor directive first. The next token returned will
916 // then be the end of file.
917 if (ParsingPreprocessorDirective) {
918 // Done parsing the "line".
919 ParsingPreprocessorDirective = false;
920 Result.SetKind(tok::eom);
Chris Lattnerd01e2912006-06-18 16:22:51 +0000921 // Update the location of token as well as BufferPtr.
922 FormTokenWithChars(Result, CurPtr);
Chris Lattner2183a6e2006-07-18 06:36:12 +0000923 return true; // Have a token.
Chris Lattner22eb9722006-06-18 05:43:12 +0000924 }
925
Chris Lattner30a2fa12006-07-19 06:31:49 +0000926 // If we are in raw mode, return this event as an EOF token. Let the caller
927 // that put us in raw mode handle the event.
928 if (LexingRawMode) {
929 Result.StartToken();
930 BufferPtr = BufferEnd;
931 FormTokenWithChars(Result, BufferEnd);
932 Result.SetKind(tok::eof);
933 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000934 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000935
Chris Lattner30a2fa12006-07-19 06:31:49 +0000936 // Otherwise, issue diagnostics for unterminated #if and missing newline.
937
938 // If we are in a #if directive, emit an error.
939 while (!ConditionalStack.empty()) {
Chris Lattner538d7f32006-07-20 04:31:52 +0000940 Diag(ConditionalStack.back().IfLoc, diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +0000941 ConditionalStack.pop_back();
942 }
943
944 // If the file was empty or didn't end in a newline, issue a pedwarn.
945 if (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
946 Diag(BufferEnd, diag::ext_no_newline_eof);
947
Chris Lattner22eb9722006-06-18 05:43:12 +0000948 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +0000949
950 // Finally, let the preprocessor handle this.
Chris Lattner2183a6e2006-07-18 06:36:12 +0000951 return PP.HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000952}
953
Chris Lattner678c8802006-07-11 05:46:12 +0000954/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
955/// the specified lexer will return a tok::l_paren token, 0 if it is something
956/// else and 2 if there are no more tokens in the buffer controlled by the
957/// lexer.
958unsigned Lexer::isNextPPTokenLParen() {
959 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
960
961 // Switch to 'skipping' mode. This will ensure that we can lex a token
962 // without emitting diagnostics, disables macro expansion, and will cause EOF
963 // to return an EOF token instead of popping the include stack.
964 LexingRawMode = true;
965
966 // Save state that can be changed while lexing so that we can restore it.
967 const char *TmpBufferPtr = BufferPtr;
968
969 LexerToken Tok;
970 Tok.StartToken();
971 LexTokenInternal(Tok);
972
973 // Restore state that may have changed.
974 BufferPtr = TmpBufferPtr;
975
976 // Restore the lexer back to non-skipping mode.
977 LexingRawMode = false;
978
979 if (Tok.getKind() == tok::eof)
980 return 2;
981 return Tok.getKind() == tok::l_paren;
982}
983
Chris Lattner22eb9722006-06-18 05:43:12 +0000984
985/// LexTokenInternal - This implements a simple C family lexer. It is an
986/// extremely performance critical piece of code. This assumes that the buffer
987/// has a null character at the end of the file. Return true if an error
988/// occurred and compilation should terminate, false if normal. This returns a
989/// preprocessing token, not a normal token, as such, it is an internal
990/// interface. It assumes that the Flags of result have been cleared before
991/// calling this.
Chris Lattnercb283342006-06-18 06:48:37 +0000992void Lexer::LexTokenInternal(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000993LexNextToken:
994 // New token, can't need cleaning yet.
995 Result.ClearFlag(LexerToken::NeedsCleaning);
Chris Lattner27746e42006-07-05 00:07:54 +0000996 Result.SetIdentifierInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +0000997
998 // CurPtr - Cache BufferPtr in an automatic variable.
999 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001000
Chris Lattnereb54b592006-07-10 06:34:27 +00001001 // Small amounts of horizontal whitespace is very common between tokens.
1002 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1003 ++CurPtr;
1004 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1005 ++CurPtr;
1006 BufferPtr = CurPtr;
1007 Result.SetFlag(LexerToken::LeadingSpace);
1008 }
1009
Chris Lattner22eb9722006-06-18 05:43:12 +00001010 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1011
1012 // Read a character, advancing over it.
1013 char Char = getAndAdvanceChar(CurPtr, Result);
1014 switch (Char) {
1015 case 0: // Null.
1016 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001017 if (CurPtr-1 == BufferEnd) {
1018 // Read the PP instance variable into an automatic variable, because
1019 // LexEndOfFile will often delete 'this'.
1020 Preprocessor &PPCache = PP;
1021 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1022 return; // Got a token to return.
1023 return PPCache.Lex(Result);
1024 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001025
Chris Lattnercb283342006-06-18 06:48:37 +00001026 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner22eb9722006-06-18 05:43:12 +00001027 Result.SetFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001028 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001029 goto LexNextToken; // GCC isn't tail call eliminating.
1030 case '\n':
1031 case '\r':
1032 // If we are inside a preprocessor directive and we see the end of line,
1033 // we know we are done with the directive, so return an EOM token.
1034 if (ParsingPreprocessorDirective) {
1035 // Done parsing the "line".
1036 ParsingPreprocessorDirective = false;
1037
1038 // Since we consumed a newline, we are back at the start of a line.
1039 IsAtStartOfLine = true;
1040
1041 Result.SetKind(tok::eom);
1042 break;
1043 }
1044 // The returned token is at the start of the line.
1045 Result.SetFlag(LexerToken::StartOfLine);
1046 // No leading whitespace seen so far.
1047 Result.ClearFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001048 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001049 goto LexNextToken; // GCC isn't tail call eliminating.
1050 case ' ':
1051 case '\t':
1052 case '\f':
1053 case '\v':
1054 Result.SetFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001055 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001056 goto LexNextToken; // GCC isn't tail call eliminating.
1057
1058 case 'L':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001059 // Notify MIOpt that we read a non-whitespace/non-comment token.
1060 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001061 Char = getCharAndSize(CurPtr, SizeTmp);
1062
1063 // Wide string literal.
1064 if (Char == '"')
1065 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1066
1067 // Wide character constant.
1068 if (Char == '\'')
1069 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1070 // FALL THROUGH, treating L like the start of an identifier.
1071
1072 // C99 6.4.2: Identifiers.
1073 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1074 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1075 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1076 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1077 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1078 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1079 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1080 case 'v': case 'w': case 'x': case 'y': case 'z':
1081 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001082 // Notify MIOpt that we read a non-whitespace/non-comment token.
1083 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001084 return LexIdentifier(Result, CurPtr);
1085
1086 // C99 6.4.4.1: Integer Constants.
1087 // C99 6.4.4.2: Floating Constants.
1088 case '0': case '1': case '2': case '3': case '4':
1089 case '5': case '6': case '7': case '8': case '9':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001090 // Notify MIOpt that we read a non-whitespace/non-comment token.
1091 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001092 return LexNumericConstant(Result, CurPtr);
1093
1094 // C99 6.4.4: Character Constants.
1095 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001096 // Notify MIOpt that we read a non-whitespace/non-comment token.
1097 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001098 return LexCharConstant(Result, CurPtr);
1099
1100 // C99 6.4.5: String Literals.
1101 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001102 // Notify MIOpt that we read a non-whitespace/non-comment token.
1103 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001104 return LexStringLiteral(Result, CurPtr);
1105
1106 // C99 6.4.6: Punctuators.
1107 case '?':
1108 Result.SetKind(tok::question);
1109 break;
1110 case '[':
1111 Result.SetKind(tok::l_square);
1112 break;
1113 case ']':
1114 Result.SetKind(tok::r_square);
1115 break;
1116 case '(':
1117 Result.SetKind(tok::l_paren);
1118 break;
1119 case ')':
1120 Result.SetKind(tok::r_paren);
1121 break;
1122 case '{':
1123 Result.SetKind(tok::l_brace);
1124 break;
1125 case '}':
1126 Result.SetKind(tok::r_brace);
1127 break;
1128 case '.':
1129 Char = getCharAndSize(CurPtr, SizeTmp);
1130 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001131 // Notify MIOpt that we read a non-whitespace/non-comment token.
1132 MIOpt.ReadToken();
1133
Chris Lattner22eb9722006-06-18 05:43:12 +00001134 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1135 } else if (Features.CPlusPlus && Char == '*') {
1136 Result.SetKind(tok::periodstar);
1137 CurPtr += SizeTmp;
1138 } else if (Char == '.' &&
1139 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
1140 Result.SetKind(tok::ellipsis);
1141 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1142 SizeTmp2, Result);
1143 } else {
1144 Result.SetKind(tok::period);
1145 }
1146 break;
1147 case '&':
1148 Char = getCharAndSize(CurPtr, SizeTmp);
1149 if (Char == '&') {
1150 Result.SetKind(tok::ampamp);
1151 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1152 } else if (Char == '=') {
1153 Result.SetKind(tok::ampequal);
1154 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1155 } else {
1156 Result.SetKind(tok::amp);
1157 }
1158 break;
1159 case '*':
1160 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
1161 Result.SetKind(tok::starequal);
1162 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1163 } else {
1164 Result.SetKind(tok::star);
1165 }
1166 break;
1167 case '+':
1168 Char = getCharAndSize(CurPtr, SizeTmp);
1169 if (Char == '+') {
1170 Result.SetKind(tok::plusplus);
1171 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1172 } else if (Char == '=') {
1173 Result.SetKind(tok::plusequal);
1174 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1175 } else {
1176 Result.SetKind(tok::plus);
1177 }
1178 break;
1179 case '-':
1180 Char = getCharAndSize(CurPtr, SizeTmp);
1181 if (Char == '-') {
1182 Result.SetKind(tok::minusminus);
1183 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1184 } else if (Char == '>' && Features.CPlusPlus &&
1185 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {
1186 Result.SetKind(tok::arrowstar); // C++ ->*
1187 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1188 SizeTmp2, Result);
1189 } else if (Char == '>') {
1190 Result.SetKind(tok::arrow);
1191 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1192 } else if (Char == '=') {
1193 Result.SetKind(tok::minusequal);
1194 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1195 } else {
1196 Result.SetKind(tok::minus);
1197 }
1198 break;
1199 case '~':
1200 Result.SetKind(tok::tilde);
1201 break;
1202 case '!':
1203 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
1204 Result.SetKind(tok::exclaimequal);
1205 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1206 } else {
1207 Result.SetKind(tok::exclaim);
1208 }
1209 break;
1210 case '/':
1211 // 6.4.9: Comments
1212 Char = getCharAndSize(CurPtr, SizeTmp);
1213 if (Char == '/') { // BCPL comment.
1214 Result.SetFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001215 SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result));
Chris Lattner22eb9722006-06-18 05:43:12 +00001216 goto LexNextToken; // GCC isn't tail call eliminating.
1217 } else if (Char == '*') { // /**/ comment.
1218 Result.SetFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001219 SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result));
Chris Lattner22eb9722006-06-18 05:43:12 +00001220 goto LexNextToken; // GCC isn't tail call eliminating.
1221 } else if (Char == '=') {
1222 Result.SetKind(tok::slashequal);
1223 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1224 } else {
1225 Result.SetKind(tok::slash);
1226 }
1227 break;
1228 case '%':
1229 Char = getCharAndSize(CurPtr, SizeTmp);
1230 if (Char == '=') {
1231 Result.SetKind(tok::percentequal);
1232 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1233 } else if (Features.Digraphs && Char == '>') {
1234 Result.SetKind(tok::r_brace); // '%>' -> '}'
1235 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1236 } else if (Features.Digraphs && Char == ':') {
1237 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001238 Char = getCharAndSize(CurPtr, SizeTmp);
1239 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001240 Result.SetKind(tok::hashhash); // '%:%:' -> '##'
1241 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1242 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001243 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
1244 Result.SetKind(tok::hashat);
1245 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1246 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner22eb9722006-06-18 05:43:12 +00001247 } else {
1248 Result.SetKind(tok::hash); // '%:' -> '#'
1249
1250 // We parsed a # character. If this occurs at the start of the line,
1251 // it's actually the start of a preprocessing directive. Callback to
1252 // the preprocessor to handle it.
1253 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001254 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001255 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001256 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001257
1258 // As an optimization, if the preprocessor didn't switch lexers, tail
1259 // recurse.
1260 if (PP.isCurrentLexer(this)) {
1261 // Start a new token. If this is a #include or something, the PP may
1262 // want us starting at the beginning of the line again. If so, set
1263 // the StartOfLine flag.
1264 if (IsAtStartOfLine) {
1265 Result.SetFlag(LexerToken::StartOfLine);
1266 IsAtStartOfLine = false;
1267 }
1268 goto LexNextToken; // GCC isn't tail call eliminating.
1269 }
1270
1271 return PP.Lex(Result);
1272 }
1273 }
1274 } else {
1275 Result.SetKind(tok::percent);
1276 }
1277 break;
1278 case '<':
1279 Char = getCharAndSize(CurPtr, SizeTmp);
1280 if (ParsingFilename) {
1281 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1282 } else if (Char == '<' &&
1283 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
1284 Result.SetKind(tok::lesslessequal);
1285 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1286 SizeTmp2, Result);
1287 } else if (Char == '<') {
1288 Result.SetKind(tok::lessless);
1289 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1290 } else if (Char == '=') {
1291 Result.SetKind(tok::lessequal);
1292 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1293 } else if (Features.Digraphs && Char == ':') {
1294 Result.SetKind(tok::l_square); // '<:' -> '['
1295 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1296 } else if (Features.Digraphs && Char == '>') {
1297 Result.SetKind(tok::l_brace); // '<%' -> '{'
1298 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1299 } else if (Features.CPPMinMax && Char == '?') { // <?
1300 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerd01e2912006-06-18 16:22:51 +00001301 Diag(BufferPtr, diag::min_max_deprecated);
Chris Lattner22eb9722006-06-18 05:43:12 +00001302
1303 if (getCharAndSize(CurPtr, SizeTmp) == '=') { // <?=
1304 Result.SetKind(tok::lessquestionequal);
1305 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1306 } else {
1307 Result.SetKind(tok::lessquestion);
1308 }
1309 } else {
1310 Result.SetKind(tok::less);
1311 }
1312 break;
1313 case '>':
1314 Char = getCharAndSize(CurPtr, SizeTmp);
1315 if (Char == '=') {
1316 Result.SetKind(tok::greaterequal);
1317 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1318 } else if (Char == '>' &&
1319 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
1320 Result.SetKind(tok::greatergreaterequal);
1321 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1322 SizeTmp2, Result);
1323 } else if (Char == '>') {
1324 Result.SetKind(tok::greatergreater);
1325 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1326 } else if (Features.CPPMinMax && Char == '?') {
1327 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerd01e2912006-06-18 16:22:51 +00001328 Diag(BufferPtr, diag::min_max_deprecated);
Chris Lattner22eb9722006-06-18 05:43:12 +00001329
1330 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
1331 Result.SetKind(tok::greaterquestionequal); // >?=
1332 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1333 } else {
1334 Result.SetKind(tok::greaterquestion); // >?
1335 }
1336 } else {
1337 Result.SetKind(tok::greater);
1338 }
1339 break;
1340 case '^':
1341 Char = getCharAndSize(CurPtr, SizeTmp);
1342 if (Char == '=') {
1343 Result.SetKind(tok::caretequal);
1344 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1345 } else {
1346 Result.SetKind(tok::caret);
1347 }
1348 break;
1349 case '|':
1350 Char = getCharAndSize(CurPtr, SizeTmp);
1351 if (Char == '=') {
1352 Result.SetKind(tok::pipeequal);
1353 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1354 } else if (Char == '|') {
1355 Result.SetKind(tok::pipepipe);
1356 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1357 } else {
1358 Result.SetKind(tok::pipe);
1359 }
1360 break;
1361 case ':':
1362 Char = getCharAndSize(CurPtr, SizeTmp);
1363 if (Features.Digraphs && Char == '>') {
1364 Result.SetKind(tok::r_square); // ':>' -> ']'
1365 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1366 } else if (Features.CPlusPlus && Char == ':') {
1367 Result.SetKind(tok::coloncolon);
1368 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1369 } else {
1370 Result.SetKind(tok::colon);
1371 }
1372 break;
1373 case ';':
1374 Result.SetKind(tok::semi);
1375 break;
1376 case '=':
1377 Char = getCharAndSize(CurPtr, SizeTmp);
1378 if (Char == '=') {
1379 Result.SetKind(tok::equalequal);
1380 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1381 } else {
1382 Result.SetKind(tok::equal);
1383 }
1384 break;
1385 case ',':
1386 Result.SetKind(tok::comma);
1387 break;
1388 case '#':
1389 Char = getCharAndSize(CurPtr, SizeTmp);
1390 if (Char == '#') {
1391 Result.SetKind(tok::hashhash);
1392 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001393 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
1394 Result.SetKind(tok::hashat);
1395 Diag(BufferPtr, diag::charize_microsoft_ext);
1396 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001397 } else {
1398 Result.SetKind(tok::hash);
1399 // We parsed a # character. If this occurs at the start of the line,
1400 // it's actually the start of a preprocessing directive. Callback to
1401 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00001402 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001403 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001404 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001405 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001406
1407 // As an optimization, if the preprocessor didn't switch lexers, tail
1408 // recurse.
1409 if (PP.isCurrentLexer(this)) {
1410 // Start a new token. If this is a #include or something, the PP may
1411 // want us starting at the beginning of the line again. If so, set
1412 // the StartOfLine flag.
1413 if (IsAtStartOfLine) {
1414 Result.SetFlag(LexerToken::StartOfLine);
1415 IsAtStartOfLine = false;
1416 }
1417 goto LexNextToken; // GCC isn't tail call eliminating.
1418 }
1419 return PP.Lex(Result);
1420 }
1421 }
1422 break;
1423
1424 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00001425 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00001426 // FALL THROUGH.
1427 default:
1428 // Objective C support.
1429 if (CurPtr[-1] == '@' && Features.ObjC1) {
1430 Result.SetKind(tok::at);
1431 break;
1432 } else if (CurPtr[-1] == '$' && Features.DollarIdents) {// $ in identifiers.
Chris Lattnercb283342006-06-18 06:48:37 +00001433 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001434 // Notify MIOpt that we read a non-whitespace/non-comment token.
1435 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001436 return LexIdentifier(Result, CurPtr);
1437 }
1438
Chris Lattner041bef82006-07-11 05:52:53 +00001439 Result.SetKind(tok::unknown);
1440 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001441 }
1442
Chris Lattner371ac8a2006-07-04 07:11:10 +00001443 // Notify MIOpt that we read a non-whitespace/non-comment token.
1444 MIOpt.ReadToken();
1445
Chris Lattnerd01e2912006-06-18 16:22:51 +00001446 // Update the location of token as well as BufferPtr.
1447 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001448}