blob: ee8a2e2b8c5a66cb82ee865a95bcf6dc317ad7dd [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;
115 for (unsigned i = 'a'; i <= 'z'; ++i)
116 CharInfo[i] = CharInfo[i+'A'-'a'] = CHAR_LETTER;
117 for (unsigned i = '0'; i <= '9'; ++i)
118 CharInfo[i] = CHAR_NUMBER;
119}
120
121/// isIdentifierBody - Return true if this is the body character of an
122/// identifier, which is [a-zA-Z0-9_].
123static inline bool isIdentifierBody(unsigned char c) {
124 return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER);
125}
126
127/// isHorizontalWhitespace - Return true if this character is horizontal
128/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
129static inline bool isHorizontalWhitespace(unsigned char c) {
130 return CharInfo[c] & CHAR_HORZ_WS;
131}
132
133/// isWhitespace - Return true if this character is horizontal or vertical
134/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
135/// for '\0'.
136static inline bool isWhitespace(unsigned char c) {
137 return CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS);
138}
139
140/// isNumberBody - Return true if this is the body character of an
141/// preprocessing number, which is [a-zA-Z0-9_.].
142static inline bool isNumberBody(unsigned char c) {
143 return CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD);
144}
145
Chris Lattnerd01e2912006-06-18 16:22:51 +0000146
Chris Lattner22eb9722006-06-18 05:43:12 +0000147//===----------------------------------------------------------------------===//
148// Diagnostics forwarding code.
149//===----------------------------------------------------------------------===//
150
151/// getSourceLocation - Return a source location identifier for the specified
152/// offset in the current file.
153SourceLocation Lexer::getSourceLocation(const char *Loc) const {
Chris Lattner8bbfe462006-07-02 22:27:49 +0000154 assert(Loc >= InputFile->getBufferStart() && Loc <= BufferEnd &&
Chris Lattner4cca5ba2006-07-02 20:05:54 +0000155 "Location out of range for this buffer!");
Chris Lattner8bbfe462006-07-02 22:27:49 +0000156 return SourceLocation(CurFileID, Loc-InputFile->getBufferStart());
Chris Lattner22eb9722006-06-18 05:43:12 +0000157}
158
159
160/// Diag - Forwarding function for diagnostics. This translate a source
161/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattnercb283342006-06-18 06:48:37 +0000162void Lexer::Diag(const char *Loc, unsigned DiagID,
Chris Lattner22eb9722006-06-18 05:43:12 +0000163 const std::string &Msg) const {
Chris Lattner538d7f32006-07-20 04:31:52 +0000164 if (LexingRawMode && Diagnostic::isNoteWarningOrExtension(DiagID))
165 return;
Chris Lattnercb283342006-06-18 06:48:37 +0000166 PP.Diag(getSourceLocation(Loc), DiagID, Msg);
Chris Lattner22eb9722006-06-18 05:43:12 +0000167}
Chris Lattner538d7f32006-07-20 04:31:52 +0000168void Lexer::Diag(SourceLocation Loc, unsigned DiagID,
169 const std::string &Msg) const {
170 if (LexingRawMode && Diagnostic::isNoteWarningOrExtension(DiagID))
171 return;
172 PP.Diag(Loc, DiagID, Msg);
173}
174
Chris Lattner22eb9722006-06-18 05:43:12 +0000175
176//===----------------------------------------------------------------------===//
177// Trigraph and Escaped Newline Handling Code.
178//===----------------------------------------------------------------------===//
179
180/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
181/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
182static char GetTrigraphCharForLetter(char Letter) {
183 switch (Letter) {
184 default: return 0;
185 case '=': return '#';
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 }
195}
196
197/// DecodeTrigraphChar - If the specified character is a legal trigraph when
198/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
199/// return the result character. Finally, emit a warning about trigraph use
200/// whether trigraphs are enabled or not.
201static char DecodeTrigraphChar(const char *CP, Lexer *L) {
202 char Res = GetTrigraphCharForLetter(*CP);
203 if (Res && L) {
204 if (!L->getFeatures().Trigraphs) {
205 L->Diag(CP-2, diag::trigraph_ignored);
206 return 0;
207 } else {
208 L->Diag(CP-2, diag::trigraph_converted, std::string()+Res);
209 }
210 }
211 return Res;
212}
213
214/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
215/// get its size, and return it. This is tricky in several cases:
216/// 1. If currently at the start of a trigraph, we warn about the trigraph,
217/// then either return the trigraph (skipping 3 chars) or the '?',
218/// depending on whether trigraphs are enabled or not.
219/// 2. If this is an escaped newline (potentially with whitespace between
220/// the backslash and newline), implicitly skip the newline and return
221/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000222/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000223///
224/// This handles the slow/uncommon case of the getCharAndSize method. Here we
225/// know that we can accumulate into Size, and that we have already incremented
226/// Ptr by Size bytes.
227///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000228/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
229/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000230///
231char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
232 LexerToken *Tok) {
233 // If we have a slash, look for an escaped newline.
234 if (Ptr[0] == '\\') {
235 ++Size;
236 ++Ptr;
237Slash:
238 // Common case, backslash-char where the char is not whitespace.
239 if (!isWhitespace(Ptr[0])) return '\\';
240
241 // See if we have optional whitespace characters followed by a newline.
242 {
243 unsigned SizeTmp = 0;
244 do {
245 ++SizeTmp;
246 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
247 // Remember that this token needs to be cleaned.
Chris Lattner8c204872006-10-14 05:19:21 +0000248 if (Tok) Tok->setFlag(LexerToken::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000249
250 // Warn if there was whitespace between the backslash and newline.
251 if (SizeTmp != 1 && Tok)
252 Diag(Ptr, diag::backslash_newline_space);
253
254 // If this is a \r\n or \n\r, skip the newlines.
255 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
256 Ptr[SizeTmp-1] != Ptr[SizeTmp])
257 ++SizeTmp;
258
259 // Found backslash<whitespace><newline>. Parse the char after it.
260 Size += SizeTmp;
261 Ptr += SizeTmp;
262 // Use slow version to accumulate a correct size field.
263 return getCharAndSizeSlow(Ptr, Size, Tok);
264 }
265 } while (isWhitespace(Ptr[SizeTmp]));
266 }
267
268 // Otherwise, this is not an escaped newline, just return the slash.
269 return '\\';
270 }
271
272 // If this is a trigraph, process it.
273 if (Ptr[0] == '?' && Ptr[1] == '?') {
274 // If this is actually a legal trigraph (not something like "??x"), emit
275 // a trigraph warning. If so, and if trigraphs are enabled, return it.
276 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
277 // Remember that this token needs to be cleaned.
Chris Lattner8c204872006-10-14 05:19:21 +0000278 if (Tok) Tok->setFlag(LexerToken::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000279
280 Ptr += 3;
281 Size += 3;
282 if (C == '\\') goto Slash;
283 return C;
284 }
285 }
286
287 // If this is neither, return a single character.
288 ++Size;
289 return *Ptr;
290}
291
Chris Lattnerd01e2912006-06-18 16:22:51 +0000292
Chris Lattner22eb9722006-06-18 05:43:12 +0000293/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
294/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
295/// and that we have already incremented Ptr by Size bytes.
296///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000297/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
298/// be updated to match.
299char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000300 const LangOptions &Features) {
301 // If we have a slash, look for an escaped newline.
302 if (Ptr[0] == '\\') {
303 ++Size;
304 ++Ptr;
305Slash:
306 // Common case, backslash-char where the char is not whitespace.
307 if (!isWhitespace(Ptr[0])) return '\\';
308
309 // See if we have optional whitespace characters followed by a newline.
310 {
311 unsigned SizeTmp = 0;
312 do {
313 ++SizeTmp;
314 if (Ptr[SizeTmp-1] == '\n' || Ptr[SizeTmp-1] == '\r') {
315
316 // If this is a \r\n or \n\r, skip the newlines.
317 if ((Ptr[SizeTmp] == '\r' || Ptr[SizeTmp] == '\n') &&
318 Ptr[SizeTmp-1] != Ptr[SizeTmp])
319 ++SizeTmp;
320
321 // Found backslash<whitespace><newline>. Parse the char after it.
322 Size += SizeTmp;
323 Ptr += SizeTmp;
324
325 // Use slow version to accumulate a correct size field.
326 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
327 }
328 } while (isWhitespace(Ptr[SizeTmp]));
329 }
330
331 // Otherwise, this is not an escaped newline, just return the slash.
332 return '\\';
333 }
334
335 // If this is a trigraph, process it.
336 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
337 // If this is actually a legal trigraph (not something like "??x"), return
338 // it.
339 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
340 Ptr += 3;
341 Size += 3;
342 if (C == '\\') goto Slash;
343 return C;
344 }
345 }
346
347 // If this is neither, return a single character.
348 ++Size;
349 return *Ptr;
350}
351
Chris Lattner22eb9722006-06-18 05:43:12 +0000352//===----------------------------------------------------------------------===//
353// Helper methods for lexing.
354//===----------------------------------------------------------------------===//
355
Chris Lattnercb283342006-06-18 06:48:37 +0000356void Lexer::LexIdentifier(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000357 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
358 unsigned Size;
359 unsigned char C = *CurPtr++;
360 while (isIdentifierBody(C)) {
361 C = *CurPtr++;
362 }
363 --CurPtr; // Back up over the skipped character.
364
365 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
366 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000367 // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000368 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
369FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000370 const char *IdStart = BufferPtr;
Chris Lattnerd01e2912006-06-18 16:22:51 +0000371 FormTokenWithChars(Result, CurPtr);
Chris Lattner8c204872006-10-14 05:19:21 +0000372 Result.setKind(tok::identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000373
Chris Lattner0f1f5052006-07-20 04:16:23 +0000374 // If we are in raw mode, return this identifier raw. There is no need to
375 // look up identifier information or attempt to macro expand it.
376 if (LexingRawMode) return;
377
Chris Lattnercefc7682006-07-08 08:28:12 +0000378 // Fill in Result.IdentifierInfo, looking up the identifier in the
379 // identifier table.
380 PP.LookUpIdentifierInfo(Result, IdStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000381
Chris Lattnerc5a00062006-06-18 16:41:01 +0000382 // Finally, now that we know we have an identifier, pass this off to the
383 // preprocessor, which may macro expand it or something.
Chris Lattner22eb9722006-06-18 05:43:12 +0000384 return PP.HandleIdentifier(Result);
385 }
386
387 // Otherwise, $,\,? in identifier found. Enter slower path.
388
389 C = getCharAndSize(CurPtr, Size);
390 while (1) {
391 if (C == '$') {
392 // If we hit a $ and they are not supported in identifiers, we are done.
393 if (!Features.DollarIdents) goto FinishIdentifier;
394
395 // Otherwise, emit a diagnostic and continue.
Chris Lattnercb283342006-06-18 06:48:37 +0000396 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000397 CurPtr = ConsumeChar(CurPtr, Size, Result);
398 C = getCharAndSize(CurPtr, Size);
399 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000400 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000401 // Found end of identifier.
402 goto FinishIdentifier;
403 }
404
405 // Otherwise, this character is good, consume it.
406 CurPtr = ConsumeChar(CurPtr, Size, Result);
407
408 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000409 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000410 CurPtr = ConsumeChar(CurPtr, Size, Result);
411 C = getCharAndSize(CurPtr, Size);
412 }
413 }
414}
415
416
417/// LexNumericConstant - Lex the remainer of a integer or floating point
418/// constant. From[-1] is the first character lexed. Return the end of the
419/// constant.
Chris Lattnercb283342006-06-18 06:48:37 +0000420void Lexer::LexNumericConstant(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000421 unsigned Size;
422 char C = getCharAndSize(CurPtr, Size);
423 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000424 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000425 CurPtr = ConsumeChar(CurPtr, Size, Result);
426 PrevCh = C;
427 C = getCharAndSize(CurPtr, Size);
428 }
429
430 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
431 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
432 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
433
434 // If we have a hex FP constant, continue.
435 if (Features.HexFloats &&
436 (C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p'))
437 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
438
Chris Lattner8c204872006-10-14 05:19:21 +0000439 Result.setKind(tok::numeric_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000440
Chris Lattnerd01e2912006-06-18 16:22:51 +0000441 // Update the location of token as well as BufferPtr.
442 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000443}
444
445/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
446/// either " or L".
Chris Lattnerd3e98952006-10-06 05:22:26 +0000447void Lexer::LexStringLiteral(LexerToken &Result, const char *CurPtr, bool Wide){
Chris Lattner22eb9722006-06-18 05:43:12 +0000448 const char *NulCharacter = 0; // Does this string contain the \0 character?
449
450 char C = getAndAdvanceChar(CurPtr, Result);
451 while (C != '"') {
452 // Skip escaped characters.
453 if (C == '\\') {
454 // Skip the escaped character.
455 C = getAndAdvanceChar(CurPtr, Result);
456 } else if (C == '\n' || C == '\r' || // Newline.
457 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000458 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000459 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000460 FormTokenWithChars(Result, CurPtr-1);
461 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000462 } else if (C == 0) {
463 NulCharacter = CurPtr-1;
464 }
465 C = getAndAdvanceChar(CurPtr, Result);
466 }
467
Chris Lattner5a78a022006-07-20 06:02:19 +0000468 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000469 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000470
Chris Lattner8c204872006-10-14 05:19:21 +0000471 Result.setKind(Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000472
Chris Lattnerd01e2912006-06-18 16:22:51 +0000473 // Update the location of the token as well as the BufferPtr instance var.
474 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000475}
476
477/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
478/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattnercb283342006-06-18 06:48:37 +0000479void Lexer::LexAngledStringLiteral(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000480 const char *NulCharacter = 0; // Does this string contain the \0 character?
481
482 char C = getAndAdvanceChar(CurPtr, Result);
483 while (C != '>') {
484 // Skip escaped characters.
485 if (C == '\\') {
486 // Skip the escaped character.
487 C = getAndAdvanceChar(CurPtr, Result);
488 } else if (C == '\n' || C == '\r' || // Newline.
489 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000490 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner8c204872006-10-14 05:19:21 +0000491 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000492 FormTokenWithChars(Result, CurPtr-1);
493 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000494 } else if (C == 0) {
495 NulCharacter = CurPtr-1;
496 }
497 C = getAndAdvanceChar(CurPtr, Result);
498 }
499
Chris Lattner5a78a022006-07-20 06:02:19 +0000500 // If a nul character existed in the string, warn about it.
Chris Lattnercb283342006-06-18 06:48:37 +0000501 if (NulCharacter) Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000502
Chris Lattner8c204872006-10-14 05:19:21 +0000503 Result.setKind(tok::angle_string_literal);
Chris Lattner22eb9722006-06-18 05:43:12 +0000504
Chris Lattnerd01e2912006-06-18 16:22:51 +0000505 // Update the location of token as well as BufferPtr.
506 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000507}
508
509
510/// LexCharConstant - Lex the remainder of a character constant, after having
511/// lexed either ' or L'.
Chris Lattnercb283342006-06-18 06:48:37 +0000512void Lexer::LexCharConstant(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000513 const char *NulCharacter = 0; // Does this character contain the \0 character?
514
515 // Handle the common case of 'x' and '\y' efficiently.
516 char C = getAndAdvanceChar(CurPtr, Result);
517 if (C == '\'') {
Chris Lattnera5f4c882006-07-20 06:08:47 +0000518 if (!LexingRawMode) Diag(BufferPtr, diag::err_empty_character);
Chris Lattner8c204872006-10-14 05:19:21 +0000519 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000520 FormTokenWithChars(Result, CurPtr);
521 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000522 } else if (C == '\\') {
523 // Skip the escaped character.
524 // FIXME: UCN's.
525 C = getAndAdvanceChar(CurPtr, Result);
526 }
527
528 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
529 ++CurPtr;
530 } else {
531 // Fall back on generic code for embedded nulls, newlines, wide chars.
532 do {
533 // Skip escaped characters.
534 if (C == '\\') {
535 // Skip the escaped character.
536 C = getAndAdvanceChar(CurPtr, Result);
537 } else if (C == '\n' || C == '\r' || // Newline.
538 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnera5f4c882006-07-20 06:08:47 +0000539 if (!LexingRawMode) Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner8c204872006-10-14 05:19:21 +0000540 Result.setKind(tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000541 FormTokenWithChars(Result, CurPtr-1);
542 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000543 } else if (C == 0) {
544 NulCharacter = CurPtr-1;
545 }
546 C = getAndAdvanceChar(CurPtr, Result);
547 } while (C != '\'');
548 }
549
Chris Lattnercb283342006-06-18 06:48:37 +0000550 if (NulCharacter) Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000551
Chris Lattner8c204872006-10-14 05:19:21 +0000552 Result.setKind(tok::char_constant);
Chris Lattner22eb9722006-06-18 05:43:12 +0000553
Chris Lattnerd01e2912006-06-18 16:22:51 +0000554 // Update the location of token as well as BufferPtr.
555 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000556}
557
558/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
559/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattnercb283342006-06-18 06:48:37 +0000560void Lexer::SkipWhitespace(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000561 // Whitespace - Skip it, then return the token after the whitespace.
562 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
563 while (1) {
564 // Skip horizontal whitespace very aggressively.
565 while (isHorizontalWhitespace(Char))
566 Char = *++CurPtr;
567
568 // Otherwise if we something other than whitespace, we're done.
569 if (Char != '\n' && Char != '\r')
570 break;
571
572 if (ParsingPreprocessorDirective) {
573 // End of preprocessor directive line, let LexTokenInternal handle this.
574 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +0000575 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000576 }
577
578 // ok, but handle newline.
579 // The returned token is at the start of the line.
Chris Lattner8c204872006-10-14 05:19:21 +0000580 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000581 // No leading whitespace seen so far.
Chris Lattner8c204872006-10-14 05:19:21 +0000582 Result.clearFlag(LexerToken::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000583 Char = *++CurPtr;
584 }
585
586 // If this isn't immediately after a newline, there is leading space.
587 char PrevChar = CurPtr[-1];
588 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattner8c204872006-10-14 05:19:21 +0000589 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000590
591 // If the next token is obviously a // or /* */ comment, skip it efficiently
592 // too (without going through the big switch stmt).
Chris Lattner457fc152006-07-29 06:30:25 +0000593 if (Char == '/' && CurPtr[1] == '/' && !KeepCommentMode) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000594 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000595 SkipBCPLComment(Result, CurPtr+1);
596 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000597 }
Chris Lattner457fc152006-07-29 06:30:25 +0000598 if (Char == '/' && CurPtr[1] == '*' && !KeepCommentMode) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000599 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000600 SkipBlockComment(Result, CurPtr+2);
601 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000602 }
603 BufferPtr = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000604}
605
606// SkipBCPLComment - We have just read the // characters from input. Skip until
607// we find the newline character thats terminate the comment. Then update
608/// BufferPtr and return.
Chris Lattner457fc152006-07-29 06:30:25 +0000609bool Lexer::SkipBCPLComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000610 // If BCPL comments aren't explicitly enabled for this language, emit an
611 // extension warning.
612 if (!Features.BCPLComment) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000613 Diag(BufferPtr, diag::ext_bcpl_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000614
615 // Mark them enabled so we only emit one warning for this translation
616 // unit.
617 Features.BCPLComment = true;
618 }
619
620 // Scan over the body of the comment. The common case, when scanning, is that
621 // the comment contains normal ascii characters with nothing interesting in
622 // them. As such, optimize for this case with the inner loop.
623 char C;
624 do {
625 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +0000626 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
627 // If we find a \n character, scan backwards, checking to see if it's an
628 // escaped newline, like we do for block comments.
Chris Lattner22eb9722006-06-18 05:43:12 +0000629
630 // Skip over characters in the fast loop.
631 while (C != 0 && // Potentially EOF.
632 C != '\\' && // Potentially escaped newline.
633 C != '?' && // Potentially trigraph.
634 C != '\n' && C != '\r') // Newline or DOS-style newline.
635 C = *++CurPtr;
636
637 // If this is a newline, we're done.
638 if (C == '\n' || C == '\r')
639 break; // Found the newline? Break out!
640
641 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
642 // properly decode the character.
643 const char *OldPtr = CurPtr;
644 C = getAndAdvanceChar(CurPtr, Result);
645
646 // If we read multiple characters, and one of those characters was a \r or
647 // \n, then we had an escaped newline within the comment. Emit diagnostic.
648 if (CurPtr != OldPtr+1) {
649 for (; OldPtr != CurPtr; ++OldPtr)
650 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnercb283342006-06-18 06:48:37 +0000651 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
652 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000653 }
654 }
655
Chris Lattner457fc152006-07-29 06:30:25 +0000656 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
Chris Lattner22eb9722006-06-18 05:43:12 +0000657 } while (C != '\n' && C != '\r');
658
Chris Lattner457fc152006-07-29 06:30:25 +0000659 // Found but did not consume the newline.
660
661 // If we are returning comments as tokens, return this comment as a token.
662 if (KeepCommentMode)
663 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000664
665 // If we are inside a preprocessor directive and we see the end of line,
666 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +0000667 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000668 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000669 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000670 }
671
672 // Otherwise, eat the \n character. We don't care if this is a \n\r or
673 // \r\n sequence.
674 ++CurPtr;
675
676 // The next returned token is at the start of the line.
Chris Lattner8c204872006-10-14 05:19:21 +0000677 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000678 // No leading whitespace seen so far.
Chris Lattner8c204872006-10-14 05:19:21 +0000679 Result.clearFlag(LexerToken::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000680
681 // It is common for the tokens immediately after a // comment to be
682 // whitespace (indentation for the next line). Instead of going through the
683 // big switch, handle it efficiently now.
684 if (isWhitespace(*CurPtr)) {
Chris Lattner8c204872006-10-14 05:19:21 +0000685 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000686 SkipWhitespace(Result, CurPtr+1);
687 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000688 }
689
690 BufferPtr = CurPtr;
Chris Lattner457fc152006-07-29 06:30:25 +0000691 return true;
692}
Chris Lattner22eb9722006-06-18 05:43:12 +0000693
Chris Lattner457fc152006-07-29 06:30:25 +0000694/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
695/// an appropriate way and return it.
696bool Lexer::SaveBCPLComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner8c204872006-10-14 05:19:21 +0000697 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +0000698 FormTokenWithChars(Result, CurPtr);
699
700 // If this BCPL-style comment is in a macro definition, transmogrify it into
701 // a C-style block comment.
702 if (ParsingPreprocessorDirective) {
703 std::string Spelling = PP.getSpelling(Result);
704 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
705 Spelling[1] = '*'; // Change prefix to "/*".
706 Spelling += "*/"; // add suffix.
707
Chris Lattner8c204872006-10-14 05:19:21 +0000708 Result.setLocation(PP.CreateString(&Spelling[0], Spelling.size(),
Chris Lattner457fc152006-07-29 06:30:25 +0000709 Result.getLocation()));
Chris Lattner8c204872006-10-14 05:19:21 +0000710 Result.setLength(Spelling.size());
Chris Lattner457fc152006-07-29 06:30:25 +0000711 }
712 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000713}
714
Chris Lattnercb283342006-06-18 06:48:37 +0000715/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
716/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner22eb9722006-06-18 05:43:12 +0000717/// diagnostic if so. We know that the is inside of a block comment.
Chris Lattner1f583052006-06-18 06:53:56 +0000718static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
719 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000720 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Chris Lattner22eb9722006-06-18 05:43:12 +0000721
722 // Back up off the newline.
723 --CurPtr;
724
725 // If this is a two-character newline sequence, skip the other character.
726 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
727 // \n\n or \r\r -> not escaped newline.
728 if (CurPtr[0] == CurPtr[1])
729 return false;
730 // \n\r or \r\n -> skip the newline.
731 --CurPtr;
732 }
733
734 // If we have horizontal whitespace, skip over it. We allow whitespace
735 // between the slash and newline.
736 bool HasSpace = false;
737 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
738 --CurPtr;
739 HasSpace = true;
740 }
741
742 // If we have a slash, we know this is an escaped newline.
743 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +0000744 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000745 } else {
746 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +0000747 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
748 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +0000749 return false;
Chris Lattnercb283342006-06-18 06:48:37 +0000750
751 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +0000752 CurPtr -= 2;
753
754 // If no trigraphs are enabled, warn that we ignored this trigraph and
755 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +0000756 if (!L->getFeatures().Trigraphs) {
757 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000758 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000759 }
Chris Lattner1f583052006-06-18 06:53:56 +0000760 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000761 }
762
763 // Warn about having an escaped newline between the */ characters.
Chris Lattner1f583052006-06-18 06:53:56 +0000764 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Chris Lattner22eb9722006-06-18 05:43:12 +0000765
766 // If there was space between the backslash and newline, warn about it.
Chris Lattner1f583052006-06-18 06:53:56 +0000767 if (HasSpace) L->Diag(CurPtr, diag::backslash_newline_space);
Chris Lattner22eb9722006-06-18 05:43:12 +0000768
Chris Lattnercb283342006-06-18 06:48:37 +0000769 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000770}
771
772/// SkipBlockComment - We have just read the /* characters from input. Read
773/// until we find the */ characters that terminate the comment. Note that we
774/// don't bother decoding trigraphs or escaped newlines in block comments,
775/// because they cannot cause the comment to end. The only thing that can
776/// happen is the comment could end with an escaped newline between the */ end
777/// of comment.
Chris Lattner457fc152006-07-29 06:30:25 +0000778bool Lexer::SkipBlockComment(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000779 // Scan one character past where we should, looking for a '/' character. Once
780 // we find it, check to see if it was preceeded by a *. This common
781 // optimization helps people who like to put a lot of * characters in their
782 // comments.
783 unsigned char C = *CurPtr++;
784 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000785 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000786 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +0000787 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000788 }
789
790 while (1) {
791 // Skip over all non-interesting characters.
792 // TODO: Vectorize this. Note: memchr on Darwin is slower than this loop.
793 while (C != '/' && C != '\0')
794 C = *CurPtr++;
795
796 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000797 if (CurPtr[-2] == '*') // We found the final */. We're done!
798 break;
799
800 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +0000801 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000802 // We found the final */, though it had an escaped newline between the
803 // * and /. We're done!
804 break;
805 }
806 }
807 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
808 // If this is a /* inside of the comment, emit a warning. Don't do this
809 // if this is a /*/, which will end the comment. This misses cases with
810 // embedded escaped newlines, but oh well.
Chris Lattnercb283342006-06-18 06:48:37 +0000811 Diag(CurPtr-1, diag::nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000812 }
813 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000814 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +0000815 // Note: the user probably forgot a */. We could continue immediately
816 // after the /*, but this would involve lexing a lot of what really is the
817 // comment, which surely would confuse the parser.
818 BufferPtr = CurPtr-1;
Chris Lattner457fc152006-07-29 06:30:25 +0000819 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000820 }
821 C = *CurPtr++;
822 }
Chris Lattner457fc152006-07-29 06:30:25 +0000823
824 // If we are returning comments as tokens, return this comment as a token.
825 if (KeepCommentMode) {
Chris Lattner8c204872006-10-14 05:19:21 +0000826 Result.setKind(tok::comment);
Chris Lattner457fc152006-07-29 06:30:25 +0000827 FormTokenWithChars(Result, CurPtr);
828 return false;
829 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000830
831 // It is common for the tokens immediately after a /**/ comment to be
832 // whitespace. Instead of going through the big switch, handle it
833 // efficiently now.
834 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner8c204872006-10-14 05:19:21 +0000835 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000836 SkipWhitespace(Result, CurPtr+1);
837 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000838 }
839
840 // Otherwise, just return so that the next character will be lexed as a token.
841 BufferPtr = CurPtr;
Chris Lattner8c204872006-10-14 05:19:21 +0000842 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +0000843 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000844}
845
846//===----------------------------------------------------------------------===//
847// Primary Lexing Entry Points
848//===----------------------------------------------------------------------===//
849
850/// LexIncludeFilename - After the preprocessor has parsed a #include, lex and
851/// (potentially) macro expand the filename.
Chris Lattner269c2322006-06-25 06:23:00 +0000852std::string Lexer::LexIncludeFilename(LexerToken &FilenameTok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000853 assert(ParsingPreprocessorDirective &&
854 ParsingFilename == false &&
855 "Must be in a preprocessing directive!");
856
857 // We are now parsing a filename!
858 ParsingFilename = true;
859
Chris Lattner269c2322006-06-25 06:23:00 +0000860 // Lex the filename.
861 Lex(FilenameTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000862
863 // We should have gotten the filename now.
864 ParsingFilename = false;
865
866 // No filename?
Chris Lattner269c2322006-06-25 06:23:00 +0000867 if (FilenameTok.getKind() == tok::eom) {
Chris Lattner538d7f32006-07-20 04:31:52 +0000868 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner269c2322006-06-25 06:23:00 +0000869 return "";
Chris Lattnercb283342006-06-18 06:48:37 +0000870 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000871
Chris Lattner269c2322006-06-25 06:23:00 +0000872 // Get the text form of the filename.
873 std::string Filename = PP.getSpelling(FilenameTok);
874 assert(!Filename.empty() && "Can't have tokens with empty spellings!");
875
876 // Make sure the filename is <x> or "x".
877 if (Filename[0] == '<') {
878 if (Filename[Filename.size()-1] != '>') {
Chris Lattner538d7f32006-07-20 04:31:52 +0000879 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner8c204872006-10-14 05:19:21 +0000880 FilenameTok.setKind(tok::eom);
Chris Lattner269c2322006-06-25 06:23:00 +0000881 return "";
882 }
883 } else if (Filename[0] == '"') {
884 if (Filename[Filename.size()-1] != '"') {
Chris Lattner538d7f32006-07-20 04:31:52 +0000885 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner8c204872006-10-14 05:19:21 +0000886 FilenameTok.setKind(tok::eom);
Chris Lattner269c2322006-06-25 06:23:00 +0000887 return "";
888 }
889 } else {
Chris Lattner538d7f32006-07-20 04:31:52 +0000890 Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
Chris Lattner8c204872006-10-14 05:19:21 +0000891 FilenameTok.setKind(tok::eom);
Chris Lattner269c2322006-06-25 06:23:00 +0000892 return "";
Chris Lattner22eb9722006-06-18 05:43:12 +0000893 }
Chris Lattner269c2322006-06-25 06:23:00 +0000894
895 // Diagnose #include "" as invalid.
896 if (Filename.size() == 2) {
Chris Lattner538d7f32006-07-20 04:31:52 +0000897 Diag(FilenameTok.getLocation(), diag::err_pp_empty_filename);
Chris Lattner8c204872006-10-14 05:19:21 +0000898 FilenameTok.setKind(tok::eom);
Chris Lattner269c2322006-06-25 06:23:00 +0000899 return "";
900 }
901
902 return Filename;
Chris Lattner22eb9722006-06-18 05:43:12 +0000903}
904
905/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
906/// uninterpreted string. This switches the lexer out of directive mode.
907std::string Lexer::ReadToEndOfLine() {
908 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
909 "Must be in a preprocessing directive!");
910 std::string Result;
911 LexerToken Tmp;
912
913 // CurPtr - Cache BufferPtr in an automatic variable.
914 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000915 while (1) {
916 char Char = getAndAdvanceChar(CurPtr, Tmp);
917 switch (Char) {
918 default:
919 Result += Char;
920 break;
921 case 0: // Null.
922 // Found end of file?
923 if (CurPtr-1 != BufferEnd) {
924 // Nope, normal character, continue.
925 Result += Char;
926 break;
927 }
928 // FALL THROUGH.
929 case '\r':
930 case '\n':
931 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
932 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
933 BufferPtr = CurPtr-1;
934
935 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +0000936 Lex(Tmp);
Chris Lattner22eb9722006-06-18 05:43:12 +0000937 assert(Tmp.getKind() == tok::eom && "Unexpected token!");
Chris Lattner22eb9722006-06-18 05:43:12 +0000938
939 // Finally, we're done, return the string we found.
940 return Result;
941 }
942 }
943}
944
945/// LexEndOfFile - CurPtr points to the end of this file. Handle this
946/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +0000947/// This returns true if Result contains a token, false if PP.Lex should be
948/// called again.
949bool Lexer::LexEndOfFile(LexerToken &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000950 // If we hit the end of the file while parsing a preprocessor directive,
951 // end the preprocessor directive first. The next token returned will
952 // then be the end of file.
953 if (ParsingPreprocessorDirective) {
954 // Done parsing the "line".
955 ParsingPreprocessorDirective = false;
Chris Lattner8c204872006-10-14 05:19:21 +0000956 Result.setKind(tok::eom);
Chris Lattnerd01e2912006-06-18 16:22:51 +0000957 // Update the location of token as well as BufferPtr.
958 FormTokenWithChars(Result, CurPtr);
Chris Lattner457fc152006-07-29 06:30:25 +0000959
960 // Restore comment saving mode, in case it was disabled for directive.
961 KeepCommentMode = Features.KeepComments;
Chris Lattner2183a6e2006-07-18 06:36:12 +0000962 return true; // Have a token.
Chris Lattner22eb9722006-06-18 05:43:12 +0000963 }
964
Chris Lattner30a2fa12006-07-19 06:31:49 +0000965 // If we are in raw mode, return this event as an EOF token. Let the caller
966 // that put us in raw mode handle the event.
967 if (LexingRawMode) {
Chris Lattner8c204872006-10-14 05:19:21 +0000968 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +0000969 BufferPtr = BufferEnd;
970 FormTokenWithChars(Result, BufferEnd);
Chris Lattner8c204872006-10-14 05:19:21 +0000971 Result.setKind(tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +0000972 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +0000973 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000974
Chris Lattner30a2fa12006-07-19 06:31:49 +0000975 // Otherwise, issue diagnostics for unterminated #if and missing newline.
976
977 // If we are in a #if directive, emit an error.
978 while (!ConditionalStack.empty()) {
Chris Lattner538d7f32006-07-20 04:31:52 +0000979 Diag(ConditionalStack.back().IfLoc, diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +0000980 ConditionalStack.pop_back();
981 }
982
983 // If the file was empty or didn't end in a newline, issue a pedwarn.
984 if (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
985 Diag(BufferEnd, diag::ext_no_newline_eof);
986
Chris Lattner22eb9722006-06-18 05:43:12 +0000987 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +0000988
989 // Finally, let the preprocessor handle this.
Chris Lattner2183a6e2006-07-18 06:36:12 +0000990 return PP.HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +0000991}
992
Chris Lattner678c8802006-07-11 05:46:12 +0000993/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
994/// the specified lexer will return a tok::l_paren token, 0 if it is something
995/// else and 2 if there are no more tokens in the buffer controlled by the
996/// lexer.
997unsigned Lexer::isNextPPTokenLParen() {
998 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
999
1000 // Switch to 'skipping' mode. This will ensure that we can lex a token
1001 // without emitting diagnostics, disables macro expansion, and will cause EOF
1002 // to return an EOF token instead of popping the include stack.
1003 LexingRawMode = true;
1004
1005 // Save state that can be changed while lexing so that we can restore it.
1006 const char *TmpBufferPtr = BufferPtr;
1007
1008 LexerToken Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001009 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001010 LexTokenInternal(Tok);
1011
1012 // Restore state that may have changed.
1013 BufferPtr = TmpBufferPtr;
1014
1015 // Restore the lexer back to non-skipping mode.
1016 LexingRawMode = false;
1017
1018 if (Tok.getKind() == tok::eof)
1019 return 2;
1020 return Tok.getKind() == tok::l_paren;
1021}
1022
Chris Lattner22eb9722006-06-18 05:43:12 +00001023
1024/// LexTokenInternal - This implements a simple C family lexer. It is an
1025/// extremely performance critical piece of code. This assumes that the buffer
1026/// has a null character at the end of the file. Return true if an error
1027/// occurred and compilation should terminate, false if normal. This returns a
1028/// preprocessing token, not a normal token, as such, it is an internal
1029/// interface. It assumes that the Flags of result have been cleared before
1030/// calling this.
Chris Lattnercb283342006-06-18 06:48:37 +00001031void Lexer::LexTokenInternal(LexerToken &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001032LexNextToken:
1033 // New token, can't need cleaning yet.
Chris Lattner8c204872006-10-14 05:19:21 +00001034 Result.clearFlag(LexerToken::NeedsCleaning);
1035 Result.setIdentifierInfo(0);
Chris Lattner22eb9722006-06-18 05:43:12 +00001036
1037 // CurPtr - Cache BufferPtr in an automatic variable.
1038 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001039
Chris Lattnereb54b592006-07-10 06:34:27 +00001040 // Small amounts of horizontal whitespace is very common between tokens.
1041 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1042 ++CurPtr;
1043 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1044 ++CurPtr;
1045 BufferPtr = CurPtr;
Chris Lattner8c204872006-10-14 05:19:21 +00001046 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001047 }
1048
Chris Lattner22eb9722006-06-18 05:43:12 +00001049 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
1050
1051 // Read a character, advancing over it.
1052 char Char = getAndAdvanceChar(CurPtr, Result);
1053 switch (Char) {
1054 case 0: // Null.
1055 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001056 if (CurPtr-1 == BufferEnd) {
1057 // Read the PP instance variable into an automatic variable, because
1058 // LexEndOfFile will often delete 'this'.
1059 Preprocessor &PPCache = PP;
1060 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1061 return; // Got a token to return.
1062 return PPCache.Lex(Result);
1063 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001064
Chris Lattnercb283342006-06-18 06:48:37 +00001065 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner8c204872006-10-14 05:19:21 +00001066 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001067 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001068 goto LexNextToken; // GCC isn't tail call eliminating.
1069 case '\n':
1070 case '\r':
1071 // If we are inside a preprocessor directive and we see the end of line,
1072 // we know we are done with the directive, so return an EOM token.
1073 if (ParsingPreprocessorDirective) {
1074 // Done parsing the "line".
1075 ParsingPreprocessorDirective = false;
1076
Chris Lattner457fc152006-07-29 06:30:25 +00001077 // Restore comment saving mode, in case it was disabled for directive.
1078 KeepCommentMode = Features.KeepComments;
1079
Chris Lattner22eb9722006-06-18 05:43:12 +00001080 // Since we consumed a newline, we are back at the start of a line.
1081 IsAtStartOfLine = true;
1082
Chris Lattner8c204872006-10-14 05:19:21 +00001083 Result.setKind(tok::eom);
Chris Lattner22eb9722006-06-18 05:43:12 +00001084 break;
1085 }
1086 // The returned token is at the start of the line.
Chris Lattner8c204872006-10-14 05:19:21 +00001087 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001088 // No leading whitespace seen so far.
Chris Lattner8c204872006-10-14 05:19:21 +00001089 Result.clearFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001090 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001091 goto LexNextToken; // GCC isn't tail call eliminating.
1092 case ' ':
1093 case '\t':
1094 case '\f':
1095 case '\v':
Chris Lattner8c204872006-10-14 05:19:21 +00001096 Result.setFlag(LexerToken::LeadingSpace);
Chris Lattnercb283342006-06-18 06:48:37 +00001097 SkipWhitespace(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001098 goto LexNextToken; // GCC isn't tail call eliminating.
1099
1100 case 'L':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001101 // Notify MIOpt that we read a non-whitespace/non-comment token.
1102 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001103 Char = getCharAndSize(CurPtr, SizeTmp);
1104
1105 // Wide string literal.
1106 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001107 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1108 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001109
1110 // Wide character constant.
1111 if (Char == '\'')
1112 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1113 // FALL THROUGH, treating L like the start of an identifier.
1114
1115 // C99 6.4.2: Identifiers.
1116 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1117 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1118 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1119 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1120 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1121 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1122 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1123 case 'v': case 'w': case 'x': case 'y': case 'z':
1124 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001125 // Notify MIOpt that we read a non-whitespace/non-comment token.
1126 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001127 return LexIdentifier(Result, CurPtr);
1128
1129 // C99 6.4.4.1: Integer Constants.
1130 // C99 6.4.4.2: Floating Constants.
1131 case '0': case '1': case '2': case '3': case '4':
1132 case '5': case '6': case '7': case '8': case '9':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001133 // Notify MIOpt that we read a non-whitespace/non-comment token.
1134 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001135 return LexNumericConstant(Result, CurPtr);
1136
1137 // C99 6.4.4: Character Constants.
1138 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001139 // Notify MIOpt that we read a non-whitespace/non-comment token.
1140 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001141 return LexCharConstant(Result, CurPtr);
1142
1143 // C99 6.4.5: String Literals.
1144 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001145 // Notify MIOpt that we read a non-whitespace/non-comment token.
1146 MIOpt.ReadToken();
Chris Lattnerd3e98952006-10-06 05:22:26 +00001147 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001148
1149 // C99 6.4.6: Punctuators.
1150 case '?':
Chris Lattner8c204872006-10-14 05:19:21 +00001151 Result.setKind(tok::question);
Chris Lattner22eb9722006-06-18 05:43:12 +00001152 break;
1153 case '[':
Chris Lattner8c204872006-10-14 05:19:21 +00001154 Result.setKind(tok::l_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001155 break;
1156 case ']':
Chris Lattner8c204872006-10-14 05:19:21 +00001157 Result.setKind(tok::r_square);
Chris Lattner22eb9722006-06-18 05:43:12 +00001158 break;
1159 case '(':
Chris Lattner8c204872006-10-14 05:19:21 +00001160 Result.setKind(tok::l_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001161 break;
1162 case ')':
Chris Lattner8c204872006-10-14 05:19:21 +00001163 Result.setKind(tok::r_paren);
Chris Lattner22eb9722006-06-18 05:43:12 +00001164 break;
1165 case '{':
Chris Lattner8c204872006-10-14 05:19:21 +00001166 Result.setKind(tok::l_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001167 break;
1168 case '}':
Chris Lattner8c204872006-10-14 05:19:21 +00001169 Result.setKind(tok::r_brace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001170 break;
1171 case '.':
1172 Char = getCharAndSize(CurPtr, SizeTmp);
1173 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001174 // Notify MIOpt that we read a non-whitespace/non-comment token.
1175 MIOpt.ReadToken();
1176
Chris Lattner22eb9722006-06-18 05:43:12 +00001177 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1178 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001179 Result.setKind(tok::periodstar);
Chris Lattner22eb9722006-06-18 05:43:12 +00001180 CurPtr += SizeTmp;
1181 } else if (Char == '.' &&
1182 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner8c204872006-10-14 05:19:21 +00001183 Result.setKind(tok::ellipsis);
Chris Lattner22eb9722006-06-18 05:43:12 +00001184 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1185 SizeTmp2, Result);
1186 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001187 Result.setKind(tok::period);
Chris Lattner22eb9722006-06-18 05:43:12 +00001188 }
1189 break;
1190 case '&':
1191 Char = getCharAndSize(CurPtr, SizeTmp);
1192 if (Char == '&') {
Chris Lattner8c204872006-10-14 05:19:21 +00001193 Result.setKind(tok::ampamp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001194 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1195 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001196 Result.setKind(tok::ampequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001197 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1198 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001199 Result.setKind(tok::amp);
Chris Lattner22eb9722006-06-18 05:43:12 +00001200 }
1201 break;
1202 case '*':
1203 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001204 Result.setKind(tok::starequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001205 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1206 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001207 Result.setKind(tok::star);
Chris Lattner22eb9722006-06-18 05:43:12 +00001208 }
1209 break;
1210 case '+':
1211 Char = getCharAndSize(CurPtr, SizeTmp);
1212 if (Char == '+') {
Chris Lattner8c204872006-10-14 05:19:21 +00001213 Result.setKind(tok::plusplus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001214 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1215 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001216 Result.setKind(tok::plusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001217 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1218 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001219 Result.setKind(tok::plus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001220 }
1221 break;
1222 case '-':
1223 Char = getCharAndSize(CurPtr, SizeTmp);
1224 if (Char == '-') {
Chris Lattner8c204872006-10-14 05:19:21 +00001225 Result.setKind(tok::minusminus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001226 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1227 } else if (Char == '>' && Features.CPlusPlus &&
1228 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') {
Chris Lattner8c204872006-10-14 05:19:21 +00001229 Result.setKind(tok::arrowstar); // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00001230 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1231 SizeTmp2, Result);
1232 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001233 Result.setKind(tok::arrow);
Chris Lattner22eb9722006-06-18 05:43:12 +00001234 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1235 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001236 Result.setKind(tok::minusequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001237 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1238 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001239 Result.setKind(tok::minus);
Chris Lattner22eb9722006-06-18 05:43:12 +00001240 }
1241 break;
1242 case '~':
Chris Lattner8c204872006-10-14 05:19:21 +00001243 Result.setKind(tok::tilde);
Chris Lattner22eb9722006-06-18 05:43:12 +00001244 break;
1245 case '!':
1246 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001247 Result.setKind(tok::exclaimequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001248 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1249 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001250 Result.setKind(tok::exclaim);
Chris Lattner22eb9722006-06-18 05:43:12 +00001251 }
1252 break;
1253 case '/':
1254 // 6.4.9: Comments
1255 Char = getCharAndSize(CurPtr, SizeTmp);
1256 if (Char == '/') { // BCPL comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001257 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1258 goto LexNextToken; // GCC isn't tail call eliminating.
1259 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001260 } else if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001261 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1262 goto LexNextToken; // GCC isn't tail call eliminating.
1263 return; // KeepCommentMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001264 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001265 Result.setKind(tok::slashequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001266 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1267 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001268 Result.setKind(tok::slash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001269 }
1270 break;
1271 case '%':
1272 Char = getCharAndSize(CurPtr, SizeTmp);
1273 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001274 Result.setKind(tok::percentequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001275 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1276 } else if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001277 Result.setKind(tok::r_brace); // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00001278 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1279 } else if (Features.Digraphs && Char == ':') {
1280 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001281 Char = getCharAndSize(CurPtr, SizeTmp);
1282 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001283 Result.setKind(tok::hashhash); // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00001284 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1285 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001286 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001287 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001288 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1289 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner22eb9722006-06-18 05:43:12 +00001290 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001291 Result.setKind(tok::hash); // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00001292
1293 // We parsed a # character. If this occurs at the start of the line,
1294 // it's actually the start of a preprocessing directive. Callback to
1295 // the preprocessor to handle it.
1296 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001297 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001298 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001299 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001300
1301 // As an optimization, if the preprocessor didn't switch lexers, tail
1302 // recurse.
1303 if (PP.isCurrentLexer(this)) {
1304 // Start a new token. If this is a #include or something, the PP may
1305 // want us starting at the beginning of the line again. If so, set
1306 // the StartOfLine flag.
1307 if (IsAtStartOfLine) {
Chris Lattner8c204872006-10-14 05:19:21 +00001308 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001309 IsAtStartOfLine = false;
1310 }
1311 goto LexNextToken; // GCC isn't tail call eliminating.
1312 }
1313
1314 return PP.Lex(Result);
1315 }
1316 }
1317 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001318 Result.setKind(tok::percent);
Chris Lattner22eb9722006-06-18 05:43:12 +00001319 }
1320 break;
1321 case '<':
1322 Char = getCharAndSize(CurPtr, SizeTmp);
1323 if (ParsingFilename) {
1324 return LexAngledStringLiteral(Result, CurPtr+SizeTmp);
1325 } else if (Char == '<' &&
1326 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001327 Result.setKind(tok::lesslessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001328 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1329 SizeTmp2, Result);
1330 } else if (Char == '<') {
Chris Lattner8c204872006-10-14 05:19:21 +00001331 Result.setKind(tok::lessless);
Chris Lattner22eb9722006-06-18 05:43:12 +00001332 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1333 } else if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001334 Result.setKind(tok::lessequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001335 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1336 } else if (Features.Digraphs && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001337 Result.setKind(tok::l_square); // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00001338 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1339 } else if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001340 Result.setKind(tok::l_brace); // '<%' -> '{'
Chris Lattner22eb9722006-06-18 05:43:12 +00001341 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1342 } else if (Features.CPPMinMax && Char == '?') { // <?
1343 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerd01e2912006-06-18 16:22:51 +00001344 Diag(BufferPtr, diag::min_max_deprecated);
Chris Lattner22eb9722006-06-18 05:43:12 +00001345
1346 if (getCharAndSize(CurPtr, SizeTmp) == '=') { // <?=
Chris Lattner8c204872006-10-14 05:19:21 +00001347 Result.setKind(tok::lessquestionequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001348 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1349 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001350 Result.setKind(tok::lessquestion);
Chris Lattner22eb9722006-06-18 05:43:12 +00001351 }
1352 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001353 Result.setKind(tok::less);
Chris Lattner22eb9722006-06-18 05:43:12 +00001354 }
1355 break;
1356 case '>':
1357 Char = getCharAndSize(CurPtr, SizeTmp);
1358 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001359 Result.setKind(tok::greaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001360 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1361 } else if (Char == '>' &&
1362 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001363 Result.setKind(tok::greatergreaterequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001364 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1365 SizeTmp2, Result);
1366 } else if (Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001367 Result.setKind(tok::greatergreater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001368 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1369 } else if (Features.CPPMinMax && Char == '?') {
1370 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerd01e2912006-06-18 16:22:51 +00001371 Diag(BufferPtr, diag::min_max_deprecated);
Chris Lattner22eb9722006-06-18 05:43:12 +00001372
1373 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001374 Result.setKind(tok::greaterquestionequal); // >?=
Chris Lattner22eb9722006-06-18 05:43:12 +00001375 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1376 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001377 Result.setKind(tok::greaterquestion); // >?
Chris Lattner22eb9722006-06-18 05:43:12 +00001378 }
1379 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001380 Result.setKind(tok::greater);
Chris Lattner22eb9722006-06-18 05:43:12 +00001381 }
1382 break;
1383 case '^':
1384 Char = getCharAndSize(CurPtr, SizeTmp);
1385 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001386 Result.setKind(tok::caretequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001387 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1388 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001389 Result.setKind(tok::caret);
Chris Lattner22eb9722006-06-18 05:43:12 +00001390 }
1391 break;
1392 case '|':
1393 Char = getCharAndSize(CurPtr, SizeTmp);
1394 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001395 Result.setKind(tok::pipeequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001396 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1397 } else if (Char == '|') {
Chris Lattner8c204872006-10-14 05:19:21 +00001398 Result.setKind(tok::pipepipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001399 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1400 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001401 Result.setKind(tok::pipe);
Chris Lattner22eb9722006-06-18 05:43:12 +00001402 }
1403 break;
1404 case ':':
1405 Char = getCharAndSize(CurPtr, SizeTmp);
1406 if (Features.Digraphs && Char == '>') {
Chris Lattner8c204872006-10-14 05:19:21 +00001407 Result.setKind(tok::r_square); // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00001408 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1409 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner8c204872006-10-14 05:19:21 +00001410 Result.setKind(tok::coloncolon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001411 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1412 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001413 Result.setKind(tok::colon);
Chris Lattner22eb9722006-06-18 05:43:12 +00001414 }
1415 break;
1416 case ';':
Chris Lattner8c204872006-10-14 05:19:21 +00001417 Result.setKind(tok::semi);
Chris Lattner22eb9722006-06-18 05:43:12 +00001418 break;
1419 case '=':
1420 Char = getCharAndSize(CurPtr, SizeTmp);
1421 if (Char == '=') {
Chris Lattner8c204872006-10-14 05:19:21 +00001422 Result.setKind(tok::equalequal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001423 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1424 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001425 Result.setKind(tok::equal);
Chris Lattner22eb9722006-06-18 05:43:12 +00001426 }
1427 break;
1428 case ',':
Chris Lattner8c204872006-10-14 05:19:21 +00001429 Result.setKind(tok::comma);
Chris Lattner22eb9722006-06-18 05:43:12 +00001430 break;
1431 case '#':
1432 Char = getCharAndSize(CurPtr, SizeTmp);
1433 if (Char == '#') {
Chris Lattner8c204872006-10-14 05:19:21 +00001434 Result.setKind(tok::hashhash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001435 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001436 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner8c204872006-10-14 05:19:21 +00001437 Result.setKind(tok::hashat);
Chris Lattner2b271db2006-07-15 05:41:09 +00001438 Diag(BufferPtr, diag::charize_microsoft_ext);
1439 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001440 } else {
Chris Lattner8c204872006-10-14 05:19:21 +00001441 Result.setKind(tok::hash);
Chris Lattner22eb9722006-06-18 05:43:12 +00001442 // We parsed a # character. If this occurs at the start of the line,
1443 // it's actually the start of a preprocessing directive. Callback to
1444 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00001445 // FIXME: -fpreprocessed mode??
Chris Lattner3ebcf4e2006-07-11 05:39:23 +00001446 if (Result.isAtStartOfLine() && !LexingRawMode) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001447 BufferPtr = CurPtr;
Chris Lattnercb283342006-06-18 06:48:37 +00001448 PP.HandleDirective(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001449
1450 // As an optimization, if the preprocessor didn't switch lexers, tail
1451 // recurse.
1452 if (PP.isCurrentLexer(this)) {
1453 // Start a new token. If this is a #include or something, the PP may
1454 // want us starting at the beginning of the line again. If so, set
1455 // the StartOfLine flag.
1456 if (IsAtStartOfLine) {
Chris Lattner8c204872006-10-14 05:19:21 +00001457 Result.setFlag(LexerToken::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001458 IsAtStartOfLine = false;
1459 }
1460 goto LexNextToken; // GCC isn't tail call eliminating.
1461 }
1462 return PP.Lex(Result);
1463 }
1464 }
1465 break;
1466
1467 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00001468 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00001469 // FALL THROUGH.
1470 default:
1471 // Objective C support.
1472 if (CurPtr[-1] == '@' && Features.ObjC1) {
Chris Lattner8c204872006-10-14 05:19:21 +00001473 Result.setKind(tok::at);
Chris Lattner22eb9722006-06-18 05:43:12 +00001474 break;
1475 } else if (CurPtr[-1] == '$' && Features.DollarIdents) {// $ in identifiers.
Chris Lattnercb283342006-06-18 06:48:37 +00001476 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner371ac8a2006-07-04 07:11:10 +00001477 // Notify MIOpt that we read a non-whitespace/non-comment token.
1478 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001479 return LexIdentifier(Result, CurPtr);
1480 }
1481
Chris Lattner8c204872006-10-14 05:19:21 +00001482 Result.setKind(tok::unknown);
Chris Lattner041bef82006-07-11 05:52:53 +00001483 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001484 }
1485
Chris Lattner371ac8a2006-07-04 07:11:10 +00001486 // Notify MIOpt that we read a non-whitespace/non-comment token.
1487 MIOpt.ReadToken();
1488
Chris Lattnerd01e2912006-06-18 16:22:51 +00001489 // Update the location of token as well as BufferPtr.
1490 FormTokenWithChars(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001491}