blob: 3c4bb5359485d39ec12631ae75807c28c748dc03 [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:
22// ERROR : attempt to use poisoned \"%s\"
23//
24// TODO: Options to support:
25// -fexec-charset,-fwide-exec-charset
26//
27//===----------------------------------------------------------------------===//
28
29#include "clang/Lex/Lexer.h"
30#include "clang/Lex/Preprocessor.h"
31#include "clang/Basic/Diagnostic.h"
32#include "clang/Basic/SourceBuffer.h"
33#include "clang/Basic/SourceLocation.h"
34#include "llvm/Config/alloca.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000035#include <cctype>
36#include <iostream>
37using namespace llvm;
38using namespace clang;
39
40static void InitCharacterInfo();
41
42Lexer::Lexer(const SourceBuffer *File, unsigned fileid, Preprocessor &pp)
43 : BufferPtr(File->getBufferStart()), BufferStart(BufferPtr),
44 BufferEnd(File->getBufferEnd()), InputFile(File), CurFileID(fileid), PP(pp),
45 Features(PP.getLangOptions()) {
46 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!");
51
52 // Start of the file is a start of line.
53 IsAtStartOfLine = true;
54
55 // We are not after parsing a #.
56 ParsingPreprocessorDirective = false;
57
58 // We are not after parsing #include.
59 ParsingFilename = false;
60}
61
62//===----------------------------------------------------------------------===//
63// LexerToken implementation.
64//===----------------------------------------------------------------------===//
65
Chris Lattner22eb9722006-06-18 05:43:12 +000066//===----------------------------------------------------------------------===//
67// Character information.
68//===----------------------------------------------------------------------===//
69
70static unsigned char CharInfo[256];
71
72enum {
73 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
74 CHAR_VERT_WS = 0x02, // '\r', '\n'
75 CHAR_LETTER = 0x04, // a-z,A-Z
76 CHAR_NUMBER = 0x08, // 0-9
77 CHAR_UNDER = 0x10, // _
78 CHAR_PERIOD = 0x20 // .
79};
80
81static void InitCharacterInfo() {
82 static bool isInited = false;
83 if (isInited) return;
84 isInited = true;
85
86 // Intiialize the CharInfo table.
87 // TODO: statically initialize this.
88 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
89 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
90 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
91
92 CharInfo[(int)'_'] = CHAR_UNDER;
93 for (unsigned i = 'a'; i <= 'z'; ++i)
94 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
95 for (unsigned i = '0'; i <= '9'; ++i)
96 CharInfo[i] = CHAR_NUMBER;
97}
98
99/// isIdentifierBody - Return true if this is the body character of an
100/// identifier, which is [a-zA-Z0-9_].
101static inline bool isIdentifierBody(unsigned char c) {
102 return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER);
103}
104
105/// isHorizontalWhitespace - Return true if this character is horizontal
106/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
107static inline bool isHorizontalWhitespace(unsigned char c) {
108 return CharInfo[c] & CHAR_HORZ_WS;
109}
110
111/// isWhitespace - Return true if this character is horizontal or vertical
112/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
113/// for '\0'.
114static inline bool isWhitespace(unsigned char c) {
115 return CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS);
116}
117
118/// isNumberBody - Return true if this is the body character of an
119/// preprocessing number, which is [a-zA-Z0-9_.].
120static inline bool isNumberBody(unsigned char c) {
121 return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD);
122}
123
Chris Lattnerd01e2912006-06-18 16:22:51 +0000124
Chris Lattner22eb9722006-06-18 05:43:12 +0000125//===----------------------------------------------------------------------===//
126// Diagnostics forwarding code.
127//===----------------------------------------------------------------------===//
128
129/// getSourceLocation - Return a source location identifier for the specified
130/// offset in the current file.
131SourceLocation Lexer::getSourceLocation(const char *Loc) const {
132 assert(Loc >= InputFile->getBufferStart() && Loc <= InputFile->getBufferEnd()
133 && "Location out of range for this buffer!");
134 return SourceLocation(CurFileID, Loc-InputFile->getBufferStart());
135}
136
137
138/// Diag - Forwarding function for diagnostics. This translate a source
139/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000140void Lexer::Diag(const char *Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000141 const std::string &Msg) const {
Chris Lattnercb283342006-06-18 06:48:37 +0000142 PP.Diag(getSourceLocation(Loc), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000143}
144
145//===----------------------------------------------------------------------===//
146// Trigraph and Escaped Newline Handling Code.
147//===----------------------------------------------------------------------===//
148
149/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
150/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
151static char GetTrigraphCharForLetter(char Letter) {
152 switch (Letter) {
153 default: return 0;
154 case '=': return '#';
155 case ')': return ']';
156 case '(': return '[';
157 case '!': return '|';
158 case '\'': return '^';
159 case '>': return '}';
160 case '/': return '\\';
161 case '<': return '{';
162 case '-': return '~';
163 }
164}
165
166/// DecodeTrigraphChar - If the specified character is a legal trigraph when
167/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
168/// return the result character. Finally, emit a warning about trigraph use
169/// whether trigraphs are enabled or not.
170static char DecodeTrigraphChar(const char *CP, Lexer *L) {
171 char Res = GetTrigraphCharForLetter(*CP);
172 if (Res && L) {
173 if (!L->getFeatures().Trigraphs) {
174 L->Diag(CP-2, diag::trigraph_ignored);
175 return 0;
176 } else {
177 L->Diag(CP-2, diag::trigraph_converted, std::string()+Res);
178 }
179 }
180 return Res;
181}
182
183/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
184/// get its size, and return it. This is tricky in several cases:
185/// 1. If currently at the start of a trigraph, we warn about the trigraph,
186/// then either return the trigraph (skipping 3 chars) or the '?',
187/// depending on whether trigraphs are enabled or not.
188/// 2. If this is an escaped newline (potentially with whitespace between
189/// the backslash and newline), implicitly skip the newline and return
190/// the char after it.
191/// 3. If this is a UCN, return it. FIXME: for C++?
192///
193/// This handles the slow/uncommon case of the getCharAndSize method. Here we
194/// know that we can accumulate into Size, and that we have already incremented
195/// Ptr by Size bytes.
196///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000197/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
198/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000199///
200char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
201 LexerToken *Tok) {
202 // If we have a slash, look for an escaped newline.
203 if (Ptr[0] == '\\') {
204 ++Size;
205 ++Ptr;
206Slash:
207 // Common case, backslash-char where the char is not whitespace.
208 if (!isWhitespace(Ptr[0])) return '\\';
209
210 // See if we have optional whitespace characters followed by a newline.
211 {
212 unsigned SizeTmp = 0;
213 do {
214 ++SizeTmp;
215 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
216 // Remember that this token needs to be cleaned.
217 if (Tok) Tok->SetFlag(LexerToken::NeedsCleaning);
218
219 // Warn if there was whitespace between the backslash and newline.
220 if (SizeTmp != 1 && Tok)
221 Diag(Ptr, diag::backslash_newline_space);
222
223 // If this is a \r\n or \n\r, skip the newlines.
224 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
225 Ptr[SizeTmp-1] != Ptr[SizeTmp])
226 ++SizeTmp;
227
228 // Found backslash<whitespace><newline>. Parse the char after it.
229 Size += SizeTmp;
230 Ptr += SizeTmp;
231 // Use slow version to accumulate a correct size field.
232 return getCharAndSizeSlow(Ptr, Size, Tok);
233 }
234 } while (isWhitespace(Ptr[SizeTmp]));
235 }
236
237 // Otherwise, this is not an escaped newline, just return the slash.
238 return '\\';
239 }
240
241 // If this is a trigraph, process it.
242 if (Ptr[0] == '?' && Ptr[1] == '?') {
243 // If this is actually a legal trigraph (not something like "??x"), emit
244 // a trigraph warning. If so, and if trigraphs are enabled, return it.
245 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
246 // Remember that this token needs to be cleaned.
247 if (Tok) Tok->SetFlag(LexerToken::NeedsCleaning);
248
249 Ptr += 3;
250 Size += 3;
251 if (C == '\\') goto Slash;
252 return C;
253 }
254 }
255
256 // If this is neither, return a single character.
257 ++Size;
258 return *Ptr;
259}
260
Chris Lattnerd01e2912006-06-18 16:22:51 +0000261
Chris Lattner22eb9722006-06-18 05:43:12 +0000262/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
263/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
264/// and that we have already incremented Ptr by Size bytes.
265///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000266/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
267/// be updated to match.
268char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000269 const LangOptions &Features) {
270 // If we have a slash, look for an escaped newline.
271 if (Ptr[0] == '\\') {
272 ++Size;
273 ++Ptr;
274Slash:
275 // Common case, backslash-char where the char is not whitespace.
276 if (!isWhitespace(Ptr[0])) return '\\';
277
278 // See if we have optional whitespace characters followed by a newline.
279 {
280 unsigned SizeTmp = 0;
281 do {
282 ++SizeTmp;
283 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
284
285 // If this is a \r\n or \n\r, skip the newlines.
286 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
287 Ptr[SizeTmp-1] != Ptr[SizeTmp])
288 ++SizeTmp;
289
290 // Found backslash<whitespace><newline>. Parse the char after it.
291 Size += SizeTmp;
292 Ptr += SizeTmp;
293
294 // Use slow version to accumulate a correct size field.
295 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
296 }
297 } while (isWhitespace(Ptr[SizeTmp]));
298 }
299
300 // Otherwise, this is not an escaped newline, just return the slash.
301 return '\\';
302 }
303
304 // If this is a trigraph, process it.
305 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
306 // If this is actually a legal trigraph (not something like "??x"), return
307 // it.
308 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
309 Ptr += 3;
310 Size += 3;
311 if (C == '\\') goto Slash;
312 return C;
313 }
314 }
315
316 // If this is neither, return a single character.
317 ++Size;
318 return *Ptr;
319}
320
Chris Lattner22eb9722006-06-18 05:43:12 +0000321//===----------------------------------------------------------------------===//
322// Helper methods for lexing.
323//===----------------------------------------------------------------------===//
324
Chris Lattnercb283342006-06-18 06:48:37 +0000325void Lexer::LexIdentifier(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000326 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
327 unsigned Size;
328 unsigned char C = *CurPtr++;
329 while (isIdentifierBody(C)) {
330 C = *CurPtr++;
331 }
332 --CurPtr; // Back up over the skipped character.
333
334 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
335 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
336 // FIXME: universal chars.
337 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
338FinishIdentifier:
Chris Lattnerd01e2912006-06-18 16:22:51 +0000339 const char *IdStart = BufferPtr, *IdEnd = CurPtr;
340 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000341 Result.SetKind(tok::identifier);
342
343 // Look up this token, see if it is a macro, or if it is a language keyword.
Chris Lattnerc5a00062006-06-18 16:41:01 +0000344 IdentifierTokenInfo *II;
Chris Lattner22eb9722006-06-18 05:43:12 +0000345 if (!Result.needsCleaning()) {
346 // No cleaning needed, just use the characters from the lexed buffer.
Chris Lattnerc5a00062006-06-18 16:41:01 +0000347 II = PP.getIdentifierInfo(IdStart, IdEnd);
Chris Lattner22eb9722006-06-18 05:43:12 +0000348 } else {
349 // Cleaning needed, alloca a buffer, clean into it, then use the buffer.
Chris Lattner33ce7282006-06-18 07:35:33 +0000350 char *TmpBuf = (char*)alloca(Result.getLength());
Chris Lattnerd01e2912006-06-18 16:22:51 +0000351 unsigned Size = PP.getSpelling(Result, TmpBuf);
Chris Lattnerc5a00062006-06-18 16:41:01 +0000352 II = PP.getIdentifierInfo(TmpBuf, TmpBuf+Size);
Chris Lattner22eb9722006-06-18 05:43:12 +0000353 }
Chris Lattnerc5a00062006-06-18 16:41:01 +0000354 Result.SetIdentifierInfo(II);
Chris Lattner22eb9722006-06-18 05:43:12 +0000355
Chris Lattnerc5a00062006-06-18 16:41:01 +0000356 // Finally, now that we know we have an identifier, pass this off to the
357 // preprocessor, which may macro expand it or something.
Chris Lattner22eb9722006-06-18 05:43:12 +0000358 return PP.HandleIdentifier(Result);
359 }
360
361 // Otherwise, $,\,? in identifier found. Enter slower path.
362
363 C = getCharAndSize(CurPtr, Size);
364 while (1) {
365 if (C == '$') {
366 // If we hit a $ and they are not supported in identifiers, we are done.
367 if (!Features.DollarIdents) goto FinishIdentifier;
368
369 // Otherwise, emit a diagnostic and continue.
Chris Lattnercb283342006-06-18 06:48:37 +0000370 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000371 CurPtr = ConsumeChar(CurPtr, Size, Result);
372 C = getCharAndSize(CurPtr, Size);
373 continue;
374 } else if (!isIdentifierBody(C)) { // FIXME: universal chars.
375 // Found end of identifier.
376 goto FinishIdentifier;
377 }
378
379 // Otherwise, this character is good, consume it.
380 CurPtr = ConsumeChar(CurPtr, Size, Result);
381
382 C = getCharAndSize(CurPtr, Size);
383 while (isIdentifierBody(C)) { // FIXME: universal chars.
384 CurPtr = ConsumeChar(CurPtr, Size, Result);
385 C = getCharAndSize(CurPtr, Size);
386 }
387 }
388}
389
390
391/// LexNumericConstant - Lex the remainer of a integer or floating point
392/// constant. From[-1] is the first character lexed. Return the end of the
393/// constant.
Chris Lattnercb283342006-06-18 06:48:37 +0000394void Lexer::LexNumericConstant(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000395 unsigned Size;
396 char C = getCharAndSize(CurPtr, Size);
397 char PrevCh = 0;
398 while (isNumberBody(C)) { // FIXME: universal chars?
399 CurPtr = ConsumeChar(CurPtr, Size, Result);
400 PrevCh = C;
401 C = getCharAndSize(CurPtr, Size);
402 }
403
404 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
405 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
406 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
407
408 // If we have a hex FP constant, continue.
409 if (Features.HexFloats &&
410 (C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p'))
411 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
412
413 Result.SetKind(tok::numeric_constant);
414
Chris Lattnerd01e2912006-06-18 16:22:51 +0000415 // Update the location of token as well as BufferPtr.
416 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000417}
418
419/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
420/// either " or L".
Chris Lattnercb283342006-06-18 06:48:37 +0000421void Lexer::LexStringLiteral(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000422 const char *NulCharacter = 0; // Does this string contain the \0 character?
423
424 char C = getAndAdvanceChar(CurPtr, Result);
425 while (C != '"') {
426 // Skip escaped characters.
427 if (C == '\\') {
428 // Skip the escaped character.
429 C = getAndAdvanceChar(CurPtr, Result);
430 } else if (C == '\n' || C == '\r' || // Newline.
431 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000432 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000433 BufferPtr = CurPtr-1;
434 return LexTokenInternal(Result);
435 } else if (C == 0) {
436 NulCharacter = CurPtr-1;
437 }
438 C = getAndAdvanceChar(CurPtr, Result);
439 }
440
Chris Lattnercb283342006-06-18 06:48:37 +0000441 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000442
443 Result.SetKind(tok::string_literal);
444
Chris Lattnerd01e2912006-06-18 16:22:51 +0000445 // Update the location of the token as well as the BufferPtr instance var.
446 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000447}
448
449/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
450/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattnercb283342006-06-18 06:48:37 +0000451void Lexer::LexAngledStringLiteral(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000452 const char *NulCharacter = 0; // Does this string contain the \0 character?
453
454 char C = getAndAdvanceChar(CurPtr, Result);
455 while (C != '>') {
456 // Skip escaped characters.
457 if (C == '\\') {
458 // Skip the escaped character.
459 C = getAndAdvanceChar(CurPtr, Result);
460 } else if (C == '\n' || C == '\r' || // Newline.
461 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000462 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000463 BufferPtr = CurPtr-1;
464 return LexTokenInternal(Result);
465 } else if (C == 0) {
466 NulCharacter = CurPtr-1;
467 }
468 C = getAndAdvanceChar(CurPtr, Result);
469 }
470
Chris Lattnercb283342006-06-18 06:48:37 +0000471 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000472
473 Result.SetKind(tok::angle_string_literal);
474
Chris Lattnerd01e2912006-06-18 16:22:51 +0000475 // Update the location of token as well as BufferPtr.
476 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000477}
478
479
480/// LexCharConstant - Lex the remainder of a character constant, after having
481/// lexed either ' or L'.
Chris Lattnercb283342006-06-18 06:48:37 +0000482void Lexer::LexCharConstant(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000483 const char *NulCharacter = 0; // Does this character contain the \0 character?
484
485 // Handle the common case of 'x' and '\y' efficiently.
486 char C = getAndAdvanceChar(CurPtr, Result);
487 if (C == '\'') {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000488 Diag(BufferPtr, diag::err_empty_character);
Chris Lattner22eb9722006-06-18 05:43:12 +0000489 BufferPtr = CurPtr;
490 return LexTokenInternal(Result);
491 } else if (C == '\\') {
492 // Skip the escaped character.
493 // FIXME: UCN's.
494 C = getAndAdvanceChar(CurPtr, Result);
495 }
496
497 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
498 ++CurPtr;
499 } else {
500 // Fall back on generic code for embedded nulls, newlines, wide chars.
501 do {
502 // Skip escaped characters.
503 if (C == '\\') {
504 // Skip the escaped character.
505 C = getAndAdvanceChar(CurPtr, Result);
506 } else if (C == '\n' || C == '\r' || // Newline.
507 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerd01e2912006-06-18 16:22:51 +0000508 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000509 BufferPtr = CurPtr-1;
510 return LexTokenInternal(Result);
511 } else if (C == 0) {
512 NulCharacter = CurPtr-1;
513 }
514 C = getAndAdvanceChar(CurPtr, Result);
515 } while (C != '\'');
516 }
517
Chris Lattnercb283342006-06-18 06:48:37 +0000518 if (NulCharacter) Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000519
520 Result.SetKind(tok::char_constant);
521
Chris Lattnerd01e2912006-06-18 16:22:51 +0000522 // Update the location of token as well as BufferPtr.
523 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000524}
525
526/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
527/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattnercb283342006-06-18 06:48:37 +0000528void Lexer::SkipWhitespace(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000529 // Whitespace - Skip it, then return the token after the whitespace.
530 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
531 while (1) {
532 // Skip horizontal whitespace very aggressively.
533 while (isHorizontalWhitespace(Char))
534 Char = *++CurPtr;
535
536 // Otherwise if we something other than whitespace, we're done.
537 if (Char != '\n' && Char != '\r')
538 break;
539
540 if (ParsingPreprocessorDirective) {
541 // End of preprocessor directive line, let LexTokenInternal handle this.
542 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000543 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000544 }
545
546 // ok, but handle newline.
547 // The returned token is at the start of the line.
548 Result.SetFlag(LexerToken::StartOfLine);
549 // No leading whitespace seen so far.
550 Result.ClearFlag(LexerToken::LeadingSpace);
551 Char = *++CurPtr;
552 }
553
554 // If this isn't immediately after a newline, there is leading space.
555 char PrevChar = CurPtr[-1];
556 if (PrevChar != '\n' && PrevChar != '\r')
557 Result.SetFlag(LexerToken::LeadingSpace);
558
559 // If the next token is obviously a // or /* */ comment, skip it efficiently
560 // too (without going through the big switch stmt).
561 if (Char == '/' && CurPtr[1] == '/') {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000562 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000563 return SkipBCPLComment(Result, CurPtr+1);
564 }
565 if (Char == '/' && CurPtr[1] == '*') {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000566 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000567 return SkipBlockComment(Result, CurPtr+2);
568 }
569 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000570}
571
572// SkipBCPLComment - We have just read the // characters from input. Skip until
573// we find the newline character thats terminate the comment. Then update
574/// BufferPtr and return.
Chris Lattnercb283342006-06-18 06:48:37 +0000575void Lexer::SkipBCPLComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000576 // If BCPL comments aren't explicitly enabled for this language, emit an
577 // extension warning.
578 if (!Features.BCPLComment) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000579 Diag(BufferPtr, diag::ext_bcpl_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000580
581 // Mark them enabled so we only emit one warning for this translation
582 // unit.
583 Features.BCPLComment = true;
584 }
585
586 // Scan over the body of the comment. The common case, when scanning, is that
587 // the comment contains normal ascii characters with nothing interesting in
588 // them. As such, optimize for this case with the inner loop.
589 char C;
590 do {
591 C = *CurPtr;
592 // FIXME: just scan for a \n or \r character. If we find a \n character,
593 // scan backwards, checking to see if it's an escaped newline, like we do
594 // for block comments.
595
596 // Skip over characters in the fast loop.
597 while (C != 0 && // Potentially EOF.
598 C != '\\' && // Potentially escaped newline.
599 C != '?' && // Potentially trigraph.
600 C != '\n' && C != '\r') // Newline or DOS-style newline.
601 C = *++CurPtr;
602
603 // If this is a newline, we're done.
604 if (C == '\n' || C == '\r')
605 break; // Found the newline? Break out!
606
607 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
608 // properly decode the character.
609 const char *OldPtr = CurPtr;
610 C = getAndAdvanceChar(CurPtr, Result);
611
612 // If we read multiple characters, and one of those characters was a \r or
613 // \n, then we had an escaped newline within the comment. Emit diagnostic.
614 if (CurPtr != OldPtr+1) {
615 for (; OldPtr != CurPtr; ++OldPtr)
616 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnercb283342006-06-18 06:48:37 +0000617 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
618 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000619 }
620 }
621
622 if (CurPtr == BufferEnd+1) goto FoundEOF;
623 } while (C != '\n' && C != '\r');
624
625 // Found and did not consume a newline.
626
627 // If we are inside a preprocessor directive and we see the end of line,
628 // return immediately, so that the lexer can return this as an EOM token.
629 if (ParsingPreprocessorDirective) {
630 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000631 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000632 }
633
634 // Otherwise, eat the \n character. We don't care if this is a \n\r or
635 // \r\n sequence.
636 ++CurPtr;
637
638 // The next returned token is at the start of the line.
639 Result.SetFlag(LexerToken::StartOfLine);
640 // No leading whitespace seen so far.
641 Result.ClearFlag(LexerToken::LeadingSpace);
642
643 // It is common for the tokens immediately after a // comment to be
644 // whitespace (indentation for the next line). Instead of going through the
645 // big switch, handle it efficiently now.
646 if (isWhitespace(*CurPtr)) {
647 Result.SetFlag(LexerToken::LeadingSpace);
648 return SkipWhitespace(Result, CurPtr+1);
649 }
650
651 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000652 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000653
654FoundEOF: // If we ran off the end of the buffer, return EOF.
655 BufferPtr = CurPtr-1;
Chris Lattnercb283342006-06-18 06:48:37 +0000656 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000657}
658
Chris Lattnercb283342006-06-18 06:48:37 +0000659/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
660/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner22eb9722006-06-18 05:43:12 +0000661/// diagnostic if so. We know that the is inside of a block comment.
Chris Lattner1f583052006-06-18 06:53:56 +0000662static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
663 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000664 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Chris Lattner22eb9722006-06-18 05:43:12 +0000665
666 // Back up off the newline.
667 --CurPtr;
668
669 // If this is a two-character newline sequence, skip the other character.
670 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
671 // \n\n or \r\r -> not escaped newline.
672 if (CurPtr[0] == CurPtr[1])
673 return false;
674 // \n\r or \r\n -> skip the newline.
675 --CurPtr;
676 }
677
678 // If we have horizontal whitespace, skip over it. We allow whitespace
679 // between the slash and newline.
680 bool HasSpace = false;
681 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
682 --CurPtr;
683 HasSpace = true;
684 }
685
686 // If we have a slash, we know this is an escaped newline.
687 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +0000688 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000689 } else {
690 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +0000691 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
692 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +0000693 return false;
Chris Lattnercb283342006-06-18 06:48:37 +0000694
695 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +0000696 CurPtr -= 2;
697
698 // If no trigraphs are enabled, warn that we ignored this trigraph and
699 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +0000700 if (!L->getFeatures().Trigraphs) {
701 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000702 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000703 }
Chris Lattner1f583052006-06-18 06:53:56 +0000704 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000705 }
706
707 // Warn about having an escaped newline between the */ characters.
Chris Lattner1f583052006-06-18 06:53:56 +0000708 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Chris Lattner22eb9722006-06-18 05:43:12 +0000709
710 // If there was space between the backslash and newline, warn about it.
Chris Lattner1f583052006-06-18 06:53:56 +0000711 if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space);
Chris Lattner22eb9722006-06-18 05:43:12 +0000712
Chris Lattnercb283342006-06-18 06:48:37 +0000713 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000714}
715
716/// SkipBlockComment - We have just read the /* characters from input. Read
717/// until we find the */ characters that terminate the comment. Note that we
718/// don't bother decoding trigraphs or escaped newlines in block comments,
719/// because they cannot cause the comment to end. The only thing that can
720/// happen is the comment could end with an escaped newline between the */ end
721/// of comment.
Chris Lattnercb283342006-06-18 06:48:37 +0000722void Lexer::SkipBlockComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000723 // Scan one character past where we should, looking for a '/' character. Once
724 // we find it, check to see if it was preceeded by a *. This common
725 // optimization helps people who like to put a lot of * characters in their
726 // comments.
727 unsigned char C = *CurPtr++;
728 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000729 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000730 BufferPtr = CurPtr-1;
Chris Lattnercb283342006-06-18 06:48:37 +0000731 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000732 }
733
734 while (1) {
735 // Skip over all non-interesting characters.
736 // TODO: Vectorize this. Note: memchr on Darwin is slower than this loop.
737 while (C != '/' && C != '\0')
738 C = *CurPtr++;
739
740 if (C == '/') {
741 char T;
742 if (CurPtr[-2] == '*') // We found the final */. We're done!
743 break;
744
745 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +0000746 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000747 // We found the final */, though it had an escaped newline between the
748 // * and /. We're done!
749 break;
750 }
751 }
752 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
753 // If this is a /* inside of the comment, emit a warning. Don't do this
754 // if this is a /*/, which will end the comment. This misses cases with
755 // embedded escaped newlines, but oh well.
Chris Lattnercb283342006-06-18 06:48:37 +0000756 Diag(CurPtr-1, diag::nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000757 }
758 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000759 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000760 // Note: the user probably forgot a */. We could continue immediately
761 // after the /*, but this would involve lexing a lot of what really is the
762 // comment, which surely would confuse the parser.
763 BufferPtr = CurPtr-1;
Chris Lattnercb283342006-06-18 06:48:37 +0000764 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000765 }
766 C = *CurPtr++;
767 }
768
769 // It is common for the tokens immediately after a /**/ comment to be
770 // whitespace. Instead of going through the big switch, handle it
771 // efficiently now.
772 if (isHorizontalWhitespace(*CurPtr)) {
773 Result.SetFlag(LexerToken::LeadingSpace);
774 return SkipWhitespace(Result, CurPtr+1);
775 }
776
777 // Otherwise, just return so that the next character will be lexed as a token.
778 BufferPtr = CurPtr;
779 Result.SetFlag(LexerToken::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000780}
781
782//===----------------------------------------------------------------------===//
783// Primary Lexing Entry Points
784//===----------------------------------------------------------------------===//
785
786/// LexIncludeFilename - After the preprocessor has parsed a #include, lex and
787/// (potentially) macro expand the filename.
Chris Lattnercb283342006-06-18 06:48:37 +0000788void Lexer::LexIncludeFilename(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000789 assert(ParsingPreprocessorDirective &&
790 ParsingFilename == false &&
791 "Must be in a preprocessing directive!");
792
793 // We are now parsing a filename!
794 ParsingFilename = true;
795
796 // There should be exactly two tokens here if everything is good: first the
797 // filename, then the EOM.
Chris Lattnercb283342006-06-18 06:48:37 +0000798 Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000799
800 // We should have gotten the filename now.
801 ParsingFilename = false;
802
803 // No filename?
Chris Lattnercb283342006-06-18 06:48:37 +0000804 if (Result.getKind() == tok::eom) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000805 PP.Diag(Result, diag::err_pp_expects_filename);
Chris Lattnercb283342006-06-18 06:48:37 +0000806 return;
807 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000808
Chris Lattnerd01e2912006-06-18 16:22:51 +0000809 // Verify that there is nothing after the filename, other than EOM. Use the
810 // preprocessor to lex this in case lexing the filename entered a macro.
Chris Lattner22eb9722006-06-18 05:43:12 +0000811 LexerToken EndTok;
Chris Lattnerd01e2912006-06-18 16:22:51 +0000812 PP.Lex(EndTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000813
814 if (EndTok.getKind() != tok::eom) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000815 PP.Diag(EndTok, diag::ext_pp_extra_tokens_at_eol, "#include");
Chris Lattner22eb9722006-06-18 05:43:12 +0000816
817 // Lex until the end of the preprocessor directive line.
Chris Lattnercb283342006-06-18 06:48:37 +0000818 while (EndTok.getKind() != tok::eom)
Chris Lattnerd01e2912006-06-18 16:22:51 +0000819 PP.Lex(EndTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000820
821 Result.SetKind(tok::eom);
822 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000823}
824
825/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
826/// uninterpreted string. This switches the lexer out of directive mode.
827std::string Lexer::ReadToEndOfLine() {
828 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
829 "Must be in a preprocessing directive!");
830 std::string Result;
831 LexerToken Tmp;
832
833 // CurPtr - Cache BufferPtr in an automatic variable.
834 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000835 while (1) {
836 char Char = getAndAdvanceChar(CurPtr, Tmp);
837 switch (Char) {
838 default:
839 Result += Char;
840 break;
841 case 0: // Null.
842 // Found end of file?
843 if (CurPtr-1 != BufferEnd) {
844 // Nope, normal character, continue.
845 Result += Char;
846 break;
847 }
848 // FALL THROUGH.
849 case '\r':
850 case '\n':
851 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
852 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
853 BufferPtr = CurPtr-1;
854
855 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +0000856 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000857 assert(Tmp.getKind() == tok::eom && "Unexpected token!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000858
859 // Finally, we're done, return the string we found.
860 return Result;
861 }
862 }
863}
864
865/// LexEndOfFile - CurPtr points to the end of this file. Handle this
866/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattnercb283342006-06-18 06:48:37 +0000867void Lexer::LexEndOfFile(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000868 // If we hit the end of the file while parsing a preprocessor directive,
869 // end the preprocessor directive first. The next token returned will
870 // then be the end of file.
871 if (ParsingPreprocessorDirective) {
872 // Done parsing the "line".
873 ParsingPreprocessorDirective = false;
874 Result.SetKind(tok::eom);
Chris Lattnerd01e2912006-06-18 16:22:51 +0000875 // Update the location of token as well as BufferPtr.
876 FormTokenWithChars(Result, CurPtr);
Chris Lattnercb283342006-06-18 06:48:37 +0000877 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000878 }
879
880 // If we are in a #if directive, emit an error.
881 while (!ConditionalStack.empty()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000882 PP.Diag(ConditionalStack.back().IfLoc,
883 diag::err_pp_unterminated_conditional);
Chris Lattner22eb9722006-06-18 05:43:12 +0000884 ConditionalStack.pop_back();
885 }
886
887 // If the file was empty or didn't end in a newline, issue a pedwarn.
Chris Lattnercb283342006-06-18 06:48:37 +0000888 if (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
889 Diag(BufferEnd, diag::ext_no_newline_eof);
Chris Lattner22eb9722006-06-18 05:43:12 +0000890
891 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000892 PP.HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000893}
894
895
896/// LexTokenInternal - This implements a simple C family lexer. It is an
897/// extremely performance critical piece of code. This assumes that the buffer
898/// has a null character at the end of the file. Return true if an error
899/// occurred and compilation should terminate, false if normal. This returns a
900/// preprocessing token, not a normal token, as such, it is an internal
901/// interface. It assumes that the Flags of result have been cleared before
902/// calling this.
Chris Lattnercb283342006-06-18 06:48:37 +0000903void Lexer::LexTokenInternal(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000904LexNextToken:
905 // New token, can't need cleaning yet.
906 Result.ClearFlag(LexerToken::NeedsCleaning);
907
908 // CurPtr - Cache BufferPtr in an automatic variable.
909 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000910
911 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
912
913 // Read a character, advancing over it.
914 char Char = getAndAdvanceChar(CurPtr, Result);
915 switch (Char) {
916 case 0: // Null.
917 // Found end of file?
918 if (CurPtr-1 == BufferEnd)
919 return LexEndOfFile(Result, CurPtr-1); // Retreat back into the file.
920
Chris Lattnercb283342006-06-18 06:48:37 +0000921 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner22eb9722006-06-18 05:43:12 +0000922 Result.SetFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +0000923 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000924 goto LexNextToken; // GCC isn't tail call eliminating.
925 case '\n':
926 case '\r':
927 // If we are inside a preprocessor directive and we see the end of line,
928 // we know we are done with the directive, so return an EOM token.
929 if (ParsingPreprocessorDirective) {
930 // Done parsing the "line".
931 ParsingPreprocessorDirective = false;
932
933 // Since we consumed a newline, we are back at the start of a line.
934 IsAtStartOfLine = true;
935
936 Result.SetKind(tok::eom);
937 break;
938 }
939 // The returned token is at the start of the line.
940 Result.SetFlag(LexerToken::StartOfLine);
941 // No leading whitespace seen so far.
942 Result.ClearFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +0000943 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000944 goto LexNextToken; // GCC isn't tail call eliminating.
945 case ' ':
946 case '\t':
947 case '\f':
948 case '\v':
949 Result.SetFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +0000950 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000951 goto LexNextToken; // GCC isn't tail call eliminating.
952
953 case 'L':
954 Char = getCharAndSize(CurPtr, SizeTmp);
955
956 // Wide string literal.
957 if (Char == '"')
958 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result));
959
960 // Wide character constant.
961 if (Char == '\'')
962 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
963 // FALL THROUGH, treating L like the start of an identifier.
964
965 // C99 6.4.2: Identifiers.
966 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
967 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
968 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
969 case 'V': case 'W': case 'X': case 'Y': case 'Z':
970 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
971 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
972 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
973 case 'v': case 'w': case 'x': case 'y': case 'z':
974 case '_':
975 return LexIdentifier(Result, CurPtr);
976
977 // C99 6.4.4.1: Integer Constants.
978 // C99 6.4.4.2: Floating Constants.
979 case '0': case '1': case '2': case '3': case '4':
980 case '5': case '6': case '7': case '8': case '9':
981 return LexNumericConstant(Result, CurPtr);
982
983 // C99 6.4.4: Character Constants.
984 case '\'':
985 return LexCharConstant(Result, CurPtr);
986
987 // C99 6.4.5: String Literals.
988 case '"':
989 return LexStringLiteral(Result, CurPtr);
990
991 // C99 6.4.6: Punctuators.
992 case '?':
993 Result.SetKind(tok::question);
994 break;
995 case '[':
996 Result.SetKind(tok::l_square);
997 break;
998 case ']':
999 Result.SetKind(tok::r_square);
1000 break;
1001 case '(':
1002 Result.SetKind(tok::l_paren);
1003 break;
1004 case ')':
1005 Result.SetKind(tok::r_paren);
1006 break;
1007 case '{':
1008 Result.SetKind(tok::l_brace);
1009 break;
1010 case '}':
1011 Result.SetKind(tok::r_brace);
1012 break;
1013 case '.':
1014 Char = getCharAndSize(CurPtr, SizeTmp);
1015 if (Char >= '0' && Char <= '9') {
1016 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1017 } else if (Features.CPlusPlus && Char == '*') {
1018 Result.SetKind(tok::periodstar);
1019 CurPtr += SizeTmp;
1020 } else if (Char == '.' &&
1021 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
1022 Result.SetKind(tok::ellipsis);
1023 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1024 SizeTmp2, Result);
1025 } else {
1026 Result.SetKind(tok::period);
1027 }
1028 break;
1029 case '&':
1030 Char = getCharAndSize(CurPtr, SizeTmp);
1031 if (Char == '&') {
1032 Result.SetKind(tok::ampamp);
1033 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1034 } else if (Char == '=') {
1035 Result.SetKind(tok::ampequal);
1036 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1037 } else {
1038 Result.SetKind(tok::amp);
1039 }
1040 break;
1041 case '*':
1042 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
1043 Result.SetKind(tok::starequal);
1044 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1045 } else {
1046 Result.SetKind(tok::star);
1047 }
1048 break;
1049 case '+':
1050 Char = getCharAndSize(CurPtr, SizeTmp);
1051 if (Char == '+') {
1052 Result.SetKind(tok::plusplus);
1053 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1054 } else if (Char == '=') {
1055 Result.SetKind(tok::plusequal);
1056 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1057 } else {
1058 Result.SetKind(tok::plus);
1059 }
1060 break;
1061 case '-':
1062 Char = getCharAndSize(CurPtr, SizeTmp);
1063 if (Char == '-') {
1064 Result.SetKind(tok::minusminus);
1065 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1066 } else if (Char == '>' && Features.CPlusPlus &&
1067 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {
1068 Result.SetKind(tok::arrowstar); // C++ ->*
1069 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1070 SizeTmp2, Result);
1071 } else if (Char == '>') {
1072 Result.SetKind(tok::arrow);
1073 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1074 } else if (Char == '=') {
1075 Result.SetKind(tok::minusequal);
1076 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1077 } else {
1078 Result.SetKind(tok::minus);
1079 }
1080 break;
1081 case '~':
1082 Result.SetKind(tok::tilde);
1083 break;
1084 case '!':
1085 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
1086 Result.SetKind(tok::exclaimequal);
1087 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1088 } else {
1089 Result.SetKind(tok::exclaim);
1090 }
1091 break;
1092 case '/':
1093 // 6.4.9: Comments
1094 Char = getCharAndSize(CurPtr, SizeTmp);
1095 if (Char == '/') { // BCPL comment.
1096 Result.SetFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001097 SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result));
Chris Lattner22eb9722006-06-18 05:43:12 +00001098 goto LexNextToken; // GCC isn't tail call eliminating.
1099 } else if (Char == '*') { // /**/ comment.
1100 Result.SetFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001101 SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result));
Chris Lattner22eb9722006-06-18 05:43:12 +00001102 goto LexNextToken; // GCC isn't tail call eliminating.
1103 } else if (Char == '=') {
1104 Result.SetKind(tok::slashequal);
1105 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1106 } else {
1107 Result.SetKind(tok::slash);
1108 }
1109 break;
1110 case '%':
1111 Char = getCharAndSize(CurPtr, SizeTmp);
1112 if (Char == '=') {
1113 Result.SetKind(tok::percentequal);
1114 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1115 } else if (Features.Digraphs && Char == '>') {
1116 Result.SetKind(tok::r_brace); // '%>' -> '}'
1117 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1118 } else if (Features.Digraphs && Char == ':') {
1119 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1120 if (getCharAndSize(CurPtr, SizeTmp) == '%' &&
1121 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
1122 Result.SetKind(tok::hashhash); // '%:%:' -> '##'
1123 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1124 SizeTmp2, Result);
1125 } else {
1126 Result.SetKind(tok::hash); // '%:' -> '#'
1127
1128 // We parsed a # character. If this occurs at the start of the line,
1129 // it's actually the start of a preprocessing directive. Callback to
1130 // the preprocessor to handle it.
1131 // FIXME: -fpreprocessed mode??
1132 if (Result.isAtStartOfLine() && !PP.isSkipping()) {
1133 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001134 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001135
1136 // As an optimization, if the preprocessor didn't switch lexers, tail
1137 // recurse.
1138 if (PP.isCurrentLexer(this)) {
1139 // Start a new token. If this is a #include or something, the PP may
1140 // want us starting at the beginning of the line again. If so, set
1141 // the StartOfLine flag.
1142 if (IsAtStartOfLine) {
1143 Result.SetFlag(LexerToken::StartOfLine);
1144 IsAtStartOfLine = false;
1145 }
1146 goto LexNextToken; // GCC isn't tail call eliminating.
1147 }
1148
1149 return PP.Lex(Result);
1150 }
1151 }
1152 } else {
1153 Result.SetKind(tok::percent);
1154 }
1155 break;
1156 case '<':
1157 Char = getCharAndSize(CurPtr, SizeTmp);
1158 if (ParsingFilename) {
1159 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1160 } else if (Char == '<' &&
1161 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
1162 Result.SetKind(tok::lesslessequal);
1163 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1164 SizeTmp2, Result);
1165 } else if (Char == '<') {
1166 Result.SetKind(tok::lessless);
1167 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1168 } else if (Char == '=') {
1169 Result.SetKind(tok::lessequal);
1170 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1171 } else if (Features.Digraphs && Char == ':') {
1172 Result.SetKind(tok::l_square); // '<:' -> '['
1173 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1174 } else if (Features.Digraphs && Char == '>') {
1175 Result.SetKind(tok::l_brace); // '<%' -> '{'
1176 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1177 } else if (Features.CPPMinMax && Char == '?') { // <?
1178 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerd01e2912006-06-18 16:22:51 +00001179 Diag(BufferPtr, diag::min_max_deprecated);
Chris Lattner22eb9722006-06-18 05:43:12 +00001180
1181 if (getCharAndSize(CurPtr, SizeTmp) == '=') { // <?=
1182 Result.SetKind(tok::lessquestionequal);
1183 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1184 } else {
1185 Result.SetKind(tok::lessquestion);
1186 }
1187 } else {
1188 Result.SetKind(tok::less);
1189 }
1190 break;
1191 case '>':
1192 Char = getCharAndSize(CurPtr, SizeTmp);
1193 if (Char == '=') {
1194 Result.SetKind(tok::greaterequal);
1195 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1196 } else if (Char == '>' &&
1197 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
1198 Result.SetKind(tok::greatergreaterequal);
1199 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1200 SizeTmp2, Result);
1201 } else if (Char == '>') {
1202 Result.SetKind(tok::greatergreater);
1203 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1204 } else if (Features.CPPMinMax && Char == '?') {
1205 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerd01e2912006-06-18 16:22:51 +00001206 Diag(BufferPtr, diag::min_max_deprecated);
Chris Lattner22eb9722006-06-18 05:43:12 +00001207
1208 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
1209 Result.SetKind(tok::greaterquestionequal); // >?=
1210 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1211 } else {
1212 Result.SetKind(tok::greaterquestion); // >?
1213 }
1214 } else {
1215 Result.SetKind(tok::greater);
1216 }
1217 break;
1218 case '^':
1219 Char = getCharAndSize(CurPtr, SizeTmp);
1220 if (Char == '=') {
1221 Result.SetKind(tok::caretequal);
1222 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1223 } else {
1224 Result.SetKind(tok::caret);
1225 }
1226 break;
1227 case '|':
1228 Char = getCharAndSize(CurPtr, SizeTmp);
1229 if (Char == '=') {
1230 Result.SetKind(tok::pipeequal);
1231 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1232 } else if (Char == '|') {
1233 Result.SetKind(tok::pipepipe);
1234 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1235 } else {
1236 Result.SetKind(tok::pipe);
1237 }
1238 break;
1239 case ':':
1240 Char = getCharAndSize(CurPtr, SizeTmp);
1241 if (Features.Digraphs && Char == '>') {
1242 Result.SetKind(tok::r_square); // ':>' -> ']'
1243 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1244 } else if (Features.CPlusPlus && Char == ':') {
1245 Result.SetKind(tok::coloncolon);
1246 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1247 } else {
1248 Result.SetKind(tok::colon);
1249 }
1250 break;
1251 case ';':
1252 Result.SetKind(tok::semi);
1253 break;
1254 case '=':
1255 Char = getCharAndSize(CurPtr, SizeTmp);
1256 if (Char == '=') {
1257 Result.SetKind(tok::equalequal);
1258 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1259 } else {
1260 Result.SetKind(tok::equal);
1261 }
1262 break;
1263 case ',':
1264 Result.SetKind(tok::comma);
1265 break;
1266 case '#':
1267 Char = getCharAndSize(CurPtr, SizeTmp);
1268 if (Char == '#') {
1269 Result.SetKind(tok::hashhash);
1270 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1271 } else {
1272 Result.SetKind(tok::hash);
1273 // We parsed a # character. If this occurs at the start of the line,
1274 // it's actually the start of a preprocessing directive. Callback to
1275 // the preprocessor to handle it.
1276 // FIXME: not in preprocessed mode??
1277 if (Result.isAtStartOfLine() && !PP.isSkipping()) {
1278 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001279 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001280
1281 // As an optimization, if the preprocessor didn't switch lexers, tail
1282 // recurse.
1283 if (PP.isCurrentLexer(this)) {
1284 // Start a new token. If this is a #include or something, the PP may
1285 // want us starting at the beginning of the line again. If so, set
1286 // the StartOfLine flag.
1287 if (IsAtStartOfLine) {
1288 Result.SetFlag(LexerToken::StartOfLine);
1289 IsAtStartOfLine = false;
1290 }
1291 goto LexNextToken; // GCC isn't tail call eliminating.
1292 }
1293 return PP.Lex(Result);
1294 }
1295 }
1296 break;
1297
1298 case '\\':
1299 // FIXME: handle UCN's.
1300 // FALL THROUGH.
1301 default:
1302 // Objective C support.
1303 if (CurPtr[-1] == '@' && Features.ObjC1) {
1304 Result.SetKind(tok::at);
1305 break;
1306 } else if (CurPtr[-1] == '$' && Features.DollarIdents) {// $ in identifiers.
Chris Lattnercb283342006-06-18 06:48:37 +00001307 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +00001308 return LexIdentifier(Result, CurPtr);
1309 }
1310
Chris Lattnercb283342006-06-18 06:48:37 +00001311 if (!PP.isSkipping()) Diag(CurPtr-1, diag::err_stray_character);
Chris Lattner22eb9722006-06-18 05:43:12 +00001312 BufferPtr = CurPtr;
1313 goto LexNextToken; // GCC isn't tail call eliminating.
1314 }
1315
Chris Lattnerd01e2912006-06-18 16:22:51 +00001316 // Update the location of token as well as BufferPtr.
1317 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001318}