blob: 8acfe2aef6a2ce1e0d9f0c233bd0646fa192a2ca [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
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 Lattner619c1742007-07-22 18:38:25 +000031#include "llvm/Support/Compiler.h"
Chris Lattner739e7392007-04-29 07:12:06 +000032#include "llvm/Support/MemoryBuffer.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000033#include <cctype>
Chris Lattner22eb9722006-06-18 05:43:12 +000034using namespace clang;
35
36static void InitCharacterInfo();
37
Chris Lattner4894f482007-10-07 08:47:24 +000038//===----------------------------------------------------------------------===//
39// Token Class Implementation
40//===----------------------------------------------------------------------===//
41
42/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
43bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
Chris Lattner98c1f7c2007-10-09 18:02:16 +000044 return is(tok::identifier) &&
45 getIdentifierInfo()->getObjCKeywordID() == objcKey;
Chris Lattner4894f482007-10-07 08:47:24 +000046}
47
48/// getObjCKeywordID - Return the ObjC keyword kind.
49tok::ObjCKeywordKind Token::getObjCKeywordID() const {
50 IdentifierInfo *specId = getIdentifierInfo();
51 return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
52}
53
Chris Lattner67671ed2007-12-13 01:59:49 +000054
Chris Lattner4894f482007-10-07 08:47:24 +000055//===----------------------------------------------------------------------===//
56// Lexer Class Implementation
57//===----------------------------------------------------------------------===//
58
59
Chris Lattner02b436a2007-10-17 20:41:00 +000060/// Lexer constructor - Create a new lexer object for the specified buffer
61/// with the specified preprocessor managing the lexing process. This lexer
62/// assumes that the associated file buffer and Preprocessor objects will
63/// outlive it, so it doesn't take ownership of either of them.
Chris Lattner77e9de52007-07-20 16:52:03 +000064Lexer::Lexer(SourceLocation fileloc, Preprocessor &pp,
65 const char *BufStart, const char *BufEnd)
Chris Lattner02b436a2007-10-17 20:41:00 +000066 : FileLoc(fileloc), PP(&pp), Features(pp.getLangOptions()) {
Chris Lattner77e9de52007-07-20 16:52:03 +000067
Chris Lattner02b436a2007-10-17 20:41:00 +000068 SourceManager &SourceMgr = PP->getSourceManager();
Chris Lattner5d1c0272007-07-22 18:44:36 +000069 unsigned InputFileID = SourceMgr.getPhysicalLoc(FileLoc).getFileID();
70 const llvm::MemoryBuffer *InputFile = SourceMgr.getBuffer(InputFileID);
Chris Lattner77e9de52007-07-20 16:52:03 +000071
Chris Lattnerecfeafe2006-07-02 21:26:45 +000072 Is_PragmaLexer = false;
Chris Lattner22eb9722006-06-18 05:43:12 +000073 InitCharacterInfo();
Chris Lattner5d1c0272007-07-22 18:44:36 +000074
75 // BufferStart must always be InputFile->getBufferStart().
76 BufferStart = InputFile->getBufferStart();
77
78 // BufferPtr and BufferEnd can start out somewhere inside the current buffer.
79 // If unspecified, they starts at the start/end of the buffer.
80 BufferPtr = BufStart ? BufStart : BufferStart;
Chris Lattner77e9de52007-07-20 16:52:03 +000081 BufferEnd = BufEnd ? BufEnd : InputFile->getBufferEnd();
82
Chris Lattner22eb9722006-06-18 05:43:12 +000083 assert(BufferEnd[0] == 0 &&
84 "We assume that the input buffer has a null character at the end"
85 " to simplify lexing!");
Chris Lattner77e9de52007-07-20 16:52:03 +000086
Chris Lattner22eb9722006-06-18 05:43:12 +000087 // Start of the file is a start of line.
88 IsAtStartOfLine = true;
89
90 // We are not after parsing a #.
91 ParsingPreprocessorDirective = false;
92
93 // We are not after parsing #include.
94 ParsingFilename = false;
Chris Lattner3ebcf4e2006-07-11 05:39:23 +000095
96 // We are not in raw mode. Raw mode disables diagnostics and interpretation
97 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
98 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
99 // or otherwise skipping over tokens.
100 LexingRawMode = false;
Chris Lattner457fc152006-07-29 06:30:25 +0000101
102 // Default to keeping comments if requested.
Chris Lattner02b436a2007-10-17 20:41:00 +0000103 KeepCommentMode = PP->getCommentRetentionState();
Chris Lattner22eb9722006-06-18 05:43:12 +0000104}
105
Chris Lattner02b436a2007-10-17 20:41:00 +0000106/// Lexer constructor - Create a new raw lexer object. This object is only
107/// suitable for calls to 'LexRawToken'. This lexer assumes that the
108/// associated file buffer will outlive it, so it doesn't take ownership of
109/// either of them.
110Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
111 const char *BufStart, const char *BufEnd)
112 : FileLoc(fileloc), PP(0), Features(features) {
113 Is_PragmaLexer = false;
114 InitCharacterInfo();
115
116 BufferStart = BufStart;
117 BufferPtr = BufStart;
118 BufferEnd = BufEnd;
119
120 assert(BufferEnd[0] == 0 &&
121 "We assume that the input buffer has a null character at the end"
122 " to simplify lexing!");
123
124 // Start of the file is a start of line.
125 IsAtStartOfLine = true;
126
127 // We are not after parsing a #.
128 ParsingPreprocessorDirective = false;
129
130 // We are not after parsing #include.
131 ParsingFilename = false;
132
133 // We *are* in raw mode.
134 LexingRawMode = true;
135
136 // Never keep comments in raw mode.
137 KeepCommentMode = false;
138}
139
140
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000141/// Stringify - Convert the specified string into a C string, with surrounding
142/// ""'s, and with escaped \ and " characters.
Chris Lattnerecc39e92006-07-15 05:23:31 +0000143std::string Lexer::Stringify(const std::string &Str, bool Charify) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000144 std::string Result = Str;
Chris Lattnerecc39e92006-07-15 05:23:31 +0000145 char Quote = Charify ? '\'' : '"';
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000146 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
Chris Lattnerecc39e92006-07-15 05:23:31 +0000147 if (Result[i] == '\\' || Result[i] == Quote) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000148 Result.insert(Result.begin()+i, '\\');
149 ++i; ++e;
150 }
151 }
Chris Lattnerecc39e92006-07-15 05:23:31 +0000152 return Result;
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000153}
154
Chris Lattner4c4a2452007-07-24 06:57:14 +0000155/// Stringify - Convert the specified string into a C string by escaping '\'
156/// and " characters. This does not add surrounding ""'s to the string.
157void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
158 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
159 if (Str[i] == '\\' || Str[i] == '"') {
160 Str.insert(Str.begin()+i, '\\');
161 ++i; ++e;
162 }
163 }
164}
165
Chris Lattner22eb9722006-06-18 05:43:12 +0000166
Chris Lattner8e129c22007-10-17 21:18:47 +0000167/// MeasureTokenLength - Relex the token at the specified location and return
168/// its length in bytes in the input file. If the token needs cleaning (e.g.
169/// includes a trigraph or an escaped newline) then this count includes bytes
170/// that are part of that.
171unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
172 const SourceManager &SM) {
173 // If this comes from a macro expansion, we really do want the macro name, not
174 // the token this macro expanded to.
175 Loc = SM.getLogicalLoc(Loc);
176
177 const char *StrData = SM.getCharacterData(Loc);
178
179 // TODO: this could be special cased for common tokens like identifiers, ')',
180 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
181 // all obviously single-char tokens. This could use
182 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
183 // something.
184
185
186 const char *BufEnd = SM.getBufferData(Loc.getFileID()).second;
187
188 // Create a langops struct and enable trigraphs. This is sufficient for
189 // measuring tokens.
190 LangOptions LangOpts;
191 LangOpts.Trigraphs = true;
192
193 // Create a lexer starting at the beginning of this token.
194 Lexer TheLexer(Loc, LangOpts, StrData, BufEnd);
195 Token TheTok;
196 TheLexer.LexRawToken(TheTok);
197 return TheTok.getLength();
198}
199
Chris Lattner22eb9722006-06-18 05:43:12 +0000200//===----------------------------------------------------------------------===//
201// Character information.
202//===----------------------------------------------------------------------===//
203
204static unsigned char CharInfo[256];
205
206enum {
207 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
208 CHAR_VERT_WS = 0x02, // '\r', '\n'
209 CHAR_LETTER = 0x04, // a-z,A-Z
210 CHAR_NUMBER = 0x08, // 0-9
211 CHAR_UNDER = 0x10, // _
212 CHAR_PERIOD = 0x20 // .
213};
214
215static void InitCharacterInfo() {
216 static bool isInited = false;
217 if (isInited) return;
218 isInited = true;
219
220 // Intiialize the CharInfo table.
221 // TODO: statically initialize this.
222 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
223 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
224 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
225
226 CharInfo[(int)'_'] = CHAR_UNDER;
Chris Lattnerdd0b7cb2006-10-17 02:53:51 +0000227 CharInfo[(int)'.'] = CHAR_PERIOD;
Chris Lattner22eb9722006-06-18 05:43:12 +0000228 for (unsigned i = 'a'; i <= 'z'; ++i)
229 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
230 for (unsigned i = '0'; i <= '9'; ++i)
231 CharInfo[i] = CHAR_NUMBER;
232}
233
234/// isIdentifierBody - Return true if this is the body character of an
235/// identifier, which is [a-zA-Z0-9_].
236static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000237 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000238}
239
240/// isHorizontalWhitespace - Return true if this character is horizontal
241/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
242static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000243 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000244}
245
246/// isWhitespace - Return true if this character is horizontal or vertical
247/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
248/// for '\0'.
249static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000250 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000251}
252
253/// isNumberBody - Return true if this is the body character of an
254/// preprocessing number, which is [a-zA-Z0-9_.].
255static inline bool isNumberBody(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000256 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
257 true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000258}
259
Chris Lattnerd01e2912006-06-18 16:22:51 +0000260
Chris Lattner22eb9722006-06-18 05:43:12 +0000261//===----------------------------------------------------------------------===//
262// Diagnostics forwarding code.
263//===----------------------------------------------------------------------===//
264
Chris Lattner619c1742007-07-22 18:38:25 +0000265/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
266/// lexer buffer was all instantiated at a single point, perform the mapping.
267/// This is currently only used for _Pragma implementation, so it is the slow
268/// path of the hot getSourceLocation method. Do not allow it to be inlined.
269static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
270 SourceLocation FileLoc,
271 unsigned CharNo) DISABLE_INLINE;
272static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
273 SourceLocation FileLoc,
274 unsigned CharNo) {
275 // Otherwise, we're lexing "mapped tokens". This is used for things like
276 // _Pragma handling. Combine the instantiation location of FileLoc with the
277 // physical location.
278 SourceManager &SourceMgr = PP.getSourceManager();
279
280 // Create a new SLoc which is expanded from logical(FileLoc) but whose
281 // characters come from phys(FileLoc)+Offset.
282 SourceLocation VirtLoc = SourceMgr.getLogicalLoc(FileLoc);
283 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(FileLoc);
284 PhysLoc = SourceLocation::getFileLoc(PhysLoc.getFileID(), CharNo);
285 return SourceMgr.getInstantiationLoc(PhysLoc, VirtLoc);
286}
287
Chris Lattner22eb9722006-06-18 05:43:12 +0000288/// getSourceLocation - Return a source location identifier for the specified
289/// offset in the current file.
290SourceLocation Lexer::getSourceLocation(const char *Loc) const {
Chris Lattner5d1c0272007-07-22 18:44:36 +0000291 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Chris Lattner4cca5ba2006-07-02 20:05:54 +0000292 "Location out of range for this buffer!");
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000293
294 // In the normal case, we're just lexing from a simple file buffer, return
295 // the file id from FileLoc with the offset specified.
Chris Lattner5d1c0272007-07-22 18:44:36 +0000296 unsigned CharNo = Loc-BufferStart;
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000297 if (FileLoc.isFileID())
298 return SourceLocation::getFileLoc(FileLoc.getFileID(), CharNo);
299
Chris Lattner02b436a2007-10-17 20:41:00 +0000300 assert(PP && "This doesn't work on raw lexers");
301 return GetMappedTokenLoc(*PP, FileLoc, CharNo);
Chris Lattner22eb9722006-06-18 05:43:12 +0000302}
303
Chris Lattner22eb9722006-06-18 05:43:12 +0000304/// Diag - Forwarding function for diagnostics. This translate a source
305/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000306void Lexer::Diag(const char *Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000307 const std::string &Msg) const {
Chris Lattner4431a1b2007-11-30 22:53:43 +0000308 if (LexingRawMode && Diagnostic::isBuiltinNoteWarningOrExtension(DiagID))
Chris Lattner538d7f32006-07-20 04:31:52 +0000309 return;
Chris Lattner02b436a2007-10-17 20:41:00 +0000310 PP->Diag(getSourceLocation(Loc), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000311}
Chris Lattner538d7f32006-07-20 04:31:52 +0000312void Lexer::Diag(SourceLocation Loc, unsigned DiagID,
313 const std::string &Msg) const {
Chris Lattner4431a1b2007-11-30 22:53:43 +0000314 if (LexingRawMode && Diagnostic::isBuiltinNoteWarningOrExtension(DiagID))
Chris Lattner538d7f32006-07-20 04:31:52 +0000315 return;
Chris Lattner02b436a2007-10-17 20:41:00 +0000316 PP->Diag(Loc, DiagID, Msg);
Chris Lattner538d7f32006-07-20 04:31:52 +0000317}
318
Chris Lattner22eb9722006-06-18 05:43:12 +0000319
320//===----------------------------------------------------------------------===//
321// Trigraph and Escaped Newline Handling Code.
322//===----------------------------------------------------------------------===//
323
324/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
325/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
326static char GetTrigraphCharForLetter(char Letter) {
327 switch (Letter) {
328 default: return 0;
329 case '=': return '#';
330 case ')': return ']';
331 case '(': return '[';
332 case '!': return '|';
333 case '\'': return '^';
334 case '>': return '}';
335 case '/': return '\\';
336 case '<': return '{';
337 case '-': return '~';
338 }
339}
340
341/// DecodeTrigraphChar - If the specified character is a legal trigraph when
342/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
343/// return the result character. Finally, emit a warning about trigraph use
344/// whether trigraphs are enabled or not.
345static char DecodeTrigraphChar(const char *CP, Lexer *L) {
346 char Res = GetTrigraphCharForLetter(*CP);
347 if (Res && L) {
348 if (!L->getFeatures().Trigraphs) {
349 L->Diag(CP-2, diag::trigraph_ignored);
350 return 0;
351 } else {
352 L->Diag(CP-2, diag::trigraph_converted, std::string()+Res);
353 }
354 }
355 return Res;
356}
357
358/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
359/// get its size, and return it. This is tricky in several cases:
360/// 1. If currently at the start of a trigraph, we warn about the trigraph,
361/// then either return the trigraph (skipping 3 chars) or the '?',
362/// depending on whether trigraphs are enabled or not.
363/// 2. If this is an escaped newline (potentially with whitespace between
364/// the backslash and newline), implicitly skip the newline and return
365/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000366/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000367///
368/// This handles the slow/uncommon case of the getCharAndSize method. Here we
369/// know that we can accumulate into Size, and that we have already incremented
370/// Ptr by Size bytes.
371///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000372/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
373/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000374///
375char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattner146762e2007-07-20 16:59:19 +0000376 Token *Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000377 // If we have a slash, look for an escaped newline.
378 if (Ptr[0] == '\\') {
379 ++Size;
380 ++Ptr;
381Slash:
382 // Common case, backslash-char where the char is not whitespace.
383 if (!isWhitespace(Ptr[0])) return '\\';
384
385 // See if we have optional whitespace characters followed by a newline.
386 {
387 unsigned SizeTmp = 0;
388 do {
389 ++SizeTmp;
390 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
391 // Remember that this token needs to be cleaned.
Chris Lattner146762e2007-07-20 16:59:19 +0000392 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000393
394 // Warn if there was whitespace between the backslash and newline.
395 if (SizeTmp != 1 && Tok)
396 Diag(Ptr, diag::backslash_newline_space);
397
398 // If this is a \r\n or \n\r, skip the newlines.
399 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
400 Ptr[SizeTmp-1] != Ptr[SizeTmp])
401 ++SizeTmp;
402
403 // Found backslash<whitespace><newline>. Parse the char after it.
404 Size += SizeTmp;
405 Ptr += SizeTmp;
406 // Use slow version to accumulate a correct size field.
407 return getCharAndSizeSlow(Ptr, Size, Tok);
408 }
409 } while (isWhitespace(Ptr[SizeTmp]));
410 }
411
412 // Otherwise, this is not an escaped newline, just return the slash.
413 return '\\';
414 }
415
416 // If this is a trigraph, process it.
417 if (Ptr[0] == '?' && Ptr[1] == '?') {
418 // If this is actually a legal trigraph (not something like "??x"), emit
419 // a trigraph warning. If so, and if trigraphs are enabled, return it.
420 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
421 // Remember that this token needs to be cleaned.
Chris Lattner146762e2007-07-20 16:59:19 +0000422 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000423
424 Ptr += 3;
425 Size += 3;
426 if (C == '\\') goto Slash;
427 return C;
428 }
429 }
430
431 // If this is neither, return a single character.
432 ++Size;
433 return *Ptr;
434}
435
Chris Lattnerd01e2912006-06-18 16:22:51 +0000436
Chris Lattner22eb9722006-06-18 05:43:12 +0000437/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
438/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
439/// and that we have already incremented Ptr by Size bytes.
440///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000441/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
442/// be updated to match.
443char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000444 const LangOptions &Features) {
445 // If we have a slash, look for an escaped newline.
446 if (Ptr[0] == '\\') {
447 ++Size;
448 ++Ptr;
449Slash:
450 // Common case, backslash-char where the char is not whitespace.
451 if (!isWhitespace(Ptr[0])) return '\\';
452
453 // See if we have optional whitespace characters followed by a newline.
454 {
455 unsigned SizeTmp = 0;
456 do {
457 ++SizeTmp;
458 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
459
460 // If this is a \r\n or \n\r, skip the newlines.
461 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
462 Ptr[SizeTmp-1] != Ptr[SizeTmp])
463 ++SizeTmp;
464
465 // Found backslash<whitespace><newline>. Parse the char after it.
466 Size += SizeTmp;
467 Ptr += SizeTmp;
468
469 // Use slow version to accumulate a correct size field.
470 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
471 }
472 } while (isWhitespace(Ptr[SizeTmp]));
473 }
474
475 // Otherwise, this is not an escaped newline, just return the slash.
476 return '\\';
477 }
478
479 // If this is a trigraph, process it.
480 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
481 // If this is actually a legal trigraph (not something like "??x"), return
482 // it.
483 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
484 Ptr += 3;
485 Size += 3;
486 if (C == '\\') goto Slash;
487 return C;
488 }
489 }
490
491 // If this is neither, return a single character.
492 ++Size;
493 return *Ptr;
494}
495
Chris Lattner22eb9722006-06-18 05:43:12 +0000496//===----------------------------------------------------------------------===//
497// Helper methods for lexing.
498//===----------------------------------------------------------------------===//
499
Chris Lattner146762e2007-07-20 16:59:19 +0000500void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000501 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
502 unsigned Size;
503 unsigned char C = *CurPtr++;
504 while (isIdentifierBody(C)) {
505 C = *CurPtr++;
506 }
507 --CurPtr; // Back up over the skipped character.
508
509 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
510 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000511 // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000512 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
513FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000514 const char *IdStart = BufferPtr;
Chris Lattnerd01e2912006-06-18 16:22:51 +0000515 FormTokenWithChars(Result, CurPtr);
Chris Lattner8c204872006-10-14 05:19:21 +0000516 Result.setKind(tok::identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000517
Chris Lattner0f1f5052006-07-20 04:16:23 +0000518 // If we are in raw mode, return this identifier raw. There is no need to
519 // look up identifier information or attempt to macro expand it.
520 if (LexingRawMode) return;
521
Chris Lattnercefc7682006-07-08 08:28:12 +0000522 // Fill in Result.IdentifierInfo, looking up the identifier in the
523 // identifier table.
Chris Lattner02b436a2007-10-17 20:41:00 +0000524 PP->LookUpIdentifierInfo(Result, IdStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000525
Chris Lattnerc5a00062006-06-18 16:41:01 +0000526 // Finally, now that we know we have an identifier, pass this off to the
527 // preprocessor, which may macro expand it or something.
Chris Lattner02b436a2007-10-17 20:41:00 +0000528 return PP->HandleIdentifier(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000529 }
530
531 // Otherwise, $,\,? in identifier found. Enter slower path.
532
533 C = getCharAndSize(CurPtr, Size);
534 while (1) {
535 if (C == '$') {
536 // If we hit a $ and they are not supported in identifiers, we are done.
537 if (!Features.DollarIdents) goto FinishIdentifier;
538
539 // Otherwise, emit a diagnostic and continue.
Chris Lattnercb283342006-06-18 06:48:37 +0000540 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000541 CurPtr = ConsumeChar(CurPtr, Size, Result);
542 C = getCharAndSize(CurPtr, Size);
543 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000544 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000545 // Found end of identifier.
546 goto FinishIdentifier;
547 }
548
549 // Otherwise, this character is good, consume it.
550 CurPtr = ConsumeChar(CurPtr, Size, Result);
551
552 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000553 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000554 CurPtr = ConsumeChar(CurPtr, Size, Result);
555 C = getCharAndSize(CurPtr, Size);
556 }
557 }
558}
559
560
Nate Begeman5eee9332008-04-14 02:26:39 +0000561/// LexNumericConstant - Lex the remainder of a integer or floating point
Chris Lattner22eb9722006-06-18 05:43:12 +0000562/// constant. From[-1] is the first character lexed. Return the end of the
563/// constant.
Chris Lattner146762e2007-07-20 16:59:19 +0000564void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000565 unsigned Size;
566 char C = getCharAndSize(CurPtr, Size);
567 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000568 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000569 CurPtr = ConsumeChar(CurPtr, Size, Result);
570 PrevCh = C;
571 C = getCharAndSize(CurPtr, Size);
572 }
573
574 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
575 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
576 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
577
578 // If we have a hex FP constant, continue.
579 if (Features.HexFloats &&
580 (C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p'))
581 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
582
Chris Lattner8c204872006-10-14 05:19:21 +0000583 Result.setKind(tok::numeric_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000584
Chris Lattnerd01e2912006-06-18 16:22:51 +0000585 // Update the location of token as well as BufferPtr.
586 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000587}
588
589/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
590/// either " or L".
Chris Lattner146762e2007-07-20 16:59:19 +0000591void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide){
Chris Lattner22eb9722006-06-18 05:43:12 +0000592 const char *NulCharacter = 0; // Does this string contain the \0 character?
593
594 char C = getAndAdvanceChar(CurPtr, Result);
595 while (C != '"') {
596 // Skip escaped characters.
597 if (C == '\\') {
598 // Skip the escaped character.
599 C = getAndAdvanceChar(CurPtr, Result);
600 } else if (C == '\n' || C == '\r' || // Newline.
601 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000602 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000603 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000604 FormTokenWithChars(Result, CurPtr-1);
605 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000606 } else if (C == 0) {
607 NulCharacter = CurPtr-1;
608 }
609 C = getAndAdvanceChar(CurPtr, Result);
610 }
611
Chris Lattner5a78a022006-07-20 06:02:19 +0000612 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000613 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000614
Chris Lattner8c204872006-10-14 05:19:21 +0000615 Result.setKind(Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000616
Chris Lattnerd01e2912006-06-18 16:22:51 +0000617 // Update the location of the token as well as the BufferPtr instance var.
618 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000619}
620
621/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
622/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattner146762e2007-07-20 16:59:19 +0000623void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000624 const char *NulCharacter = 0; // Does this string contain the \0 character?
625
626 char C = getAndAdvanceChar(CurPtr, Result);
627 while (C != '>') {
628 // Skip escaped characters.
629 if (C == '\\') {
630 // Skip the escaped character.
631 C = getAndAdvanceChar(CurPtr, Result);
632 } else if (C == '\n' || C == '\r' || // Newline.
633 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000634 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000635 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000636 FormTokenWithChars(Result, CurPtr-1);
637 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000638 } else if (C == 0) {
639 NulCharacter = CurPtr-1;
640 }
641 C = getAndAdvanceChar(CurPtr, Result);
642 }
643
Chris Lattner5a78a022006-07-20 06:02:19 +0000644 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000645 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000646
Chris Lattner8c204872006-10-14 05:19:21 +0000647 Result.setKind(tok::angle_string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000648
Chris Lattnerd01e2912006-06-18 16:22:51 +0000649 // Update the location of token as well as BufferPtr.
650 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000651}
652
653
654/// LexCharConstant - Lex the remainder of a character constant, after having
655/// lexed either ' or L'.
Chris Lattner146762e2007-07-20 16:59:19 +0000656void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000657 const char *NulCharacter = 0; // Does this character contain the \0 character?
658
659 // Handle the common case of 'x' and '\y' efficiently.
660 char C = getAndAdvanceChar(CurPtr, Result);
661 if (C == '\'') {
Chris Lattnera5f4c882006-07-20 06:08:47 +0000662 if (!LexingRawMode) Diag(BufferPtr, diag::err_empty_character);
Chris Lattner8c204872006-10-14 05:19:21 +0000663 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000664 FormTokenWithChars(Result, CurPtr);
665 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000666 } else if (C == '\\') {
667 // Skip the escaped character.
668 // FIXME: UCN's.
669 C = getAndAdvanceChar(CurPtr, Result);
670 }
671
672 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
673 ++CurPtr;
674 } else {
675 // Fall back on generic code for embedded nulls, newlines, wide chars.
676 do {
677 // Skip escaped characters.
678 if (C == '\\') {
679 // Skip the escaped character.
680 C = getAndAdvanceChar(CurPtr, Result);
681 } else if (C == '\n' || C == '\r' || // Newline.
682 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000683 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner8c204872006-10-14 05:19:21 +0000684 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000685 FormTokenWithChars(Result, CurPtr-1);
686 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000687 } else if (C == 0) {
688 NulCharacter = CurPtr-1;
689 }
690 C = getAndAdvanceChar(CurPtr, Result);
691 } while (C != '\'');
692 }
693
Chris Lattnercb283342006-06-18 06:48:37 +0000694 if (NulCharacter) Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000695
Chris Lattner8c204872006-10-14 05:19:21 +0000696 Result.setKind(tok::char_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000697
Chris Lattnerd01e2912006-06-18 16:22:51 +0000698 // Update the location of token as well as BufferPtr.
699 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000700}
701
702/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
703/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattner146762e2007-07-20 16:59:19 +0000704void Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000705 // Whitespace - Skip it, then return the token after the whitespace.
706 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
707 while (1) {
708 // Skip horizontal whitespace very aggressively.
709 while (isHorizontalWhitespace(Char))
710 Char = *++CurPtr;
711
712 // Otherwise if we something other than whitespace, we're done.
713 if (Char != '\n' && Char != '\r')
714 break;
715
716 if (ParsingPreprocessorDirective) {
717 // End of preprocessor directive line, let LexTokenInternal handle this.
718 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000719 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000720 }
721
722 // ok, but handle newline.
723 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +0000724 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000725 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +0000726 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000727 Char = *++CurPtr;
728 }
729
730 // If this isn't immediately after a newline, there is leading space.
731 char PrevChar = CurPtr[-1];
732 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattner146762e2007-07-20 16:59:19 +0000733 Result.setFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000734
Chris Lattner22eb9722006-06-18 05:43:12 +0000735 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000736}
737
738// SkipBCPLComment - We have just read the // characters from input. Skip until
739// we find the newline character thats terminate the comment. Then update
740/// BufferPtr and return.
Chris Lattner146762e2007-07-20 16:59:19 +0000741bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000742 // If BCPL comments aren't explicitly enabled for this language, emit an
743 // extension warning.
744 if (!Features.BCPLComment) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000745 Diag(BufferPtr, diag::ext_bcpl_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000746
747 // Mark them enabled so we only emit one warning for this translation
748 // unit.
749 Features.BCPLComment = true;
750 }
751
752 // Scan over the body of the comment. The common case, when scanning, is that
753 // the comment contains normal ascii characters with nothing interesting in
754 // them. As such, optimize for this case with the inner loop.
755 char C;
756 do {
757 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +0000758 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
759 // If we find a \n character, scan backwards, checking to see if it's an
760 // escaped newline, like we do for block comments.
Chris Lattner22eb9722006-06-18 05:43:12 +0000761
762 // Skip over characters in the fast loop.
763 while (C != 0 && // Potentially EOF.
764 C != '\\' && // Potentially escaped newline.
765 C != '?' && // Potentially trigraph.
766 C != '\n' && C != '\r') // Newline or DOS-style newline.
767 C = *++CurPtr;
768
769 // If this is a newline, we're done.
770 if (C == '\n' || C == '\r')
771 break; // Found the newline? Break out!
772
773 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
774 // properly decode the character.
775 const char *OldPtr = CurPtr;
776 C = getAndAdvanceChar(CurPtr, Result);
777
778 // If we read multiple characters, and one of those characters was a \r or
Chris Lattnerff591e22007-06-09 06:07:22 +0000779 // \n, then we had an escaped newline within the comment. Emit diagnostic
780 // unless the next line is also a // comment.
781 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000782 for (; OldPtr != CurPtr; ++OldPtr)
783 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnerff591e22007-06-09 06:07:22 +0000784 // Okay, we found a // comment that ends in a newline, if the next
785 // line is also a // comment, but has spaces, don't emit a diagnostic.
786 if (isspace(C)) {
787 const char *ForwardPtr = CurPtr;
788 while (isspace(*ForwardPtr)) // Skip whitespace.
789 ++ForwardPtr;
790 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
791 break;
792 }
793
Chris Lattnercb283342006-06-18 06:48:37 +0000794 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
795 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000796 }
797 }
798
Chris Lattner457fc152006-07-29 06:30:25 +0000799 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
Chris Lattner22eb9722006-06-18 05:43:12 +0000800 } while (C != '\n' && C != '\r');
801
Chris Lattner457fc152006-07-29 06:30:25 +0000802 // Found but did not consume the newline.
803
804 // If we are returning comments as tokens, return this comment as a token.
805 if (KeepCommentMode)
806 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000807
808 // If we are inside a preprocessor directive and we see the end of line,
809 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +0000810 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000811 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000812 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000813 }
814
815 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner87e97ea2008-10-12 00:23:07 +0000816 // \r\n sequence. This is an efficiency hack (because we know the \n can't
817 // contribute to another token), it isn't needed for correctness.
Chris Lattner22eb9722006-06-18 05:43:12 +0000818 ++CurPtr;
819
820 // The next returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +0000821 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000822 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +0000823 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000824 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000825 return true;
826}
Chris Lattner22eb9722006-06-18 05:43:12 +0000827
Chris Lattner457fc152006-07-29 06:30:25 +0000828/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
829/// an appropriate way and return it.
Chris Lattner146762e2007-07-20 16:59:19 +0000830bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner8c204872006-10-14 05:19:21 +0000831 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +0000832 FormTokenWithChars(Result, CurPtr);
833
834 // If this BCPL-style comment is in a macro definition, transmogrify it into
835 // a C-style block comment.
836 if (ParsingPreprocessorDirective) {
Chris Lattner02b436a2007-10-17 20:41:00 +0000837 std::string Spelling = PP->getSpelling(Result);
Chris Lattner457fc152006-07-29 06:30:25 +0000838 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
839 Spelling[1] = '*'; // Change prefix to "/*".
840 Spelling += "*/"; // add suffix.
841
Chris Lattner02b436a2007-10-17 20:41:00 +0000842 Result.setLocation(PP->CreateString(&Spelling[0], Spelling.size(),
843 Result.getLocation()));
Chris Lattner8c204872006-10-14 05:19:21 +0000844 Result.setLength(Spelling.size());
Chris Lattner457fc152006-07-29 06:30:25 +0000845 }
846 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000847}
848
Chris Lattnercb283342006-06-18 06:48:37 +0000849/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
850/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner22eb9722006-06-18 05:43:12 +0000851/// diagnostic if so. We know that the is inside of a block comment.
Chris Lattner1f583052006-06-18 06:53:56 +0000852static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
853 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000854 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Chris Lattner22eb9722006-06-18 05:43:12 +0000855
856 // Back up off the newline.
857 --CurPtr;
858
859 // If this is a two-character newline sequence, skip the other character.
860 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
861 // \n\n or \r\r -> not escaped newline.
862 if (CurPtr[0] == CurPtr[1])
863 return false;
864 // \n\r or \r\n -> skip the newline.
865 --CurPtr;
866 }
867
868 // If we have horizontal whitespace, skip over it. We allow whitespace
869 // between the slash and newline.
870 bool HasSpace = false;
871 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
872 --CurPtr;
873 HasSpace = true;
874 }
875
876 // If we have a slash, we know this is an escaped newline.
877 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +0000878 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000879 } else {
880 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +0000881 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
882 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +0000883 return false;
Chris Lattnercb283342006-06-18 06:48:37 +0000884
885 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +0000886 CurPtr -= 2;
887
888 // If no trigraphs are enabled, warn that we ignored this trigraph and
889 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +0000890 if (!L->getFeatures().Trigraphs) {
891 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000892 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000893 }
Chris Lattner1f583052006-06-18 06:53:56 +0000894 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000895 }
896
897 // Warn about having an escaped newline between the */ characters.
Chris Lattner1f583052006-06-18 06:53:56 +0000898 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Chris Lattner22eb9722006-06-18 05:43:12 +0000899
900 // If there was space between the backslash and newline, warn about it.
Chris Lattner1f583052006-06-18 06:53:56 +0000901 if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space);
Chris Lattner22eb9722006-06-18 05:43:12 +0000902
Chris Lattnercb283342006-06-18 06:48:37 +0000903 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000904}
905
Chris Lattneraded4a92006-10-27 04:42:31 +0000906#ifdef __SSE2__
907#include <emmintrin.h>
Chris Lattner9f6604f2006-10-30 20:01:22 +0000908#elif __ALTIVEC__
909#include <altivec.h>
910#undef bool
Chris Lattneraded4a92006-10-27 04:42:31 +0000911#endif
912
Chris Lattner22eb9722006-06-18 05:43:12 +0000913/// SkipBlockComment - We have just read the /* characters from input. Read
914/// until we find the */ characters that terminate the comment. Note that we
915/// don't bother decoding trigraphs or escaped newlines in block comments,
916/// because they cannot cause the comment to end. The only thing that can
917/// happen is the comment could end with an escaped newline between the */ end
918/// of comment.
Chris Lattner146762e2007-07-20 16:59:19 +0000919bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000920 // Scan one character past where we should, looking for a '/' character. Once
921 // we find it, check to see if it was preceeded by a *. This common
922 // optimization helps people who like to put a lot of * characters in their
923 // comments.
Chris Lattnerc850ad62007-07-21 23:43:37 +0000924
925 // The first character we get with newlines and trigraphs skipped to handle
926 // the degenerate /*/ case below correctly if the * has an escaped newline
927 // after it.
928 unsigned CharSize;
929 unsigned char C = getCharAndSize(CurPtr, CharSize);
930 CurPtr += CharSize;
Chris Lattner22eb9722006-06-18 05:43:12 +0000931 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000932 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000933 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +0000934 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000935 }
936
Chris Lattnerc850ad62007-07-21 23:43:37 +0000937 // Check to see if the first character after the '/*' is another /. If so,
938 // then this slash does not end the block comment, it is part of it.
939 if (C == '/')
940 C = *CurPtr++;
941
Chris Lattner22eb9722006-06-18 05:43:12 +0000942 while (1) {
Chris Lattner6cc3e362006-10-27 04:12:35 +0000943 // Skip over all non-interesting characters until we find end of buffer or a
944 // (probably ending) '/' character.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000945 if (CurPtr + 24 < BufferEnd) {
946 // While not aligned to a 16-byte boundary.
947 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
948 C = *CurPtr++;
949
950 if (C == '/') goto FoundSlash;
Chris Lattneraded4a92006-10-27 04:42:31 +0000951
952#ifdef __SSE2__
953 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
954 '/', '/', '/', '/', '/', '/', '/', '/');
955 while (CurPtr+16 <= BufferEnd &&
956 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
957 CurPtr += 16;
Chris Lattner9f6604f2006-10-30 20:01:22 +0000958#elif __ALTIVEC__
959 __vector unsigned char Slashes = {
960 '/', '/', '/', '/', '/', '/', '/', '/',
961 '/', '/', '/', '/', '/', '/', '/', '/'
962 };
963 while (CurPtr+16 <= BufferEnd &&
964 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
965 CurPtr += 16;
966#else
Chris Lattneraded4a92006-10-27 04:42:31 +0000967 // Scan for '/' quickly. Many block comments are very large.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000968 while (CurPtr[0] != '/' &&
969 CurPtr[1] != '/' &&
970 CurPtr[2] != '/' &&
971 CurPtr[3] != '/' &&
972 CurPtr+4 < BufferEnd) {
973 CurPtr += 4;
974 }
Chris Lattneraded4a92006-10-27 04:42:31 +0000975#endif
976
977 // It has to be one of the bytes scanned, increment to it and read one.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000978 C = *CurPtr++;
979 }
980
Chris Lattneraded4a92006-10-27 04:42:31 +0000981 // Loop to scan the remainder.
Chris Lattner22eb9722006-06-18 05:43:12 +0000982 while (C != '/' && C != '\0')
983 C = *CurPtr++;
984
Chris Lattner6cc3e362006-10-27 04:12:35 +0000985 FoundSlash:
Chris Lattner22eb9722006-06-18 05:43:12 +0000986 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000987 if (CurPtr[-2] == '*') // We found the final */. We're done!
988 break;
989
990 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +0000991 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000992 // We found the final */, though it had an escaped newline between the
993 // * and /. We're done!
994 break;
995 }
996 }
997 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
998 // If this is a /* inside of the comment, emit a warning. Don't do this
999 // if this is a /*/, which will end the comment. This misses cases with
1000 // embedded escaped newlines, but oh well.
Chris Lattnercb283342006-06-18 06:48:37 +00001001 Diag(CurPtr-1, diag::nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001002 }
1003 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +00001004 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001005 // Note: the user probably forgot a */. We could continue immediately
1006 // after the /*, but this would involve lexing a lot of what really is the
1007 // comment, which surely would confuse the parser.
1008 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +00001009 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001010 }
1011 C = *CurPtr++;
1012 }
Chris Lattner457fc152006-07-29 06:30:25 +00001013
1014 // If we are returning comments as tokens, return this comment as a token.
1015 if (KeepCommentMode) {
Chris Lattner8c204872006-10-14 05:19:21 +00001016 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +00001017 FormTokenWithChars(Result, CurPtr);
1018 return false;
1019 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001020
1021 // It is common for the tokens immediately after a /**/ comment to be
1022 // whitespace. Instead of going through the big switch, handle it
1023 // efficiently now.
1024 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner146762e2007-07-20 16:59:19 +00001025 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +00001026 SkipWhitespace(Result, CurPtr+1);
1027 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001028 }
1029
1030 // Otherwise, just return so that the next character will be lexed as a token.
1031 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001032 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +00001033 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001034}
1035
1036//===----------------------------------------------------------------------===//
1037// Primary Lexing Entry Points
1038//===----------------------------------------------------------------------===//
1039
1040/// LexIncludeFilename - After the preprocessor has parsed a #include, lex and
1041/// (potentially) macro expand the filename.
Chris Lattner146762e2007-07-20 16:59:19 +00001042void Lexer::LexIncludeFilename(Token &FilenameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001043 assert(ParsingPreprocessorDirective &&
1044 ParsingFilename == false &&
1045 "Must be in a preprocessing directive!");
1046
1047 // We are now parsing a filename!
1048 ParsingFilename = true;
1049
Chris Lattner269c2322006-06-25 06:23:00 +00001050 // Lex the filename.
1051 Lex(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001052
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001053 // We should have obtained the filename now.
Chris Lattner22eb9722006-06-18 05:43:12 +00001054 ParsingFilename = false;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001055
Chris Lattner22eb9722006-06-18 05:43:12 +00001056 // No filename?
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001057 if (FilenameTok.is(tok::eom))
Chris Lattner538d7f32006-07-20 04:31:52 +00001058 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner22eb9722006-06-18 05:43:12 +00001059}
1060
1061/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1062/// uninterpreted string. This switches the lexer out of directive mode.
1063std::string Lexer::ReadToEndOfLine() {
1064 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1065 "Must be in a preprocessing directive!");
1066 std::string Result;
Chris Lattner146762e2007-07-20 16:59:19 +00001067 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001068
1069 // CurPtr - Cache BufferPtr in an automatic variable.
1070 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001071 while (1) {
1072 char Char = getAndAdvanceChar(CurPtr, Tmp);
1073 switch (Char) {
1074 default:
1075 Result += Char;
1076 break;
1077 case 0: // Null.
1078 // Found end of file?
1079 if (CurPtr-1 != BufferEnd) {
1080 // Nope, normal character, continue.
1081 Result += Char;
1082 break;
1083 }
1084 // FALL THROUGH.
1085 case '\r':
1086 case '\n':
1087 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1088 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1089 BufferPtr = CurPtr-1;
1090
1091 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +00001092 Lex(Tmp);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001093 assert(Tmp.is(tok::eom) && "Unexpected token!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001094
1095 // Finally, we're done, return the string we found.
1096 return Result;
1097 }
1098 }
1099}
1100
1101/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1102/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +00001103/// This returns true if Result contains a token, false if PP.Lex should be
1104/// called again.
Chris Lattner146762e2007-07-20 16:59:19 +00001105bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001106 // If we hit the end of the file while parsing a preprocessor directive,
1107 // end the preprocessor directive first. The next token returned will
1108 // then be the end of file.
1109 if (ParsingPreprocessorDirective) {
1110 // Done parsing the "line".
1111 ParsingPreprocessorDirective = false;
Chris Lattner8c204872006-10-14 05:19:21 +00001112 Result.setKind(tok::eom);
Chris Lattnerd01e2912006-06-18 16:22:51 +00001113 // Update the location of token as well as BufferPtr.
1114 FormTokenWithChars(Result, CurPtr);
Chris Lattner457fc152006-07-29 06:30:25 +00001115
1116 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner02b436a2007-10-17 20:41:00 +00001117 KeepCommentMode = PP->getCommentRetentionState();
Chris Lattner2183a6e2006-07-18 06:36:12 +00001118 return true; // Have a token.
Chris Lattner22eb9722006-06-18 05:43:12 +00001119 }
1120
Chris Lattner30a2fa12006-07-19 06:31:49 +00001121 // If we are in raw mode, return this event as an EOF token. Let the caller
1122 // that put us in raw mode handle the event.
1123 if (LexingRawMode) {
Chris Lattner8c204872006-10-14 05:19:21 +00001124 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +00001125 BufferPtr = BufferEnd;
1126 FormTokenWithChars(Result, BufferEnd);
Chris Lattner8c204872006-10-14 05:19:21 +00001127 Result.setKind(tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001128 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001129 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001130
Chris Lattner30a2fa12006-07-19 06:31:49 +00001131 // Otherwise, issue diagnostics for unterminated #if and missing newline.
1132
1133 // If we are in a #if directive, emit an error.
1134 while (!ConditionalStack.empty()) {
Chris Lattner538d7f32006-07-20 04:31:52 +00001135 Diag(ConditionalStack.back().IfLoc, diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001136 ConditionalStack.pop_back();
1137 }
1138
Chris Lattner8f96d042008-04-12 05:54:25 +00001139 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1140 // a pedwarn.
1141 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Chris Lattner30a2fa12006-07-19 06:31:49 +00001142 Diag(BufferEnd, diag::ext_no_newline_eof);
1143
Chris Lattner22eb9722006-06-18 05:43:12 +00001144 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +00001145
1146 // Finally, let the preprocessor handle this.
Chris Lattner02b436a2007-10-17 20:41:00 +00001147 return PP->HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001148}
1149
Chris Lattner678c8802006-07-11 05:46:12 +00001150/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1151/// the specified lexer will return a tok::l_paren token, 0 if it is something
1152/// else and 2 if there are no more tokens in the buffer controlled by the
1153/// lexer.
1154unsigned Lexer::isNextPPTokenLParen() {
1155 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1156
1157 // Switch to 'skipping' mode. This will ensure that we can lex a token
1158 // without emitting diagnostics, disables macro expansion, and will cause EOF
1159 // to return an EOF token instead of popping the include stack.
1160 LexingRawMode = true;
1161
1162 // Save state that can be changed while lexing so that we can restore it.
1163 const char *TmpBufferPtr = BufferPtr;
1164
Chris Lattner146762e2007-07-20 16:59:19 +00001165 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001166 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001167 LexTokenInternal(Tok);
1168
1169 // Restore state that may have changed.
1170 BufferPtr = TmpBufferPtr;
1171
1172 // Restore the lexer back to non-skipping mode.
1173 LexingRawMode = false;
1174
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001175 if (Tok.is(tok::eof))
Chris Lattner678c8802006-07-11 05:46:12 +00001176 return 2;
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001177 return Tok.is(tok::l_paren);
Chris Lattner678c8802006-07-11 05:46:12 +00001178}
1179
Chris Lattner22eb9722006-06-18 05:43:12 +00001180
1181/// LexTokenInternal - This implements a simple C family lexer. It is an
1182/// extremely performance critical piece of code. This assumes that the buffer
1183/// has a null character at the end of the file. Return true if an error
1184/// occurred and compilation should terminate, false if normal. This returns a
1185/// preprocessing token, not a normal token, as such, it is an internal
1186/// interface. It assumes that the Flags of result have been cleared before
1187/// calling this.
Chris Lattner146762e2007-07-20 16:59:19 +00001188void Lexer::LexTokenInternal(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001189LexNextToken:
1190 // New token, can't need cleaning yet.
Chris Lattner146762e2007-07-20 16:59:19 +00001191 Result.clearFlag(Token::NeedsCleaning);
Chris Lattner8c204872006-10-14 05:19:21 +00001192 Result.setIdentifierInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001193
1194 // CurPtr - Cache BufferPtr in an automatic variable.
1195 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001196
Chris Lattnereb54b592006-07-10 06:34:27 +00001197 // Small amounts of horizontal whitespace is very common between tokens.
1198 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1199 ++CurPtr;
1200 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1201 ++CurPtr;
1202 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001203 Result.setFlag(Token::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001204 }
1205
Chris Lattner22eb9722006-06-18 05:43:12 +00001206 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1207
1208 // Read a character, advancing over it.
1209 char Char = getAndAdvanceChar(CurPtr, Result);
1210 switch (Char) {
1211 case 0: // Null.
1212 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001213 if (CurPtr-1 == BufferEnd) {
1214 // Read the PP instance variable into an automatic variable, because
1215 // LexEndOfFile will often delete 'this'.
Chris Lattner02b436a2007-10-17 20:41:00 +00001216 Preprocessor *PPCache = PP;
Chris Lattner2183a6e2006-07-18 06:36:12 +00001217 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1218 return; // Got a token to return.
Chris Lattner02b436a2007-10-17 20:41:00 +00001219 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1220 return PPCache->Lex(Result);
Chris Lattner2183a6e2006-07-18 06:36:12 +00001221 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001222
Chris Lattnercb283342006-06-18 06:48:37 +00001223 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner146762e2007-07-20 16:59:19 +00001224 Result.setFlag(Token::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001225 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001226 goto LexNextToken; // GCC isn't tail call eliminating.
1227 case '\n':
1228 case '\r':
1229 // If we are inside a preprocessor directive and we see the end of line,
1230 // we know we are done with the directive, so return an EOM token.
1231 if (ParsingPreprocessorDirective) {
1232 // Done parsing the "line".
1233 ParsingPreprocessorDirective = false;
1234
Chris Lattner457fc152006-07-29 06:30:25 +00001235 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner02b436a2007-10-17 20:41:00 +00001236 KeepCommentMode = PP->getCommentRetentionState();
Chris Lattner457fc152006-07-29 06:30:25 +00001237
Chris Lattner22eb9722006-06-18 05:43:12 +00001238 // Since we consumed a newline, we are back at the start of a line.
1239 IsAtStartOfLine = true;
1240
Chris Lattner8c204872006-10-14 05:19:21 +00001241 Result.setKind(tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001242 break;
1243 }
1244 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001245 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001246 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001247 Result.clearFlag(Token::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001248 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001249 goto LexNextToken; // GCC isn't tail call eliminating.
1250 case ' ':
1251 case '\t':
1252 case '\f':
1253 case '\v':
Chris Lattnerb9b85972007-07-22 06:29:05 +00001254 SkipHorizontalWhitespace:
Chris Lattner146762e2007-07-20 16:59:19 +00001255 Result.setFlag(Token::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001256 SkipWhitespace(Result, CurPtr);
Chris Lattnerb9b85972007-07-22 06:29:05 +00001257
1258 SkipIgnoredUnits:
1259 CurPtr = BufferPtr;
1260
1261 // If the next token is obviously a // or /* */ comment, skip it efficiently
1262 // too (without going through the big switch stmt).
1263 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !KeepCommentMode) {
1264 SkipBCPLComment(Result, CurPtr+2);
1265 goto SkipIgnoredUnits;
1266 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !KeepCommentMode) {
1267 SkipBlockComment(Result, CurPtr+2);
1268 goto SkipIgnoredUnits;
1269 } else if (isHorizontalWhitespace(*CurPtr)) {
1270 goto SkipHorizontalWhitespace;
1271 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001272 goto LexNextToken; // GCC isn't tail call eliminating.
1273
Chris Lattner2b15cf72008-01-03 17:58:54 +00001274 // C99 6.4.4.1: Integer Constants.
1275 // C99 6.4.4.2: Floating Constants.
1276 case '0': case '1': case '2': case '3': case '4':
1277 case '5': case '6': case '7': case '8': case '9':
1278 // Notify MIOpt that we read a non-whitespace/non-comment token.
1279 MIOpt.ReadToken();
1280 return LexNumericConstant(Result, CurPtr);
1281
1282 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Chris Lattner371ac8a2006-07-04 07:11:10 +00001283 // Notify MIOpt that we read a non-whitespace/non-comment token.
1284 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001285 Char = getCharAndSize(CurPtr, SizeTmp);
1286
1287 // Wide string literal.
1288 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001289 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1290 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001291
1292 // Wide character constant.
1293 if (Char == '\'')
1294 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1295 // FALL THROUGH, treating L like the start of an identifier.
1296
1297 // C99 6.4.2: Identifiers.
1298 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1299 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1300 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1301 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1302 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1303 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1304 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1305 case 'v': case 'w': case 'x': case 'y': case 'z':
1306 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001307 // Notify MIOpt that we read a non-whitespace/non-comment token.
1308 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001309 return LexIdentifier(Result, CurPtr);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001310
1311 case '$': // $ in identifiers.
1312 if (Features.DollarIdents) {
1313 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
1314 // Notify MIOpt that we read a non-whitespace/non-comment token.
1315 MIOpt.ReadToken();
1316 return LexIdentifier(Result, CurPtr);
1317 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001318
Chris Lattner2b15cf72008-01-03 17:58:54 +00001319 Result.setKind(tok::unknown);
1320 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001321
1322 // C99 6.4.4: Character Constants.
1323 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001324 // Notify MIOpt that we read a non-whitespace/non-comment token.
1325 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001326 return LexCharConstant(Result, CurPtr);
1327
1328 // C99 6.4.5: String Literals.
1329 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001330 // Notify MIOpt that we read a non-whitespace/non-comment token.
1331 MIOpt.ReadToken();
Chris Lattnerd3e98952006-10-06 05:22:26 +00001332 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001333
1334 // C99 6.4.6: Punctuators.
1335 case '?':
Chris Lattner8c204872006-10-14 05:19:21 +00001336 Result.setKind(tok::question);
Chris Lattner22eb9722006-06-18 05:43:12 +00001337 break;
1338 case '[':
Chris Lattner8c204872006-10-14 05:19:21 +00001339 Result.setKind(tok::l_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001340 break;
1341 case ']':
Chris Lattner8c204872006-10-14 05:19:21 +00001342 Result.setKind(tok::r_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001343 break;
1344 case '(':
Chris Lattner8c204872006-10-14 05:19:21 +00001345 Result.setKind(tok::l_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001346 break;
1347 case ')':
Chris Lattner8c204872006-10-14 05:19:21 +00001348 Result.setKind(tok::r_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001349 break;
1350 case '{':
Chris Lattner8c204872006-10-14 05:19:21 +00001351 Result.setKind(tok::l_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001352 break;
1353 case '}':
Chris Lattner8c204872006-10-14 05:19:21 +00001354 Result.setKind(tok::r_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001355 break;
1356 case '.':
1357 Char = getCharAndSize(CurPtr, SizeTmp);
1358 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001359 // Notify MIOpt that we read a non-whitespace/non-comment token.
1360 MIOpt.ReadToken();
1361
Chris Lattner22eb9722006-06-18 05:43:12 +00001362 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1363 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001364 Result.setKind(tok::periodstar);
Chris Lattner22eb9722006-06-18 05:43:12 +00001365 CurPtr += SizeTmp;
1366 } else if (Char == '.' &&
1367 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner8c204872006-10-14 05:19:21 +00001368 Result.setKind(tok::ellipsis);
Chris Lattner22eb9722006-06-18 05:43:12 +00001369 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1370 SizeTmp2, Result);
1371 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001372 Result.setKind(tok::period);
Chris Lattner22eb9722006-06-18 05:43:12 +00001373 }
1374 break;
1375 case '&':
1376 Char = getCharAndSize(CurPtr, SizeTmp);
1377 if (Char == '&') {
Chris Lattner8c204872006-10-14 05:19:21 +00001378 Result.setKind(tok::ampamp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001379 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1380 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001381 Result.setKind(tok::ampequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001382 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1383 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001384 Result.setKind(tok::amp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001385 }
1386 break;
1387 case '*':
1388 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001389 Result.setKind(tok::starequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001390 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1391 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001392 Result.setKind(tok::star);
Chris Lattner22eb9722006-06-18 05:43:12 +00001393 }
1394 break;
1395 case '+':
1396 Char = getCharAndSize(CurPtr, SizeTmp);
1397 if (Char == '+') {
Chris Lattner8c204872006-10-14 05:19:21 +00001398 Result.setKind(tok::plusplus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001399 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1400 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001401 Result.setKind(tok::plusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001402 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1403 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001404 Result.setKind(tok::plus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001405 }
1406 break;
1407 case '-':
1408 Char = getCharAndSize(CurPtr, SizeTmp);
1409 if (Char == '-') {
Chris Lattner8c204872006-10-14 05:19:21 +00001410 Result.setKind(tok::minusminus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001411 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1412 } else if (Char == '>' && Features.CPlusPlus &&
1413 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001414 Result.setKind(tok::arrowstar); // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00001415 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1416 SizeTmp2, Result);
1417 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001418 Result.setKind(tok::arrow);
Chris Lattner22eb9722006-06-18 05:43:12 +00001419 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1420 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001421 Result.setKind(tok::minusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001422 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1423 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001424 Result.setKind(tok::minus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001425 }
1426 break;
1427 case '~':
Chris Lattner8c204872006-10-14 05:19:21 +00001428 Result.setKind(tok::tilde);
Chris Lattner22eb9722006-06-18 05:43:12 +00001429 break;
1430 case '!':
1431 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001432 Result.setKind(tok::exclaimequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001433 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1434 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001435 Result.setKind(tok::exclaim);
Chris Lattner22eb9722006-06-18 05:43:12 +00001436 }
1437 break;
1438 case '/':
1439 // 6.4.9: Comments
1440 Char = getCharAndSize(CurPtr, SizeTmp);
1441 if (Char == '/') { // BCPL comment.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001442 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) {
1443 // It is common for the tokens immediately after a // comment to be
Chris Lattner619c1742007-07-22 18:38:25 +00001444 // whitespace (indentation for the next line). Instead of going through
1445 // the big switch, handle it efficiently now.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001446 goto SkipIgnoredUnits;
1447 }
Chris Lattner457fc152006-07-29 06:30:25 +00001448 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001449 } else if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001450 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1451 goto LexNextToken; // GCC isn't tail call eliminating.
1452 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001453 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001454 Result.setKind(tok::slashequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001455 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1456 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001457 Result.setKind(tok::slash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001458 }
1459 break;
1460 case '%':
1461 Char = getCharAndSize(CurPtr, SizeTmp);
1462 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001463 Result.setKind(tok::percentequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001464 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1465 } else if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001466 Result.setKind(tok::r_brace); // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00001467 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1468 } else if (Features.Digraphs && Char == ':') {
1469 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001470 Char = getCharAndSize(CurPtr, SizeTmp);
1471 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001472 Result.setKind(tok::hashhash); // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00001473 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1474 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001475 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001476 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001477 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1478 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner22eb9722006-06-18 05:43:12 +00001479 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001480 Result.setKind(tok::hash); // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00001481
1482 // We parsed a # character. If this occurs at the start of the line,
1483 // it's actually the start of a preprocessing directive. Callback to
1484 // the preprocessor to handle it.
1485 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001486 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001487 BufferPtr = CurPtr;
Chris Lattner02b436a2007-10-17 20:41:00 +00001488 PP->HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001489
1490 // As an optimization, if the preprocessor didn't switch lexers, tail
1491 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00001492 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001493 // Start a new token. If this is a #include or something, the PP may
1494 // want us starting at the beginning of the line again. If so, set
1495 // the StartOfLine flag.
1496 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00001497 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001498 IsAtStartOfLine = false;
1499 }
1500 goto LexNextToken; // GCC isn't tail call eliminating.
1501 }
1502
Chris Lattner02b436a2007-10-17 20:41:00 +00001503 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001504 }
1505 }
1506 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001507 Result.setKind(tok::percent);
Chris Lattner22eb9722006-06-18 05:43:12 +00001508 }
1509 break;
1510 case '<':
1511 Char = getCharAndSize(CurPtr, SizeTmp);
1512 if (ParsingFilename) {
1513 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1514 } else if (Char == '<' &&
1515 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001516 Result.setKind(tok::lesslessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001517 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1518 SizeTmp2, Result);
1519 } else if (Char == '<') {
Chris Lattner8c204872006-10-14 05:19:21 +00001520 Result.setKind(tok::lessless);
Chris Lattner22eb9722006-06-18 05:43:12 +00001521 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1522 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001523 Result.setKind(tok::lessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001524 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1525 } else if (Features.Digraphs && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001526 Result.setKind(tok::l_square); // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00001527 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner5329e7e2008-02-24 19:05:57 +00001528 } else if (Features.Digraphs && Char == '%') {
Chris Lattner8c204872006-10-14 05:19:21 +00001529 Result.setKind(tok::l_brace); // '<%' -> '{'
Chris Lattner22eb9722006-06-18 05:43:12 +00001530 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001531 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001532 Result.setKind(tok::less);
Chris Lattner22eb9722006-06-18 05:43:12 +00001533 }
1534 break;
1535 case '>':
1536 Char = getCharAndSize(CurPtr, SizeTmp);
1537 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001538 Result.setKind(tok::greaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001539 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1540 } else if (Char == '>' &&
1541 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001542 Result.setKind(tok::greatergreaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001543 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1544 SizeTmp2, Result);
1545 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001546 Result.setKind(tok::greatergreater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001547 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001548 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001549 Result.setKind(tok::greater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001550 }
1551 break;
1552 case '^':
1553 Char = getCharAndSize(CurPtr, SizeTmp);
1554 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001555 Result.setKind(tok::caretequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001556 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1557 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001558 Result.setKind(tok::caret);
Chris Lattner22eb9722006-06-18 05:43:12 +00001559 }
1560 break;
1561 case '|':
1562 Char = getCharAndSize(CurPtr, SizeTmp);
1563 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001564 Result.setKind(tok::pipeequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001565 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1566 } else if (Char == '|') {
Chris Lattner8c204872006-10-14 05:19:21 +00001567 Result.setKind(tok::pipepipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001568 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1569 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001570 Result.setKind(tok::pipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001571 }
1572 break;
1573 case ':':
1574 Char = getCharAndSize(CurPtr, SizeTmp);
1575 if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001576 Result.setKind(tok::r_square); // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00001577 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1578 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001579 Result.setKind(tok::coloncolon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001580 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1581 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001582 Result.setKind(tok::colon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001583 }
1584 break;
1585 case ';':
Chris Lattner8c204872006-10-14 05:19:21 +00001586 Result.setKind(tok::semi);
Chris Lattner22eb9722006-06-18 05:43:12 +00001587 break;
1588 case '=':
1589 Char = getCharAndSize(CurPtr, SizeTmp);
1590 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001591 Result.setKind(tok::equalequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001592 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1593 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001594 Result.setKind(tok::equal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001595 }
1596 break;
1597 case ',':
Chris Lattner8c204872006-10-14 05:19:21 +00001598 Result.setKind(tok::comma);
Chris Lattner22eb9722006-06-18 05:43:12 +00001599 break;
1600 case '#':
1601 Char = getCharAndSize(CurPtr, SizeTmp);
1602 if (Char == '#') {
Chris Lattner8c204872006-10-14 05:19:21 +00001603 Result.setKind(tok::hashhash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001604 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001605 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001606 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001607 Diag(BufferPtr, diag::charize_microsoft_ext);
1608 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001609 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001610 Result.setKind(tok::hash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001611 // We parsed a # character. If this occurs at the start of the line,
1612 // it's actually the start of a preprocessing directive. Callback to
1613 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00001614 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001615 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001616 BufferPtr = CurPtr;
Chris Lattner02b436a2007-10-17 20:41:00 +00001617 PP->HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001618
1619 // As an optimization, if the preprocessor didn't switch lexers, tail
1620 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00001621 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001622 // Start a new token. If this is a #include or something, the PP may
1623 // want us starting at the beginning of the line again. If so, set
1624 // the StartOfLine flag.
1625 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00001626 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001627 IsAtStartOfLine = false;
1628 }
1629 goto LexNextToken; // GCC isn't tail call eliminating.
1630 }
Chris Lattner02b436a2007-10-17 20:41:00 +00001631 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001632 }
1633 }
1634 break;
1635
Chris Lattner2b15cf72008-01-03 17:58:54 +00001636 case '@':
1637 // Objective C support.
1638 if (CurPtr[-1] == '@' && Features.ObjC1)
1639 Result.setKind(tok::at);
1640 else
1641 Result.setKind(tok::unknown);
1642 break;
1643
Chris Lattner22eb9722006-06-18 05:43:12 +00001644 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00001645 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00001646 // FALL THROUGH.
1647 default:
Chris Lattner8c204872006-10-14 05:19:21 +00001648 Result.setKind(tok::unknown);
Chris Lattner041bef82006-07-11 05:52:53 +00001649 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001650 }
1651
Chris Lattner371ac8a2006-07-04 07:11:10 +00001652 // Notify MIOpt that we read a non-whitespace/non-comment token.
1653 MIOpt.ReadToken();
1654
Chris Lattnerd01e2912006-06-18 16:22:51 +00001655 // Update the location of token as well as BufferPtr.
1656 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001657}