blob: 5e435908bebd48bbd7de99b6c4057945a5c15ace [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerd2177732007-07-20 16:59:19 +000010// This file implements the Lexer and Token interfaces.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13//
14// TODO: GCC Diagnostics emitted by the lexer:
15// PEDWARN: (form feed|vertical tab) in preprocessing directive
16//
17// Universal characters, unicode, char mapping:
18// WARNING: `%.*s' is not in NFKC
19// WARNING: `%.*s' is not in NFC
20//
21// Other:
22// 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"
Chris Lattner500d3292009-01-29 05:15:15 +000029#include "clang/Lex/LexDiagnostic.h"
Chris Lattner9dc1f532007-07-20 16:37:10 +000030#include "clang/Basic/SourceManager.h"
Douglas Gregorf033f1d2010-07-20 20:18:03 +000031#include "llvm/ADT/StringSwitch.h"
Chris Lattner409a0362007-07-22 18:38:25 +000032#include "llvm/Support/Compiler.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000033#include "llvm/Support/MemoryBuffer.h"
34#include <cctype>
35using namespace clang;
36
Chris Lattnera2bf1052009-12-17 05:29:40 +000037static void InitCharacterInfo();
Reid Spencer5f016e22007-07-11 17:01:13 +000038
Chris Lattnerdbf388b2007-10-07 08:47:24 +000039//===----------------------------------------------------------------------===//
40// Token Class Implementation
41//===----------------------------------------------------------------------===//
42
Mike Stump1eb44332009-09-09 15:08:12 +000043/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
Chris Lattnerdbf388b2007-10-07 08:47:24 +000044bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
Douglas Gregorbec1c9d2008-12-01 21:46:47 +000045 if (IdentifierInfo *II = getIdentifierInfo())
46 return II->getObjCKeywordID() == objcKey;
47 return false;
Chris Lattnerdbf388b2007-10-07 08:47:24 +000048}
49
50/// getObjCKeywordID - Return the ObjC keyword kind.
51tok::ObjCKeywordKind Token::getObjCKeywordID() const {
52 IdentifierInfo *specId = getIdentifierInfo();
53 return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
54}
55
Chris Lattner53702cd2007-12-13 01:59:49 +000056
Chris Lattnerdbf388b2007-10-07 08:47:24 +000057//===----------------------------------------------------------------------===//
58// Lexer Class Implementation
59//===----------------------------------------------------------------------===//
60
Mike Stump1eb44332009-09-09 15:08:12 +000061void Lexer::InitLexer(const char *BufStart, const char *BufPtr,
Chris Lattner22d91ca2009-01-17 06:55:17 +000062 const char *BufEnd) {
Chris Lattnera2bf1052009-12-17 05:29:40 +000063 InitCharacterInfo();
Mike Stump1eb44332009-09-09 15:08:12 +000064
Chris Lattner22d91ca2009-01-17 06:55:17 +000065 BufferStart = BufStart;
66 BufferPtr = BufPtr;
67 BufferEnd = BufEnd;
Mike Stump1eb44332009-09-09 15:08:12 +000068
Chris Lattner22d91ca2009-01-17 06:55:17 +000069 assert(BufEnd[0] == 0 &&
70 "We assume that the input buffer has a null character at the end"
71 " to simplify lexing!");
Mike Stump1eb44332009-09-09 15:08:12 +000072
Chris Lattner22d91ca2009-01-17 06:55:17 +000073 Is_PragmaLexer = false;
Chris Lattner34f349d2009-12-14 06:16:57 +000074 IsInConflictMarker = false;
Douglas Gregor81b747b2009-09-17 21:32:03 +000075
Chris Lattner22d91ca2009-01-17 06:55:17 +000076 // Start of the file is a start of line.
77 IsAtStartOfLine = true;
Mike Stump1eb44332009-09-09 15:08:12 +000078
Chris Lattner22d91ca2009-01-17 06:55:17 +000079 // We are not after parsing a #.
80 ParsingPreprocessorDirective = false;
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattner22d91ca2009-01-17 06:55:17 +000082 // We are not after parsing #include.
83 ParsingFilename = false;
Mike Stump1eb44332009-09-09 15:08:12 +000084
Chris Lattner22d91ca2009-01-17 06:55:17 +000085 // We are not in raw mode. Raw mode disables diagnostics and interpretation
86 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
87 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
88 // or otherwise skipping over tokens.
89 LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +000090
Chris Lattner22d91ca2009-01-17 06:55:17 +000091 // Default to not keeping comments.
92 ExtendedTokenMode = 0;
93}
94
Chris Lattner0770dab2009-01-17 07:56:59 +000095/// Lexer constructor - Create a new lexer object for the specified buffer
96/// with the specified preprocessor managing the lexing process. This lexer
97/// assumes that the associated file buffer and Preprocessor objects will
98/// outlive it, so it doesn't take ownership of either of them.
Chris Lattner6e290142009-11-30 04:18:44 +000099Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP)
Chris Lattner88d3ac12009-01-17 08:03:42 +0000100 : PreprocessorLexer(&PP, FID),
101 FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
102 Features(PP.getLangOptions()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000103
Chris Lattner0770dab2009-01-17 07:56:59 +0000104 InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
105 InputFile->getBufferEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000106
Chris Lattner0770dab2009-01-17 07:56:59 +0000107 // Default to keeping comments if the preprocessor wants them.
108 SetCommentRetentionState(PP.getCommentRetentionState());
109}
Chris Lattnerdbf388b2007-10-07 08:47:24 +0000110
Chris Lattner168ae2d2007-10-17 20:41:00 +0000111/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner590f0cc2008-10-12 01:15:46 +0000112/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
113/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000114Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattnerde96c0f2009-01-17 07:42:27 +0000115 const char *BufStart, const char *BufPtr, const char *BufEnd)
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000116 : FileLoc(fileloc), Features(features) {
Chris Lattner22d91ca2009-01-17 06:55:17 +0000117
Chris Lattner22d91ca2009-01-17 06:55:17 +0000118 InitLexer(BufStart, BufPtr, BufEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Chris Lattner168ae2d2007-10-17 20:41:00 +0000120 // We *are* in raw mode.
121 LexingRawMode = true;
Chris Lattner168ae2d2007-10-17 20:41:00 +0000122}
123
Chris Lattner025c3a62009-01-17 07:35:14 +0000124/// Lexer constructor - Create a new raw lexer object. This object is only
125/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
126/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner6e290142009-11-30 04:18:44 +0000127Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile,
128 const SourceManager &SM, const LangOptions &features)
Chris Lattner025c3a62009-01-17 07:35:14 +0000129 : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
Chris Lattner025c3a62009-01-17 07:35:14 +0000130
Mike Stump1eb44332009-09-09 15:08:12 +0000131 InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
Chris Lattner025c3a62009-01-17 07:35:14 +0000132 FromFile->getBufferEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000133
Chris Lattner025c3a62009-01-17 07:35:14 +0000134 // We *are* in raw mode.
135 LexingRawMode = true;
136}
137
Chris Lattner42e00d12009-01-17 08:27:52 +0000138/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
139/// _Pragma expansion. This has a variety of magic semantics that this method
140/// sets up. It returns a new'd Lexer that must be delete'd when done.
141///
142/// On entrance to this routine, TokStartLoc is a macro location which has a
143/// spelling loc that indicates the bytes to be lexed for the token and an
144/// instantiation location that indicates where all lexed tokens should be
145/// "expanded from".
146///
147/// FIXME: It would really be nice to make _Pragma just be a wrapper around a
148/// normal lexer that remaps tokens as they fly by. This would require making
149/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer
150/// interface that could handle this stuff. This would pull GetMappedTokenLoc
151/// out of the critical path of the lexer!
152///
Mike Stump1eb44332009-09-09 15:08:12 +0000153Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000154 SourceLocation InstantiationLocStart,
155 SourceLocation InstantiationLocEnd,
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000156 unsigned TokLen, Preprocessor &PP) {
Chris Lattner42e00d12009-01-17 08:27:52 +0000157 SourceManager &SM = PP.getSourceManager();
Chris Lattner42e00d12009-01-17 08:27:52 +0000158
159 // Create the lexer as if we were going to lex the file normally.
Chris Lattnera11d6172009-01-19 07:46:45 +0000160 FileID SpellingFID = SM.getFileID(SpellingLoc);
Chris Lattner6e290142009-11-30 04:18:44 +0000161 const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID);
162 Lexer *L = new Lexer(SpellingFID, InputFile, PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Chris Lattner42e00d12009-01-17 08:27:52 +0000164 // Now that the lexer is created, change the start/end locations so that we
165 // just lex the subsection of the file that we want. This is lexing from a
166 // scratch buffer.
167 const char *StrData = SM.getCharacterData(SpellingLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Chris Lattner42e00d12009-01-17 08:27:52 +0000169 L->BufferPtr = StrData;
170 L->BufferEnd = StrData+TokLen;
Chris Lattner1fa49532009-03-08 08:08:45 +0000171 assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
Chris Lattner42e00d12009-01-17 08:27:52 +0000172
173 // Set the SourceLocation with the remapping information. This ensures that
174 // GetMappedTokenLoc will remap the tokens as they are lexed.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000175 L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
Chris Lattnere7fb4842009-02-15 20:52:18 +0000176 InstantiationLocStart,
177 InstantiationLocEnd, TokLen);
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattner42e00d12009-01-17 08:27:52 +0000179 // Ensure that the lexer thinks it is inside a directive, so that end \n will
180 // return an EOM token.
181 L->ParsingPreprocessorDirective = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Chris Lattner42e00d12009-01-17 08:27:52 +0000183 // This lexer really is for _Pragma.
184 L->Is_PragmaLexer = true;
185 return L;
186}
187
Chris Lattner168ae2d2007-10-17 20:41:00 +0000188
Reid Spencer5f016e22007-07-11 17:01:13 +0000189/// Stringify - Convert the specified string into a C string, with surrounding
190/// ""'s, and with escaped \ and " characters.
191std::string Lexer::Stringify(const std::string &Str, bool Charify) {
192 std::string Result = Str;
193 char Quote = Charify ? '\'' : '"';
194 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
195 if (Result[i] == '\\' || Result[i] == Quote) {
196 Result.insert(Result.begin()+i, '\\');
197 ++i; ++e;
198 }
199 }
200 return Result;
201}
202
Chris Lattnerd8e30832007-07-24 06:57:14 +0000203/// Stringify - Convert the specified string into a C string by escaping '\'
204/// and " characters. This does not add surrounding ""'s to the string.
205void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
206 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
207 if (Str[i] == '\\' || Str[i] == '"') {
208 Str.insert(Str.begin()+i, '\\');
209 ++i; ++e;
210 }
211 }
212}
213
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000214static bool isWhitespace(unsigned char c);
Reid Spencer5f016e22007-07-11 17:01:13 +0000215
Chris Lattner9a611942007-10-17 21:18:47 +0000216/// MeasureTokenLength - Relex the token at the specified location and return
217/// its length in bytes in the input file. If the token needs cleaning (e.g.
218/// includes a trigraph or an escaped newline) then this count includes bytes
219/// that are part of that.
220unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
Chris Lattner2c78b872009-04-14 23:22:57 +0000221 const SourceManager &SM,
222 const LangOptions &LangOpts) {
Chris Lattner9a611942007-10-17 21:18:47 +0000223 // TODO: this could be special cased for common tokens like identifiers, ')',
224 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
Mike Stump1eb44332009-09-09 15:08:12 +0000225 // all obviously single-char tokens. This could use
Chris Lattner9a611942007-10-17 21:18:47 +0000226 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
227 // something.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000228
229 // If this comes from a macro expansion, we really do want the macro name, not
230 // the token this macro expanded to.
Chris Lattner363fdc22009-01-26 22:24:27 +0000231 Loc = SM.getInstantiationLoc(Loc);
232 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
Douglas Gregorf715ca12010-03-16 00:06:06 +0000233 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +0000234 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Douglas Gregorf715ca12010-03-16 00:06:06 +0000235 if (Invalid)
Douglas Gregoraea67db2010-03-15 22:54:52 +0000236 return 0;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +0000237
238 const char *StrData = Buffer.data()+LocInfo.second;
Chris Lattner83503942009-01-17 08:30:10 +0000239
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000240 if (isWhitespace(StrData[0]))
241 return 0;
242
Chris Lattner9a611942007-10-17 21:18:47 +0000243 // Create a lexer starting at the beginning of this token.
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +0000244 Lexer TheLexer(Loc, LangOpts, Buffer.begin(), StrData, Buffer.end());
Chris Lattner39de7402009-10-14 15:04:18 +0000245 TheLexer.SetCommentRetentionState(true);
Chris Lattner9a611942007-10-17 21:18:47 +0000246 Token TheTok;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000247 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner9a611942007-10-17 21:18:47 +0000248 return TheTok.getLength();
249}
250
Douglas Gregora8e5c5b2010-07-22 20:22:31 +0000251SourceLocation Lexer::GetBeginningOfToken(SourceLocation Loc,
252 const SourceManager &SM,
253 const LangOptions &LangOpts) {
254 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
255 bool Invalid = false;
256 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
257 if (Invalid)
258 return Loc;
259
260 // Back up from the current location until we hit the beginning of a line
261 // (or the buffer). We'll relex from that point.
262 const char *BufStart = Buffer.data();
263 const char *StrData = BufStart+LocInfo.second;
264 if (StrData[0] == '\n' || StrData[0] == '\r')
265 return Loc;
266
267 const char *LexStart = StrData;
268 while (LexStart != BufStart) {
269 if (LexStart[0] == '\n' || LexStart[0] == '\r') {
270 ++LexStart;
271 break;
272 }
273
274 --LexStart;
275 }
276
277 // Create a lexer starting at the beginning of this token.
278 SourceLocation LexerStartLoc = Loc.getFileLocWithOffset(-LocInfo.second);
279 Lexer TheLexer(LexerStartLoc, LangOpts, BufStart, LexStart, Buffer.end());
280 TheLexer.SetCommentRetentionState(true);
281
282 // Lex tokens until we find the token that contains the source location.
283 Token TheTok;
284 do {
285 TheLexer.LexFromRawLexer(TheTok);
286
287 if (TheLexer.getBufferLocation() > StrData) {
288 // Lexing this token has taken the lexer past the source location we're
289 // looking for. If the current token encompasses our source location,
290 // return the beginning of that token.
291 if (TheLexer.getBufferLocation() - TheTok.getLength() <= StrData)
292 return TheTok.getLocation();
293
294 // We ended up skipping over the source location entirely, which means
295 // that it points into whitespace. We're done here.
296 break;
297 }
298 } while (TheTok.getKind() != tok::eof);
299
300 // We've passed our source location; just return the original source location.
301 return Loc;
302}
303
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000304namespace {
305 enum PreambleDirectiveKind {
306 PDK_Skipped,
307 PDK_StartIf,
308 PDK_EndIf,
309 PDK_Unknown
310 };
311}
312
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000313std::pair<unsigned, bool>
314Lexer::ComputePreamble(const llvm::MemoryBuffer *Buffer) {
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000315 // Create a lexer starting at the beginning of the file. Note that we use a
316 // "fake" file source location at offset 1 so that the lexer will track our
317 // position within the file.
318 const unsigned StartOffset = 1;
319 SourceLocation StartLoc = SourceLocation::getFromRawEncoding(StartOffset);
320 LangOptions LangOpts;
321 Lexer TheLexer(StartLoc, LangOpts, Buffer->getBufferStart(),
322 Buffer->getBufferStart(), Buffer->getBufferEnd());
323
324 bool InPreprocessorDirective = false;
325 Token TheTok;
326 Token IfStartTok;
327 unsigned IfCount = 0;
328 do {
329 TheLexer.LexFromRawLexer(TheTok);
330
331 if (InPreprocessorDirective) {
332 // If we've hit the end of the file, we're done.
333 if (TheTok.getKind() == tok::eof) {
334 InPreprocessorDirective = false;
335 break;
336 }
337
338 // If we haven't hit the end of the preprocessor directive, skip this
339 // token.
340 if (!TheTok.isAtStartOfLine())
341 continue;
342
343 // We've passed the end of the preprocessor directive, and will look
344 // at this token again below.
345 InPreprocessorDirective = false;
346 }
347
348 // Comments are okay; skip over them.
349 if (TheTok.getKind() == tok::comment)
350 continue;
351
352 if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) {
353 // This is the start of a preprocessor directive.
354 Token HashTok = TheTok;
355 InPreprocessorDirective = true;
356
357 // Figure out which direective this is. Since we're lexing raw tokens,
358 // we don't have an identifier table available. Instead, just look at
359 // the raw identifier to recognize and categorize preprocessor directives.
360 TheLexer.LexFromRawLexer(TheTok);
361 if (TheTok.getKind() == tok::identifier && !TheTok.needsCleaning()) {
362 const char *IdStart = Buffer->getBufferStart()
363 + TheTok.getLocation().getRawEncoding() - 1;
364 llvm::StringRef Keyword(IdStart, TheTok.getLength());
365 PreambleDirectiveKind PDK
366 = llvm::StringSwitch<PreambleDirectiveKind>(Keyword)
367 .Case("include", PDK_Skipped)
368 .Case("__include_macros", PDK_Skipped)
369 .Case("define", PDK_Skipped)
370 .Case("undef", PDK_Skipped)
371 .Case("line", PDK_Skipped)
372 .Case("error", PDK_Skipped)
373 .Case("pragma", PDK_Skipped)
374 .Case("import", PDK_Skipped)
375 .Case("include_next", PDK_Skipped)
376 .Case("warning", PDK_Skipped)
377 .Case("ident", PDK_Skipped)
378 .Case("sccs", PDK_Skipped)
379 .Case("assert", PDK_Skipped)
380 .Case("unassert", PDK_Skipped)
381 .Case("if", PDK_StartIf)
382 .Case("ifdef", PDK_StartIf)
383 .Case("ifndef", PDK_StartIf)
384 .Case("elif", PDK_Skipped)
385 .Case("else", PDK_Skipped)
386 .Case("endif", PDK_EndIf)
387 .Default(PDK_Unknown);
388
389 switch (PDK) {
390 case PDK_Skipped:
391 continue;
392
393 case PDK_StartIf:
394 if (IfCount == 0)
395 IfStartTok = HashTok;
396
397 ++IfCount;
398 continue;
399
400 case PDK_EndIf:
401 // Mismatched #endif. The preamble ends here.
402 if (IfCount == 0)
403 break;
404
405 --IfCount;
406 continue;
407
408 case PDK_Unknown:
409 // We don't know what this directive is; stop at the '#'.
410 break;
411 }
412 }
413
414 // We only end up here if we didn't recognize the preprocessor
415 // directive or it was one that can't occur in the preamble at this
416 // point. Roll back the current token to the location of the '#'.
417 InPreprocessorDirective = false;
418 TheTok = HashTok;
419 }
420
421 // We hit a token
422 break;
423 } while (true);
424
425 SourceLocation End = IfCount? IfStartTok.getLocation() : TheTok.getLocation();
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000426 return std::make_pair(End.getRawEncoding() - StartLoc.getRawEncoding(),
427 IfCount? IfStartTok.isAtStartOfLine()
428 : TheTok.isAtStartOfLine());
Douglas Gregorf033f1d2010-07-20 20:18:03 +0000429}
430
Reid Spencer5f016e22007-07-11 17:01:13 +0000431//===----------------------------------------------------------------------===//
432// Character information.
433//===----------------------------------------------------------------------===//
434
Reid Spencer5f016e22007-07-11 17:01:13 +0000435enum {
436 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
437 CHAR_VERT_WS = 0x02, // '\r', '\n'
438 CHAR_LETTER = 0x04, // a-z,A-Z
439 CHAR_NUMBER = 0x08, // 0-9
440 CHAR_UNDER = 0x10, // _
441 CHAR_PERIOD = 0x20 // .
442};
443
Chris Lattner03b98662009-07-07 17:09:54 +0000444// Statically initialize CharInfo table based on ASCII character set
445// Reference: FreeBSD 7.2 /usr/share/misc/ascii
Chris Lattnera2bf1052009-12-17 05:29:40 +0000446static const unsigned char CharInfo[256] =
Chris Lattner03b98662009-07-07 17:09:54 +0000447{
448// 0 NUL 1 SOH 2 STX 3 ETX
449// 4 EOT 5 ENQ 6 ACK 7 BEL
450 0 , 0 , 0 , 0 ,
451 0 , 0 , 0 , 0 ,
452// 8 BS 9 HT 10 NL 11 VT
453//12 NP 13 CR 14 SO 15 SI
454 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS,
455 CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 ,
456//16 DLE 17 DC1 18 DC2 19 DC3
457//20 DC4 21 NAK 22 SYN 23 ETB
458 0 , 0 , 0 , 0 ,
459 0 , 0 , 0 , 0 ,
460//24 CAN 25 EM 26 SUB 27 ESC
461//28 FS 29 GS 30 RS 31 US
462 0 , 0 , 0 , 0 ,
463 0 , 0 , 0 , 0 ,
464//32 SP 33 ! 34 " 35 #
465//36 $ 37 % 38 & 39 '
466 CHAR_HORZ_WS, 0 , 0 , 0 ,
467 0 , 0 , 0 , 0 ,
468//40 ( 41 ) 42 * 43 +
469//44 , 45 - 46 . 47 /
470 0 , 0 , 0 , 0 ,
471 0 , 0 , CHAR_PERIOD , 0 ,
472//48 0 49 1 50 2 51 3
473//52 4 53 5 54 6 55 7
474 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
475 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
476//56 8 57 9 58 : 59 ;
477//60 < 61 = 62 > 63 ?
478 CHAR_NUMBER , CHAR_NUMBER , 0 , 0 ,
479 0 , 0 , 0 , 0 ,
480//64 @ 65 A 66 B 67 C
481//68 D 69 E 70 F 71 G
482 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
483 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
484//72 H 73 I 74 J 75 K
485//76 L 77 M 78 N 79 O
486 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
487 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
488//80 P 81 Q 82 R 83 S
489//84 T 85 U 86 V 87 W
490 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
491 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
492//88 X 89 Y 90 Z 91 [
493//92 \ 93 ] 94 ^ 95 _
494 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
495 0 , 0 , 0 , CHAR_UNDER ,
496//96 ` 97 a 98 b 99 c
497//100 d 101 e 102 f 103 g
498 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
499 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
500//104 h 105 i 106 j 107 k
501//108 l 109 m 110 n 111 o
502 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
503 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
504//112 p 113 q 114 r 115 s
505//116 t 117 u 118 v 119 w
506 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
507 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
508//120 x 121 y 122 z 123 {
509//124 | 125 } 126 ~ 127 DEL
510 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
511 0 , 0 , 0 , 0
512};
513
Chris Lattnera2bf1052009-12-17 05:29:40 +0000514static void InitCharacterInfo() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000515 static bool isInited = false;
516 if (isInited) return;
Chris Lattner03b98662009-07-07 17:09:54 +0000517 // check the statically-initialized CharInfo table
518 assert(CHAR_HORZ_WS == CharInfo[(int)' ']);
519 assert(CHAR_HORZ_WS == CharInfo[(int)'\t']);
520 assert(CHAR_HORZ_WS == CharInfo[(int)'\f']);
521 assert(CHAR_HORZ_WS == CharInfo[(int)'\v']);
522 assert(CHAR_VERT_WS == CharInfo[(int)'\n']);
523 assert(CHAR_VERT_WS == CharInfo[(int)'\r']);
524 assert(CHAR_UNDER == CharInfo[(int)'_']);
525 assert(CHAR_PERIOD == CharInfo[(int)'.']);
526 for (unsigned i = 'a'; i <= 'z'; ++i) {
527 assert(CHAR_LETTER == CharInfo[i]);
528 assert(CHAR_LETTER == CharInfo[i+'A'-'a']);
529 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000530 for (unsigned i = '0'; i <= '9'; ++i)
Chris Lattner03b98662009-07-07 17:09:54 +0000531 assert(CHAR_NUMBER == CharInfo[i]);
Steve Naroff7b682652009-12-08 16:38:12 +0000532
Chris Lattner03b98662009-07-07 17:09:54 +0000533 isInited = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000534}
535
Chris Lattner03b98662009-07-07 17:09:54 +0000536
Reid Spencer5f016e22007-07-11 17:01:13 +0000537/// isIdentifierBody - Return true if this is the body character of an
538/// identifier, which is [a-zA-Z0-9_].
539static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000540 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000541}
542
543/// isHorizontalWhitespace - Return true if this character is horizontal
544/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
545static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000546 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000547}
548
549/// isWhitespace - Return true if this character is horizontal or vertical
550/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
551/// for '\0'.
552static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000553 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000554}
555
556/// isNumberBody - Return true if this is the body character of an
557/// preprocessing number, which is [a-zA-Z0-9_.].
558static inline bool isNumberBody(unsigned char c) {
Mike Stump1eb44332009-09-09 15:08:12 +0000559 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000560 true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000561}
562
563
564//===----------------------------------------------------------------------===//
565// Diagnostics forwarding code.
566//===----------------------------------------------------------------------===//
567
Chris Lattner409a0362007-07-22 18:38:25 +0000568/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
569/// lexer buffer was all instantiated at a single point, perform the mapping.
570/// This is currently only used for _Pragma implementation, so it is the slow
571/// path of the hot getSourceLocation method. Do not allow it to be inlined.
Benjamin Kramerc997eb42009-11-14 16:36:57 +0000572static DISABLE_INLINE SourceLocation GetMappedTokenLoc(Preprocessor &PP,
573 SourceLocation FileLoc,
574 unsigned CharNo,
575 unsigned TokLen);
Chris Lattner409a0362007-07-22 18:38:25 +0000576static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
577 SourceLocation FileLoc,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000578 unsigned CharNo, unsigned TokLen) {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000579 assert(FileLoc.isMacroID() && "Must be an instantiation");
Mike Stump1eb44332009-09-09 15:08:12 +0000580
Chris Lattner409a0362007-07-22 18:38:25 +0000581 // Otherwise, we're lexing "mapped tokens". This is used for things like
582 // _Pragma handling. Combine the instantiation location of FileLoc with the
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000583 // spelling location.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000584 SourceManager &SM = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000585
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000586 // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000587 // characters come from spelling(FileLoc)+Offset.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000588 SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000589 SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000590
Chris Lattnere7fb4842009-02-15 20:52:18 +0000591 // Figure out the expansion loc range, which is the range covered by the
592 // original _Pragma(...) sequence.
593 std::pair<SourceLocation,SourceLocation> II =
594 SM.getImmediateInstantiationRange(FileLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000595
Chris Lattnere7fb4842009-02-15 20:52:18 +0000596 return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
Chris Lattner409a0362007-07-22 18:38:25 +0000597}
598
Reid Spencer5f016e22007-07-11 17:01:13 +0000599/// getSourceLocation - Return a source location identifier for the specified
600/// offset in the current file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000601SourceLocation Lexer::getSourceLocation(const char *Loc,
602 unsigned TokLen) const {
Chris Lattner448cec42007-07-22 18:44:36 +0000603 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000604 "Location out of range for this buffer!");
Chris Lattner9dc1f532007-07-20 16:37:10 +0000605
606 // In the normal case, we're just lexing from a simple file buffer, return
607 // the file id from FileLoc with the offset specified.
Chris Lattner448cec42007-07-22 18:44:36 +0000608 unsigned CharNo = Loc-BufferStart;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000609 if (FileLoc.isFileID())
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000610 return FileLoc.getFileLocWithOffset(CharNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Chris Lattner2b2453a2009-01-17 06:22:33 +0000612 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
613 // tokens are lexed from where the _Pragma was defined.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000614 assert(PP && "This doesn't work on raw lexers");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000615 return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
Reid Spencer5f016e22007-07-11 17:01:13 +0000616}
617
Reid Spencer5f016e22007-07-11 17:01:13 +0000618/// Diag - Forwarding function for diagnostics. This translate a source
619/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000620DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner3692b092008-11-18 07:59:24 +0000621 return PP->Diag(getSourceLocation(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000622}
Reid Spencer5f016e22007-07-11 17:01:13 +0000623
624//===----------------------------------------------------------------------===//
625// Trigraph and Escaped Newline Handling Code.
626//===----------------------------------------------------------------------===//
627
628/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
629/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
630static char GetTrigraphCharForLetter(char Letter) {
631 switch (Letter) {
632 default: return 0;
633 case '=': return '#';
634 case ')': return ']';
635 case '(': return '[';
636 case '!': return '|';
637 case '\'': return '^';
638 case '>': return '}';
639 case '/': return '\\';
640 case '<': return '{';
641 case '-': return '~';
642 }
643}
644
645/// DecodeTrigraphChar - If the specified character is a legal trigraph when
646/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
647/// return the result character. Finally, emit a warning about trigraph use
648/// whether trigraphs are enabled or not.
649static char DecodeTrigraphChar(const char *CP, Lexer *L) {
650 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner3692b092008-11-18 07:59:24 +0000651 if (!Res || !L) return Res;
Mike Stump1eb44332009-09-09 15:08:12 +0000652
Chris Lattner3692b092008-11-18 07:59:24 +0000653 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +0000654 if (!L->isLexingRawMode())
655 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner3692b092008-11-18 07:59:24 +0000656 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 }
Mike Stump1eb44332009-09-09 15:08:12 +0000658
Chris Lattner74d15df2008-11-22 02:02:22 +0000659 if (!L->isLexingRawMode())
660 L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000661 return Res;
662}
663
Chris Lattner24f0e482009-04-18 22:05:41 +0000664/// getEscapedNewLineSize - Return the size of the specified escaped newline,
665/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a
Mike Stump1eb44332009-09-09 15:08:12 +0000666/// trigraph equivalent on entry to this function.
Chris Lattner24f0e482009-04-18 22:05:41 +0000667unsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
668 unsigned Size = 0;
669 while (isWhitespace(Ptr[Size])) {
670 ++Size;
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Chris Lattner24f0e482009-04-18 22:05:41 +0000672 if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
673 continue;
674
675 // If this is a \r\n or \n\r, skip the other half.
676 if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
677 Ptr[Size-1] != Ptr[Size])
678 ++Size;
Mike Stump1eb44332009-09-09 15:08:12 +0000679
Chris Lattner24f0e482009-04-18 22:05:41 +0000680 return Size;
Mike Stump1eb44332009-09-09 15:08:12 +0000681 }
682
Chris Lattner24f0e482009-04-18 22:05:41 +0000683 // Not an escaped newline, must be a \t or something else.
684 return 0;
685}
686
Chris Lattner03374952009-04-18 22:27:02 +0000687/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
688/// them), skip over them and return the first non-escaped-newline found,
689/// otherwise return P.
690const char *Lexer::SkipEscapedNewLines(const char *P) {
691 while (1) {
692 const char *AfterEscape;
693 if (*P == '\\') {
694 AfterEscape = P+1;
695 } else if (*P == '?') {
696 // If not a trigraph for escape, bail out.
697 if (P[1] != '?' || P[2] != '/')
698 return P;
699 AfterEscape = P+3;
700 } else {
701 return P;
702 }
Mike Stump1eb44332009-09-09 15:08:12 +0000703
Chris Lattner03374952009-04-18 22:27:02 +0000704 unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
705 if (NewLineSize == 0) return P;
706 P = AfterEscape+NewLineSize;
707 }
708}
709
Chris Lattner24f0e482009-04-18 22:05:41 +0000710
Reid Spencer5f016e22007-07-11 17:01:13 +0000711/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
712/// get its size, and return it. This is tricky in several cases:
713/// 1. If currently at the start of a trigraph, we warn about the trigraph,
714/// then either return the trigraph (skipping 3 chars) or the '?',
715/// depending on whether trigraphs are enabled or not.
716/// 2. If this is an escaped newline (potentially with whitespace between
717/// the backslash and newline), implicitly skip the newline and return
718/// the char after it.
719/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
720///
721/// This handles the slow/uncommon case of the getCharAndSize method. Here we
722/// know that we can accumulate into Size, and that we have already incremented
723/// Ptr by Size bytes.
724///
725/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
726/// be updated to match.
727///
728char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattnerd2177732007-07-20 16:59:19 +0000729 Token *Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000730 // If we have a slash, look for an escaped newline.
731 if (Ptr[0] == '\\') {
732 ++Size;
733 ++Ptr;
734Slash:
735 // Common case, backslash-char where the char is not whitespace.
736 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Chris Lattner5636a3b2009-06-23 05:15:06 +0000738 // See if we have optional whitespace characters between the slash and
739 // newline.
Chris Lattner24f0e482009-04-18 22:05:41 +0000740 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
741 // Remember that this token needs to be cleaned.
742 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000743
Chris Lattner24f0e482009-04-18 22:05:41 +0000744 // Warn if there was whitespace between the backslash and newline.
Chris Lattner5636a3b2009-06-23 05:15:06 +0000745 if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
Chris Lattner24f0e482009-04-18 22:05:41 +0000746 Diag(Ptr, diag::backslash_newline_space);
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Chris Lattner24f0e482009-04-18 22:05:41 +0000748 // Found backslash<whitespace><newline>. Parse the char after it.
749 Size += EscapedNewLineSize;
750 Ptr += EscapedNewLineSize;
751 // Use slow version to accumulate a correct size field.
752 return getCharAndSizeSlow(Ptr, Size, Tok);
753 }
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 // Otherwise, this is not an escaped newline, just return the slash.
756 return '\\';
757 }
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Reid Spencer5f016e22007-07-11 17:01:13 +0000759 // If this is a trigraph, process it.
760 if (Ptr[0] == '?' && Ptr[1] == '?') {
761 // If this is actually a legal trigraph (not something like "??x"), emit
762 // a trigraph warning. If so, and if trigraphs are enabled, return it.
763 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
764 // Remember that this token needs to be cleaned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000765 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000766
767 Ptr += 3;
768 Size += 3;
769 if (C == '\\') goto Slash;
770 return C;
771 }
772 }
Mike Stump1eb44332009-09-09 15:08:12 +0000773
Reid Spencer5f016e22007-07-11 17:01:13 +0000774 // If this is neither, return a single character.
775 ++Size;
776 return *Ptr;
777}
778
779
780/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
781/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
782/// and that we have already incremented Ptr by Size bytes.
783///
784/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
785/// be updated to match.
786char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
787 const LangOptions &Features) {
788 // If we have a slash, look for an escaped newline.
789 if (Ptr[0] == '\\') {
790 ++Size;
791 ++Ptr;
792Slash:
793 // Common case, backslash-char where the char is not whitespace.
794 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Reid Spencer5f016e22007-07-11 17:01:13 +0000796 // See if we have optional whitespace characters followed by a newline.
Chris Lattner24f0e482009-04-18 22:05:41 +0000797 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
798 // Found backslash<whitespace><newline>. Parse the char after it.
799 Size += EscapedNewLineSize;
800 Ptr += EscapedNewLineSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000801
Chris Lattner24f0e482009-04-18 22:05:41 +0000802 // Use slow version to accumulate a correct size field.
803 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
804 }
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Reid Spencer5f016e22007-07-11 17:01:13 +0000806 // Otherwise, this is not an escaped newline, just return the slash.
807 return '\\';
808 }
Mike Stump1eb44332009-09-09 15:08:12 +0000809
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 // If this is a trigraph, process it.
811 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
812 // If this is actually a legal trigraph (not something like "??x"), return
813 // it.
814 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
815 Ptr += 3;
816 Size += 3;
817 if (C == '\\') goto Slash;
818 return C;
819 }
820 }
Mike Stump1eb44332009-09-09 15:08:12 +0000821
Reid Spencer5f016e22007-07-11 17:01:13 +0000822 // If this is neither, return a single character.
823 ++Size;
824 return *Ptr;
825}
826
827//===----------------------------------------------------------------------===//
828// Helper methods for lexing.
829//===----------------------------------------------------------------------===//
830
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000831/// \brief Routine that indiscriminately skips bytes in the source file.
832void Lexer::SkipBytes(unsigned Bytes, bool StartOfLine) {
833 BufferPtr += Bytes;
834 if (BufferPtr > BufferEnd)
835 BufferPtr = BufferEnd;
836 IsAtStartOfLine = StartOfLine;
837}
838
Chris Lattnerd2177732007-07-20 16:59:19 +0000839void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000840 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
841 unsigned Size;
842 unsigned char C = *CurPtr++;
Chris Lattnercd991db2010-01-11 02:38:50 +0000843 while (isIdentifierBody(C))
Reid Spencer5f016e22007-07-11 17:01:13 +0000844 C = *CurPtr++;
Chris Lattnercd991db2010-01-11 02:38:50 +0000845
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 --CurPtr; // Back up over the skipped character.
847
848 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
849 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
850 // FIXME: UCNs.
Chris Lattnercd991db2010-01-11 02:38:50 +0000851 //
852 // TODO: Could merge these checks into a CharInfo flag to make the comparison
853 // cheaper
Reid Spencer5f016e22007-07-11 17:01:13 +0000854 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
855FinishIdentifier:
856 const char *IdStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000857 FormTokenWithChars(Result, CurPtr, tok::identifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Reid Spencer5f016e22007-07-11 17:01:13 +0000859 // If we are in raw mode, return this identifier raw. There is no need to
860 // look up identifier information or attempt to macro expand it.
861 if (LexingRawMode) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Reid Spencer5f016e22007-07-11 17:01:13 +0000863 // Fill in Result.IdentifierInfo, looking up the identifier in the
864 // identifier table.
Chris Lattnerd1186fa2009-01-21 07:45:14 +0000865 IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000866
Chris Lattner863c4862009-01-23 18:35:48 +0000867 // Change the kind of this identifier to the appropriate token kind, e.g.
868 // turning "for" into a keyword.
869 Result.setKind(II->getTokenID());
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Reid Spencer5f016e22007-07-11 17:01:13 +0000871 // Finally, now that we know we have an identifier, pass this off to the
872 // preprocessor, which may macro expand it or something.
Chris Lattnerd1186fa2009-01-21 07:45:14 +0000873 if (II->isHandleIdentifierCase())
Chris Lattner6a170eb2009-01-21 07:43:11 +0000874 PP->HandleIdentifier(Result);
875 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 // Otherwise, $,\,? in identifier found. Enter slower path.
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 C = getCharAndSize(CurPtr, Size);
881 while (1) {
882 if (C == '$') {
883 // If we hit a $ and they are not supported in identifiers, we are done.
884 if (!Features.DollarIdents) goto FinishIdentifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000885
Reid Spencer5f016e22007-07-11 17:01:13 +0000886 // Otherwise, emit a diagnostic and continue.
Chris Lattner74d15df2008-11-22 02:02:22 +0000887 if (!isLexingRawMode())
888 Diag(CurPtr, diag::ext_dollar_in_identifier);
Reid Spencer5f016e22007-07-11 17:01:13 +0000889 CurPtr = ConsumeChar(CurPtr, Size, Result);
890 C = getCharAndSize(CurPtr, Size);
891 continue;
892 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
893 // Found end of identifier.
894 goto FinishIdentifier;
895 }
896
897 // Otherwise, this character is good, consume it.
898 CurPtr = ConsumeChar(CurPtr, Size, Result);
899
900 C = getCharAndSize(CurPtr, Size);
901 while (isIdentifierBody(C)) { // FIXME: UCNs.
902 CurPtr = ConsumeChar(CurPtr, Size, Result);
903 C = getCharAndSize(CurPtr, Size);
904 }
905 }
906}
907
908
Nate Begeman5253c7f2008-04-14 02:26:39 +0000909/// LexNumericConstant - Lex the remainder of a integer or floating point
Reid Spencer5f016e22007-07-11 17:01:13 +0000910/// constant. From[-1] is the first character lexed. Return the end of the
911/// constant.
Chris Lattnerd2177732007-07-20 16:59:19 +0000912void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 unsigned Size;
914 char C = getCharAndSize(CurPtr, Size);
915 char PrevCh = 0;
916 while (isNumberBody(C)) { // FIXME: UCNs?
917 CurPtr = ConsumeChar(CurPtr, Size, Result);
918 PrevCh = C;
919 C = getCharAndSize(CurPtr, Size);
920 }
Mike Stump1eb44332009-09-09 15:08:12 +0000921
Reid Spencer5f016e22007-07-11 17:01:13 +0000922 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
923 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
924 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
925
926 // If we have a hex FP constant, continue.
Sean Hunt8c723402010-01-10 23:37:56 +0000927 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
928 (!PP || !PP->getLangOptions().CPlusPlus0x))
Reid Spencer5f016e22007-07-11 17:01:13 +0000929 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
Mike Stump1eb44332009-09-09 15:08:12 +0000930
Reid Spencer5f016e22007-07-11 17:01:13 +0000931 // Update the location of token as well as BufferPtr.
Chris Lattner47246be2009-01-26 19:29:26 +0000932 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000933 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Chris Lattner47246be2009-01-26 19:29:26 +0000934 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000935}
936
937/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
938/// either " or L".
Chris Lattnerd88dc482008-10-12 04:05:48 +0000939void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 const char *NulCharacter = 0; // Does this string contain the \0 character?
Mike Stump1eb44332009-09-09 15:08:12 +0000941
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 char C = getAndAdvanceChar(CurPtr, Result);
943 while (C != '"') {
Chris Lattner571339c2010-05-30 23:27:38 +0000944 // Skip escaped characters. Escaped newlines will already be processed by
945 // getAndAdvanceChar.
946 if (C == '\\')
Reid Spencer5f016e22007-07-11 17:01:13 +0000947 C = getAndAdvanceChar(CurPtr, Result);
Douglas Gregor33611e02010-05-30 22:59:50 +0000948
Chris Lattner571339c2010-05-30 23:27:38 +0000949 if (C == '\n' || C == '\r' || // Newline.
Douglas Gregor33611e02010-05-30 22:59:50 +0000950 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner33ab3f62009-03-18 21:10:12 +0000951 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner74d15df2008-11-22 02:02:22 +0000952 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000953 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000954 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 }
Chris Lattner571339c2010-05-30 23:27:38 +0000956
957 if (C == 0)
958 NulCharacter = CurPtr-1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000959 C = getAndAdvanceChar(CurPtr, Result);
960 }
Mike Stump1eb44332009-09-09 15:08:12 +0000961
Reid Spencer5f016e22007-07-11 17:01:13 +0000962 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000963 if (NulCharacter && !isLexingRawMode())
964 Diag(NulCharacter, diag::null_in_string);
Reid Spencer5f016e22007-07-11 17:01:13 +0000965
Reid Spencer5f016e22007-07-11 17:01:13 +0000966 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner47246be2009-01-26 19:29:26 +0000967 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000968 FormTokenWithChars(Result, CurPtr,
969 Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +0000970 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000971}
972
973/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
974/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattnerd2177732007-07-20 16:59:19 +0000975void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000976 const char *NulCharacter = 0; // Does this string contain the \0 character?
Chris Lattner9cb51ce2009-04-17 23:56:52 +0000977 const char *AfterLessPos = CurPtr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 char C = getAndAdvanceChar(CurPtr, Result);
979 while (C != '>') {
980 // Skip escaped characters.
981 if (C == '\\') {
982 // Skip the escaped character.
983 C = getAndAdvanceChar(CurPtr, Result);
984 } else if (C == '\n' || C == '\r' || // Newline.
985 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner9cb51ce2009-04-17 23:56:52 +0000986 // If the filename is unterminated, then it must just be a lone <
987 // character. Return this as such.
988 FormTokenWithChars(Result, AfterLessPos, tok::less);
Reid Spencer5f016e22007-07-11 17:01:13 +0000989 return;
990 } else if (C == 0) {
991 NulCharacter = CurPtr-1;
992 }
993 C = getAndAdvanceChar(CurPtr, Result);
994 }
Mike Stump1eb44332009-09-09 15:08:12 +0000995
Reid Spencer5f016e22007-07-11 17:01:13 +0000996 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000997 if (NulCharacter && !isLexingRawMode())
998 Diag(NulCharacter, diag::null_in_string);
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 // Update the location of token as well as BufferPtr.
Chris Lattner47246be2009-01-26 19:29:26 +00001001 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +00001002 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +00001003 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +00001004}
1005
1006
1007/// LexCharConstant - Lex the remainder of a character constant, after having
1008/// lexed either ' or L'.
Chris Lattnerd2177732007-07-20 16:59:19 +00001009void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001010 const char *NulCharacter = 0; // Does this character contain the \0 character?
1011
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 char C = getAndAdvanceChar(CurPtr, Result);
1013 if (C == '\'') {
Chris Lattner33ab3f62009-03-18 21:10:12 +00001014 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner74d15df2008-11-22 02:02:22 +00001015 Diag(BufferPtr, diag::err_empty_character);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001016 FormTokenWithChars(Result, CurPtr, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +00001017 return;
Chris Lattnerd80f7862010-07-07 23:24:27 +00001018 }
1019
1020 while (C != '\'') {
1021 // Skip escaped characters.
1022 if (C == '\\') {
1023 // Skip the escaped character.
1024 // FIXME: UCN's
1025 C = getAndAdvanceChar(CurPtr, Result);
1026 } else if (C == '\n' || C == '\r' || // Newline.
1027 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
1028 if (!isLexingRawMode() && !Features.AsmPreprocessor)
1029 Diag(BufferPtr, diag::err_unterminated_char);
1030 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
1031 return;
1032 } else if (C == 0) {
1033 NulCharacter = CurPtr-1;
1034 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 C = getAndAdvanceChar(CurPtr, Result);
1036 }
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Chris Lattnerd80f7862010-07-07 23:24:27 +00001038 // If a nul character existed in the character, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +00001039 if (NulCharacter && !isLexingRawMode())
1040 Diag(NulCharacter, diag::null_in_char);
Reid Spencer5f016e22007-07-11 17:01:13 +00001041
Reid Spencer5f016e22007-07-11 17:01:13 +00001042 // Update the location of token as well as BufferPtr.
Chris Lattner47246be2009-01-26 19:29:26 +00001043 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +00001044 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Chris Lattner47246be2009-01-26 19:29:26 +00001045 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +00001046}
1047
1048/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
1049/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattnerd88dc482008-10-12 04:05:48 +00001050///
1051/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
1052///
1053bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 // Whitespace - Skip it, then return the token after the whitespace.
1055 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
1056 while (1) {
1057 // Skip horizontal whitespace very aggressively.
1058 while (isHorizontalWhitespace(Char))
1059 Char = *++CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Daniel Dunbarddd3e8b2008-11-25 00:20:22 +00001061 // Otherwise if we have something other than whitespace, we're done.
Reid Spencer5f016e22007-07-11 17:01:13 +00001062 if (Char != '\n' && Char != '\r')
1063 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Reid Spencer5f016e22007-07-11 17:01:13 +00001065 if (ParsingPreprocessorDirective) {
1066 // End of preprocessor directive line, let LexTokenInternal handle this.
1067 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +00001068 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001069 }
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 // ok, but handle newline.
1072 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +00001073 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001074 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +00001075 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001076 Char = *++CurPtr;
1077 }
1078
1079 // If this isn't immediately after a newline, there is leading space.
1080 char PrevChar = CurPtr[-1];
1081 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattnerd2177732007-07-20 16:59:19 +00001082 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001083
Chris Lattnerd88dc482008-10-12 04:05:48 +00001084 // If the client wants us to return whitespace, return it now.
1085 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001086 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001087 return true;
1088 }
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Reid Spencer5f016e22007-07-11 17:01:13 +00001090 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +00001091 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001092}
1093
1094// SkipBCPLComment - We have just read the // characters from input. Skip until
1095// we find the newline character thats terminate the comment. Then update
Chris Lattner046c2272010-01-18 22:35:47 +00001096/// BufferPtr and return.
1097///
1098/// If we're in KeepCommentMode or any CommentHandler has inserted
1099/// some tokens, this will store the first token and return true.
Chris Lattnerd2177732007-07-20 16:59:19 +00001100bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001101 // If BCPL comments aren't explicitly enabled for this language, emit an
1102 // extension warning.
Chris Lattner74d15df2008-11-22 02:02:22 +00001103 if (!Features.BCPLComment && !isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 Diag(BufferPtr, diag::ext_bcpl_comment);
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 // Mark them enabled so we only emit one warning for this translation
1107 // unit.
1108 Features.BCPLComment = true;
1109 }
Mike Stump1eb44332009-09-09 15:08:12 +00001110
Reid Spencer5f016e22007-07-11 17:01:13 +00001111 // Scan over the body of the comment. The common case, when scanning, is that
1112 // the comment contains normal ascii characters with nothing interesting in
1113 // them. As such, optimize for this case with the inner loop.
1114 char C;
1115 do {
1116 C = *CurPtr;
1117 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
1118 // If we find a \n character, scan backwards, checking to see if it's an
1119 // escaped newline, like we do for block comments.
Mike Stump1eb44332009-09-09 15:08:12 +00001120
Reid Spencer5f016e22007-07-11 17:01:13 +00001121 // Skip over characters in the fast loop.
1122 while (C != 0 && // Potentially EOF.
1123 C != '\\' && // Potentially escaped newline.
1124 C != '?' && // Potentially trigraph.
1125 C != '\n' && C != '\r') // Newline or DOS-style newline.
1126 C = *++CurPtr;
1127
1128 // If this is a newline, we're done.
1129 if (C == '\n' || C == '\r')
1130 break; // Found the newline? Break out!
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Reid Spencer5f016e22007-07-11 17:01:13 +00001132 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnerbc3e9842008-12-12 07:34:39 +00001133 // properly decode the character. Read it in raw mode to avoid emitting
1134 // diagnostics about things like trigraphs. If we see an escaped newline,
1135 // we'll handle it below.
Reid Spencer5f016e22007-07-11 17:01:13 +00001136 const char *OldPtr = CurPtr;
Chris Lattnerbc3e9842008-12-12 07:34:39 +00001137 bool OldRawMode = isLexingRawMode();
1138 LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001139 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerbc3e9842008-12-12 07:34:39 +00001140 LexingRawMode = OldRawMode;
Chris Lattneread616c2009-04-05 00:26:41 +00001141
1142 // If the char that we finally got was a \n, then we must have had something
1143 // like \<newline><newline>. We don't want to have consumed the second
1144 // newline, we want CurPtr, to end up pointing to it down below.
1145 if (C == '\n' || C == '\r') {
1146 --CurPtr;
1147 C = 'x'; // doesn't matter what this is.
1148 }
Mike Stump1eb44332009-09-09 15:08:12 +00001149
Reid Spencer5f016e22007-07-11 17:01:13 +00001150 // If we read multiple characters, and one of those characters was a \r or
1151 // \n, then we had an escaped newline within the comment. Emit diagnostic
1152 // unless the next line is also a // comment.
1153 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
1154 for (; OldPtr != CurPtr; ++OldPtr)
1155 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
1156 // Okay, we found a // comment that ends in a newline, if the next
1157 // line is also a // comment, but has spaces, don't emit a diagnostic.
1158 if (isspace(C)) {
1159 const char *ForwardPtr = CurPtr;
1160 while (isspace(*ForwardPtr)) // Skip whitespace.
1161 ++ForwardPtr;
1162 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
1163 break;
1164 }
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Chris Lattner74d15df2008-11-22 02:02:22 +00001166 if (!isLexingRawMode())
1167 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001168 break;
1169 }
1170 }
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Reid Spencer5f016e22007-07-11 17:01:13 +00001172 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
1173 } while (C != '\n' && C != '\r');
1174
Chris Lattner3d0ad582010-02-03 21:06:21 +00001175 // Found but did not consume the newline. Notify comment handlers about the
1176 // comment unless we're in a #if 0 block.
1177 if (PP && !isLexingRawMode() &&
1178 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
1179 getSourceLocation(CurPtr)))) {
Chris Lattner046c2272010-01-18 22:35:47 +00001180 BufferPtr = CurPtr;
1181 return true; // A token has to be returned.
1182 }
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +00001185 if (inKeepCommentMode())
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 return SaveBCPLComment(Result, CurPtr);
1187
1188 // If we are inside a preprocessor directive and we see the end of line,
1189 // return immediately, so that the lexer can return this as an EOM token.
1190 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
1191 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001192 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 }
Mike Stump1eb44332009-09-09 15:08:12 +00001194
Reid Spencer5f016e22007-07-11 17:01:13 +00001195 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner7a4f0042008-10-12 00:23:07 +00001196 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattnerd88dc482008-10-12 04:05:48 +00001197 // contribute to another token), it isn't needed for correctness. Note that
1198 // this is ok even in KeepWhitespaceMode, because we would have returned the
1199 /// comment above in that mode.
Reid Spencer5f016e22007-07-11 17:01:13 +00001200 ++CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 // The next returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +00001203 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001204 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +00001205 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001207 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001208}
1209
1210/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
1211/// an appropriate way and return it.
Chris Lattnerd2177732007-07-20 16:59:19 +00001212bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001213 // If we're not in a preprocessor directive, just return the // comment
1214 // directly.
1215 FormTokenWithChars(Result, CurPtr, tok::comment);
Mike Stump1eb44332009-09-09 15:08:12 +00001216
Chris Lattner9e6293d2008-10-12 04:51:35 +00001217 if (!ParsingPreprocessorDirective)
1218 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001219
Chris Lattner9e6293d2008-10-12 04:51:35 +00001220 // If this BCPL-style comment is in a macro definition, transmogrify it into
1221 // a C-style block comment.
Douglas Gregor453091c2010-03-16 22:30:13 +00001222 bool Invalid = false;
1223 std::string Spelling = PP->getSpelling(Result, &Invalid);
1224 if (Invalid)
1225 return true;
1226
Chris Lattner9e6293d2008-10-12 04:51:35 +00001227 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
1228 Spelling[1] = '*'; // Change prefix to "/*".
1229 Spelling += "*/"; // add suffix.
Mike Stump1eb44332009-09-09 15:08:12 +00001230
Chris Lattner9e6293d2008-10-12 04:51:35 +00001231 Result.setKind(tok::comment);
Chris Lattner47246be2009-01-26 19:29:26 +00001232 PP->CreateString(&Spelling[0], Spelling.size(), Result,
1233 Result.getLocation());
Chris Lattner2d381892008-10-12 04:15:42 +00001234 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001235}
1236
1237/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
1238/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner47a2b402008-12-12 07:14:34 +00001239/// diagnostic if so. We know that the newline is inside of a block comment.
Mike Stump1eb44332009-09-09 15:08:12 +00001240static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
Reid Spencer5f016e22007-07-11 17:01:13 +00001241 Lexer *L) {
1242 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 // Back up off the newline.
1245 --CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001246
Reid Spencer5f016e22007-07-11 17:01:13 +00001247 // If this is a two-character newline sequence, skip the other character.
1248 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
1249 // \n\n or \r\r -> not escaped newline.
1250 if (CurPtr[0] == CurPtr[1])
1251 return false;
1252 // \n\r or \r\n -> skip the newline.
1253 --CurPtr;
1254 }
Mike Stump1eb44332009-09-09 15:08:12 +00001255
Reid Spencer5f016e22007-07-11 17:01:13 +00001256 // If we have horizontal whitespace, skip over it. We allow whitespace
1257 // between the slash and newline.
1258 bool HasSpace = false;
1259 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
1260 --CurPtr;
1261 HasSpace = true;
1262 }
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Reid Spencer5f016e22007-07-11 17:01:13 +00001264 // If we have a slash, we know this is an escaped newline.
1265 if (*CurPtr == '\\') {
1266 if (CurPtr[-1] != '*') return false;
1267 } else {
1268 // It isn't a slash, is it the ?? / trigraph?
1269 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
1270 CurPtr[-3] != '*')
1271 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 // This is the trigraph ending the comment. Emit a stern warning!
1274 CurPtr -= 2;
1275
1276 // If no trigraphs are enabled, warn that we ignored this trigraph and
1277 // ignore this * character.
1278 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001279 if (!L->isLexingRawMode())
1280 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001281 return false;
1282 }
Chris Lattner74d15df2008-11-22 02:02:22 +00001283 if (!L->isLexingRawMode())
1284 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001285 }
Mike Stump1eb44332009-09-09 15:08:12 +00001286
Reid Spencer5f016e22007-07-11 17:01:13 +00001287 // Warn about having an escaped newline between the */ characters.
Chris Lattner74d15df2008-11-22 02:02:22 +00001288 if (!L->isLexingRawMode())
1289 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Mike Stump1eb44332009-09-09 15:08:12 +00001290
Reid Spencer5f016e22007-07-11 17:01:13 +00001291 // If there was space between the backslash and newline, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +00001292 if (HasSpace && !L->isLexingRawMode())
1293 L->Diag(CurPtr, diag::backslash_newline_space);
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Reid Spencer5f016e22007-07-11 17:01:13 +00001295 return true;
1296}
1297
1298#ifdef __SSE2__
1299#include <emmintrin.h>
1300#elif __ALTIVEC__
1301#include <altivec.h>
1302#undef bool
1303#endif
1304
1305/// SkipBlockComment - We have just read the /* characters from input. Read
1306/// until we find the */ characters that terminate the comment. Note that we
1307/// don't bother decoding trigraphs or escaped newlines in block comments,
1308/// because they cannot cause the comment to end. The only thing that can
1309/// happen is the comment could end with an escaped newline between the */ end
1310/// of comment.
Chris Lattner2d381892008-10-12 04:15:42 +00001311///
Chris Lattner046c2272010-01-18 22:35:47 +00001312/// If we're in KeepCommentMode or any CommentHandler has inserted
1313/// some tokens, this will store the first token and return true.
Chris Lattnerd2177732007-07-20 16:59:19 +00001314bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001315 // Scan one character past where we should, looking for a '/' character. Once
1316 // we find it, check to see if it was preceeded by a *. This common
1317 // optimization helps people who like to put a lot of * characters in their
1318 // comments.
Chris Lattner8146b682007-07-21 23:43:37 +00001319
1320 // The first character we get with newlines and trigraphs skipped to handle
1321 // the degenerate /*/ case below correctly if the * has an escaped newline
1322 // after it.
1323 unsigned CharSize;
1324 unsigned char C = getCharAndSize(CurPtr, CharSize);
1325 CurPtr += CharSize;
Reid Spencer5f016e22007-07-11 17:01:13 +00001326 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner150fcd52010-05-16 19:54:05 +00001327 if (!isLexingRawMode() &&
1328 !PP->isCodeCompletionFile(FileLoc))
Chris Lattner0af57422008-10-12 01:31:51 +00001329 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001330 --CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001331
Chris Lattner31f0eca2008-10-12 04:19:49 +00001332 // KeepWhitespaceMode should return this broken comment as a token. Since
1333 // it isn't a well formed comment, just return it as an 'unknown' token.
1334 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001335 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001336 return true;
1337 }
Mike Stump1eb44332009-09-09 15:08:12 +00001338
Chris Lattner31f0eca2008-10-12 04:19:49 +00001339 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001340 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 }
Mike Stump1eb44332009-09-09 15:08:12 +00001342
Chris Lattner8146b682007-07-21 23:43:37 +00001343 // Check to see if the first character after the '/*' is another /. If so,
1344 // then this slash does not end the block comment, it is part of it.
1345 if (C == '/')
1346 C = *CurPtr++;
Mike Stump1eb44332009-09-09 15:08:12 +00001347
Reid Spencer5f016e22007-07-11 17:01:13 +00001348 while (1) {
1349 // Skip over all non-interesting characters until we find end of buffer or a
1350 // (probably ending) '/' character.
1351 if (CurPtr + 24 < BufferEnd) {
1352 // While not aligned to a 16-byte boundary.
1353 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1354 C = *CurPtr++;
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Reid Spencer5f016e22007-07-11 17:01:13 +00001356 if (C == '/') goto FoundSlash;
1357
1358#ifdef __SSE2__
1359 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1360 '/', '/', '/', '/', '/', '/', '/', '/');
1361 while (CurPtr+16 <= BufferEnd &&
1362 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1363 CurPtr += 16;
1364#elif __ALTIVEC__
1365 __vector unsigned char Slashes = {
Mike Stump1eb44332009-09-09 15:08:12 +00001366 '/', '/', '/', '/', '/', '/', '/', '/',
Reid Spencer5f016e22007-07-11 17:01:13 +00001367 '/', '/', '/', '/', '/', '/', '/', '/'
1368 };
1369 while (CurPtr+16 <= BufferEnd &&
1370 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1371 CurPtr += 16;
Mike Stump1eb44332009-09-09 15:08:12 +00001372#else
Reid Spencer5f016e22007-07-11 17:01:13 +00001373 // Scan for '/' quickly. Many block comments are very large.
1374 while (CurPtr[0] != '/' &&
1375 CurPtr[1] != '/' &&
1376 CurPtr[2] != '/' &&
1377 CurPtr[3] != '/' &&
1378 CurPtr+4 < BufferEnd) {
1379 CurPtr += 4;
1380 }
1381#endif
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Reid Spencer5f016e22007-07-11 17:01:13 +00001383 // It has to be one of the bytes scanned, increment to it and read one.
1384 C = *CurPtr++;
1385 }
Mike Stump1eb44332009-09-09 15:08:12 +00001386
Reid Spencer5f016e22007-07-11 17:01:13 +00001387 // Loop to scan the remainder.
1388 while (C != '/' && C != '\0')
1389 C = *CurPtr++;
Mike Stump1eb44332009-09-09 15:08:12 +00001390
Reid Spencer5f016e22007-07-11 17:01:13 +00001391 FoundSlash:
1392 if (C == '/') {
1393 if (CurPtr[-2] == '*') // We found the final */. We're done!
1394 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Reid Spencer5f016e22007-07-11 17:01:13 +00001396 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
1397 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
1398 // We found the final */, though it had an escaped newline between the
1399 // * and /. We're done!
1400 break;
1401 }
1402 }
1403 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1404 // If this is a /* inside of the comment, emit a warning. Don't do this
1405 // if this is a /*/, which will end the comment. This misses cases with
1406 // embedded escaped newlines, but oh well.
Chris Lattner74d15df2008-11-22 02:02:22 +00001407 if (!isLexingRawMode())
1408 Diag(CurPtr-1, diag::warn_nested_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001409 }
1410 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner150fcd52010-05-16 19:54:05 +00001411 if (!isLexingRawMode() && !PP->isCodeCompletionFile(FileLoc))
Chris Lattner74d15df2008-11-22 02:02:22 +00001412 Diag(BufferPtr, diag::err_unterminated_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001413 // Note: the user probably forgot a */. We could continue immediately
1414 // after the /*, but this would involve lexing a lot of what really is the
1415 // comment, which surely would confuse the parser.
Chris Lattner31f0eca2008-10-12 04:19:49 +00001416 --CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Chris Lattner31f0eca2008-10-12 04:19:49 +00001418 // KeepWhitespaceMode should return this broken comment as a token. Since
1419 // it isn't a well formed comment, just return it as an 'unknown' token.
1420 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001421 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001422 return true;
1423 }
Mike Stump1eb44332009-09-09 15:08:12 +00001424
Chris Lattner31f0eca2008-10-12 04:19:49 +00001425 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001426 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001427 }
1428 C = *CurPtr++;
1429 }
Mike Stump1eb44332009-09-09 15:08:12 +00001430
Chris Lattner3d0ad582010-02-03 21:06:21 +00001431 // Notify comment handlers about the comment unless we're in a #if 0 block.
1432 if (PP && !isLexingRawMode() &&
1433 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
1434 getSourceLocation(CurPtr)))) {
Chris Lattner046c2272010-01-18 22:35:47 +00001435 BufferPtr = CurPtr;
1436 return true; // A token has to be returned.
1437 }
Douglas Gregor2e222532009-07-02 17:08:52 +00001438
Reid Spencer5f016e22007-07-11 17:01:13 +00001439 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +00001440 if (inKeepCommentMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001441 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattner2d381892008-10-12 04:15:42 +00001442 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001443 }
1444
1445 // It is common for the tokens immediately after a /**/ comment to be
1446 // whitespace. Instead of going through the big switch, handle it
Chris Lattnerd88dc482008-10-12 04:05:48 +00001447 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1448 // have already returned above with the comment as a token.
Reid Spencer5f016e22007-07-11 17:01:13 +00001449 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001450 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001451 SkipWhitespace(Result, CurPtr+1);
Chris Lattner2d381892008-10-12 04:15:42 +00001452 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001453 }
1454
1455 // Otherwise, just return so that the next character will be lexed as a token.
1456 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001457 Result.setFlag(Token::LeadingSpace);
Chris Lattner2d381892008-10-12 04:15:42 +00001458 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001459}
1460
1461//===----------------------------------------------------------------------===//
1462// Primary Lexing Entry Points
1463//===----------------------------------------------------------------------===//
1464
Reid Spencer5f016e22007-07-11 17:01:13 +00001465/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1466/// uninterpreted string. This switches the lexer out of directive mode.
1467std::string Lexer::ReadToEndOfLine() {
1468 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1469 "Must be in a preprocessing directive!");
1470 std::string Result;
Chris Lattnerd2177732007-07-20 16:59:19 +00001471 Token Tmp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001472
1473 // CurPtr - Cache BufferPtr in an automatic variable.
1474 const char *CurPtr = BufferPtr;
1475 while (1) {
1476 char Char = getAndAdvanceChar(CurPtr, Tmp);
1477 switch (Char) {
1478 default:
1479 Result += Char;
1480 break;
1481 case 0: // Null.
1482 // Found end of file?
1483 if (CurPtr-1 != BufferEnd) {
1484 // Nope, normal character, continue.
1485 Result += Char;
1486 break;
1487 }
1488 // FALL THROUGH.
1489 case '\r':
1490 case '\n':
1491 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1492 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1493 BufferPtr = CurPtr-1;
Mike Stump1eb44332009-09-09 15:08:12 +00001494
Reid Spencer5f016e22007-07-11 17:01:13 +00001495 // Next, lex the character, which should handle the EOM transition.
1496 Lex(Tmp);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001497 assert(Tmp.is(tok::eom) && "Unexpected token!");
Mike Stump1eb44332009-09-09 15:08:12 +00001498
Reid Spencer5f016e22007-07-11 17:01:13 +00001499 // Finally, we're done, return the string we found.
1500 return Result;
1501 }
1502 }
1503}
1504
1505/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1506/// condition, reporting diagnostics and handling other edge cases as required.
1507/// This returns true if Result contains a token, false if PP.Lex should be
1508/// called again.
Chris Lattnerd2177732007-07-20 16:59:19 +00001509bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001510 // If we hit the end of the file while parsing a preprocessor directive,
1511 // end the preprocessor directive first. The next token returned will
1512 // then be the end of file.
1513 if (ParsingPreprocessorDirective) {
1514 // Done parsing the "line".
1515 ParsingPreprocessorDirective = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001516 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00001517 FormTokenWithChars(Result, CurPtr, tok::eom);
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Reid Spencer5f016e22007-07-11 17:01:13 +00001519 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001520 SetCommentRetentionState(PP->getCommentRetentionState());
Reid Spencer5f016e22007-07-11 17:01:13 +00001521 return true; // Have a token.
Mike Stump1eb44332009-09-09 15:08:12 +00001522 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00001523
Reid Spencer5f016e22007-07-11 17:01:13 +00001524 // If we are in raw mode, return this event as an EOF token. Let the caller
1525 // that put us in raw mode handle the event.
Chris Lattner74d15df2008-11-22 02:02:22 +00001526 if (isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001527 Result.startToken();
1528 BufferPtr = BufferEnd;
Chris Lattner9e6293d2008-10-12 04:51:35 +00001529 FormTokenWithChars(Result, BufferEnd, tok::eof);
Reid Spencer5f016e22007-07-11 17:01:13 +00001530 return true;
1531 }
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Douglas Gregor86d9a522009-09-21 16:56:56 +00001533 // Otherwise, check if we are code-completing, then issue diagnostics for
1534 // unterminated #if and missing newline.
Reid Spencer5f016e22007-07-11 17:01:13 +00001535
Douglas Gregor29684422009-12-02 06:49:09 +00001536 if (PP && PP->isCodeCompletionFile(FileLoc)) {
1537 // We're at the end of the file, but we've been asked to consider the
1538 // end of the file to be a code-completion token. Return the
1539 // code-completion token.
1540 Result.startToken();
1541 FormTokenWithChars(Result, CurPtr, tok::code_completion);
Douglas Gregor86d9a522009-09-21 16:56:56 +00001542
Douglas Gregor29684422009-12-02 06:49:09 +00001543 // Only do the eof -> code_completion translation once.
1544 PP->SetCodeCompletionPoint(0, 0, 0);
Douglas Gregordc845342010-05-25 05:58:43 +00001545
1546 // Silence any diagnostics that occur once we hit the code-completion point.
1547 PP->getDiagnostics().setSuppressAllDiagnostics(true);
Douglas Gregor29684422009-12-02 06:49:09 +00001548 return true;
Douglas Gregor86d9a522009-09-21 16:56:56 +00001549 }
1550
Reid Spencer5f016e22007-07-11 17:01:13 +00001551 // If we are in a #if directive, emit an error.
1552 while (!ConditionalStack.empty()) {
Chris Lattner30c64762008-11-22 06:22:39 +00001553 PP->Diag(ConditionalStack.back().IfLoc,
1554 diag::err_pp_unterminated_conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +00001555 ConditionalStack.pop_back();
1556 }
Mike Stump1eb44332009-09-09 15:08:12 +00001557
Chris Lattnerb25e5d72008-04-12 05:54:25 +00001558 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1559 // a pedwarn.
1560 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Mike Stump20d0ee52009-04-02 02:29:42 +00001561 Diag(BufferEnd, diag::ext_no_newline_eof)
Douglas Gregor849b2432010-03-31 17:46:05 +00001562 << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001563
Reid Spencer5f016e22007-07-11 17:01:13 +00001564 BufferPtr = CurPtr;
1565
1566 // Finally, let the preprocessor handle this.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001567 return PP->HandleEndOfFile(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001568}
1569
1570/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1571/// the specified lexer will return a tok::l_paren token, 0 if it is something
1572/// else and 2 if there are no more tokens in the buffer controlled by the
1573/// lexer.
1574unsigned Lexer::isNextPPTokenLParen() {
1575 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +00001576
Reid Spencer5f016e22007-07-11 17:01:13 +00001577 // Switch to 'skipping' mode. This will ensure that we can lex a token
1578 // without emitting diagnostics, disables macro expansion, and will cause EOF
1579 // to return an EOF token instead of popping the include stack.
1580 LexingRawMode = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001581
Reid Spencer5f016e22007-07-11 17:01:13 +00001582 // Save state that can be changed while lexing so that we can restore it.
1583 const char *TmpBufferPtr = BufferPtr;
Chris Lattnera864cf72009-04-24 07:15:46 +00001584 bool inPPDirectiveMode = ParsingPreprocessorDirective;
Mike Stump1eb44332009-09-09 15:08:12 +00001585
Chris Lattnerd2177732007-07-20 16:59:19 +00001586 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001587 Tok.startToken();
1588 LexTokenInternal(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +00001589
Reid Spencer5f016e22007-07-11 17:01:13 +00001590 // Restore state that may have changed.
1591 BufferPtr = TmpBufferPtr;
Chris Lattnera864cf72009-04-24 07:15:46 +00001592 ParsingPreprocessorDirective = inPPDirectiveMode;
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Reid Spencer5f016e22007-07-11 17:01:13 +00001594 // Restore the lexer back to non-skipping mode.
1595 LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001596
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001597 if (Tok.is(tok::eof))
Reid Spencer5f016e22007-07-11 17:01:13 +00001598 return 2;
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001599 return Tok.is(tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001600}
1601
Chris Lattner34f349d2009-12-14 06:16:57 +00001602/// FindConflictEnd - Find the end of a version control conflict marker.
1603static const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd) {
1604 llvm::StringRef RestOfBuffer(CurPtr+7, BufferEnd-CurPtr-7);
1605 size_t Pos = RestOfBuffer.find(">>>>>>>");
1606 while (Pos != llvm::StringRef::npos) {
1607 // Must occur at start of line.
1608 if (RestOfBuffer[Pos-1] != '\r' &&
1609 RestOfBuffer[Pos-1] != '\n') {
1610 RestOfBuffer = RestOfBuffer.substr(Pos+7);
Chris Lattner3d488992010-05-17 20:27:25 +00001611 Pos = RestOfBuffer.find(">>>>>>>");
Chris Lattner34f349d2009-12-14 06:16:57 +00001612 continue;
1613 }
1614 return RestOfBuffer.data()+Pos;
1615 }
1616 return 0;
1617}
1618
1619/// IsStartOfConflictMarker - If the specified pointer is the start of a version
1620/// control conflict marker like '<<<<<<<', recognize it as such, emit an error
1621/// and recover nicely. This returns true if it is a conflict marker and false
1622/// if not.
1623bool Lexer::IsStartOfConflictMarker(const char *CurPtr) {
1624 // Only a conflict marker if it starts at the beginning of a line.
1625 if (CurPtr != BufferStart &&
1626 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1627 return false;
1628
1629 // Check to see if we have <<<<<<<.
1630 if (BufferEnd-CurPtr < 8 ||
1631 llvm::StringRef(CurPtr, 7) != "<<<<<<<")
1632 return false;
1633
1634 // If we have a situation where we don't care about conflict markers, ignore
1635 // it.
1636 if (IsInConflictMarker || isLexingRawMode())
1637 return false;
1638
1639 // Check to see if there is a >>>>>>> somewhere in the buffer at the start of
1640 // a line to terminate this conflict marker.
Chris Lattner3d488992010-05-17 20:27:25 +00001641 if (FindConflictEnd(CurPtr, BufferEnd)) {
Chris Lattner34f349d2009-12-14 06:16:57 +00001642 // We found a match. We are really in a conflict marker.
1643 // Diagnose this, and ignore to the end of line.
1644 Diag(CurPtr, diag::err_conflict_marker);
1645 IsInConflictMarker = true;
1646
1647 // Skip ahead to the end of line. We know this exists because the
1648 // end-of-conflict marker starts with \r or \n.
1649 while (*CurPtr != '\r' && *CurPtr != '\n') {
1650 assert(CurPtr != BufferEnd && "Didn't find end of line");
1651 ++CurPtr;
1652 }
1653 BufferPtr = CurPtr;
1654 return true;
1655 }
1656
1657 // No end of conflict marker found.
1658 return false;
1659}
1660
1661
1662/// HandleEndOfConflictMarker - If this is a '=======' or '|||||||' or '>>>>>>>'
1663/// marker, then it is the end of a conflict marker. Handle it by ignoring up
1664/// until the end of the line. This returns true if it is a conflict marker and
1665/// false if not.
1666bool Lexer::HandleEndOfConflictMarker(const char *CurPtr) {
1667 // Only a conflict marker if it starts at the beginning of a line.
1668 if (CurPtr != BufferStart &&
1669 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1670 return false;
1671
1672 // If we have a situation where we don't care about conflict markers, ignore
1673 // it.
1674 if (!IsInConflictMarker || isLexingRawMode())
1675 return false;
1676
1677 // Check to see if we have the marker (7 characters in a row).
1678 for (unsigned i = 1; i != 7; ++i)
1679 if (CurPtr[i] != CurPtr[0])
1680 return false;
1681
1682 // If we do have it, search for the end of the conflict marker. This could
1683 // fail if it got skipped with a '#if 0' or something. Note that CurPtr might
1684 // be the end of conflict marker.
1685 if (const char *End = FindConflictEnd(CurPtr, BufferEnd)) {
1686 CurPtr = End;
1687
1688 // Skip ahead to the end of line.
1689 while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n')
1690 ++CurPtr;
1691
1692 BufferPtr = CurPtr;
1693
1694 // No longer in the conflict marker.
1695 IsInConflictMarker = false;
1696 return true;
1697 }
1698
1699 return false;
1700}
1701
Reid Spencer5f016e22007-07-11 17:01:13 +00001702
1703/// LexTokenInternal - This implements a simple C family lexer. It is an
1704/// extremely performance critical piece of code. This assumes that the buffer
Chris Lattnerefb173d2009-07-07 05:05:42 +00001705/// has a null character at the end of the file. This returns a preprocessing
1706/// token, not a normal token, as such, it is an internal interface. It assumes
1707/// that the Flags of result have been cleared before calling this.
Chris Lattnerd2177732007-07-20 16:59:19 +00001708void Lexer::LexTokenInternal(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001709LexNextToken:
1710 // New token, can't need cleaning yet.
Chris Lattnerd2177732007-07-20 16:59:19 +00001711 Result.clearFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +00001712 Result.setIdentifierInfo(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001713
Reid Spencer5f016e22007-07-11 17:01:13 +00001714 // CurPtr - Cache BufferPtr in an automatic variable.
1715 const char *CurPtr = BufferPtr;
1716
1717 // Small amounts of horizontal whitespace is very common between tokens.
1718 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1719 ++CurPtr;
1720 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1721 ++CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001722
Chris Lattnerd88dc482008-10-12 04:05:48 +00001723 // If we are keeping whitespace and other tokens, just return what we just
1724 // skipped. The next lexer invocation will return the token after the
1725 // whitespace.
1726 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001727 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001728 return;
1729 }
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Reid Spencer5f016e22007-07-11 17:01:13 +00001731 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001732 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001733 }
Mike Stump1eb44332009-09-09 15:08:12 +00001734
Reid Spencer5f016e22007-07-11 17:01:13 +00001735 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
Mike Stump1eb44332009-09-09 15:08:12 +00001736
Reid Spencer5f016e22007-07-11 17:01:13 +00001737 // Read a character, advancing over it.
1738 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001739 tok::TokenKind Kind;
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Reid Spencer5f016e22007-07-11 17:01:13 +00001741 switch (Char) {
1742 case 0: // Null.
1743 // Found end of file?
1744 if (CurPtr-1 == BufferEnd) {
1745 // Read the PP instance variable into an automatic variable, because
1746 // LexEndOfFile will often delete 'this'.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001747 Preprocessor *PPCache = PP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001748 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1749 return; // Got a token to return.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001750 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1751 return PPCache->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001752 }
Mike Stump1eb44332009-09-09 15:08:12 +00001753
Chris Lattner74d15df2008-11-22 02:02:22 +00001754 if (!isLexingRawMode())
1755 Diag(CurPtr-1, diag::null_in_file);
Chris Lattnerd2177732007-07-20 16:59:19 +00001756 Result.setFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001757 if (SkipWhitespace(Result, CurPtr))
1758 return; // KeepWhitespaceMode
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Reid Spencer5f016e22007-07-11 17:01:13 +00001760 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattnera2bf1052009-12-17 05:29:40 +00001761
1762 case 26: // DOS & CP/M EOF: "^Z".
1763 // If we're in Microsoft extensions mode, treat this as end of file.
1764 if (Features.Microsoft) {
1765 // Read the PP instance variable into an automatic variable, because
1766 // LexEndOfFile will often delete 'this'.
1767 Preprocessor *PPCache = PP;
1768 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1769 return; // Got a token to return.
1770 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1771 return PPCache->Lex(Result);
1772 }
1773 // If Microsoft extensions are disabled, this is just random garbage.
1774 Kind = tok::unknown;
1775 break;
1776
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 case '\n':
1778 case '\r':
1779 // If we are inside a preprocessor directive and we see the end of line,
1780 // we know we are done with the directive, so return an EOM token.
1781 if (ParsingPreprocessorDirective) {
1782 // Done parsing the "line".
1783 ParsingPreprocessorDirective = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001784
Reid Spencer5f016e22007-07-11 17:01:13 +00001785 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001786 SetCommentRetentionState(PP->getCommentRetentionState());
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Reid Spencer5f016e22007-07-11 17:01:13 +00001788 // Since we consumed a newline, we are back at the start of a line.
1789 IsAtStartOfLine = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Chris Lattner9e6293d2008-10-12 04:51:35 +00001791 Kind = tok::eom;
Reid Spencer5f016e22007-07-11 17:01:13 +00001792 break;
1793 }
1794 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +00001795 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001796 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +00001797 Result.clearFlag(Token::LeadingSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001798
Chris Lattnerd88dc482008-10-12 04:05:48 +00001799 if (SkipWhitespace(Result, CurPtr))
1800 return; // KeepWhitespaceMode
Reid Spencer5f016e22007-07-11 17:01:13 +00001801 goto LexNextToken; // GCC isn't tail call eliminating.
1802 case ' ':
1803 case '\t':
1804 case '\f':
1805 case '\v':
Chris Lattner8133cfc2007-07-22 06:29:05 +00001806 SkipHorizontalWhitespace:
Chris Lattnerd2177732007-07-20 16:59:19 +00001807 Result.setFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001808 if (SkipWhitespace(Result, CurPtr))
1809 return; // KeepWhitespaceMode
Chris Lattner8133cfc2007-07-22 06:29:05 +00001810
1811 SkipIgnoredUnits:
1812 CurPtr = BufferPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001813
Chris Lattner8133cfc2007-07-22 06:29:05 +00001814 // If the next token is obviously a // or /* */ comment, skip it efficiently
1815 // too (without going through the big switch stmt).
Chris Lattner8402c732009-01-16 22:39:25 +00001816 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1817 Features.BCPLComment) {
Chris Lattner046c2272010-01-18 22:35:47 +00001818 if (SkipBCPLComment(Result, CurPtr+2))
1819 return; // There is a token to return.
Chris Lattner8133cfc2007-07-22 06:29:05 +00001820 goto SkipIgnoredUnits;
Chris Lattnerfa95a012008-10-12 03:22:02 +00001821 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner046c2272010-01-18 22:35:47 +00001822 if (SkipBlockComment(Result, CurPtr+2))
1823 return; // There is a token to return.
Chris Lattner8133cfc2007-07-22 06:29:05 +00001824 goto SkipIgnoredUnits;
1825 } else if (isHorizontalWhitespace(*CurPtr)) {
1826 goto SkipHorizontalWhitespace;
1827 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001828 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattnera2bf1052009-12-17 05:29:40 +00001829
Chris Lattner3a570772008-01-03 17:58:54 +00001830 // C99 6.4.4.1: Integer Constants.
1831 // C99 6.4.4.2: Floating Constants.
1832 case '0': case '1': case '2': case '3': case '4':
1833 case '5': case '6': case '7': case '8': case '9':
1834 // Notify MIOpt that we read a non-whitespace/non-comment token.
1835 MIOpt.ReadToken();
1836 return LexNumericConstant(Result, CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001837
Chris Lattner3a570772008-01-03 17:58:54 +00001838 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Reid Spencer5f016e22007-07-11 17:01:13 +00001839 // Notify MIOpt that we read a non-whitespace/non-comment token.
1840 MIOpt.ReadToken();
1841 Char = getCharAndSize(CurPtr, SizeTmp);
1842
1843 // Wide string literal.
1844 if (Char == '"')
1845 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1846 true);
1847
1848 // Wide character constant.
1849 if (Char == '\'')
1850 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1851 // FALL THROUGH, treating L like the start of an identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001852
Reid Spencer5f016e22007-07-11 17:01:13 +00001853 // C99 6.4.2: Identifiers.
1854 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1855 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1856 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1857 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1858 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1859 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1860 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1861 case 'v': case 'w': case 'x': case 'y': case 'z':
1862 case '_':
1863 // Notify MIOpt that we read a non-whitespace/non-comment token.
1864 MIOpt.ReadToken();
1865 return LexIdentifier(Result, CurPtr);
Chris Lattner3a570772008-01-03 17:58:54 +00001866
1867 case '$': // $ in identifiers.
1868 if (Features.DollarIdents) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001869 if (!isLexingRawMode())
1870 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner3a570772008-01-03 17:58:54 +00001871 // Notify MIOpt that we read a non-whitespace/non-comment token.
1872 MIOpt.ReadToken();
1873 return LexIdentifier(Result, CurPtr);
1874 }
Mike Stump1eb44332009-09-09 15:08:12 +00001875
Chris Lattner9e6293d2008-10-12 04:51:35 +00001876 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00001877 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001878
Reid Spencer5f016e22007-07-11 17:01:13 +00001879 // C99 6.4.4: Character Constants.
1880 case '\'':
1881 // Notify MIOpt that we read a non-whitespace/non-comment token.
1882 MIOpt.ReadToken();
1883 return LexCharConstant(Result, CurPtr);
1884
1885 // C99 6.4.5: String Literals.
1886 case '"':
1887 // Notify MIOpt that we read a non-whitespace/non-comment token.
1888 MIOpt.ReadToken();
1889 return LexStringLiteral(Result, CurPtr, false);
1890
1891 // C99 6.4.6: Punctuators.
1892 case '?':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001893 Kind = tok::question;
Reid Spencer5f016e22007-07-11 17:01:13 +00001894 break;
1895 case '[':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001896 Kind = tok::l_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001897 break;
1898 case ']':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001899 Kind = tok::r_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001900 break;
1901 case '(':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001902 Kind = tok::l_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001903 break;
1904 case ')':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001905 Kind = tok::r_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001906 break;
1907 case '{':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001908 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001909 break;
1910 case '}':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001911 Kind = tok::r_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001912 break;
1913 case '.':
1914 Char = getCharAndSize(CurPtr, SizeTmp);
1915 if (Char >= '0' && Char <= '9') {
1916 // Notify MIOpt that we read a non-whitespace/non-comment token.
1917 MIOpt.ReadToken();
1918
1919 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1920 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001921 Kind = tok::periodstar;
Reid Spencer5f016e22007-07-11 17:01:13 +00001922 CurPtr += SizeTmp;
1923 } else if (Char == '.' &&
1924 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001925 Kind = tok::ellipsis;
Reid Spencer5f016e22007-07-11 17:01:13 +00001926 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1927 SizeTmp2, Result);
1928 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001929 Kind = tok::period;
Reid Spencer5f016e22007-07-11 17:01:13 +00001930 }
1931 break;
1932 case '&':
1933 Char = getCharAndSize(CurPtr, SizeTmp);
1934 if (Char == '&') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001935 Kind = tok::ampamp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001936 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1937 } else if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001938 Kind = tok::ampequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001939 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1940 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001941 Kind = tok::amp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001942 }
1943 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001944 case '*':
Reid Spencer5f016e22007-07-11 17:01:13 +00001945 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001946 Kind = tok::starequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001947 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1948 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001949 Kind = tok::star;
Reid Spencer5f016e22007-07-11 17:01:13 +00001950 }
1951 break;
1952 case '+':
1953 Char = getCharAndSize(CurPtr, SizeTmp);
1954 if (Char == '+') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001955 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001956 Kind = tok::plusplus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001957 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001958 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001959 Kind = tok::plusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001960 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001961 Kind = tok::plus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001962 }
1963 break;
1964 case '-':
1965 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001966 if (Char == '-') { // --
Reid Spencer5f016e22007-07-11 17:01:13 +00001967 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001968 Kind = tok::minusminus;
Mike Stump1eb44332009-09-09 15:08:12 +00001969 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattner9e6293d2008-10-12 04:51:35 +00001970 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Reid Spencer5f016e22007-07-11 17:01:13 +00001971 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1972 SizeTmp2, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001973 Kind = tok::arrowstar;
1974 } else if (Char == '>') { // ->
Reid Spencer5f016e22007-07-11 17:01:13 +00001975 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001976 Kind = tok::arrow;
1977 } else if (Char == '=') { // -=
Reid Spencer5f016e22007-07-11 17:01:13 +00001978 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001979 Kind = tok::minusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001980 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001981 Kind = tok::minus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001982 }
1983 break;
1984 case '~':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001985 Kind = tok::tilde;
Reid Spencer5f016e22007-07-11 17:01:13 +00001986 break;
1987 case '!':
1988 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001989 Kind = tok::exclaimequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001990 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1991 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001992 Kind = tok::exclaim;
Reid Spencer5f016e22007-07-11 17:01:13 +00001993 }
1994 break;
1995 case '/':
1996 // 6.4.9: Comments
1997 Char = getCharAndSize(CurPtr, SizeTmp);
1998 if (Char == '/') { // BCPL comment.
Chris Lattner8402c732009-01-16 22:39:25 +00001999 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
2000 // want to lex this as a comment. There is one problem with this though,
2001 // that in one particular corner case, this can change the behavior of the
2002 // resultant program. For example, In "foo //**/ bar", C89 would lex
2003 // this as "foo / bar" and langauges with BCPL comments would lex it as
2004 // "foo". Check to see if the character after the second slash is a '*'.
2005 // If so, we will lex that as a "/" instead of the start of a comment.
2006 if (Features.BCPLComment ||
2007 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
2008 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner046c2272010-01-18 22:35:47 +00002009 return; // There is a token to return.
Mike Stump1eb44332009-09-09 15:08:12 +00002010
Chris Lattner8402c732009-01-16 22:39:25 +00002011 // It is common for the tokens immediately after a // comment to be
2012 // whitespace (indentation for the next line). Instead of going through
2013 // the big switch, handle it efficiently now.
2014 goto SkipIgnoredUnits;
2015 }
2016 }
Mike Stump1eb44332009-09-09 15:08:12 +00002017
Chris Lattner8402c732009-01-16 22:39:25 +00002018 if (Char == '*') { // /**/ comment.
Reid Spencer5f016e22007-07-11 17:01:13 +00002019 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner046c2272010-01-18 22:35:47 +00002020 return; // There is a token to return.
Chris Lattner2d381892008-10-12 04:15:42 +00002021 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner8402c732009-01-16 22:39:25 +00002022 }
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Chris Lattner8402c732009-01-16 22:39:25 +00002024 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00002025 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00002026 Kind = tok::slashequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00002027 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002028 Kind = tok::slash;
Reid Spencer5f016e22007-07-11 17:01:13 +00002029 }
2030 break;
2031 case '%':
2032 Char = getCharAndSize(CurPtr, SizeTmp);
2033 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002034 Kind = tok::percentequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00002035 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2036 } else if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002037 Kind = tok::r_brace; // '%>' -> '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002038 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2039 } else if (Features.Digraphs && Char == ':') {
2040 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2041 Char = getCharAndSize(CurPtr, SizeTmp);
2042 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002043 Kind = tok::hashhash; // '%:%:' -> '##'
Reid Spencer5f016e22007-07-11 17:01:13 +00002044 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
2045 SizeTmp2, Result);
2046 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Reid Spencer5f016e22007-07-11 17:01:13 +00002047 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner74d15df2008-11-22 02:02:22 +00002048 if (!isLexingRawMode())
2049 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner9e6293d2008-10-12 04:51:35 +00002050 Kind = tok::hashat;
Chris Lattnere91e9322009-03-18 20:58:27 +00002051 } else { // '%:' -> '#'
Reid Spencer5f016e22007-07-11 17:01:13 +00002052 // We parsed a # character. If this occurs at the start of the line,
2053 // it's actually the start of a preprocessing directive. Callback to
2054 // the preprocessor to handle it.
2055 // FIXME: -fpreprocessed mode??
Chris Lattner766703b2009-05-13 06:10:29 +00002056 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattnere91e9322009-03-18 20:58:27 +00002057 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner168ae2d2007-10-17 20:41:00 +00002058 PP->HandleDirective(Result);
Mike Stump1eb44332009-09-09 15:08:12 +00002059
Reid Spencer5f016e22007-07-11 17:01:13 +00002060 // As an optimization, if the preprocessor didn't switch lexers, tail
2061 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00002062 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002063 // Start a new token. If this is a #include or something, the PP may
2064 // want us starting at the beginning of the line again. If so, set
Chris Lattner515f43f2010-04-12 23:04:41 +00002065 // the StartOfLine flag and clear LeadingSpace.
Reid Spencer5f016e22007-07-11 17:01:13 +00002066 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00002067 Result.setFlag(Token::StartOfLine);
Chris Lattner515f43f2010-04-12 23:04:41 +00002068 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00002069 IsAtStartOfLine = false;
2070 }
2071 goto LexNextToken; // GCC isn't tail call eliminating.
2072 }
Mike Stump1eb44332009-09-09 15:08:12 +00002073
Chris Lattner168ae2d2007-10-17 20:41:00 +00002074 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00002075 }
Mike Stump1eb44332009-09-09 15:08:12 +00002076
Chris Lattnere91e9322009-03-18 20:58:27 +00002077 Kind = tok::hash;
Reid Spencer5f016e22007-07-11 17:01:13 +00002078 }
2079 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002080 Kind = tok::percent;
Reid Spencer5f016e22007-07-11 17:01:13 +00002081 }
2082 break;
2083 case '<':
2084 Char = getCharAndSize(CurPtr, SizeTmp);
2085 if (ParsingFilename) {
Chris Lattner9cb51ce2009-04-17 23:56:52 +00002086 return LexAngledStringLiteral(Result, CurPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +00002087 } else if (Char == '<') {
Chris Lattner34f349d2009-12-14 06:16:57 +00002088 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
2089 if (After == '=') {
2090 Kind = tok::lesslessequal;
2091 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
2092 SizeTmp2, Result);
2093 } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) {
2094 // If this is actually a '<<<<<<<' version control conflict marker,
2095 // recognize it as such and recover nicely.
2096 goto LexNextToken;
2097 } else {
2098 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2099 Kind = tok::lessless;
2100 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002101 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00002102 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00002103 Kind = tok::lessequal;
2104 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Reid Spencer5f016e22007-07-11 17:01:13 +00002105 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00002106 Kind = tok::l_square;
2107 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Reid Spencer5f016e22007-07-11 17:01:13 +00002108 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00002109 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00002110 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002111 Kind = tok::less;
Reid Spencer5f016e22007-07-11 17:01:13 +00002112 }
2113 break;
2114 case '>':
2115 Char = getCharAndSize(CurPtr, SizeTmp);
2116 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00002117 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00002118 Kind = tok::greaterequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00002119 } else if (Char == '>') {
Chris Lattner34f349d2009-12-14 06:16:57 +00002120 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
2121 if (After == '=') {
2122 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
2123 SizeTmp2, Result);
2124 Kind = tok::greatergreaterequal;
2125 } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) {
2126 // If this is '>>>>>>>' and we're in a conflict marker, ignore it.
2127 goto LexNextToken;
2128 } else {
2129 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2130 Kind = tok::greatergreater;
2131 }
2132
Reid Spencer5f016e22007-07-11 17:01:13 +00002133 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002134 Kind = tok::greater;
Reid Spencer5f016e22007-07-11 17:01:13 +00002135 }
2136 break;
2137 case '^':
2138 Char = getCharAndSize(CurPtr, SizeTmp);
2139 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00002140 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00002141 Kind = tok::caretequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00002142 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002143 Kind = tok::caret;
Reid Spencer5f016e22007-07-11 17:01:13 +00002144 }
2145 break;
2146 case '|':
2147 Char = getCharAndSize(CurPtr, SizeTmp);
2148 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002149 Kind = tok::pipeequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00002150 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2151 } else if (Char == '|') {
Chris Lattner34f349d2009-12-14 06:16:57 +00002152 // If this is '|||||||' and we're in a conflict marker, ignore it.
2153 if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1))
2154 goto LexNextToken;
Chris Lattner9e6293d2008-10-12 04:51:35 +00002155 Kind = tok::pipepipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00002156 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2157 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002158 Kind = tok::pipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00002159 }
2160 break;
2161 case ':':
2162 Char = getCharAndSize(CurPtr, SizeTmp);
2163 if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002164 Kind = tok::r_square; // ':>' -> ']'
Reid Spencer5f016e22007-07-11 17:01:13 +00002165 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2166 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002167 Kind = tok::coloncolon;
Reid Spencer5f016e22007-07-11 17:01:13 +00002168 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump1eb44332009-09-09 15:08:12 +00002169 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002170 Kind = tok::colon;
Reid Spencer5f016e22007-07-11 17:01:13 +00002171 }
2172 break;
2173 case ';':
Chris Lattner9e6293d2008-10-12 04:51:35 +00002174 Kind = tok::semi;
Reid Spencer5f016e22007-07-11 17:01:13 +00002175 break;
2176 case '=':
2177 Char = getCharAndSize(CurPtr, SizeTmp);
2178 if (Char == '=') {
Chris Lattner34f349d2009-12-14 06:16:57 +00002179 // If this is '=======' and we're in a conflict marker, ignore it.
2180 if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1))
2181 goto LexNextToken;
2182
Chris Lattner9e6293d2008-10-12 04:51:35 +00002183 Kind = tok::equalequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00002184 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump1eb44332009-09-09 15:08:12 +00002185 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002186 Kind = tok::equal;
Reid Spencer5f016e22007-07-11 17:01:13 +00002187 }
2188 break;
2189 case ',':
Chris Lattner9e6293d2008-10-12 04:51:35 +00002190 Kind = tok::comma;
Reid Spencer5f016e22007-07-11 17:01:13 +00002191 break;
2192 case '#':
2193 Char = getCharAndSize(CurPtr, SizeTmp);
2194 if (Char == '#') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002195 Kind = tok::hashhash;
Reid Spencer5f016e22007-07-11 17:01:13 +00002196 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2197 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner9e6293d2008-10-12 04:51:35 +00002198 Kind = tok::hashat;
Chris Lattner74d15df2008-11-22 02:02:22 +00002199 if (!isLexingRawMode())
2200 Diag(BufferPtr, diag::charize_microsoft_ext);
Reid Spencer5f016e22007-07-11 17:01:13 +00002201 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2202 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +00002203 // We parsed a # character. If this occurs at the start of the line,
2204 // it's actually the start of a preprocessing directive. Callback to
2205 // the preprocessor to handle it.
2206 // FIXME: -fpreprocessed mode??
Chris Lattner766703b2009-05-13 06:10:29 +00002207 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattnere91e9322009-03-18 20:58:27 +00002208 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner168ae2d2007-10-17 20:41:00 +00002209 PP->HandleDirective(Result);
Mike Stump1eb44332009-09-09 15:08:12 +00002210
Reid Spencer5f016e22007-07-11 17:01:13 +00002211 // As an optimization, if the preprocessor didn't switch lexers, tail
2212 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00002213 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002214 // Start a new token. If this is a #include or something, the PP may
2215 // want us starting at the beginning of the line again. If so, set
Chris Lattner515f43f2010-04-12 23:04:41 +00002216 // the StartOfLine flag and clear LeadingSpace.
Reid Spencer5f016e22007-07-11 17:01:13 +00002217 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00002218 Result.setFlag(Token::StartOfLine);
Chris Lattner515f43f2010-04-12 23:04:41 +00002219 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00002220 IsAtStartOfLine = false;
2221 }
2222 goto LexNextToken; // GCC isn't tail call eliminating.
2223 }
Chris Lattner168ae2d2007-10-17 20:41:00 +00002224 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00002225 }
Mike Stump1eb44332009-09-09 15:08:12 +00002226
Chris Lattnere91e9322009-03-18 20:58:27 +00002227 Kind = tok::hash;
Reid Spencer5f016e22007-07-11 17:01:13 +00002228 }
2229 break;
2230
Chris Lattner3a570772008-01-03 17:58:54 +00002231 case '@':
2232 // Objective C support.
2233 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattner9e6293d2008-10-12 04:51:35 +00002234 Kind = tok::at;
Chris Lattner3a570772008-01-03 17:58:54 +00002235 else
Chris Lattner9e6293d2008-10-12 04:51:35 +00002236 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00002237 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002238
Reid Spencer5f016e22007-07-11 17:01:13 +00002239 case '\\':
2240 // FIXME: UCN's.
2241 // FALL THROUGH.
2242 default:
Chris Lattner9e6293d2008-10-12 04:51:35 +00002243 Kind = tok::unknown;
Reid Spencer5f016e22007-07-11 17:01:13 +00002244 break;
2245 }
Mike Stump1eb44332009-09-09 15:08:12 +00002246
Reid Spencer5f016e22007-07-11 17:01:13 +00002247 // Notify MIOpt that we read a non-whitespace/non-comment token.
2248 MIOpt.ReadToken();
2249
2250 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00002251 FormTokenWithChars(Result, CurPtr, Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00002252}