blob: dabc15951ff380a48733f1c0b98204e46cb84997 [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"
Douglas Gregor11583702010-08-25 17:04:25 +000030#include "clang/Lex/CodeCompletionHandler.h"
Chris Lattnerdc5c0552007-07-20 16:37:10 +000031#include "clang/Basic/SourceManager.h"
Douglas Gregoraf82e352010-07-20 20:18:03 +000032#include "llvm/ADT/StringSwitch.h"
Chris Lattner619c1742007-07-22 18:38:25 +000033#include "llvm/Support/Compiler.h"
Chris Lattner739e7392007-04-29 07:12:06 +000034#include "llvm/Support/MemoryBuffer.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000035#include <cctype>
Chris Lattner22eb9722006-06-18 05:43:12 +000036using namespace clang;
37
Chris Lattner3dfff972009-12-17 05:29:40 +000038static void InitCharacterInfo();
Chris Lattner22eb9722006-06-18 05:43:12 +000039
Chris Lattner4894f482007-10-07 08:47:24 +000040//===----------------------------------------------------------------------===//
41// Token Class Implementation
42//===----------------------------------------------------------------------===//
43
Mike Stump11289f42009-09-09 15:08:12 +000044/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
Chris Lattner4894f482007-10-07 08:47:24 +000045bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
Douglas Gregor90abb6d2008-12-01 21:46:47 +000046 if (IdentifierInfo *II = getIdentifierInfo())
47 return II->getObjCKeywordID() == objcKey;
48 return false;
Chris Lattner4894f482007-10-07 08:47:24 +000049}
50
51/// getObjCKeywordID - Return the ObjC keyword kind.
52tok::ObjCKeywordKind Token::getObjCKeywordID() const {
53 IdentifierInfo *specId = getIdentifierInfo();
54 return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
55}
56
Chris Lattner67671ed2007-12-13 01:59:49 +000057
Chris Lattner4894f482007-10-07 08:47:24 +000058//===----------------------------------------------------------------------===//
59// Lexer Class Implementation
60//===----------------------------------------------------------------------===//
61
Mike Stump11289f42009-09-09 15:08:12 +000062void Lexer::InitLexer(const char *BufStart, const char *BufPtr,
Chris Lattnerf76b9202009-01-17 06:55:17 +000063 const char *BufEnd) {
Chris Lattner3dfff972009-12-17 05:29:40 +000064 InitCharacterInfo();
Mike Stump11289f42009-09-09 15:08:12 +000065
Chris Lattnerf76b9202009-01-17 06:55:17 +000066 BufferStart = BufStart;
67 BufferPtr = BufPtr;
68 BufferEnd = BufEnd;
Mike Stump11289f42009-09-09 15:08:12 +000069
Chris Lattnerf76b9202009-01-17 06:55:17 +000070 assert(BufEnd[0] == 0 &&
71 "We assume that the input buffer has a null character at the end"
72 " to simplify lexing!");
Mike Stump11289f42009-09-09 15:08:12 +000073
Chris Lattnerf76b9202009-01-17 06:55:17 +000074 Is_PragmaLexer = false;
Chris Lattner7c027ee2009-12-14 06:16:57 +000075 IsInConflictMarker = false;
Douglas Gregor2436e712009-09-17 21:32:03 +000076
Chris Lattnerf76b9202009-01-17 06:55:17 +000077 // Start of the file is a start of line.
78 IsAtStartOfLine = true;
Mike Stump11289f42009-09-09 15:08:12 +000079
Chris Lattnerf76b9202009-01-17 06:55:17 +000080 // We are not after parsing a #.
81 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +000082
Chris Lattnerf76b9202009-01-17 06:55:17 +000083 // We are not after parsing #include.
84 ParsingFilename = false;
Mike Stump11289f42009-09-09 15:08:12 +000085
Chris Lattnerf76b9202009-01-17 06:55:17 +000086 // We are not in raw mode. Raw mode disables diagnostics and interpretation
87 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
88 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
89 // or otherwise skipping over tokens.
90 LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +000091
Chris Lattnerf76b9202009-01-17 06:55:17 +000092 // Default to not keeping comments.
93 ExtendedTokenMode = 0;
94}
95
Chris Lattner5965a282009-01-17 07:56:59 +000096/// Lexer constructor - Create a new lexer object for the specified buffer
97/// with the specified preprocessor managing the lexing process. This lexer
98/// assumes that the associated file buffer and Preprocessor objects will
99/// outlive it, so it doesn't take ownership of either of them.
Chris Lattner710bb872009-11-30 04:18:44 +0000100Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP)
Chris Lattnerc8090892009-01-17 08:03:42 +0000101 : PreprocessorLexer(&PP, FID),
102 FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
103 Features(PP.getLangOptions()) {
Mike Stump11289f42009-09-09 15:08:12 +0000104
Chris Lattner5965a282009-01-17 07:56:59 +0000105 InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
106 InputFile->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000107
Chris Lattner5965a282009-01-17 07:56:59 +0000108 // Default to keeping comments if the preprocessor wants them.
109 SetCommentRetentionState(PP.getCommentRetentionState());
110}
Chris Lattner4894f482007-10-07 08:47:24 +0000111
Chris Lattner02b436a2007-10-17 20:41:00 +0000112/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner50c90502008-10-12 01:15:46 +0000113/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
114/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner02b436a2007-10-17 20:41:00 +0000115Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattnerfcf64522009-01-17 07:42:27 +0000116 const char *BufStart, const char *BufPtr, const char *BufEnd)
Chris Lattner1abd2092009-01-17 03:48:08 +0000117 : FileLoc(fileloc), Features(features) {
Chris Lattnerf76b9202009-01-17 06:55:17 +0000118
Chris Lattnerf76b9202009-01-17 06:55:17 +0000119 InitLexer(BufStart, BufPtr, BufEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000120
Chris Lattner02b436a2007-10-17 20:41:00 +0000121 // We *are* in raw mode.
122 LexingRawMode = true;
Chris Lattner02b436a2007-10-17 20:41:00 +0000123}
124
Chris Lattner08354fe2009-01-17 07:35:14 +0000125/// Lexer constructor - Create a new raw lexer object. This object is only
126/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
127/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner710bb872009-11-30 04:18:44 +0000128Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile,
129 const SourceManager &SM, const LangOptions &features)
Chris Lattner08354fe2009-01-17 07:35:14 +0000130 : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
Chris Lattner08354fe2009-01-17 07:35:14 +0000131
Mike Stump11289f42009-09-09 15:08:12 +0000132 InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
Chris Lattner08354fe2009-01-17 07:35:14 +0000133 FromFile->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000134
Chris Lattner08354fe2009-01-17 07:35:14 +0000135 // We *are* in raw mode.
136 LexingRawMode = true;
137}
138
Chris Lattner757169b2009-01-17 08:27:52 +0000139/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
140/// _Pragma expansion. This has a variety of magic semantics that this method
141/// sets up. It returns a new'd Lexer that must be delete'd when done.
142///
143/// On entrance to this routine, TokStartLoc is a macro location which has a
144/// spelling loc that indicates the bytes to be lexed for the token and an
145/// instantiation location that indicates where all lexed tokens should be
146/// "expanded from".
147///
148/// FIXME: It would really be nice to make _Pragma just be a wrapper around a
149/// normal lexer that remaps tokens as they fly by. This would require making
150/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer
151/// interface that could handle this stuff. This would pull GetMappedTokenLoc
152/// out of the critical path of the lexer!
153///
Mike Stump11289f42009-09-09 15:08:12 +0000154Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
Chris Lattner9dc9c202009-02-15 20:52:18 +0000155 SourceLocation InstantiationLocStart,
156 SourceLocation InstantiationLocEnd,
Chris Lattner29a2a192009-01-19 06:46:35 +0000157 unsigned TokLen, Preprocessor &PP) {
Chris Lattner757169b2009-01-17 08:27:52 +0000158 SourceManager &SM = PP.getSourceManager();
Chris Lattner757169b2009-01-17 08:27:52 +0000159
160 // Create the lexer as if we were going to lex the file normally.
Chris Lattnercbc35ecb2009-01-19 07:46:45 +0000161 FileID SpellingFID = SM.getFileID(SpellingLoc);
Chris Lattner710bb872009-11-30 04:18:44 +0000162 const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID);
163 Lexer *L = new Lexer(SpellingFID, InputFile, PP);
Mike Stump11289f42009-09-09 15:08:12 +0000164
Chris Lattner757169b2009-01-17 08:27:52 +0000165 // Now that the lexer is created, change the start/end locations so that we
166 // just lex the subsection of the file that we want. This is lexing from a
167 // scratch buffer.
168 const char *StrData = SM.getCharacterData(SpellingLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000169
Chris Lattner757169b2009-01-17 08:27:52 +0000170 L->BufferPtr = StrData;
171 L->BufferEnd = StrData+TokLen;
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000172 assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
Chris Lattner757169b2009-01-17 08:27:52 +0000173
174 // Set the SourceLocation with the remapping information. This ensures that
175 // GetMappedTokenLoc will remap the tokens as they are lexed.
Chris Lattner4fa23622009-01-26 00:43:02 +0000176 L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
Chris Lattner9dc9c202009-02-15 20:52:18 +0000177 InstantiationLocStart,
178 InstantiationLocEnd, TokLen);
Mike Stump11289f42009-09-09 15:08:12 +0000179
Chris Lattner757169b2009-01-17 08:27:52 +0000180 // Ensure that the lexer thinks it is inside a directive, so that end \n will
181 // return an EOM token.
182 L->ParsingPreprocessorDirective = true;
Mike Stump11289f42009-09-09 15:08:12 +0000183
Chris Lattner757169b2009-01-17 08:27:52 +0000184 // This lexer really is for _Pragma.
185 L->Is_PragmaLexer = true;
186 return L;
187}
188
Chris Lattner02b436a2007-10-17 20:41:00 +0000189
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000190/// Stringify - Convert the specified string into a C string, with surrounding
191/// ""'s, and with escaped \ and " characters.
Chris Lattnerecc39e92006-07-15 05:23:31 +0000192std::string Lexer::Stringify(const std::string &Str, bool Charify) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000193 std::string Result = Str;
Chris Lattnerecc39e92006-07-15 05:23:31 +0000194 char Quote = Charify ? '\'' : '"';
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000195 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
Chris Lattnerecc39e92006-07-15 05:23:31 +0000196 if (Result[i] == '\\' || Result[i] == Quote) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000197 Result.insert(Result.begin()+i, '\\');
198 ++i; ++e;
199 }
200 }
Chris Lattnerecc39e92006-07-15 05:23:31 +0000201 return Result;
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000202}
203
Chris Lattner4c4a2452007-07-24 06:57:14 +0000204/// Stringify - Convert the specified string into a C string by escaping '\'
205/// and " characters. This does not add surrounding ""'s to the string.
206void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
207 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
208 if (Str[i] == '\\' || Str[i] == '"') {
209 Str.insert(Str.begin()+i, '\\');
210 ++i; ++e;
211 }
212 }
213}
214
Douglas Gregor562c1f92010-01-22 19:49:59 +0000215static bool isWhitespace(unsigned char c);
Chris Lattner22eb9722006-06-18 05:43:12 +0000216
Chris Lattner8e129c22007-10-17 21:18:47 +0000217/// MeasureTokenLength - Relex the token at the specified location and return
218/// its length in bytes in the input file. If the token needs cleaning (e.g.
219/// includes a trigraph or an escaped newline) then this count includes bytes
220/// that are part of that.
221unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
Chris Lattner184e65d2009-04-14 23:22:57 +0000222 const SourceManager &SM,
223 const LangOptions &LangOpts) {
Chris Lattner8e129c22007-10-17 21:18:47 +0000224 // TODO: this could be special cased for common tokens like identifiers, ')',
225 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
Mike Stump11289f42009-09-09 15:08:12 +0000226 // all obviously single-char tokens. This could use
Chris Lattner8e129c22007-10-17 21:18:47 +0000227 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
228 // something.
Chris Lattner4fa23622009-01-26 00:43:02 +0000229
230 // If this comes from a macro expansion, we really do want the macro name, not
231 // the token this macro expanded to.
Chris Lattnerd3817212009-01-26 22:24:27 +0000232 Loc = SM.getInstantiationLoc(Loc);
233 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
Douglas Gregore0fbb832010-03-16 00:06:06 +0000234 bool Invalid = false;
Benjamin Kramereb92dc02010-03-16 14:14:31 +0000235 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Douglas Gregore0fbb832010-03-16 00:06:06 +0000236 if (Invalid)
Douglas Gregor802b7762010-03-15 22:54:52 +0000237 return 0;
Benjamin Kramereb92dc02010-03-16 14:14:31 +0000238
239 const char *StrData = Buffer.data()+LocInfo.second;
Chris Lattner5509d532009-01-17 08:30:10 +0000240
Douglas Gregor562c1f92010-01-22 19:49:59 +0000241 if (isWhitespace(StrData[0]))
242 return 0;
243
Chris Lattner8e129c22007-10-17 21:18:47 +0000244 // Create a lexer starting at the beginning of this token.
Benjamin Kramereb92dc02010-03-16 14:14:31 +0000245 Lexer TheLexer(Loc, LangOpts, Buffer.begin(), StrData, Buffer.end());
Chris Lattnera3d4f162009-10-14 15:04:18 +0000246 TheLexer.SetCommentRetentionState(true);
Chris Lattner8e129c22007-10-17 21:18:47 +0000247 Token TheTok;
Chris Lattner50c90502008-10-12 01:15:46 +0000248 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner8e129c22007-10-17 21:18:47 +0000249 return TheTok.getLength();
250}
251
Douglas Gregorcd8bdd02010-07-22 20:22:31 +0000252SourceLocation Lexer::GetBeginningOfToken(SourceLocation Loc,
253 const SourceManager &SM,
254 const LangOptions &LangOpts) {
255 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
256 bool Invalid = false;
257 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
258 if (Invalid)
259 return Loc;
260
261 // Back up from the current location until we hit the beginning of a line
262 // (or the buffer). We'll relex from that point.
263 const char *BufStart = Buffer.data();
264 const char *StrData = BufStart+LocInfo.second;
265 if (StrData[0] == '\n' || StrData[0] == '\r')
266 return Loc;
267
268 const char *LexStart = StrData;
269 while (LexStart != BufStart) {
270 if (LexStart[0] == '\n' || LexStart[0] == '\r') {
271 ++LexStart;
272 break;
273 }
274
275 --LexStart;
276 }
277
278 // Create a lexer starting at the beginning of this token.
279 SourceLocation LexerStartLoc = Loc.getFileLocWithOffset(-LocInfo.second);
280 Lexer TheLexer(LexerStartLoc, LangOpts, BufStart, LexStart, Buffer.end());
281 TheLexer.SetCommentRetentionState(true);
282
283 // Lex tokens until we find the token that contains the source location.
284 Token TheTok;
285 do {
286 TheLexer.LexFromRawLexer(TheTok);
287
288 if (TheLexer.getBufferLocation() > StrData) {
289 // Lexing this token has taken the lexer past the source location we're
290 // looking for. If the current token encompasses our source location,
291 // return the beginning of that token.
292 if (TheLexer.getBufferLocation() - TheTok.getLength() <= StrData)
293 return TheTok.getLocation();
294
295 // We ended up skipping over the source location entirely, which means
296 // that it points into whitespace. We're done here.
297 break;
298 }
299 } while (TheTok.getKind() != tok::eof);
300
301 // We've passed our source location; just return the original source location.
302 return Loc;
303}
304
Douglas Gregoraf82e352010-07-20 20:18:03 +0000305namespace {
306 enum PreambleDirectiveKind {
307 PDK_Skipped,
308 PDK_StartIf,
309 PDK_EndIf,
310 PDK_Unknown
311 };
312}
313
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000314std::pair<unsigned, bool>
Douglas Gregor028d3e42010-08-09 20:45:32 +0000315Lexer::ComputePreamble(const llvm::MemoryBuffer *Buffer, unsigned MaxLines) {
Douglas Gregoraf82e352010-07-20 20:18:03 +0000316 // Create a lexer starting at the beginning of the file. Note that we use a
317 // "fake" file source location at offset 1 so that the lexer will track our
318 // position within the file.
319 const unsigned StartOffset = 1;
320 SourceLocation StartLoc = SourceLocation::getFromRawEncoding(StartOffset);
321 LangOptions LangOpts;
322 Lexer TheLexer(StartLoc, LangOpts, Buffer->getBufferStart(),
323 Buffer->getBufferStart(), Buffer->getBufferEnd());
324
325 bool InPreprocessorDirective = false;
326 Token TheTok;
327 Token IfStartTok;
328 unsigned IfCount = 0;
Douglas Gregor028d3e42010-08-09 20:45:32 +0000329 unsigned Line = 0;
330
Douglas Gregoraf82e352010-07-20 20:18:03 +0000331 do {
332 TheLexer.LexFromRawLexer(TheTok);
333
334 if (InPreprocessorDirective) {
335 // If we've hit the end of the file, we're done.
336 if (TheTok.getKind() == tok::eof) {
337 InPreprocessorDirective = false;
338 break;
339 }
340
341 // If we haven't hit the end of the preprocessor directive, skip this
342 // token.
343 if (!TheTok.isAtStartOfLine())
344 continue;
345
346 // We've passed the end of the preprocessor directive, and will look
347 // at this token again below.
348 InPreprocessorDirective = false;
349 }
350
Douglas Gregor028d3e42010-08-09 20:45:32 +0000351 // Keep track of the # of lines in the preamble.
352 if (TheTok.isAtStartOfLine()) {
353 ++Line;
354
355 // If we were asked to limit the number of lines in the preamble,
356 // and we're about to exceed that limit, we're done.
357 if (MaxLines && Line >= MaxLines)
358 break;
359 }
360
Douglas Gregoraf82e352010-07-20 20:18:03 +0000361 // Comments are okay; skip over them.
362 if (TheTok.getKind() == tok::comment)
363 continue;
364
365 if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) {
366 // This is the start of a preprocessor directive.
367 Token HashTok = TheTok;
368 InPreprocessorDirective = true;
369
370 // Figure out which direective this is. Since we're lexing raw tokens,
371 // we don't have an identifier table available. Instead, just look at
372 // the raw identifier to recognize and categorize preprocessor directives.
373 TheLexer.LexFromRawLexer(TheTok);
374 if (TheTok.getKind() == tok::identifier && !TheTok.needsCleaning()) {
375 const char *IdStart = Buffer->getBufferStart()
376 + TheTok.getLocation().getRawEncoding() - 1;
377 llvm::StringRef Keyword(IdStart, TheTok.getLength());
378 PreambleDirectiveKind PDK
379 = llvm::StringSwitch<PreambleDirectiveKind>(Keyword)
380 .Case("include", PDK_Skipped)
381 .Case("__include_macros", PDK_Skipped)
382 .Case("define", PDK_Skipped)
383 .Case("undef", PDK_Skipped)
384 .Case("line", PDK_Skipped)
385 .Case("error", PDK_Skipped)
386 .Case("pragma", PDK_Skipped)
387 .Case("import", PDK_Skipped)
388 .Case("include_next", PDK_Skipped)
389 .Case("warning", PDK_Skipped)
390 .Case("ident", PDK_Skipped)
391 .Case("sccs", PDK_Skipped)
392 .Case("assert", PDK_Skipped)
393 .Case("unassert", PDK_Skipped)
394 .Case("if", PDK_StartIf)
395 .Case("ifdef", PDK_StartIf)
396 .Case("ifndef", PDK_StartIf)
397 .Case("elif", PDK_Skipped)
398 .Case("else", PDK_Skipped)
399 .Case("endif", PDK_EndIf)
400 .Default(PDK_Unknown);
401
402 switch (PDK) {
403 case PDK_Skipped:
404 continue;
405
406 case PDK_StartIf:
407 if (IfCount == 0)
408 IfStartTok = HashTok;
409
410 ++IfCount;
411 continue;
412
413 case PDK_EndIf:
414 // Mismatched #endif. The preamble ends here.
415 if (IfCount == 0)
416 break;
417
418 --IfCount;
419 continue;
420
421 case PDK_Unknown:
422 // We don't know what this directive is; stop at the '#'.
423 break;
424 }
425 }
426
427 // We only end up here if we didn't recognize the preprocessor
428 // directive or it was one that can't occur in the preamble at this
429 // point. Roll back the current token to the location of the '#'.
430 InPreprocessorDirective = false;
431 TheTok = HashTok;
432 }
433
Douglas Gregor028d3e42010-08-09 20:45:32 +0000434 // We hit a token that we don't recognize as being in the
435 // "preprocessing only" part of the file, so we're no longer in
436 // the preamble.
Douglas Gregoraf82e352010-07-20 20:18:03 +0000437 break;
438 } while (true);
439
440 SourceLocation End = IfCount? IfStartTok.getLocation() : TheTok.getLocation();
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000441 return std::make_pair(End.getRawEncoding() - StartLoc.getRawEncoding(),
442 IfCount? IfStartTok.isAtStartOfLine()
443 : TheTok.isAtStartOfLine());
Douglas Gregoraf82e352010-07-20 20:18:03 +0000444}
445
Chris Lattner22eb9722006-06-18 05:43:12 +0000446//===----------------------------------------------------------------------===//
447// Character information.
448//===----------------------------------------------------------------------===//
449
Chris Lattner22eb9722006-06-18 05:43:12 +0000450enum {
451 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
452 CHAR_VERT_WS = 0x02, // '\r', '\n'
453 CHAR_LETTER = 0x04, // a-z,A-Z
454 CHAR_NUMBER = 0x08, // 0-9
455 CHAR_UNDER = 0x10, // _
456 CHAR_PERIOD = 0x20 // .
457};
458
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000459// Statically initialize CharInfo table based on ASCII character set
460// Reference: FreeBSD 7.2 /usr/share/misc/ascii
Chris Lattner3dfff972009-12-17 05:29:40 +0000461static const unsigned char CharInfo[256] =
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000462{
463// 0 NUL 1 SOH 2 STX 3 ETX
464// 4 EOT 5 ENQ 6 ACK 7 BEL
465 0 , 0 , 0 , 0 ,
466 0 , 0 , 0 , 0 ,
467// 8 BS 9 HT 10 NL 11 VT
468//12 NP 13 CR 14 SO 15 SI
469 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS,
470 CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 ,
471//16 DLE 17 DC1 18 DC2 19 DC3
472//20 DC4 21 NAK 22 SYN 23 ETB
473 0 , 0 , 0 , 0 ,
474 0 , 0 , 0 , 0 ,
475//24 CAN 25 EM 26 SUB 27 ESC
476//28 FS 29 GS 30 RS 31 US
477 0 , 0 , 0 , 0 ,
478 0 , 0 , 0 , 0 ,
479//32 SP 33 ! 34 " 35 #
480//36 $ 37 % 38 & 39 '
481 CHAR_HORZ_WS, 0 , 0 , 0 ,
482 0 , 0 , 0 , 0 ,
483//40 ( 41 ) 42 * 43 +
484//44 , 45 - 46 . 47 /
485 0 , 0 , 0 , 0 ,
486 0 , 0 , CHAR_PERIOD , 0 ,
487//48 0 49 1 50 2 51 3
488//52 4 53 5 54 6 55 7
489 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
490 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
491//56 8 57 9 58 : 59 ;
492//60 < 61 = 62 > 63 ?
493 CHAR_NUMBER , CHAR_NUMBER , 0 , 0 ,
494 0 , 0 , 0 , 0 ,
495//64 @ 65 A 66 B 67 C
496//68 D 69 E 70 F 71 G
497 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
498 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
499//72 H 73 I 74 J 75 K
500//76 L 77 M 78 N 79 O
501 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
502 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
503//80 P 81 Q 82 R 83 S
504//84 T 85 U 86 V 87 W
505 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
506 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
507//88 X 89 Y 90 Z 91 [
508//92 \ 93 ] 94 ^ 95 _
509 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
510 0 , 0 , 0 , CHAR_UNDER ,
511//96 ` 97 a 98 b 99 c
512//100 d 101 e 102 f 103 g
513 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
514 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
515//104 h 105 i 106 j 107 k
516//108 l 109 m 110 n 111 o
517 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
518 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
519//112 p 113 q 114 r 115 s
520//116 t 117 u 118 v 119 w
521 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
522 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
523//120 x 121 y 122 z 123 {
524//124 | 125 } 126 ~ 127 DEL
525 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
526 0 , 0 , 0 , 0
527};
528
Chris Lattner3dfff972009-12-17 05:29:40 +0000529static void InitCharacterInfo() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000530 static bool isInited = false;
531 if (isInited) return;
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000532 // check the statically-initialized CharInfo table
533 assert(CHAR_HORZ_WS == CharInfo[(int)' ']);
534 assert(CHAR_HORZ_WS == CharInfo[(int)'\t']);
535 assert(CHAR_HORZ_WS == CharInfo[(int)'\f']);
536 assert(CHAR_HORZ_WS == CharInfo[(int)'\v']);
537 assert(CHAR_VERT_WS == CharInfo[(int)'\n']);
538 assert(CHAR_VERT_WS == CharInfo[(int)'\r']);
539 assert(CHAR_UNDER == CharInfo[(int)'_']);
540 assert(CHAR_PERIOD == CharInfo[(int)'.']);
541 for (unsigned i = 'a'; i <= 'z'; ++i) {
542 assert(CHAR_LETTER == CharInfo[i]);
543 assert(CHAR_LETTER == CharInfo[i+'A'-'a']);
544 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000545 for (unsigned i = '0'; i <= '9'; ++i)
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000546 assert(CHAR_NUMBER == CharInfo[i]);
Steve Naroff04bc0182009-12-08 16:38:12 +0000547
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000548 isInited = true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000549}
550
Alexis Hunt79eb5462010-08-29 21:26:48 +0000551/// isIdentifierStart - Return true if this is the start character of an
552/// identifier, which is [a-zA-Z_].
553static inline bool isIdentifierStart(unsigned char c) {
554 return (CharInfo[c] & (CHAR_LETTER|CHAR_UNDER)) ? true : false;
555}
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000556
Chris Lattner22eb9722006-06-18 05:43:12 +0000557/// isIdentifierBody - Return true if this is the body character of an
558/// identifier, which is [a-zA-Z0-9_].
559static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000560 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000561}
562
563/// isHorizontalWhitespace - Return true if this character is horizontal
564/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
565static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000566 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000567}
568
569/// isWhitespace - Return true if this character is horizontal or vertical
570/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
571/// for '\0'.
572static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000573 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000574}
575
576/// isNumberBody - Return true if this is the body character of an
577/// preprocessing number, which is [a-zA-Z0-9_.].
578static inline bool isNumberBody(unsigned char c) {
Mike Stump11289f42009-09-09 15:08:12 +0000579 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000580 true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000581}
582
Chris Lattnerd01e2912006-06-18 16:22:51 +0000583
Chris Lattner22eb9722006-06-18 05:43:12 +0000584//===----------------------------------------------------------------------===//
585// Diagnostics forwarding code.
586//===----------------------------------------------------------------------===//
587
Chris Lattner619c1742007-07-22 18:38:25 +0000588/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
589/// lexer buffer was all instantiated at a single point, perform the mapping.
590/// This is currently only used for _Pragma implementation, so it is the slow
591/// path of the hot getSourceLocation method. Do not allow it to be inlined.
Benjamin Kramer5e738282009-11-14 16:36:57 +0000592static DISABLE_INLINE SourceLocation GetMappedTokenLoc(Preprocessor &PP,
593 SourceLocation FileLoc,
594 unsigned CharNo,
595 unsigned TokLen);
Chris Lattner619c1742007-07-22 18:38:25 +0000596static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
597 SourceLocation FileLoc,
Chris Lattner4fa23622009-01-26 00:43:02 +0000598 unsigned CharNo, unsigned TokLen) {
Chris Lattner9dc9c202009-02-15 20:52:18 +0000599 assert(FileLoc.isMacroID() && "Must be an instantiation");
Mike Stump11289f42009-09-09 15:08:12 +0000600
Chris Lattner619c1742007-07-22 18:38:25 +0000601 // Otherwise, we're lexing "mapped tokens". This is used for things like
602 // _Pragma handling. Combine the instantiation location of FileLoc with the
Chris Lattner53e384f2009-01-16 07:00:02 +0000603 // spelling location.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000604 SourceManager &SM = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000605
Chris Lattner8a425862009-01-16 07:36:28 +0000606 // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
Chris Lattner53e384f2009-01-16 07:00:02 +0000607 // characters come from spelling(FileLoc)+Offset.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000608 SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
Chris Lattner29a2a192009-01-19 06:46:35 +0000609 SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
Mike Stump11289f42009-09-09 15:08:12 +0000610
Chris Lattner9dc9c202009-02-15 20:52:18 +0000611 // Figure out the expansion loc range, which is the range covered by the
612 // original _Pragma(...) sequence.
613 std::pair<SourceLocation,SourceLocation> II =
614 SM.getImmediateInstantiationRange(FileLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000615
Chris Lattner9dc9c202009-02-15 20:52:18 +0000616 return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
Chris Lattner619c1742007-07-22 18:38:25 +0000617}
618
Chris Lattner22eb9722006-06-18 05:43:12 +0000619/// getSourceLocation - Return a source location identifier for the specified
620/// offset in the current file.
Chris Lattner4fa23622009-01-26 00:43:02 +0000621SourceLocation Lexer::getSourceLocation(const char *Loc,
622 unsigned TokLen) const {
Chris Lattner5d1c0272007-07-22 18:44:36 +0000623 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Chris Lattner4cca5ba2006-07-02 20:05:54 +0000624 "Location out of range for this buffer!");
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000625
626 // In the normal case, we're just lexing from a simple file buffer, return
627 // the file id from FileLoc with the offset specified.
Chris Lattner5d1c0272007-07-22 18:44:36 +0000628 unsigned CharNo = Loc-BufferStart;
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000629 if (FileLoc.isFileID())
Chris Lattner29a2a192009-01-19 06:46:35 +0000630 return FileLoc.getFileLocWithOffset(CharNo);
Mike Stump11289f42009-09-09 15:08:12 +0000631
Chris Lattnerd32480d2009-01-17 06:22:33 +0000632 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
633 // tokens are lexed from where the _Pragma was defined.
Chris Lattner02b436a2007-10-17 20:41:00 +0000634 assert(PP && "This doesn't work on raw lexers");
Chris Lattner4fa23622009-01-26 00:43:02 +0000635 return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
Chris Lattner22eb9722006-06-18 05:43:12 +0000636}
637
Chris Lattner22eb9722006-06-18 05:43:12 +0000638/// Diag - Forwarding function for diagnostics. This translate a source
639/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner427c9c12008-11-22 00:59:29 +0000640DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner907dfe92008-11-18 07:59:24 +0000641 return PP->Diag(getSourceLocation(Loc), DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000642}
643
644//===----------------------------------------------------------------------===//
645// Trigraph and Escaped Newline Handling Code.
646//===----------------------------------------------------------------------===//
647
648/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
649/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
650static char GetTrigraphCharForLetter(char Letter) {
651 switch (Letter) {
652 default: return 0;
653 case '=': return '#';
654 case ')': return ']';
655 case '(': return '[';
656 case '!': return '|';
657 case '\'': return '^';
658 case '>': return '}';
659 case '/': return '\\';
660 case '<': return '{';
661 case '-': return '~';
662 }
663}
664
665/// DecodeTrigraphChar - If the specified character is a legal trigraph when
666/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
667/// return the result character. Finally, emit a warning about trigraph use
668/// whether trigraphs are enabled or not.
669static char DecodeTrigraphChar(const char *CP, Lexer *L) {
670 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner907dfe92008-11-18 07:59:24 +0000671 if (!Res || !L) return Res;
Mike Stump11289f42009-09-09 15:08:12 +0000672
Chris Lattner907dfe92008-11-18 07:59:24 +0000673 if (!L->getFeatures().Trigraphs) {
Chris Lattner6d27a162008-11-22 02:02:22 +0000674 if (!L->isLexingRawMode())
675 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner907dfe92008-11-18 07:59:24 +0000676 return 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000677 }
Mike Stump11289f42009-09-09 15:08:12 +0000678
Chris Lattner6d27a162008-11-22 02:02:22 +0000679 if (!L->isLexingRawMode())
Benjamin Kramere8394df2010-08-11 14:47:12 +0000680 L->Diag(CP-2, diag::trigraph_converted) << llvm::StringRef(&Res, 1);
Chris Lattner22eb9722006-06-18 05:43:12 +0000681 return Res;
682}
683
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000684/// getEscapedNewLineSize - Return the size of the specified escaped newline,
685/// 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 +0000686/// trigraph equivalent on entry to this function.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000687unsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
688 unsigned Size = 0;
689 while (isWhitespace(Ptr[Size])) {
690 ++Size;
Mike Stump11289f42009-09-09 15:08:12 +0000691
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000692 if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
693 continue;
694
695 // If this is a \r\n or \n\r, skip the other half.
696 if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
697 Ptr[Size-1] != Ptr[Size])
698 ++Size;
Mike Stump11289f42009-09-09 15:08:12 +0000699
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000700 return Size;
Mike Stump11289f42009-09-09 15:08:12 +0000701 }
702
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000703 // Not an escaped newline, must be a \t or something else.
704 return 0;
705}
706
Chris Lattner38b2cde2009-04-18 22:27:02 +0000707/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
708/// them), skip over them and return the first non-escaped-newline found,
709/// otherwise return P.
710const char *Lexer::SkipEscapedNewLines(const char *P) {
711 while (1) {
712 const char *AfterEscape;
713 if (*P == '\\') {
714 AfterEscape = P+1;
715 } else if (*P == '?') {
716 // If not a trigraph for escape, bail out.
717 if (P[1] != '?' || P[2] != '/')
718 return P;
719 AfterEscape = P+3;
720 } else {
721 return P;
722 }
Mike Stump11289f42009-09-09 15:08:12 +0000723
Chris Lattner38b2cde2009-04-18 22:27:02 +0000724 unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
725 if (NewLineSize == 0) return P;
726 P = AfterEscape+NewLineSize;
727 }
728}
729
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000730
Chris Lattner22eb9722006-06-18 05:43:12 +0000731/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
732/// get its size, and return it. This is tricky in several cases:
733/// 1. If currently at the start of a trigraph, we warn about the trigraph,
734/// then either return the trigraph (skipping 3 chars) or the '?',
735/// depending on whether trigraphs are enabled or not.
736/// 2. If this is an escaped newline (potentially with whitespace between
737/// the backslash and newline), implicitly skip the newline and return
738/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000739/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000740///
741/// This handles the slow/uncommon case of the getCharAndSize method. Here we
742/// know that we can accumulate into Size, and that we have already incremented
743/// Ptr by Size bytes.
744///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000745/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
746/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000747///
748char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattner146762e2007-07-20 16:59:19 +0000749 Token *Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000750 // If we have a slash, look for an escaped newline.
751 if (Ptr[0] == '\\') {
752 ++Size;
753 ++Ptr;
754Slash:
755 // Common case, backslash-char where the char is not whitespace.
756 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump11289f42009-09-09 15:08:12 +0000757
Chris Lattnerc1835952009-06-23 05:15:06 +0000758 // See if we have optional whitespace characters between the slash and
759 // newline.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000760 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
761 // Remember that this token needs to be cleaned.
762 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000763
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000764 // Warn if there was whitespace between the backslash and newline.
Chris Lattnerc1835952009-06-23 05:15:06 +0000765 if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000766 Diag(Ptr, diag::backslash_newline_space);
Mike Stump11289f42009-09-09 15:08:12 +0000767
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000768 // Found backslash<whitespace><newline>. Parse the char after it.
769 Size += EscapedNewLineSize;
770 Ptr += EscapedNewLineSize;
771 // Use slow version to accumulate a correct size field.
772 return getCharAndSizeSlow(Ptr, Size, Tok);
773 }
Mike Stump11289f42009-09-09 15:08:12 +0000774
Chris Lattner22eb9722006-06-18 05:43:12 +0000775 // Otherwise, this is not an escaped newline, just return the slash.
776 return '\\';
777 }
Mike Stump11289f42009-09-09 15:08:12 +0000778
Chris Lattner22eb9722006-06-18 05:43:12 +0000779 // If this is a trigraph, process it.
780 if (Ptr[0] == '?' && Ptr[1] == '?') {
781 // If this is actually a legal trigraph (not something like "??x"), emit
782 // a trigraph warning. If so, and if trigraphs are enabled, return it.
783 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
784 // Remember that this token needs to be cleaned.
Chris Lattner146762e2007-07-20 16:59:19 +0000785 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000786
787 Ptr += 3;
788 Size += 3;
789 if (C == '\\') goto Slash;
790 return C;
791 }
792 }
Mike Stump11289f42009-09-09 15:08:12 +0000793
Chris Lattner22eb9722006-06-18 05:43:12 +0000794 // If this is neither, return a single character.
795 ++Size;
796 return *Ptr;
797}
798
Chris Lattnerd01e2912006-06-18 16:22:51 +0000799
Chris Lattner22eb9722006-06-18 05:43:12 +0000800/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
801/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
802/// and that we have already incremented Ptr by Size bytes.
803///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000804/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
805/// be updated to match.
806char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000807 const LangOptions &Features) {
808 // If we have a slash, look for an escaped newline.
809 if (Ptr[0] == '\\') {
810 ++Size;
811 ++Ptr;
812Slash:
813 // Common case, backslash-char where the char is not whitespace.
814 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump11289f42009-09-09 15:08:12 +0000815
Chris Lattner22eb9722006-06-18 05:43:12 +0000816 // See if we have optional whitespace characters followed by a newline.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000817 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
818 // Found backslash<whitespace><newline>. Parse the char after it.
819 Size += EscapedNewLineSize;
820 Ptr += EscapedNewLineSize;
Mike Stump11289f42009-09-09 15:08:12 +0000821
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000822 // Use slow version to accumulate a correct size field.
823 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
824 }
Mike Stump11289f42009-09-09 15:08:12 +0000825
Chris Lattner22eb9722006-06-18 05:43:12 +0000826 // Otherwise, this is not an escaped newline, just return the slash.
827 return '\\';
828 }
Mike Stump11289f42009-09-09 15:08:12 +0000829
Chris Lattner22eb9722006-06-18 05:43:12 +0000830 // If this is a trigraph, process it.
831 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
832 // If this is actually a legal trigraph (not something like "??x"), return
833 // it.
834 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
835 Ptr += 3;
836 Size += 3;
837 if (C == '\\') goto Slash;
838 return C;
839 }
840 }
Mike Stump11289f42009-09-09 15:08:12 +0000841
Chris Lattner22eb9722006-06-18 05:43:12 +0000842 // If this is neither, return a single character.
843 ++Size;
844 return *Ptr;
845}
846
Chris Lattner22eb9722006-06-18 05:43:12 +0000847//===----------------------------------------------------------------------===//
848// Helper methods for lexing.
849//===----------------------------------------------------------------------===//
850
Douglas Gregor3f4bea02010-07-26 21:36:20 +0000851/// \brief Routine that indiscriminately skips bytes in the source file.
852void Lexer::SkipBytes(unsigned Bytes, bool StartOfLine) {
853 BufferPtr += Bytes;
854 if (BufferPtr > BufferEnd)
855 BufferPtr = BufferEnd;
856 IsAtStartOfLine = StartOfLine;
857}
858
Chris Lattner146762e2007-07-20 16:59:19 +0000859void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000860 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
861 unsigned Size;
862 unsigned char C = *CurPtr++;
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000863 while (isIdentifierBody(C))
Chris Lattner22eb9722006-06-18 05:43:12 +0000864 C = *CurPtr++;
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000865
Chris Lattner22eb9722006-06-18 05:43:12 +0000866 --CurPtr; // Back up over the skipped character.
867
868 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
869 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000870 // FIXME: UCNs.
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000871 //
872 // TODO: Could merge these checks into a CharInfo flag to make the comparison
873 // cheaper
Chris Lattner22eb9722006-06-18 05:43:12 +0000874 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
875FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000876 const char *IdStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000877 FormTokenWithChars(Result, CurPtr, tok::identifier);
Mike Stump11289f42009-09-09 15:08:12 +0000878
Chris Lattner0f1f5052006-07-20 04:16:23 +0000879 // If we are in raw mode, return this identifier raw. There is no need to
880 // look up identifier information or attempt to macro expand it.
881 if (LexingRawMode) return;
Mike Stump11289f42009-09-09 15:08:12 +0000882
Chris Lattnercefc7682006-07-08 08:28:12 +0000883 // Fill in Result.IdentifierInfo, looking up the identifier in the
884 // identifier table.
Chris Lattner8256b972009-01-21 07:45:14 +0000885 IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
Mike Stump11289f42009-09-09 15:08:12 +0000886
Chris Lattner1f6c7fe2009-01-23 18:35:48 +0000887 // Change the kind of this identifier to the appropriate token kind, e.g.
888 // turning "for" into a keyword.
889 Result.setKind(II->getTokenID());
Mike Stump11289f42009-09-09 15:08:12 +0000890
Chris Lattnerc5a00062006-06-18 16:41:01 +0000891 // Finally, now that we know we have an identifier, pass this off to the
892 // preprocessor, which may macro expand it or something.
Chris Lattner8256b972009-01-21 07:45:14 +0000893 if (II->isHandleIdentifierCase())
Chris Lattnerad89ec02009-01-21 07:43:11 +0000894 PP->HandleIdentifier(Result);
895 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000896 }
Mike Stump11289f42009-09-09 15:08:12 +0000897
Chris Lattner22eb9722006-06-18 05:43:12 +0000898 // Otherwise, $,\,? in identifier found. Enter slower path.
Mike Stump11289f42009-09-09 15:08:12 +0000899
Chris Lattner22eb9722006-06-18 05:43:12 +0000900 C = getCharAndSize(CurPtr, Size);
901 while (1) {
902 if (C == '$') {
903 // If we hit a $ and they are not supported in identifiers, we are done.
904 if (!Features.DollarIdents) goto FinishIdentifier;
Mike Stump11289f42009-09-09 15:08:12 +0000905
Chris Lattner22eb9722006-06-18 05:43:12 +0000906 // Otherwise, emit a diagnostic and continue.
Chris Lattner6d27a162008-11-22 02:02:22 +0000907 if (!isLexingRawMode())
908 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000909 CurPtr = ConsumeChar(CurPtr, Size, Result);
910 C = getCharAndSize(CurPtr, Size);
911 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000912 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000913 // Found end of identifier.
914 goto FinishIdentifier;
915 }
916
917 // Otherwise, this character is good, consume it.
918 CurPtr = ConsumeChar(CurPtr, Size, Result);
919
920 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000921 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000922 CurPtr = ConsumeChar(CurPtr, Size, Result);
923 C = getCharAndSize(CurPtr, Size);
924 }
925 }
926}
927
Douglas Gregor759ef232010-08-30 14:50:47 +0000928/// isHexaLiteral - Return true if Start points to a hex constant.
Chris Lattner5f183aa2010-08-30 17:11:14 +0000929/// FIXME: This isn't correct, it will mislex:
930/// 0\
931/// x1234e+1
932/// in microsoft mode (where this is supposed to be several different tokens).
933static inline bool isHexaLiteral(const char *Start, const char *End) {
Douglas Gregor759ef232010-08-30 14:50:47 +0000934 return ((End - Start > 2) && Start[0] == '0' &&
935 (Start[1] == 'x' || Start[1] == 'X'));
936}
Chris Lattner22eb9722006-06-18 05:43:12 +0000937
Nate Begeman5eee9332008-04-14 02:26:39 +0000938/// LexNumericConstant - Lex the remainder of a integer or floating point
Chris Lattner22eb9722006-06-18 05:43:12 +0000939/// constant. From[-1] is the first character lexed. Return the end of the
940/// constant.
Chris Lattner146762e2007-07-20 16:59:19 +0000941void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000942 unsigned Size;
943 char C = getCharAndSize(CurPtr, Size);
944 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000945 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000946 CurPtr = ConsumeChar(CurPtr, Size, Result);
947 PrevCh = C;
948 C = getCharAndSize(CurPtr, Size);
949 }
Mike Stump11289f42009-09-09 15:08:12 +0000950
Chris Lattner22eb9722006-06-18 05:43:12 +0000951 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
Chris Lattner7a9e9e72010-08-30 17:09:08 +0000952 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e')) {
953 // If we are in Microsoft mode, don't continue if the constant is hex.
954 // For example, MSVC will accept the following as 3 tokens: 0x1234567e+1
955 if (!Features.Microsoft || !isHexaLiteral(BufferPtr, CurPtr))
956 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
957 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000958
959 // If we have a hex FP constant, continue.
Alexis Hunt91b78382010-01-10 23:37:56 +0000960 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
Chris Lattner7a9e9e72010-08-30 17:09:08 +0000961 !Features.CPlusPlus0x)
Chris Lattner22eb9722006-06-18 05:43:12 +0000962 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
Mike Stump11289f42009-09-09 15:08:12 +0000963
Chris Lattnerd01e2912006-06-18 16:22:51 +0000964 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000965 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000966 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000967 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000968}
969
970/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
971/// either " or L".
Chris Lattner4d963442008-10-12 04:05:48 +0000972void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000973 const char *NulCharacter = 0; // Does this string contain the \0 character?
Mike Stump11289f42009-09-09 15:08:12 +0000974
Chris Lattner22eb9722006-06-18 05:43:12 +0000975 char C = getAndAdvanceChar(CurPtr, Result);
976 while (C != '"') {
Chris Lattner52d96ac2010-05-30 23:27:38 +0000977 // Skip escaped characters. Escaped newlines will already be processed by
978 // getAndAdvanceChar.
979 if (C == '\\')
Chris Lattner22eb9722006-06-18 05:43:12 +0000980 C = getAndAdvanceChar(CurPtr, Result);
Douglas Gregorfe4a4102010-05-30 22:59:50 +0000981
Chris Lattner52d96ac2010-05-30 23:27:38 +0000982 if (C == '\n' || C == '\r' || // Newline.
Douglas Gregorfe4a4102010-05-30 22:59:50 +0000983 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Douglas Gregor11583702010-08-25 17:04:25 +0000984 if (C == 0 && PP && PP->isCodeCompletionFile(FileLoc))
985 PP->CodeCompleteNaturalLanguage();
986 else if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +0000987 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattnerb11c3232008-10-12 04:51:35 +0000988 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000989 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000990 }
Chris Lattner52d96ac2010-05-30 23:27:38 +0000991
992 if (C == 0)
993 NulCharacter = CurPtr-1;
Chris Lattner22eb9722006-06-18 05:43:12 +0000994 C = getAndAdvanceChar(CurPtr, Result);
995 }
Mike Stump11289f42009-09-09 15:08:12 +0000996
Chris Lattner5a78a022006-07-20 06:02:19 +0000997 // If a nul character existed in the string, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +0000998 if (NulCharacter && !isLexingRawMode())
999 Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +00001000
Chris Lattnerd01e2912006-06-18 16:22:51 +00001001 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner5a7971e2009-01-26 19:29:26 +00001002 const char *TokStart = BufferPtr;
Alexis Hunt79eb5462010-08-29 21:26:48 +00001003 tok::TokenKind Kind = Wide ? tok::wide_string_literal : tok::string_literal;
1004
1005 // FIXME: Handle UCNs
1006 unsigned Size;
Chris Lattner7a9e9e72010-08-30 17:09:08 +00001007 if (Features.CPlusPlus0x && PP &&
Alexis Hunt79eb5462010-08-29 21:26:48 +00001008 isIdentifierStart(getCharAndSize(CurPtr, Size))) {
1009 Result.makeUserDefinedLiteral(ExtraDataAllocator);
1010 Result.setFlagValue(Token::LiteralPortionClean, !Result.needsCleaning());
1011 Result.setKind(Kind);
1012 Result.setLiteralLength(CurPtr - BufferPtr);
1013
1014 // FIXME: We hack around the lexer's routines a lot here.
1015 BufferPtr = CurPtr;
1016 bool OldRawMode = LexingRawMode;
1017 LexingRawMode = true;
1018 LexIdentifier(Result, ConsumeChar(CurPtr, Size, Result));
1019 LexingRawMode = OldRawMode;
1020 PP->LookUpIdentifierInfo(Result, CurPtr);
1021
1022 CurPtr = BufferPtr;
1023 BufferPtr = TokStart;
1024 }
1025
1026 FormTokenWithChars(Result, CurPtr, Kind);
Chris Lattner5a7971e2009-01-26 19:29:26 +00001027 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +00001028}
1029
1030/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
1031/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattner146762e2007-07-20 16:59:19 +00001032void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001033 const char *NulCharacter = 0; // Does this string contain the \0 character?
Chris Lattnerb40289b2009-04-17 23:56:52 +00001034 const char *AfterLessPos = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001035 char C = getAndAdvanceChar(CurPtr, Result);
1036 while (C != '>') {
1037 // Skip escaped characters.
1038 if (C == '\\') {
1039 // Skip the escaped character.
1040 C = getAndAdvanceChar(CurPtr, Result);
1041 } else if (C == '\n' || C == '\r' || // Newline.
1042 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerb40289b2009-04-17 23:56:52 +00001043 // If the filename is unterminated, then it must just be a lone <
1044 // character. Return this as such.
1045 FormTokenWithChars(Result, AfterLessPos, tok::less);
Chris Lattner5a78a022006-07-20 06:02:19 +00001046 return;
Chris Lattner22eb9722006-06-18 05:43:12 +00001047 } else if (C == 0) {
1048 NulCharacter = CurPtr-1;
1049 }
1050 C = getAndAdvanceChar(CurPtr, Result);
1051 }
Mike Stump11289f42009-09-09 15:08:12 +00001052
Chris Lattner5a78a022006-07-20 06:02:19 +00001053 // If a nul character existed in the string, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +00001054 if (NulCharacter && !isLexingRawMode())
1055 Diag(NulCharacter, diag::null_in_string);
Mike Stump11289f42009-09-09 15:08:12 +00001056
Chris Lattnerd01e2912006-06-18 16:22:51 +00001057 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +00001058 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001059 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +00001060 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +00001061}
1062
1063
1064/// LexCharConstant - Lex the remainder of a character constant, after having
1065/// lexed either ' or L'.
Chris Lattner146762e2007-07-20 16:59:19 +00001066void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001067 const char *NulCharacter = 0; // Does this character contain the \0 character?
1068
Chris Lattner22eb9722006-06-18 05:43:12 +00001069 char C = getAndAdvanceChar(CurPtr, Result);
1070 if (C == '\'') {
Chris Lattnerd14705b2009-03-18 21:10:12 +00001071 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +00001072 Diag(BufferPtr, diag::err_empty_character);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001073 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +00001074 return;
Chris Lattner86851b82010-07-07 23:24:27 +00001075 }
1076
1077 while (C != '\'') {
1078 // Skip escaped characters.
1079 if (C == '\\') {
1080 // Skip the escaped character.
1081 // FIXME: UCN's
1082 C = getAndAdvanceChar(CurPtr, Result);
1083 } else if (C == '\n' || C == '\r' || // Newline.
1084 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Douglas Gregor11583702010-08-25 17:04:25 +00001085 if (C == 0 && PP && PP->isCodeCompletionFile(FileLoc))
1086 PP->CodeCompleteNaturalLanguage();
1087 else if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner86851b82010-07-07 23:24:27 +00001088 Diag(BufferPtr, diag::err_unterminated_char);
1089 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
1090 return;
1091 } else if (C == 0) {
1092 NulCharacter = CurPtr-1;
1093 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001094 C = getAndAdvanceChar(CurPtr, Result);
1095 }
Mike Stump11289f42009-09-09 15:08:12 +00001096
Chris Lattner86851b82010-07-07 23:24:27 +00001097 // If a nul character existed in the character, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +00001098 if (NulCharacter && !isLexingRawMode())
1099 Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +00001100
Chris Lattnerd01e2912006-06-18 16:22:51 +00001101 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +00001102 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001103 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Chris Lattner5a7971e2009-01-26 19:29:26 +00001104 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +00001105}
1106
1107/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
1108/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattner4d963442008-10-12 04:05:48 +00001109///
1110/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
1111///
1112bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001113 // Whitespace - Skip it, then return the token after the whitespace.
1114 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
1115 while (1) {
1116 // Skip horizontal whitespace very aggressively.
1117 while (isHorizontalWhitespace(Char))
1118 Char = *++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001119
Daniel Dunbar5c4cc092008-11-25 00:20:22 +00001120 // Otherwise if we have something other than whitespace, we're done.
Chris Lattner22eb9722006-06-18 05:43:12 +00001121 if (Char != '\n' && Char != '\r')
1122 break;
Mike Stump11289f42009-09-09 15:08:12 +00001123
Chris Lattner22eb9722006-06-18 05:43:12 +00001124 if (ParsingPreprocessorDirective) {
1125 // End of preprocessor directive line, let LexTokenInternal handle this.
1126 BufferPtr = CurPtr;
Chris Lattner4d963442008-10-12 04:05:48 +00001127 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001128 }
Mike Stump11289f42009-09-09 15:08:12 +00001129
Chris Lattner22eb9722006-06-18 05:43:12 +00001130 // ok, but handle newline.
1131 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001132 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001133 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001134 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001135 Char = *++CurPtr;
1136 }
1137
1138 // If this isn't immediately after a newline, there is leading space.
1139 char PrevChar = CurPtr[-1];
1140 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattner146762e2007-07-20 16:59:19 +00001141 Result.setFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001142
Chris Lattner4d963442008-10-12 04:05:48 +00001143 // If the client wants us to return whitespace, return it now.
1144 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001145 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4d963442008-10-12 04:05:48 +00001146 return true;
1147 }
Mike Stump11289f42009-09-09 15:08:12 +00001148
Chris Lattner22eb9722006-06-18 05:43:12 +00001149 BufferPtr = CurPtr;
Chris Lattner4d963442008-10-12 04:05:48 +00001150 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001151}
1152
1153// SkipBCPLComment - We have just read the // characters from input. Skip until
1154// we find the newline character thats terminate the comment. Then update
Chris Lattner87d02082010-01-18 22:35:47 +00001155/// BufferPtr and return.
1156///
1157/// If we're in KeepCommentMode or any CommentHandler has inserted
1158/// some tokens, this will store the first token and return true.
Chris Lattner146762e2007-07-20 16:59:19 +00001159bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001160 // If BCPL comments aren't explicitly enabled for this language, emit an
1161 // extension warning.
Chris Lattner6d27a162008-11-22 02:02:22 +00001162 if (!Features.BCPLComment && !isLexingRawMode()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +00001163 Diag(BufferPtr, diag::ext_bcpl_comment);
Mike Stump11289f42009-09-09 15:08:12 +00001164
Chris Lattner22eb9722006-06-18 05:43:12 +00001165 // Mark them enabled so we only emit one warning for this translation
1166 // unit.
1167 Features.BCPLComment = true;
1168 }
Mike Stump11289f42009-09-09 15:08:12 +00001169
Chris Lattner22eb9722006-06-18 05:43:12 +00001170 // Scan over the body of the comment. The common case, when scanning, is that
1171 // the comment contains normal ascii characters with nothing interesting in
1172 // them. As such, optimize for this case with the inner loop.
1173 char C;
1174 do {
1175 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +00001176 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
1177 // If we find a \n character, scan backwards, checking to see if it's an
1178 // escaped newline, like we do for block comments.
Mike Stump11289f42009-09-09 15:08:12 +00001179
Chris Lattner22eb9722006-06-18 05:43:12 +00001180 // Skip over characters in the fast loop.
1181 while (C != 0 && // Potentially EOF.
1182 C != '\\' && // Potentially escaped newline.
1183 C != '?' && // Potentially trigraph.
1184 C != '\n' && C != '\r') // Newline or DOS-style newline.
1185 C = *++CurPtr;
1186
1187 // If this is a newline, we're done.
1188 if (C == '\n' || C == '\r')
1189 break; // Found the newline? Break out!
Mike Stump11289f42009-09-09 15:08:12 +00001190
Chris Lattner22eb9722006-06-18 05:43:12 +00001191 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnere141a9e2008-12-12 07:34:39 +00001192 // properly decode the character. Read it in raw mode to avoid emitting
1193 // diagnostics about things like trigraphs. If we see an escaped newline,
1194 // we'll handle it below.
Chris Lattner22eb9722006-06-18 05:43:12 +00001195 const char *OldPtr = CurPtr;
Chris Lattnere141a9e2008-12-12 07:34:39 +00001196 bool OldRawMode = isLexingRawMode();
1197 LexingRawMode = true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001198 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnere141a9e2008-12-12 07:34:39 +00001199 LexingRawMode = OldRawMode;
Chris Lattnerecdaf402009-04-05 00:26:41 +00001200
1201 // If the char that we finally got was a \n, then we must have had something
1202 // like \<newline><newline>. We don't want to have consumed the second
1203 // newline, we want CurPtr, to end up pointing to it down below.
1204 if (C == '\n' || C == '\r') {
1205 --CurPtr;
1206 C = 'x'; // doesn't matter what this is.
1207 }
Mike Stump11289f42009-09-09 15:08:12 +00001208
Chris Lattner22eb9722006-06-18 05:43:12 +00001209 // If we read multiple characters, and one of those characters was a \r or
Chris Lattnerff591e22007-06-09 06:07:22 +00001210 // \n, then we had an escaped newline within the comment. Emit diagnostic
1211 // unless the next line is also a // comment.
1212 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001213 for (; OldPtr != CurPtr; ++OldPtr)
1214 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnerff591e22007-06-09 06:07:22 +00001215 // Okay, we found a // comment that ends in a newline, if the next
1216 // line is also a // comment, but has spaces, don't emit a diagnostic.
1217 if (isspace(C)) {
1218 const char *ForwardPtr = CurPtr;
1219 while (isspace(*ForwardPtr)) // Skip whitespace.
1220 ++ForwardPtr;
1221 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
1222 break;
1223 }
Mike Stump11289f42009-09-09 15:08:12 +00001224
Chris Lattner6d27a162008-11-22 02:02:22 +00001225 if (!isLexingRawMode())
1226 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Chris Lattnercb283342006-06-18 06:48:37 +00001227 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00001228 }
1229 }
Mike Stump11289f42009-09-09 15:08:12 +00001230
Douglas Gregor11583702010-08-25 17:04:25 +00001231 if (CurPtr == BufferEnd+1) {
1232 if (PP && PP->isCodeCompletionFile(FileLoc))
1233 PP->CodeCompleteNaturalLanguage();
1234
1235 --CurPtr;
1236 break;
1237 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001238 } while (C != '\n' && C != '\r');
1239
Chris Lattner93ddf802010-02-03 21:06:21 +00001240 // Found but did not consume the newline. Notify comment handlers about the
1241 // comment unless we're in a #if 0 block.
1242 if (PP && !isLexingRawMode() &&
1243 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
1244 getSourceLocation(CurPtr)))) {
Chris Lattner87d02082010-01-18 22:35:47 +00001245 BufferPtr = CurPtr;
1246 return true; // A token has to be returned.
1247 }
Mike Stump11289f42009-09-09 15:08:12 +00001248
Chris Lattner457fc152006-07-29 06:30:25 +00001249 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +00001250 if (inKeepCommentMode())
Chris Lattner457fc152006-07-29 06:30:25 +00001251 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001252
1253 // If we are inside a preprocessor directive and we see the end of line,
1254 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +00001255 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001256 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001257 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001258 }
Mike Stump11289f42009-09-09 15:08:12 +00001259
Chris Lattner22eb9722006-06-18 05:43:12 +00001260 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner87e97ea2008-10-12 00:23:07 +00001261 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattner4d963442008-10-12 04:05:48 +00001262 // contribute to another token), it isn't needed for correctness. Note that
1263 // this is ok even in KeepWhitespaceMode, because we would have returned the
1264 /// comment above in that mode.
Chris Lattner22eb9722006-06-18 05:43:12 +00001265 ++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001266
Chris Lattner22eb9722006-06-18 05:43:12 +00001267 // The next returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001268 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001269 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001270 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001271 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001272 return false;
Chris Lattner457fc152006-07-29 06:30:25 +00001273}
Chris Lattner22eb9722006-06-18 05:43:12 +00001274
Chris Lattner457fc152006-07-29 06:30:25 +00001275/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
1276/// an appropriate way and return it.
Chris Lattner146762e2007-07-20 16:59:19 +00001277bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001278 // If we're not in a preprocessor directive, just return the // comment
1279 // directly.
1280 FormTokenWithChars(Result, CurPtr, tok::comment);
Mike Stump11289f42009-09-09 15:08:12 +00001281
Chris Lattnerb11c3232008-10-12 04:51:35 +00001282 if (!ParsingPreprocessorDirective)
1283 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001284
Chris Lattnerb11c3232008-10-12 04:51:35 +00001285 // If this BCPL-style comment is in a macro definition, transmogrify it into
1286 // a C-style block comment.
Douglas Gregordc970f02010-03-16 22:30:13 +00001287 bool Invalid = false;
1288 std::string Spelling = PP->getSpelling(Result, &Invalid);
1289 if (Invalid)
1290 return true;
1291
Chris Lattnerb11c3232008-10-12 04:51:35 +00001292 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
1293 Spelling[1] = '*'; // Change prefix to "/*".
1294 Spelling += "*/"; // add suffix.
Mike Stump11289f42009-09-09 15:08:12 +00001295
Chris Lattnerb11c3232008-10-12 04:51:35 +00001296 Result.setKind(tok::comment);
Chris Lattner5a7971e2009-01-26 19:29:26 +00001297 PP->CreateString(&Spelling[0], Spelling.size(), Result,
1298 Result.getLocation());
Chris Lattnere01e7582008-10-12 04:15:42 +00001299 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001300}
1301
Chris Lattnercb283342006-06-18 06:48:37 +00001302/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
1303/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner89770572008-12-12 07:14:34 +00001304/// diagnostic if so. We know that the newline is inside of a block comment.
Mike Stump11289f42009-09-09 15:08:12 +00001305static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
Chris Lattner1f583052006-06-18 06:53:56 +00001306 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001307 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Mike Stump11289f42009-09-09 15:08:12 +00001308
Chris Lattner22eb9722006-06-18 05:43:12 +00001309 // Back up off the newline.
1310 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001311
Chris Lattner22eb9722006-06-18 05:43:12 +00001312 // If this is a two-character newline sequence, skip the other character.
1313 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
1314 // \n\n or \r\r -> not escaped newline.
1315 if (CurPtr[0] == CurPtr[1])
1316 return false;
1317 // \n\r or \r\n -> skip the newline.
1318 --CurPtr;
1319 }
Mike Stump11289f42009-09-09 15:08:12 +00001320
Chris Lattner22eb9722006-06-18 05:43:12 +00001321 // If we have horizontal whitespace, skip over it. We allow whitespace
1322 // between the slash and newline.
1323 bool HasSpace = false;
1324 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
1325 --CurPtr;
1326 HasSpace = true;
1327 }
Mike Stump11289f42009-09-09 15:08:12 +00001328
Chris Lattner22eb9722006-06-18 05:43:12 +00001329 // If we have a slash, we know this is an escaped newline.
1330 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +00001331 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001332 } else {
1333 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +00001334 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
1335 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +00001336 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001337
Chris Lattnercb283342006-06-18 06:48:37 +00001338 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +00001339 CurPtr -= 2;
1340
1341 // If no trigraphs are enabled, warn that we ignored this trigraph and
1342 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +00001343 if (!L->getFeatures().Trigraphs) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001344 if (!L->isLexingRawMode())
1345 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +00001346 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001347 }
Chris Lattner6d27a162008-11-22 02:02:22 +00001348 if (!L->isLexingRawMode())
1349 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001350 }
Mike Stump11289f42009-09-09 15:08:12 +00001351
Chris Lattner22eb9722006-06-18 05:43:12 +00001352 // Warn about having an escaped newline between the */ characters.
Chris Lattner6d27a162008-11-22 02:02:22 +00001353 if (!L->isLexingRawMode())
1354 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Mike Stump11289f42009-09-09 15:08:12 +00001355
Chris Lattner22eb9722006-06-18 05:43:12 +00001356 // If there was space between the backslash and newline, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +00001357 if (HasSpace && !L->isLexingRawMode())
1358 L->Diag(CurPtr, diag::backslash_newline_space);
Mike Stump11289f42009-09-09 15:08:12 +00001359
Chris Lattnercb283342006-06-18 06:48:37 +00001360 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001361}
1362
Chris Lattneraded4a92006-10-27 04:42:31 +00001363#ifdef __SSE2__
1364#include <emmintrin.h>
Chris Lattner9f6604f2006-10-30 20:01:22 +00001365#elif __ALTIVEC__
1366#include <altivec.h>
1367#undef bool
Chris Lattneraded4a92006-10-27 04:42:31 +00001368#endif
1369
Chris Lattner22eb9722006-06-18 05:43:12 +00001370/// SkipBlockComment - We have just read the /* characters from input. Read
1371/// until we find the */ characters that terminate the comment. Note that we
1372/// don't bother decoding trigraphs or escaped newlines in block comments,
1373/// because they cannot cause the comment to end. The only thing that can
1374/// happen is the comment could end with an escaped newline between the */ end
1375/// of comment.
Chris Lattnere01e7582008-10-12 04:15:42 +00001376///
Chris Lattner87d02082010-01-18 22:35:47 +00001377/// If we're in KeepCommentMode or any CommentHandler has inserted
1378/// some tokens, this will store the first token and return true.
Chris Lattner146762e2007-07-20 16:59:19 +00001379bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001380 // Scan one character past where we should, looking for a '/' character. Once
1381 // we find it, check to see if it was preceeded by a *. This common
1382 // optimization helps people who like to put a lot of * characters in their
1383 // comments.
Chris Lattnerc850ad62007-07-21 23:43:37 +00001384
1385 // The first character we get with newlines and trigraphs skipped to handle
1386 // the degenerate /*/ case below correctly if the * has an escaped newline
1387 // after it.
1388 unsigned CharSize;
1389 unsigned char C = getCharAndSize(CurPtr, CharSize);
1390 CurPtr += CharSize;
Chris Lattner22eb9722006-06-18 05:43:12 +00001391 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner561aabd2010-05-16 19:54:05 +00001392 if (!isLexingRawMode() &&
1393 !PP->isCodeCompletionFile(FileLoc))
Chris Lattner7c2e9802008-10-12 01:31:51 +00001394 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner99e7d232008-10-12 04:19:49 +00001395 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001396
Chris Lattner99e7d232008-10-12 04:19:49 +00001397 // KeepWhitespaceMode should return this broken comment as a token. Since
1398 // it isn't a well formed comment, just return it as an 'unknown' token.
1399 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001400 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner99e7d232008-10-12 04:19:49 +00001401 return true;
1402 }
Mike Stump11289f42009-09-09 15:08:12 +00001403
Chris Lattner99e7d232008-10-12 04:19:49 +00001404 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001405 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001406 }
Mike Stump11289f42009-09-09 15:08:12 +00001407
Chris Lattnerc850ad62007-07-21 23:43:37 +00001408 // Check to see if the first character after the '/*' is another /. If so,
1409 // then this slash does not end the block comment, it is part of it.
1410 if (C == '/')
1411 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001412
Chris Lattner22eb9722006-06-18 05:43:12 +00001413 while (1) {
Chris Lattner6cc3e362006-10-27 04:12:35 +00001414 // Skip over all non-interesting characters until we find end of buffer or a
1415 // (probably ending) '/' character.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001416 if (CurPtr + 24 < BufferEnd) {
1417 // While not aligned to a 16-byte boundary.
1418 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1419 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001420
Chris Lattner6cc3e362006-10-27 04:12:35 +00001421 if (C == '/') goto FoundSlash;
Chris Lattneraded4a92006-10-27 04:42:31 +00001422
1423#ifdef __SSE2__
1424 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1425 '/', '/', '/', '/', '/', '/', '/', '/');
1426 while (CurPtr+16 <= BufferEnd &&
1427 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1428 CurPtr += 16;
Chris Lattner9f6604f2006-10-30 20:01:22 +00001429#elif __ALTIVEC__
1430 __vector unsigned char Slashes = {
Mike Stump11289f42009-09-09 15:08:12 +00001431 '/', '/', '/', '/', '/', '/', '/', '/',
Chris Lattner9f6604f2006-10-30 20:01:22 +00001432 '/', '/', '/', '/', '/', '/', '/', '/'
1433 };
1434 while (CurPtr+16 <= BufferEnd &&
1435 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1436 CurPtr += 16;
Mike Stump11289f42009-09-09 15:08:12 +00001437#else
Chris Lattneraded4a92006-10-27 04:42:31 +00001438 // Scan for '/' quickly. Many block comments are very large.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001439 while (CurPtr[0] != '/' &&
1440 CurPtr[1] != '/' &&
1441 CurPtr[2] != '/' &&
1442 CurPtr[3] != '/' &&
1443 CurPtr+4 < BufferEnd) {
1444 CurPtr += 4;
1445 }
Chris Lattneraded4a92006-10-27 04:42:31 +00001446#endif
Mike Stump11289f42009-09-09 15:08:12 +00001447
Chris Lattneraded4a92006-10-27 04:42:31 +00001448 // It has to be one of the bytes scanned, increment to it and read one.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001449 C = *CurPtr++;
1450 }
Mike Stump11289f42009-09-09 15:08:12 +00001451
Chris Lattneraded4a92006-10-27 04:42:31 +00001452 // Loop to scan the remainder.
Chris Lattner22eb9722006-06-18 05:43:12 +00001453 while (C != '/' && C != '\0')
1454 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001455
Chris Lattner6cc3e362006-10-27 04:12:35 +00001456 FoundSlash:
Chris Lattner22eb9722006-06-18 05:43:12 +00001457 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001458 if (CurPtr[-2] == '*') // We found the final */. We're done!
1459 break;
Mike Stump11289f42009-09-09 15:08:12 +00001460
Chris Lattner22eb9722006-06-18 05:43:12 +00001461 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +00001462 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001463 // We found the final */, though it had an escaped newline between the
1464 // * and /. We're done!
1465 break;
1466 }
1467 }
1468 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1469 // If this is a /* inside of the comment, emit a warning. Don't do this
1470 // if this is a /*/, which will end the comment. This misses cases with
1471 // embedded escaped newlines, but oh well.
Chris Lattner6d27a162008-11-22 02:02:22 +00001472 if (!isLexingRawMode())
1473 Diag(CurPtr-1, diag::warn_nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001474 }
1475 } else if (C == 0 && CurPtr == BufferEnd+1) {
Douglas Gregor11583702010-08-25 17:04:25 +00001476 if (PP && PP->isCodeCompletionFile(FileLoc))
1477 PP->CodeCompleteNaturalLanguage();
1478 else if (!isLexingRawMode())
Chris Lattner6d27a162008-11-22 02:02:22 +00001479 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001480 // Note: the user probably forgot a */. We could continue immediately
1481 // after the /*, but this would involve lexing a lot of what really is the
1482 // comment, which surely would confuse the parser.
Chris Lattner99e7d232008-10-12 04:19:49 +00001483 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001484
Chris Lattner99e7d232008-10-12 04:19:49 +00001485 // KeepWhitespaceMode should return this broken comment as a token. Since
1486 // it isn't a well formed comment, just return it as an 'unknown' token.
1487 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001488 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner99e7d232008-10-12 04:19:49 +00001489 return true;
1490 }
Mike Stump11289f42009-09-09 15:08:12 +00001491
Chris Lattner99e7d232008-10-12 04:19:49 +00001492 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001493 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001494 }
1495 C = *CurPtr++;
1496 }
Mike Stump11289f42009-09-09 15:08:12 +00001497
Chris Lattner93ddf802010-02-03 21:06:21 +00001498 // Notify comment handlers about the comment unless we're in a #if 0 block.
1499 if (PP && !isLexingRawMode() &&
1500 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
1501 getSourceLocation(CurPtr)))) {
Chris Lattner87d02082010-01-18 22:35:47 +00001502 BufferPtr = CurPtr;
1503 return true; // A token has to be returned.
1504 }
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001505
Chris Lattner457fc152006-07-29 06:30:25 +00001506 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +00001507 if (inKeepCommentMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001508 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattnere01e7582008-10-12 04:15:42 +00001509 return true;
Chris Lattner457fc152006-07-29 06:30:25 +00001510 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001511
1512 // It is common for the tokens immediately after a /**/ comment to be
1513 // whitespace. Instead of going through the big switch, handle it
Chris Lattner4d963442008-10-12 04:05:48 +00001514 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1515 // have already returned above with the comment as a token.
Chris Lattner22eb9722006-06-18 05:43:12 +00001516 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner146762e2007-07-20 16:59:19 +00001517 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +00001518 SkipWhitespace(Result, CurPtr+1);
Chris Lattnere01e7582008-10-12 04:15:42 +00001519 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001520 }
1521
1522 // Otherwise, just return so that the next character will be lexed as a token.
1523 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001524 Result.setFlag(Token::LeadingSpace);
Chris Lattnere01e7582008-10-12 04:15:42 +00001525 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001526}
1527
1528//===----------------------------------------------------------------------===//
1529// Primary Lexing Entry Points
1530//===----------------------------------------------------------------------===//
1531
Chris Lattner22eb9722006-06-18 05:43:12 +00001532/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1533/// uninterpreted string. This switches the lexer out of directive mode.
1534std::string Lexer::ReadToEndOfLine() {
1535 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1536 "Must be in a preprocessing directive!");
1537 std::string Result;
Chris Lattner146762e2007-07-20 16:59:19 +00001538 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001539
1540 // CurPtr - Cache BufferPtr in an automatic variable.
1541 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001542 while (1) {
1543 char Char = getAndAdvanceChar(CurPtr, Tmp);
1544 switch (Char) {
1545 default:
1546 Result += Char;
1547 break;
1548 case 0: // Null.
1549 // Found end of file?
1550 if (CurPtr-1 != BufferEnd) {
1551 // Nope, normal character, continue.
1552 Result += Char;
1553 break;
1554 }
1555 // FALL THROUGH.
1556 case '\r':
1557 case '\n':
1558 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1559 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1560 BufferPtr = CurPtr-1;
Mike Stump11289f42009-09-09 15:08:12 +00001561
Chris Lattner22eb9722006-06-18 05:43:12 +00001562 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +00001563 Lex(Tmp);
Douglas Gregor11583702010-08-25 17:04:25 +00001564 if (Tmp.is(tok::code_completion)) {
1565 if (PP && PP->getCodeCompletionHandler())
1566 PP->getCodeCompletionHandler()->CodeCompleteNaturalLanguage();
1567 Lex(Tmp);
1568 }
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001569 assert(Tmp.is(tok::eom) && "Unexpected token!");
Mike Stump11289f42009-09-09 15:08:12 +00001570
Chris Lattner22eb9722006-06-18 05:43:12 +00001571 // Finally, we're done, return the string we found.
1572 return Result;
1573 }
1574 }
1575}
1576
1577/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1578/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +00001579/// This returns true if Result contains a token, false if PP.Lex should be
1580/// called again.
Chris Lattner146762e2007-07-20 16:59:19 +00001581bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Douglas Gregor3a7ad252010-08-24 19:08:16 +00001582 // Check if we are performing code completion.
1583 if (PP && PP->isCodeCompletionFile(FileLoc)) {
1584 // We're at the end of the file, but we've been asked to consider the
1585 // end of the file to be a code-completion token. Return the
1586 // code-completion token.
1587 Result.startToken();
1588 FormTokenWithChars(Result, CurPtr, tok::code_completion);
1589
1590 // Only do the eof -> code_completion translation once.
1591 PP->SetCodeCompletionPoint(0, 0, 0);
1592
1593 // Silence any diagnostics that occur once we hit the code-completion point.
1594 PP->getDiagnostics().setSuppressAllDiagnostics(true);
1595 return true;
1596 }
1597
Chris Lattner22eb9722006-06-18 05:43:12 +00001598 // If we hit the end of the file while parsing a preprocessor directive,
1599 // end the preprocessor directive first. The next token returned will
1600 // then be the end of file.
1601 if (ParsingPreprocessorDirective) {
1602 // Done parsing the "line".
1603 ParsingPreprocessorDirective = false;
Chris Lattnerd01e2912006-06-18 16:22:51 +00001604 // Update the location of token as well as BufferPtr.
Chris Lattnerb11c3232008-10-12 04:51:35 +00001605 FormTokenWithChars(Result, CurPtr, tok::eom);
Mike Stump11289f42009-09-09 15:08:12 +00001606
Chris Lattner457fc152006-07-29 06:30:25 +00001607 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001608 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner2183a6e2006-07-18 06:36:12 +00001609 return true; // Have a token.
Mike Stump11289f42009-09-09 15:08:12 +00001610 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00001611
Chris Lattner30a2fa12006-07-19 06:31:49 +00001612 // If we are in raw mode, return this event as an EOF token. Let the caller
1613 // that put us in raw mode handle the event.
Chris Lattner6d27a162008-11-22 02:02:22 +00001614 if (isLexingRawMode()) {
Chris Lattner8c204872006-10-14 05:19:21 +00001615 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +00001616 BufferPtr = BufferEnd;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001617 FormTokenWithChars(Result, BufferEnd, tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001618 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001619 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00001620
Douglas Gregor3a7ad252010-08-24 19:08:16 +00001621 // Issue diagnostics for unterminated #if and missing newline.
1622
Chris Lattner30a2fa12006-07-19 06:31:49 +00001623 // If we are in a #if directive, emit an error.
1624 while (!ConditionalStack.empty()) {
Douglas Gregor02690ba2010-08-12 17:04:55 +00001625 if (!PP->isCodeCompletionFile(FileLoc))
1626 PP->Diag(ConditionalStack.back().IfLoc,
1627 diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001628 ConditionalStack.pop_back();
1629 }
Mike Stump11289f42009-09-09 15:08:12 +00001630
Chris Lattner8f96d042008-04-12 05:54:25 +00001631 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1632 // a pedwarn.
1633 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Mike Stump0be88752009-04-02 02:29:42 +00001634 Diag(BufferEnd, diag::ext_no_newline_eof)
Douglas Gregora771f462010-03-31 17:46:05 +00001635 << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n");
Mike Stump11289f42009-09-09 15:08:12 +00001636
Chris Lattner22eb9722006-06-18 05:43:12 +00001637 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +00001638
1639 // Finally, let the preprocessor handle this.
Chris Lattner02b436a2007-10-17 20:41:00 +00001640 return PP->HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001641}
1642
Chris Lattner678c8802006-07-11 05:46:12 +00001643/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1644/// the specified lexer will return a tok::l_paren token, 0 if it is something
1645/// else and 2 if there are no more tokens in the buffer controlled by the
1646/// lexer.
1647unsigned Lexer::isNextPPTokenLParen() {
1648 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
Mike Stump11289f42009-09-09 15:08:12 +00001649
Chris Lattner678c8802006-07-11 05:46:12 +00001650 // Switch to 'skipping' mode. This will ensure that we can lex a token
1651 // without emitting diagnostics, disables macro expansion, and will cause EOF
1652 // to return an EOF token instead of popping the include stack.
1653 LexingRawMode = true;
Mike Stump11289f42009-09-09 15:08:12 +00001654
Chris Lattner678c8802006-07-11 05:46:12 +00001655 // Save state that can be changed while lexing so that we can restore it.
1656 const char *TmpBufferPtr = BufferPtr;
Chris Lattner40493eb2009-04-24 07:15:46 +00001657 bool inPPDirectiveMode = ParsingPreprocessorDirective;
Mike Stump11289f42009-09-09 15:08:12 +00001658
Chris Lattner146762e2007-07-20 16:59:19 +00001659 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001660 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001661 LexTokenInternal(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001662
Chris Lattner678c8802006-07-11 05:46:12 +00001663 // Restore state that may have changed.
1664 BufferPtr = TmpBufferPtr;
Chris Lattner40493eb2009-04-24 07:15:46 +00001665 ParsingPreprocessorDirective = inPPDirectiveMode;
Mike Stump11289f42009-09-09 15:08:12 +00001666
Chris Lattner678c8802006-07-11 05:46:12 +00001667 // Restore the lexer back to non-skipping mode.
1668 LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +00001669
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001670 if (Tok.is(tok::eof))
Chris Lattner678c8802006-07-11 05:46:12 +00001671 return 2;
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001672 return Tok.is(tok::l_paren);
Chris Lattner678c8802006-07-11 05:46:12 +00001673}
1674
Chris Lattner7c027ee2009-12-14 06:16:57 +00001675/// FindConflictEnd - Find the end of a version control conflict marker.
1676static const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd) {
1677 llvm::StringRef RestOfBuffer(CurPtr+7, BufferEnd-CurPtr-7);
1678 size_t Pos = RestOfBuffer.find(">>>>>>>");
1679 while (Pos != llvm::StringRef::npos) {
1680 // Must occur at start of line.
1681 if (RestOfBuffer[Pos-1] != '\r' &&
1682 RestOfBuffer[Pos-1] != '\n') {
1683 RestOfBuffer = RestOfBuffer.substr(Pos+7);
Chris Lattner467f6bc2010-05-17 20:27:25 +00001684 Pos = RestOfBuffer.find(">>>>>>>");
Chris Lattner7c027ee2009-12-14 06:16:57 +00001685 continue;
1686 }
1687 return RestOfBuffer.data()+Pos;
1688 }
1689 return 0;
1690}
1691
1692/// IsStartOfConflictMarker - If the specified pointer is the start of a version
1693/// control conflict marker like '<<<<<<<', recognize it as such, emit an error
1694/// and recover nicely. This returns true if it is a conflict marker and false
1695/// if not.
1696bool Lexer::IsStartOfConflictMarker(const char *CurPtr) {
1697 // Only a conflict marker if it starts at the beginning of a line.
1698 if (CurPtr != BufferStart &&
1699 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1700 return false;
1701
1702 // Check to see if we have <<<<<<<.
1703 if (BufferEnd-CurPtr < 8 ||
1704 llvm::StringRef(CurPtr, 7) != "<<<<<<<")
1705 return false;
1706
1707 // If we have a situation where we don't care about conflict markers, ignore
1708 // it.
1709 if (IsInConflictMarker || isLexingRawMode())
1710 return false;
1711
1712 // Check to see if there is a >>>>>>> somewhere in the buffer at the start of
1713 // a line to terminate this conflict marker.
Chris Lattner467f6bc2010-05-17 20:27:25 +00001714 if (FindConflictEnd(CurPtr, BufferEnd)) {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001715 // We found a match. We are really in a conflict marker.
1716 // Diagnose this, and ignore to the end of line.
1717 Diag(CurPtr, diag::err_conflict_marker);
1718 IsInConflictMarker = true;
1719
1720 // Skip ahead to the end of line. We know this exists because the
1721 // end-of-conflict marker starts with \r or \n.
1722 while (*CurPtr != '\r' && *CurPtr != '\n') {
1723 assert(CurPtr != BufferEnd && "Didn't find end of line");
1724 ++CurPtr;
1725 }
1726 BufferPtr = CurPtr;
1727 return true;
1728 }
1729
1730 // No end of conflict marker found.
1731 return false;
1732}
1733
1734
1735/// HandleEndOfConflictMarker - If this is a '=======' or '|||||||' or '>>>>>>>'
1736/// marker, then it is the end of a conflict marker. Handle it by ignoring up
1737/// until the end of the line. This returns true if it is a conflict marker and
1738/// false if not.
1739bool Lexer::HandleEndOfConflictMarker(const char *CurPtr) {
1740 // Only a conflict marker if it starts at the beginning of a line.
1741 if (CurPtr != BufferStart &&
1742 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1743 return false;
1744
1745 // If we have a situation where we don't care about conflict markers, ignore
1746 // it.
1747 if (!IsInConflictMarker || isLexingRawMode())
1748 return false;
1749
1750 // Check to see if we have the marker (7 characters in a row).
1751 for (unsigned i = 1; i != 7; ++i)
1752 if (CurPtr[i] != CurPtr[0])
1753 return false;
1754
1755 // If we do have it, search for the end of the conflict marker. This could
1756 // fail if it got skipped with a '#if 0' or something. Note that CurPtr might
1757 // be the end of conflict marker.
1758 if (const char *End = FindConflictEnd(CurPtr, BufferEnd)) {
1759 CurPtr = End;
1760
1761 // Skip ahead to the end of line.
1762 while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n')
1763 ++CurPtr;
1764
1765 BufferPtr = CurPtr;
1766
1767 // No longer in the conflict marker.
1768 IsInConflictMarker = false;
1769 return true;
1770 }
1771
1772 return false;
1773}
1774
Chris Lattner22eb9722006-06-18 05:43:12 +00001775
1776/// LexTokenInternal - This implements a simple C family lexer. It is an
1777/// extremely performance critical piece of code. This assumes that the buffer
Chris Lattner5c349382009-07-07 05:05:42 +00001778/// has a null character at the end of the file. This returns a preprocessing
1779/// token, not a normal token, as such, it is an internal interface. It assumes
1780/// that the Flags of result have been cleared before calling this.
Chris Lattner146762e2007-07-20 16:59:19 +00001781void Lexer::LexTokenInternal(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001782LexNextToken:
1783 // New token, can't need cleaning yet.
Chris Lattner146762e2007-07-20 16:59:19 +00001784 Result.clearFlag(Token::NeedsCleaning);
Chris Lattner8c204872006-10-14 05:19:21 +00001785 Result.setIdentifierInfo(0);
Mike Stump11289f42009-09-09 15:08:12 +00001786
Chris Lattner22eb9722006-06-18 05:43:12 +00001787 // CurPtr - Cache BufferPtr in an automatic variable.
1788 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001789
Chris Lattnereb54b592006-07-10 06:34:27 +00001790 // Small amounts of horizontal whitespace is very common between tokens.
1791 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1792 ++CurPtr;
1793 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1794 ++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001795
Chris Lattner4d963442008-10-12 04:05:48 +00001796 // If we are keeping whitespace and other tokens, just return what we just
1797 // skipped. The next lexer invocation will return the token after the
1798 // whitespace.
1799 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001800 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4d963442008-10-12 04:05:48 +00001801 return;
1802 }
Mike Stump11289f42009-09-09 15:08:12 +00001803
Chris Lattnereb54b592006-07-10 06:34:27 +00001804 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001805 Result.setFlag(Token::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001806 }
Mike Stump11289f42009-09-09 15:08:12 +00001807
Chris Lattner22eb9722006-06-18 05:43:12 +00001808 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
Mike Stump11289f42009-09-09 15:08:12 +00001809
Chris Lattner22eb9722006-06-18 05:43:12 +00001810 // Read a character, advancing over it.
1811 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001812 tok::TokenKind Kind;
Mike Stump11289f42009-09-09 15:08:12 +00001813
Chris Lattner22eb9722006-06-18 05:43:12 +00001814 switch (Char) {
1815 case 0: // Null.
1816 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001817 if (CurPtr-1 == BufferEnd) {
1818 // Read the PP instance variable into an automatic variable, because
1819 // LexEndOfFile will often delete 'this'.
Chris Lattner02b436a2007-10-17 20:41:00 +00001820 Preprocessor *PPCache = PP;
Chris Lattner2183a6e2006-07-18 06:36:12 +00001821 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1822 return; // Got a token to return.
Chris Lattner02b436a2007-10-17 20:41:00 +00001823 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1824 return PPCache->Lex(Result);
Chris Lattner2183a6e2006-07-18 06:36:12 +00001825 }
Mike Stump11289f42009-09-09 15:08:12 +00001826
Chris Lattner6d27a162008-11-22 02:02:22 +00001827 if (!isLexingRawMode())
1828 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner146762e2007-07-20 16:59:19 +00001829 Result.setFlag(Token::LeadingSpace);
Chris Lattner4d963442008-10-12 04:05:48 +00001830 if (SkipWhitespace(Result, CurPtr))
1831 return; // KeepWhitespaceMode
Mike Stump11289f42009-09-09 15:08:12 +00001832
Chris Lattner22eb9722006-06-18 05:43:12 +00001833 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner3dfff972009-12-17 05:29:40 +00001834
1835 case 26: // DOS & CP/M EOF: "^Z".
1836 // If we're in Microsoft extensions mode, treat this as end of file.
1837 if (Features.Microsoft) {
1838 // Read the PP instance variable into an automatic variable, because
1839 // LexEndOfFile will often delete 'this'.
1840 Preprocessor *PPCache = PP;
1841 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1842 return; // Got a token to return.
1843 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1844 return PPCache->Lex(Result);
1845 }
1846 // If Microsoft extensions are disabled, this is just random garbage.
1847 Kind = tok::unknown;
1848 break;
1849
Chris Lattner22eb9722006-06-18 05:43:12 +00001850 case '\n':
1851 case '\r':
1852 // If we are inside a preprocessor directive and we see the end of line,
1853 // we know we are done with the directive, so return an EOM token.
1854 if (ParsingPreprocessorDirective) {
1855 // Done parsing the "line".
1856 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +00001857
Chris Lattner457fc152006-07-29 06:30:25 +00001858 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001859 SetCommentRetentionState(PP->getCommentRetentionState());
Mike Stump11289f42009-09-09 15:08:12 +00001860
Chris Lattner22eb9722006-06-18 05:43:12 +00001861 // Since we consumed a newline, we are back at the start of a line.
1862 IsAtStartOfLine = true;
Mike Stump11289f42009-09-09 15:08:12 +00001863
Chris Lattnerb11c3232008-10-12 04:51:35 +00001864 Kind = tok::eom;
Chris Lattner22eb9722006-06-18 05:43:12 +00001865 break;
1866 }
1867 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001868 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001869 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001870 Result.clearFlag(Token::LeadingSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001871
Chris Lattner4d963442008-10-12 04:05:48 +00001872 if (SkipWhitespace(Result, CurPtr))
1873 return; // KeepWhitespaceMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001874 goto LexNextToken; // GCC isn't tail call eliminating.
1875 case ' ':
1876 case '\t':
1877 case '\f':
1878 case '\v':
Chris Lattnerb9b85972007-07-22 06:29:05 +00001879 SkipHorizontalWhitespace:
Chris Lattner146762e2007-07-20 16:59:19 +00001880 Result.setFlag(Token::LeadingSpace);
Chris Lattner4d963442008-10-12 04:05:48 +00001881 if (SkipWhitespace(Result, CurPtr))
1882 return; // KeepWhitespaceMode
Chris Lattnerb9b85972007-07-22 06:29:05 +00001883
1884 SkipIgnoredUnits:
1885 CurPtr = BufferPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001886
Chris Lattnerb9b85972007-07-22 06:29:05 +00001887 // If the next token is obviously a // or /* */ comment, skip it efficiently
1888 // too (without going through the big switch stmt).
Chris Lattner58827712009-01-16 22:39:25 +00001889 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1890 Features.BCPLComment) {
Chris Lattner87d02082010-01-18 22:35:47 +00001891 if (SkipBCPLComment(Result, CurPtr+2))
1892 return; // There is a token to return.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001893 goto SkipIgnoredUnits;
Chris Lattner8637abd2008-10-12 03:22:02 +00001894 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner87d02082010-01-18 22:35:47 +00001895 if (SkipBlockComment(Result, CurPtr+2))
1896 return; // There is a token to return.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001897 goto SkipIgnoredUnits;
1898 } else if (isHorizontalWhitespace(*CurPtr)) {
1899 goto SkipHorizontalWhitespace;
1900 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001901 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner3dfff972009-12-17 05:29:40 +00001902
Chris Lattner2b15cf72008-01-03 17:58:54 +00001903 // C99 6.4.4.1: Integer Constants.
1904 // C99 6.4.4.2: Floating Constants.
1905 case '0': case '1': case '2': case '3': case '4':
1906 case '5': case '6': case '7': case '8': case '9':
1907 // Notify MIOpt that we read a non-whitespace/non-comment token.
1908 MIOpt.ReadToken();
1909 return LexNumericConstant(Result, CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001910
Chris Lattner2b15cf72008-01-03 17:58:54 +00001911 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Chris Lattner371ac8a2006-07-04 07:11:10 +00001912 // Notify MIOpt that we read a non-whitespace/non-comment token.
1913 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001914 Char = getCharAndSize(CurPtr, SizeTmp);
1915
1916 // Wide string literal.
1917 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001918 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1919 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001920
1921 // Wide character constant.
1922 if (Char == '\'')
1923 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1924 // FALL THROUGH, treating L like the start of an identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001925
Chris Lattner22eb9722006-06-18 05:43:12 +00001926 // C99 6.4.2: Identifiers.
1927 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1928 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1929 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1930 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1931 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1932 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1933 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1934 case 'v': case 'w': case 'x': case 'y': case 'z':
1935 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001936 // Notify MIOpt that we read a non-whitespace/non-comment token.
1937 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001938 return LexIdentifier(Result, CurPtr);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001939
1940 case '$': // $ in identifiers.
1941 if (Features.DollarIdents) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001942 if (!isLexingRawMode())
1943 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001944 // Notify MIOpt that we read a non-whitespace/non-comment token.
1945 MIOpt.ReadToken();
1946 return LexIdentifier(Result, CurPtr);
1947 }
Mike Stump11289f42009-09-09 15:08:12 +00001948
Chris Lattnerb11c3232008-10-12 04:51:35 +00001949 Kind = tok::unknown;
Chris Lattner2b15cf72008-01-03 17:58:54 +00001950 break;
Mike Stump11289f42009-09-09 15:08:12 +00001951
Chris Lattner22eb9722006-06-18 05:43:12 +00001952 // C99 6.4.4: Character Constants.
1953 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001954 // Notify MIOpt that we read a non-whitespace/non-comment token.
1955 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001956 return LexCharConstant(Result, CurPtr);
1957
1958 // C99 6.4.5: String Literals.
1959 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001960 // Notify MIOpt that we read a non-whitespace/non-comment token.
1961 MIOpt.ReadToken();
Chris Lattnerd3e98952006-10-06 05:22:26 +00001962 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001963
1964 // C99 6.4.6: Punctuators.
1965 case '?':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001966 Kind = tok::question;
Chris Lattner22eb9722006-06-18 05:43:12 +00001967 break;
1968 case '[':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001969 Kind = tok::l_square;
Chris Lattner22eb9722006-06-18 05:43:12 +00001970 break;
1971 case ']':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001972 Kind = tok::r_square;
Chris Lattner22eb9722006-06-18 05:43:12 +00001973 break;
1974 case '(':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001975 Kind = tok::l_paren;
Chris Lattner22eb9722006-06-18 05:43:12 +00001976 break;
1977 case ')':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001978 Kind = tok::r_paren;
Chris Lattner22eb9722006-06-18 05:43:12 +00001979 break;
1980 case '{':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001981 Kind = tok::l_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001982 break;
1983 case '}':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001984 Kind = tok::r_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001985 break;
1986 case '.':
1987 Char = getCharAndSize(CurPtr, SizeTmp);
1988 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001989 // Notify MIOpt that we read a non-whitespace/non-comment token.
1990 MIOpt.ReadToken();
1991
Chris Lattner22eb9722006-06-18 05:43:12 +00001992 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1993 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001994 Kind = tok::periodstar;
Chris Lattner22eb9722006-06-18 05:43:12 +00001995 CurPtr += SizeTmp;
1996 } else if (Char == '.' &&
1997 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001998 Kind = tok::ellipsis;
Chris Lattner22eb9722006-06-18 05:43:12 +00001999 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
2000 SizeTmp2, Result);
2001 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002002 Kind = tok::period;
Chris Lattner22eb9722006-06-18 05:43:12 +00002003 }
2004 break;
2005 case '&':
2006 Char = getCharAndSize(CurPtr, SizeTmp);
2007 if (Char == '&') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002008 Kind = tok::ampamp;
Chris Lattner22eb9722006-06-18 05:43:12 +00002009 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2010 } else if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002011 Kind = tok::ampequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002012 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2013 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002014 Kind = tok::amp;
Chris Lattner22eb9722006-06-18 05:43:12 +00002015 }
2016 break;
Mike Stump11289f42009-09-09 15:08:12 +00002017 case '*':
Chris Lattner22eb9722006-06-18 05:43:12 +00002018 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002019 Kind = tok::starequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002020 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2021 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002022 Kind = tok::star;
Chris Lattner22eb9722006-06-18 05:43:12 +00002023 }
2024 break;
2025 case '+':
2026 Char = getCharAndSize(CurPtr, SizeTmp);
2027 if (Char == '+') {
Chris Lattner22eb9722006-06-18 05:43:12 +00002028 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002029 Kind = tok::plusplus;
Chris Lattner22eb9722006-06-18 05:43:12 +00002030 } else if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00002031 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002032 Kind = tok::plusequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002033 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002034 Kind = tok::plus;
Chris Lattner22eb9722006-06-18 05:43:12 +00002035 }
2036 break;
2037 case '-':
2038 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002039 if (Char == '-') { // --
Chris Lattner22eb9722006-06-18 05:43:12 +00002040 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002041 Kind = tok::minusminus;
Mike Stump11289f42009-09-09 15:08:12 +00002042 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattnerb11c3232008-10-12 04:51:35 +00002043 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00002044 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
2045 SizeTmp2, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002046 Kind = tok::arrowstar;
2047 } else if (Char == '>') { // ->
Chris Lattner22eb9722006-06-18 05:43:12 +00002048 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002049 Kind = tok::arrow;
2050 } else if (Char == '=') { // -=
Chris Lattner22eb9722006-06-18 05:43:12 +00002051 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002052 Kind = tok::minusequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002053 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002054 Kind = tok::minus;
Chris Lattner22eb9722006-06-18 05:43:12 +00002055 }
2056 break;
2057 case '~':
Chris Lattnerb11c3232008-10-12 04:51:35 +00002058 Kind = tok::tilde;
Chris Lattner22eb9722006-06-18 05:43:12 +00002059 break;
2060 case '!':
2061 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002062 Kind = tok::exclaimequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002063 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2064 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002065 Kind = tok::exclaim;
Chris Lattner22eb9722006-06-18 05:43:12 +00002066 }
2067 break;
2068 case '/':
2069 // 6.4.9: Comments
2070 Char = getCharAndSize(CurPtr, SizeTmp);
2071 if (Char == '/') { // BCPL comment.
Chris Lattner58827712009-01-16 22:39:25 +00002072 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
2073 // want to lex this as a comment. There is one problem with this though,
2074 // that in one particular corner case, this can change the behavior of the
2075 // resultant program. For example, In "foo //**/ bar", C89 would lex
2076 // this as "foo / bar" and langauges with BCPL comments would lex it as
2077 // "foo". Check to see if the character after the second slash is a '*'.
2078 // If so, we will lex that as a "/" instead of the start of a comment.
2079 if (Features.BCPLComment ||
2080 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
2081 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner87d02082010-01-18 22:35:47 +00002082 return; // There is a token to return.
Mike Stump11289f42009-09-09 15:08:12 +00002083
Chris Lattner58827712009-01-16 22:39:25 +00002084 // It is common for the tokens immediately after a // comment to be
2085 // whitespace (indentation for the next line). Instead of going through
2086 // the big switch, handle it efficiently now.
2087 goto SkipIgnoredUnits;
2088 }
2089 }
Mike Stump11289f42009-09-09 15:08:12 +00002090
Chris Lattner58827712009-01-16 22:39:25 +00002091 if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00002092 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner87d02082010-01-18 22:35:47 +00002093 return; // There is a token to return.
Chris Lattnere01e7582008-10-12 04:15:42 +00002094 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner58827712009-01-16 22:39:25 +00002095 }
Mike Stump11289f42009-09-09 15:08:12 +00002096
Chris Lattner58827712009-01-16 22:39:25 +00002097 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00002098 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002099 Kind = tok::slashequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002100 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002101 Kind = tok::slash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002102 }
2103 break;
2104 case '%':
2105 Char = getCharAndSize(CurPtr, SizeTmp);
2106 if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002107 Kind = tok::percentequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002108 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2109 } else if (Features.Digraphs && Char == '>') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002110 Kind = tok::r_brace; // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00002111 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2112 } else if (Features.Digraphs && Char == ':') {
2113 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00002114 Char = getCharAndSize(CurPtr, SizeTmp);
2115 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002116 Kind = tok::hashhash; // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00002117 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
2118 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00002119 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner2b271db2006-07-15 05:41:09 +00002120 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner6d27a162008-11-22 02:02:22 +00002121 if (!isLexingRawMode())
2122 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002123 Kind = tok::hashat;
Chris Lattner2534324a2009-03-18 20:58:27 +00002124 } else { // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00002125 // We parsed a # character. If this occurs at the start of the line,
2126 // it's actually the start of a preprocessing directive. Callback to
2127 // the preprocessor to handle it.
2128 // FIXME: -fpreprocessed mode??
Chris Lattnerff96dd02009-05-13 06:10:29 +00002129 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattner2534324a2009-03-18 20:58:27 +00002130 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner02b436a2007-10-17 20:41:00 +00002131 PP->HandleDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +00002132
Chris Lattner22eb9722006-06-18 05:43:12 +00002133 // As an optimization, if the preprocessor didn't switch lexers, tail
2134 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00002135 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002136 // Start a new token. If this is a #include or something, the PP may
2137 // want us starting at the beginning of the line again. If so, set
Chris Lattner1a9e8732010-04-12 23:04:41 +00002138 // the StartOfLine flag and clear LeadingSpace.
Chris Lattner22eb9722006-06-18 05:43:12 +00002139 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00002140 Result.setFlag(Token::StartOfLine);
Chris Lattner1a9e8732010-04-12 23:04:41 +00002141 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00002142 IsAtStartOfLine = false;
2143 }
2144 goto LexNextToken; // GCC isn't tail call eliminating.
2145 }
Mike Stump11289f42009-09-09 15:08:12 +00002146
Chris Lattner02b436a2007-10-17 20:41:00 +00002147 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002148 }
Mike Stump11289f42009-09-09 15:08:12 +00002149
Chris Lattner2534324a2009-03-18 20:58:27 +00002150 Kind = tok::hash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002151 }
2152 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002153 Kind = tok::percent;
Chris Lattner22eb9722006-06-18 05:43:12 +00002154 }
2155 break;
2156 case '<':
2157 Char = getCharAndSize(CurPtr, SizeTmp);
2158 if (ParsingFilename) {
Chris Lattnerb40289b2009-04-17 23:56:52 +00002159 return LexAngledStringLiteral(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00002160 } else if (Char == '<') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00002161 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
2162 if (After == '=') {
2163 Kind = tok::lesslessequal;
2164 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
2165 SizeTmp2, Result);
2166 } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) {
2167 // If this is actually a '<<<<<<<' version control conflict marker,
2168 // recognize it as such and recover nicely.
2169 goto LexNextToken;
2170 } else {
2171 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2172 Kind = tok::lessless;
2173 }
Chris Lattner22eb9722006-06-18 05:43:12 +00002174 } else if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00002175 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002176 Kind = tok::lessequal;
2177 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00002178 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002179 Kind = tok::l_square;
2180 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Chris Lattner22eb9722006-06-18 05:43:12 +00002181 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002182 Kind = tok::l_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00002183 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002184 Kind = tok::less;
Chris Lattner22eb9722006-06-18 05:43:12 +00002185 }
2186 break;
2187 case '>':
2188 Char = getCharAndSize(CurPtr, SizeTmp);
2189 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00002190 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002191 Kind = tok::greaterequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002192 } else if (Char == '>') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00002193 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
2194 if (After == '=') {
2195 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
2196 SizeTmp2, Result);
2197 Kind = tok::greatergreaterequal;
2198 } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) {
2199 // If this is '>>>>>>>' and we're in a conflict marker, ignore it.
2200 goto LexNextToken;
2201 } else {
2202 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2203 Kind = tok::greatergreater;
2204 }
2205
Chris Lattner22eb9722006-06-18 05:43:12 +00002206 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002207 Kind = tok::greater;
Chris Lattner22eb9722006-06-18 05:43:12 +00002208 }
2209 break;
2210 case '^':
2211 Char = getCharAndSize(CurPtr, SizeTmp);
2212 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00002213 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00002214 Kind = tok::caretequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002215 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002216 Kind = tok::caret;
Chris Lattner22eb9722006-06-18 05:43:12 +00002217 }
2218 break;
2219 case '|':
2220 Char = getCharAndSize(CurPtr, SizeTmp);
2221 if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002222 Kind = tok::pipeequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002223 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2224 } else if (Char == '|') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00002225 // If this is '|||||||' and we're in a conflict marker, ignore it.
2226 if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1))
2227 goto LexNextToken;
Chris Lattnerb11c3232008-10-12 04:51:35 +00002228 Kind = tok::pipepipe;
Chris Lattner22eb9722006-06-18 05:43:12 +00002229 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2230 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002231 Kind = tok::pipe;
Chris Lattner22eb9722006-06-18 05:43:12 +00002232 }
2233 break;
2234 case ':':
2235 Char = getCharAndSize(CurPtr, SizeTmp);
2236 if (Features.Digraphs && Char == '>') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002237 Kind = tok::r_square; // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00002238 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2239 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002240 Kind = tok::coloncolon;
Chris Lattner22eb9722006-06-18 05:43:12 +00002241 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump11289f42009-09-09 15:08:12 +00002242 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002243 Kind = tok::colon;
Chris Lattner22eb9722006-06-18 05:43:12 +00002244 }
2245 break;
2246 case ';':
Chris Lattnerb11c3232008-10-12 04:51:35 +00002247 Kind = tok::semi;
Chris Lattner22eb9722006-06-18 05:43:12 +00002248 break;
2249 case '=':
2250 Char = getCharAndSize(CurPtr, SizeTmp);
2251 if (Char == '=') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00002252 // If this is '=======' and we're in a conflict marker, ignore it.
2253 if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1))
2254 goto LexNextToken;
2255
Chris Lattnerb11c3232008-10-12 04:51:35 +00002256 Kind = tok::equalequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002257 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump11289f42009-09-09 15:08:12 +00002258 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002259 Kind = tok::equal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002260 }
2261 break;
2262 case ',':
Chris Lattnerb11c3232008-10-12 04:51:35 +00002263 Kind = tok::comma;
Chris Lattner22eb9722006-06-18 05:43:12 +00002264 break;
2265 case '#':
2266 Char = getCharAndSize(CurPtr, SizeTmp);
2267 if (Char == '#') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002268 Kind = tok::hashhash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002269 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00002270 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattnerb11c3232008-10-12 04:51:35 +00002271 Kind = tok::hashat;
Chris Lattner6d27a162008-11-22 02:02:22 +00002272 if (!isLexingRawMode())
2273 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner2b271db2006-07-15 05:41:09 +00002274 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002275 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +00002276 // We parsed a # character. If this occurs at the start of the line,
2277 // it's actually the start of a preprocessing directive. Callback to
2278 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00002279 // FIXME: -fpreprocessed mode??
Chris Lattnerff96dd02009-05-13 06:10:29 +00002280 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattner2534324a2009-03-18 20:58:27 +00002281 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner02b436a2007-10-17 20:41:00 +00002282 PP->HandleDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +00002283
Chris Lattner22eb9722006-06-18 05:43:12 +00002284 // As an optimization, if the preprocessor didn't switch lexers, tail
2285 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00002286 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002287 // Start a new token. If this is a #include or something, the PP may
2288 // want us starting at the beginning of the line again. If so, set
Chris Lattner1a9e8732010-04-12 23:04:41 +00002289 // the StartOfLine flag and clear LeadingSpace.
Chris Lattner22eb9722006-06-18 05:43:12 +00002290 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00002291 Result.setFlag(Token::StartOfLine);
Chris Lattner1a9e8732010-04-12 23:04:41 +00002292 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00002293 IsAtStartOfLine = false;
2294 }
2295 goto LexNextToken; // GCC isn't tail call eliminating.
2296 }
Chris Lattner02b436a2007-10-17 20:41:00 +00002297 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002298 }
Mike Stump11289f42009-09-09 15:08:12 +00002299
Chris Lattner2534324a2009-03-18 20:58:27 +00002300 Kind = tok::hash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002301 }
2302 break;
2303
Chris Lattner2b15cf72008-01-03 17:58:54 +00002304 case '@':
2305 // Objective C support.
2306 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattnerb11c3232008-10-12 04:51:35 +00002307 Kind = tok::at;
Chris Lattner2b15cf72008-01-03 17:58:54 +00002308 else
Chris Lattnerb11c3232008-10-12 04:51:35 +00002309 Kind = tok::unknown;
Chris Lattner2b15cf72008-01-03 17:58:54 +00002310 break;
Mike Stump11289f42009-09-09 15:08:12 +00002311
Chris Lattner22eb9722006-06-18 05:43:12 +00002312 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00002313 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00002314 // FALL THROUGH.
2315 default:
Chris Lattnerb11c3232008-10-12 04:51:35 +00002316 Kind = tok::unknown;
Chris Lattner041bef82006-07-11 05:52:53 +00002317 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00002318 }
Mike Stump11289f42009-09-09 15:08:12 +00002319
Chris Lattner371ac8a2006-07-04 07:11:10 +00002320 // Notify MIOpt that we read a non-whitespace/non-comment token.
2321 MIOpt.ReadToken();
2322
Chris Lattnerd01e2912006-06-18 16:22:51 +00002323 // Update the location of token as well as BufferPtr.
Chris Lattnerb11c3232008-10-12 04:51:35 +00002324 FormTokenWithChars(Result, CurPtr, Kind);
Chris Lattner22eb9722006-06-18 05:43:12 +00002325}