blob: 3a77bbbfba71ad51c6d202d46fea7c55297d6e2e [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner146762e2007-07-20 16:59:19 +000010// This file implements the Lexer and Token interfaces.
Chris Lattner22eb9722006-06-18 05:43:12 +000011//
12//===----------------------------------------------------------------------===//
13//
14// TODO: GCC Diagnostics emitted by the lexer:
15// PEDWARN: (form feed|vertical tab) in preprocessing directive
16//
17// Universal characters, unicode, char mapping:
18// WARNING: `%.*s' is not in NFKC
19// WARNING: `%.*s' is not in NFC
20//
21// Other:
Chris Lattner22eb9722006-06-18 05:43:12 +000022// TODO: Options to support:
23// -fexec-charset,-fwide-exec-charset
24//
25//===----------------------------------------------------------------------===//
26
27#include "clang/Lex/Lexer.h"
28#include "clang/Lex/Preprocessor.h"
Chris Lattner60f36222009-01-29 05:15:15 +000029#include "clang/Lex/LexDiagnostic.h"
Chris Lattnerdc5c0552007-07-20 16:37:10 +000030#include "clang/Basic/SourceManager.h"
Douglas Gregoraf82e352010-07-20 20:18:03 +000031#include "llvm/ADT/StringSwitch.h"
Chris Lattner619c1742007-07-22 18:38:25 +000032#include "llvm/Support/Compiler.h"
Chris Lattner739e7392007-04-29 07:12:06 +000033#include "llvm/Support/MemoryBuffer.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000034#include <cctype>
Chris Lattner22eb9722006-06-18 05:43:12 +000035using namespace clang;
36
Chris Lattner3dfff972009-12-17 05:29:40 +000037static void InitCharacterInfo();
Chris Lattner22eb9722006-06-18 05:43:12 +000038
Chris Lattner4894f482007-10-07 08:47:24 +000039//===----------------------------------------------------------------------===//
40// Token Class Implementation
41//===----------------------------------------------------------------------===//
42
Mike Stump11289f42009-09-09 15:08:12 +000043/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
Chris Lattner4894f482007-10-07 08:47:24 +000044bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
Douglas Gregor90abb6d2008-12-01 21:46:47 +000045 if (IdentifierInfo *II = getIdentifierInfo())
46 return II->getObjCKeywordID() == objcKey;
47 return false;
Chris Lattner4894f482007-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 Lattner67671ed2007-12-13 01:59:49 +000056
Chris Lattner4894f482007-10-07 08:47:24 +000057//===----------------------------------------------------------------------===//
58// Lexer Class Implementation
59//===----------------------------------------------------------------------===//
60
Mike Stump11289f42009-09-09 15:08:12 +000061void Lexer::InitLexer(const char *BufStart, const char *BufPtr,
Chris Lattnerf76b9202009-01-17 06:55:17 +000062 const char *BufEnd) {
Chris Lattner3dfff972009-12-17 05:29:40 +000063 InitCharacterInfo();
Mike Stump11289f42009-09-09 15:08:12 +000064
Chris Lattnerf76b9202009-01-17 06:55:17 +000065 BufferStart = BufStart;
66 BufferPtr = BufPtr;
67 BufferEnd = BufEnd;
Mike Stump11289f42009-09-09 15:08:12 +000068
Chris Lattnerf76b9202009-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 Stump11289f42009-09-09 15:08:12 +000072
Chris Lattnerf76b9202009-01-17 06:55:17 +000073 Is_PragmaLexer = false;
Chris Lattner7c027ee2009-12-14 06:16:57 +000074 IsInConflictMarker = false;
Douglas Gregor2436e712009-09-17 21:32:03 +000075
Chris Lattnerf76b9202009-01-17 06:55:17 +000076 // Start of the file is a start of line.
77 IsAtStartOfLine = true;
Mike Stump11289f42009-09-09 15:08:12 +000078
Chris Lattnerf76b9202009-01-17 06:55:17 +000079 // We are not after parsing a #.
80 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +000081
Chris Lattnerf76b9202009-01-17 06:55:17 +000082 // We are not after parsing #include.
83 ParsingFilename = false;
Mike Stump11289f42009-09-09 15:08:12 +000084
Chris Lattnerf76b9202009-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 Stump11289f42009-09-09 15:08:12 +000090
Chris Lattnerf76b9202009-01-17 06:55:17 +000091 // Default to not keeping comments.
92 ExtendedTokenMode = 0;
93}
94
Chris Lattner5965a282009-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 Lattner710bb872009-11-30 04:18:44 +000099Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP)
Chris Lattnerc8090892009-01-17 08:03:42 +0000100 : PreprocessorLexer(&PP, FID),
101 FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
102 Features(PP.getLangOptions()) {
Mike Stump11289f42009-09-09 15:08:12 +0000103
Chris Lattner5965a282009-01-17 07:56:59 +0000104 InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
105 InputFile->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000106
Chris Lattner5965a282009-01-17 07:56:59 +0000107 // Default to keeping comments if the preprocessor wants them.
108 SetCommentRetentionState(PP.getCommentRetentionState());
109}
Chris Lattner4894f482007-10-07 08:47:24 +0000110
Chris Lattner02b436a2007-10-17 20:41:00 +0000111/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner50c90502008-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 Lattner02b436a2007-10-17 20:41:00 +0000114Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattnerfcf64522009-01-17 07:42:27 +0000115 const char *BufStart, const char *BufPtr, const char *BufEnd)
Chris Lattner1abd2092009-01-17 03:48:08 +0000116 : FileLoc(fileloc), Features(features) {
Chris Lattnerf76b9202009-01-17 06:55:17 +0000117
Chris Lattnerf76b9202009-01-17 06:55:17 +0000118 InitLexer(BufStart, BufPtr, BufEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000119
Chris Lattner02b436a2007-10-17 20:41:00 +0000120 // We *are* in raw mode.
121 LexingRawMode = true;
Chris Lattner02b436a2007-10-17 20:41:00 +0000122}
123
Chris Lattner08354fe2009-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 Lattner710bb872009-11-30 04:18:44 +0000127Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile,
128 const SourceManager &SM, const LangOptions &features)
Chris Lattner08354fe2009-01-17 07:35:14 +0000129 : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
Chris Lattner08354fe2009-01-17 07:35:14 +0000130
Mike Stump11289f42009-09-09 15:08:12 +0000131 InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
Chris Lattner08354fe2009-01-17 07:35:14 +0000132 FromFile->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000133
Chris Lattner08354fe2009-01-17 07:35:14 +0000134 // We *are* in raw mode.
135 LexingRawMode = true;
136}
137
Chris Lattner757169b2009-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 Stump11289f42009-09-09 15:08:12 +0000153Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
Chris Lattner9dc9c202009-02-15 20:52:18 +0000154 SourceLocation InstantiationLocStart,
155 SourceLocation InstantiationLocEnd,
Chris Lattner29a2a192009-01-19 06:46:35 +0000156 unsigned TokLen, Preprocessor &PP) {
Chris Lattner757169b2009-01-17 08:27:52 +0000157 SourceManager &SM = PP.getSourceManager();
Chris Lattner757169b2009-01-17 08:27:52 +0000158
159 // Create the lexer as if we were going to lex the file normally.
Chris Lattnercbc35ecb2009-01-19 07:46:45 +0000160 FileID SpellingFID = SM.getFileID(SpellingLoc);
Chris Lattner710bb872009-11-30 04:18:44 +0000161 const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID);
162 Lexer *L = new Lexer(SpellingFID, InputFile, PP);
Mike Stump11289f42009-09-09 15:08:12 +0000163
Chris Lattner757169b2009-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 Stump11289f42009-09-09 15:08:12 +0000168
Chris Lattner757169b2009-01-17 08:27:52 +0000169 L->BufferPtr = StrData;
170 L->BufferEnd = StrData+TokLen;
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000171 assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
Chris Lattner757169b2009-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 Lattner4fa23622009-01-26 00:43:02 +0000175 L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
Chris Lattner9dc9c202009-02-15 20:52:18 +0000176 InstantiationLocStart,
177 InstantiationLocEnd, TokLen);
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattner757169b2009-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 Stump11289f42009-09-09 15:08:12 +0000182
Chris Lattner757169b2009-01-17 08:27:52 +0000183 // This lexer really is for _Pragma.
184 L->Is_PragmaLexer = true;
185 return L;
186}
187
Chris Lattner02b436a2007-10-17 20:41:00 +0000188
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000189/// Stringify - Convert the specified string into a C string, with surrounding
190/// ""'s, and with escaped \ and " characters.
Chris Lattnerecc39e92006-07-15 05:23:31 +0000191std::string Lexer::Stringify(const std::string &Str, bool Charify) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000192 std::string Result = Str;
Chris Lattnerecc39e92006-07-15 05:23:31 +0000193 char Quote = Charify ? '\'' : '"';
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000194 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
Chris Lattnerecc39e92006-07-15 05:23:31 +0000195 if (Result[i] == '\\' || Result[i] == Quote) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000196 Result.insert(Result.begin()+i, '\\');
197 ++i; ++e;
198 }
199 }
Chris Lattnerecc39e92006-07-15 05:23:31 +0000200 return Result;
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000201}
202
Chris Lattner4c4a2452007-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 Gregor562c1f92010-01-22 19:49:59 +0000214static bool isWhitespace(unsigned char c);
Chris Lattner22eb9722006-06-18 05:43:12 +0000215
Chris Lattner8e129c22007-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 Lattner184e65d2009-04-14 23:22:57 +0000221 const SourceManager &SM,
222 const LangOptions &LangOpts) {
Chris Lattner8e129c22007-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 Stump11289f42009-09-09 15:08:12 +0000225 // all obviously single-char tokens. This could use
Chris Lattner8e129c22007-10-17 21:18:47 +0000226 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
227 // something.
Chris Lattner4fa23622009-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 Lattnerd3817212009-01-26 22:24:27 +0000231 Loc = SM.getInstantiationLoc(Loc);
232 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
Douglas Gregore0fbb832010-03-16 00:06:06 +0000233 bool Invalid = false;
Benjamin Kramereb92dc02010-03-16 14:14:31 +0000234 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Douglas Gregore0fbb832010-03-16 00:06:06 +0000235 if (Invalid)
Douglas Gregor802b7762010-03-15 22:54:52 +0000236 return 0;
Benjamin Kramereb92dc02010-03-16 14:14:31 +0000237
238 const char *StrData = Buffer.data()+LocInfo.second;
Chris Lattner5509d532009-01-17 08:30:10 +0000239
Douglas Gregor562c1f92010-01-22 19:49:59 +0000240 if (isWhitespace(StrData[0]))
241 return 0;
242
Chris Lattner8e129c22007-10-17 21:18:47 +0000243 // Create a lexer starting at the beginning of this token.
Benjamin Kramereb92dc02010-03-16 14:14:31 +0000244 Lexer TheLexer(Loc, LangOpts, Buffer.begin(), StrData, Buffer.end());
Chris Lattnera3d4f162009-10-14 15:04:18 +0000245 TheLexer.SetCommentRetentionState(true);
Chris Lattner8e129c22007-10-17 21:18:47 +0000246 Token TheTok;
Chris Lattner50c90502008-10-12 01:15:46 +0000247 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner8e129c22007-10-17 21:18:47 +0000248 return TheTok.getLength();
249}
250
Douglas Gregorcd8bdd02010-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 Gregoraf82e352010-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 Gregor3f4bea02010-07-26 21:36:20 +0000313std::pair<unsigned, bool>
Douglas Gregor028d3e42010-08-09 20:45:32 +0000314Lexer::ComputePreamble(const llvm::MemoryBuffer *Buffer, unsigned MaxLines) {
Douglas Gregoraf82e352010-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;
Douglas Gregor028d3e42010-08-09 20:45:32 +0000328 unsigned Line = 0;
329
Douglas Gregoraf82e352010-07-20 20:18:03 +0000330 do {
331 TheLexer.LexFromRawLexer(TheTok);
332
333 if (InPreprocessorDirective) {
334 // If we've hit the end of the file, we're done.
335 if (TheTok.getKind() == tok::eof) {
336 InPreprocessorDirective = false;
337 break;
338 }
339
340 // If we haven't hit the end of the preprocessor directive, skip this
341 // token.
342 if (!TheTok.isAtStartOfLine())
343 continue;
344
345 // We've passed the end of the preprocessor directive, and will look
346 // at this token again below.
347 InPreprocessorDirective = false;
348 }
349
Douglas Gregor028d3e42010-08-09 20:45:32 +0000350 // Keep track of the # of lines in the preamble.
351 if (TheTok.isAtStartOfLine()) {
352 ++Line;
353
354 // If we were asked to limit the number of lines in the preamble,
355 // and we're about to exceed that limit, we're done.
356 if (MaxLines && Line >= MaxLines)
357 break;
358 }
359
Douglas Gregoraf82e352010-07-20 20:18:03 +0000360 // Comments are okay; skip over them.
361 if (TheTok.getKind() == tok::comment)
362 continue;
363
364 if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) {
365 // This is the start of a preprocessor directive.
366 Token HashTok = TheTok;
367 InPreprocessorDirective = true;
368
369 // Figure out which direective this is. Since we're lexing raw tokens,
370 // we don't have an identifier table available. Instead, just look at
371 // the raw identifier to recognize and categorize preprocessor directives.
372 TheLexer.LexFromRawLexer(TheTok);
373 if (TheTok.getKind() == tok::identifier && !TheTok.needsCleaning()) {
374 const char *IdStart = Buffer->getBufferStart()
375 + TheTok.getLocation().getRawEncoding() - 1;
376 llvm::StringRef Keyword(IdStart, TheTok.getLength());
377 PreambleDirectiveKind PDK
378 = llvm::StringSwitch<PreambleDirectiveKind>(Keyword)
379 .Case("include", PDK_Skipped)
380 .Case("__include_macros", PDK_Skipped)
381 .Case("define", PDK_Skipped)
382 .Case("undef", PDK_Skipped)
383 .Case("line", PDK_Skipped)
384 .Case("error", PDK_Skipped)
385 .Case("pragma", PDK_Skipped)
386 .Case("import", PDK_Skipped)
387 .Case("include_next", PDK_Skipped)
388 .Case("warning", PDK_Skipped)
389 .Case("ident", PDK_Skipped)
390 .Case("sccs", PDK_Skipped)
391 .Case("assert", PDK_Skipped)
392 .Case("unassert", PDK_Skipped)
393 .Case("if", PDK_StartIf)
394 .Case("ifdef", PDK_StartIf)
395 .Case("ifndef", PDK_StartIf)
396 .Case("elif", PDK_Skipped)
397 .Case("else", PDK_Skipped)
398 .Case("endif", PDK_EndIf)
399 .Default(PDK_Unknown);
400
401 switch (PDK) {
402 case PDK_Skipped:
403 continue;
404
405 case PDK_StartIf:
406 if (IfCount == 0)
407 IfStartTok = HashTok;
408
409 ++IfCount;
410 continue;
411
412 case PDK_EndIf:
413 // Mismatched #endif. The preamble ends here.
414 if (IfCount == 0)
415 break;
416
417 --IfCount;
418 continue;
419
420 case PDK_Unknown:
421 // We don't know what this directive is; stop at the '#'.
422 break;
423 }
424 }
425
426 // We only end up here if we didn't recognize the preprocessor
427 // directive or it was one that can't occur in the preamble at this
428 // point. Roll back the current token to the location of the '#'.
429 InPreprocessorDirective = false;
430 TheTok = HashTok;
431 }
432
Douglas Gregor028d3e42010-08-09 20:45:32 +0000433 // We hit a token that we don't recognize as being in the
434 // "preprocessing only" part of the file, so we're no longer in
435 // the preamble.
Douglas Gregoraf82e352010-07-20 20:18:03 +0000436 break;
437 } while (true);
438
439 SourceLocation End = IfCount? IfStartTok.getLocation() : TheTok.getLocation();
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000440 return std::make_pair(End.getRawEncoding() - StartLoc.getRawEncoding(),
441 IfCount? IfStartTok.isAtStartOfLine()
442 : TheTok.isAtStartOfLine());
Douglas Gregoraf82e352010-07-20 20:18:03 +0000443}
444
Chris Lattner22eb9722006-06-18 05:43:12 +0000445//===----------------------------------------------------------------------===//
446// Character information.
447//===----------------------------------------------------------------------===//
448
Chris Lattner22eb9722006-06-18 05:43:12 +0000449enum {
450 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
451 CHAR_VERT_WS = 0x02, // '\r', '\n'
452 CHAR_LETTER = 0x04, // a-z,A-Z
453 CHAR_NUMBER = 0x08, // 0-9
454 CHAR_UNDER = 0x10, // _
455 CHAR_PERIOD = 0x20 // .
456};
457
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000458// Statically initialize CharInfo table based on ASCII character set
459// Reference: FreeBSD 7.2 /usr/share/misc/ascii
Chris Lattner3dfff972009-12-17 05:29:40 +0000460static const unsigned char CharInfo[256] =
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000461{
462// 0 NUL 1 SOH 2 STX 3 ETX
463// 4 EOT 5 ENQ 6 ACK 7 BEL
464 0 , 0 , 0 , 0 ,
465 0 , 0 , 0 , 0 ,
466// 8 BS 9 HT 10 NL 11 VT
467//12 NP 13 CR 14 SO 15 SI
468 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS,
469 CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 ,
470//16 DLE 17 DC1 18 DC2 19 DC3
471//20 DC4 21 NAK 22 SYN 23 ETB
472 0 , 0 , 0 , 0 ,
473 0 , 0 , 0 , 0 ,
474//24 CAN 25 EM 26 SUB 27 ESC
475//28 FS 29 GS 30 RS 31 US
476 0 , 0 , 0 , 0 ,
477 0 , 0 , 0 , 0 ,
478//32 SP 33 ! 34 " 35 #
479//36 $ 37 % 38 & 39 '
480 CHAR_HORZ_WS, 0 , 0 , 0 ,
481 0 , 0 , 0 , 0 ,
482//40 ( 41 ) 42 * 43 +
483//44 , 45 - 46 . 47 /
484 0 , 0 , 0 , 0 ,
485 0 , 0 , CHAR_PERIOD , 0 ,
486//48 0 49 1 50 2 51 3
487//52 4 53 5 54 6 55 7
488 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
489 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
490//56 8 57 9 58 : 59 ;
491//60 < 61 = 62 > 63 ?
492 CHAR_NUMBER , CHAR_NUMBER , 0 , 0 ,
493 0 , 0 , 0 , 0 ,
494//64 @ 65 A 66 B 67 C
495//68 D 69 E 70 F 71 G
496 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
497 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
498//72 H 73 I 74 J 75 K
499//76 L 77 M 78 N 79 O
500 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
501 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
502//80 P 81 Q 82 R 83 S
503//84 T 85 U 86 V 87 W
504 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
505 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
506//88 X 89 Y 90 Z 91 [
507//92 \ 93 ] 94 ^ 95 _
508 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
509 0 , 0 , 0 , CHAR_UNDER ,
510//96 ` 97 a 98 b 99 c
511//100 d 101 e 102 f 103 g
512 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
513 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
514//104 h 105 i 106 j 107 k
515//108 l 109 m 110 n 111 o
516 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
517 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
518//112 p 113 q 114 r 115 s
519//116 t 117 u 118 v 119 w
520 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
521 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
522//120 x 121 y 122 z 123 {
523//124 | 125 } 126 ~ 127 DEL
524 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
525 0 , 0 , 0 , 0
526};
527
Chris Lattner3dfff972009-12-17 05:29:40 +0000528static void InitCharacterInfo() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000529 static bool isInited = false;
530 if (isInited) return;
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000531 // check the statically-initialized CharInfo table
532 assert(CHAR_HORZ_WS == CharInfo[(int)' ']);
533 assert(CHAR_HORZ_WS == CharInfo[(int)'\t']);
534 assert(CHAR_HORZ_WS == CharInfo[(int)'\f']);
535 assert(CHAR_HORZ_WS == CharInfo[(int)'\v']);
536 assert(CHAR_VERT_WS == CharInfo[(int)'\n']);
537 assert(CHAR_VERT_WS == CharInfo[(int)'\r']);
538 assert(CHAR_UNDER == CharInfo[(int)'_']);
539 assert(CHAR_PERIOD == CharInfo[(int)'.']);
540 for (unsigned i = 'a'; i <= 'z'; ++i) {
541 assert(CHAR_LETTER == CharInfo[i]);
542 assert(CHAR_LETTER == CharInfo[i+'A'-'a']);
543 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000544 for (unsigned i = '0'; i <= '9'; ++i)
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000545 assert(CHAR_NUMBER == CharInfo[i]);
Steve Naroff04bc0182009-12-08 16:38:12 +0000546
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000547 isInited = true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000548}
549
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000550
Chris Lattner22eb9722006-06-18 05:43:12 +0000551/// isIdentifierBody - Return true if this is the body character of an
552/// identifier, which is [a-zA-Z0-9_].
553static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000554 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000555}
556
557/// isHorizontalWhitespace - Return true if this character is horizontal
558/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
559static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000560 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000561}
562
563/// isWhitespace - Return true if this character is horizontal or vertical
564/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
565/// for '\0'.
566static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000567 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000568}
569
570/// isNumberBody - Return true if this is the body character of an
571/// preprocessing number, which is [a-zA-Z0-9_.].
572static inline bool isNumberBody(unsigned char c) {
Mike Stump11289f42009-09-09 15:08:12 +0000573 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000574 true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000575}
576
Chris Lattnerd01e2912006-06-18 16:22:51 +0000577
Chris Lattner22eb9722006-06-18 05:43:12 +0000578//===----------------------------------------------------------------------===//
579// Diagnostics forwarding code.
580//===----------------------------------------------------------------------===//
581
Chris Lattner619c1742007-07-22 18:38:25 +0000582/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
583/// lexer buffer was all instantiated at a single point, perform the mapping.
584/// This is currently only used for _Pragma implementation, so it is the slow
585/// path of the hot getSourceLocation method. Do not allow it to be inlined.
Benjamin Kramer5e738282009-11-14 16:36:57 +0000586static DISABLE_INLINE SourceLocation GetMappedTokenLoc(Preprocessor &PP,
587 SourceLocation FileLoc,
588 unsigned CharNo,
589 unsigned TokLen);
Chris Lattner619c1742007-07-22 18:38:25 +0000590static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
591 SourceLocation FileLoc,
Chris Lattner4fa23622009-01-26 00:43:02 +0000592 unsigned CharNo, unsigned TokLen) {
Chris Lattner9dc9c202009-02-15 20:52:18 +0000593 assert(FileLoc.isMacroID() && "Must be an instantiation");
Mike Stump11289f42009-09-09 15:08:12 +0000594
Chris Lattner619c1742007-07-22 18:38:25 +0000595 // Otherwise, we're lexing "mapped tokens". This is used for things like
596 // _Pragma handling. Combine the instantiation location of FileLoc with the
Chris Lattner53e384f2009-01-16 07:00:02 +0000597 // spelling location.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000598 SourceManager &SM = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000599
Chris Lattner8a425862009-01-16 07:36:28 +0000600 // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
Chris Lattner53e384f2009-01-16 07:00:02 +0000601 // characters come from spelling(FileLoc)+Offset.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000602 SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
Chris Lattner29a2a192009-01-19 06:46:35 +0000603 SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
Mike Stump11289f42009-09-09 15:08:12 +0000604
Chris Lattner9dc9c202009-02-15 20:52:18 +0000605 // Figure out the expansion loc range, which is the range covered by the
606 // original _Pragma(...) sequence.
607 std::pair<SourceLocation,SourceLocation> II =
608 SM.getImmediateInstantiationRange(FileLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000609
Chris Lattner9dc9c202009-02-15 20:52:18 +0000610 return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
Chris Lattner619c1742007-07-22 18:38:25 +0000611}
612
Chris Lattner22eb9722006-06-18 05:43:12 +0000613/// getSourceLocation - Return a source location identifier for the specified
614/// offset in the current file.
Chris Lattner4fa23622009-01-26 00:43:02 +0000615SourceLocation Lexer::getSourceLocation(const char *Loc,
616 unsigned TokLen) const {
Chris Lattner5d1c0272007-07-22 18:44:36 +0000617 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Chris Lattner4cca5ba2006-07-02 20:05:54 +0000618 "Location out of range for this buffer!");
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000619
620 // In the normal case, we're just lexing from a simple file buffer, return
621 // the file id from FileLoc with the offset specified.
Chris Lattner5d1c0272007-07-22 18:44:36 +0000622 unsigned CharNo = Loc-BufferStart;
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000623 if (FileLoc.isFileID())
Chris Lattner29a2a192009-01-19 06:46:35 +0000624 return FileLoc.getFileLocWithOffset(CharNo);
Mike Stump11289f42009-09-09 15:08:12 +0000625
Chris Lattnerd32480d2009-01-17 06:22:33 +0000626 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
627 // tokens are lexed from where the _Pragma was defined.
Chris Lattner02b436a2007-10-17 20:41:00 +0000628 assert(PP && "This doesn't work on raw lexers");
Chris Lattner4fa23622009-01-26 00:43:02 +0000629 return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
Chris Lattner22eb9722006-06-18 05:43:12 +0000630}
631
Chris Lattner22eb9722006-06-18 05:43:12 +0000632/// Diag - Forwarding function for diagnostics. This translate a source
633/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner427c9c12008-11-22 00:59:29 +0000634DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner907dfe92008-11-18 07:59:24 +0000635 return PP->Diag(getSourceLocation(Loc), DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000636}
637
638//===----------------------------------------------------------------------===//
639// Trigraph and Escaped Newline Handling Code.
640//===----------------------------------------------------------------------===//
641
642/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
643/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
644static char GetTrigraphCharForLetter(char Letter) {
645 switch (Letter) {
646 default: return 0;
647 case '=': return '#';
648 case ')': return ']';
649 case '(': return '[';
650 case '!': return '|';
651 case '\'': return '^';
652 case '>': return '}';
653 case '/': return '\\';
654 case '<': return '{';
655 case '-': return '~';
656 }
657}
658
659/// DecodeTrigraphChar - If the specified character is a legal trigraph when
660/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
661/// return the result character. Finally, emit a warning about trigraph use
662/// whether trigraphs are enabled or not.
663static char DecodeTrigraphChar(const char *CP, Lexer *L) {
664 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner907dfe92008-11-18 07:59:24 +0000665 if (!Res || !L) return Res;
Mike Stump11289f42009-09-09 15:08:12 +0000666
Chris Lattner907dfe92008-11-18 07:59:24 +0000667 if (!L->getFeatures().Trigraphs) {
Chris Lattner6d27a162008-11-22 02:02:22 +0000668 if (!L->isLexingRawMode())
669 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner907dfe92008-11-18 07:59:24 +0000670 return 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000671 }
Mike Stump11289f42009-09-09 15:08:12 +0000672
Chris Lattner6d27a162008-11-22 02:02:22 +0000673 if (!L->isLexingRawMode())
Benjamin Kramere8394df2010-08-11 14:47:12 +0000674 L->Diag(CP-2, diag::trigraph_converted) << llvm::StringRef(&Res, 1);
Chris Lattner22eb9722006-06-18 05:43:12 +0000675 return Res;
676}
677
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000678/// getEscapedNewLineSize - Return the size of the specified escaped newline,
679/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a
Mike Stump11289f42009-09-09 15:08:12 +0000680/// trigraph equivalent on entry to this function.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000681unsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
682 unsigned Size = 0;
683 while (isWhitespace(Ptr[Size])) {
684 ++Size;
Mike Stump11289f42009-09-09 15:08:12 +0000685
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000686 if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
687 continue;
688
689 // If this is a \r\n or \n\r, skip the other half.
690 if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
691 Ptr[Size-1] != Ptr[Size])
692 ++Size;
Mike Stump11289f42009-09-09 15:08:12 +0000693
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000694 return Size;
Mike Stump11289f42009-09-09 15:08:12 +0000695 }
696
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000697 // Not an escaped newline, must be a \t or something else.
698 return 0;
699}
700
Chris Lattner38b2cde2009-04-18 22:27:02 +0000701/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
702/// them), skip over them and return the first non-escaped-newline found,
703/// otherwise return P.
704const char *Lexer::SkipEscapedNewLines(const char *P) {
705 while (1) {
706 const char *AfterEscape;
707 if (*P == '\\') {
708 AfterEscape = P+1;
709 } else if (*P == '?') {
710 // If not a trigraph for escape, bail out.
711 if (P[1] != '?' || P[2] != '/')
712 return P;
713 AfterEscape = P+3;
714 } else {
715 return P;
716 }
Mike Stump11289f42009-09-09 15:08:12 +0000717
Chris Lattner38b2cde2009-04-18 22:27:02 +0000718 unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
719 if (NewLineSize == 0) return P;
720 P = AfterEscape+NewLineSize;
721 }
722}
723
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000724
Chris Lattner22eb9722006-06-18 05:43:12 +0000725/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
726/// get its size, and return it. This is tricky in several cases:
727/// 1. If currently at the start of a trigraph, we warn about the trigraph,
728/// then either return the trigraph (skipping 3 chars) or the '?',
729/// depending on whether trigraphs are enabled or not.
730/// 2. If this is an escaped newline (potentially with whitespace between
731/// the backslash and newline), implicitly skip the newline and return
732/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000733/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000734///
735/// This handles the slow/uncommon case of the getCharAndSize method. Here we
736/// know that we can accumulate into Size, and that we have already incremented
737/// Ptr by Size bytes.
738///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000739/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
740/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000741///
742char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattner146762e2007-07-20 16:59:19 +0000743 Token *Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000744 // If we have a slash, look for an escaped newline.
745 if (Ptr[0] == '\\') {
746 ++Size;
747 ++Ptr;
748Slash:
749 // Common case, backslash-char where the char is not whitespace.
750 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump11289f42009-09-09 15:08:12 +0000751
Chris Lattnerc1835952009-06-23 05:15:06 +0000752 // See if we have optional whitespace characters between the slash and
753 // newline.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000754 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
755 // Remember that this token needs to be cleaned.
756 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000757
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000758 // Warn if there was whitespace between the backslash and newline.
Chris Lattnerc1835952009-06-23 05:15:06 +0000759 if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000760 Diag(Ptr, diag::backslash_newline_space);
Mike Stump11289f42009-09-09 15:08:12 +0000761
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000762 // Found backslash<whitespace><newline>. Parse the char after it.
763 Size += EscapedNewLineSize;
764 Ptr += EscapedNewLineSize;
765 // Use slow version to accumulate a correct size field.
766 return getCharAndSizeSlow(Ptr, Size, Tok);
767 }
Mike Stump11289f42009-09-09 15:08:12 +0000768
Chris Lattner22eb9722006-06-18 05:43:12 +0000769 // Otherwise, this is not an escaped newline, just return the slash.
770 return '\\';
771 }
Mike Stump11289f42009-09-09 15:08:12 +0000772
Chris Lattner22eb9722006-06-18 05:43:12 +0000773 // If this is a trigraph, process it.
774 if (Ptr[0] == '?' && Ptr[1] == '?') {
775 // If this is actually a legal trigraph (not something like "??x"), emit
776 // a trigraph warning. If so, and if trigraphs are enabled, return it.
777 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
778 // Remember that this token needs to be cleaned.
Chris Lattner146762e2007-07-20 16:59:19 +0000779 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000780
781 Ptr += 3;
782 Size += 3;
783 if (C == '\\') goto Slash;
784 return C;
785 }
786 }
Mike Stump11289f42009-09-09 15:08:12 +0000787
Chris Lattner22eb9722006-06-18 05:43:12 +0000788 // If this is neither, return a single character.
789 ++Size;
790 return *Ptr;
791}
792
Chris Lattnerd01e2912006-06-18 16:22:51 +0000793
Chris Lattner22eb9722006-06-18 05:43:12 +0000794/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
795/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
796/// and that we have already incremented Ptr by Size bytes.
797///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000798/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
799/// be updated to match.
800char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000801 const LangOptions &Features) {
802 // If we have a slash, look for an escaped newline.
803 if (Ptr[0] == '\\') {
804 ++Size;
805 ++Ptr;
806Slash:
807 // Common case, backslash-char where the char is not whitespace.
808 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump11289f42009-09-09 15:08:12 +0000809
Chris Lattner22eb9722006-06-18 05:43:12 +0000810 // See if we have optional whitespace characters followed by a newline.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000811 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
812 // Found backslash<whitespace><newline>. Parse the char after it.
813 Size += EscapedNewLineSize;
814 Ptr += EscapedNewLineSize;
Mike Stump11289f42009-09-09 15:08:12 +0000815
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000816 // Use slow version to accumulate a correct size field.
817 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
818 }
Mike Stump11289f42009-09-09 15:08:12 +0000819
Chris Lattner22eb9722006-06-18 05:43:12 +0000820 // Otherwise, this is not an escaped newline, just return the slash.
821 return '\\';
822 }
Mike Stump11289f42009-09-09 15:08:12 +0000823
Chris Lattner22eb9722006-06-18 05:43:12 +0000824 // If this is a trigraph, process it.
825 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
826 // If this is actually a legal trigraph (not something like "??x"), return
827 // it.
828 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
829 Ptr += 3;
830 Size += 3;
831 if (C == '\\') goto Slash;
832 return C;
833 }
834 }
Mike Stump11289f42009-09-09 15:08:12 +0000835
Chris Lattner22eb9722006-06-18 05:43:12 +0000836 // If this is neither, return a single character.
837 ++Size;
838 return *Ptr;
839}
840
Chris Lattner22eb9722006-06-18 05:43:12 +0000841//===----------------------------------------------------------------------===//
842// Helper methods for lexing.
843//===----------------------------------------------------------------------===//
844
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000845/// \brief Routine that indiscriminately skips bytes in the source file.
846void Lexer::SkipBytes(unsigned Bytes, bool StartOfLine) {
847 BufferPtr += Bytes;
848 if (BufferPtr > BufferEnd)
849 BufferPtr = BufferEnd;
850 IsAtStartOfLine = StartOfLine;
851}
852
Chris Lattner146762e2007-07-20 16:59:19 +0000853void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000854 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
855 unsigned Size;
856 unsigned char C = *CurPtr++;
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000857 while (isIdentifierBody(C))
Chris Lattner22eb9722006-06-18 05:43:12 +0000858 C = *CurPtr++;
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000859
Chris Lattner22eb9722006-06-18 05:43:12 +0000860 --CurPtr; // Back up over the skipped character.
861
862 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
863 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000864 // FIXME: UCNs.
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000865 //
866 // TODO: Could merge these checks into a CharInfo flag to make the comparison
867 // cheaper
Chris Lattner22eb9722006-06-18 05:43:12 +0000868 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
869FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000870 const char *IdStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000871 FormTokenWithChars(Result, CurPtr, tok::identifier);
Mike Stump11289f42009-09-09 15:08:12 +0000872
Chris Lattner0f1f5052006-07-20 04:16:23 +0000873 // If we are in raw mode, return this identifier raw. There is no need to
874 // look up identifier information or attempt to macro expand it.
875 if (LexingRawMode) return;
Mike Stump11289f42009-09-09 15:08:12 +0000876
Chris Lattnercefc7682006-07-08 08:28:12 +0000877 // Fill in Result.IdentifierInfo, looking up the identifier in the
878 // identifier table.
Chris Lattner8256b972009-01-21 07:45:14 +0000879 IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
Mike Stump11289f42009-09-09 15:08:12 +0000880
Chris Lattner1f6c7fe2009-01-23 18:35:48 +0000881 // Change the kind of this identifier to the appropriate token kind, e.g.
882 // turning "for" into a keyword.
883 Result.setKind(II->getTokenID());
Mike Stump11289f42009-09-09 15:08:12 +0000884
Chris Lattnerc5a00062006-06-18 16:41:01 +0000885 // Finally, now that we know we have an identifier, pass this off to the
886 // preprocessor, which may macro expand it or something.
Chris Lattner8256b972009-01-21 07:45:14 +0000887 if (II->isHandleIdentifierCase())
Chris Lattnerad89ec02009-01-21 07:43:11 +0000888 PP->HandleIdentifier(Result);
889 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000890 }
Mike Stump11289f42009-09-09 15:08:12 +0000891
Chris Lattner22eb9722006-06-18 05:43:12 +0000892 // Otherwise, $,\,? in identifier found. Enter slower path.
Mike Stump11289f42009-09-09 15:08:12 +0000893
Chris Lattner22eb9722006-06-18 05:43:12 +0000894 C = getCharAndSize(CurPtr, Size);
895 while (1) {
896 if (C == '$') {
897 // If we hit a $ and they are not supported in identifiers, we are done.
898 if (!Features.DollarIdents) goto FinishIdentifier;
Mike Stump11289f42009-09-09 15:08:12 +0000899
Chris Lattner22eb9722006-06-18 05:43:12 +0000900 // Otherwise, emit a diagnostic and continue.
Chris Lattner6d27a162008-11-22 02:02:22 +0000901 if (!isLexingRawMode())
902 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000903 CurPtr = ConsumeChar(CurPtr, Size, Result);
904 C = getCharAndSize(CurPtr, Size);
905 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000906 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000907 // Found end of identifier.
908 goto FinishIdentifier;
909 }
910
911 // Otherwise, this character is good, consume it.
912 CurPtr = ConsumeChar(CurPtr, Size, Result);
913
914 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000915 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000916 CurPtr = ConsumeChar(CurPtr, Size, Result);
917 C = getCharAndSize(CurPtr, Size);
918 }
919 }
920}
921
922
Nate Begeman5eee9332008-04-14 02:26:39 +0000923/// LexNumericConstant - Lex the remainder of a integer or floating point
Chris Lattner22eb9722006-06-18 05:43:12 +0000924/// constant. From[-1] is the first character lexed. Return the end of the
925/// constant.
Chris Lattner146762e2007-07-20 16:59:19 +0000926void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000927 unsigned Size;
928 char C = getCharAndSize(CurPtr, Size);
929 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000930 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000931 CurPtr = ConsumeChar(CurPtr, Size, Result);
932 PrevCh = C;
933 C = getCharAndSize(CurPtr, Size);
934 }
Mike Stump11289f42009-09-09 15:08:12 +0000935
Chris Lattner22eb9722006-06-18 05:43:12 +0000936 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
937 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
938 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
939
940 // If we have a hex FP constant, continue.
Alexis Hunt91b78382010-01-10 23:37:56 +0000941 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
942 (!PP || !PP->getLangOptions().CPlusPlus0x))
Chris Lattner22eb9722006-06-18 05:43:12 +0000943 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
Mike Stump11289f42009-09-09 15:08:12 +0000944
Chris Lattnerd01e2912006-06-18 16:22:51 +0000945 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000946 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000947 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000948 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000949}
950
951/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
952/// either " or L".
Chris Lattner4d963442008-10-12 04:05:48 +0000953void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000954 const char *NulCharacter = 0; // Does this string contain the \0 character?
Mike Stump11289f42009-09-09 15:08:12 +0000955
Chris Lattner22eb9722006-06-18 05:43:12 +0000956 char C = getAndAdvanceChar(CurPtr, Result);
957 while (C != '"') {
Chris Lattner52d96ac2010-05-30 23:27:38 +0000958 // Skip escaped characters. Escaped newlines will already be processed by
959 // getAndAdvanceChar.
960 if (C == '\\')
Chris Lattner22eb9722006-06-18 05:43:12 +0000961 C = getAndAdvanceChar(CurPtr, Result);
Douglas Gregorfe4a4102010-05-30 22:59:50 +0000962
Chris Lattner52d96ac2010-05-30 23:27:38 +0000963 if (C == '\n' || C == '\r' || // Newline.
Douglas Gregorfe4a4102010-05-30 22:59:50 +0000964 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Douglas Gregor02690ba2010-08-12 17:04:55 +0000965 if (!isLexingRawMode() && !Features.AsmPreprocessor &&
966 !PP->isCodeCompletionFile(FileLoc))
Chris Lattner6d27a162008-11-22 02:02:22 +0000967 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattnerb11c3232008-10-12 04:51:35 +0000968 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000969 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000970 }
Chris Lattner52d96ac2010-05-30 23:27:38 +0000971
972 if (C == 0)
973 NulCharacter = CurPtr-1;
Chris Lattner22eb9722006-06-18 05:43:12 +0000974 C = getAndAdvanceChar(CurPtr, Result);
975 }
Mike Stump11289f42009-09-09 15:08:12 +0000976
Chris Lattner5a78a022006-07-20 06:02:19 +0000977 // If a nul character existed in the string, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +0000978 if (NulCharacter && !isLexingRawMode())
979 Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000980
Chris Lattnerd01e2912006-06-18 16:22:51 +0000981 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000982 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000983 FormTokenWithChars(Result, CurPtr,
984 Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000985 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000986}
987
988/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
989/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattner146762e2007-07-20 16:59:19 +0000990void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000991 const char *NulCharacter = 0; // Does this string contain the \0 character?
Chris Lattnerb40289b2009-04-17 23:56:52 +0000992 const char *AfterLessPos = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000993 char C = getAndAdvanceChar(CurPtr, Result);
994 while (C != '>') {
995 // Skip escaped characters.
996 if (C == '\\') {
997 // Skip the escaped character.
998 C = getAndAdvanceChar(CurPtr, Result);
999 } else if (C == '\n' || C == '\r' || // Newline.
1000 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerb40289b2009-04-17 23:56:52 +00001001 // If the filename is unterminated, then it must just be a lone <
1002 // character. Return this as such.
1003 FormTokenWithChars(Result, AfterLessPos, tok::less);
Chris Lattner5a78a022006-07-20 06:02:19 +00001004 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001005 } else if (C == 0) {
1006 NulCharacter = CurPtr-1;
1007 }
1008 C = getAndAdvanceChar(CurPtr, Result);
1009 }
Mike Stump11289f42009-09-09 15:08:12 +00001010
Chris Lattner5a78a022006-07-20 06:02:19 +00001011 // If a nul character existed in the string, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +00001012 if (NulCharacter && !isLexingRawMode())
1013 Diag(NulCharacter, diag::null_in_string);
Mike Stump11289f42009-09-09 15:08:12 +00001014
Chris Lattnerd01e2912006-06-18 16:22:51 +00001015 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +00001016 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001017 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +00001018 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +00001019}
1020
1021
1022/// LexCharConstant - Lex the remainder of a character constant, after having
1023/// lexed either ' or L'.
Chris Lattner146762e2007-07-20 16:59:19 +00001024void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001025 const char *NulCharacter = 0; // Does this character contain the \0 character?
1026
Chris Lattner22eb9722006-06-18 05:43:12 +00001027 char C = getAndAdvanceChar(CurPtr, Result);
1028 if (C == '\'') {
Chris Lattnerd14705b2009-03-18 21:10:12 +00001029 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +00001030 Diag(BufferPtr, diag::err_empty_character);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001031 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +00001032 return;
Chris Lattner86851b82010-07-07 23:24:27 +00001033 }
1034
1035 while (C != '\'') {
1036 // Skip escaped characters.
1037 if (C == '\\') {
1038 // Skip the escaped character.
1039 // FIXME: UCN's
1040 C = getAndAdvanceChar(CurPtr, Result);
1041 } else if (C == '\n' || C == '\r' || // Newline.
1042 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Douglas Gregor02690ba2010-08-12 17:04:55 +00001043 if (!isLexingRawMode() && !Features.AsmPreprocessor &&
1044 !PP->isCodeCompletionFile(FileLoc))
Chris Lattner86851b82010-07-07 23:24:27 +00001045 Diag(BufferPtr, diag::err_unterminated_char);
1046 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
1047 return;
1048 } else if (C == 0) {
1049 NulCharacter = CurPtr-1;
1050 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001051 C = getAndAdvanceChar(CurPtr, Result);
1052 }
Mike Stump11289f42009-09-09 15:08:12 +00001053
Chris Lattner86851b82010-07-07 23:24:27 +00001054 // If a nul character existed in the character, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +00001055 if (NulCharacter && !isLexingRawMode())
1056 Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +00001057
Chris Lattnerd01e2912006-06-18 16:22:51 +00001058 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +00001059 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001060 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Chris Lattner5a7971e2009-01-26 19:29:26 +00001061 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +00001062}
1063
1064/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
1065/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattner4d963442008-10-12 04:05:48 +00001066///
1067/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
1068///
1069bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001070 // Whitespace - Skip it, then return the token after the whitespace.
1071 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
1072 while (1) {
1073 // Skip horizontal whitespace very aggressively.
1074 while (isHorizontalWhitespace(Char))
1075 Char = *++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001076
Daniel Dunbar5c4cc092008-11-25 00:20:22 +00001077 // Otherwise if we have something other than whitespace, we're done.
Chris Lattner22eb9722006-06-18 05:43:12 +00001078 if (Char != '\n' && Char != '\r')
1079 break;
Mike Stump11289f42009-09-09 15:08:12 +00001080
Chris Lattner22eb9722006-06-18 05:43:12 +00001081 if (ParsingPreprocessorDirective) {
1082 // End of preprocessor directive line, let LexTokenInternal handle this.
1083 BufferPtr = CurPtr;
Chris Lattner4d963442008-10-12 04:05:48 +00001084 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001085 }
Mike Stump11289f42009-09-09 15:08:12 +00001086
Chris Lattner22eb9722006-06-18 05:43:12 +00001087 // ok, but handle newline.
1088 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001089 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001090 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001091 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001092 Char = *++CurPtr;
1093 }
1094
1095 // If this isn't immediately after a newline, there is leading space.
1096 char PrevChar = CurPtr[-1];
1097 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattner146762e2007-07-20 16:59:19 +00001098 Result.setFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001099
Chris Lattner4d963442008-10-12 04:05:48 +00001100 // If the client wants us to return whitespace, return it now.
1101 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001102 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4d963442008-10-12 04:05:48 +00001103 return true;
1104 }
Mike Stump11289f42009-09-09 15:08:12 +00001105
Chris Lattner22eb9722006-06-18 05:43:12 +00001106 BufferPtr = CurPtr;
Chris Lattner4d963442008-10-12 04:05:48 +00001107 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001108}
1109
1110// SkipBCPLComment - We have just read the // characters from input. Skip until
1111// we find the newline character thats terminate the comment. Then update
Chris Lattner87d02082010-01-18 22:35:47 +00001112/// BufferPtr and return.
1113///
1114/// If we're in KeepCommentMode or any CommentHandler has inserted
1115/// some tokens, this will store the first token and return true.
Chris Lattner146762e2007-07-20 16:59:19 +00001116bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001117 // If BCPL comments aren't explicitly enabled for this language, emit an
1118 // extension warning.
Chris Lattner6d27a162008-11-22 02:02:22 +00001119 if (!Features.BCPLComment && !isLexingRawMode()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +00001120 Diag(BufferPtr, diag::ext_bcpl_comment);
Mike Stump11289f42009-09-09 15:08:12 +00001121
Chris Lattner22eb9722006-06-18 05:43:12 +00001122 // Mark them enabled so we only emit one warning for this translation
1123 // unit.
1124 Features.BCPLComment = true;
1125 }
Mike Stump11289f42009-09-09 15:08:12 +00001126
Chris Lattner22eb9722006-06-18 05:43:12 +00001127 // Scan over the body of the comment. The common case, when scanning, is that
1128 // the comment contains normal ascii characters with nothing interesting in
1129 // them. As such, optimize for this case with the inner loop.
1130 char C;
1131 do {
1132 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +00001133 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
1134 // If we find a \n character, scan backwards, checking to see if it's an
1135 // escaped newline, like we do for block comments.
Mike Stump11289f42009-09-09 15:08:12 +00001136
Chris Lattner22eb9722006-06-18 05:43:12 +00001137 // Skip over characters in the fast loop.
1138 while (C != 0 && // Potentially EOF.
1139 C != '\\' && // Potentially escaped newline.
1140 C != '?' && // Potentially trigraph.
1141 C != '\n' && C != '\r') // Newline or DOS-style newline.
1142 C = *++CurPtr;
1143
1144 // If this is a newline, we're done.
1145 if (C == '\n' || C == '\r')
1146 break; // Found the newline? Break out!
Mike Stump11289f42009-09-09 15:08:12 +00001147
Chris Lattner22eb9722006-06-18 05:43:12 +00001148 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnere141a9e2008-12-12 07:34:39 +00001149 // properly decode the character. Read it in raw mode to avoid emitting
1150 // diagnostics about things like trigraphs. If we see an escaped newline,
1151 // we'll handle it below.
Chris Lattner22eb9722006-06-18 05:43:12 +00001152 const char *OldPtr = CurPtr;
Chris Lattnere141a9e2008-12-12 07:34:39 +00001153 bool OldRawMode = isLexingRawMode();
1154 LexingRawMode = true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001155 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnere141a9e2008-12-12 07:34:39 +00001156 LexingRawMode = OldRawMode;
Chris Lattnerecdaf402009-04-05 00:26:41 +00001157
1158 // If the char that we finally got was a \n, then we must have had something
1159 // like \<newline><newline>. We don't want to have consumed the second
1160 // newline, we want CurPtr, to end up pointing to it down below.
1161 if (C == '\n' || C == '\r') {
1162 --CurPtr;
1163 C = 'x'; // doesn't matter what this is.
1164 }
Mike Stump11289f42009-09-09 15:08:12 +00001165
Chris Lattner22eb9722006-06-18 05:43:12 +00001166 // If we read multiple characters, and one of those characters was a \r or
Chris Lattnerff591e22007-06-09 06:07:22 +00001167 // \n, then we had an escaped newline within the comment. Emit diagnostic
1168 // unless the next line is also a // comment.
1169 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001170 for (; OldPtr != CurPtr; ++OldPtr)
1171 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnerff591e22007-06-09 06:07:22 +00001172 // Okay, we found a // comment that ends in a newline, if the next
1173 // line is also a // comment, but has spaces, don't emit a diagnostic.
1174 if (isspace(C)) {
1175 const char *ForwardPtr = CurPtr;
1176 while (isspace(*ForwardPtr)) // Skip whitespace.
1177 ++ForwardPtr;
1178 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
1179 break;
1180 }
Mike Stump11289f42009-09-09 15:08:12 +00001181
Chris Lattner6d27a162008-11-22 02:02:22 +00001182 if (!isLexingRawMode())
1183 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Chris Lattnercb283342006-06-18 06:48:37 +00001184 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001185 }
1186 }
Mike Stump11289f42009-09-09 15:08:12 +00001187
Chris Lattner457fc152006-07-29 06:30:25 +00001188 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
Chris Lattner22eb9722006-06-18 05:43:12 +00001189 } while (C != '\n' && C != '\r');
1190
Chris Lattner93ddf802010-02-03 21:06:21 +00001191 // Found but did not consume the newline. Notify comment handlers about the
1192 // comment unless we're in a #if 0 block.
1193 if (PP && !isLexingRawMode() &&
1194 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
1195 getSourceLocation(CurPtr)))) {
Chris Lattner87d02082010-01-18 22:35:47 +00001196 BufferPtr = CurPtr;
1197 return true; // A token has to be returned.
1198 }
Mike Stump11289f42009-09-09 15:08:12 +00001199
Chris Lattner457fc152006-07-29 06:30:25 +00001200 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +00001201 if (inKeepCommentMode())
Chris Lattner457fc152006-07-29 06:30:25 +00001202 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001203
1204 // If we are inside a preprocessor directive and we see the end of line,
1205 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +00001206 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001207 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001208 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001209 }
Mike Stump11289f42009-09-09 15:08:12 +00001210
Chris Lattner22eb9722006-06-18 05:43:12 +00001211 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner87e97ea2008-10-12 00:23:07 +00001212 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattner4d963442008-10-12 04:05:48 +00001213 // contribute to another token), it isn't needed for correctness. Note that
1214 // this is ok even in KeepWhitespaceMode, because we would have returned the
1215 /// comment above in that mode.
Chris Lattner22eb9722006-06-18 05:43:12 +00001216 ++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001217
Chris Lattner22eb9722006-06-18 05:43:12 +00001218 // The next returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001219 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001220 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001221 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001222 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001223 return false;
Chris Lattner457fc152006-07-29 06:30:25 +00001224}
Chris Lattner22eb9722006-06-18 05:43:12 +00001225
Chris Lattner457fc152006-07-29 06:30:25 +00001226/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
1227/// an appropriate way and return it.
Chris Lattner146762e2007-07-20 16:59:19 +00001228bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001229 // If we're not in a preprocessor directive, just return the // comment
1230 // directly.
1231 FormTokenWithChars(Result, CurPtr, tok::comment);
Mike Stump11289f42009-09-09 15:08:12 +00001232
Chris Lattnerb11c3232008-10-12 04:51:35 +00001233 if (!ParsingPreprocessorDirective)
1234 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001235
Chris Lattnerb11c3232008-10-12 04:51:35 +00001236 // If this BCPL-style comment is in a macro definition, transmogrify it into
1237 // a C-style block comment.
Douglas Gregordc970f02010-03-16 22:30:13 +00001238 bool Invalid = false;
1239 std::string Spelling = PP->getSpelling(Result, &Invalid);
1240 if (Invalid)
1241 return true;
1242
Chris Lattnerb11c3232008-10-12 04:51:35 +00001243 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
1244 Spelling[1] = '*'; // Change prefix to "/*".
1245 Spelling += "*/"; // add suffix.
Mike Stump11289f42009-09-09 15:08:12 +00001246
Chris Lattnerb11c3232008-10-12 04:51:35 +00001247 Result.setKind(tok::comment);
Chris Lattner5a7971e2009-01-26 19:29:26 +00001248 PP->CreateString(&Spelling[0], Spelling.size(), Result,
1249 Result.getLocation());
Chris Lattnere01e7582008-10-12 04:15:42 +00001250 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001251}
1252
Chris Lattnercb283342006-06-18 06:48:37 +00001253/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
1254/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner89770572008-12-12 07:14:34 +00001255/// diagnostic if so. We know that the newline is inside of a block comment.
Mike Stump11289f42009-09-09 15:08:12 +00001256static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
Chris Lattner1f583052006-06-18 06:53:56 +00001257 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001258 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Mike Stump11289f42009-09-09 15:08:12 +00001259
Chris Lattner22eb9722006-06-18 05:43:12 +00001260 // Back up off the newline.
1261 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001262
Chris Lattner22eb9722006-06-18 05:43:12 +00001263 // If this is a two-character newline sequence, skip the other character.
1264 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
1265 // \n\n or \r\r -> not escaped newline.
1266 if (CurPtr[0] == CurPtr[1])
1267 return false;
1268 // \n\r or \r\n -> skip the newline.
1269 --CurPtr;
1270 }
Mike Stump11289f42009-09-09 15:08:12 +00001271
Chris Lattner22eb9722006-06-18 05:43:12 +00001272 // If we have horizontal whitespace, skip over it. We allow whitespace
1273 // between the slash and newline.
1274 bool HasSpace = false;
1275 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
1276 --CurPtr;
1277 HasSpace = true;
1278 }
Mike Stump11289f42009-09-09 15:08:12 +00001279
Chris Lattner22eb9722006-06-18 05:43:12 +00001280 // If we have a slash, we know this is an escaped newline.
1281 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +00001282 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001283 } else {
1284 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +00001285 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
1286 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +00001287 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001288
Chris Lattnercb283342006-06-18 06:48:37 +00001289 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +00001290 CurPtr -= 2;
1291
1292 // If no trigraphs are enabled, warn that we ignored this trigraph and
1293 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +00001294 if (!L->getFeatures().Trigraphs) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001295 if (!L->isLexingRawMode())
1296 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +00001297 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001298 }
Chris Lattner6d27a162008-11-22 02:02:22 +00001299 if (!L->isLexingRawMode())
1300 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001301 }
Mike Stump11289f42009-09-09 15:08:12 +00001302
Chris Lattner22eb9722006-06-18 05:43:12 +00001303 // Warn about having an escaped newline between the */ characters.
Chris Lattner6d27a162008-11-22 02:02:22 +00001304 if (!L->isLexingRawMode())
1305 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Mike Stump11289f42009-09-09 15:08:12 +00001306
Chris Lattner22eb9722006-06-18 05:43:12 +00001307 // If there was space between the backslash and newline, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +00001308 if (HasSpace && !L->isLexingRawMode())
1309 L->Diag(CurPtr, diag::backslash_newline_space);
Mike Stump11289f42009-09-09 15:08:12 +00001310
Chris Lattnercb283342006-06-18 06:48:37 +00001311 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001312}
1313
Chris Lattneraded4a92006-10-27 04:42:31 +00001314#ifdef __SSE2__
1315#include <emmintrin.h>
Chris Lattner9f6604f2006-10-30 20:01:22 +00001316#elif __ALTIVEC__
1317#include <altivec.h>
1318#undef bool
Chris Lattneraded4a92006-10-27 04:42:31 +00001319#endif
1320
Chris Lattner22eb9722006-06-18 05:43:12 +00001321/// SkipBlockComment - We have just read the /* characters from input. Read
1322/// until we find the */ characters that terminate the comment. Note that we
1323/// don't bother decoding trigraphs or escaped newlines in block comments,
1324/// because they cannot cause the comment to end. The only thing that can
1325/// happen is the comment could end with an escaped newline between the */ end
1326/// of comment.
Chris Lattnere01e7582008-10-12 04:15:42 +00001327///
Chris Lattner87d02082010-01-18 22:35:47 +00001328/// If we're in KeepCommentMode or any CommentHandler has inserted
1329/// some tokens, this will store the first token and return true.
Chris Lattner146762e2007-07-20 16:59:19 +00001330bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001331 // Scan one character past where we should, looking for a '/' character. Once
1332 // we find it, check to see if it was preceeded by a *. This common
1333 // optimization helps people who like to put a lot of * characters in their
1334 // comments.
Chris Lattnerc850ad62007-07-21 23:43:37 +00001335
1336 // The first character we get with newlines and trigraphs skipped to handle
1337 // the degenerate /*/ case below correctly if the * has an escaped newline
1338 // after it.
1339 unsigned CharSize;
1340 unsigned char C = getCharAndSize(CurPtr, CharSize);
1341 CurPtr += CharSize;
Chris Lattner22eb9722006-06-18 05:43:12 +00001342 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner561aabd2010-05-16 19:54:05 +00001343 if (!isLexingRawMode() &&
1344 !PP->isCodeCompletionFile(FileLoc))
Chris Lattner7c2e9802008-10-12 01:31:51 +00001345 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner99e7d232008-10-12 04:19:49 +00001346 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001347
Chris Lattner99e7d232008-10-12 04:19:49 +00001348 // KeepWhitespaceMode should return this broken comment as a token. Since
1349 // it isn't a well formed comment, just return it as an 'unknown' token.
1350 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001351 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner99e7d232008-10-12 04:19:49 +00001352 return true;
1353 }
Mike Stump11289f42009-09-09 15:08:12 +00001354
Chris Lattner99e7d232008-10-12 04:19:49 +00001355 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001356 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001357 }
Mike Stump11289f42009-09-09 15:08:12 +00001358
Chris Lattnerc850ad62007-07-21 23:43:37 +00001359 // Check to see if the first character after the '/*' is another /. If so,
1360 // then this slash does not end the block comment, it is part of it.
1361 if (C == '/')
1362 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001363
Chris Lattner22eb9722006-06-18 05:43:12 +00001364 while (1) {
Chris Lattner6cc3e362006-10-27 04:12:35 +00001365 // Skip over all non-interesting characters until we find end of buffer or a
1366 // (probably ending) '/' character.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001367 if (CurPtr + 24 < BufferEnd) {
1368 // While not aligned to a 16-byte boundary.
1369 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1370 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001371
Chris Lattner6cc3e362006-10-27 04:12:35 +00001372 if (C == '/') goto FoundSlash;
Chris Lattneraded4a92006-10-27 04:42:31 +00001373
1374#ifdef __SSE2__
1375 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1376 '/', '/', '/', '/', '/', '/', '/', '/');
1377 while (CurPtr+16 <= BufferEnd &&
1378 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1379 CurPtr += 16;
Chris Lattner9f6604f2006-10-30 20:01:22 +00001380#elif __ALTIVEC__
1381 __vector unsigned char Slashes = {
Mike Stump11289f42009-09-09 15:08:12 +00001382 '/', '/', '/', '/', '/', '/', '/', '/',
Chris Lattner9f6604f2006-10-30 20:01:22 +00001383 '/', '/', '/', '/', '/', '/', '/', '/'
1384 };
1385 while (CurPtr+16 <= BufferEnd &&
1386 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1387 CurPtr += 16;
Mike Stump11289f42009-09-09 15:08:12 +00001388#else
Chris Lattneraded4a92006-10-27 04:42:31 +00001389 // Scan for '/' quickly. Many block comments are very large.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001390 while (CurPtr[0] != '/' &&
1391 CurPtr[1] != '/' &&
1392 CurPtr[2] != '/' &&
1393 CurPtr[3] != '/' &&
1394 CurPtr+4 < BufferEnd) {
1395 CurPtr += 4;
1396 }
Chris Lattneraded4a92006-10-27 04:42:31 +00001397#endif
Mike Stump11289f42009-09-09 15:08:12 +00001398
Chris Lattneraded4a92006-10-27 04:42:31 +00001399 // It has to be one of the bytes scanned, increment to it and read one.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001400 C = *CurPtr++;
1401 }
Mike Stump11289f42009-09-09 15:08:12 +00001402
Chris Lattneraded4a92006-10-27 04:42:31 +00001403 // Loop to scan the remainder.
Chris Lattner22eb9722006-06-18 05:43:12 +00001404 while (C != '/' && C != '\0')
1405 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001406
Chris Lattner6cc3e362006-10-27 04:12:35 +00001407 FoundSlash:
Chris Lattner22eb9722006-06-18 05:43:12 +00001408 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001409 if (CurPtr[-2] == '*') // We found the final */. We're done!
1410 break;
Mike Stump11289f42009-09-09 15:08:12 +00001411
Chris Lattner22eb9722006-06-18 05:43:12 +00001412 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +00001413 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001414 // We found the final */, though it had an escaped newline between the
1415 // * and /. We're done!
1416 break;
1417 }
1418 }
1419 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1420 // If this is a /* inside of the comment, emit a warning. Don't do this
1421 // if this is a /*/, which will end the comment. This misses cases with
1422 // embedded escaped newlines, but oh well.
Chris Lattner6d27a162008-11-22 02:02:22 +00001423 if (!isLexingRawMode())
1424 Diag(CurPtr-1, diag::warn_nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001425 }
1426 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner561aabd2010-05-16 19:54:05 +00001427 if (!isLexingRawMode() && !PP->isCodeCompletionFile(FileLoc))
Chris Lattner6d27a162008-11-22 02:02:22 +00001428 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001429 // Note: the user probably forgot a */. We could continue immediately
1430 // after the /*, but this would involve lexing a lot of what really is the
1431 // comment, which surely would confuse the parser.
Chris Lattner99e7d232008-10-12 04:19:49 +00001432 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001433
Chris Lattner99e7d232008-10-12 04:19:49 +00001434 // KeepWhitespaceMode should return this broken comment as a token. Since
1435 // it isn't a well formed comment, just return it as an 'unknown' token.
1436 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001437 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner99e7d232008-10-12 04:19:49 +00001438 return true;
1439 }
Mike Stump11289f42009-09-09 15:08:12 +00001440
Chris Lattner99e7d232008-10-12 04:19:49 +00001441 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001442 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001443 }
1444 C = *CurPtr++;
1445 }
Mike Stump11289f42009-09-09 15:08:12 +00001446
Chris Lattner93ddf802010-02-03 21:06:21 +00001447 // Notify comment handlers about the comment unless we're in a #if 0 block.
1448 if (PP && !isLexingRawMode() &&
1449 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
1450 getSourceLocation(CurPtr)))) {
Chris Lattner87d02082010-01-18 22:35:47 +00001451 BufferPtr = CurPtr;
1452 return true; // A token has to be returned.
1453 }
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001454
Chris Lattner457fc152006-07-29 06:30:25 +00001455 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +00001456 if (inKeepCommentMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001457 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattnere01e7582008-10-12 04:15:42 +00001458 return true;
Chris Lattner457fc152006-07-29 06:30:25 +00001459 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001460
1461 // It is common for the tokens immediately after a /**/ comment to be
1462 // whitespace. Instead of going through the big switch, handle it
Chris Lattner4d963442008-10-12 04:05:48 +00001463 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1464 // have already returned above with the comment as a token.
Chris Lattner22eb9722006-06-18 05:43:12 +00001465 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner146762e2007-07-20 16:59:19 +00001466 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +00001467 SkipWhitespace(Result, CurPtr+1);
Chris Lattnere01e7582008-10-12 04:15:42 +00001468 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001469 }
1470
1471 // Otherwise, just return so that the next character will be lexed as a token.
1472 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001473 Result.setFlag(Token::LeadingSpace);
Chris Lattnere01e7582008-10-12 04:15:42 +00001474 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001475}
1476
1477//===----------------------------------------------------------------------===//
1478// Primary Lexing Entry Points
1479//===----------------------------------------------------------------------===//
1480
Chris Lattner22eb9722006-06-18 05:43:12 +00001481/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1482/// uninterpreted string. This switches the lexer out of directive mode.
1483std::string Lexer::ReadToEndOfLine() {
1484 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1485 "Must be in a preprocessing directive!");
1486 std::string Result;
Chris Lattner146762e2007-07-20 16:59:19 +00001487 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001488
1489 // CurPtr - Cache BufferPtr in an automatic variable.
1490 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001491 while (1) {
1492 char Char = getAndAdvanceChar(CurPtr, Tmp);
1493 switch (Char) {
1494 default:
1495 Result += Char;
1496 break;
1497 case 0: // Null.
1498 // Found end of file?
1499 if (CurPtr-1 != BufferEnd) {
1500 // Nope, normal character, continue.
1501 Result += Char;
1502 break;
1503 }
1504 // FALL THROUGH.
1505 case '\r':
1506 case '\n':
1507 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1508 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1509 BufferPtr = CurPtr-1;
Mike Stump11289f42009-09-09 15:08:12 +00001510
Chris Lattner22eb9722006-06-18 05:43:12 +00001511 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +00001512 Lex(Tmp);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001513 assert(Tmp.is(tok::eom) && "Unexpected token!");
Mike Stump11289f42009-09-09 15:08:12 +00001514
Chris Lattner22eb9722006-06-18 05:43:12 +00001515 // Finally, we're done, return the string we found.
1516 return Result;
1517 }
1518 }
1519}
1520
1521/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1522/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +00001523/// This returns true if Result contains a token, false if PP.Lex should be
1524/// called again.
Chris Lattner146762e2007-07-20 16:59:19 +00001525bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Douglas Gregor3a7ad252010-08-24 19:08:16 +00001526 // Check if we are performing code completion.
1527 if (PP && PP->isCodeCompletionFile(FileLoc)) {
1528 // We're at the end of the file, but we've been asked to consider the
1529 // end of the file to be a code-completion token. Return the
1530 // code-completion token.
1531 Result.startToken();
1532 FormTokenWithChars(Result, CurPtr, tok::code_completion);
1533
1534 // Only do the eof -> code_completion translation once.
1535 PP->SetCodeCompletionPoint(0, 0, 0);
1536
1537 // Silence any diagnostics that occur once we hit the code-completion point.
1538 PP->getDiagnostics().setSuppressAllDiagnostics(true);
1539 return true;
1540 }
1541
Chris Lattner22eb9722006-06-18 05:43:12 +00001542 // If we hit the end of the file while parsing a preprocessor directive,
1543 // end the preprocessor directive first. The next token returned will
1544 // then be the end of file.
1545 if (ParsingPreprocessorDirective) {
1546 // Done parsing the "line".
1547 ParsingPreprocessorDirective = false;
Chris Lattnerd01e2912006-06-18 16:22:51 +00001548 // Update the location of token as well as BufferPtr.
Chris Lattnerb11c3232008-10-12 04:51:35 +00001549 FormTokenWithChars(Result, CurPtr, tok::eom);
Mike Stump11289f42009-09-09 15:08:12 +00001550
Chris Lattner457fc152006-07-29 06:30:25 +00001551 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001552 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner2183a6e2006-07-18 06:36:12 +00001553 return true; // Have a token.
Mike Stump11289f42009-09-09 15:08:12 +00001554 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00001555
Chris Lattner30a2fa12006-07-19 06:31:49 +00001556 // If we are in raw mode, return this event as an EOF token. Let the caller
1557 // that put us in raw mode handle the event.
Chris Lattner6d27a162008-11-22 02:02:22 +00001558 if (isLexingRawMode()) {
Chris Lattner8c204872006-10-14 05:19:21 +00001559 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +00001560 BufferPtr = BufferEnd;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001561 FormTokenWithChars(Result, BufferEnd, tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001562 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001563 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00001564
Douglas Gregor3a7ad252010-08-24 19:08:16 +00001565 // Issue diagnostics for unterminated #if and missing newline.
1566
Chris Lattner30a2fa12006-07-19 06:31:49 +00001567 // If we are in a #if directive, emit an error.
1568 while (!ConditionalStack.empty()) {
Douglas Gregor02690ba2010-08-12 17:04:55 +00001569 if (!PP->isCodeCompletionFile(FileLoc))
1570 PP->Diag(ConditionalStack.back().IfLoc,
1571 diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001572 ConditionalStack.pop_back();
1573 }
Mike Stump11289f42009-09-09 15:08:12 +00001574
Chris Lattner8f96d042008-04-12 05:54:25 +00001575 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1576 // a pedwarn.
1577 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Mike Stump0be88752009-04-02 02:29:42 +00001578 Diag(BufferEnd, diag::ext_no_newline_eof)
Douglas Gregora771f462010-03-31 17:46:05 +00001579 << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n");
Mike Stump11289f42009-09-09 15:08:12 +00001580
Chris Lattner22eb9722006-06-18 05:43:12 +00001581 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +00001582
1583 // Finally, let the preprocessor handle this.
Chris Lattner02b436a2007-10-17 20:41:00 +00001584 return PP->HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001585}
1586
Chris Lattner678c8802006-07-11 05:46:12 +00001587/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1588/// the specified lexer will return a tok::l_paren token, 0 if it is something
1589/// else and 2 if there are no more tokens in the buffer controlled by the
1590/// lexer.
1591unsigned Lexer::isNextPPTokenLParen() {
1592 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
Mike Stump11289f42009-09-09 15:08:12 +00001593
Chris Lattner678c8802006-07-11 05:46:12 +00001594 // Switch to 'skipping' mode. This will ensure that we can lex a token
1595 // without emitting diagnostics, disables macro expansion, and will cause EOF
1596 // to return an EOF token instead of popping the include stack.
1597 LexingRawMode = true;
Mike Stump11289f42009-09-09 15:08:12 +00001598
Chris Lattner678c8802006-07-11 05:46:12 +00001599 // Save state that can be changed while lexing so that we can restore it.
1600 const char *TmpBufferPtr = BufferPtr;
Chris Lattner40493eb2009-04-24 07:15:46 +00001601 bool inPPDirectiveMode = ParsingPreprocessorDirective;
Mike Stump11289f42009-09-09 15:08:12 +00001602
Chris Lattner146762e2007-07-20 16:59:19 +00001603 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001604 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001605 LexTokenInternal(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001606
Chris Lattner678c8802006-07-11 05:46:12 +00001607 // Restore state that may have changed.
1608 BufferPtr = TmpBufferPtr;
Chris Lattner40493eb2009-04-24 07:15:46 +00001609 ParsingPreprocessorDirective = inPPDirectiveMode;
Mike Stump11289f42009-09-09 15:08:12 +00001610
Chris Lattner678c8802006-07-11 05:46:12 +00001611 // Restore the lexer back to non-skipping mode.
1612 LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +00001613
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001614 if (Tok.is(tok::eof))
Chris Lattner678c8802006-07-11 05:46:12 +00001615 return 2;
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001616 return Tok.is(tok::l_paren);
Chris Lattner678c8802006-07-11 05:46:12 +00001617}
1618
Chris Lattner7c027ee2009-12-14 06:16:57 +00001619/// FindConflictEnd - Find the end of a version control conflict marker.
1620static const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd) {
1621 llvm::StringRef RestOfBuffer(CurPtr+7, BufferEnd-CurPtr-7);
1622 size_t Pos = RestOfBuffer.find(">>>>>>>");
1623 while (Pos != llvm::StringRef::npos) {
1624 // Must occur at start of line.
1625 if (RestOfBuffer[Pos-1] != '\r' &&
1626 RestOfBuffer[Pos-1] != '\n') {
1627 RestOfBuffer = RestOfBuffer.substr(Pos+7);
Chris Lattner467f6bc2010-05-17 20:27:25 +00001628 Pos = RestOfBuffer.find(">>>>>>>");
Chris Lattner7c027ee2009-12-14 06:16:57 +00001629 continue;
1630 }
1631 return RestOfBuffer.data()+Pos;
1632 }
1633 return 0;
1634}
1635
1636/// IsStartOfConflictMarker - If the specified pointer is the start of a version
1637/// control conflict marker like '<<<<<<<', recognize it as such, emit an error
1638/// and recover nicely. This returns true if it is a conflict marker and false
1639/// if not.
1640bool Lexer::IsStartOfConflictMarker(const char *CurPtr) {
1641 // Only a conflict marker if it starts at the beginning of a line.
1642 if (CurPtr != BufferStart &&
1643 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1644 return false;
1645
1646 // Check to see if we have <<<<<<<.
1647 if (BufferEnd-CurPtr < 8 ||
1648 llvm::StringRef(CurPtr, 7) != "<<<<<<<")
1649 return false;
1650
1651 // If we have a situation where we don't care about conflict markers, ignore
1652 // it.
1653 if (IsInConflictMarker || isLexingRawMode())
1654 return false;
1655
1656 // Check to see if there is a >>>>>>> somewhere in the buffer at the start of
1657 // a line to terminate this conflict marker.
Chris Lattner467f6bc2010-05-17 20:27:25 +00001658 if (FindConflictEnd(CurPtr, BufferEnd)) {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001659 // We found a match. We are really in a conflict marker.
1660 // Diagnose this, and ignore to the end of line.
1661 Diag(CurPtr, diag::err_conflict_marker);
1662 IsInConflictMarker = true;
1663
1664 // Skip ahead to the end of line. We know this exists because the
1665 // end-of-conflict marker starts with \r or \n.
1666 while (*CurPtr != '\r' && *CurPtr != '\n') {
1667 assert(CurPtr != BufferEnd && "Didn't find end of line");
1668 ++CurPtr;
1669 }
1670 BufferPtr = CurPtr;
1671 return true;
1672 }
1673
1674 // No end of conflict marker found.
1675 return false;
1676}
1677
1678
1679/// HandleEndOfConflictMarker - If this is a '=======' or '|||||||' or '>>>>>>>'
1680/// marker, then it is the end of a conflict marker. Handle it by ignoring up
1681/// until the end of the line. This returns true if it is a conflict marker and
1682/// false if not.
1683bool Lexer::HandleEndOfConflictMarker(const char *CurPtr) {
1684 // Only a conflict marker if it starts at the beginning of a line.
1685 if (CurPtr != BufferStart &&
1686 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1687 return false;
1688
1689 // If we have a situation where we don't care about conflict markers, ignore
1690 // it.
1691 if (!IsInConflictMarker || isLexingRawMode())
1692 return false;
1693
1694 // Check to see if we have the marker (7 characters in a row).
1695 for (unsigned i = 1; i != 7; ++i)
1696 if (CurPtr[i] != CurPtr[0])
1697 return false;
1698
1699 // If we do have it, search for the end of the conflict marker. This could
1700 // fail if it got skipped with a '#if 0' or something. Note that CurPtr might
1701 // be the end of conflict marker.
1702 if (const char *End = FindConflictEnd(CurPtr, BufferEnd)) {
1703 CurPtr = End;
1704
1705 // Skip ahead to the end of line.
1706 while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n')
1707 ++CurPtr;
1708
1709 BufferPtr = CurPtr;
1710
1711 // No longer in the conflict marker.
1712 IsInConflictMarker = false;
1713 return true;
1714 }
1715
1716 return false;
1717}
1718
Chris Lattner22eb9722006-06-18 05:43:12 +00001719
1720/// LexTokenInternal - This implements a simple C family lexer. It is an
1721/// extremely performance critical piece of code. This assumes that the buffer
Chris Lattner5c349382009-07-07 05:05:42 +00001722/// has a null character at the end of the file. This returns a preprocessing
1723/// token, not a normal token, as such, it is an internal interface. It assumes
1724/// that the Flags of result have been cleared before calling this.
Chris Lattner146762e2007-07-20 16:59:19 +00001725void Lexer::LexTokenInternal(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001726LexNextToken:
1727 // New token, can't need cleaning yet.
Chris Lattner146762e2007-07-20 16:59:19 +00001728 Result.clearFlag(Token::NeedsCleaning);
Chris Lattner8c204872006-10-14 05:19:21 +00001729 Result.setIdentifierInfo(0);
Mike Stump11289f42009-09-09 15:08:12 +00001730
Chris Lattner22eb9722006-06-18 05:43:12 +00001731 // CurPtr - Cache BufferPtr in an automatic variable.
1732 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001733
Chris Lattnereb54b592006-07-10 06:34:27 +00001734 // Small amounts of horizontal whitespace is very common between tokens.
1735 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1736 ++CurPtr;
1737 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1738 ++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001739
Chris Lattner4d963442008-10-12 04:05:48 +00001740 // If we are keeping whitespace and other tokens, just return what we just
1741 // skipped. The next lexer invocation will return the token after the
1742 // whitespace.
1743 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001744 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4d963442008-10-12 04:05:48 +00001745 return;
1746 }
Mike Stump11289f42009-09-09 15:08:12 +00001747
Chris Lattnereb54b592006-07-10 06:34:27 +00001748 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001749 Result.setFlag(Token::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001750 }
Mike Stump11289f42009-09-09 15:08:12 +00001751
Chris Lattner22eb9722006-06-18 05:43:12 +00001752 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
Mike Stump11289f42009-09-09 15:08:12 +00001753
Chris Lattner22eb9722006-06-18 05:43:12 +00001754 // Read a character, advancing over it.
1755 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001756 tok::TokenKind Kind;
Mike Stump11289f42009-09-09 15:08:12 +00001757
Chris Lattner22eb9722006-06-18 05:43:12 +00001758 switch (Char) {
1759 case 0: // Null.
1760 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001761 if (CurPtr-1 == BufferEnd) {
1762 // Read the PP instance variable into an automatic variable, because
1763 // LexEndOfFile will often delete 'this'.
Chris Lattner02b436a2007-10-17 20:41:00 +00001764 Preprocessor *PPCache = PP;
Chris Lattner2183a6e2006-07-18 06:36:12 +00001765 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1766 return; // Got a token to return.
Chris Lattner02b436a2007-10-17 20:41:00 +00001767 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1768 return PPCache->Lex(Result);
Chris Lattner2183a6e2006-07-18 06:36:12 +00001769 }
Mike Stump11289f42009-09-09 15:08:12 +00001770
Chris Lattner6d27a162008-11-22 02:02:22 +00001771 if (!isLexingRawMode())
1772 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner146762e2007-07-20 16:59:19 +00001773 Result.setFlag(Token::LeadingSpace);
Chris Lattner4d963442008-10-12 04:05:48 +00001774 if (SkipWhitespace(Result, CurPtr))
1775 return; // KeepWhitespaceMode
Mike Stump11289f42009-09-09 15:08:12 +00001776
Chris Lattner22eb9722006-06-18 05:43:12 +00001777 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner3dfff972009-12-17 05:29:40 +00001778
1779 case 26: // DOS & CP/M EOF: "^Z".
1780 // If we're in Microsoft extensions mode, treat this as end of file.
1781 if (Features.Microsoft) {
1782 // Read the PP instance variable into an automatic variable, because
1783 // LexEndOfFile will often delete 'this'.
1784 Preprocessor *PPCache = PP;
1785 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1786 return; // Got a token to return.
1787 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1788 return PPCache->Lex(Result);
1789 }
1790 // If Microsoft extensions are disabled, this is just random garbage.
1791 Kind = tok::unknown;
1792 break;
1793
Chris Lattner22eb9722006-06-18 05:43:12 +00001794 case '\n':
1795 case '\r':
1796 // If we are inside a preprocessor directive and we see the end of line,
1797 // we know we are done with the directive, so return an EOM token.
1798 if (ParsingPreprocessorDirective) {
1799 // Done parsing the "line".
1800 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +00001801
Chris Lattner457fc152006-07-29 06:30:25 +00001802 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001803 SetCommentRetentionState(PP->getCommentRetentionState());
Mike Stump11289f42009-09-09 15:08:12 +00001804
Chris Lattner22eb9722006-06-18 05:43:12 +00001805 // Since we consumed a newline, we are back at the start of a line.
1806 IsAtStartOfLine = true;
Mike Stump11289f42009-09-09 15:08:12 +00001807
Chris Lattnerb11c3232008-10-12 04:51:35 +00001808 Kind = tok::eom;
Chris Lattner22eb9722006-06-18 05:43:12 +00001809 break;
1810 }
1811 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001812 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001813 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001814 Result.clearFlag(Token::LeadingSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001815
Chris Lattner4d963442008-10-12 04:05:48 +00001816 if (SkipWhitespace(Result, CurPtr))
1817 return; // KeepWhitespaceMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001818 goto LexNextToken; // GCC isn't tail call eliminating.
1819 case ' ':
1820 case '\t':
1821 case '\f':
1822 case '\v':
Chris Lattnerb9b85972007-07-22 06:29:05 +00001823 SkipHorizontalWhitespace:
Chris Lattner146762e2007-07-20 16:59:19 +00001824 Result.setFlag(Token::LeadingSpace);
Chris Lattner4d963442008-10-12 04:05:48 +00001825 if (SkipWhitespace(Result, CurPtr))
1826 return; // KeepWhitespaceMode
Chris Lattnerb9b85972007-07-22 06:29:05 +00001827
1828 SkipIgnoredUnits:
1829 CurPtr = BufferPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001830
Chris Lattnerb9b85972007-07-22 06:29:05 +00001831 // If the next token is obviously a // or /* */ comment, skip it efficiently
1832 // too (without going through the big switch stmt).
Chris Lattner58827712009-01-16 22:39:25 +00001833 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1834 Features.BCPLComment) {
Chris Lattner87d02082010-01-18 22:35:47 +00001835 if (SkipBCPLComment(Result, CurPtr+2))
1836 return; // There is a token to return.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001837 goto SkipIgnoredUnits;
Chris Lattner8637abd2008-10-12 03:22:02 +00001838 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner87d02082010-01-18 22:35:47 +00001839 if (SkipBlockComment(Result, CurPtr+2))
1840 return; // There is a token to return.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001841 goto SkipIgnoredUnits;
1842 } else if (isHorizontalWhitespace(*CurPtr)) {
1843 goto SkipHorizontalWhitespace;
1844 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001845 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner3dfff972009-12-17 05:29:40 +00001846
Chris Lattner2b15cf72008-01-03 17:58:54 +00001847 // C99 6.4.4.1: Integer Constants.
1848 // C99 6.4.4.2: Floating Constants.
1849 case '0': case '1': case '2': case '3': case '4':
1850 case '5': case '6': case '7': case '8': case '9':
1851 // Notify MIOpt that we read a non-whitespace/non-comment token.
1852 MIOpt.ReadToken();
1853 return LexNumericConstant(Result, CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001854
Chris Lattner2b15cf72008-01-03 17:58:54 +00001855 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Chris Lattner371ac8a2006-07-04 07:11:10 +00001856 // Notify MIOpt that we read a non-whitespace/non-comment token.
1857 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001858 Char = getCharAndSize(CurPtr, SizeTmp);
1859
1860 // Wide string literal.
1861 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001862 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1863 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001864
1865 // Wide character constant.
1866 if (Char == '\'')
1867 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1868 // FALL THROUGH, treating L like the start of an identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001869
Chris Lattner22eb9722006-06-18 05:43:12 +00001870 // C99 6.4.2: Identifiers.
1871 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1872 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1873 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1874 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1875 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1876 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1877 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1878 case 'v': case 'w': case 'x': case 'y': case 'z':
1879 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001880 // Notify MIOpt that we read a non-whitespace/non-comment token.
1881 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001882 return LexIdentifier(Result, CurPtr);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001883
1884 case '$': // $ in identifiers.
1885 if (Features.DollarIdents) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001886 if (!isLexingRawMode())
1887 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001888 // Notify MIOpt that we read a non-whitespace/non-comment token.
1889 MIOpt.ReadToken();
1890 return LexIdentifier(Result, CurPtr);
1891 }
Mike Stump11289f42009-09-09 15:08:12 +00001892
Chris Lattnerb11c3232008-10-12 04:51:35 +00001893 Kind = tok::unknown;
Chris Lattner2b15cf72008-01-03 17:58:54 +00001894 break;
Mike Stump11289f42009-09-09 15:08:12 +00001895
Chris Lattner22eb9722006-06-18 05:43:12 +00001896 // C99 6.4.4: Character Constants.
1897 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001898 // Notify MIOpt that we read a non-whitespace/non-comment token.
1899 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001900 return LexCharConstant(Result, CurPtr);
1901
1902 // C99 6.4.5: String Literals.
1903 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001904 // Notify MIOpt that we read a non-whitespace/non-comment token.
1905 MIOpt.ReadToken();
Chris Lattnerd3e98952006-10-06 05:22:26 +00001906 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001907
1908 // C99 6.4.6: Punctuators.
1909 case '?':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001910 Kind = tok::question;
Chris Lattner22eb9722006-06-18 05:43:12 +00001911 break;
1912 case '[':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001913 Kind = tok::l_square;
Chris Lattner22eb9722006-06-18 05:43:12 +00001914 break;
1915 case ']':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001916 Kind = tok::r_square;
Chris Lattner22eb9722006-06-18 05:43:12 +00001917 break;
1918 case '(':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001919 Kind = tok::l_paren;
Chris Lattner22eb9722006-06-18 05:43:12 +00001920 break;
1921 case ')':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001922 Kind = tok::r_paren;
Chris Lattner22eb9722006-06-18 05:43:12 +00001923 break;
1924 case '{':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001925 Kind = tok::l_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001926 break;
1927 case '}':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001928 Kind = tok::r_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001929 break;
1930 case '.':
1931 Char = getCharAndSize(CurPtr, SizeTmp);
1932 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001933 // Notify MIOpt that we read a non-whitespace/non-comment token.
1934 MIOpt.ReadToken();
1935
Chris Lattner22eb9722006-06-18 05:43:12 +00001936 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1937 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001938 Kind = tok::periodstar;
Chris Lattner22eb9722006-06-18 05:43:12 +00001939 CurPtr += SizeTmp;
1940 } else if (Char == '.' &&
1941 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001942 Kind = tok::ellipsis;
Chris Lattner22eb9722006-06-18 05:43:12 +00001943 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1944 SizeTmp2, Result);
1945 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001946 Kind = tok::period;
Chris Lattner22eb9722006-06-18 05:43:12 +00001947 }
1948 break;
1949 case '&':
1950 Char = getCharAndSize(CurPtr, SizeTmp);
1951 if (Char == '&') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001952 Kind = tok::ampamp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001953 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1954 } else if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001955 Kind = tok::ampequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001956 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1957 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001958 Kind = tok::amp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001959 }
1960 break;
Mike Stump11289f42009-09-09 15:08:12 +00001961 case '*':
Chris Lattner22eb9722006-06-18 05:43:12 +00001962 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001963 Kind = tok::starequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001964 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1965 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001966 Kind = tok::star;
Chris Lattner22eb9722006-06-18 05:43:12 +00001967 }
1968 break;
1969 case '+':
1970 Char = getCharAndSize(CurPtr, SizeTmp);
1971 if (Char == '+') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001972 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001973 Kind = tok::plusplus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001974 } else if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001975 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001976 Kind = tok::plusequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001977 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001978 Kind = tok::plus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001979 }
1980 break;
1981 case '-':
1982 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001983 if (Char == '-') { // --
Chris Lattner22eb9722006-06-18 05:43:12 +00001984 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001985 Kind = tok::minusminus;
Mike Stump11289f42009-09-09 15:08:12 +00001986 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattnerb11c3232008-10-12 04:51:35 +00001987 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00001988 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1989 SizeTmp2, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001990 Kind = tok::arrowstar;
1991 } else if (Char == '>') { // ->
Chris Lattner22eb9722006-06-18 05:43:12 +00001992 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001993 Kind = tok::arrow;
1994 } else if (Char == '=') { // -=
Chris Lattner22eb9722006-06-18 05:43:12 +00001995 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001996 Kind = tok::minusequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001997 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001998 Kind = tok::minus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001999 }
2000 break;
2001 case '~':
Chris Lattnerb11c3232008-10-12 04:51:35 +00002002 Kind = tok::tilde;
Chris Lattner22eb9722006-06-18 05:43:12 +00002003 break;
2004 case '!':
2005 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002006 Kind = tok::exclaimequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002007 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2008 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002009 Kind = tok::exclaim;
Chris Lattner22eb9722006-06-18 05:43:12 +00002010 }
2011 break;
2012 case '/':
2013 // 6.4.9: Comments
2014 Char = getCharAndSize(CurPtr, SizeTmp);
2015 if (Char == '/') { // BCPL comment.
Chris Lattner58827712009-01-16 22:39:25 +00002016 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
2017 // want to lex this as a comment. There is one problem with this though,
2018 // that in one particular corner case, this can change the behavior of the
2019 // resultant program. For example, In "foo //**/ bar", C89 would lex
2020 // this as "foo / bar" and langauges with BCPL comments would lex it as
2021 // "foo". Check to see if the character after the second slash is a '*'.
2022 // If so, we will lex that as a "/" instead of the start of a comment.
2023 if (Features.BCPLComment ||
2024 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
2025 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner87d02082010-01-18 22:35:47 +00002026 return; // There is a token to return.
Mike Stump11289f42009-09-09 15:08:12 +00002027
Chris Lattner58827712009-01-16 22:39:25 +00002028 // It is common for the tokens immediately after a // comment to be
2029 // whitespace (indentation for the next line). Instead of going through
2030 // the big switch, handle it efficiently now.
2031 goto SkipIgnoredUnits;
2032 }
2033 }
Mike Stump11289f42009-09-09 15:08:12 +00002034
Chris Lattner58827712009-01-16 22:39:25 +00002035 if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00002036 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner87d02082010-01-18 22:35:47 +00002037 return; // There is a token to return.
Chris Lattnere01e7582008-10-12 04:15:42 +00002038 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner58827712009-01-16 22:39:25 +00002039 }
Mike Stump11289f42009-09-09 15:08:12 +00002040
Chris Lattner58827712009-01-16 22:39:25 +00002041 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00002042 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002043 Kind = tok::slashequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002044 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002045 Kind = tok::slash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002046 }
2047 break;
2048 case '%':
2049 Char = getCharAndSize(CurPtr, SizeTmp);
2050 if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002051 Kind = tok::percentequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002052 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2053 } else if (Features.Digraphs && Char == '>') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002054 Kind = tok::r_brace; // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00002055 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2056 } else if (Features.Digraphs && Char == ':') {
2057 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00002058 Char = getCharAndSize(CurPtr, SizeTmp);
2059 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002060 Kind = tok::hashhash; // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00002061 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
2062 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00002063 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner2b271db2006-07-15 05:41:09 +00002064 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner6d27a162008-11-22 02:02:22 +00002065 if (!isLexingRawMode())
2066 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002067 Kind = tok::hashat;
Chris Lattner2534324a2009-03-18 20:58:27 +00002068 } else { // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00002069 // We parsed a # character. If this occurs at the start of the line,
2070 // it's actually the start of a preprocessing directive. Callback to
2071 // the preprocessor to handle it.
2072 // FIXME: -fpreprocessed mode??
Chris Lattnerff96dd02009-05-13 06:10:29 +00002073 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattner2534324a2009-03-18 20:58:27 +00002074 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner02b436a2007-10-17 20:41:00 +00002075 PP->HandleDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +00002076
Chris Lattner22eb9722006-06-18 05:43:12 +00002077 // As an optimization, if the preprocessor didn't switch lexers, tail
2078 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00002079 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002080 // Start a new token. If this is a #include or something, the PP may
2081 // want us starting at the beginning of the line again. If so, set
Chris Lattner1a9e8732010-04-12 23:04:41 +00002082 // the StartOfLine flag and clear LeadingSpace.
Chris Lattner22eb9722006-06-18 05:43:12 +00002083 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00002084 Result.setFlag(Token::StartOfLine);
Chris Lattner1a9e8732010-04-12 23:04:41 +00002085 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00002086 IsAtStartOfLine = false;
2087 }
2088 goto LexNextToken; // GCC isn't tail call eliminating.
2089 }
Mike Stump11289f42009-09-09 15:08:12 +00002090
Chris Lattner02b436a2007-10-17 20:41:00 +00002091 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002092 }
Mike Stump11289f42009-09-09 15:08:12 +00002093
Chris Lattner2534324a2009-03-18 20:58:27 +00002094 Kind = tok::hash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002095 }
2096 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002097 Kind = tok::percent;
Chris Lattner22eb9722006-06-18 05:43:12 +00002098 }
2099 break;
2100 case '<':
2101 Char = getCharAndSize(CurPtr, SizeTmp);
2102 if (ParsingFilename) {
Chris Lattnerb40289b2009-04-17 23:56:52 +00002103 return LexAngledStringLiteral(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00002104 } else if (Char == '<') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00002105 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
2106 if (After == '=') {
2107 Kind = tok::lesslessequal;
2108 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
2109 SizeTmp2, Result);
2110 } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) {
2111 // If this is actually a '<<<<<<<' version control conflict marker,
2112 // recognize it as such and recover nicely.
2113 goto LexNextToken;
2114 } else {
2115 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2116 Kind = tok::lessless;
2117 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002118 } else if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00002119 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002120 Kind = tok::lessequal;
2121 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00002122 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002123 Kind = tok::l_square;
2124 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Chris Lattner22eb9722006-06-18 05:43:12 +00002125 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002126 Kind = tok::l_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00002127 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002128 Kind = tok::less;
Chris Lattner22eb9722006-06-18 05:43:12 +00002129 }
2130 break;
2131 case '>':
2132 Char = getCharAndSize(CurPtr, SizeTmp);
2133 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00002134 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002135 Kind = tok::greaterequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002136 } else if (Char == '>') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00002137 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
2138 if (After == '=') {
2139 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
2140 SizeTmp2, Result);
2141 Kind = tok::greatergreaterequal;
2142 } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) {
2143 // If this is '>>>>>>>' and we're in a conflict marker, ignore it.
2144 goto LexNextToken;
2145 } else {
2146 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2147 Kind = tok::greatergreater;
2148 }
2149
Chris Lattner22eb9722006-06-18 05:43:12 +00002150 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002151 Kind = tok::greater;
Chris Lattner22eb9722006-06-18 05:43:12 +00002152 }
2153 break;
2154 case '^':
2155 Char = getCharAndSize(CurPtr, SizeTmp);
2156 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00002157 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002158 Kind = tok::caretequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002159 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002160 Kind = tok::caret;
Chris Lattner22eb9722006-06-18 05:43:12 +00002161 }
2162 break;
2163 case '|':
2164 Char = getCharAndSize(CurPtr, SizeTmp);
2165 if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002166 Kind = tok::pipeequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002167 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2168 } else if (Char == '|') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00002169 // If this is '|||||||' and we're in a conflict marker, ignore it.
2170 if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1))
2171 goto LexNextToken;
Chris Lattnerb11c3232008-10-12 04:51:35 +00002172 Kind = tok::pipepipe;
Chris Lattner22eb9722006-06-18 05:43:12 +00002173 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2174 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002175 Kind = tok::pipe;
Chris Lattner22eb9722006-06-18 05:43:12 +00002176 }
2177 break;
2178 case ':':
2179 Char = getCharAndSize(CurPtr, SizeTmp);
2180 if (Features.Digraphs && Char == '>') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002181 Kind = tok::r_square; // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00002182 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2183 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002184 Kind = tok::coloncolon;
Chris Lattner22eb9722006-06-18 05:43:12 +00002185 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump11289f42009-09-09 15:08:12 +00002186 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002187 Kind = tok::colon;
Chris Lattner22eb9722006-06-18 05:43:12 +00002188 }
2189 break;
2190 case ';':
Chris Lattnerb11c3232008-10-12 04:51:35 +00002191 Kind = tok::semi;
Chris Lattner22eb9722006-06-18 05:43:12 +00002192 break;
2193 case '=':
2194 Char = getCharAndSize(CurPtr, SizeTmp);
2195 if (Char == '=') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00002196 // If this is '=======' and we're in a conflict marker, ignore it.
2197 if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1))
2198 goto LexNextToken;
2199
Chris Lattnerb11c3232008-10-12 04:51:35 +00002200 Kind = tok::equalequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002201 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump11289f42009-09-09 15:08:12 +00002202 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002203 Kind = tok::equal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002204 }
2205 break;
2206 case ',':
Chris Lattnerb11c3232008-10-12 04:51:35 +00002207 Kind = tok::comma;
Chris Lattner22eb9722006-06-18 05:43:12 +00002208 break;
2209 case '#':
2210 Char = getCharAndSize(CurPtr, SizeTmp);
2211 if (Char == '#') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002212 Kind = tok::hashhash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002213 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00002214 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattnerb11c3232008-10-12 04:51:35 +00002215 Kind = tok::hashat;
Chris Lattner6d27a162008-11-22 02:02:22 +00002216 if (!isLexingRawMode())
2217 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner2b271db2006-07-15 05:41:09 +00002218 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002219 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +00002220 // We parsed a # character. If this occurs at the start of the line,
2221 // it's actually the start of a preprocessing directive. Callback to
2222 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00002223 // FIXME: -fpreprocessed mode??
Chris Lattnerff96dd02009-05-13 06:10:29 +00002224 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattner2534324a2009-03-18 20:58:27 +00002225 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner02b436a2007-10-17 20:41:00 +00002226 PP->HandleDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +00002227
Chris Lattner22eb9722006-06-18 05:43:12 +00002228 // As an optimization, if the preprocessor didn't switch lexers, tail
2229 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00002230 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002231 // Start a new token. If this is a #include or something, the PP may
2232 // want us starting at the beginning of the line again. If so, set
Chris Lattner1a9e8732010-04-12 23:04:41 +00002233 // the StartOfLine flag and clear LeadingSpace.
Chris Lattner22eb9722006-06-18 05:43:12 +00002234 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00002235 Result.setFlag(Token::StartOfLine);
Chris Lattner1a9e8732010-04-12 23:04:41 +00002236 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00002237 IsAtStartOfLine = false;
2238 }
2239 goto LexNextToken; // GCC isn't tail call eliminating.
2240 }
Chris Lattner02b436a2007-10-17 20:41:00 +00002241 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002242 }
Mike Stump11289f42009-09-09 15:08:12 +00002243
Chris Lattner2534324a2009-03-18 20:58:27 +00002244 Kind = tok::hash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002245 }
2246 break;
2247
Chris Lattner2b15cf72008-01-03 17:58:54 +00002248 case '@':
2249 // Objective C support.
2250 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattnerb11c3232008-10-12 04:51:35 +00002251 Kind = tok::at;
Chris Lattner2b15cf72008-01-03 17:58:54 +00002252 else
Chris Lattnerb11c3232008-10-12 04:51:35 +00002253 Kind = tok::unknown;
Chris Lattner2b15cf72008-01-03 17:58:54 +00002254 break;
Mike Stump11289f42009-09-09 15:08:12 +00002255
Chris Lattner22eb9722006-06-18 05:43:12 +00002256 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00002257 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00002258 // FALL THROUGH.
2259 default:
Chris Lattnerb11c3232008-10-12 04:51:35 +00002260 Kind = tok::unknown;
Chris Lattner041bef82006-07-11 05:52:53 +00002261 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00002262 }
Mike Stump11289f42009-09-09 15:08:12 +00002263
Chris Lattner371ac8a2006-07-04 07:11:10 +00002264 // Notify MIOpt that we read a non-whitespace/non-comment token.
2265 MIOpt.ReadToken();
2266
Chris Lattnerd01e2912006-06-18 16:22:51 +00002267 // Update the location of token as well as BufferPtr.
Chris Lattnerb11c3232008-10-12 04:51:35 +00002268 FormTokenWithChars(Result, CurPtr, Kind);
Chris Lattner22eb9722006-06-18 05:43:12 +00002269}