blob: a7a58f0b1fb949d434b6d1bd6ba6d25ef8077249 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner146762e2007-07-20 16:59:19 +000010// This file implements the Lexer and Token interfaces.
Chris Lattner22eb9722006-06-18 05:43:12 +000011//
12//===----------------------------------------------------------------------===//
13//
14// TODO: GCC Diagnostics emitted by the lexer:
15// PEDWARN: (form feed|vertical tab) in preprocessing directive
16//
17// Universal characters, unicode, char mapping:
18// WARNING: `%.*s' is not in NFKC
19// WARNING: `%.*s' is not in NFC
20//
21// Other:
Chris Lattner22eb9722006-06-18 05:43:12 +000022// TODO: Options to support:
23// -fexec-charset,-fwide-exec-charset
24//
25//===----------------------------------------------------------------------===//
26
27#include "clang/Lex/Lexer.h"
28#include "clang/Lex/Preprocessor.h"
29#include "clang/Basic/Diagnostic.h"
Chris Lattnerdc5c0552007-07-20 16:37:10 +000030#include "clang/Basic/SourceManager.h"
Chris 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
54//===----------------------------------------------------------------------===//
55// Lexer Class Implementation
56//===----------------------------------------------------------------------===//
57
58
Chris Lattner77e9de52007-07-20 16:52:03 +000059Lexer::Lexer(SourceLocation fileloc, Preprocessor &pp,
60 const char *BufStart, const char *BufEnd)
61 : FileLoc(fileloc), PP(pp), Features(PP.getLangOptions()) {
62
63 SourceManager &SourceMgr = PP.getSourceManager();
Chris Lattner5d1c0272007-07-22 18:44:36 +000064 unsigned InputFileID = SourceMgr.getPhysicalLoc(FileLoc).getFileID();
65 const llvm::MemoryBuffer *InputFile = SourceMgr.getBuffer(InputFileID);
Chris Lattner77e9de52007-07-20 16:52:03 +000066
Chris Lattnerecfeafe2006-07-02 21:26:45 +000067 Is_PragmaLexer = false;
Chris Lattner22eb9722006-06-18 05:43:12 +000068 InitCharacterInfo();
Chris Lattner5d1c0272007-07-22 18:44:36 +000069
70 // BufferStart must always be InputFile->getBufferStart().
71 BufferStart = InputFile->getBufferStart();
72
73 // BufferPtr and BufferEnd can start out somewhere inside the current buffer.
74 // If unspecified, they starts at the start/end of the buffer.
75 BufferPtr = BufStart ? BufStart : BufferStart;
Chris Lattner77e9de52007-07-20 16:52:03 +000076 BufferEnd = BufEnd ? BufEnd : InputFile->getBufferEnd();
77
Chris Lattner22eb9722006-06-18 05:43:12 +000078 assert(BufferEnd[0] == 0 &&
79 "We assume that the input buffer has a null character at the end"
80 " to simplify lexing!");
Chris Lattner77e9de52007-07-20 16:52:03 +000081
Chris Lattner22eb9722006-06-18 05:43:12 +000082 // Start of the file is a start of line.
83 IsAtStartOfLine = true;
84
85 // We are not after parsing a #.
86 ParsingPreprocessorDirective = false;
87
88 // We are not after parsing #include.
89 ParsingFilename = false;
Chris Lattner3ebcf4e2006-07-11 05:39:23 +000090
91 // We are not in raw mode. Raw mode disables diagnostics and interpretation
92 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
93 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
94 // or otherwise skipping over tokens.
95 LexingRawMode = false;
Chris Lattner457fc152006-07-29 06:30:25 +000096
97 // Default to keeping comments if requested.
Chris Lattnerb352e3e2006-11-21 06:17:10 +000098 KeepCommentMode = PP.getCommentRetentionState();
Chris Lattner22eb9722006-06-18 05:43:12 +000099}
100
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000101/// Stringify - Convert the specified string into a C string, with surrounding
102/// ""'s, and with escaped \ and " characters.
Chris Lattnerecc39e92006-07-15 05:23:31 +0000103std::string Lexer::Stringify(const std::string &Str, bool Charify) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000104 std::string Result = Str;
Chris Lattnerecc39e92006-07-15 05:23:31 +0000105 char Quote = Charify ? '\'' : '"';
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000106 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
Chris Lattnerecc39e92006-07-15 05:23:31 +0000107 if (Result[i] == '\\' || Result[i] == Quote) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000108 Result.insert(Result.begin()+i, '\\');
109 ++i; ++e;
110 }
111 }
Chris Lattnerecc39e92006-07-15 05:23:31 +0000112 return Result;
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000113}
114
Chris Lattner4c4a2452007-07-24 06:57:14 +0000115/// Stringify - Convert the specified string into a C string by escaping '\'
116/// and " characters. This does not add surrounding ""'s to the string.
117void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
118 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
119 if (Str[i] == '\\' || Str[i] == '"') {
120 Str.insert(Str.begin()+i, '\\');
121 ++i; ++e;
122 }
123 }
124}
125
Chris Lattner22eb9722006-06-18 05:43:12 +0000126
Chris Lattner22eb9722006-06-18 05:43:12 +0000127//===----------------------------------------------------------------------===//
128// Character information.
129//===----------------------------------------------------------------------===//
130
131static unsigned char CharInfo[256];
132
133enum {
134 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
135 CHAR_VERT_WS = 0x02, // '\r', '\n'
136 CHAR_LETTER = 0x04, // a-z,A-Z
137 CHAR_NUMBER = 0x08, // 0-9
138 CHAR_UNDER = 0x10, // _
139 CHAR_PERIOD = 0x20 // .
140};
141
142static void InitCharacterInfo() {
143 static bool isInited = false;
144 if (isInited) return;
145 isInited = true;
146
147 // Intiialize the CharInfo table.
148 // TODO: statically initialize this.
149 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
150 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
151 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
152
153 CharInfo[(int)'_'] = CHAR_UNDER;
Chris Lattnerdd0b7cb2006-10-17 02:53:51 +0000154 CharInfo[(int)'.'] = CHAR_PERIOD;
Chris Lattner22eb9722006-06-18 05:43:12 +0000155 for (unsigned i = 'a'; i <= 'z'; ++i)
156 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
157 for (unsigned i = '0'; i <= '9'; ++i)
158 CharInfo[i] = CHAR_NUMBER;
159}
160
161/// isIdentifierBody - Return true if this is the body character of an
162/// identifier, which is [a-zA-Z0-9_].
163static inline bool isIdentifierBody(unsigned char c) {
Chris Lattner4894f482007-10-07 08:47:24 +0000164 return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER);
Chris Lattner22eb9722006-06-18 05:43:12 +0000165}
166
167/// isHorizontalWhitespace - Return true if this character is horizontal
168/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
169static inline bool isHorizontalWhitespace(unsigned char c) {
Chris Lattner4894f482007-10-07 08:47:24 +0000170 return CharInfo[c] & CHAR_HORZ_WS;
Chris Lattner22eb9722006-06-18 05:43:12 +0000171}
172
173/// isWhitespace - Return true if this character is horizontal or vertical
174/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
175/// for '\0'.
176static inline bool isWhitespace(unsigned char c) {
Chris Lattner4894f482007-10-07 08:47:24 +0000177 return CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS);
Chris Lattner22eb9722006-06-18 05:43:12 +0000178}
179
180/// isNumberBody - Return true if this is the body character of an
181/// preprocessing number, which is [a-zA-Z0-9_.].
182static inline bool isNumberBody(unsigned char c) {
Chris Lattner4894f482007-10-07 08:47:24 +0000183 return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD);
Chris Lattner22eb9722006-06-18 05:43:12 +0000184}
185
Chris Lattnerd01e2912006-06-18 16:22:51 +0000186
Chris Lattner22eb9722006-06-18 05:43:12 +0000187//===----------------------------------------------------------------------===//
188// Diagnostics forwarding code.
189//===----------------------------------------------------------------------===//
190
Chris Lattner619c1742007-07-22 18:38:25 +0000191/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
192/// lexer buffer was all instantiated at a single point, perform the mapping.
193/// This is currently only used for _Pragma implementation, so it is the slow
194/// path of the hot getSourceLocation method. Do not allow it to be inlined.
195static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
196 SourceLocation FileLoc,
197 unsigned CharNo) DISABLE_INLINE;
198static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
199 SourceLocation FileLoc,
200 unsigned CharNo) {
201 // Otherwise, we're lexing "mapped tokens". This is used for things like
202 // _Pragma handling. Combine the instantiation location of FileLoc with the
203 // physical location.
204 SourceManager &SourceMgr = PP.getSourceManager();
205
206 // Create a new SLoc which is expanded from logical(FileLoc) but whose
207 // characters come from phys(FileLoc)+Offset.
208 SourceLocation VirtLoc = SourceMgr.getLogicalLoc(FileLoc);
209 SourceLocation PhysLoc = SourceMgr.getPhysicalLoc(FileLoc);
210 PhysLoc = SourceLocation::getFileLoc(PhysLoc.getFileID(), CharNo);
211 return SourceMgr.getInstantiationLoc(PhysLoc, VirtLoc);
212}
213
Chris Lattner22eb9722006-06-18 05:43:12 +0000214/// getSourceLocation - Return a source location identifier for the specified
215/// offset in the current file.
216SourceLocation Lexer::getSourceLocation(const char *Loc) const {
Chris Lattner5d1c0272007-07-22 18:44:36 +0000217 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Chris Lattner4cca5ba2006-07-02 20:05:54 +0000218 "Location out of range for this buffer!");
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000219
220 // In the normal case, we're just lexing from a simple file buffer, return
221 // the file id from FileLoc with the offset specified.
Chris Lattner5d1c0272007-07-22 18:44:36 +0000222 unsigned CharNo = Loc-BufferStart;
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000223 if (FileLoc.isFileID())
224 return SourceLocation::getFileLoc(FileLoc.getFileID(), CharNo);
225
Chris Lattner619c1742007-07-22 18:38:25 +0000226 return GetMappedTokenLoc(PP, FileLoc, CharNo);
Chris Lattner22eb9722006-06-18 05:43:12 +0000227}
228
Chris Lattner22eb9722006-06-18 05:43:12 +0000229/// Diag - Forwarding function for diagnostics. This translate a source
230/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000231void Lexer::Diag(const char *Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000232 const std::string &Msg) const {
Chris Lattner538d7f32006-07-20 04:31:52 +0000233 if (LexingRawMode && Diagnostic::isNoteWarningOrExtension(DiagID))
234 return;
Chris Lattnercb283342006-06-18 06:48:37 +0000235 PP.Diag(getSourceLocation(Loc), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000236}
Chris Lattner538d7f32006-07-20 04:31:52 +0000237void Lexer::Diag(SourceLocation Loc, unsigned DiagID,
238 const std::string &Msg) const {
239 if (LexingRawMode && Diagnostic::isNoteWarningOrExtension(DiagID))
240 return;
241 PP.Diag(Loc, DiagID, Msg);
242}
243
Chris Lattner22eb9722006-06-18 05:43:12 +0000244
245//===----------------------------------------------------------------------===//
246// Trigraph and Escaped Newline Handling Code.
247//===----------------------------------------------------------------------===//
248
249/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
250/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
251static char GetTrigraphCharForLetter(char Letter) {
252 switch (Letter) {
253 default: return 0;
254 case '=': return '#';
255 case ')': return ']';
256 case '(': return '[';
257 case '!': return '|';
258 case '\'': return '^';
259 case '>': return '}';
260 case '/': return '\\';
261 case '<': return '{';
262 case '-': return '~';
263 }
264}
265
266/// DecodeTrigraphChar - If the specified character is a legal trigraph when
267/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
268/// return the result character. Finally, emit a warning about trigraph use
269/// whether trigraphs are enabled or not.
270static char DecodeTrigraphChar(const char *CP, Lexer *L) {
271 char Res = GetTrigraphCharForLetter(*CP);
272 if (Res && L) {
273 if (!L->getFeatures().Trigraphs) {
274 L->Diag(CP-2, diag::trigraph_ignored);
275 return 0;
276 } else {
277 L->Diag(CP-2, diag::trigraph_converted, std::string()+Res);
278 }
279 }
280 return Res;
281}
282
283/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
284/// get its size, and return it. This is tricky in several cases:
285/// 1. If currently at the start of a trigraph, we warn about the trigraph,
286/// then either return the trigraph (skipping 3 chars) or the '?',
287/// depending on whether trigraphs are enabled or not.
288/// 2. If this is an escaped newline (potentially with whitespace between
289/// the backslash and newline), implicitly skip the newline and return
290/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000291/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000292///
293/// This handles the slow/uncommon case of the getCharAndSize method. Here we
294/// know that we can accumulate into Size, and that we have already incremented
295/// Ptr by Size bytes.
296///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000297/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
298/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000299///
300char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattner146762e2007-07-20 16:59:19 +0000301 Token *Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000302 // If we have a slash, look for an escaped newline.
303 if (Ptr[0] == '\\') {
304 ++Size;
305 ++Ptr;
306Slash:
307 // Common case, backslash-char where the char is not whitespace.
308 if (!isWhitespace(Ptr[0])) return '\\';
309
310 // See if we have optional whitespace characters followed by a newline.
311 {
312 unsigned SizeTmp = 0;
313 do {
314 ++SizeTmp;
315 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
316 // Remember that this token needs to be cleaned.
Chris Lattner146762e2007-07-20 16:59:19 +0000317 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000318
319 // Warn if there was whitespace between the backslash and newline.
320 if (SizeTmp != 1 && Tok)
321 Diag(Ptr, diag::backslash_newline_space);
322
323 // If this is a \r\n or \n\r, skip the newlines.
324 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
325 Ptr[SizeTmp-1] != Ptr[SizeTmp])
326 ++SizeTmp;
327
328 // Found backslash<whitespace><newline>. Parse the char after it.
329 Size += SizeTmp;
330 Ptr += SizeTmp;
331 // Use slow version to accumulate a correct size field.
332 return getCharAndSizeSlow(Ptr, Size, Tok);
333 }
334 } while (isWhitespace(Ptr[SizeTmp]));
335 }
336
337 // Otherwise, this is not an escaped newline, just return the slash.
338 return '\\';
339 }
340
341 // If this is a trigraph, process it.
342 if (Ptr[0] == '?' && Ptr[1] == '?') {
343 // If this is actually a legal trigraph (not something like "??x"), emit
344 // a trigraph warning. If so, and if trigraphs are enabled, return it.
345 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
346 // Remember that this token needs to be cleaned.
Chris Lattner146762e2007-07-20 16:59:19 +0000347 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000348
349 Ptr += 3;
350 Size += 3;
351 if (C == '\\') goto Slash;
352 return C;
353 }
354 }
355
356 // If this is neither, return a single character.
357 ++Size;
358 return *Ptr;
359}
360
Chris Lattnerd01e2912006-06-18 16:22:51 +0000361
Chris Lattner22eb9722006-06-18 05:43:12 +0000362/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
363/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
364/// and that we have already incremented Ptr by Size bytes.
365///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000366/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
367/// be updated to match.
368char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000369 const LangOptions &Features) {
370 // If we have a slash, look for an escaped newline.
371 if (Ptr[0] == '\\') {
372 ++Size;
373 ++Ptr;
374Slash:
375 // Common case, backslash-char where the char is not whitespace.
376 if (!isWhitespace(Ptr[0])) return '\\';
377
378 // See if we have optional whitespace characters followed by a newline.
379 {
380 unsigned SizeTmp = 0;
381 do {
382 ++SizeTmp;
383 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
384
385 // If this is a \r\n or \n\r, skip the newlines.
386 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
387 Ptr[SizeTmp-1] != Ptr[SizeTmp])
388 ++SizeTmp;
389
390 // Found backslash<whitespace><newline>. Parse the char after it.
391 Size += SizeTmp;
392 Ptr += SizeTmp;
393
394 // Use slow version to accumulate a correct size field.
395 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
396 }
397 } while (isWhitespace(Ptr[SizeTmp]));
398 }
399
400 // Otherwise, this is not an escaped newline, just return the slash.
401 return '\\';
402 }
403
404 // If this is a trigraph, process it.
405 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
406 // If this is actually a legal trigraph (not something like "??x"), return
407 // it.
408 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
409 Ptr += 3;
410 Size += 3;
411 if (C == '\\') goto Slash;
412 return C;
413 }
414 }
415
416 // If this is neither, return a single character.
417 ++Size;
418 return *Ptr;
419}
420
Chris Lattner22eb9722006-06-18 05:43:12 +0000421//===----------------------------------------------------------------------===//
422// Helper methods for lexing.
423//===----------------------------------------------------------------------===//
424
Chris Lattner146762e2007-07-20 16:59:19 +0000425void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000426 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
427 unsigned Size;
428 unsigned char C = *CurPtr++;
429 while (isIdentifierBody(C)) {
430 C = *CurPtr++;
431 }
432 --CurPtr; // Back up over the skipped character.
433
434 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
435 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000436 // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000437 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
438FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000439 const char *IdStart = BufferPtr;
Chris Lattnerd01e2912006-06-18 16:22:51 +0000440 FormTokenWithChars(Result, CurPtr);
Chris Lattner8c204872006-10-14 05:19:21 +0000441 Result.setKind(tok::identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000442
Chris Lattner0f1f5052006-07-20 04:16:23 +0000443 // If we are in raw mode, return this identifier raw. There is no need to
444 // look up identifier information or attempt to macro expand it.
445 if (LexingRawMode) return;
446
Chris Lattnercefc7682006-07-08 08:28:12 +0000447 // Fill in Result.IdentifierInfo, looking up the identifier in the
448 // identifier table.
449 PP.LookUpIdentifierInfo(Result, IdStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000450
Chris Lattnerc5a00062006-06-18 16:41:01 +0000451 // Finally, now that we know we have an identifier, pass this off to the
452 // preprocessor, which may macro expand it or something.
Chris Lattner22eb9722006-06-18 05:43:12 +0000453 return PP.HandleIdentifier(Result);
454 }
455
456 // Otherwise, $,\,? in identifier found. Enter slower path.
457
458 C = getCharAndSize(CurPtr, Size);
459 while (1) {
460 if (C == '$') {
461 // If we hit a $ and they are not supported in identifiers, we are done.
462 if (!Features.DollarIdents) goto FinishIdentifier;
463
464 // Otherwise, emit a diagnostic and continue.
Chris Lattnercb283342006-06-18 06:48:37 +0000465 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000466 CurPtr = ConsumeChar(CurPtr, Size, Result);
467 C = getCharAndSize(CurPtr, Size);
468 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000469 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000470 // Found end of identifier.
471 goto FinishIdentifier;
472 }
473
474 // Otherwise, this character is good, consume it.
475 CurPtr = ConsumeChar(CurPtr, Size, Result);
476
477 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000478 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000479 CurPtr = ConsumeChar(CurPtr, Size, Result);
480 C = getCharAndSize(CurPtr, Size);
481 }
482 }
483}
484
485
486/// LexNumericConstant - Lex the remainer of a integer or floating point
487/// constant. From[-1] is the first character lexed. Return the end of the
488/// constant.
Chris Lattner146762e2007-07-20 16:59:19 +0000489void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000490 unsigned Size;
491 char C = getCharAndSize(CurPtr, Size);
492 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000493 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000494 CurPtr = ConsumeChar(CurPtr, Size, Result);
495 PrevCh = C;
496 C = getCharAndSize(CurPtr, Size);
497 }
498
499 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
500 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
501 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
502
503 // If we have a hex FP constant, continue.
504 if (Features.HexFloats &&
505 (C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p'))
506 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
507
Chris Lattner8c204872006-10-14 05:19:21 +0000508 Result.setKind(tok::numeric_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000509
Chris Lattnerd01e2912006-06-18 16:22:51 +0000510 // Update the location of token as well as BufferPtr.
511 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000512}
513
514/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
515/// either " or L".
Chris Lattner146762e2007-07-20 16:59:19 +0000516void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide){
Chris Lattner22eb9722006-06-18 05:43:12 +0000517 const char *NulCharacter = 0; // Does this string contain the \0 character?
518
519 char C = getAndAdvanceChar(CurPtr, Result);
520 while (C != '"') {
521 // Skip escaped characters.
522 if (C == '\\') {
523 // Skip the escaped character.
524 C = getAndAdvanceChar(CurPtr, Result);
525 } else if (C == '\n' || C == '\r' || // Newline.
526 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000527 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000528 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000529 FormTokenWithChars(Result, CurPtr-1);
530 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000531 } else if (C == 0) {
532 NulCharacter = CurPtr-1;
533 }
534 C = getAndAdvanceChar(CurPtr, Result);
535 }
536
Chris Lattner5a78a022006-07-20 06:02:19 +0000537 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000538 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000539
Chris Lattner8c204872006-10-14 05:19:21 +0000540 Result.setKind(Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000541
Chris Lattnerd01e2912006-06-18 16:22:51 +0000542 // Update the location of the token as well as the BufferPtr instance var.
543 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000544}
545
546/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
547/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattner146762e2007-07-20 16:59:19 +0000548void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000549 const char *NulCharacter = 0; // Does this string contain the \0 character?
550
551 char C = getAndAdvanceChar(CurPtr, Result);
552 while (C != '>') {
553 // Skip escaped characters.
554 if (C == '\\') {
555 // Skip the escaped character.
556 C = getAndAdvanceChar(CurPtr, Result);
557 } else if (C == '\n' || C == '\r' || // Newline.
558 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000559 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000560 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000561 FormTokenWithChars(Result, CurPtr-1);
562 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000563 } else if (C == 0) {
564 NulCharacter = CurPtr-1;
565 }
566 C = getAndAdvanceChar(CurPtr, Result);
567 }
568
Chris Lattner5a78a022006-07-20 06:02:19 +0000569 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000570 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000571
Chris Lattner8c204872006-10-14 05:19:21 +0000572 Result.setKind(tok::angle_string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000573
Chris Lattnerd01e2912006-06-18 16:22:51 +0000574 // Update the location of token as well as BufferPtr.
575 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000576}
577
578
579/// LexCharConstant - Lex the remainder of a character constant, after having
580/// lexed either ' or L'.
Chris Lattner146762e2007-07-20 16:59:19 +0000581void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000582 const char *NulCharacter = 0; // Does this character contain the \0 character?
583
584 // Handle the common case of 'x' and '\y' efficiently.
585 char C = getAndAdvanceChar(CurPtr, Result);
586 if (C == '\'') {
Chris Lattnera5f4c882006-07-20 06:08:47 +0000587 if (!LexingRawMode) Diag(BufferPtr, diag::err_empty_character);
Chris Lattner8c204872006-10-14 05:19:21 +0000588 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000589 FormTokenWithChars(Result, CurPtr);
590 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000591 } else if (C == '\\') {
592 // Skip the escaped character.
593 // FIXME: UCN's.
594 C = getAndAdvanceChar(CurPtr, Result);
595 }
596
597 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
598 ++CurPtr;
599 } else {
600 // Fall back on generic code for embedded nulls, newlines, wide chars.
601 do {
602 // Skip escaped characters.
603 if (C == '\\') {
604 // Skip the escaped character.
605 C = getAndAdvanceChar(CurPtr, Result);
606 } else if (C == '\n' || C == '\r' || // Newline.
607 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000608 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner8c204872006-10-14 05:19:21 +0000609 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000610 FormTokenWithChars(Result, CurPtr-1);
611 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000612 } else if (C == 0) {
613 NulCharacter = CurPtr-1;
614 }
615 C = getAndAdvanceChar(CurPtr, Result);
616 } while (C != '\'');
617 }
618
Chris Lattnercb283342006-06-18 06:48:37 +0000619 if (NulCharacter) Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000620
Chris Lattner8c204872006-10-14 05:19:21 +0000621 Result.setKind(tok::char_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000622
Chris Lattnerd01e2912006-06-18 16:22:51 +0000623 // Update the location of token as well as BufferPtr.
624 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000625}
626
627/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
628/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattner146762e2007-07-20 16:59:19 +0000629void Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000630 // Whitespace - Skip it, then return the token after the whitespace.
631 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
632 while (1) {
633 // Skip horizontal whitespace very aggressively.
634 while (isHorizontalWhitespace(Char))
635 Char = *++CurPtr;
636
637 // Otherwise if we something other than whitespace, we're done.
638 if (Char != '\n' && Char != '\r')
639 break;
640
641 if (ParsingPreprocessorDirective) {
642 // End of preprocessor directive line, let LexTokenInternal handle this.
643 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000644 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000645 }
646
647 // ok, but handle newline.
648 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +0000649 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000650 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +0000651 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000652 Char = *++CurPtr;
653 }
654
655 // If this isn't immediately after a newline, there is leading space.
656 char PrevChar = CurPtr[-1];
657 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattner146762e2007-07-20 16:59:19 +0000658 Result.setFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000659
Chris Lattner22eb9722006-06-18 05:43:12 +0000660 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000661}
662
663// SkipBCPLComment - We have just read the // characters from input. Skip until
664// we find the newline character thats terminate the comment. Then update
665/// BufferPtr and return.
Chris Lattner146762e2007-07-20 16:59:19 +0000666bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000667 // If BCPL comments aren't explicitly enabled for this language, emit an
668 // extension warning.
669 if (!Features.BCPLComment) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000670 Diag(BufferPtr, diag::ext_bcpl_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000671
672 // Mark them enabled so we only emit one warning for this translation
673 // unit.
674 Features.BCPLComment = true;
675 }
676
677 // Scan over the body of the comment. The common case, when scanning, is that
678 // the comment contains normal ascii characters with nothing interesting in
679 // them. As such, optimize for this case with the inner loop.
680 char C;
681 do {
682 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +0000683 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
684 // If we find a \n character, scan backwards, checking to see if it's an
685 // escaped newline, like we do for block comments.
Chris Lattner22eb9722006-06-18 05:43:12 +0000686
687 // Skip over characters in the fast loop.
688 while (C != 0 && // Potentially EOF.
689 C != '\\' && // Potentially escaped newline.
690 C != '?' && // Potentially trigraph.
691 C != '\n' && C != '\r') // Newline or DOS-style newline.
692 C = *++CurPtr;
693
694 // If this is a newline, we're done.
695 if (C == '\n' || C == '\r')
696 break; // Found the newline? Break out!
697
698 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
699 // properly decode the character.
700 const char *OldPtr = CurPtr;
701 C = getAndAdvanceChar(CurPtr, Result);
702
703 // If we read multiple characters, and one of those characters was a \r or
Chris Lattnerff591e22007-06-09 06:07:22 +0000704 // \n, then we had an escaped newline within the comment. Emit diagnostic
705 // unless the next line is also a // comment.
706 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000707 for (; OldPtr != CurPtr; ++OldPtr)
708 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnerff591e22007-06-09 06:07:22 +0000709 // Okay, we found a // comment that ends in a newline, if the next
710 // line is also a // comment, but has spaces, don't emit a diagnostic.
711 if (isspace(C)) {
712 const char *ForwardPtr = CurPtr;
713 while (isspace(*ForwardPtr)) // Skip whitespace.
714 ++ForwardPtr;
715 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
716 break;
717 }
718
Chris Lattnercb283342006-06-18 06:48:37 +0000719 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
720 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000721 }
722 }
723
Chris Lattner457fc152006-07-29 06:30:25 +0000724 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
Chris Lattner22eb9722006-06-18 05:43:12 +0000725 } while (C != '\n' && C != '\r');
726
Chris Lattner457fc152006-07-29 06:30:25 +0000727 // Found but did not consume the newline.
728
729 // If we are returning comments as tokens, return this comment as a token.
730 if (KeepCommentMode)
731 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000732
733 // If we are inside a preprocessor directive and we see the end of line,
734 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +0000735 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000736 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000737 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000738 }
739
740 // Otherwise, eat the \n character. We don't care if this is a \n\r or
741 // \r\n sequence.
742 ++CurPtr;
743
744 // The next returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +0000745 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000746 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +0000747 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000748 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000749 return true;
750}
Chris Lattner22eb9722006-06-18 05:43:12 +0000751
Chris Lattner457fc152006-07-29 06:30:25 +0000752/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
753/// an appropriate way and return it.
Chris Lattner146762e2007-07-20 16:59:19 +0000754bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner8c204872006-10-14 05:19:21 +0000755 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +0000756 FormTokenWithChars(Result, CurPtr);
757
758 // If this BCPL-style comment is in a macro definition, transmogrify it into
759 // a C-style block comment.
760 if (ParsingPreprocessorDirective) {
761 std::string Spelling = PP.getSpelling(Result);
762 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
763 Spelling[1] = '*'; // Change prefix to "/*".
764 Spelling += "*/"; // add suffix.
765
Chris Lattner8c204872006-10-14 05:19:21 +0000766 Result.setLocation(PP.CreateString(&Spelling[0], Spelling.size(),
Chris Lattner457fc152006-07-29 06:30:25 +0000767 Result.getLocation()));
Chris Lattner8c204872006-10-14 05:19:21 +0000768 Result.setLength(Spelling.size());
Chris Lattner457fc152006-07-29 06:30:25 +0000769 }
770 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000771}
772
Chris Lattnercb283342006-06-18 06:48:37 +0000773/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
774/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner22eb9722006-06-18 05:43:12 +0000775/// diagnostic if so. We know that the is inside of a block comment.
Chris Lattner1f583052006-06-18 06:53:56 +0000776static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
777 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000778 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Chris Lattner22eb9722006-06-18 05:43:12 +0000779
780 // Back up off the newline.
781 --CurPtr;
782
783 // If this is a two-character newline sequence, skip the other character.
784 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
785 // \n\n or \r\r -> not escaped newline.
786 if (CurPtr[0] == CurPtr[1])
787 return false;
788 // \n\r or \r\n -> skip the newline.
789 --CurPtr;
790 }
791
792 // If we have horizontal whitespace, skip over it. We allow whitespace
793 // between the slash and newline.
794 bool HasSpace = false;
795 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
796 --CurPtr;
797 HasSpace = true;
798 }
799
800 // If we have a slash, we know this is an escaped newline.
801 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +0000802 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000803 } else {
804 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +0000805 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
806 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +0000807 return false;
Chris Lattnercb283342006-06-18 06:48:37 +0000808
809 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +0000810 CurPtr -= 2;
811
812 // If no trigraphs are enabled, warn that we ignored this trigraph and
813 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +0000814 if (!L->getFeatures().Trigraphs) {
815 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000816 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000817 }
Chris Lattner1f583052006-06-18 06:53:56 +0000818 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000819 }
820
821 // Warn about having an escaped newline between the */ characters.
Chris Lattner1f583052006-06-18 06:53:56 +0000822 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Chris Lattner22eb9722006-06-18 05:43:12 +0000823
824 // If there was space between the backslash and newline, warn about it.
Chris Lattner1f583052006-06-18 06:53:56 +0000825 if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space);
Chris Lattner22eb9722006-06-18 05:43:12 +0000826
Chris Lattnercb283342006-06-18 06:48:37 +0000827 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000828}
829
Chris Lattneraded4a92006-10-27 04:42:31 +0000830#ifdef __SSE2__
831#include <emmintrin.h>
Chris Lattner9f6604f2006-10-30 20:01:22 +0000832#elif __ALTIVEC__
833#include <altivec.h>
834#undef bool
Chris Lattneraded4a92006-10-27 04:42:31 +0000835#endif
836
Chris Lattner22eb9722006-06-18 05:43:12 +0000837/// SkipBlockComment - We have just read the /* characters from input. Read
838/// until we find the */ characters that terminate the comment. Note that we
839/// don't bother decoding trigraphs or escaped newlines in block comments,
840/// because they cannot cause the comment to end. The only thing that can
841/// happen is the comment could end with an escaped newline between the */ end
842/// of comment.
Chris Lattner146762e2007-07-20 16:59:19 +0000843bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000844 // Scan one character past where we should, looking for a '/' character. Once
845 // we find it, check to see if it was preceeded by a *. This common
846 // optimization helps people who like to put a lot of * characters in their
847 // comments.
Chris Lattnerc850ad62007-07-21 23:43:37 +0000848
849 // The first character we get with newlines and trigraphs skipped to handle
850 // the degenerate /*/ case below correctly if the * has an escaped newline
851 // after it.
852 unsigned CharSize;
853 unsigned char C = getCharAndSize(CurPtr, CharSize);
854 CurPtr += CharSize;
Chris Lattner22eb9722006-06-18 05:43:12 +0000855 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000856 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000857 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +0000858 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000859 }
860
Chris Lattnerc850ad62007-07-21 23:43:37 +0000861 // Check to see if the first character after the '/*' is another /. If so,
862 // then this slash does not end the block comment, it is part of it.
863 if (C == '/')
864 C = *CurPtr++;
865
Chris Lattner22eb9722006-06-18 05:43:12 +0000866 while (1) {
Chris Lattner6cc3e362006-10-27 04:12:35 +0000867 // Skip over all non-interesting characters until we find end of buffer or a
868 // (probably ending) '/' character.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000869 if (CurPtr + 24 < BufferEnd) {
870 // While not aligned to a 16-byte boundary.
871 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
872 C = *CurPtr++;
873
874 if (C == '/') goto FoundSlash;
Chris Lattneraded4a92006-10-27 04:42:31 +0000875
876#ifdef __SSE2__
877 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
878 '/', '/', '/', '/', '/', '/', '/', '/');
879 while (CurPtr+16 <= BufferEnd &&
880 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
881 CurPtr += 16;
Chris Lattner9f6604f2006-10-30 20:01:22 +0000882#elif __ALTIVEC__
883 __vector unsigned char Slashes = {
884 '/', '/', '/', '/', '/', '/', '/', '/',
885 '/', '/', '/', '/', '/', '/', '/', '/'
886 };
887 while (CurPtr+16 <= BufferEnd &&
888 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
889 CurPtr += 16;
890#else
Chris Lattneraded4a92006-10-27 04:42:31 +0000891 // Scan for '/' quickly. Many block comments are very large.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000892 while (CurPtr[0] != '/' &&
893 CurPtr[1] != '/' &&
894 CurPtr[2] != '/' &&
895 CurPtr[3] != '/' &&
896 CurPtr+4 < BufferEnd) {
897 CurPtr += 4;
898 }
Chris Lattneraded4a92006-10-27 04:42:31 +0000899#endif
900
901 // It has to be one of the bytes scanned, increment to it and read one.
Chris Lattner6cc3e362006-10-27 04:12:35 +0000902 C = *CurPtr++;
903 }
904
Chris Lattneraded4a92006-10-27 04:42:31 +0000905 // Loop to scan the remainder.
Chris Lattner22eb9722006-06-18 05:43:12 +0000906 while (C != '/' && C != '\0')
907 C = *CurPtr++;
908
Chris Lattner6cc3e362006-10-27 04:12:35 +0000909 FoundSlash:
Chris Lattner22eb9722006-06-18 05:43:12 +0000910 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000911 if (CurPtr[-2] == '*') // We found the final */. We're done!
912 break;
913
914 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +0000915 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000916 // We found the final */, though it had an escaped newline between the
917 // * and /. We're done!
918 break;
919 }
920 }
921 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
922 // If this is a /* inside of the comment, emit a warning. Don't do this
923 // if this is a /*/, which will end the comment. This misses cases with
924 // embedded escaped newlines, but oh well.
Chris Lattnercb283342006-06-18 06:48:37 +0000925 Diag(CurPtr-1, diag::nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000926 }
927 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000928 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000929 // Note: the user probably forgot a */. We could continue immediately
930 // after the /*, but this would involve lexing a lot of what really is the
931 // comment, which surely would confuse the parser.
932 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +0000933 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000934 }
935 C = *CurPtr++;
936 }
Chris Lattner457fc152006-07-29 06:30:25 +0000937
938 // If we are returning comments as tokens, return this comment as a token.
939 if (KeepCommentMode) {
Chris Lattner8c204872006-10-14 05:19:21 +0000940 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +0000941 FormTokenWithChars(Result, CurPtr);
942 return false;
943 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000944
945 // It is common for the tokens immediately after a /**/ comment to be
946 // whitespace. Instead of going through the big switch, handle it
947 // efficiently now.
948 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner146762e2007-07-20 16:59:19 +0000949 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000950 SkipWhitespace(Result, CurPtr+1);
951 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000952 }
953
954 // Otherwise, just return so that the next character will be lexed as a token.
955 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +0000956 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000957 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000958}
959
960//===----------------------------------------------------------------------===//
961// Primary Lexing Entry Points
962//===----------------------------------------------------------------------===//
963
964/// LexIncludeFilename - After the preprocessor has parsed a #include, lex and
965/// (potentially) macro expand the filename.
Chris Lattner146762e2007-07-20 16:59:19 +0000966void Lexer::LexIncludeFilename(Token &FilenameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000967 assert(ParsingPreprocessorDirective &&
968 ParsingFilename == false &&
969 "Must be in a preprocessing directive!");
970
971 // We are now parsing a filename!
972 ParsingFilename = true;
973
Chris Lattner269c2322006-06-25 06:23:00 +0000974 // Lex the filename.
975 Lex(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000976
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000977 // We should have obtained the filename now.
Chris Lattner22eb9722006-06-18 05:43:12 +0000978 ParsingFilename = false;
Chris Lattnerc07ba1f2006-10-30 05:58:32 +0000979
Chris Lattner22eb9722006-06-18 05:43:12 +0000980 // No filename?
Chris Lattner98c1f7c2007-10-09 18:02:16 +0000981 if (FilenameTok.is(tok::eom))
Chris Lattner538d7f32006-07-20 04:31:52 +0000982 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner22eb9722006-06-18 05:43:12 +0000983}
984
985/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
986/// uninterpreted string. This switches the lexer out of directive mode.
987std::string Lexer::ReadToEndOfLine() {
988 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
989 "Must be in a preprocessing directive!");
990 std::string Result;
Chris Lattner146762e2007-07-20 16:59:19 +0000991 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +0000992
993 // CurPtr - Cache BufferPtr in an automatic variable.
994 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000995 while (1) {
996 char Char = getAndAdvanceChar(CurPtr, Tmp);
997 switch (Char) {
998 default:
999 Result += Char;
1000 break;
1001 case 0: // Null.
1002 // Found end of file?
1003 if (CurPtr-1 != BufferEnd) {
1004 // Nope, normal character, continue.
1005 Result += Char;
1006 break;
1007 }
1008 // FALL THROUGH.
1009 case '\r':
1010 case '\n':
1011 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1012 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1013 BufferPtr = CurPtr-1;
1014
1015 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +00001016 Lex(Tmp);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001017 assert(Tmp.is(tok::eom) && "Unexpected token!");
Chris Lattner22eb9722006-06-18 05:43:12 +00001018
1019 // Finally, we're done, return the string we found.
1020 return Result;
1021 }
1022 }
1023}
1024
1025/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1026/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +00001027/// This returns true if Result contains a token, false if PP.Lex should be
1028/// called again.
Chris Lattner146762e2007-07-20 16:59:19 +00001029bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001030 // If we hit the end of the file while parsing a preprocessor directive,
1031 // end the preprocessor directive first. The next token returned will
1032 // then be the end of file.
1033 if (ParsingPreprocessorDirective) {
1034 // Done parsing the "line".
1035 ParsingPreprocessorDirective = false;
Chris Lattner8c204872006-10-14 05:19:21 +00001036 Result.setKind(tok::eom);
Chris Lattnerd01e2912006-06-18 16:22:51 +00001037 // Update the location of token as well as BufferPtr.
1038 FormTokenWithChars(Result, CurPtr);
Chris Lattner457fc152006-07-29 06:30:25 +00001039
1040 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001041 KeepCommentMode = PP.getCommentRetentionState();
Chris Lattner2183a6e2006-07-18 06:36:12 +00001042 return true; // Have a token.
Chris Lattner22eb9722006-06-18 05:43:12 +00001043 }
1044
Chris Lattner30a2fa12006-07-19 06:31:49 +00001045 // If we are in raw mode, return this event as an EOF token. Let the caller
1046 // that put us in raw mode handle the event.
1047 if (LexingRawMode) {
Chris Lattner8c204872006-10-14 05:19:21 +00001048 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +00001049 BufferPtr = BufferEnd;
1050 FormTokenWithChars(Result, BufferEnd);
Chris Lattner8c204872006-10-14 05:19:21 +00001051 Result.setKind(tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001052 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001053 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001054
Chris Lattner30a2fa12006-07-19 06:31:49 +00001055 // Otherwise, issue diagnostics for unterminated #if and missing newline.
1056
1057 // If we are in a #if directive, emit an error.
1058 while (!ConditionalStack.empty()) {
Chris Lattner538d7f32006-07-20 04:31:52 +00001059 Diag(ConditionalStack.back().IfLoc, diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001060 ConditionalStack.pop_back();
1061 }
1062
1063 // If the file was empty or didn't end in a newline, issue a pedwarn.
1064 if (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1065 Diag(BufferEnd, diag::ext_no_newline_eof);
1066
Chris Lattner22eb9722006-06-18 05:43:12 +00001067 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +00001068
1069 // Finally, let the preprocessor handle this.
Chris Lattner2183a6e2006-07-18 06:36:12 +00001070 return PP.HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001071}
1072
Chris Lattner678c8802006-07-11 05:46:12 +00001073/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1074/// the specified lexer will return a tok::l_paren token, 0 if it is something
1075/// else and 2 if there are no more tokens in the buffer controlled by the
1076/// lexer.
1077unsigned Lexer::isNextPPTokenLParen() {
1078 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1079
1080 // Switch to 'skipping' mode. This will ensure that we can lex a token
1081 // without emitting diagnostics, disables macro expansion, and will cause EOF
1082 // to return an EOF token instead of popping the include stack.
1083 LexingRawMode = true;
1084
1085 // Save state that can be changed while lexing so that we can restore it.
1086 const char *TmpBufferPtr = BufferPtr;
1087
Chris Lattner146762e2007-07-20 16:59:19 +00001088 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001089 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001090 LexTokenInternal(Tok);
1091
1092 // Restore state that may have changed.
1093 BufferPtr = TmpBufferPtr;
1094
1095 // Restore the lexer back to non-skipping mode.
1096 LexingRawMode = false;
1097
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001098 if (Tok.is(tok::eof))
Chris Lattner678c8802006-07-11 05:46:12 +00001099 return 2;
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001100 return Tok.is(tok::l_paren);
Chris Lattner678c8802006-07-11 05:46:12 +00001101}
1102
Chris Lattner22eb9722006-06-18 05:43:12 +00001103
1104/// LexTokenInternal - This implements a simple C family lexer. It is an
1105/// extremely performance critical piece of code. This assumes that the buffer
1106/// has a null character at the end of the file. Return true if an error
1107/// occurred and compilation should terminate, false if normal. This returns a
1108/// preprocessing token, not a normal token, as such, it is an internal
1109/// interface. It assumes that the Flags of result have been cleared before
1110/// calling this.
Chris Lattner146762e2007-07-20 16:59:19 +00001111void Lexer::LexTokenInternal(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001112LexNextToken:
1113 // New token, can't need cleaning yet.
Chris Lattner146762e2007-07-20 16:59:19 +00001114 Result.clearFlag(Token::NeedsCleaning);
Chris Lattner8c204872006-10-14 05:19:21 +00001115 Result.setIdentifierInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001116
1117 // CurPtr - Cache BufferPtr in an automatic variable.
1118 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001119
Chris Lattnereb54b592006-07-10 06:34:27 +00001120 // Small amounts of horizontal whitespace is very common between tokens.
1121 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1122 ++CurPtr;
1123 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1124 ++CurPtr;
1125 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001126 Result.setFlag(Token::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001127 }
1128
Chris Lattner22eb9722006-06-18 05:43:12 +00001129 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1130
1131 // Read a character, advancing over it.
1132 char Char = getAndAdvanceChar(CurPtr, Result);
1133 switch (Char) {
1134 case 0: // Null.
1135 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001136 if (CurPtr-1 == BufferEnd) {
1137 // Read the PP instance variable into an automatic variable, because
1138 // LexEndOfFile will often delete 'this'.
1139 Preprocessor &PPCache = PP;
1140 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1141 return; // Got a token to return.
1142 return PPCache.Lex(Result);
1143 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001144
Chris Lattnercb283342006-06-18 06:48:37 +00001145 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner146762e2007-07-20 16:59:19 +00001146 Result.setFlag(Token::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001147 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001148 goto LexNextToken; // GCC isn't tail call eliminating.
1149 case '\n':
1150 case '\r':
1151 // If we are inside a preprocessor directive and we see the end of line,
1152 // we know we are done with the directive, so return an EOM token.
1153 if (ParsingPreprocessorDirective) {
1154 // Done parsing the "line".
1155 ParsingPreprocessorDirective = false;
1156
Chris Lattner457fc152006-07-29 06:30:25 +00001157 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerb352e3e2006-11-21 06:17:10 +00001158 KeepCommentMode = PP.getCommentRetentionState();
Chris Lattner457fc152006-07-29 06:30:25 +00001159
Chris Lattner22eb9722006-06-18 05:43:12 +00001160 // Since we consumed a newline, we are back at the start of a line.
1161 IsAtStartOfLine = true;
1162
Chris Lattner8c204872006-10-14 05:19:21 +00001163 Result.setKind(tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001164 break;
1165 }
1166 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001167 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001168 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001169 Result.clearFlag(Token::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001170 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001171 goto LexNextToken; // GCC isn't tail call eliminating.
1172 case ' ':
1173 case '\t':
1174 case '\f':
1175 case '\v':
Chris Lattnerb9b85972007-07-22 06:29:05 +00001176 SkipHorizontalWhitespace:
Chris Lattner146762e2007-07-20 16:59:19 +00001177 Result.setFlag(Token::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001178 SkipWhitespace(Result, CurPtr);
Chris Lattnerb9b85972007-07-22 06:29:05 +00001179
1180 SkipIgnoredUnits:
1181 CurPtr = BufferPtr;
1182
1183 // If the next token is obviously a // or /* */ comment, skip it efficiently
1184 // too (without going through the big switch stmt).
1185 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !KeepCommentMode) {
1186 SkipBCPLComment(Result, CurPtr+2);
1187 goto SkipIgnoredUnits;
1188 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !KeepCommentMode) {
1189 SkipBlockComment(Result, CurPtr+2);
1190 goto SkipIgnoredUnits;
1191 } else if (isHorizontalWhitespace(*CurPtr)) {
1192 goto SkipHorizontalWhitespace;
1193 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001194 goto LexNextToken; // GCC isn't tail call eliminating.
1195
1196 case 'L':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001197 // Notify MIOpt that we read a non-whitespace/non-comment token.
1198 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001199 Char = getCharAndSize(CurPtr, SizeTmp);
1200
1201 // Wide string literal.
1202 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001203 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1204 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001205
1206 // Wide character constant.
1207 if (Char == '\'')
1208 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1209 // FALL THROUGH, treating L like the start of an identifier.
1210
1211 // C99 6.4.2: Identifiers.
1212 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1213 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1214 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1215 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1216 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1217 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1218 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1219 case 'v': case 'w': case 'x': case 'y': case 'z':
1220 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001221 // Notify MIOpt that we read a non-whitespace/non-comment token.
1222 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001223 return LexIdentifier(Result, CurPtr);
1224
1225 // C99 6.4.4.1: Integer Constants.
1226 // C99 6.4.4.2: Floating Constants.
1227 case '0': case '1': case '2': case '3': case '4':
1228 case '5': case '6': case '7': case '8': case '9':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001229 // Notify MIOpt that we read a non-whitespace/non-comment token.
1230 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001231 return LexNumericConstant(Result, CurPtr);
1232
1233 // C99 6.4.4: Character Constants.
1234 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001235 // Notify MIOpt that we read a non-whitespace/non-comment token.
1236 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001237 return LexCharConstant(Result, CurPtr);
1238
1239 // C99 6.4.5: String Literals.
1240 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001241 // Notify MIOpt that we read a non-whitespace/non-comment token.
1242 MIOpt.ReadToken();
Chris Lattnerd3e98952006-10-06 05:22:26 +00001243 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001244
1245 // C99 6.4.6: Punctuators.
1246 case '?':
Chris Lattner8c204872006-10-14 05:19:21 +00001247 Result.setKind(tok::question);
Chris Lattner22eb9722006-06-18 05:43:12 +00001248 break;
1249 case '[':
Chris Lattner8c204872006-10-14 05:19:21 +00001250 Result.setKind(tok::l_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001251 break;
1252 case ']':
Chris Lattner8c204872006-10-14 05:19:21 +00001253 Result.setKind(tok::r_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001254 break;
1255 case '(':
Chris Lattner8c204872006-10-14 05:19:21 +00001256 Result.setKind(tok::l_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001257 break;
1258 case ')':
Chris Lattner8c204872006-10-14 05:19:21 +00001259 Result.setKind(tok::r_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001260 break;
1261 case '{':
Chris Lattner8c204872006-10-14 05:19:21 +00001262 Result.setKind(tok::l_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001263 break;
1264 case '}':
Chris Lattner8c204872006-10-14 05:19:21 +00001265 Result.setKind(tok::r_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001266 break;
1267 case '.':
1268 Char = getCharAndSize(CurPtr, SizeTmp);
1269 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001270 // Notify MIOpt that we read a non-whitespace/non-comment token.
1271 MIOpt.ReadToken();
1272
Chris Lattner22eb9722006-06-18 05:43:12 +00001273 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1274 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001275 Result.setKind(tok::periodstar);
Chris Lattner22eb9722006-06-18 05:43:12 +00001276 CurPtr += SizeTmp;
1277 } else if (Char == '.' &&
1278 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner8c204872006-10-14 05:19:21 +00001279 Result.setKind(tok::ellipsis);
Chris Lattner22eb9722006-06-18 05:43:12 +00001280 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1281 SizeTmp2, Result);
1282 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001283 Result.setKind(tok::period);
Chris Lattner22eb9722006-06-18 05:43:12 +00001284 }
1285 break;
1286 case '&':
1287 Char = getCharAndSize(CurPtr, SizeTmp);
1288 if (Char == '&') {
Chris Lattner8c204872006-10-14 05:19:21 +00001289 Result.setKind(tok::ampamp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001290 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1291 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001292 Result.setKind(tok::ampequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001293 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1294 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001295 Result.setKind(tok::amp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001296 }
1297 break;
1298 case '*':
1299 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001300 Result.setKind(tok::starequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001301 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1302 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001303 Result.setKind(tok::star);
Chris Lattner22eb9722006-06-18 05:43:12 +00001304 }
1305 break;
1306 case '+':
1307 Char = getCharAndSize(CurPtr, SizeTmp);
1308 if (Char == '+') {
Chris Lattner8c204872006-10-14 05:19:21 +00001309 Result.setKind(tok::plusplus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001310 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1311 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001312 Result.setKind(tok::plusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001313 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1314 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001315 Result.setKind(tok::plus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001316 }
1317 break;
1318 case '-':
1319 Char = getCharAndSize(CurPtr, SizeTmp);
1320 if (Char == '-') {
Chris Lattner8c204872006-10-14 05:19:21 +00001321 Result.setKind(tok::minusminus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001322 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1323 } else if (Char == '>' && Features.CPlusPlus &&
1324 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001325 Result.setKind(tok::arrowstar); // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00001326 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1327 SizeTmp2, Result);
1328 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001329 Result.setKind(tok::arrow);
Chris Lattner22eb9722006-06-18 05:43:12 +00001330 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1331 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001332 Result.setKind(tok::minusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001333 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1334 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001335 Result.setKind(tok::minus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001336 }
1337 break;
1338 case '~':
Chris Lattner8c204872006-10-14 05:19:21 +00001339 Result.setKind(tok::tilde);
Chris Lattner22eb9722006-06-18 05:43:12 +00001340 break;
1341 case '!':
1342 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001343 Result.setKind(tok::exclaimequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001344 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1345 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001346 Result.setKind(tok::exclaim);
Chris Lattner22eb9722006-06-18 05:43:12 +00001347 }
1348 break;
1349 case '/':
1350 // 6.4.9: Comments
1351 Char = getCharAndSize(CurPtr, SizeTmp);
1352 if (Char == '/') { // BCPL comment.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001353 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) {
1354 // It is common for the tokens immediately after a // comment to be
Chris Lattner619c1742007-07-22 18:38:25 +00001355 // whitespace (indentation for the next line). Instead of going through
1356 // the big switch, handle it efficiently now.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001357 goto SkipIgnoredUnits;
1358 }
Chris Lattner457fc152006-07-29 06:30:25 +00001359 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001360 } else if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001361 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1362 goto LexNextToken; // GCC isn't tail call eliminating.
1363 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001364 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001365 Result.setKind(tok::slashequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001366 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1367 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001368 Result.setKind(tok::slash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001369 }
1370 break;
1371 case '%':
1372 Char = getCharAndSize(CurPtr, SizeTmp);
1373 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001374 Result.setKind(tok::percentequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001375 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1376 } else if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001377 Result.setKind(tok::r_brace); // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00001378 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1379 } else if (Features.Digraphs && Char == ':') {
1380 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001381 Char = getCharAndSize(CurPtr, SizeTmp);
1382 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001383 Result.setKind(tok::hashhash); // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00001384 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1385 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001386 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001387 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001388 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1389 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner22eb9722006-06-18 05:43:12 +00001390 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001391 Result.setKind(tok::hash); // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00001392
1393 // We parsed a # character. If this occurs at the start of the line,
1394 // it's actually the start of a preprocessing directive. Callback to
1395 // the preprocessor to handle it.
1396 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001397 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001398 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001399 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001400
1401 // As an optimization, if the preprocessor didn't switch lexers, tail
1402 // recurse.
1403 if (PP.isCurrentLexer(this)) {
1404 // Start a new token. If this is a #include or something, the PP may
1405 // want us starting at the beginning of the line again. If so, set
1406 // the StartOfLine flag.
1407 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00001408 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001409 IsAtStartOfLine = false;
1410 }
1411 goto LexNextToken; // GCC isn't tail call eliminating.
1412 }
1413
1414 return PP.Lex(Result);
1415 }
1416 }
1417 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001418 Result.setKind(tok::percent);
Chris Lattner22eb9722006-06-18 05:43:12 +00001419 }
1420 break;
1421 case '<':
1422 Char = getCharAndSize(CurPtr, SizeTmp);
1423 if (ParsingFilename) {
1424 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1425 } else if (Char == '<' &&
1426 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001427 Result.setKind(tok::lesslessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001428 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1429 SizeTmp2, Result);
1430 } else if (Char == '<') {
Chris Lattner8c204872006-10-14 05:19:21 +00001431 Result.setKind(tok::lessless);
Chris Lattner22eb9722006-06-18 05:43:12 +00001432 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1433 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001434 Result.setKind(tok::lessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001435 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1436 } else if (Features.Digraphs && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001437 Result.setKind(tok::l_square); // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00001438 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1439 } else if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001440 Result.setKind(tok::l_brace); // '<%' -> '{'
Chris Lattner22eb9722006-06-18 05:43:12 +00001441 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001442 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001443 Result.setKind(tok::less);
Chris Lattner22eb9722006-06-18 05:43:12 +00001444 }
1445 break;
1446 case '>':
1447 Char = getCharAndSize(CurPtr, SizeTmp);
1448 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001449 Result.setKind(tok::greaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001450 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1451 } else if (Char == '>' &&
1452 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001453 Result.setKind(tok::greatergreaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001454 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1455 SizeTmp2, Result);
1456 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001457 Result.setKind(tok::greatergreater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001458 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001459 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001460 Result.setKind(tok::greater);
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::caretequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001467 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1468 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001469 Result.setKind(tok::caret);
Chris Lattner22eb9722006-06-18 05:43:12 +00001470 }
1471 break;
1472 case '|':
1473 Char = getCharAndSize(CurPtr, SizeTmp);
1474 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001475 Result.setKind(tok::pipeequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001476 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1477 } else if (Char == '|') {
Chris Lattner8c204872006-10-14 05:19:21 +00001478 Result.setKind(tok::pipepipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001479 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1480 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001481 Result.setKind(tok::pipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001482 }
1483 break;
1484 case ':':
1485 Char = getCharAndSize(CurPtr, SizeTmp);
1486 if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001487 Result.setKind(tok::r_square); // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00001488 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1489 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001490 Result.setKind(tok::coloncolon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001491 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1492 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001493 Result.setKind(tok::colon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001494 }
1495 break;
1496 case ';':
Chris Lattner8c204872006-10-14 05:19:21 +00001497 Result.setKind(tok::semi);
Chris Lattner22eb9722006-06-18 05:43:12 +00001498 break;
1499 case '=':
1500 Char = getCharAndSize(CurPtr, SizeTmp);
1501 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001502 Result.setKind(tok::equalequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001503 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1504 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001505 Result.setKind(tok::equal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001506 }
1507 break;
1508 case ',':
Chris Lattner8c204872006-10-14 05:19:21 +00001509 Result.setKind(tok::comma);
Chris Lattner22eb9722006-06-18 05:43:12 +00001510 break;
1511 case '#':
1512 Char = getCharAndSize(CurPtr, SizeTmp);
1513 if (Char == '#') {
Chris Lattner8c204872006-10-14 05:19:21 +00001514 Result.setKind(tok::hashhash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001515 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001516 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001517 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001518 Diag(BufferPtr, diag::charize_microsoft_ext);
1519 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001520 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001521 Result.setKind(tok::hash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001522 // We parsed a # character. If this occurs at the start of the line,
1523 // it's actually the start of a preprocessing directive. Callback to
1524 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00001525 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001526 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001527 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001528 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001529
1530 // As an optimization, if the preprocessor didn't switch lexers, tail
1531 // recurse.
1532 if (PP.isCurrentLexer(this)) {
1533 // Start a new token. If this is a #include or something, the PP may
1534 // want us starting at the beginning of the line again. If so, set
1535 // the StartOfLine flag.
1536 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00001537 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001538 IsAtStartOfLine = false;
1539 }
1540 goto LexNextToken; // GCC isn't tail call eliminating.
1541 }
1542 return PP.Lex(Result);
1543 }
1544 }
1545 break;
1546
1547 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00001548 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00001549 // FALL THROUGH.
1550 default:
1551 // Objective C support.
1552 if (CurPtr[-1] == '@' && Features.ObjC1) {
Chris Lattner8c204872006-10-14 05:19:21 +00001553 Result.setKind(tok::at);
Chris Lattner22eb9722006-06-18 05:43:12 +00001554 break;
1555 } else if (CurPtr[-1] == '$' && Features.DollarIdents) {// $ in identifiers.
Chris Lattnercb283342006-06-18 06:48:37 +00001556 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001557 // Notify MIOpt that we read a non-whitespace/non-comment token.
1558 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001559 return LexIdentifier(Result, CurPtr);
1560 }
1561
Chris Lattner8c204872006-10-14 05:19:21 +00001562 Result.setKind(tok::unknown);
Chris Lattner041bef82006-07-11 05:52:53 +00001563 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001564 }
1565
Chris Lattner371ac8a2006-07-04 07:11:10 +00001566 // Notify MIOpt that we read a non-whitespace/non-comment token.
1567 MIOpt.ReadToken();
1568
Chris Lattnerd01e2912006-06-18 16:22:51 +00001569 // Update the location of token as well as BufferPtr.
1570 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001571}