blob: 3207062ccadd623ba1a87b7589bfae3ce3b3cdcd [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner146762e2007-07-20 16:59:19 +000010// This file implements the Lexer and Token interfaces.
Chris Lattner22eb9722006-06-18 05:43:12 +000011//
12//===----------------------------------------------------------------------===//
13//
14// TODO: GCC Diagnostics emitted by the lexer:
15// PEDWARN: (form feed|vertical tab) in preprocessing directive
16//
17// Universal characters, unicode, char mapping:
18// WARNING: `%.*s' is not in NFKC
19// WARNING: `%.*s' is not in NFC
20//
21// Other:
Chris Lattner22eb9722006-06-18 05:43:12 +000022// TODO: Options to support:
23// -fexec-charset,-fwide-exec-charset
24//
25//===----------------------------------------------------------------------===//
26
27#include "clang/Lex/Lexer.h"
28#include "clang/Lex/Preprocessor.h"
Chris Lattner60f36222009-01-29 05:15:15 +000029#include "clang/Lex/LexDiagnostic.h"
Chris Lattnerdc5c0552007-07-20 16:37:10 +000030#include "clang/Basic/SourceManager.h"
Chris Lattner619c1742007-07-22 18:38:25 +000031#include "llvm/Support/Compiler.h"
Chris Lattner739e7392007-04-29 07:12:06 +000032#include "llvm/Support/MemoryBuffer.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000033#include <cctype>
Chris Lattner22eb9722006-06-18 05:43:12 +000034using namespace clang;
35
Chris Lattner3dfff972009-12-17 05:29:40 +000036static void InitCharacterInfo();
Chris Lattner22eb9722006-06-18 05:43:12 +000037
Chris Lattner4894f482007-10-07 08:47:24 +000038//===----------------------------------------------------------------------===//
39// Token Class Implementation
40//===----------------------------------------------------------------------===//
41
Mike Stump11289f42009-09-09 15:08:12 +000042/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
Chris Lattner4894f482007-10-07 08:47:24 +000043bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
Douglas Gregor90abb6d2008-12-01 21:46:47 +000044 if (IdentifierInfo *II = getIdentifierInfo())
45 return II->getObjCKeywordID() == objcKey;
46 return false;
Chris Lattner4894f482007-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 Lattner67671ed2007-12-13 01:59:49 +000055
Chris Lattner4894f482007-10-07 08:47:24 +000056//===----------------------------------------------------------------------===//
57// Lexer Class Implementation
58//===----------------------------------------------------------------------===//
59
Mike Stump11289f42009-09-09 15:08:12 +000060void Lexer::InitLexer(const char *BufStart, const char *BufPtr,
Chris Lattnerf76b9202009-01-17 06:55:17 +000061 const char *BufEnd) {
Chris Lattner3dfff972009-12-17 05:29:40 +000062 InitCharacterInfo();
Mike Stump11289f42009-09-09 15:08:12 +000063
Chris Lattnerf76b9202009-01-17 06:55:17 +000064 BufferStart = BufStart;
65 BufferPtr = BufPtr;
66 BufferEnd = BufEnd;
Mike Stump11289f42009-09-09 15:08:12 +000067
Chris Lattnerf76b9202009-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 Stump11289f42009-09-09 15:08:12 +000071
Chris Lattnerf76b9202009-01-17 06:55:17 +000072 Is_PragmaLexer = false;
Chris Lattner7c027ee2009-12-14 06:16:57 +000073 IsInConflictMarker = false;
Douglas Gregor2436e712009-09-17 21:32:03 +000074
Chris Lattnerf76b9202009-01-17 06:55:17 +000075 // Start of the file is a start of line.
76 IsAtStartOfLine = true;
Mike Stump11289f42009-09-09 15:08:12 +000077
Chris Lattnerf76b9202009-01-17 06:55:17 +000078 // We are not after parsing a #.
79 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +000080
Chris Lattnerf76b9202009-01-17 06:55:17 +000081 // We are not after parsing #include.
82 ParsingFilename = false;
Mike Stump11289f42009-09-09 15:08:12 +000083
Chris Lattnerf76b9202009-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 Stump11289f42009-09-09 15:08:12 +000089
Chris Lattnerf76b9202009-01-17 06:55:17 +000090 // Default to not keeping comments.
91 ExtendedTokenMode = 0;
92}
93
Chris Lattner5965a282009-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 Lattner710bb872009-11-30 04:18:44 +000098Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP)
Chris Lattnerc8090892009-01-17 08:03:42 +000099 : PreprocessorLexer(&PP, FID),
100 FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
101 Features(PP.getLangOptions()) {
Mike Stump11289f42009-09-09 15:08:12 +0000102
Chris Lattner5965a282009-01-17 07:56:59 +0000103 InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
104 InputFile->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000105
Chris Lattner5965a282009-01-17 07:56:59 +0000106 // Default to keeping comments if the preprocessor wants them.
107 SetCommentRetentionState(PP.getCommentRetentionState());
108}
Chris Lattner4894f482007-10-07 08:47:24 +0000109
Chris Lattner02b436a2007-10-17 20:41:00 +0000110/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner50c90502008-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 Lattner02b436a2007-10-17 20:41:00 +0000113Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattnerfcf64522009-01-17 07:42:27 +0000114 const char *BufStart, const char *BufPtr, const char *BufEnd)
Chris Lattner1abd2092009-01-17 03:48:08 +0000115 : FileLoc(fileloc), Features(features) {
Chris Lattnerf76b9202009-01-17 06:55:17 +0000116
Chris Lattnerf76b9202009-01-17 06:55:17 +0000117 InitLexer(BufStart, BufPtr, BufEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000118
Chris Lattner02b436a2007-10-17 20:41:00 +0000119 // We *are* in raw mode.
120 LexingRawMode = true;
Chris Lattner02b436a2007-10-17 20:41:00 +0000121}
122
Chris Lattner08354fe2009-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 Lattner710bb872009-11-30 04:18:44 +0000126Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile,
127 const SourceManager &SM, const LangOptions &features)
Chris Lattner08354fe2009-01-17 07:35:14 +0000128 : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
Chris Lattner08354fe2009-01-17 07:35:14 +0000129
Mike Stump11289f42009-09-09 15:08:12 +0000130 InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
Chris Lattner08354fe2009-01-17 07:35:14 +0000131 FromFile->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000132
Chris Lattner08354fe2009-01-17 07:35:14 +0000133 // We *are* in raw mode.
134 LexingRawMode = true;
135}
136
Chris Lattner757169b2009-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 Stump11289f42009-09-09 15:08:12 +0000152Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
Chris Lattner9dc9c202009-02-15 20:52:18 +0000153 SourceLocation InstantiationLocStart,
154 SourceLocation InstantiationLocEnd,
Chris Lattner29a2a192009-01-19 06:46:35 +0000155 unsigned TokLen, Preprocessor &PP) {
Chris Lattner757169b2009-01-17 08:27:52 +0000156 SourceManager &SM = PP.getSourceManager();
Chris Lattner757169b2009-01-17 08:27:52 +0000157
158 // Create the lexer as if we were going to lex the file normally.
Chris Lattnercbc35ecb2009-01-19 07:46:45 +0000159 FileID SpellingFID = SM.getFileID(SpellingLoc);
Chris Lattner710bb872009-11-30 04:18:44 +0000160 const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID);
161 Lexer *L = new Lexer(SpellingFID, InputFile, PP);
Mike Stump11289f42009-09-09 15:08:12 +0000162
Chris Lattner757169b2009-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 Stump11289f42009-09-09 15:08:12 +0000167
Chris Lattner757169b2009-01-17 08:27:52 +0000168 L->BufferPtr = StrData;
169 L->BufferEnd = StrData+TokLen;
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000170 assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
Chris Lattner757169b2009-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 Lattner4fa23622009-01-26 00:43:02 +0000174 L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
Chris Lattner9dc9c202009-02-15 20:52:18 +0000175 InstantiationLocStart,
176 InstantiationLocEnd, TokLen);
Mike Stump11289f42009-09-09 15:08:12 +0000177
Chris Lattner757169b2009-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 Stump11289f42009-09-09 15:08:12 +0000181
Chris Lattner757169b2009-01-17 08:27:52 +0000182 // This lexer really is for _Pragma.
183 L->Is_PragmaLexer = true;
184 return L;
185}
186
Chris Lattner02b436a2007-10-17 20:41:00 +0000187
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000188/// Stringify - Convert the specified string into a C string, with surrounding
189/// ""'s, and with escaped \ and " characters.
Chris Lattnerecc39e92006-07-15 05:23:31 +0000190std::string Lexer::Stringify(const std::string &Str, bool Charify) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000191 std::string Result = Str;
Chris Lattnerecc39e92006-07-15 05:23:31 +0000192 char Quote = Charify ? '\'' : '"';
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000193 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
Chris Lattnerecc39e92006-07-15 05:23:31 +0000194 if (Result[i] == '\\' || Result[i] == Quote) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000195 Result.insert(Result.begin()+i, '\\');
196 ++i; ++e;
197 }
198 }
Chris Lattnerecc39e92006-07-15 05:23:31 +0000199 return Result;
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000200}
201
Chris Lattner4c4a2452007-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 Gregor562c1f92010-01-22 19:49:59 +0000213static bool isWhitespace(unsigned char c);
Chris Lattner22eb9722006-06-18 05:43:12 +0000214
Chris Lattner8e129c22007-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 Lattner184e65d2009-04-14 23:22:57 +0000220 const SourceManager &SM,
221 const LangOptions &LangOpts) {
Chris Lattner8e129c22007-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 Stump11289f42009-09-09 15:08:12 +0000224 // all obviously single-char tokens. This could use
Chris Lattner8e129c22007-10-17 21:18:47 +0000225 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
226 // something.
Chris Lattner4fa23622009-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 Lattnerd3817212009-01-26 22:24:27 +0000230 Loc = SM.getInstantiationLoc(Loc);
231 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
Chris Lattner5509d532009-01-17 08:30:10 +0000232 std::pair<const char *,const char *> Buffer = SM.getBufferData(LocInfo.first);
233 const char *StrData = Buffer.first+LocInfo.second;
234
Douglas Gregor562c1f92010-01-22 19:49:59 +0000235 if (isWhitespace(StrData[0]))
236 return 0;
237
Chris Lattner8e129c22007-10-17 21:18:47 +0000238 // Create a lexer starting at the beginning of this token.
Chris Lattnerfcf64522009-01-17 07:42:27 +0000239 Lexer TheLexer(Loc, LangOpts, Buffer.first, StrData, Buffer.second);
Chris Lattnera3d4f162009-10-14 15:04:18 +0000240 TheLexer.SetCommentRetentionState(true);
Chris Lattner8e129c22007-10-17 21:18:47 +0000241 Token TheTok;
Chris Lattner50c90502008-10-12 01:15:46 +0000242 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner8e129c22007-10-17 21:18:47 +0000243 return TheTok.getLength();
244}
245
Chris Lattner22eb9722006-06-18 05:43:12 +0000246//===----------------------------------------------------------------------===//
247// Character information.
248//===----------------------------------------------------------------------===//
249
Chris Lattner22eb9722006-06-18 05:43:12 +0000250enum {
251 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
252 CHAR_VERT_WS = 0x02, // '\r', '\n'
253 CHAR_LETTER = 0x04, // a-z,A-Z
254 CHAR_NUMBER = 0x08, // 0-9
255 CHAR_UNDER = 0x10, // _
256 CHAR_PERIOD = 0x20 // .
257};
258
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000259// Statically initialize CharInfo table based on ASCII character set
260// Reference: FreeBSD 7.2 /usr/share/misc/ascii
Chris Lattner3dfff972009-12-17 05:29:40 +0000261static const unsigned char CharInfo[256] =
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000262{
263// 0 NUL 1 SOH 2 STX 3 ETX
264// 4 EOT 5 ENQ 6 ACK 7 BEL
265 0 , 0 , 0 , 0 ,
266 0 , 0 , 0 , 0 ,
267// 8 BS 9 HT 10 NL 11 VT
268//12 NP 13 CR 14 SO 15 SI
269 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS,
270 CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 ,
271//16 DLE 17 DC1 18 DC2 19 DC3
272//20 DC4 21 NAK 22 SYN 23 ETB
273 0 , 0 , 0 , 0 ,
274 0 , 0 , 0 , 0 ,
275//24 CAN 25 EM 26 SUB 27 ESC
276//28 FS 29 GS 30 RS 31 US
277 0 , 0 , 0 , 0 ,
278 0 , 0 , 0 , 0 ,
279//32 SP 33 ! 34 " 35 #
280//36 $ 37 % 38 & 39 '
281 CHAR_HORZ_WS, 0 , 0 , 0 ,
282 0 , 0 , 0 , 0 ,
283//40 ( 41 ) 42 * 43 +
284//44 , 45 - 46 . 47 /
285 0 , 0 , 0 , 0 ,
286 0 , 0 , CHAR_PERIOD , 0 ,
287//48 0 49 1 50 2 51 3
288//52 4 53 5 54 6 55 7
289 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
290 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
291//56 8 57 9 58 : 59 ;
292//60 < 61 = 62 > 63 ?
293 CHAR_NUMBER , CHAR_NUMBER , 0 , 0 ,
294 0 , 0 , 0 , 0 ,
295//64 @ 65 A 66 B 67 C
296//68 D 69 E 70 F 71 G
297 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
298 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
299//72 H 73 I 74 J 75 K
300//76 L 77 M 78 N 79 O
301 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
302 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
303//80 P 81 Q 82 R 83 S
304//84 T 85 U 86 V 87 W
305 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
306 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
307//88 X 89 Y 90 Z 91 [
308//92 \ 93 ] 94 ^ 95 _
309 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
310 0 , 0 , 0 , CHAR_UNDER ,
311//96 ` 97 a 98 b 99 c
312//100 d 101 e 102 f 103 g
313 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
314 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
315//104 h 105 i 106 j 107 k
316//108 l 109 m 110 n 111 o
317 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
318 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
319//112 p 113 q 114 r 115 s
320//116 t 117 u 118 v 119 w
321 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
322 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
323//120 x 121 y 122 z 123 {
324//124 | 125 } 126 ~ 127 DEL
325 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
326 0 , 0 , 0 , 0
327};
328
Chris Lattner3dfff972009-12-17 05:29:40 +0000329static void InitCharacterInfo() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000330 static bool isInited = false;
331 if (isInited) return;
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000332 // check the statically-initialized CharInfo table
333 assert(CHAR_HORZ_WS == CharInfo[(int)' ']);
334 assert(CHAR_HORZ_WS == CharInfo[(int)'\t']);
335 assert(CHAR_HORZ_WS == CharInfo[(int)'\f']);
336 assert(CHAR_HORZ_WS == CharInfo[(int)'\v']);
337 assert(CHAR_VERT_WS == CharInfo[(int)'\n']);
338 assert(CHAR_VERT_WS == CharInfo[(int)'\r']);
339 assert(CHAR_UNDER == CharInfo[(int)'_']);
340 assert(CHAR_PERIOD == CharInfo[(int)'.']);
341 for (unsigned i = 'a'; i <= 'z'; ++i) {
342 assert(CHAR_LETTER == CharInfo[i]);
343 assert(CHAR_LETTER == CharInfo[i+'A'-'a']);
344 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000345 for (unsigned i = '0'; i <= '9'; ++i)
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000346 assert(CHAR_NUMBER == CharInfo[i]);
Steve Naroff04bc0182009-12-08 16:38:12 +0000347
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000348 isInited = true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000349}
350
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000351
Chris Lattner22eb9722006-06-18 05:43:12 +0000352/// isIdentifierBody - Return true if this is the body character of an
353/// identifier, which is [a-zA-Z0-9_].
354static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000355 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000356}
357
358/// isHorizontalWhitespace - Return true if this character is horizontal
359/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
360static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000361 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000362}
363
364/// isWhitespace - Return true if this character is horizontal or vertical
365/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
366/// for '\0'.
367static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000368 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000369}
370
371/// isNumberBody - Return true if this is the body character of an
372/// preprocessing number, which is [a-zA-Z0-9_.].
373static inline bool isNumberBody(unsigned char c) {
Mike Stump11289f42009-09-09 15:08:12 +0000374 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000375 true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000376}
377
Chris Lattnerd01e2912006-06-18 16:22:51 +0000378
Chris Lattner22eb9722006-06-18 05:43:12 +0000379//===----------------------------------------------------------------------===//
380// Diagnostics forwarding code.
381//===----------------------------------------------------------------------===//
382
Chris Lattner619c1742007-07-22 18:38:25 +0000383/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
384/// lexer buffer was all instantiated at a single point, perform the mapping.
385/// This is currently only used for _Pragma implementation, so it is the slow
386/// path of the hot getSourceLocation method. Do not allow it to be inlined.
Benjamin Kramer5e738282009-11-14 16:36:57 +0000387static DISABLE_INLINE SourceLocation GetMappedTokenLoc(Preprocessor &PP,
388 SourceLocation FileLoc,
389 unsigned CharNo,
390 unsigned TokLen);
Chris Lattner619c1742007-07-22 18:38:25 +0000391static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
392 SourceLocation FileLoc,
Chris Lattner4fa23622009-01-26 00:43:02 +0000393 unsigned CharNo, unsigned TokLen) {
Chris Lattner9dc9c202009-02-15 20:52:18 +0000394 assert(FileLoc.isMacroID() && "Must be an instantiation");
Mike Stump11289f42009-09-09 15:08:12 +0000395
Chris Lattner619c1742007-07-22 18:38:25 +0000396 // Otherwise, we're lexing "mapped tokens". This is used for things like
397 // _Pragma handling. Combine the instantiation location of FileLoc with the
Chris Lattner53e384f2009-01-16 07:00:02 +0000398 // spelling location.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000399 SourceManager &SM = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000400
Chris Lattner8a425862009-01-16 07:36:28 +0000401 // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
Chris Lattner53e384f2009-01-16 07:00:02 +0000402 // characters come from spelling(FileLoc)+Offset.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000403 SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
Chris Lattner29a2a192009-01-19 06:46:35 +0000404 SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
Mike Stump11289f42009-09-09 15:08:12 +0000405
Chris Lattner9dc9c202009-02-15 20:52:18 +0000406 // Figure out the expansion loc range, which is the range covered by the
407 // original _Pragma(...) sequence.
408 std::pair<SourceLocation,SourceLocation> II =
409 SM.getImmediateInstantiationRange(FileLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000410
Chris Lattner9dc9c202009-02-15 20:52:18 +0000411 return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
Chris Lattner619c1742007-07-22 18:38:25 +0000412}
413
Chris Lattner22eb9722006-06-18 05:43:12 +0000414/// getSourceLocation - Return a source location identifier for the specified
415/// offset in the current file.
Chris Lattner4fa23622009-01-26 00:43:02 +0000416SourceLocation Lexer::getSourceLocation(const char *Loc,
417 unsigned TokLen) const {
Chris Lattner5d1c0272007-07-22 18:44:36 +0000418 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Chris Lattner4cca5ba2006-07-02 20:05:54 +0000419 "Location out of range for this buffer!");
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000420
421 // In the normal case, we're just lexing from a simple file buffer, return
422 // the file id from FileLoc with the offset specified.
Chris Lattner5d1c0272007-07-22 18:44:36 +0000423 unsigned CharNo = Loc-BufferStart;
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000424 if (FileLoc.isFileID())
Chris Lattner29a2a192009-01-19 06:46:35 +0000425 return FileLoc.getFileLocWithOffset(CharNo);
Mike Stump11289f42009-09-09 15:08:12 +0000426
Chris Lattnerd32480d2009-01-17 06:22:33 +0000427 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
428 // tokens are lexed from where the _Pragma was defined.
Chris Lattner02b436a2007-10-17 20:41:00 +0000429 assert(PP && "This doesn't work on raw lexers");
Chris Lattner4fa23622009-01-26 00:43:02 +0000430 return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
Chris Lattner22eb9722006-06-18 05:43:12 +0000431}
432
Chris Lattner22eb9722006-06-18 05:43:12 +0000433/// Diag - Forwarding function for diagnostics. This translate a source
434/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner427c9c12008-11-22 00:59:29 +0000435DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner907dfe92008-11-18 07:59:24 +0000436 return PP->Diag(getSourceLocation(Loc), DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000437}
438
439//===----------------------------------------------------------------------===//
440// Trigraph and Escaped Newline Handling Code.
441//===----------------------------------------------------------------------===//
442
443/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
444/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
445static char GetTrigraphCharForLetter(char Letter) {
446 switch (Letter) {
447 default: return 0;
448 case '=': return '#';
449 case ')': return ']';
450 case '(': return '[';
451 case '!': return '|';
452 case '\'': return '^';
453 case '>': return '}';
454 case '/': return '\\';
455 case '<': return '{';
456 case '-': return '~';
457 }
458}
459
460/// DecodeTrigraphChar - If the specified character is a legal trigraph when
461/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
462/// return the result character. Finally, emit a warning about trigraph use
463/// whether trigraphs are enabled or not.
464static char DecodeTrigraphChar(const char *CP, Lexer *L) {
465 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner907dfe92008-11-18 07:59:24 +0000466 if (!Res || !L) return Res;
Mike Stump11289f42009-09-09 15:08:12 +0000467
Chris Lattner907dfe92008-11-18 07:59:24 +0000468 if (!L->getFeatures().Trigraphs) {
Chris Lattner6d27a162008-11-22 02:02:22 +0000469 if (!L->isLexingRawMode())
470 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner907dfe92008-11-18 07:59:24 +0000471 return 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000472 }
Mike Stump11289f42009-09-09 15:08:12 +0000473
Chris Lattner6d27a162008-11-22 02:02:22 +0000474 if (!L->isLexingRawMode())
475 L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
Chris Lattner22eb9722006-06-18 05:43:12 +0000476 return Res;
477}
478
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000479/// getEscapedNewLineSize - Return the size of the specified escaped newline,
480/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a
Mike Stump11289f42009-09-09 15:08:12 +0000481/// trigraph equivalent on entry to this function.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000482unsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
483 unsigned Size = 0;
484 while (isWhitespace(Ptr[Size])) {
485 ++Size;
Mike Stump11289f42009-09-09 15:08:12 +0000486
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000487 if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
488 continue;
489
490 // If this is a \r\n or \n\r, skip the other half.
491 if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
492 Ptr[Size-1] != Ptr[Size])
493 ++Size;
Mike Stump11289f42009-09-09 15:08:12 +0000494
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000495 return Size;
Mike Stump11289f42009-09-09 15:08:12 +0000496 }
497
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000498 // Not an escaped newline, must be a \t or something else.
499 return 0;
500}
501
Chris Lattner38b2cde2009-04-18 22:27:02 +0000502/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
503/// them), skip over them and return the first non-escaped-newline found,
504/// otherwise return P.
505const char *Lexer::SkipEscapedNewLines(const char *P) {
506 while (1) {
507 const char *AfterEscape;
508 if (*P == '\\') {
509 AfterEscape = P+1;
510 } else if (*P == '?') {
511 // If not a trigraph for escape, bail out.
512 if (P[1] != '?' || P[2] != '/')
513 return P;
514 AfterEscape = P+3;
515 } else {
516 return P;
517 }
Mike Stump11289f42009-09-09 15:08:12 +0000518
Chris Lattner38b2cde2009-04-18 22:27:02 +0000519 unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
520 if (NewLineSize == 0) return P;
521 P = AfterEscape+NewLineSize;
522 }
523}
524
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000525
Chris Lattner22eb9722006-06-18 05:43:12 +0000526/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
527/// get its size, and return it. This is tricky in several cases:
528/// 1. If currently at the start of a trigraph, we warn about the trigraph,
529/// then either return the trigraph (skipping 3 chars) or the '?',
530/// depending on whether trigraphs are enabled or not.
531/// 2. If this is an escaped newline (potentially with whitespace between
532/// the backslash and newline), implicitly skip the newline and return
533/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000534/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000535///
536/// This handles the slow/uncommon case of the getCharAndSize method. Here we
537/// know that we can accumulate into Size, and that we have already incremented
538/// Ptr by Size bytes.
539///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000540/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
541/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000542///
543char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattner146762e2007-07-20 16:59:19 +0000544 Token *Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000545 // If we have a slash, look for an escaped newline.
546 if (Ptr[0] == '\\') {
547 ++Size;
548 ++Ptr;
549Slash:
550 // Common case, backslash-char where the char is not whitespace.
551 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump11289f42009-09-09 15:08:12 +0000552
Chris Lattnerc1835952009-06-23 05:15:06 +0000553 // See if we have optional whitespace characters between the slash and
554 // newline.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000555 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
556 // Remember that this token needs to be cleaned.
557 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000558
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000559 // Warn if there was whitespace between the backslash and newline.
Chris Lattnerc1835952009-06-23 05:15:06 +0000560 if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000561 Diag(Ptr, diag::backslash_newline_space);
Mike Stump11289f42009-09-09 15:08:12 +0000562
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000563 // Found backslash<whitespace><newline>. Parse the char after it.
564 Size += EscapedNewLineSize;
565 Ptr += EscapedNewLineSize;
566 // Use slow version to accumulate a correct size field.
567 return getCharAndSizeSlow(Ptr, Size, Tok);
568 }
Mike Stump11289f42009-09-09 15:08:12 +0000569
Chris Lattner22eb9722006-06-18 05:43:12 +0000570 // Otherwise, this is not an escaped newline, just return the slash.
571 return '\\';
572 }
Mike Stump11289f42009-09-09 15:08:12 +0000573
Chris Lattner22eb9722006-06-18 05:43:12 +0000574 // If this is a trigraph, process it.
575 if (Ptr[0] == '?' && Ptr[1] == '?') {
576 // If this is actually a legal trigraph (not something like "??x"), emit
577 // a trigraph warning. If so, and if trigraphs are enabled, return it.
578 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
579 // Remember that this token needs to be cleaned.
Chris Lattner146762e2007-07-20 16:59:19 +0000580 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000581
582 Ptr += 3;
583 Size += 3;
584 if (C == '\\') goto Slash;
585 return C;
586 }
587 }
Mike Stump11289f42009-09-09 15:08:12 +0000588
Chris Lattner22eb9722006-06-18 05:43:12 +0000589 // If this is neither, return a single character.
590 ++Size;
591 return *Ptr;
592}
593
Chris Lattnerd01e2912006-06-18 16:22:51 +0000594
Chris Lattner22eb9722006-06-18 05:43:12 +0000595/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
596/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
597/// and that we have already incremented Ptr by Size bytes.
598///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000599/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
600/// be updated to match.
601char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000602 const LangOptions &Features) {
603 // If we have a slash, look for an escaped newline.
604 if (Ptr[0] == '\\') {
605 ++Size;
606 ++Ptr;
607Slash:
608 // Common case, backslash-char where the char is not whitespace.
609 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump11289f42009-09-09 15:08:12 +0000610
Chris Lattner22eb9722006-06-18 05:43:12 +0000611 // See if we have optional whitespace characters followed by a newline.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000612 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
613 // Found backslash<whitespace><newline>. Parse the char after it.
614 Size += EscapedNewLineSize;
615 Ptr += EscapedNewLineSize;
Mike Stump11289f42009-09-09 15:08:12 +0000616
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000617 // Use slow version to accumulate a correct size field.
618 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
619 }
Mike Stump11289f42009-09-09 15:08:12 +0000620
Chris Lattner22eb9722006-06-18 05:43:12 +0000621 // Otherwise, this is not an escaped newline, just return the slash.
622 return '\\';
623 }
Mike Stump11289f42009-09-09 15:08:12 +0000624
Chris Lattner22eb9722006-06-18 05:43:12 +0000625 // If this is a trigraph, process it.
626 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
627 // If this is actually a legal trigraph (not something like "??x"), return
628 // it.
629 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
630 Ptr += 3;
631 Size += 3;
632 if (C == '\\') goto Slash;
633 return C;
634 }
635 }
Mike Stump11289f42009-09-09 15:08:12 +0000636
Chris Lattner22eb9722006-06-18 05:43:12 +0000637 // If this is neither, return a single character.
638 ++Size;
639 return *Ptr;
640}
641
Chris Lattner22eb9722006-06-18 05:43:12 +0000642//===----------------------------------------------------------------------===//
643// Helper methods for lexing.
644//===----------------------------------------------------------------------===//
645
Chris Lattner146762e2007-07-20 16:59:19 +0000646void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000647 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
648 unsigned Size;
649 unsigned char C = *CurPtr++;
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000650 while (isIdentifierBody(C))
Chris Lattner22eb9722006-06-18 05:43:12 +0000651 C = *CurPtr++;
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000652
Chris Lattner22eb9722006-06-18 05:43:12 +0000653 --CurPtr; // Back up over the skipped character.
654
655 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
656 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000657 // FIXME: UCNs.
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000658 //
659 // TODO: Could merge these checks into a CharInfo flag to make the comparison
660 // cheaper
Chris Lattner22eb9722006-06-18 05:43:12 +0000661 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
662FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000663 const char *IdStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000664 FormTokenWithChars(Result, CurPtr, tok::identifier);
Mike Stump11289f42009-09-09 15:08:12 +0000665
Chris Lattner0f1f5052006-07-20 04:16:23 +0000666 // If we are in raw mode, return this identifier raw. There is no need to
667 // look up identifier information or attempt to macro expand it.
668 if (LexingRawMode) return;
Mike Stump11289f42009-09-09 15:08:12 +0000669
Chris Lattnercefc7682006-07-08 08:28:12 +0000670 // Fill in Result.IdentifierInfo, looking up the identifier in the
671 // identifier table.
Chris Lattner8256b972009-01-21 07:45:14 +0000672 IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
Mike Stump11289f42009-09-09 15:08:12 +0000673
Chris Lattner1f6c7fe2009-01-23 18:35:48 +0000674 // Change the kind of this identifier to the appropriate token kind, e.g.
675 // turning "for" into a keyword.
676 Result.setKind(II->getTokenID());
Mike Stump11289f42009-09-09 15:08:12 +0000677
Chris Lattnerc5a00062006-06-18 16:41:01 +0000678 // Finally, now that we know we have an identifier, pass this off to the
679 // preprocessor, which may macro expand it or something.
Chris Lattner8256b972009-01-21 07:45:14 +0000680 if (II->isHandleIdentifierCase())
Chris Lattnerad89ec02009-01-21 07:43:11 +0000681 PP->HandleIdentifier(Result);
682 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000683 }
Mike Stump11289f42009-09-09 15:08:12 +0000684
Chris Lattner22eb9722006-06-18 05:43:12 +0000685 // Otherwise, $,\,? in identifier found. Enter slower path.
Mike Stump11289f42009-09-09 15:08:12 +0000686
Chris Lattner22eb9722006-06-18 05:43:12 +0000687 C = getCharAndSize(CurPtr, Size);
688 while (1) {
689 if (C == '$') {
690 // If we hit a $ and they are not supported in identifiers, we are done.
691 if (!Features.DollarIdents) goto FinishIdentifier;
Mike Stump11289f42009-09-09 15:08:12 +0000692
Chris Lattner22eb9722006-06-18 05:43:12 +0000693 // Otherwise, emit a diagnostic and continue.
Chris Lattner6d27a162008-11-22 02:02:22 +0000694 if (!isLexingRawMode())
695 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000696 CurPtr = ConsumeChar(CurPtr, Size, Result);
697 C = getCharAndSize(CurPtr, Size);
698 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000699 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000700 // Found end of identifier.
701 goto FinishIdentifier;
702 }
703
704 // Otherwise, this character is good, consume it.
705 CurPtr = ConsumeChar(CurPtr, Size, Result);
706
707 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000708 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000709 CurPtr = ConsumeChar(CurPtr, Size, Result);
710 C = getCharAndSize(CurPtr, Size);
711 }
712 }
713}
714
715
Nate Begeman5eee9332008-04-14 02:26:39 +0000716/// LexNumericConstant - Lex the remainder of a integer or floating point
Chris Lattner22eb9722006-06-18 05:43:12 +0000717/// constant. From[-1] is the first character lexed. Return the end of the
718/// constant.
Chris Lattner146762e2007-07-20 16:59:19 +0000719void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000720 unsigned Size;
721 char C = getCharAndSize(CurPtr, Size);
722 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000723 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000724 CurPtr = ConsumeChar(CurPtr, Size, Result);
725 PrevCh = C;
726 C = getCharAndSize(CurPtr, Size);
727 }
Mike Stump11289f42009-09-09 15:08:12 +0000728
Chris Lattner22eb9722006-06-18 05:43:12 +0000729 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
730 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
731 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
732
733 // If we have a hex FP constant, continue.
Alexis Hunt91b78382010-01-10 23:37:56 +0000734 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
735 (!PP || !PP->getLangOptions().CPlusPlus0x))
Chris Lattner22eb9722006-06-18 05:43:12 +0000736 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
Mike Stump11289f42009-09-09 15:08:12 +0000737
Chris Lattnerd01e2912006-06-18 16:22:51 +0000738 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000739 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000740 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000741 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000742}
743
744/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
745/// either " or L".
Chris Lattner4d963442008-10-12 04:05:48 +0000746void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000747 const char *NulCharacter = 0; // Does this string contain the \0 character?
Mike Stump11289f42009-09-09 15:08:12 +0000748
Chris Lattner22eb9722006-06-18 05:43:12 +0000749 char C = getAndAdvanceChar(CurPtr, Result);
750 while (C != '"') {
751 // Skip escaped characters.
752 if (C == '\\') {
753 // Skip the escaped character.
754 C = getAndAdvanceChar(CurPtr, Result);
755 } else if (C == '\n' || C == '\r' || // Newline.
756 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerd14705b2009-03-18 21:10:12 +0000757 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +0000758 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattnerb11c3232008-10-12 04:51:35 +0000759 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000760 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000761 } else if (C == 0) {
762 NulCharacter = CurPtr-1;
763 }
764 C = getAndAdvanceChar(CurPtr, Result);
765 }
Mike Stump11289f42009-09-09 15:08:12 +0000766
Chris Lattner5a78a022006-07-20 06:02:19 +0000767 // If a nul character existed in the string, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +0000768 if (NulCharacter && !isLexingRawMode())
769 Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000770
Chris Lattnerd01e2912006-06-18 16:22:51 +0000771 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000772 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000773 FormTokenWithChars(Result, CurPtr,
774 Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000775 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000776}
777
778/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
779/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattner146762e2007-07-20 16:59:19 +0000780void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000781 const char *NulCharacter = 0; // Does this string contain the \0 character?
Chris Lattnerb40289b2009-04-17 23:56:52 +0000782 const char *AfterLessPos = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000783 char C = getAndAdvanceChar(CurPtr, Result);
784 while (C != '>') {
785 // Skip escaped characters.
786 if (C == '\\') {
787 // Skip the escaped character.
788 C = getAndAdvanceChar(CurPtr, Result);
789 } else if (C == '\n' || C == '\r' || // Newline.
790 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerb40289b2009-04-17 23:56:52 +0000791 // If the filename is unterminated, then it must just be a lone <
792 // character. Return this as such.
793 FormTokenWithChars(Result, AfterLessPos, tok::less);
Chris Lattner5a78a022006-07-20 06:02:19 +0000794 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000795 } else if (C == 0) {
796 NulCharacter = CurPtr-1;
797 }
798 C = getAndAdvanceChar(CurPtr, Result);
799 }
Mike Stump11289f42009-09-09 15:08:12 +0000800
Chris Lattner5a78a022006-07-20 06:02:19 +0000801 // If a nul character existed in the string, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +0000802 if (NulCharacter && !isLexingRawMode())
803 Diag(NulCharacter, diag::null_in_string);
Mike Stump11289f42009-09-09 15:08:12 +0000804
Chris Lattnerd01e2912006-06-18 16:22:51 +0000805 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000806 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000807 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000808 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000809}
810
811
812/// LexCharConstant - Lex the remainder of a character constant, after having
813/// lexed either ' or L'.
Chris Lattner146762e2007-07-20 16:59:19 +0000814void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000815 const char *NulCharacter = 0; // Does this character contain the \0 character?
816
817 // Handle the common case of 'x' and '\y' efficiently.
818 char C = getAndAdvanceChar(CurPtr, Result);
819 if (C == '\'') {
Chris Lattnerd14705b2009-03-18 21:10:12 +0000820 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +0000821 Diag(BufferPtr, diag::err_empty_character);
Chris Lattnerb11c3232008-10-12 04:51:35 +0000822 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000823 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000824 } else if (C == '\\') {
825 // Skip the escaped character.
826 // FIXME: UCN's.
827 C = getAndAdvanceChar(CurPtr, Result);
828 }
Mike Stump11289f42009-09-09 15:08:12 +0000829
Chris Lattner22eb9722006-06-18 05:43:12 +0000830 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
831 ++CurPtr;
832 } else {
833 // Fall back on generic code for embedded nulls, newlines, wide chars.
834 do {
835 // Skip escaped characters.
836 if (C == '\\') {
837 // Skip the escaped character.
838 C = getAndAdvanceChar(CurPtr, Result);
839 } else if (C == '\n' || C == '\r' || // Newline.
840 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerd14705b2009-03-18 21:10:12 +0000841 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +0000842 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattnerb11c3232008-10-12 04:51:35 +0000843 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000844 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000845 } else if (C == 0) {
846 NulCharacter = CurPtr-1;
847 }
848 C = getAndAdvanceChar(CurPtr, Result);
849 } while (C != '\'');
850 }
Mike Stump11289f42009-09-09 15:08:12 +0000851
Chris Lattner6d27a162008-11-22 02:02:22 +0000852 if (NulCharacter && !isLexingRawMode())
853 Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000854
Chris Lattnerd01e2912006-06-18 16:22:51 +0000855 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000856 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000857 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000858 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000859}
860
861/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
862/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattner4d963442008-10-12 04:05:48 +0000863///
864/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
865///
866bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000867 // Whitespace - Skip it, then return the token after the whitespace.
868 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
869 while (1) {
870 // Skip horizontal whitespace very aggressively.
871 while (isHorizontalWhitespace(Char))
872 Char = *++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000873
Daniel Dunbar5c4cc092008-11-25 00:20:22 +0000874 // Otherwise if we have something other than whitespace, we're done.
Chris Lattner22eb9722006-06-18 05:43:12 +0000875 if (Char != '\n' && Char != '\r')
876 break;
Mike Stump11289f42009-09-09 15:08:12 +0000877
Chris Lattner22eb9722006-06-18 05:43:12 +0000878 if (ParsingPreprocessorDirective) {
879 // End of preprocessor directive line, let LexTokenInternal handle this.
880 BufferPtr = CurPtr;
Chris Lattner4d963442008-10-12 04:05:48 +0000881 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000882 }
Mike Stump11289f42009-09-09 15:08:12 +0000883
Chris Lattner22eb9722006-06-18 05:43:12 +0000884 // ok, but handle newline.
885 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +0000886 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000887 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +0000888 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000889 Char = *++CurPtr;
890 }
891
892 // If this isn't immediately after a newline, there is leading space.
893 char PrevChar = CurPtr[-1];
894 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattner146762e2007-07-20 16:59:19 +0000895 Result.setFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000896
Chris Lattner4d963442008-10-12 04:05:48 +0000897 // If the client wants us to return whitespace, return it now.
898 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +0000899 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4d963442008-10-12 04:05:48 +0000900 return true;
901 }
Mike Stump11289f42009-09-09 15:08:12 +0000902
Chris Lattner22eb9722006-06-18 05:43:12 +0000903 BufferPtr = CurPtr;
Chris Lattner4d963442008-10-12 04:05:48 +0000904 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000905}
906
907// SkipBCPLComment - We have just read the // characters from input. Skip until
908// we find the newline character thats terminate the comment. Then update
Chris Lattner87d02082010-01-18 22:35:47 +0000909/// BufferPtr and return.
910///
911/// If we're in KeepCommentMode or any CommentHandler has inserted
912/// some tokens, this will store the first token and return true.
Chris Lattner146762e2007-07-20 16:59:19 +0000913bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000914 // If BCPL comments aren't explicitly enabled for this language, emit an
915 // extension warning.
Chris Lattner6d27a162008-11-22 02:02:22 +0000916 if (!Features.BCPLComment && !isLexingRawMode()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000917 Diag(BufferPtr, diag::ext_bcpl_comment);
Mike Stump11289f42009-09-09 15:08:12 +0000918
Chris Lattner22eb9722006-06-18 05:43:12 +0000919 // Mark them enabled so we only emit one warning for this translation
920 // unit.
921 Features.BCPLComment = true;
922 }
Mike Stump11289f42009-09-09 15:08:12 +0000923
Chris Lattner22eb9722006-06-18 05:43:12 +0000924 // Scan over the body of the comment. The common case, when scanning, is that
925 // the comment contains normal ascii characters with nothing interesting in
926 // them. As such, optimize for this case with the inner loop.
927 char C;
928 do {
929 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +0000930 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
931 // If we find a \n character, scan backwards, checking to see if it's an
932 // escaped newline, like we do for block comments.
Mike Stump11289f42009-09-09 15:08:12 +0000933
Chris Lattner22eb9722006-06-18 05:43:12 +0000934 // Skip over characters in the fast loop.
935 while (C != 0 && // Potentially EOF.
936 C != '\\' && // Potentially escaped newline.
937 C != '?' && // Potentially trigraph.
938 C != '\n' && C != '\r') // Newline or DOS-style newline.
939 C = *++CurPtr;
940
941 // If this is a newline, we're done.
942 if (C == '\n' || C == '\r')
943 break; // Found the newline? Break out!
Mike Stump11289f42009-09-09 15:08:12 +0000944
Chris Lattner22eb9722006-06-18 05:43:12 +0000945 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnere141a9e2008-12-12 07:34:39 +0000946 // properly decode the character. Read it in raw mode to avoid emitting
947 // diagnostics about things like trigraphs. If we see an escaped newline,
948 // we'll handle it below.
Chris Lattner22eb9722006-06-18 05:43:12 +0000949 const char *OldPtr = CurPtr;
Chris Lattnere141a9e2008-12-12 07:34:39 +0000950 bool OldRawMode = isLexingRawMode();
951 LexingRawMode = true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000952 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnere141a9e2008-12-12 07:34:39 +0000953 LexingRawMode = OldRawMode;
Chris Lattnerecdaf402009-04-05 00:26:41 +0000954
955 // If the char that we finally got was a \n, then we must have had something
956 // like \<newline><newline>. We don't want to have consumed the second
957 // newline, we want CurPtr, to end up pointing to it down below.
958 if (C == '\n' || C == '\r') {
959 --CurPtr;
960 C = 'x'; // doesn't matter what this is.
961 }
Mike Stump11289f42009-09-09 15:08:12 +0000962
Chris Lattner22eb9722006-06-18 05:43:12 +0000963 // If we read multiple characters, and one of those characters was a \r or
Chris Lattnerff591e22007-06-09 06:07:22 +0000964 // \n, then we had an escaped newline within the comment. Emit diagnostic
965 // unless the next line is also a // comment.
966 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000967 for (; OldPtr != CurPtr; ++OldPtr)
968 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnerff591e22007-06-09 06:07:22 +0000969 // Okay, we found a // comment that ends in a newline, if the next
970 // line is also a // comment, but has spaces, don't emit a diagnostic.
971 if (isspace(C)) {
972 const char *ForwardPtr = CurPtr;
973 while (isspace(*ForwardPtr)) // Skip whitespace.
974 ++ForwardPtr;
975 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
976 break;
977 }
Mike Stump11289f42009-09-09 15:08:12 +0000978
Chris Lattner6d27a162008-11-22 02:02:22 +0000979 if (!isLexingRawMode())
980 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000981 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000982 }
983 }
Mike Stump11289f42009-09-09 15:08:12 +0000984
Chris Lattner457fc152006-07-29 06:30:25 +0000985 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
Chris Lattner22eb9722006-06-18 05:43:12 +0000986 } while (C != '\n' && C != '\r');
987
Chris Lattner93ddf802010-02-03 21:06:21 +0000988 // Found but did not consume the newline. Notify comment handlers about the
989 // comment unless we're in a #if 0 block.
990 if (PP && !isLexingRawMode() &&
991 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
992 getSourceLocation(CurPtr)))) {
Chris Lattner87d02082010-01-18 22:35:47 +0000993 BufferPtr = CurPtr;
994 return true; // A token has to be returned.
995 }
Mike Stump11289f42009-09-09 15:08:12 +0000996
Chris Lattner457fc152006-07-29 06:30:25 +0000997 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +0000998 if (inKeepCommentMode())
Chris Lattner457fc152006-07-29 06:30:25 +0000999 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001000
1001 // If we are inside a preprocessor directive and we see the end of line,
1002 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +00001003 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001004 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001005 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001006 }
Mike Stump11289f42009-09-09 15:08:12 +00001007
Chris Lattner22eb9722006-06-18 05:43:12 +00001008 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner87e97ea2008-10-12 00:23:07 +00001009 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattner4d963442008-10-12 04:05:48 +00001010 // contribute to another token), it isn't needed for correctness. Note that
1011 // this is ok even in KeepWhitespaceMode, because we would have returned the
1012 /// comment above in that mode.
Chris Lattner22eb9722006-06-18 05:43:12 +00001013 ++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001014
Chris Lattner22eb9722006-06-18 05:43:12 +00001015 // The next returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001016 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001017 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001018 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001019 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001020 return false;
Chris Lattner457fc152006-07-29 06:30:25 +00001021}
Chris Lattner22eb9722006-06-18 05:43:12 +00001022
Chris Lattner457fc152006-07-29 06:30:25 +00001023/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
1024/// an appropriate way and return it.
Chris Lattner146762e2007-07-20 16:59:19 +00001025bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001026 // If we're not in a preprocessor directive, just return the // comment
1027 // directly.
1028 FormTokenWithChars(Result, CurPtr, tok::comment);
Mike Stump11289f42009-09-09 15:08:12 +00001029
Chris Lattnerb11c3232008-10-12 04:51:35 +00001030 if (!ParsingPreprocessorDirective)
1031 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001032
Chris Lattnerb11c3232008-10-12 04:51:35 +00001033 // If this BCPL-style comment is in a macro definition, transmogrify it into
1034 // a C-style block comment.
1035 std::string Spelling = PP->getSpelling(Result);
1036 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
1037 Spelling[1] = '*'; // Change prefix to "/*".
1038 Spelling += "*/"; // add suffix.
Mike Stump11289f42009-09-09 15:08:12 +00001039
Chris Lattnerb11c3232008-10-12 04:51:35 +00001040 Result.setKind(tok::comment);
Chris Lattner5a7971e2009-01-26 19:29:26 +00001041 PP->CreateString(&Spelling[0], Spelling.size(), Result,
1042 Result.getLocation());
Chris Lattnere01e7582008-10-12 04:15:42 +00001043 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001044}
1045
Chris Lattnercb283342006-06-18 06:48:37 +00001046/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
1047/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner89770572008-12-12 07:14:34 +00001048/// diagnostic if so. We know that the newline is inside of a block comment.
Mike Stump11289f42009-09-09 15:08:12 +00001049static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
Chris Lattner1f583052006-06-18 06:53:56 +00001050 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001051 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Mike Stump11289f42009-09-09 15:08:12 +00001052
Chris Lattner22eb9722006-06-18 05:43:12 +00001053 // Back up off the newline.
1054 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001055
Chris Lattner22eb9722006-06-18 05:43:12 +00001056 // If this is a two-character newline sequence, skip the other character.
1057 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
1058 // \n\n or \r\r -> not escaped newline.
1059 if (CurPtr[0] == CurPtr[1])
1060 return false;
1061 // \n\r or \r\n -> skip the newline.
1062 --CurPtr;
1063 }
Mike Stump11289f42009-09-09 15:08:12 +00001064
Chris Lattner22eb9722006-06-18 05:43:12 +00001065 // If we have horizontal whitespace, skip over it. We allow whitespace
1066 // between the slash and newline.
1067 bool HasSpace = false;
1068 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
1069 --CurPtr;
1070 HasSpace = true;
1071 }
Mike Stump11289f42009-09-09 15:08:12 +00001072
Chris Lattner22eb9722006-06-18 05:43:12 +00001073 // If we have a slash, we know this is an escaped newline.
1074 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +00001075 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001076 } else {
1077 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +00001078 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
1079 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +00001080 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001081
Chris Lattnercb283342006-06-18 06:48:37 +00001082 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +00001083 CurPtr -= 2;
1084
1085 // If no trigraphs are enabled, warn that we ignored this trigraph and
1086 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +00001087 if (!L->getFeatures().Trigraphs) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001088 if (!L->isLexingRawMode())
1089 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +00001090 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001091 }
Chris Lattner6d27a162008-11-22 02:02:22 +00001092 if (!L->isLexingRawMode())
1093 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001094 }
Mike Stump11289f42009-09-09 15:08:12 +00001095
Chris Lattner22eb9722006-06-18 05:43:12 +00001096 // Warn about having an escaped newline between the */ characters.
Chris Lattner6d27a162008-11-22 02:02:22 +00001097 if (!L->isLexingRawMode())
1098 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Mike Stump11289f42009-09-09 15:08:12 +00001099
Chris Lattner22eb9722006-06-18 05:43:12 +00001100 // If there was space between the backslash and newline, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +00001101 if (HasSpace && !L->isLexingRawMode())
1102 L->Diag(CurPtr, diag::backslash_newline_space);
Mike Stump11289f42009-09-09 15:08:12 +00001103
Chris Lattnercb283342006-06-18 06:48:37 +00001104 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001105}
1106
Chris Lattneraded4a92006-10-27 04:42:31 +00001107#ifdef __SSE2__
1108#include <emmintrin.h>
Chris Lattner9f6604f2006-10-30 20:01:22 +00001109#elif __ALTIVEC__
1110#include <altivec.h>
1111#undef bool
Chris Lattneraded4a92006-10-27 04:42:31 +00001112#endif
1113
Chris Lattner22eb9722006-06-18 05:43:12 +00001114/// SkipBlockComment - We have just read the /* characters from input. Read
1115/// until we find the */ characters that terminate the comment. Note that we
1116/// don't bother decoding trigraphs or escaped newlines in block comments,
1117/// because they cannot cause the comment to end. The only thing that can
1118/// happen is the comment could end with an escaped newline between the */ end
1119/// of comment.
Chris Lattnere01e7582008-10-12 04:15:42 +00001120///
Chris Lattner87d02082010-01-18 22:35:47 +00001121/// If we're in KeepCommentMode or any CommentHandler has inserted
1122/// some tokens, this will store the first token and return true.
Chris Lattner146762e2007-07-20 16:59:19 +00001123bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001124 // Scan one character past where we should, looking for a '/' character. Once
1125 // we find it, check to see if it was preceeded by a *. This common
1126 // optimization helps people who like to put a lot of * characters in their
1127 // comments.
Chris Lattnerc850ad62007-07-21 23:43:37 +00001128
1129 // The first character we get with newlines and trigraphs skipped to handle
1130 // the degenerate /*/ case below correctly if the * has an escaped newline
1131 // after it.
1132 unsigned CharSize;
1133 unsigned char C = getCharAndSize(CurPtr, CharSize);
1134 CurPtr += CharSize;
Chris Lattner22eb9722006-06-18 05:43:12 +00001135 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001136 if (!isLexingRawMode())
Chris Lattner7c2e9802008-10-12 01:31:51 +00001137 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner99e7d232008-10-12 04:19:49 +00001138 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001139
Chris Lattner99e7d232008-10-12 04:19:49 +00001140 // KeepWhitespaceMode should return this broken comment as a token. Since
1141 // it isn't a well formed comment, just return it as an 'unknown' token.
1142 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001143 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner99e7d232008-10-12 04:19:49 +00001144 return true;
1145 }
Mike Stump11289f42009-09-09 15:08:12 +00001146
Chris Lattner99e7d232008-10-12 04:19:49 +00001147 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001148 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001149 }
Mike Stump11289f42009-09-09 15:08:12 +00001150
Chris Lattnerc850ad62007-07-21 23:43:37 +00001151 // Check to see if the first character after the '/*' is another /. If so,
1152 // then this slash does not end the block comment, it is part of it.
1153 if (C == '/')
1154 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001155
Chris Lattner22eb9722006-06-18 05:43:12 +00001156 while (1) {
Chris Lattner6cc3e362006-10-27 04:12:35 +00001157 // Skip over all non-interesting characters until we find end of buffer or a
1158 // (probably ending) '/' character.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001159 if (CurPtr + 24 < BufferEnd) {
1160 // While not aligned to a 16-byte boundary.
1161 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1162 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001163
Chris Lattner6cc3e362006-10-27 04:12:35 +00001164 if (C == '/') goto FoundSlash;
Chris Lattneraded4a92006-10-27 04:42:31 +00001165
1166#ifdef __SSE2__
1167 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1168 '/', '/', '/', '/', '/', '/', '/', '/');
1169 while (CurPtr+16 <= BufferEnd &&
1170 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1171 CurPtr += 16;
Chris Lattner9f6604f2006-10-30 20:01:22 +00001172#elif __ALTIVEC__
1173 __vector unsigned char Slashes = {
Mike Stump11289f42009-09-09 15:08:12 +00001174 '/', '/', '/', '/', '/', '/', '/', '/',
Chris Lattner9f6604f2006-10-30 20:01:22 +00001175 '/', '/', '/', '/', '/', '/', '/', '/'
1176 };
1177 while (CurPtr+16 <= BufferEnd &&
1178 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1179 CurPtr += 16;
Mike Stump11289f42009-09-09 15:08:12 +00001180#else
Chris Lattneraded4a92006-10-27 04:42:31 +00001181 // Scan for '/' quickly. Many block comments are very large.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001182 while (CurPtr[0] != '/' &&
1183 CurPtr[1] != '/' &&
1184 CurPtr[2] != '/' &&
1185 CurPtr[3] != '/' &&
1186 CurPtr+4 < BufferEnd) {
1187 CurPtr += 4;
1188 }
Chris Lattneraded4a92006-10-27 04:42:31 +00001189#endif
Mike Stump11289f42009-09-09 15:08:12 +00001190
Chris Lattneraded4a92006-10-27 04:42:31 +00001191 // It has to be one of the bytes scanned, increment to it and read one.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001192 C = *CurPtr++;
1193 }
Mike Stump11289f42009-09-09 15:08:12 +00001194
Chris Lattneraded4a92006-10-27 04:42:31 +00001195 // Loop to scan the remainder.
Chris Lattner22eb9722006-06-18 05:43:12 +00001196 while (C != '/' && C != '\0')
1197 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001198
Chris Lattner6cc3e362006-10-27 04:12:35 +00001199 FoundSlash:
Chris Lattner22eb9722006-06-18 05:43:12 +00001200 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001201 if (CurPtr[-2] == '*') // We found the final */. We're done!
1202 break;
Mike Stump11289f42009-09-09 15:08:12 +00001203
Chris Lattner22eb9722006-06-18 05:43:12 +00001204 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +00001205 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001206 // We found the final */, though it had an escaped newline between the
1207 // * and /. We're done!
1208 break;
1209 }
1210 }
1211 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1212 // If this is a /* inside of the comment, emit a warning. Don't do this
1213 // if this is a /*/, which will end the comment. This misses cases with
1214 // embedded escaped newlines, but oh well.
Chris Lattner6d27a162008-11-22 02:02:22 +00001215 if (!isLexingRawMode())
1216 Diag(CurPtr-1, diag::warn_nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001217 }
1218 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001219 if (!isLexingRawMode())
1220 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001221 // Note: the user probably forgot a */. We could continue immediately
1222 // after the /*, but this would involve lexing a lot of what really is the
1223 // comment, which surely would confuse the parser.
Chris Lattner99e7d232008-10-12 04:19:49 +00001224 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001225
Chris Lattner99e7d232008-10-12 04:19:49 +00001226 // KeepWhitespaceMode should return this broken comment as a token. Since
1227 // it isn't a well formed comment, just return it as an 'unknown' token.
1228 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001229 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner99e7d232008-10-12 04:19:49 +00001230 return true;
1231 }
Mike Stump11289f42009-09-09 15:08:12 +00001232
Chris Lattner99e7d232008-10-12 04:19:49 +00001233 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001234 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001235 }
1236 C = *CurPtr++;
1237 }
Mike Stump11289f42009-09-09 15:08:12 +00001238
Chris Lattner93ddf802010-02-03 21:06:21 +00001239 // Notify comment handlers about the comment unless we're in a #if 0 block.
1240 if (PP && !isLexingRawMode() &&
1241 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
1242 getSourceLocation(CurPtr)))) {
Chris Lattner87d02082010-01-18 22:35:47 +00001243 BufferPtr = CurPtr;
1244 return true; // A token has to be returned.
1245 }
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001246
Chris Lattner457fc152006-07-29 06:30:25 +00001247 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +00001248 if (inKeepCommentMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001249 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattnere01e7582008-10-12 04:15:42 +00001250 return true;
Chris Lattner457fc152006-07-29 06:30:25 +00001251 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001252
1253 // It is common for the tokens immediately after a /**/ comment to be
1254 // whitespace. Instead of going through the big switch, handle it
Chris Lattner4d963442008-10-12 04:05:48 +00001255 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1256 // have already returned above with the comment as a token.
Chris Lattner22eb9722006-06-18 05:43:12 +00001257 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner146762e2007-07-20 16:59:19 +00001258 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +00001259 SkipWhitespace(Result, CurPtr+1);
Chris Lattnere01e7582008-10-12 04:15:42 +00001260 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001261 }
1262
1263 // Otherwise, just return so that the next character will be lexed as a token.
1264 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001265 Result.setFlag(Token::LeadingSpace);
Chris Lattnere01e7582008-10-12 04:15:42 +00001266 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001267}
1268
1269//===----------------------------------------------------------------------===//
1270// Primary Lexing Entry Points
1271//===----------------------------------------------------------------------===//
1272
Chris Lattner22eb9722006-06-18 05:43:12 +00001273/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1274/// uninterpreted string. This switches the lexer out of directive mode.
1275std::string Lexer::ReadToEndOfLine() {
1276 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1277 "Must be in a preprocessing directive!");
1278 std::string Result;
Chris Lattner146762e2007-07-20 16:59:19 +00001279 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001280
1281 // CurPtr - Cache BufferPtr in an automatic variable.
1282 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001283 while (1) {
1284 char Char = getAndAdvanceChar(CurPtr, Tmp);
1285 switch (Char) {
1286 default:
1287 Result += Char;
1288 break;
1289 case 0: // Null.
1290 // Found end of file?
1291 if (CurPtr-1 != BufferEnd) {
1292 // Nope, normal character, continue.
1293 Result += Char;
1294 break;
1295 }
1296 // FALL THROUGH.
1297 case '\r':
1298 case '\n':
1299 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1300 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1301 BufferPtr = CurPtr-1;
Mike Stump11289f42009-09-09 15:08:12 +00001302
Chris Lattner22eb9722006-06-18 05:43:12 +00001303 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +00001304 Lex(Tmp);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001305 assert(Tmp.is(tok::eom) && "Unexpected token!");
Mike Stump11289f42009-09-09 15:08:12 +00001306
Chris Lattner22eb9722006-06-18 05:43:12 +00001307 // Finally, we're done, return the string we found.
1308 return Result;
1309 }
1310 }
1311}
1312
1313/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1314/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +00001315/// This returns true if Result contains a token, false if PP.Lex should be
1316/// called again.
Chris Lattner146762e2007-07-20 16:59:19 +00001317bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001318 // If we hit the end of the file while parsing a preprocessor directive,
1319 // end the preprocessor directive first. The next token returned will
1320 // then be the end of file.
1321 if (ParsingPreprocessorDirective) {
1322 // Done parsing the "line".
1323 ParsingPreprocessorDirective = false;
Chris Lattnerd01e2912006-06-18 16:22:51 +00001324 // Update the location of token as well as BufferPtr.
Chris Lattnerb11c3232008-10-12 04:51:35 +00001325 FormTokenWithChars(Result, CurPtr, tok::eom);
Mike Stump11289f42009-09-09 15:08:12 +00001326
Chris Lattner457fc152006-07-29 06:30:25 +00001327 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001328 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner2183a6e2006-07-18 06:36:12 +00001329 return true; // Have a token.
Mike Stump11289f42009-09-09 15:08:12 +00001330 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00001331
Chris Lattner30a2fa12006-07-19 06:31:49 +00001332 // If we are in raw mode, return this event as an EOF token. Let the caller
1333 // that put us in raw mode handle the event.
Chris Lattner6d27a162008-11-22 02:02:22 +00001334 if (isLexingRawMode()) {
Chris Lattner8c204872006-10-14 05:19:21 +00001335 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +00001336 BufferPtr = BufferEnd;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001337 FormTokenWithChars(Result, BufferEnd, tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001338 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001339 }
Mike Stump11289f42009-09-09 15:08:12 +00001340
Douglas Gregor3545ff42009-09-21 16:56:56 +00001341 // Otherwise, check if we are code-completing, then issue diagnostics for
1342 // unterminated #if and missing newline.
Chris Lattner30a2fa12006-07-19 06:31:49 +00001343
Douglas Gregor53ad6b92009-12-02 06:49:09 +00001344 if (PP && PP->isCodeCompletionFile(FileLoc)) {
1345 // We're at the end of the file, but we've been asked to consider the
1346 // end of the file to be a code-completion token. Return the
1347 // code-completion token.
1348 Result.startToken();
1349 FormTokenWithChars(Result, CurPtr, tok::code_completion);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001350
Douglas Gregor53ad6b92009-12-02 06:49:09 +00001351 // Only do the eof -> code_completion translation once.
1352 PP->SetCodeCompletionPoint(0, 0, 0);
1353 return true;
Douglas Gregor3545ff42009-09-21 16:56:56 +00001354 }
1355
Chris Lattner30a2fa12006-07-19 06:31:49 +00001356 // If we are in a #if directive, emit an error.
1357 while (!ConditionalStack.empty()) {
Chris Lattner014156e2008-11-22 06:22:39 +00001358 PP->Diag(ConditionalStack.back().IfLoc,
1359 diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001360 ConditionalStack.pop_back();
1361 }
Mike Stump11289f42009-09-09 15:08:12 +00001362
Chris Lattner8f96d042008-04-12 05:54:25 +00001363 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1364 // a pedwarn.
1365 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Mike Stump0be88752009-04-02 02:29:42 +00001366 Diag(BufferEnd, diag::ext_no_newline_eof)
1367 << CodeModificationHint::CreateInsertion(getSourceLocation(BufferEnd),
1368 "\n");
Mike Stump11289f42009-09-09 15:08:12 +00001369
Chris Lattner22eb9722006-06-18 05:43:12 +00001370 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +00001371
1372 // Finally, let the preprocessor handle this.
Chris Lattner02b436a2007-10-17 20:41:00 +00001373 return PP->HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001374}
1375
Chris Lattner678c8802006-07-11 05:46:12 +00001376/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1377/// the specified lexer will return a tok::l_paren token, 0 if it is something
1378/// else and 2 if there are no more tokens in the buffer controlled by the
1379/// lexer.
1380unsigned Lexer::isNextPPTokenLParen() {
1381 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
Mike Stump11289f42009-09-09 15:08:12 +00001382
Chris Lattner678c8802006-07-11 05:46:12 +00001383 // Switch to 'skipping' mode. This will ensure that we can lex a token
1384 // without emitting diagnostics, disables macro expansion, and will cause EOF
1385 // to return an EOF token instead of popping the include stack.
1386 LexingRawMode = true;
Mike Stump11289f42009-09-09 15:08:12 +00001387
Chris Lattner678c8802006-07-11 05:46:12 +00001388 // Save state that can be changed while lexing so that we can restore it.
1389 const char *TmpBufferPtr = BufferPtr;
Chris Lattner40493eb2009-04-24 07:15:46 +00001390 bool inPPDirectiveMode = ParsingPreprocessorDirective;
Mike Stump11289f42009-09-09 15:08:12 +00001391
Chris Lattner146762e2007-07-20 16:59:19 +00001392 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001393 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001394 LexTokenInternal(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001395
Chris Lattner678c8802006-07-11 05:46:12 +00001396 // Restore state that may have changed.
1397 BufferPtr = TmpBufferPtr;
Chris Lattner40493eb2009-04-24 07:15:46 +00001398 ParsingPreprocessorDirective = inPPDirectiveMode;
Mike Stump11289f42009-09-09 15:08:12 +00001399
Chris Lattner678c8802006-07-11 05:46:12 +00001400 // Restore the lexer back to non-skipping mode.
1401 LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +00001402
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001403 if (Tok.is(tok::eof))
Chris Lattner678c8802006-07-11 05:46:12 +00001404 return 2;
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001405 return Tok.is(tok::l_paren);
Chris Lattner678c8802006-07-11 05:46:12 +00001406}
1407
Chris Lattner7c027ee2009-12-14 06:16:57 +00001408/// FindConflictEnd - Find the end of a version control conflict marker.
1409static const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd) {
1410 llvm::StringRef RestOfBuffer(CurPtr+7, BufferEnd-CurPtr-7);
1411 size_t Pos = RestOfBuffer.find(">>>>>>>");
1412 while (Pos != llvm::StringRef::npos) {
1413 // Must occur at start of line.
1414 if (RestOfBuffer[Pos-1] != '\r' &&
1415 RestOfBuffer[Pos-1] != '\n') {
1416 RestOfBuffer = RestOfBuffer.substr(Pos+7);
1417 continue;
1418 }
1419 return RestOfBuffer.data()+Pos;
1420 }
1421 return 0;
1422}
1423
1424/// IsStartOfConflictMarker - If the specified pointer is the start of a version
1425/// control conflict marker like '<<<<<<<', recognize it as such, emit an error
1426/// and recover nicely. This returns true if it is a conflict marker and false
1427/// if not.
1428bool Lexer::IsStartOfConflictMarker(const char *CurPtr) {
1429 // Only a conflict marker if it starts at the beginning of a line.
1430 if (CurPtr != BufferStart &&
1431 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1432 return false;
1433
1434 // Check to see if we have <<<<<<<.
1435 if (BufferEnd-CurPtr < 8 ||
1436 llvm::StringRef(CurPtr, 7) != "<<<<<<<")
1437 return false;
1438
1439 // If we have a situation where we don't care about conflict markers, ignore
1440 // it.
1441 if (IsInConflictMarker || isLexingRawMode())
1442 return false;
1443
1444 // Check to see if there is a >>>>>>> somewhere in the buffer at the start of
1445 // a line to terminate this conflict marker.
1446 if (FindConflictEnd(CurPtr+7, BufferEnd)) {
1447 // We found a match. We are really in a conflict marker.
1448 // Diagnose this, and ignore to the end of line.
1449 Diag(CurPtr, diag::err_conflict_marker);
1450 IsInConflictMarker = true;
1451
1452 // Skip ahead to the end of line. We know this exists because the
1453 // end-of-conflict marker starts with \r or \n.
1454 while (*CurPtr != '\r' && *CurPtr != '\n') {
1455 assert(CurPtr != BufferEnd && "Didn't find end of line");
1456 ++CurPtr;
1457 }
1458 BufferPtr = CurPtr;
1459 return true;
1460 }
1461
1462 // No end of conflict marker found.
1463 return false;
1464}
1465
1466
1467/// HandleEndOfConflictMarker - If this is a '=======' or '|||||||' or '>>>>>>>'
1468/// marker, then it is the end of a conflict marker. Handle it by ignoring up
1469/// until the end of the line. This returns true if it is a conflict marker and
1470/// false if not.
1471bool Lexer::HandleEndOfConflictMarker(const char *CurPtr) {
1472 // Only a conflict marker if it starts at the beginning of a line.
1473 if (CurPtr != BufferStart &&
1474 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1475 return false;
1476
1477 // If we have a situation where we don't care about conflict markers, ignore
1478 // it.
1479 if (!IsInConflictMarker || isLexingRawMode())
1480 return false;
1481
1482 // Check to see if we have the marker (7 characters in a row).
1483 for (unsigned i = 1; i != 7; ++i)
1484 if (CurPtr[i] != CurPtr[0])
1485 return false;
1486
1487 // If we do have it, search for the end of the conflict marker. This could
1488 // fail if it got skipped with a '#if 0' or something. Note that CurPtr might
1489 // be the end of conflict marker.
1490 if (const char *End = FindConflictEnd(CurPtr, BufferEnd)) {
1491 CurPtr = End;
1492
1493 // Skip ahead to the end of line.
1494 while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n')
1495 ++CurPtr;
1496
1497 BufferPtr = CurPtr;
1498
1499 // No longer in the conflict marker.
1500 IsInConflictMarker = false;
1501 return true;
1502 }
1503
1504 return false;
1505}
1506
Chris Lattner22eb9722006-06-18 05:43:12 +00001507
1508/// LexTokenInternal - This implements a simple C family lexer. It is an
1509/// extremely performance critical piece of code. This assumes that the buffer
Chris Lattner5c349382009-07-07 05:05:42 +00001510/// has a null character at the end of the file. This returns a preprocessing
1511/// token, not a normal token, as such, it is an internal interface. It assumes
1512/// that the Flags of result have been cleared before calling this.
Chris Lattner146762e2007-07-20 16:59:19 +00001513void Lexer::LexTokenInternal(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001514LexNextToken:
1515 // New token, can't need cleaning yet.
Chris Lattner146762e2007-07-20 16:59:19 +00001516 Result.clearFlag(Token::NeedsCleaning);
Chris Lattner8c204872006-10-14 05:19:21 +00001517 Result.setIdentifierInfo(0);
Mike Stump11289f42009-09-09 15:08:12 +00001518
Chris Lattner22eb9722006-06-18 05:43:12 +00001519 // CurPtr - Cache BufferPtr in an automatic variable.
1520 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001521
Chris Lattnereb54b592006-07-10 06:34:27 +00001522 // Small amounts of horizontal whitespace is very common between tokens.
1523 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1524 ++CurPtr;
1525 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1526 ++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001527
Chris Lattner4d963442008-10-12 04:05:48 +00001528 // If we are keeping whitespace and other tokens, just return what we just
1529 // skipped. The next lexer invocation will return the token after the
1530 // whitespace.
1531 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001532 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4d963442008-10-12 04:05:48 +00001533 return;
1534 }
Mike Stump11289f42009-09-09 15:08:12 +00001535
Chris Lattnereb54b592006-07-10 06:34:27 +00001536 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001537 Result.setFlag(Token::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001538 }
Mike Stump11289f42009-09-09 15:08:12 +00001539
Chris Lattner22eb9722006-06-18 05:43:12 +00001540 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
Mike Stump11289f42009-09-09 15:08:12 +00001541
Chris Lattner22eb9722006-06-18 05:43:12 +00001542 // Read a character, advancing over it.
1543 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001544 tok::TokenKind Kind;
Mike Stump11289f42009-09-09 15:08:12 +00001545
Chris Lattner22eb9722006-06-18 05:43:12 +00001546 switch (Char) {
1547 case 0: // Null.
1548 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001549 if (CurPtr-1 == BufferEnd) {
1550 // Read the PP instance variable into an automatic variable, because
1551 // LexEndOfFile will often delete 'this'.
Chris Lattner02b436a2007-10-17 20:41:00 +00001552 Preprocessor *PPCache = PP;
Chris Lattner2183a6e2006-07-18 06:36:12 +00001553 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1554 return; // Got a token to return.
Chris Lattner02b436a2007-10-17 20:41:00 +00001555 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1556 return PPCache->Lex(Result);
Chris Lattner2183a6e2006-07-18 06:36:12 +00001557 }
Mike Stump11289f42009-09-09 15:08:12 +00001558
Chris Lattner6d27a162008-11-22 02:02:22 +00001559 if (!isLexingRawMode())
1560 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner146762e2007-07-20 16:59:19 +00001561 Result.setFlag(Token::LeadingSpace);
Chris Lattner4d963442008-10-12 04:05:48 +00001562 if (SkipWhitespace(Result, CurPtr))
1563 return; // KeepWhitespaceMode
Mike Stump11289f42009-09-09 15:08:12 +00001564
Chris Lattner22eb9722006-06-18 05:43:12 +00001565 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner3dfff972009-12-17 05:29:40 +00001566
1567 case 26: // DOS & CP/M EOF: "^Z".
1568 // If we're in Microsoft extensions mode, treat this as end of file.
1569 if (Features.Microsoft) {
1570 // Read the PP instance variable into an automatic variable, because
1571 // LexEndOfFile will often delete 'this'.
1572 Preprocessor *PPCache = PP;
1573 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1574 return; // Got a token to return.
1575 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1576 return PPCache->Lex(Result);
1577 }
1578 // If Microsoft extensions are disabled, this is just random garbage.
1579 Kind = tok::unknown;
1580 break;
1581
Chris Lattner22eb9722006-06-18 05:43:12 +00001582 case '\n':
1583 case '\r':
1584 // If we are inside a preprocessor directive and we see the end of line,
1585 // we know we are done with the directive, so return an EOM token.
1586 if (ParsingPreprocessorDirective) {
1587 // Done parsing the "line".
1588 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +00001589
Chris Lattner457fc152006-07-29 06:30:25 +00001590 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001591 SetCommentRetentionState(PP->getCommentRetentionState());
Mike Stump11289f42009-09-09 15:08:12 +00001592
Chris Lattner22eb9722006-06-18 05:43:12 +00001593 // Since we consumed a newline, we are back at the start of a line.
1594 IsAtStartOfLine = true;
Mike Stump11289f42009-09-09 15:08:12 +00001595
Chris Lattnerb11c3232008-10-12 04:51:35 +00001596 Kind = tok::eom;
Chris Lattner22eb9722006-06-18 05:43:12 +00001597 break;
1598 }
1599 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001600 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001601 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001602 Result.clearFlag(Token::LeadingSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001603
Chris Lattner4d963442008-10-12 04:05:48 +00001604 if (SkipWhitespace(Result, CurPtr))
1605 return; // KeepWhitespaceMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001606 goto LexNextToken; // GCC isn't tail call eliminating.
1607 case ' ':
1608 case '\t':
1609 case '\f':
1610 case '\v':
Chris Lattnerb9b85972007-07-22 06:29:05 +00001611 SkipHorizontalWhitespace:
Chris Lattner146762e2007-07-20 16:59:19 +00001612 Result.setFlag(Token::LeadingSpace);
Chris Lattner4d963442008-10-12 04:05:48 +00001613 if (SkipWhitespace(Result, CurPtr))
1614 return; // KeepWhitespaceMode
Chris Lattnerb9b85972007-07-22 06:29:05 +00001615
1616 SkipIgnoredUnits:
1617 CurPtr = BufferPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001618
Chris Lattnerb9b85972007-07-22 06:29:05 +00001619 // If the next token is obviously a // or /* */ comment, skip it efficiently
1620 // too (without going through the big switch stmt).
Chris Lattner58827712009-01-16 22:39:25 +00001621 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1622 Features.BCPLComment) {
Chris Lattner87d02082010-01-18 22:35:47 +00001623 if (SkipBCPLComment(Result, CurPtr+2))
1624 return; // There is a token to return.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001625 goto SkipIgnoredUnits;
Chris Lattner8637abd2008-10-12 03:22:02 +00001626 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner87d02082010-01-18 22:35:47 +00001627 if (SkipBlockComment(Result, CurPtr+2))
1628 return; // There is a token to return.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001629 goto SkipIgnoredUnits;
1630 } else if (isHorizontalWhitespace(*CurPtr)) {
1631 goto SkipHorizontalWhitespace;
1632 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001633 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner3dfff972009-12-17 05:29:40 +00001634
Chris Lattner2b15cf72008-01-03 17:58:54 +00001635 // C99 6.4.4.1: Integer Constants.
1636 // C99 6.4.4.2: Floating Constants.
1637 case '0': case '1': case '2': case '3': case '4':
1638 case '5': case '6': case '7': case '8': case '9':
1639 // Notify MIOpt that we read a non-whitespace/non-comment token.
1640 MIOpt.ReadToken();
1641 return LexNumericConstant(Result, CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001642
Chris Lattner2b15cf72008-01-03 17:58:54 +00001643 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Chris Lattner371ac8a2006-07-04 07:11:10 +00001644 // Notify MIOpt that we read a non-whitespace/non-comment token.
1645 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001646 Char = getCharAndSize(CurPtr, SizeTmp);
1647
1648 // Wide string literal.
1649 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001650 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1651 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001652
1653 // Wide character constant.
1654 if (Char == '\'')
1655 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1656 // FALL THROUGH, treating L like the start of an identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001657
Chris Lattner22eb9722006-06-18 05:43:12 +00001658 // C99 6.4.2: Identifiers.
1659 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1660 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1661 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1662 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1663 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1664 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1665 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1666 case 'v': case 'w': case 'x': case 'y': case 'z':
1667 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001668 // Notify MIOpt that we read a non-whitespace/non-comment token.
1669 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001670 return LexIdentifier(Result, CurPtr);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001671
1672 case '$': // $ in identifiers.
1673 if (Features.DollarIdents) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001674 if (!isLexingRawMode())
1675 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001676 // Notify MIOpt that we read a non-whitespace/non-comment token.
1677 MIOpt.ReadToken();
1678 return LexIdentifier(Result, CurPtr);
1679 }
Mike Stump11289f42009-09-09 15:08:12 +00001680
Chris Lattnerb11c3232008-10-12 04:51:35 +00001681 Kind = tok::unknown;
Chris Lattner2b15cf72008-01-03 17:58:54 +00001682 break;
Mike Stump11289f42009-09-09 15:08:12 +00001683
Chris Lattner22eb9722006-06-18 05:43:12 +00001684 // C99 6.4.4: Character Constants.
1685 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001686 // Notify MIOpt that we read a non-whitespace/non-comment token.
1687 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001688 return LexCharConstant(Result, CurPtr);
1689
1690 // C99 6.4.5: String Literals.
1691 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001692 // Notify MIOpt that we read a non-whitespace/non-comment token.
1693 MIOpt.ReadToken();
Chris Lattnerd3e98952006-10-06 05:22:26 +00001694 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001695
1696 // C99 6.4.6: Punctuators.
1697 case '?':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001698 Kind = tok::question;
Chris Lattner22eb9722006-06-18 05:43:12 +00001699 break;
1700 case '[':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001701 Kind = tok::l_square;
Chris Lattner22eb9722006-06-18 05:43:12 +00001702 break;
1703 case ']':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001704 Kind = tok::r_square;
Chris Lattner22eb9722006-06-18 05:43:12 +00001705 break;
1706 case '(':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001707 Kind = tok::l_paren;
Chris Lattner22eb9722006-06-18 05:43:12 +00001708 break;
1709 case ')':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001710 Kind = tok::r_paren;
Chris Lattner22eb9722006-06-18 05:43:12 +00001711 break;
1712 case '{':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001713 Kind = tok::l_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001714 break;
1715 case '}':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001716 Kind = tok::r_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001717 break;
1718 case '.':
1719 Char = getCharAndSize(CurPtr, SizeTmp);
1720 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001721 // Notify MIOpt that we read a non-whitespace/non-comment token.
1722 MIOpt.ReadToken();
1723
Chris Lattner22eb9722006-06-18 05:43:12 +00001724 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1725 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001726 Kind = tok::periodstar;
Chris Lattner22eb9722006-06-18 05:43:12 +00001727 CurPtr += SizeTmp;
1728 } else if (Char == '.' &&
1729 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001730 Kind = tok::ellipsis;
Chris Lattner22eb9722006-06-18 05:43:12 +00001731 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1732 SizeTmp2, Result);
1733 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001734 Kind = tok::period;
Chris Lattner22eb9722006-06-18 05:43:12 +00001735 }
1736 break;
1737 case '&':
1738 Char = getCharAndSize(CurPtr, SizeTmp);
1739 if (Char == '&') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001740 Kind = tok::ampamp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001741 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1742 } else if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001743 Kind = tok::ampequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001744 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1745 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001746 Kind = tok::amp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001747 }
1748 break;
Mike Stump11289f42009-09-09 15:08:12 +00001749 case '*':
Chris Lattner22eb9722006-06-18 05:43:12 +00001750 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001751 Kind = tok::starequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001752 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1753 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001754 Kind = tok::star;
Chris Lattner22eb9722006-06-18 05:43:12 +00001755 }
1756 break;
1757 case '+':
1758 Char = getCharAndSize(CurPtr, SizeTmp);
1759 if (Char == '+') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001760 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001761 Kind = tok::plusplus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001762 } else if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001763 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001764 Kind = tok::plusequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001765 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001766 Kind = tok::plus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001767 }
1768 break;
1769 case '-':
1770 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001771 if (Char == '-') { // --
Chris Lattner22eb9722006-06-18 05:43:12 +00001772 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001773 Kind = tok::minusminus;
Mike Stump11289f42009-09-09 15:08:12 +00001774 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattnerb11c3232008-10-12 04:51:35 +00001775 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00001776 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1777 SizeTmp2, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001778 Kind = tok::arrowstar;
1779 } else if (Char == '>') { // ->
Chris Lattner22eb9722006-06-18 05:43:12 +00001780 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001781 Kind = tok::arrow;
1782 } else if (Char == '=') { // -=
Chris Lattner22eb9722006-06-18 05:43:12 +00001783 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001784 Kind = tok::minusequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001785 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001786 Kind = tok::minus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001787 }
1788 break;
1789 case '~':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001790 Kind = tok::tilde;
Chris Lattner22eb9722006-06-18 05:43:12 +00001791 break;
1792 case '!':
1793 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001794 Kind = tok::exclaimequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001795 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1796 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001797 Kind = tok::exclaim;
Chris Lattner22eb9722006-06-18 05:43:12 +00001798 }
1799 break;
1800 case '/':
1801 // 6.4.9: Comments
1802 Char = getCharAndSize(CurPtr, SizeTmp);
1803 if (Char == '/') { // BCPL comment.
Chris Lattner58827712009-01-16 22:39:25 +00001804 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
1805 // want to lex this as a comment. There is one problem with this though,
1806 // that in one particular corner case, this can change the behavior of the
1807 // resultant program. For example, In "foo //**/ bar", C89 would lex
1808 // this as "foo / bar" and langauges with BCPL comments would lex it as
1809 // "foo". Check to see if the character after the second slash is a '*'.
1810 // If so, we will lex that as a "/" instead of the start of a comment.
1811 if (Features.BCPLComment ||
1812 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
1813 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner87d02082010-01-18 22:35:47 +00001814 return; // There is a token to return.
Mike Stump11289f42009-09-09 15:08:12 +00001815
Chris Lattner58827712009-01-16 22:39:25 +00001816 // It is common for the tokens immediately after a // comment to be
1817 // whitespace (indentation for the next line). Instead of going through
1818 // the big switch, handle it efficiently now.
1819 goto SkipIgnoredUnits;
1820 }
1821 }
Mike Stump11289f42009-09-09 15:08:12 +00001822
Chris Lattner58827712009-01-16 22:39:25 +00001823 if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001824 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner87d02082010-01-18 22:35:47 +00001825 return; // There is a token to return.
Chris Lattnere01e7582008-10-12 04:15:42 +00001826 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner58827712009-01-16 22:39:25 +00001827 }
Mike Stump11289f42009-09-09 15:08:12 +00001828
Chris Lattner58827712009-01-16 22:39:25 +00001829 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001830 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001831 Kind = tok::slashequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001832 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001833 Kind = tok::slash;
Chris Lattner22eb9722006-06-18 05:43:12 +00001834 }
1835 break;
1836 case '%':
1837 Char = getCharAndSize(CurPtr, SizeTmp);
1838 if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001839 Kind = tok::percentequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001840 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1841 } else if (Features.Digraphs && Char == '>') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001842 Kind = tok::r_brace; // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00001843 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1844 } else if (Features.Digraphs && Char == ':') {
1845 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001846 Char = getCharAndSize(CurPtr, SizeTmp);
1847 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001848 Kind = tok::hashhash; // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00001849 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1850 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001851 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner2b271db2006-07-15 05:41:09 +00001852 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner6d27a162008-11-22 02:02:22 +00001853 if (!isLexingRawMode())
1854 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001855 Kind = tok::hashat;
Chris Lattner2534324a2009-03-18 20:58:27 +00001856 } else { // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00001857 // We parsed a # character. If this occurs at the start of the line,
1858 // it's actually the start of a preprocessing directive. Callback to
1859 // the preprocessor to handle it.
1860 // FIXME: -fpreprocessed mode??
Chris Lattnerff96dd02009-05-13 06:10:29 +00001861 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattner2534324a2009-03-18 20:58:27 +00001862 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner02b436a2007-10-17 20:41:00 +00001863 PP->HandleDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +00001864
Chris Lattner22eb9722006-06-18 05:43:12 +00001865 // As an optimization, if the preprocessor didn't switch lexers, tail
1866 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00001867 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001868 // Start a new token. If this is a #include or something, the PP may
1869 // want us starting at the beginning of the line again. If so, set
1870 // the StartOfLine flag.
1871 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00001872 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001873 IsAtStartOfLine = false;
1874 }
1875 goto LexNextToken; // GCC isn't tail call eliminating.
1876 }
Mike Stump11289f42009-09-09 15:08:12 +00001877
Chris Lattner02b436a2007-10-17 20:41:00 +00001878 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001879 }
Mike Stump11289f42009-09-09 15:08:12 +00001880
Chris Lattner2534324a2009-03-18 20:58:27 +00001881 Kind = tok::hash;
Chris Lattner22eb9722006-06-18 05:43:12 +00001882 }
1883 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001884 Kind = tok::percent;
Chris Lattner22eb9722006-06-18 05:43:12 +00001885 }
1886 break;
1887 case '<':
1888 Char = getCharAndSize(CurPtr, SizeTmp);
1889 if (ParsingFilename) {
Chris Lattnerb40289b2009-04-17 23:56:52 +00001890 return LexAngledStringLiteral(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001891 } else if (Char == '<') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001892 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
1893 if (After == '=') {
1894 Kind = tok::lesslessequal;
1895 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1896 SizeTmp2, Result);
1897 } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) {
1898 // If this is actually a '<<<<<<<' version control conflict marker,
1899 // recognize it as such and recover nicely.
1900 goto LexNextToken;
1901 } else {
1902 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1903 Kind = tok::lessless;
1904 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001905 } else if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001906 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001907 Kind = tok::lessequal;
1908 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00001909 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001910 Kind = tok::l_square;
1911 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Chris Lattner22eb9722006-06-18 05:43:12 +00001912 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001913 Kind = tok::l_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001914 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001915 Kind = tok::less;
Chris Lattner22eb9722006-06-18 05:43:12 +00001916 }
1917 break;
1918 case '>':
1919 Char = getCharAndSize(CurPtr, SizeTmp);
1920 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001921 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001922 Kind = tok::greaterequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001923 } else if (Char == '>') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001924 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
1925 if (After == '=') {
1926 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1927 SizeTmp2, Result);
1928 Kind = tok::greatergreaterequal;
1929 } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) {
1930 // If this is '>>>>>>>' and we're in a conflict marker, ignore it.
1931 goto LexNextToken;
1932 } else {
1933 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1934 Kind = tok::greatergreater;
1935 }
1936
Chris Lattner22eb9722006-06-18 05:43:12 +00001937 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001938 Kind = tok::greater;
Chris Lattner22eb9722006-06-18 05:43:12 +00001939 }
1940 break;
1941 case '^':
1942 Char = getCharAndSize(CurPtr, SizeTmp);
1943 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001944 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001945 Kind = tok::caretequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001946 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001947 Kind = tok::caret;
Chris Lattner22eb9722006-06-18 05:43:12 +00001948 }
1949 break;
1950 case '|':
1951 Char = getCharAndSize(CurPtr, SizeTmp);
1952 if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001953 Kind = tok::pipeequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001954 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1955 } else if (Char == '|') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001956 // If this is '|||||||' and we're in a conflict marker, ignore it.
1957 if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1))
1958 goto LexNextToken;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001959 Kind = tok::pipepipe;
Chris Lattner22eb9722006-06-18 05:43:12 +00001960 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1961 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001962 Kind = tok::pipe;
Chris Lattner22eb9722006-06-18 05:43:12 +00001963 }
1964 break;
1965 case ':':
1966 Char = getCharAndSize(CurPtr, SizeTmp);
1967 if (Features.Digraphs && Char == '>') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001968 Kind = tok::r_square; // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00001969 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1970 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001971 Kind = tok::coloncolon;
Chris Lattner22eb9722006-06-18 05:43:12 +00001972 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump11289f42009-09-09 15:08:12 +00001973 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001974 Kind = tok::colon;
Chris Lattner22eb9722006-06-18 05:43:12 +00001975 }
1976 break;
1977 case ';':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001978 Kind = tok::semi;
Chris Lattner22eb9722006-06-18 05:43:12 +00001979 break;
1980 case '=':
1981 Char = getCharAndSize(CurPtr, SizeTmp);
1982 if (Char == '=') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001983 // If this is '=======' and we're in a conflict marker, ignore it.
1984 if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1))
1985 goto LexNextToken;
1986
Chris Lattnerb11c3232008-10-12 04:51:35 +00001987 Kind = tok::equalequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001988 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump11289f42009-09-09 15:08:12 +00001989 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001990 Kind = tok::equal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001991 }
1992 break;
1993 case ',':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001994 Kind = tok::comma;
Chris Lattner22eb9722006-06-18 05:43:12 +00001995 break;
1996 case '#':
1997 Char = getCharAndSize(CurPtr, SizeTmp);
1998 if (Char == '#') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001999 Kind = tok::hashhash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002000 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00002001 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattnerb11c3232008-10-12 04:51:35 +00002002 Kind = tok::hashat;
Chris Lattner6d27a162008-11-22 02:02:22 +00002003 if (!isLexingRawMode())
2004 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner2b271db2006-07-15 05:41:09 +00002005 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002006 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +00002007 // We parsed a # character. If this occurs at the start of the line,
2008 // it's actually the start of a preprocessing directive. Callback to
2009 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00002010 // FIXME: -fpreprocessed mode??
Chris Lattnerff96dd02009-05-13 06:10:29 +00002011 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattner2534324a2009-03-18 20:58:27 +00002012 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner02b436a2007-10-17 20:41:00 +00002013 PP->HandleDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +00002014
Chris Lattner22eb9722006-06-18 05:43:12 +00002015 // As an optimization, if the preprocessor didn't switch lexers, tail
2016 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00002017 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002018 // Start a new token. If this is a #include or something, the PP may
2019 // want us starting at the beginning of the line again. If so, set
2020 // the StartOfLine flag.
2021 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00002022 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00002023 IsAtStartOfLine = false;
2024 }
2025 goto LexNextToken; // GCC isn't tail call eliminating.
2026 }
Chris Lattner02b436a2007-10-17 20:41:00 +00002027 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002028 }
Mike Stump11289f42009-09-09 15:08:12 +00002029
Chris Lattner2534324a2009-03-18 20:58:27 +00002030 Kind = tok::hash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002031 }
2032 break;
2033
Chris Lattner2b15cf72008-01-03 17:58:54 +00002034 case '@':
2035 // Objective C support.
2036 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattnerb11c3232008-10-12 04:51:35 +00002037 Kind = tok::at;
Chris Lattner2b15cf72008-01-03 17:58:54 +00002038 else
Chris Lattnerb11c3232008-10-12 04:51:35 +00002039 Kind = tok::unknown;
Chris Lattner2b15cf72008-01-03 17:58:54 +00002040 break;
Mike Stump11289f42009-09-09 15:08:12 +00002041
Chris Lattner22eb9722006-06-18 05:43:12 +00002042 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00002043 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00002044 // FALL THROUGH.
2045 default:
Chris Lattnerb11c3232008-10-12 04:51:35 +00002046 Kind = tok::unknown;
Chris Lattner041bef82006-07-11 05:52:53 +00002047 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00002048 }
Mike Stump11289f42009-09-09 15:08:12 +00002049
Chris Lattner371ac8a2006-07-04 07:11:10 +00002050 // Notify MIOpt that we read a non-whitespace/non-comment token.
2051 MIOpt.ReadToken();
2052
Chris Lattnerd01e2912006-06-18 16:22:51 +00002053 // Update the location of token as well as BufferPtr.
Chris Lattnerb11c3232008-10-12 04:51:35 +00002054 FormTokenWithChars(Result, CurPtr, Kind);
Chris Lattner22eb9722006-06-18 05:43:12 +00002055}