blob: d2aef765260c07514da5f7d978b138bd44560132 [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 Lattner097a8b82008-10-12 03:27:19 +0000103 KeepCommentMode = false;
104 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner22eb9722006-06-18 05:43:12 +0000105}
106
Chris Lattner02b436a2007-10-17 20:41:00 +0000107/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner50c90502008-10-12 01:15:46 +0000108/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
109/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner02b436a2007-10-17 20:41:00 +0000110Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattner50c90502008-10-12 01:15:46 +0000111 const char *BufStart, const char *BufEnd,
112 const llvm::MemoryBuffer *FromFile)
Chris Lattner02b436a2007-10-17 20:41:00 +0000113 : FileLoc(fileloc), PP(0), Features(features) {
114 Is_PragmaLexer = false;
115 InitCharacterInfo();
116
Chris Lattner6b0c5ad2008-10-12 01:23:27 +0000117 // If a MemoryBuffer was specified, use its start as BufferStart. This affects
118 // the source location objects produced by this lexer.
Chris Lattner50c90502008-10-12 01:15:46 +0000119 BufferStart = FromFile ? FromFile->getBufferStart() : BufStart;
Chris Lattner02b436a2007-10-17 20:41:00 +0000120 BufferPtr = BufStart;
121 BufferEnd = BufEnd;
122
123 assert(BufferEnd[0] == 0 &&
124 "We assume that the input buffer has a null character at the end"
125 " to simplify lexing!");
126
127 // Start of the file is a start of line.
128 IsAtStartOfLine = true;
129
130 // We are not after parsing a #.
131 ParsingPreprocessorDirective = false;
132
133 // We are not after parsing #include.
134 ParsingFilename = false;
135
136 // We *are* in raw mode.
137 LexingRawMode = true;
138
Chris Lattnere3f863a2008-10-12 01:34:51 +0000139 // Default to not keeping comments in raw mode.
Chris Lattner02b436a2007-10-17 20:41:00 +0000140 KeepCommentMode = false;
141}
142
143
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000144/// Stringify - Convert the specified string into a C string, with surrounding
145/// ""'s, and with escaped \ and " characters.
Chris Lattnerecc39e92006-07-15 05:23:31 +0000146std::string Lexer::Stringify(const std::string &Str, bool Charify) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000147 std::string Result = Str;
Chris Lattnerecc39e92006-07-15 05:23:31 +0000148 char Quote = Charify ? '\'' : '"';
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000149 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
Chris Lattnerecc39e92006-07-15 05:23:31 +0000150 if (Result[i] == '\\' || Result[i] == Quote) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000151 Result.insert(Result.begin()+i, '\\');
152 ++i; ++e;
153 }
154 }
Chris Lattnerecc39e92006-07-15 05:23:31 +0000155 return Result;
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000156}
157
Chris Lattner4c4a2452007-07-24 06:57:14 +0000158/// Stringify - Convert the specified string into a C string by escaping '\'
159/// and " characters. This does not add surrounding ""'s to the string.
160void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
161 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
162 if (Str[i] == '\\' || Str[i] == '"') {
163 Str.insert(Str.begin()+i, '\\');
164 ++i; ++e;
165 }
166 }
167}
168
Chris Lattner22eb9722006-06-18 05:43:12 +0000169
Chris Lattner8e129c22007-10-17 21:18:47 +0000170/// MeasureTokenLength - Relex the token at the specified location and return
171/// its length in bytes in the input file. If the token needs cleaning (e.g.
172/// includes a trigraph or an escaped newline) then this count includes bytes
173/// that are part of that.
174unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
175 const SourceManager &SM) {
176 // If this comes from a macro expansion, we really do want the macro name, not
177 // the token this macro expanded to.
178 Loc = SM.getLogicalLoc(Loc);
179
180 const char *StrData = SM.getCharacterData(Loc);
181
182 // TODO: this could be special cased for common tokens like identifiers, ')',
183 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
184 // all obviously single-char tokens. This could use
185 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
186 // something.
187
188
189 const char *BufEnd = SM.getBufferData(Loc.getFileID()).second;
190
191 // Create a langops struct and enable trigraphs. This is sufficient for
192 // measuring tokens.
193 LangOptions LangOpts;
194 LangOpts.Trigraphs = true;
195
196 // Create a lexer starting at the beginning of this token.
197 Lexer TheLexer(Loc, LangOpts, StrData, BufEnd);
198 Token TheTok;
Chris Lattner50c90502008-10-12 01:15:46 +0000199 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner8e129c22007-10-17 21:18:47 +0000200 return TheTok.getLength();
201}
202
Chris Lattner22eb9722006-06-18 05:43:12 +0000203//===----------------------------------------------------------------------===//
204// Character information.
205//===----------------------------------------------------------------------===//
206
207static unsigned char CharInfo[256];
208
209enum {
210 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
211 CHAR_VERT_WS = 0x02, // '\r', '\n'
212 CHAR_LETTER = 0x04, // a-z,A-Z
213 CHAR_NUMBER = 0x08, // 0-9
214 CHAR_UNDER = 0x10, // _
215 CHAR_PERIOD = 0x20 // .
216};
217
218static void InitCharacterInfo() {
219 static bool isInited = false;
220 if (isInited) return;
221 isInited = true;
222
223 // Intiialize the CharInfo table.
224 // TODO: statically initialize this.
225 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
226 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
227 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
228
229 CharInfo[(int)'_'] = CHAR_UNDER;
Chris Lattnerdd0b7cb2006-10-17 02:53:51 +0000230 CharInfo[(int)'.'] = CHAR_PERIOD;
Chris Lattner22eb9722006-06-18 05:43:12 +0000231 for (unsigned i = 'a'; i <= 'z'; ++i)
232 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
233 for (unsigned i = '0'; i <= '9'; ++i)
234 CharInfo[i] = CHAR_NUMBER;
235}
236
237/// isIdentifierBody - Return true if this is the body character of an
238/// identifier, which is [a-zA-Z0-9_].
239static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000240 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000241}
242
243/// isHorizontalWhitespace - Return true if this character is horizontal
244/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
245static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000246 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000247}
248
249/// isWhitespace - Return true if this character is horizontal or vertical
250/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
251/// for '\0'.
252static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000253 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000254}
255
256/// isNumberBody - Return true if this is the body character of an
257/// preprocessing number, which is [a-zA-Z0-9_.].
258static inline bool isNumberBody(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000259 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
260 true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000261}
262
Chris Lattnerd01e2912006-06-18 16:22:51 +0000263
Chris Lattner22eb9722006-06-18 05:43:12 +0000264//===----------------------------------------------------------------------===//
265// Diagnostics forwarding code.
266//===----------------------------------------------------------------------===//
267
Chris Lattner619c1742007-07-22 18:38:25 +0000268/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
269/// lexer buffer was all instantiated at a single point, perform the mapping.
270/// This is currently only used for _Pragma implementation, so it is the slow
271/// path of the hot getSourceLocation method. Do not allow it to be inlined.
272static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
273 SourceLocation FileLoc,
274 unsigned CharNo) DISABLE_INLINE;
275static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
276 SourceLocation FileLoc,
277 unsigned CharNo) {
278 // Otherwise, we're lexing "mapped tokens". This is used for things like
279 // _Pragma handling. Combine the instantiation location of FileLoc with the
280 // physical location.
281 SourceManager &SourceMgr = PP.getSourceManager();
282
283 // Create a new SLoc which is expanded from logical(FileLoc) but whose
284 // characters come from phys(FileLoc)+Offset.
285 SourceLocation VirtLoc = SourceMgr.getLogicalLoc(FileLoc);
286 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(FileLoc);
287 PhysLoc = SourceLocation::getFileLoc(PhysLoc.getFileID(), CharNo);
288 return SourceMgr.getInstantiationLoc(PhysLoc, VirtLoc);
289}
290
Chris Lattner22eb9722006-06-18 05:43:12 +0000291/// getSourceLocation - Return a source location identifier for the specified
292/// offset in the current file.
293SourceLocation Lexer::getSourceLocation(const char *Loc) const {
Chris Lattner5d1c0272007-07-22 18:44:36 +0000294 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Chris Lattner4cca5ba2006-07-02 20:05:54 +0000295 "Location out of range for this buffer!");
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000296
297 // In the normal case, we're just lexing from a simple file buffer, return
298 // the file id from FileLoc with the offset specified.
Chris Lattner5d1c0272007-07-22 18:44:36 +0000299 unsigned CharNo = Loc-BufferStart;
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000300 if (FileLoc.isFileID())
301 return SourceLocation::getFileLoc(FileLoc.getFileID(), CharNo);
302
Chris Lattner02b436a2007-10-17 20:41:00 +0000303 assert(PP && "This doesn't work on raw lexers");
304 return GetMappedTokenLoc(*PP, FileLoc, CharNo);
Chris Lattner22eb9722006-06-18 05:43:12 +0000305}
306
Chris Lattner22eb9722006-06-18 05:43:12 +0000307/// Diag - Forwarding function for diagnostics. This translate a source
308/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000309void Lexer::Diag(const char *Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000310 const std::string &Msg) const {
Chris Lattner4431a1b2007-11-30 22:53:43 +0000311 if (LexingRawMode && Diagnostic::isBuiltinNoteWarningOrExtension(DiagID))
Chris Lattner538d7f32006-07-20 04:31:52 +0000312 return;
Chris Lattner02b436a2007-10-17 20:41:00 +0000313 PP->Diag(getSourceLocation(Loc), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000314}
Chris Lattner538d7f32006-07-20 04:31:52 +0000315void Lexer::Diag(SourceLocation Loc, unsigned DiagID,
316 const std::string &Msg) const {
Chris Lattner4431a1b2007-11-30 22:53:43 +0000317 if (LexingRawMode && Diagnostic::isBuiltinNoteWarningOrExtension(DiagID))
Chris Lattner538d7f32006-07-20 04:31:52 +0000318 return;
Chris Lattner02b436a2007-10-17 20:41:00 +0000319 PP->Diag(Loc, DiagID, Msg);
Chris Lattner538d7f32006-07-20 04:31:52 +0000320}
321
Chris Lattner22eb9722006-06-18 05:43:12 +0000322
323//===----------------------------------------------------------------------===//
324// Trigraph and Escaped Newline Handling Code.
325//===----------------------------------------------------------------------===//
326
327/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
328/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
329static char GetTrigraphCharForLetter(char Letter) {
330 switch (Letter) {
331 default: return 0;
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 case '-': return '~';
341 }
342}
343
344/// DecodeTrigraphChar - If the specified character is a legal trigraph when
345/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
346/// return the result character. Finally, emit a warning about trigraph use
347/// whether trigraphs are enabled or not.
348static char DecodeTrigraphChar(const char *CP, Lexer *L) {
349 char Res = GetTrigraphCharForLetter(*CP);
350 if (Res && L) {
351 if (!L->getFeatures().Trigraphs) {
352 L->Diag(CP-2, diag::trigraph_ignored);
353 return 0;
354 } else {
355 L->Diag(CP-2, diag::trigraph_converted, std::string()+Res);
356 }
357 }
358 return Res;
359}
360
361/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
362/// get its size, and return it. This is tricky in several cases:
363/// 1. If currently at the start of a trigraph, we warn about the trigraph,
364/// then either return the trigraph (skipping 3 chars) or the '?',
365/// depending on whether trigraphs are enabled or not.
366/// 2. If this is an escaped newline (potentially with whitespace between
367/// the backslash and newline), implicitly skip the newline and return
368/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000369/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000370///
371/// This handles the slow/uncommon case of the getCharAndSize method. Here we
372/// know that we can accumulate into Size, and that we have already incremented
373/// Ptr by Size bytes.
374///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000375/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
376/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000377///
378char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattner146762e2007-07-20 16:59:19 +0000379 Token *Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000380 // If we have a slash, look for an escaped newline.
381 if (Ptr[0] == '\\') {
382 ++Size;
383 ++Ptr;
384Slash:
385 // Common case, backslash-char where the char is not whitespace.
386 if (!isWhitespace(Ptr[0])) return '\\';
387
388 // See if we have optional whitespace characters followed by a newline.
389 {
390 unsigned SizeTmp = 0;
391 do {
392 ++SizeTmp;
393 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
394 // Remember that this token needs to be cleaned.
Chris Lattner146762e2007-07-20 16:59:19 +0000395 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000396
397 // Warn if there was whitespace between the backslash and newline.
398 if (SizeTmp != 1 && Tok)
399 Diag(Ptr, diag::backslash_newline_space);
400
401 // If this is a \r\n or \n\r, skip the newlines.
402 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
403 Ptr[SizeTmp-1] != Ptr[SizeTmp])
404 ++SizeTmp;
405
406 // Found backslash<whitespace><newline>. Parse the char after it.
407 Size += SizeTmp;
408 Ptr += SizeTmp;
409 // Use slow version to accumulate a correct size field.
410 return getCharAndSizeSlow(Ptr, Size, Tok);
411 }
412 } while (isWhitespace(Ptr[SizeTmp]));
413 }
414
415 // Otherwise, this is not an escaped newline, just return the slash.
416 return '\\';
417 }
418
419 // If this is a trigraph, process it.
420 if (Ptr[0] == '?' && Ptr[1] == '?') {
421 // If this is actually a legal trigraph (not something like "??x"), emit
422 // a trigraph warning. If so, and if trigraphs are enabled, return it.
423 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
424 // Remember that this token needs to be cleaned.
Chris Lattner146762e2007-07-20 16:59:19 +0000425 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000426
427 Ptr += 3;
428 Size += 3;
429 if (C == '\\') goto Slash;
430 return C;
431 }
432 }
433
434 // If this is neither, return a single character.
435 ++Size;
436 return *Ptr;
437}
438
Chris Lattnerd01e2912006-06-18 16:22:51 +0000439
Chris Lattner22eb9722006-06-18 05:43:12 +0000440/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
441/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
442/// and that we have already incremented Ptr by Size bytes.
443///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000444/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
445/// be updated to match.
446char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000447 const LangOptions &Features) {
448 // If we have a slash, look for an escaped newline.
449 if (Ptr[0] == '\\') {
450 ++Size;
451 ++Ptr;
452Slash:
453 // Common case, backslash-char where the char is not whitespace.
454 if (!isWhitespace(Ptr[0])) return '\\';
455
456 // See if we have optional whitespace characters followed by a newline.
457 {
458 unsigned SizeTmp = 0;
459 do {
460 ++SizeTmp;
461 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
462
463 // If this is a \r\n or \n\r, skip the newlines.
464 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
465 Ptr[SizeTmp-1] != Ptr[SizeTmp])
466 ++SizeTmp;
467
468 // Found backslash<whitespace><newline>. Parse the char after it.
469 Size += SizeTmp;
470 Ptr += SizeTmp;
471
472 // Use slow version to accumulate a correct size field.
473 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
474 }
475 } while (isWhitespace(Ptr[SizeTmp]));
476 }
477
478 // Otherwise, this is not an escaped newline, just return the slash.
479 return '\\';
480 }
481
482 // If this is a trigraph, process it.
483 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
484 // If this is actually a legal trigraph (not something like "??x"), return
485 // it.
486 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
487 Ptr += 3;
488 Size += 3;
489 if (C == '\\') goto Slash;
490 return C;
491 }
492 }
493
494 // If this is neither, return a single character.
495 ++Size;
496 return *Ptr;
497}
498
Chris Lattner22eb9722006-06-18 05:43:12 +0000499//===----------------------------------------------------------------------===//
500// Helper methods for lexing.
501//===----------------------------------------------------------------------===//
502
Chris Lattner146762e2007-07-20 16:59:19 +0000503void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000504 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
505 unsigned Size;
506 unsigned char C = *CurPtr++;
507 while (isIdentifierBody(C)) {
508 C = *CurPtr++;
509 }
510 --CurPtr; // Back up over the skipped character.
511
512 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
513 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000514 // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000515 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
516FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000517 const char *IdStart = BufferPtr;
Chris Lattnerd01e2912006-06-18 16:22:51 +0000518 FormTokenWithChars(Result, CurPtr);
Chris Lattner8c204872006-10-14 05:19:21 +0000519 Result.setKind(tok::identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000520
Chris Lattner0f1f5052006-07-20 04:16:23 +0000521 // If we are in raw mode, return this identifier raw. There is no need to
522 // look up identifier information or attempt to macro expand it.
523 if (LexingRawMode) return;
524
Chris Lattnercefc7682006-07-08 08:28:12 +0000525 // Fill in Result.IdentifierInfo, looking up the identifier in the
526 // identifier table.
Chris Lattner02b436a2007-10-17 20:41:00 +0000527 PP->LookUpIdentifierInfo(Result, IdStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000528
Chris Lattnerc5a00062006-06-18 16:41:01 +0000529 // Finally, now that we know we have an identifier, pass this off to the
530 // preprocessor, which may macro expand it or something.
Chris Lattner02b436a2007-10-17 20:41:00 +0000531 return PP->HandleIdentifier(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000532 }
533
534 // Otherwise, $,\,? in identifier found. Enter slower path.
535
536 C = getCharAndSize(CurPtr, Size);
537 while (1) {
538 if (C == '$') {
539 // If we hit a $ and they are not supported in identifiers, we are done.
540 if (!Features.DollarIdents) goto FinishIdentifier;
541
542 // Otherwise, emit a diagnostic and continue.
Chris Lattnercb283342006-06-18 06:48:37 +0000543 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000544 CurPtr = ConsumeChar(CurPtr, Size, Result);
545 C = getCharAndSize(CurPtr, Size);
546 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000547 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000548 // Found end of identifier.
549 goto FinishIdentifier;
550 }
551
552 // Otherwise, this character is good, consume it.
553 CurPtr = ConsumeChar(CurPtr, Size, Result);
554
555 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000556 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000557 CurPtr = ConsumeChar(CurPtr, Size, Result);
558 C = getCharAndSize(CurPtr, Size);
559 }
560 }
561}
562
563
Nate Begeman5eee9332008-04-14 02:26:39 +0000564/// LexNumericConstant - Lex the remainder of a integer or floating point
Chris Lattner22eb9722006-06-18 05:43:12 +0000565/// constant. From[-1] is the first character lexed. Return the end of the
566/// constant.
Chris Lattner146762e2007-07-20 16:59:19 +0000567void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000568 unsigned Size;
569 char C = getCharAndSize(CurPtr, Size);
570 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000571 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000572 CurPtr = ConsumeChar(CurPtr, Size, Result);
573 PrevCh = C;
574 C = getCharAndSize(CurPtr, Size);
575 }
576
577 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
578 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
579 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
580
581 // If we have a hex FP constant, continue.
582 if (Features.HexFloats &&
583 (C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p'))
584 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
585
Chris Lattner8c204872006-10-14 05:19:21 +0000586 Result.setKind(tok::numeric_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000587
Chris Lattnerd01e2912006-06-18 16:22:51 +0000588 // Update the location of token as well as BufferPtr.
589 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000590}
591
592/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
593/// either " or L".
Chris Lattner146762e2007-07-20 16:59:19 +0000594void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide){
Chris Lattner22eb9722006-06-18 05:43:12 +0000595 const char *NulCharacter = 0; // Does this string contain the \0 character?
596
597 char C = getAndAdvanceChar(CurPtr, Result);
598 while (C != '"') {
599 // Skip escaped characters.
600 if (C == '\\') {
601 // Skip the escaped character.
602 C = getAndAdvanceChar(CurPtr, Result);
603 } else if (C == '\n' || C == '\r' || // Newline.
604 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000605 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000606 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000607 FormTokenWithChars(Result, CurPtr-1);
608 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000609 } else if (C == 0) {
610 NulCharacter = CurPtr-1;
611 }
612 C = getAndAdvanceChar(CurPtr, Result);
613 }
614
Chris Lattner5a78a022006-07-20 06:02:19 +0000615 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000616 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000617
Chris Lattner8c204872006-10-14 05:19:21 +0000618 Result.setKind(Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000619
Chris Lattnerd01e2912006-06-18 16:22:51 +0000620 // Update the location of the token as well as the BufferPtr instance var.
621 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000622}
623
624/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
625/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattner146762e2007-07-20 16:59:19 +0000626void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000627 const char *NulCharacter = 0; // Does this string contain the \0 character?
628
629 char C = getAndAdvanceChar(CurPtr, Result);
630 while (C != '>') {
631 // Skip escaped characters.
632 if (C == '\\') {
633 // Skip the escaped character.
634 C = getAndAdvanceChar(CurPtr, Result);
635 } else if (C == '\n' || C == '\r' || // Newline.
636 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000637 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000638 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000639 FormTokenWithChars(Result, CurPtr-1);
640 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000641 } else if (C == 0) {
642 NulCharacter = CurPtr-1;
643 }
644 C = getAndAdvanceChar(CurPtr, Result);
645 }
646
Chris Lattner5a78a022006-07-20 06:02:19 +0000647 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000648 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000649
Chris Lattner8c204872006-10-14 05:19:21 +0000650 Result.setKind(tok::angle_string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000651
Chris Lattnerd01e2912006-06-18 16:22:51 +0000652 // Update the location of token as well as BufferPtr.
653 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000654}
655
656
657/// LexCharConstant - Lex the remainder of a character constant, after having
658/// lexed either ' or L'.
Chris Lattner146762e2007-07-20 16:59:19 +0000659void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000660 const char *NulCharacter = 0; // Does this character contain the \0 character?
661
662 // Handle the common case of 'x' and '\y' efficiently.
663 char C = getAndAdvanceChar(CurPtr, Result);
664 if (C == '\'') {
Chris Lattnera5f4c882006-07-20 06:08:47 +0000665 if (!LexingRawMode) Diag(BufferPtr, diag::err_empty_character);
Chris Lattner8c204872006-10-14 05:19:21 +0000666 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000667 FormTokenWithChars(Result, CurPtr);
668 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000669 } else if (C == '\\') {
670 // Skip the escaped character.
671 // FIXME: UCN's.
672 C = getAndAdvanceChar(CurPtr, Result);
673 }
674
675 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
676 ++CurPtr;
677 } else {
678 // Fall back on generic code for embedded nulls, newlines, wide chars.
679 do {
680 // Skip escaped characters.
681 if (C == '\\') {
682 // Skip the escaped character.
683 C = getAndAdvanceChar(CurPtr, Result);
684 } else if (C == '\n' || C == '\r' || // Newline.
685 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000686 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner8c204872006-10-14 05:19:21 +0000687 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000688 FormTokenWithChars(Result, CurPtr-1);
689 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000690 } else if (C == 0) {
691 NulCharacter = CurPtr-1;
692 }
693 C = getAndAdvanceChar(CurPtr, Result);
694 } while (C != '\'');
695 }
696
Chris Lattnercb283342006-06-18 06:48:37 +0000697 if (NulCharacter) Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000698
Chris Lattner8c204872006-10-14 05:19:21 +0000699 Result.setKind(tok::char_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000700
Chris Lattnerd01e2912006-06-18 16:22:51 +0000701 // Update the location of token as well as BufferPtr.
702 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000703}
704
705/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
706/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattner146762e2007-07-20 16:59:19 +0000707void Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000708 // Whitespace - Skip it, then return the token after the whitespace.
709 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
710 while (1) {
711 // Skip horizontal whitespace very aggressively.
712 while (isHorizontalWhitespace(Char))
713 Char = *++CurPtr;
714
715 // Otherwise if we something other than whitespace, we're done.
716 if (Char != '\n' && Char != '\r')
717 break;
718
719 if (ParsingPreprocessorDirective) {
720 // End of preprocessor directive line, let LexTokenInternal handle this.
721 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000722 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000723 }
724
725 // ok, but handle newline.
726 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +0000727 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000728 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +0000729 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000730 Char = *++CurPtr;
731 }
732
733 // If this isn't immediately after a newline, there is leading space.
734 char PrevChar = CurPtr[-1];
735 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattner146762e2007-07-20 16:59:19 +0000736 Result.setFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000737
Chris Lattner22eb9722006-06-18 05:43:12 +0000738 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000739}
740
741// SkipBCPLComment - We have just read the // characters from input. Skip until
742// we find the newline character thats terminate the comment. Then update
743/// BufferPtr and return.
Chris Lattner146762e2007-07-20 16:59:19 +0000744bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000745 // If BCPL comments aren't explicitly enabled for this language, emit an
746 // extension warning.
747 if (!Features.BCPLComment) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000748 Diag(BufferPtr, diag::ext_bcpl_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000749
750 // Mark them enabled so we only emit one warning for this translation
751 // unit.
752 Features.BCPLComment = true;
753 }
754
755 // Scan over the body of the comment. The common case, when scanning, is that
756 // the comment contains normal ascii characters with nothing interesting in
757 // them. As such, optimize for this case with the inner loop.
758 char C;
759 do {
760 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +0000761 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
762 // If we find a \n character, scan backwards, checking to see if it's an
763 // escaped newline, like we do for block comments.
Chris Lattner22eb9722006-06-18 05:43:12 +0000764
765 // Skip over characters in the fast loop.
766 while (C != 0 && // Potentially EOF.
767 C != '\\' && // Potentially escaped newline.
768 C != '?' && // Potentially trigraph.
769 C != '\n' && C != '\r') // Newline or DOS-style newline.
770 C = *++CurPtr;
771
772 // If this is a newline, we're done.
773 if (C == '\n' || C == '\r')
774 break; // Found the newline? Break out!
775
776 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
777 // properly decode the character.
778 const char *OldPtr = CurPtr;
779 C = getAndAdvanceChar(CurPtr, Result);
780
781 // If we read multiple characters, and one of those characters was a \r or
Chris Lattnerff591e22007-06-09 06:07:22 +0000782 // \n, then we had an escaped newline within the comment. Emit diagnostic
783 // unless the next line is also a // comment.
784 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000785 for (; OldPtr != CurPtr; ++OldPtr)
786 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnerff591e22007-06-09 06:07:22 +0000787 // Okay, we found a // comment that ends in a newline, if the next
788 // line is also a // comment, but has spaces, don't emit a diagnostic.
789 if (isspace(C)) {
790 const char *ForwardPtr = CurPtr;
791 while (isspace(*ForwardPtr)) // Skip whitespace.
792 ++ForwardPtr;
793 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
794 break;
795 }
796
Chris Lattnercb283342006-06-18 06:48:37 +0000797 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
798 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000799 }
800 }
801
Chris Lattner457fc152006-07-29 06:30:25 +0000802 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
Chris Lattner22eb9722006-06-18 05:43:12 +0000803 } while (C != '\n' && C != '\r');
804
Chris Lattner457fc152006-07-29 06:30:25 +0000805 // Found but did not consume the newline.
806
807 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +0000808 if (inKeepCommentMode())
Chris Lattner457fc152006-07-29 06:30:25 +0000809 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000810
811 // If we are inside a preprocessor directive and we see the end of line,
812 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +0000813 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000814 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000815 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000816 }
817
818 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner87e97ea2008-10-12 00:23:07 +0000819 // \r\n sequence. This is an efficiency hack (because we know the \n can't
820 // contribute to another token), it isn't needed for correctness.
Chris Lattner22eb9722006-06-18 05:43:12 +0000821 ++CurPtr;
822
823 // The next returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +0000824 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000825 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +0000826 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000827 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000828 return true;
829}
Chris Lattner22eb9722006-06-18 05:43:12 +0000830
Chris Lattner457fc152006-07-29 06:30:25 +0000831/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
832/// an appropriate way and return it.
Chris Lattner146762e2007-07-20 16:59:19 +0000833bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner8c204872006-10-14 05:19:21 +0000834 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +0000835 FormTokenWithChars(Result, CurPtr);
836
837 // If this BCPL-style comment is in a macro definition, transmogrify it into
838 // a C-style block comment.
839 if (ParsingPreprocessorDirective) {
Chris Lattner02b436a2007-10-17 20:41:00 +0000840 std::string Spelling = PP->getSpelling(Result);
Chris Lattner457fc152006-07-29 06:30:25 +0000841 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
842 Spelling[1] = '*'; // Change prefix to "/*".
843 Spelling += "*/"; // add suffix.
844
Chris Lattner02b436a2007-10-17 20:41:00 +0000845 Result.setLocation(PP->CreateString(&Spelling[0], Spelling.size(),
846 Result.getLocation()));
Chris Lattner8c204872006-10-14 05:19:21 +0000847 Result.setLength(Spelling.size());
Chris Lattner457fc152006-07-29 06:30:25 +0000848 }
849 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000850}
851
Chris Lattnercb283342006-06-18 06:48:37 +0000852/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
853/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner22eb9722006-06-18 05:43:12 +0000854/// diagnostic if so. We know that the is inside of a block comment.
Chris Lattner1f583052006-06-18 06:53:56 +0000855static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
856 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000857 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Chris Lattner22eb9722006-06-18 05:43:12 +0000858
859 // Back up off the newline.
860 --CurPtr;
861
862 // If this is a two-character newline sequence, skip the other character.
863 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
864 // \n\n or \r\r -> not escaped newline.
865 if (CurPtr[0] == CurPtr[1])
866 return false;
867 // \n\r or \r\n -> skip the newline.
868 --CurPtr;
869 }
870
871 // If we have horizontal whitespace, skip over it. We allow whitespace
872 // between the slash and newline.
873 bool HasSpace = false;
874 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
875 --CurPtr;
876 HasSpace = true;
877 }
878
879 // If we have a slash, we know this is an escaped newline.
880 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +0000881 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000882 } else {
883 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +0000884 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
885 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +0000886 return false;
Chris Lattnercb283342006-06-18 06:48:37 +0000887
888 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +0000889 CurPtr -= 2;
890
891 // If no trigraphs are enabled, warn that we ignored this trigraph and
892 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +0000893 if (!L->getFeatures().Trigraphs) {
894 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000895 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000896 }
Chris Lattner1f583052006-06-18 06:53:56 +0000897 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000898 }
899
900 // Warn about having an escaped newline between the */ characters.
Chris Lattner1f583052006-06-18 06:53:56 +0000901 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Chris Lattner22eb9722006-06-18 05:43:12 +0000902
903 // If there was space between the backslash and newline, warn about it.
Chris Lattner1f583052006-06-18 06:53:56 +0000904 if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space);
Chris Lattner22eb9722006-06-18 05:43:12 +0000905
Chris Lattnercb283342006-06-18 06:48:37 +0000906 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000907}
908
Chris Lattneraded4a92006-10-27 04:42:31 +0000909#ifdef __SSE2__
910#include <emmintrin.h>
Chris Lattner9f6604f2006-10-30 20:01:22 +0000911#elif __ALTIVEC__
912#include <altivec.h>
913#undef bool
Chris Lattneraded4a92006-10-27 04:42:31 +0000914#endif
915
Chris Lattner22eb9722006-06-18 05:43:12 +0000916/// SkipBlockComment - We have just read the /* characters from input. Read
917/// until we find the */ characters that terminate the comment. Note that we
918/// don't bother decoding trigraphs or escaped newlines in block comments,
919/// because they cannot cause the comment to end. The only thing that can
920/// happen is the comment could end with an escaped newline between the */ end
921/// of comment.
Chris Lattner146762e2007-07-20 16:59:19 +0000922bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000923 // Scan one character past where we should, looking for a '/' character. Once
924 // we find it, check to see if it was preceeded by a *. This common
925 // optimization helps people who like to put a lot of * characters in their
926 // comments.
Chris Lattnerc850ad62007-07-21 23:43:37 +0000927
928 // The first character we get with newlines and trigraphs skipped to handle
929 // the degenerate /*/ case below correctly if the * has an escaped newline
930 // after it.
931 unsigned CharSize;
932 unsigned char C = getCharAndSize(CurPtr, CharSize);
933 CurPtr += CharSize;
Chris Lattner22eb9722006-06-18 05:43:12 +0000934 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner7c2e9802008-10-12 01:31:51 +0000935 if (!LexingRawMode)
936 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000937 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +0000938 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000939 }
940
Chris Lattnerc850ad62007-07-21 23:43:37 +0000941 // Check to see if the first character after the '/*' is another /. If so,
942 // then this slash does not end the block comment, it is part of it.
943 if (C == '/')
944 C = *CurPtr++;
945
Chris Lattner22eb9722006-06-18 05:43:12 +0000946 while (1) {
Chris Lattner6cc3e362006-10-27 04:12:35 +0000947 // Skip over all non-interesting characters until we find end of buffer or a
948 // (probably ending) '/' character.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000949 if (CurPtr + 24 < BufferEnd) {
950 // While not aligned to a 16-byte boundary.
951 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
952 C = *CurPtr++;
953
954 if (C == '/') goto FoundSlash;
Chris Lattneraded4a92006-10-27 04:42:31 +0000955
956#ifdef __SSE2__
957 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
958 '/', '/', '/', '/', '/', '/', '/', '/');
959 while (CurPtr+16 <= BufferEnd &&
960 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
961 CurPtr += 16;
Chris Lattner9f6604f2006-10-30 20:01:22 +0000962#elif __ALTIVEC__
963 __vector unsigned char Slashes = {
964 '/', '/', '/', '/', '/', '/', '/', '/',
965 '/', '/', '/', '/', '/', '/', '/', '/'
966 };
967 while (CurPtr+16 <= BufferEnd &&
968 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
969 CurPtr += 16;
970#else
Chris Lattneraded4a92006-10-27 04:42:31 +0000971 // Scan for '/' quickly. Many block comments are very large.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000972 while (CurPtr[0] != '/' &&
973 CurPtr[1] != '/' &&
974 CurPtr[2] != '/' &&
975 CurPtr[3] != '/' &&
976 CurPtr+4 < BufferEnd) {
977 CurPtr += 4;
978 }
Chris Lattneraded4a92006-10-27 04:42:31 +0000979#endif
980
981 // It has to be one of the bytes scanned, increment to it and read one.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000982 C = *CurPtr++;
983 }
984
Chris Lattneraded4a92006-10-27 04:42:31 +0000985 // Loop to scan the remainder.
Chris Lattner22eb9722006-06-18 05:43:12 +0000986 while (C != '/' && C != '\0')
987 C = *CurPtr++;
988
Chris Lattner6cc3e362006-10-27 04:12:35 +0000989 FoundSlash:
Chris Lattner22eb9722006-06-18 05:43:12 +0000990 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000991 if (CurPtr[-2] == '*') // We found the final */. We're done!
992 break;
993
994 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +0000995 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000996 // We found the final */, though it had an escaped newline between the
997 // * and /. We're done!
998 break;
999 }
1000 }
1001 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1002 // If this is a /* inside of the comment, emit a warning. Don't do this
1003 // if this is a /*/, which will end the comment. This misses cases with
1004 // embedded escaped newlines, but oh well.
Chris Lattner7c2e9802008-10-12 01:31:51 +00001005 Diag(CurPtr-1, diag::warn_nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001006 }
1007 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner7c2e9802008-10-12 01:31:51 +00001008 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001009 // Note: the user probably forgot a */. We could continue immediately
1010 // after the /*, but this would involve lexing a lot of what really is the
1011 // comment, which surely would confuse the parser.
1012 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +00001013 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001014 }
1015 C = *CurPtr++;
1016 }
Chris Lattner457fc152006-07-29 06:30:25 +00001017
1018 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +00001019 if (inKeepCommentMode()) {
Chris Lattner8c204872006-10-14 05:19:21 +00001020 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +00001021 FormTokenWithChars(Result, CurPtr);
1022 return false;
1023 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001024
1025 // It is common for the tokens immediately after a /**/ comment to be
1026 // whitespace. Instead of going through the big switch, handle it
1027 // efficiently now.
1028 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner146762e2007-07-20 16:59:19 +00001029 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +00001030 SkipWhitespace(Result, CurPtr+1);
1031 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001032 }
1033
1034 // Otherwise, just return so that the next character will be lexed as a token.
1035 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001036 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +00001037 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001038}
1039
1040//===----------------------------------------------------------------------===//
1041// Primary Lexing Entry Points
1042//===----------------------------------------------------------------------===//
1043
1044/// LexIncludeFilename - After the preprocessor has parsed a #include, lex and
1045/// (potentially) macro expand the filename.
Chris Lattner146762e2007-07-20 16:59:19 +00001046void Lexer::LexIncludeFilename(Token &FilenameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001047 assert(ParsingPreprocessorDirective &&
1048 ParsingFilename == false &&
1049 "Must be in a preprocessing directive!");
1050
1051 // We are now parsing a filename!
1052 ParsingFilename = true;
1053
Chris Lattner269c2322006-06-25 06:23:00 +00001054 // Lex the filename.
1055 Lex(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +00001056
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001057 // We should have obtained the filename now.
Chris Lattner22eb9722006-06-18 05:43:12 +00001058 ParsingFilename = false;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +00001059
Chris Lattner22eb9722006-06-18 05:43:12 +00001060 // No filename?
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001061 if (FilenameTok.is(tok::eom))
Chris Lattner538d7f32006-07-20 04:31:52 +00001062 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner22eb9722006-06-18 05:43:12 +00001063}
1064
1065/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1066/// uninterpreted string. This switches the lexer out of directive mode.
1067std::string Lexer::ReadToEndOfLine() {
1068 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1069 "Must be in a preprocessing directive!");
1070 std::string Result;
Chris Lattner146762e2007-07-20 16:59:19 +00001071 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001072
1073 // CurPtr - Cache BufferPtr in an automatic variable.
1074 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001075 while (1) {
1076 char Char = getAndAdvanceChar(CurPtr, Tmp);
1077 switch (Char) {
1078 default:
1079 Result += Char;
1080 break;
1081 case 0: // Null.
1082 // Found end of file?
1083 if (CurPtr-1 != BufferEnd) {
1084 // Nope, normal character, continue.
1085 Result += Char;
1086 break;
1087 }
1088 // FALL THROUGH.
1089 case '\r':
1090 case '\n':
1091 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1092 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1093 BufferPtr = CurPtr-1;
1094
1095 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +00001096 Lex(Tmp);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001097 assert(Tmp.is(tok::eom) && "Unexpected token!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001098
1099 // Finally, we're done, return the string we found.
1100 return Result;
1101 }
1102 }
1103}
1104
1105/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1106/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +00001107/// This returns true if Result contains a token, false if PP.Lex should be
1108/// called again.
Chris Lattner146762e2007-07-20 16:59:19 +00001109bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001110 // If we hit the end of the file while parsing a preprocessor directive,
1111 // end the preprocessor directive first. The next token returned will
1112 // then be the end of file.
1113 if (ParsingPreprocessorDirective) {
1114 // Done parsing the "line".
1115 ParsingPreprocessorDirective = false;
Chris Lattner8c204872006-10-14 05:19:21 +00001116 Result.setKind(tok::eom);
Chris Lattnerd01e2912006-06-18 16:22:51 +00001117 // Update the location of token as well as BufferPtr.
1118 FormTokenWithChars(Result, CurPtr);
Chris Lattner457fc152006-07-29 06:30:25 +00001119
1120 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001121 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner2183a6e2006-07-18 06:36:12 +00001122 return true; // Have a token.
Chris Lattner22eb9722006-06-18 05:43:12 +00001123 }
1124
Chris Lattner30a2fa12006-07-19 06:31:49 +00001125 // If we are in raw mode, return this event as an EOF token. Let the caller
1126 // that put us in raw mode handle the event.
1127 if (LexingRawMode) {
Chris Lattner8c204872006-10-14 05:19:21 +00001128 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +00001129 BufferPtr = BufferEnd;
1130 FormTokenWithChars(Result, BufferEnd);
Chris Lattner8c204872006-10-14 05:19:21 +00001131 Result.setKind(tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001132 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001133 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001134
Chris Lattner30a2fa12006-07-19 06:31:49 +00001135 // Otherwise, issue diagnostics for unterminated #if and missing newline.
1136
1137 // If we are in a #if directive, emit an error.
1138 while (!ConditionalStack.empty()) {
Chris Lattner538d7f32006-07-20 04:31:52 +00001139 Diag(ConditionalStack.back().IfLoc, diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001140 ConditionalStack.pop_back();
1141 }
1142
Chris Lattner8f96d042008-04-12 05:54:25 +00001143 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1144 // a pedwarn.
1145 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Chris Lattner30a2fa12006-07-19 06:31:49 +00001146 Diag(BufferEnd, diag::ext_no_newline_eof);
1147
Chris Lattner22eb9722006-06-18 05:43:12 +00001148 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +00001149
1150 // Finally, let the preprocessor handle this.
Chris Lattner02b436a2007-10-17 20:41:00 +00001151 return PP->HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001152}
1153
Chris Lattner678c8802006-07-11 05:46:12 +00001154/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1155/// the specified lexer will return a tok::l_paren token, 0 if it is something
1156/// else and 2 if there are no more tokens in the buffer controlled by the
1157/// lexer.
1158unsigned Lexer::isNextPPTokenLParen() {
1159 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1160
1161 // Switch to 'skipping' mode. This will ensure that we can lex a token
1162 // without emitting diagnostics, disables macro expansion, and will cause EOF
1163 // to return an EOF token instead of popping the include stack.
1164 LexingRawMode = true;
1165
1166 // Save state that can be changed while lexing so that we can restore it.
1167 const char *TmpBufferPtr = BufferPtr;
1168
Chris Lattner146762e2007-07-20 16:59:19 +00001169 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001170 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001171 LexTokenInternal(Tok);
1172
1173 // Restore state that may have changed.
1174 BufferPtr = TmpBufferPtr;
1175
1176 // Restore the lexer back to non-skipping mode.
1177 LexingRawMode = false;
1178
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001179 if (Tok.is(tok::eof))
Chris Lattner678c8802006-07-11 05:46:12 +00001180 return 2;
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001181 return Tok.is(tok::l_paren);
Chris Lattner678c8802006-07-11 05:46:12 +00001182}
1183
Chris Lattner22eb9722006-06-18 05:43:12 +00001184
1185/// LexTokenInternal - This implements a simple C family lexer. It is an
1186/// extremely performance critical piece of code. This assumes that the buffer
1187/// has a null character at the end of the file. Return true if an error
1188/// occurred and compilation should terminate, false if normal. This returns a
1189/// preprocessing token, not a normal token, as such, it is an internal
1190/// interface. It assumes that the Flags of result have been cleared before
1191/// calling this.
Chris Lattner146762e2007-07-20 16:59:19 +00001192void Lexer::LexTokenInternal(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001193LexNextToken:
1194 // New token, can't need cleaning yet.
Chris Lattner146762e2007-07-20 16:59:19 +00001195 Result.clearFlag(Token::NeedsCleaning);
Chris Lattner8c204872006-10-14 05:19:21 +00001196 Result.setIdentifierInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001197
1198 // CurPtr - Cache BufferPtr in an automatic variable.
1199 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001200
Chris Lattnereb54b592006-07-10 06:34:27 +00001201 // Small amounts of horizontal whitespace is very common between tokens.
1202 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1203 ++CurPtr;
1204 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1205 ++CurPtr;
1206 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001207 Result.setFlag(Token::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001208 }
1209
Chris Lattner22eb9722006-06-18 05:43:12 +00001210 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1211
1212 // Read a character, advancing over it.
1213 char Char = getAndAdvanceChar(CurPtr, Result);
1214 switch (Char) {
1215 case 0: // Null.
1216 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001217 if (CurPtr-1 == BufferEnd) {
1218 // Read the PP instance variable into an automatic variable, because
1219 // LexEndOfFile will often delete 'this'.
Chris Lattner02b436a2007-10-17 20:41:00 +00001220 Preprocessor *PPCache = PP;
Chris Lattner2183a6e2006-07-18 06:36:12 +00001221 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1222 return; // Got a token to return.
Chris Lattner02b436a2007-10-17 20:41:00 +00001223 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1224 return PPCache->Lex(Result);
Chris Lattner2183a6e2006-07-18 06:36:12 +00001225 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001226
Chris Lattnercb283342006-06-18 06:48:37 +00001227 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner146762e2007-07-20 16:59:19 +00001228 Result.setFlag(Token::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001229 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001230 goto LexNextToken; // GCC isn't tail call eliminating.
1231 case '\n':
1232 case '\r':
1233 // If we are inside a preprocessor directive and we see the end of line,
1234 // we know we are done with the directive, so return an EOM token.
1235 if (ParsingPreprocessorDirective) {
1236 // Done parsing the "line".
1237 ParsingPreprocessorDirective = false;
1238
Chris Lattner457fc152006-07-29 06:30:25 +00001239 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001240 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner457fc152006-07-29 06:30:25 +00001241
Chris Lattner22eb9722006-06-18 05:43:12 +00001242 // Since we consumed a newline, we are back at the start of a line.
1243 IsAtStartOfLine = true;
1244
Chris Lattner8c204872006-10-14 05:19:21 +00001245 Result.setKind(tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001246 break;
1247 }
1248 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001249 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001250 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001251 Result.clearFlag(Token::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001252 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001253 goto LexNextToken; // GCC isn't tail call eliminating.
1254 case ' ':
1255 case '\t':
1256 case '\f':
1257 case '\v':
Chris Lattnerb9b85972007-07-22 06:29:05 +00001258 SkipHorizontalWhitespace:
Chris Lattner146762e2007-07-20 16:59:19 +00001259 Result.setFlag(Token::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001260 SkipWhitespace(Result, CurPtr);
Chris Lattnerb9b85972007-07-22 06:29:05 +00001261
1262 SkipIgnoredUnits:
1263 CurPtr = BufferPtr;
1264
1265 // If the next token is obviously a // or /* */ comment, skip it efficiently
1266 // too (without going through the big switch stmt).
Chris Lattner8637abd2008-10-12 03:22:02 +00001267 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode()) {
Chris Lattnerb9b85972007-07-22 06:29:05 +00001268 SkipBCPLComment(Result, CurPtr+2);
1269 goto SkipIgnoredUnits;
Chris Lattner8637abd2008-10-12 03:22:02 +00001270 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattnerb9b85972007-07-22 06:29:05 +00001271 SkipBlockComment(Result, CurPtr+2);
1272 goto SkipIgnoredUnits;
1273 } else if (isHorizontalWhitespace(*CurPtr)) {
1274 goto SkipHorizontalWhitespace;
1275 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001276 goto LexNextToken; // GCC isn't tail call eliminating.
1277
Chris Lattner2b15cf72008-01-03 17:58:54 +00001278 // C99 6.4.4.1: Integer Constants.
1279 // C99 6.4.4.2: Floating Constants.
1280 case '0': case '1': case '2': case '3': case '4':
1281 case '5': case '6': case '7': case '8': case '9':
1282 // Notify MIOpt that we read a non-whitespace/non-comment token.
1283 MIOpt.ReadToken();
1284 return LexNumericConstant(Result, CurPtr);
1285
1286 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Chris Lattner371ac8a2006-07-04 07:11:10 +00001287 // Notify MIOpt that we read a non-whitespace/non-comment token.
1288 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001289 Char = getCharAndSize(CurPtr, SizeTmp);
1290
1291 // Wide string literal.
1292 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001293 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1294 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001295
1296 // Wide character constant.
1297 if (Char == '\'')
1298 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1299 // FALL THROUGH, treating L like the start of an identifier.
1300
1301 // C99 6.4.2: Identifiers.
1302 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1303 case 'H': case 'I': case 'J': case 'K': /*'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 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1307 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1308 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1309 case 'v': case 'w': case 'x': case 'y': case 'z':
1310 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001311 // Notify MIOpt that we read a non-whitespace/non-comment token.
1312 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001313 return LexIdentifier(Result, CurPtr);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001314
1315 case '$': // $ in identifiers.
1316 if (Features.DollarIdents) {
1317 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
1318 // Notify MIOpt that we read a non-whitespace/non-comment token.
1319 MIOpt.ReadToken();
1320 return LexIdentifier(Result, CurPtr);
1321 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001322
Chris Lattner2b15cf72008-01-03 17:58:54 +00001323 Result.setKind(tok::unknown);
1324 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001325
1326 // C99 6.4.4: Character Constants.
1327 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001328 // Notify MIOpt that we read a non-whitespace/non-comment token.
1329 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001330 return LexCharConstant(Result, CurPtr);
1331
1332 // C99 6.4.5: String Literals.
1333 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001334 // Notify MIOpt that we read a non-whitespace/non-comment token.
1335 MIOpt.ReadToken();
Chris Lattnerd3e98952006-10-06 05:22:26 +00001336 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001337
1338 // C99 6.4.6: Punctuators.
1339 case '?':
Chris Lattner8c204872006-10-14 05:19:21 +00001340 Result.setKind(tok::question);
Chris Lattner22eb9722006-06-18 05:43:12 +00001341 break;
1342 case '[':
Chris Lattner8c204872006-10-14 05:19:21 +00001343 Result.setKind(tok::l_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001344 break;
1345 case ']':
Chris Lattner8c204872006-10-14 05:19:21 +00001346 Result.setKind(tok::r_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001347 break;
1348 case '(':
Chris Lattner8c204872006-10-14 05:19:21 +00001349 Result.setKind(tok::l_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001350 break;
1351 case ')':
Chris Lattner8c204872006-10-14 05:19:21 +00001352 Result.setKind(tok::r_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001353 break;
1354 case '{':
Chris Lattner8c204872006-10-14 05:19:21 +00001355 Result.setKind(tok::l_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001356 break;
1357 case '}':
Chris Lattner8c204872006-10-14 05:19:21 +00001358 Result.setKind(tok::r_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001359 break;
1360 case '.':
1361 Char = getCharAndSize(CurPtr, SizeTmp);
1362 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001363 // Notify MIOpt that we read a non-whitespace/non-comment token.
1364 MIOpt.ReadToken();
1365
Chris Lattner22eb9722006-06-18 05:43:12 +00001366 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1367 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001368 Result.setKind(tok::periodstar);
Chris Lattner22eb9722006-06-18 05:43:12 +00001369 CurPtr += SizeTmp;
1370 } else if (Char == '.' &&
1371 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner8c204872006-10-14 05:19:21 +00001372 Result.setKind(tok::ellipsis);
Chris Lattner22eb9722006-06-18 05:43:12 +00001373 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1374 SizeTmp2, Result);
1375 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001376 Result.setKind(tok::period);
Chris Lattner22eb9722006-06-18 05:43:12 +00001377 }
1378 break;
1379 case '&':
1380 Char = getCharAndSize(CurPtr, SizeTmp);
1381 if (Char == '&') {
Chris Lattner8c204872006-10-14 05:19:21 +00001382 Result.setKind(tok::ampamp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001383 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1384 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001385 Result.setKind(tok::ampequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001386 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1387 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001388 Result.setKind(tok::amp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001389 }
1390 break;
1391 case '*':
1392 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001393 Result.setKind(tok::starequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001394 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1395 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001396 Result.setKind(tok::star);
Chris Lattner22eb9722006-06-18 05:43:12 +00001397 }
1398 break;
1399 case '+':
1400 Char = getCharAndSize(CurPtr, SizeTmp);
1401 if (Char == '+') {
Chris Lattner8c204872006-10-14 05:19:21 +00001402 Result.setKind(tok::plusplus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001403 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1404 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001405 Result.setKind(tok::plusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001406 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1407 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001408 Result.setKind(tok::plus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001409 }
1410 break;
1411 case '-':
1412 Char = getCharAndSize(CurPtr, SizeTmp);
1413 if (Char == '-') {
Chris Lattner8c204872006-10-14 05:19:21 +00001414 Result.setKind(tok::minusminus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001415 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1416 } else if (Char == '>' && Features.CPlusPlus &&
1417 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001418 Result.setKind(tok::arrowstar); // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00001419 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1420 SizeTmp2, Result);
1421 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001422 Result.setKind(tok::arrow);
Chris Lattner22eb9722006-06-18 05:43:12 +00001423 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1424 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001425 Result.setKind(tok::minusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001426 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1427 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001428 Result.setKind(tok::minus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001429 }
1430 break;
1431 case '~':
Chris Lattner8c204872006-10-14 05:19:21 +00001432 Result.setKind(tok::tilde);
Chris Lattner22eb9722006-06-18 05:43:12 +00001433 break;
1434 case '!':
1435 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001436 Result.setKind(tok::exclaimequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001437 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1438 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001439 Result.setKind(tok::exclaim);
Chris Lattner22eb9722006-06-18 05:43:12 +00001440 }
1441 break;
1442 case '/':
1443 // 6.4.9: Comments
1444 Char = getCharAndSize(CurPtr, SizeTmp);
1445 if (Char == '/') { // BCPL comment.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001446 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) {
1447 // It is common for the tokens immediately after a // comment to be
Chris Lattner619c1742007-07-22 18:38:25 +00001448 // whitespace (indentation for the next line). Instead of going through
1449 // the big switch, handle it efficiently now.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001450 goto SkipIgnoredUnits;
1451 }
Chris Lattner457fc152006-07-29 06:30:25 +00001452 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001453 } else if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001454 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1455 goto LexNextToken; // GCC isn't tail call eliminating.
1456 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001457 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001458 Result.setKind(tok::slashequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001459 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1460 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001461 Result.setKind(tok::slash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001462 }
1463 break;
1464 case '%':
1465 Char = getCharAndSize(CurPtr, SizeTmp);
1466 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001467 Result.setKind(tok::percentequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001468 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1469 } else if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001470 Result.setKind(tok::r_brace); // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00001471 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1472 } else if (Features.Digraphs && Char == ':') {
1473 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001474 Char = getCharAndSize(CurPtr, SizeTmp);
1475 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001476 Result.setKind(tok::hashhash); // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00001477 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1478 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001479 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001480 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001481 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1482 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner22eb9722006-06-18 05:43:12 +00001483 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001484 Result.setKind(tok::hash); // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00001485
1486 // We parsed a # character. If this occurs at the start of the line,
1487 // it's actually the start of a preprocessing directive. Callback to
1488 // the preprocessor to handle it.
1489 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001490 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001491 BufferPtr = CurPtr;
Chris Lattner02b436a2007-10-17 20:41:00 +00001492 PP->HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001493
1494 // As an optimization, if the preprocessor didn't switch lexers, tail
1495 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00001496 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001497 // Start a new token. If this is a #include or something, the PP may
1498 // want us starting at the beginning of the line again. If so, set
1499 // the StartOfLine flag.
1500 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00001501 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001502 IsAtStartOfLine = false;
1503 }
1504 goto LexNextToken; // GCC isn't tail call eliminating.
1505 }
1506
Chris Lattner02b436a2007-10-17 20:41:00 +00001507 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001508 }
1509 }
1510 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001511 Result.setKind(tok::percent);
Chris Lattner22eb9722006-06-18 05:43:12 +00001512 }
1513 break;
1514 case '<':
1515 Char = getCharAndSize(CurPtr, SizeTmp);
1516 if (ParsingFilename) {
1517 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1518 } else if (Char == '<' &&
1519 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001520 Result.setKind(tok::lesslessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001521 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1522 SizeTmp2, Result);
1523 } else if (Char == '<') {
Chris Lattner8c204872006-10-14 05:19:21 +00001524 Result.setKind(tok::lessless);
Chris Lattner22eb9722006-06-18 05:43:12 +00001525 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1526 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001527 Result.setKind(tok::lessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001528 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1529 } else if (Features.Digraphs && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001530 Result.setKind(tok::l_square); // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00001531 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner5329e7e2008-02-24 19:05:57 +00001532 } else if (Features.Digraphs && Char == '%') {
Chris Lattner8c204872006-10-14 05:19:21 +00001533 Result.setKind(tok::l_brace); // '<%' -> '{'
Chris Lattner22eb9722006-06-18 05:43:12 +00001534 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001535 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001536 Result.setKind(tok::less);
Chris Lattner22eb9722006-06-18 05:43:12 +00001537 }
1538 break;
1539 case '>':
1540 Char = getCharAndSize(CurPtr, SizeTmp);
1541 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001542 Result.setKind(tok::greaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001543 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1544 } else if (Char == '>' &&
1545 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001546 Result.setKind(tok::greatergreaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001547 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1548 SizeTmp2, Result);
1549 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001550 Result.setKind(tok::greatergreater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001551 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001552 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001553 Result.setKind(tok::greater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001554 }
1555 break;
1556 case '^':
1557 Char = getCharAndSize(CurPtr, SizeTmp);
1558 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001559 Result.setKind(tok::caretequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001560 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1561 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001562 Result.setKind(tok::caret);
Chris Lattner22eb9722006-06-18 05:43:12 +00001563 }
1564 break;
1565 case '|':
1566 Char = getCharAndSize(CurPtr, SizeTmp);
1567 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001568 Result.setKind(tok::pipeequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001569 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1570 } else if (Char == '|') {
Chris Lattner8c204872006-10-14 05:19:21 +00001571 Result.setKind(tok::pipepipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001572 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1573 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001574 Result.setKind(tok::pipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001575 }
1576 break;
1577 case ':':
1578 Char = getCharAndSize(CurPtr, SizeTmp);
1579 if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001580 Result.setKind(tok::r_square); // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00001581 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1582 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001583 Result.setKind(tok::coloncolon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001584 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1585 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001586 Result.setKind(tok::colon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001587 }
1588 break;
1589 case ';':
Chris Lattner8c204872006-10-14 05:19:21 +00001590 Result.setKind(tok::semi);
Chris Lattner22eb9722006-06-18 05:43:12 +00001591 break;
1592 case '=':
1593 Char = getCharAndSize(CurPtr, SizeTmp);
1594 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001595 Result.setKind(tok::equalequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001596 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1597 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001598 Result.setKind(tok::equal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001599 }
1600 break;
1601 case ',':
Chris Lattner8c204872006-10-14 05:19:21 +00001602 Result.setKind(tok::comma);
Chris Lattner22eb9722006-06-18 05:43:12 +00001603 break;
1604 case '#':
1605 Char = getCharAndSize(CurPtr, SizeTmp);
1606 if (Char == '#') {
Chris Lattner8c204872006-10-14 05:19:21 +00001607 Result.setKind(tok::hashhash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001608 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001609 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001610 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001611 Diag(BufferPtr, diag::charize_microsoft_ext);
1612 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001613 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001614 Result.setKind(tok::hash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001615 // We parsed a # character. If this occurs at the start of the line,
1616 // it's actually the start of a preprocessing directive. Callback to
1617 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00001618 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001619 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001620 BufferPtr = CurPtr;
Chris Lattner02b436a2007-10-17 20:41:00 +00001621 PP->HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001622
1623 // As an optimization, if the preprocessor didn't switch lexers, tail
1624 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00001625 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001626 // Start a new token. If this is a #include or something, the PP may
1627 // want us starting at the beginning of the line again. If so, set
1628 // the StartOfLine flag.
1629 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00001630 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001631 IsAtStartOfLine = false;
1632 }
1633 goto LexNextToken; // GCC isn't tail call eliminating.
1634 }
Chris Lattner02b436a2007-10-17 20:41:00 +00001635 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001636 }
1637 }
1638 break;
1639
Chris Lattner2b15cf72008-01-03 17:58:54 +00001640 case '@':
1641 // Objective C support.
1642 if (CurPtr[-1] == '@' && Features.ObjC1)
1643 Result.setKind(tok::at);
1644 else
1645 Result.setKind(tok::unknown);
1646 break;
1647
Chris Lattner22eb9722006-06-18 05:43:12 +00001648 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00001649 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00001650 // FALL THROUGH.
1651 default:
Chris Lattner8c204872006-10-14 05:19:21 +00001652 Result.setKind(tok::unknown);
Chris Lattner041bef82006-07-11 05:52:53 +00001653 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001654 }
1655
Chris Lattner371ac8a2006-07-04 07:11:10 +00001656 // Notify MIOpt that we read a non-whitespace/non-comment token.
1657 MIOpt.ReadToken();
1658
Chris Lattnerd01e2912006-06-18 16:22:51 +00001659 // Update the location of token as well as BufferPtr.
1660 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001661}