blob: 4efd62113d4f75dbf0d832e89a2408a9e6549fd3 [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 Lattnerdc5c0552007-07-20 16:37:10 +000030#include "clang/Basic/SourceManager.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 clang;
34
35static void InitCharacterInfo();
36
Chris Lattner77e9de52007-07-20 16:52:03 +000037Lexer::Lexer(SourceLocation fileloc, Preprocessor &pp,
38 const char *BufStart, const char *BufEnd)
39 : FileLoc(fileloc), PP(pp), Features(PP.getLangOptions()) {
40
41 SourceManager &SourceMgr = PP.getSourceManager();
42 InputFile =SourceMgr.getBuffer(SourceMgr.getPhysicalLoc(FileLoc).getFileID());
43
Chris Lattnerecfeafe2006-07-02 21:26:45 +000044 Is_PragmaLexer = false;
Chris Lattner4ec473f2006-07-03 05:16:05 +000045 IsMainFile = false;
Chris Lattner22eb9722006-06-18 05:43:12 +000046 InitCharacterInfo();
47
Chris Lattner77e9de52007-07-20 16:52:03 +000048 BufferPtr = BufStart ? BufStart : InputFile->getBufferStart();
49 BufferEnd = BufEnd ? BufEnd : InputFile->getBufferEnd();
50
Chris Lattner22eb9722006-06-18 05:43:12 +000051 assert(BufferEnd[0] == 0 &&
52 "We assume that the input buffer has a null character at the end"
53 " to simplify lexing!");
Chris Lattner77e9de52007-07-20 16:52:03 +000054
Chris Lattner22eb9722006-06-18 05:43:12 +000055 // Start of the file is a start of line.
56 IsAtStartOfLine = true;
57
58 // We are not after parsing a #.
59 ParsingPreprocessorDirective = false;
60
61 // We are not after parsing #include.
62 ParsingFilename = false;
Chris Lattner3ebcf4e2006-07-11 05:39:23 +000063
64 // We are not in raw mode. Raw mode disables diagnostics and interpretation
65 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
66 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
67 // or otherwise skipping over tokens.
68 LexingRawMode = false;
Chris Lattner457fc152006-07-29 06:30:25 +000069
70 // Default to keeping comments if requested.
Chris Lattnerb352e3e2006-11-21 06:17:10 +000071 KeepCommentMode = PP.getCommentRetentionState();
Chris Lattner22eb9722006-06-18 05:43:12 +000072}
73
Chris Lattnere3e81ea2006-07-03 01:13:26 +000074/// Stringify - Convert the specified string into a C string, with surrounding
75/// ""'s, and with escaped \ and " characters.
Chris Lattnerecc39e92006-07-15 05:23:31 +000076std::string Lexer::Stringify(const std::string &Str, bool Charify) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +000077 std::string Result = Str;
Chris Lattnerecc39e92006-07-15 05:23:31 +000078 char Quote = Charify ? '\'' : '"';
Chris Lattnere3e81ea2006-07-03 01:13:26 +000079 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
Chris Lattnerecc39e92006-07-15 05:23:31 +000080 if (Result[i] == '\\' || Result[i] == Quote) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +000081 Result.insert(Result.begin()+i, '\\');
82 ++i; ++e;
83 }
84 }
Chris Lattnerecc39e92006-07-15 05:23:31 +000085 return Result;
Chris Lattnere3e81ea2006-07-03 01:13:26 +000086}
87
Chris Lattner22eb9722006-06-18 05:43:12 +000088
Chris Lattner22eb9722006-06-18 05:43:12 +000089//===----------------------------------------------------------------------===//
90// Character information.
91//===----------------------------------------------------------------------===//
92
93static unsigned char CharInfo[256];
94
95enum {
96 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
97 CHAR_VERT_WS = 0x02, // '\r', '\n'
98 CHAR_LETTER = 0x04, // a-z,A-Z
99 CHAR_NUMBER = 0x08, // 0-9
100 CHAR_UNDER = 0x10, // _
101 CHAR_PERIOD = 0x20 // .
102};
103
104static void InitCharacterInfo() {
105 static bool isInited = false;
106 if (isInited) return;
107 isInited = true;
108
109 // Intiialize the CharInfo table.
110 // TODO: statically initialize this.
111 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
112 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
113 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
114
115 CharInfo[(int)'_'] = CHAR_UNDER;
Chris Lattnerdd0b7cb2006-10-17 02:53:51 +0000116 CharInfo[(int)'.'] = CHAR_PERIOD;
Chris Lattner22eb9722006-06-18 05:43:12 +0000117 for (unsigned i = 'a'; i <= 'z'; ++i)
118 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
119 for (unsigned i = '0'; i <= '9'; ++i)
120 CharInfo[i] = CHAR_NUMBER;
121}
122
123/// isIdentifierBody - Return true if this is the body character of an
124/// identifier, which is [a-zA-Z0-9_].
125static inline bool isIdentifierBody(unsigned char c) {
126 return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER);
127}
128
129/// isHorizontalWhitespace - Return true if this character is horizontal
130/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
131static inline bool isHorizontalWhitespace(unsigned char c) {
132 return CharInfo[c] & CHAR_HORZ_WS;
133}
134
135/// isWhitespace - Return true if this character is horizontal or vertical
136/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
137/// for '\0'.
138static inline bool isWhitespace(unsigned char c) {
139 return CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS);
140}
141
142/// isNumberBody - Return true if this is the body character of an
143/// preprocessing number, which is [a-zA-Z0-9_.].
144static inline bool isNumberBody(unsigned char c) {
145 return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD);
146}
147
Chris Lattnerd01e2912006-06-18 16:22:51 +0000148
Chris Lattner22eb9722006-06-18 05:43:12 +0000149//===----------------------------------------------------------------------===//
150// Diagnostics forwarding code.
151//===----------------------------------------------------------------------===//
152
153/// getSourceLocation - Return a source location identifier for the specified
154/// offset in the current file.
155SourceLocation Lexer::getSourceLocation(const char *Loc) const {
Chris Lattner8bbfe462006-07-02 22:27:49 +0000156 assert(Loc >= InputFile->getBufferStart() && Loc <= BufferEnd &&
Chris Lattner4cca5ba2006-07-02 20:05:54 +0000157 "Location out of range for this buffer!");
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000158
159 // In the normal case, we're just lexing from a simple file buffer, return
160 // the file id from FileLoc with the offset specified.
161 unsigned CharNo = Loc-InputFile->getBufferStart();
162 if (FileLoc.isFileID())
163 return SourceLocation::getFileLoc(FileLoc.getFileID(), CharNo);
164
165 // Otherwise, we're lexing "mapped tokens". This is used for things like
166 // _Pragma handling. Combine the instantiation location of FileLoc with the
167 // physical location.
168 SourceManager &SourceMgr = PP.getSourceManager();
169
170 // Create a new SLoc which is expanded from logical(FileLoc) but whose
171 // characters come from phys(FileLoc)+Offset.
172 SourceLocation VirtLoc = SourceMgr.getLogicalLoc(FileLoc);
173 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(FileLoc);
174 PhysLoc = SourceLocation::getFileLoc(PhysLoc.getFileID(), CharNo);
175 return SourceMgr.getInstantiationLoc(PhysLoc, VirtLoc);
Chris Lattner22eb9722006-06-18 05:43:12 +0000176}
177
178
179/// Diag - Forwarding function for diagnostics. This translate a source
180/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000181void Lexer::Diag(const char *Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000182 const std::string &Msg) const {
Chris Lattner538d7f32006-07-20 04:31:52 +0000183 if (LexingRawMode && Diagnostic::isNoteWarningOrExtension(DiagID))
184 return;
Chris Lattnercb283342006-06-18 06:48:37 +0000185 PP.Diag(getSourceLocation(Loc), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000186}
Chris Lattner538d7f32006-07-20 04:31:52 +0000187void Lexer::Diag(SourceLocation Loc, unsigned DiagID,
188 const std::string &Msg) const {
189 if (LexingRawMode && Diagnostic::isNoteWarningOrExtension(DiagID))
190 return;
191 PP.Diag(Loc, DiagID, Msg);
192}
193
Chris Lattner22eb9722006-06-18 05:43:12 +0000194
195//===----------------------------------------------------------------------===//
196// Trigraph and Escaped Newline Handling Code.
197//===----------------------------------------------------------------------===//
198
199/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
200/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
201static char GetTrigraphCharForLetter(char Letter) {
202 switch (Letter) {
203 default: return 0;
204 case '=': return '#';
205 case ')': return ']';
206 case '(': return '[';
207 case '!': return '|';
208 case '\'': return '^';
209 case '>': return '}';
210 case '/': return '\\';
211 case '<': return '{';
212 case '-': return '~';
213 }
214}
215
216/// DecodeTrigraphChar - If the specified character is a legal trigraph when
217/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
218/// return the result character. Finally, emit a warning about trigraph use
219/// whether trigraphs are enabled or not.
220static char DecodeTrigraphChar(const char *CP, Lexer *L) {
221 char Res = GetTrigraphCharForLetter(*CP);
222 if (Res && L) {
223 if (!L->getFeatures().Trigraphs) {
224 L->Diag(CP-2, diag::trigraph_ignored);
225 return 0;
226 } else {
227 L->Diag(CP-2, diag::trigraph_converted, std::string()+Res);
228 }
229 }
230 return Res;
231}
232
233/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
234/// get its size, and return it. This is tricky in several cases:
235/// 1. If currently at the start of a trigraph, we warn about the trigraph,
236/// then either return the trigraph (skipping 3 chars) or the '?',
237/// depending on whether trigraphs are enabled or not.
238/// 2. If this is an escaped newline (potentially with whitespace between
239/// the backslash and newline), implicitly skip the newline and return
240/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000241/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000242///
243/// This handles the slow/uncommon case of the getCharAndSize method. Here we
244/// know that we can accumulate into Size, and that we have already incremented
245/// Ptr by Size bytes.
246///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000247/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
248/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000249///
250char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
251 LexerToken *Tok) {
252 // If we have a slash, look for an escaped newline.
253 if (Ptr[0] == '\\') {
254 ++Size;
255 ++Ptr;
256Slash:
257 // Common case, backslash-char where the char is not whitespace.
258 if (!isWhitespace(Ptr[0])) return '\\';
259
260 // See if we have optional whitespace characters followed by a newline.
261 {
262 unsigned SizeTmp = 0;
263 do {
264 ++SizeTmp;
265 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
266 // Remember that this token needs to be cleaned.
Chris Lattner8c204872006-10-14 05:19:21 +0000267 if (Tok) Tok->setFlag(LexerToken::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000268
269 // Warn if there was whitespace between the backslash and newline.
270 if (SizeTmp != 1 && Tok)
271 Diag(Ptr, diag::backslash_newline_space);
272
273 // If this is a \r\n or \n\r, skip the newlines.
274 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
275 Ptr[SizeTmp-1] != Ptr[SizeTmp])
276 ++SizeTmp;
277
278 // Found backslash<whitespace><newline>. Parse the char after it.
279 Size += SizeTmp;
280 Ptr += SizeTmp;
281 // Use slow version to accumulate a correct size field.
282 return getCharAndSizeSlow(Ptr, Size, Tok);
283 }
284 } while (isWhitespace(Ptr[SizeTmp]));
285 }
286
287 // Otherwise, this is not an escaped newline, just return the slash.
288 return '\\';
289 }
290
291 // If this is a trigraph, process it.
292 if (Ptr[0] == '?' && Ptr[1] == '?') {
293 // If this is actually a legal trigraph (not something like "??x"), emit
294 // a trigraph warning. If so, and if trigraphs are enabled, return it.
295 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
296 // Remember that this token needs to be cleaned.
Chris Lattner8c204872006-10-14 05:19:21 +0000297 if (Tok) Tok->setFlag(LexerToken::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000298
299 Ptr += 3;
300 Size += 3;
301 if (C == '\\') goto Slash;
302 return C;
303 }
304 }
305
306 // If this is neither, return a single character.
307 ++Size;
308 return *Ptr;
309}
310
Chris Lattnerd01e2912006-06-18 16:22:51 +0000311
Chris Lattner22eb9722006-06-18 05:43:12 +0000312/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
313/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
314/// and that we have already incremented Ptr by Size bytes.
315///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000316/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
317/// be updated to match.
318char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000319 const LangOptions &Features) {
320 // If we have a slash, look for an escaped newline.
321 if (Ptr[0] == '\\') {
322 ++Size;
323 ++Ptr;
324Slash:
325 // Common case, backslash-char where the char is not whitespace.
326 if (!isWhitespace(Ptr[0])) return '\\';
327
328 // See if we have optional whitespace characters followed by a newline.
329 {
330 unsigned SizeTmp = 0;
331 do {
332 ++SizeTmp;
333 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
334
335 // If this is a \r\n or \n\r, skip the newlines.
336 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
337 Ptr[SizeTmp-1] != Ptr[SizeTmp])
338 ++SizeTmp;
339
340 // Found backslash<whitespace><newline>. Parse the char after it.
341 Size += SizeTmp;
342 Ptr += SizeTmp;
343
344 // Use slow version to accumulate a correct size field.
345 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
346 }
347 } while (isWhitespace(Ptr[SizeTmp]));
348 }
349
350 // Otherwise, this is not an escaped newline, just return the slash.
351 return '\\';
352 }
353
354 // If this is a trigraph, process it.
355 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
356 // If this is actually a legal trigraph (not something like "??x"), return
357 // it.
358 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
359 Ptr += 3;
360 Size += 3;
361 if (C == '\\') goto Slash;
362 return C;
363 }
364 }
365
366 // If this is neither, return a single character.
367 ++Size;
368 return *Ptr;
369}
370
Chris Lattner22eb9722006-06-18 05:43:12 +0000371//===----------------------------------------------------------------------===//
372// Helper methods for lexing.
373//===----------------------------------------------------------------------===//
374
Chris Lattnercb283342006-06-18 06:48:37 +0000375void Lexer::LexIdentifier(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000376 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
377 unsigned Size;
378 unsigned char C = *CurPtr++;
379 while (isIdentifierBody(C)) {
380 C = *CurPtr++;
381 }
382 --CurPtr; // Back up over the skipped character.
383
384 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
385 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000386 // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000387 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
388FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000389 const char *IdStart = BufferPtr;
Chris Lattnerd01e2912006-06-18 16:22:51 +0000390 FormTokenWithChars(Result, CurPtr);
Chris Lattner8c204872006-10-14 05:19:21 +0000391 Result.setKind(tok::identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000392
Chris Lattner0f1f5052006-07-20 04:16:23 +0000393 // If we are in raw mode, return this identifier raw. There is no need to
394 // look up identifier information or attempt to macro expand it.
395 if (LexingRawMode) return;
396
Chris Lattnercefc7682006-07-08 08:28:12 +0000397 // Fill in Result.IdentifierInfo, looking up the identifier in the
398 // identifier table.
399 PP.LookUpIdentifierInfo(Result, IdStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000400
Chris Lattnerc5a00062006-06-18 16:41:01 +0000401 // Finally, now that we know we have an identifier, pass this off to the
402 // preprocessor, which may macro expand it or something.
Chris Lattner22eb9722006-06-18 05:43:12 +0000403 return PP.HandleIdentifier(Result);
404 }
405
406 // Otherwise, $,\,? in identifier found. Enter slower path.
407
408 C = getCharAndSize(CurPtr, Size);
409 while (1) {
410 if (C == '$') {
411 // If we hit a $ and they are not supported in identifiers, we are done.
412 if (!Features.DollarIdents) goto FinishIdentifier;
413
414 // Otherwise, emit a diagnostic and continue.
Chris Lattnercb283342006-06-18 06:48:37 +0000415 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000416 CurPtr = ConsumeChar(CurPtr, Size, Result);
417 C = getCharAndSize(CurPtr, Size);
418 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000419 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000420 // Found end of identifier.
421 goto FinishIdentifier;
422 }
423
424 // Otherwise, this character is good, consume it.
425 CurPtr = ConsumeChar(CurPtr, Size, Result);
426
427 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000428 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000429 CurPtr = ConsumeChar(CurPtr, Size, Result);
430 C = getCharAndSize(CurPtr, Size);
431 }
432 }
433}
434
435
436/// LexNumericConstant - Lex the remainer of a integer or floating point
437/// constant. From[-1] is the first character lexed. Return the end of the
438/// constant.
Chris Lattnercb283342006-06-18 06:48:37 +0000439void Lexer::LexNumericConstant(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000440 unsigned Size;
441 char C = getCharAndSize(CurPtr, Size);
442 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000443 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000444 CurPtr = ConsumeChar(CurPtr, Size, Result);
445 PrevCh = C;
446 C = getCharAndSize(CurPtr, Size);
447 }
448
449 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
450 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
451 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
452
453 // If we have a hex FP constant, continue.
454 if (Features.HexFloats &&
455 (C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p'))
456 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
457
Chris Lattner8c204872006-10-14 05:19:21 +0000458 Result.setKind(tok::numeric_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000459
Chris Lattnerd01e2912006-06-18 16:22:51 +0000460 // Update the location of token as well as BufferPtr.
461 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000462}
463
464/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
465/// either " or L".
Chris Lattnerd3e98952006-10-06 05:22:26 +0000466void Lexer::LexStringLiteral(LexerToken &Result, const char *CurPtr, bool Wide){
Chris Lattner22eb9722006-06-18 05:43:12 +0000467 const char *NulCharacter = 0; // Does this string contain the \0 character?
468
469 char C = getAndAdvanceChar(CurPtr, Result);
470 while (C != '"') {
471 // Skip escaped characters.
472 if (C == '\\') {
473 // Skip the escaped character.
474 C = getAndAdvanceChar(CurPtr, Result);
475 } else if (C == '\n' || C == '\r' || // Newline.
476 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000477 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000478 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000479 FormTokenWithChars(Result, CurPtr-1);
480 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000481 } else if (C == 0) {
482 NulCharacter = CurPtr-1;
483 }
484 C = getAndAdvanceChar(CurPtr, Result);
485 }
486
Chris Lattner5a78a022006-07-20 06:02:19 +0000487 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000488 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000489
Chris Lattner8c204872006-10-14 05:19:21 +0000490 Result.setKind(Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000491
Chris Lattnerd01e2912006-06-18 16:22:51 +0000492 // Update the location of the token as well as the BufferPtr instance var.
493 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000494}
495
496/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
497/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattnercb283342006-06-18 06:48:37 +0000498void Lexer::LexAngledStringLiteral(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000499 const char *NulCharacter = 0; // Does this string contain the \0 character?
500
501 char C = getAndAdvanceChar(CurPtr, Result);
502 while (C != '>') {
503 // Skip escaped characters.
504 if (C == '\\') {
505 // Skip the escaped character.
506 C = getAndAdvanceChar(CurPtr, Result);
507 } else if (C == '\n' || C == '\r' || // Newline.
508 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000509 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000510 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000511 FormTokenWithChars(Result, CurPtr-1);
512 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000513 } else if (C == 0) {
514 NulCharacter = CurPtr-1;
515 }
516 C = getAndAdvanceChar(CurPtr, Result);
517 }
518
Chris Lattner5a78a022006-07-20 06:02:19 +0000519 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000520 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000521
Chris Lattner8c204872006-10-14 05:19:21 +0000522 Result.setKind(tok::angle_string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000523
Chris Lattnerd01e2912006-06-18 16:22:51 +0000524 // Update the location of token as well as BufferPtr.
525 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000526}
527
528
529/// LexCharConstant - Lex the remainder of a character constant, after having
530/// lexed either ' or L'.
Chris Lattnercb283342006-06-18 06:48:37 +0000531void Lexer::LexCharConstant(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000532 const char *NulCharacter = 0; // Does this character contain the \0 character?
533
534 // Handle the common case of 'x' and '\y' efficiently.
535 char C = getAndAdvanceChar(CurPtr, Result);
536 if (C == '\'') {
Chris Lattnera5f4c882006-07-20 06:08:47 +0000537 if (!LexingRawMode) Diag(BufferPtr, diag::err_empty_character);
Chris Lattner8c204872006-10-14 05:19:21 +0000538 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000539 FormTokenWithChars(Result, CurPtr);
540 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000541 } else if (C == '\\') {
542 // Skip the escaped character.
543 // FIXME: UCN's.
544 C = getAndAdvanceChar(CurPtr, Result);
545 }
546
547 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
548 ++CurPtr;
549 } else {
550 // Fall back on generic code for embedded nulls, newlines, wide chars.
551 do {
552 // Skip escaped characters.
553 if (C == '\\') {
554 // Skip the escaped character.
555 C = getAndAdvanceChar(CurPtr, Result);
556 } else if (C == '\n' || C == '\r' || // Newline.
557 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000558 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner8c204872006-10-14 05:19:21 +0000559 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000560 FormTokenWithChars(Result, CurPtr-1);
561 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000562 } else if (C == 0) {
563 NulCharacter = CurPtr-1;
564 }
565 C = getAndAdvanceChar(CurPtr, Result);
566 } while (C != '\'');
567 }
568
Chris Lattnercb283342006-06-18 06:48:37 +0000569 if (NulCharacter) Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000570
Chris Lattner8c204872006-10-14 05:19:21 +0000571 Result.setKind(tok::char_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000572
Chris Lattnerd01e2912006-06-18 16:22:51 +0000573 // Update the location of token as well as BufferPtr.
574 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000575}
576
577/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
578/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattnercb283342006-06-18 06:48:37 +0000579void Lexer::SkipWhitespace(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000580 // Whitespace - Skip it, then return the token after the whitespace.
581 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
582 while (1) {
583 // Skip horizontal whitespace very aggressively.
584 while (isHorizontalWhitespace(Char))
585 Char = *++CurPtr;
586
587 // Otherwise if we something other than whitespace, we're done.
588 if (Char != '\n' && Char != '\r')
589 break;
590
591 if (ParsingPreprocessorDirective) {
592 // End of preprocessor directive line, let LexTokenInternal handle this.
593 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000594 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000595 }
596
597 // ok, but handle newline.
598 // The returned token is at the start of the line.
Chris Lattner8c204872006-10-14 05:19:21 +0000599 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000600 // No leading whitespace seen so far.
Chris Lattner8c204872006-10-14 05:19:21 +0000601 Result.clearFlag(LexerToken::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000602 Char = *++CurPtr;
603 }
604
605 // If this isn't immediately after a newline, there is leading space.
606 char PrevChar = CurPtr[-1];
607 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattner8c204872006-10-14 05:19:21 +0000608 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000609
610 // If the next token is obviously a // or /* */ comment, skip it efficiently
611 // too (without going through the big switch stmt).
Chris Lattner457fc152006-07-29 06:30:25 +0000612 if (Char == '/' && CurPtr[1] == '/' && !KeepCommentMode) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000613 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000614 SkipBCPLComment(Result, CurPtr+1);
615 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000616 }
Chris Lattner457fc152006-07-29 06:30:25 +0000617 if (Char == '/' && CurPtr[1] == '*' && !KeepCommentMode) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000618 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000619 SkipBlockComment(Result, CurPtr+2);
620 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000621 }
622 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000623}
624
625// SkipBCPLComment - We have just read the // characters from input. Skip until
626// we find the newline character thats terminate the comment. Then update
627/// BufferPtr and return.
Chris Lattner457fc152006-07-29 06:30:25 +0000628bool Lexer::SkipBCPLComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000629 // If BCPL comments aren't explicitly enabled for this language, emit an
630 // extension warning.
631 if (!Features.BCPLComment) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000632 Diag(BufferPtr, diag::ext_bcpl_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000633
634 // Mark them enabled so we only emit one warning for this translation
635 // unit.
636 Features.BCPLComment = true;
637 }
638
639 // Scan over the body of the comment. The common case, when scanning, is that
640 // the comment contains normal ascii characters with nothing interesting in
641 // them. As such, optimize for this case with the inner loop.
642 char C;
643 do {
644 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +0000645 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
646 // If we find a \n character, scan backwards, checking to see if it's an
647 // escaped newline, like we do for block comments.
Chris Lattner22eb9722006-06-18 05:43:12 +0000648
649 // Skip over characters in the fast loop.
650 while (C != 0 && // Potentially EOF.
651 C != '\\' && // Potentially escaped newline.
652 C != '?' && // Potentially trigraph.
653 C != '\n' && C != '\r') // Newline or DOS-style newline.
654 C = *++CurPtr;
655
656 // If this is a newline, we're done.
657 if (C == '\n' || C == '\r')
658 break; // Found the newline? Break out!
659
660 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
661 // properly decode the character.
662 const char *OldPtr = CurPtr;
663 C = getAndAdvanceChar(CurPtr, Result);
664
665 // If we read multiple characters, and one of those characters was a \r or
Chris Lattnerff591e22007-06-09 06:07:22 +0000666 // \n, then we had an escaped newline within the comment. Emit diagnostic
667 // unless the next line is also a // comment.
668 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000669 for (; OldPtr != CurPtr; ++OldPtr)
670 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnerff591e22007-06-09 06:07:22 +0000671 // Okay, we found a // comment that ends in a newline, if the next
672 // line is also a // comment, but has spaces, don't emit a diagnostic.
673 if (isspace(C)) {
674 const char *ForwardPtr = CurPtr;
675 while (isspace(*ForwardPtr)) // Skip whitespace.
676 ++ForwardPtr;
677 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
678 break;
679 }
680
Chris Lattnercb283342006-06-18 06:48:37 +0000681 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
682 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000683 }
684 }
685
Chris Lattner457fc152006-07-29 06:30:25 +0000686 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
Chris Lattner22eb9722006-06-18 05:43:12 +0000687 } while (C != '\n' && C != '\r');
688
Chris Lattner457fc152006-07-29 06:30:25 +0000689 // Found but did not consume the newline.
690
691 // If we are returning comments as tokens, return this comment as a token.
692 if (KeepCommentMode)
693 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000694
695 // If we are inside a preprocessor directive and we see the end of line,
696 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +0000697 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000698 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000699 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000700 }
701
702 // Otherwise, eat the \n character. We don't care if this is a \n\r or
703 // \r\n sequence.
704 ++CurPtr;
705
706 // The next returned token is at the start of the line.
Chris Lattner8c204872006-10-14 05:19:21 +0000707 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000708 // No leading whitespace seen so far.
Chris Lattner8c204872006-10-14 05:19:21 +0000709 Result.clearFlag(LexerToken::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000710
711 // It is common for the tokens immediately after a // comment to be
712 // whitespace (indentation for the next line). Instead of going through the
713 // big switch, handle it efficiently now.
714 if (isWhitespace(*CurPtr)) {
Chris Lattner8c204872006-10-14 05:19:21 +0000715 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000716 SkipWhitespace(Result, CurPtr+1);
717 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000718 }
719
720 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000721 return true;
722}
Chris Lattner22eb9722006-06-18 05:43:12 +0000723
Chris Lattner457fc152006-07-29 06:30:25 +0000724/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
725/// an appropriate way and return it.
726bool Lexer::SaveBCPLComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner8c204872006-10-14 05:19:21 +0000727 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +0000728 FormTokenWithChars(Result, CurPtr);
729
730 // If this BCPL-style comment is in a macro definition, transmogrify it into
731 // a C-style block comment.
732 if (ParsingPreprocessorDirective) {
733 std::string Spelling = PP.getSpelling(Result);
734 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
735 Spelling[1] = '*'; // Change prefix to "/*".
736 Spelling += "*/"; // add suffix.
737
Chris Lattner8c204872006-10-14 05:19:21 +0000738 Result.setLocation(PP.CreateString(&Spelling[0], Spelling.size(),
Chris Lattner457fc152006-07-29 06:30:25 +0000739 Result.getLocation()));
Chris Lattner8c204872006-10-14 05:19:21 +0000740 Result.setLength(Spelling.size());
Chris Lattner457fc152006-07-29 06:30:25 +0000741 }
742 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000743}
744
Chris Lattnercb283342006-06-18 06:48:37 +0000745/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
746/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner22eb9722006-06-18 05:43:12 +0000747/// diagnostic if so. We know that the is inside of a block comment.
Chris Lattner1f583052006-06-18 06:53:56 +0000748static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
749 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000750 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Chris Lattner22eb9722006-06-18 05:43:12 +0000751
752 // Back up off the newline.
753 --CurPtr;
754
755 // If this is a two-character newline sequence, skip the other character.
756 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
757 // \n\n or \r\r -> not escaped newline.
758 if (CurPtr[0] == CurPtr[1])
759 return false;
760 // \n\r or \r\n -> skip the newline.
761 --CurPtr;
762 }
763
764 // If we have horizontal whitespace, skip over it. We allow whitespace
765 // between the slash and newline.
766 bool HasSpace = false;
767 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
768 --CurPtr;
769 HasSpace = true;
770 }
771
772 // If we have a slash, we know this is an escaped newline.
773 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +0000774 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000775 } else {
776 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +0000777 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
778 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +0000779 return false;
Chris Lattnercb283342006-06-18 06:48:37 +0000780
781 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +0000782 CurPtr -= 2;
783
784 // If no trigraphs are enabled, warn that we ignored this trigraph and
785 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +0000786 if (!L->getFeatures().Trigraphs) {
787 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000788 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000789 }
Chris Lattner1f583052006-06-18 06:53:56 +0000790 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000791 }
792
793 // Warn about having an escaped newline between the */ characters.
Chris Lattner1f583052006-06-18 06:53:56 +0000794 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Chris Lattner22eb9722006-06-18 05:43:12 +0000795
796 // If there was space between the backslash and newline, warn about it.
Chris Lattner1f583052006-06-18 06:53:56 +0000797 if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space);
Chris Lattner22eb9722006-06-18 05:43:12 +0000798
Chris Lattnercb283342006-06-18 06:48:37 +0000799 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000800}
801
Chris Lattneraded4a92006-10-27 04:42:31 +0000802#ifdef __SSE2__
803#include <emmintrin.h>
Chris Lattner9f6604f2006-10-30 20:01:22 +0000804#elif __ALTIVEC__
805#include <altivec.h>
806#undef bool
Chris Lattneraded4a92006-10-27 04:42:31 +0000807#endif
808
Chris Lattner22eb9722006-06-18 05:43:12 +0000809/// SkipBlockComment - We have just read the /* characters from input. Read
810/// until we find the */ characters that terminate the comment. Note that we
811/// don't bother decoding trigraphs or escaped newlines in block comments,
812/// because they cannot cause the comment to end. The only thing that can
813/// happen is the comment could end with an escaped newline between the */ end
814/// of comment.
Chris Lattner457fc152006-07-29 06:30:25 +0000815bool Lexer::SkipBlockComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000816 // Scan one character past where we should, looking for a '/' character. Once
817 // we find it, check to see if it was preceeded by a *. This common
818 // optimization helps people who like to put a lot of * characters in their
819 // comments.
820 unsigned char C = *CurPtr++;
821 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000822 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000823 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +0000824 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000825 }
826
827 while (1) {
Chris Lattner6cc3e362006-10-27 04:12:35 +0000828 // Skip over all non-interesting characters until we find end of buffer or a
829 // (probably ending) '/' character.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000830 if (CurPtr + 24 < BufferEnd) {
831 // While not aligned to a 16-byte boundary.
832 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
833 C = *CurPtr++;
834
835 if (C == '/') goto FoundSlash;
Chris Lattneraded4a92006-10-27 04:42:31 +0000836
837#ifdef __SSE2__
838 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
839 '/', '/', '/', '/', '/', '/', '/', '/');
840 while (CurPtr+16 <= BufferEnd &&
841 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
842 CurPtr += 16;
Chris Lattner9f6604f2006-10-30 20:01:22 +0000843#elif __ALTIVEC__
844 __vector unsigned char Slashes = {
845 '/', '/', '/', '/', '/', '/', '/', '/',
846 '/', '/', '/', '/', '/', '/', '/', '/'
847 };
848 while (CurPtr+16 <= BufferEnd &&
849 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
850 CurPtr += 16;
851#else
Chris Lattneraded4a92006-10-27 04:42:31 +0000852 // Scan for '/' quickly. Many block comments are very large.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000853 while (CurPtr[0] != '/' &&
854 CurPtr[1] != '/' &&
855 CurPtr[2] != '/' &&
856 CurPtr[3] != '/' &&
857 CurPtr+4 < BufferEnd) {
858 CurPtr += 4;
859 }
Chris Lattneraded4a92006-10-27 04:42:31 +0000860#endif
861
862 // It has to be one of the bytes scanned, increment to it and read one.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000863 C = *CurPtr++;
864 }
865
Chris Lattneraded4a92006-10-27 04:42:31 +0000866 // Loop to scan the remainder.
Chris Lattner22eb9722006-06-18 05:43:12 +0000867 while (C != '/' && C != '\0')
868 C = *CurPtr++;
869
Chris Lattner6cc3e362006-10-27 04:12:35 +0000870 FoundSlash:
Chris Lattner22eb9722006-06-18 05:43:12 +0000871 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000872 if (CurPtr[-2] == '*') // We found the final */. We're done!
873 break;
874
875 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +0000876 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000877 // We found the final */, though it had an escaped newline between the
878 // * and /. We're done!
879 break;
880 }
881 }
882 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
883 // If this is a /* inside of the comment, emit a warning. Don't do this
884 // if this is a /*/, which will end the comment. This misses cases with
885 // embedded escaped newlines, but oh well.
Chris Lattnercb283342006-06-18 06:48:37 +0000886 Diag(CurPtr-1, diag::nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000887 }
888 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000889 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000890 // Note: the user probably forgot a */. We could continue immediately
891 // after the /*, but this would involve lexing a lot of what really is the
892 // comment, which surely would confuse the parser.
893 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +0000894 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000895 }
896 C = *CurPtr++;
897 }
Chris Lattner457fc152006-07-29 06:30:25 +0000898
899 // If we are returning comments as tokens, return this comment as a token.
900 if (KeepCommentMode) {
Chris Lattner8c204872006-10-14 05:19:21 +0000901 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +0000902 FormTokenWithChars(Result, CurPtr);
903 return false;
904 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000905
906 // It is common for the tokens immediately after a /**/ comment to be
907 // whitespace. Instead of going through the big switch, handle it
908 // efficiently now.
909 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner8c204872006-10-14 05:19:21 +0000910 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000911 SkipWhitespace(Result, CurPtr+1);
912 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000913 }
914
915 // Otherwise, just return so that the next character will be lexed as a token.
916 BufferPtr = CurPtr;
Chris Lattner8c204872006-10-14 05:19:21 +0000917 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000918 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000919}
920
921//===----------------------------------------------------------------------===//
922// Primary Lexing Entry Points
923//===----------------------------------------------------------------------===//
924
925/// LexIncludeFilename - After the preprocessor has parsed a #include, lex and
926/// (potentially) macro expand the filename.
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000927void Lexer::LexIncludeFilename(LexerToken &FilenameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000928 assert(ParsingPreprocessorDirective &&
929 ParsingFilename == false &&
930 "Must be in a preprocessing directive!");
931
932 // We are now parsing a filename!
933 ParsingFilename = true;
934
Chris Lattner269c2322006-06-25 06:23:00 +0000935 // Lex the filename.
936 Lex(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000937
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000938 // We should have obtained the filename now.
Chris Lattner22eb9722006-06-18 05:43:12 +0000939 ParsingFilename = false;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000940
Chris Lattner22eb9722006-06-18 05:43:12 +0000941 // No filename?
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000942 if (FilenameTok.getKind() == tok::eom)
Chris Lattner538d7f32006-07-20 04:31:52 +0000943 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner22eb9722006-06-18 05:43:12 +0000944}
945
946/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
947/// uninterpreted string. This switches the lexer out of directive mode.
948std::string Lexer::ReadToEndOfLine() {
949 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
950 "Must be in a preprocessing directive!");
951 std::string Result;
952 LexerToken Tmp;
953
954 // CurPtr - Cache BufferPtr in an automatic variable.
955 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000956 while (1) {
957 char Char = getAndAdvanceChar(CurPtr, Tmp);
958 switch (Char) {
959 default:
960 Result += Char;
961 break;
962 case 0: // Null.
963 // Found end of file?
964 if (CurPtr-1 != BufferEnd) {
965 // Nope, normal character, continue.
966 Result += Char;
967 break;
968 }
969 // FALL THROUGH.
970 case '\r':
971 case '\n':
972 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
973 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
974 BufferPtr = CurPtr-1;
975
976 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +0000977 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000978 assert(Tmp.getKind() == tok::eom && "Unexpected token!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000979
980 // Finally, we're done, return the string we found.
981 return Result;
982 }
983 }
984}
985
986/// LexEndOfFile - CurPtr points to the end of this file. Handle this
987/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +0000988/// This returns true if Result contains a token, false if PP.Lex should be
989/// called again.
990bool Lexer::LexEndOfFile(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000991 // If we hit the end of the file while parsing a preprocessor directive,
992 // end the preprocessor directive first. The next token returned will
993 // then be the end of file.
994 if (ParsingPreprocessorDirective) {
995 // Done parsing the "line".
996 ParsingPreprocessorDirective = false;
Chris Lattner8c204872006-10-14 05:19:21 +0000997 Result.setKind(tok::eom);
Chris Lattnerd01e2912006-06-18 16:22:51 +0000998 // Update the location of token as well as BufferPtr.
999 FormTokenWithChars(Result, CurPtr);
Chris Lattner457fc152006-07-29 06:30:25 +00001000
1001 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001002 KeepCommentMode = PP.getCommentRetentionState();
Chris Lattner2183a6e2006-07-18 06:36:12 +00001003 return true; // Have a token.
Chris Lattner22eb9722006-06-18 05:43:12 +00001004 }
1005
Chris Lattner30a2fa12006-07-19 06:31:49 +00001006 // If we are in raw mode, return this event as an EOF token. Let the caller
1007 // that put us in raw mode handle the event.
1008 if (LexingRawMode) {
Chris Lattner8c204872006-10-14 05:19:21 +00001009 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +00001010 BufferPtr = BufferEnd;
1011 FormTokenWithChars(Result, BufferEnd);
Chris Lattner8c204872006-10-14 05:19:21 +00001012 Result.setKind(tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001013 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001014 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001015
Chris Lattner30a2fa12006-07-19 06:31:49 +00001016 // Otherwise, issue diagnostics for unterminated #if and missing newline.
1017
1018 // If we are in a #if directive, emit an error.
1019 while (!ConditionalStack.empty()) {
Chris Lattner538d7f32006-07-20 04:31:52 +00001020 Diag(ConditionalStack.back().IfLoc, diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001021 ConditionalStack.pop_back();
1022 }
1023
1024 // If the file was empty or didn't end in a newline, issue a pedwarn.
1025 if (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1026 Diag(BufferEnd, diag::ext_no_newline_eof);
1027
Chris Lattner22eb9722006-06-18 05:43:12 +00001028 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +00001029
1030 // Finally, let the preprocessor handle this.
Chris Lattner2183a6e2006-07-18 06:36:12 +00001031 return PP.HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001032}
1033
Chris Lattner678c8802006-07-11 05:46:12 +00001034/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1035/// the specified lexer will return a tok::l_paren token, 0 if it is something
1036/// else and 2 if there are no more tokens in the buffer controlled by the
1037/// lexer.
1038unsigned Lexer::isNextPPTokenLParen() {
1039 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1040
1041 // Switch to 'skipping' mode. This will ensure that we can lex a token
1042 // without emitting diagnostics, disables macro expansion, and will cause EOF
1043 // to return an EOF token instead of popping the include stack.
1044 LexingRawMode = true;
1045
1046 // Save state that can be changed while lexing so that we can restore it.
1047 const char *TmpBufferPtr = BufferPtr;
1048
1049 LexerToken Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001050 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001051 LexTokenInternal(Tok);
1052
1053 // Restore state that may have changed.
1054 BufferPtr = TmpBufferPtr;
1055
1056 // Restore the lexer back to non-skipping mode.
1057 LexingRawMode = false;
1058
1059 if (Tok.getKind() == tok::eof)
1060 return 2;
1061 return Tok.getKind() == tok::l_paren;
1062}
1063
Chris Lattner22eb9722006-06-18 05:43:12 +00001064
1065/// LexTokenInternal - This implements a simple C family lexer. It is an
1066/// extremely performance critical piece of code. This assumes that the buffer
1067/// has a null character at the end of the file. Return true if an error
1068/// occurred and compilation should terminate, false if normal. This returns a
1069/// preprocessing token, not a normal token, as such, it is an internal
1070/// interface. It assumes that the Flags of result have been cleared before
1071/// calling this.
Chris Lattnercb283342006-06-18 06:48:37 +00001072void Lexer::LexTokenInternal(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001073LexNextToken:
1074 // New token, can't need cleaning yet.
Chris Lattner8c204872006-10-14 05:19:21 +00001075 Result.clearFlag(LexerToken::NeedsCleaning);
1076 Result.setIdentifierInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001077
1078 // CurPtr - Cache BufferPtr in an automatic variable.
1079 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001080
Chris Lattnereb54b592006-07-10 06:34:27 +00001081 // Small amounts of horizontal whitespace is very common between tokens.
1082 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1083 ++CurPtr;
1084 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1085 ++CurPtr;
1086 BufferPtr = CurPtr;
Chris Lattner8c204872006-10-14 05:19:21 +00001087 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001088 }
1089
Chris Lattner22eb9722006-06-18 05:43:12 +00001090 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1091
1092 // Read a character, advancing over it.
1093 char Char = getAndAdvanceChar(CurPtr, Result);
1094 switch (Char) {
1095 case 0: // Null.
1096 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001097 if (CurPtr-1 == BufferEnd) {
1098 // Read the PP instance variable into an automatic variable, because
1099 // LexEndOfFile will often delete 'this'.
1100 Preprocessor &PPCache = PP;
1101 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1102 return; // Got a token to return.
1103 return PPCache.Lex(Result);
1104 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001105
Chris Lattnercb283342006-06-18 06:48:37 +00001106 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner8c204872006-10-14 05:19:21 +00001107 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001108 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001109 goto LexNextToken; // GCC isn't tail call eliminating.
1110 case '\n':
1111 case '\r':
1112 // If we are inside a preprocessor directive and we see the end of line,
1113 // we know we are done with the directive, so return an EOM token.
1114 if (ParsingPreprocessorDirective) {
1115 // Done parsing the "line".
1116 ParsingPreprocessorDirective = false;
1117
Chris Lattner457fc152006-07-29 06:30:25 +00001118 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001119 KeepCommentMode = PP.getCommentRetentionState();
Chris Lattner457fc152006-07-29 06:30:25 +00001120
Chris Lattner22eb9722006-06-18 05:43:12 +00001121 // Since we consumed a newline, we are back at the start of a line.
1122 IsAtStartOfLine = true;
1123
Chris Lattner8c204872006-10-14 05:19:21 +00001124 Result.setKind(tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001125 break;
1126 }
1127 // The returned token is at the start of the line.
Chris Lattner8c204872006-10-14 05:19:21 +00001128 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001129 // No leading whitespace seen so far.
Chris Lattner8c204872006-10-14 05:19:21 +00001130 Result.clearFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001131 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001132 goto LexNextToken; // GCC isn't tail call eliminating.
1133 case ' ':
1134 case '\t':
1135 case '\f':
1136 case '\v':
Chris Lattner8c204872006-10-14 05:19:21 +00001137 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001138 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001139 goto LexNextToken; // GCC isn't tail call eliminating.
1140
1141 case 'L':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001142 // Notify MIOpt that we read a non-whitespace/non-comment token.
1143 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001144 Char = getCharAndSize(CurPtr, SizeTmp);
1145
1146 // Wide string literal.
1147 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001148 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1149 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001150
1151 // Wide character constant.
1152 if (Char == '\'')
1153 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1154 // FALL THROUGH, treating L like the start of an identifier.
1155
1156 // C99 6.4.2: Identifiers.
1157 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1158 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1159 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1160 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1161 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1162 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1163 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1164 case 'v': case 'w': case 'x': case 'y': case 'z':
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 Lattner22eb9722006-06-18 05:43:12 +00001168 return LexIdentifier(Result, CurPtr);
1169
1170 // C99 6.4.4.1: Integer Constants.
1171 // C99 6.4.4.2: Floating Constants.
1172 case '0': case '1': case '2': case '3': case '4':
1173 case '5': case '6': case '7': case '8': case '9':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001174 // Notify MIOpt that we read a non-whitespace/non-comment token.
1175 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001176 return LexNumericConstant(Result, CurPtr);
1177
1178 // C99 6.4.4: Character Constants.
1179 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001180 // Notify MIOpt that we read a non-whitespace/non-comment token.
1181 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001182 return LexCharConstant(Result, CurPtr);
1183
1184 // C99 6.4.5: String Literals.
1185 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001186 // Notify MIOpt that we read a non-whitespace/non-comment token.
1187 MIOpt.ReadToken();
Chris Lattnerd3e98952006-10-06 05:22:26 +00001188 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001189
1190 // C99 6.4.6: Punctuators.
1191 case '?':
Chris Lattner8c204872006-10-14 05:19:21 +00001192 Result.setKind(tok::question);
Chris Lattner22eb9722006-06-18 05:43:12 +00001193 break;
1194 case '[':
Chris Lattner8c204872006-10-14 05:19:21 +00001195 Result.setKind(tok::l_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001196 break;
1197 case ']':
Chris Lattner8c204872006-10-14 05:19:21 +00001198 Result.setKind(tok::r_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001199 break;
1200 case '(':
Chris Lattner8c204872006-10-14 05:19:21 +00001201 Result.setKind(tok::l_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001202 break;
1203 case ')':
Chris Lattner8c204872006-10-14 05:19:21 +00001204 Result.setKind(tok::r_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001205 break;
1206 case '{':
Chris Lattner8c204872006-10-14 05:19:21 +00001207 Result.setKind(tok::l_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001208 break;
1209 case '}':
Chris Lattner8c204872006-10-14 05:19:21 +00001210 Result.setKind(tok::r_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001211 break;
1212 case '.':
1213 Char = getCharAndSize(CurPtr, SizeTmp);
1214 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001215 // Notify MIOpt that we read a non-whitespace/non-comment token.
1216 MIOpt.ReadToken();
1217
Chris Lattner22eb9722006-06-18 05:43:12 +00001218 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1219 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001220 Result.setKind(tok::periodstar);
Chris Lattner22eb9722006-06-18 05:43:12 +00001221 CurPtr += SizeTmp;
1222 } else if (Char == '.' &&
1223 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner8c204872006-10-14 05:19:21 +00001224 Result.setKind(tok::ellipsis);
Chris Lattner22eb9722006-06-18 05:43:12 +00001225 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1226 SizeTmp2, Result);
1227 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001228 Result.setKind(tok::period);
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::ampamp);
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::ampequal);
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::amp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001241 }
1242 break;
1243 case '*':
1244 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001245 Result.setKind(tok::starequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001246 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1247 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001248 Result.setKind(tok::star);
Chris Lattner22eb9722006-06-18 05:43:12 +00001249 }
1250 break;
1251 case '+':
1252 Char = getCharAndSize(CurPtr, SizeTmp);
1253 if (Char == '+') {
Chris Lattner8c204872006-10-14 05:19:21 +00001254 Result.setKind(tok::plusplus);
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::plusequal);
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::plus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001261 }
1262 break;
1263 case '-':
1264 Char = getCharAndSize(CurPtr, SizeTmp);
1265 if (Char == '-') {
Chris Lattner8c204872006-10-14 05:19:21 +00001266 Result.setKind(tok::minusminus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001267 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1268 } else if (Char == '>' && Features.CPlusPlus &&
1269 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001270 Result.setKind(tok::arrowstar); // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00001271 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1272 SizeTmp2, Result);
1273 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001274 Result.setKind(tok::arrow);
Chris Lattner22eb9722006-06-18 05:43:12 +00001275 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1276 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001277 Result.setKind(tok::minusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001278 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1279 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001280 Result.setKind(tok::minus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001281 }
1282 break;
1283 case '~':
Chris Lattner8c204872006-10-14 05:19:21 +00001284 Result.setKind(tok::tilde);
Chris Lattner22eb9722006-06-18 05:43:12 +00001285 break;
1286 case '!':
1287 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001288 Result.setKind(tok::exclaimequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001289 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1290 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001291 Result.setKind(tok::exclaim);
Chris Lattner22eb9722006-06-18 05:43:12 +00001292 }
1293 break;
1294 case '/':
1295 // 6.4.9: Comments
1296 Char = getCharAndSize(CurPtr, SizeTmp);
1297 if (Char == '/') { // BCPL comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001298 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1299 goto LexNextToken; // GCC isn't tail call eliminating.
1300 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001301 } else if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001302 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1303 goto LexNextToken; // GCC isn't tail call eliminating.
1304 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001305 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001306 Result.setKind(tok::slashequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001307 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1308 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001309 Result.setKind(tok::slash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001310 }
1311 break;
1312 case '%':
1313 Char = getCharAndSize(CurPtr, SizeTmp);
1314 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001315 Result.setKind(tok::percentequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001316 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1317 } else if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001318 Result.setKind(tok::r_brace); // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00001319 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1320 } else if (Features.Digraphs && Char == ':') {
1321 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001322 Char = getCharAndSize(CurPtr, SizeTmp);
1323 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001324 Result.setKind(tok::hashhash); // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00001325 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1326 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001327 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001328 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001329 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1330 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner22eb9722006-06-18 05:43:12 +00001331 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001332 Result.setKind(tok::hash); // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00001333
1334 // We parsed a # character. If this occurs at the start of the line,
1335 // it's actually the start of a preprocessing directive. Callback to
1336 // the preprocessor to handle it.
1337 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001338 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001339 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001340 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001341
1342 // As an optimization, if the preprocessor didn't switch lexers, tail
1343 // recurse.
1344 if (PP.isCurrentLexer(this)) {
1345 // Start a new token. If this is a #include or something, the PP may
1346 // want us starting at the beginning of the line again. If so, set
1347 // the StartOfLine flag.
1348 if (IsAtStartOfLine) {
Chris Lattner8c204872006-10-14 05:19:21 +00001349 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001350 IsAtStartOfLine = false;
1351 }
1352 goto LexNextToken; // GCC isn't tail call eliminating.
1353 }
1354
1355 return PP.Lex(Result);
1356 }
1357 }
1358 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001359 Result.setKind(tok::percent);
Chris Lattner22eb9722006-06-18 05:43:12 +00001360 }
1361 break;
1362 case '<':
1363 Char = getCharAndSize(CurPtr, SizeTmp);
1364 if (ParsingFilename) {
1365 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1366 } else if (Char == '<' &&
1367 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001368 Result.setKind(tok::lesslessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001369 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1370 SizeTmp2, Result);
1371 } else if (Char == '<') {
Chris Lattner8c204872006-10-14 05:19:21 +00001372 Result.setKind(tok::lessless);
Chris Lattner22eb9722006-06-18 05:43:12 +00001373 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1374 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001375 Result.setKind(tok::lessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001376 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1377 } else if (Features.Digraphs && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001378 Result.setKind(tok::l_square); // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00001379 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1380 } else if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001381 Result.setKind(tok::l_brace); // '<%' -> '{'
Chris Lattner22eb9722006-06-18 05:43:12 +00001382 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001383 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001384 Result.setKind(tok::less);
Chris Lattner22eb9722006-06-18 05:43:12 +00001385 }
1386 break;
1387 case '>':
1388 Char = getCharAndSize(CurPtr, SizeTmp);
1389 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001390 Result.setKind(tok::greaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001391 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1392 } else if (Char == '>' &&
1393 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001394 Result.setKind(tok::greatergreaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001395 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1396 SizeTmp2, Result);
1397 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001398 Result.setKind(tok::greatergreater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001399 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001400 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001401 Result.setKind(tok::greater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001402 }
1403 break;
1404 case '^':
1405 Char = getCharAndSize(CurPtr, SizeTmp);
1406 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001407 Result.setKind(tok::caretequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001408 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1409 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001410 Result.setKind(tok::caret);
Chris Lattner22eb9722006-06-18 05:43:12 +00001411 }
1412 break;
1413 case '|':
1414 Char = getCharAndSize(CurPtr, SizeTmp);
1415 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001416 Result.setKind(tok::pipeequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001417 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1418 } else if (Char == '|') {
Chris Lattner8c204872006-10-14 05:19:21 +00001419 Result.setKind(tok::pipepipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001420 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1421 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001422 Result.setKind(tok::pipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001423 }
1424 break;
1425 case ':':
1426 Char = getCharAndSize(CurPtr, SizeTmp);
1427 if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001428 Result.setKind(tok::r_square); // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00001429 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1430 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001431 Result.setKind(tok::coloncolon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001432 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1433 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001434 Result.setKind(tok::colon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001435 }
1436 break;
1437 case ';':
Chris Lattner8c204872006-10-14 05:19:21 +00001438 Result.setKind(tok::semi);
Chris Lattner22eb9722006-06-18 05:43:12 +00001439 break;
1440 case '=':
1441 Char = getCharAndSize(CurPtr, SizeTmp);
1442 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001443 Result.setKind(tok::equalequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001444 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1445 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001446 Result.setKind(tok::equal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001447 }
1448 break;
1449 case ',':
Chris Lattner8c204872006-10-14 05:19:21 +00001450 Result.setKind(tok::comma);
Chris Lattner22eb9722006-06-18 05:43:12 +00001451 break;
1452 case '#':
1453 Char = getCharAndSize(CurPtr, SizeTmp);
1454 if (Char == '#') {
Chris Lattner8c204872006-10-14 05:19:21 +00001455 Result.setKind(tok::hashhash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001456 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001457 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001458 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001459 Diag(BufferPtr, diag::charize_microsoft_ext);
1460 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001461 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001462 Result.setKind(tok::hash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001463 // We parsed a # character. If this occurs at the start of the line,
1464 // it's actually the start of a preprocessing directive. Callback to
1465 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00001466 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001467 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001468 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001469 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001470
1471 // As an optimization, if the preprocessor didn't switch lexers, tail
1472 // recurse.
1473 if (PP.isCurrentLexer(this)) {
1474 // Start a new token. If this is a #include or something, the PP may
1475 // want us starting at the beginning of the line again. If so, set
1476 // the StartOfLine flag.
1477 if (IsAtStartOfLine) {
Chris Lattner8c204872006-10-14 05:19:21 +00001478 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001479 IsAtStartOfLine = false;
1480 }
1481 goto LexNextToken; // GCC isn't tail call eliminating.
1482 }
1483 return PP.Lex(Result);
1484 }
1485 }
1486 break;
1487
1488 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00001489 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00001490 // FALL THROUGH.
1491 default:
1492 // Objective C support.
1493 if (CurPtr[-1] == '@' && Features.ObjC1) {
Chris Lattner8c204872006-10-14 05:19:21 +00001494 Result.setKind(tok::at);
Chris Lattner22eb9722006-06-18 05:43:12 +00001495 break;
1496 } else if (CurPtr[-1] == '$' && Features.DollarIdents) {// $ in identifiers.
Chris Lattnercb283342006-06-18 06:48:37 +00001497 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001498 // Notify MIOpt that we read a non-whitespace/non-comment token.
1499 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001500 return LexIdentifier(Result, CurPtr);
1501 }
1502
Chris Lattner8c204872006-10-14 05:19:21 +00001503 Result.setKind(tok::unknown);
Chris Lattner041bef82006-07-11 05:52:53 +00001504 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001505 }
1506
Chris Lattner371ac8a2006-07-04 07:11:10 +00001507 // Notify MIOpt that we read a non-whitespace/non-comment token.
1508 MIOpt.ReadToken();
1509
Chris Lattnerd01e2912006-06-18 16:22:51 +00001510 // Update the location of token as well as BufferPtr.
1511 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001512}