blob: 7e6f7c1af6373bd5aa58397e80b50485b15014c7 [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.
Chris Lattnerd14705b2009-03-18 21:10:12 +0000965 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +0000966 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattnerb11c3232008-10-12 04:51:35 +0000967 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000968 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000969 }
Chris Lattner52d96ac2010-05-30 23:27:38 +0000970
971 if (C == 0)
972 NulCharacter = CurPtr-1;
Chris Lattner22eb9722006-06-18 05:43:12 +0000973 C = getAndAdvanceChar(CurPtr, Result);
974 }
Mike Stump11289f42009-09-09 15:08:12 +0000975
Chris Lattner5a78a022006-07-20 06:02:19 +0000976 // If a nul character existed in the string, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +0000977 if (NulCharacter && !isLexingRawMode())
978 Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000979
Chris Lattnerd01e2912006-06-18 16:22:51 +0000980 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000981 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000982 FormTokenWithChars(Result, CurPtr,
983 Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000984 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000985}
986
987/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
988/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattner146762e2007-07-20 16:59:19 +0000989void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000990 const char *NulCharacter = 0; // Does this string contain the \0 character?
Chris Lattnerb40289b2009-04-17 23:56:52 +0000991 const char *AfterLessPos = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000992 char C = getAndAdvanceChar(CurPtr, Result);
993 while (C != '>') {
994 // Skip escaped characters.
995 if (C == '\\') {
996 // Skip the escaped character.
997 C = getAndAdvanceChar(CurPtr, Result);
998 } else if (C == '\n' || C == '\r' || // Newline.
999 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerb40289b2009-04-17 23:56:52 +00001000 // If the filename is unterminated, then it must just be a lone <
1001 // character. Return this as such.
1002 FormTokenWithChars(Result, AfterLessPos, tok::less);
Chris Lattner5a78a022006-07-20 06:02:19 +00001003 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001004 } else if (C == 0) {
1005 NulCharacter = CurPtr-1;
1006 }
1007 C = getAndAdvanceChar(CurPtr, Result);
1008 }
Mike Stump11289f42009-09-09 15:08:12 +00001009
Chris Lattner5a78a022006-07-20 06:02:19 +00001010 // If a nul character existed in the string, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +00001011 if (NulCharacter && !isLexingRawMode())
1012 Diag(NulCharacter, diag::null_in_string);
Mike Stump11289f42009-09-09 15:08:12 +00001013
Chris Lattnerd01e2912006-06-18 16:22:51 +00001014 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +00001015 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001016 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +00001017 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +00001018}
1019
1020
1021/// LexCharConstant - Lex the remainder of a character constant, after having
1022/// lexed either ' or L'.
Chris Lattner146762e2007-07-20 16:59:19 +00001023void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001024 const char *NulCharacter = 0; // Does this character contain the \0 character?
1025
Chris Lattner22eb9722006-06-18 05:43:12 +00001026 char C = getAndAdvanceChar(CurPtr, Result);
1027 if (C == '\'') {
Chris Lattnerd14705b2009-03-18 21:10:12 +00001028 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +00001029 Diag(BufferPtr, diag::err_empty_character);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001030 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +00001031 return;
Chris Lattner86851b82010-07-07 23:24:27 +00001032 }
1033
1034 while (C != '\'') {
1035 // Skip escaped characters.
1036 if (C == '\\') {
1037 // Skip the escaped character.
1038 // FIXME: UCN's
1039 C = getAndAdvanceChar(CurPtr, Result);
1040 } else if (C == '\n' || C == '\r' || // Newline.
1041 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
1042 if (!isLexingRawMode() && !Features.AsmPreprocessor)
1043 Diag(BufferPtr, diag::err_unterminated_char);
1044 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
1045 return;
1046 } else if (C == 0) {
1047 NulCharacter = CurPtr-1;
1048 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001049 C = getAndAdvanceChar(CurPtr, Result);
1050 }
Mike Stump11289f42009-09-09 15:08:12 +00001051
Chris Lattner86851b82010-07-07 23:24:27 +00001052 // If a nul character existed in the character, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +00001053 if (NulCharacter && !isLexingRawMode())
1054 Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +00001055
Chris Lattnerd01e2912006-06-18 16:22:51 +00001056 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +00001057 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001058 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Chris Lattner5a7971e2009-01-26 19:29:26 +00001059 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +00001060}
1061
1062/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
1063/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattner4d963442008-10-12 04:05:48 +00001064///
1065/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
1066///
1067bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001068 // Whitespace - Skip it, then return the token after the whitespace.
1069 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
1070 while (1) {
1071 // Skip horizontal whitespace very aggressively.
1072 while (isHorizontalWhitespace(Char))
1073 Char = *++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001074
Daniel Dunbar5c4cc092008-11-25 00:20:22 +00001075 // Otherwise if we have something other than whitespace, we're done.
Chris Lattner22eb9722006-06-18 05:43:12 +00001076 if (Char != '\n' && Char != '\r')
1077 break;
Mike Stump11289f42009-09-09 15:08:12 +00001078
Chris Lattner22eb9722006-06-18 05:43:12 +00001079 if (ParsingPreprocessorDirective) {
1080 // End of preprocessor directive line, let LexTokenInternal handle this.
1081 BufferPtr = CurPtr;
Chris Lattner4d963442008-10-12 04:05:48 +00001082 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001083 }
Mike Stump11289f42009-09-09 15:08:12 +00001084
Chris Lattner22eb9722006-06-18 05:43:12 +00001085 // ok, but handle newline.
1086 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001087 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001088 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001089 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001090 Char = *++CurPtr;
1091 }
1092
1093 // If this isn't immediately after a newline, there is leading space.
1094 char PrevChar = CurPtr[-1];
1095 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattner146762e2007-07-20 16:59:19 +00001096 Result.setFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001097
Chris Lattner4d963442008-10-12 04:05:48 +00001098 // If the client wants us to return whitespace, return it now.
1099 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001100 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4d963442008-10-12 04:05:48 +00001101 return true;
1102 }
Mike Stump11289f42009-09-09 15:08:12 +00001103
Chris Lattner22eb9722006-06-18 05:43:12 +00001104 BufferPtr = CurPtr;
Chris Lattner4d963442008-10-12 04:05:48 +00001105 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001106}
1107
1108// SkipBCPLComment - We have just read the // characters from input. Skip until
1109// we find the newline character thats terminate the comment. Then update
Chris Lattner87d02082010-01-18 22:35:47 +00001110/// BufferPtr and return.
1111///
1112/// If we're in KeepCommentMode or any CommentHandler has inserted
1113/// some tokens, this will store the first token and return true.
Chris Lattner146762e2007-07-20 16:59:19 +00001114bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001115 // If BCPL comments aren't explicitly enabled for this language, emit an
1116 // extension warning.
Chris Lattner6d27a162008-11-22 02:02:22 +00001117 if (!Features.BCPLComment && !isLexingRawMode()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +00001118 Diag(BufferPtr, diag::ext_bcpl_comment);
Mike Stump11289f42009-09-09 15:08:12 +00001119
Chris Lattner22eb9722006-06-18 05:43:12 +00001120 // Mark them enabled so we only emit one warning for this translation
1121 // unit.
1122 Features.BCPLComment = true;
1123 }
Mike Stump11289f42009-09-09 15:08:12 +00001124
Chris Lattner22eb9722006-06-18 05:43:12 +00001125 // Scan over the body of the comment. The common case, when scanning, is that
1126 // the comment contains normal ascii characters with nothing interesting in
1127 // them. As such, optimize for this case with the inner loop.
1128 char C;
1129 do {
1130 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +00001131 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
1132 // If we find a \n character, scan backwards, checking to see if it's an
1133 // escaped newline, like we do for block comments.
Mike Stump11289f42009-09-09 15:08:12 +00001134
Chris Lattner22eb9722006-06-18 05:43:12 +00001135 // Skip over characters in the fast loop.
1136 while (C != 0 && // Potentially EOF.
1137 C != '\\' && // Potentially escaped newline.
1138 C != '?' && // Potentially trigraph.
1139 C != '\n' && C != '\r') // Newline or DOS-style newline.
1140 C = *++CurPtr;
1141
1142 // If this is a newline, we're done.
1143 if (C == '\n' || C == '\r')
1144 break; // Found the newline? Break out!
Mike Stump11289f42009-09-09 15:08:12 +00001145
Chris Lattner22eb9722006-06-18 05:43:12 +00001146 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnere141a9e2008-12-12 07:34:39 +00001147 // properly decode the character. Read it in raw mode to avoid emitting
1148 // diagnostics about things like trigraphs. If we see an escaped newline,
1149 // we'll handle it below.
Chris Lattner22eb9722006-06-18 05:43:12 +00001150 const char *OldPtr = CurPtr;
Chris Lattnere141a9e2008-12-12 07:34:39 +00001151 bool OldRawMode = isLexingRawMode();
1152 LexingRawMode = true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001153 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnere141a9e2008-12-12 07:34:39 +00001154 LexingRawMode = OldRawMode;
Chris Lattnerecdaf402009-04-05 00:26:41 +00001155
1156 // If the char that we finally got was a \n, then we must have had something
1157 // like \<newline><newline>. We don't want to have consumed the second
1158 // newline, we want CurPtr, to end up pointing to it down below.
1159 if (C == '\n' || C == '\r') {
1160 --CurPtr;
1161 C = 'x'; // doesn't matter what this is.
1162 }
Mike Stump11289f42009-09-09 15:08:12 +00001163
Chris Lattner22eb9722006-06-18 05:43:12 +00001164 // If we read multiple characters, and one of those characters was a \r or
Chris Lattnerff591e22007-06-09 06:07:22 +00001165 // \n, then we had an escaped newline within the comment. Emit diagnostic
1166 // unless the next line is also a // comment.
1167 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001168 for (; OldPtr != CurPtr; ++OldPtr)
1169 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnerff591e22007-06-09 06:07:22 +00001170 // Okay, we found a // comment that ends in a newline, if the next
1171 // line is also a // comment, but has spaces, don't emit a diagnostic.
1172 if (isspace(C)) {
1173 const char *ForwardPtr = CurPtr;
1174 while (isspace(*ForwardPtr)) // Skip whitespace.
1175 ++ForwardPtr;
1176 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
1177 break;
1178 }
Mike Stump11289f42009-09-09 15:08:12 +00001179
Chris Lattner6d27a162008-11-22 02:02:22 +00001180 if (!isLexingRawMode())
1181 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Chris Lattnercb283342006-06-18 06:48:37 +00001182 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001183 }
1184 }
Mike Stump11289f42009-09-09 15:08:12 +00001185
Chris Lattner457fc152006-07-29 06:30:25 +00001186 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
Chris Lattner22eb9722006-06-18 05:43:12 +00001187 } while (C != '\n' && C != '\r');
1188
Chris Lattner93ddf802010-02-03 21:06:21 +00001189 // Found but did not consume the newline. Notify comment handlers about the
1190 // comment unless we're in a #if 0 block.
1191 if (PP && !isLexingRawMode() &&
1192 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
1193 getSourceLocation(CurPtr)))) {
Chris Lattner87d02082010-01-18 22:35:47 +00001194 BufferPtr = CurPtr;
1195 return true; // A token has to be returned.
1196 }
Mike Stump11289f42009-09-09 15:08:12 +00001197
Chris Lattner457fc152006-07-29 06:30:25 +00001198 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +00001199 if (inKeepCommentMode())
Chris Lattner457fc152006-07-29 06:30:25 +00001200 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001201
1202 // If we are inside a preprocessor directive and we see the end of line,
1203 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +00001204 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001205 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001206 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001207 }
Mike Stump11289f42009-09-09 15:08:12 +00001208
Chris Lattner22eb9722006-06-18 05:43:12 +00001209 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner87e97ea2008-10-12 00:23:07 +00001210 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattner4d963442008-10-12 04:05:48 +00001211 // contribute to another token), it isn't needed for correctness. Note that
1212 // this is ok even in KeepWhitespaceMode, because we would have returned the
1213 /// comment above in that mode.
Chris Lattner22eb9722006-06-18 05:43:12 +00001214 ++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001215
Chris Lattner22eb9722006-06-18 05:43:12 +00001216 // The next returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001217 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001218 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001219 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001220 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001221 return false;
Chris Lattner457fc152006-07-29 06:30:25 +00001222}
Chris Lattner22eb9722006-06-18 05:43:12 +00001223
Chris Lattner457fc152006-07-29 06:30:25 +00001224/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
1225/// an appropriate way and return it.
Chris Lattner146762e2007-07-20 16:59:19 +00001226bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001227 // If we're not in a preprocessor directive, just return the // comment
1228 // directly.
1229 FormTokenWithChars(Result, CurPtr, tok::comment);
Mike Stump11289f42009-09-09 15:08:12 +00001230
Chris Lattnerb11c3232008-10-12 04:51:35 +00001231 if (!ParsingPreprocessorDirective)
1232 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001233
Chris Lattnerb11c3232008-10-12 04:51:35 +00001234 // If this BCPL-style comment is in a macro definition, transmogrify it into
1235 // a C-style block comment.
Douglas Gregordc970f02010-03-16 22:30:13 +00001236 bool Invalid = false;
1237 std::string Spelling = PP->getSpelling(Result, &Invalid);
1238 if (Invalid)
1239 return true;
1240
Chris Lattnerb11c3232008-10-12 04:51:35 +00001241 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
1242 Spelling[1] = '*'; // Change prefix to "/*".
1243 Spelling += "*/"; // add suffix.
Mike Stump11289f42009-09-09 15:08:12 +00001244
Chris Lattnerb11c3232008-10-12 04:51:35 +00001245 Result.setKind(tok::comment);
Chris Lattner5a7971e2009-01-26 19:29:26 +00001246 PP->CreateString(&Spelling[0], Spelling.size(), Result,
1247 Result.getLocation());
Chris Lattnere01e7582008-10-12 04:15:42 +00001248 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001249}
1250
Chris Lattnercb283342006-06-18 06:48:37 +00001251/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
1252/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner89770572008-12-12 07:14:34 +00001253/// diagnostic if so. We know that the newline is inside of a block comment.
Mike Stump11289f42009-09-09 15:08:12 +00001254static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
Chris Lattner1f583052006-06-18 06:53:56 +00001255 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001256 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Mike Stump11289f42009-09-09 15:08:12 +00001257
Chris Lattner22eb9722006-06-18 05:43:12 +00001258 // Back up off the newline.
1259 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001260
Chris Lattner22eb9722006-06-18 05:43:12 +00001261 // If this is a two-character newline sequence, skip the other character.
1262 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
1263 // \n\n or \r\r -> not escaped newline.
1264 if (CurPtr[0] == CurPtr[1])
1265 return false;
1266 // \n\r or \r\n -> skip the newline.
1267 --CurPtr;
1268 }
Mike Stump11289f42009-09-09 15:08:12 +00001269
Chris Lattner22eb9722006-06-18 05:43:12 +00001270 // If we have horizontal whitespace, skip over it. We allow whitespace
1271 // between the slash and newline.
1272 bool HasSpace = false;
1273 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
1274 --CurPtr;
1275 HasSpace = true;
1276 }
Mike Stump11289f42009-09-09 15:08:12 +00001277
Chris Lattner22eb9722006-06-18 05:43:12 +00001278 // If we have a slash, we know this is an escaped newline.
1279 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +00001280 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001281 } else {
1282 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +00001283 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
1284 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +00001285 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001286
Chris Lattnercb283342006-06-18 06:48:37 +00001287 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +00001288 CurPtr -= 2;
1289
1290 // If no trigraphs are enabled, warn that we ignored this trigraph and
1291 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +00001292 if (!L->getFeatures().Trigraphs) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001293 if (!L->isLexingRawMode())
1294 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +00001295 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001296 }
Chris Lattner6d27a162008-11-22 02:02:22 +00001297 if (!L->isLexingRawMode())
1298 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001299 }
Mike Stump11289f42009-09-09 15:08:12 +00001300
Chris Lattner22eb9722006-06-18 05:43:12 +00001301 // Warn about having an escaped newline between the */ characters.
Chris Lattner6d27a162008-11-22 02:02:22 +00001302 if (!L->isLexingRawMode())
1303 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Mike Stump11289f42009-09-09 15:08:12 +00001304
Chris Lattner22eb9722006-06-18 05:43:12 +00001305 // If there was space between the backslash and newline, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +00001306 if (HasSpace && !L->isLexingRawMode())
1307 L->Diag(CurPtr, diag::backslash_newline_space);
Mike Stump11289f42009-09-09 15:08:12 +00001308
Chris Lattnercb283342006-06-18 06:48:37 +00001309 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001310}
1311
Chris Lattneraded4a92006-10-27 04:42:31 +00001312#ifdef __SSE2__
1313#include <emmintrin.h>
Chris Lattner9f6604f2006-10-30 20:01:22 +00001314#elif __ALTIVEC__
1315#include <altivec.h>
1316#undef bool
Chris Lattneraded4a92006-10-27 04:42:31 +00001317#endif
1318
Chris Lattner22eb9722006-06-18 05:43:12 +00001319/// SkipBlockComment - We have just read the /* characters from input. Read
1320/// until we find the */ characters that terminate the comment. Note that we
1321/// don't bother decoding trigraphs or escaped newlines in block comments,
1322/// because they cannot cause the comment to end. The only thing that can
1323/// happen is the comment could end with an escaped newline between the */ end
1324/// of comment.
Chris Lattnere01e7582008-10-12 04:15:42 +00001325///
Chris Lattner87d02082010-01-18 22:35:47 +00001326/// If we're in KeepCommentMode or any CommentHandler has inserted
1327/// some tokens, this will store the first token and return true.
Chris Lattner146762e2007-07-20 16:59:19 +00001328bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001329 // Scan one character past where we should, looking for a '/' character. Once
1330 // we find it, check to see if it was preceeded by a *. This common
1331 // optimization helps people who like to put a lot of * characters in their
1332 // comments.
Chris Lattnerc850ad62007-07-21 23:43:37 +00001333
1334 // The first character we get with newlines and trigraphs skipped to handle
1335 // the degenerate /*/ case below correctly if the * has an escaped newline
1336 // after it.
1337 unsigned CharSize;
1338 unsigned char C = getCharAndSize(CurPtr, CharSize);
1339 CurPtr += CharSize;
Chris Lattner22eb9722006-06-18 05:43:12 +00001340 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner561aabd2010-05-16 19:54:05 +00001341 if (!isLexingRawMode() &&
1342 !PP->isCodeCompletionFile(FileLoc))
Chris Lattner7c2e9802008-10-12 01:31:51 +00001343 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner99e7d232008-10-12 04:19:49 +00001344 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001345
Chris Lattner99e7d232008-10-12 04:19:49 +00001346 // KeepWhitespaceMode should return this broken comment as a token. Since
1347 // it isn't a well formed comment, just return it as an 'unknown' token.
1348 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001349 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner99e7d232008-10-12 04:19:49 +00001350 return true;
1351 }
Mike Stump11289f42009-09-09 15:08:12 +00001352
Chris Lattner99e7d232008-10-12 04:19:49 +00001353 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001354 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001355 }
Mike Stump11289f42009-09-09 15:08:12 +00001356
Chris Lattnerc850ad62007-07-21 23:43:37 +00001357 // Check to see if the first character after the '/*' is another /. If so,
1358 // then this slash does not end the block comment, it is part of it.
1359 if (C == '/')
1360 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001361
Chris Lattner22eb9722006-06-18 05:43:12 +00001362 while (1) {
Chris Lattner6cc3e362006-10-27 04:12:35 +00001363 // Skip over all non-interesting characters until we find end of buffer or a
1364 // (probably ending) '/' character.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001365 if (CurPtr + 24 < BufferEnd) {
1366 // While not aligned to a 16-byte boundary.
1367 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1368 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001369
Chris Lattner6cc3e362006-10-27 04:12:35 +00001370 if (C == '/') goto FoundSlash;
Chris Lattneraded4a92006-10-27 04:42:31 +00001371
1372#ifdef __SSE2__
1373 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1374 '/', '/', '/', '/', '/', '/', '/', '/');
1375 while (CurPtr+16 <= BufferEnd &&
1376 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1377 CurPtr += 16;
Chris Lattner9f6604f2006-10-30 20:01:22 +00001378#elif __ALTIVEC__
1379 __vector unsigned char Slashes = {
Mike Stump11289f42009-09-09 15:08:12 +00001380 '/', '/', '/', '/', '/', '/', '/', '/',
Chris Lattner9f6604f2006-10-30 20:01:22 +00001381 '/', '/', '/', '/', '/', '/', '/', '/'
1382 };
1383 while (CurPtr+16 <= BufferEnd &&
1384 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1385 CurPtr += 16;
Mike Stump11289f42009-09-09 15:08:12 +00001386#else
Chris Lattneraded4a92006-10-27 04:42:31 +00001387 // Scan for '/' quickly. Many block comments are very large.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001388 while (CurPtr[0] != '/' &&
1389 CurPtr[1] != '/' &&
1390 CurPtr[2] != '/' &&
1391 CurPtr[3] != '/' &&
1392 CurPtr+4 < BufferEnd) {
1393 CurPtr += 4;
1394 }
Chris Lattneraded4a92006-10-27 04:42:31 +00001395#endif
Mike Stump11289f42009-09-09 15:08:12 +00001396
Chris Lattneraded4a92006-10-27 04:42:31 +00001397 // It has to be one of the bytes scanned, increment to it and read one.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001398 C = *CurPtr++;
1399 }
Mike Stump11289f42009-09-09 15:08:12 +00001400
Chris Lattneraded4a92006-10-27 04:42:31 +00001401 // Loop to scan the remainder.
Chris Lattner22eb9722006-06-18 05:43:12 +00001402 while (C != '/' && C != '\0')
1403 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001404
Chris Lattner6cc3e362006-10-27 04:12:35 +00001405 FoundSlash:
Chris Lattner22eb9722006-06-18 05:43:12 +00001406 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001407 if (CurPtr[-2] == '*') // We found the final */. We're done!
1408 break;
Mike Stump11289f42009-09-09 15:08:12 +00001409
Chris Lattner22eb9722006-06-18 05:43:12 +00001410 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +00001411 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001412 // We found the final */, though it had an escaped newline between the
1413 // * and /. We're done!
1414 break;
1415 }
1416 }
1417 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1418 // If this is a /* inside of the comment, emit a warning. Don't do this
1419 // if this is a /*/, which will end the comment. This misses cases with
1420 // embedded escaped newlines, but oh well.
Chris Lattner6d27a162008-11-22 02:02:22 +00001421 if (!isLexingRawMode())
1422 Diag(CurPtr-1, diag::warn_nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001423 }
1424 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner561aabd2010-05-16 19:54:05 +00001425 if (!isLexingRawMode() && !PP->isCodeCompletionFile(FileLoc))
Chris Lattner6d27a162008-11-22 02:02:22 +00001426 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001427 // Note: the user probably forgot a */. We could continue immediately
1428 // after the /*, but this would involve lexing a lot of what really is the
1429 // comment, which surely would confuse the parser.
Chris Lattner99e7d232008-10-12 04:19:49 +00001430 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001431
Chris Lattner99e7d232008-10-12 04:19:49 +00001432 // KeepWhitespaceMode should return this broken comment as a token. Since
1433 // it isn't a well formed comment, just return it as an 'unknown' token.
1434 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001435 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner99e7d232008-10-12 04:19:49 +00001436 return true;
1437 }
Mike Stump11289f42009-09-09 15:08:12 +00001438
Chris Lattner99e7d232008-10-12 04:19:49 +00001439 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001440 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001441 }
1442 C = *CurPtr++;
1443 }
Mike Stump11289f42009-09-09 15:08:12 +00001444
Chris Lattner93ddf802010-02-03 21:06:21 +00001445 // Notify comment handlers about the comment unless we're in a #if 0 block.
1446 if (PP && !isLexingRawMode() &&
1447 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
1448 getSourceLocation(CurPtr)))) {
Chris Lattner87d02082010-01-18 22:35:47 +00001449 BufferPtr = CurPtr;
1450 return true; // A token has to be returned.
1451 }
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001452
Chris Lattner457fc152006-07-29 06:30:25 +00001453 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +00001454 if (inKeepCommentMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001455 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattnere01e7582008-10-12 04:15:42 +00001456 return true;
Chris Lattner457fc152006-07-29 06:30:25 +00001457 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001458
1459 // It is common for the tokens immediately after a /**/ comment to be
1460 // whitespace. Instead of going through the big switch, handle it
Chris Lattner4d963442008-10-12 04:05:48 +00001461 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1462 // have already returned above with the comment as a token.
Chris Lattner22eb9722006-06-18 05:43:12 +00001463 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner146762e2007-07-20 16:59:19 +00001464 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +00001465 SkipWhitespace(Result, CurPtr+1);
Chris Lattnere01e7582008-10-12 04:15:42 +00001466 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001467 }
1468
1469 // Otherwise, just return so that the next character will be lexed as a token.
1470 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001471 Result.setFlag(Token::LeadingSpace);
Chris Lattnere01e7582008-10-12 04:15:42 +00001472 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001473}
1474
1475//===----------------------------------------------------------------------===//
1476// Primary Lexing Entry Points
1477//===----------------------------------------------------------------------===//
1478
Chris Lattner22eb9722006-06-18 05:43:12 +00001479/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1480/// uninterpreted string. This switches the lexer out of directive mode.
1481std::string Lexer::ReadToEndOfLine() {
1482 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1483 "Must be in a preprocessing directive!");
1484 std::string Result;
Chris Lattner146762e2007-07-20 16:59:19 +00001485 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001486
1487 // CurPtr - Cache BufferPtr in an automatic variable.
1488 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001489 while (1) {
1490 char Char = getAndAdvanceChar(CurPtr, Tmp);
1491 switch (Char) {
1492 default:
1493 Result += Char;
1494 break;
1495 case 0: // Null.
1496 // Found end of file?
1497 if (CurPtr-1 != BufferEnd) {
1498 // Nope, normal character, continue.
1499 Result += Char;
1500 break;
1501 }
1502 // FALL THROUGH.
1503 case '\r':
1504 case '\n':
1505 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1506 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1507 BufferPtr = CurPtr-1;
Mike Stump11289f42009-09-09 15:08:12 +00001508
Chris Lattner22eb9722006-06-18 05:43:12 +00001509 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +00001510 Lex(Tmp);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001511 assert(Tmp.is(tok::eom) && "Unexpected token!");
Mike Stump11289f42009-09-09 15:08:12 +00001512
Chris Lattner22eb9722006-06-18 05:43:12 +00001513 // Finally, we're done, return the string we found.
1514 return Result;
1515 }
1516 }
1517}
1518
1519/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1520/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +00001521/// This returns true if Result contains a token, false if PP.Lex should be
1522/// called again.
Chris Lattner146762e2007-07-20 16:59:19 +00001523bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001524 // If we hit the end of the file while parsing a preprocessor directive,
1525 // end the preprocessor directive first. The next token returned will
1526 // then be the end of file.
1527 if (ParsingPreprocessorDirective) {
1528 // Done parsing the "line".
1529 ParsingPreprocessorDirective = false;
Chris Lattnerd01e2912006-06-18 16:22:51 +00001530 // Update the location of token as well as BufferPtr.
Chris Lattnerb11c3232008-10-12 04:51:35 +00001531 FormTokenWithChars(Result, CurPtr, tok::eom);
Mike Stump11289f42009-09-09 15:08:12 +00001532
Chris Lattner457fc152006-07-29 06:30:25 +00001533 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001534 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner2183a6e2006-07-18 06:36:12 +00001535 return true; // Have a token.
Mike Stump11289f42009-09-09 15:08:12 +00001536 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00001537
Chris Lattner30a2fa12006-07-19 06:31:49 +00001538 // If we are in raw mode, return this event as an EOF token. Let the caller
1539 // that put us in raw mode handle the event.
Chris Lattner6d27a162008-11-22 02:02:22 +00001540 if (isLexingRawMode()) {
Chris Lattner8c204872006-10-14 05:19:21 +00001541 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +00001542 BufferPtr = BufferEnd;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001543 FormTokenWithChars(Result, BufferEnd, tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001544 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001545 }
Mike Stump11289f42009-09-09 15:08:12 +00001546
Douglas Gregor3545ff42009-09-21 16:56:56 +00001547 // Otherwise, check if we are code-completing, then issue diagnostics for
1548 // unterminated #if and missing newline.
Chris Lattner30a2fa12006-07-19 06:31:49 +00001549
Douglas Gregor53ad6b92009-12-02 06:49:09 +00001550 if (PP && PP->isCodeCompletionFile(FileLoc)) {
1551 // We're at the end of the file, but we've been asked to consider the
1552 // end of the file to be a code-completion token. Return the
1553 // code-completion token.
1554 Result.startToken();
1555 FormTokenWithChars(Result, CurPtr, tok::code_completion);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001556
Douglas Gregor53ad6b92009-12-02 06:49:09 +00001557 // Only do the eof -> code_completion translation once.
1558 PP->SetCodeCompletionPoint(0, 0, 0);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001559
1560 // Silence any diagnostics that occur once we hit the code-completion point.
1561 PP->getDiagnostics().setSuppressAllDiagnostics(true);
Douglas Gregor53ad6b92009-12-02 06:49:09 +00001562 return true;
Douglas Gregor3545ff42009-09-21 16:56:56 +00001563 }
1564
Chris Lattner30a2fa12006-07-19 06:31:49 +00001565 // If we are in a #if directive, emit an error.
1566 while (!ConditionalStack.empty()) {
Chris Lattner014156e2008-11-22 06:22:39 +00001567 PP->Diag(ConditionalStack.back().IfLoc,
1568 diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001569 ConditionalStack.pop_back();
1570 }
Mike Stump11289f42009-09-09 15:08:12 +00001571
Chris Lattner8f96d042008-04-12 05:54:25 +00001572 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1573 // a pedwarn.
1574 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Mike Stump0be88752009-04-02 02:29:42 +00001575 Diag(BufferEnd, diag::ext_no_newline_eof)
Douglas Gregora771f462010-03-31 17:46:05 +00001576 << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n");
Mike Stump11289f42009-09-09 15:08:12 +00001577
Chris Lattner22eb9722006-06-18 05:43:12 +00001578 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +00001579
1580 // Finally, let the preprocessor handle this.
Chris Lattner02b436a2007-10-17 20:41:00 +00001581 return PP->HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001582}
1583
Chris Lattner678c8802006-07-11 05:46:12 +00001584/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1585/// the specified lexer will return a tok::l_paren token, 0 if it is something
1586/// else and 2 if there are no more tokens in the buffer controlled by the
1587/// lexer.
1588unsigned Lexer::isNextPPTokenLParen() {
1589 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
Mike Stump11289f42009-09-09 15:08:12 +00001590
Chris Lattner678c8802006-07-11 05:46:12 +00001591 // Switch to 'skipping' mode. This will ensure that we can lex a token
1592 // without emitting diagnostics, disables macro expansion, and will cause EOF
1593 // to return an EOF token instead of popping the include stack.
1594 LexingRawMode = true;
Mike Stump11289f42009-09-09 15:08:12 +00001595
Chris Lattner678c8802006-07-11 05:46:12 +00001596 // Save state that can be changed while lexing so that we can restore it.
1597 const char *TmpBufferPtr = BufferPtr;
Chris Lattner40493eb2009-04-24 07:15:46 +00001598 bool inPPDirectiveMode = ParsingPreprocessorDirective;
Mike Stump11289f42009-09-09 15:08:12 +00001599
Chris Lattner146762e2007-07-20 16:59:19 +00001600 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001601 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001602 LexTokenInternal(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001603
Chris Lattner678c8802006-07-11 05:46:12 +00001604 // Restore state that may have changed.
1605 BufferPtr = TmpBufferPtr;
Chris Lattner40493eb2009-04-24 07:15:46 +00001606 ParsingPreprocessorDirective = inPPDirectiveMode;
Mike Stump11289f42009-09-09 15:08:12 +00001607
Chris Lattner678c8802006-07-11 05:46:12 +00001608 // Restore the lexer back to non-skipping mode.
1609 LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +00001610
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001611 if (Tok.is(tok::eof))
Chris Lattner678c8802006-07-11 05:46:12 +00001612 return 2;
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001613 return Tok.is(tok::l_paren);
Chris Lattner678c8802006-07-11 05:46:12 +00001614}
1615
Chris Lattner7c027ee2009-12-14 06:16:57 +00001616/// FindConflictEnd - Find the end of a version control conflict marker.
1617static const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd) {
1618 llvm::StringRef RestOfBuffer(CurPtr+7, BufferEnd-CurPtr-7);
1619 size_t Pos = RestOfBuffer.find(">>>>>>>");
1620 while (Pos != llvm::StringRef::npos) {
1621 // Must occur at start of line.
1622 if (RestOfBuffer[Pos-1] != '\r' &&
1623 RestOfBuffer[Pos-1] != '\n') {
1624 RestOfBuffer = RestOfBuffer.substr(Pos+7);
Chris Lattner467f6bc2010-05-17 20:27:25 +00001625 Pos = RestOfBuffer.find(">>>>>>>");
Chris Lattner7c027ee2009-12-14 06:16:57 +00001626 continue;
1627 }
1628 return RestOfBuffer.data()+Pos;
1629 }
1630 return 0;
1631}
1632
1633/// IsStartOfConflictMarker - If the specified pointer is the start of a version
1634/// control conflict marker like '<<<<<<<', recognize it as such, emit an error
1635/// and recover nicely. This returns true if it is a conflict marker and false
1636/// if not.
1637bool Lexer::IsStartOfConflictMarker(const char *CurPtr) {
1638 // Only a conflict marker if it starts at the beginning of a line.
1639 if (CurPtr != BufferStart &&
1640 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1641 return false;
1642
1643 // Check to see if we have <<<<<<<.
1644 if (BufferEnd-CurPtr < 8 ||
1645 llvm::StringRef(CurPtr, 7) != "<<<<<<<")
1646 return false;
1647
1648 // If we have a situation where we don't care about conflict markers, ignore
1649 // it.
1650 if (IsInConflictMarker || isLexingRawMode())
1651 return false;
1652
1653 // Check to see if there is a >>>>>>> somewhere in the buffer at the start of
1654 // a line to terminate this conflict marker.
Chris Lattner467f6bc2010-05-17 20:27:25 +00001655 if (FindConflictEnd(CurPtr, BufferEnd)) {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001656 // We found a match. We are really in a conflict marker.
1657 // Diagnose this, and ignore to the end of line.
1658 Diag(CurPtr, diag::err_conflict_marker);
1659 IsInConflictMarker = true;
1660
1661 // Skip ahead to the end of line. We know this exists because the
1662 // end-of-conflict marker starts with \r or \n.
1663 while (*CurPtr != '\r' && *CurPtr != '\n') {
1664 assert(CurPtr != BufferEnd && "Didn't find end of line");
1665 ++CurPtr;
1666 }
1667 BufferPtr = CurPtr;
1668 return true;
1669 }
1670
1671 // No end of conflict marker found.
1672 return false;
1673}
1674
1675
1676/// HandleEndOfConflictMarker - If this is a '=======' or '|||||||' or '>>>>>>>'
1677/// marker, then it is the end of a conflict marker. Handle it by ignoring up
1678/// until the end of the line. This returns true if it is a conflict marker and
1679/// false if not.
1680bool Lexer::HandleEndOfConflictMarker(const char *CurPtr) {
1681 // Only a conflict marker if it starts at the beginning of a line.
1682 if (CurPtr != BufferStart &&
1683 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1684 return false;
1685
1686 // If we have a situation where we don't care about conflict markers, ignore
1687 // it.
1688 if (!IsInConflictMarker || isLexingRawMode())
1689 return false;
1690
1691 // Check to see if we have the marker (7 characters in a row).
1692 for (unsigned i = 1; i != 7; ++i)
1693 if (CurPtr[i] != CurPtr[0])
1694 return false;
1695
1696 // If we do have it, search for the end of the conflict marker. This could
1697 // fail if it got skipped with a '#if 0' or something. Note that CurPtr might
1698 // be the end of conflict marker.
1699 if (const char *End = FindConflictEnd(CurPtr, BufferEnd)) {
1700 CurPtr = End;
1701
1702 // Skip ahead to the end of line.
1703 while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n')
1704 ++CurPtr;
1705
1706 BufferPtr = CurPtr;
1707
1708 // No longer in the conflict marker.
1709 IsInConflictMarker = false;
1710 return true;
1711 }
1712
1713 return false;
1714}
1715
Chris Lattner22eb9722006-06-18 05:43:12 +00001716
1717/// LexTokenInternal - This implements a simple C family lexer. It is an
1718/// extremely performance critical piece of code. This assumes that the buffer
Chris Lattner5c349382009-07-07 05:05:42 +00001719/// has a null character at the end of the file. This returns a preprocessing
1720/// token, not a normal token, as such, it is an internal interface. It assumes
1721/// that the Flags of result have been cleared before calling this.
Chris Lattner146762e2007-07-20 16:59:19 +00001722void Lexer::LexTokenInternal(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001723LexNextToken:
1724 // New token, can't need cleaning yet.
Chris Lattner146762e2007-07-20 16:59:19 +00001725 Result.clearFlag(Token::NeedsCleaning);
Chris Lattner8c204872006-10-14 05:19:21 +00001726 Result.setIdentifierInfo(0);
Mike Stump11289f42009-09-09 15:08:12 +00001727
Chris Lattner22eb9722006-06-18 05:43:12 +00001728 // CurPtr - Cache BufferPtr in an automatic variable.
1729 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001730
Chris Lattnereb54b592006-07-10 06:34:27 +00001731 // Small amounts of horizontal whitespace is very common between tokens.
1732 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1733 ++CurPtr;
1734 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1735 ++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001736
Chris Lattner4d963442008-10-12 04:05:48 +00001737 // If we are keeping whitespace and other tokens, just return what we just
1738 // skipped. The next lexer invocation will return the token after the
1739 // whitespace.
1740 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001741 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4d963442008-10-12 04:05:48 +00001742 return;
1743 }
Mike Stump11289f42009-09-09 15:08:12 +00001744
Chris Lattnereb54b592006-07-10 06:34:27 +00001745 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001746 Result.setFlag(Token::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001747 }
Mike Stump11289f42009-09-09 15:08:12 +00001748
Chris Lattner22eb9722006-06-18 05:43:12 +00001749 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
Mike Stump11289f42009-09-09 15:08:12 +00001750
Chris Lattner22eb9722006-06-18 05:43:12 +00001751 // Read a character, advancing over it.
1752 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001753 tok::TokenKind Kind;
Mike Stump11289f42009-09-09 15:08:12 +00001754
Chris Lattner22eb9722006-06-18 05:43:12 +00001755 switch (Char) {
1756 case 0: // Null.
1757 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001758 if (CurPtr-1 == BufferEnd) {
1759 // Read the PP instance variable into an automatic variable, because
1760 // LexEndOfFile will often delete 'this'.
Chris Lattner02b436a2007-10-17 20:41:00 +00001761 Preprocessor *PPCache = PP;
Chris Lattner2183a6e2006-07-18 06:36:12 +00001762 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1763 return; // Got a token to return.
Chris Lattner02b436a2007-10-17 20:41:00 +00001764 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1765 return PPCache->Lex(Result);
Chris Lattner2183a6e2006-07-18 06:36:12 +00001766 }
Mike Stump11289f42009-09-09 15:08:12 +00001767
Chris Lattner6d27a162008-11-22 02:02:22 +00001768 if (!isLexingRawMode())
1769 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner146762e2007-07-20 16:59:19 +00001770 Result.setFlag(Token::LeadingSpace);
Chris Lattner4d963442008-10-12 04:05:48 +00001771 if (SkipWhitespace(Result, CurPtr))
1772 return; // KeepWhitespaceMode
Mike Stump11289f42009-09-09 15:08:12 +00001773
Chris Lattner22eb9722006-06-18 05:43:12 +00001774 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner3dfff972009-12-17 05:29:40 +00001775
1776 case 26: // DOS & CP/M EOF: "^Z".
1777 // If we're in Microsoft extensions mode, treat this as end of file.
1778 if (Features.Microsoft) {
1779 // Read the PP instance variable into an automatic variable, because
1780 // LexEndOfFile will often delete 'this'.
1781 Preprocessor *PPCache = PP;
1782 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1783 return; // Got a token to return.
1784 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1785 return PPCache->Lex(Result);
1786 }
1787 // If Microsoft extensions are disabled, this is just random garbage.
1788 Kind = tok::unknown;
1789 break;
1790
Chris Lattner22eb9722006-06-18 05:43:12 +00001791 case '\n':
1792 case '\r':
1793 // If we are inside a preprocessor directive and we see the end of line,
1794 // we know we are done with the directive, so return an EOM token.
1795 if (ParsingPreprocessorDirective) {
1796 // Done parsing the "line".
1797 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +00001798
Chris Lattner457fc152006-07-29 06:30:25 +00001799 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001800 SetCommentRetentionState(PP->getCommentRetentionState());
Mike Stump11289f42009-09-09 15:08:12 +00001801
Chris Lattner22eb9722006-06-18 05:43:12 +00001802 // Since we consumed a newline, we are back at the start of a line.
1803 IsAtStartOfLine = true;
Mike Stump11289f42009-09-09 15:08:12 +00001804
Chris Lattnerb11c3232008-10-12 04:51:35 +00001805 Kind = tok::eom;
Chris Lattner22eb9722006-06-18 05:43:12 +00001806 break;
1807 }
1808 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001809 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001810 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001811 Result.clearFlag(Token::LeadingSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001812
Chris Lattner4d963442008-10-12 04:05:48 +00001813 if (SkipWhitespace(Result, CurPtr))
1814 return; // KeepWhitespaceMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001815 goto LexNextToken; // GCC isn't tail call eliminating.
1816 case ' ':
1817 case '\t':
1818 case '\f':
1819 case '\v':
Chris Lattnerb9b85972007-07-22 06:29:05 +00001820 SkipHorizontalWhitespace:
Chris Lattner146762e2007-07-20 16:59:19 +00001821 Result.setFlag(Token::LeadingSpace);
Chris Lattner4d963442008-10-12 04:05:48 +00001822 if (SkipWhitespace(Result, CurPtr))
1823 return; // KeepWhitespaceMode
Chris Lattnerb9b85972007-07-22 06:29:05 +00001824
1825 SkipIgnoredUnits:
1826 CurPtr = BufferPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001827
Chris Lattnerb9b85972007-07-22 06:29:05 +00001828 // If the next token is obviously a // or /* */ comment, skip it efficiently
1829 // too (without going through the big switch stmt).
Chris Lattner58827712009-01-16 22:39:25 +00001830 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1831 Features.BCPLComment) {
Chris Lattner87d02082010-01-18 22:35:47 +00001832 if (SkipBCPLComment(Result, CurPtr+2))
1833 return; // There is a token to return.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001834 goto SkipIgnoredUnits;
Chris Lattner8637abd2008-10-12 03:22:02 +00001835 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner87d02082010-01-18 22:35:47 +00001836 if (SkipBlockComment(Result, CurPtr+2))
1837 return; // There is a token to return.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001838 goto SkipIgnoredUnits;
1839 } else if (isHorizontalWhitespace(*CurPtr)) {
1840 goto SkipHorizontalWhitespace;
1841 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001842 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner3dfff972009-12-17 05:29:40 +00001843
Chris Lattner2b15cf72008-01-03 17:58:54 +00001844 // C99 6.4.4.1: Integer Constants.
1845 // C99 6.4.4.2: Floating Constants.
1846 case '0': case '1': case '2': case '3': case '4':
1847 case '5': case '6': case '7': case '8': case '9':
1848 // Notify MIOpt that we read a non-whitespace/non-comment token.
1849 MIOpt.ReadToken();
1850 return LexNumericConstant(Result, CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001851
Chris Lattner2b15cf72008-01-03 17:58:54 +00001852 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Chris Lattner371ac8a2006-07-04 07:11:10 +00001853 // Notify MIOpt that we read a non-whitespace/non-comment token.
1854 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001855 Char = getCharAndSize(CurPtr, SizeTmp);
1856
1857 // Wide string literal.
1858 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001859 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1860 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001861
1862 // Wide character constant.
1863 if (Char == '\'')
1864 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1865 // FALL THROUGH, treating L like the start of an identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001866
Chris Lattner22eb9722006-06-18 05:43:12 +00001867 // C99 6.4.2: Identifiers.
1868 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1869 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1870 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1871 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1872 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1873 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1874 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1875 case 'v': case 'w': case 'x': case 'y': case 'z':
1876 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001877 // Notify MIOpt that we read a non-whitespace/non-comment token.
1878 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001879 return LexIdentifier(Result, CurPtr);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001880
1881 case '$': // $ in identifiers.
1882 if (Features.DollarIdents) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001883 if (!isLexingRawMode())
1884 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001885 // Notify MIOpt that we read a non-whitespace/non-comment token.
1886 MIOpt.ReadToken();
1887 return LexIdentifier(Result, CurPtr);
1888 }
Mike Stump11289f42009-09-09 15:08:12 +00001889
Chris Lattnerb11c3232008-10-12 04:51:35 +00001890 Kind = tok::unknown;
Chris Lattner2b15cf72008-01-03 17:58:54 +00001891 break;
Mike Stump11289f42009-09-09 15:08:12 +00001892
Chris Lattner22eb9722006-06-18 05:43:12 +00001893 // C99 6.4.4: Character Constants.
1894 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001895 // Notify MIOpt that we read a non-whitespace/non-comment token.
1896 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001897 return LexCharConstant(Result, CurPtr);
1898
1899 // C99 6.4.5: String Literals.
1900 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001901 // Notify MIOpt that we read a non-whitespace/non-comment token.
1902 MIOpt.ReadToken();
Chris Lattnerd3e98952006-10-06 05:22:26 +00001903 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001904
1905 // C99 6.4.6: Punctuators.
1906 case '?':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001907 Kind = tok::question;
Chris Lattner22eb9722006-06-18 05:43:12 +00001908 break;
1909 case '[':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001910 Kind = tok::l_square;
Chris Lattner22eb9722006-06-18 05:43:12 +00001911 break;
1912 case ']':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001913 Kind = tok::r_square;
Chris Lattner22eb9722006-06-18 05:43:12 +00001914 break;
1915 case '(':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001916 Kind = tok::l_paren;
Chris Lattner22eb9722006-06-18 05:43:12 +00001917 break;
1918 case ')':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001919 Kind = tok::r_paren;
Chris Lattner22eb9722006-06-18 05:43:12 +00001920 break;
1921 case '{':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001922 Kind = tok::l_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001923 break;
1924 case '}':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001925 Kind = tok::r_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001926 break;
1927 case '.':
1928 Char = getCharAndSize(CurPtr, SizeTmp);
1929 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001930 // Notify MIOpt that we read a non-whitespace/non-comment token.
1931 MIOpt.ReadToken();
1932
Chris Lattner22eb9722006-06-18 05:43:12 +00001933 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1934 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001935 Kind = tok::periodstar;
Chris Lattner22eb9722006-06-18 05:43:12 +00001936 CurPtr += SizeTmp;
1937 } else if (Char == '.' &&
1938 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001939 Kind = tok::ellipsis;
Chris Lattner22eb9722006-06-18 05:43:12 +00001940 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1941 SizeTmp2, Result);
1942 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001943 Kind = tok::period;
Chris Lattner22eb9722006-06-18 05:43:12 +00001944 }
1945 break;
1946 case '&':
1947 Char = getCharAndSize(CurPtr, SizeTmp);
1948 if (Char == '&') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001949 Kind = tok::ampamp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001950 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1951 } else if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001952 Kind = tok::ampequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001953 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1954 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001955 Kind = tok::amp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001956 }
1957 break;
Mike Stump11289f42009-09-09 15:08:12 +00001958 case '*':
Chris Lattner22eb9722006-06-18 05:43:12 +00001959 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001960 Kind = tok::starequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001961 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1962 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001963 Kind = tok::star;
Chris Lattner22eb9722006-06-18 05:43:12 +00001964 }
1965 break;
1966 case '+':
1967 Char = getCharAndSize(CurPtr, SizeTmp);
1968 if (Char == '+') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001969 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001970 Kind = tok::plusplus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001971 } else 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::plusequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001974 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001975 Kind = tok::plus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001976 }
1977 break;
1978 case '-':
1979 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001980 if (Char == '-') { // --
Chris Lattner22eb9722006-06-18 05:43:12 +00001981 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001982 Kind = tok::minusminus;
Mike Stump11289f42009-09-09 15:08:12 +00001983 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattnerb11c3232008-10-12 04:51:35 +00001984 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00001985 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1986 SizeTmp2, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001987 Kind = tok::arrowstar;
1988 } else if (Char == '>') { // ->
Chris Lattner22eb9722006-06-18 05:43:12 +00001989 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001990 Kind = tok::arrow;
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::minusequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001994 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001995 Kind = tok::minus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001996 }
1997 break;
1998 case '~':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001999 Kind = tok::tilde;
Chris Lattner22eb9722006-06-18 05:43:12 +00002000 break;
2001 case '!':
2002 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002003 Kind = tok::exclaimequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002004 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2005 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002006 Kind = tok::exclaim;
Chris Lattner22eb9722006-06-18 05:43:12 +00002007 }
2008 break;
2009 case '/':
2010 // 6.4.9: Comments
2011 Char = getCharAndSize(CurPtr, SizeTmp);
2012 if (Char == '/') { // BCPL comment.
Chris Lattner58827712009-01-16 22:39:25 +00002013 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
2014 // want to lex this as a comment. There is one problem with this though,
2015 // that in one particular corner case, this can change the behavior of the
2016 // resultant program. For example, In "foo //**/ bar", C89 would lex
2017 // this as "foo / bar" and langauges with BCPL comments would lex it as
2018 // "foo". Check to see if the character after the second slash is a '*'.
2019 // If so, we will lex that as a "/" instead of the start of a comment.
2020 if (Features.BCPLComment ||
2021 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
2022 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner87d02082010-01-18 22:35:47 +00002023 return; // There is a token to return.
Mike Stump11289f42009-09-09 15:08:12 +00002024
Chris Lattner58827712009-01-16 22:39:25 +00002025 // It is common for the tokens immediately after a // comment to be
2026 // whitespace (indentation for the next line). Instead of going through
2027 // the big switch, handle it efficiently now.
2028 goto SkipIgnoredUnits;
2029 }
2030 }
Mike Stump11289f42009-09-09 15:08:12 +00002031
Chris Lattner58827712009-01-16 22:39:25 +00002032 if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00002033 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner87d02082010-01-18 22:35:47 +00002034 return; // There is a token to return.
Chris Lattnere01e7582008-10-12 04:15:42 +00002035 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner58827712009-01-16 22:39:25 +00002036 }
Mike Stump11289f42009-09-09 15:08:12 +00002037
Chris Lattner58827712009-01-16 22:39:25 +00002038 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00002039 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002040 Kind = tok::slashequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002041 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002042 Kind = tok::slash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002043 }
2044 break;
2045 case '%':
2046 Char = getCharAndSize(CurPtr, SizeTmp);
2047 if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002048 Kind = tok::percentequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002049 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2050 } else if (Features.Digraphs && Char == '>') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002051 Kind = tok::r_brace; // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00002052 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2053 } else if (Features.Digraphs && Char == ':') {
2054 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00002055 Char = getCharAndSize(CurPtr, SizeTmp);
2056 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002057 Kind = tok::hashhash; // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00002058 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
2059 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00002060 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner2b271db2006-07-15 05:41:09 +00002061 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner6d27a162008-11-22 02:02:22 +00002062 if (!isLexingRawMode())
2063 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002064 Kind = tok::hashat;
Chris Lattner2534324a2009-03-18 20:58:27 +00002065 } else { // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00002066 // We parsed a # character. If this occurs at the start of the line,
2067 // it's actually the start of a preprocessing directive. Callback to
2068 // the preprocessor to handle it.
2069 // FIXME: -fpreprocessed mode??
Chris Lattnerff96dd02009-05-13 06:10:29 +00002070 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattner2534324a2009-03-18 20:58:27 +00002071 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner02b436a2007-10-17 20:41:00 +00002072 PP->HandleDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +00002073
Chris Lattner22eb9722006-06-18 05:43:12 +00002074 // As an optimization, if the preprocessor didn't switch lexers, tail
2075 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00002076 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002077 // Start a new token. If this is a #include or something, the PP may
2078 // want us starting at the beginning of the line again. If so, set
Chris Lattner1a9e8732010-04-12 23:04:41 +00002079 // the StartOfLine flag and clear LeadingSpace.
Chris Lattner22eb9722006-06-18 05:43:12 +00002080 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00002081 Result.setFlag(Token::StartOfLine);
Chris Lattner1a9e8732010-04-12 23:04:41 +00002082 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00002083 IsAtStartOfLine = false;
2084 }
2085 goto LexNextToken; // GCC isn't tail call eliminating.
2086 }
Mike Stump11289f42009-09-09 15:08:12 +00002087
Chris Lattner02b436a2007-10-17 20:41:00 +00002088 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002089 }
Mike Stump11289f42009-09-09 15:08:12 +00002090
Chris Lattner2534324a2009-03-18 20:58:27 +00002091 Kind = tok::hash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002092 }
2093 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002094 Kind = tok::percent;
Chris Lattner22eb9722006-06-18 05:43:12 +00002095 }
2096 break;
2097 case '<':
2098 Char = getCharAndSize(CurPtr, SizeTmp);
2099 if (ParsingFilename) {
Chris Lattnerb40289b2009-04-17 23:56:52 +00002100 return LexAngledStringLiteral(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00002101 } else if (Char == '<') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00002102 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
2103 if (After == '=') {
2104 Kind = tok::lesslessequal;
2105 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
2106 SizeTmp2, Result);
2107 } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) {
2108 // If this is actually a '<<<<<<<' version control conflict marker,
2109 // recognize it as such and recover nicely.
2110 goto LexNextToken;
2111 } else {
2112 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2113 Kind = tok::lessless;
2114 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002115 } else if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00002116 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002117 Kind = tok::lessequal;
2118 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00002119 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002120 Kind = tok::l_square;
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_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00002124 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002125 Kind = tok::less;
Chris Lattner22eb9722006-06-18 05:43:12 +00002126 }
2127 break;
2128 case '>':
2129 Char = getCharAndSize(CurPtr, SizeTmp);
2130 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00002131 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002132 Kind = tok::greaterequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002133 } else if (Char == '>') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00002134 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
2135 if (After == '=') {
2136 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
2137 SizeTmp2, Result);
2138 Kind = tok::greatergreaterequal;
2139 } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) {
2140 // If this is '>>>>>>>' and we're in a conflict marker, ignore it.
2141 goto LexNextToken;
2142 } else {
2143 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2144 Kind = tok::greatergreater;
2145 }
2146
Chris Lattner22eb9722006-06-18 05:43:12 +00002147 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002148 Kind = tok::greater;
Chris Lattner22eb9722006-06-18 05:43:12 +00002149 }
2150 break;
2151 case '^':
2152 Char = getCharAndSize(CurPtr, SizeTmp);
2153 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00002154 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002155 Kind = tok::caretequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002156 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002157 Kind = tok::caret;
Chris Lattner22eb9722006-06-18 05:43:12 +00002158 }
2159 break;
2160 case '|':
2161 Char = getCharAndSize(CurPtr, SizeTmp);
2162 if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002163 Kind = tok::pipeequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002164 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2165 } else if (Char == '|') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00002166 // If this is '|||||||' and we're in a conflict marker, ignore it.
2167 if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1))
2168 goto LexNextToken;
Chris Lattnerb11c3232008-10-12 04:51:35 +00002169 Kind = tok::pipepipe;
Chris Lattner22eb9722006-06-18 05:43:12 +00002170 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2171 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002172 Kind = tok::pipe;
Chris Lattner22eb9722006-06-18 05:43:12 +00002173 }
2174 break;
2175 case ':':
2176 Char = getCharAndSize(CurPtr, SizeTmp);
2177 if (Features.Digraphs && Char == '>') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002178 Kind = tok::r_square; // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00002179 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2180 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002181 Kind = tok::coloncolon;
Chris Lattner22eb9722006-06-18 05:43:12 +00002182 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump11289f42009-09-09 15:08:12 +00002183 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002184 Kind = tok::colon;
Chris Lattner22eb9722006-06-18 05:43:12 +00002185 }
2186 break;
2187 case ';':
Chris Lattnerb11c3232008-10-12 04:51:35 +00002188 Kind = tok::semi;
Chris Lattner22eb9722006-06-18 05:43:12 +00002189 break;
2190 case '=':
2191 Char = getCharAndSize(CurPtr, SizeTmp);
2192 if (Char == '=') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00002193 // If this is '=======' and we're in a conflict marker, ignore it.
2194 if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1))
2195 goto LexNextToken;
2196
Chris Lattnerb11c3232008-10-12 04:51:35 +00002197 Kind = tok::equalequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002198 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump11289f42009-09-09 15:08:12 +00002199 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002200 Kind = tok::equal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002201 }
2202 break;
2203 case ',':
Chris Lattnerb11c3232008-10-12 04:51:35 +00002204 Kind = tok::comma;
Chris Lattner22eb9722006-06-18 05:43:12 +00002205 break;
2206 case '#':
2207 Char = getCharAndSize(CurPtr, SizeTmp);
2208 if (Char == '#') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002209 Kind = tok::hashhash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002210 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00002211 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattnerb11c3232008-10-12 04:51:35 +00002212 Kind = tok::hashat;
Chris Lattner6d27a162008-11-22 02:02:22 +00002213 if (!isLexingRawMode())
2214 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner2b271db2006-07-15 05:41:09 +00002215 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002216 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +00002217 // We parsed a # character. If this occurs at the start of the line,
2218 // it's actually the start of a preprocessing directive. Callback to
2219 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00002220 // FIXME: -fpreprocessed mode??
Chris Lattnerff96dd02009-05-13 06:10:29 +00002221 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattner2534324a2009-03-18 20:58:27 +00002222 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner02b436a2007-10-17 20:41:00 +00002223 PP->HandleDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +00002224
Chris Lattner22eb9722006-06-18 05:43:12 +00002225 // As an optimization, if the preprocessor didn't switch lexers, tail
2226 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00002227 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002228 // Start a new token. If this is a #include or something, the PP may
2229 // want us starting at the beginning of the line again. If so, set
Chris Lattner1a9e8732010-04-12 23:04:41 +00002230 // the StartOfLine flag and clear LeadingSpace.
Chris Lattner22eb9722006-06-18 05:43:12 +00002231 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00002232 Result.setFlag(Token::StartOfLine);
Chris Lattner1a9e8732010-04-12 23:04:41 +00002233 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00002234 IsAtStartOfLine = false;
2235 }
2236 goto LexNextToken; // GCC isn't tail call eliminating.
2237 }
Chris Lattner02b436a2007-10-17 20:41:00 +00002238 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002239 }
Mike Stump11289f42009-09-09 15:08:12 +00002240
Chris Lattner2534324a2009-03-18 20:58:27 +00002241 Kind = tok::hash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002242 }
2243 break;
2244
Chris Lattner2b15cf72008-01-03 17:58:54 +00002245 case '@':
2246 // Objective C support.
2247 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattnerb11c3232008-10-12 04:51:35 +00002248 Kind = tok::at;
Chris Lattner2b15cf72008-01-03 17:58:54 +00002249 else
Chris Lattnerb11c3232008-10-12 04:51:35 +00002250 Kind = tok::unknown;
Chris Lattner2b15cf72008-01-03 17:58:54 +00002251 break;
Mike Stump11289f42009-09-09 15:08:12 +00002252
Chris Lattner22eb9722006-06-18 05:43:12 +00002253 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00002254 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00002255 // FALL THROUGH.
2256 default:
Chris Lattnerb11c3232008-10-12 04:51:35 +00002257 Kind = tok::unknown;
Chris Lattner041bef82006-07-11 05:52:53 +00002258 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00002259 }
Mike Stump11289f42009-09-09 15:08:12 +00002260
Chris Lattner371ac8a2006-07-04 07:11:10 +00002261 // Notify MIOpt that we read a non-whitespace/non-comment token.
2262 MIOpt.ReadToken();
2263
Chris Lattnerd01e2912006-06-18 16:22:51 +00002264 // Update the location of token as well as BufferPtr.
Chris Lattnerb11c3232008-10-12 04:51:35 +00002265 FormTokenWithChars(Result, CurPtr, Kind);
Chris Lattner22eb9722006-06-18 05:43:12 +00002266}