blob: eb2e1e3b24c1bf2efe80d6792373af28df264b53 [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//
10// This file implements the Lexer and LexerToken interfaces.
11//
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"
30#include "clang/Basic/SourceBuffer.h"
31#include "clang/Basic/SourceLocation.h"
32#include "llvm/Config/alloca.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000033#include <cctype>
34#include <iostream>
35using namespace llvm;
36using namespace clang;
37
38static void InitCharacterInfo();
39
Chris Lattner4cca5ba2006-07-02 20:05:54 +000040Lexer::Lexer(const SourceBuffer *File, unsigned fileid, Preprocessor &pp,
41 const char *BufStart, const char *BufEnd)
Chris Lattner678c8802006-07-11 05:46:12 +000042 : BufferEnd(BufEnd ? BufEnd : File->getBufferEnd()),
Chris Lattner4cca5ba2006-07-02 20:05:54 +000043 InputFile(File), CurFileID(fileid), PP(pp), Features(PP.getLangOptions()) {
Chris Lattnerecfeafe2006-07-02 21:26:45 +000044 Is_PragmaLexer = false;
Chris Lattner4ec473f2006-07-03 05:16:05 +000045 IsMainFile = false;
Chris Lattner22eb9722006-06-18 05:43:12 +000046 InitCharacterInfo();
47
48 assert(BufferEnd[0] == 0 &&
49 "We assume that the input buffer has a null character at the end"
50 " to simplify lexing!");
Chris Lattner678c8802006-07-11 05:46:12 +000051
52 BufferPtr = BufStart ? BufStart : File->getBufferStart();
53
Chris Lattner22eb9722006-06-18 05:43:12 +000054 // Start of the file is a start of line.
55 IsAtStartOfLine = true;
56
57 // We are not after parsing a #.
58 ParsingPreprocessorDirective = false;
59
60 // We are not after parsing #include.
61 ParsingFilename = false;
Chris Lattner3ebcf4e2006-07-11 05:39:23 +000062
63 // We are not in raw mode. Raw mode disables diagnostics and interpretation
64 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
65 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
66 // or otherwise skipping over tokens.
67 LexingRawMode = false;
Chris Lattner457fc152006-07-29 06:30:25 +000068
69 // Default to keeping comments if requested.
70 KeepCommentMode = Features.KeepComments;
Chris Lattner22eb9722006-06-18 05:43:12 +000071}
72
Chris Lattnere3e81ea2006-07-03 01:13:26 +000073/// Stringify - Convert the specified string into a C string, with surrounding
74/// ""'s, and with escaped \ and " characters.
Chris Lattnerecc39e92006-07-15 05:23:31 +000075std::string Lexer::Stringify(const std::string &Str, bool Charify) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +000076 std::string Result = Str;
Chris Lattnerecc39e92006-07-15 05:23:31 +000077 char Quote = Charify ? '\'' : '"';
Chris Lattnere3e81ea2006-07-03 01:13:26 +000078 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
Chris Lattnerecc39e92006-07-15 05:23:31 +000079 if (Result[i] == '\\' || Result[i] == Quote) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +000080 Result.insert(Result.begin()+i, '\\');
81 ++i; ++e;
82 }
83 }
Chris Lattnerecc39e92006-07-15 05:23:31 +000084 return Result;
Chris Lattnere3e81ea2006-07-03 01:13:26 +000085}
86
Chris Lattner22eb9722006-06-18 05:43:12 +000087
Chris Lattner22eb9722006-06-18 05:43:12 +000088//===----------------------------------------------------------------------===//
89// Character information.
90//===----------------------------------------------------------------------===//
91
92static unsigned char CharInfo[256];
93
94enum {
95 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
96 CHAR_VERT_WS = 0x02, // '\r', '\n'
97 CHAR_LETTER = 0x04, // a-z,A-Z
98 CHAR_NUMBER = 0x08, // 0-9
99 CHAR_UNDER = 0x10, // _
100 CHAR_PERIOD = 0x20 // .
101};
102
103static void InitCharacterInfo() {
104 static bool isInited = false;
105 if (isInited) return;
106 isInited = true;
107
108 // Intiialize the CharInfo table.
109 // TODO: statically initialize this.
110 CharInfo[(int)' '] = CharInfo[(int)'\t'] =
111 CharInfo[(int)'\f'] = CharInfo[(int)'\v'] = CHAR_HORZ_WS;
112 CharInfo[(int)'\n'] = CharInfo[(int)'\r'] = CHAR_VERT_WS;
113
114 CharInfo[(int)'_'] = CHAR_UNDER;
Chris Lattnerdd0b7cb2006-10-17 02:53:51 +0000115 CharInfo[(int)'.'] = CHAR_PERIOD;
Chris Lattner22eb9722006-06-18 05:43:12 +0000116 for (unsigned i = 'a'; i <= 'z'; ++i)
117 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
118 for (unsigned i = '0'; i <= '9'; ++i)
119 CharInfo[i] = CHAR_NUMBER;
120}
121
122/// isIdentifierBody - Return true if this is the body character of an
123/// identifier, which is [a-zA-Z0-9_].
124static inline bool isIdentifierBody(unsigned char c) {
125 return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER);
126}
127
128/// isHorizontalWhitespace - Return true if this character is horizontal
129/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
130static inline bool isHorizontalWhitespace(unsigned char c) {
131 return CharInfo[c] & CHAR_HORZ_WS;
132}
133
134/// isWhitespace - Return true if this character is horizontal or vertical
135/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
136/// for '\0'.
137static inline bool isWhitespace(unsigned char c) {
138 return CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS);
139}
140
141/// isNumberBody - Return true if this is the body character of an
142/// preprocessing number, which is [a-zA-Z0-9_.].
143static inline bool isNumberBody(unsigned char c) {
144 return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD);
145}
146
Chris Lattnerd01e2912006-06-18 16:22:51 +0000147
Chris Lattner22eb9722006-06-18 05:43:12 +0000148//===----------------------------------------------------------------------===//
149// Diagnostics forwarding code.
150//===----------------------------------------------------------------------===//
151
152/// getSourceLocation - Return a source location identifier for the specified
153/// offset in the current file.
154SourceLocation Lexer::getSourceLocation(const char *Loc) const {
Chris Lattner8bbfe462006-07-02 22:27:49 +0000155 assert(Loc >= InputFile->getBufferStart() && Loc <= BufferEnd &&
Chris Lattner4cca5ba2006-07-02 20:05:54 +0000156 "Location out of range for this buffer!");
Chris Lattner8bbfe462006-07-02 22:27:49 +0000157 return SourceLocation(CurFileID, Loc-InputFile->getBufferStart());
Chris Lattner22eb9722006-06-18 05:43:12 +0000158}
159
160
161/// Diag - Forwarding function for diagnostics. This translate a source
162/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000163void Lexer::Diag(const char *Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000164 const std::string &Msg) const {
Chris Lattner538d7f32006-07-20 04:31:52 +0000165 if (LexingRawMode && Diagnostic::isNoteWarningOrExtension(DiagID))
166 return;
Chris Lattnercb283342006-06-18 06:48:37 +0000167 PP.Diag(getSourceLocation(Loc), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000168}
Chris Lattner538d7f32006-07-20 04:31:52 +0000169void Lexer::Diag(SourceLocation Loc, unsigned DiagID,
170 const std::string &Msg) const {
171 if (LexingRawMode && Diagnostic::isNoteWarningOrExtension(DiagID))
172 return;
173 PP.Diag(Loc, DiagID, Msg);
174}
175
Chris Lattner22eb9722006-06-18 05:43:12 +0000176
177//===----------------------------------------------------------------------===//
178// Trigraph and Escaped Newline Handling Code.
179//===----------------------------------------------------------------------===//
180
181/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
182/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
183static char GetTrigraphCharForLetter(char Letter) {
184 switch (Letter) {
185 default: return 0;
186 case '=': return '#';
187 case ')': return ']';
188 case '(': return '[';
189 case '!': return '|';
190 case '\'': return '^';
191 case '>': return '}';
192 case '/': return '\\';
193 case '<': return '{';
194 case '-': return '~';
195 }
196}
197
198/// DecodeTrigraphChar - If the specified character is a legal trigraph when
199/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
200/// return the result character. Finally, emit a warning about trigraph use
201/// whether trigraphs are enabled or not.
202static char DecodeTrigraphChar(const char *CP, Lexer *L) {
203 char Res = GetTrigraphCharForLetter(*CP);
204 if (Res && L) {
205 if (!L->getFeatures().Trigraphs) {
206 L->Diag(CP-2, diag::trigraph_ignored);
207 return 0;
208 } else {
209 L->Diag(CP-2, diag::trigraph_converted, std::string()+Res);
210 }
211 }
212 return Res;
213}
214
215/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
216/// get its size, and return it. This is tricky in several cases:
217/// 1. If currently at the start of a trigraph, we warn about the trigraph,
218/// then either return the trigraph (skipping 3 chars) or the '?',
219/// depending on whether trigraphs are enabled or not.
220/// 2. If this is an escaped newline (potentially with whitespace between
221/// the backslash and newline), implicitly skip the newline and return
222/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000223/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000224///
225/// This handles the slow/uncommon case of the getCharAndSize method. Here we
226/// know that we can accumulate into Size, and that we have already incremented
227/// Ptr by Size bytes.
228///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000229/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
230/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000231///
232char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
233 LexerToken *Tok) {
234 // If we have a slash, look for an escaped newline.
235 if (Ptr[0] == '\\') {
236 ++Size;
237 ++Ptr;
238Slash:
239 // Common case, backslash-char where the char is not whitespace.
240 if (!isWhitespace(Ptr[0])) return '\\';
241
242 // See if we have optional whitespace characters followed by a newline.
243 {
244 unsigned SizeTmp = 0;
245 do {
246 ++SizeTmp;
247 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
248 // Remember that this token needs to be cleaned.
Chris Lattner8c204872006-10-14 05:19:21 +0000249 if (Tok) Tok->setFlag(LexerToken::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000250
251 // Warn if there was whitespace between the backslash and newline.
252 if (SizeTmp != 1 && Tok)
253 Diag(Ptr, diag::backslash_newline_space);
254
255 // If this is a \r\n or \n\r, skip the newlines.
256 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
257 Ptr[SizeTmp-1] != Ptr[SizeTmp])
258 ++SizeTmp;
259
260 // Found backslash<whitespace><newline>. Parse the char after it.
261 Size += SizeTmp;
262 Ptr += SizeTmp;
263 // Use slow version to accumulate a correct size field.
264 return getCharAndSizeSlow(Ptr, Size, Tok);
265 }
266 } while (isWhitespace(Ptr[SizeTmp]));
267 }
268
269 // Otherwise, this is not an escaped newline, just return the slash.
270 return '\\';
271 }
272
273 // If this is a trigraph, process it.
274 if (Ptr[0] == '?' && Ptr[1] == '?') {
275 // If this is actually a legal trigraph (not something like "??x"), emit
276 // a trigraph warning. If so, and if trigraphs are enabled, return it.
277 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
278 // Remember that this token needs to be cleaned.
Chris Lattner8c204872006-10-14 05:19:21 +0000279 if (Tok) Tok->setFlag(LexerToken::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000280
281 Ptr += 3;
282 Size += 3;
283 if (C == '\\') goto Slash;
284 return C;
285 }
286 }
287
288 // If this is neither, return a single character.
289 ++Size;
290 return *Ptr;
291}
292
Chris Lattnerd01e2912006-06-18 16:22:51 +0000293
Chris Lattner22eb9722006-06-18 05:43:12 +0000294/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
295/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
296/// and that we have already incremented Ptr by Size bytes.
297///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000298/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
299/// be updated to match.
300char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000301 const LangOptions &Features) {
302 // 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
317 // If this is a \r\n or \n\r, skip the newlines.
318 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
319 Ptr[SizeTmp-1] != Ptr[SizeTmp])
320 ++SizeTmp;
321
322 // Found backslash<whitespace><newline>. Parse the char after it.
323 Size += SizeTmp;
324 Ptr += SizeTmp;
325
326 // Use slow version to accumulate a correct size field.
327 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
328 }
329 } while (isWhitespace(Ptr[SizeTmp]));
330 }
331
332 // Otherwise, this is not an escaped newline, just return the slash.
333 return '\\';
334 }
335
336 // If this is a trigraph, process it.
337 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
338 // If this is actually a legal trigraph (not something like "??x"), return
339 // it.
340 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
341 Ptr += 3;
342 Size += 3;
343 if (C == '\\') goto Slash;
344 return C;
345 }
346 }
347
348 // If this is neither, return a single character.
349 ++Size;
350 return *Ptr;
351}
352
Chris Lattner22eb9722006-06-18 05:43:12 +0000353//===----------------------------------------------------------------------===//
354// Helper methods for lexing.
355//===----------------------------------------------------------------------===//
356
Chris Lattnercb283342006-06-18 06:48:37 +0000357void Lexer::LexIdentifier(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000358 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
359 unsigned Size;
360 unsigned char C = *CurPtr++;
361 while (isIdentifierBody(C)) {
362 C = *CurPtr++;
363 }
364 --CurPtr; // Back up over the skipped character.
365
366 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
367 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000368 // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000369 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
370FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000371 const char *IdStart = BufferPtr;
Chris Lattnerd01e2912006-06-18 16:22:51 +0000372 FormTokenWithChars(Result, CurPtr);
Chris Lattner8c204872006-10-14 05:19:21 +0000373 Result.setKind(tok::identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000374
Chris Lattner0f1f5052006-07-20 04:16:23 +0000375 // If we are in raw mode, return this identifier raw. There is no need to
376 // look up identifier information or attempt to macro expand it.
377 if (LexingRawMode) return;
378
Chris Lattnercefc7682006-07-08 08:28:12 +0000379 // Fill in Result.IdentifierInfo, looking up the identifier in the
380 // identifier table.
381 PP.LookUpIdentifierInfo(Result, IdStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000382
Chris Lattnerc5a00062006-06-18 16:41:01 +0000383 // Finally, now that we know we have an identifier, pass this off to the
384 // preprocessor, which may macro expand it or something.
Chris Lattner22eb9722006-06-18 05:43:12 +0000385 return PP.HandleIdentifier(Result);
386 }
387
388 // Otherwise, $,\,? in identifier found. Enter slower path.
389
390 C = getCharAndSize(CurPtr, Size);
391 while (1) {
392 if (C == '$') {
393 // If we hit a $ and they are not supported in identifiers, we are done.
394 if (!Features.DollarIdents) goto FinishIdentifier;
395
396 // Otherwise, emit a diagnostic and continue.
Chris Lattnercb283342006-06-18 06:48:37 +0000397 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000398 CurPtr = ConsumeChar(CurPtr, Size, Result);
399 C = getCharAndSize(CurPtr, Size);
400 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000401 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000402 // Found end of identifier.
403 goto FinishIdentifier;
404 }
405
406 // Otherwise, this character is good, consume it.
407 CurPtr = ConsumeChar(CurPtr, Size, Result);
408
409 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000410 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000411 CurPtr = ConsumeChar(CurPtr, Size, Result);
412 C = getCharAndSize(CurPtr, Size);
413 }
414 }
415}
416
417
418/// LexNumericConstant - Lex the remainer of a integer or floating point
419/// constant. From[-1] is the first character lexed. Return the end of the
420/// constant.
Chris Lattnercb283342006-06-18 06:48:37 +0000421void Lexer::LexNumericConstant(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000422 unsigned Size;
423 char C = getCharAndSize(CurPtr, Size);
424 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000425 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000426 CurPtr = ConsumeChar(CurPtr, Size, Result);
427 PrevCh = C;
428 C = getCharAndSize(CurPtr, Size);
429 }
430
431 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
432 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
433 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
434
435 // If we have a hex FP constant, continue.
436 if (Features.HexFloats &&
437 (C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p'))
438 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
439
Chris Lattner8c204872006-10-14 05:19:21 +0000440 Result.setKind(tok::numeric_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000441
Chris Lattnerd01e2912006-06-18 16:22:51 +0000442 // Update the location of token as well as BufferPtr.
443 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000444}
445
446/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
447/// either " or L".
Chris Lattnerd3e98952006-10-06 05:22:26 +0000448void Lexer::LexStringLiteral(LexerToken &Result, const char *CurPtr, bool Wide){
Chris Lattner22eb9722006-06-18 05:43:12 +0000449 const char *NulCharacter = 0; // Does this string contain the \0 character?
450
451 char C = getAndAdvanceChar(CurPtr, Result);
452 while (C != '"') {
453 // Skip escaped characters.
454 if (C == '\\') {
455 // Skip the escaped character.
456 C = getAndAdvanceChar(CurPtr, Result);
457 } else if (C == '\n' || C == '\r' || // Newline.
458 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000459 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000460 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000461 FormTokenWithChars(Result, CurPtr-1);
462 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000463 } else if (C == 0) {
464 NulCharacter = CurPtr-1;
465 }
466 C = getAndAdvanceChar(CurPtr, Result);
467 }
468
Chris Lattner5a78a022006-07-20 06:02:19 +0000469 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000470 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000471
Chris Lattner8c204872006-10-14 05:19:21 +0000472 Result.setKind(Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000473
Chris Lattnerd01e2912006-06-18 16:22:51 +0000474 // Update the location of the token as well as the BufferPtr instance var.
475 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000476}
477
478/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
479/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattnercb283342006-06-18 06:48:37 +0000480void Lexer::LexAngledStringLiteral(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000481 const char *NulCharacter = 0; // Does this string contain the \0 character?
482
483 char C = getAndAdvanceChar(CurPtr, Result);
484 while (C != '>') {
485 // Skip escaped characters.
486 if (C == '\\') {
487 // Skip the escaped character.
488 C = getAndAdvanceChar(CurPtr, Result);
489 } else if (C == '\n' || C == '\r' || // Newline.
490 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000491 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000492 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000493 FormTokenWithChars(Result, CurPtr-1);
494 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000495 } else if (C == 0) {
496 NulCharacter = CurPtr-1;
497 }
498 C = getAndAdvanceChar(CurPtr, Result);
499 }
500
Chris Lattner5a78a022006-07-20 06:02:19 +0000501 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000502 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000503
Chris Lattner8c204872006-10-14 05:19:21 +0000504 Result.setKind(tok::angle_string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000505
Chris Lattnerd01e2912006-06-18 16:22:51 +0000506 // Update the location of token as well as BufferPtr.
507 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000508}
509
510
511/// LexCharConstant - Lex the remainder of a character constant, after having
512/// lexed either ' or L'.
Chris Lattnercb283342006-06-18 06:48:37 +0000513void Lexer::LexCharConstant(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000514 const char *NulCharacter = 0; // Does this character contain the \0 character?
515
516 // Handle the common case of 'x' and '\y' efficiently.
517 char C = getAndAdvanceChar(CurPtr, Result);
518 if (C == '\'') {
Chris Lattnera5f4c882006-07-20 06:08:47 +0000519 if (!LexingRawMode) Diag(BufferPtr, diag::err_empty_character);
Chris Lattner8c204872006-10-14 05:19:21 +0000520 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000521 FormTokenWithChars(Result, CurPtr);
522 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000523 } else if (C == '\\') {
524 // Skip the escaped character.
525 // FIXME: UCN's.
526 C = getAndAdvanceChar(CurPtr, Result);
527 }
528
529 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
530 ++CurPtr;
531 } else {
532 // Fall back on generic code for embedded nulls, newlines, wide chars.
533 do {
534 // Skip escaped characters.
535 if (C == '\\') {
536 // Skip the escaped character.
537 C = getAndAdvanceChar(CurPtr, Result);
538 } else if (C == '\n' || C == '\r' || // Newline.
539 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000540 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner8c204872006-10-14 05:19:21 +0000541 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000542 FormTokenWithChars(Result, CurPtr-1);
543 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000544 } else if (C == 0) {
545 NulCharacter = CurPtr-1;
546 }
547 C = getAndAdvanceChar(CurPtr, Result);
548 } while (C != '\'');
549 }
550
Chris Lattnercb283342006-06-18 06:48:37 +0000551 if (NulCharacter) Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000552
Chris Lattner8c204872006-10-14 05:19:21 +0000553 Result.setKind(tok::char_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000554
Chris Lattnerd01e2912006-06-18 16:22:51 +0000555 // Update the location of token as well as BufferPtr.
556 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000557}
558
559/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
560/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattnercb283342006-06-18 06:48:37 +0000561void Lexer::SkipWhitespace(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000562 // Whitespace - Skip it, then return the token after the whitespace.
563 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
564 while (1) {
565 // Skip horizontal whitespace very aggressively.
566 while (isHorizontalWhitespace(Char))
567 Char = *++CurPtr;
568
569 // Otherwise if we something other than whitespace, we're done.
570 if (Char != '\n' && Char != '\r')
571 break;
572
573 if (ParsingPreprocessorDirective) {
574 // End of preprocessor directive line, let LexTokenInternal handle this.
575 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000576 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000577 }
578
579 // ok, but handle newline.
580 // The returned token is at the start of the line.
Chris Lattner8c204872006-10-14 05:19:21 +0000581 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000582 // No leading whitespace seen so far.
Chris Lattner8c204872006-10-14 05:19:21 +0000583 Result.clearFlag(LexerToken::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000584 Char = *++CurPtr;
585 }
586
587 // If this isn't immediately after a newline, there is leading space.
588 char PrevChar = CurPtr[-1];
589 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattner8c204872006-10-14 05:19:21 +0000590 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000591
592 // If the next token is obviously a // or /* */ comment, skip it efficiently
593 // too (without going through the big switch stmt).
Chris Lattner457fc152006-07-29 06:30:25 +0000594 if (Char == '/' && CurPtr[1] == '/' && !KeepCommentMode) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000595 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000596 SkipBCPLComment(Result, CurPtr+1);
597 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000598 }
Chris Lattner457fc152006-07-29 06:30:25 +0000599 if (Char == '/' && CurPtr[1] == '*' && !KeepCommentMode) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000600 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000601 SkipBlockComment(Result, CurPtr+2);
602 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000603 }
604 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000605}
606
607// SkipBCPLComment - We have just read the // characters from input. Skip until
608// we find the newline character thats terminate the comment. Then update
609/// BufferPtr and return.
Chris Lattner457fc152006-07-29 06:30:25 +0000610bool Lexer::SkipBCPLComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000611 // If BCPL comments aren't explicitly enabled for this language, emit an
612 // extension warning.
613 if (!Features.BCPLComment) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000614 Diag(BufferPtr, diag::ext_bcpl_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000615
616 // Mark them enabled so we only emit one warning for this translation
617 // unit.
618 Features.BCPLComment = true;
619 }
620
621 // Scan over the body of the comment. The common case, when scanning, is that
622 // the comment contains normal ascii characters with nothing interesting in
623 // them. As such, optimize for this case with the inner loop.
624 char C;
625 do {
626 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +0000627 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
628 // If we find a \n character, scan backwards, checking to see if it's an
629 // escaped newline, like we do for block comments.
Chris Lattner22eb9722006-06-18 05:43:12 +0000630
631 // Skip over characters in the fast loop.
632 while (C != 0 && // Potentially EOF.
633 C != '\\' && // Potentially escaped newline.
634 C != '?' && // Potentially trigraph.
635 C != '\n' && C != '\r') // Newline or DOS-style newline.
636 C = *++CurPtr;
637
638 // If this is a newline, we're done.
639 if (C == '\n' || C == '\r')
640 break; // Found the newline? Break out!
641
642 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
643 // properly decode the character.
644 const char *OldPtr = CurPtr;
645 C = getAndAdvanceChar(CurPtr, Result);
646
647 // If we read multiple characters, and one of those characters was a \r or
648 // \n, then we had an escaped newline within the comment. Emit diagnostic.
649 if (CurPtr != OldPtr+1) {
650 for (; OldPtr != CurPtr; ++OldPtr)
651 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnercb283342006-06-18 06:48:37 +0000652 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
653 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000654 }
655 }
656
Chris Lattner457fc152006-07-29 06:30:25 +0000657 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
Chris Lattner22eb9722006-06-18 05:43:12 +0000658 } while (C != '\n' && C != '\r');
659
Chris Lattner457fc152006-07-29 06:30:25 +0000660 // Found but did not consume the newline.
661
662 // If we are returning comments as tokens, return this comment as a token.
663 if (KeepCommentMode)
664 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000665
666 // If we are inside a preprocessor directive and we see the end of line,
667 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +0000668 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000669 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000670 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000671 }
672
673 // Otherwise, eat the \n character. We don't care if this is a \n\r or
674 // \r\n sequence.
675 ++CurPtr;
676
677 // The next returned token is at the start of the line.
Chris Lattner8c204872006-10-14 05:19:21 +0000678 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000679 // No leading whitespace seen so far.
Chris Lattner8c204872006-10-14 05:19:21 +0000680 Result.clearFlag(LexerToken::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000681
682 // It is common for the tokens immediately after a // comment to be
683 // whitespace (indentation for the next line). Instead of going through the
684 // big switch, handle it efficiently now.
685 if (isWhitespace(*CurPtr)) {
Chris Lattner8c204872006-10-14 05:19:21 +0000686 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000687 SkipWhitespace(Result, CurPtr+1);
688 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000689 }
690
691 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000692 return true;
693}
Chris Lattner22eb9722006-06-18 05:43:12 +0000694
Chris Lattner457fc152006-07-29 06:30:25 +0000695/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
696/// an appropriate way and return it.
697bool Lexer::SaveBCPLComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner8c204872006-10-14 05:19:21 +0000698 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +0000699 FormTokenWithChars(Result, CurPtr);
700
701 // If this BCPL-style comment is in a macro definition, transmogrify it into
702 // a C-style block comment.
703 if (ParsingPreprocessorDirective) {
704 std::string Spelling = PP.getSpelling(Result);
705 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
706 Spelling[1] = '*'; // Change prefix to "/*".
707 Spelling += "*/"; // add suffix.
708
Chris Lattner8c204872006-10-14 05:19:21 +0000709 Result.setLocation(PP.CreateString(&Spelling[0], Spelling.size(),
Chris Lattner457fc152006-07-29 06:30:25 +0000710 Result.getLocation()));
Chris Lattner8c204872006-10-14 05:19:21 +0000711 Result.setLength(Spelling.size());
Chris Lattner457fc152006-07-29 06:30:25 +0000712 }
713 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000714}
715
Chris Lattnercb283342006-06-18 06:48:37 +0000716/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
717/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner22eb9722006-06-18 05:43:12 +0000718/// diagnostic if so. We know that the is inside of a block comment.
Chris Lattner1f583052006-06-18 06:53:56 +0000719static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
720 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000721 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Chris Lattner22eb9722006-06-18 05:43:12 +0000722
723 // Back up off the newline.
724 --CurPtr;
725
726 // If this is a two-character newline sequence, skip the other character.
727 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
728 // \n\n or \r\r -> not escaped newline.
729 if (CurPtr[0] == CurPtr[1])
730 return false;
731 // \n\r or \r\n -> skip the newline.
732 --CurPtr;
733 }
734
735 // If we have horizontal whitespace, skip over it. We allow whitespace
736 // between the slash and newline.
737 bool HasSpace = false;
738 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
739 --CurPtr;
740 HasSpace = true;
741 }
742
743 // If we have a slash, we know this is an escaped newline.
744 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +0000745 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000746 } else {
747 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +0000748 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
749 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +0000750 return false;
Chris Lattnercb283342006-06-18 06:48:37 +0000751
752 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +0000753 CurPtr -= 2;
754
755 // If no trigraphs are enabled, warn that we ignored this trigraph and
756 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +0000757 if (!L->getFeatures().Trigraphs) {
758 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000759 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000760 }
Chris Lattner1f583052006-06-18 06:53:56 +0000761 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000762 }
763
764 // Warn about having an escaped newline between the */ characters.
Chris Lattner1f583052006-06-18 06:53:56 +0000765 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Chris Lattner22eb9722006-06-18 05:43:12 +0000766
767 // If there was space between the backslash and newline, warn about it.
Chris Lattner1f583052006-06-18 06:53:56 +0000768 if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space);
Chris Lattner22eb9722006-06-18 05:43:12 +0000769
Chris Lattnercb283342006-06-18 06:48:37 +0000770 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000771}
772
773/// SkipBlockComment - We have just read the /* characters from input. Read
774/// until we find the */ characters that terminate the comment. Note that we
775/// don't bother decoding trigraphs or escaped newlines in block comments,
776/// because they cannot cause the comment to end. The only thing that can
777/// happen is the comment could end with an escaped newline between the */ end
778/// of comment.
Chris Lattner457fc152006-07-29 06:30:25 +0000779bool Lexer::SkipBlockComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000780 // Scan one character past where we should, looking for a '/' character. Once
781 // we find it, check to see if it was preceeded by a *. This common
782 // optimization helps people who like to put a lot of * characters in their
783 // comments.
784 unsigned char C = *CurPtr++;
785 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000786 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000787 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +0000788 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000789 }
790
791 while (1) {
792 // Skip over all non-interesting characters.
793 // TODO: Vectorize this. Note: memchr on Darwin is slower than this loop.
794 while (C != '/' && C != '\0')
795 C = *CurPtr++;
796
797 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000798 if (CurPtr[-2] == '*') // We found the final */. We're done!
799 break;
800
801 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +0000802 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000803 // We found the final */, though it had an escaped newline between the
804 // * and /. We're done!
805 break;
806 }
807 }
808 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
809 // If this is a /* inside of the comment, emit a warning. Don't do this
810 // if this is a /*/, which will end the comment. This misses cases with
811 // embedded escaped newlines, but oh well.
Chris Lattnercb283342006-06-18 06:48:37 +0000812 Diag(CurPtr-1, diag::nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000813 }
814 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000815 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000816 // Note: the user probably forgot a */. We could continue immediately
817 // after the /*, but this would involve lexing a lot of what really is the
818 // comment, which surely would confuse the parser.
819 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +0000820 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000821 }
822 C = *CurPtr++;
823 }
Chris Lattner457fc152006-07-29 06:30:25 +0000824
825 // If we are returning comments as tokens, return this comment as a token.
826 if (KeepCommentMode) {
Chris Lattner8c204872006-10-14 05:19:21 +0000827 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +0000828 FormTokenWithChars(Result, CurPtr);
829 return false;
830 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000831
832 // It is common for the tokens immediately after a /**/ comment to be
833 // whitespace. Instead of going through the big switch, handle it
834 // efficiently now.
835 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner8c204872006-10-14 05:19:21 +0000836 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000837 SkipWhitespace(Result, CurPtr+1);
838 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000839 }
840
841 // Otherwise, just return so that the next character will be lexed as a token.
842 BufferPtr = CurPtr;
Chris Lattner8c204872006-10-14 05:19:21 +0000843 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000844 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000845}
846
847//===----------------------------------------------------------------------===//
848// Primary Lexing Entry Points
849//===----------------------------------------------------------------------===//
850
851/// LexIncludeFilename - After the preprocessor has parsed a #include, lex and
852/// (potentially) macro expand the filename.
Chris Lattner269c2322006-06-25 06:23:00 +0000853std::string Lexer::LexIncludeFilename(LexerToken &FilenameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000854 assert(ParsingPreprocessorDirective &&
855 ParsingFilename == false &&
856 "Must be in a preprocessing directive!");
857
858 // We are now parsing a filename!
859 ParsingFilename = true;
860
Chris Lattner269c2322006-06-25 06:23:00 +0000861 // Lex the filename.
862 Lex(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000863
864 // We should have gotten the filename now.
865 ParsingFilename = false;
866
867 // No filename?
Chris Lattner269c2322006-06-25 06:23:00 +0000868 if (FilenameTok.getKind() == tok::eom) {
Chris Lattner538d7f32006-07-20 04:31:52 +0000869 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner269c2322006-06-25 06:23:00 +0000870 return "";
Chris Lattnercb283342006-06-18 06:48:37 +0000871 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000872
Chris Lattner269c2322006-06-25 06:23:00 +0000873 // Get the text form of the filename.
874 std::string Filename = PP.getSpelling(FilenameTok);
875 assert(!Filename.empty() && "Can't have tokens with empty spellings!");
876
877 // Make sure the filename is <x> or "x".
878 if (Filename[0] == '<') {
879 if (Filename[Filename.size()-1] != '>') {
Chris Lattner538d7f32006-07-20 04:31:52 +0000880 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner8c204872006-10-14 05:19:21 +0000881 FilenameTok.setKind(tok::eom);
Chris Lattner269c2322006-06-25 06:23:00 +0000882 return "";
883 }
884 } else if (Filename[0] == '"') {
885 if (Filename[Filename.size()-1] != '"') {
Chris Lattner538d7f32006-07-20 04:31:52 +0000886 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner8c204872006-10-14 05:19:21 +0000887 FilenameTok.setKind(tok::eom);
Chris Lattner269c2322006-06-25 06:23:00 +0000888 return "";
889 }
890 } else {
Chris Lattner538d7f32006-07-20 04:31:52 +0000891 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner8c204872006-10-14 05:19:21 +0000892 FilenameTok.setKind(tok::eom);
Chris Lattner269c2322006-06-25 06:23:00 +0000893 return "";
Chris Lattner22eb9722006-06-18 05:43:12 +0000894 }
Chris Lattner269c2322006-06-25 06:23:00 +0000895
896 // Diagnose #include "" as invalid.
897 if (Filename.size() == 2) {
Chris Lattner538d7f32006-07-20 04:31:52 +0000898 Diag(FilenameTok.getLocation(), diag::err_pp_empty_filename);
Chris Lattner8c204872006-10-14 05:19:21 +0000899 FilenameTok.setKind(tok::eom);
Chris Lattner269c2322006-06-25 06:23:00 +0000900 return "";
901 }
902
903 return Filename;
Chris Lattner22eb9722006-06-18 05:43:12 +0000904}
905
906/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
907/// uninterpreted string. This switches the lexer out of directive mode.
908std::string Lexer::ReadToEndOfLine() {
909 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
910 "Must be in a preprocessing directive!");
911 std::string Result;
912 LexerToken Tmp;
913
914 // CurPtr - Cache BufferPtr in an automatic variable.
915 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000916 while (1) {
917 char Char = getAndAdvanceChar(CurPtr, Tmp);
918 switch (Char) {
919 default:
920 Result += Char;
921 break;
922 case 0: // Null.
923 // Found end of file?
924 if (CurPtr-1 != BufferEnd) {
925 // Nope, normal character, continue.
926 Result += Char;
927 break;
928 }
929 // FALL THROUGH.
930 case '\r':
931 case '\n':
932 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
933 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
934 BufferPtr = CurPtr-1;
935
936 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +0000937 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000938 assert(Tmp.getKind() == tok::eom && "Unexpected token!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000939
940 // Finally, we're done, return the string we found.
941 return Result;
942 }
943 }
944}
945
946/// LexEndOfFile - CurPtr points to the end of this file. Handle this
947/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +0000948/// This returns true if Result contains a token, false if PP.Lex should be
949/// called again.
950bool Lexer::LexEndOfFile(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000951 // If we hit the end of the file while parsing a preprocessor directive,
952 // end the preprocessor directive first. The next token returned will
953 // then be the end of file.
954 if (ParsingPreprocessorDirective) {
955 // Done parsing the "line".
956 ParsingPreprocessorDirective = false;
Chris Lattner8c204872006-10-14 05:19:21 +0000957 Result.setKind(tok::eom);
Chris Lattnerd01e2912006-06-18 16:22:51 +0000958 // Update the location of token as well as BufferPtr.
959 FormTokenWithChars(Result, CurPtr);
Chris Lattner457fc152006-07-29 06:30:25 +0000960
961 // Restore comment saving mode, in case it was disabled for directive.
962 KeepCommentMode = Features.KeepComments;
Chris Lattner2183a6e2006-07-18 06:36:12 +0000963 return true; // Have a token.
Chris Lattner22eb9722006-06-18 05:43:12 +0000964 }
965
Chris Lattner30a2fa12006-07-19 06:31:49 +0000966 // If we are in raw mode, return this event as an EOF token. Let the caller
967 // that put us in raw mode handle the event.
968 if (LexingRawMode) {
Chris Lattner8c204872006-10-14 05:19:21 +0000969 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +0000970 BufferPtr = BufferEnd;
971 FormTokenWithChars(Result, BufferEnd);
Chris Lattner8c204872006-10-14 05:19:21 +0000972 Result.setKind(tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +0000973 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000974 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000975
Chris Lattner30a2fa12006-07-19 06:31:49 +0000976 // Otherwise, issue diagnostics for unterminated #if and missing newline.
977
978 // If we are in a #if directive, emit an error.
979 while (!ConditionalStack.empty()) {
Chris Lattner538d7f32006-07-20 04:31:52 +0000980 Diag(ConditionalStack.back().IfLoc, diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +0000981 ConditionalStack.pop_back();
982 }
983
984 // If the file was empty or didn't end in a newline, issue a pedwarn.
985 if (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
986 Diag(BufferEnd, diag::ext_no_newline_eof);
987
Chris Lattner22eb9722006-06-18 05:43:12 +0000988 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +0000989
990 // Finally, let the preprocessor handle this.
Chris Lattner2183a6e2006-07-18 06:36:12 +0000991 return PP.HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000992}
993
Chris Lattner678c8802006-07-11 05:46:12 +0000994/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
995/// the specified lexer will return a tok::l_paren token, 0 if it is something
996/// else and 2 if there are no more tokens in the buffer controlled by the
997/// lexer.
998unsigned Lexer::isNextPPTokenLParen() {
999 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
1000
1001 // Switch to 'skipping' mode. This will ensure that we can lex a token
1002 // without emitting diagnostics, disables macro expansion, and will cause EOF
1003 // to return an EOF token instead of popping the include stack.
1004 LexingRawMode = true;
1005
1006 // Save state that can be changed while lexing so that we can restore it.
1007 const char *TmpBufferPtr = BufferPtr;
1008
1009 LexerToken Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001010 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001011 LexTokenInternal(Tok);
1012
1013 // Restore state that may have changed.
1014 BufferPtr = TmpBufferPtr;
1015
1016 // Restore the lexer back to non-skipping mode.
1017 LexingRawMode = false;
1018
1019 if (Tok.getKind() == tok::eof)
1020 return 2;
1021 return Tok.getKind() == tok::l_paren;
1022}
1023
Chris Lattner22eb9722006-06-18 05:43:12 +00001024
1025/// LexTokenInternal - This implements a simple C family lexer. It is an
1026/// extremely performance critical piece of code. This assumes that the buffer
1027/// has a null character at the end of the file. Return true if an error
1028/// occurred and compilation should terminate, false if normal. This returns a
1029/// preprocessing token, not a normal token, as such, it is an internal
1030/// interface. It assumes that the Flags of result have been cleared before
1031/// calling this.
Chris Lattnercb283342006-06-18 06:48:37 +00001032void Lexer::LexTokenInternal(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001033LexNextToken:
1034 // New token, can't need cleaning yet.
Chris Lattner8c204872006-10-14 05:19:21 +00001035 Result.clearFlag(LexerToken::NeedsCleaning);
1036 Result.setIdentifierInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001037
1038 // CurPtr - Cache BufferPtr in an automatic variable.
1039 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001040
Chris Lattnereb54b592006-07-10 06:34:27 +00001041 // Small amounts of horizontal whitespace is very common between tokens.
1042 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1043 ++CurPtr;
1044 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1045 ++CurPtr;
1046 BufferPtr = CurPtr;
Chris Lattner8c204872006-10-14 05:19:21 +00001047 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001048 }
1049
Chris Lattner22eb9722006-06-18 05:43:12 +00001050 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1051
1052 // Read a character, advancing over it.
1053 char Char = getAndAdvanceChar(CurPtr, Result);
1054 switch (Char) {
1055 case 0: // Null.
1056 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001057 if (CurPtr-1 == BufferEnd) {
1058 // Read the PP instance variable into an automatic variable, because
1059 // LexEndOfFile will often delete 'this'.
1060 Preprocessor &PPCache = PP;
1061 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1062 return; // Got a token to return.
1063 return PPCache.Lex(Result);
1064 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001065
Chris Lattnercb283342006-06-18 06:48:37 +00001066 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner8c204872006-10-14 05:19:21 +00001067 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001068 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001069 goto LexNextToken; // GCC isn't tail call eliminating.
1070 case '\n':
1071 case '\r':
1072 // If we are inside a preprocessor directive and we see the end of line,
1073 // we know we are done with the directive, so return an EOM token.
1074 if (ParsingPreprocessorDirective) {
1075 // Done parsing the "line".
1076 ParsingPreprocessorDirective = false;
1077
Chris Lattner457fc152006-07-29 06:30:25 +00001078 // Restore comment saving mode, in case it was disabled for directive.
1079 KeepCommentMode = Features.KeepComments;
1080
Chris Lattner22eb9722006-06-18 05:43:12 +00001081 // Since we consumed a newline, we are back at the start of a line.
1082 IsAtStartOfLine = true;
1083
Chris Lattner8c204872006-10-14 05:19:21 +00001084 Result.setKind(tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001085 break;
1086 }
1087 // The returned token is at the start of the line.
Chris Lattner8c204872006-10-14 05:19:21 +00001088 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001089 // No leading whitespace seen so far.
Chris Lattner8c204872006-10-14 05:19:21 +00001090 Result.clearFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001091 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001092 goto LexNextToken; // GCC isn't tail call eliminating.
1093 case ' ':
1094 case '\t':
1095 case '\f':
1096 case '\v':
Chris Lattner8c204872006-10-14 05:19:21 +00001097 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001098 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001099 goto LexNextToken; // GCC isn't tail call eliminating.
1100
1101 case 'L':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001102 // Notify MIOpt that we read a non-whitespace/non-comment token.
1103 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001104 Char = getCharAndSize(CurPtr, SizeTmp);
1105
1106 // Wide string literal.
1107 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001108 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1109 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001110
1111 // Wide character constant.
1112 if (Char == '\'')
1113 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1114 // FALL THROUGH, treating L like the start of an identifier.
1115
1116 // C99 6.4.2: Identifiers.
1117 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1118 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1119 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1120 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1121 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1122 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1123 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1124 case 'v': case 'w': case 'x': case 'y': case 'z':
1125 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001126 // Notify MIOpt that we read a non-whitespace/non-comment token.
1127 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001128 return LexIdentifier(Result, CurPtr);
1129
1130 // C99 6.4.4.1: Integer Constants.
1131 // C99 6.4.4.2: Floating Constants.
1132 case '0': case '1': case '2': case '3': case '4':
1133 case '5': case '6': case '7': case '8': case '9':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001134 // Notify MIOpt that we read a non-whitespace/non-comment token.
1135 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001136 return LexNumericConstant(Result, CurPtr);
1137
1138 // C99 6.4.4: Character Constants.
1139 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001140 // Notify MIOpt that we read a non-whitespace/non-comment token.
1141 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001142 return LexCharConstant(Result, CurPtr);
1143
1144 // C99 6.4.5: String Literals.
1145 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001146 // Notify MIOpt that we read a non-whitespace/non-comment token.
1147 MIOpt.ReadToken();
Chris Lattnerd3e98952006-10-06 05:22:26 +00001148 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001149
1150 // C99 6.4.6: Punctuators.
1151 case '?':
Chris Lattner8c204872006-10-14 05:19:21 +00001152 Result.setKind(tok::question);
Chris Lattner22eb9722006-06-18 05:43:12 +00001153 break;
1154 case '[':
Chris Lattner8c204872006-10-14 05:19:21 +00001155 Result.setKind(tok::l_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001156 break;
1157 case ']':
Chris Lattner8c204872006-10-14 05:19:21 +00001158 Result.setKind(tok::r_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001159 break;
1160 case '(':
Chris Lattner8c204872006-10-14 05:19:21 +00001161 Result.setKind(tok::l_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001162 break;
1163 case ')':
Chris Lattner8c204872006-10-14 05:19:21 +00001164 Result.setKind(tok::r_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001165 break;
1166 case '{':
Chris Lattner8c204872006-10-14 05:19:21 +00001167 Result.setKind(tok::l_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001168 break;
1169 case '}':
Chris Lattner8c204872006-10-14 05:19:21 +00001170 Result.setKind(tok::r_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001171 break;
1172 case '.':
1173 Char = getCharAndSize(CurPtr, SizeTmp);
1174 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001175 // Notify MIOpt that we read a non-whitespace/non-comment token.
1176 MIOpt.ReadToken();
1177
Chris Lattner22eb9722006-06-18 05:43:12 +00001178 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1179 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001180 Result.setKind(tok::periodstar);
Chris Lattner22eb9722006-06-18 05:43:12 +00001181 CurPtr += SizeTmp;
1182 } else if (Char == '.' &&
1183 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner8c204872006-10-14 05:19:21 +00001184 Result.setKind(tok::ellipsis);
Chris Lattner22eb9722006-06-18 05:43:12 +00001185 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1186 SizeTmp2, Result);
1187 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001188 Result.setKind(tok::period);
Chris Lattner22eb9722006-06-18 05:43:12 +00001189 }
1190 break;
1191 case '&':
1192 Char = getCharAndSize(CurPtr, SizeTmp);
1193 if (Char == '&') {
Chris Lattner8c204872006-10-14 05:19:21 +00001194 Result.setKind(tok::ampamp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001195 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1196 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001197 Result.setKind(tok::ampequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001198 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1199 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001200 Result.setKind(tok::amp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001201 }
1202 break;
1203 case '*':
1204 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001205 Result.setKind(tok::starequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001206 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1207 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001208 Result.setKind(tok::star);
Chris Lattner22eb9722006-06-18 05:43:12 +00001209 }
1210 break;
1211 case '+':
1212 Char = getCharAndSize(CurPtr, SizeTmp);
1213 if (Char == '+') {
Chris Lattner8c204872006-10-14 05:19:21 +00001214 Result.setKind(tok::plusplus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001215 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1216 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001217 Result.setKind(tok::plusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001218 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1219 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001220 Result.setKind(tok::plus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001221 }
1222 break;
1223 case '-':
1224 Char = getCharAndSize(CurPtr, SizeTmp);
1225 if (Char == '-') {
Chris Lattner8c204872006-10-14 05:19:21 +00001226 Result.setKind(tok::minusminus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001227 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1228 } else if (Char == '>' && Features.CPlusPlus &&
1229 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001230 Result.setKind(tok::arrowstar); // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00001231 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1232 SizeTmp2, Result);
1233 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001234 Result.setKind(tok::arrow);
Chris Lattner22eb9722006-06-18 05:43:12 +00001235 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1236 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001237 Result.setKind(tok::minusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001238 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1239 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001240 Result.setKind(tok::minus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001241 }
1242 break;
1243 case '~':
Chris Lattner8c204872006-10-14 05:19:21 +00001244 Result.setKind(tok::tilde);
Chris Lattner22eb9722006-06-18 05:43:12 +00001245 break;
1246 case '!':
1247 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001248 Result.setKind(tok::exclaimequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001249 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1250 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001251 Result.setKind(tok::exclaim);
Chris Lattner22eb9722006-06-18 05:43:12 +00001252 }
1253 break;
1254 case '/':
1255 // 6.4.9: Comments
1256 Char = getCharAndSize(CurPtr, SizeTmp);
1257 if (Char == '/') { // BCPL comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001258 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1259 goto LexNextToken; // GCC isn't tail call eliminating.
1260 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001261 } else if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001262 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1263 goto LexNextToken; // GCC isn't tail call eliminating.
1264 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001265 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001266 Result.setKind(tok::slashequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001267 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1268 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001269 Result.setKind(tok::slash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001270 }
1271 break;
1272 case '%':
1273 Char = getCharAndSize(CurPtr, SizeTmp);
1274 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001275 Result.setKind(tok::percentequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001276 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1277 } else if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001278 Result.setKind(tok::r_brace); // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00001279 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1280 } else if (Features.Digraphs && Char == ':') {
1281 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001282 Char = getCharAndSize(CurPtr, SizeTmp);
1283 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001284 Result.setKind(tok::hashhash); // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00001285 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1286 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001287 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001288 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001289 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1290 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner22eb9722006-06-18 05:43:12 +00001291 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001292 Result.setKind(tok::hash); // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00001293
1294 // We parsed a # character. If this occurs at the start of the line,
1295 // it's actually the start of a preprocessing directive. Callback to
1296 // the preprocessor to handle it.
1297 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001298 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001299 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001300 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001301
1302 // As an optimization, if the preprocessor didn't switch lexers, tail
1303 // recurse.
1304 if (PP.isCurrentLexer(this)) {
1305 // Start a new token. If this is a #include or something, the PP may
1306 // want us starting at the beginning of the line again. If so, set
1307 // the StartOfLine flag.
1308 if (IsAtStartOfLine) {
Chris Lattner8c204872006-10-14 05:19:21 +00001309 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001310 IsAtStartOfLine = false;
1311 }
1312 goto LexNextToken; // GCC isn't tail call eliminating.
1313 }
1314
1315 return PP.Lex(Result);
1316 }
1317 }
1318 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001319 Result.setKind(tok::percent);
Chris Lattner22eb9722006-06-18 05:43:12 +00001320 }
1321 break;
1322 case '<':
1323 Char = getCharAndSize(CurPtr, SizeTmp);
1324 if (ParsingFilename) {
1325 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1326 } else if (Char == '<' &&
1327 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001328 Result.setKind(tok::lesslessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001329 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1330 SizeTmp2, Result);
1331 } else if (Char == '<') {
Chris Lattner8c204872006-10-14 05:19:21 +00001332 Result.setKind(tok::lessless);
Chris Lattner22eb9722006-06-18 05:43:12 +00001333 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1334 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001335 Result.setKind(tok::lessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001336 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1337 } else if (Features.Digraphs && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001338 Result.setKind(tok::l_square); // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00001339 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1340 } else if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001341 Result.setKind(tok::l_brace); // '<%' -> '{'
Chris Lattner22eb9722006-06-18 05:43:12 +00001342 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1343 } else if (Features.CPPMinMax && Char == '?') { // <?
1344 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerd01e2912006-06-18 16:22:51 +00001345 Diag(BufferPtr, diag::min_max_deprecated);
Chris Lattner22eb9722006-06-18 05:43:12 +00001346
1347 if (getCharAndSize(CurPtr, SizeTmp) == '=') { // <?=
Chris Lattner8c204872006-10-14 05:19:21 +00001348 Result.setKind(tok::lessquestionequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001349 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1350 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001351 Result.setKind(tok::lessquestion);
Chris Lattner22eb9722006-06-18 05:43:12 +00001352 }
1353 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001354 Result.setKind(tok::less);
Chris Lattner22eb9722006-06-18 05:43:12 +00001355 }
1356 break;
1357 case '>':
1358 Char = getCharAndSize(CurPtr, SizeTmp);
1359 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001360 Result.setKind(tok::greaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001361 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1362 } else if (Char == '>' &&
1363 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001364 Result.setKind(tok::greatergreaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001365 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1366 SizeTmp2, Result);
1367 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001368 Result.setKind(tok::greatergreater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001369 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1370 } else if (Features.CPPMinMax && Char == '?') {
1371 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerd01e2912006-06-18 16:22:51 +00001372 Diag(BufferPtr, diag::min_max_deprecated);
Chris Lattner22eb9722006-06-18 05:43:12 +00001373
1374 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001375 Result.setKind(tok::greaterquestionequal); // >?=
Chris Lattner22eb9722006-06-18 05:43:12 +00001376 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1377 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001378 Result.setKind(tok::greaterquestion); // >?
Chris Lattner22eb9722006-06-18 05:43:12 +00001379 }
1380 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001381 Result.setKind(tok::greater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001382 }
1383 break;
1384 case '^':
1385 Char = getCharAndSize(CurPtr, SizeTmp);
1386 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001387 Result.setKind(tok::caretequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001388 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1389 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001390 Result.setKind(tok::caret);
Chris Lattner22eb9722006-06-18 05:43:12 +00001391 }
1392 break;
1393 case '|':
1394 Char = getCharAndSize(CurPtr, SizeTmp);
1395 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001396 Result.setKind(tok::pipeequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001397 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1398 } else if (Char == '|') {
Chris Lattner8c204872006-10-14 05:19:21 +00001399 Result.setKind(tok::pipepipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001400 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1401 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001402 Result.setKind(tok::pipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001403 }
1404 break;
1405 case ':':
1406 Char = getCharAndSize(CurPtr, SizeTmp);
1407 if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001408 Result.setKind(tok::r_square); // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00001409 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1410 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001411 Result.setKind(tok::coloncolon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001412 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1413 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001414 Result.setKind(tok::colon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001415 }
1416 break;
1417 case ';':
Chris Lattner8c204872006-10-14 05:19:21 +00001418 Result.setKind(tok::semi);
Chris Lattner22eb9722006-06-18 05:43:12 +00001419 break;
1420 case '=':
1421 Char = getCharAndSize(CurPtr, SizeTmp);
1422 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001423 Result.setKind(tok::equalequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001424 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1425 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001426 Result.setKind(tok::equal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001427 }
1428 break;
1429 case ',':
Chris Lattner8c204872006-10-14 05:19:21 +00001430 Result.setKind(tok::comma);
Chris Lattner22eb9722006-06-18 05:43:12 +00001431 break;
1432 case '#':
1433 Char = getCharAndSize(CurPtr, SizeTmp);
1434 if (Char == '#') {
Chris Lattner8c204872006-10-14 05:19:21 +00001435 Result.setKind(tok::hashhash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001436 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001437 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001438 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001439 Diag(BufferPtr, diag::charize_microsoft_ext);
1440 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001441 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001442 Result.setKind(tok::hash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001443 // We parsed a # character. If this occurs at the start of the line,
1444 // it's actually the start of a preprocessing directive. Callback to
1445 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00001446 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001447 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001448 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001449 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001450
1451 // As an optimization, if the preprocessor didn't switch lexers, tail
1452 // recurse.
1453 if (PP.isCurrentLexer(this)) {
1454 // Start a new token. If this is a #include or something, the PP may
1455 // want us starting at the beginning of the line again. If so, set
1456 // the StartOfLine flag.
1457 if (IsAtStartOfLine) {
Chris Lattner8c204872006-10-14 05:19:21 +00001458 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001459 IsAtStartOfLine = false;
1460 }
1461 goto LexNextToken; // GCC isn't tail call eliminating.
1462 }
1463 return PP.Lex(Result);
1464 }
1465 }
1466 break;
1467
1468 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00001469 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00001470 // FALL THROUGH.
1471 default:
1472 // Objective C support.
1473 if (CurPtr[-1] == '@' && Features.ObjC1) {
Chris Lattner8c204872006-10-14 05:19:21 +00001474 Result.setKind(tok::at);
Chris Lattner22eb9722006-06-18 05:43:12 +00001475 break;
1476 } else if (CurPtr[-1] == '$' && Features.DollarIdents) {// $ in identifiers.
Chris Lattnercb283342006-06-18 06:48:37 +00001477 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001478 // Notify MIOpt that we read a non-whitespace/non-comment token.
1479 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001480 return LexIdentifier(Result, CurPtr);
1481 }
1482
Chris Lattner8c204872006-10-14 05:19:21 +00001483 Result.setKind(tok::unknown);
Chris Lattner041bef82006-07-11 05:52:53 +00001484 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001485 }
1486
Chris Lattner371ac8a2006-07-04 07:11:10 +00001487 // Notify MIOpt that we read a non-whitespace/non-comment token.
1488 MIOpt.ReadToken();
1489
Chris Lattnerd01e2912006-06-18 16:22:51 +00001490 // Update the location of token as well as BufferPtr.
1491 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001492}