blob: 9556bc39ecc00f22690aa3bbd378a77bc4582ee9 [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
Chris Lattner22eb9722006-06-18 05:43:12 +0000213
Chris Lattner8e129c22007-10-17 21:18:47 +0000214/// MeasureTokenLength - Relex the token at the specified location and return
215/// its length in bytes in the input file. If the token needs cleaning (e.g.
216/// includes a trigraph or an escaped newline) then this count includes bytes
217/// that are part of that.
218unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
Chris Lattner184e65d2009-04-14 23:22:57 +0000219 const SourceManager &SM,
220 const LangOptions &LangOpts) {
Chris Lattner8e129c22007-10-17 21:18:47 +0000221 // TODO: this could be special cased for common tokens like identifiers, ')',
222 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
Mike Stump11289f42009-09-09 15:08:12 +0000223 // all obviously single-char tokens. This could use
Chris Lattner8e129c22007-10-17 21:18:47 +0000224 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
225 // something.
Chris Lattner4fa23622009-01-26 00:43:02 +0000226
227 // If this comes from a macro expansion, we really do want the macro name, not
228 // the token this macro expanded to.
Chris Lattnerd3817212009-01-26 22:24:27 +0000229 Loc = SM.getInstantiationLoc(Loc);
230 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
Chris Lattner5509d532009-01-17 08:30:10 +0000231 std::pair<const char *,const char *> Buffer = SM.getBufferData(LocInfo.first);
232 const char *StrData = Buffer.first+LocInfo.second;
233
Chris Lattner8e129c22007-10-17 21:18:47 +0000234 // Create a lexer starting at the beginning of this token.
Chris Lattnerfcf64522009-01-17 07:42:27 +0000235 Lexer TheLexer(Loc, LangOpts, Buffer.first, StrData, Buffer.second);
Chris Lattnera3d4f162009-10-14 15:04:18 +0000236 TheLexer.SetCommentRetentionState(true);
Chris Lattner8e129c22007-10-17 21:18:47 +0000237 Token TheTok;
Chris Lattner50c90502008-10-12 01:15:46 +0000238 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner8e129c22007-10-17 21:18:47 +0000239 return TheTok.getLength();
240}
241
Chris Lattner22eb9722006-06-18 05:43:12 +0000242//===----------------------------------------------------------------------===//
243// Character information.
244//===----------------------------------------------------------------------===//
245
Chris Lattner22eb9722006-06-18 05:43:12 +0000246enum {
247 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
248 CHAR_VERT_WS = 0x02, // '\r', '\n'
249 CHAR_LETTER = 0x04, // a-z,A-Z
250 CHAR_NUMBER = 0x08, // 0-9
251 CHAR_UNDER = 0x10, // _
252 CHAR_PERIOD = 0x20 // .
253};
254
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000255// Statically initialize CharInfo table based on ASCII character set
256// Reference: FreeBSD 7.2 /usr/share/misc/ascii
Chris Lattner3dfff972009-12-17 05:29:40 +0000257static const unsigned char CharInfo[256] =
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000258{
259// 0 NUL 1 SOH 2 STX 3 ETX
260// 4 EOT 5 ENQ 6 ACK 7 BEL
261 0 , 0 , 0 , 0 ,
262 0 , 0 , 0 , 0 ,
263// 8 BS 9 HT 10 NL 11 VT
264//12 NP 13 CR 14 SO 15 SI
265 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS,
266 CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 ,
267//16 DLE 17 DC1 18 DC2 19 DC3
268//20 DC4 21 NAK 22 SYN 23 ETB
269 0 , 0 , 0 , 0 ,
270 0 , 0 , 0 , 0 ,
271//24 CAN 25 EM 26 SUB 27 ESC
272//28 FS 29 GS 30 RS 31 US
273 0 , 0 , 0 , 0 ,
274 0 , 0 , 0 , 0 ,
275//32 SP 33 ! 34 " 35 #
276//36 $ 37 % 38 & 39 '
277 CHAR_HORZ_WS, 0 , 0 , 0 ,
278 0 , 0 , 0 , 0 ,
279//40 ( 41 ) 42 * 43 +
280//44 , 45 - 46 . 47 /
281 0 , 0 , 0 , 0 ,
282 0 , 0 , CHAR_PERIOD , 0 ,
283//48 0 49 1 50 2 51 3
284//52 4 53 5 54 6 55 7
285 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
286 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
287//56 8 57 9 58 : 59 ;
288//60 < 61 = 62 > 63 ?
289 CHAR_NUMBER , CHAR_NUMBER , 0 , 0 ,
290 0 , 0 , 0 , 0 ,
291//64 @ 65 A 66 B 67 C
292//68 D 69 E 70 F 71 G
293 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
294 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
295//72 H 73 I 74 J 75 K
296//76 L 77 M 78 N 79 O
297 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
298 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
299//80 P 81 Q 82 R 83 S
300//84 T 85 U 86 V 87 W
301 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
302 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
303//88 X 89 Y 90 Z 91 [
304//92 \ 93 ] 94 ^ 95 _
305 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
306 0 , 0 , 0 , CHAR_UNDER ,
307//96 ` 97 a 98 b 99 c
308//100 d 101 e 102 f 103 g
309 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
310 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
311//104 h 105 i 106 j 107 k
312//108 l 109 m 110 n 111 o
313 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
314 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
315//112 p 113 q 114 r 115 s
316//116 t 117 u 118 v 119 w
317 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
318 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
319//120 x 121 y 122 z 123 {
320//124 | 125 } 126 ~ 127 DEL
321 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
322 0 , 0 , 0 , 0
323};
324
Chris Lattner3dfff972009-12-17 05:29:40 +0000325static void InitCharacterInfo() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000326 static bool isInited = false;
327 if (isInited) return;
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000328 // check the statically-initialized CharInfo table
329 assert(CHAR_HORZ_WS == CharInfo[(int)' ']);
330 assert(CHAR_HORZ_WS == CharInfo[(int)'\t']);
331 assert(CHAR_HORZ_WS == CharInfo[(int)'\f']);
332 assert(CHAR_HORZ_WS == CharInfo[(int)'\v']);
333 assert(CHAR_VERT_WS == CharInfo[(int)'\n']);
334 assert(CHAR_VERT_WS == CharInfo[(int)'\r']);
335 assert(CHAR_UNDER == CharInfo[(int)'_']);
336 assert(CHAR_PERIOD == CharInfo[(int)'.']);
337 for (unsigned i = 'a'; i <= 'z'; ++i) {
338 assert(CHAR_LETTER == CharInfo[i]);
339 assert(CHAR_LETTER == CharInfo[i+'A'-'a']);
340 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000341 for (unsigned i = '0'; i <= '9'; ++i)
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000342 assert(CHAR_NUMBER == CharInfo[i]);
Steve Naroff04bc0182009-12-08 16:38:12 +0000343
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000344 isInited = true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000345}
346
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000347
Chris Lattner22eb9722006-06-18 05:43:12 +0000348/// isIdentifierBody - Return true if this is the body character of an
349/// identifier, which is [a-zA-Z0-9_].
350static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000351 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000352}
353
354/// isHorizontalWhitespace - Return true if this character is horizontal
355/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
356static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000357 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000358}
359
360/// isWhitespace - Return true if this character is horizontal or vertical
361/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
362/// for '\0'.
363static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000364 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000365}
366
367/// isNumberBody - Return true if this is the body character of an
368/// preprocessing number, which is [a-zA-Z0-9_.].
369static inline bool isNumberBody(unsigned char c) {
Mike Stump11289f42009-09-09 15:08:12 +0000370 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000371 true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000372}
373
Chris Lattnerd01e2912006-06-18 16:22:51 +0000374
Chris Lattner22eb9722006-06-18 05:43:12 +0000375//===----------------------------------------------------------------------===//
376// Diagnostics forwarding code.
377//===----------------------------------------------------------------------===//
378
Chris Lattner619c1742007-07-22 18:38:25 +0000379/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
380/// lexer buffer was all instantiated at a single point, perform the mapping.
381/// This is currently only used for _Pragma implementation, so it is the slow
382/// path of the hot getSourceLocation method. Do not allow it to be inlined.
Benjamin Kramer5e738282009-11-14 16:36:57 +0000383static DISABLE_INLINE SourceLocation GetMappedTokenLoc(Preprocessor &PP,
384 SourceLocation FileLoc,
385 unsigned CharNo,
386 unsigned TokLen);
Chris Lattner619c1742007-07-22 18:38:25 +0000387static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
388 SourceLocation FileLoc,
Chris Lattner4fa23622009-01-26 00:43:02 +0000389 unsigned CharNo, unsigned TokLen) {
Chris Lattner9dc9c202009-02-15 20:52:18 +0000390 assert(FileLoc.isMacroID() && "Must be an instantiation");
Mike Stump11289f42009-09-09 15:08:12 +0000391
Chris Lattner619c1742007-07-22 18:38:25 +0000392 // Otherwise, we're lexing "mapped tokens". This is used for things like
393 // _Pragma handling. Combine the instantiation location of FileLoc with the
Chris Lattner53e384f2009-01-16 07:00:02 +0000394 // spelling location.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000395 SourceManager &SM = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000396
Chris Lattner8a425862009-01-16 07:36:28 +0000397 // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
Chris Lattner53e384f2009-01-16 07:00:02 +0000398 // characters come from spelling(FileLoc)+Offset.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000399 SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
Chris Lattner29a2a192009-01-19 06:46:35 +0000400 SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
Mike Stump11289f42009-09-09 15:08:12 +0000401
Chris Lattner9dc9c202009-02-15 20:52:18 +0000402 // Figure out the expansion loc range, which is the range covered by the
403 // original _Pragma(...) sequence.
404 std::pair<SourceLocation,SourceLocation> II =
405 SM.getImmediateInstantiationRange(FileLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000406
Chris Lattner9dc9c202009-02-15 20:52:18 +0000407 return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
Chris Lattner619c1742007-07-22 18:38:25 +0000408}
409
Chris Lattner22eb9722006-06-18 05:43:12 +0000410/// getSourceLocation - Return a source location identifier for the specified
411/// offset in the current file.
Chris Lattner4fa23622009-01-26 00:43:02 +0000412SourceLocation Lexer::getSourceLocation(const char *Loc,
413 unsigned TokLen) const {
Chris Lattner5d1c0272007-07-22 18:44:36 +0000414 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Chris Lattner4cca5ba2006-07-02 20:05:54 +0000415 "Location out of range for this buffer!");
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000416
417 // In the normal case, we're just lexing from a simple file buffer, return
418 // the file id from FileLoc with the offset specified.
Chris Lattner5d1c0272007-07-22 18:44:36 +0000419 unsigned CharNo = Loc-BufferStart;
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000420 if (FileLoc.isFileID())
Chris Lattner29a2a192009-01-19 06:46:35 +0000421 return FileLoc.getFileLocWithOffset(CharNo);
Mike Stump11289f42009-09-09 15:08:12 +0000422
Chris Lattnerd32480d2009-01-17 06:22:33 +0000423 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
424 // tokens are lexed from where the _Pragma was defined.
Chris Lattner02b436a2007-10-17 20:41:00 +0000425 assert(PP && "This doesn't work on raw lexers");
Chris Lattner4fa23622009-01-26 00:43:02 +0000426 return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
Chris Lattner22eb9722006-06-18 05:43:12 +0000427}
428
Chris Lattner22eb9722006-06-18 05:43:12 +0000429/// Diag - Forwarding function for diagnostics. This translate a source
430/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner427c9c12008-11-22 00:59:29 +0000431DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner907dfe92008-11-18 07:59:24 +0000432 return PP->Diag(getSourceLocation(Loc), DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000433}
434
435//===----------------------------------------------------------------------===//
436// Trigraph and Escaped Newline Handling Code.
437//===----------------------------------------------------------------------===//
438
439/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
440/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
441static char GetTrigraphCharForLetter(char Letter) {
442 switch (Letter) {
443 default: return 0;
444 case '=': return '#';
445 case ')': return ']';
446 case '(': return '[';
447 case '!': return '|';
448 case '\'': return '^';
449 case '>': return '}';
450 case '/': return '\\';
451 case '<': return '{';
452 case '-': return '~';
453 }
454}
455
456/// DecodeTrigraphChar - If the specified character is a legal trigraph when
457/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
458/// return the result character. Finally, emit a warning about trigraph use
459/// whether trigraphs are enabled or not.
460static char DecodeTrigraphChar(const char *CP, Lexer *L) {
461 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner907dfe92008-11-18 07:59:24 +0000462 if (!Res || !L) return Res;
Mike Stump11289f42009-09-09 15:08:12 +0000463
Chris Lattner907dfe92008-11-18 07:59:24 +0000464 if (!L->getFeatures().Trigraphs) {
Chris Lattner6d27a162008-11-22 02:02:22 +0000465 if (!L->isLexingRawMode())
466 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner907dfe92008-11-18 07:59:24 +0000467 return 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000468 }
Mike Stump11289f42009-09-09 15:08:12 +0000469
Chris Lattner6d27a162008-11-22 02:02:22 +0000470 if (!L->isLexingRawMode())
471 L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
Chris Lattner22eb9722006-06-18 05:43:12 +0000472 return Res;
473}
474
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000475/// getEscapedNewLineSize - Return the size of the specified escaped newline,
476/// 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 +0000477/// trigraph equivalent on entry to this function.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000478unsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
479 unsigned Size = 0;
480 while (isWhitespace(Ptr[Size])) {
481 ++Size;
Mike Stump11289f42009-09-09 15:08:12 +0000482
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000483 if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
484 continue;
485
486 // If this is a \r\n or \n\r, skip the other half.
487 if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
488 Ptr[Size-1] != Ptr[Size])
489 ++Size;
Mike Stump11289f42009-09-09 15:08:12 +0000490
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000491 return Size;
Mike Stump11289f42009-09-09 15:08:12 +0000492 }
493
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000494 // Not an escaped newline, must be a \t or something else.
495 return 0;
496}
497
Chris Lattner38b2cde2009-04-18 22:27:02 +0000498/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
499/// them), skip over them and return the first non-escaped-newline found,
500/// otherwise return P.
501const char *Lexer::SkipEscapedNewLines(const char *P) {
502 while (1) {
503 const char *AfterEscape;
504 if (*P == '\\') {
505 AfterEscape = P+1;
506 } else if (*P == '?') {
507 // If not a trigraph for escape, bail out.
508 if (P[1] != '?' || P[2] != '/')
509 return P;
510 AfterEscape = P+3;
511 } else {
512 return P;
513 }
Mike Stump11289f42009-09-09 15:08:12 +0000514
Chris Lattner38b2cde2009-04-18 22:27:02 +0000515 unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
516 if (NewLineSize == 0) return P;
517 P = AfterEscape+NewLineSize;
518 }
519}
520
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000521
Chris Lattner22eb9722006-06-18 05:43:12 +0000522/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
523/// get its size, and return it. This is tricky in several cases:
524/// 1. If currently at the start of a trigraph, we warn about the trigraph,
525/// then either return the trigraph (skipping 3 chars) or the '?',
526/// depending on whether trigraphs are enabled or not.
527/// 2. If this is an escaped newline (potentially with whitespace between
528/// the backslash and newline), implicitly skip the newline and return
529/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000530/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000531///
532/// This handles the slow/uncommon case of the getCharAndSize method. Here we
533/// know that we can accumulate into Size, and that we have already incremented
534/// Ptr by Size bytes.
535///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000536/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
537/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000538///
539char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattner146762e2007-07-20 16:59:19 +0000540 Token *Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000541 // If we have a slash, look for an escaped newline.
542 if (Ptr[0] == '\\') {
543 ++Size;
544 ++Ptr;
545Slash:
546 // Common case, backslash-char where the char is not whitespace.
547 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump11289f42009-09-09 15:08:12 +0000548
Chris Lattnerc1835952009-06-23 05:15:06 +0000549 // See if we have optional whitespace characters between the slash and
550 // newline.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000551 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
552 // Remember that this token needs to be cleaned.
553 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000554
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000555 // Warn if there was whitespace between the backslash and newline.
Chris Lattnerc1835952009-06-23 05:15:06 +0000556 if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000557 Diag(Ptr, diag::backslash_newline_space);
Mike Stump11289f42009-09-09 15:08:12 +0000558
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000559 // Found backslash<whitespace><newline>. Parse the char after it.
560 Size += EscapedNewLineSize;
561 Ptr += EscapedNewLineSize;
562 // Use slow version to accumulate a correct size field.
563 return getCharAndSizeSlow(Ptr, Size, Tok);
564 }
Mike Stump11289f42009-09-09 15:08:12 +0000565
Chris Lattner22eb9722006-06-18 05:43:12 +0000566 // Otherwise, this is not an escaped newline, just return the slash.
567 return '\\';
568 }
Mike Stump11289f42009-09-09 15:08:12 +0000569
Chris Lattner22eb9722006-06-18 05:43:12 +0000570 // If this is a trigraph, process it.
571 if (Ptr[0] == '?' && Ptr[1] == '?') {
572 // If this is actually a legal trigraph (not something like "??x"), emit
573 // a trigraph warning. If so, and if trigraphs are enabled, return it.
574 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
575 // Remember that this token needs to be cleaned.
Chris Lattner146762e2007-07-20 16:59:19 +0000576 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000577
578 Ptr += 3;
579 Size += 3;
580 if (C == '\\') goto Slash;
581 return C;
582 }
583 }
Mike Stump11289f42009-09-09 15:08:12 +0000584
Chris Lattner22eb9722006-06-18 05:43:12 +0000585 // If this is neither, return a single character.
586 ++Size;
587 return *Ptr;
588}
589
Chris Lattnerd01e2912006-06-18 16:22:51 +0000590
Chris Lattner22eb9722006-06-18 05:43:12 +0000591/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
592/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
593/// and that we have already incremented Ptr by Size bytes.
594///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000595/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
596/// be updated to match.
597char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000598 const LangOptions &Features) {
599 // If we have a slash, look for an escaped newline.
600 if (Ptr[0] == '\\') {
601 ++Size;
602 ++Ptr;
603Slash:
604 // Common case, backslash-char where the char is not whitespace.
605 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump11289f42009-09-09 15:08:12 +0000606
Chris Lattner22eb9722006-06-18 05:43:12 +0000607 // See if we have optional whitespace characters followed by a newline.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000608 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
609 // Found backslash<whitespace><newline>. Parse the char after it.
610 Size += EscapedNewLineSize;
611 Ptr += EscapedNewLineSize;
Mike Stump11289f42009-09-09 15:08:12 +0000612
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000613 // Use slow version to accumulate a correct size field.
614 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
615 }
Mike Stump11289f42009-09-09 15:08:12 +0000616
Chris Lattner22eb9722006-06-18 05:43:12 +0000617 // Otherwise, this is not an escaped newline, just return the slash.
618 return '\\';
619 }
Mike Stump11289f42009-09-09 15:08:12 +0000620
Chris Lattner22eb9722006-06-18 05:43:12 +0000621 // If this is a trigraph, process it.
622 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
623 // If this is actually a legal trigraph (not something like "??x"), return
624 // it.
625 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
626 Ptr += 3;
627 Size += 3;
628 if (C == '\\') goto Slash;
629 return C;
630 }
631 }
Mike Stump11289f42009-09-09 15:08:12 +0000632
Chris Lattner22eb9722006-06-18 05:43:12 +0000633 // If this is neither, return a single character.
634 ++Size;
635 return *Ptr;
636}
637
Chris Lattner22eb9722006-06-18 05:43:12 +0000638//===----------------------------------------------------------------------===//
639// Helper methods for lexing.
640//===----------------------------------------------------------------------===//
641
Chris Lattner146762e2007-07-20 16:59:19 +0000642void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000643 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
644 unsigned Size;
645 unsigned char C = *CurPtr++;
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000646 while (isIdentifierBody(C))
Chris Lattner22eb9722006-06-18 05:43:12 +0000647 C = *CurPtr++;
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000648
Chris Lattner22eb9722006-06-18 05:43:12 +0000649 --CurPtr; // Back up over the skipped character.
650
651 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
652 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000653 // FIXME: UCNs.
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000654 //
655 // TODO: Could merge these checks into a CharInfo flag to make the comparison
656 // cheaper
Chris Lattner22eb9722006-06-18 05:43:12 +0000657 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
658FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000659 const char *IdStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000660 FormTokenWithChars(Result, CurPtr, tok::identifier);
Mike Stump11289f42009-09-09 15:08:12 +0000661
Chris Lattner0f1f5052006-07-20 04:16:23 +0000662 // If we are in raw mode, return this identifier raw. There is no need to
663 // look up identifier information or attempt to macro expand it.
664 if (LexingRawMode) return;
Mike Stump11289f42009-09-09 15:08:12 +0000665
Chris Lattnercefc7682006-07-08 08:28:12 +0000666 // Fill in Result.IdentifierInfo, looking up the identifier in the
667 // identifier table.
Chris Lattner8256b972009-01-21 07:45:14 +0000668 IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
Mike Stump11289f42009-09-09 15:08:12 +0000669
Chris Lattner1f6c7fe2009-01-23 18:35:48 +0000670 // Change the kind of this identifier to the appropriate token kind, e.g.
671 // turning "for" into a keyword.
672 Result.setKind(II->getTokenID());
Mike Stump11289f42009-09-09 15:08:12 +0000673
Chris Lattnerc5a00062006-06-18 16:41:01 +0000674 // Finally, now that we know we have an identifier, pass this off to the
675 // preprocessor, which may macro expand it or something.
Chris Lattner8256b972009-01-21 07:45:14 +0000676 if (II->isHandleIdentifierCase())
Chris Lattnerad89ec02009-01-21 07:43:11 +0000677 PP->HandleIdentifier(Result);
678 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000679 }
Mike Stump11289f42009-09-09 15:08:12 +0000680
Chris Lattner22eb9722006-06-18 05:43:12 +0000681 // Otherwise, $,\,? in identifier found. Enter slower path.
Mike Stump11289f42009-09-09 15:08:12 +0000682
Chris Lattner22eb9722006-06-18 05:43:12 +0000683 C = getCharAndSize(CurPtr, Size);
684 while (1) {
685 if (C == '$') {
686 // If we hit a $ and they are not supported in identifiers, we are done.
687 if (!Features.DollarIdents) goto FinishIdentifier;
Mike Stump11289f42009-09-09 15:08:12 +0000688
Chris Lattner22eb9722006-06-18 05:43:12 +0000689 // Otherwise, emit a diagnostic and continue.
Chris Lattner6d27a162008-11-22 02:02:22 +0000690 if (!isLexingRawMode())
691 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000692 CurPtr = ConsumeChar(CurPtr, Size, Result);
693 C = getCharAndSize(CurPtr, Size);
694 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000695 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000696 // Found end of identifier.
697 goto FinishIdentifier;
698 }
699
700 // Otherwise, this character is good, consume it.
701 CurPtr = ConsumeChar(CurPtr, Size, Result);
702
703 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000704 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000705 CurPtr = ConsumeChar(CurPtr, Size, Result);
706 C = getCharAndSize(CurPtr, Size);
707 }
708 }
709}
710
711
Nate Begeman5eee9332008-04-14 02:26:39 +0000712/// LexNumericConstant - Lex the remainder of a integer or floating point
Chris Lattner22eb9722006-06-18 05:43:12 +0000713/// constant. From[-1] is the first character lexed. Return the end of the
714/// constant.
Chris Lattner146762e2007-07-20 16:59:19 +0000715void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000716 unsigned Size;
717 char C = getCharAndSize(CurPtr, Size);
718 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000719 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000720 CurPtr = ConsumeChar(CurPtr, Size, Result);
721 PrevCh = C;
722 C = getCharAndSize(CurPtr, Size);
723 }
Mike Stump11289f42009-09-09 15:08:12 +0000724
Chris Lattner22eb9722006-06-18 05:43:12 +0000725 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
726 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
727 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
728
729 // If we have a hex FP constant, continue.
Alexis Hunt91b78382010-01-10 23:37:56 +0000730 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
731 (!PP || !PP->getLangOptions().CPlusPlus0x))
Chris Lattner22eb9722006-06-18 05:43:12 +0000732 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattnerd01e2912006-06-18 16:22:51 +0000734 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000735 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000736 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000737 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000738}
739
740/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
741/// either " or L".
Chris Lattner4d963442008-10-12 04:05:48 +0000742void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000743 const char *NulCharacter = 0; // Does this string contain the \0 character?
Mike Stump11289f42009-09-09 15:08:12 +0000744
Chris Lattner22eb9722006-06-18 05:43:12 +0000745 char C = getAndAdvanceChar(CurPtr, Result);
746 while (C != '"') {
747 // Skip escaped characters.
748 if (C == '\\') {
749 // Skip the escaped character.
750 C = getAndAdvanceChar(CurPtr, Result);
751 } else if (C == '\n' || C == '\r' || // Newline.
752 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerd14705b2009-03-18 21:10:12 +0000753 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +0000754 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattnerb11c3232008-10-12 04:51:35 +0000755 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000756 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000757 } else if (C == 0) {
758 NulCharacter = CurPtr-1;
759 }
760 C = getAndAdvanceChar(CurPtr, Result);
761 }
Mike Stump11289f42009-09-09 15:08:12 +0000762
Chris Lattner5a78a022006-07-20 06:02:19 +0000763 // If a nul character existed in the string, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +0000764 if (NulCharacter && !isLexingRawMode())
765 Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000766
Chris Lattnerd01e2912006-06-18 16:22:51 +0000767 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000768 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000769 FormTokenWithChars(Result, CurPtr,
770 Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000771 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000772}
773
774/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
775/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattner146762e2007-07-20 16:59:19 +0000776void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000777 const char *NulCharacter = 0; // Does this string contain the \0 character?
Chris Lattnerb40289b2009-04-17 23:56:52 +0000778 const char *AfterLessPos = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000779 char C = getAndAdvanceChar(CurPtr, Result);
780 while (C != '>') {
781 // Skip escaped characters.
782 if (C == '\\') {
783 // Skip the escaped character.
784 C = getAndAdvanceChar(CurPtr, Result);
785 } else if (C == '\n' || C == '\r' || // Newline.
786 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerb40289b2009-04-17 23:56:52 +0000787 // If the filename is unterminated, then it must just be a lone <
788 // character. Return this as such.
789 FormTokenWithChars(Result, AfterLessPos, tok::less);
Chris Lattner5a78a022006-07-20 06:02:19 +0000790 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000791 } else if (C == 0) {
792 NulCharacter = CurPtr-1;
793 }
794 C = getAndAdvanceChar(CurPtr, Result);
795 }
Mike Stump11289f42009-09-09 15:08:12 +0000796
Chris Lattner5a78a022006-07-20 06:02:19 +0000797 // If a nul character existed in the string, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +0000798 if (NulCharacter && !isLexingRawMode())
799 Diag(NulCharacter, diag::null_in_string);
Mike Stump11289f42009-09-09 15:08:12 +0000800
Chris Lattnerd01e2912006-06-18 16:22:51 +0000801 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000802 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000803 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000804 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000805}
806
807
808/// LexCharConstant - Lex the remainder of a character constant, after having
809/// lexed either ' or L'.
Chris Lattner146762e2007-07-20 16:59:19 +0000810void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000811 const char *NulCharacter = 0; // Does this character contain the \0 character?
812
813 // Handle the common case of 'x' and '\y' efficiently.
814 char C = getAndAdvanceChar(CurPtr, Result);
815 if (C == '\'') {
Chris Lattnerd14705b2009-03-18 21:10:12 +0000816 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +0000817 Diag(BufferPtr, diag::err_empty_character);
Chris Lattnerb11c3232008-10-12 04:51:35 +0000818 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000819 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000820 } else if (C == '\\') {
821 // Skip the escaped character.
822 // FIXME: UCN's.
823 C = getAndAdvanceChar(CurPtr, Result);
824 }
Mike Stump11289f42009-09-09 15:08:12 +0000825
Chris Lattner22eb9722006-06-18 05:43:12 +0000826 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
827 ++CurPtr;
828 } else {
829 // Fall back on generic code for embedded nulls, newlines, wide chars.
830 do {
831 // Skip escaped characters.
832 if (C == '\\') {
833 // Skip the escaped character.
834 C = getAndAdvanceChar(CurPtr, Result);
835 } else if (C == '\n' || C == '\r' || // Newline.
836 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerd14705b2009-03-18 21:10:12 +0000837 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +0000838 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattnerb11c3232008-10-12 04:51:35 +0000839 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000840 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000841 } else if (C == 0) {
842 NulCharacter = CurPtr-1;
843 }
844 C = getAndAdvanceChar(CurPtr, Result);
845 } while (C != '\'');
846 }
Mike Stump11289f42009-09-09 15:08:12 +0000847
Chris Lattner6d27a162008-11-22 02:02:22 +0000848 if (NulCharacter && !isLexingRawMode())
849 Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000850
Chris Lattnerd01e2912006-06-18 16:22:51 +0000851 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000852 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000853 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000854 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000855}
856
857/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
858/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattner4d963442008-10-12 04:05:48 +0000859///
860/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
861///
862bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000863 // Whitespace - Skip it, then return the token after the whitespace.
864 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
865 while (1) {
866 // Skip horizontal whitespace very aggressively.
867 while (isHorizontalWhitespace(Char))
868 Char = *++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000869
Daniel Dunbar5c4cc092008-11-25 00:20:22 +0000870 // Otherwise if we have something other than whitespace, we're done.
Chris Lattner22eb9722006-06-18 05:43:12 +0000871 if (Char != '\n' && Char != '\r')
872 break;
Mike Stump11289f42009-09-09 15:08:12 +0000873
Chris Lattner22eb9722006-06-18 05:43:12 +0000874 if (ParsingPreprocessorDirective) {
875 // End of preprocessor directive line, let LexTokenInternal handle this.
876 BufferPtr = CurPtr;
Chris Lattner4d963442008-10-12 04:05:48 +0000877 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000878 }
Mike Stump11289f42009-09-09 15:08:12 +0000879
Chris Lattner22eb9722006-06-18 05:43:12 +0000880 // ok, but handle newline.
881 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +0000882 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000883 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +0000884 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000885 Char = *++CurPtr;
886 }
887
888 // If this isn't immediately after a newline, there is leading space.
889 char PrevChar = CurPtr[-1];
890 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattner146762e2007-07-20 16:59:19 +0000891 Result.setFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000892
Chris Lattner4d963442008-10-12 04:05:48 +0000893 // If the client wants us to return whitespace, return it now.
894 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +0000895 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4d963442008-10-12 04:05:48 +0000896 return true;
897 }
Mike Stump11289f42009-09-09 15:08:12 +0000898
Chris Lattner22eb9722006-06-18 05:43:12 +0000899 BufferPtr = CurPtr;
Chris Lattner4d963442008-10-12 04:05:48 +0000900 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000901}
902
903// SkipBCPLComment - We have just read the // characters from input. Skip until
904// we find the newline character thats terminate the comment. Then update
Chris Lattner87d02082010-01-18 22:35:47 +0000905/// BufferPtr and return.
906///
907/// If we're in KeepCommentMode or any CommentHandler has inserted
908/// some tokens, this will store the first token and return true.
Chris Lattner146762e2007-07-20 16:59:19 +0000909bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000910 // If BCPL comments aren't explicitly enabled for this language, emit an
911 // extension warning.
Chris Lattner6d27a162008-11-22 02:02:22 +0000912 if (!Features.BCPLComment && !isLexingRawMode()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000913 Diag(BufferPtr, diag::ext_bcpl_comment);
Mike Stump11289f42009-09-09 15:08:12 +0000914
Chris Lattner22eb9722006-06-18 05:43:12 +0000915 // Mark them enabled so we only emit one warning for this translation
916 // unit.
917 Features.BCPLComment = true;
918 }
Mike Stump11289f42009-09-09 15:08:12 +0000919
Chris Lattner22eb9722006-06-18 05:43:12 +0000920 // Scan over the body of the comment. The common case, when scanning, is that
921 // the comment contains normal ascii characters with nothing interesting in
922 // them. As such, optimize for this case with the inner loop.
923 char C;
924 do {
925 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +0000926 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
927 // If we find a \n character, scan backwards, checking to see if it's an
928 // escaped newline, like we do for block comments.
Mike Stump11289f42009-09-09 15:08:12 +0000929
Chris Lattner22eb9722006-06-18 05:43:12 +0000930 // Skip over characters in the fast loop.
931 while (C != 0 && // Potentially EOF.
932 C != '\\' && // Potentially escaped newline.
933 C != '?' && // Potentially trigraph.
934 C != '\n' && C != '\r') // Newline or DOS-style newline.
935 C = *++CurPtr;
936
937 // If this is a newline, we're done.
938 if (C == '\n' || C == '\r')
939 break; // Found the newline? Break out!
Mike Stump11289f42009-09-09 15:08:12 +0000940
Chris Lattner22eb9722006-06-18 05:43:12 +0000941 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnere141a9e2008-12-12 07:34:39 +0000942 // properly decode the character. Read it in raw mode to avoid emitting
943 // diagnostics about things like trigraphs. If we see an escaped newline,
944 // we'll handle it below.
Chris Lattner22eb9722006-06-18 05:43:12 +0000945 const char *OldPtr = CurPtr;
Chris Lattnere141a9e2008-12-12 07:34:39 +0000946 bool OldRawMode = isLexingRawMode();
947 LexingRawMode = true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000948 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnere141a9e2008-12-12 07:34:39 +0000949 LexingRawMode = OldRawMode;
Chris Lattnerecdaf402009-04-05 00:26:41 +0000950
951 // If the char that we finally got was a \n, then we must have had something
952 // like \<newline><newline>. We don't want to have consumed the second
953 // newline, we want CurPtr, to end up pointing to it down below.
954 if (C == '\n' || C == '\r') {
955 --CurPtr;
956 C = 'x'; // doesn't matter what this is.
957 }
Mike Stump11289f42009-09-09 15:08:12 +0000958
Chris Lattner22eb9722006-06-18 05:43:12 +0000959 // If we read multiple characters, and one of those characters was a \r or
Chris Lattnerff591e22007-06-09 06:07:22 +0000960 // \n, then we had an escaped newline within the comment. Emit diagnostic
961 // unless the next line is also a // comment.
962 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000963 for (; OldPtr != CurPtr; ++OldPtr)
964 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnerff591e22007-06-09 06:07:22 +0000965 // Okay, we found a // comment that ends in a newline, if the next
966 // line is also a // comment, but has spaces, don't emit a diagnostic.
967 if (isspace(C)) {
968 const char *ForwardPtr = CurPtr;
969 while (isspace(*ForwardPtr)) // Skip whitespace.
970 ++ForwardPtr;
971 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
972 break;
973 }
Mike Stump11289f42009-09-09 15:08:12 +0000974
Chris Lattner6d27a162008-11-22 02:02:22 +0000975 if (!isLexingRawMode())
976 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000977 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000978 }
979 }
Mike Stump11289f42009-09-09 15:08:12 +0000980
Chris Lattner457fc152006-07-29 06:30:25 +0000981 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
Chris Lattner22eb9722006-06-18 05:43:12 +0000982 } while (C != '\n' && C != '\r');
983
Chris Lattner457fc152006-07-29 06:30:25 +0000984 // Found but did not consume the newline.
Chris Lattner87d02082010-01-18 22:35:47 +0000985 if (PP && PP->HandleComment(Result,
986 SourceRange(getSourceLocation(BufferPtr),
987 getSourceLocation(CurPtr)))) {
988 BufferPtr = CurPtr;
989 return true; // A token has to be returned.
990 }
Mike Stump11289f42009-09-09 15:08:12 +0000991
Chris Lattner457fc152006-07-29 06:30:25 +0000992 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +0000993 if (inKeepCommentMode())
Chris Lattner457fc152006-07-29 06:30:25 +0000994 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000995
996 // If we are inside a preprocessor directive and we see the end of line,
997 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +0000998 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000999 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001000 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001001 }
Mike Stump11289f42009-09-09 15:08:12 +00001002
Chris Lattner22eb9722006-06-18 05:43:12 +00001003 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner87e97ea2008-10-12 00:23:07 +00001004 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattner4d963442008-10-12 04:05:48 +00001005 // contribute to another token), it isn't needed for correctness. Note that
1006 // this is ok even in KeepWhitespaceMode, because we would have returned the
1007 /// comment above in that mode.
Chris Lattner22eb9722006-06-18 05:43:12 +00001008 ++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001009
Chris Lattner22eb9722006-06-18 05:43:12 +00001010 // The next returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001011 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001012 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001013 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001014 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001015 return false;
Chris Lattner457fc152006-07-29 06:30:25 +00001016}
Chris Lattner22eb9722006-06-18 05:43:12 +00001017
Chris Lattner457fc152006-07-29 06:30:25 +00001018/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
1019/// an appropriate way and return it.
Chris Lattner146762e2007-07-20 16:59:19 +00001020bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001021 // If we're not in a preprocessor directive, just return the // comment
1022 // directly.
1023 FormTokenWithChars(Result, CurPtr, tok::comment);
Mike Stump11289f42009-09-09 15:08:12 +00001024
Chris Lattnerb11c3232008-10-12 04:51:35 +00001025 if (!ParsingPreprocessorDirective)
1026 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001027
Chris Lattnerb11c3232008-10-12 04:51:35 +00001028 // If this BCPL-style comment is in a macro definition, transmogrify it into
1029 // a C-style block comment.
1030 std::string Spelling = PP->getSpelling(Result);
1031 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
1032 Spelling[1] = '*'; // Change prefix to "/*".
1033 Spelling += "*/"; // add suffix.
Mike Stump11289f42009-09-09 15:08:12 +00001034
Chris Lattnerb11c3232008-10-12 04:51:35 +00001035 Result.setKind(tok::comment);
Chris Lattner5a7971e2009-01-26 19:29:26 +00001036 PP->CreateString(&Spelling[0], Spelling.size(), Result,
1037 Result.getLocation());
Chris Lattnere01e7582008-10-12 04:15:42 +00001038 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001039}
1040
Chris Lattnercb283342006-06-18 06:48:37 +00001041/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
1042/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner89770572008-12-12 07:14:34 +00001043/// diagnostic if so. We know that the newline is inside of a block comment.
Mike Stump11289f42009-09-09 15:08:12 +00001044static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
Chris Lattner1f583052006-06-18 06:53:56 +00001045 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001046 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Mike Stump11289f42009-09-09 15:08:12 +00001047
Chris Lattner22eb9722006-06-18 05:43:12 +00001048 // Back up off the newline.
1049 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001050
Chris Lattner22eb9722006-06-18 05:43:12 +00001051 // If this is a two-character newline sequence, skip the other character.
1052 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
1053 // \n\n or \r\r -> not escaped newline.
1054 if (CurPtr[0] == CurPtr[1])
1055 return false;
1056 // \n\r or \r\n -> skip the newline.
1057 --CurPtr;
1058 }
Mike Stump11289f42009-09-09 15:08:12 +00001059
Chris Lattner22eb9722006-06-18 05:43:12 +00001060 // If we have horizontal whitespace, skip over it. We allow whitespace
1061 // between the slash and newline.
1062 bool HasSpace = false;
1063 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
1064 --CurPtr;
1065 HasSpace = true;
1066 }
Mike Stump11289f42009-09-09 15:08:12 +00001067
Chris Lattner22eb9722006-06-18 05:43:12 +00001068 // If we have a slash, we know this is an escaped newline.
1069 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +00001070 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001071 } else {
1072 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +00001073 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
1074 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +00001075 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001076
Chris Lattnercb283342006-06-18 06:48:37 +00001077 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +00001078 CurPtr -= 2;
1079
1080 // If no trigraphs are enabled, warn that we ignored this trigraph and
1081 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +00001082 if (!L->getFeatures().Trigraphs) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001083 if (!L->isLexingRawMode())
1084 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +00001085 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001086 }
Chris Lattner6d27a162008-11-22 02:02:22 +00001087 if (!L->isLexingRawMode())
1088 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001089 }
Mike Stump11289f42009-09-09 15:08:12 +00001090
Chris Lattner22eb9722006-06-18 05:43:12 +00001091 // Warn about having an escaped newline between the */ characters.
Chris Lattner6d27a162008-11-22 02:02:22 +00001092 if (!L->isLexingRawMode())
1093 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Mike Stump11289f42009-09-09 15:08:12 +00001094
Chris Lattner22eb9722006-06-18 05:43:12 +00001095 // If there was space between the backslash and newline, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +00001096 if (HasSpace && !L->isLexingRawMode())
1097 L->Diag(CurPtr, diag::backslash_newline_space);
Mike Stump11289f42009-09-09 15:08:12 +00001098
Chris Lattnercb283342006-06-18 06:48:37 +00001099 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001100}
1101
Chris Lattneraded4a92006-10-27 04:42:31 +00001102#ifdef __SSE2__
1103#include <emmintrin.h>
Chris Lattner9f6604f2006-10-30 20:01:22 +00001104#elif __ALTIVEC__
1105#include <altivec.h>
1106#undef bool
Chris Lattneraded4a92006-10-27 04:42:31 +00001107#endif
1108
Chris Lattner22eb9722006-06-18 05:43:12 +00001109/// SkipBlockComment - We have just read the /* characters from input. Read
1110/// until we find the */ characters that terminate the comment. Note that we
1111/// don't bother decoding trigraphs or escaped newlines in block comments,
1112/// because they cannot cause the comment to end. The only thing that can
1113/// happen is the comment could end with an escaped newline between the */ end
1114/// of comment.
Chris Lattnere01e7582008-10-12 04:15:42 +00001115///
Chris Lattner87d02082010-01-18 22:35:47 +00001116/// If we're in KeepCommentMode or any CommentHandler has inserted
1117/// some tokens, this will store the first token and return true.
Chris Lattner146762e2007-07-20 16:59:19 +00001118bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001119 // Scan one character past where we should, looking for a '/' character. Once
1120 // we find it, check to see if it was preceeded by a *. This common
1121 // optimization helps people who like to put a lot of * characters in their
1122 // comments.
Chris Lattnerc850ad62007-07-21 23:43:37 +00001123
1124 // The first character we get with newlines and trigraphs skipped to handle
1125 // the degenerate /*/ case below correctly if the * has an escaped newline
1126 // after it.
1127 unsigned CharSize;
1128 unsigned char C = getCharAndSize(CurPtr, CharSize);
1129 CurPtr += CharSize;
Chris Lattner22eb9722006-06-18 05:43:12 +00001130 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001131 if (!isLexingRawMode())
Chris Lattner7c2e9802008-10-12 01:31:51 +00001132 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner99e7d232008-10-12 04:19:49 +00001133 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001134
Chris Lattner99e7d232008-10-12 04:19:49 +00001135 // KeepWhitespaceMode should return this broken comment as a token. Since
1136 // it isn't a well formed comment, just return it as an 'unknown' token.
1137 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001138 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner99e7d232008-10-12 04:19:49 +00001139 return true;
1140 }
Mike Stump11289f42009-09-09 15:08:12 +00001141
Chris Lattner99e7d232008-10-12 04:19:49 +00001142 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001143 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001144 }
Mike Stump11289f42009-09-09 15:08:12 +00001145
Chris Lattnerc850ad62007-07-21 23:43:37 +00001146 // Check to see if the first character after the '/*' is another /. If so,
1147 // then this slash does not end the block comment, it is part of it.
1148 if (C == '/')
1149 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001150
Chris Lattner22eb9722006-06-18 05:43:12 +00001151 while (1) {
Chris Lattner6cc3e362006-10-27 04:12:35 +00001152 // Skip over all non-interesting characters until we find end of buffer or a
1153 // (probably ending) '/' character.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001154 if (CurPtr + 24 < BufferEnd) {
1155 // While not aligned to a 16-byte boundary.
1156 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1157 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001158
Chris Lattner6cc3e362006-10-27 04:12:35 +00001159 if (C == '/') goto FoundSlash;
Chris Lattneraded4a92006-10-27 04:42:31 +00001160
1161#ifdef __SSE2__
1162 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1163 '/', '/', '/', '/', '/', '/', '/', '/');
1164 while (CurPtr+16 <= BufferEnd &&
1165 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1166 CurPtr += 16;
Chris Lattner9f6604f2006-10-30 20:01:22 +00001167#elif __ALTIVEC__
1168 __vector unsigned char Slashes = {
Mike Stump11289f42009-09-09 15:08:12 +00001169 '/', '/', '/', '/', '/', '/', '/', '/',
Chris Lattner9f6604f2006-10-30 20:01:22 +00001170 '/', '/', '/', '/', '/', '/', '/', '/'
1171 };
1172 while (CurPtr+16 <= BufferEnd &&
1173 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1174 CurPtr += 16;
Mike Stump11289f42009-09-09 15:08:12 +00001175#else
Chris Lattneraded4a92006-10-27 04:42:31 +00001176 // Scan for '/' quickly. Many block comments are very large.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001177 while (CurPtr[0] != '/' &&
1178 CurPtr[1] != '/' &&
1179 CurPtr[2] != '/' &&
1180 CurPtr[3] != '/' &&
1181 CurPtr+4 < BufferEnd) {
1182 CurPtr += 4;
1183 }
Chris Lattneraded4a92006-10-27 04:42:31 +00001184#endif
Mike Stump11289f42009-09-09 15:08:12 +00001185
Chris Lattneraded4a92006-10-27 04:42:31 +00001186 // It has to be one of the bytes scanned, increment to it and read one.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001187 C = *CurPtr++;
1188 }
Mike Stump11289f42009-09-09 15:08:12 +00001189
Chris Lattneraded4a92006-10-27 04:42:31 +00001190 // Loop to scan the remainder.
Chris Lattner22eb9722006-06-18 05:43:12 +00001191 while (C != '/' && C != '\0')
1192 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001193
Chris Lattner6cc3e362006-10-27 04:12:35 +00001194 FoundSlash:
Chris Lattner22eb9722006-06-18 05:43:12 +00001195 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001196 if (CurPtr[-2] == '*') // We found the final */. We're done!
1197 break;
Mike Stump11289f42009-09-09 15:08:12 +00001198
Chris Lattner22eb9722006-06-18 05:43:12 +00001199 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +00001200 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001201 // We found the final */, though it had an escaped newline between the
1202 // * and /. We're done!
1203 break;
1204 }
1205 }
1206 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1207 // If this is a /* inside of the comment, emit a warning. Don't do this
1208 // if this is a /*/, which will end the comment. This misses cases with
1209 // embedded escaped newlines, but oh well.
Chris Lattner6d27a162008-11-22 02:02:22 +00001210 if (!isLexingRawMode())
1211 Diag(CurPtr-1, diag::warn_nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001212 }
1213 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001214 if (!isLexingRawMode())
1215 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001216 // Note: the user probably forgot a */. We could continue immediately
1217 // after the /*, but this would involve lexing a lot of what really is the
1218 // comment, which surely would confuse the parser.
Chris Lattner99e7d232008-10-12 04:19:49 +00001219 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001220
Chris Lattner99e7d232008-10-12 04:19:49 +00001221 // KeepWhitespaceMode should return this broken comment as a token. Since
1222 // it isn't a well formed comment, just return it as an 'unknown' token.
1223 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001224 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner99e7d232008-10-12 04:19:49 +00001225 return true;
1226 }
Mike Stump11289f42009-09-09 15:08:12 +00001227
Chris Lattner99e7d232008-10-12 04:19:49 +00001228 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001229 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001230 }
1231 C = *CurPtr++;
1232 }
Mike Stump11289f42009-09-09 15:08:12 +00001233
Chris Lattner87d02082010-01-18 22:35:47 +00001234 if (PP && PP->HandleComment(Result,
1235 SourceRange(getSourceLocation(BufferPtr),
1236 getSourceLocation(CurPtr)))) {
1237 BufferPtr = CurPtr;
1238 return true; // A token has to be returned.
1239 }
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001240
Chris Lattner457fc152006-07-29 06:30:25 +00001241 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +00001242 if (inKeepCommentMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001243 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattnere01e7582008-10-12 04:15:42 +00001244 return true;
Chris Lattner457fc152006-07-29 06:30:25 +00001245 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001246
1247 // It is common for the tokens immediately after a /**/ comment to be
1248 // whitespace. Instead of going through the big switch, handle it
Chris Lattner4d963442008-10-12 04:05:48 +00001249 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1250 // have already returned above with the comment as a token.
Chris Lattner22eb9722006-06-18 05:43:12 +00001251 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner146762e2007-07-20 16:59:19 +00001252 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +00001253 SkipWhitespace(Result, CurPtr+1);
Chris Lattnere01e7582008-10-12 04:15:42 +00001254 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001255 }
1256
1257 // Otherwise, just return so that the next character will be lexed as a token.
1258 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001259 Result.setFlag(Token::LeadingSpace);
Chris Lattnere01e7582008-10-12 04:15:42 +00001260 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001261}
1262
1263//===----------------------------------------------------------------------===//
1264// Primary Lexing Entry Points
1265//===----------------------------------------------------------------------===//
1266
Chris Lattner22eb9722006-06-18 05:43:12 +00001267/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1268/// uninterpreted string. This switches the lexer out of directive mode.
1269std::string Lexer::ReadToEndOfLine() {
1270 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1271 "Must be in a preprocessing directive!");
1272 std::string Result;
Chris Lattner146762e2007-07-20 16:59:19 +00001273 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001274
1275 // CurPtr - Cache BufferPtr in an automatic variable.
1276 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001277 while (1) {
1278 char Char = getAndAdvanceChar(CurPtr, Tmp);
1279 switch (Char) {
1280 default:
1281 Result += Char;
1282 break;
1283 case 0: // Null.
1284 // Found end of file?
1285 if (CurPtr-1 != BufferEnd) {
1286 // Nope, normal character, continue.
1287 Result += Char;
1288 break;
1289 }
1290 // FALL THROUGH.
1291 case '\r':
1292 case '\n':
1293 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1294 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1295 BufferPtr = CurPtr-1;
Mike Stump11289f42009-09-09 15:08:12 +00001296
Chris Lattner22eb9722006-06-18 05:43:12 +00001297 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +00001298 Lex(Tmp);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001299 assert(Tmp.is(tok::eom) && "Unexpected token!");
Mike Stump11289f42009-09-09 15:08:12 +00001300
Chris Lattner22eb9722006-06-18 05:43:12 +00001301 // Finally, we're done, return the string we found.
1302 return Result;
1303 }
1304 }
1305}
1306
1307/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1308/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +00001309/// This returns true if Result contains a token, false if PP.Lex should be
1310/// called again.
Chris Lattner146762e2007-07-20 16:59:19 +00001311bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001312 // If we hit the end of the file while parsing a preprocessor directive,
1313 // end the preprocessor directive first. The next token returned will
1314 // then be the end of file.
1315 if (ParsingPreprocessorDirective) {
1316 // Done parsing the "line".
1317 ParsingPreprocessorDirective = false;
Chris Lattnerd01e2912006-06-18 16:22:51 +00001318 // Update the location of token as well as BufferPtr.
Chris Lattnerb11c3232008-10-12 04:51:35 +00001319 FormTokenWithChars(Result, CurPtr, tok::eom);
Mike Stump11289f42009-09-09 15:08:12 +00001320
Chris Lattner457fc152006-07-29 06:30:25 +00001321 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001322 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner2183a6e2006-07-18 06:36:12 +00001323 return true; // Have a token.
Mike Stump11289f42009-09-09 15:08:12 +00001324 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00001325
Chris Lattner30a2fa12006-07-19 06:31:49 +00001326 // If we are in raw mode, return this event as an EOF token. Let the caller
1327 // that put us in raw mode handle the event.
Chris Lattner6d27a162008-11-22 02:02:22 +00001328 if (isLexingRawMode()) {
Chris Lattner8c204872006-10-14 05:19:21 +00001329 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +00001330 BufferPtr = BufferEnd;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001331 FormTokenWithChars(Result, BufferEnd, tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001332 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001333 }
Mike Stump11289f42009-09-09 15:08:12 +00001334
Douglas Gregor3545ff42009-09-21 16:56:56 +00001335 // Otherwise, check if we are code-completing, then issue diagnostics for
1336 // unterminated #if and missing newline.
Chris Lattner30a2fa12006-07-19 06:31:49 +00001337
Douglas Gregor53ad6b92009-12-02 06:49:09 +00001338 if (PP && PP->isCodeCompletionFile(FileLoc)) {
1339 // We're at the end of the file, but we've been asked to consider the
1340 // end of the file to be a code-completion token. Return the
1341 // code-completion token.
1342 Result.startToken();
1343 FormTokenWithChars(Result, CurPtr, tok::code_completion);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001344
Douglas Gregor53ad6b92009-12-02 06:49:09 +00001345 // Only do the eof -> code_completion translation once.
1346 PP->SetCodeCompletionPoint(0, 0, 0);
1347 return true;
Douglas Gregor3545ff42009-09-21 16:56:56 +00001348 }
1349
Chris Lattner30a2fa12006-07-19 06:31:49 +00001350 // If we are in a #if directive, emit an error.
1351 while (!ConditionalStack.empty()) {
Chris Lattner014156e2008-11-22 06:22:39 +00001352 PP->Diag(ConditionalStack.back().IfLoc,
1353 diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001354 ConditionalStack.pop_back();
1355 }
Mike Stump11289f42009-09-09 15:08:12 +00001356
Chris Lattner8f96d042008-04-12 05:54:25 +00001357 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1358 // a pedwarn.
1359 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Mike Stump0be88752009-04-02 02:29:42 +00001360 Diag(BufferEnd, diag::ext_no_newline_eof)
1361 << CodeModificationHint::CreateInsertion(getSourceLocation(BufferEnd),
1362 "\n");
Mike Stump11289f42009-09-09 15:08:12 +00001363
Chris Lattner22eb9722006-06-18 05:43:12 +00001364 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +00001365
1366 // Finally, let the preprocessor handle this.
Chris Lattner02b436a2007-10-17 20:41:00 +00001367 return PP->HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001368}
1369
Chris Lattner678c8802006-07-11 05:46:12 +00001370/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1371/// the specified lexer will return a tok::l_paren token, 0 if it is something
1372/// else and 2 if there are no more tokens in the buffer controlled by the
1373/// lexer.
1374unsigned Lexer::isNextPPTokenLParen() {
1375 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
Mike Stump11289f42009-09-09 15:08:12 +00001376
Chris Lattner678c8802006-07-11 05:46:12 +00001377 // Switch to 'skipping' mode. This will ensure that we can lex a token
1378 // without emitting diagnostics, disables macro expansion, and will cause EOF
1379 // to return an EOF token instead of popping the include stack.
1380 LexingRawMode = true;
Mike Stump11289f42009-09-09 15:08:12 +00001381
Chris Lattner678c8802006-07-11 05:46:12 +00001382 // Save state that can be changed while lexing so that we can restore it.
1383 const char *TmpBufferPtr = BufferPtr;
Chris Lattner40493eb2009-04-24 07:15:46 +00001384 bool inPPDirectiveMode = ParsingPreprocessorDirective;
Mike Stump11289f42009-09-09 15:08:12 +00001385
Chris Lattner146762e2007-07-20 16:59:19 +00001386 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001387 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001388 LexTokenInternal(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001389
Chris Lattner678c8802006-07-11 05:46:12 +00001390 // Restore state that may have changed.
1391 BufferPtr = TmpBufferPtr;
Chris Lattner40493eb2009-04-24 07:15:46 +00001392 ParsingPreprocessorDirective = inPPDirectiveMode;
Mike Stump11289f42009-09-09 15:08:12 +00001393
Chris Lattner678c8802006-07-11 05:46:12 +00001394 // Restore the lexer back to non-skipping mode.
1395 LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +00001396
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001397 if (Tok.is(tok::eof))
Chris Lattner678c8802006-07-11 05:46:12 +00001398 return 2;
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001399 return Tok.is(tok::l_paren);
Chris Lattner678c8802006-07-11 05:46:12 +00001400}
1401
Chris Lattner7c027ee2009-12-14 06:16:57 +00001402/// FindConflictEnd - Find the end of a version control conflict marker.
1403static const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd) {
1404 llvm::StringRef RestOfBuffer(CurPtr+7, BufferEnd-CurPtr-7);
1405 size_t Pos = RestOfBuffer.find(">>>>>>>");
1406 while (Pos != llvm::StringRef::npos) {
1407 // Must occur at start of line.
1408 if (RestOfBuffer[Pos-1] != '\r' &&
1409 RestOfBuffer[Pos-1] != '\n') {
1410 RestOfBuffer = RestOfBuffer.substr(Pos+7);
1411 continue;
1412 }
1413 return RestOfBuffer.data()+Pos;
1414 }
1415 return 0;
1416}
1417
1418/// IsStartOfConflictMarker - If the specified pointer is the start of a version
1419/// control conflict marker like '<<<<<<<', recognize it as such, emit an error
1420/// and recover nicely. This returns true if it is a conflict marker and false
1421/// if not.
1422bool Lexer::IsStartOfConflictMarker(const char *CurPtr) {
1423 // Only a conflict marker if it starts at the beginning of a line.
1424 if (CurPtr != BufferStart &&
1425 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1426 return false;
1427
1428 // Check to see if we have <<<<<<<.
1429 if (BufferEnd-CurPtr < 8 ||
1430 llvm::StringRef(CurPtr, 7) != "<<<<<<<")
1431 return false;
1432
1433 // If we have a situation where we don't care about conflict markers, ignore
1434 // it.
1435 if (IsInConflictMarker || isLexingRawMode())
1436 return false;
1437
1438 // Check to see if there is a >>>>>>> somewhere in the buffer at the start of
1439 // a line to terminate this conflict marker.
1440 if (FindConflictEnd(CurPtr+7, BufferEnd)) {
1441 // We found a match. We are really in a conflict marker.
1442 // Diagnose this, and ignore to the end of line.
1443 Diag(CurPtr, diag::err_conflict_marker);
1444 IsInConflictMarker = true;
1445
1446 // Skip ahead to the end of line. We know this exists because the
1447 // end-of-conflict marker starts with \r or \n.
1448 while (*CurPtr != '\r' && *CurPtr != '\n') {
1449 assert(CurPtr != BufferEnd && "Didn't find end of line");
1450 ++CurPtr;
1451 }
1452 BufferPtr = CurPtr;
1453 return true;
1454 }
1455
1456 // No end of conflict marker found.
1457 return false;
1458}
1459
1460
1461/// HandleEndOfConflictMarker - If this is a '=======' or '|||||||' or '>>>>>>>'
1462/// marker, then it is the end of a conflict marker. Handle it by ignoring up
1463/// until the end of the line. This returns true if it is a conflict marker and
1464/// false if not.
1465bool Lexer::HandleEndOfConflictMarker(const char *CurPtr) {
1466 // Only a conflict marker if it starts at the beginning of a line.
1467 if (CurPtr != BufferStart &&
1468 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1469 return false;
1470
1471 // If we have a situation where we don't care about conflict markers, ignore
1472 // it.
1473 if (!IsInConflictMarker || isLexingRawMode())
1474 return false;
1475
1476 // Check to see if we have the marker (7 characters in a row).
1477 for (unsigned i = 1; i != 7; ++i)
1478 if (CurPtr[i] != CurPtr[0])
1479 return false;
1480
1481 // If we do have it, search for the end of the conflict marker. This could
1482 // fail if it got skipped with a '#if 0' or something. Note that CurPtr might
1483 // be the end of conflict marker.
1484 if (const char *End = FindConflictEnd(CurPtr, BufferEnd)) {
1485 CurPtr = End;
1486
1487 // Skip ahead to the end of line.
1488 while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n')
1489 ++CurPtr;
1490
1491 BufferPtr = CurPtr;
1492
1493 // No longer in the conflict marker.
1494 IsInConflictMarker = false;
1495 return true;
1496 }
1497
1498 return false;
1499}
1500
Chris Lattner22eb9722006-06-18 05:43:12 +00001501
1502/// LexTokenInternal - This implements a simple C family lexer. It is an
1503/// extremely performance critical piece of code. This assumes that the buffer
Chris Lattner5c349382009-07-07 05:05:42 +00001504/// has a null character at the end of the file. This returns a preprocessing
1505/// token, not a normal token, as such, it is an internal interface. It assumes
1506/// that the Flags of result have been cleared before calling this.
Chris Lattner146762e2007-07-20 16:59:19 +00001507void Lexer::LexTokenInternal(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001508LexNextToken:
1509 // New token, can't need cleaning yet.
Chris Lattner146762e2007-07-20 16:59:19 +00001510 Result.clearFlag(Token::NeedsCleaning);
Chris Lattner8c204872006-10-14 05:19:21 +00001511 Result.setIdentifierInfo(0);
Mike Stump11289f42009-09-09 15:08:12 +00001512
Chris Lattner22eb9722006-06-18 05:43:12 +00001513 // CurPtr - Cache BufferPtr in an automatic variable.
1514 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001515
Chris Lattnereb54b592006-07-10 06:34:27 +00001516 // Small amounts of horizontal whitespace is very common between tokens.
1517 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1518 ++CurPtr;
1519 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1520 ++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001521
Chris Lattner4d963442008-10-12 04:05:48 +00001522 // If we are keeping whitespace and other tokens, just return what we just
1523 // skipped. The next lexer invocation will return the token after the
1524 // whitespace.
1525 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001526 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4d963442008-10-12 04:05:48 +00001527 return;
1528 }
Mike Stump11289f42009-09-09 15:08:12 +00001529
Chris Lattnereb54b592006-07-10 06:34:27 +00001530 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001531 Result.setFlag(Token::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001532 }
Mike Stump11289f42009-09-09 15:08:12 +00001533
Chris Lattner22eb9722006-06-18 05:43:12 +00001534 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
Mike Stump11289f42009-09-09 15:08:12 +00001535
Chris Lattner22eb9722006-06-18 05:43:12 +00001536 // Read a character, advancing over it.
1537 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001538 tok::TokenKind Kind;
Mike Stump11289f42009-09-09 15:08:12 +00001539
Chris Lattner22eb9722006-06-18 05:43:12 +00001540 switch (Char) {
1541 case 0: // Null.
1542 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001543 if (CurPtr-1 == BufferEnd) {
1544 // Read the PP instance variable into an automatic variable, because
1545 // LexEndOfFile will often delete 'this'.
Chris Lattner02b436a2007-10-17 20:41:00 +00001546 Preprocessor *PPCache = PP;
Chris Lattner2183a6e2006-07-18 06:36:12 +00001547 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1548 return; // Got a token to return.
Chris Lattner02b436a2007-10-17 20:41:00 +00001549 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1550 return PPCache->Lex(Result);
Chris Lattner2183a6e2006-07-18 06:36:12 +00001551 }
Mike Stump11289f42009-09-09 15:08:12 +00001552
Chris Lattner6d27a162008-11-22 02:02:22 +00001553 if (!isLexingRawMode())
1554 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner146762e2007-07-20 16:59:19 +00001555 Result.setFlag(Token::LeadingSpace);
Chris Lattner4d963442008-10-12 04:05:48 +00001556 if (SkipWhitespace(Result, CurPtr))
1557 return; // KeepWhitespaceMode
Mike Stump11289f42009-09-09 15:08:12 +00001558
Chris Lattner22eb9722006-06-18 05:43:12 +00001559 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner3dfff972009-12-17 05:29:40 +00001560
1561 case 26: // DOS & CP/M EOF: "^Z".
1562 // If we're in Microsoft extensions mode, treat this as end of file.
1563 if (Features.Microsoft) {
1564 // Read the PP instance variable into an automatic variable, because
1565 // LexEndOfFile will often delete 'this'.
1566 Preprocessor *PPCache = PP;
1567 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1568 return; // Got a token to return.
1569 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1570 return PPCache->Lex(Result);
1571 }
1572 // If Microsoft extensions are disabled, this is just random garbage.
1573 Kind = tok::unknown;
1574 break;
1575
Chris Lattner22eb9722006-06-18 05:43:12 +00001576 case '\n':
1577 case '\r':
1578 // If we are inside a preprocessor directive and we see the end of line,
1579 // we know we are done with the directive, so return an EOM token.
1580 if (ParsingPreprocessorDirective) {
1581 // Done parsing the "line".
1582 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +00001583
Chris Lattner457fc152006-07-29 06:30:25 +00001584 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001585 SetCommentRetentionState(PP->getCommentRetentionState());
Mike Stump11289f42009-09-09 15:08:12 +00001586
Chris Lattner22eb9722006-06-18 05:43:12 +00001587 // Since we consumed a newline, we are back at the start of a line.
1588 IsAtStartOfLine = true;
Mike Stump11289f42009-09-09 15:08:12 +00001589
Chris Lattnerb11c3232008-10-12 04:51:35 +00001590 Kind = tok::eom;
Chris Lattner22eb9722006-06-18 05:43:12 +00001591 break;
1592 }
1593 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001594 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001595 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001596 Result.clearFlag(Token::LeadingSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001597
Chris Lattner4d963442008-10-12 04:05:48 +00001598 if (SkipWhitespace(Result, CurPtr))
1599 return; // KeepWhitespaceMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001600 goto LexNextToken; // GCC isn't tail call eliminating.
1601 case ' ':
1602 case '\t':
1603 case '\f':
1604 case '\v':
Chris Lattnerb9b85972007-07-22 06:29:05 +00001605 SkipHorizontalWhitespace:
Chris Lattner146762e2007-07-20 16:59:19 +00001606 Result.setFlag(Token::LeadingSpace);
Chris Lattner4d963442008-10-12 04:05:48 +00001607 if (SkipWhitespace(Result, CurPtr))
1608 return; // KeepWhitespaceMode
Chris Lattnerb9b85972007-07-22 06:29:05 +00001609
1610 SkipIgnoredUnits:
1611 CurPtr = BufferPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001612
Chris Lattnerb9b85972007-07-22 06:29:05 +00001613 // If the next token is obviously a // or /* */ comment, skip it efficiently
1614 // too (without going through the big switch stmt).
Chris Lattner58827712009-01-16 22:39:25 +00001615 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1616 Features.BCPLComment) {
Chris Lattner87d02082010-01-18 22:35:47 +00001617 if (SkipBCPLComment(Result, CurPtr+2))
1618 return; // There is a token to return.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001619 goto SkipIgnoredUnits;
Chris Lattner8637abd2008-10-12 03:22:02 +00001620 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner87d02082010-01-18 22:35:47 +00001621 if (SkipBlockComment(Result, CurPtr+2))
1622 return; // There is a token to return.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001623 goto SkipIgnoredUnits;
1624 } else if (isHorizontalWhitespace(*CurPtr)) {
1625 goto SkipHorizontalWhitespace;
1626 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001627 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner3dfff972009-12-17 05:29:40 +00001628
Chris Lattner2b15cf72008-01-03 17:58:54 +00001629 // C99 6.4.4.1: Integer Constants.
1630 // C99 6.4.4.2: Floating Constants.
1631 case '0': case '1': case '2': case '3': case '4':
1632 case '5': case '6': case '7': case '8': case '9':
1633 // Notify MIOpt that we read a non-whitespace/non-comment token.
1634 MIOpt.ReadToken();
1635 return LexNumericConstant(Result, CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001636
Chris Lattner2b15cf72008-01-03 17:58:54 +00001637 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Chris Lattner371ac8a2006-07-04 07:11:10 +00001638 // Notify MIOpt that we read a non-whitespace/non-comment token.
1639 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001640 Char = getCharAndSize(CurPtr, SizeTmp);
1641
1642 // Wide string literal.
1643 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001644 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1645 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001646
1647 // Wide character constant.
1648 if (Char == '\'')
1649 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1650 // FALL THROUGH, treating L like the start of an identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001651
Chris Lattner22eb9722006-06-18 05:43:12 +00001652 // C99 6.4.2: Identifiers.
1653 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1654 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1655 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1656 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1657 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1658 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1659 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1660 case 'v': case 'w': case 'x': case 'y': case 'z':
1661 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001662 // Notify MIOpt that we read a non-whitespace/non-comment token.
1663 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001664 return LexIdentifier(Result, CurPtr);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001665
1666 case '$': // $ in identifiers.
1667 if (Features.DollarIdents) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001668 if (!isLexingRawMode())
1669 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001670 // Notify MIOpt that we read a non-whitespace/non-comment token.
1671 MIOpt.ReadToken();
1672 return LexIdentifier(Result, CurPtr);
1673 }
Mike Stump11289f42009-09-09 15:08:12 +00001674
Chris Lattnerb11c3232008-10-12 04:51:35 +00001675 Kind = tok::unknown;
Chris Lattner2b15cf72008-01-03 17:58:54 +00001676 break;
Mike Stump11289f42009-09-09 15:08:12 +00001677
Chris Lattner22eb9722006-06-18 05:43:12 +00001678 // C99 6.4.4: Character Constants.
1679 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001680 // Notify MIOpt that we read a non-whitespace/non-comment token.
1681 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001682 return LexCharConstant(Result, CurPtr);
1683
1684 // C99 6.4.5: String Literals.
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 Lattnerd3e98952006-10-06 05:22:26 +00001688 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001689
1690 // C99 6.4.6: Punctuators.
1691 case '?':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001692 Kind = tok::question;
Chris Lattner22eb9722006-06-18 05:43:12 +00001693 break;
1694 case '[':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001695 Kind = tok::l_square;
Chris Lattner22eb9722006-06-18 05:43:12 +00001696 break;
1697 case ']':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001698 Kind = tok::r_square;
Chris Lattner22eb9722006-06-18 05:43:12 +00001699 break;
1700 case '(':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001701 Kind = tok::l_paren;
Chris Lattner22eb9722006-06-18 05:43:12 +00001702 break;
1703 case ')':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001704 Kind = tok::r_paren;
Chris Lattner22eb9722006-06-18 05:43:12 +00001705 break;
1706 case '{':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001707 Kind = tok::l_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001708 break;
1709 case '}':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001710 Kind = tok::r_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001711 break;
1712 case '.':
1713 Char = getCharAndSize(CurPtr, SizeTmp);
1714 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001715 // Notify MIOpt that we read a non-whitespace/non-comment token.
1716 MIOpt.ReadToken();
1717
Chris Lattner22eb9722006-06-18 05:43:12 +00001718 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1719 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001720 Kind = tok::periodstar;
Chris Lattner22eb9722006-06-18 05:43:12 +00001721 CurPtr += SizeTmp;
1722 } else if (Char == '.' &&
1723 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001724 Kind = tok::ellipsis;
Chris Lattner22eb9722006-06-18 05:43:12 +00001725 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1726 SizeTmp2, Result);
1727 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001728 Kind = tok::period;
Chris Lattner22eb9722006-06-18 05:43:12 +00001729 }
1730 break;
1731 case '&':
1732 Char = getCharAndSize(CurPtr, SizeTmp);
1733 if (Char == '&') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001734 Kind = tok::ampamp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001735 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1736 } else if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001737 Kind = tok::ampequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001738 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1739 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001740 Kind = tok::amp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001741 }
1742 break;
Mike Stump11289f42009-09-09 15:08:12 +00001743 case '*':
Chris Lattner22eb9722006-06-18 05:43:12 +00001744 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001745 Kind = tok::starequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001746 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1747 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001748 Kind = tok::star;
Chris Lattner22eb9722006-06-18 05:43:12 +00001749 }
1750 break;
1751 case '+':
1752 Char = getCharAndSize(CurPtr, SizeTmp);
1753 if (Char == '+') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001754 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001755 Kind = tok::plusplus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001756 } else if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001757 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001758 Kind = tok::plusequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001759 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001760 Kind = tok::plus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001761 }
1762 break;
1763 case '-':
1764 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001765 if (Char == '-') { // --
Chris Lattner22eb9722006-06-18 05:43:12 +00001766 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001767 Kind = tok::minusminus;
Mike Stump11289f42009-09-09 15:08:12 +00001768 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattnerb11c3232008-10-12 04:51:35 +00001769 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00001770 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1771 SizeTmp2, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001772 Kind = tok::arrowstar;
1773 } else if (Char == '>') { // ->
Chris Lattner22eb9722006-06-18 05:43:12 +00001774 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001775 Kind = tok::arrow;
1776 } else if (Char == '=') { // -=
Chris Lattner22eb9722006-06-18 05:43:12 +00001777 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001778 Kind = tok::minusequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001779 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001780 Kind = tok::minus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001781 }
1782 break;
1783 case '~':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001784 Kind = tok::tilde;
Chris Lattner22eb9722006-06-18 05:43:12 +00001785 break;
1786 case '!':
1787 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001788 Kind = tok::exclaimequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001789 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1790 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001791 Kind = tok::exclaim;
Chris Lattner22eb9722006-06-18 05:43:12 +00001792 }
1793 break;
1794 case '/':
1795 // 6.4.9: Comments
1796 Char = getCharAndSize(CurPtr, SizeTmp);
1797 if (Char == '/') { // BCPL comment.
Chris Lattner58827712009-01-16 22:39:25 +00001798 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
1799 // want to lex this as a comment. There is one problem with this though,
1800 // that in one particular corner case, this can change the behavior of the
1801 // resultant program. For example, In "foo //**/ bar", C89 would lex
1802 // this as "foo / bar" and langauges with BCPL comments would lex it as
1803 // "foo". Check to see if the character after the second slash is a '*'.
1804 // If so, we will lex that as a "/" instead of the start of a comment.
1805 if (Features.BCPLComment ||
1806 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
1807 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner87d02082010-01-18 22:35:47 +00001808 return; // There is a token to return.
Mike Stump11289f42009-09-09 15:08:12 +00001809
Chris Lattner58827712009-01-16 22:39:25 +00001810 // It is common for the tokens immediately after a // comment to be
1811 // whitespace (indentation for the next line). Instead of going through
1812 // the big switch, handle it efficiently now.
1813 goto SkipIgnoredUnits;
1814 }
1815 }
Mike Stump11289f42009-09-09 15:08:12 +00001816
Chris Lattner58827712009-01-16 22:39:25 +00001817 if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001818 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner87d02082010-01-18 22:35:47 +00001819 return; // There is a token to return.
Chris Lattnere01e7582008-10-12 04:15:42 +00001820 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner58827712009-01-16 22:39:25 +00001821 }
Mike Stump11289f42009-09-09 15:08:12 +00001822
Chris Lattner58827712009-01-16 22:39:25 +00001823 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001824 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001825 Kind = tok::slashequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001826 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001827 Kind = tok::slash;
Chris Lattner22eb9722006-06-18 05:43:12 +00001828 }
1829 break;
1830 case '%':
1831 Char = getCharAndSize(CurPtr, SizeTmp);
1832 if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001833 Kind = tok::percentequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001834 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1835 } else if (Features.Digraphs && Char == '>') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001836 Kind = tok::r_brace; // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00001837 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1838 } else if (Features.Digraphs && Char == ':') {
1839 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001840 Char = getCharAndSize(CurPtr, SizeTmp);
1841 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001842 Kind = tok::hashhash; // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00001843 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1844 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001845 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner2b271db2006-07-15 05:41:09 +00001846 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner6d27a162008-11-22 02:02:22 +00001847 if (!isLexingRawMode())
1848 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001849 Kind = tok::hashat;
Chris Lattner2534324a2009-03-18 20:58:27 +00001850 } else { // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00001851 // We parsed a # character. If this occurs at the start of the line,
1852 // it's actually the start of a preprocessing directive. Callback to
1853 // the preprocessor to handle it.
1854 // FIXME: -fpreprocessed mode??
Chris Lattnerff96dd02009-05-13 06:10:29 +00001855 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattner2534324a2009-03-18 20:58:27 +00001856 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner02b436a2007-10-17 20:41:00 +00001857 PP->HandleDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +00001858
Chris Lattner22eb9722006-06-18 05:43:12 +00001859 // As an optimization, if the preprocessor didn't switch lexers, tail
1860 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00001861 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001862 // Start a new token. If this is a #include or something, the PP may
1863 // want us starting at the beginning of the line again. If so, set
1864 // the StartOfLine flag.
1865 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00001866 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001867 IsAtStartOfLine = false;
1868 }
1869 goto LexNextToken; // GCC isn't tail call eliminating.
1870 }
Mike Stump11289f42009-09-09 15:08:12 +00001871
Chris Lattner02b436a2007-10-17 20:41:00 +00001872 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001873 }
Mike Stump11289f42009-09-09 15:08:12 +00001874
Chris Lattner2534324a2009-03-18 20:58:27 +00001875 Kind = tok::hash;
Chris Lattner22eb9722006-06-18 05:43:12 +00001876 }
1877 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001878 Kind = tok::percent;
Chris Lattner22eb9722006-06-18 05:43:12 +00001879 }
1880 break;
1881 case '<':
1882 Char = getCharAndSize(CurPtr, SizeTmp);
1883 if (ParsingFilename) {
Chris Lattnerb40289b2009-04-17 23:56:52 +00001884 return LexAngledStringLiteral(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001885 } else if (Char == '<') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001886 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
1887 if (After == '=') {
1888 Kind = tok::lesslessequal;
1889 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1890 SizeTmp2, Result);
1891 } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) {
1892 // If this is actually a '<<<<<<<' version control conflict marker,
1893 // recognize it as such and recover nicely.
1894 goto LexNextToken;
1895 } else {
1896 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1897 Kind = tok::lessless;
1898 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001899 } else if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001900 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001901 Kind = tok::lessequal;
1902 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00001903 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001904 Kind = tok::l_square;
1905 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Chris Lattner22eb9722006-06-18 05:43:12 +00001906 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001907 Kind = tok::l_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001908 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001909 Kind = tok::less;
Chris Lattner22eb9722006-06-18 05:43:12 +00001910 }
1911 break;
1912 case '>':
1913 Char = getCharAndSize(CurPtr, SizeTmp);
1914 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001915 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001916 Kind = tok::greaterequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001917 } else if (Char == '>') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001918 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
1919 if (After == '=') {
1920 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1921 SizeTmp2, Result);
1922 Kind = tok::greatergreaterequal;
1923 } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) {
1924 // If this is '>>>>>>>' and we're in a conflict marker, ignore it.
1925 goto LexNextToken;
1926 } else {
1927 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1928 Kind = tok::greatergreater;
1929 }
1930
Chris Lattner22eb9722006-06-18 05:43:12 +00001931 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001932 Kind = tok::greater;
Chris Lattner22eb9722006-06-18 05:43:12 +00001933 }
1934 break;
1935 case '^':
1936 Char = getCharAndSize(CurPtr, SizeTmp);
1937 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001938 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001939 Kind = tok::caretequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001940 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001941 Kind = tok::caret;
Chris Lattner22eb9722006-06-18 05:43:12 +00001942 }
1943 break;
1944 case '|':
1945 Char = getCharAndSize(CurPtr, SizeTmp);
1946 if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001947 Kind = tok::pipeequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001948 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1949 } else if (Char == '|') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001950 // If this is '|||||||' and we're in a conflict marker, ignore it.
1951 if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1))
1952 goto LexNextToken;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001953 Kind = tok::pipepipe;
Chris Lattner22eb9722006-06-18 05:43:12 +00001954 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1955 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001956 Kind = tok::pipe;
Chris Lattner22eb9722006-06-18 05:43:12 +00001957 }
1958 break;
1959 case ':':
1960 Char = getCharAndSize(CurPtr, SizeTmp);
1961 if (Features.Digraphs && Char == '>') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001962 Kind = tok::r_square; // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00001963 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1964 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001965 Kind = tok::coloncolon;
Chris Lattner22eb9722006-06-18 05:43:12 +00001966 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump11289f42009-09-09 15:08:12 +00001967 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001968 Kind = tok::colon;
Chris Lattner22eb9722006-06-18 05:43:12 +00001969 }
1970 break;
1971 case ';':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001972 Kind = tok::semi;
Chris Lattner22eb9722006-06-18 05:43:12 +00001973 break;
1974 case '=':
1975 Char = getCharAndSize(CurPtr, SizeTmp);
1976 if (Char == '=') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001977 // If this is '=======' and we're in a conflict marker, ignore it.
1978 if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1))
1979 goto LexNextToken;
1980
Chris Lattnerb11c3232008-10-12 04:51:35 +00001981 Kind = tok::equalequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001982 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump11289f42009-09-09 15:08:12 +00001983 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001984 Kind = tok::equal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001985 }
1986 break;
1987 case ',':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001988 Kind = tok::comma;
Chris Lattner22eb9722006-06-18 05:43:12 +00001989 break;
1990 case '#':
1991 Char = getCharAndSize(CurPtr, SizeTmp);
1992 if (Char == '#') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001993 Kind = tok::hashhash;
Chris Lattner22eb9722006-06-18 05:43:12 +00001994 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001995 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattnerb11c3232008-10-12 04:51:35 +00001996 Kind = tok::hashat;
Chris Lattner6d27a162008-11-22 02:02:22 +00001997 if (!isLexingRawMode())
1998 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner2b271db2006-07-15 05:41:09 +00001999 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002000 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +00002001 // We parsed a # character. If this occurs at the start of the line,
2002 // it's actually the start of a preprocessing directive. Callback to
2003 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00002004 // FIXME: -fpreprocessed mode??
Chris Lattnerff96dd02009-05-13 06:10:29 +00002005 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattner2534324a2009-03-18 20:58:27 +00002006 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner02b436a2007-10-17 20:41:00 +00002007 PP->HandleDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +00002008
Chris Lattner22eb9722006-06-18 05:43:12 +00002009 // As an optimization, if the preprocessor didn't switch lexers, tail
2010 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00002011 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002012 // Start a new token. If this is a #include or something, the PP may
2013 // want us starting at the beginning of the line again. If so, set
2014 // the StartOfLine flag.
2015 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00002016 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00002017 IsAtStartOfLine = false;
2018 }
2019 goto LexNextToken; // GCC isn't tail call eliminating.
2020 }
Chris Lattner02b436a2007-10-17 20:41:00 +00002021 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002022 }
Mike Stump11289f42009-09-09 15:08:12 +00002023
Chris Lattner2534324a2009-03-18 20:58:27 +00002024 Kind = tok::hash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002025 }
2026 break;
2027
Chris Lattner2b15cf72008-01-03 17:58:54 +00002028 case '@':
2029 // Objective C support.
2030 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattnerb11c3232008-10-12 04:51:35 +00002031 Kind = tok::at;
Chris Lattner2b15cf72008-01-03 17:58:54 +00002032 else
Chris Lattnerb11c3232008-10-12 04:51:35 +00002033 Kind = tok::unknown;
Chris Lattner2b15cf72008-01-03 17:58:54 +00002034 break;
Mike Stump11289f42009-09-09 15:08:12 +00002035
Chris Lattner22eb9722006-06-18 05:43:12 +00002036 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00002037 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00002038 // FALL THROUGH.
2039 default:
Chris Lattnerb11c3232008-10-12 04:51:35 +00002040 Kind = tok::unknown;
Chris Lattner041bef82006-07-11 05:52:53 +00002041 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00002042 }
Mike Stump11289f42009-09-09 15:08:12 +00002043
Chris Lattner371ac8a2006-07-04 07:11:10 +00002044 // Notify MIOpt that we read a non-whitespace/non-comment token.
2045 MIOpt.ReadToken();
2046
Chris Lattnerd01e2912006-06-18 16:22:51 +00002047 // Update the location of token as well as BufferPtr.
Chris Lattnerb11c3232008-10-12 04:51:35 +00002048 FormTokenWithChars(Result, CurPtr, Kind);
Chris Lattner22eb9722006-06-18 05:43:12 +00002049}