blob: 9c95af1475aa3225bc5b8f4c15534d201d46e778 [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"
Chris Lattner22eb9722006-06-18 05:43:12 +000030#include "clang/Basic/SourceLocation.h"
Chris Lattner739e7392007-04-29 07:12:06 +000031#include "llvm/Support/MemoryBuffer.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000032#include <cctype>
Chris Lattner22eb9722006-06-18 05:43:12 +000033using namespace llvm;
34using namespace clang;
35
36static void InitCharacterInfo();
37
Chris Lattner739e7392007-04-29 07:12:06 +000038Lexer::Lexer(const MemoryBuffer *File, unsigned fileid, Preprocessor &pp,
Chris Lattner4cca5ba2006-07-02 20:05:54 +000039 const char *BufStart, const char *BufEnd)
Chris Lattner678c8802006-07-11 05:46:12 +000040 : BufferEnd(BufEnd ? BufEnd : File->getBufferEnd()),
Chris Lattner4cca5ba2006-07-02 20:05:54 +000041 InputFile(File), CurFileID(fileid), PP(pp), Features(PP.getLangOptions()) {
Chris Lattnerecfeafe2006-07-02 21:26:45 +000042 Is_PragmaLexer = false;
Chris Lattner4ec473f2006-07-03 05:16:05 +000043 IsMainFile = false;
Chris Lattner22eb9722006-06-18 05:43:12 +000044 InitCharacterInfo();
45
46 assert(BufferEnd[0] == 0 &&
47 "We assume that the input buffer has a null character at the end"
48 " to simplify lexing!");
Chris Lattner678c8802006-07-11 05:46:12 +000049
50 BufferPtr = BufStart ? BufStart : File->getBufferStart();
51
Chris Lattner22eb9722006-06-18 05:43:12 +000052 // 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;
Chris Lattner3ebcf4e2006-07-11 05:39:23 +000060
61 // We are not in raw mode. Raw mode disables diagnostics and interpretation
62 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
63 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
64 // or otherwise skipping over tokens.
65 LexingRawMode = false;
Chris Lattner457fc152006-07-29 06:30:25 +000066
67 // Default to keeping comments if requested.
Chris Lattnerb352e3e2006-11-21 06:17:10 +000068 KeepCommentMode = PP.getCommentRetentionState();
Chris Lattner22eb9722006-06-18 05:43:12 +000069}
70
Chris Lattnere3e81ea2006-07-03 01:13:26 +000071/// Stringify - Convert the specified string into a C string, with surrounding
72/// ""'s, and with escaped \ and " characters.
Chris Lattnerecc39e92006-07-15 05:23:31 +000073std::string Lexer::Stringify(const std::string &Str, bool Charify) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +000074 std::string Result = Str;
Chris Lattnerecc39e92006-07-15 05:23:31 +000075 char Quote = Charify ? '\'' : '"';
Chris Lattnere3e81ea2006-07-03 01:13:26 +000076 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
Chris Lattnerecc39e92006-07-15 05:23:31 +000077 if (Result[i] == '\\' || Result[i] == Quote) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +000078 Result.insert(Result.begin()+i, '\\');
79 ++i; ++e;
80 }
81 }
Chris Lattnerecc39e92006-07-15 05:23:31 +000082 return Result;
Chris Lattnere3e81ea2006-07-03 01:13:26 +000083}
84
Chris Lattner22eb9722006-06-18 05:43:12 +000085
Chris Lattner22eb9722006-06-18 05:43:12 +000086//===----------------------------------------------------------------------===//
87// Character information.
88//===----------------------------------------------------------------------===//
89
90static unsigned char CharInfo[256];
91
92enum {
93 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
94 CHAR_VERT_WS = 0x02, // '\r', '\n'
95 CHAR_LETTER = 0x04, // a-z,A-Z
96 CHAR_NUMBER = 0x08, // 0-9
97 CHAR_UNDER = 0x10, // _
98 CHAR_PERIOD = 0x20 // .
99};
100
101static void InitCharacterInfo() {
102 static bool isInited = false;
103 if (isInited) return;
104 isInited = true;
105
106 // Intiialize the CharInfo table.
107 // TODO: statically initialize this.
108 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
109 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
110 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
111
112 CharInfo[(int)'_'] = CHAR_UNDER;
Chris Lattnerdd0b7cb2006-10-17 02:53:51 +0000113 CharInfo[(int)'.'] = CHAR_PERIOD;
Chris Lattner22eb9722006-06-18 05:43:12 +0000114 for (unsigned i = 'a'; i <= 'z'; ++i)
115 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
116 for (unsigned i = '0'; i <= '9'; ++i)
117 CharInfo[i] = CHAR_NUMBER;
118}
119
120/// isIdentifierBody - Return true if this is the body character of an
121/// identifier, which is [a-zA-Z0-9_].
122static inline bool isIdentifierBody(unsigned char c) {
123 return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER);
124}
125
126/// isHorizontalWhitespace - Return true if this character is horizontal
127/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
128static inline bool isHorizontalWhitespace(unsigned char c) {
129 return CharInfo[c] & CHAR_HORZ_WS;
130}
131
132/// isWhitespace - Return true if this character is horizontal or vertical
133/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
134/// for '\0'.
135static inline bool isWhitespace(unsigned char c) {
136 return CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS);
137}
138
139/// isNumberBody - Return true if this is the body character of an
140/// preprocessing number, which is [a-zA-Z0-9_.].
141static inline bool isNumberBody(unsigned char c) {
142 return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD);
143}
144
Chris Lattnerd01e2912006-06-18 16:22:51 +0000145
Chris Lattner22eb9722006-06-18 05:43:12 +0000146//===----------------------------------------------------------------------===//
147// Diagnostics forwarding code.
148//===----------------------------------------------------------------------===//
149
150/// getSourceLocation - Return a source location identifier for the specified
151/// offset in the current file.
152SourceLocation Lexer::getSourceLocation(const char *Loc) const {
Chris Lattner8bbfe462006-07-02 22:27:49 +0000153 assert(Loc >= InputFile->getBufferStart() && Loc <= BufferEnd &&
Chris Lattner4cca5ba2006-07-02 20:05:54 +0000154 "Location out of range for this buffer!");
Chris Lattner8bbfe462006-07-02 22:27:49 +0000155 return SourceLocation(CurFileID, Loc-InputFile->getBufferStart());
Chris Lattner22eb9722006-06-18 05:43:12 +0000156}
157
158
159/// Diag - Forwarding function for diagnostics. This translate a source
160/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000161void Lexer::Diag(const char *Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000162 const std::string &Msg) const {
Chris Lattner538d7f32006-07-20 04:31:52 +0000163 if (LexingRawMode && Diagnostic::isNoteWarningOrExtension(DiagID))
164 return;
Chris Lattnercb283342006-06-18 06:48:37 +0000165 PP.Diag(getSourceLocation(Loc), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000166}
Chris Lattner538d7f32006-07-20 04:31:52 +0000167void Lexer::Diag(SourceLocation Loc, unsigned DiagID,
168 const std::string &Msg) const {
169 if (LexingRawMode && Diagnostic::isNoteWarningOrExtension(DiagID))
170 return;
171 PP.Diag(Loc, DiagID, Msg);
172}
173
Chris Lattner22eb9722006-06-18 05:43:12 +0000174
175//===----------------------------------------------------------------------===//
176// Trigraph and Escaped Newline Handling Code.
177//===----------------------------------------------------------------------===//
178
179/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
180/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
181static char GetTrigraphCharForLetter(char Letter) {
182 switch (Letter) {
183 default: return 0;
184 case '=': return '#';
185 case ')': return ']';
186 case '(': return '[';
187 case '!': return '|';
188 case '\'': return '^';
189 case '>': return '}';
190 case '/': return '\\';
191 case '<': return '{';
192 case '-': return '~';
193 }
194}
195
196/// DecodeTrigraphChar - If the specified character is a legal trigraph when
197/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
198/// return the result character. Finally, emit a warning about trigraph use
199/// whether trigraphs are enabled or not.
200static char DecodeTrigraphChar(const char *CP, Lexer *L) {
201 char Res = GetTrigraphCharForLetter(*CP);
202 if (Res && L) {
203 if (!L->getFeatures().Trigraphs) {
204 L->Diag(CP-2, diag::trigraph_ignored);
205 return 0;
206 } else {
207 L->Diag(CP-2, diag::trigraph_converted, std::string()+Res);
208 }
209 }
210 return Res;
211}
212
213/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
214/// get its size, and return it. This is tricky in several cases:
215/// 1. If currently at the start of a trigraph, we warn about the trigraph,
216/// then either return the trigraph (skipping 3 chars) or the '?',
217/// depending on whether trigraphs are enabled or not.
218/// 2. If this is an escaped newline (potentially with whitespace between
219/// the backslash and newline), implicitly skip the newline and return
220/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000221/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000222///
223/// This handles the slow/uncommon case of the getCharAndSize method. Here we
224/// know that we can accumulate into Size, and that we have already incremented
225/// Ptr by Size bytes.
226///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000227/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
228/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000229///
230char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
231 LexerToken *Tok) {
232 // If we have a slash, look for an escaped newline.
233 if (Ptr[0] == '\\') {
234 ++Size;
235 ++Ptr;
236Slash:
237 // Common case, backslash-char where the char is not whitespace.
238 if (!isWhitespace(Ptr[0])) return '\\';
239
240 // See if we have optional whitespace characters followed by a newline.
241 {
242 unsigned SizeTmp = 0;
243 do {
244 ++SizeTmp;
245 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
246 // Remember that this token needs to be cleaned.
Chris Lattner8c204872006-10-14 05:19:21 +0000247 if (Tok) Tok->setFlag(LexerToken::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000248
249 // Warn if there was whitespace between the backslash and newline.
250 if (SizeTmp != 1 && Tok)
251 Diag(Ptr, diag::backslash_newline_space);
252
253 // If this is a \r\n or \n\r, skip the newlines.
254 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
255 Ptr[SizeTmp-1] != Ptr[SizeTmp])
256 ++SizeTmp;
257
258 // Found backslash<whitespace><newline>. Parse the char after it.
259 Size += SizeTmp;
260 Ptr += SizeTmp;
261 // Use slow version to accumulate a correct size field.
262 return getCharAndSizeSlow(Ptr, Size, Tok);
263 }
264 } while (isWhitespace(Ptr[SizeTmp]));
265 }
266
267 // Otherwise, this is not an escaped newline, just return the slash.
268 return '\\';
269 }
270
271 // If this is a trigraph, process it.
272 if (Ptr[0] == '?' && Ptr[1] == '?') {
273 // If this is actually a legal trigraph (not something like "??x"), emit
274 // a trigraph warning. If so, and if trigraphs are enabled, return it.
275 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
276 // Remember that this token needs to be cleaned.
Chris Lattner8c204872006-10-14 05:19:21 +0000277 if (Tok) Tok->setFlag(LexerToken::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000278
279 Ptr += 3;
280 Size += 3;
281 if (C == '\\') goto Slash;
282 return C;
283 }
284 }
285
286 // If this is neither, return a single character.
287 ++Size;
288 return *Ptr;
289}
290
Chris Lattnerd01e2912006-06-18 16:22:51 +0000291
Chris Lattner22eb9722006-06-18 05:43:12 +0000292/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
293/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
294/// and that we have already incremented Ptr by Size bytes.
295///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000296/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
297/// be updated to match.
298char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000299 const LangOptions &Features) {
300 // If we have a slash, look for an escaped newline.
301 if (Ptr[0] == '\\') {
302 ++Size;
303 ++Ptr;
304Slash:
305 // Common case, backslash-char where the char is not whitespace.
306 if (!isWhitespace(Ptr[0])) return '\\';
307
308 // See if we have optional whitespace characters followed by a newline.
309 {
310 unsigned SizeTmp = 0;
311 do {
312 ++SizeTmp;
313 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
314
315 // If this is a \r\n or \n\r, skip the newlines.
316 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
317 Ptr[SizeTmp-1] != Ptr[SizeTmp])
318 ++SizeTmp;
319
320 // Found backslash<whitespace><newline>. Parse the char after it.
321 Size += SizeTmp;
322 Ptr += SizeTmp;
323
324 // Use slow version to accumulate a correct size field.
325 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
326 }
327 } while (isWhitespace(Ptr[SizeTmp]));
328 }
329
330 // Otherwise, this is not an escaped newline, just return the slash.
331 return '\\';
332 }
333
334 // If this is a trigraph, process it.
335 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
336 // If this is actually a legal trigraph (not something like "??x"), return
337 // it.
338 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
339 Ptr += 3;
340 Size += 3;
341 if (C == '\\') goto Slash;
342 return C;
343 }
344 }
345
346 // If this is neither, return a single character.
347 ++Size;
348 return *Ptr;
349}
350
Chris Lattner22eb9722006-06-18 05:43:12 +0000351//===----------------------------------------------------------------------===//
352// Helper methods for lexing.
353//===----------------------------------------------------------------------===//
354
Chris Lattnercb283342006-06-18 06:48:37 +0000355void Lexer::LexIdentifier(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000356 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
357 unsigned Size;
358 unsigned char C = *CurPtr++;
359 while (isIdentifierBody(C)) {
360 C = *CurPtr++;
361 }
362 --CurPtr; // Back up over the skipped character.
363
364 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
365 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000366 // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000367 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
368FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000369 const char *IdStart = BufferPtr;
Chris Lattnerd01e2912006-06-18 16:22:51 +0000370 FormTokenWithChars(Result, CurPtr);
Chris Lattner8c204872006-10-14 05:19:21 +0000371 Result.setKind(tok::identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000372
Chris Lattner0f1f5052006-07-20 04:16:23 +0000373 // If we are in raw mode, return this identifier raw. There is no need to
374 // look up identifier information or attempt to macro expand it.
375 if (LexingRawMode) return;
376
Chris Lattnercefc7682006-07-08 08:28:12 +0000377 // Fill in Result.IdentifierInfo, looking up the identifier in the
378 // identifier table.
379 PP.LookUpIdentifierInfo(Result, IdStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000380
Chris Lattnerc5a00062006-06-18 16:41:01 +0000381 // Finally, now that we know we have an identifier, pass this off to the
382 // preprocessor, which may macro expand it or something.
Chris Lattner22eb9722006-06-18 05:43:12 +0000383 return PP.HandleIdentifier(Result);
384 }
385
386 // Otherwise, $,\,? in identifier found. Enter slower path.
387
388 C = getCharAndSize(CurPtr, Size);
389 while (1) {
390 if (C == '$') {
391 // If we hit a $ and they are not supported in identifiers, we are done.
392 if (!Features.DollarIdents) goto FinishIdentifier;
393
394 // Otherwise, emit a diagnostic and continue.
Chris Lattnercb283342006-06-18 06:48:37 +0000395 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000396 CurPtr = ConsumeChar(CurPtr, Size, Result);
397 C = getCharAndSize(CurPtr, Size);
398 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000399 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000400 // Found end of identifier.
401 goto FinishIdentifier;
402 }
403
404 // Otherwise, this character is good, consume it.
405 CurPtr = ConsumeChar(CurPtr, Size, Result);
406
407 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000408 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000409 CurPtr = ConsumeChar(CurPtr, Size, Result);
410 C = getCharAndSize(CurPtr, Size);
411 }
412 }
413}
414
415
416/// LexNumericConstant - Lex the remainer of a integer or floating point
417/// constant. From[-1] is the first character lexed. Return the end of the
418/// constant.
Chris Lattnercb283342006-06-18 06:48:37 +0000419void Lexer::LexNumericConstant(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000420 unsigned Size;
421 char C = getCharAndSize(CurPtr, Size);
422 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000423 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000424 CurPtr = ConsumeChar(CurPtr, Size, Result);
425 PrevCh = C;
426 C = getCharAndSize(CurPtr, Size);
427 }
428
429 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
430 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
431 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
432
433 // If we have a hex FP constant, continue.
434 if (Features.HexFloats &&
435 (C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p'))
436 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
437
Chris Lattner8c204872006-10-14 05:19:21 +0000438 Result.setKind(tok::numeric_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000439
Chris Lattnerd01e2912006-06-18 16:22:51 +0000440 // Update the location of token as well as BufferPtr.
441 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000442}
443
444/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
445/// either " or L".
Chris Lattnerd3e98952006-10-06 05:22:26 +0000446void Lexer::LexStringLiteral(LexerToken &Result, const char *CurPtr, bool Wide){
Chris Lattner22eb9722006-06-18 05:43:12 +0000447 const char *NulCharacter = 0; // Does this string contain the \0 character?
448
449 char C = getAndAdvanceChar(CurPtr, Result);
450 while (C != '"') {
451 // Skip escaped characters.
452 if (C == '\\') {
453 // Skip the escaped character.
454 C = getAndAdvanceChar(CurPtr, Result);
455 } else if (C == '\n' || C == '\r' || // Newline.
456 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000457 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000458 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000459 FormTokenWithChars(Result, CurPtr-1);
460 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000461 } else if (C == 0) {
462 NulCharacter = CurPtr-1;
463 }
464 C = getAndAdvanceChar(CurPtr, Result);
465 }
466
Chris Lattner5a78a022006-07-20 06:02:19 +0000467 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000468 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000469
Chris Lattner8c204872006-10-14 05:19:21 +0000470 Result.setKind(Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000471
Chris Lattnerd01e2912006-06-18 16:22:51 +0000472 // Update the location of the token as well as the BufferPtr instance var.
473 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000474}
475
476/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
477/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattnercb283342006-06-18 06:48:37 +0000478void Lexer::LexAngledStringLiteral(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000479 const char *NulCharacter = 0; // Does this string contain the \0 character?
480
481 char C = getAndAdvanceChar(CurPtr, Result);
482 while (C != '>') {
483 // Skip escaped characters.
484 if (C == '\\') {
485 // Skip the escaped character.
486 C = getAndAdvanceChar(CurPtr, Result);
487 } else if (C == '\n' || C == '\r' || // Newline.
488 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000489 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000490 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000491 FormTokenWithChars(Result, CurPtr-1);
492 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000493 } else if (C == 0) {
494 NulCharacter = CurPtr-1;
495 }
496 C = getAndAdvanceChar(CurPtr, Result);
497 }
498
Chris Lattner5a78a022006-07-20 06:02:19 +0000499 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000500 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000501
Chris Lattner8c204872006-10-14 05:19:21 +0000502 Result.setKind(tok::angle_string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000503
Chris Lattnerd01e2912006-06-18 16:22:51 +0000504 // Update the location of token as well as BufferPtr.
505 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000506}
507
508
509/// LexCharConstant - Lex the remainder of a character constant, after having
510/// lexed either ' or L'.
Chris Lattnercb283342006-06-18 06:48:37 +0000511void Lexer::LexCharConstant(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000512 const char *NulCharacter = 0; // Does this character contain the \0 character?
513
514 // Handle the common case of 'x' and '\y' efficiently.
515 char C = getAndAdvanceChar(CurPtr, Result);
516 if (C == '\'') {
Chris Lattnera5f4c882006-07-20 06:08:47 +0000517 if (!LexingRawMode) Diag(BufferPtr, diag::err_empty_character);
Chris Lattner8c204872006-10-14 05:19:21 +0000518 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000519 FormTokenWithChars(Result, CurPtr);
520 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000521 } else if (C == '\\') {
522 // Skip the escaped character.
523 // FIXME: UCN's.
524 C = getAndAdvanceChar(CurPtr, Result);
525 }
526
527 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
528 ++CurPtr;
529 } else {
530 // Fall back on generic code for embedded nulls, newlines, wide chars.
531 do {
532 // Skip escaped characters.
533 if (C == '\\') {
534 // Skip the escaped character.
535 C = getAndAdvanceChar(CurPtr, Result);
536 } else if (C == '\n' || C == '\r' || // Newline.
537 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000538 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner8c204872006-10-14 05:19:21 +0000539 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000540 FormTokenWithChars(Result, CurPtr-1);
541 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000542 } else if (C == 0) {
543 NulCharacter = CurPtr-1;
544 }
545 C = getAndAdvanceChar(CurPtr, Result);
546 } while (C != '\'');
547 }
548
Chris Lattnercb283342006-06-18 06:48:37 +0000549 if (NulCharacter) Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000550
Chris Lattner8c204872006-10-14 05:19:21 +0000551 Result.setKind(tok::char_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000552
Chris Lattnerd01e2912006-06-18 16:22:51 +0000553 // Update the location of token as well as BufferPtr.
554 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000555}
556
557/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
558/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattnercb283342006-06-18 06:48:37 +0000559void Lexer::SkipWhitespace(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000560 // Whitespace - Skip it, then return the token after the whitespace.
561 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
562 while (1) {
563 // Skip horizontal whitespace very aggressively.
564 while (isHorizontalWhitespace(Char))
565 Char = *++CurPtr;
566
567 // Otherwise if we something other than whitespace, we're done.
568 if (Char != '\n' && Char != '\r')
569 break;
570
571 if (ParsingPreprocessorDirective) {
572 // End of preprocessor directive line, let LexTokenInternal handle this.
573 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000574 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000575 }
576
577 // ok, but handle newline.
578 // The returned token is at the start of the line.
Chris Lattner8c204872006-10-14 05:19:21 +0000579 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000580 // No leading whitespace seen so far.
Chris Lattner8c204872006-10-14 05:19:21 +0000581 Result.clearFlag(LexerToken::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000582 Char = *++CurPtr;
583 }
584
585 // If this isn't immediately after a newline, there is leading space.
586 char PrevChar = CurPtr[-1];
587 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattner8c204872006-10-14 05:19:21 +0000588 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000589
590 // If the next token is obviously a // or /* */ comment, skip it efficiently
591 // too (without going through the big switch stmt).
Chris Lattner457fc152006-07-29 06:30:25 +0000592 if (Char == '/' && CurPtr[1] == '/' && !KeepCommentMode) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000593 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000594 SkipBCPLComment(Result, CurPtr+1);
595 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000596 }
Chris Lattner457fc152006-07-29 06:30:25 +0000597 if (Char == '/' && CurPtr[1] == '*' && !KeepCommentMode) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000598 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000599 SkipBlockComment(Result, CurPtr+2);
600 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000601 }
602 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000603}
604
605// SkipBCPLComment - We have just read the // characters from input. Skip until
606// we find the newline character thats terminate the comment. Then update
607/// BufferPtr and return.
Chris Lattner457fc152006-07-29 06:30:25 +0000608bool Lexer::SkipBCPLComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000609 // If BCPL comments aren't explicitly enabled for this language, emit an
610 // extension warning.
611 if (!Features.BCPLComment) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000612 Diag(BufferPtr, diag::ext_bcpl_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000613
614 // Mark them enabled so we only emit one warning for this translation
615 // unit.
616 Features.BCPLComment = true;
617 }
618
619 // Scan over the body of the comment. The common case, when scanning, is that
620 // the comment contains normal ascii characters with nothing interesting in
621 // them. As such, optimize for this case with the inner loop.
622 char C;
623 do {
624 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +0000625 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
626 // If we find a \n character, scan backwards, checking to see if it's an
627 // escaped newline, like we do for block comments.
Chris Lattner22eb9722006-06-18 05:43:12 +0000628
629 // Skip over characters in the fast loop.
630 while (C != 0 && // Potentially EOF.
631 C != '\\' && // Potentially escaped newline.
632 C != '?' && // Potentially trigraph.
633 C != '\n' && C != '\r') // Newline or DOS-style newline.
634 C = *++CurPtr;
635
636 // If this is a newline, we're done.
637 if (C == '\n' || C == '\r')
638 break; // Found the newline? Break out!
639
640 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
641 // properly decode the character.
642 const char *OldPtr = CurPtr;
643 C = getAndAdvanceChar(CurPtr, Result);
644
645 // If we read multiple characters, and one of those characters was a \r or
Chris Lattnerff591e22007-06-09 06:07:22 +0000646 // \n, then we had an escaped newline within the comment. Emit diagnostic
647 // unless the next line is also a // comment.
648 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000649 for (; OldPtr != CurPtr; ++OldPtr)
650 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnerff591e22007-06-09 06:07:22 +0000651 // Okay, we found a // comment that ends in a newline, if the next
652 // line is also a // comment, but has spaces, don't emit a diagnostic.
653 if (isspace(C)) {
654 const char *ForwardPtr = CurPtr;
655 while (isspace(*ForwardPtr)) // Skip whitespace.
656 ++ForwardPtr;
657 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
658 break;
659 }
660
Chris Lattnercb283342006-06-18 06:48:37 +0000661 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
662 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000663 }
664 }
665
Chris Lattner457fc152006-07-29 06:30:25 +0000666 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
Chris Lattner22eb9722006-06-18 05:43:12 +0000667 } while (C != '\n' && C != '\r');
668
Chris Lattner457fc152006-07-29 06:30:25 +0000669 // Found but did not consume the newline.
670
671 // If we are returning comments as tokens, return this comment as a token.
672 if (KeepCommentMode)
673 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000674
675 // If we are inside a preprocessor directive and we see the end of line,
676 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +0000677 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000678 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000679 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000680 }
681
682 // Otherwise, eat the \n character. We don't care if this is a \n\r or
683 // \r\n sequence.
684 ++CurPtr;
685
686 // The next returned token is at the start of the line.
Chris Lattner8c204872006-10-14 05:19:21 +0000687 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000688 // No leading whitespace seen so far.
Chris Lattner8c204872006-10-14 05:19:21 +0000689 Result.clearFlag(LexerToken::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000690
691 // It is common for the tokens immediately after a // comment to be
692 // whitespace (indentation for the next line). Instead of going through the
693 // big switch, handle it efficiently now.
694 if (isWhitespace(*CurPtr)) {
Chris Lattner8c204872006-10-14 05:19:21 +0000695 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000696 SkipWhitespace(Result, CurPtr+1);
697 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000698 }
699
700 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000701 return true;
702}
Chris Lattner22eb9722006-06-18 05:43:12 +0000703
Chris Lattner457fc152006-07-29 06:30:25 +0000704/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
705/// an appropriate way and return it.
706bool Lexer::SaveBCPLComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner8c204872006-10-14 05:19:21 +0000707 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +0000708 FormTokenWithChars(Result, CurPtr);
709
710 // If this BCPL-style comment is in a macro definition, transmogrify it into
711 // a C-style block comment.
712 if (ParsingPreprocessorDirective) {
713 std::string Spelling = PP.getSpelling(Result);
714 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
715 Spelling[1] = '*'; // Change prefix to "/*".
716 Spelling += "*/"; // add suffix.
717
Chris Lattner8c204872006-10-14 05:19:21 +0000718 Result.setLocation(PP.CreateString(&Spelling[0], Spelling.size(),
Chris Lattner457fc152006-07-29 06:30:25 +0000719 Result.getLocation()));
Chris Lattner8c204872006-10-14 05:19:21 +0000720 Result.setLength(Spelling.size());
Chris Lattner457fc152006-07-29 06:30:25 +0000721 }
722 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000723}
724
Chris Lattnercb283342006-06-18 06:48:37 +0000725/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
726/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner22eb9722006-06-18 05:43:12 +0000727/// diagnostic if so. We know that the is inside of a block comment.
Chris Lattner1f583052006-06-18 06:53:56 +0000728static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
729 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000730 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Chris Lattner22eb9722006-06-18 05:43:12 +0000731
732 // Back up off the newline.
733 --CurPtr;
734
735 // If this is a two-character newline sequence, skip the other character.
736 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
737 // \n\n or \r\r -> not escaped newline.
738 if (CurPtr[0] == CurPtr[1])
739 return false;
740 // \n\r or \r\n -> skip the newline.
741 --CurPtr;
742 }
743
744 // If we have horizontal whitespace, skip over it. We allow whitespace
745 // between the slash and newline.
746 bool HasSpace = false;
747 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
748 --CurPtr;
749 HasSpace = true;
750 }
751
752 // If we have a slash, we know this is an escaped newline.
753 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +0000754 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000755 } else {
756 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +0000757 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
758 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +0000759 return false;
Chris Lattnercb283342006-06-18 06:48:37 +0000760
761 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +0000762 CurPtr -= 2;
763
764 // If no trigraphs are enabled, warn that we ignored this trigraph and
765 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +0000766 if (!L->getFeatures().Trigraphs) {
767 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000768 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000769 }
Chris Lattner1f583052006-06-18 06:53:56 +0000770 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000771 }
772
773 // Warn about having an escaped newline between the */ characters.
Chris Lattner1f583052006-06-18 06:53:56 +0000774 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Chris Lattner22eb9722006-06-18 05:43:12 +0000775
776 // If there was space between the backslash and newline, warn about it.
Chris Lattner1f583052006-06-18 06:53:56 +0000777 if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space);
Chris Lattner22eb9722006-06-18 05:43:12 +0000778
Chris Lattnercb283342006-06-18 06:48:37 +0000779 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000780}
781
Chris Lattneraded4a92006-10-27 04:42:31 +0000782#ifdef __SSE2__
783#include <emmintrin.h>
Chris Lattner9f6604f2006-10-30 20:01:22 +0000784#elif __ALTIVEC__
785#include <altivec.h>
786#undef bool
Chris Lattneraded4a92006-10-27 04:42:31 +0000787#endif
788
Chris Lattner22eb9722006-06-18 05:43:12 +0000789/// SkipBlockComment - We have just read the /* characters from input. Read
790/// until we find the */ characters that terminate the comment. Note that we
791/// don't bother decoding trigraphs or escaped newlines in block comments,
792/// because they cannot cause the comment to end. The only thing that can
793/// happen is the comment could end with an escaped newline between the */ end
794/// of comment.
Chris Lattner457fc152006-07-29 06:30:25 +0000795bool Lexer::SkipBlockComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000796 // Scan one character past where we should, looking for a '/' character. Once
797 // we find it, check to see if it was preceeded by a *. This common
798 // optimization helps people who like to put a lot of * characters in their
799 // comments.
800 unsigned char C = *CurPtr++;
801 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000802 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000803 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +0000804 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000805 }
806
807 while (1) {
Chris Lattner6cc3e362006-10-27 04:12:35 +0000808 // Skip over all non-interesting characters until we find end of buffer or a
809 // (probably ending) '/' character.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000810 if (CurPtr + 24 < BufferEnd) {
811 // While not aligned to a 16-byte boundary.
812 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
813 C = *CurPtr++;
814
815 if (C == '/') goto FoundSlash;
Chris Lattneraded4a92006-10-27 04:42:31 +0000816
817#ifdef __SSE2__
818 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
819 '/', '/', '/', '/', '/', '/', '/', '/');
820 while (CurPtr+16 <= BufferEnd &&
821 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
822 CurPtr += 16;
Chris Lattner9f6604f2006-10-30 20:01:22 +0000823#elif __ALTIVEC__
824 __vector unsigned char Slashes = {
825 '/', '/', '/', '/', '/', '/', '/', '/',
826 '/', '/', '/', '/', '/', '/', '/', '/'
827 };
828 while (CurPtr+16 <= BufferEnd &&
829 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
830 CurPtr += 16;
831#else
Chris Lattneraded4a92006-10-27 04:42:31 +0000832 // Scan for '/' quickly. Many block comments are very large.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000833 while (CurPtr[0] != '/' &&
834 CurPtr[1] != '/' &&
835 CurPtr[2] != '/' &&
836 CurPtr[3] != '/' &&
837 CurPtr+4 < BufferEnd) {
838 CurPtr += 4;
839 }
Chris Lattneraded4a92006-10-27 04:42:31 +0000840#endif
841
842 // It has to be one of the bytes scanned, increment to it and read one.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000843 C = *CurPtr++;
844 }
845
Chris Lattneraded4a92006-10-27 04:42:31 +0000846 // Loop to scan the remainder.
Chris Lattner22eb9722006-06-18 05:43:12 +0000847 while (C != '/' && C != '\0')
848 C = *CurPtr++;
849
Chris Lattner6cc3e362006-10-27 04:12:35 +0000850 FoundSlash:
Chris Lattner22eb9722006-06-18 05:43:12 +0000851 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000852 if (CurPtr[-2] == '*') // We found the final */. We're done!
853 break;
854
855 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +0000856 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000857 // We found the final */, though it had an escaped newline between the
858 // * and /. We're done!
859 break;
860 }
861 }
862 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
863 // If this is a /* inside of the comment, emit a warning. Don't do this
864 // if this is a /*/, which will end the comment. This misses cases with
865 // embedded escaped newlines, but oh well.
Chris Lattnercb283342006-06-18 06:48:37 +0000866 Diag(CurPtr-1, diag::nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000867 }
868 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000869 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000870 // Note: the user probably forgot a */. We could continue immediately
871 // after the /*, but this would involve lexing a lot of what really is the
872 // comment, which surely would confuse the parser.
873 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +0000874 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000875 }
876 C = *CurPtr++;
877 }
Chris Lattner457fc152006-07-29 06:30:25 +0000878
879 // If we are returning comments as tokens, return this comment as a token.
880 if (KeepCommentMode) {
Chris Lattner8c204872006-10-14 05:19:21 +0000881 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +0000882 FormTokenWithChars(Result, CurPtr);
883 return false;
884 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000885
886 // It is common for the tokens immediately after a /**/ comment to be
887 // whitespace. Instead of going through the big switch, handle it
888 // efficiently now.
889 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner8c204872006-10-14 05:19:21 +0000890 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000891 SkipWhitespace(Result, CurPtr+1);
892 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000893 }
894
895 // Otherwise, just return so that the next character will be lexed as a token.
896 BufferPtr = CurPtr;
Chris Lattner8c204872006-10-14 05:19:21 +0000897 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000898 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000899}
900
901//===----------------------------------------------------------------------===//
902// Primary Lexing Entry Points
903//===----------------------------------------------------------------------===//
904
905/// LexIncludeFilename - After the preprocessor has parsed a #include, lex and
906/// (potentially) macro expand the filename.
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000907void Lexer::LexIncludeFilename(LexerToken &FilenameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000908 assert(ParsingPreprocessorDirective &&
909 ParsingFilename == false &&
910 "Must be in a preprocessing directive!");
911
912 // We are now parsing a filename!
913 ParsingFilename = true;
914
Chris Lattner269c2322006-06-25 06:23:00 +0000915 // Lex the filename.
916 Lex(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000917
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000918 // We should have obtained the filename now.
Chris Lattner22eb9722006-06-18 05:43:12 +0000919 ParsingFilename = false;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000920
Chris Lattner22eb9722006-06-18 05:43:12 +0000921 // No filename?
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000922 if (FilenameTok.getKind() == tok::eom)
Chris Lattner538d7f32006-07-20 04:31:52 +0000923 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner22eb9722006-06-18 05:43:12 +0000924}
925
926/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
927/// uninterpreted string. This switches the lexer out of directive mode.
928std::string Lexer::ReadToEndOfLine() {
929 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
930 "Must be in a preprocessing directive!");
931 std::string Result;
932 LexerToken Tmp;
933
934 // CurPtr - Cache BufferPtr in an automatic variable.
935 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000936 while (1) {
937 char Char = getAndAdvanceChar(CurPtr, Tmp);
938 switch (Char) {
939 default:
940 Result += Char;
941 break;
942 case 0: // Null.
943 // Found end of file?
944 if (CurPtr-1 != BufferEnd) {
945 // Nope, normal character, continue.
946 Result += Char;
947 break;
948 }
949 // FALL THROUGH.
950 case '\r':
951 case '\n':
952 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
953 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
954 BufferPtr = CurPtr-1;
955
956 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +0000957 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000958 assert(Tmp.getKind() == tok::eom && "Unexpected token!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000959
960 // Finally, we're done, return the string we found.
961 return Result;
962 }
963 }
964}
965
966/// LexEndOfFile - CurPtr points to the end of this file. Handle this
967/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +0000968/// This returns true if Result contains a token, false if PP.Lex should be
969/// called again.
970bool Lexer::LexEndOfFile(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000971 // If we hit the end of the file while parsing a preprocessor directive,
972 // end the preprocessor directive first. The next token returned will
973 // then be the end of file.
974 if (ParsingPreprocessorDirective) {
975 // Done parsing the "line".
976 ParsingPreprocessorDirective = false;
Chris Lattner8c204872006-10-14 05:19:21 +0000977 Result.setKind(tok::eom);
Chris Lattnerd01e2912006-06-18 16:22:51 +0000978 // Update the location of token as well as BufferPtr.
979 FormTokenWithChars(Result, CurPtr);
Chris Lattner457fc152006-07-29 06:30:25 +0000980
981 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerb352e3e2006-11-21 06:17:10 +0000982 KeepCommentMode = PP.getCommentRetentionState();
Chris Lattner2183a6e2006-07-18 06:36:12 +0000983 return true; // Have a token.
Chris Lattner22eb9722006-06-18 05:43:12 +0000984 }
985
Chris Lattner30a2fa12006-07-19 06:31:49 +0000986 // If we are in raw mode, return this event as an EOF token. Let the caller
987 // that put us in raw mode handle the event.
988 if (LexingRawMode) {
Chris Lattner8c204872006-10-14 05:19:21 +0000989 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +0000990 BufferPtr = BufferEnd;
991 FormTokenWithChars(Result, BufferEnd);
Chris Lattner8c204872006-10-14 05:19:21 +0000992 Result.setKind(tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +0000993 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000994 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000995
Chris Lattner30a2fa12006-07-19 06:31:49 +0000996 // Otherwise, issue diagnostics for unterminated #if and missing newline.
997
998 // If we are in a #if directive, emit an error.
999 while (!ConditionalStack.empty()) {
Chris Lattner538d7f32006-07-20 04:31:52 +00001000 Diag(ConditionalStack.back().IfLoc, diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001001 ConditionalStack.pop_back();
1002 }
1003
1004 // If the file was empty or didn't end in a newline, issue a pedwarn.
1005 if (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1006 Diag(BufferEnd, diag::ext_no_newline_eof);
1007
Chris Lattner22eb9722006-06-18 05:43:12 +00001008 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +00001009
1010 // Finally, let the preprocessor handle this.
Chris Lattner2183a6e2006-07-18 06:36:12 +00001011 return PP.HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001012}
1013
Chris Lattner678c8802006-07-11 05:46:12 +00001014/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1015/// the specified lexer will return a tok::l_paren token, 0 if it is something
1016/// else and 2 if there are no more tokens in the buffer controlled by the
1017/// lexer.
1018unsigned Lexer::isNextPPTokenLParen() {
1019 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1020
1021 // Switch to 'skipping' mode. This will ensure that we can lex a token
1022 // without emitting diagnostics, disables macro expansion, and will cause EOF
1023 // to return an EOF token instead of popping the include stack.
1024 LexingRawMode = true;
1025
1026 // Save state that can be changed while lexing so that we can restore it.
1027 const char *TmpBufferPtr = BufferPtr;
1028
1029 LexerToken Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001030 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001031 LexTokenInternal(Tok);
1032
1033 // Restore state that may have changed.
1034 BufferPtr = TmpBufferPtr;
1035
1036 // Restore the lexer back to non-skipping mode.
1037 LexingRawMode = false;
1038
1039 if (Tok.getKind() == tok::eof)
1040 return 2;
1041 return Tok.getKind() == tok::l_paren;
1042}
1043
Chris Lattner22eb9722006-06-18 05:43:12 +00001044
1045/// LexTokenInternal - This implements a simple C family lexer. It is an
1046/// extremely performance critical piece of code. This assumes that the buffer
1047/// has a null character at the end of the file. Return true if an error
1048/// occurred and compilation should terminate, false if normal. This returns a
1049/// preprocessing token, not a normal token, as such, it is an internal
1050/// interface. It assumes that the Flags of result have been cleared before
1051/// calling this.
Chris Lattnercb283342006-06-18 06:48:37 +00001052void Lexer::LexTokenInternal(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001053LexNextToken:
1054 // New token, can't need cleaning yet.
Chris Lattner8c204872006-10-14 05:19:21 +00001055 Result.clearFlag(LexerToken::NeedsCleaning);
1056 Result.setIdentifierInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001057
1058 // CurPtr - Cache BufferPtr in an automatic variable.
1059 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001060
Chris Lattnereb54b592006-07-10 06:34:27 +00001061 // Small amounts of horizontal whitespace is very common between tokens.
1062 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1063 ++CurPtr;
1064 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1065 ++CurPtr;
1066 BufferPtr = CurPtr;
Chris Lattner8c204872006-10-14 05:19:21 +00001067 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001068 }
1069
Chris Lattner22eb9722006-06-18 05:43:12 +00001070 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1071
1072 // Read a character, advancing over it.
1073 char Char = getAndAdvanceChar(CurPtr, Result);
1074 switch (Char) {
1075 case 0: // Null.
1076 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001077 if (CurPtr-1 == BufferEnd) {
1078 // Read the PP instance variable into an automatic variable, because
1079 // LexEndOfFile will often delete 'this'.
1080 Preprocessor &PPCache = PP;
1081 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1082 return; // Got a token to return.
1083 return PPCache.Lex(Result);
1084 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001085
Chris Lattnercb283342006-06-18 06:48:37 +00001086 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner8c204872006-10-14 05:19:21 +00001087 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001088 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001089 goto LexNextToken; // GCC isn't tail call eliminating.
1090 case '\n':
1091 case '\r':
1092 // If we are inside a preprocessor directive and we see the end of line,
1093 // we know we are done with the directive, so return an EOM token.
1094 if (ParsingPreprocessorDirective) {
1095 // Done parsing the "line".
1096 ParsingPreprocessorDirective = false;
1097
Chris Lattner457fc152006-07-29 06:30:25 +00001098 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001099 KeepCommentMode = PP.getCommentRetentionState();
Chris Lattner457fc152006-07-29 06:30:25 +00001100
Chris Lattner22eb9722006-06-18 05:43:12 +00001101 // Since we consumed a newline, we are back at the start of a line.
1102 IsAtStartOfLine = true;
1103
Chris Lattner8c204872006-10-14 05:19:21 +00001104 Result.setKind(tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001105 break;
1106 }
1107 // The returned token is at the start of the line.
Chris Lattner8c204872006-10-14 05:19:21 +00001108 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001109 // No leading whitespace seen so far.
Chris Lattner8c204872006-10-14 05:19:21 +00001110 Result.clearFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001111 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001112 goto LexNextToken; // GCC isn't tail call eliminating.
1113 case ' ':
1114 case '\t':
1115 case '\f':
1116 case '\v':
Chris Lattner8c204872006-10-14 05:19:21 +00001117 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001118 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001119 goto LexNextToken; // GCC isn't tail call eliminating.
1120
1121 case 'L':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001122 // Notify MIOpt that we read a non-whitespace/non-comment token.
1123 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001124 Char = getCharAndSize(CurPtr, SizeTmp);
1125
1126 // Wide string literal.
1127 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001128 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1129 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001130
1131 // Wide character constant.
1132 if (Char == '\'')
1133 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1134 // FALL THROUGH, treating L like the start of an identifier.
1135
1136 // C99 6.4.2: Identifiers.
1137 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1138 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1139 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1140 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1141 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1142 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1143 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1144 case 'v': case 'w': case 'x': case 'y': case 'z':
1145 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001146 // Notify MIOpt that we read a non-whitespace/non-comment token.
1147 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001148 return LexIdentifier(Result, CurPtr);
1149
1150 // C99 6.4.4.1: Integer Constants.
1151 // C99 6.4.4.2: Floating Constants.
1152 case '0': case '1': case '2': case '3': case '4':
1153 case '5': case '6': case '7': case '8': case '9':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001154 // Notify MIOpt that we read a non-whitespace/non-comment token.
1155 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001156 return LexNumericConstant(Result, CurPtr);
1157
1158 // C99 6.4.4: Character Constants.
1159 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001160 // Notify MIOpt that we read a non-whitespace/non-comment token.
1161 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001162 return LexCharConstant(Result, CurPtr);
1163
1164 // C99 6.4.5: String Literals.
1165 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001166 // Notify MIOpt that we read a non-whitespace/non-comment token.
1167 MIOpt.ReadToken();
Chris Lattnerd3e98952006-10-06 05:22:26 +00001168 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001169
1170 // C99 6.4.6: Punctuators.
1171 case '?':
Chris Lattner8c204872006-10-14 05:19:21 +00001172 Result.setKind(tok::question);
Chris Lattner22eb9722006-06-18 05:43:12 +00001173 break;
1174 case '[':
Chris Lattner8c204872006-10-14 05:19:21 +00001175 Result.setKind(tok::l_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001176 break;
1177 case ']':
Chris Lattner8c204872006-10-14 05:19:21 +00001178 Result.setKind(tok::r_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001179 break;
1180 case '(':
Chris Lattner8c204872006-10-14 05:19:21 +00001181 Result.setKind(tok::l_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001182 break;
1183 case ')':
Chris Lattner8c204872006-10-14 05:19:21 +00001184 Result.setKind(tok::r_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001185 break;
1186 case '{':
Chris Lattner8c204872006-10-14 05:19:21 +00001187 Result.setKind(tok::l_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001188 break;
1189 case '}':
Chris Lattner8c204872006-10-14 05:19:21 +00001190 Result.setKind(tok::r_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001191 break;
1192 case '.':
1193 Char = getCharAndSize(CurPtr, SizeTmp);
1194 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001195 // Notify MIOpt that we read a non-whitespace/non-comment token.
1196 MIOpt.ReadToken();
1197
Chris Lattner22eb9722006-06-18 05:43:12 +00001198 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1199 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001200 Result.setKind(tok::periodstar);
Chris Lattner22eb9722006-06-18 05:43:12 +00001201 CurPtr += SizeTmp;
1202 } else if (Char == '.' &&
1203 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner8c204872006-10-14 05:19:21 +00001204 Result.setKind(tok::ellipsis);
Chris Lattner22eb9722006-06-18 05:43:12 +00001205 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1206 SizeTmp2, Result);
1207 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001208 Result.setKind(tok::period);
Chris Lattner22eb9722006-06-18 05:43:12 +00001209 }
1210 break;
1211 case '&':
1212 Char = getCharAndSize(CurPtr, SizeTmp);
1213 if (Char == '&') {
Chris Lattner8c204872006-10-14 05:19:21 +00001214 Result.setKind(tok::ampamp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001215 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1216 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001217 Result.setKind(tok::ampequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001218 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1219 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001220 Result.setKind(tok::amp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001221 }
1222 break;
1223 case '*':
1224 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001225 Result.setKind(tok::starequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001226 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1227 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001228 Result.setKind(tok::star);
Chris Lattner22eb9722006-06-18 05:43:12 +00001229 }
1230 break;
1231 case '+':
1232 Char = getCharAndSize(CurPtr, SizeTmp);
1233 if (Char == '+') {
Chris Lattner8c204872006-10-14 05:19:21 +00001234 Result.setKind(tok::plusplus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001235 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1236 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001237 Result.setKind(tok::plusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001238 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1239 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001240 Result.setKind(tok::plus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001241 }
1242 break;
1243 case '-':
1244 Char = getCharAndSize(CurPtr, SizeTmp);
1245 if (Char == '-') {
Chris Lattner8c204872006-10-14 05:19:21 +00001246 Result.setKind(tok::minusminus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001247 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1248 } else if (Char == '>' && Features.CPlusPlus &&
1249 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001250 Result.setKind(tok::arrowstar); // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00001251 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1252 SizeTmp2, Result);
1253 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001254 Result.setKind(tok::arrow);
Chris Lattner22eb9722006-06-18 05:43:12 +00001255 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1256 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001257 Result.setKind(tok::minusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001258 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1259 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001260 Result.setKind(tok::minus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001261 }
1262 break;
1263 case '~':
Chris Lattner8c204872006-10-14 05:19:21 +00001264 Result.setKind(tok::tilde);
Chris Lattner22eb9722006-06-18 05:43:12 +00001265 break;
1266 case '!':
1267 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001268 Result.setKind(tok::exclaimequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001269 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1270 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001271 Result.setKind(tok::exclaim);
Chris Lattner22eb9722006-06-18 05:43:12 +00001272 }
1273 break;
1274 case '/':
1275 // 6.4.9: Comments
1276 Char = getCharAndSize(CurPtr, SizeTmp);
1277 if (Char == '/') { // BCPL comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001278 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1279 goto LexNextToken; // GCC isn't tail call eliminating.
1280 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001281 } else if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001282 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1283 goto LexNextToken; // GCC isn't tail call eliminating.
1284 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001285 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001286 Result.setKind(tok::slashequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001287 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1288 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001289 Result.setKind(tok::slash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001290 }
1291 break;
1292 case '%':
1293 Char = getCharAndSize(CurPtr, SizeTmp);
1294 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001295 Result.setKind(tok::percentequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001296 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1297 } else if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001298 Result.setKind(tok::r_brace); // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00001299 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1300 } else if (Features.Digraphs && Char == ':') {
1301 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001302 Char = getCharAndSize(CurPtr, SizeTmp);
1303 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001304 Result.setKind(tok::hashhash); // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00001305 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1306 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001307 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001308 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001309 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1310 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner22eb9722006-06-18 05:43:12 +00001311 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001312 Result.setKind(tok::hash); // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00001313
1314 // We parsed a # character. If this occurs at the start of the line,
1315 // it's actually the start of a preprocessing directive. Callback to
1316 // the preprocessor to handle it.
1317 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001318 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001319 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001320 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001321
1322 // As an optimization, if the preprocessor didn't switch lexers, tail
1323 // recurse.
1324 if (PP.isCurrentLexer(this)) {
1325 // Start a new token. If this is a #include or something, the PP may
1326 // want us starting at the beginning of the line again. If so, set
1327 // the StartOfLine flag.
1328 if (IsAtStartOfLine) {
Chris Lattner8c204872006-10-14 05:19:21 +00001329 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001330 IsAtStartOfLine = false;
1331 }
1332 goto LexNextToken; // GCC isn't tail call eliminating.
1333 }
1334
1335 return PP.Lex(Result);
1336 }
1337 }
1338 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001339 Result.setKind(tok::percent);
Chris Lattner22eb9722006-06-18 05:43:12 +00001340 }
1341 break;
1342 case '<':
1343 Char = getCharAndSize(CurPtr, SizeTmp);
1344 if (ParsingFilename) {
1345 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1346 } else if (Char == '<' &&
1347 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001348 Result.setKind(tok::lesslessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001349 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1350 SizeTmp2, Result);
1351 } else if (Char == '<') {
Chris Lattner8c204872006-10-14 05:19:21 +00001352 Result.setKind(tok::lessless);
Chris Lattner22eb9722006-06-18 05:43:12 +00001353 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1354 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001355 Result.setKind(tok::lessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001356 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1357 } else if (Features.Digraphs && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001358 Result.setKind(tok::l_square); // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00001359 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1360 } else if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001361 Result.setKind(tok::l_brace); // '<%' -> '{'
Chris Lattner22eb9722006-06-18 05:43:12 +00001362 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001363 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001364 Result.setKind(tok::less);
Chris Lattner22eb9722006-06-18 05:43:12 +00001365 }
1366 break;
1367 case '>':
1368 Char = getCharAndSize(CurPtr, SizeTmp);
1369 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001370 Result.setKind(tok::greaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001371 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1372 } else if (Char == '>' &&
1373 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001374 Result.setKind(tok::greatergreaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001375 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1376 SizeTmp2, Result);
1377 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001378 Result.setKind(tok::greatergreater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001379 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001380 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001381 Result.setKind(tok::greater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001382 }
1383 break;
1384 case '^':
1385 Char = getCharAndSize(CurPtr, SizeTmp);
1386 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001387 Result.setKind(tok::caretequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001388 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1389 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001390 Result.setKind(tok::caret);
Chris Lattner22eb9722006-06-18 05:43:12 +00001391 }
1392 break;
1393 case '|':
1394 Char = getCharAndSize(CurPtr, SizeTmp);
1395 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001396 Result.setKind(tok::pipeequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001397 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1398 } else if (Char == '|') {
Chris Lattner8c204872006-10-14 05:19:21 +00001399 Result.setKind(tok::pipepipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001400 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1401 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001402 Result.setKind(tok::pipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001403 }
1404 break;
1405 case ':':
1406 Char = getCharAndSize(CurPtr, SizeTmp);
1407 if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001408 Result.setKind(tok::r_square); // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00001409 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1410 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001411 Result.setKind(tok::coloncolon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001412 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1413 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001414 Result.setKind(tok::colon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001415 }
1416 break;
1417 case ';':
Chris Lattner8c204872006-10-14 05:19:21 +00001418 Result.setKind(tok::semi);
Chris Lattner22eb9722006-06-18 05:43:12 +00001419 break;
1420 case '=':
1421 Char = getCharAndSize(CurPtr, SizeTmp);
1422 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001423 Result.setKind(tok::equalequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001424 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1425 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001426 Result.setKind(tok::equal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001427 }
1428 break;
1429 case ',':
Chris Lattner8c204872006-10-14 05:19:21 +00001430 Result.setKind(tok::comma);
Chris Lattner22eb9722006-06-18 05:43:12 +00001431 break;
1432 case '#':
1433 Char = getCharAndSize(CurPtr, SizeTmp);
1434 if (Char == '#') {
Chris Lattner8c204872006-10-14 05:19:21 +00001435 Result.setKind(tok::hashhash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001436 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001437 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001438 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001439 Diag(BufferPtr, diag::charize_microsoft_ext);
1440 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001441 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001442 Result.setKind(tok::hash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001443 // We parsed a # character. If this occurs at the start of the line,
1444 // it's actually the start of a preprocessing directive. Callback to
1445 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00001446 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001447 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001448 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001449 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001450
1451 // As an optimization, if the preprocessor didn't switch lexers, tail
1452 // recurse.
1453 if (PP.isCurrentLexer(this)) {
1454 // Start a new token. If this is a #include or something, the PP may
1455 // want us starting at the beginning of the line again. If so, set
1456 // the StartOfLine flag.
1457 if (IsAtStartOfLine) {
Chris Lattner8c204872006-10-14 05:19:21 +00001458 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001459 IsAtStartOfLine = false;
1460 }
1461 goto LexNextToken; // GCC isn't tail call eliminating.
1462 }
1463 return PP.Lex(Result);
1464 }
1465 }
1466 break;
1467
1468 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00001469 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00001470 // FALL THROUGH.
1471 default:
1472 // Objective C support.
1473 if (CurPtr[-1] == '@' && Features.ObjC1) {
Chris Lattner8c204872006-10-14 05:19:21 +00001474 Result.setKind(tok::at);
Chris Lattner22eb9722006-06-18 05:43:12 +00001475 break;
1476 } else if (CurPtr[-1] == '$' && Features.DollarIdents) {// $ in identifiers.
Chris Lattnercb283342006-06-18 06:48:37 +00001477 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001478 // Notify MIOpt that we read a non-whitespace/non-comment token.
1479 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001480 return LexIdentifier(Result, CurPtr);
1481 }
1482
Chris Lattner8c204872006-10-14 05:19:21 +00001483 Result.setKind(tok::unknown);
Chris Lattner041bef82006-07-11 05:52:53 +00001484 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001485 }
1486
Chris Lattner371ac8a2006-07-04 07:11:10 +00001487 // Notify MIOpt that we read a non-whitespace/non-comment token.
1488 MIOpt.ReadToken();
1489
Chris Lattnerd01e2912006-06-18 16:22:51 +00001490 // Update the location of token as well as BufferPtr.
1491 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001492}