blob: a259b2aacf940959b612a9d7d3070819061485e9 [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
Chris Lattner50c90502008-10-12 01:15:46 +0000107/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
108/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner02b436a2007-10-17 20:41:00 +0000109Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattner50c90502008-10-12 01:15:46 +0000110 const char *BufStart, const char *BufEnd,
111 const llvm::MemoryBuffer *FromFile)
Chris Lattner02b436a2007-10-17 20:41:00 +0000112 : FileLoc(fileloc), PP(0), Features(features) {
113 Is_PragmaLexer = false;
114 InitCharacterInfo();
115
Chris Lattner6b0c5ad2008-10-12 01:23:27 +0000116 // If a MemoryBuffer was specified, use its start as BufferStart. This affects
117 // the source location objects produced by this lexer.
Chris Lattner50c90502008-10-12 01:15:46 +0000118 BufferStart = FromFile ? FromFile->getBufferStart() : BufStart;
Chris Lattner02b436a2007-10-17 20:41:00 +0000119 BufferPtr = BufStart;
120 BufferEnd = BufEnd;
121
122 assert(BufferEnd[0] == 0 &&
123 "We assume that the input buffer has a null character at the end"
124 " to simplify lexing!");
125
126 // Start of the file is a start of line.
127 IsAtStartOfLine = true;
128
129 // We are not after parsing a #.
130 ParsingPreprocessorDirective = false;
131
132 // We are not after parsing #include.
133 ParsingFilename = false;
134
135 // We *are* in raw mode.
136 LexingRawMode = true;
137
Chris Lattnere3f863a2008-10-12 01:34:51 +0000138 // Default to not keeping comments in raw mode.
Chris Lattner02b436a2007-10-17 20:41:00 +0000139 KeepCommentMode = false;
140}
141
142
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000143/// Stringify - Convert the specified string into a C string, with surrounding
144/// ""'s, and with escaped \ and " characters.
Chris Lattnerecc39e92006-07-15 05:23:31 +0000145std::string Lexer::Stringify(const std::string &Str, bool Charify) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000146 std::string Result = Str;
Chris Lattnerecc39e92006-07-15 05:23:31 +0000147 char Quote = Charify ? '\'' : '"';
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000148 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
Chris Lattnerecc39e92006-07-15 05:23:31 +0000149 if (Result[i] == '\\' || Result[i] == Quote) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000150 Result.insert(Result.begin()+i, '\\');
151 ++i; ++e;
152 }
153 }
Chris Lattnerecc39e92006-07-15 05:23:31 +0000154 return Result;
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000155}
156
Chris Lattner4c4a2452007-07-24 06:57:14 +0000157/// Stringify - Convert the specified string into a C string by escaping '\'
158/// and " characters. This does not add surrounding ""'s to the string.
159void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
160 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
161 if (Str[i] == '\\' || Str[i] == '"') {
162 Str.insert(Str.begin()+i, '\\');
163 ++i; ++e;
164 }
165 }
166}
167
Chris Lattner22eb9722006-06-18 05:43:12 +0000168
Chris Lattner8e129c22007-10-17 21:18:47 +0000169/// MeasureTokenLength - Relex the token at the specified location and return
170/// its length in bytes in the input file. If the token needs cleaning (e.g.
171/// includes a trigraph or an escaped newline) then this count includes bytes
172/// that are part of that.
173unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
174 const SourceManager &SM) {
175 // If this comes from a macro expansion, we really do want the macro name, not
176 // the token this macro expanded to.
177 Loc = SM.getLogicalLoc(Loc);
178
179 const char *StrData = SM.getCharacterData(Loc);
180
181 // TODO: this could be special cased for common tokens like identifiers, ')',
182 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
183 // all obviously single-char tokens. This could use
184 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
185 // something.
186
187
188 const char *BufEnd = SM.getBufferData(Loc.getFileID()).second;
189
190 // Create a langops struct and enable trigraphs. This is sufficient for
191 // measuring tokens.
192 LangOptions LangOpts;
193 LangOpts.Trigraphs = true;
194
195 // Create a lexer starting at the beginning of this token.
196 Lexer TheLexer(Loc, LangOpts, StrData, BufEnd);
197 Token TheTok;
Chris Lattner50c90502008-10-12 01:15:46 +0000198 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner8e129c22007-10-17 21:18:47 +0000199 return TheTok.getLength();
200}
201
Chris Lattner22eb9722006-06-18 05:43:12 +0000202//===----------------------------------------------------------------------===//
203// Character information.
204//===----------------------------------------------------------------------===//
205
206static unsigned char CharInfo[256];
207
208enum {
209 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
210 CHAR_VERT_WS = 0x02, // '\r', '\n'
211 CHAR_LETTER = 0x04, // a-z,A-Z
212 CHAR_NUMBER = 0x08, // 0-9
213 CHAR_UNDER = 0x10, // _
214 CHAR_PERIOD = 0x20 // .
215};
216
217static void InitCharacterInfo() {
218 static bool isInited = false;
219 if (isInited) return;
220 isInited = true;
221
222 // Intiialize the CharInfo table.
223 // TODO: statically initialize this.
224 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
225 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
226 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
227
228 CharInfo[(int)'_'] = CHAR_UNDER;
Chris Lattnerdd0b7cb2006-10-17 02:53:51 +0000229 CharInfo[(int)'.'] = CHAR_PERIOD;
Chris Lattner22eb9722006-06-18 05:43:12 +0000230 for (unsigned i = 'a'; i <= 'z'; ++i)
231 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
232 for (unsigned i = '0'; i <= '9'; ++i)
233 CharInfo[i] = CHAR_NUMBER;
234}
235
236/// isIdentifierBody - Return true if this is the body character of an
237/// identifier, which is [a-zA-Z0-9_].
238static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000239 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000240}
241
242/// isHorizontalWhitespace - Return true if this character is horizontal
243/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
244static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000245 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000246}
247
248/// isWhitespace - Return true if this character is horizontal or vertical
249/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
250/// for '\0'.
251static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000252 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000253}
254
255/// isNumberBody - Return true if this is the body character of an
256/// preprocessing number, which is [a-zA-Z0-9_.].
257static inline bool isNumberBody(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000258 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
259 true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000260}
261
Chris Lattnerd01e2912006-06-18 16:22:51 +0000262
Chris Lattner22eb9722006-06-18 05:43:12 +0000263//===----------------------------------------------------------------------===//
264// Diagnostics forwarding code.
265//===----------------------------------------------------------------------===//
266
Chris Lattner619c1742007-07-22 18:38:25 +0000267/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
268/// lexer buffer was all instantiated at a single point, perform the mapping.
269/// This is currently only used for _Pragma implementation, so it is the slow
270/// path of the hot getSourceLocation method. Do not allow it to be inlined.
271static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
272 SourceLocation FileLoc,
273 unsigned CharNo) DISABLE_INLINE;
274static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
275 SourceLocation FileLoc,
276 unsigned CharNo) {
277 // Otherwise, we're lexing "mapped tokens". This is used for things like
278 // _Pragma handling. Combine the instantiation location of FileLoc with the
279 // physical location.
280 SourceManager &SourceMgr = PP.getSourceManager();
281
282 // Create a new SLoc which is expanded from logical(FileLoc) but whose
283 // characters come from phys(FileLoc)+Offset.
284 SourceLocation VirtLoc = SourceMgr.getLogicalLoc(FileLoc);
285 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(FileLoc);
286 PhysLoc = SourceLocation::getFileLoc(PhysLoc.getFileID(), CharNo);
287 return SourceMgr.getInstantiationLoc(PhysLoc, VirtLoc);
288}
289
Chris Lattner22eb9722006-06-18 05:43:12 +0000290/// getSourceLocation - Return a source location identifier for the specified
291/// offset in the current file.
292SourceLocation Lexer::getSourceLocation(const char *Loc) const {
Chris Lattner5d1c0272007-07-22 18:44:36 +0000293 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Chris Lattner4cca5ba2006-07-02 20:05:54 +0000294 "Location out of range for this buffer!");
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000295
296 // In the normal case, we're just lexing from a simple file buffer, return
297 // the file id from FileLoc with the offset specified.
Chris Lattner5d1c0272007-07-22 18:44:36 +0000298 unsigned CharNo = Loc-BufferStart;
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000299 if (FileLoc.isFileID())
300 return SourceLocation::getFileLoc(FileLoc.getFileID(), CharNo);
301
Chris Lattner02b436a2007-10-17 20:41:00 +0000302 assert(PP && "This doesn't work on raw lexers");
303 return GetMappedTokenLoc(*PP, FileLoc, CharNo);
Chris Lattner22eb9722006-06-18 05:43:12 +0000304}
305
Chris Lattner22eb9722006-06-18 05:43:12 +0000306/// Diag - Forwarding function for diagnostics. This translate a source
307/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000308void Lexer::Diag(const char *Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000309 const std::string &Msg) const {
Chris Lattner4431a1b2007-11-30 22:53:43 +0000310 if (LexingRawMode && Diagnostic::isBuiltinNoteWarningOrExtension(DiagID))
Chris Lattner538d7f32006-07-20 04:31:52 +0000311 return;
Chris Lattner02b436a2007-10-17 20:41:00 +0000312 PP->Diag(getSourceLocation(Loc), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000313}
Chris Lattner538d7f32006-07-20 04:31:52 +0000314void Lexer::Diag(SourceLocation Loc, unsigned DiagID,
315 const std::string &Msg) const {
Chris Lattner4431a1b2007-11-30 22:53:43 +0000316 if (LexingRawMode && Diagnostic::isBuiltinNoteWarningOrExtension(DiagID))
Chris Lattner538d7f32006-07-20 04:31:52 +0000317 return;
Chris Lattner02b436a2007-10-17 20:41:00 +0000318 PP->Diag(Loc, DiagID, Msg);
Chris Lattner538d7f32006-07-20 04:31:52 +0000319}
320
Chris Lattner22eb9722006-06-18 05:43:12 +0000321
322//===----------------------------------------------------------------------===//
323// Trigraph and Escaped Newline Handling Code.
324//===----------------------------------------------------------------------===//
325
326/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
327/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
328static char GetTrigraphCharForLetter(char Letter) {
329 switch (Letter) {
330 default: return 0;
331 case '=': return '#';
332 case ')': return ']';
333 case '(': return '[';
334 case '!': return '|';
335 case '\'': return '^';
336 case '>': return '}';
337 case '/': return '\\';
338 case '<': return '{';
339 case '-': return '~';
340 }
341}
342
343/// DecodeTrigraphChar - If the specified character is a legal trigraph when
344/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
345/// return the result character. Finally, emit a warning about trigraph use
346/// whether trigraphs are enabled or not.
347static char DecodeTrigraphChar(const char *CP, Lexer *L) {
348 char Res = GetTrigraphCharForLetter(*CP);
349 if (Res && L) {
350 if (!L->getFeatures().Trigraphs) {
351 L->Diag(CP-2, diag::trigraph_ignored);
352 return 0;
353 } else {
354 L->Diag(CP-2, diag::trigraph_converted, std::string()+Res);
355 }
356 }
357 return Res;
358}
359
360/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
361/// get its size, and return it. This is tricky in several cases:
362/// 1. If currently at the start of a trigraph, we warn about the trigraph,
363/// then either return the trigraph (skipping 3 chars) or the '?',
364/// depending on whether trigraphs are enabled or not.
365/// 2. If this is an escaped newline (potentially with whitespace between
366/// the backslash and newline), implicitly skip the newline and return
367/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000368/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000369///
370/// This handles the slow/uncommon case of the getCharAndSize method. Here we
371/// know that we can accumulate into Size, and that we have already incremented
372/// Ptr by Size bytes.
373///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000374/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
375/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000376///
377char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattner146762e2007-07-20 16:59:19 +0000378 Token *Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000379 // If we have a slash, look for an escaped newline.
380 if (Ptr[0] == '\\') {
381 ++Size;
382 ++Ptr;
383Slash:
384 // Common case, backslash-char where the char is not whitespace.
385 if (!isWhitespace(Ptr[0])) return '\\';
386
387 // See if we have optional whitespace characters followed by a newline.
388 {
389 unsigned SizeTmp = 0;
390 do {
391 ++SizeTmp;
392 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
393 // Remember that this token needs to be cleaned.
Chris Lattner146762e2007-07-20 16:59:19 +0000394 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000395
396 // Warn if there was whitespace between the backslash and newline.
397 if (SizeTmp != 1 && Tok)
398 Diag(Ptr, diag::backslash_newline_space);
399
400 // If this is a \r\n or \n\r, skip the newlines.
401 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
402 Ptr[SizeTmp-1] != Ptr[SizeTmp])
403 ++SizeTmp;
404
405 // Found backslash<whitespace><newline>. Parse the char after it.
406 Size += SizeTmp;
407 Ptr += SizeTmp;
408 // Use slow version to accumulate a correct size field.
409 return getCharAndSizeSlow(Ptr, Size, Tok);
410 }
411 } while (isWhitespace(Ptr[SizeTmp]));
412 }
413
414 // Otherwise, this is not an escaped newline, just return the slash.
415 return '\\';
416 }
417
418 // If this is a trigraph, process it.
419 if (Ptr[0] == '?' && Ptr[1] == '?') {
420 // If this is actually a legal trigraph (not something like "??x"), emit
421 // a trigraph warning. If so, and if trigraphs are enabled, return it.
422 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
423 // Remember that this token needs to be cleaned.
Chris Lattner146762e2007-07-20 16:59:19 +0000424 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000425
426 Ptr += 3;
427 Size += 3;
428 if (C == '\\') goto Slash;
429 return C;
430 }
431 }
432
433 // If this is neither, return a single character.
434 ++Size;
435 return *Ptr;
436}
437
Chris Lattnerd01e2912006-06-18 16:22:51 +0000438
Chris Lattner22eb9722006-06-18 05:43:12 +0000439/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
440/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
441/// and that we have already incremented Ptr by Size bytes.
442///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000443/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
444/// be updated to match.
445char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000446 const LangOptions &Features) {
447 // If we have a slash, look for an escaped newline.
448 if (Ptr[0] == '\\') {
449 ++Size;
450 ++Ptr;
451Slash:
452 // Common case, backslash-char where the char is not whitespace.
453 if (!isWhitespace(Ptr[0])) return '\\';
454
455 // See if we have optional whitespace characters followed by a newline.
456 {
457 unsigned SizeTmp = 0;
458 do {
459 ++SizeTmp;
460 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
461
462 // If this is a \r\n or \n\r, skip the newlines.
463 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
464 Ptr[SizeTmp-1] != Ptr[SizeTmp])
465 ++SizeTmp;
466
467 // Found backslash<whitespace><newline>. Parse the char after it.
468 Size += SizeTmp;
469 Ptr += SizeTmp;
470
471 // Use slow version to accumulate a correct size field.
472 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
473 }
474 } while (isWhitespace(Ptr[SizeTmp]));
475 }
476
477 // Otherwise, this is not an escaped newline, just return the slash.
478 return '\\';
479 }
480
481 // If this is a trigraph, process it.
482 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
483 // If this is actually a legal trigraph (not something like "??x"), return
484 // it.
485 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
486 Ptr += 3;
487 Size += 3;
488 if (C == '\\') goto Slash;
489 return C;
490 }
491 }
492
493 // If this is neither, return a single character.
494 ++Size;
495 return *Ptr;
496}
497
Chris Lattner22eb9722006-06-18 05:43:12 +0000498//===----------------------------------------------------------------------===//
499// Helper methods for lexing.
500//===----------------------------------------------------------------------===//
501
Chris Lattner146762e2007-07-20 16:59:19 +0000502void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000503 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
504 unsigned Size;
505 unsigned char C = *CurPtr++;
506 while (isIdentifierBody(C)) {
507 C = *CurPtr++;
508 }
509 --CurPtr; // Back up over the skipped character.
510
511 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
512 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000513 // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000514 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
515FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000516 const char *IdStart = BufferPtr;
Chris Lattnerd01e2912006-06-18 16:22:51 +0000517 FormTokenWithChars(Result, CurPtr);
Chris Lattner8c204872006-10-14 05:19:21 +0000518 Result.setKind(tok::identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000519
Chris Lattner0f1f5052006-07-20 04:16:23 +0000520 // If we are in raw mode, return this identifier raw. There is no need to
521 // look up identifier information or attempt to macro expand it.
522 if (LexingRawMode) return;
523
Chris Lattnercefc7682006-07-08 08:28:12 +0000524 // Fill in Result.IdentifierInfo, looking up the identifier in the
525 // identifier table.
Chris Lattner02b436a2007-10-17 20:41:00 +0000526 PP->LookUpIdentifierInfo(Result, IdStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000527
Chris Lattnerc5a00062006-06-18 16:41:01 +0000528 // Finally, now that we know we have an identifier, pass this off to the
529 // preprocessor, which may macro expand it or something.
Chris Lattner02b436a2007-10-17 20:41:00 +0000530 return PP->HandleIdentifier(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000531 }
532
533 // Otherwise, $,\,? in identifier found. Enter slower path.
534
535 C = getCharAndSize(CurPtr, Size);
536 while (1) {
537 if (C == '$') {
538 // If we hit a $ and they are not supported in identifiers, we are done.
539 if (!Features.DollarIdents) goto FinishIdentifier;
540
541 // Otherwise, emit a diagnostic and continue.
Chris Lattnercb283342006-06-18 06:48:37 +0000542 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000543 CurPtr = ConsumeChar(CurPtr, Size, Result);
544 C = getCharAndSize(CurPtr, Size);
545 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000546 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000547 // Found end of identifier.
548 goto FinishIdentifier;
549 }
550
551 // Otherwise, this character is good, consume it.
552 CurPtr = ConsumeChar(CurPtr, Size, Result);
553
554 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000555 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000556 CurPtr = ConsumeChar(CurPtr, Size, Result);
557 C = getCharAndSize(CurPtr, Size);
558 }
559 }
560}
561
562
Nate Begeman5eee9332008-04-14 02:26:39 +0000563/// LexNumericConstant - Lex the remainder of a integer or floating point
Chris Lattner22eb9722006-06-18 05:43:12 +0000564/// constant. From[-1] is the first character lexed. Return the end of the
565/// constant.
Chris Lattner146762e2007-07-20 16:59:19 +0000566void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000567 unsigned Size;
568 char C = getCharAndSize(CurPtr, Size);
569 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000570 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000571 CurPtr = ConsumeChar(CurPtr, Size, Result);
572 PrevCh = C;
573 C = getCharAndSize(CurPtr, Size);
574 }
575
576 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
577 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
578 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
579
580 // If we have a hex FP constant, continue.
581 if (Features.HexFloats &&
582 (C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p'))
583 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
584
Chris Lattner8c204872006-10-14 05:19:21 +0000585 Result.setKind(tok::numeric_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000586
Chris Lattnerd01e2912006-06-18 16:22:51 +0000587 // Update the location of token as well as BufferPtr.
588 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000589}
590
591/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
592/// either " or L".
Chris Lattner146762e2007-07-20 16:59:19 +0000593void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide){
Chris Lattner22eb9722006-06-18 05:43:12 +0000594 const char *NulCharacter = 0; // Does this string contain the \0 character?
595
596 char C = getAndAdvanceChar(CurPtr, Result);
597 while (C != '"') {
598 // Skip escaped characters.
599 if (C == '\\') {
600 // Skip the escaped character.
601 C = getAndAdvanceChar(CurPtr, Result);
602 } else if (C == '\n' || C == '\r' || // Newline.
603 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000604 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000605 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000606 FormTokenWithChars(Result, CurPtr-1);
607 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000608 } else if (C == 0) {
609 NulCharacter = CurPtr-1;
610 }
611 C = getAndAdvanceChar(CurPtr, Result);
612 }
613
Chris Lattner5a78a022006-07-20 06:02:19 +0000614 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000615 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000616
Chris Lattner8c204872006-10-14 05:19:21 +0000617 Result.setKind(Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000618
Chris Lattnerd01e2912006-06-18 16:22:51 +0000619 // Update the location of the token as well as the BufferPtr instance var.
620 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000621}
622
623/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
624/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattner146762e2007-07-20 16:59:19 +0000625void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000626 const char *NulCharacter = 0; // Does this string contain the \0 character?
627
628 char C = getAndAdvanceChar(CurPtr, Result);
629 while (C != '>') {
630 // Skip escaped characters.
631 if (C == '\\') {
632 // Skip the escaped character.
633 C = getAndAdvanceChar(CurPtr, Result);
634 } else if (C == '\n' || C == '\r' || // Newline.
635 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000636 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000637 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000638 FormTokenWithChars(Result, CurPtr-1);
639 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000640 } else if (C == 0) {
641 NulCharacter = CurPtr-1;
642 }
643 C = getAndAdvanceChar(CurPtr, Result);
644 }
645
Chris Lattner5a78a022006-07-20 06:02:19 +0000646 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000647 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000648
Chris Lattner8c204872006-10-14 05:19:21 +0000649 Result.setKind(tok::angle_string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000650
Chris Lattnerd01e2912006-06-18 16:22:51 +0000651 // Update the location of token as well as BufferPtr.
652 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000653}
654
655
656/// LexCharConstant - Lex the remainder of a character constant, after having
657/// lexed either ' or L'.
Chris Lattner146762e2007-07-20 16:59:19 +0000658void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000659 const char *NulCharacter = 0; // Does this character contain the \0 character?
660
661 // Handle the common case of 'x' and '\y' efficiently.
662 char C = getAndAdvanceChar(CurPtr, Result);
663 if (C == '\'') {
Chris Lattnera5f4c882006-07-20 06:08:47 +0000664 if (!LexingRawMode) Diag(BufferPtr, diag::err_empty_character);
Chris Lattner8c204872006-10-14 05:19:21 +0000665 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000666 FormTokenWithChars(Result, CurPtr);
667 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000668 } else if (C == '\\') {
669 // Skip the escaped character.
670 // FIXME: UCN's.
671 C = getAndAdvanceChar(CurPtr, Result);
672 }
673
674 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
675 ++CurPtr;
676 } else {
677 // Fall back on generic code for embedded nulls, newlines, wide chars.
678 do {
679 // Skip escaped characters.
680 if (C == '\\') {
681 // Skip the escaped character.
682 C = getAndAdvanceChar(CurPtr, Result);
683 } else if (C == '\n' || C == '\r' || // Newline.
684 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000685 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner8c204872006-10-14 05:19:21 +0000686 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000687 FormTokenWithChars(Result, CurPtr-1);
688 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000689 } else if (C == 0) {
690 NulCharacter = CurPtr-1;
691 }
692 C = getAndAdvanceChar(CurPtr, Result);
693 } while (C != '\'');
694 }
695
Chris Lattnercb283342006-06-18 06:48:37 +0000696 if (NulCharacter) Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000697
Chris Lattner8c204872006-10-14 05:19:21 +0000698 Result.setKind(tok::char_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000699
Chris Lattnerd01e2912006-06-18 16:22:51 +0000700 // Update the location of token as well as BufferPtr.
701 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000702}
703
704/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
705/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattner146762e2007-07-20 16:59:19 +0000706void Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000707 // Whitespace - Skip it, then return the token after the whitespace.
708 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
709 while (1) {
710 // Skip horizontal whitespace very aggressively.
711 while (isHorizontalWhitespace(Char))
712 Char = *++CurPtr;
713
714 // Otherwise if we something other than whitespace, we're done.
715 if (Char != '\n' && Char != '\r')
716 break;
717
718 if (ParsingPreprocessorDirective) {
719 // End of preprocessor directive line, let LexTokenInternal handle this.
720 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000721 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000722 }
723
724 // ok, but handle newline.
725 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +0000726 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000727 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +0000728 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000729 Char = *++CurPtr;
730 }
731
732 // If this isn't immediately after a newline, there is leading space.
733 char PrevChar = CurPtr[-1];
734 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattner146762e2007-07-20 16:59:19 +0000735 Result.setFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000736
Chris Lattner22eb9722006-06-18 05:43:12 +0000737 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000738}
739
740// SkipBCPLComment - We have just read the // characters from input. Skip until
741// we find the newline character thats terminate the comment. Then update
742/// BufferPtr and return.
Chris Lattner146762e2007-07-20 16:59:19 +0000743bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000744 // If BCPL comments aren't explicitly enabled for this language, emit an
745 // extension warning.
746 if (!Features.BCPLComment) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000747 Diag(BufferPtr, diag::ext_bcpl_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000748
749 // Mark them enabled so we only emit one warning for this translation
750 // unit.
751 Features.BCPLComment = true;
752 }
753
754 // Scan over the body of the comment. The common case, when scanning, is that
755 // the comment contains normal ascii characters with nothing interesting in
756 // them. As such, optimize for this case with the inner loop.
757 char C;
758 do {
759 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +0000760 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
761 // If we find a \n character, scan backwards, checking to see if it's an
762 // escaped newline, like we do for block comments.
Chris Lattner22eb9722006-06-18 05:43:12 +0000763
764 // Skip over characters in the fast loop.
765 while (C != 0 && // Potentially EOF.
766 C != '\\' && // Potentially escaped newline.
767 C != '?' && // Potentially trigraph.
768 C != '\n' && C != '\r') // Newline or DOS-style newline.
769 C = *++CurPtr;
770
771 // If this is a newline, we're done.
772 if (C == '\n' || C == '\r')
773 break; // Found the newline? Break out!
774
775 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
776 // properly decode the character.
777 const char *OldPtr = CurPtr;
778 C = getAndAdvanceChar(CurPtr, Result);
779
780 // If we read multiple characters, and one of those characters was a \r or
Chris Lattnerff591e22007-06-09 06:07:22 +0000781 // \n, then we had an escaped newline within the comment. Emit diagnostic
782 // unless the next line is also a // comment.
783 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000784 for (; OldPtr != CurPtr; ++OldPtr)
785 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnerff591e22007-06-09 06:07:22 +0000786 // Okay, we found a // comment that ends in a newline, if the next
787 // line is also a // comment, but has spaces, don't emit a diagnostic.
788 if (isspace(C)) {
789 const char *ForwardPtr = CurPtr;
790 while (isspace(*ForwardPtr)) // Skip whitespace.
791 ++ForwardPtr;
792 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
793 break;
794 }
795
Chris Lattnercb283342006-06-18 06:48:37 +0000796 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
797 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000798 }
799 }
800
Chris Lattner457fc152006-07-29 06:30:25 +0000801 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
Chris Lattner22eb9722006-06-18 05:43:12 +0000802 } while (C != '\n' && C != '\r');
803
Chris Lattner457fc152006-07-29 06:30:25 +0000804 // Found but did not consume the newline.
805
806 // If we are returning comments as tokens, return this comment as a token.
807 if (KeepCommentMode)
808 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000809
810 // If we are inside a preprocessor directive and we see the end of line,
811 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +0000812 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000813 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000814 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000815 }
816
817 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner87e97ea2008-10-12 00:23:07 +0000818 // \r\n sequence. This is an efficiency hack (because we know the \n can't
819 // contribute to another token), it isn't needed for correctness.
Chris Lattner22eb9722006-06-18 05:43:12 +0000820 ++CurPtr;
821
822 // The next returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +0000823 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000824 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +0000825 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000826 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000827 return true;
828}
Chris Lattner22eb9722006-06-18 05:43:12 +0000829
Chris Lattner457fc152006-07-29 06:30:25 +0000830/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
831/// an appropriate way and return it.
Chris Lattner146762e2007-07-20 16:59:19 +0000832bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner8c204872006-10-14 05:19:21 +0000833 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +0000834 FormTokenWithChars(Result, CurPtr);
835
836 // If this BCPL-style comment is in a macro definition, transmogrify it into
837 // a C-style block comment.
838 if (ParsingPreprocessorDirective) {
Chris Lattner02b436a2007-10-17 20:41:00 +0000839 std::string Spelling = PP->getSpelling(Result);
Chris Lattner457fc152006-07-29 06:30:25 +0000840 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
841 Spelling[1] = '*'; // Change prefix to "/*".
842 Spelling += "*/"; // add suffix.
843
Chris Lattner02b436a2007-10-17 20:41:00 +0000844 Result.setLocation(PP->CreateString(&Spelling[0], Spelling.size(),
845 Result.getLocation()));
Chris Lattner8c204872006-10-14 05:19:21 +0000846 Result.setLength(Spelling.size());
Chris Lattner457fc152006-07-29 06:30:25 +0000847 }
848 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000849}
850
Chris Lattnercb283342006-06-18 06:48:37 +0000851/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
852/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner22eb9722006-06-18 05:43:12 +0000853/// diagnostic if so. We know that the is inside of a block comment.
Chris Lattner1f583052006-06-18 06:53:56 +0000854static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
855 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000856 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Chris Lattner22eb9722006-06-18 05:43:12 +0000857
858 // Back up off the newline.
859 --CurPtr;
860
861 // If this is a two-character newline sequence, skip the other character.
862 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
863 // \n\n or \r\r -> not escaped newline.
864 if (CurPtr[0] == CurPtr[1])
865 return false;
866 // \n\r or \r\n -> skip the newline.
867 --CurPtr;
868 }
869
870 // If we have horizontal whitespace, skip over it. We allow whitespace
871 // between the slash and newline.
872 bool HasSpace = false;
873 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
874 --CurPtr;
875 HasSpace = true;
876 }
877
878 // If we have a slash, we know this is an escaped newline.
879 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +0000880 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000881 } else {
882 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +0000883 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
884 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +0000885 return false;
Chris Lattnercb283342006-06-18 06:48:37 +0000886
887 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +0000888 CurPtr -= 2;
889
890 // If no trigraphs are enabled, warn that we ignored this trigraph and
891 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +0000892 if (!L->getFeatures().Trigraphs) {
893 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000894 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000895 }
Chris Lattner1f583052006-06-18 06:53:56 +0000896 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000897 }
898
899 // Warn about having an escaped newline between the */ characters.
Chris Lattner1f583052006-06-18 06:53:56 +0000900 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Chris Lattner22eb9722006-06-18 05:43:12 +0000901
902 // If there was space between the backslash and newline, warn about it.
Chris Lattner1f583052006-06-18 06:53:56 +0000903 if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space);
Chris Lattner22eb9722006-06-18 05:43:12 +0000904
Chris Lattnercb283342006-06-18 06:48:37 +0000905 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000906}
907
Chris Lattneraded4a92006-10-27 04:42:31 +0000908#ifdef __SSE2__
909#include <emmintrin.h>
Chris Lattner9f6604f2006-10-30 20:01:22 +0000910#elif __ALTIVEC__
911#include <altivec.h>
912#undef bool
Chris Lattneraded4a92006-10-27 04:42:31 +0000913#endif
914
Chris Lattner22eb9722006-06-18 05:43:12 +0000915/// SkipBlockComment - We have just read the /* characters from input. Read
916/// until we find the */ characters that terminate the comment. Note that we
917/// don't bother decoding trigraphs or escaped newlines in block comments,
918/// because they cannot cause the comment to end. The only thing that can
919/// happen is the comment could end with an escaped newline between the */ end
920/// of comment.
Chris Lattner146762e2007-07-20 16:59:19 +0000921bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000922 // Scan one character past where we should, looking for a '/' character. Once
923 // we find it, check to see if it was preceeded by a *. This common
924 // optimization helps people who like to put a lot of * characters in their
925 // comments.
Chris Lattnerc850ad62007-07-21 23:43:37 +0000926
927 // The first character we get with newlines and trigraphs skipped to handle
928 // the degenerate /*/ case below correctly if the * has an escaped newline
929 // after it.
930 unsigned CharSize;
931 unsigned char C = getCharAndSize(CurPtr, CharSize);
932 CurPtr += CharSize;
Chris Lattner22eb9722006-06-18 05:43:12 +0000933 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner7c2e9802008-10-12 01:31:51 +0000934 if (!LexingRawMode)
935 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000936 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +0000937 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000938 }
939
Chris Lattnerc850ad62007-07-21 23:43:37 +0000940 // Check to see if the first character after the '/*' is another /. If so,
941 // then this slash does not end the block comment, it is part of it.
942 if (C == '/')
943 C = *CurPtr++;
944
Chris Lattner22eb9722006-06-18 05:43:12 +0000945 while (1) {
Chris Lattner6cc3e362006-10-27 04:12:35 +0000946 // Skip over all non-interesting characters until we find end of buffer or a
947 // (probably ending) '/' character.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000948 if (CurPtr + 24 < BufferEnd) {
949 // While not aligned to a 16-byte boundary.
950 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
951 C = *CurPtr++;
952
953 if (C == '/') goto FoundSlash;
Chris Lattneraded4a92006-10-27 04:42:31 +0000954
955#ifdef __SSE2__
956 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
957 '/', '/', '/', '/', '/', '/', '/', '/');
958 while (CurPtr+16 <= BufferEnd &&
959 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
960 CurPtr += 16;
Chris Lattner9f6604f2006-10-30 20:01:22 +0000961#elif __ALTIVEC__
962 __vector unsigned char Slashes = {
963 '/', '/', '/', '/', '/', '/', '/', '/',
964 '/', '/', '/', '/', '/', '/', '/', '/'
965 };
966 while (CurPtr+16 <= BufferEnd &&
967 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
968 CurPtr += 16;
969#else
Chris Lattneraded4a92006-10-27 04:42:31 +0000970 // Scan for '/' quickly. Many block comments are very large.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000971 while (CurPtr[0] != '/' &&
972 CurPtr[1] != '/' &&
973 CurPtr[2] != '/' &&
974 CurPtr[3] != '/' &&
975 CurPtr+4 < BufferEnd) {
976 CurPtr += 4;
977 }
Chris Lattneraded4a92006-10-27 04:42:31 +0000978#endif
979
980 // It has to be one of the bytes scanned, increment to it and read one.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000981 C = *CurPtr++;
982 }
983
Chris Lattneraded4a92006-10-27 04:42:31 +0000984 // Loop to scan the remainder.
Chris Lattner22eb9722006-06-18 05:43:12 +0000985 while (C != '/' && C != '\0')
986 C = *CurPtr++;
987
Chris Lattner6cc3e362006-10-27 04:12:35 +0000988 FoundSlash:
Chris Lattner22eb9722006-06-18 05:43:12 +0000989 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000990 if (CurPtr[-2] == '*') // We found the final */. We're done!
991 break;
992
993 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +0000994 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000995 // We found the final */, though it had an escaped newline between the
996 // * and /. We're done!
997 break;
998 }
999 }
1000 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1001 // If this is a /* inside of the comment, emit a warning. Don't do this
1002 // if this is a /*/, which will end the comment. This misses cases with
1003 // embedded escaped newlines, but oh well.
Chris Lattner7c2e9802008-10-12 01:31:51 +00001004 Diag(CurPtr-1, diag::warn_nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001005 }
1006 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner7c2e9802008-10-12 01:31:51 +00001007 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001008 // Note: the user probably forgot a */. We could continue immediately
1009 // after the /*, but this would involve lexing a lot of what really is the
1010 // comment, which surely would confuse the parser.
1011 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +00001012 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001013 }
1014 C = *CurPtr++;
1015 }
Chris Lattner457fc152006-07-29 06:30:25 +00001016
1017 // If we are returning comments as tokens, return this comment as a token.
1018 if (KeepCommentMode) {
Chris Lattner8c204872006-10-14 05:19:21 +00001019 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +00001020 FormTokenWithChars(Result, CurPtr);
1021 return false;
1022 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001023
1024 // It is common for the tokens immediately after a /**/ comment to be
1025 // whitespace. Instead of going through the big switch, handle it
1026 // efficiently now.
1027 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner146762e2007-07-20 16:59:19 +00001028 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +00001029 SkipWhitespace(Result, CurPtr+1);
1030 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001031 }
1032
1033 // Otherwise, just return so that the next character will be lexed as a token.
1034 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001035 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +00001036 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001037}
1038
1039//===----------------------------------------------------------------------===//
1040// Primary Lexing Entry Points
1041//===----------------------------------------------------------------------===//
1042
1043/// LexIncludeFilename - After the preprocessor has parsed a #include, lex and
1044/// (potentially) macro expand the filename.
Chris Lattner146762e2007-07-20 16:59:19 +00001045void Lexer::LexIncludeFilename(Token &FilenameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001046 assert(ParsingPreprocessorDirective &&
1047 ParsingFilename == false &&
1048 "Must be in a preprocessing directive!");
1049
1050 // We are now parsing a filename!
1051 ParsingFilename = true;
1052
Chris Lattner269c2322006-06-25 06:23:00 +00001053 // Lex the filename.
1054 Lex(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001055
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001056 // We should have obtained the filename now.
Chris Lattner22eb9722006-06-18 05:43:12 +00001057 ParsingFilename = false;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001058
Chris Lattner22eb9722006-06-18 05:43:12 +00001059 // No filename?
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001060 if (FilenameTok.is(tok::eom))
Chris Lattner538d7f32006-07-20 04:31:52 +00001061 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner22eb9722006-06-18 05:43:12 +00001062}
1063
1064/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1065/// uninterpreted string. This switches the lexer out of directive mode.
1066std::string Lexer::ReadToEndOfLine() {
1067 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1068 "Must be in a preprocessing directive!");
1069 std::string Result;
Chris Lattner146762e2007-07-20 16:59:19 +00001070 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001071
1072 // CurPtr - Cache BufferPtr in an automatic variable.
1073 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001074 while (1) {
1075 char Char = getAndAdvanceChar(CurPtr, Tmp);
1076 switch (Char) {
1077 default:
1078 Result += Char;
1079 break;
1080 case 0: // Null.
1081 // Found end of file?
1082 if (CurPtr-1 != BufferEnd) {
1083 // Nope, normal character, continue.
1084 Result += Char;
1085 break;
1086 }
1087 // FALL THROUGH.
1088 case '\r':
1089 case '\n':
1090 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1091 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1092 BufferPtr = CurPtr-1;
1093
1094 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +00001095 Lex(Tmp);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001096 assert(Tmp.is(tok::eom) && "Unexpected token!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001097
1098 // Finally, we're done, return the string we found.
1099 return Result;
1100 }
1101 }
1102}
1103
1104/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1105/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +00001106/// This returns true if Result contains a token, false if PP.Lex should be
1107/// called again.
Chris Lattner146762e2007-07-20 16:59:19 +00001108bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001109 // If we hit the end of the file while parsing a preprocessor directive,
1110 // end the preprocessor directive first. The next token returned will
1111 // then be the end of file.
1112 if (ParsingPreprocessorDirective) {
1113 // Done parsing the "line".
1114 ParsingPreprocessorDirective = false;
Chris Lattner8c204872006-10-14 05:19:21 +00001115 Result.setKind(tok::eom);
Chris Lattnerd01e2912006-06-18 16:22:51 +00001116 // Update the location of token as well as BufferPtr.
1117 FormTokenWithChars(Result, CurPtr);
Chris Lattner457fc152006-07-29 06:30:25 +00001118
1119 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner02b436a2007-10-17 20:41:00 +00001120 KeepCommentMode = PP->getCommentRetentionState();
Chris Lattner2183a6e2006-07-18 06:36:12 +00001121 return true; // Have a token.
Chris Lattner22eb9722006-06-18 05:43:12 +00001122 }
1123
Chris Lattner30a2fa12006-07-19 06:31:49 +00001124 // If we are in raw mode, return this event as an EOF token. Let the caller
1125 // that put us in raw mode handle the event.
1126 if (LexingRawMode) {
Chris Lattner8c204872006-10-14 05:19:21 +00001127 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +00001128 BufferPtr = BufferEnd;
1129 FormTokenWithChars(Result, BufferEnd);
Chris Lattner8c204872006-10-14 05:19:21 +00001130 Result.setKind(tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001131 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001132 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001133
Chris Lattner30a2fa12006-07-19 06:31:49 +00001134 // Otherwise, issue diagnostics for unterminated #if and missing newline.
1135
1136 // If we are in a #if directive, emit an error.
1137 while (!ConditionalStack.empty()) {
Chris Lattner538d7f32006-07-20 04:31:52 +00001138 Diag(ConditionalStack.back().IfLoc, diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001139 ConditionalStack.pop_back();
1140 }
1141
Chris Lattner8f96d042008-04-12 05:54:25 +00001142 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1143 // a pedwarn.
1144 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Chris Lattner30a2fa12006-07-19 06:31:49 +00001145 Diag(BufferEnd, diag::ext_no_newline_eof);
1146
Chris Lattner22eb9722006-06-18 05:43:12 +00001147 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +00001148
1149 // Finally, let the preprocessor handle this.
Chris Lattner02b436a2007-10-17 20:41:00 +00001150 return PP->HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001151}
1152
Chris Lattner678c8802006-07-11 05:46:12 +00001153/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1154/// the specified lexer will return a tok::l_paren token, 0 if it is something
1155/// else and 2 if there are no more tokens in the buffer controlled by the
1156/// lexer.
1157unsigned Lexer::isNextPPTokenLParen() {
1158 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1159
1160 // Switch to 'skipping' mode. This will ensure that we can lex a token
1161 // without emitting diagnostics, disables macro expansion, and will cause EOF
1162 // to return an EOF token instead of popping the include stack.
1163 LexingRawMode = true;
1164
1165 // Save state that can be changed while lexing so that we can restore it.
1166 const char *TmpBufferPtr = BufferPtr;
1167
Chris Lattner146762e2007-07-20 16:59:19 +00001168 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001169 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001170 LexTokenInternal(Tok);
1171
1172 // Restore state that may have changed.
1173 BufferPtr = TmpBufferPtr;
1174
1175 // Restore the lexer back to non-skipping mode.
1176 LexingRawMode = false;
1177
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001178 if (Tok.is(tok::eof))
Chris Lattner678c8802006-07-11 05:46:12 +00001179 return 2;
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001180 return Tok.is(tok::l_paren);
Chris Lattner678c8802006-07-11 05:46:12 +00001181}
1182
Chris Lattner22eb9722006-06-18 05:43:12 +00001183
1184/// LexTokenInternal - This implements a simple C family lexer. It is an
1185/// extremely performance critical piece of code. This assumes that the buffer
1186/// has a null character at the end of the file. Return true if an error
1187/// occurred and compilation should terminate, false if normal. This returns a
1188/// preprocessing token, not a normal token, as such, it is an internal
1189/// interface. It assumes that the Flags of result have been cleared before
1190/// calling this.
Chris Lattner146762e2007-07-20 16:59:19 +00001191void Lexer::LexTokenInternal(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001192LexNextToken:
1193 // New token, can't need cleaning yet.
Chris Lattner146762e2007-07-20 16:59:19 +00001194 Result.clearFlag(Token::NeedsCleaning);
Chris Lattner8c204872006-10-14 05:19:21 +00001195 Result.setIdentifierInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001196
1197 // CurPtr - Cache BufferPtr in an automatic variable.
1198 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001199
Chris Lattnereb54b592006-07-10 06:34:27 +00001200 // Small amounts of horizontal whitespace is very common between tokens.
1201 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1202 ++CurPtr;
1203 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1204 ++CurPtr;
1205 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001206 Result.setFlag(Token::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001207 }
1208
Chris Lattner22eb9722006-06-18 05:43:12 +00001209 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1210
1211 // Read a character, advancing over it.
1212 char Char = getAndAdvanceChar(CurPtr, Result);
1213 switch (Char) {
1214 case 0: // Null.
1215 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001216 if (CurPtr-1 == BufferEnd) {
1217 // Read the PP instance variable into an automatic variable, because
1218 // LexEndOfFile will often delete 'this'.
Chris Lattner02b436a2007-10-17 20:41:00 +00001219 Preprocessor *PPCache = PP;
Chris Lattner2183a6e2006-07-18 06:36:12 +00001220 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1221 return; // Got a token to return.
Chris Lattner02b436a2007-10-17 20:41:00 +00001222 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1223 return PPCache->Lex(Result);
Chris Lattner2183a6e2006-07-18 06:36:12 +00001224 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001225
Chris Lattnercb283342006-06-18 06:48:37 +00001226 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner146762e2007-07-20 16:59:19 +00001227 Result.setFlag(Token::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001228 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001229 goto LexNextToken; // GCC isn't tail call eliminating.
1230 case '\n':
1231 case '\r':
1232 // If we are inside a preprocessor directive and we see the end of line,
1233 // we know we are done with the directive, so return an EOM token.
1234 if (ParsingPreprocessorDirective) {
1235 // Done parsing the "line".
1236 ParsingPreprocessorDirective = false;
1237
Chris Lattner457fc152006-07-29 06:30:25 +00001238 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner02b436a2007-10-17 20:41:00 +00001239 KeepCommentMode = PP->getCommentRetentionState();
Chris Lattner457fc152006-07-29 06:30:25 +00001240
Chris Lattner22eb9722006-06-18 05:43:12 +00001241 // Since we consumed a newline, we are back at the start of a line.
1242 IsAtStartOfLine = true;
1243
Chris Lattner8c204872006-10-14 05:19:21 +00001244 Result.setKind(tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001245 break;
1246 }
1247 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001248 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001249 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001250 Result.clearFlag(Token::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001251 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001252 goto LexNextToken; // GCC isn't tail call eliminating.
1253 case ' ':
1254 case '\t':
1255 case '\f':
1256 case '\v':
Chris Lattnerb9b85972007-07-22 06:29:05 +00001257 SkipHorizontalWhitespace:
Chris Lattner146762e2007-07-20 16:59:19 +00001258 Result.setFlag(Token::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001259 SkipWhitespace(Result, CurPtr);
Chris Lattnerb9b85972007-07-22 06:29:05 +00001260
1261 SkipIgnoredUnits:
1262 CurPtr = BufferPtr;
1263
1264 // If the next token is obviously a // or /* */ comment, skip it efficiently
1265 // too (without going through the big switch stmt).
1266 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !KeepCommentMode) {
1267 SkipBCPLComment(Result, CurPtr+2);
1268 goto SkipIgnoredUnits;
1269 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !KeepCommentMode) {
1270 SkipBlockComment(Result, CurPtr+2);
1271 goto SkipIgnoredUnits;
1272 } else if (isHorizontalWhitespace(*CurPtr)) {
1273 goto SkipHorizontalWhitespace;
1274 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001275 goto LexNextToken; // GCC isn't tail call eliminating.
1276
Chris Lattner2b15cf72008-01-03 17:58:54 +00001277 // C99 6.4.4.1: Integer Constants.
1278 // C99 6.4.4.2: Floating Constants.
1279 case '0': case '1': case '2': case '3': case '4':
1280 case '5': case '6': case '7': case '8': case '9':
1281 // Notify MIOpt that we read a non-whitespace/non-comment token.
1282 MIOpt.ReadToken();
1283 return LexNumericConstant(Result, CurPtr);
1284
1285 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Chris Lattner371ac8a2006-07-04 07:11:10 +00001286 // Notify MIOpt that we read a non-whitespace/non-comment token.
1287 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001288 Char = getCharAndSize(CurPtr, SizeTmp);
1289
1290 // Wide string literal.
1291 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001292 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1293 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001294
1295 // Wide character constant.
1296 if (Char == '\'')
1297 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1298 // FALL THROUGH, treating L like the start of an identifier.
1299
1300 // C99 6.4.2: Identifiers.
1301 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1302 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1303 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1304 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1305 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1306 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1307 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1308 case 'v': case 'w': case 'x': case 'y': case 'z':
1309 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001310 // Notify MIOpt that we read a non-whitespace/non-comment token.
1311 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001312 return LexIdentifier(Result, CurPtr);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001313
1314 case '$': // $ in identifiers.
1315 if (Features.DollarIdents) {
1316 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
1317 // Notify MIOpt that we read a non-whitespace/non-comment token.
1318 MIOpt.ReadToken();
1319 return LexIdentifier(Result, CurPtr);
1320 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001321
Chris Lattner2b15cf72008-01-03 17:58:54 +00001322 Result.setKind(tok::unknown);
1323 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001324
1325 // C99 6.4.4: Character Constants.
1326 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001327 // Notify MIOpt that we read a non-whitespace/non-comment token.
1328 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001329 return LexCharConstant(Result, CurPtr);
1330
1331 // C99 6.4.5: String Literals.
1332 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001333 // Notify MIOpt that we read a non-whitespace/non-comment token.
1334 MIOpt.ReadToken();
Chris Lattnerd3e98952006-10-06 05:22:26 +00001335 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001336
1337 // C99 6.4.6: Punctuators.
1338 case '?':
Chris Lattner8c204872006-10-14 05:19:21 +00001339 Result.setKind(tok::question);
Chris Lattner22eb9722006-06-18 05:43:12 +00001340 break;
1341 case '[':
Chris Lattner8c204872006-10-14 05:19:21 +00001342 Result.setKind(tok::l_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001343 break;
1344 case ']':
Chris Lattner8c204872006-10-14 05:19:21 +00001345 Result.setKind(tok::r_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001346 break;
1347 case '(':
Chris Lattner8c204872006-10-14 05:19:21 +00001348 Result.setKind(tok::l_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001349 break;
1350 case ')':
Chris Lattner8c204872006-10-14 05:19:21 +00001351 Result.setKind(tok::r_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001352 break;
1353 case '{':
Chris Lattner8c204872006-10-14 05:19:21 +00001354 Result.setKind(tok::l_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001355 break;
1356 case '}':
Chris Lattner8c204872006-10-14 05:19:21 +00001357 Result.setKind(tok::r_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001358 break;
1359 case '.':
1360 Char = getCharAndSize(CurPtr, SizeTmp);
1361 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001362 // Notify MIOpt that we read a non-whitespace/non-comment token.
1363 MIOpt.ReadToken();
1364
Chris Lattner22eb9722006-06-18 05:43:12 +00001365 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1366 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001367 Result.setKind(tok::periodstar);
Chris Lattner22eb9722006-06-18 05:43:12 +00001368 CurPtr += SizeTmp;
1369 } else if (Char == '.' &&
1370 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner8c204872006-10-14 05:19:21 +00001371 Result.setKind(tok::ellipsis);
Chris Lattner22eb9722006-06-18 05:43:12 +00001372 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1373 SizeTmp2, Result);
1374 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001375 Result.setKind(tok::period);
Chris Lattner22eb9722006-06-18 05:43:12 +00001376 }
1377 break;
1378 case '&':
1379 Char = getCharAndSize(CurPtr, SizeTmp);
1380 if (Char == '&') {
Chris Lattner8c204872006-10-14 05:19:21 +00001381 Result.setKind(tok::ampamp);
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::ampequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001385 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1386 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001387 Result.setKind(tok::amp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001388 }
1389 break;
1390 case '*':
1391 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001392 Result.setKind(tok::starequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001393 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1394 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001395 Result.setKind(tok::star);
Chris Lattner22eb9722006-06-18 05:43:12 +00001396 }
1397 break;
1398 case '+':
1399 Char = getCharAndSize(CurPtr, SizeTmp);
1400 if (Char == '+') {
Chris Lattner8c204872006-10-14 05:19:21 +00001401 Result.setKind(tok::plusplus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001402 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1403 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001404 Result.setKind(tok::plusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001405 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1406 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001407 Result.setKind(tok::plus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001408 }
1409 break;
1410 case '-':
1411 Char = getCharAndSize(CurPtr, SizeTmp);
1412 if (Char == '-') {
Chris Lattner8c204872006-10-14 05:19:21 +00001413 Result.setKind(tok::minusminus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001414 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1415 } else if (Char == '>' && Features.CPlusPlus &&
1416 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001417 Result.setKind(tok::arrowstar); // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00001418 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1419 SizeTmp2, Result);
1420 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001421 Result.setKind(tok::arrow);
Chris Lattner22eb9722006-06-18 05:43:12 +00001422 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1423 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001424 Result.setKind(tok::minusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001425 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1426 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001427 Result.setKind(tok::minus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001428 }
1429 break;
1430 case '~':
Chris Lattner8c204872006-10-14 05:19:21 +00001431 Result.setKind(tok::tilde);
Chris Lattner22eb9722006-06-18 05:43:12 +00001432 break;
1433 case '!':
1434 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001435 Result.setKind(tok::exclaimequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001436 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1437 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001438 Result.setKind(tok::exclaim);
Chris Lattner22eb9722006-06-18 05:43:12 +00001439 }
1440 break;
1441 case '/':
1442 // 6.4.9: Comments
1443 Char = getCharAndSize(CurPtr, SizeTmp);
1444 if (Char == '/') { // BCPL comment.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001445 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) {
1446 // It is common for the tokens immediately after a // comment to be
Chris Lattner619c1742007-07-22 18:38:25 +00001447 // whitespace (indentation for the next line). Instead of going through
1448 // the big switch, handle it efficiently now.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001449 goto SkipIgnoredUnits;
1450 }
Chris Lattner457fc152006-07-29 06:30:25 +00001451 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001452 } else if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001453 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1454 goto LexNextToken; // GCC isn't tail call eliminating.
1455 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001456 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001457 Result.setKind(tok::slashequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001458 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1459 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001460 Result.setKind(tok::slash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001461 }
1462 break;
1463 case '%':
1464 Char = getCharAndSize(CurPtr, SizeTmp);
1465 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001466 Result.setKind(tok::percentequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001467 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1468 } else if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001469 Result.setKind(tok::r_brace); // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00001470 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1471 } else if (Features.Digraphs && Char == ':') {
1472 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001473 Char = getCharAndSize(CurPtr, SizeTmp);
1474 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001475 Result.setKind(tok::hashhash); // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00001476 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1477 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001478 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001479 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001480 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1481 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner22eb9722006-06-18 05:43:12 +00001482 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001483 Result.setKind(tok::hash); // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00001484
1485 // We parsed a # character. If this occurs at the start of the line,
1486 // it's actually the start of a preprocessing directive. Callback to
1487 // the preprocessor to handle it.
1488 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001489 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001490 BufferPtr = CurPtr;
Chris Lattner02b436a2007-10-17 20:41:00 +00001491 PP->HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001492
1493 // As an optimization, if the preprocessor didn't switch lexers, tail
1494 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00001495 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001496 // Start a new token. If this is a #include or something, the PP may
1497 // want us starting at the beginning of the line again. If so, set
1498 // the StartOfLine flag.
1499 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00001500 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001501 IsAtStartOfLine = false;
1502 }
1503 goto LexNextToken; // GCC isn't tail call eliminating.
1504 }
1505
Chris Lattner02b436a2007-10-17 20:41:00 +00001506 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001507 }
1508 }
1509 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001510 Result.setKind(tok::percent);
Chris Lattner22eb9722006-06-18 05:43:12 +00001511 }
1512 break;
1513 case '<':
1514 Char = getCharAndSize(CurPtr, SizeTmp);
1515 if (ParsingFilename) {
1516 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1517 } else if (Char == '<' &&
1518 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001519 Result.setKind(tok::lesslessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001520 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1521 SizeTmp2, Result);
1522 } else if (Char == '<') {
Chris Lattner8c204872006-10-14 05:19:21 +00001523 Result.setKind(tok::lessless);
Chris Lattner22eb9722006-06-18 05:43:12 +00001524 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1525 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001526 Result.setKind(tok::lessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001527 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1528 } else if (Features.Digraphs && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001529 Result.setKind(tok::l_square); // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00001530 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner5329e7e2008-02-24 19:05:57 +00001531 } else if (Features.Digraphs && Char == '%') {
Chris Lattner8c204872006-10-14 05:19:21 +00001532 Result.setKind(tok::l_brace); // '<%' -> '{'
Chris Lattner22eb9722006-06-18 05:43:12 +00001533 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001534 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001535 Result.setKind(tok::less);
Chris Lattner22eb9722006-06-18 05:43:12 +00001536 }
1537 break;
1538 case '>':
1539 Char = getCharAndSize(CurPtr, SizeTmp);
1540 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001541 Result.setKind(tok::greaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001542 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1543 } else if (Char == '>' &&
1544 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001545 Result.setKind(tok::greatergreaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001546 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1547 SizeTmp2, Result);
1548 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001549 Result.setKind(tok::greatergreater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001550 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001551 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001552 Result.setKind(tok::greater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001553 }
1554 break;
1555 case '^':
1556 Char = getCharAndSize(CurPtr, SizeTmp);
1557 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001558 Result.setKind(tok::caretequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001559 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1560 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001561 Result.setKind(tok::caret);
Chris Lattner22eb9722006-06-18 05:43:12 +00001562 }
1563 break;
1564 case '|':
1565 Char = getCharAndSize(CurPtr, SizeTmp);
1566 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001567 Result.setKind(tok::pipeequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001568 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1569 } else if (Char == '|') {
Chris Lattner8c204872006-10-14 05:19:21 +00001570 Result.setKind(tok::pipepipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001571 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1572 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001573 Result.setKind(tok::pipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001574 }
1575 break;
1576 case ':':
1577 Char = getCharAndSize(CurPtr, SizeTmp);
1578 if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001579 Result.setKind(tok::r_square); // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00001580 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1581 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001582 Result.setKind(tok::coloncolon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001583 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1584 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001585 Result.setKind(tok::colon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001586 }
1587 break;
1588 case ';':
Chris Lattner8c204872006-10-14 05:19:21 +00001589 Result.setKind(tok::semi);
Chris Lattner22eb9722006-06-18 05:43:12 +00001590 break;
1591 case '=':
1592 Char = getCharAndSize(CurPtr, SizeTmp);
1593 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001594 Result.setKind(tok::equalequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001595 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1596 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001597 Result.setKind(tok::equal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001598 }
1599 break;
1600 case ',':
Chris Lattner8c204872006-10-14 05:19:21 +00001601 Result.setKind(tok::comma);
Chris Lattner22eb9722006-06-18 05:43:12 +00001602 break;
1603 case '#':
1604 Char = getCharAndSize(CurPtr, SizeTmp);
1605 if (Char == '#') {
Chris Lattner8c204872006-10-14 05:19:21 +00001606 Result.setKind(tok::hashhash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001607 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001608 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001609 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001610 Diag(BufferPtr, diag::charize_microsoft_ext);
1611 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001612 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001613 Result.setKind(tok::hash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001614 // We parsed a # character. If this occurs at the start of the line,
1615 // it's actually the start of a preprocessing directive. Callback to
1616 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00001617 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001618 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001619 BufferPtr = CurPtr;
Chris Lattner02b436a2007-10-17 20:41:00 +00001620 PP->HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001621
1622 // As an optimization, if the preprocessor didn't switch lexers, tail
1623 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00001624 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001625 // Start a new token. If this is a #include or something, the PP may
1626 // want us starting at the beginning of the line again. If so, set
1627 // the StartOfLine flag.
1628 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00001629 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001630 IsAtStartOfLine = false;
1631 }
1632 goto LexNextToken; // GCC isn't tail call eliminating.
1633 }
Chris Lattner02b436a2007-10-17 20:41:00 +00001634 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001635 }
1636 }
1637 break;
1638
Chris Lattner2b15cf72008-01-03 17:58:54 +00001639 case '@':
1640 // Objective C support.
1641 if (CurPtr[-1] == '@' && Features.ObjC1)
1642 Result.setKind(tok::at);
1643 else
1644 Result.setKind(tok::unknown);
1645 break;
1646
Chris Lattner22eb9722006-06-18 05:43:12 +00001647 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00001648 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00001649 // FALL THROUGH.
1650 default:
Chris Lattner8c204872006-10-14 05:19:21 +00001651 Result.setKind(tok::unknown);
Chris Lattner041bef82006-07-11 05:52:53 +00001652 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001653 }
1654
Chris Lattner371ac8a2006-07-04 07:11:10 +00001655 // Notify MIOpt that we read a non-whitespace/non-comment token.
1656 MIOpt.ReadToken();
1657
Chris Lattnerd01e2912006-06-18 16:22:51 +00001658 // Update the location of token as well as BufferPtr.
1659 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001660}