blob: 84457fa2c839f5303996019be6ba36482f90a5a1 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerd2177732007-07-20 16:59:19 +000010// This file implements the Lexer and Token interfaces.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13//
14// TODO: GCC Diagnostics emitted by the lexer:
15// PEDWARN: (form feed|vertical tab) in preprocessing directive
16//
17// Universal characters, unicode, char mapping:
18// WARNING: `%.*s' is not in NFKC
19// WARNING: `%.*s' is not in NFC
20//
21// Other:
22// TODO: Options to support:
23// -fexec-charset,-fwide-exec-charset
24//
25//===----------------------------------------------------------------------===//
26
27#include "clang/Lex/Lexer.h"
28#include "clang/Lex/Preprocessor.h"
Chris Lattner500d3292009-01-29 05:15:15 +000029#include "clang/Lex/LexDiagnostic.h"
Chris Lattner9dc1f532007-07-20 16:37:10 +000030#include "clang/Basic/SourceManager.h"
Chris Lattner409a0362007-07-22 18:38:25 +000031#include "llvm/Support/Compiler.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000032#include "llvm/Support/MemoryBuffer.h"
33#include <cctype>
34using namespace clang;
35
Chris Lattnera2bf1052009-12-17 05:29:40 +000036static void InitCharacterInfo();
Reid Spencer5f016e22007-07-11 17:01:13 +000037
Chris Lattnerdbf388b2007-10-07 08:47:24 +000038//===----------------------------------------------------------------------===//
39// Token Class Implementation
40//===----------------------------------------------------------------------===//
41
Mike Stump1eb44332009-09-09 15:08:12 +000042/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
Chris Lattnerdbf388b2007-10-07 08:47:24 +000043bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
Douglas Gregorbec1c9d2008-12-01 21:46:47 +000044 if (IdentifierInfo *II = getIdentifierInfo())
45 return II->getObjCKeywordID() == objcKey;
46 return false;
Chris Lattnerdbf388b2007-10-07 08:47:24 +000047}
48
49/// getObjCKeywordID - Return the ObjC keyword kind.
50tok::ObjCKeywordKind Token::getObjCKeywordID() const {
51 IdentifierInfo *specId = getIdentifierInfo();
52 return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
53}
54
Chris Lattner53702cd2007-12-13 01:59:49 +000055
Chris Lattnerdbf388b2007-10-07 08:47:24 +000056//===----------------------------------------------------------------------===//
57// Lexer Class Implementation
58//===----------------------------------------------------------------------===//
59
Mike Stump1eb44332009-09-09 15:08:12 +000060void Lexer::InitLexer(const char *BufStart, const char *BufPtr,
Chris Lattner22d91ca2009-01-17 06:55:17 +000061 const char *BufEnd) {
Chris Lattnera2bf1052009-12-17 05:29:40 +000062 InitCharacterInfo();
Mike Stump1eb44332009-09-09 15:08:12 +000063
Chris Lattner22d91ca2009-01-17 06:55:17 +000064 BufferStart = BufStart;
65 BufferPtr = BufPtr;
66 BufferEnd = BufEnd;
Mike Stump1eb44332009-09-09 15:08:12 +000067
Chris Lattner22d91ca2009-01-17 06:55:17 +000068 assert(BufEnd[0] == 0 &&
69 "We assume that the input buffer has a null character at the end"
70 " to simplify lexing!");
Mike Stump1eb44332009-09-09 15:08:12 +000071
Chris Lattner22d91ca2009-01-17 06:55:17 +000072 Is_PragmaLexer = false;
Chris Lattner34f349d2009-12-14 06:16:57 +000073 IsInConflictMarker = false;
Douglas Gregor81b747b2009-09-17 21:32:03 +000074
Chris Lattner22d91ca2009-01-17 06:55:17 +000075 // Start of the file is a start of line.
76 IsAtStartOfLine = true;
Mike Stump1eb44332009-09-09 15:08:12 +000077
Chris Lattner22d91ca2009-01-17 06:55:17 +000078 // We are not after parsing a #.
79 ParsingPreprocessorDirective = false;
Mike Stump1eb44332009-09-09 15:08:12 +000080
Chris Lattner22d91ca2009-01-17 06:55:17 +000081 // We are not after parsing #include.
82 ParsingFilename = false;
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chris Lattner22d91ca2009-01-17 06:55:17 +000084 // We are not in raw mode. Raw mode disables diagnostics and interpretation
85 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
86 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
87 // or otherwise skipping over tokens.
88 LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner22d91ca2009-01-17 06:55:17 +000090 // Default to not keeping comments.
91 ExtendedTokenMode = 0;
92}
93
Chris Lattner0770dab2009-01-17 07:56:59 +000094/// Lexer constructor - Create a new lexer object for the specified buffer
95/// with the specified preprocessor managing the lexing process. This lexer
96/// assumes that the associated file buffer and Preprocessor objects will
97/// outlive it, so it doesn't take ownership of either of them.
Chris Lattner6e290142009-11-30 04:18:44 +000098Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP)
Chris Lattner88d3ac12009-01-17 08:03:42 +000099 : PreprocessorLexer(&PP, FID),
100 FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
101 Features(PP.getLangOptions()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattner0770dab2009-01-17 07:56:59 +0000103 InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
104 InputFile->getBufferEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000105
Chris Lattner0770dab2009-01-17 07:56:59 +0000106 // Default to keeping comments if the preprocessor wants them.
107 SetCommentRetentionState(PP.getCommentRetentionState());
108}
Chris Lattnerdbf388b2007-10-07 08:47:24 +0000109
Chris Lattner168ae2d2007-10-17 20:41:00 +0000110/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner590f0cc2008-10-12 01:15:46 +0000111/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
112/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000113Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattnerde96c0f2009-01-17 07:42:27 +0000114 const char *BufStart, const char *BufPtr, const char *BufEnd)
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000115 : FileLoc(fileloc), Features(features) {
Chris Lattner22d91ca2009-01-17 06:55:17 +0000116
Chris Lattner22d91ca2009-01-17 06:55:17 +0000117 InitLexer(BufStart, BufPtr, BufEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Chris Lattner168ae2d2007-10-17 20:41:00 +0000119 // We *are* in raw mode.
120 LexingRawMode = true;
Chris Lattner168ae2d2007-10-17 20:41:00 +0000121}
122
Chris Lattner025c3a62009-01-17 07:35:14 +0000123/// Lexer constructor - Create a new raw lexer object. This object is only
124/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
125/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner6e290142009-11-30 04:18:44 +0000126Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile,
127 const SourceManager &SM, const LangOptions &features)
Chris Lattner025c3a62009-01-17 07:35:14 +0000128 : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
Chris Lattner025c3a62009-01-17 07:35:14 +0000129
Mike Stump1eb44332009-09-09 15:08:12 +0000130 InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
Chris Lattner025c3a62009-01-17 07:35:14 +0000131 FromFile->getBufferEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000132
Chris Lattner025c3a62009-01-17 07:35:14 +0000133 // We *are* in raw mode.
134 LexingRawMode = true;
135}
136
Chris Lattner42e00d12009-01-17 08:27:52 +0000137/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
138/// _Pragma expansion. This has a variety of magic semantics that this method
139/// sets up. It returns a new'd Lexer that must be delete'd when done.
140///
141/// On entrance to this routine, TokStartLoc is a macro location which has a
142/// spelling loc that indicates the bytes to be lexed for the token and an
143/// instantiation location that indicates where all lexed tokens should be
144/// "expanded from".
145///
146/// FIXME: It would really be nice to make _Pragma just be a wrapper around a
147/// normal lexer that remaps tokens as they fly by. This would require making
148/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer
149/// interface that could handle this stuff. This would pull GetMappedTokenLoc
150/// out of the critical path of the lexer!
151///
Mike Stump1eb44332009-09-09 15:08:12 +0000152Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000153 SourceLocation InstantiationLocStart,
154 SourceLocation InstantiationLocEnd,
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000155 unsigned TokLen, Preprocessor &PP) {
Chris Lattner42e00d12009-01-17 08:27:52 +0000156 SourceManager &SM = PP.getSourceManager();
Chris Lattner42e00d12009-01-17 08:27:52 +0000157
158 // Create the lexer as if we were going to lex the file normally.
Chris Lattnera11d6172009-01-19 07:46:45 +0000159 FileID SpellingFID = SM.getFileID(SpellingLoc);
Chris Lattner6e290142009-11-30 04:18:44 +0000160 const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID);
161 Lexer *L = new Lexer(SpellingFID, InputFile, PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Chris Lattner42e00d12009-01-17 08:27:52 +0000163 // Now that the lexer is created, change the start/end locations so that we
164 // just lex the subsection of the file that we want. This is lexing from a
165 // scratch buffer.
166 const char *StrData = SM.getCharacterData(SpellingLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Chris Lattner42e00d12009-01-17 08:27:52 +0000168 L->BufferPtr = StrData;
169 L->BufferEnd = StrData+TokLen;
Chris Lattner1fa49532009-03-08 08:08:45 +0000170 assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
Chris Lattner42e00d12009-01-17 08:27:52 +0000171
172 // Set the SourceLocation with the remapping information. This ensures that
173 // GetMappedTokenLoc will remap the tokens as they are lexed.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000174 L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
Chris Lattnere7fb4842009-02-15 20:52:18 +0000175 InstantiationLocStart,
176 InstantiationLocEnd, TokLen);
Mike Stump1eb44332009-09-09 15:08:12 +0000177
Chris Lattner42e00d12009-01-17 08:27:52 +0000178 // Ensure that the lexer thinks it is inside a directive, so that end \n will
179 // return an EOM token.
180 L->ParsingPreprocessorDirective = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000181
Chris Lattner42e00d12009-01-17 08:27:52 +0000182 // This lexer really is for _Pragma.
183 L->Is_PragmaLexer = true;
184 return L;
185}
186
Chris Lattner168ae2d2007-10-17 20:41:00 +0000187
Reid Spencer5f016e22007-07-11 17:01:13 +0000188/// Stringify - Convert the specified string into a C string, with surrounding
189/// ""'s, and with escaped \ and " characters.
190std::string Lexer::Stringify(const std::string &Str, bool Charify) {
191 std::string Result = Str;
192 char Quote = Charify ? '\'' : '"';
193 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
194 if (Result[i] == '\\' || Result[i] == Quote) {
195 Result.insert(Result.begin()+i, '\\');
196 ++i; ++e;
197 }
198 }
199 return Result;
200}
201
Chris Lattnerd8e30832007-07-24 06:57:14 +0000202/// Stringify - Convert the specified string into a C string by escaping '\'
203/// and " characters. This does not add surrounding ""'s to the string.
204void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
205 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
206 if (Str[i] == '\\' || Str[i] == '"') {
207 Str.insert(Str.begin()+i, '\\');
208 ++i; ++e;
209 }
210 }
211}
212
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000213static bool isWhitespace(unsigned char c);
Reid Spencer5f016e22007-07-11 17:01:13 +0000214
Chris Lattner9a611942007-10-17 21:18:47 +0000215/// MeasureTokenLength - Relex the token at the specified location and return
216/// its length in bytes in the input file. If the token needs cleaning (e.g.
217/// includes a trigraph or an escaped newline) then this count includes bytes
218/// that are part of that.
219unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
Chris Lattner2c78b872009-04-14 23:22:57 +0000220 const SourceManager &SM,
221 const LangOptions &LangOpts) {
Chris Lattner9a611942007-10-17 21:18:47 +0000222 // TODO: this could be special cased for common tokens like identifiers, ')',
223 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
Mike Stump1eb44332009-09-09 15:08:12 +0000224 // all obviously single-char tokens. This could use
Chris Lattner9a611942007-10-17 21:18:47 +0000225 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
226 // something.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000227
228 // If this comes from a macro expansion, we really do want the macro name, not
229 // the token this macro expanded to.
Chris Lattner363fdc22009-01-26 22:24:27 +0000230 Loc = SM.getInstantiationLoc(Loc);
231 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
Douglas Gregorf715ca12010-03-16 00:06:06 +0000232 bool Invalid = false;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +0000233 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Douglas Gregorf715ca12010-03-16 00:06:06 +0000234 if (Invalid)
Douglas Gregoraea67db2010-03-15 22:54:52 +0000235 return 0;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +0000236
237 const char *StrData = Buffer.data()+LocInfo.second;
Chris Lattner83503942009-01-17 08:30:10 +0000238
Douglas Gregor33e9abd2010-01-22 19:49:59 +0000239 if (isWhitespace(StrData[0]))
240 return 0;
241
Chris Lattner9a611942007-10-17 21:18:47 +0000242 // Create a lexer starting at the beginning of this token.
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +0000243 Lexer TheLexer(Loc, LangOpts, Buffer.begin(), StrData, Buffer.end());
Chris Lattner39de7402009-10-14 15:04:18 +0000244 TheLexer.SetCommentRetentionState(true);
Chris Lattner9a611942007-10-17 21:18:47 +0000245 Token TheTok;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000246 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner9a611942007-10-17 21:18:47 +0000247 return TheTok.getLength();
248}
249
Reid Spencer5f016e22007-07-11 17:01:13 +0000250//===----------------------------------------------------------------------===//
251// Character information.
252//===----------------------------------------------------------------------===//
253
Reid Spencer5f016e22007-07-11 17:01:13 +0000254enum {
255 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
256 CHAR_VERT_WS = 0x02, // '\r', '\n'
257 CHAR_LETTER = 0x04, // a-z,A-Z
258 CHAR_NUMBER = 0x08, // 0-9
259 CHAR_UNDER = 0x10, // _
260 CHAR_PERIOD = 0x20 // .
261};
262
Chris Lattner03b98662009-07-07 17:09:54 +0000263// Statically initialize CharInfo table based on ASCII character set
264// Reference: FreeBSD 7.2 /usr/share/misc/ascii
Chris Lattnera2bf1052009-12-17 05:29:40 +0000265static const unsigned char CharInfo[256] =
Chris Lattner03b98662009-07-07 17:09:54 +0000266{
267// 0 NUL 1 SOH 2 STX 3 ETX
268// 4 EOT 5 ENQ 6 ACK 7 BEL
269 0 , 0 , 0 , 0 ,
270 0 , 0 , 0 , 0 ,
271// 8 BS 9 HT 10 NL 11 VT
272//12 NP 13 CR 14 SO 15 SI
273 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS,
274 CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 ,
275//16 DLE 17 DC1 18 DC2 19 DC3
276//20 DC4 21 NAK 22 SYN 23 ETB
277 0 , 0 , 0 , 0 ,
278 0 , 0 , 0 , 0 ,
279//24 CAN 25 EM 26 SUB 27 ESC
280//28 FS 29 GS 30 RS 31 US
281 0 , 0 , 0 , 0 ,
282 0 , 0 , 0 , 0 ,
283//32 SP 33 ! 34 " 35 #
284//36 $ 37 % 38 & 39 '
285 CHAR_HORZ_WS, 0 , 0 , 0 ,
286 0 , 0 , 0 , 0 ,
287//40 ( 41 ) 42 * 43 +
288//44 , 45 - 46 . 47 /
289 0 , 0 , 0 , 0 ,
290 0 , 0 , CHAR_PERIOD , 0 ,
291//48 0 49 1 50 2 51 3
292//52 4 53 5 54 6 55 7
293 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
294 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
295//56 8 57 9 58 : 59 ;
296//60 < 61 = 62 > 63 ?
297 CHAR_NUMBER , CHAR_NUMBER , 0 , 0 ,
298 0 , 0 , 0 , 0 ,
299//64 @ 65 A 66 B 67 C
300//68 D 69 E 70 F 71 G
301 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
302 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
303//72 H 73 I 74 J 75 K
304//76 L 77 M 78 N 79 O
305 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
306 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
307//80 P 81 Q 82 R 83 S
308//84 T 85 U 86 V 87 W
309 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
310 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
311//88 X 89 Y 90 Z 91 [
312//92 \ 93 ] 94 ^ 95 _
313 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
314 0 , 0 , 0 , CHAR_UNDER ,
315//96 ` 97 a 98 b 99 c
316//100 d 101 e 102 f 103 g
317 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
318 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
319//104 h 105 i 106 j 107 k
320//108 l 109 m 110 n 111 o
321 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
322 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
323//112 p 113 q 114 r 115 s
324//116 t 117 u 118 v 119 w
325 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
326 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
327//120 x 121 y 122 z 123 {
328//124 | 125 } 126 ~ 127 DEL
329 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
330 0 , 0 , 0 , 0
331};
332
Chris Lattnera2bf1052009-12-17 05:29:40 +0000333static void InitCharacterInfo() {
Reid Spencer5f016e22007-07-11 17:01:13 +0000334 static bool isInited = false;
335 if (isInited) return;
Chris Lattner03b98662009-07-07 17:09:54 +0000336 // check the statically-initialized CharInfo table
337 assert(CHAR_HORZ_WS == CharInfo[(int)' ']);
338 assert(CHAR_HORZ_WS == CharInfo[(int)'\t']);
339 assert(CHAR_HORZ_WS == CharInfo[(int)'\f']);
340 assert(CHAR_HORZ_WS == CharInfo[(int)'\v']);
341 assert(CHAR_VERT_WS == CharInfo[(int)'\n']);
342 assert(CHAR_VERT_WS == CharInfo[(int)'\r']);
343 assert(CHAR_UNDER == CharInfo[(int)'_']);
344 assert(CHAR_PERIOD == CharInfo[(int)'.']);
345 for (unsigned i = 'a'; i <= 'z'; ++i) {
346 assert(CHAR_LETTER == CharInfo[i]);
347 assert(CHAR_LETTER == CharInfo[i+'A'-'a']);
348 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000349 for (unsigned i = '0'; i <= '9'; ++i)
Chris Lattner03b98662009-07-07 17:09:54 +0000350 assert(CHAR_NUMBER == CharInfo[i]);
Steve Naroff7b682652009-12-08 16:38:12 +0000351
Chris Lattner03b98662009-07-07 17:09:54 +0000352 isInited = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000353}
354
Chris Lattner03b98662009-07-07 17:09:54 +0000355
Reid Spencer5f016e22007-07-11 17:01:13 +0000356/// isIdentifierBody - Return true if this is the body character of an
357/// identifier, which is [a-zA-Z0-9_].
358static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000359 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000360}
361
362/// isHorizontalWhitespace - Return true if this character is horizontal
363/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
364static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000365 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000366}
367
368/// isWhitespace - Return true if this character is horizontal or vertical
369/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
370/// for '\0'.
371static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000372 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000373}
374
375/// isNumberBody - Return true if this is the body character of an
376/// preprocessing number, which is [a-zA-Z0-9_.].
377static inline bool isNumberBody(unsigned char c) {
Mike Stump1eb44332009-09-09 15:08:12 +0000378 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000379 true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000380}
381
382
383//===----------------------------------------------------------------------===//
384// Diagnostics forwarding code.
385//===----------------------------------------------------------------------===//
386
Chris Lattner409a0362007-07-22 18:38:25 +0000387/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
388/// lexer buffer was all instantiated at a single point, perform the mapping.
389/// This is currently only used for _Pragma implementation, so it is the slow
390/// path of the hot getSourceLocation method. Do not allow it to be inlined.
Benjamin Kramerc997eb42009-11-14 16:36:57 +0000391static DISABLE_INLINE SourceLocation GetMappedTokenLoc(Preprocessor &PP,
392 SourceLocation FileLoc,
393 unsigned CharNo,
394 unsigned TokLen);
Chris Lattner409a0362007-07-22 18:38:25 +0000395static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
396 SourceLocation FileLoc,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000397 unsigned CharNo, unsigned TokLen) {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000398 assert(FileLoc.isMacroID() && "Must be an instantiation");
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattner409a0362007-07-22 18:38:25 +0000400 // Otherwise, we're lexing "mapped tokens". This is used for things like
401 // _Pragma handling. Combine the instantiation location of FileLoc with the
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000402 // spelling location.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000403 SourceManager &SM = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000405 // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000406 // characters come from spelling(FileLoc)+Offset.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000407 SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000408 SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattnere7fb4842009-02-15 20:52:18 +0000410 // Figure out the expansion loc range, which is the range covered by the
411 // original _Pragma(...) sequence.
412 std::pair<SourceLocation,SourceLocation> II =
413 SM.getImmediateInstantiationRange(FileLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Chris Lattnere7fb4842009-02-15 20:52:18 +0000415 return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
Chris Lattner409a0362007-07-22 18:38:25 +0000416}
417
Reid Spencer5f016e22007-07-11 17:01:13 +0000418/// getSourceLocation - Return a source location identifier for the specified
419/// offset in the current file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000420SourceLocation Lexer::getSourceLocation(const char *Loc,
421 unsigned TokLen) const {
Chris Lattner448cec42007-07-22 18:44:36 +0000422 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000423 "Location out of range for this buffer!");
Chris Lattner9dc1f532007-07-20 16:37:10 +0000424
425 // In the normal case, we're just lexing from a simple file buffer, return
426 // the file id from FileLoc with the offset specified.
Chris Lattner448cec42007-07-22 18:44:36 +0000427 unsigned CharNo = Loc-BufferStart;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000428 if (FileLoc.isFileID())
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000429 return FileLoc.getFileLocWithOffset(CharNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000430
Chris Lattner2b2453a2009-01-17 06:22:33 +0000431 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
432 // tokens are lexed from where the _Pragma was defined.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000433 assert(PP && "This doesn't work on raw lexers");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000434 return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
Reid Spencer5f016e22007-07-11 17:01:13 +0000435}
436
Reid Spencer5f016e22007-07-11 17:01:13 +0000437/// Diag - Forwarding function for diagnostics. This translate a source
438/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000439DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner3692b092008-11-18 07:59:24 +0000440 return PP->Diag(getSourceLocation(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000441}
Reid Spencer5f016e22007-07-11 17:01:13 +0000442
443//===----------------------------------------------------------------------===//
444// Trigraph and Escaped Newline Handling Code.
445//===----------------------------------------------------------------------===//
446
447/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
448/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
449static char GetTrigraphCharForLetter(char Letter) {
450 switch (Letter) {
451 default: return 0;
452 case '=': return '#';
453 case ')': return ']';
454 case '(': return '[';
455 case '!': return '|';
456 case '\'': return '^';
457 case '>': return '}';
458 case '/': return '\\';
459 case '<': return '{';
460 case '-': return '~';
461 }
462}
463
464/// DecodeTrigraphChar - If the specified character is a legal trigraph when
465/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
466/// return the result character. Finally, emit a warning about trigraph use
467/// whether trigraphs are enabled or not.
468static char DecodeTrigraphChar(const char *CP, Lexer *L) {
469 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner3692b092008-11-18 07:59:24 +0000470 if (!Res || !L) return Res;
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Chris Lattner3692b092008-11-18 07:59:24 +0000472 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +0000473 if (!L->isLexingRawMode())
474 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner3692b092008-11-18 07:59:24 +0000475 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000476 }
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Chris Lattner74d15df2008-11-22 02:02:22 +0000478 if (!L->isLexingRawMode())
479 L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000480 return Res;
481}
482
Chris Lattner24f0e482009-04-18 22:05:41 +0000483/// getEscapedNewLineSize - Return the size of the specified escaped newline,
484/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a
Mike Stump1eb44332009-09-09 15:08:12 +0000485/// trigraph equivalent on entry to this function.
Chris Lattner24f0e482009-04-18 22:05:41 +0000486unsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
487 unsigned Size = 0;
488 while (isWhitespace(Ptr[Size])) {
489 ++Size;
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Chris Lattner24f0e482009-04-18 22:05:41 +0000491 if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
492 continue;
493
494 // If this is a \r\n or \n\r, skip the other half.
495 if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
496 Ptr[Size-1] != Ptr[Size])
497 ++Size;
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Chris Lattner24f0e482009-04-18 22:05:41 +0000499 return Size;
Mike Stump1eb44332009-09-09 15:08:12 +0000500 }
501
Chris Lattner24f0e482009-04-18 22:05:41 +0000502 // Not an escaped newline, must be a \t or something else.
503 return 0;
504}
505
Chris Lattner03374952009-04-18 22:27:02 +0000506/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
507/// them), skip over them and return the first non-escaped-newline found,
508/// otherwise return P.
509const char *Lexer::SkipEscapedNewLines(const char *P) {
510 while (1) {
511 const char *AfterEscape;
512 if (*P == '\\') {
513 AfterEscape = P+1;
514 } else if (*P == '?') {
515 // If not a trigraph for escape, bail out.
516 if (P[1] != '?' || P[2] != '/')
517 return P;
518 AfterEscape = P+3;
519 } else {
520 return P;
521 }
Mike Stump1eb44332009-09-09 15:08:12 +0000522
Chris Lattner03374952009-04-18 22:27:02 +0000523 unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
524 if (NewLineSize == 0) return P;
525 P = AfterEscape+NewLineSize;
526 }
527}
528
Chris Lattner24f0e482009-04-18 22:05:41 +0000529
Reid Spencer5f016e22007-07-11 17:01:13 +0000530/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
531/// get its size, and return it. This is tricky in several cases:
532/// 1. If currently at the start of a trigraph, we warn about the trigraph,
533/// then either return the trigraph (skipping 3 chars) or the '?',
534/// depending on whether trigraphs are enabled or not.
535/// 2. If this is an escaped newline (potentially with whitespace between
536/// the backslash and newline), implicitly skip the newline and return
537/// the char after it.
538/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
539///
540/// This handles the slow/uncommon case of the getCharAndSize method. Here we
541/// know that we can accumulate into Size, and that we have already incremented
542/// Ptr by Size bytes.
543///
544/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
545/// be updated to match.
546///
547char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattnerd2177732007-07-20 16:59:19 +0000548 Token *Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000549 // If we have a slash, look for an escaped newline.
550 if (Ptr[0] == '\\') {
551 ++Size;
552 ++Ptr;
553Slash:
554 // Common case, backslash-char where the char is not whitespace.
555 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump1eb44332009-09-09 15:08:12 +0000556
Chris Lattner5636a3b2009-06-23 05:15:06 +0000557 // See if we have optional whitespace characters between the slash and
558 // newline.
Chris Lattner24f0e482009-04-18 22:05:41 +0000559 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
560 // Remember that this token needs to be cleaned.
561 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000562
Chris Lattner24f0e482009-04-18 22:05:41 +0000563 // Warn if there was whitespace between the backslash and newline.
Chris Lattner5636a3b2009-06-23 05:15:06 +0000564 if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
Chris Lattner24f0e482009-04-18 22:05:41 +0000565 Diag(Ptr, diag::backslash_newline_space);
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Chris Lattner24f0e482009-04-18 22:05:41 +0000567 // Found backslash<whitespace><newline>. Parse the char after it.
568 Size += EscapedNewLineSize;
569 Ptr += EscapedNewLineSize;
570 // Use slow version to accumulate a correct size field.
571 return getCharAndSizeSlow(Ptr, Size, Tok);
572 }
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Reid Spencer5f016e22007-07-11 17:01:13 +0000574 // Otherwise, this is not an escaped newline, just return the slash.
575 return '\\';
576 }
Mike Stump1eb44332009-09-09 15:08:12 +0000577
Reid Spencer5f016e22007-07-11 17:01:13 +0000578 // If this is a trigraph, process it.
579 if (Ptr[0] == '?' && Ptr[1] == '?') {
580 // If this is actually a legal trigraph (not something like "??x"), emit
581 // a trigraph warning. If so, and if trigraphs are enabled, return it.
582 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
583 // Remember that this token needs to be cleaned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000584 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000585
586 Ptr += 3;
587 Size += 3;
588 if (C == '\\') goto Slash;
589 return C;
590 }
591 }
Mike Stump1eb44332009-09-09 15:08:12 +0000592
Reid Spencer5f016e22007-07-11 17:01:13 +0000593 // If this is neither, return a single character.
594 ++Size;
595 return *Ptr;
596}
597
598
599/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
600/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
601/// and that we have already incremented Ptr by Size bytes.
602///
603/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
604/// be updated to match.
605char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
606 const LangOptions &Features) {
607 // If we have a slash, look for an escaped newline.
608 if (Ptr[0] == '\\') {
609 ++Size;
610 ++Ptr;
611Slash:
612 // Common case, backslash-char where the char is not whitespace.
613 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Reid Spencer5f016e22007-07-11 17:01:13 +0000615 // See if we have optional whitespace characters followed by a newline.
Chris Lattner24f0e482009-04-18 22:05:41 +0000616 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
617 // Found backslash<whitespace><newline>. Parse the char after it.
618 Size += EscapedNewLineSize;
619 Ptr += EscapedNewLineSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Chris Lattner24f0e482009-04-18 22:05:41 +0000621 // Use slow version to accumulate a correct size field.
622 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
623 }
Mike Stump1eb44332009-09-09 15:08:12 +0000624
Reid Spencer5f016e22007-07-11 17:01:13 +0000625 // Otherwise, this is not an escaped newline, just return the slash.
626 return '\\';
627 }
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Reid Spencer5f016e22007-07-11 17:01:13 +0000629 // If this is a trigraph, process it.
630 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
631 // If this is actually a legal trigraph (not something like "??x"), return
632 // it.
633 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
634 Ptr += 3;
635 Size += 3;
636 if (C == '\\') goto Slash;
637 return C;
638 }
639 }
Mike Stump1eb44332009-09-09 15:08:12 +0000640
Reid Spencer5f016e22007-07-11 17:01:13 +0000641 // If this is neither, return a single character.
642 ++Size;
643 return *Ptr;
644}
645
646//===----------------------------------------------------------------------===//
647// Helper methods for lexing.
648//===----------------------------------------------------------------------===//
649
Chris Lattnerd2177732007-07-20 16:59:19 +0000650void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000651 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
652 unsigned Size;
653 unsigned char C = *CurPtr++;
Chris Lattnercd991db2010-01-11 02:38:50 +0000654 while (isIdentifierBody(C))
Reid Spencer5f016e22007-07-11 17:01:13 +0000655 C = *CurPtr++;
Chris Lattnercd991db2010-01-11 02:38:50 +0000656
Reid Spencer5f016e22007-07-11 17:01:13 +0000657 --CurPtr; // Back up over the skipped character.
658
659 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
660 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
661 // FIXME: UCNs.
Chris Lattnercd991db2010-01-11 02:38:50 +0000662 //
663 // TODO: Could merge these checks into a CharInfo flag to make the comparison
664 // cheaper
Reid Spencer5f016e22007-07-11 17:01:13 +0000665 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
666FinishIdentifier:
667 const char *IdStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000668 FormTokenWithChars(Result, CurPtr, tok::identifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 // If we are in raw mode, return this identifier raw. There is no need to
671 // look up identifier information or attempt to macro expand it.
672 if (LexingRawMode) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 // Fill in Result.IdentifierInfo, looking up the identifier in the
675 // identifier table.
Chris Lattnerd1186fa2009-01-21 07:45:14 +0000676 IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000677
Chris Lattner863c4862009-01-23 18:35:48 +0000678 // Change the kind of this identifier to the appropriate token kind, e.g.
679 // turning "for" into a keyword.
680 Result.setKind(II->getTokenID());
Mike Stump1eb44332009-09-09 15:08:12 +0000681
Reid Spencer5f016e22007-07-11 17:01:13 +0000682 // Finally, now that we know we have an identifier, pass this off to the
683 // preprocessor, which may macro expand it or something.
Chris Lattnerd1186fa2009-01-21 07:45:14 +0000684 if (II->isHandleIdentifierCase())
Chris Lattner6a170eb2009-01-21 07:43:11 +0000685 PP->HandleIdentifier(Result);
686 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000687 }
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 // Otherwise, $,\,? in identifier found. Enter slower path.
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Reid Spencer5f016e22007-07-11 17:01:13 +0000691 C = getCharAndSize(CurPtr, Size);
692 while (1) {
693 if (C == '$') {
694 // If we hit a $ and they are not supported in identifiers, we are done.
695 if (!Features.DollarIdents) goto FinishIdentifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Reid Spencer5f016e22007-07-11 17:01:13 +0000697 // Otherwise, emit a diagnostic and continue.
Chris Lattner74d15df2008-11-22 02:02:22 +0000698 if (!isLexingRawMode())
699 Diag(CurPtr, diag::ext_dollar_in_identifier);
Reid Spencer5f016e22007-07-11 17:01:13 +0000700 CurPtr = ConsumeChar(CurPtr, Size, Result);
701 C = getCharAndSize(CurPtr, Size);
702 continue;
703 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
704 // Found end of identifier.
705 goto FinishIdentifier;
706 }
707
708 // Otherwise, this character is good, consume it.
709 CurPtr = ConsumeChar(CurPtr, Size, Result);
710
711 C = getCharAndSize(CurPtr, Size);
712 while (isIdentifierBody(C)) { // FIXME: UCNs.
713 CurPtr = ConsumeChar(CurPtr, Size, Result);
714 C = getCharAndSize(CurPtr, Size);
715 }
716 }
717}
718
719
Nate Begeman5253c7f2008-04-14 02:26:39 +0000720/// LexNumericConstant - Lex the remainder of a integer or floating point
Reid Spencer5f016e22007-07-11 17:01:13 +0000721/// constant. From[-1] is the first character lexed. Return the end of the
722/// constant.
Chris Lattnerd2177732007-07-20 16:59:19 +0000723void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000724 unsigned Size;
725 char C = getCharAndSize(CurPtr, Size);
726 char PrevCh = 0;
727 while (isNumberBody(C)) { // FIXME: UCNs?
728 CurPtr = ConsumeChar(CurPtr, Size, Result);
729 PrevCh = C;
730 C = getCharAndSize(CurPtr, Size);
731 }
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
734 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
735 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
736
737 // If we have a hex FP constant, continue.
Sean Hunt8c723402010-01-10 23:37:56 +0000738 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
739 (!PP || !PP->getLangOptions().CPlusPlus0x))
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
Mike Stump1eb44332009-09-09 15:08:12 +0000741
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 // Update the location of token as well as BufferPtr.
Chris Lattner47246be2009-01-26 19:29:26 +0000743 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000744 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Chris Lattner47246be2009-01-26 19:29:26 +0000745 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000746}
747
748/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
749/// either " or L".
Chris Lattnerd88dc482008-10-12 04:05:48 +0000750void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 const char *NulCharacter = 0; // Does this string contain the \0 character?
Mike Stump1eb44332009-09-09 15:08:12 +0000752
Reid Spencer5f016e22007-07-11 17:01:13 +0000753 char C = getAndAdvanceChar(CurPtr, Result);
754 while (C != '"') {
Chris Lattner571339c2010-05-30 23:27:38 +0000755 // Skip escaped characters. Escaped newlines will already be processed by
756 // getAndAdvanceChar.
757 if (C == '\\')
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 C = getAndAdvanceChar(CurPtr, Result);
Douglas Gregor33611e02010-05-30 22:59:50 +0000759
Chris Lattner571339c2010-05-30 23:27:38 +0000760 if (C == '\n' || C == '\r' || // Newline.
Douglas Gregor33611e02010-05-30 22:59:50 +0000761 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner33ab3f62009-03-18 21:10:12 +0000762 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner74d15df2008-11-22 02:02:22 +0000763 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000764 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000765 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 }
Chris Lattner571339c2010-05-30 23:27:38 +0000767
768 if (C == 0)
769 NulCharacter = CurPtr-1;
Reid Spencer5f016e22007-07-11 17:01:13 +0000770 C = getAndAdvanceChar(CurPtr, Result);
771 }
Mike Stump1eb44332009-09-09 15:08:12 +0000772
Reid Spencer5f016e22007-07-11 17:01:13 +0000773 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000774 if (NulCharacter && !isLexingRawMode())
775 Diag(NulCharacter, diag::null_in_string);
Reid Spencer5f016e22007-07-11 17:01:13 +0000776
Reid Spencer5f016e22007-07-11 17:01:13 +0000777 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner47246be2009-01-26 19:29:26 +0000778 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000779 FormTokenWithChars(Result, CurPtr,
780 Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +0000781 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000782}
783
784/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
785/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattnerd2177732007-07-20 16:59:19 +0000786void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000787 const char *NulCharacter = 0; // Does this string contain the \0 character?
Chris Lattner9cb51ce2009-04-17 23:56:52 +0000788 const char *AfterLessPos = CurPtr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000789 char C = getAndAdvanceChar(CurPtr, Result);
790 while (C != '>') {
791 // Skip escaped characters.
792 if (C == '\\') {
793 // Skip the escaped character.
794 C = getAndAdvanceChar(CurPtr, Result);
795 } else if (C == '\n' || C == '\r' || // Newline.
796 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner9cb51ce2009-04-17 23:56:52 +0000797 // If the filename is unterminated, then it must just be a lone <
798 // character. Return this as such.
799 FormTokenWithChars(Result, AfterLessPos, tok::less);
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 return;
801 } else if (C == 0) {
802 NulCharacter = CurPtr-1;
803 }
804 C = getAndAdvanceChar(CurPtr, Result);
805 }
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Reid Spencer5f016e22007-07-11 17:01:13 +0000807 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000808 if (NulCharacter && !isLexingRawMode())
809 Diag(NulCharacter, diag::null_in_string);
Mike Stump1eb44332009-09-09 15:08:12 +0000810
Reid Spencer5f016e22007-07-11 17:01:13 +0000811 // Update the location of token as well as BufferPtr.
Chris Lattner47246be2009-01-26 19:29:26 +0000812 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000813 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +0000814 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000815}
816
817
818/// LexCharConstant - Lex the remainder of a character constant, after having
819/// lexed either ' or L'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000820void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 const char *NulCharacter = 0; // Does this character contain the \0 character?
822
823 // Handle the common case of 'x' and '\y' efficiently.
824 char C = getAndAdvanceChar(CurPtr, Result);
825 if (C == '\'') {
Chris Lattner33ab3f62009-03-18 21:10:12 +0000826 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner74d15df2008-11-22 02:02:22 +0000827 Diag(BufferPtr, diag::err_empty_character);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000828 FormTokenWithChars(Result, CurPtr, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000829 return;
830 } else if (C == '\\') {
831 // Skip the escaped character.
832 // FIXME: UCN's.
833 C = getAndAdvanceChar(CurPtr, Result);
834 }
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Reid Spencer5f016e22007-07-11 17:01:13 +0000836 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
837 ++CurPtr;
838 } else {
839 // Fall back on generic code for embedded nulls, newlines, wide chars.
840 do {
841 // Skip escaped characters.
842 if (C == '\\') {
843 // Skip the escaped character.
844 C = getAndAdvanceChar(CurPtr, Result);
845 } else if (C == '\n' || C == '\r' || // Newline.
846 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner33ab3f62009-03-18 21:10:12 +0000847 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner74d15df2008-11-22 02:02:22 +0000848 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000849 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000850 return;
851 } else if (C == 0) {
852 NulCharacter = CurPtr-1;
853 }
854 C = getAndAdvanceChar(CurPtr, Result);
855 } while (C != '\'');
856 }
Mike Stump1eb44332009-09-09 15:08:12 +0000857
Chris Lattner74d15df2008-11-22 02:02:22 +0000858 if (NulCharacter && !isLexingRawMode())
859 Diag(NulCharacter, diag::null_in_char);
Reid Spencer5f016e22007-07-11 17:01:13 +0000860
Reid Spencer5f016e22007-07-11 17:01:13 +0000861 // Update the location of token as well as BufferPtr.
Chris Lattner47246be2009-01-26 19:29:26 +0000862 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000863 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Chris Lattner47246be2009-01-26 19:29:26 +0000864 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000865}
866
867/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
868/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattnerd88dc482008-10-12 04:05:48 +0000869///
870/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
871///
872bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 // Whitespace - Skip it, then return the token after the whitespace.
874 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
875 while (1) {
876 // Skip horizontal whitespace very aggressively.
877 while (isHorizontalWhitespace(Char))
878 Char = *++CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Daniel Dunbarddd3e8b2008-11-25 00:20:22 +0000880 // Otherwise if we have something other than whitespace, we're done.
Reid Spencer5f016e22007-07-11 17:01:13 +0000881 if (Char != '\n' && Char != '\r')
882 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000883
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 if (ParsingPreprocessorDirective) {
885 // End of preprocessor directive line, let LexTokenInternal handle this.
886 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +0000887 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000888 }
Mike Stump1eb44332009-09-09 15:08:12 +0000889
Reid Spencer5f016e22007-07-11 17:01:13 +0000890 // ok, but handle newline.
891 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +0000892 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000893 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +0000894 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000895 Char = *++CurPtr;
896 }
897
898 // If this isn't immediately after a newline, there is leading space.
899 char PrevChar = CurPtr[-1];
900 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattnerd2177732007-07-20 16:59:19 +0000901 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000902
Chris Lattnerd88dc482008-10-12 04:05:48 +0000903 // If the client wants us to return whitespace, return it now.
904 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +0000905 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +0000906 return true;
907 }
Mike Stump1eb44332009-09-09 15:08:12 +0000908
Reid Spencer5f016e22007-07-11 17:01:13 +0000909 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +0000910 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000911}
912
913// SkipBCPLComment - We have just read the // characters from input. Skip until
914// we find the newline character thats terminate the comment. Then update
Chris Lattner046c2272010-01-18 22:35:47 +0000915/// BufferPtr and return.
916///
917/// If we're in KeepCommentMode or any CommentHandler has inserted
918/// some tokens, this will store the first token and return true.
Chris Lattnerd2177732007-07-20 16:59:19 +0000919bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000920 // If BCPL comments aren't explicitly enabled for this language, emit an
921 // extension warning.
Chris Lattner74d15df2008-11-22 02:02:22 +0000922 if (!Features.BCPLComment && !isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 Diag(BufferPtr, diag::ext_bcpl_comment);
Mike Stump1eb44332009-09-09 15:08:12 +0000924
Reid Spencer5f016e22007-07-11 17:01:13 +0000925 // Mark them enabled so we only emit one warning for this translation
926 // unit.
927 Features.BCPLComment = true;
928 }
Mike Stump1eb44332009-09-09 15:08:12 +0000929
Reid Spencer5f016e22007-07-11 17:01:13 +0000930 // Scan over the body of the comment. The common case, when scanning, is that
931 // the comment contains normal ascii characters with nothing interesting in
932 // them. As such, optimize for this case with the inner loop.
933 char C;
934 do {
935 C = *CurPtr;
936 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
937 // If we find a \n character, scan backwards, checking to see if it's an
938 // escaped newline, like we do for block comments.
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Reid Spencer5f016e22007-07-11 17:01:13 +0000940 // Skip over characters in the fast loop.
941 while (C != 0 && // Potentially EOF.
942 C != '\\' && // Potentially escaped newline.
943 C != '?' && // Potentially trigraph.
944 C != '\n' && C != '\r') // Newline or DOS-style newline.
945 C = *++CurPtr;
946
947 // If this is a newline, we're done.
948 if (C == '\n' || C == '\r')
949 break; // Found the newline? Break out!
Mike Stump1eb44332009-09-09 15:08:12 +0000950
Reid Spencer5f016e22007-07-11 17:01:13 +0000951 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000952 // properly decode the character. Read it in raw mode to avoid emitting
953 // diagnostics about things like trigraphs. If we see an escaped newline,
954 // we'll handle it below.
Reid Spencer5f016e22007-07-11 17:01:13 +0000955 const char *OldPtr = CurPtr;
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000956 bool OldRawMode = isLexingRawMode();
957 LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000958 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000959 LexingRawMode = OldRawMode;
Chris Lattneread616c2009-04-05 00:26:41 +0000960
961 // If the char that we finally got was a \n, then we must have had something
962 // like \<newline><newline>. We don't want to have consumed the second
963 // newline, we want CurPtr, to end up pointing to it down below.
964 if (C == '\n' || C == '\r') {
965 --CurPtr;
966 C = 'x'; // doesn't matter what this is.
967 }
Mike Stump1eb44332009-09-09 15:08:12 +0000968
Reid Spencer5f016e22007-07-11 17:01:13 +0000969 // If we read multiple characters, and one of those characters was a \r or
970 // \n, then we had an escaped newline within the comment. Emit diagnostic
971 // unless the next line is also a // comment.
972 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
973 for (; OldPtr != CurPtr; ++OldPtr)
974 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
975 // Okay, we found a // comment that ends in a newline, if the next
976 // line is also a // comment, but has spaces, don't emit a diagnostic.
977 if (isspace(C)) {
978 const char *ForwardPtr = CurPtr;
979 while (isspace(*ForwardPtr)) // Skip whitespace.
980 ++ForwardPtr;
981 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
982 break;
983 }
Mike Stump1eb44332009-09-09 15:08:12 +0000984
Chris Lattner74d15df2008-11-22 02:02:22 +0000985 if (!isLexingRawMode())
986 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000987 break;
988 }
989 }
Mike Stump1eb44332009-09-09 15:08:12 +0000990
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
992 } while (C != '\n' && C != '\r');
993
Chris Lattner3d0ad582010-02-03 21:06:21 +0000994 // Found but did not consume the newline. Notify comment handlers about the
995 // comment unless we're in a #if 0 block.
996 if (PP && !isLexingRawMode() &&
997 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
998 getSourceLocation(CurPtr)))) {
Chris Lattner046c2272010-01-18 22:35:47 +0000999 BufferPtr = CurPtr;
1000 return true; // A token has to be returned.
1001 }
Mike Stump1eb44332009-09-09 15:08:12 +00001002
Reid Spencer5f016e22007-07-11 17:01:13 +00001003 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +00001004 if (inKeepCommentMode())
Reid Spencer5f016e22007-07-11 17:01:13 +00001005 return SaveBCPLComment(Result, CurPtr);
1006
1007 // If we are inside a preprocessor directive and we see the end of line,
1008 // return immediately, so that the lexer can return this as an EOM token.
1009 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
1010 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001011 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001012 }
Mike Stump1eb44332009-09-09 15:08:12 +00001013
Reid Spencer5f016e22007-07-11 17:01:13 +00001014 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner7a4f0042008-10-12 00:23:07 +00001015 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattnerd88dc482008-10-12 04:05:48 +00001016 // contribute to another token), it isn't needed for correctness. Note that
1017 // this is ok even in KeepWhitespaceMode, because we would have returned the
1018 /// comment above in that mode.
Reid Spencer5f016e22007-07-11 17:01:13 +00001019 ++CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Reid Spencer5f016e22007-07-11 17:01:13 +00001021 // The next returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +00001022 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001023 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +00001024 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001025 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001026 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001027}
1028
1029/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
1030/// an appropriate way and return it.
Chris Lattnerd2177732007-07-20 16:59:19 +00001031bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001032 // If we're not in a preprocessor directive, just return the // comment
1033 // directly.
1034 FormTokenWithChars(Result, CurPtr, tok::comment);
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Chris Lattner9e6293d2008-10-12 04:51:35 +00001036 if (!ParsingPreprocessorDirective)
1037 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001038
Chris Lattner9e6293d2008-10-12 04:51:35 +00001039 // If this BCPL-style comment is in a macro definition, transmogrify it into
1040 // a C-style block comment.
Douglas Gregor453091c2010-03-16 22:30:13 +00001041 bool Invalid = false;
1042 std::string Spelling = PP->getSpelling(Result, &Invalid);
1043 if (Invalid)
1044 return true;
1045
Chris Lattner9e6293d2008-10-12 04:51:35 +00001046 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
1047 Spelling[1] = '*'; // Change prefix to "/*".
1048 Spelling += "*/"; // add suffix.
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Chris Lattner9e6293d2008-10-12 04:51:35 +00001050 Result.setKind(tok::comment);
Chris Lattner47246be2009-01-26 19:29:26 +00001051 PP->CreateString(&Spelling[0], Spelling.size(), Result,
1052 Result.getLocation());
Chris Lattner2d381892008-10-12 04:15:42 +00001053 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001054}
1055
1056/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
1057/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner47a2b402008-12-12 07:14:34 +00001058/// diagnostic if so. We know that the newline is inside of a block comment.
Mike Stump1eb44332009-09-09 15:08:12 +00001059static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
Reid Spencer5f016e22007-07-11 17:01:13 +00001060 Lexer *L) {
1061 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Mike Stump1eb44332009-09-09 15:08:12 +00001062
Reid Spencer5f016e22007-07-11 17:01:13 +00001063 // Back up off the newline.
1064 --CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Reid Spencer5f016e22007-07-11 17:01:13 +00001066 // If this is a two-character newline sequence, skip the other character.
1067 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
1068 // \n\n or \r\r -> not escaped newline.
1069 if (CurPtr[0] == CurPtr[1])
1070 return false;
1071 // \n\r or \r\n -> skip the newline.
1072 --CurPtr;
1073 }
Mike Stump1eb44332009-09-09 15:08:12 +00001074
Reid Spencer5f016e22007-07-11 17:01:13 +00001075 // If we have horizontal whitespace, skip over it. We allow whitespace
1076 // between the slash and newline.
1077 bool HasSpace = false;
1078 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
1079 --CurPtr;
1080 HasSpace = true;
1081 }
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 // If we have a slash, we know this is an escaped newline.
1084 if (*CurPtr == '\\') {
1085 if (CurPtr[-1] != '*') return false;
1086 } else {
1087 // It isn't a slash, is it the ?? / trigraph?
1088 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
1089 CurPtr[-3] != '*')
1090 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001091
Reid Spencer5f016e22007-07-11 17:01:13 +00001092 // This is the trigraph ending the comment. Emit a stern warning!
1093 CurPtr -= 2;
1094
1095 // If no trigraphs are enabled, warn that we ignored this trigraph and
1096 // ignore this * character.
1097 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001098 if (!L->isLexingRawMode())
1099 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001100 return false;
1101 }
Chris Lattner74d15df2008-11-22 02:02:22 +00001102 if (!L->isLexingRawMode())
1103 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001104 }
Mike Stump1eb44332009-09-09 15:08:12 +00001105
Reid Spencer5f016e22007-07-11 17:01:13 +00001106 // Warn about having an escaped newline between the */ characters.
Chris Lattner74d15df2008-11-22 02:02:22 +00001107 if (!L->isLexingRawMode())
1108 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Mike Stump1eb44332009-09-09 15:08:12 +00001109
Reid Spencer5f016e22007-07-11 17:01:13 +00001110 // If there was space between the backslash and newline, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +00001111 if (HasSpace && !L->isLexingRawMode())
1112 L->Diag(CurPtr, diag::backslash_newline_space);
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Reid Spencer5f016e22007-07-11 17:01:13 +00001114 return true;
1115}
1116
1117#ifdef __SSE2__
1118#include <emmintrin.h>
1119#elif __ALTIVEC__
1120#include <altivec.h>
1121#undef bool
1122#endif
1123
1124/// SkipBlockComment - We have just read the /* characters from input. Read
1125/// until we find the */ characters that terminate the comment. Note that we
1126/// don't bother decoding trigraphs or escaped newlines in block comments,
1127/// because they cannot cause the comment to end. The only thing that can
1128/// happen is the comment could end with an escaped newline between the */ end
1129/// of comment.
Chris Lattner2d381892008-10-12 04:15:42 +00001130///
Chris Lattner046c2272010-01-18 22:35:47 +00001131/// If we're in KeepCommentMode or any CommentHandler has inserted
1132/// some tokens, this will store the first token and return true.
Chris Lattnerd2177732007-07-20 16:59:19 +00001133bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 // Scan one character past where we should, looking for a '/' character. Once
1135 // we find it, check to see if it was preceeded by a *. This common
1136 // optimization helps people who like to put a lot of * characters in their
1137 // comments.
Chris Lattner8146b682007-07-21 23:43:37 +00001138
1139 // The first character we get with newlines and trigraphs skipped to handle
1140 // the degenerate /*/ case below correctly if the * has an escaped newline
1141 // after it.
1142 unsigned CharSize;
1143 unsigned char C = getCharAndSize(CurPtr, CharSize);
1144 CurPtr += CharSize;
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner150fcd52010-05-16 19:54:05 +00001146 if (!isLexingRawMode() &&
1147 !PP->isCodeCompletionFile(FileLoc))
Chris Lattner0af57422008-10-12 01:31:51 +00001148 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001149 --CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001150
Chris Lattner31f0eca2008-10-12 04:19:49 +00001151 // KeepWhitespaceMode should return this broken comment as a token. Since
1152 // it isn't a well formed comment, just return it as an 'unknown' token.
1153 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001154 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001155 return true;
1156 }
Mike Stump1eb44332009-09-09 15:08:12 +00001157
Chris Lattner31f0eca2008-10-12 04:19:49 +00001158 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001159 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 }
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Chris Lattner8146b682007-07-21 23:43:37 +00001162 // Check to see if the first character after the '/*' is another /. If so,
1163 // then this slash does not end the block comment, it is part of it.
1164 if (C == '/')
1165 C = *CurPtr++;
Mike Stump1eb44332009-09-09 15:08:12 +00001166
Reid Spencer5f016e22007-07-11 17:01:13 +00001167 while (1) {
1168 // Skip over all non-interesting characters until we find end of buffer or a
1169 // (probably ending) '/' character.
1170 if (CurPtr + 24 < BufferEnd) {
1171 // While not aligned to a 16-byte boundary.
1172 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1173 C = *CurPtr++;
Mike Stump1eb44332009-09-09 15:08:12 +00001174
Reid Spencer5f016e22007-07-11 17:01:13 +00001175 if (C == '/') goto FoundSlash;
1176
1177#ifdef __SSE2__
1178 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1179 '/', '/', '/', '/', '/', '/', '/', '/');
1180 while (CurPtr+16 <= BufferEnd &&
1181 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1182 CurPtr += 16;
1183#elif __ALTIVEC__
1184 __vector unsigned char Slashes = {
Mike Stump1eb44332009-09-09 15:08:12 +00001185 '/', '/', '/', '/', '/', '/', '/', '/',
Reid Spencer5f016e22007-07-11 17:01:13 +00001186 '/', '/', '/', '/', '/', '/', '/', '/'
1187 };
1188 while (CurPtr+16 <= BufferEnd &&
1189 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1190 CurPtr += 16;
Mike Stump1eb44332009-09-09 15:08:12 +00001191#else
Reid Spencer5f016e22007-07-11 17:01:13 +00001192 // Scan for '/' quickly. Many block comments are very large.
1193 while (CurPtr[0] != '/' &&
1194 CurPtr[1] != '/' &&
1195 CurPtr[2] != '/' &&
1196 CurPtr[3] != '/' &&
1197 CurPtr+4 < BufferEnd) {
1198 CurPtr += 4;
1199 }
1200#endif
Mike Stump1eb44332009-09-09 15:08:12 +00001201
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 // It has to be one of the bytes scanned, increment to it and read one.
1203 C = *CurPtr++;
1204 }
Mike Stump1eb44332009-09-09 15:08:12 +00001205
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 // Loop to scan the remainder.
1207 while (C != '/' && C != '\0')
1208 C = *CurPtr++;
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 FoundSlash:
1211 if (C == '/') {
1212 if (CurPtr[-2] == '*') // We found the final */. We're done!
1213 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Reid Spencer5f016e22007-07-11 17:01:13 +00001215 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
1216 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
1217 // We found the final */, though it had an escaped newline between the
1218 // * and /. We're done!
1219 break;
1220 }
1221 }
1222 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1223 // If this is a /* inside of the comment, emit a warning. Don't do this
1224 // if this is a /*/, which will end the comment. This misses cases with
1225 // embedded escaped newlines, but oh well.
Chris Lattner74d15df2008-11-22 02:02:22 +00001226 if (!isLexingRawMode())
1227 Diag(CurPtr-1, diag::warn_nested_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 }
1229 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner150fcd52010-05-16 19:54:05 +00001230 if (!isLexingRawMode() && !PP->isCodeCompletionFile(FileLoc))
Chris Lattner74d15df2008-11-22 02:02:22 +00001231 Diag(BufferPtr, diag::err_unterminated_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 // Note: the user probably forgot a */. We could continue immediately
1233 // after the /*, but this would involve lexing a lot of what really is the
1234 // comment, which surely would confuse the parser.
Chris Lattner31f0eca2008-10-12 04:19:49 +00001235 --CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001236
Chris Lattner31f0eca2008-10-12 04:19:49 +00001237 // KeepWhitespaceMode should return this broken comment as a token. Since
1238 // it isn't a well formed comment, just return it as an 'unknown' token.
1239 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001240 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001241 return true;
1242 }
Mike Stump1eb44332009-09-09 15:08:12 +00001243
Chris Lattner31f0eca2008-10-12 04:19:49 +00001244 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001245 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 }
1247 C = *CurPtr++;
1248 }
Mike Stump1eb44332009-09-09 15:08:12 +00001249
Chris Lattner3d0ad582010-02-03 21:06:21 +00001250 // Notify comment handlers about the comment unless we're in a #if 0 block.
1251 if (PP && !isLexingRawMode() &&
1252 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
1253 getSourceLocation(CurPtr)))) {
Chris Lattner046c2272010-01-18 22:35:47 +00001254 BufferPtr = CurPtr;
1255 return true; // A token has to be returned.
1256 }
Douglas Gregor2e222532009-07-02 17:08:52 +00001257
Reid Spencer5f016e22007-07-11 17:01:13 +00001258 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +00001259 if (inKeepCommentMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001260 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattner2d381892008-10-12 04:15:42 +00001261 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001262 }
1263
1264 // It is common for the tokens immediately after a /**/ comment to be
1265 // whitespace. Instead of going through the big switch, handle it
Chris Lattnerd88dc482008-10-12 04:05:48 +00001266 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1267 // have already returned above with the comment as a token.
Reid Spencer5f016e22007-07-11 17:01:13 +00001268 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001269 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001270 SkipWhitespace(Result, CurPtr+1);
Chris Lattner2d381892008-10-12 04:15:42 +00001271 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001272 }
1273
1274 // Otherwise, just return so that the next character will be lexed as a token.
1275 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001276 Result.setFlag(Token::LeadingSpace);
Chris Lattner2d381892008-10-12 04:15:42 +00001277 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001278}
1279
1280//===----------------------------------------------------------------------===//
1281// Primary Lexing Entry Points
1282//===----------------------------------------------------------------------===//
1283
Reid Spencer5f016e22007-07-11 17:01:13 +00001284/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1285/// uninterpreted string. This switches the lexer out of directive mode.
1286std::string Lexer::ReadToEndOfLine() {
1287 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1288 "Must be in a preprocessing directive!");
1289 std::string Result;
Chris Lattnerd2177732007-07-20 16:59:19 +00001290 Token Tmp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001291
1292 // CurPtr - Cache BufferPtr in an automatic variable.
1293 const char *CurPtr = BufferPtr;
1294 while (1) {
1295 char Char = getAndAdvanceChar(CurPtr, Tmp);
1296 switch (Char) {
1297 default:
1298 Result += Char;
1299 break;
1300 case 0: // Null.
1301 // Found end of file?
1302 if (CurPtr-1 != BufferEnd) {
1303 // Nope, normal character, continue.
1304 Result += Char;
1305 break;
1306 }
1307 // FALL THROUGH.
1308 case '\r':
1309 case '\n':
1310 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1311 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1312 BufferPtr = CurPtr-1;
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 // Next, lex the character, which should handle the EOM transition.
1315 Lex(Tmp);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001316 assert(Tmp.is(tok::eom) && "Unexpected token!");
Mike Stump1eb44332009-09-09 15:08:12 +00001317
Reid Spencer5f016e22007-07-11 17:01:13 +00001318 // Finally, we're done, return the string we found.
1319 return Result;
1320 }
1321 }
1322}
1323
1324/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1325/// condition, reporting diagnostics and handling other edge cases as required.
1326/// This returns true if Result contains a token, false if PP.Lex should be
1327/// called again.
Chris Lattnerd2177732007-07-20 16:59:19 +00001328bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001329 // If we hit the end of the file while parsing a preprocessor directive,
1330 // end the preprocessor directive first. The next token returned will
1331 // then be the end of file.
1332 if (ParsingPreprocessorDirective) {
1333 // Done parsing the "line".
1334 ParsingPreprocessorDirective = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001335 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00001336 FormTokenWithChars(Result, CurPtr, tok::eom);
Mike Stump1eb44332009-09-09 15:08:12 +00001337
Reid Spencer5f016e22007-07-11 17:01:13 +00001338 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001339 SetCommentRetentionState(PP->getCommentRetentionState());
Reid Spencer5f016e22007-07-11 17:01:13 +00001340 return true; // Have a token.
Mike Stump1eb44332009-09-09 15:08:12 +00001341 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00001342
Reid Spencer5f016e22007-07-11 17:01:13 +00001343 // If we are in raw mode, return this event as an EOF token. Let the caller
1344 // that put us in raw mode handle the event.
Chris Lattner74d15df2008-11-22 02:02:22 +00001345 if (isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001346 Result.startToken();
1347 BufferPtr = BufferEnd;
Chris Lattner9e6293d2008-10-12 04:51:35 +00001348 FormTokenWithChars(Result, BufferEnd, tok::eof);
Reid Spencer5f016e22007-07-11 17:01:13 +00001349 return true;
1350 }
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Douglas Gregor86d9a522009-09-21 16:56:56 +00001352 // Otherwise, check if we are code-completing, then issue diagnostics for
1353 // unterminated #if and missing newline.
Reid Spencer5f016e22007-07-11 17:01:13 +00001354
Douglas Gregor29684422009-12-02 06:49:09 +00001355 if (PP && PP->isCodeCompletionFile(FileLoc)) {
1356 // We're at the end of the file, but we've been asked to consider the
1357 // end of the file to be a code-completion token. Return the
1358 // code-completion token.
1359 Result.startToken();
1360 FormTokenWithChars(Result, CurPtr, tok::code_completion);
Douglas Gregor86d9a522009-09-21 16:56:56 +00001361
Douglas Gregor29684422009-12-02 06:49:09 +00001362 // Only do the eof -> code_completion translation once.
1363 PP->SetCodeCompletionPoint(0, 0, 0);
Douglas Gregordc845342010-05-25 05:58:43 +00001364
1365 // Silence any diagnostics that occur once we hit the code-completion point.
1366 PP->getDiagnostics().setSuppressAllDiagnostics(true);
Douglas Gregor29684422009-12-02 06:49:09 +00001367 return true;
Douglas Gregor86d9a522009-09-21 16:56:56 +00001368 }
1369
Reid Spencer5f016e22007-07-11 17:01:13 +00001370 // If we are in a #if directive, emit an error.
1371 while (!ConditionalStack.empty()) {
Chris Lattner30c64762008-11-22 06:22:39 +00001372 PP->Diag(ConditionalStack.back().IfLoc,
1373 diag::err_pp_unterminated_conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 ConditionalStack.pop_back();
1375 }
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Chris Lattnerb25e5d72008-04-12 05:54:25 +00001377 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1378 // a pedwarn.
1379 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Mike Stump20d0ee52009-04-02 02:29:42 +00001380 Diag(BufferEnd, diag::ext_no_newline_eof)
Douglas Gregor849b2432010-03-31 17:46:05 +00001381 << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001382
Reid Spencer5f016e22007-07-11 17:01:13 +00001383 BufferPtr = CurPtr;
1384
1385 // Finally, let the preprocessor handle this.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001386 return PP->HandleEndOfFile(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001387}
1388
1389/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1390/// the specified lexer will return a tok::l_paren token, 0 if it is something
1391/// else and 2 if there are no more tokens in the buffer controlled by the
1392/// lexer.
1393unsigned Lexer::isNextPPTokenLParen() {
1394 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Reid Spencer5f016e22007-07-11 17:01:13 +00001396 // Switch to 'skipping' mode. This will ensure that we can lex a token
1397 // without emitting diagnostics, disables macro expansion, and will cause EOF
1398 // to return an EOF token instead of popping the include stack.
1399 LexingRawMode = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 // Save state that can be changed while lexing so that we can restore it.
1402 const char *TmpBufferPtr = BufferPtr;
Chris Lattnera864cf72009-04-24 07:15:46 +00001403 bool inPPDirectiveMode = ParsingPreprocessorDirective;
Mike Stump1eb44332009-09-09 15:08:12 +00001404
Chris Lattnerd2177732007-07-20 16:59:19 +00001405 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001406 Tok.startToken();
1407 LexTokenInternal(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +00001408
Reid Spencer5f016e22007-07-11 17:01:13 +00001409 // Restore state that may have changed.
1410 BufferPtr = TmpBufferPtr;
Chris Lattnera864cf72009-04-24 07:15:46 +00001411 ParsingPreprocessorDirective = inPPDirectiveMode;
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Reid Spencer5f016e22007-07-11 17:01:13 +00001413 // Restore the lexer back to non-skipping mode.
1414 LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001415
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001416 if (Tok.is(tok::eof))
Reid Spencer5f016e22007-07-11 17:01:13 +00001417 return 2;
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001418 return Tok.is(tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001419}
1420
Chris Lattner34f349d2009-12-14 06:16:57 +00001421/// FindConflictEnd - Find the end of a version control conflict marker.
1422static const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd) {
1423 llvm::StringRef RestOfBuffer(CurPtr+7, BufferEnd-CurPtr-7);
1424 size_t Pos = RestOfBuffer.find(">>>>>>>");
1425 while (Pos != llvm::StringRef::npos) {
1426 // Must occur at start of line.
1427 if (RestOfBuffer[Pos-1] != '\r' &&
1428 RestOfBuffer[Pos-1] != '\n') {
1429 RestOfBuffer = RestOfBuffer.substr(Pos+7);
Chris Lattner3d488992010-05-17 20:27:25 +00001430 Pos = RestOfBuffer.find(">>>>>>>");
Chris Lattner34f349d2009-12-14 06:16:57 +00001431 continue;
1432 }
1433 return RestOfBuffer.data()+Pos;
1434 }
1435 return 0;
1436}
1437
1438/// IsStartOfConflictMarker - If the specified pointer is the start of a version
1439/// control conflict marker like '<<<<<<<', recognize it as such, emit an error
1440/// and recover nicely. This returns true if it is a conflict marker and false
1441/// if not.
1442bool Lexer::IsStartOfConflictMarker(const char *CurPtr) {
1443 // Only a conflict marker if it starts at the beginning of a line.
1444 if (CurPtr != BufferStart &&
1445 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1446 return false;
1447
1448 // Check to see if we have <<<<<<<.
1449 if (BufferEnd-CurPtr < 8 ||
1450 llvm::StringRef(CurPtr, 7) != "<<<<<<<")
1451 return false;
1452
1453 // If we have a situation where we don't care about conflict markers, ignore
1454 // it.
1455 if (IsInConflictMarker || isLexingRawMode())
1456 return false;
1457
1458 // Check to see if there is a >>>>>>> somewhere in the buffer at the start of
1459 // a line to terminate this conflict marker.
Chris Lattner3d488992010-05-17 20:27:25 +00001460 if (FindConflictEnd(CurPtr, BufferEnd)) {
Chris Lattner34f349d2009-12-14 06:16:57 +00001461 // We found a match. We are really in a conflict marker.
1462 // Diagnose this, and ignore to the end of line.
1463 Diag(CurPtr, diag::err_conflict_marker);
1464 IsInConflictMarker = true;
1465
1466 // Skip ahead to the end of line. We know this exists because the
1467 // end-of-conflict marker starts with \r or \n.
1468 while (*CurPtr != '\r' && *CurPtr != '\n') {
1469 assert(CurPtr != BufferEnd && "Didn't find end of line");
1470 ++CurPtr;
1471 }
1472 BufferPtr = CurPtr;
1473 return true;
1474 }
1475
1476 // No end of conflict marker found.
1477 return false;
1478}
1479
1480
1481/// HandleEndOfConflictMarker - If this is a '=======' or '|||||||' or '>>>>>>>'
1482/// marker, then it is the end of a conflict marker. Handle it by ignoring up
1483/// until the end of the line. This returns true if it is a conflict marker and
1484/// false if not.
1485bool Lexer::HandleEndOfConflictMarker(const char *CurPtr) {
1486 // Only a conflict marker if it starts at the beginning of a line.
1487 if (CurPtr != BufferStart &&
1488 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1489 return false;
1490
1491 // If we have a situation where we don't care about conflict markers, ignore
1492 // it.
1493 if (!IsInConflictMarker || isLexingRawMode())
1494 return false;
1495
1496 // Check to see if we have the marker (7 characters in a row).
1497 for (unsigned i = 1; i != 7; ++i)
1498 if (CurPtr[i] != CurPtr[0])
1499 return false;
1500
1501 // If we do have it, search for the end of the conflict marker. This could
1502 // fail if it got skipped with a '#if 0' or something. Note that CurPtr might
1503 // be the end of conflict marker.
1504 if (const char *End = FindConflictEnd(CurPtr, BufferEnd)) {
1505 CurPtr = End;
1506
1507 // Skip ahead to the end of line.
1508 while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n')
1509 ++CurPtr;
1510
1511 BufferPtr = CurPtr;
1512
1513 // No longer in the conflict marker.
1514 IsInConflictMarker = false;
1515 return true;
1516 }
1517
1518 return false;
1519}
1520
Reid Spencer5f016e22007-07-11 17:01:13 +00001521
1522/// LexTokenInternal - This implements a simple C family lexer. It is an
1523/// extremely performance critical piece of code. This assumes that the buffer
Chris Lattnerefb173d2009-07-07 05:05:42 +00001524/// has a null character at the end of the file. This returns a preprocessing
1525/// token, not a normal token, as such, it is an internal interface. It assumes
1526/// that the Flags of result have been cleared before calling this.
Chris Lattnerd2177732007-07-20 16:59:19 +00001527void Lexer::LexTokenInternal(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001528LexNextToken:
1529 // New token, can't need cleaning yet.
Chris Lattnerd2177732007-07-20 16:59:19 +00001530 Result.clearFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +00001531 Result.setIdentifierInfo(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001532
Reid Spencer5f016e22007-07-11 17:01:13 +00001533 // CurPtr - Cache BufferPtr in an automatic variable.
1534 const char *CurPtr = BufferPtr;
1535
1536 // Small amounts of horizontal whitespace is very common between tokens.
1537 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1538 ++CurPtr;
1539 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1540 ++CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001541
Chris Lattnerd88dc482008-10-12 04:05:48 +00001542 // If we are keeping whitespace and other tokens, just return what we just
1543 // skipped. The next lexer invocation will return the token after the
1544 // whitespace.
1545 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001546 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001547 return;
1548 }
Mike Stump1eb44332009-09-09 15:08:12 +00001549
Reid Spencer5f016e22007-07-11 17:01:13 +00001550 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001551 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001552 }
Mike Stump1eb44332009-09-09 15:08:12 +00001553
Reid Spencer5f016e22007-07-11 17:01:13 +00001554 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
Mike Stump1eb44332009-09-09 15:08:12 +00001555
Reid Spencer5f016e22007-07-11 17:01:13 +00001556 // Read a character, advancing over it.
1557 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001558 tok::TokenKind Kind;
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Reid Spencer5f016e22007-07-11 17:01:13 +00001560 switch (Char) {
1561 case 0: // Null.
1562 // Found end of file?
1563 if (CurPtr-1 == BufferEnd) {
1564 // Read the PP instance variable into an automatic variable, because
1565 // LexEndOfFile will often delete 'this'.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001566 Preprocessor *PPCache = PP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001567 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1568 return; // Got a token to return.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001569 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1570 return PPCache->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001571 }
Mike Stump1eb44332009-09-09 15:08:12 +00001572
Chris Lattner74d15df2008-11-22 02:02:22 +00001573 if (!isLexingRawMode())
1574 Diag(CurPtr-1, diag::null_in_file);
Chris Lattnerd2177732007-07-20 16:59:19 +00001575 Result.setFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001576 if (SkipWhitespace(Result, CurPtr))
1577 return; // KeepWhitespaceMode
Mike Stump1eb44332009-09-09 15:08:12 +00001578
Reid Spencer5f016e22007-07-11 17:01:13 +00001579 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattnera2bf1052009-12-17 05:29:40 +00001580
1581 case 26: // DOS & CP/M EOF: "^Z".
1582 // If we're in Microsoft extensions mode, treat this as end of file.
1583 if (Features.Microsoft) {
1584 // Read the PP instance variable into an automatic variable, because
1585 // LexEndOfFile will often delete 'this'.
1586 Preprocessor *PPCache = PP;
1587 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1588 return; // Got a token to return.
1589 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1590 return PPCache->Lex(Result);
1591 }
1592 // If Microsoft extensions are disabled, this is just random garbage.
1593 Kind = tok::unknown;
1594 break;
1595
Reid Spencer5f016e22007-07-11 17:01:13 +00001596 case '\n':
1597 case '\r':
1598 // If we are inside a preprocessor directive and we see the end of line,
1599 // we know we are done with the directive, so return an EOM token.
1600 if (ParsingPreprocessorDirective) {
1601 // Done parsing the "line".
1602 ParsingPreprocessorDirective = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001603
Reid Spencer5f016e22007-07-11 17:01:13 +00001604 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001605 SetCommentRetentionState(PP->getCommentRetentionState());
Mike Stump1eb44332009-09-09 15:08:12 +00001606
Reid Spencer5f016e22007-07-11 17:01:13 +00001607 // Since we consumed a newline, we are back at the start of a line.
1608 IsAtStartOfLine = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Chris Lattner9e6293d2008-10-12 04:51:35 +00001610 Kind = tok::eom;
Reid Spencer5f016e22007-07-11 17:01:13 +00001611 break;
1612 }
1613 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +00001614 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001615 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +00001616 Result.clearFlag(Token::LeadingSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001617
Chris Lattnerd88dc482008-10-12 04:05:48 +00001618 if (SkipWhitespace(Result, CurPtr))
1619 return; // KeepWhitespaceMode
Reid Spencer5f016e22007-07-11 17:01:13 +00001620 goto LexNextToken; // GCC isn't tail call eliminating.
1621 case ' ':
1622 case '\t':
1623 case '\f':
1624 case '\v':
Chris Lattner8133cfc2007-07-22 06:29:05 +00001625 SkipHorizontalWhitespace:
Chris Lattnerd2177732007-07-20 16:59:19 +00001626 Result.setFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001627 if (SkipWhitespace(Result, CurPtr))
1628 return; // KeepWhitespaceMode
Chris Lattner8133cfc2007-07-22 06:29:05 +00001629
1630 SkipIgnoredUnits:
1631 CurPtr = BufferPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Chris Lattner8133cfc2007-07-22 06:29:05 +00001633 // If the next token is obviously a // or /* */ comment, skip it efficiently
1634 // too (without going through the big switch stmt).
Chris Lattner8402c732009-01-16 22:39:25 +00001635 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1636 Features.BCPLComment) {
Chris Lattner046c2272010-01-18 22:35:47 +00001637 if (SkipBCPLComment(Result, CurPtr+2))
1638 return; // There is a token to return.
Chris Lattner8133cfc2007-07-22 06:29:05 +00001639 goto SkipIgnoredUnits;
Chris Lattnerfa95a012008-10-12 03:22:02 +00001640 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner046c2272010-01-18 22:35:47 +00001641 if (SkipBlockComment(Result, CurPtr+2))
1642 return; // There is a token to return.
Chris Lattner8133cfc2007-07-22 06:29:05 +00001643 goto SkipIgnoredUnits;
1644 } else if (isHorizontalWhitespace(*CurPtr)) {
1645 goto SkipHorizontalWhitespace;
1646 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattnera2bf1052009-12-17 05:29:40 +00001648
Chris Lattner3a570772008-01-03 17:58:54 +00001649 // C99 6.4.4.1: Integer Constants.
1650 // C99 6.4.4.2: Floating Constants.
1651 case '0': case '1': case '2': case '3': case '4':
1652 case '5': case '6': case '7': case '8': case '9':
1653 // Notify MIOpt that we read a non-whitespace/non-comment token.
1654 MIOpt.ReadToken();
1655 return LexNumericConstant(Result, CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001656
Chris Lattner3a570772008-01-03 17:58:54 +00001657 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Reid Spencer5f016e22007-07-11 17:01:13 +00001658 // Notify MIOpt that we read a non-whitespace/non-comment token.
1659 MIOpt.ReadToken();
1660 Char = getCharAndSize(CurPtr, SizeTmp);
1661
1662 // Wide string literal.
1663 if (Char == '"')
1664 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1665 true);
1666
1667 // Wide character constant.
1668 if (Char == '\'')
1669 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1670 // FALL THROUGH, treating L like the start of an identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001671
Reid Spencer5f016e22007-07-11 17:01:13 +00001672 // C99 6.4.2: Identifiers.
1673 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1674 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1675 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1676 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1677 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1678 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1679 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1680 case 'v': case 'w': case 'x': case 'y': case 'z':
1681 case '_':
1682 // Notify MIOpt that we read a non-whitespace/non-comment token.
1683 MIOpt.ReadToken();
1684 return LexIdentifier(Result, CurPtr);
Chris Lattner3a570772008-01-03 17:58:54 +00001685
1686 case '$': // $ in identifiers.
1687 if (Features.DollarIdents) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001688 if (!isLexingRawMode())
1689 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner3a570772008-01-03 17:58:54 +00001690 // Notify MIOpt that we read a non-whitespace/non-comment token.
1691 MIOpt.ReadToken();
1692 return LexIdentifier(Result, CurPtr);
1693 }
Mike Stump1eb44332009-09-09 15:08:12 +00001694
Chris Lattner9e6293d2008-10-12 04:51:35 +00001695 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00001696 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001697
Reid Spencer5f016e22007-07-11 17:01:13 +00001698 // C99 6.4.4: Character Constants.
1699 case '\'':
1700 // Notify MIOpt that we read a non-whitespace/non-comment token.
1701 MIOpt.ReadToken();
1702 return LexCharConstant(Result, CurPtr);
1703
1704 // C99 6.4.5: String Literals.
1705 case '"':
1706 // Notify MIOpt that we read a non-whitespace/non-comment token.
1707 MIOpt.ReadToken();
1708 return LexStringLiteral(Result, CurPtr, false);
1709
1710 // C99 6.4.6: Punctuators.
1711 case '?':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001712 Kind = tok::question;
Reid Spencer5f016e22007-07-11 17:01:13 +00001713 break;
1714 case '[':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001715 Kind = tok::l_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001716 break;
1717 case ']':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001718 Kind = tok::r_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001719 break;
1720 case '(':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001721 Kind = tok::l_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001722 break;
1723 case ')':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001724 Kind = tok::r_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001725 break;
1726 case '{':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001727 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001728 break;
1729 case '}':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001730 Kind = tok::r_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001731 break;
1732 case '.':
1733 Char = getCharAndSize(CurPtr, SizeTmp);
1734 if (Char >= '0' && Char <= '9') {
1735 // Notify MIOpt that we read a non-whitespace/non-comment token.
1736 MIOpt.ReadToken();
1737
1738 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1739 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001740 Kind = tok::periodstar;
Reid Spencer5f016e22007-07-11 17:01:13 +00001741 CurPtr += SizeTmp;
1742 } else if (Char == '.' &&
1743 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001744 Kind = tok::ellipsis;
Reid Spencer5f016e22007-07-11 17:01:13 +00001745 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1746 SizeTmp2, Result);
1747 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001748 Kind = tok::period;
Reid Spencer5f016e22007-07-11 17:01:13 +00001749 }
1750 break;
1751 case '&':
1752 Char = getCharAndSize(CurPtr, SizeTmp);
1753 if (Char == '&') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001754 Kind = tok::ampamp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001755 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1756 } else if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001757 Kind = tok::ampequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1759 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001760 Kind = tok::amp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001761 }
1762 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001763 case '*':
Reid Spencer5f016e22007-07-11 17:01:13 +00001764 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001765 Kind = tok::starequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001766 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1767 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001768 Kind = tok::star;
Reid Spencer5f016e22007-07-11 17:01:13 +00001769 }
1770 break;
1771 case '+':
1772 Char = getCharAndSize(CurPtr, SizeTmp);
1773 if (Char == '+') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001774 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001775 Kind = tok::plusplus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001776 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001777 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001778 Kind = tok::plusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001779 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001780 Kind = tok::plus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001781 }
1782 break;
1783 case '-':
1784 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001785 if (Char == '-') { // --
Reid Spencer5f016e22007-07-11 17:01:13 +00001786 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001787 Kind = tok::minusminus;
Mike Stump1eb44332009-09-09 15:08:12 +00001788 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattner9e6293d2008-10-12 04:51:35 +00001789 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Reid Spencer5f016e22007-07-11 17:01:13 +00001790 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1791 SizeTmp2, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001792 Kind = tok::arrowstar;
1793 } else if (Char == '>') { // ->
Reid Spencer5f016e22007-07-11 17:01:13 +00001794 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001795 Kind = tok::arrow;
1796 } else if (Char == '=') { // -=
Reid Spencer5f016e22007-07-11 17:01:13 +00001797 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001798 Kind = tok::minusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001799 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001800 Kind = tok::minus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001801 }
1802 break;
1803 case '~':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001804 Kind = tok::tilde;
Reid Spencer5f016e22007-07-11 17:01:13 +00001805 break;
1806 case '!':
1807 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001808 Kind = tok::exclaimequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001809 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1810 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001811 Kind = tok::exclaim;
Reid Spencer5f016e22007-07-11 17:01:13 +00001812 }
1813 break;
1814 case '/':
1815 // 6.4.9: Comments
1816 Char = getCharAndSize(CurPtr, SizeTmp);
1817 if (Char == '/') { // BCPL comment.
Chris Lattner8402c732009-01-16 22:39:25 +00001818 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
1819 // want to lex this as a comment. There is one problem with this though,
1820 // that in one particular corner case, this can change the behavior of the
1821 // resultant program. For example, In "foo //**/ bar", C89 would lex
1822 // this as "foo / bar" and langauges with BCPL comments would lex it as
1823 // "foo". Check to see if the character after the second slash is a '*'.
1824 // If so, we will lex that as a "/" instead of the start of a comment.
1825 if (Features.BCPLComment ||
1826 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
1827 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner046c2272010-01-18 22:35:47 +00001828 return; // There is a token to return.
Mike Stump1eb44332009-09-09 15:08:12 +00001829
Chris Lattner8402c732009-01-16 22:39:25 +00001830 // It is common for the tokens immediately after a // comment to be
1831 // whitespace (indentation for the next line). Instead of going through
1832 // the big switch, handle it efficiently now.
1833 goto SkipIgnoredUnits;
1834 }
1835 }
Mike Stump1eb44332009-09-09 15:08:12 +00001836
Chris Lattner8402c732009-01-16 22:39:25 +00001837 if (Char == '*') { // /**/ comment.
Reid Spencer5f016e22007-07-11 17:01:13 +00001838 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner046c2272010-01-18 22:35:47 +00001839 return; // There is a token to return.
Chris Lattner2d381892008-10-12 04:15:42 +00001840 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner8402c732009-01-16 22:39:25 +00001841 }
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Chris Lattner8402c732009-01-16 22:39:25 +00001843 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001844 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001845 Kind = tok::slashequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001846 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001847 Kind = tok::slash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001848 }
1849 break;
1850 case '%':
1851 Char = getCharAndSize(CurPtr, SizeTmp);
1852 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001853 Kind = tok::percentequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001854 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1855 } else if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001856 Kind = tok::r_brace; // '%>' -> '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001857 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1858 } else if (Features.Digraphs && Char == ':') {
1859 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1860 Char = getCharAndSize(CurPtr, SizeTmp);
1861 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001862 Kind = tok::hashhash; // '%:%:' -> '##'
Reid Spencer5f016e22007-07-11 17:01:13 +00001863 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1864 SizeTmp2, Result);
1865 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Reid Spencer5f016e22007-07-11 17:01:13 +00001866 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner74d15df2008-11-22 02:02:22 +00001867 if (!isLexingRawMode())
1868 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001869 Kind = tok::hashat;
Chris Lattnere91e9322009-03-18 20:58:27 +00001870 } else { // '%:' -> '#'
Reid Spencer5f016e22007-07-11 17:01:13 +00001871 // We parsed a # character. If this occurs at the start of the line,
1872 // it's actually the start of a preprocessing directive. Callback to
1873 // the preprocessor to handle it.
1874 // FIXME: -fpreprocessed mode??
Chris Lattner766703b2009-05-13 06:10:29 +00001875 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattnere91e9322009-03-18 20:58:27 +00001876 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner168ae2d2007-10-17 20:41:00 +00001877 PP->HandleDirective(Result);
Mike Stump1eb44332009-09-09 15:08:12 +00001878
Reid Spencer5f016e22007-07-11 17:01:13 +00001879 // As an optimization, if the preprocessor didn't switch lexers, tail
1880 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001881 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001882 // Start a new token. If this is a #include or something, the PP may
1883 // want us starting at the beginning of the line again. If so, set
Chris Lattner515f43f2010-04-12 23:04:41 +00001884 // the StartOfLine flag and clear LeadingSpace.
Reid Spencer5f016e22007-07-11 17:01:13 +00001885 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001886 Result.setFlag(Token::StartOfLine);
Chris Lattner515f43f2010-04-12 23:04:41 +00001887 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001888 IsAtStartOfLine = false;
1889 }
1890 goto LexNextToken; // GCC isn't tail call eliminating.
1891 }
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Chris Lattner168ae2d2007-10-17 20:41:00 +00001893 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001894 }
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Chris Lattnere91e9322009-03-18 20:58:27 +00001896 Kind = tok::hash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001897 }
1898 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001899 Kind = tok::percent;
Reid Spencer5f016e22007-07-11 17:01:13 +00001900 }
1901 break;
1902 case '<':
1903 Char = getCharAndSize(CurPtr, SizeTmp);
1904 if (ParsingFilename) {
Chris Lattner9cb51ce2009-04-17 23:56:52 +00001905 return LexAngledStringLiteral(Result, CurPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001906 } else if (Char == '<') {
Chris Lattner34f349d2009-12-14 06:16:57 +00001907 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
1908 if (After == '=') {
1909 Kind = tok::lesslessequal;
1910 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1911 SizeTmp2, Result);
1912 } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) {
1913 // If this is actually a '<<<<<<<' version control conflict marker,
1914 // recognize it as such and recover nicely.
1915 goto LexNextToken;
1916 } else {
1917 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1918 Kind = tok::lessless;
1919 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001920 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001921 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001922 Kind = tok::lessequal;
1923 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Reid Spencer5f016e22007-07-11 17:01:13 +00001924 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001925 Kind = tok::l_square;
1926 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Reid Spencer5f016e22007-07-11 17:01:13 +00001927 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001928 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001929 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001930 Kind = tok::less;
Reid Spencer5f016e22007-07-11 17:01:13 +00001931 }
1932 break;
1933 case '>':
1934 Char = getCharAndSize(CurPtr, SizeTmp);
1935 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001936 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001937 Kind = tok::greaterequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001938 } else if (Char == '>') {
Chris Lattner34f349d2009-12-14 06:16:57 +00001939 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
1940 if (After == '=') {
1941 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1942 SizeTmp2, Result);
1943 Kind = tok::greatergreaterequal;
1944 } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) {
1945 // If this is '>>>>>>>' and we're in a conflict marker, ignore it.
1946 goto LexNextToken;
1947 } else {
1948 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1949 Kind = tok::greatergreater;
1950 }
1951
Reid Spencer5f016e22007-07-11 17:01:13 +00001952 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001953 Kind = tok::greater;
Reid Spencer5f016e22007-07-11 17:01:13 +00001954 }
1955 break;
1956 case '^':
1957 Char = getCharAndSize(CurPtr, SizeTmp);
1958 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001959 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001960 Kind = tok::caretequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001961 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001962 Kind = tok::caret;
Reid Spencer5f016e22007-07-11 17:01:13 +00001963 }
1964 break;
1965 case '|':
1966 Char = getCharAndSize(CurPtr, SizeTmp);
1967 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001968 Kind = tok::pipeequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001969 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1970 } else if (Char == '|') {
Chris Lattner34f349d2009-12-14 06:16:57 +00001971 // If this is '|||||||' and we're in a conflict marker, ignore it.
1972 if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1))
1973 goto LexNextToken;
Chris Lattner9e6293d2008-10-12 04:51:35 +00001974 Kind = tok::pipepipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00001975 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1976 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001977 Kind = tok::pipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00001978 }
1979 break;
1980 case ':':
1981 Char = getCharAndSize(CurPtr, SizeTmp);
1982 if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001983 Kind = tok::r_square; // ':>' -> ']'
Reid Spencer5f016e22007-07-11 17:01:13 +00001984 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1985 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001986 Kind = tok::coloncolon;
Reid Spencer5f016e22007-07-11 17:01:13 +00001987 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump1eb44332009-09-09 15:08:12 +00001988 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001989 Kind = tok::colon;
Reid Spencer5f016e22007-07-11 17:01:13 +00001990 }
1991 break;
1992 case ';':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001993 Kind = tok::semi;
Reid Spencer5f016e22007-07-11 17:01:13 +00001994 break;
1995 case '=':
1996 Char = getCharAndSize(CurPtr, SizeTmp);
1997 if (Char == '=') {
Chris Lattner34f349d2009-12-14 06:16:57 +00001998 // If this is '=======' and we're in a conflict marker, ignore it.
1999 if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1))
2000 goto LexNextToken;
2001
Chris Lattner9e6293d2008-10-12 04:51:35 +00002002 Kind = tok::equalequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00002003 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump1eb44332009-09-09 15:08:12 +00002004 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002005 Kind = tok::equal;
Reid Spencer5f016e22007-07-11 17:01:13 +00002006 }
2007 break;
2008 case ',':
Chris Lattner9e6293d2008-10-12 04:51:35 +00002009 Kind = tok::comma;
Reid Spencer5f016e22007-07-11 17:01:13 +00002010 break;
2011 case '#':
2012 Char = getCharAndSize(CurPtr, SizeTmp);
2013 if (Char == '#') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00002014 Kind = tok::hashhash;
Reid Spencer5f016e22007-07-11 17:01:13 +00002015 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2016 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner9e6293d2008-10-12 04:51:35 +00002017 Kind = tok::hashat;
Chris Lattner74d15df2008-11-22 02:02:22 +00002018 if (!isLexingRawMode())
2019 Diag(BufferPtr, diag::charize_microsoft_ext);
Reid Spencer5f016e22007-07-11 17:01:13 +00002020 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
2021 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +00002022 // We parsed a # character. If this occurs at the start of the line,
2023 // it's actually the start of a preprocessing directive. Callback to
2024 // the preprocessor to handle it.
2025 // FIXME: -fpreprocessed mode??
Chris Lattner766703b2009-05-13 06:10:29 +00002026 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattnere91e9322009-03-18 20:58:27 +00002027 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner168ae2d2007-10-17 20:41:00 +00002028 PP->HandleDirective(Result);
Mike Stump1eb44332009-09-09 15:08:12 +00002029
Reid Spencer5f016e22007-07-11 17:01:13 +00002030 // As an optimization, if the preprocessor didn't switch lexers, tail
2031 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00002032 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002033 // Start a new token. If this is a #include or something, the PP may
2034 // want us starting at the beginning of the line again. If so, set
Chris Lattner515f43f2010-04-12 23:04:41 +00002035 // the StartOfLine flag and clear LeadingSpace.
Reid Spencer5f016e22007-07-11 17:01:13 +00002036 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00002037 Result.setFlag(Token::StartOfLine);
Chris Lattner515f43f2010-04-12 23:04:41 +00002038 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00002039 IsAtStartOfLine = false;
2040 }
2041 goto LexNextToken; // GCC isn't tail call eliminating.
2042 }
Chris Lattner168ae2d2007-10-17 20:41:00 +00002043 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00002044 }
Mike Stump1eb44332009-09-09 15:08:12 +00002045
Chris Lattnere91e9322009-03-18 20:58:27 +00002046 Kind = tok::hash;
Reid Spencer5f016e22007-07-11 17:01:13 +00002047 }
2048 break;
2049
Chris Lattner3a570772008-01-03 17:58:54 +00002050 case '@':
2051 // Objective C support.
2052 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattner9e6293d2008-10-12 04:51:35 +00002053 Kind = tok::at;
Chris Lattner3a570772008-01-03 17:58:54 +00002054 else
Chris Lattner9e6293d2008-10-12 04:51:35 +00002055 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00002056 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002057
Reid Spencer5f016e22007-07-11 17:01:13 +00002058 case '\\':
2059 // FIXME: UCN's.
2060 // FALL THROUGH.
2061 default:
Chris Lattner9e6293d2008-10-12 04:51:35 +00002062 Kind = tok::unknown;
Reid Spencer5f016e22007-07-11 17:01:13 +00002063 break;
2064 }
Mike Stump1eb44332009-09-09 15:08:12 +00002065
Reid Spencer5f016e22007-07-11 17:01:13 +00002066 // Notify MIOpt that we read a non-whitespace/non-comment token.
2067 MIOpt.ReadToken();
2068
2069 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00002070 FormTokenWithChars(Result, CurPtr, Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00002071}