blob: bdf8387b976e8ecf5ae09e7408caea366a49c7f6 [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//
Chris Lattner146762e2007-07-20 16:59:19 +000010// This file implements the Lexer and Token interfaces.
Chris Lattner22eb9722006-06-18 05:43:12 +000011//
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,
Chris Lattner146762e2007-07-20 16:59:19 +0000251 Token *Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000252 // 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 Lattner146762e2007-07-20 16:59:19 +0000267 if (Tok) Tok->setFlag(Token::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 Lattner146762e2007-07-20 16:59:19 +0000297 if (Tok) Tok->setFlag(Token::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 Lattner146762e2007-07-20 16:59:19 +0000375void Lexer::LexIdentifier(Token &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 Lattner146762e2007-07-20 16:59:19 +0000439void Lexer::LexNumericConstant(Token &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 Lattner146762e2007-07-20 16:59:19 +0000466void Lexer::LexStringLiteral(Token &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 Lattner146762e2007-07-20 16:59:19 +0000498void Lexer::LexAngledStringLiteral(Token &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 Lattner146762e2007-07-20 16:59:19 +0000531void Lexer::LexCharConstant(Token &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 Lattner146762e2007-07-20 16:59:19 +0000579void Lexer::SkipWhitespace(Token &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 Lattner146762e2007-07-20 16:59:19 +0000599 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000600 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +0000601 Result.clearFlag(Token::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 Lattner146762e2007-07-20 16:59:19 +0000608 Result.setFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000609
Chris Lattner22eb9722006-06-18 05:43:12 +0000610 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000611}
612
613// SkipBCPLComment - We have just read the // characters from input. Skip until
614// we find the newline character thats terminate the comment. Then update
615/// BufferPtr and return.
Chris Lattner146762e2007-07-20 16:59:19 +0000616bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000617 // If BCPL comments aren't explicitly enabled for this language, emit an
618 // extension warning.
619 if (!Features.BCPLComment) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000620 Diag(BufferPtr, diag::ext_bcpl_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000621
622 // Mark them enabled so we only emit one warning for this translation
623 // unit.
624 Features.BCPLComment = true;
625 }
626
627 // Scan over the body of the comment. The common case, when scanning, is that
628 // the comment contains normal ascii characters with nothing interesting in
629 // them. As such, optimize for this case with the inner loop.
630 char C;
631 do {
632 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +0000633 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
634 // If we find a \n character, scan backwards, checking to see if it's an
635 // escaped newline, like we do for block comments.
Chris Lattner22eb9722006-06-18 05:43:12 +0000636
637 // Skip over characters in the fast loop.
638 while (C != 0 && // Potentially EOF.
639 C != '\\' && // Potentially escaped newline.
640 C != '?' && // Potentially trigraph.
641 C != '\n' && C != '\r') // Newline or DOS-style newline.
642 C = *++CurPtr;
643
644 // If this is a newline, we're done.
645 if (C == '\n' || C == '\r')
646 break; // Found the newline? Break out!
647
648 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
649 // properly decode the character.
650 const char *OldPtr = CurPtr;
651 C = getAndAdvanceChar(CurPtr, Result);
652
653 // If we read multiple characters, and one of those characters was a \r or
Chris Lattnerff591e22007-06-09 06:07:22 +0000654 // \n, then we had an escaped newline within the comment. Emit diagnostic
655 // unless the next line is also a // comment.
656 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000657 for (; OldPtr != CurPtr; ++OldPtr)
658 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnerff591e22007-06-09 06:07:22 +0000659 // Okay, we found a // comment that ends in a newline, if the next
660 // line is also a // comment, but has spaces, don't emit a diagnostic.
661 if (isspace(C)) {
662 const char *ForwardPtr = CurPtr;
663 while (isspace(*ForwardPtr)) // Skip whitespace.
664 ++ForwardPtr;
665 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
666 break;
667 }
668
Chris Lattnercb283342006-06-18 06:48:37 +0000669 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
670 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000671 }
672 }
673
Chris Lattner457fc152006-07-29 06:30:25 +0000674 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
Chris Lattner22eb9722006-06-18 05:43:12 +0000675 } while (C != '\n' && C != '\r');
676
Chris Lattner457fc152006-07-29 06:30:25 +0000677 // Found but did not consume the newline.
678
679 // If we are returning comments as tokens, return this comment as a token.
680 if (KeepCommentMode)
681 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000682
683 // If we are inside a preprocessor directive and we see the end of line,
684 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +0000685 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000686 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000687 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000688 }
689
690 // Otherwise, eat the \n character. We don't care if this is a \n\r or
691 // \r\n sequence.
692 ++CurPtr;
693
694 // The next returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +0000695 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000696 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +0000697 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000698 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000699 return true;
700}
Chris Lattner22eb9722006-06-18 05:43:12 +0000701
Chris Lattner457fc152006-07-29 06:30:25 +0000702/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
703/// an appropriate way and return it.
Chris Lattner146762e2007-07-20 16:59:19 +0000704bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner8c204872006-10-14 05:19:21 +0000705 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +0000706 FormTokenWithChars(Result, CurPtr);
707
708 // If this BCPL-style comment is in a macro definition, transmogrify it into
709 // a C-style block comment.
710 if (ParsingPreprocessorDirective) {
711 std::string Spelling = PP.getSpelling(Result);
712 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
713 Spelling[1] = '*'; // Change prefix to "/*".
714 Spelling += "*/"; // add suffix.
715
Chris Lattner8c204872006-10-14 05:19:21 +0000716 Result.setLocation(PP.CreateString(&Spelling[0], Spelling.size(),
Chris Lattner457fc152006-07-29 06:30:25 +0000717 Result.getLocation()));
Chris Lattner8c204872006-10-14 05:19:21 +0000718 Result.setLength(Spelling.size());
Chris Lattner457fc152006-07-29 06:30:25 +0000719 }
720 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000721}
722
Chris Lattnercb283342006-06-18 06:48:37 +0000723/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
724/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner22eb9722006-06-18 05:43:12 +0000725/// diagnostic if so. We know that the is inside of a block comment.
Chris Lattner1f583052006-06-18 06:53:56 +0000726static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
727 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000728 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Chris Lattner22eb9722006-06-18 05:43:12 +0000729
730 // Back up off the newline.
731 --CurPtr;
732
733 // If this is a two-character newline sequence, skip the other character.
734 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
735 // \n\n or \r\r -> not escaped newline.
736 if (CurPtr[0] == CurPtr[1])
737 return false;
738 // \n\r or \r\n -> skip the newline.
739 --CurPtr;
740 }
741
742 // If we have horizontal whitespace, skip over it. We allow whitespace
743 // between the slash and newline.
744 bool HasSpace = false;
745 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
746 --CurPtr;
747 HasSpace = true;
748 }
749
750 // If we have a slash, we know this is an escaped newline.
751 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +0000752 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000753 } else {
754 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +0000755 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
756 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +0000757 return false;
Chris Lattnercb283342006-06-18 06:48:37 +0000758
759 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +0000760 CurPtr -= 2;
761
762 // If no trigraphs are enabled, warn that we ignored this trigraph and
763 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +0000764 if (!L->getFeatures().Trigraphs) {
765 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000766 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000767 }
Chris Lattner1f583052006-06-18 06:53:56 +0000768 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000769 }
770
771 // Warn about having an escaped newline between the */ characters.
Chris Lattner1f583052006-06-18 06:53:56 +0000772 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Chris Lattner22eb9722006-06-18 05:43:12 +0000773
774 // If there was space between the backslash and newline, warn about it.
Chris Lattner1f583052006-06-18 06:53:56 +0000775 if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space);
Chris Lattner22eb9722006-06-18 05:43:12 +0000776
Chris Lattnercb283342006-06-18 06:48:37 +0000777 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000778}
779
Chris Lattneraded4a92006-10-27 04:42:31 +0000780#ifdef __SSE2__
781#include <emmintrin.h>
Chris Lattner9f6604f2006-10-30 20:01:22 +0000782#elif __ALTIVEC__
783#include <altivec.h>
784#undef bool
Chris Lattneraded4a92006-10-27 04:42:31 +0000785#endif
786
Chris Lattner22eb9722006-06-18 05:43:12 +0000787/// SkipBlockComment - We have just read the /* characters from input. Read
788/// until we find the */ characters that terminate the comment. Note that we
789/// don't bother decoding trigraphs or escaped newlines in block comments,
790/// because they cannot cause the comment to end. The only thing that can
791/// happen is the comment could end with an escaped newline between the */ end
792/// of comment.
Chris Lattner146762e2007-07-20 16:59:19 +0000793bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000794 // Scan one character past where we should, looking for a '/' character. Once
795 // we find it, check to see if it was preceeded by a *. This common
796 // optimization helps people who like to put a lot of * characters in their
797 // comments.
Chris Lattnerc850ad62007-07-21 23:43:37 +0000798
799 // The first character we get with newlines and trigraphs skipped to handle
800 // the degenerate /*/ case below correctly if the * has an escaped newline
801 // after it.
802 unsigned CharSize;
803 unsigned char C = getCharAndSize(CurPtr, CharSize);
804 CurPtr += CharSize;
Chris Lattner22eb9722006-06-18 05:43:12 +0000805 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000806 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000807 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +0000808 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000809 }
810
Chris Lattnerc850ad62007-07-21 23:43:37 +0000811 // Check to see if the first character after the '/*' is another /. If so,
812 // then this slash does not end the block comment, it is part of it.
813 if (C == '/')
814 C = *CurPtr++;
815
Chris Lattner22eb9722006-06-18 05:43:12 +0000816 while (1) {
Chris Lattner6cc3e362006-10-27 04:12:35 +0000817 // Skip over all non-interesting characters until we find end of buffer or a
818 // (probably ending) '/' character.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000819 if (CurPtr + 24 < BufferEnd) {
820 // While not aligned to a 16-byte boundary.
821 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
822 C = *CurPtr++;
823
824 if (C == '/') goto FoundSlash;
Chris Lattneraded4a92006-10-27 04:42:31 +0000825
826#ifdef __SSE2__
827 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
828 '/', '/', '/', '/', '/', '/', '/', '/');
829 while (CurPtr+16 <= BufferEnd &&
830 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
831 CurPtr += 16;
Chris Lattner9f6604f2006-10-30 20:01:22 +0000832#elif __ALTIVEC__
833 __vector unsigned char Slashes = {
834 '/', '/', '/', '/', '/', '/', '/', '/',
835 '/', '/', '/', '/', '/', '/', '/', '/'
836 };
837 while (CurPtr+16 <= BufferEnd &&
838 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
839 CurPtr += 16;
840#else
Chris Lattneraded4a92006-10-27 04:42:31 +0000841 // Scan for '/' quickly. Many block comments are very large.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000842 while (CurPtr[0] != '/' &&
843 CurPtr[1] != '/' &&
844 CurPtr[2] != '/' &&
845 CurPtr[3] != '/' &&
846 CurPtr+4 < BufferEnd) {
847 CurPtr += 4;
848 }
Chris Lattneraded4a92006-10-27 04:42:31 +0000849#endif
850
851 // It has to be one of the bytes scanned, increment to it and read one.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000852 C = *CurPtr++;
853 }
854
Chris Lattneraded4a92006-10-27 04:42:31 +0000855 // Loop to scan the remainder.
Chris Lattner22eb9722006-06-18 05:43:12 +0000856 while (C != '/' && C != '\0')
857 C = *CurPtr++;
858
Chris Lattner6cc3e362006-10-27 04:12:35 +0000859 FoundSlash:
Chris Lattner22eb9722006-06-18 05:43:12 +0000860 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000861 if (CurPtr[-2] == '*') // We found the final */. We're done!
862 break;
863
864 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +0000865 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000866 // We found the final */, though it had an escaped newline between the
867 // * and /. We're done!
868 break;
869 }
870 }
871 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
872 // If this is a /* inside of the comment, emit a warning. Don't do this
873 // if this is a /*/, which will end the comment. This misses cases with
874 // embedded escaped newlines, but oh well.
Chris Lattnercb283342006-06-18 06:48:37 +0000875 Diag(CurPtr-1, diag::nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000876 }
877 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000878 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000879 // Note: the user probably forgot a */. We could continue immediately
880 // after the /*, but this would involve lexing a lot of what really is the
881 // comment, which surely would confuse the parser.
882 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +0000883 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000884 }
885 C = *CurPtr++;
886 }
Chris Lattner457fc152006-07-29 06:30:25 +0000887
888 // If we are returning comments as tokens, return this comment as a token.
889 if (KeepCommentMode) {
Chris Lattner8c204872006-10-14 05:19:21 +0000890 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +0000891 FormTokenWithChars(Result, CurPtr);
892 return false;
893 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000894
895 // It is common for the tokens immediately after a /**/ comment to be
896 // whitespace. Instead of going through the big switch, handle it
897 // efficiently now.
898 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner146762e2007-07-20 16:59:19 +0000899 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000900 SkipWhitespace(Result, CurPtr+1);
901 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000902 }
903
904 // Otherwise, just return so that the next character will be lexed as a token.
905 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +0000906 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000907 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000908}
909
910//===----------------------------------------------------------------------===//
911// Primary Lexing Entry Points
912//===----------------------------------------------------------------------===//
913
914/// LexIncludeFilename - After the preprocessor has parsed a #include, lex and
915/// (potentially) macro expand the filename.
Chris Lattner146762e2007-07-20 16:59:19 +0000916void Lexer::LexIncludeFilename(Token &FilenameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000917 assert(ParsingPreprocessorDirective &&
918 ParsingFilename == false &&
919 "Must be in a preprocessing directive!");
920
921 // We are now parsing a filename!
922 ParsingFilename = true;
923
Chris Lattner269c2322006-06-25 06:23:00 +0000924 // Lex the filename.
925 Lex(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000926
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000927 // We should have obtained the filename now.
Chris Lattner22eb9722006-06-18 05:43:12 +0000928 ParsingFilename = false;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000929
Chris Lattner22eb9722006-06-18 05:43:12 +0000930 // No filename?
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000931 if (FilenameTok.getKind() == tok::eom)
Chris Lattner538d7f32006-07-20 04:31:52 +0000932 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner22eb9722006-06-18 05:43:12 +0000933}
934
935/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
936/// uninterpreted string. This switches the lexer out of directive mode.
937std::string Lexer::ReadToEndOfLine() {
938 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
939 "Must be in a preprocessing directive!");
940 std::string Result;
Chris Lattner146762e2007-07-20 16:59:19 +0000941 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +0000942
943 // CurPtr - Cache BufferPtr in an automatic variable.
944 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000945 while (1) {
946 char Char = getAndAdvanceChar(CurPtr, Tmp);
947 switch (Char) {
948 default:
949 Result += Char;
950 break;
951 case 0: // Null.
952 // Found end of file?
953 if (CurPtr-1 != BufferEnd) {
954 // Nope, normal character, continue.
955 Result += Char;
956 break;
957 }
958 // FALL THROUGH.
959 case '\r':
960 case '\n':
961 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
962 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
963 BufferPtr = CurPtr-1;
964
965 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +0000966 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000967 assert(Tmp.getKind() == tok::eom && "Unexpected token!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000968
969 // Finally, we're done, return the string we found.
970 return Result;
971 }
972 }
973}
974
975/// LexEndOfFile - CurPtr points to the end of this file. Handle this
976/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +0000977/// This returns true if Result contains a token, false if PP.Lex should be
978/// called again.
Chris Lattner146762e2007-07-20 16:59:19 +0000979bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000980 // If we hit the end of the file while parsing a preprocessor directive,
981 // end the preprocessor directive first. The next token returned will
982 // then be the end of file.
983 if (ParsingPreprocessorDirective) {
984 // Done parsing the "line".
985 ParsingPreprocessorDirective = false;
Chris Lattner8c204872006-10-14 05:19:21 +0000986 Result.setKind(tok::eom);
Chris Lattnerd01e2912006-06-18 16:22:51 +0000987 // Update the location of token as well as BufferPtr.
988 FormTokenWithChars(Result, CurPtr);
Chris Lattner457fc152006-07-29 06:30:25 +0000989
990 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerb352e3e2006-11-21 06:17:10 +0000991 KeepCommentMode = PP.getCommentRetentionState();
Chris Lattner2183a6e2006-07-18 06:36:12 +0000992 return true; // Have a token.
Chris Lattner22eb9722006-06-18 05:43:12 +0000993 }
994
Chris Lattner30a2fa12006-07-19 06:31:49 +0000995 // If we are in raw mode, return this event as an EOF token. Let the caller
996 // that put us in raw mode handle the event.
997 if (LexingRawMode) {
Chris Lattner8c204872006-10-14 05:19:21 +0000998 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +0000999 BufferPtr = BufferEnd;
1000 FormTokenWithChars(Result, BufferEnd);
Chris Lattner8c204872006-10-14 05:19:21 +00001001 Result.setKind(tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001002 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001003 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001004
Chris Lattner30a2fa12006-07-19 06:31:49 +00001005 // Otherwise, issue diagnostics for unterminated #if and missing newline.
1006
1007 // If we are in a #if directive, emit an error.
1008 while (!ConditionalStack.empty()) {
Chris Lattner538d7f32006-07-20 04:31:52 +00001009 Diag(ConditionalStack.back().IfLoc, diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001010 ConditionalStack.pop_back();
1011 }
1012
1013 // If the file was empty or didn't end in a newline, issue a pedwarn.
1014 if (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1015 Diag(BufferEnd, diag::ext_no_newline_eof);
1016
Chris Lattner22eb9722006-06-18 05:43:12 +00001017 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +00001018
1019 // Finally, let the preprocessor handle this.
Chris Lattner2183a6e2006-07-18 06:36:12 +00001020 return PP.HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001021}
1022
Chris Lattner678c8802006-07-11 05:46:12 +00001023/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1024/// the specified lexer will return a tok::l_paren token, 0 if it is something
1025/// else and 2 if there are no more tokens in the buffer controlled by the
1026/// lexer.
1027unsigned Lexer::isNextPPTokenLParen() {
1028 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1029
1030 // Switch to 'skipping' mode. This will ensure that we can lex a token
1031 // without emitting diagnostics, disables macro expansion, and will cause EOF
1032 // to return an EOF token instead of popping the include stack.
1033 LexingRawMode = true;
1034
1035 // Save state that can be changed while lexing so that we can restore it.
1036 const char *TmpBufferPtr = BufferPtr;
1037
Chris Lattner146762e2007-07-20 16:59:19 +00001038 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001039 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001040 LexTokenInternal(Tok);
1041
1042 // Restore state that may have changed.
1043 BufferPtr = TmpBufferPtr;
1044
1045 // Restore the lexer back to non-skipping mode.
1046 LexingRawMode = false;
1047
1048 if (Tok.getKind() == tok::eof)
1049 return 2;
1050 return Tok.getKind() == tok::l_paren;
1051}
1052
Chris Lattner22eb9722006-06-18 05:43:12 +00001053
1054/// LexTokenInternal - This implements a simple C family lexer. It is an
1055/// extremely performance critical piece of code. This assumes that the buffer
1056/// has a null character at the end of the file. Return true if an error
1057/// occurred and compilation should terminate, false if normal. This returns a
1058/// preprocessing token, not a normal token, as such, it is an internal
1059/// interface. It assumes that the Flags of result have been cleared before
1060/// calling this.
Chris Lattner146762e2007-07-20 16:59:19 +00001061void Lexer::LexTokenInternal(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001062LexNextToken:
1063 // New token, can't need cleaning yet.
Chris Lattner146762e2007-07-20 16:59:19 +00001064 Result.clearFlag(Token::NeedsCleaning);
Chris Lattner8c204872006-10-14 05:19:21 +00001065 Result.setIdentifierInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001066
1067 // CurPtr - Cache BufferPtr in an automatic variable.
1068 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001069
Chris Lattnereb54b592006-07-10 06:34:27 +00001070 // Small amounts of horizontal whitespace is very common between tokens.
1071 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1072 ++CurPtr;
1073 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1074 ++CurPtr;
1075 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001076 Result.setFlag(Token::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001077 }
1078
Chris Lattner22eb9722006-06-18 05:43:12 +00001079 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1080
1081 // Read a character, advancing over it.
1082 char Char = getAndAdvanceChar(CurPtr, Result);
1083 switch (Char) {
1084 case 0: // Null.
1085 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001086 if (CurPtr-1 == BufferEnd) {
1087 // Read the PP instance variable into an automatic variable, because
1088 // LexEndOfFile will often delete 'this'.
1089 Preprocessor &PPCache = PP;
1090 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1091 return; // Got a token to return.
1092 return PPCache.Lex(Result);
1093 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001094
Chris Lattnercb283342006-06-18 06:48:37 +00001095 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner146762e2007-07-20 16:59:19 +00001096 Result.setFlag(Token::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001097 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001098 goto LexNextToken; // GCC isn't tail call eliminating.
1099 case '\n':
1100 case '\r':
1101 // If we are inside a preprocessor directive and we see the end of line,
1102 // we know we are done with the directive, so return an EOM token.
1103 if (ParsingPreprocessorDirective) {
1104 // Done parsing the "line".
1105 ParsingPreprocessorDirective = false;
1106
Chris Lattner457fc152006-07-29 06:30:25 +00001107 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001108 KeepCommentMode = PP.getCommentRetentionState();
Chris Lattner457fc152006-07-29 06:30:25 +00001109
Chris Lattner22eb9722006-06-18 05:43:12 +00001110 // Since we consumed a newline, we are back at the start of a line.
1111 IsAtStartOfLine = true;
1112
Chris Lattner8c204872006-10-14 05:19:21 +00001113 Result.setKind(tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001114 break;
1115 }
1116 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001117 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001118 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001119 Result.clearFlag(Token::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001120 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001121 goto LexNextToken; // GCC isn't tail call eliminating.
1122 case ' ':
1123 case '\t':
1124 case '\f':
1125 case '\v':
Chris Lattnerb9b85972007-07-22 06:29:05 +00001126 SkipHorizontalWhitespace:
Chris Lattner146762e2007-07-20 16:59:19 +00001127 Result.setFlag(Token::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001128 SkipWhitespace(Result, CurPtr);
Chris Lattnerb9b85972007-07-22 06:29:05 +00001129
1130 SkipIgnoredUnits:
1131 CurPtr = BufferPtr;
1132
1133 // If the next token is obviously a // or /* */ comment, skip it efficiently
1134 // too (without going through the big switch stmt).
1135 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !KeepCommentMode) {
1136 SkipBCPLComment(Result, CurPtr+2);
1137 goto SkipIgnoredUnits;
1138 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !KeepCommentMode) {
1139 SkipBlockComment(Result, CurPtr+2);
1140 goto SkipIgnoredUnits;
1141 } else if (isHorizontalWhitespace(*CurPtr)) {
1142 goto SkipHorizontalWhitespace;
1143 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001144 goto LexNextToken; // GCC isn't tail call eliminating.
1145
1146 case 'L':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001147 // Notify MIOpt that we read a non-whitespace/non-comment token.
1148 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001149 Char = getCharAndSize(CurPtr, SizeTmp);
1150
1151 // Wide string literal.
1152 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001153 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1154 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001155
1156 // Wide character constant.
1157 if (Char == '\'')
1158 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1159 // FALL THROUGH, treating L like the start of an identifier.
1160
1161 // C99 6.4.2: Identifiers.
1162 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1163 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1164 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1165 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1166 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1167 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1168 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1169 case 'v': case 'w': case 'x': case 'y': case 'z':
1170 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001171 // Notify MIOpt that we read a non-whitespace/non-comment token.
1172 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001173 return LexIdentifier(Result, CurPtr);
1174
1175 // C99 6.4.4.1: Integer Constants.
1176 // C99 6.4.4.2: Floating Constants.
1177 case '0': case '1': case '2': case '3': case '4':
1178 case '5': case '6': case '7': case '8': case '9':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001179 // Notify MIOpt that we read a non-whitespace/non-comment token.
1180 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001181 return LexNumericConstant(Result, CurPtr);
1182
1183 // C99 6.4.4: Character Constants.
1184 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001185 // Notify MIOpt that we read a non-whitespace/non-comment token.
1186 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001187 return LexCharConstant(Result, CurPtr);
1188
1189 // C99 6.4.5: String Literals.
1190 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001191 // Notify MIOpt that we read a non-whitespace/non-comment token.
1192 MIOpt.ReadToken();
Chris Lattnerd3e98952006-10-06 05:22:26 +00001193 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001194
1195 // C99 6.4.6: Punctuators.
1196 case '?':
Chris Lattner8c204872006-10-14 05:19:21 +00001197 Result.setKind(tok::question);
Chris Lattner22eb9722006-06-18 05:43:12 +00001198 break;
1199 case '[':
Chris Lattner8c204872006-10-14 05:19:21 +00001200 Result.setKind(tok::l_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001201 break;
1202 case ']':
Chris Lattner8c204872006-10-14 05:19:21 +00001203 Result.setKind(tok::r_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001204 break;
1205 case '(':
Chris Lattner8c204872006-10-14 05:19:21 +00001206 Result.setKind(tok::l_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001207 break;
1208 case ')':
Chris Lattner8c204872006-10-14 05:19:21 +00001209 Result.setKind(tok::r_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001210 break;
1211 case '{':
Chris Lattner8c204872006-10-14 05:19:21 +00001212 Result.setKind(tok::l_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001213 break;
1214 case '}':
Chris Lattner8c204872006-10-14 05:19:21 +00001215 Result.setKind(tok::r_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001216 break;
1217 case '.':
1218 Char = getCharAndSize(CurPtr, SizeTmp);
1219 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001220 // Notify MIOpt that we read a non-whitespace/non-comment token.
1221 MIOpt.ReadToken();
1222
Chris Lattner22eb9722006-06-18 05:43:12 +00001223 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1224 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001225 Result.setKind(tok::periodstar);
Chris Lattner22eb9722006-06-18 05:43:12 +00001226 CurPtr += SizeTmp;
1227 } else if (Char == '.' &&
1228 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner8c204872006-10-14 05:19:21 +00001229 Result.setKind(tok::ellipsis);
Chris Lattner22eb9722006-06-18 05:43:12 +00001230 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1231 SizeTmp2, Result);
1232 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001233 Result.setKind(tok::period);
Chris Lattner22eb9722006-06-18 05:43:12 +00001234 }
1235 break;
1236 case '&':
1237 Char = getCharAndSize(CurPtr, SizeTmp);
1238 if (Char == '&') {
Chris Lattner8c204872006-10-14 05:19:21 +00001239 Result.setKind(tok::ampamp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001240 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1241 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001242 Result.setKind(tok::ampequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001243 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1244 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001245 Result.setKind(tok::amp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001246 }
1247 break;
1248 case '*':
1249 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001250 Result.setKind(tok::starequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001251 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1252 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001253 Result.setKind(tok::star);
Chris Lattner22eb9722006-06-18 05:43:12 +00001254 }
1255 break;
1256 case '+':
1257 Char = getCharAndSize(CurPtr, SizeTmp);
1258 if (Char == '+') {
Chris Lattner8c204872006-10-14 05:19:21 +00001259 Result.setKind(tok::plusplus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001260 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1261 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001262 Result.setKind(tok::plusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001263 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1264 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001265 Result.setKind(tok::plus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001266 }
1267 break;
1268 case '-':
1269 Char = getCharAndSize(CurPtr, SizeTmp);
1270 if (Char == '-') {
Chris Lattner8c204872006-10-14 05:19:21 +00001271 Result.setKind(tok::minusminus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001272 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1273 } else if (Char == '>' && Features.CPlusPlus &&
1274 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001275 Result.setKind(tok::arrowstar); // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00001276 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1277 SizeTmp2, Result);
1278 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001279 Result.setKind(tok::arrow);
Chris Lattner22eb9722006-06-18 05:43:12 +00001280 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1281 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001282 Result.setKind(tok::minusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001283 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1284 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001285 Result.setKind(tok::minus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001286 }
1287 break;
1288 case '~':
Chris Lattner8c204872006-10-14 05:19:21 +00001289 Result.setKind(tok::tilde);
Chris Lattner22eb9722006-06-18 05:43:12 +00001290 break;
1291 case '!':
1292 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001293 Result.setKind(tok::exclaimequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001294 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1295 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001296 Result.setKind(tok::exclaim);
Chris Lattner22eb9722006-06-18 05:43:12 +00001297 }
1298 break;
1299 case '/':
1300 // 6.4.9: Comments
1301 Char = getCharAndSize(CurPtr, SizeTmp);
1302 if (Char == '/') { // BCPL comment.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001303 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) {
1304 // It is common for the tokens immediately after a // comment to be
1305 // whitespace (indentation for the next line). Instead of going through the
1306 // big switch, handle it efficiently now.
1307 goto SkipIgnoredUnits;
1308 }
Chris Lattner457fc152006-07-29 06:30:25 +00001309 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001310 } else if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001311 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1312 goto LexNextToken; // GCC isn't tail call eliminating.
1313 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001314 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001315 Result.setKind(tok::slashequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001316 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1317 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001318 Result.setKind(tok::slash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001319 }
1320 break;
1321 case '%':
1322 Char = getCharAndSize(CurPtr, SizeTmp);
1323 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001324 Result.setKind(tok::percentequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001325 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1326 } else if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001327 Result.setKind(tok::r_brace); // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00001328 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1329 } else if (Features.Digraphs && Char == ':') {
1330 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001331 Char = getCharAndSize(CurPtr, SizeTmp);
1332 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001333 Result.setKind(tok::hashhash); // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00001334 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1335 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001336 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001337 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001338 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1339 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner22eb9722006-06-18 05:43:12 +00001340 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001341 Result.setKind(tok::hash); // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00001342
1343 // We parsed a # character. If this occurs at the start of the line,
1344 // it's actually the start of a preprocessing directive. Callback to
1345 // the preprocessor to handle it.
1346 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001347 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001348 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001349 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001350
1351 // As an optimization, if the preprocessor didn't switch lexers, tail
1352 // recurse.
1353 if (PP.isCurrentLexer(this)) {
1354 // Start a new token. If this is a #include or something, the PP may
1355 // want us starting at the beginning of the line again. If so, set
1356 // the StartOfLine flag.
1357 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00001358 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001359 IsAtStartOfLine = false;
1360 }
1361 goto LexNextToken; // GCC isn't tail call eliminating.
1362 }
1363
1364 return PP.Lex(Result);
1365 }
1366 }
1367 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001368 Result.setKind(tok::percent);
Chris Lattner22eb9722006-06-18 05:43:12 +00001369 }
1370 break;
1371 case '<':
1372 Char = getCharAndSize(CurPtr, SizeTmp);
1373 if (ParsingFilename) {
1374 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1375 } else if (Char == '<' &&
1376 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001377 Result.setKind(tok::lesslessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001378 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1379 SizeTmp2, Result);
1380 } else if (Char == '<') {
Chris Lattner8c204872006-10-14 05:19:21 +00001381 Result.setKind(tok::lessless);
Chris Lattner22eb9722006-06-18 05:43:12 +00001382 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1383 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001384 Result.setKind(tok::lessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001385 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1386 } else if (Features.Digraphs && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001387 Result.setKind(tok::l_square); // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00001388 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1389 } else if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001390 Result.setKind(tok::l_brace); // '<%' -> '{'
Chris Lattner22eb9722006-06-18 05:43:12 +00001391 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001392 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001393 Result.setKind(tok::less);
Chris Lattner22eb9722006-06-18 05:43:12 +00001394 }
1395 break;
1396 case '>':
1397 Char = getCharAndSize(CurPtr, SizeTmp);
1398 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001399 Result.setKind(tok::greaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001400 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1401 } else if (Char == '>' &&
1402 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001403 Result.setKind(tok::greatergreaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001404 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1405 SizeTmp2, Result);
1406 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001407 Result.setKind(tok::greatergreater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001408 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001409 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001410 Result.setKind(tok::greater);
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::caretequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001417 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1418 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001419 Result.setKind(tok::caret);
Chris Lattner22eb9722006-06-18 05:43:12 +00001420 }
1421 break;
1422 case '|':
1423 Char = getCharAndSize(CurPtr, SizeTmp);
1424 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001425 Result.setKind(tok::pipeequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001426 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1427 } else if (Char == '|') {
Chris Lattner8c204872006-10-14 05:19:21 +00001428 Result.setKind(tok::pipepipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001429 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1430 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001431 Result.setKind(tok::pipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001432 }
1433 break;
1434 case ':':
1435 Char = getCharAndSize(CurPtr, SizeTmp);
1436 if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001437 Result.setKind(tok::r_square); // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00001438 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1439 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001440 Result.setKind(tok::coloncolon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001441 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1442 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001443 Result.setKind(tok::colon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001444 }
1445 break;
1446 case ';':
Chris Lattner8c204872006-10-14 05:19:21 +00001447 Result.setKind(tok::semi);
Chris Lattner22eb9722006-06-18 05:43:12 +00001448 break;
1449 case '=':
1450 Char = getCharAndSize(CurPtr, SizeTmp);
1451 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001452 Result.setKind(tok::equalequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001453 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1454 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001455 Result.setKind(tok::equal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001456 }
1457 break;
1458 case ',':
Chris Lattner8c204872006-10-14 05:19:21 +00001459 Result.setKind(tok::comma);
Chris Lattner22eb9722006-06-18 05:43:12 +00001460 break;
1461 case '#':
1462 Char = getCharAndSize(CurPtr, SizeTmp);
1463 if (Char == '#') {
Chris Lattner8c204872006-10-14 05:19:21 +00001464 Result.setKind(tok::hashhash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001465 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001466 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001467 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001468 Diag(BufferPtr, diag::charize_microsoft_ext);
1469 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001470 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001471 Result.setKind(tok::hash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001472 // We parsed a # character. If this occurs at the start of the line,
1473 // it's actually the start of a preprocessing directive. Callback to
1474 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00001475 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001476 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001477 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001478 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001479
1480 // As an optimization, if the preprocessor didn't switch lexers, tail
1481 // recurse.
1482 if (PP.isCurrentLexer(this)) {
1483 // Start a new token. If this is a #include or something, the PP may
1484 // want us starting at the beginning of the line again. If so, set
1485 // the StartOfLine flag.
1486 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00001487 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001488 IsAtStartOfLine = false;
1489 }
1490 goto LexNextToken; // GCC isn't tail call eliminating.
1491 }
1492 return PP.Lex(Result);
1493 }
1494 }
1495 break;
1496
1497 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00001498 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00001499 // FALL THROUGH.
1500 default:
1501 // Objective C support.
1502 if (CurPtr[-1] == '@' && Features.ObjC1) {
Chris Lattner8c204872006-10-14 05:19:21 +00001503 Result.setKind(tok::at);
Chris Lattner22eb9722006-06-18 05:43:12 +00001504 break;
1505 } else if (CurPtr[-1] == '$' && Features.DollarIdents) {// $ in identifiers.
Chris Lattnercb283342006-06-18 06:48:37 +00001506 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001507 // Notify MIOpt that we read a non-whitespace/non-comment token.
1508 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001509 return LexIdentifier(Result, CurPtr);
1510 }
1511
Chris Lattner8c204872006-10-14 05:19:21 +00001512 Result.setKind(tok::unknown);
Chris Lattner041bef82006-07-11 05:52:53 +00001513 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001514 }
1515
Chris Lattner371ac8a2006-07-04 07:11:10 +00001516 // Notify MIOpt that we read a non-whitespace/non-comment token.
1517 MIOpt.ReadToken();
1518
Chris Lattnerd01e2912006-06-18 16:22:51 +00001519 // Update the location of token as well as BufferPtr.
1520 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001521}