blob: 4f2e29e80d44ecab54a6efa5a0e6408d9a455b0a [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner146762e2007-07-20 16:59:19 +000010// This file implements the Lexer and Token interfaces.
Chris Lattner22eb9722006-06-18 05:43:12 +000011//
12//===----------------------------------------------------------------------===//
13//
14// TODO: GCC Diagnostics emitted by the lexer:
15// PEDWARN: (form feed|vertical tab) in preprocessing directive
16//
17// Universal characters, unicode, char mapping:
18// WARNING: `%.*s' is not in NFKC
19// WARNING: `%.*s' is not in NFC
20//
21// Other:
Chris Lattner22eb9722006-06-18 05:43:12 +000022// TODO: Options to support:
23// -fexec-charset,-fwide-exec-charset
24//
25//===----------------------------------------------------------------------===//
26
27#include "clang/Lex/Lexer.h"
28#include "clang/Lex/Preprocessor.h"
Chris Lattner60f36222009-01-29 05:15:15 +000029#include "clang/Lex/LexDiagnostic.h"
Chris Lattnerdc5c0552007-07-20 16:37:10 +000030#include "clang/Basic/SourceManager.h"
Chris Lattner619c1742007-07-22 18:38:25 +000031#include "llvm/Support/Compiler.h"
Chris Lattner739e7392007-04-29 07:12:06 +000032#include "llvm/Support/MemoryBuffer.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000033#include <cctype>
Chris Lattner22eb9722006-06-18 05:43:12 +000034using namespace clang;
35
Chris Lattner3dfff972009-12-17 05:29:40 +000036static void InitCharacterInfo();
Chris Lattner22eb9722006-06-18 05:43:12 +000037
Chris Lattner4894f482007-10-07 08:47:24 +000038//===----------------------------------------------------------------------===//
39// Token Class Implementation
40//===----------------------------------------------------------------------===//
41
Mike Stump11289f42009-09-09 15:08:12 +000042/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
Chris Lattner4894f482007-10-07 08:47:24 +000043bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
Douglas Gregor90abb6d2008-12-01 21:46:47 +000044 if (IdentifierInfo *II = getIdentifierInfo())
45 return II->getObjCKeywordID() == objcKey;
46 return false;
Chris Lattner4894f482007-10-07 08:47:24 +000047}
48
49/// getObjCKeywordID - Return the ObjC keyword kind.
50tok::ObjCKeywordKind Token::getObjCKeywordID() const {
51 IdentifierInfo *specId = getIdentifierInfo();
52 return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
53}
54
Chris Lattner67671ed2007-12-13 01:59:49 +000055
Chris Lattner4894f482007-10-07 08:47:24 +000056//===----------------------------------------------------------------------===//
57// Lexer Class Implementation
58//===----------------------------------------------------------------------===//
59
Mike Stump11289f42009-09-09 15:08:12 +000060void Lexer::InitLexer(const char *BufStart, const char *BufPtr,
Chris Lattnerf76b9202009-01-17 06:55:17 +000061 const char *BufEnd) {
Chris Lattner3dfff972009-12-17 05:29:40 +000062 InitCharacterInfo();
Mike Stump11289f42009-09-09 15:08:12 +000063
Chris Lattnerf76b9202009-01-17 06:55:17 +000064 BufferStart = BufStart;
65 BufferPtr = BufPtr;
66 BufferEnd = BufEnd;
Mike Stump11289f42009-09-09 15:08:12 +000067
Chris Lattnerf76b9202009-01-17 06:55:17 +000068 assert(BufEnd[0] == 0 &&
69 "We assume that the input buffer has a null character at the end"
70 " to simplify lexing!");
Mike Stump11289f42009-09-09 15:08:12 +000071
Chris Lattnerf76b9202009-01-17 06:55:17 +000072 Is_PragmaLexer = false;
Chris Lattner7c027ee2009-12-14 06:16:57 +000073 IsInConflictMarker = false;
Douglas Gregor2436e712009-09-17 21:32:03 +000074
Chris Lattnerf76b9202009-01-17 06:55:17 +000075 // Start of the file is a start of line.
76 IsAtStartOfLine = true;
Mike Stump11289f42009-09-09 15:08:12 +000077
Chris Lattnerf76b9202009-01-17 06:55:17 +000078 // We are not after parsing a #.
79 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +000080
Chris Lattnerf76b9202009-01-17 06:55:17 +000081 // We are not after parsing #include.
82 ParsingFilename = false;
Mike Stump11289f42009-09-09 15:08:12 +000083
Chris Lattnerf76b9202009-01-17 06:55:17 +000084 // We are not in raw mode. Raw mode disables diagnostics and interpretation
85 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
86 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
87 // or otherwise skipping over tokens.
88 LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +000089
Chris Lattnerf76b9202009-01-17 06:55:17 +000090 // Default to not keeping comments.
91 ExtendedTokenMode = 0;
92}
93
Chris Lattner5965a282009-01-17 07:56:59 +000094/// Lexer constructor - Create a new lexer object for the specified buffer
95/// with the specified preprocessor managing the lexing process. This lexer
96/// assumes that the associated file buffer and Preprocessor objects will
97/// outlive it, so it doesn't take ownership of either of them.
Chris Lattner710bb872009-11-30 04:18:44 +000098Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP)
Chris Lattnerc8090892009-01-17 08:03:42 +000099 : PreprocessorLexer(&PP, FID),
100 FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
101 Features(PP.getLangOptions()) {
Mike Stump11289f42009-09-09 15:08:12 +0000102
Chris Lattner5965a282009-01-17 07:56:59 +0000103 InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
104 InputFile->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000105
Chris Lattner5965a282009-01-17 07:56:59 +0000106 // Default to keeping comments if the preprocessor wants them.
107 SetCommentRetentionState(PP.getCommentRetentionState());
108}
Chris Lattner4894f482007-10-07 08:47:24 +0000109
Chris Lattner02b436a2007-10-17 20:41:00 +0000110/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner50c90502008-10-12 01:15:46 +0000111/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
112/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner02b436a2007-10-17 20:41:00 +0000113Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattnerfcf64522009-01-17 07:42:27 +0000114 const char *BufStart, const char *BufPtr, const char *BufEnd)
Chris Lattner1abd2092009-01-17 03:48:08 +0000115 : FileLoc(fileloc), Features(features) {
Chris Lattnerf76b9202009-01-17 06:55:17 +0000116
Chris Lattnerf76b9202009-01-17 06:55:17 +0000117 InitLexer(BufStart, BufPtr, BufEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000118
Chris Lattner02b436a2007-10-17 20:41:00 +0000119 // We *are* in raw mode.
120 LexingRawMode = true;
Chris Lattner02b436a2007-10-17 20:41:00 +0000121}
122
Chris Lattner08354fe2009-01-17 07:35:14 +0000123/// Lexer constructor - Create a new raw lexer object. This object is only
124/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
125/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner710bb872009-11-30 04:18:44 +0000126Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile,
127 const SourceManager &SM, const LangOptions &features)
Chris Lattner08354fe2009-01-17 07:35:14 +0000128 : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
Chris Lattner08354fe2009-01-17 07:35:14 +0000129
Mike Stump11289f42009-09-09 15:08:12 +0000130 InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
Chris Lattner08354fe2009-01-17 07:35:14 +0000131 FromFile->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000132
Chris Lattner08354fe2009-01-17 07:35:14 +0000133 // We *are* in raw mode.
134 LexingRawMode = true;
135}
136
Chris Lattner757169b2009-01-17 08:27:52 +0000137/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
138/// _Pragma expansion. This has a variety of magic semantics that this method
139/// sets up. It returns a new'd Lexer that must be delete'd when done.
140///
141/// On entrance to this routine, TokStartLoc is a macro location which has a
142/// spelling loc that indicates the bytes to be lexed for the token and an
143/// instantiation location that indicates where all lexed tokens should be
144/// "expanded from".
145///
146/// FIXME: It would really be nice to make _Pragma just be a wrapper around a
147/// normal lexer that remaps tokens as they fly by. This would require making
148/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer
149/// interface that could handle this stuff. This would pull GetMappedTokenLoc
150/// out of the critical path of the lexer!
151///
Mike Stump11289f42009-09-09 15:08:12 +0000152Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
Chris Lattner9dc9c202009-02-15 20:52:18 +0000153 SourceLocation InstantiationLocStart,
154 SourceLocation InstantiationLocEnd,
Chris Lattner29a2a192009-01-19 06:46:35 +0000155 unsigned TokLen, Preprocessor &PP) {
Chris Lattner757169b2009-01-17 08:27:52 +0000156 SourceManager &SM = PP.getSourceManager();
Chris Lattner757169b2009-01-17 08:27:52 +0000157
158 // Create the lexer as if we were going to lex the file normally.
Chris Lattnercbc35ecb2009-01-19 07:46:45 +0000159 FileID SpellingFID = SM.getFileID(SpellingLoc);
Chris Lattner710bb872009-11-30 04:18:44 +0000160 const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID);
161 Lexer *L = new Lexer(SpellingFID, InputFile, PP);
Mike Stump11289f42009-09-09 15:08:12 +0000162
Chris Lattner757169b2009-01-17 08:27:52 +0000163 // Now that the lexer is created, change the start/end locations so that we
164 // just lex the subsection of the file that we want. This is lexing from a
165 // scratch buffer.
166 const char *StrData = SM.getCharacterData(SpellingLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000167
Chris Lattner757169b2009-01-17 08:27:52 +0000168 L->BufferPtr = StrData;
169 L->BufferEnd = StrData+TokLen;
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000170 assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
Chris Lattner757169b2009-01-17 08:27:52 +0000171
172 // Set the SourceLocation with the remapping information. This ensures that
173 // GetMappedTokenLoc will remap the tokens as they are lexed.
Chris Lattner4fa23622009-01-26 00:43:02 +0000174 L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
Chris Lattner9dc9c202009-02-15 20:52:18 +0000175 InstantiationLocStart,
176 InstantiationLocEnd, TokLen);
Mike Stump11289f42009-09-09 15:08:12 +0000177
Chris Lattner757169b2009-01-17 08:27:52 +0000178 // Ensure that the lexer thinks it is inside a directive, so that end \n will
179 // return an EOM token.
180 L->ParsingPreprocessorDirective = true;
Mike Stump11289f42009-09-09 15:08:12 +0000181
Chris Lattner757169b2009-01-17 08:27:52 +0000182 // This lexer really is for _Pragma.
183 L->Is_PragmaLexer = true;
184 return L;
185}
186
Chris Lattner02b436a2007-10-17 20:41:00 +0000187
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000188/// Stringify - Convert the specified string into a C string, with surrounding
189/// ""'s, and with escaped \ and " characters.
Chris Lattnerecc39e92006-07-15 05:23:31 +0000190std::string Lexer::Stringify(const std::string &Str, bool Charify) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000191 std::string Result = Str;
Chris Lattnerecc39e92006-07-15 05:23:31 +0000192 char Quote = Charify ? '\'' : '"';
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000193 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
Chris Lattnerecc39e92006-07-15 05:23:31 +0000194 if (Result[i] == '\\' || Result[i] == Quote) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000195 Result.insert(Result.begin()+i, '\\');
196 ++i; ++e;
197 }
198 }
Chris Lattnerecc39e92006-07-15 05:23:31 +0000199 return Result;
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000200}
201
Chris Lattner4c4a2452007-07-24 06:57:14 +0000202/// Stringify - Convert the specified string into a C string by escaping '\'
203/// and " characters. This does not add surrounding ""'s to the string.
204void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
205 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
206 if (Str[i] == '\\' || Str[i] == '"') {
207 Str.insert(Str.begin()+i, '\\');
208 ++i; ++e;
209 }
210 }
211}
212
Douglas Gregor562c1f92010-01-22 19:49:59 +0000213static bool isWhitespace(unsigned char c);
Chris Lattner22eb9722006-06-18 05:43:12 +0000214
Chris Lattner8e129c22007-10-17 21:18:47 +0000215/// MeasureTokenLength - Relex the token at the specified location and return
216/// its length in bytes in the input file. If the token needs cleaning (e.g.
217/// includes a trigraph or an escaped newline) then this count includes bytes
218/// that are part of that.
219unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
Chris Lattner184e65d2009-04-14 23:22:57 +0000220 const SourceManager &SM,
221 const LangOptions &LangOpts) {
Chris Lattner8e129c22007-10-17 21:18:47 +0000222 // TODO: this could be special cased for common tokens like identifiers, ')',
223 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
Mike Stump11289f42009-09-09 15:08:12 +0000224 // all obviously single-char tokens. This could use
Chris Lattner8e129c22007-10-17 21:18:47 +0000225 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
226 // something.
Chris Lattner4fa23622009-01-26 00:43:02 +0000227
228 // If this comes from a macro expansion, we really do want the macro name, not
229 // the token this macro expanded to.
Chris Lattnerd3817212009-01-26 22:24:27 +0000230 Loc = SM.getInstantiationLoc(Loc);
231 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
Douglas Gregore0fbb832010-03-16 00:06:06 +0000232 bool Invalid = false;
Benjamin Kramereb92dc02010-03-16 14:14:31 +0000233 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Douglas Gregore0fbb832010-03-16 00:06:06 +0000234 if (Invalid)
Douglas Gregor802b7762010-03-15 22:54:52 +0000235 return 0;
Benjamin Kramereb92dc02010-03-16 14:14:31 +0000236
237 const char *StrData = Buffer.data()+LocInfo.second;
Chris Lattner5509d532009-01-17 08:30:10 +0000238
Douglas Gregor562c1f92010-01-22 19:49:59 +0000239 if (isWhitespace(StrData[0]))
240 return 0;
241
Chris Lattner8e129c22007-10-17 21:18:47 +0000242 // Create a lexer starting at the beginning of this token.
Benjamin Kramereb92dc02010-03-16 14:14:31 +0000243 Lexer TheLexer(Loc, LangOpts, Buffer.begin(), StrData, Buffer.end());
Chris Lattnera3d4f162009-10-14 15:04:18 +0000244 TheLexer.SetCommentRetentionState(true);
Chris Lattner8e129c22007-10-17 21:18:47 +0000245 Token TheTok;
Chris Lattner50c90502008-10-12 01:15:46 +0000246 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner8e129c22007-10-17 21:18:47 +0000247 return TheTok.getLength();
248}
249
Chris Lattner22eb9722006-06-18 05:43:12 +0000250//===----------------------------------------------------------------------===//
251// Character information.
252//===----------------------------------------------------------------------===//
253
Chris Lattner22eb9722006-06-18 05:43:12 +0000254enum {
255 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
256 CHAR_VERT_WS = 0x02, // '\r', '\n'
257 CHAR_LETTER = 0x04, // a-z,A-Z
258 CHAR_NUMBER = 0x08, // 0-9
259 CHAR_UNDER = 0x10, // _
260 CHAR_PERIOD = 0x20 // .
261};
262
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000263// Statically initialize CharInfo table based on ASCII character set
264// Reference: FreeBSD 7.2 /usr/share/misc/ascii
Chris Lattner3dfff972009-12-17 05:29:40 +0000265static const unsigned char CharInfo[256] =
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000266{
267// 0 NUL 1 SOH 2 STX 3 ETX
268// 4 EOT 5 ENQ 6 ACK 7 BEL
269 0 , 0 , 0 , 0 ,
270 0 , 0 , 0 , 0 ,
271// 8 BS 9 HT 10 NL 11 VT
272//12 NP 13 CR 14 SO 15 SI
273 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS,
274 CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 ,
275//16 DLE 17 DC1 18 DC2 19 DC3
276//20 DC4 21 NAK 22 SYN 23 ETB
277 0 , 0 , 0 , 0 ,
278 0 , 0 , 0 , 0 ,
279//24 CAN 25 EM 26 SUB 27 ESC
280//28 FS 29 GS 30 RS 31 US
281 0 , 0 , 0 , 0 ,
282 0 , 0 , 0 , 0 ,
283//32 SP 33 ! 34 " 35 #
284//36 $ 37 % 38 & 39 '
285 CHAR_HORZ_WS, 0 , 0 , 0 ,
286 0 , 0 , 0 , 0 ,
287//40 ( 41 ) 42 * 43 +
288//44 , 45 - 46 . 47 /
289 0 , 0 , 0 , 0 ,
290 0 , 0 , CHAR_PERIOD , 0 ,
291//48 0 49 1 50 2 51 3
292//52 4 53 5 54 6 55 7
293 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
294 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
295//56 8 57 9 58 : 59 ;
296//60 < 61 = 62 > 63 ?
297 CHAR_NUMBER , CHAR_NUMBER , 0 , 0 ,
298 0 , 0 , 0 , 0 ,
299//64 @ 65 A 66 B 67 C
300//68 D 69 E 70 F 71 G
301 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
302 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
303//72 H 73 I 74 J 75 K
304//76 L 77 M 78 N 79 O
305 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
306 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
307//80 P 81 Q 82 R 83 S
308//84 T 85 U 86 V 87 W
309 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
310 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
311//88 X 89 Y 90 Z 91 [
312//92 \ 93 ] 94 ^ 95 _
313 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
314 0 , 0 , 0 , CHAR_UNDER ,
315//96 ` 97 a 98 b 99 c
316//100 d 101 e 102 f 103 g
317 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
318 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
319//104 h 105 i 106 j 107 k
320//108 l 109 m 110 n 111 o
321 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
322 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
323//112 p 113 q 114 r 115 s
324//116 t 117 u 118 v 119 w
325 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
326 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
327//120 x 121 y 122 z 123 {
328//124 | 125 } 126 ~ 127 DEL
329 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
330 0 , 0 , 0 , 0
331};
332
Chris Lattner3dfff972009-12-17 05:29:40 +0000333static void InitCharacterInfo() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000334 static bool isInited = false;
335 if (isInited) return;
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000336 // check the statically-initialized CharInfo table
337 assert(CHAR_HORZ_WS == CharInfo[(int)' ']);
338 assert(CHAR_HORZ_WS == CharInfo[(int)'\t']);
339 assert(CHAR_HORZ_WS == CharInfo[(int)'\f']);
340 assert(CHAR_HORZ_WS == CharInfo[(int)'\v']);
341 assert(CHAR_VERT_WS == CharInfo[(int)'\n']);
342 assert(CHAR_VERT_WS == CharInfo[(int)'\r']);
343 assert(CHAR_UNDER == CharInfo[(int)'_']);
344 assert(CHAR_PERIOD == CharInfo[(int)'.']);
345 for (unsigned i = 'a'; i <= 'z'; ++i) {
346 assert(CHAR_LETTER == CharInfo[i]);
347 assert(CHAR_LETTER == CharInfo[i+'A'-'a']);
348 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000349 for (unsigned i = '0'; i <= '9'; ++i)
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000350 assert(CHAR_NUMBER == CharInfo[i]);
Steve Naroff04bc0182009-12-08 16:38:12 +0000351
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000352 isInited = true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000353}
354
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000355
Chris Lattner22eb9722006-06-18 05:43:12 +0000356/// isIdentifierBody - Return true if this is the body character of an
357/// identifier, which is [a-zA-Z0-9_].
358static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000359 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000360}
361
362/// isHorizontalWhitespace - Return true if this character is horizontal
363/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
364static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000365 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000366}
367
368/// isWhitespace - Return true if this character is horizontal or vertical
369/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
370/// for '\0'.
371static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000372 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000373}
374
375/// isNumberBody - Return true if this is the body character of an
376/// preprocessing number, which is [a-zA-Z0-9_.].
377static inline bool isNumberBody(unsigned char c) {
Mike Stump11289f42009-09-09 15:08:12 +0000378 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000379 true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000380}
381
Chris Lattnerd01e2912006-06-18 16:22:51 +0000382
Chris Lattner22eb9722006-06-18 05:43:12 +0000383//===----------------------------------------------------------------------===//
384// Diagnostics forwarding code.
385//===----------------------------------------------------------------------===//
386
Chris Lattner619c1742007-07-22 18:38:25 +0000387/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
388/// lexer buffer was all instantiated at a single point, perform the mapping.
389/// This is currently only used for _Pragma implementation, so it is the slow
390/// path of the hot getSourceLocation method. Do not allow it to be inlined.
Benjamin Kramer5e738282009-11-14 16:36:57 +0000391static DISABLE_INLINE SourceLocation GetMappedTokenLoc(Preprocessor &PP,
392 SourceLocation FileLoc,
393 unsigned CharNo,
394 unsigned TokLen);
Chris Lattner619c1742007-07-22 18:38:25 +0000395static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
396 SourceLocation FileLoc,
Chris Lattner4fa23622009-01-26 00:43:02 +0000397 unsigned CharNo, unsigned TokLen) {
Chris Lattner9dc9c202009-02-15 20:52:18 +0000398 assert(FileLoc.isMacroID() && "Must be an instantiation");
Mike Stump11289f42009-09-09 15:08:12 +0000399
Chris Lattner619c1742007-07-22 18:38:25 +0000400 // Otherwise, we're lexing "mapped tokens". This is used for things like
401 // _Pragma handling. Combine the instantiation location of FileLoc with the
Chris Lattner53e384f2009-01-16 07:00:02 +0000402 // spelling location.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000403 SourceManager &SM = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000404
Chris Lattner8a425862009-01-16 07:36:28 +0000405 // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
Chris Lattner53e384f2009-01-16 07:00:02 +0000406 // characters come from spelling(FileLoc)+Offset.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000407 SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
Chris Lattner29a2a192009-01-19 06:46:35 +0000408 SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
Mike Stump11289f42009-09-09 15:08:12 +0000409
Chris Lattner9dc9c202009-02-15 20:52:18 +0000410 // Figure out the expansion loc range, which is the range covered by the
411 // original _Pragma(...) sequence.
412 std::pair<SourceLocation,SourceLocation> II =
413 SM.getImmediateInstantiationRange(FileLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000414
Chris Lattner9dc9c202009-02-15 20:52:18 +0000415 return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
Chris Lattner619c1742007-07-22 18:38:25 +0000416}
417
Chris Lattner22eb9722006-06-18 05:43:12 +0000418/// getSourceLocation - Return a source location identifier for the specified
419/// offset in the current file.
Chris Lattner4fa23622009-01-26 00:43:02 +0000420SourceLocation Lexer::getSourceLocation(const char *Loc,
421 unsigned TokLen) const {
Chris Lattner5d1c0272007-07-22 18:44:36 +0000422 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Chris Lattner4cca5ba2006-07-02 20:05:54 +0000423 "Location out of range for this buffer!");
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000424
425 // In the normal case, we're just lexing from a simple file buffer, return
426 // the file id from FileLoc with the offset specified.
Chris Lattner5d1c0272007-07-22 18:44:36 +0000427 unsigned CharNo = Loc-BufferStart;
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000428 if (FileLoc.isFileID())
Chris Lattner29a2a192009-01-19 06:46:35 +0000429 return FileLoc.getFileLocWithOffset(CharNo);
Mike Stump11289f42009-09-09 15:08:12 +0000430
Chris Lattnerd32480d2009-01-17 06:22:33 +0000431 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
432 // tokens are lexed from where the _Pragma was defined.
Chris Lattner02b436a2007-10-17 20:41:00 +0000433 assert(PP && "This doesn't work on raw lexers");
Chris Lattner4fa23622009-01-26 00:43:02 +0000434 return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
Chris Lattner22eb9722006-06-18 05:43:12 +0000435}
436
Chris Lattner22eb9722006-06-18 05:43:12 +0000437/// Diag - Forwarding function for diagnostics. This translate a source
438/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner427c9c12008-11-22 00:59:29 +0000439DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner907dfe92008-11-18 07:59:24 +0000440 return PP->Diag(getSourceLocation(Loc), DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000441}
442
443//===----------------------------------------------------------------------===//
444// Trigraph and Escaped Newline Handling Code.
445//===----------------------------------------------------------------------===//
446
447/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
448/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
449static char GetTrigraphCharForLetter(char Letter) {
450 switch (Letter) {
451 default: return 0;
452 case '=': return '#';
453 case ')': return ']';
454 case '(': return '[';
455 case '!': return '|';
456 case '\'': return '^';
457 case '>': return '}';
458 case '/': return '\\';
459 case '<': return '{';
460 case '-': return '~';
461 }
462}
463
464/// DecodeTrigraphChar - If the specified character is a legal trigraph when
465/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
466/// return the result character. Finally, emit a warning about trigraph use
467/// whether trigraphs are enabled or not.
468static char DecodeTrigraphChar(const char *CP, Lexer *L) {
469 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner907dfe92008-11-18 07:59:24 +0000470 if (!Res || !L) return Res;
Mike Stump11289f42009-09-09 15:08:12 +0000471
Chris Lattner907dfe92008-11-18 07:59:24 +0000472 if (!L->getFeatures().Trigraphs) {
Chris Lattner6d27a162008-11-22 02:02:22 +0000473 if (!L->isLexingRawMode())
474 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner907dfe92008-11-18 07:59:24 +0000475 return 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000476 }
Mike Stump11289f42009-09-09 15:08:12 +0000477
Chris Lattner6d27a162008-11-22 02:02:22 +0000478 if (!L->isLexingRawMode())
479 L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
Chris Lattner22eb9722006-06-18 05:43:12 +0000480 return Res;
481}
482
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000483/// getEscapedNewLineSize - Return the size of the specified escaped newline,
484/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a
Mike Stump11289f42009-09-09 15:08:12 +0000485/// trigraph equivalent on entry to this function.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000486unsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
487 unsigned Size = 0;
488 while (isWhitespace(Ptr[Size])) {
489 ++Size;
Mike Stump11289f42009-09-09 15:08:12 +0000490
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000491 if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
492 continue;
493
494 // If this is a \r\n or \n\r, skip the other half.
495 if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
496 Ptr[Size-1] != Ptr[Size])
497 ++Size;
Mike Stump11289f42009-09-09 15:08:12 +0000498
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000499 return Size;
Mike Stump11289f42009-09-09 15:08:12 +0000500 }
501
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000502 // Not an escaped newline, must be a \t or something else.
503 return 0;
504}
505
Chris Lattner38b2cde2009-04-18 22:27:02 +0000506/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
507/// them), skip over them and return the first non-escaped-newline found,
508/// otherwise return P.
509const char *Lexer::SkipEscapedNewLines(const char *P) {
510 while (1) {
511 const char *AfterEscape;
512 if (*P == '\\') {
513 AfterEscape = P+1;
514 } else if (*P == '?') {
515 // If not a trigraph for escape, bail out.
516 if (P[1] != '?' || P[2] != '/')
517 return P;
518 AfterEscape = P+3;
519 } else {
520 return P;
521 }
Mike Stump11289f42009-09-09 15:08:12 +0000522
Chris Lattner38b2cde2009-04-18 22:27:02 +0000523 unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
524 if (NewLineSize == 0) return P;
525 P = AfterEscape+NewLineSize;
526 }
527}
528
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000529
Chris Lattner22eb9722006-06-18 05:43:12 +0000530/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
531/// get its size, and return it. This is tricky in several cases:
532/// 1. If currently at the start of a trigraph, we warn about the trigraph,
533/// then either return the trigraph (skipping 3 chars) or the '?',
534/// depending on whether trigraphs are enabled or not.
535/// 2. If this is an escaped newline (potentially with whitespace between
536/// the backslash and newline), implicitly skip the newline and return
537/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000538/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000539///
540/// This handles the slow/uncommon case of the getCharAndSize method. Here we
541/// know that we can accumulate into Size, and that we have already incremented
542/// Ptr by Size bytes.
543///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000544/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
545/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000546///
547char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattner146762e2007-07-20 16:59:19 +0000548 Token *Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000549 // If we have a slash, look for an escaped newline.
550 if (Ptr[0] == '\\') {
551 ++Size;
552 ++Ptr;
553Slash:
554 // Common case, backslash-char where the char is not whitespace.
555 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump11289f42009-09-09 15:08:12 +0000556
Chris Lattnerc1835952009-06-23 05:15:06 +0000557 // See if we have optional whitespace characters between the slash and
558 // newline.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000559 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
560 // Remember that this token needs to be cleaned.
561 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000562
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000563 // Warn if there was whitespace between the backslash and newline.
Chris Lattnerc1835952009-06-23 05:15:06 +0000564 if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000565 Diag(Ptr, diag::backslash_newline_space);
Mike Stump11289f42009-09-09 15:08:12 +0000566
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000567 // Found backslash<whitespace><newline>. Parse the char after it.
568 Size += EscapedNewLineSize;
569 Ptr += EscapedNewLineSize;
570 // Use slow version to accumulate a correct size field.
571 return getCharAndSizeSlow(Ptr, Size, Tok);
572 }
Mike Stump11289f42009-09-09 15:08:12 +0000573
Chris Lattner22eb9722006-06-18 05:43:12 +0000574 // Otherwise, this is not an escaped newline, just return the slash.
575 return '\\';
576 }
Mike Stump11289f42009-09-09 15:08:12 +0000577
Chris Lattner22eb9722006-06-18 05:43:12 +0000578 // If this is a trigraph, process it.
579 if (Ptr[0] == '?' && Ptr[1] == '?') {
580 // If this is actually a legal trigraph (not something like "??x"), emit
581 // a trigraph warning. If so, and if trigraphs are enabled, return it.
582 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
583 // Remember that this token needs to be cleaned.
Chris Lattner146762e2007-07-20 16:59:19 +0000584 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000585
586 Ptr += 3;
587 Size += 3;
588 if (C == '\\') goto Slash;
589 return C;
590 }
591 }
Mike Stump11289f42009-09-09 15:08:12 +0000592
Chris Lattner22eb9722006-06-18 05:43:12 +0000593 // If this is neither, return a single character.
594 ++Size;
595 return *Ptr;
596}
597
Chris Lattnerd01e2912006-06-18 16:22:51 +0000598
Chris Lattner22eb9722006-06-18 05:43:12 +0000599/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
600/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
601/// and that we have already incremented Ptr by Size bytes.
602///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000603/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
604/// be updated to match.
605char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000606 const LangOptions &Features) {
607 // If we have a slash, look for an escaped newline.
608 if (Ptr[0] == '\\') {
609 ++Size;
610 ++Ptr;
611Slash:
612 // Common case, backslash-char where the char is not whitespace.
613 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump11289f42009-09-09 15:08:12 +0000614
Chris Lattner22eb9722006-06-18 05:43:12 +0000615 // See if we have optional whitespace characters followed by a newline.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000616 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
617 // Found backslash<whitespace><newline>. Parse the char after it.
618 Size += EscapedNewLineSize;
619 Ptr += EscapedNewLineSize;
Mike Stump11289f42009-09-09 15:08:12 +0000620
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000621 // Use slow version to accumulate a correct size field.
622 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
623 }
Mike Stump11289f42009-09-09 15:08:12 +0000624
Chris Lattner22eb9722006-06-18 05:43:12 +0000625 // Otherwise, this is not an escaped newline, just return the slash.
626 return '\\';
627 }
Mike Stump11289f42009-09-09 15:08:12 +0000628
Chris Lattner22eb9722006-06-18 05:43:12 +0000629 // If this is a trigraph, process it.
630 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
631 // If this is actually a legal trigraph (not something like "??x"), return
632 // it.
633 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
634 Ptr += 3;
635 Size += 3;
636 if (C == '\\') goto Slash;
637 return C;
638 }
639 }
Mike Stump11289f42009-09-09 15:08:12 +0000640
Chris Lattner22eb9722006-06-18 05:43:12 +0000641 // If this is neither, return a single character.
642 ++Size;
643 return *Ptr;
644}
645
Chris Lattner22eb9722006-06-18 05:43:12 +0000646//===----------------------------------------------------------------------===//
647// Helper methods for lexing.
648//===----------------------------------------------------------------------===//
649
Chris Lattner146762e2007-07-20 16:59:19 +0000650void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000651 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
652 unsigned Size;
653 unsigned char C = *CurPtr++;
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000654 while (isIdentifierBody(C))
Chris Lattner22eb9722006-06-18 05:43:12 +0000655 C = *CurPtr++;
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000656
Chris Lattner22eb9722006-06-18 05:43:12 +0000657 --CurPtr; // Back up over the skipped character.
658
659 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
660 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000661 // FIXME: UCNs.
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000662 //
663 // TODO: Could merge these checks into a CharInfo flag to make the comparison
664 // cheaper
Chris Lattner22eb9722006-06-18 05:43:12 +0000665 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
666FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000667 const char *IdStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000668 FormTokenWithChars(Result, CurPtr, tok::identifier);
Mike Stump11289f42009-09-09 15:08:12 +0000669
Chris Lattner0f1f5052006-07-20 04:16:23 +0000670 // If we are in raw mode, return this identifier raw. There is no need to
671 // look up identifier information or attempt to macro expand it.
672 if (LexingRawMode) return;
Mike Stump11289f42009-09-09 15:08:12 +0000673
Chris Lattnercefc7682006-07-08 08:28:12 +0000674 // Fill in Result.IdentifierInfo, looking up the identifier in the
675 // identifier table.
Chris Lattner8256b972009-01-21 07:45:14 +0000676 IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
Mike Stump11289f42009-09-09 15:08:12 +0000677
Chris Lattner1f6c7fe2009-01-23 18:35:48 +0000678 // Change the kind of this identifier to the appropriate token kind, e.g.
679 // turning "for" into a keyword.
680 Result.setKind(II->getTokenID());
Mike Stump11289f42009-09-09 15:08:12 +0000681
Chris Lattnerc5a00062006-06-18 16:41:01 +0000682 // Finally, now that we know we have an identifier, pass this off to the
683 // preprocessor, which may macro expand it or something.
Chris Lattner8256b972009-01-21 07:45:14 +0000684 if (II->isHandleIdentifierCase())
Chris Lattnerad89ec02009-01-21 07:43:11 +0000685 PP->HandleIdentifier(Result);
686 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000687 }
Mike Stump11289f42009-09-09 15:08:12 +0000688
Chris Lattner22eb9722006-06-18 05:43:12 +0000689 // Otherwise, $,\,? in identifier found. Enter slower path.
Mike Stump11289f42009-09-09 15:08:12 +0000690
Chris Lattner22eb9722006-06-18 05:43:12 +0000691 C = getCharAndSize(CurPtr, Size);
692 while (1) {
693 if (C == '$') {
694 // If we hit a $ and they are not supported in identifiers, we are done.
695 if (!Features.DollarIdents) goto FinishIdentifier;
Mike Stump11289f42009-09-09 15:08:12 +0000696
Chris Lattner22eb9722006-06-18 05:43:12 +0000697 // Otherwise, emit a diagnostic and continue.
Chris Lattner6d27a162008-11-22 02:02:22 +0000698 if (!isLexingRawMode())
699 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000700 CurPtr = ConsumeChar(CurPtr, Size, Result);
701 C = getCharAndSize(CurPtr, Size);
702 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000703 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000704 // Found end of identifier.
705 goto FinishIdentifier;
706 }
707
708 // Otherwise, this character is good, consume it.
709 CurPtr = ConsumeChar(CurPtr, Size, Result);
710
711 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000712 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000713 CurPtr = ConsumeChar(CurPtr, Size, Result);
714 C = getCharAndSize(CurPtr, Size);
715 }
716 }
717}
718
719
Nate Begeman5eee9332008-04-14 02:26:39 +0000720/// LexNumericConstant - Lex the remainder of a integer or floating point
Chris Lattner22eb9722006-06-18 05:43:12 +0000721/// constant. From[-1] is the first character lexed. Return the end of the
722/// constant.
Chris Lattner146762e2007-07-20 16:59:19 +0000723void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000724 unsigned Size;
725 char C = getCharAndSize(CurPtr, Size);
726 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000727 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000728 CurPtr = ConsumeChar(CurPtr, Size, Result);
729 PrevCh = C;
730 C = getCharAndSize(CurPtr, Size);
731 }
Mike Stump11289f42009-09-09 15:08:12 +0000732
Chris Lattner22eb9722006-06-18 05:43:12 +0000733 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
734 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
735 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
736
737 // If we have a hex FP constant, continue.
Alexis Hunt91b78382010-01-10 23:37:56 +0000738 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
739 (!PP || !PP->getLangOptions().CPlusPlus0x))
Chris Lattner22eb9722006-06-18 05:43:12 +0000740 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
Mike Stump11289f42009-09-09 15:08:12 +0000741
Chris Lattnerd01e2912006-06-18 16:22:51 +0000742 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000743 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000744 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000745 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000746}
747
748/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
749/// either " or L".
Chris Lattner4d963442008-10-12 04:05:48 +0000750void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000751 const char *NulCharacter = 0; // Does this string contain the \0 character?
Mike Stump11289f42009-09-09 15:08:12 +0000752
Chris Lattner22eb9722006-06-18 05:43:12 +0000753 char C = getAndAdvanceChar(CurPtr, Result);
754 while (C != '"') {
755 // Skip escaped characters.
Douglas Gregorfe4a4102010-05-30 22:59:50 +0000756 bool Escaped = false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000757 if (C == '\\') {
758 // Skip the escaped character.
759 C = getAndAdvanceChar(CurPtr, Result);
Douglas Gregorfe4a4102010-05-30 22:59:50 +0000760 Escaped = true;
761 }
762
763 if ((!Escaped && (C == '\n' || C == '\r')) || // Newline.
764 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerd14705b2009-03-18 21:10:12 +0000765 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +0000766 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattnerb11c3232008-10-12 04:51:35 +0000767 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000768 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000769 } else if (C == 0) {
770 NulCharacter = CurPtr-1;
771 }
Douglas Gregorfe4a4102010-05-30 22:59:50 +0000772
Chris Lattner22eb9722006-06-18 05:43:12 +0000773 C = getAndAdvanceChar(CurPtr, Result);
774 }
Mike Stump11289f42009-09-09 15:08:12 +0000775
Chris Lattner5a78a022006-07-20 06:02:19 +0000776 // If a nul character existed in the string, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +0000777 if (NulCharacter && !isLexingRawMode())
778 Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000779
Chris Lattnerd01e2912006-06-18 16:22:51 +0000780 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000781 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000782 FormTokenWithChars(Result, CurPtr,
783 Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000784 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000785}
786
787/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
788/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattner146762e2007-07-20 16:59:19 +0000789void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000790 const char *NulCharacter = 0; // Does this string contain the \0 character?
Chris Lattnerb40289b2009-04-17 23:56:52 +0000791 const char *AfterLessPos = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000792 char C = getAndAdvanceChar(CurPtr, Result);
793 while (C != '>') {
794 // Skip escaped characters.
795 if (C == '\\') {
796 // Skip the escaped character.
797 C = getAndAdvanceChar(CurPtr, Result);
798 } else if (C == '\n' || C == '\r' || // Newline.
799 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerb40289b2009-04-17 23:56:52 +0000800 // If the filename is unterminated, then it must just be a lone <
801 // character. Return this as such.
802 FormTokenWithChars(Result, AfterLessPos, tok::less);
Chris Lattner5a78a022006-07-20 06:02:19 +0000803 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000804 } else if (C == 0) {
805 NulCharacter = CurPtr-1;
806 }
807 C = getAndAdvanceChar(CurPtr, Result);
808 }
Mike Stump11289f42009-09-09 15:08:12 +0000809
Chris Lattner5a78a022006-07-20 06:02:19 +0000810 // If a nul character existed in the string, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +0000811 if (NulCharacter && !isLexingRawMode())
812 Diag(NulCharacter, diag::null_in_string);
Mike Stump11289f42009-09-09 15:08:12 +0000813
Chris Lattnerd01e2912006-06-18 16:22:51 +0000814 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000815 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000816 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000817 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000818}
819
820
821/// LexCharConstant - Lex the remainder of a character constant, after having
822/// lexed either ' or L'.
Chris Lattner146762e2007-07-20 16:59:19 +0000823void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000824 const char *NulCharacter = 0; // Does this character contain the \0 character?
825
826 // Handle the common case of 'x' and '\y' efficiently.
827 char C = getAndAdvanceChar(CurPtr, Result);
828 if (C == '\'') {
Chris Lattnerd14705b2009-03-18 21:10:12 +0000829 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +0000830 Diag(BufferPtr, diag::err_empty_character);
Chris Lattnerb11c3232008-10-12 04:51:35 +0000831 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000832 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000833 } else if (C == '\\') {
834 // Skip the escaped character.
835 // FIXME: UCN's.
836 C = getAndAdvanceChar(CurPtr, Result);
837 }
Mike Stump11289f42009-09-09 15:08:12 +0000838
Chris Lattner22eb9722006-06-18 05:43:12 +0000839 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
840 ++CurPtr;
841 } else {
842 // Fall back on generic code for embedded nulls, newlines, wide chars.
843 do {
844 // Skip escaped characters.
845 if (C == '\\') {
846 // Skip the escaped character.
847 C = getAndAdvanceChar(CurPtr, Result);
848 } else if (C == '\n' || C == '\r' || // Newline.
849 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerd14705b2009-03-18 21:10:12 +0000850 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +0000851 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattnerb11c3232008-10-12 04:51:35 +0000852 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000853 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000854 } else if (C == 0) {
855 NulCharacter = CurPtr-1;
856 }
857 C = getAndAdvanceChar(CurPtr, Result);
858 } while (C != '\'');
859 }
Mike Stump11289f42009-09-09 15:08:12 +0000860
Chris Lattner6d27a162008-11-22 02:02:22 +0000861 if (NulCharacter && !isLexingRawMode())
862 Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000863
Chris Lattnerd01e2912006-06-18 16:22:51 +0000864 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000865 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000866 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000867 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000868}
869
870/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
871/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattner4d963442008-10-12 04:05:48 +0000872///
873/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
874///
875bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000876 // Whitespace - Skip it, then return the token after the whitespace.
877 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
878 while (1) {
879 // Skip horizontal whitespace very aggressively.
880 while (isHorizontalWhitespace(Char))
881 Char = *++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000882
Daniel Dunbar5c4cc092008-11-25 00:20:22 +0000883 // Otherwise if we have something other than whitespace, we're done.
Chris Lattner22eb9722006-06-18 05:43:12 +0000884 if (Char != '\n' && Char != '\r')
885 break;
Mike Stump11289f42009-09-09 15:08:12 +0000886
Chris Lattner22eb9722006-06-18 05:43:12 +0000887 if (ParsingPreprocessorDirective) {
888 // End of preprocessor directive line, let LexTokenInternal handle this.
889 BufferPtr = CurPtr;
Chris Lattner4d963442008-10-12 04:05:48 +0000890 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000891 }
Mike Stump11289f42009-09-09 15:08:12 +0000892
Chris Lattner22eb9722006-06-18 05:43:12 +0000893 // ok, but handle newline.
894 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +0000895 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000896 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +0000897 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000898 Char = *++CurPtr;
899 }
900
901 // If this isn't immediately after a newline, there is leading space.
902 char PrevChar = CurPtr[-1];
903 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattner146762e2007-07-20 16:59:19 +0000904 Result.setFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000905
Chris Lattner4d963442008-10-12 04:05:48 +0000906 // If the client wants us to return whitespace, return it now.
907 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +0000908 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4d963442008-10-12 04:05:48 +0000909 return true;
910 }
Mike Stump11289f42009-09-09 15:08:12 +0000911
Chris Lattner22eb9722006-06-18 05:43:12 +0000912 BufferPtr = CurPtr;
Chris Lattner4d963442008-10-12 04:05:48 +0000913 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000914}
915
916// SkipBCPLComment - We have just read the // characters from input. Skip until
917// we find the newline character thats terminate the comment. Then update
Chris Lattner87d02082010-01-18 22:35:47 +0000918/// BufferPtr and return.
919///
920/// If we're in KeepCommentMode or any CommentHandler has inserted
921/// some tokens, this will store the first token and return true.
Chris Lattner146762e2007-07-20 16:59:19 +0000922bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000923 // If BCPL comments aren't explicitly enabled for this language, emit an
924 // extension warning.
Chris Lattner6d27a162008-11-22 02:02:22 +0000925 if (!Features.BCPLComment && !isLexingRawMode()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000926 Diag(BufferPtr, diag::ext_bcpl_comment);
Mike Stump11289f42009-09-09 15:08:12 +0000927
Chris Lattner22eb9722006-06-18 05:43:12 +0000928 // Mark them enabled so we only emit one warning for this translation
929 // unit.
930 Features.BCPLComment = true;
931 }
Mike Stump11289f42009-09-09 15:08:12 +0000932
Chris Lattner22eb9722006-06-18 05:43:12 +0000933 // Scan over the body of the comment. The common case, when scanning, is that
934 // the comment contains normal ascii characters with nothing interesting in
935 // them. As such, optimize for this case with the inner loop.
936 char C;
937 do {
938 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +0000939 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
940 // If we find a \n character, scan backwards, checking to see if it's an
941 // escaped newline, like we do for block comments.
Mike Stump11289f42009-09-09 15:08:12 +0000942
Chris Lattner22eb9722006-06-18 05:43:12 +0000943 // Skip over characters in the fast loop.
944 while (C != 0 && // Potentially EOF.
945 C != '\\' && // Potentially escaped newline.
946 C != '?' && // Potentially trigraph.
947 C != '\n' && C != '\r') // Newline or DOS-style newline.
948 C = *++CurPtr;
949
950 // If this is a newline, we're done.
951 if (C == '\n' || C == '\r')
952 break; // Found the newline? Break out!
Mike Stump11289f42009-09-09 15:08:12 +0000953
Chris Lattner22eb9722006-06-18 05:43:12 +0000954 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnere141a9e2008-12-12 07:34:39 +0000955 // properly decode the character. Read it in raw mode to avoid emitting
956 // diagnostics about things like trigraphs. If we see an escaped newline,
957 // we'll handle it below.
Chris Lattner22eb9722006-06-18 05:43:12 +0000958 const char *OldPtr = CurPtr;
Chris Lattnere141a9e2008-12-12 07:34:39 +0000959 bool OldRawMode = isLexingRawMode();
960 LexingRawMode = true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000961 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnere141a9e2008-12-12 07:34:39 +0000962 LexingRawMode = OldRawMode;
Chris Lattnerecdaf402009-04-05 00:26:41 +0000963
964 // If the char that we finally got was a \n, then we must have had something
965 // like \<newline><newline>. We don't want to have consumed the second
966 // newline, we want CurPtr, to end up pointing to it down below.
967 if (C == '\n' || C == '\r') {
968 --CurPtr;
969 C = 'x'; // doesn't matter what this is.
970 }
Mike Stump11289f42009-09-09 15:08:12 +0000971
Chris Lattner22eb9722006-06-18 05:43:12 +0000972 // If we read multiple characters, and one of those characters was a \r or
Chris Lattnerff591e22007-06-09 06:07:22 +0000973 // \n, then we had an escaped newline within the comment. Emit diagnostic
974 // unless the next line is also a // comment.
975 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000976 for (; OldPtr != CurPtr; ++OldPtr)
977 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnerff591e22007-06-09 06:07:22 +0000978 // Okay, we found a // comment that ends in a newline, if the next
979 // line is also a // comment, but has spaces, don't emit a diagnostic.
980 if (isspace(C)) {
981 const char *ForwardPtr = CurPtr;
982 while (isspace(*ForwardPtr)) // Skip whitespace.
983 ++ForwardPtr;
984 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
985 break;
986 }
Mike Stump11289f42009-09-09 15:08:12 +0000987
Chris Lattner6d27a162008-11-22 02:02:22 +0000988 if (!isLexingRawMode())
989 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000990 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000991 }
992 }
Mike Stump11289f42009-09-09 15:08:12 +0000993
Chris Lattner457fc152006-07-29 06:30:25 +0000994 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
Chris Lattner22eb9722006-06-18 05:43:12 +0000995 } while (C != '\n' && C != '\r');
996
Chris Lattner93ddf802010-02-03 21:06:21 +0000997 // Found but did not consume the newline. Notify comment handlers about the
998 // comment unless we're in a #if 0 block.
999 if (PP && !isLexingRawMode() &&
1000 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
1001 getSourceLocation(CurPtr)))) {
Chris Lattner87d02082010-01-18 22:35:47 +00001002 BufferPtr = CurPtr;
1003 return true; // A token has to be returned.
1004 }
Mike Stump11289f42009-09-09 15:08:12 +00001005
Chris Lattner457fc152006-07-29 06:30:25 +00001006 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +00001007 if (inKeepCommentMode())
Chris Lattner457fc152006-07-29 06:30:25 +00001008 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001009
1010 // If we are inside a preprocessor directive and we see the end of line,
1011 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +00001012 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001013 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001014 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001015 }
Mike Stump11289f42009-09-09 15:08:12 +00001016
Chris Lattner22eb9722006-06-18 05:43:12 +00001017 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner87e97ea2008-10-12 00:23:07 +00001018 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattner4d963442008-10-12 04:05:48 +00001019 // contribute to another token), it isn't needed for correctness. Note that
1020 // this is ok even in KeepWhitespaceMode, because we would have returned the
1021 /// comment above in that mode.
Chris Lattner22eb9722006-06-18 05:43:12 +00001022 ++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001023
Chris Lattner22eb9722006-06-18 05:43:12 +00001024 // The next returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001025 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001026 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001027 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001028 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001029 return false;
Chris Lattner457fc152006-07-29 06:30:25 +00001030}
Chris Lattner22eb9722006-06-18 05:43:12 +00001031
Chris Lattner457fc152006-07-29 06:30:25 +00001032/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
1033/// an appropriate way and return it.
Chris Lattner146762e2007-07-20 16:59:19 +00001034bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001035 // If we're not in a preprocessor directive, just return the // comment
1036 // directly.
1037 FormTokenWithChars(Result, CurPtr, tok::comment);
Mike Stump11289f42009-09-09 15:08:12 +00001038
Chris Lattnerb11c3232008-10-12 04:51:35 +00001039 if (!ParsingPreprocessorDirective)
1040 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001041
Chris Lattnerb11c3232008-10-12 04:51:35 +00001042 // If this BCPL-style comment is in a macro definition, transmogrify it into
1043 // a C-style block comment.
Douglas Gregordc970f02010-03-16 22:30:13 +00001044 bool Invalid = false;
1045 std::string Spelling = PP->getSpelling(Result, &Invalid);
1046 if (Invalid)
1047 return true;
1048
Chris Lattnerb11c3232008-10-12 04:51:35 +00001049 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
1050 Spelling[1] = '*'; // Change prefix to "/*".
1051 Spelling += "*/"; // add suffix.
Mike Stump11289f42009-09-09 15:08:12 +00001052
Chris Lattnerb11c3232008-10-12 04:51:35 +00001053 Result.setKind(tok::comment);
Chris Lattner5a7971e2009-01-26 19:29:26 +00001054 PP->CreateString(&Spelling[0], Spelling.size(), Result,
1055 Result.getLocation());
Chris Lattnere01e7582008-10-12 04:15:42 +00001056 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001057}
1058
Chris Lattnercb283342006-06-18 06:48:37 +00001059/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
1060/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner89770572008-12-12 07:14:34 +00001061/// diagnostic if so. We know that the newline is inside of a block comment.
Mike Stump11289f42009-09-09 15:08:12 +00001062static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
Chris Lattner1f583052006-06-18 06:53:56 +00001063 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001064 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Mike Stump11289f42009-09-09 15:08:12 +00001065
Chris Lattner22eb9722006-06-18 05:43:12 +00001066 // Back up off the newline.
1067 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001068
Chris Lattner22eb9722006-06-18 05:43:12 +00001069 // If this is a two-character newline sequence, skip the other character.
1070 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
1071 // \n\n or \r\r -> not escaped newline.
1072 if (CurPtr[0] == CurPtr[1])
1073 return false;
1074 // \n\r or \r\n -> skip the newline.
1075 --CurPtr;
1076 }
Mike Stump11289f42009-09-09 15:08:12 +00001077
Chris Lattner22eb9722006-06-18 05:43:12 +00001078 // If we have horizontal whitespace, skip over it. We allow whitespace
1079 // between the slash and newline.
1080 bool HasSpace = false;
1081 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
1082 --CurPtr;
1083 HasSpace = true;
1084 }
Mike Stump11289f42009-09-09 15:08:12 +00001085
Chris Lattner22eb9722006-06-18 05:43:12 +00001086 // If we have a slash, we know this is an escaped newline.
1087 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +00001088 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001089 } else {
1090 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +00001091 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
1092 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +00001093 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001094
Chris Lattnercb283342006-06-18 06:48:37 +00001095 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +00001096 CurPtr -= 2;
1097
1098 // If no trigraphs are enabled, warn that we ignored this trigraph and
1099 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +00001100 if (!L->getFeatures().Trigraphs) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001101 if (!L->isLexingRawMode())
1102 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +00001103 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001104 }
Chris Lattner6d27a162008-11-22 02:02:22 +00001105 if (!L->isLexingRawMode())
1106 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001107 }
Mike Stump11289f42009-09-09 15:08:12 +00001108
Chris Lattner22eb9722006-06-18 05:43:12 +00001109 // Warn about having an escaped newline between the */ characters.
Chris Lattner6d27a162008-11-22 02:02:22 +00001110 if (!L->isLexingRawMode())
1111 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Mike Stump11289f42009-09-09 15:08:12 +00001112
Chris Lattner22eb9722006-06-18 05:43:12 +00001113 // If there was space between the backslash and newline, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +00001114 if (HasSpace && !L->isLexingRawMode())
1115 L->Diag(CurPtr, diag::backslash_newline_space);
Mike Stump11289f42009-09-09 15:08:12 +00001116
Chris Lattnercb283342006-06-18 06:48:37 +00001117 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001118}
1119
Chris Lattneraded4a92006-10-27 04:42:31 +00001120#ifdef __SSE2__
1121#include <emmintrin.h>
Chris Lattner9f6604f2006-10-30 20:01:22 +00001122#elif __ALTIVEC__
1123#include <altivec.h>
1124#undef bool
Chris Lattneraded4a92006-10-27 04:42:31 +00001125#endif
1126
Chris Lattner22eb9722006-06-18 05:43:12 +00001127/// SkipBlockComment - We have just read the /* characters from input. Read
1128/// until we find the */ characters that terminate the comment. Note that we
1129/// don't bother decoding trigraphs or escaped newlines in block comments,
1130/// because they cannot cause the comment to end. The only thing that can
1131/// happen is the comment could end with an escaped newline between the */ end
1132/// of comment.
Chris Lattnere01e7582008-10-12 04:15:42 +00001133///
Chris Lattner87d02082010-01-18 22:35:47 +00001134/// If we're in KeepCommentMode or any CommentHandler has inserted
1135/// some tokens, this will store the first token and return true.
Chris Lattner146762e2007-07-20 16:59:19 +00001136bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001137 // Scan one character past where we should, looking for a '/' character. Once
1138 // we find it, check to see if it was preceeded by a *. This common
1139 // optimization helps people who like to put a lot of * characters in their
1140 // comments.
Chris Lattnerc850ad62007-07-21 23:43:37 +00001141
1142 // The first character we get with newlines and trigraphs skipped to handle
1143 // the degenerate /*/ case below correctly if the * has an escaped newline
1144 // after it.
1145 unsigned CharSize;
1146 unsigned char C = getCharAndSize(CurPtr, CharSize);
1147 CurPtr += CharSize;
Chris Lattner22eb9722006-06-18 05:43:12 +00001148 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner561aabd2010-05-16 19:54:05 +00001149 if (!isLexingRawMode() &&
1150 !PP->isCodeCompletionFile(FileLoc))
Chris Lattner7c2e9802008-10-12 01:31:51 +00001151 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner99e7d232008-10-12 04:19:49 +00001152 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001153
Chris Lattner99e7d232008-10-12 04:19:49 +00001154 // KeepWhitespaceMode should return this broken comment as a token. Since
1155 // it isn't a well formed comment, just return it as an 'unknown' token.
1156 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001157 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner99e7d232008-10-12 04:19:49 +00001158 return true;
1159 }
Mike Stump11289f42009-09-09 15:08:12 +00001160
Chris Lattner99e7d232008-10-12 04:19:49 +00001161 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001162 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001163 }
Mike Stump11289f42009-09-09 15:08:12 +00001164
Chris Lattnerc850ad62007-07-21 23:43:37 +00001165 // Check to see if the first character after the '/*' is another /. If so,
1166 // then this slash does not end the block comment, it is part of it.
1167 if (C == '/')
1168 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001169
Chris Lattner22eb9722006-06-18 05:43:12 +00001170 while (1) {
Chris Lattner6cc3e362006-10-27 04:12:35 +00001171 // Skip over all non-interesting characters until we find end of buffer or a
1172 // (probably ending) '/' character.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001173 if (CurPtr + 24 < BufferEnd) {
1174 // While not aligned to a 16-byte boundary.
1175 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1176 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001177
Chris Lattner6cc3e362006-10-27 04:12:35 +00001178 if (C == '/') goto FoundSlash;
Chris Lattneraded4a92006-10-27 04:42:31 +00001179
1180#ifdef __SSE2__
1181 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1182 '/', '/', '/', '/', '/', '/', '/', '/');
1183 while (CurPtr+16 <= BufferEnd &&
1184 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1185 CurPtr += 16;
Chris Lattner9f6604f2006-10-30 20:01:22 +00001186#elif __ALTIVEC__
1187 __vector unsigned char Slashes = {
Mike Stump11289f42009-09-09 15:08:12 +00001188 '/', '/', '/', '/', '/', '/', '/', '/',
Chris Lattner9f6604f2006-10-30 20:01:22 +00001189 '/', '/', '/', '/', '/', '/', '/', '/'
1190 };
1191 while (CurPtr+16 <= BufferEnd &&
1192 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1193 CurPtr += 16;
Mike Stump11289f42009-09-09 15:08:12 +00001194#else
Chris Lattneraded4a92006-10-27 04:42:31 +00001195 // Scan for '/' quickly. Many block comments are very large.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001196 while (CurPtr[0] != '/' &&
1197 CurPtr[1] != '/' &&
1198 CurPtr[2] != '/' &&
1199 CurPtr[3] != '/' &&
1200 CurPtr+4 < BufferEnd) {
1201 CurPtr += 4;
1202 }
Chris Lattneraded4a92006-10-27 04:42:31 +00001203#endif
Mike Stump11289f42009-09-09 15:08:12 +00001204
Chris Lattneraded4a92006-10-27 04:42:31 +00001205 // It has to be one of the bytes scanned, increment to it and read one.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001206 C = *CurPtr++;
1207 }
Mike Stump11289f42009-09-09 15:08:12 +00001208
Chris Lattneraded4a92006-10-27 04:42:31 +00001209 // Loop to scan the remainder.
Chris Lattner22eb9722006-06-18 05:43:12 +00001210 while (C != '/' && C != '\0')
1211 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001212
Chris Lattner6cc3e362006-10-27 04:12:35 +00001213 FoundSlash:
Chris Lattner22eb9722006-06-18 05:43:12 +00001214 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001215 if (CurPtr[-2] == '*') // We found the final */. We're done!
1216 break;
Mike Stump11289f42009-09-09 15:08:12 +00001217
Chris Lattner22eb9722006-06-18 05:43:12 +00001218 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +00001219 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001220 // We found the final */, though it had an escaped newline between the
1221 // * and /. We're done!
1222 break;
1223 }
1224 }
1225 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1226 // If this is a /* inside of the comment, emit a warning. Don't do this
1227 // if this is a /*/, which will end the comment. This misses cases with
1228 // embedded escaped newlines, but oh well.
Chris Lattner6d27a162008-11-22 02:02:22 +00001229 if (!isLexingRawMode())
1230 Diag(CurPtr-1, diag::warn_nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001231 }
1232 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner561aabd2010-05-16 19:54:05 +00001233 if (!isLexingRawMode() && !PP->isCodeCompletionFile(FileLoc))
Chris Lattner6d27a162008-11-22 02:02:22 +00001234 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001235 // Note: the user probably forgot a */. We could continue immediately
1236 // after the /*, but this would involve lexing a lot of what really is the
1237 // comment, which surely would confuse the parser.
Chris Lattner99e7d232008-10-12 04:19:49 +00001238 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001239
Chris Lattner99e7d232008-10-12 04:19:49 +00001240 // KeepWhitespaceMode should return this broken comment as a token. Since
1241 // it isn't a well formed comment, just return it as an 'unknown' token.
1242 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001243 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner99e7d232008-10-12 04:19:49 +00001244 return true;
1245 }
Mike Stump11289f42009-09-09 15:08:12 +00001246
Chris Lattner99e7d232008-10-12 04:19:49 +00001247 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001248 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001249 }
1250 C = *CurPtr++;
1251 }
Mike Stump11289f42009-09-09 15:08:12 +00001252
Chris Lattner93ddf802010-02-03 21:06:21 +00001253 // Notify comment handlers about the comment unless we're in a #if 0 block.
1254 if (PP && !isLexingRawMode() &&
1255 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
1256 getSourceLocation(CurPtr)))) {
Chris Lattner87d02082010-01-18 22:35:47 +00001257 BufferPtr = CurPtr;
1258 return true; // A token has to be returned.
1259 }
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001260
Chris Lattner457fc152006-07-29 06:30:25 +00001261 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +00001262 if (inKeepCommentMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001263 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattnere01e7582008-10-12 04:15:42 +00001264 return true;
Chris Lattner457fc152006-07-29 06:30:25 +00001265 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001266
1267 // It is common for the tokens immediately after a /**/ comment to be
1268 // whitespace. Instead of going through the big switch, handle it
Chris Lattner4d963442008-10-12 04:05:48 +00001269 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1270 // have already returned above with the comment as a token.
Chris Lattner22eb9722006-06-18 05:43:12 +00001271 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner146762e2007-07-20 16:59:19 +00001272 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +00001273 SkipWhitespace(Result, CurPtr+1);
Chris Lattnere01e7582008-10-12 04:15:42 +00001274 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001275 }
1276
1277 // Otherwise, just return so that the next character will be lexed as a token.
1278 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001279 Result.setFlag(Token::LeadingSpace);
Chris Lattnere01e7582008-10-12 04:15:42 +00001280 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001281}
1282
1283//===----------------------------------------------------------------------===//
1284// Primary Lexing Entry Points
1285//===----------------------------------------------------------------------===//
1286
Chris Lattner22eb9722006-06-18 05:43:12 +00001287/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1288/// uninterpreted string. This switches the lexer out of directive mode.
1289std::string Lexer::ReadToEndOfLine() {
1290 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1291 "Must be in a preprocessing directive!");
1292 std::string Result;
Chris Lattner146762e2007-07-20 16:59:19 +00001293 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001294
1295 // CurPtr - Cache BufferPtr in an automatic variable.
1296 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001297 while (1) {
1298 char Char = getAndAdvanceChar(CurPtr, Tmp);
1299 switch (Char) {
1300 default:
1301 Result += Char;
1302 break;
1303 case 0: // Null.
1304 // Found end of file?
1305 if (CurPtr-1 != BufferEnd) {
1306 // Nope, normal character, continue.
1307 Result += Char;
1308 break;
1309 }
1310 // FALL THROUGH.
1311 case '\r':
1312 case '\n':
1313 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1314 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1315 BufferPtr = CurPtr-1;
Mike Stump11289f42009-09-09 15:08:12 +00001316
Chris Lattner22eb9722006-06-18 05:43:12 +00001317 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +00001318 Lex(Tmp);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001319 assert(Tmp.is(tok::eom) && "Unexpected token!");
Mike Stump11289f42009-09-09 15:08:12 +00001320
Chris Lattner22eb9722006-06-18 05:43:12 +00001321 // Finally, we're done, return the string we found.
1322 return Result;
1323 }
1324 }
1325}
1326
1327/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1328/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +00001329/// This returns true if Result contains a token, false if PP.Lex should be
1330/// called again.
Chris Lattner146762e2007-07-20 16:59:19 +00001331bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001332 // If we hit the end of the file while parsing a preprocessor directive,
1333 // end the preprocessor directive first. The next token returned will
1334 // then be the end of file.
1335 if (ParsingPreprocessorDirective) {
1336 // Done parsing the "line".
1337 ParsingPreprocessorDirective = false;
Chris Lattnerd01e2912006-06-18 16:22:51 +00001338 // Update the location of token as well as BufferPtr.
Chris Lattnerb11c3232008-10-12 04:51:35 +00001339 FormTokenWithChars(Result, CurPtr, tok::eom);
Mike Stump11289f42009-09-09 15:08:12 +00001340
Chris Lattner457fc152006-07-29 06:30:25 +00001341 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001342 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner2183a6e2006-07-18 06:36:12 +00001343 return true; // Have a token.
Mike Stump11289f42009-09-09 15:08:12 +00001344 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00001345
Chris Lattner30a2fa12006-07-19 06:31:49 +00001346 // If we are in raw mode, return this event as an EOF token. Let the caller
1347 // that put us in raw mode handle the event.
Chris Lattner6d27a162008-11-22 02:02:22 +00001348 if (isLexingRawMode()) {
Chris Lattner8c204872006-10-14 05:19:21 +00001349 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +00001350 BufferPtr = BufferEnd;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001351 FormTokenWithChars(Result, BufferEnd, tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001352 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001353 }
Mike Stump11289f42009-09-09 15:08:12 +00001354
Douglas Gregor3545ff42009-09-21 16:56:56 +00001355 // Otherwise, check if we are code-completing, then issue diagnostics for
1356 // unterminated #if and missing newline.
Chris Lattner30a2fa12006-07-19 06:31:49 +00001357
Douglas Gregor53ad6b92009-12-02 06:49:09 +00001358 if (PP && PP->isCodeCompletionFile(FileLoc)) {
1359 // We're at the end of the file, but we've been asked to consider the
1360 // end of the file to be a code-completion token. Return the
1361 // code-completion token.
1362 Result.startToken();
1363 FormTokenWithChars(Result, CurPtr, tok::code_completion);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001364
Douglas Gregor53ad6b92009-12-02 06:49:09 +00001365 // Only do the eof -> code_completion translation once.
1366 PP->SetCodeCompletionPoint(0, 0, 0);
Douglas Gregor6da3db42010-05-25 05:58:43 +00001367
1368 // Silence any diagnostics that occur once we hit the code-completion point.
1369 PP->getDiagnostics().setSuppressAllDiagnostics(true);
Douglas Gregor53ad6b92009-12-02 06:49:09 +00001370 return true;
Douglas Gregor3545ff42009-09-21 16:56:56 +00001371 }
1372
Chris Lattner30a2fa12006-07-19 06:31:49 +00001373 // If we are in a #if directive, emit an error.
1374 while (!ConditionalStack.empty()) {
Chris Lattner014156e2008-11-22 06:22:39 +00001375 PP->Diag(ConditionalStack.back().IfLoc,
1376 diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001377 ConditionalStack.pop_back();
1378 }
Mike Stump11289f42009-09-09 15:08:12 +00001379
Chris Lattner8f96d042008-04-12 05:54:25 +00001380 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1381 // a pedwarn.
1382 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Mike Stump0be88752009-04-02 02:29:42 +00001383 Diag(BufferEnd, diag::ext_no_newline_eof)
Douglas Gregora771f462010-03-31 17:46:05 +00001384 << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n");
Mike Stump11289f42009-09-09 15:08:12 +00001385
Chris Lattner22eb9722006-06-18 05:43:12 +00001386 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +00001387
1388 // Finally, let the preprocessor handle this.
Chris Lattner02b436a2007-10-17 20:41:00 +00001389 return PP->HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001390}
1391
Chris Lattner678c8802006-07-11 05:46:12 +00001392/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1393/// the specified lexer will return a tok::l_paren token, 0 if it is something
1394/// else and 2 if there are no more tokens in the buffer controlled by the
1395/// lexer.
1396unsigned Lexer::isNextPPTokenLParen() {
1397 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
Mike Stump11289f42009-09-09 15:08:12 +00001398
Chris Lattner678c8802006-07-11 05:46:12 +00001399 // Switch to 'skipping' mode. This will ensure that we can lex a token
1400 // without emitting diagnostics, disables macro expansion, and will cause EOF
1401 // to return an EOF token instead of popping the include stack.
1402 LexingRawMode = true;
Mike Stump11289f42009-09-09 15:08:12 +00001403
Chris Lattner678c8802006-07-11 05:46:12 +00001404 // Save state that can be changed while lexing so that we can restore it.
1405 const char *TmpBufferPtr = BufferPtr;
Chris Lattner40493eb2009-04-24 07:15:46 +00001406 bool inPPDirectiveMode = ParsingPreprocessorDirective;
Mike Stump11289f42009-09-09 15:08:12 +00001407
Chris Lattner146762e2007-07-20 16:59:19 +00001408 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001409 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001410 LexTokenInternal(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001411
Chris Lattner678c8802006-07-11 05:46:12 +00001412 // Restore state that may have changed.
1413 BufferPtr = TmpBufferPtr;
Chris Lattner40493eb2009-04-24 07:15:46 +00001414 ParsingPreprocessorDirective = inPPDirectiveMode;
Mike Stump11289f42009-09-09 15:08:12 +00001415
Chris Lattner678c8802006-07-11 05:46:12 +00001416 // Restore the lexer back to non-skipping mode.
1417 LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +00001418
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001419 if (Tok.is(tok::eof))
Chris Lattner678c8802006-07-11 05:46:12 +00001420 return 2;
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001421 return Tok.is(tok::l_paren);
Chris Lattner678c8802006-07-11 05:46:12 +00001422}
1423
Chris Lattner7c027ee2009-12-14 06:16:57 +00001424/// FindConflictEnd - Find the end of a version control conflict marker.
1425static const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd) {
1426 llvm::StringRef RestOfBuffer(CurPtr+7, BufferEnd-CurPtr-7);
1427 size_t Pos = RestOfBuffer.find(">>>>>>>");
1428 while (Pos != llvm::StringRef::npos) {
1429 // Must occur at start of line.
1430 if (RestOfBuffer[Pos-1] != '\r' &&
1431 RestOfBuffer[Pos-1] != '\n') {
1432 RestOfBuffer = RestOfBuffer.substr(Pos+7);
Chris Lattner467f6bc2010-05-17 20:27:25 +00001433 Pos = RestOfBuffer.find(">>>>>>>");
Chris Lattner7c027ee2009-12-14 06:16:57 +00001434 continue;
1435 }
1436 return RestOfBuffer.data()+Pos;
1437 }
1438 return 0;
1439}
1440
1441/// IsStartOfConflictMarker - If the specified pointer is the start of a version
1442/// control conflict marker like '<<<<<<<', recognize it as such, emit an error
1443/// and recover nicely. This returns true if it is a conflict marker and false
1444/// if not.
1445bool Lexer::IsStartOfConflictMarker(const char *CurPtr) {
1446 // Only a conflict marker if it starts at the beginning of a line.
1447 if (CurPtr != BufferStart &&
1448 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1449 return false;
1450
1451 // Check to see if we have <<<<<<<.
1452 if (BufferEnd-CurPtr < 8 ||
1453 llvm::StringRef(CurPtr, 7) != "<<<<<<<")
1454 return false;
1455
1456 // If we have a situation where we don't care about conflict markers, ignore
1457 // it.
1458 if (IsInConflictMarker || isLexingRawMode())
1459 return false;
1460
1461 // Check to see if there is a >>>>>>> somewhere in the buffer at the start of
1462 // a line to terminate this conflict marker.
Chris Lattner467f6bc2010-05-17 20:27:25 +00001463 if (FindConflictEnd(CurPtr, BufferEnd)) {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001464 // We found a match. We are really in a conflict marker.
1465 // Diagnose this, and ignore to the end of line.
1466 Diag(CurPtr, diag::err_conflict_marker);
1467 IsInConflictMarker = true;
1468
1469 // Skip ahead to the end of line. We know this exists because the
1470 // end-of-conflict marker starts with \r or \n.
1471 while (*CurPtr != '\r' && *CurPtr != '\n') {
1472 assert(CurPtr != BufferEnd && "Didn't find end of line");
1473 ++CurPtr;
1474 }
1475 BufferPtr = CurPtr;
1476 return true;
1477 }
1478
1479 // No end of conflict marker found.
1480 return false;
1481}
1482
1483
1484/// HandleEndOfConflictMarker - If this is a '=======' or '|||||||' or '>>>>>>>'
1485/// marker, then it is the end of a conflict marker. Handle it by ignoring up
1486/// until the end of the line. This returns true if it is a conflict marker and
1487/// false if not.
1488bool Lexer::HandleEndOfConflictMarker(const char *CurPtr) {
1489 // Only a conflict marker if it starts at the beginning of a line.
1490 if (CurPtr != BufferStart &&
1491 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1492 return false;
1493
1494 // If we have a situation where we don't care about conflict markers, ignore
1495 // it.
1496 if (!IsInConflictMarker || isLexingRawMode())
1497 return false;
1498
1499 // Check to see if we have the marker (7 characters in a row).
1500 for (unsigned i = 1; i != 7; ++i)
1501 if (CurPtr[i] != CurPtr[0])
1502 return false;
1503
1504 // If we do have it, search for the end of the conflict marker. This could
1505 // fail if it got skipped with a '#if 0' or something. Note that CurPtr might
1506 // be the end of conflict marker.
1507 if (const char *End = FindConflictEnd(CurPtr, BufferEnd)) {
1508 CurPtr = End;
1509
1510 // Skip ahead to the end of line.
1511 while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n')
1512 ++CurPtr;
1513
1514 BufferPtr = CurPtr;
1515
1516 // No longer in the conflict marker.
1517 IsInConflictMarker = false;
1518 return true;
1519 }
1520
1521 return false;
1522}
1523
Chris Lattner22eb9722006-06-18 05:43:12 +00001524
1525/// LexTokenInternal - This implements a simple C family lexer. It is an
1526/// extremely performance critical piece of code. This assumes that the buffer
Chris Lattner5c349382009-07-07 05:05:42 +00001527/// has a null character at the end of the file. This returns a preprocessing
1528/// token, not a normal token, as such, it is an internal interface. It assumes
1529/// that the Flags of result have been cleared before calling this.
Chris Lattner146762e2007-07-20 16:59:19 +00001530void Lexer::LexTokenInternal(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001531LexNextToken:
1532 // New token, can't need cleaning yet.
Chris Lattner146762e2007-07-20 16:59:19 +00001533 Result.clearFlag(Token::NeedsCleaning);
Chris Lattner8c204872006-10-14 05:19:21 +00001534 Result.setIdentifierInfo(0);
Mike Stump11289f42009-09-09 15:08:12 +00001535
Chris Lattner22eb9722006-06-18 05:43:12 +00001536 // CurPtr - Cache BufferPtr in an automatic variable.
1537 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001538
Chris Lattnereb54b592006-07-10 06:34:27 +00001539 // Small amounts of horizontal whitespace is very common between tokens.
1540 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1541 ++CurPtr;
1542 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1543 ++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001544
Chris Lattner4d963442008-10-12 04:05:48 +00001545 // If we are keeping whitespace and other tokens, just return what we just
1546 // skipped. The next lexer invocation will return the token after the
1547 // whitespace.
1548 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001549 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4d963442008-10-12 04:05:48 +00001550 return;
1551 }
Mike Stump11289f42009-09-09 15:08:12 +00001552
Chris Lattnereb54b592006-07-10 06:34:27 +00001553 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001554 Result.setFlag(Token::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001555 }
Mike Stump11289f42009-09-09 15:08:12 +00001556
Chris Lattner22eb9722006-06-18 05:43:12 +00001557 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
Mike Stump11289f42009-09-09 15:08:12 +00001558
Chris Lattner22eb9722006-06-18 05:43:12 +00001559 // Read a character, advancing over it.
1560 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001561 tok::TokenKind Kind;
Mike Stump11289f42009-09-09 15:08:12 +00001562
Chris Lattner22eb9722006-06-18 05:43:12 +00001563 switch (Char) {
1564 case 0: // Null.
1565 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001566 if (CurPtr-1 == BufferEnd) {
1567 // Read the PP instance variable into an automatic variable, because
1568 // LexEndOfFile will often delete 'this'.
Chris Lattner02b436a2007-10-17 20:41:00 +00001569 Preprocessor *PPCache = PP;
Chris Lattner2183a6e2006-07-18 06:36:12 +00001570 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1571 return; // Got a token to return.
Chris Lattner02b436a2007-10-17 20:41:00 +00001572 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1573 return PPCache->Lex(Result);
Chris Lattner2183a6e2006-07-18 06:36:12 +00001574 }
Mike Stump11289f42009-09-09 15:08:12 +00001575
Chris Lattner6d27a162008-11-22 02:02:22 +00001576 if (!isLexingRawMode())
1577 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner146762e2007-07-20 16:59:19 +00001578 Result.setFlag(Token::LeadingSpace);
Chris Lattner4d963442008-10-12 04:05:48 +00001579 if (SkipWhitespace(Result, CurPtr))
1580 return; // KeepWhitespaceMode
Mike Stump11289f42009-09-09 15:08:12 +00001581
Chris Lattner22eb9722006-06-18 05:43:12 +00001582 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner3dfff972009-12-17 05:29:40 +00001583
1584 case 26: // DOS & CP/M EOF: "^Z".
1585 // If we're in Microsoft extensions mode, treat this as end of file.
1586 if (Features.Microsoft) {
1587 // Read the PP instance variable into an automatic variable, because
1588 // LexEndOfFile will often delete 'this'.
1589 Preprocessor *PPCache = PP;
1590 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1591 return; // Got a token to return.
1592 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1593 return PPCache->Lex(Result);
1594 }
1595 // If Microsoft extensions are disabled, this is just random garbage.
1596 Kind = tok::unknown;
1597 break;
1598
Chris Lattner22eb9722006-06-18 05:43:12 +00001599 case '\n':
1600 case '\r':
1601 // If we are inside a preprocessor directive and we see the end of line,
1602 // we know we are done with the directive, so return an EOM token.
1603 if (ParsingPreprocessorDirective) {
1604 // Done parsing the "line".
1605 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +00001606
Chris Lattner457fc152006-07-29 06:30:25 +00001607 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001608 SetCommentRetentionState(PP->getCommentRetentionState());
Mike Stump11289f42009-09-09 15:08:12 +00001609
Chris Lattner22eb9722006-06-18 05:43:12 +00001610 // Since we consumed a newline, we are back at the start of a line.
1611 IsAtStartOfLine = true;
Mike Stump11289f42009-09-09 15:08:12 +00001612
Chris Lattnerb11c3232008-10-12 04:51:35 +00001613 Kind = tok::eom;
Chris Lattner22eb9722006-06-18 05:43:12 +00001614 break;
1615 }
1616 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001617 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001618 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001619 Result.clearFlag(Token::LeadingSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001620
Chris Lattner4d963442008-10-12 04:05:48 +00001621 if (SkipWhitespace(Result, CurPtr))
1622 return; // KeepWhitespaceMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001623 goto LexNextToken; // GCC isn't tail call eliminating.
1624 case ' ':
1625 case '\t':
1626 case '\f':
1627 case '\v':
Chris Lattnerb9b85972007-07-22 06:29:05 +00001628 SkipHorizontalWhitespace:
Chris Lattner146762e2007-07-20 16:59:19 +00001629 Result.setFlag(Token::LeadingSpace);
Chris Lattner4d963442008-10-12 04:05:48 +00001630 if (SkipWhitespace(Result, CurPtr))
1631 return; // KeepWhitespaceMode
Chris Lattnerb9b85972007-07-22 06:29:05 +00001632
1633 SkipIgnoredUnits:
1634 CurPtr = BufferPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001635
Chris Lattnerb9b85972007-07-22 06:29:05 +00001636 // If the next token is obviously a // or /* */ comment, skip it efficiently
1637 // too (without going through the big switch stmt).
Chris Lattner58827712009-01-16 22:39:25 +00001638 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1639 Features.BCPLComment) {
Chris Lattner87d02082010-01-18 22:35:47 +00001640 if (SkipBCPLComment(Result, CurPtr+2))
1641 return; // There is a token to return.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001642 goto SkipIgnoredUnits;
Chris Lattner8637abd2008-10-12 03:22:02 +00001643 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner87d02082010-01-18 22:35:47 +00001644 if (SkipBlockComment(Result, CurPtr+2))
1645 return; // There is a token to return.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001646 goto SkipIgnoredUnits;
1647 } else if (isHorizontalWhitespace(*CurPtr)) {
1648 goto SkipHorizontalWhitespace;
1649 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001650 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner3dfff972009-12-17 05:29:40 +00001651
Chris Lattner2b15cf72008-01-03 17:58:54 +00001652 // C99 6.4.4.1: Integer Constants.
1653 // C99 6.4.4.2: Floating Constants.
1654 case '0': case '1': case '2': case '3': case '4':
1655 case '5': case '6': case '7': case '8': case '9':
1656 // Notify MIOpt that we read a non-whitespace/non-comment token.
1657 MIOpt.ReadToken();
1658 return LexNumericConstant(Result, CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001659
Chris Lattner2b15cf72008-01-03 17:58:54 +00001660 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Chris Lattner371ac8a2006-07-04 07:11:10 +00001661 // Notify MIOpt that we read a non-whitespace/non-comment token.
1662 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001663 Char = getCharAndSize(CurPtr, SizeTmp);
1664
1665 // Wide string literal.
1666 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001667 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1668 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001669
1670 // Wide character constant.
1671 if (Char == '\'')
1672 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1673 // FALL THROUGH, treating L like the start of an identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001674
Chris Lattner22eb9722006-06-18 05:43:12 +00001675 // C99 6.4.2: Identifiers.
1676 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1677 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1678 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1679 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1680 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1681 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1682 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1683 case 'v': case 'w': case 'x': case 'y': case 'z':
1684 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001685 // Notify MIOpt that we read a non-whitespace/non-comment token.
1686 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001687 return LexIdentifier(Result, CurPtr);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001688
1689 case '$': // $ in identifiers.
1690 if (Features.DollarIdents) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001691 if (!isLexingRawMode())
1692 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001693 // Notify MIOpt that we read a non-whitespace/non-comment token.
1694 MIOpt.ReadToken();
1695 return LexIdentifier(Result, CurPtr);
1696 }
Mike Stump11289f42009-09-09 15:08:12 +00001697
Chris Lattnerb11c3232008-10-12 04:51:35 +00001698 Kind = tok::unknown;
Chris Lattner2b15cf72008-01-03 17:58:54 +00001699 break;
Mike Stump11289f42009-09-09 15:08:12 +00001700
Chris Lattner22eb9722006-06-18 05:43:12 +00001701 // C99 6.4.4: Character Constants.
1702 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001703 // Notify MIOpt that we read a non-whitespace/non-comment token.
1704 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001705 return LexCharConstant(Result, CurPtr);
1706
1707 // C99 6.4.5: String Literals.
1708 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001709 // Notify MIOpt that we read a non-whitespace/non-comment token.
1710 MIOpt.ReadToken();
Chris Lattnerd3e98952006-10-06 05:22:26 +00001711 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001712
1713 // C99 6.4.6: Punctuators.
1714 case '?':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001715 Kind = tok::question;
Chris Lattner22eb9722006-06-18 05:43:12 +00001716 break;
1717 case '[':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001718 Kind = tok::l_square;
Chris Lattner22eb9722006-06-18 05:43:12 +00001719 break;
1720 case ']':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001721 Kind = tok::r_square;
Chris Lattner22eb9722006-06-18 05:43:12 +00001722 break;
1723 case '(':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001724 Kind = tok::l_paren;
Chris Lattner22eb9722006-06-18 05:43:12 +00001725 break;
1726 case ')':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001727 Kind = tok::r_paren;
Chris Lattner22eb9722006-06-18 05:43:12 +00001728 break;
1729 case '{':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001730 Kind = tok::l_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001731 break;
1732 case '}':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001733 Kind = tok::r_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001734 break;
1735 case '.':
1736 Char = getCharAndSize(CurPtr, SizeTmp);
1737 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001738 // Notify MIOpt that we read a non-whitespace/non-comment token.
1739 MIOpt.ReadToken();
1740
Chris Lattner22eb9722006-06-18 05:43:12 +00001741 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1742 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001743 Kind = tok::periodstar;
Chris Lattner22eb9722006-06-18 05:43:12 +00001744 CurPtr += SizeTmp;
1745 } else if (Char == '.' &&
1746 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001747 Kind = tok::ellipsis;
Chris Lattner22eb9722006-06-18 05:43:12 +00001748 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1749 SizeTmp2, Result);
1750 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001751 Kind = tok::period;
Chris Lattner22eb9722006-06-18 05:43:12 +00001752 }
1753 break;
1754 case '&':
1755 Char = getCharAndSize(CurPtr, SizeTmp);
1756 if (Char == '&') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001757 Kind = tok::ampamp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001758 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1759 } else if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001760 Kind = tok::ampequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001761 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1762 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001763 Kind = tok::amp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001764 }
1765 break;
Mike Stump11289f42009-09-09 15:08:12 +00001766 case '*':
Chris Lattner22eb9722006-06-18 05:43:12 +00001767 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001768 Kind = tok::starequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001769 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1770 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001771 Kind = tok::star;
Chris Lattner22eb9722006-06-18 05:43:12 +00001772 }
1773 break;
1774 case '+':
1775 Char = getCharAndSize(CurPtr, SizeTmp);
1776 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::plusplus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001779 } else if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001780 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001781 Kind = tok::plusequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001782 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001783 Kind = tok::plus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001784 }
1785 break;
1786 case '-':
1787 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001788 if (Char == '-') { // --
Chris Lattner22eb9722006-06-18 05:43:12 +00001789 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001790 Kind = tok::minusminus;
Mike Stump11289f42009-09-09 15:08:12 +00001791 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattnerb11c3232008-10-12 04:51:35 +00001792 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00001793 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1794 SizeTmp2, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001795 Kind = tok::arrowstar;
1796 } else if (Char == '>') { // ->
Chris Lattner22eb9722006-06-18 05:43:12 +00001797 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001798 Kind = tok::arrow;
1799 } else if (Char == '=') { // -=
Chris Lattner22eb9722006-06-18 05:43:12 +00001800 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001801 Kind = tok::minusequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001802 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001803 Kind = tok::minus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001804 }
1805 break;
1806 case '~':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001807 Kind = tok::tilde;
Chris Lattner22eb9722006-06-18 05:43:12 +00001808 break;
1809 case '!':
1810 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001811 Kind = tok::exclaimequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001812 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1813 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001814 Kind = tok::exclaim;
Chris Lattner22eb9722006-06-18 05:43:12 +00001815 }
1816 break;
1817 case '/':
1818 // 6.4.9: Comments
1819 Char = getCharAndSize(CurPtr, SizeTmp);
1820 if (Char == '/') { // BCPL comment.
Chris Lattner58827712009-01-16 22:39:25 +00001821 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
1822 // want to lex this as a comment. There is one problem with this though,
1823 // that in one particular corner case, this can change the behavior of the
1824 // resultant program. For example, In "foo //**/ bar", C89 would lex
1825 // this as "foo / bar" and langauges with BCPL comments would lex it as
1826 // "foo". Check to see if the character after the second slash is a '*'.
1827 // If so, we will lex that as a "/" instead of the start of a comment.
1828 if (Features.BCPLComment ||
1829 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
1830 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner87d02082010-01-18 22:35:47 +00001831 return; // There is a token to return.
Mike Stump11289f42009-09-09 15:08:12 +00001832
Chris Lattner58827712009-01-16 22:39:25 +00001833 // It is common for the tokens immediately after a // comment to be
1834 // whitespace (indentation for the next line). Instead of going through
1835 // the big switch, handle it efficiently now.
1836 goto SkipIgnoredUnits;
1837 }
1838 }
Mike Stump11289f42009-09-09 15:08:12 +00001839
Chris Lattner58827712009-01-16 22:39:25 +00001840 if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001841 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner87d02082010-01-18 22:35:47 +00001842 return; // There is a token to return.
Chris Lattnere01e7582008-10-12 04:15:42 +00001843 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner58827712009-01-16 22:39:25 +00001844 }
Mike Stump11289f42009-09-09 15:08:12 +00001845
Chris Lattner58827712009-01-16 22:39:25 +00001846 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001847 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001848 Kind = tok::slashequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001849 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001850 Kind = tok::slash;
Chris Lattner22eb9722006-06-18 05:43:12 +00001851 }
1852 break;
1853 case '%':
1854 Char = getCharAndSize(CurPtr, SizeTmp);
1855 if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001856 Kind = tok::percentequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001857 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1858 } else if (Features.Digraphs && Char == '>') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001859 Kind = tok::r_brace; // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00001860 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1861 } else if (Features.Digraphs && Char == ':') {
1862 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001863 Char = getCharAndSize(CurPtr, SizeTmp);
1864 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001865 Kind = tok::hashhash; // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00001866 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1867 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001868 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner2b271db2006-07-15 05:41:09 +00001869 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner6d27a162008-11-22 02:02:22 +00001870 if (!isLexingRawMode())
1871 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001872 Kind = tok::hashat;
Chris Lattner2534324a2009-03-18 20:58:27 +00001873 } else { // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00001874 // We parsed a # character. If this occurs at the start of the line,
1875 // it's actually the start of a preprocessing directive. Callback to
1876 // the preprocessor to handle it.
1877 // FIXME: -fpreprocessed mode??
Chris Lattnerff96dd02009-05-13 06:10:29 +00001878 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattner2534324a2009-03-18 20:58:27 +00001879 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner02b436a2007-10-17 20:41:00 +00001880 PP->HandleDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +00001881
Chris Lattner22eb9722006-06-18 05:43:12 +00001882 // As an optimization, if the preprocessor didn't switch lexers, tail
1883 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00001884 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001885 // Start a new token. If this is a #include or something, the PP may
1886 // want us starting at the beginning of the line again. If so, set
Chris Lattner1a9e8732010-04-12 23:04:41 +00001887 // the StartOfLine flag and clear LeadingSpace.
Chris Lattner22eb9722006-06-18 05:43:12 +00001888 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00001889 Result.setFlag(Token::StartOfLine);
Chris Lattner1a9e8732010-04-12 23:04:41 +00001890 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001891 IsAtStartOfLine = false;
1892 }
1893 goto LexNextToken; // GCC isn't tail call eliminating.
1894 }
Mike Stump11289f42009-09-09 15:08:12 +00001895
Chris Lattner02b436a2007-10-17 20:41:00 +00001896 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001897 }
Mike Stump11289f42009-09-09 15:08:12 +00001898
Chris Lattner2534324a2009-03-18 20:58:27 +00001899 Kind = tok::hash;
Chris Lattner22eb9722006-06-18 05:43:12 +00001900 }
1901 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001902 Kind = tok::percent;
Chris Lattner22eb9722006-06-18 05:43:12 +00001903 }
1904 break;
1905 case '<':
1906 Char = getCharAndSize(CurPtr, SizeTmp);
1907 if (ParsingFilename) {
Chris Lattnerb40289b2009-04-17 23:56:52 +00001908 return LexAngledStringLiteral(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001909 } else if (Char == '<') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001910 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
1911 if (After == '=') {
1912 Kind = tok::lesslessequal;
1913 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1914 SizeTmp2, Result);
1915 } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) {
1916 // If this is actually a '<<<<<<<' version control conflict marker,
1917 // recognize it as such and recover nicely.
1918 goto LexNextToken;
1919 } else {
1920 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1921 Kind = tok::lessless;
1922 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001923 } else if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001924 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001925 Kind = tok::lessequal;
1926 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00001927 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001928 Kind = tok::l_square;
1929 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Chris Lattner22eb9722006-06-18 05:43:12 +00001930 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001931 Kind = tok::l_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001932 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001933 Kind = tok::less;
Chris Lattner22eb9722006-06-18 05:43:12 +00001934 }
1935 break;
1936 case '>':
1937 Char = getCharAndSize(CurPtr, SizeTmp);
1938 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001939 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001940 Kind = tok::greaterequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001941 } else if (Char == '>') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001942 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
1943 if (After == '=') {
1944 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1945 SizeTmp2, Result);
1946 Kind = tok::greatergreaterequal;
1947 } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) {
1948 // If this is '>>>>>>>' and we're in a conflict marker, ignore it.
1949 goto LexNextToken;
1950 } else {
1951 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1952 Kind = tok::greatergreater;
1953 }
1954
Chris Lattner22eb9722006-06-18 05:43:12 +00001955 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001956 Kind = tok::greater;
Chris Lattner22eb9722006-06-18 05:43:12 +00001957 }
1958 break;
1959 case '^':
1960 Char = getCharAndSize(CurPtr, SizeTmp);
1961 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001962 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001963 Kind = tok::caretequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001964 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001965 Kind = tok::caret;
Chris Lattner22eb9722006-06-18 05:43:12 +00001966 }
1967 break;
1968 case '|':
1969 Char = getCharAndSize(CurPtr, SizeTmp);
1970 if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001971 Kind = tok::pipeequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001972 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1973 } else if (Char == '|') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001974 // If this is '|||||||' and we're in a conflict marker, ignore it.
1975 if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1))
1976 goto LexNextToken;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001977 Kind = tok::pipepipe;
Chris Lattner22eb9722006-06-18 05:43:12 +00001978 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1979 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001980 Kind = tok::pipe;
Chris Lattner22eb9722006-06-18 05:43:12 +00001981 }
1982 break;
1983 case ':':
1984 Char = getCharAndSize(CurPtr, SizeTmp);
1985 if (Features.Digraphs && Char == '>') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001986 Kind = tok::r_square; // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00001987 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1988 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001989 Kind = tok::coloncolon;
Chris Lattner22eb9722006-06-18 05:43:12 +00001990 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump11289f42009-09-09 15:08:12 +00001991 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001992 Kind = tok::colon;
Chris Lattner22eb9722006-06-18 05:43:12 +00001993 }
1994 break;
1995 case ';':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001996 Kind = tok::semi;
Chris Lattner22eb9722006-06-18 05:43:12 +00001997 break;
1998 case '=':
1999 Char = getCharAndSize(CurPtr, SizeTmp);
2000 if (Char == '=') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00002001 // If this is '=======' and we're in a conflict marker, ignore it.
2002 if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1))
2003 goto LexNextToken;
2004
Chris Lattnerb11c3232008-10-12 04:51:35 +00002005 Kind = tok::equalequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002006 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump11289f42009-09-09 15:08:12 +00002007 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002008 Kind = tok::equal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002009 }
2010 break;
2011 case ',':
Chris Lattnerb11c3232008-10-12 04:51:35 +00002012 Kind = tok::comma;
Chris Lattner22eb9722006-06-18 05:43:12 +00002013 break;
2014 case '#':
2015 Char = getCharAndSize(CurPtr, SizeTmp);
2016 if (Char == '#') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002017 Kind = tok::hashhash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002018 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00002019 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattnerb11c3232008-10-12 04:51:35 +00002020 Kind = tok::hashat;
Chris Lattner6d27a162008-11-22 02:02:22 +00002021 if (!isLexingRawMode())
2022 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner2b271db2006-07-15 05:41:09 +00002023 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002024 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +00002025 // We parsed a # character. If this occurs at the start of the line,
2026 // it's actually the start of a preprocessing directive. Callback to
2027 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00002028 // FIXME: -fpreprocessed mode??
Chris Lattnerff96dd02009-05-13 06:10:29 +00002029 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattner2534324a2009-03-18 20:58:27 +00002030 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner02b436a2007-10-17 20:41:00 +00002031 PP->HandleDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +00002032
Chris Lattner22eb9722006-06-18 05:43:12 +00002033 // As an optimization, if the preprocessor didn't switch lexers, tail
2034 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00002035 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002036 // Start a new token. If this is a #include or something, the PP may
2037 // want us starting at the beginning of the line again. If so, set
Chris Lattner1a9e8732010-04-12 23:04:41 +00002038 // the StartOfLine flag and clear LeadingSpace.
Chris Lattner22eb9722006-06-18 05:43:12 +00002039 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00002040 Result.setFlag(Token::StartOfLine);
Chris Lattner1a9e8732010-04-12 23:04:41 +00002041 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00002042 IsAtStartOfLine = false;
2043 }
2044 goto LexNextToken; // GCC isn't tail call eliminating.
2045 }
Chris Lattner02b436a2007-10-17 20:41:00 +00002046 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002047 }
Mike Stump11289f42009-09-09 15:08:12 +00002048
Chris Lattner2534324a2009-03-18 20:58:27 +00002049 Kind = tok::hash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002050 }
2051 break;
2052
Chris Lattner2b15cf72008-01-03 17:58:54 +00002053 case '@':
2054 // Objective C support.
2055 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattnerb11c3232008-10-12 04:51:35 +00002056 Kind = tok::at;
Chris Lattner2b15cf72008-01-03 17:58:54 +00002057 else
Chris Lattnerb11c3232008-10-12 04:51:35 +00002058 Kind = tok::unknown;
Chris Lattner2b15cf72008-01-03 17:58:54 +00002059 break;
Mike Stump11289f42009-09-09 15:08:12 +00002060
Chris Lattner22eb9722006-06-18 05:43:12 +00002061 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00002062 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00002063 // FALL THROUGH.
2064 default:
Chris Lattnerb11c3232008-10-12 04:51:35 +00002065 Kind = tok::unknown;
Chris Lattner041bef82006-07-11 05:52:53 +00002066 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00002067 }
Mike Stump11289f42009-09-09 15:08:12 +00002068
Chris Lattner371ac8a2006-07-04 07:11:10 +00002069 // Notify MIOpt that we read a non-whitespace/non-comment token.
2070 MIOpt.ReadToken();
2071
Chris Lattnerd01e2912006-06-18 16:22:51 +00002072 // Update the location of token as well as BufferPtr.
Chris Lattnerb11c3232008-10-12 04:51:35 +00002073 FormTokenWithChars(Result, CurPtr, Kind);
Chris Lattner22eb9722006-06-18 05:43:12 +00002074}