blob: 0f01155a8f2c836df8434f33638b81b276c3d9ae [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattnerd2177732007-07-20 16:59:19 +000010// This file implements the Lexer and Token interfaces.
Reid Spencer5f016e22007-07-11 17:01:13 +000011//
12//===----------------------------------------------------------------------===//
13//
14// TODO: GCC Diagnostics emitted by the lexer:
15// PEDWARN: (form feed|vertical tab) in preprocessing directive
16//
17// Universal characters, unicode, char mapping:
18// WARNING: `%.*s' is not in NFKC
19// WARNING: `%.*s' is not in NFC
20//
21// Other:
22// TODO: Options to support:
23// -fexec-charset,-fwide-exec-charset
24//
25//===----------------------------------------------------------------------===//
26
27#include "clang/Lex/Lexer.h"
28#include "clang/Lex/Preprocessor.h"
Chris Lattner500d3292009-01-29 05:15:15 +000029#include "clang/Lex/LexDiagnostic.h"
Chris Lattner9dc1f532007-07-20 16:37:10 +000030#include "clang/Basic/SourceManager.h"
Chris Lattner409a0362007-07-22 18:38:25 +000031#include "llvm/Support/Compiler.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000032#include "llvm/Support/MemoryBuffer.h"
33#include <cctype>
34using namespace clang;
35
36static void InitCharacterInfo();
37
Chris Lattnerdbf388b2007-10-07 08:47:24 +000038//===----------------------------------------------------------------------===//
39// Token Class Implementation
40//===----------------------------------------------------------------------===//
41
Mike Stump1eb44332009-09-09 15:08:12 +000042/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
Chris Lattnerdbf388b2007-10-07 08:47:24 +000043bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
Douglas Gregorbec1c9d2008-12-01 21:46:47 +000044 if (IdentifierInfo *II = getIdentifierInfo())
45 return II->getObjCKeywordID() == objcKey;
46 return false;
Chris Lattnerdbf388b2007-10-07 08:47:24 +000047}
48
49/// getObjCKeywordID - Return the ObjC keyword kind.
50tok::ObjCKeywordKind Token::getObjCKeywordID() const {
51 IdentifierInfo *specId = getIdentifierInfo();
52 return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
53}
54
Chris Lattner53702cd2007-12-13 01:59:49 +000055
Chris Lattnerdbf388b2007-10-07 08:47:24 +000056//===----------------------------------------------------------------------===//
57// Lexer Class Implementation
58//===----------------------------------------------------------------------===//
59
Mike Stump1eb44332009-09-09 15:08:12 +000060void Lexer::InitLexer(const char *BufStart, const char *BufPtr,
Chris Lattner22d91ca2009-01-17 06:55:17 +000061 const char *BufEnd) {
62 InitCharacterInfo();
Mike Stump1eb44332009-09-09 15:08:12 +000063
Chris Lattner22d91ca2009-01-17 06:55:17 +000064 BufferStart = BufStart;
65 BufferPtr = BufPtr;
66 BufferEnd = BufEnd;
Mike Stump1eb44332009-09-09 15:08:12 +000067
Chris Lattner22d91ca2009-01-17 06:55:17 +000068 assert(BufEnd[0] == 0 &&
69 "We assume that the input buffer has a null character at the end"
70 " to simplify lexing!");
Mike Stump1eb44332009-09-09 15:08:12 +000071
Chris Lattner22d91ca2009-01-17 06:55:17 +000072 Is_PragmaLexer = false;
Douglas Gregor81b747b2009-09-17 21:32:03 +000073 IsEofCodeCompletion = false;
74
Chris Lattner22d91ca2009-01-17 06:55:17 +000075 // Start of the file is a start of line.
76 IsAtStartOfLine = true;
Mike Stump1eb44332009-09-09 15:08:12 +000077
Chris Lattner22d91ca2009-01-17 06:55:17 +000078 // We are not after parsing a #.
79 ParsingPreprocessorDirective = false;
Mike Stump1eb44332009-09-09 15:08:12 +000080
Chris Lattner22d91ca2009-01-17 06:55:17 +000081 // We are not after parsing #include.
82 ParsingFilename = false;
Mike Stump1eb44332009-09-09 15:08:12 +000083
Chris Lattner22d91ca2009-01-17 06:55:17 +000084 // We are not in raw mode. Raw mode disables diagnostics and interpretation
85 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
86 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
87 // or otherwise skipping over tokens.
88 LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +000089
Chris Lattner22d91ca2009-01-17 06:55:17 +000090 // Default to not keeping comments.
91 ExtendedTokenMode = 0;
92}
93
Chris Lattner0770dab2009-01-17 07:56:59 +000094/// Lexer constructor - Create a new lexer object for the specified buffer
95/// with the specified preprocessor managing the lexing process. This lexer
96/// assumes that the associated file buffer and Preprocessor objects will
97/// outlive it, so it doesn't take ownership of either of them.
Chris Lattner88d3ac12009-01-17 08:03:42 +000098Lexer::Lexer(FileID FID, Preprocessor &PP)
99 : PreprocessorLexer(&PP, FID),
100 FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
101 Features(PP.getLangOptions()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Chris Lattner88d3ac12009-01-17 08:03:42 +0000103 const llvm::MemoryBuffer *InputFile = PP.getSourceManager().getBuffer(FID);
Douglas Gregorb657f112009-09-22 21:11:38 +0000104
Chris Lattner0770dab2009-01-17 07:56:59 +0000105 InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
106 InputFile->getBufferEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Chris Lattner0770dab2009-01-17 07:56:59 +0000108 // Default to keeping comments if the preprocessor wants them.
109 SetCommentRetentionState(PP.getCommentRetentionState());
Douglas Gregorb657f112009-09-22 21:11:38 +0000110
111 // If the input file is truncated, the EOF is a code-completion token.
112 if (PP.getSourceManager().isTruncatedFile(FID))
113 IsEofCodeCompletion = true;
Chris Lattner0770dab2009-01-17 07:56:59 +0000114}
Chris Lattnerdbf388b2007-10-07 08:47:24 +0000115
Chris Lattner168ae2d2007-10-17 20:41:00 +0000116/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner590f0cc2008-10-12 01:15:46 +0000117/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
118/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000119Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattnerde96c0f2009-01-17 07:42:27 +0000120 const char *BufStart, const char *BufPtr, const char *BufEnd)
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000121 : FileLoc(fileloc), Features(features) {
Chris Lattner22d91ca2009-01-17 06:55:17 +0000122
Chris Lattner22d91ca2009-01-17 06:55:17 +0000123 InitLexer(BufStart, BufPtr, BufEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattner168ae2d2007-10-17 20:41:00 +0000125 // We *are* in raw mode.
126 LexingRawMode = true;
Chris Lattner168ae2d2007-10-17 20:41:00 +0000127}
128
Chris Lattner025c3a62009-01-17 07:35:14 +0000129/// Lexer constructor - Create a new raw lexer object. This object is only
130/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
131/// range will outlive it, so it doesn't take ownership of it.
132Lexer::Lexer(FileID FID, const SourceManager &SM, const LangOptions &features)
133 : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
134 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
135
Mike Stump1eb44332009-09-09 15:08:12 +0000136 InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
Chris Lattner025c3a62009-01-17 07:35:14 +0000137 FromFile->getBufferEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Chris Lattner025c3a62009-01-17 07:35:14 +0000139 // We *are* in raw mode.
140 LexingRawMode = true;
141}
142
Chris Lattner42e00d12009-01-17 08:27:52 +0000143/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
144/// _Pragma expansion. This has a variety of magic semantics that this method
145/// sets up. It returns a new'd Lexer that must be delete'd when done.
146///
147/// On entrance to this routine, TokStartLoc is a macro location which has a
148/// spelling loc that indicates the bytes to be lexed for the token and an
149/// instantiation location that indicates where all lexed tokens should be
150/// "expanded from".
151///
152/// FIXME: It would really be nice to make _Pragma just be a wrapper around a
153/// normal lexer that remaps tokens as they fly by. This would require making
154/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer
155/// interface that could handle this stuff. This would pull GetMappedTokenLoc
156/// out of the critical path of the lexer!
157///
Mike Stump1eb44332009-09-09 15:08:12 +0000158Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000159 SourceLocation InstantiationLocStart,
160 SourceLocation InstantiationLocEnd,
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000161 unsigned TokLen, Preprocessor &PP) {
Chris Lattner42e00d12009-01-17 08:27:52 +0000162 SourceManager &SM = PP.getSourceManager();
Chris Lattner42e00d12009-01-17 08:27:52 +0000163
164 // Create the lexer as if we were going to lex the file normally.
Chris Lattnera11d6172009-01-19 07:46:45 +0000165 FileID SpellingFID = SM.getFileID(SpellingLoc);
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000166 Lexer *L = new Lexer(SpellingFID, PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Chris Lattner42e00d12009-01-17 08:27:52 +0000168 // Now that the lexer is created, change the start/end locations so that we
169 // just lex the subsection of the file that we want. This is lexing from a
170 // scratch buffer.
171 const char *StrData = SM.getCharacterData(SpellingLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000172
Chris Lattner42e00d12009-01-17 08:27:52 +0000173 L->BufferPtr = StrData;
174 L->BufferEnd = StrData+TokLen;
Chris Lattner1fa49532009-03-08 08:08:45 +0000175 assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
Chris Lattner42e00d12009-01-17 08:27:52 +0000176
177 // Set the SourceLocation with the remapping information. This ensures that
178 // GetMappedTokenLoc will remap the tokens as they are lexed.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000179 L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
Chris Lattnere7fb4842009-02-15 20:52:18 +0000180 InstantiationLocStart,
181 InstantiationLocEnd, TokLen);
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Chris Lattner42e00d12009-01-17 08:27:52 +0000183 // Ensure that the lexer thinks it is inside a directive, so that end \n will
184 // return an EOM token.
185 L->ParsingPreprocessorDirective = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Chris Lattner42e00d12009-01-17 08:27:52 +0000187 // This lexer really is for _Pragma.
188 L->Is_PragmaLexer = true;
189 return L;
190}
191
Chris Lattner168ae2d2007-10-17 20:41:00 +0000192
Reid Spencer5f016e22007-07-11 17:01:13 +0000193/// Stringify - Convert the specified string into a C string, with surrounding
194/// ""'s, and with escaped \ and " characters.
195std::string Lexer::Stringify(const std::string &Str, bool Charify) {
196 std::string Result = Str;
197 char Quote = Charify ? '\'' : '"';
198 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
199 if (Result[i] == '\\' || Result[i] == Quote) {
200 Result.insert(Result.begin()+i, '\\');
201 ++i; ++e;
202 }
203 }
204 return Result;
205}
206
Chris Lattnerd8e30832007-07-24 06:57:14 +0000207/// Stringify - Convert the specified string into a C string by escaping '\'
208/// and " characters. This does not add surrounding ""'s to the string.
209void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
210 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
211 if (Str[i] == '\\' || Str[i] == '"') {
212 Str.insert(Str.begin()+i, '\\');
213 ++i; ++e;
214 }
215 }
216}
217
Reid Spencer5f016e22007-07-11 17:01:13 +0000218
Chris Lattner9a611942007-10-17 21:18:47 +0000219/// MeasureTokenLength - Relex the token at the specified location and return
220/// its length in bytes in the input file. If the token needs cleaning (e.g.
221/// includes a trigraph or an escaped newline) then this count includes bytes
222/// that are part of that.
223unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
Chris Lattner2c78b872009-04-14 23:22:57 +0000224 const SourceManager &SM,
225 const LangOptions &LangOpts) {
Chris Lattner9a611942007-10-17 21:18:47 +0000226 // TODO: this could be special cased for common tokens like identifiers, ')',
227 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
Mike Stump1eb44332009-09-09 15:08:12 +0000228 // all obviously single-char tokens. This could use
Chris Lattner9a611942007-10-17 21:18:47 +0000229 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
230 // something.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000231
232 // If this comes from a macro expansion, we really do want the macro name, not
233 // the token this macro expanded to.
Chris Lattner363fdc22009-01-26 22:24:27 +0000234 Loc = SM.getInstantiationLoc(Loc);
235 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
Chris Lattner83503942009-01-17 08:30:10 +0000236 std::pair<const char *,const char *> Buffer = SM.getBufferData(LocInfo.first);
237 const char *StrData = Buffer.first+LocInfo.second;
238
Chris Lattner9a611942007-10-17 21:18:47 +0000239 // Create a lexer starting at the beginning of this token.
Chris Lattnerde96c0f2009-01-17 07:42:27 +0000240 Lexer TheLexer(Loc, LangOpts, Buffer.first, StrData, Buffer.second);
Chris Lattner9a611942007-10-17 21:18:47 +0000241 Token TheTok;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000242 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner9a611942007-10-17 21:18:47 +0000243 return TheTok.getLength();
244}
245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246//===----------------------------------------------------------------------===//
247// Character information.
248//===----------------------------------------------------------------------===//
249
Reid Spencer5f016e22007-07-11 17:01:13 +0000250enum {
251 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
252 CHAR_VERT_WS = 0x02, // '\r', '\n'
253 CHAR_LETTER = 0x04, // a-z,A-Z
254 CHAR_NUMBER = 0x08, // 0-9
255 CHAR_UNDER = 0x10, // _
256 CHAR_PERIOD = 0x20 // .
257};
258
Chris Lattner03b98662009-07-07 17:09:54 +0000259// Statically initialize CharInfo table based on ASCII character set
260// Reference: FreeBSD 7.2 /usr/share/misc/ascii
261static const unsigned char CharInfo[256] =
262{
263// 0 NUL 1 SOH 2 STX 3 ETX
264// 4 EOT 5 ENQ 6 ACK 7 BEL
265 0 , 0 , 0 , 0 ,
266 0 , 0 , 0 , 0 ,
267// 8 BS 9 HT 10 NL 11 VT
268//12 NP 13 CR 14 SO 15 SI
269 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS,
270 CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 ,
271//16 DLE 17 DC1 18 DC2 19 DC3
272//20 DC4 21 NAK 22 SYN 23 ETB
273 0 , 0 , 0 , 0 ,
274 0 , 0 , 0 , 0 ,
275//24 CAN 25 EM 26 SUB 27 ESC
276//28 FS 29 GS 30 RS 31 US
277 0 , 0 , 0 , 0 ,
278 0 , 0 , 0 , 0 ,
279//32 SP 33 ! 34 " 35 #
280//36 $ 37 % 38 & 39 '
281 CHAR_HORZ_WS, 0 , 0 , 0 ,
282 0 , 0 , 0 , 0 ,
283//40 ( 41 ) 42 * 43 +
284//44 , 45 - 46 . 47 /
285 0 , 0 , 0 , 0 ,
286 0 , 0 , CHAR_PERIOD , 0 ,
287//48 0 49 1 50 2 51 3
288//52 4 53 5 54 6 55 7
289 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
290 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
291//56 8 57 9 58 : 59 ;
292//60 < 61 = 62 > 63 ?
293 CHAR_NUMBER , CHAR_NUMBER , 0 , 0 ,
294 0 , 0 , 0 , 0 ,
295//64 @ 65 A 66 B 67 C
296//68 D 69 E 70 F 71 G
297 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
298 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
299//72 H 73 I 74 J 75 K
300//76 L 77 M 78 N 79 O
301 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
302 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
303//80 P 81 Q 82 R 83 S
304//84 T 85 U 86 V 87 W
305 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
306 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
307//88 X 89 Y 90 Z 91 [
308//92 \ 93 ] 94 ^ 95 _
309 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
310 0 , 0 , 0 , CHAR_UNDER ,
311//96 ` 97 a 98 b 99 c
312//100 d 101 e 102 f 103 g
313 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
314 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
315//104 h 105 i 106 j 107 k
316//108 l 109 m 110 n 111 o
317 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
318 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
319//112 p 113 q 114 r 115 s
320//116 t 117 u 118 v 119 w
321 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
322 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
323//120 x 121 y 122 z 123 {
324//124 | 125 } 126 ~ 127 DEL
325 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
326 0 , 0 , 0 , 0
327};
328
Reid Spencer5f016e22007-07-11 17:01:13 +0000329static void InitCharacterInfo() {
330 static bool isInited = false;
331 if (isInited) return;
Chris Lattner03b98662009-07-07 17:09:54 +0000332 // check the statically-initialized CharInfo table
333 assert(CHAR_HORZ_WS == CharInfo[(int)' ']);
334 assert(CHAR_HORZ_WS == CharInfo[(int)'\t']);
335 assert(CHAR_HORZ_WS == CharInfo[(int)'\f']);
336 assert(CHAR_HORZ_WS == CharInfo[(int)'\v']);
337 assert(CHAR_VERT_WS == CharInfo[(int)'\n']);
338 assert(CHAR_VERT_WS == CharInfo[(int)'\r']);
339 assert(CHAR_UNDER == CharInfo[(int)'_']);
340 assert(CHAR_PERIOD == CharInfo[(int)'.']);
341 for (unsigned i = 'a'; i <= 'z'; ++i) {
342 assert(CHAR_LETTER == CharInfo[i]);
343 assert(CHAR_LETTER == CharInfo[i+'A'-'a']);
344 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000345 for (unsigned i = '0'; i <= '9'; ++i)
Chris Lattner03b98662009-07-07 17:09:54 +0000346 assert(CHAR_NUMBER == CharInfo[i]);
347 isInited = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000348}
349
Chris Lattner03b98662009-07-07 17:09:54 +0000350
Reid Spencer5f016e22007-07-11 17:01:13 +0000351/// isIdentifierBody - Return true if this is the body character of an
352/// identifier, which is [a-zA-Z0-9_].
353static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000354 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000355}
356
357/// isHorizontalWhitespace - Return true if this character is horizontal
358/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
359static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000360 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000361}
362
363/// isWhitespace - Return true if this character is horizontal or vertical
364/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
365/// for '\0'.
366static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000367 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000368}
369
370/// isNumberBody - Return true if this is the body character of an
371/// preprocessing number, which is [a-zA-Z0-9_.].
372static inline bool isNumberBody(unsigned char c) {
Mike Stump1eb44332009-09-09 15:08:12 +0000373 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000374 true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000375}
376
377
378//===----------------------------------------------------------------------===//
379// Diagnostics forwarding code.
380//===----------------------------------------------------------------------===//
381
Chris Lattner409a0362007-07-22 18:38:25 +0000382/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
383/// lexer buffer was all instantiated at a single point, perform the mapping.
384/// This is currently only used for _Pragma implementation, so it is the slow
385/// path of the hot getSourceLocation method. Do not allow it to be inlined.
386static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
387 SourceLocation FileLoc,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000388 unsigned CharNo,
389 unsigned TokLen) DISABLE_INLINE;
Chris Lattner409a0362007-07-22 18:38:25 +0000390static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
391 SourceLocation FileLoc,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000392 unsigned CharNo, unsigned TokLen) {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000393 assert(FileLoc.isMacroID() && "Must be an instantiation");
Mike Stump1eb44332009-09-09 15:08:12 +0000394
Chris Lattner409a0362007-07-22 18:38:25 +0000395 // Otherwise, we're lexing "mapped tokens". This is used for things like
396 // _Pragma handling. Combine the instantiation location of FileLoc with the
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000397 // spelling location.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000398 SourceManager &SM = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000400 // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000401 // characters come from spelling(FileLoc)+Offset.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000402 SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000403 SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattnere7fb4842009-02-15 20:52:18 +0000405 // Figure out the expansion loc range, which is the range covered by the
406 // original _Pragma(...) sequence.
407 std::pair<SourceLocation,SourceLocation> II =
408 SM.getImmediateInstantiationRange(FileLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattnere7fb4842009-02-15 20:52:18 +0000410 return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
Chris Lattner409a0362007-07-22 18:38:25 +0000411}
412
Reid Spencer5f016e22007-07-11 17:01:13 +0000413/// getSourceLocation - Return a source location identifier for the specified
414/// offset in the current file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000415SourceLocation Lexer::getSourceLocation(const char *Loc,
416 unsigned TokLen) const {
Chris Lattner448cec42007-07-22 18:44:36 +0000417 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000418 "Location out of range for this buffer!");
Chris Lattner9dc1f532007-07-20 16:37:10 +0000419
420 // In the normal case, we're just lexing from a simple file buffer, return
421 // the file id from FileLoc with the offset specified.
Chris Lattner448cec42007-07-22 18:44:36 +0000422 unsigned CharNo = Loc-BufferStart;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000423 if (FileLoc.isFileID())
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000424 return FileLoc.getFileLocWithOffset(CharNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Chris Lattner2b2453a2009-01-17 06:22:33 +0000426 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
427 // tokens are lexed from where the _Pragma was defined.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000428 assert(PP && "This doesn't work on raw lexers");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000429 return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
Reid Spencer5f016e22007-07-11 17:01:13 +0000430}
431
Reid Spencer5f016e22007-07-11 17:01:13 +0000432/// Diag - Forwarding function for diagnostics. This translate a source
433/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000434DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner3692b092008-11-18 07:59:24 +0000435 return PP->Diag(getSourceLocation(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000436}
Reid Spencer5f016e22007-07-11 17:01:13 +0000437
438//===----------------------------------------------------------------------===//
439// Trigraph and Escaped Newline Handling Code.
440//===----------------------------------------------------------------------===//
441
442/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
443/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
444static char GetTrigraphCharForLetter(char Letter) {
445 switch (Letter) {
446 default: return 0;
447 case '=': return '#';
448 case ')': return ']';
449 case '(': return '[';
450 case '!': return '|';
451 case '\'': return '^';
452 case '>': return '}';
453 case '/': return '\\';
454 case '<': return '{';
455 case '-': return '~';
456 }
457}
458
459/// DecodeTrigraphChar - If the specified character is a legal trigraph when
460/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
461/// return the result character. Finally, emit a warning about trigraph use
462/// whether trigraphs are enabled or not.
463static char DecodeTrigraphChar(const char *CP, Lexer *L) {
464 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner3692b092008-11-18 07:59:24 +0000465 if (!Res || !L) return Res;
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Chris Lattner3692b092008-11-18 07:59:24 +0000467 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +0000468 if (!L->isLexingRawMode())
469 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner3692b092008-11-18 07:59:24 +0000470 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 }
Mike Stump1eb44332009-09-09 15:08:12 +0000472
Chris Lattner74d15df2008-11-22 02:02:22 +0000473 if (!L->isLexingRawMode())
474 L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000475 return Res;
476}
477
Chris Lattner24f0e482009-04-18 22:05:41 +0000478/// getEscapedNewLineSize - Return the size of the specified escaped newline,
479/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a
Mike Stump1eb44332009-09-09 15:08:12 +0000480/// trigraph equivalent on entry to this function.
Chris Lattner24f0e482009-04-18 22:05:41 +0000481unsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
482 unsigned Size = 0;
483 while (isWhitespace(Ptr[Size])) {
484 ++Size;
Mike Stump1eb44332009-09-09 15:08:12 +0000485
Chris Lattner24f0e482009-04-18 22:05:41 +0000486 if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
487 continue;
488
489 // If this is a \r\n or \n\r, skip the other half.
490 if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
491 Ptr[Size-1] != Ptr[Size])
492 ++Size;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Chris Lattner24f0e482009-04-18 22:05:41 +0000494 return Size;
Mike Stump1eb44332009-09-09 15:08:12 +0000495 }
496
Chris Lattner24f0e482009-04-18 22:05:41 +0000497 // Not an escaped newline, must be a \t or something else.
498 return 0;
499}
500
Chris Lattner03374952009-04-18 22:27:02 +0000501/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
502/// them), skip over them and return the first non-escaped-newline found,
503/// otherwise return P.
504const char *Lexer::SkipEscapedNewLines(const char *P) {
505 while (1) {
506 const char *AfterEscape;
507 if (*P == '\\') {
508 AfterEscape = P+1;
509 } else if (*P == '?') {
510 // If not a trigraph for escape, bail out.
511 if (P[1] != '?' || P[2] != '/')
512 return P;
513 AfterEscape = P+3;
514 } else {
515 return P;
516 }
Mike Stump1eb44332009-09-09 15:08:12 +0000517
Chris Lattner03374952009-04-18 22:27:02 +0000518 unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
519 if (NewLineSize == 0) return P;
520 P = AfterEscape+NewLineSize;
521 }
522}
523
Chris Lattner24f0e482009-04-18 22:05:41 +0000524
Reid Spencer5f016e22007-07-11 17:01:13 +0000525/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
526/// get its size, and return it. This is tricky in several cases:
527/// 1. If currently at the start of a trigraph, we warn about the trigraph,
528/// then either return the trigraph (skipping 3 chars) or the '?',
529/// depending on whether trigraphs are enabled or not.
530/// 2. If this is an escaped newline (potentially with whitespace between
531/// the backslash and newline), implicitly skip the newline and return
532/// the char after it.
533/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
534///
535/// This handles the slow/uncommon case of the getCharAndSize method. Here we
536/// know that we can accumulate into Size, and that we have already incremented
537/// Ptr by Size bytes.
538///
539/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
540/// be updated to match.
541///
542char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattnerd2177732007-07-20 16:59:19 +0000543 Token *Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000544 // If we have a slash, look for an escaped newline.
545 if (Ptr[0] == '\\') {
546 ++Size;
547 ++Ptr;
548Slash:
549 // Common case, backslash-char where the char is not whitespace.
550 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Chris Lattner5636a3b2009-06-23 05:15:06 +0000552 // See if we have optional whitespace characters between the slash and
553 // newline.
Chris Lattner24f0e482009-04-18 22:05:41 +0000554 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
555 // Remember that this token needs to be cleaned.
556 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000557
Chris Lattner24f0e482009-04-18 22:05:41 +0000558 // Warn if there was whitespace between the backslash and newline.
Chris Lattner5636a3b2009-06-23 05:15:06 +0000559 if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
Chris Lattner24f0e482009-04-18 22:05:41 +0000560 Diag(Ptr, diag::backslash_newline_space);
Mike Stump1eb44332009-09-09 15:08:12 +0000561
Chris Lattner24f0e482009-04-18 22:05:41 +0000562 // Found backslash<whitespace><newline>. Parse the char after it.
563 Size += EscapedNewLineSize;
564 Ptr += EscapedNewLineSize;
565 // Use slow version to accumulate a correct size field.
566 return getCharAndSizeSlow(Ptr, Size, Tok);
567 }
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 // Otherwise, this is not an escaped newline, just return the slash.
570 return '\\';
571 }
Mike Stump1eb44332009-09-09 15:08:12 +0000572
Reid Spencer5f016e22007-07-11 17:01:13 +0000573 // If this is a trigraph, process it.
574 if (Ptr[0] == '?' && Ptr[1] == '?') {
575 // If this is actually a legal trigraph (not something like "??x"), emit
576 // a trigraph warning. If so, and if trigraphs are enabled, return it.
577 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
578 // Remember that this token needs to be cleaned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000579 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000580
581 Ptr += 3;
582 Size += 3;
583 if (C == '\\') goto Slash;
584 return C;
585 }
586 }
Mike Stump1eb44332009-09-09 15:08:12 +0000587
Reid Spencer5f016e22007-07-11 17:01:13 +0000588 // If this is neither, return a single character.
589 ++Size;
590 return *Ptr;
591}
592
593
594/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
595/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
596/// and that we have already incremented Ptr by Size bytes.
597///
598/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
599/// be updated to match.
600char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
601 const LangOptions &Features) {
602 // If we have a slash, look for an escaped newline.
603 if (Ptr[0] == '\\') {
604 ++Size;
605 ++Ptr;
606Slash:
607 // Common case, backslash-char where the char is not whitespace.
608 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Reid Spencer5f016e22007-07-11 17:01:13 +0000610 // See if we have optional whitespace characters followed by a newline.
Chris Lattner24f0e482009-04-18 22:05:41 +0000611 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
612 // Found backslash<whitespace><newline>. Parse the char after it.
613 Size += EscapedNewLineSize;
614 Ptr += EscapedNewLineSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Chris Lattner24f0e482009-04-18 22:05:41 +0000616 // Use slow version to accumulate a correct size field.
617 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
618 }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 // Otherwise, this is not an escaped newline, just return the slash.
621 return '\\';
622 }
Mike Stump1eb44332009-09-09 15:08:12 +0000623
Reid Spencer5f016e22007-07-11 17:01:13 +0000624 // If this is a trigraph, process it.
625 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
626 // If this is actually a legal trigraph (not something like "??x"), return
627 // it.
628 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
629 Ptr += 3;
630 Size += 3;
631 if (C == '\\') goto Slash;
632 return C;
633 }
634 }
Mike Stump1eb44332009-09-09 15:08:12 +0000635
Reid Spencer5f016e22007-07-11 17:01:13 +0000636 // If this is neither, return a single character.
637 ++Size;
638 return *Ptr;
639}
640
641//===----------------------------------------------------------------------===//
642// Helper methods for lexing.
643//===----------------------------------------------------------------------===//
644
Chris Lattnerd2177732007-07-20 16:59:19 +0000645void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
647 unsigned Size;
648 unsigned char C = *CurPtr++;
649 while (isIdentifierBody(C)) {
650 C = *CurPtr++;
651 }
652 --CurPtr; // Back up over the skipped character.
653
654 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
655 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
656 // FIXME: UCNs.
657 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
658FinishIdentifier:
659 const char *IdStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000660 FormTokenWithChars(Result, CurPtr, tok::identifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 // If we are in raw mode, return this identifier raw. There is no need to
663 // look up identifier information or attempt to macro expand it.
664 if (LexingRawMode) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Reid Spencer5f016e22007-07-11 17:01:13 +0000666 // Fill in Result.IdentifierInfo, looking up the identifier in the
667 // identifier table.
Chris Lattnerd1186fa2009-01-21 07:45:14 +0000668 IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Chris Lattner863c4862009-01-23 18:35:48 +0000670 // Change the kind of this identifier to the appropriate token kind, e.g.
671 // turning "for" into a keyword.
672 Result.setKind(II->getTokenID());
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Reid Spencer5f016e22007-07-11 17:01:13 +0000674 // Finally, now that we know we have an identifier, pass this off to the
675 // preprocessor, which may macro expand it or something.
Chris Lattnerd1186fa2009-01-21 07:45:14 +0000676 if (II->isHandleIdentifierCase())
Chris Lattner6a170eb2009-01-21 07:43:11 +0000677 PP->HandleIdentifier(Result);
678 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 }
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Reid Spencer5f016e22007-07-11 17:01:13 +0000681 // Otherwise, $,\,? in identifier found. Enter slower path.
Mike Stump1eb44332009-09-09 15:08:12 +0000682
Reid Spencer5f016e22007-07-11 17:01:13 +0000683 C = getCharAndSize(CurPtr, Size);
684 while (1) {
685 if (C == '$') {
686 // If we hit a $ and they are not supported in identifiers, we are done.
687 if (!Features.DollarIdents) goto FinishIdentifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000688
Reid Spencer5f016e22007-07-11 17:01:13 +0000689 // Otherwise, emit a diagnostic and continue.
Chris Lattner74d15df2008-11-22 02:02:22 +0000690 if (!isLexingRawMode())
691 Diag(CurPtr, diag::ext_dollar_in_identifier);
Reid Spencer5f016e22007-07-11 17:01:13 +0000692 CurPtr = ConsumeChar(CurPtr, Size, Result);
693 C = getCharAndSize(CurPtr, Size);
694 continue;
695 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
696 // Found end of identifier.
697 goto FinishIdentifier;
698 }
699
700 // Otherwise, this character is good, consume it.
701 CurPtr = ConsumeChar(CurPtr, Size, Result);
702
703 C = getCharAndSize(CurPtr, Size);
704 while (isIdentifierBody(C)) { // FIXME: UCNs.
705 CurPtr = ConsumeChar(CurPtr, Size, Result);
706 C = getCharAndSize(CurPtr, Size);
707 }
708 }
709}
710
711
Nate Begeman5253c7f2008-04-14 02:26:39 +0000712/// LexNumericConstant - Lex the remainder of a integer or floating point
Reid Spencer5f016e22007-07-11 17:01:13 +0000713/// constant. From[-1] is the first character lexed. Return the end of the
714/// constant.
Chris Lattnerd2177732007-07-20 16:59:19 +0000715void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000716 unsigned Size;
717 char C = getCharAndSize(CurPtr, Size);
718 char PrevCh = 0;
719 while (isNumberBody(C)) { // FIXME: UCNs?
720 CurPtr = ConsumeChar(CurPtr, Size, Result);
721 PrevCh = C;
722 C = getCharAndSize(CurPtr, Size);
723 }
Mike Stump1eb44332009-09-09 15:08:12 +0000724
Reid Spencer5f016e22007-07-11 17:01:13 +0000725 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
726 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
727 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
728
729 // If we have a hex FP constant, continue.
Eli Friedmanf01fdff2009-04-28 00:51:18 +0000730 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p'))
Reid Spencer5f016e22007-07-11 17:01:13 +0000731 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
Mike Stump1eb44332009-09-09 15:08:12 +0000732
Reid Spencer5f016e22007-07-11 17:01:13 +0000733 // Update the location of token as well as BufferPtr.
Chris Lattner47246be2009-01-26 19:29:26 +0000734 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000735 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Chris Lattner47246be2009-01-26 19:29:26 +0000736 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000737}
738
739/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
740/// either " or L".
Chris Lattnerd88dc482008-10-12 04:05:48 +0000741void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000742 const char *NulCharacter = 0; // Does this string contain the \0 character?
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Reid Spencer5f016e22007-07-11 17:01:13 +0000744 char C = getAndAdvanceChar(CurPtr, Result);
745 while (C != '"') {
746 // Skip escaped characters.
747 if (C == '\\') {
748 // Skip the escaped character.
749 C = getAndAdvanceChar(CurPtr, Result);
750 } else if (C == '\n' || C == '\r' || // Newline.
751 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner33ab3f62009-03-18 21:10:12 +0000752 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner74d15df2008-11-22 02:02:22 +0000753 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000754 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000755 return;
756 } else if (C == 0) {
757 NulCharacter = CurPtr-1;
758 }
759 C = getAndAdvanceChar(CurPtr, Result);
760 }
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000763 if (NulCharacter && !isLexingRawMode())
764 Diag(NulCharacter, diag::null_in_string);
Reid Spencer5f016e22007-07-11 17:01:13 +0000765
Reid Spencer5f016e22007-07-11 17:01:13 +0000766 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner47246be2009-01-26 19:29:26 +0000767 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000768 FormTokenWithChars(Result, CurPtr,
769 Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +0000770 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000771}
772
773/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
774/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattnerd2177732007-07-20 16:59:19 +0000775void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000776 const char *NulCharacter = 0; // Does this string contain the \0 character?
Chris Lattner9cb51ce2009-04-17 23:56:52 +0000777 const char *AfterLessPos = CurPtr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000778 char C = getAndAdvanceChar(CurPtr, Result);
779 while (C != '>') {
780 // Skip escaped characters.
781 if (C == '\\') {
782 // Skip the escaped character.
783 C = getAndAdvanceChar(CurPtr, Result);
784 } else if (C == '\n' || C == '\r' || // Newline.
785 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner9cb51ce2009-04-17 23:56:52 +0000786 // If the filename is unterminated, then it must just be a lone <
787 // character. Return this as such.
788 FormTokenWithChars(Result, AfterLessPos, tok::less);
Reid Spencer5f016e22007-07-11 17:01:13 +0000789 return;
790 } else if (C == 0) {
791 NulCharacter = CurPtr-1;
792 }
793 C = getAndAdvanceChar(CurPtr, Result);
794 }
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Reid Spencer5f016e22007-07-11 17:01:13 +0000796 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000797 if (NulCharacter && !isLexingRawMode())
798 Diag(NulCharacter, diag::null_in_string);
Mike Stump1eb44332009-09-09 15:08:12 +0000799
Reid Spencer5f016e22007-07-11 17:01:13 +0000800 // Update the location of token as well as BufferPtr.
Chris Lattner47246be2009-01-26 19:29:26 +0000801 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000802 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +0000803 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000804}
805
806
807/// LexCharConstant - Lex the remainder of a character constant, after having
808/// lexed either ' or L'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000809void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000810 const char *NulCharacter = 0; // Does this character contain the \0 character?
811
812 // Handle the common case of 'x' and '\y' efficiently.
813 char C = getAndAdvanceChar(CurPtr, Result);
814 if (C == '\'') {
Chris Lattner33ab3f62009-03-18 21:10:12 +0000815 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner74d15df2008-11-22 02:02:22 +0000816 Diag(BufferPtr, diag::err_empty_character);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000817 FormTokenWithChars(Result, CurPtr, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000818 return;
819 } else if (C == '\\') {
820 // Skip the escaped character.
821 // FIXME: UCN's.
822 C = getAndAdvanceChar(CurPtr, Result);
823 }
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Reid Spencer5f016e22007-07-11 17:01:13 +0000825 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
826 ++CurPtr;
827 } else {
828 // Fall back on generic code for embedded nulls, newlines, wide chars.
829 do {
830 // Skip escaped characters.
831 if (C == '\\') {
832 // Skip the escaped character.
833 C = getAndAdvanceChar(CurPtr, Result);
834 } else if (C == '\n' || C == '\r' || // Newline.
835 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner33ab3f62009-03-18 21:10:12 +0000836 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner74d15df2008-11-22 02:02:22 +0000837 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000838 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000839 return;
840 } else if (C == 0) {
841 NulCharacter = CurPtr-1;
842 }
843 C = getAndAdvanceChar(CurPtr, Result);
844 } while (C != '\'');
845 }
Mike Stump1eb44332009-09-09 15:08:12 +0000846
Chris Lattner74d15df2008-11-22 02:02:22 +0000847 if (NulCharacter && !isLexingRawMode())
848 Diag(NulCharacter, diag::null_in_char);
Reid Spencer5f016e22007-07-11 17:01:13 +0000849
Reid Spencer5f016e22007-07-11 17:01:13 +0000850 // Update the location of token as well as BufferPtr.
Chris Lattner47246be2009-01-26 19:29:26 +0000851 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000852 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Chris Lattner47246be2009-01-26 19:29:26 +0000853 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000854}
855
856/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
857/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattnerd88dc482008-10-12 04:05:48 +0000858///
859/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
860///
861bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000862 // Whitespace - Skip it, then return the token after the whitespace.
863 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
864 while (1) {
865 // Skip horizontal whitespace very aggressively.
866 while (isHorizontalWhitespace(Char))
867 Char = *++CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Daniel Dunbarddd3e8b2008-11-25 00:20:22 +0000869 // Otherwise if we have something other than whitespace, we're done.
Reid Spencer5f016e22007-07-11 17:01:13 +0000870 if (Char != '\n' && Char != '\r')
871 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 if (ParsingPreprocessorDirective) {
874 // End of preprocessor directive line, let LexTokenInternal handle this.
875 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +0000876 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Reid Spencer5f016e22007-07-11 17:01:13 +0000879 // ok, but handle newline.
880 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +0000881 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000882 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +0000883 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000884 Char = *++CurPtr;
885 }
886
887 // If this isn't immediately after a newline, there is leading space.
888 char PrevChar = CurPtr[-1];
889 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattnerd2177732007-07-20 16:59:19 +0000890 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000891
Chris Lattnerd88dc482008-10-12 04:05:48 +0000892 // If the client wants us to return whitespace, return it now.
893 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +0000894 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +0000895 return true;
896 }
Mike Stump1eb44332009-09-09 15:08:12 +0000897
Reid Spencer5f016e22007-07-11 17:01:13 +0000898 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +0000899 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000900}
901
902// SkipBCPLComment - We have just read the // characters from input. Skip until
903// we find the newline character thats terminate the comment. Then update
Chris Lattner2d381892008-10-12 04:15:42 +0000904/// BufferPtr and return. If we're in KeepCommentMode, this will form the token
905/// and return true.
Chris Lattnerd2177732007-07-20 16:59:19 +0000906bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000907 // If BCPL comments aren't explicitly enabled for this language, emit an
908 // extension warning.
Chris Lattner74d15df2008-11-22 02:02:22 +0000909 if (!Features.BCPLComment && !isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000910 Diag(BufferPtr, diag::ext_bcpl_comment);
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Reid Spencer5f016e22007-07-11 17:01:13 +0000912 // Mark them enabled so we only emit one warning for this translation
913 // unit.
914 Features.BCPLComment = true;
915 }
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Reid Spencer5f016e22007-07-11 17:01:13 +0000917 // Scan over the body of the comment. The common case, when scanning, is that
918 // the comment contains normal ascii characters with nothing interesting in
919 // them. As such, optimize for this case with the inner loop.
920 char C;
921 do {
922 C = *CurPtr;
923 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
924 // If we find a \n character, scan backwards, checking to see if it's an
925 // escaped newline, like we do for block comments.
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Reid Spencer5f016e22007-07-11 17:01:13 +0000927 // Skip over characters in the fast loop.
928 while (C != 0 && // Potentially EOF.
929 C != '\\' && // Potentially escaped newline.
930 C != '?' && // Potentially trigraph.
931 C != '\n' && C != '\r') // Newline or DOS-style newline.
932 C = *++CurPtr;
933
934 // If this is a newline, we're done.
935 if (C == '\n' || C == '\r')
936 break; // Found the newline? Break out!
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000939 // properly decode the character. Read it in raw mode to avoid emitting
940 // diagnostics about things like trigraphs. If we see an escaped newline,
941 // we'll handle it below.
Reid Spencer5f016e22007-07-11 17:01:13 +0000942 const char *OldPtr = CurPtr;
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000943 bool OldRawMode = isLexingRawMode();
944 LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000945 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000946 LexingRawMode = OldRawMode;
Chris Lattneread616c2009-04-05 00:26:41 +0000947
948 // If the char that we finally got was a \n, then we must have had something
949 // like \<newline><newline>. We don't want to have consumed the second
950 // newline, we want CurPtr, to end up pointing to it down below.
951 if (C == '\n' || C == '\r') {
952 --CurPtr;
953 C = 'x'; // doesn't matter what this is.
954 }
Mike Stump1eb44332009-09-09 15:08:12 +0000955
Reid Spencer5f016e22007-07-11 17:01:13 +0000956 // If we read multiple characters, and one of those characters was a \r or
957 // \n, then we had an escaped newline within the comment. Emit diagnostic
958 // unless the next line is also a // comment.
959 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
960 for (; OldPtr != CurPtr; ++OldPtr)
961 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
962 // Okay, we found a // comment that ends in a newline, if the next
963 // line is also a // comment, but has spaces, don't emit a diagnostic.
964 if (isspace(C)) {
965 const char *ForwardPtr = CurPtr;
966 while (isspace(*ForwardPtr)) // Skip whitespace.
967 ++ForwardPtr;
968 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
969 break;
970 }
Mike Stump1eb44332009-09-09 15:08:12 +0000971
Chris Lattner74d15df2008-11-22 02:02:22 +0000972 if (!isLexingRawMode())
973 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 break;
975 }
976 }
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Reid Spencer5f016e22007-07-11 17:01:13 +0000978 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
979 } while (C != '\n' && C != '\r');
980
981 // Found but did not consume the newline.
Douglas Gregor2e222532009-07-02 17:08:52 +0000982 if (PP)
Mike Stump1eb44332009-09-09 15:08:12 +0000983 PP->HandleComment(SourceRange(getSourceLocation(BufferPtr),
Douglas Gregor2e222532009-07-02 17:08:52 +0000984 getSourceLocation(CurPtr)));
Mike Stump1eb44332009-09-09 15:08:12 +0000985
Reid Spencer5f016e22007-07-11 17:01:13 +0000986 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +0000987 if (inKeepCommentMode())
Reid Spencer5f016e22007-07-11 17:01:13 +0000988 return SaveBCPLComment(Result, CurPtr);
989
990 // If we are inside a preprocessor directive and we see the end of line,
991 // return immediately, so that the lexer can return this as an EOM token.
992 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
993 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +0000994 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000995 }
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Reid Spencer5f016e22007-07-11 17:01:13 +0000997 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner7a4f0042008-10-12 00:23:07 +0000998 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattnerd88dc482008-10-12 04:05:48 +0000999 // contribute to another token), it isn't needed for correctness. Note that
1000 // this is ok even in KeepWhitespaceMode, because we would have returned the
1001 /// comment above in that mode.
Reid Spencer5f016e22007-07-11 17:01:13 +00001002 ++CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Reid Spencer5f016e22007-07-11 17:01:13 +00001004 // The next returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +00001005 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001006 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +00001007 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001008 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001009 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001010}
1011
1012/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
1013/// an appropriate way and return it.
Chris Lattnerd2177732007-07-20 16:59:19 +00001014bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001015 // If we're not in a preprocessor directive, just return the // comment
1016 // directly.
1017 FormTokenWithChars(Result, CurPtr, tok::comment);
Mike Stump1eb44332009-09-09 15:08:12 +00001018
Chris Lattner9e6293d2008-10-12 04:51:35 +00001019 if (!ParsingPreprocessorDirective)
1020 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001021
Chris Lattner9e6293d2008-10-12 04:51:35 +00001022 // If this BCPL-style comment is in a macro definition, transmogrify it into
1023 // a C-style block comment.
1024 std::string Spelling = PP->getSpelling(Result);
1025 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
1026 Spelling[1] = '*'; // Change prefix to "/*".
1027 Spelling += "*/"; // add suffix.
Mike Stump1eb44332009-09-09 15:08:12 +00001028
Chris Lattner9e6293d2008-10-12 04:51:35 +00001029 Result.setKind(tok::comment);
Chris Lattner47246be2009-01-26 19:29:26 +00001030 PP->CreateString(&Spelling[0], Spelling.size(), Result,
1031 Result.getLocation());
Chris Lattner2d381892008-10-12 04:15:42 +00001032 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001033}
1034
1035/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
1036/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner47a2b402008-12-12 07:14:34 +00001037/// diagnostic if so. We know that the newline is inside of a block comment.
Mike Stump1eb44332009-09-09 15:08:12 +00001038static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
Reid Spencer5f016e22007-07-11 17:01:13 +00001039 Lexer *L) {
1040 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Mike Stump1eb44332009-09-09 15:08:12 +00001041
Reid Spencer5f016e22007-07-11 17:01:13 +00001042 // Back up off the newline.
1043 --CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Reid Spencer5f016e22007-07-11 17:01:13 +00001045 // If this is a two-character newline sequence, skip the other character.
1046 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
1047 // \n\n or \r\r -> not escaped newline.
1048 if (CurPtr[0] == CurPtr[1])
1049 return false;
1050 // \n\r or \r\n -> skip the newline.
1051 --CurPtr;
1052 }
Mike Stump1eb44332009-09-09 15:08:12 +00001053
Reid Spencer5f016e22007-07-11 17:01:13 +00001054 // If we have horizontal whitespace, skip over it. We allow whitespace
1055 // between the slash and newline.
1056 bool HasSpace = false;
1057 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
1058 --CurPtr;
1059 HasSpace = true;
1060 }
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Reid Spencer5f016e22007-07-11 17:01:13 +00001062 // If we have a slash, we know this is an escaped newline.
1063 if (*CurPtr == '\\') {
1064 if (CurPtr[-1] != '*') return false;
1065 } else {
1066 // It isn't a slash, is it the ?? / trigraph?
1067 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
1068 CurPtr[-3] != '*')
1069 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001070
Reid Spencer5f016e22007-07-11 17:01:13 +00001071 // This is the trigraph ending the comment. Emit a stern warning!
1072 CurPtr -= 2;
1073
1074 // If no trigraphs are enabled, warn that we ignored this trigraph and
1075 // ignore this * character.
1076 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001077 if (!L->isLexingRawMode())
1078 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 return false;
1080 }
Chris Lattner74d15df2008-11-22 02:02:22 +00001081 if (!L->isLexingRawMode())
1082 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001083 }
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 // Warn about having an escaped newline between the */ characters.
Chris Lattner74d15df2008-11-22 02:02:22 +00001086 if (!L->isLexingRawMode())
1087 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 // If there was space between the backslash and newline, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +00001090 if (HasSpace && !L->isLexingRawMode())
1091 L->Diag(CurPtr, diag::backslash_newline_space);
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Reid Spencer5f016e22007-07-11 17:01:13 +00001093 return true;
1094}
1095
1096#ifdef __SSE2__
1097#include <emmintrin.h>
1098#elif __ALTIVEC__
1099#include <altivec.h>
1100#undef bool
1101#endif
1102
1103/// SkipBlockComment - We have just read the /* characters from input. Read
1104/// until we find the */ characters that terminate the comment. Note that we
1105/// don't bother decoding trigraphs or escaped newlines in block comments,
1106/// because they cannot cause the comment to end. The only thing that can
1107/// happen is the comment could end with an escaped newline between the */ end
1108/// of comment.
Chris Lattner2d381892008-10-12 04:15:42 +00001109///
1110/// If KeepCommentMode is enabled, this forms a token from the comment and
1111/// returns true.
Chris Lattnerd2177732007-07-20 16:59:19 +00001112bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001113 // Scan one character past where we should, looking for a '/' character. Once
1114 // we find it, check to see if it was preceeded by a *. This common
1115 // optimization helps people who like to put a lot of * characters in their
1116 // comments.
Chris Lattner8146b682007-07-21 23:43:37 +00001117
1118 // The first character we get with newlines and trigraphs skipped to handle
1119 // the degenerate /*/ case below correctly if the * has an escaped newline
1120 // after it.
1121 unsigned CharSize;
1122 unsigned char C = getCharAndSize(CurPtr, CharSize);
1123 CurPtr += CharSize;
Reid Spencer5f016e22007-07-11 17:01:13 +00001124 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001125 if (!isLexingRawMode())
Chris Lattner0af57422008-10-12 01:31:51 +00001126 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001127 --CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001128
Chris Lattner31f0eca2008-10-12 04:19:49 +00001129 // KeepWhitespaceMode should return this broken comment as a token. Since
1130 // it isn't a well formed comment, just return it as an 'unknown' token.
1131 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001132 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001133 return true;
1134 }
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Chris Lattner31f0eca2008-10-12 04:19:49 +00001136 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001137 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001138 }
Mike Stump1eb44332009-09-09 15:08:12 +00001139
Chris Lattner8146b682007-07-21 23:43:37 +00001140 // Check to see if the first character after the '/*' is another /. If so,
1141 // then this slash does not end the block comment, it is part of it.
1142 if (C == '/')
1143 C = *CurPtr++;
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Reid Spencer5f016e22007-07-11 17:01:13 +00001145 while (1) {
1146 // Skip over all non-interesting characters until we find end of buffer or a
1147 // (probably ending) '/' character.
1148 if (CurPtr + 24 < BufferEnd) {
1149 // While not aligned to a 16-byte boundary.
1150 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1151 C = *CurPtr++;
Mike Stump1eb44332009-09-09 15:08:12 +00001152
Reid Spencer5f016e22007-07-11 17:01:13 +00001153 if (C == '/') goto FoundSlash;
1154
1155#ifdef __SSE2__
1156 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1157 '/', '/', '/', '/', '/', '/', '/', '/');
1158 while (CurPtr+16 <= BufferEnd &&
1159 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1160 CurPtr += 16;
1161#elif __ALTIVEC__
1162 __vector unsigned char Slashes = {
Mike Stump1eb44332009-09-09 15:08:12 +00001163 '/', '/', '/', '/', '/', '/', '/', '/',
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 '/', '/', '/', '/', '/', '/', '/', '/'
1165 };
1166 while (CurPtr+16 <= BufferEnd &&
1167 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1168 CurPtr += 16;
Mike Stump1eb44332009-09-09 15:08:12 +00001169#else
Reid Spencer5f016e22007-07-11 17:01:13 +00001170 // Scan for '/' quickly. Many block comments are very large.
1171 while (CurPtr[0] != '/' &&
1172 CurPtr[1] != '/' &&
1173 CurPtr[2] != '/' &&
1174 CurPtr[3] != '/' &&
1175 CurPtr+4 < BufferEnd) {
1176 CurPtr += 4;
1177 }
1178#endif
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 // It has to be one of the bytes scanned, increment to it and read one.
1181 C = *CurPtr++;
1182 }
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 // Loop to scan the remainder.
1185 while (C != '/' && C != '\0')
1186 C = *CurPtr++;
Mike Stump1eb44332009-09-09 15:08:12 +00001187
Reid Spencer5f016e22007-07-11 17:01:13 +00001188 FoundSlash:
1189 if (C == '/') {
1190 if (CurPtr[-2] == '*') // We found the final */. We're done!
1191 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001192
Reid Spencer5f016e22007-07-11 17:01:13 +00001193 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
1194 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
1195 // We found the final */, though it had an escaped newline between the
1196 // * and /. We're done!
1197 break;
1198 }
1199 }
1200 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1201 // If this is a /* inside of the comment, emit a warning. Don't do this
1202 // if this is a /*/, which will end the comment. This misses cases with
1203 // embedded escaped newlines, but oh well.
Chris Lattner74d15df2008-11-22 02:02:22 +00001204 if (!isLexingRawMode())
1205 Diag(CurPtr-1, diag::warn_nested_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 }
1207 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001208 if (!isLexingRawMode())
1209 Diag(BufferPtr, diag::err_unterminated_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001210 // Note: the user probably forgot a */. We could continue immediately
1211 // after the /*, but this would involve lexing a lot of what really is the
1212 // comment, which surely would confuse the parser.
Chris Lattner31f0eca2008-10-12 04:19:49 +00001213 --CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001214
Chris Lattner31f0eca2008-10-12 04:19:49 +00001215 // KeepWhitespaceMode should return this broken comment as a token. Since
1216 // it isn't a well formed comment, just return it as an 'unknown' token.
1217 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001218 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001219 return true;
1220 }
Mike Stump1eb44332009-09-09 15:08:12 +00001221
Chris Lattner31f0eca2008-10-12 04:19:49 +00001222 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001223 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001224 }
1225 C = *CurPtr++;
1226 }
Mike Stump1eb44332009-09-09 15:08:12 +00001227
1228 if (PP)
1229 PP->HandleComment(SourceRange(getSourceLocation(BufferPtr),
Douglas Gregor2e222532009-07-02 17:08:52 +00001230 getSourceLocation(CurPtr)));
1231
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +00001233 if (inKeepCommentMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001234 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattner2d381892008-10-12 04:15:42 +00001235 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001236 }
1237
1238 // It is common for the tokens immediately after a /**/ comment to be
1239 // whitespace. Instead of going through the big switch, handle it
Chris Lattnerd88dc482008-10-12 04:05:48 +00001240 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1241 // have already returned above with the comment as a token.
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001243 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001244 SkipWhitespace(Result, CurPtr+1);
Chris Lattner2d381892008-10-12 04:15:42 +00001245 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001246 }
1247
1248 // Otherwise, just return so that the next character will be lexed as a token.
1249 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001250 Result.setFlag(Token::LeadingSpace);
Chris Lattner2d381892008-10-12 04:15:42 +00001251 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001252}
1253
1254//===----------------------------------------------------------------------===//
1255// Primary Lexing Entry Points
1256//===----------------------------------------------------------------------===//
1257
Reid Spencer5f016e22007-07-11 17:01:13 +00001258/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1259/// uninterpreted string. This switches the lexer out of directive mode.
1260std::string Lexer::ReadToEndOfLine() {
1261 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1262 "Must be in a preprocessing directive!");
1263 std::string Result;
Chris Lattnerd2177732007-07-20 16:59:19 +00001264 Token Tmp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001265
1266 // CurPtr - Cache BufferPtr in an automatic variable.
1267 const char *CurPtr = BufferPtr;
1268 while (1) {
1269 char Char = getAndAdvanceChar(CurPtr, Tmp);
1270 switch (Char) {
1271 default:
1272 Result += Char;
1273 break;
1274 case 0: // Null.
1275 // Found end of file?
1276 if (CurPtr-1 != BufferEnd) {
1277 // Nope, normal character, continue.
1278 Result += Char;
1279 break;
1280 }
1281 // FALL THROUGH.
1282 case '\r':
1283 case '\n':
1284 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1285 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1286 BufferPtr = CurPtr-1;
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 // Next, lex the character, which should handle the EOM transition.
1289 Lex(Tmp);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001290 assert(Tmp.is(tok::eom) && "Unexpected token!");
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Reid Spencer5f016e22007-07-11 17:01:13 +00001292 // Finally, we're done, return the string we found.
1293 return Result;
1294 }
1295 }
1296}
1297
1298/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1299/// condition, reporting diagnostics and handling other edge cases as required.
1300/// This returns true if Result contains a token, false if PP.Lex should be
1301/// called again.
Chris Lattnerd2177732007-07-20 16:59:19 +00001302bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001303 // If we hit the end of the file while parsing a preprocessor directive,
1304 // end the preprocessor directive first. The next token returned will
1305 // then be the end of file.
1306 if (ParsingPreprocessorDirective) {
1307 // Done parsing the "line".
1308 ParsingPreprocessorDirective = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001309 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00001310 FormTokenWithChars(Result, CurPtr, tok::eom);
Mike Stump1eb44332009-09-09 15:08:12 +00001311
Reid Spencer5f016e22007-07-11 17:01:13 +00001312 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001313 SetCommentRetentionState(PP->getCommentRetentionState());
Reid Spencer5f016e22007-07-11 17:01:13 +00001314 return true; // Have a token.
Mike Stump1eb44332009-09-09 15:08:12 +00001315 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00001316
Reid Spencer5f016e22007-07-11 17:01:13 +00001317 // If we are in raw mode, return this event as an EOF token. Let the caller
1318 // that put us in raw mode handle the event.
Chris Lattner74d15df2008-11-22 02:02:22 +00001319 if (isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001320 Result.startToken();
1321 BufferPtr = BufferEnd;
Chris Lattner9e6293d2008-10-12 04:51:35 +00001322 FormTokenWithChars(Result, BufferEnd, tok::eof);
Reid Spencer5f016e22007-07-11 17:01:13 +00001323 return true;
1324 }
Mike Stump1eb44332009-09-09 15:08:12 +00001325
Douglas Gregor86d9a522009-09-21 16:56:56 +00001326 // Otherwise, check if we are code-completing, then issue diagnostics for
1327 // unterminated #if and missing newline.
Reid Spencer5f016e22007-07-11 17:01:13 +00001328
Douglas Gregor86d9a522009-09-21 16:56:56 +00001329 if (IsEofCodeCompletion) {
Douglas Gregorb657f112009-09-22 21:11:38 +00001330 bool isIntendedFile = true;
1331 if (PP && FileLoc.isFileID()) {
1332 SourceManager &SM = PP->getSourceManager();
1333 isIntendedFile = SM.isTruncatedFile(SM.getFileID(FileLoc));
1334 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00001335
Douglas Gregorb657f112009-09-22 21:11:38 +00001336 if (isIntendedFile) {
1337 // We're at the end of the file, but we've been asked to consider the
1338 // end of the file to be a code-completion token. Return the
1339 // code-completion token.
1340 Result.startToken();
1341 FormTokenWithChars(Result, CurPtr, tok::code_completion);
1342
1343 // Only do the eof -> code_completion translation once.
1344 IsEofCodeCompletion = false;
1345 return true;
1346 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00001347 }
1348
Reid Spencer5f016e22007-07-11 17:01:13 +00001349 // If we are in a #if directive, emit an error.
1350 while (!ConditionalStack.empty()) {
Chris Lattner30c64762008-11-22 06:22:39 +00001351 PP->Diag(ConditionalStack.back().IfLoc,
1352 diag::err_pp_unterminated_conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +00001353 ConditionalStack.pop_back();
1354 }
Mike Stump1eb44332009-09-09 15:08:12 +00001355
Chris Lattnerb25e5d72008-04-12 05:54:25 +00001356 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1357 // a pedwarn.
1358 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Mike Stump20d0ee52009-04-02 02:29:42 +00001359 Diag(BufferEnd, diag::ext_no_newline_eof)
1360 << CodeModificationHint::CreateInsertion(getSourceLocation(BufferEnd),
1361 "\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001362
Reid Spencer5f016e22007-07-11 17:01:13 +00001363 BufferPtr = CurPtr;
1364
1365 // Finally, let the preprocessor handle this.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001366 return PP->HandleEndOfFile(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001367}
1368
1369/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1370/// the specified lexer will return a tok::l_paren token, 0 if it is something
1371/// else and 2 if there are no more tokens in the buffer controlled by the
1372/// lexer.
1373unsigned Lexer::isNextPPTokenLParen() {
1374 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +00001375
Reid Spencer5f016e22007-07-11 17:01:13 +00001376 // Switch to 'skipping' mode. This will ensure that we can lex a token
1377 // without emitting diagnostics, disables macro expansion, and will cause EOF
1378 // to return an EOF token instead of popping the include stack.
1379 LexingRawMode = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Reid Spencer5f016e22007-07-11 17:01:13 +00001381 // Save state that can be changed while lexing so that we can restore it.
1382 const char *TmpBufferPtr = BufferPtr;
Chris Lattnera864cf72009-04-24 07:15:46 +00001383 bool inPPDirectiveMode = ParsingPreprocessorDirective;
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Chris Lattnerd2177732007-07-20 16:59:19 +00001385 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001386 Tok.startToken();
1387 LexTokenInternal(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +00001388
Reid Spencer5f016e22007-07-11 17:01:13 +00001389 // Restore state that may have changed.
1390 BufferPtr = TmpBufferPtr;
Chris Lattnera864cf72009-04-24 07:15:46 +00001391 ParsingPreprocessorDirective = inPPDirectiveMode;
Mike Stump1eb44332009-09-09 15:08:12 +00001392
Reid Spencer5f016e22007-07-11 17:01:13 +00001393 // Restore the lexer back to non-skipping mode.
1394 LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001395
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001396 if (Tok.is(tok::eof))
Reid Spencer5f016e22007-07-11 17:01:13 +00001397 return 2;
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001398 return Tok.is(tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001399}
1400
1401
1402/// LexTokenInternal - This implements a simple C family lexer. It is an
1403/// extremely performance critical piece of code. This assumes that the buffer
Chris Lattnerefb173d2009-07-07 05:05:42 +00001404/// has a null character at the end of the file. This returns a preprocessing
1405/// token, not a normal token, as such, it is an internal interface. It assumes
1406/// that the Flags of result have been cleared before calling this.
Chris Lattnerd2177732007-07-20 16:59:19 +00001407void Lexer::LexTokenInternal(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001408LexNextToken:
1409 // New token, can't need cleaning yet.
Chris Lattnerd2177732007-07-20 16:59:19 +00001410 Result.clearFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +00001411 Result.setIdentifierInfo(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001412
Reid Spencer5f016e22007-07-11 17:01:13 +00001413 // CurPtr - Cache BufferPtr in an automatic variable.
1414 const char *CurPtr = BufferPtr;
1415
1416 // Small amounts of horizontal whitespace is very common between tokens.
1417 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1418 ++CurPtr;
1419 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1420 ++CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Chris Lattnerd88dc482008-10-12 04:05:48 +00001422 // If we are keeping whitespace and other tokens, just return what we just
1423 // skipped. The next lexer invocation will return the token after the
1424 // whitespace.
1425 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001426 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001427 return;
1428 }
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Reid Spencer5f016e22007-07-11 17:01:13 +00001430 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001431 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001432 }
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Reid Spencer5f016e22007-07-11 17:01:13 +00001434 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Reid Spencer5f016e22007-07-11 17:01:13 +00001436 // Read a character, advancing over it.
1437 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001438 tok::TokenKind Kind;
Mike Stump1eb44332009-09-09 15:08:12 +00001439
Reid Spencer5f016e22007-07-11 17:01:13 +00001440 switch (Char) {
1441 case 0: // Null.
1442 // Found end of file?
1443 if (CurPtr-1 == BufferEnd) {
1444 // Read the PP instance variable into an automatic variable, because
1445 // LexEndOfFile will often delete 'this'.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001446 Preprocessor *PPCache = PP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001447 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1448 return; // Got a token to return.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001449 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1450 return PPCache->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001451 }
Mike Stump1eb44332009-09-09 15:08:12 +00001452
Chris Lattner74d15df2008-11-22 02:02:22 +00001453 if (!isLexingRawMode())
1454 Diag(CurPtr-1, diag::null_in_file);
Chris Lattnerd2177732007-07-20 16:59:19 +00001455 Result.setFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001456 if (SkipWhitespace(Result, CurPtr))
1457 return; // KeepWhitespaceMode
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Reid Spencer5f016e22007-07-11 17:01:13 +00001459 goto LexNextToken; // GCC isn't tail call eliminating.
1460 case '\n':
1461 case '\r':
1462 // If we are inside a preprocessor directive and we see the end of line,
1463 // we know we are done with the directive, so return an EOM token.
1464 if (ParsingPreprocessorDirective) {
1465 // Done parsing the "line".
1466 ParsingPreprocessorDirective = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001467
Reid Spencer5f016e22007-07-11 17:01:13 +00001468 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001469 SetCommentRetentionState(PP->getCommentRetentionState());
Mike Stump1eb44332009-09-09 15:08:12 +00001470
Reid Spencer5f016e22007-07-11 17:01:13 +00001471 // Since we consumed a newline, we are back at the start of a line.
1472 IsAtStartOfLine = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001473
Chris Lattner9e6293d2008-10-12 04:51:35 +00001474 Kind = tok::eom;
Reid Spencer5f016e22007-07-11 17:01:13 +00001475 break;
1476 }
1477 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +00001478 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001479 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +00001480 Result.clearFlag(Token::LeadingSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Chris Lattnerd88dc482008-10-12 04:05:48 +00001482 if (SkipWhitespace(Result, CurPtr))
1483 return; // KeepWhitespaceMode
Reid Spencer5f016e22007-07-11 17:01:13 +00001484 goto LexNextToken; // GCC isn't tail call eliminating.
1485 case ' ':
1486 case '\t':
1487 case '\f':
1488 case '\v':
Chris Lattner8133cfc2007-07-22 06:29:05 +00001489 SkipHorizontalWhitespace:
Chris Lattnerd2177732007-07-20 16:59:19 +00001490 Result.setFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001491 if (SkipWhitespace(Result, CurPtr))
1492 return; // KeepWhitespaceMode
Chris Lattner8133cfc2007-07-22 06:29:05 +00001493
1494 SkipIgnoredUnits:
1495 CurPtr = BufferPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001496
Chris Lattner8133cfc2007-07-22 06:29:05 +00001497 // If the next token is obviously a // or /* */ comment, skip it efficiently
1498 // too (without going through the big switch stmt).
Chris Lattner8402c732009-01-16 22:39:25 +00001499 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1500 Features.BCPLComment) {
Chris Lattner8133cfc2007-07-22 06:29:05 +00001501 SkipBCPLComment(Result, CurPtr+2);
1502 goto SkipIgnoredUnits;
Chris Lattnerfa95a012008-10-12 03:22:02 +00001503 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner8133cfc2007-07-22 06:29:05 +00001504 SkipBlockComment(Result, CurPtr+2);
1505 goto SkipIgnoredUnits;
1506 } else if (isHorizontalWhitespace(*CurPtr)) {
1507 goto SkipHorizontalWhitespace;
1508 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001509 goto LexNextToken; // GCC isn't tail call eliminating.
1510
Chris Lattner3a570772008-01-03 17:58:54 +00001511 // C99 6.4.4.1: Integer Constants.
1512 // C99 6.4.4.2: Floating Constants.
1513 case '0': case '1': case '2': case '3': case '4':
1514 case '5': case '6': case '7': case '8': case '9':
1515 // Notify MIOpt that we read a non-whitespace/non-comment token.
1516 MIOpt.ReadToken();
1517 return LexNumericConstant(Result, CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001518
Chris Lattner3a570772008-01-03 17:58:54 +00001519 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Reid Spencer5f016e22007-07-11 17:01:13 +00001520 // Notify MIOpt that we read a non-whitespace/non-comment token.
1521 MIOpt.ReadToken();
1522 Char = getCharAndSize(CurPtr, SizeTmp);
1523
1524 // Wide string literal.
1525 if (Char == '"')
1526 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1527 true);
1528
1529 // Wide character constant.
1530 if (Char == '\'')
1531 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1532 // FALL THROUGH, treating L like the start of an identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001533
Reid Spencer5f016e22007-07-11 17:01:13 +00001534 // C99 6.4.2: Identifiers.
1535 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1536 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1537 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1538 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1539 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1540 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1541 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1542 case 'v': case 'w': case 'x': case 'y': case 'z':
1543 case '_':
1544 // Notify MIOpt that we read a non-whitespace/non-comment token.
1545 MIOpt.ReadToken();
1546 return LexIdentifier(Result, CurPtr);
Chris Lattner3a570772008-01-03 17:58:54 +00001547
1548 case '$': // $ in identifiers.
1549 if (Features.DollarIdents) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001550 if (!isLexingRawMode())
1551 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner3a570772008-01-03 17:58:54 +00001552 // Notify MIOpt that we read a non-whitespace/non-comment token.
1553 MIOpt.ReadToken();
1554 return LexIdentifier(Result, CurPtr);
1555 }
Mike Stump1eb44332009-09-09 15:08:12 +00001556
Chris Lattner9e6293d2008-10-12 04:51:35 +00001557 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00001558 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001559
Reid Spencer5f016e22007-07-11 17:01:13 +00001560 // C99 6.4.4: Character Constants.
1561 case '\'':
1562 // Notify MIOpt that we read a non-whitespace/non-comment token.
1563 MIOpt.ReadToken();
1564 return LexCharConstant(Result, CurPtr);
1565
1566 // C99 6.4.5: String Literals.
1567 case '"':
1568 // Notify MIOpt that we read a non-whitespace/non-comment token.
1569 MIOpt.ReadToken();
1570 return LexStringLiteral(Result, CurPtr, false);
1571
1572 // C99 6.4.6: Punctuators.
1573 case '?':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001574 Kind = tok::question;
Reid Spencer5f016e22007-07-11 17:01:13 +00001575 break;
1576 case '[':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001577 Kind = tok::l_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001578 break;
1579 case ']':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001580 Kind = tok::r_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001581 break;
1582 case '(':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001583 Kind = tok::l_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001584 break;
1585 case ')':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001586 Kind = tok::r_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001587 break;
1588 case '{':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001589 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001590 break;
1591 case '}':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001592 Kind = tok::r_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001593 break;
1594 case '.':
1595 Char = getCharAndSize(CurPtr, SizeTmp);
1596 if (Char >= '0' && Char <= '9') {
1597 // Notify MIOpt that we read a non-whitespace/non-comment token.
1598 MIOpt.ReadToken();
1599
1600 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1601 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001602 Kind = tok::periodstar;
Reid Spencer5f016e22007-07-11 17:01:13 +00001603 CurPtr += SizeTmp;
1604 } else if (Char == '.' &&
1605 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001606 Kind = tok::ellipsis;
Reid Spencer5f016e22007-07-11 17:01:13 +00001607 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1608 SizeTmp2, Result);
1609 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001610 Kind = tok::period;
Reid Spencer5f016e22007-07-11 17:01:13 +00001611 }
1612 break;
1613 case '&':
1614 Char = getCharAndSize(CurPtr, SizeTmp);
1615 if (Char == '&') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001616 Kind = tok::ampamp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001617 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1618 } else if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001619 Kind = tok::ampequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001620 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1621 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001622 Kind = tok::amp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001623 }
1624 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001625 case '*':
Reid Spencer5f016e22007-07-11 17:01:13 +00001626 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001627 Kind = tok::starequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001628 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1629 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001630 Kind = tok::star;
Reid Spencer5f016e22007-07-11 17:01:13 +00001631 }
1632 break;
1633 case '+':
1634 Char = getCharAndSize(CurPtr, SizeTmp);
1635 if (Char == '+') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001636 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001637 Kind = tok::plusplus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001638 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001639 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001640 Kind = tok::plusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001641 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001642 Kind = tok::plus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001643 }
1644 break;
1645 case '-':
1646 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001647 if (Char == '-') { // --
Reid Spencer5f016e22007-07-11 17:01:13 +00001648 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001649 Kind = tok::minusminus;
Mike Stump1eb44332009-09-09 15:08:12 +00001650 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattner9e6293d2008-10-12 04:51:35 +00001651 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Reid Spencer5f016e22007-07-11 17:01:13 +00001652 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1653 SizeTmp2, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001654 Kind = tok::arrowstar;
1655 } else if (Char == '>') { // ->
Reid Spencer5f016e22007-07-11 17:01:13 +00001656 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001657 Kind = tok::arrow;
1658 } else if (Char == '=') { // -=
Reid Spencer5f016e22007-07-11 17:01:13 +00001659 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001660 Kind = tok::minusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001661 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001662 Kind = tok::minus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001663 }
1664 break;
1665 case '~':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001666 Kind = tok::tilde;
Reid Spencer5f016e22007-07-11 17:01:13 +00001667 break;
1668 case '!':
1669 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001670 Kind = tok::exclaimequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001671 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1672 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001673 Kind = tok::exclaim;
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 }
1675 break;
1676 case '/':
1677 // 6.4.9: Comments
1678 Char = getCharAndSize(CurPtr, SizeTmp);
1679 if (Char == '/') { // BCPL comment.
Chris Lattner8402c732009-01-16 22:39:25 +00001680 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
1681 // want to lex this as a comment. There is one problem with this though,
1682 // that in one particular corner case, this can change the behavior of the
1683 // resultant program. For example, In "foo //**/ bar", C89 would lex
1684 // this as "foo / bar" and langauges with BCPL comments would lex it as
1685 // "foo". Check to see if the character after the second slash is a '*'.
1686 // If so, we will lex that as a "/" instead of the start of a comment.
1687 if (Features.BCPLComment ||
1688 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
1689 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1690 return; // KeepCommentMode
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Chris Lattner8402c732009-01-16 22:39:25 +00001692 // It is common for the tokens immediately after a // comment to be
1693 // whitespace (indentation for the next line). Instead of going through
1694 // the big switch, handle it efficiently now.
1695 goto SkipIgnoredUnits;
1696 }
1697 }
Mike Stump1eb44332009-09-09 15:08:12 +00001698
Chris Lattner8402c732009-01-16 22:39:25 +00001699 if (Char == '*') { // /**/ comment.
Reid Spencer5f016e22007-07-11 17:01:13 +00001700 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner2d381892008-10-12 04:15:42 +00001701 return; // KeepCommentMode
1702 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner8402c732009-01-16 22:39:25 +00001703 }
Mike Stump1eb44332009-09-09 15:08:12 +00001704
Chris Lattner8402c732009-01-16 22:39:25 +00001705 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001706 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001707 Kind = tok::slashequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001708 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001709 Kind = tok::slash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001710 }
1711 break;
1712 case '%':
1713 Char = getCharAndSize(CurPtr, SizeTmp);
1714 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001715 Kind = tok::percentequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001716 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1717 } else if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001718 Kind = tok::r_brace; // '%>' -> '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001719 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1720 } else if (Features.Digraphs && Char == ':') {
1721 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1722 Char = getCharAndSize(CurPtr, SizeTmp);
1723 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001724 Kind = tok::hashhash; // '%:%:' -> '##'
Reid Spencer5f016e22007-07-11 17:01:13 +00001725 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1726 SizeTmp2, Result);
1727 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Reid Spencer5f016e22007-07-11 17:01:13 +00001728 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner74d15df2008-11-22 02:02:22 +00001729 if (!isLexingRawMode())
1730 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001731 Kind = tok::hashat;
Chris Lattnere91e9322009-03-18 20:58:27 +00001732 } else { // '%:' -> '#'
Reid Spencer5f016e22007-07-11 17:01:13 +00001733 // We parsed a # character. If this occurs at the start of the line,
1734 // it's actually the start of a preprocessing directive. Callback to
1735 // the preprocessor to handle it.
1736 // FIXME: -fpreprocessed mode??
Chris Lattner766703b2009-05-13 06:10:29 +00001737 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattnere91e9322009-03-18 20:58:27 +00001738 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner168ae2d2007-10-17 20:41:00 +00001739 PP->HandleDirective(Result);
Mike Stump1eb44332009-09-09 15:08:12 +00001740
Reid Spencer5f016e22007-07-11 17:01:13 +00001741 // As an optimization, if the preprocessor didn't switch lexers, tail
1742 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001743 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001744 // Start a new token. If this is a #include or something, the PP may
1745 // want us starting at the beginning of the line again. If so, set
1746 // the StartOfLine flag.
1747 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001748 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001749 IsAtStartOfLine = false;
1750 }
1751 goto LexNextToken; // GCC isn't tail call eliminating.
1752 }
Mike Stump1eb44332009-09-09 15:08:12 +00001753
Chris Lattner168ae2d2007-10-17 20:41:00 +00001754 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001755 }
Mike Stump1eb44332009-09-09 15:08:12 +00001756
Chris Lattnere91e9322009-03-18 20:58:27 +00001757 Kind = tok::hash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 }
1759 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001760 Kind = tok::percent;
Reid Spencer5f016e22007-07-11 17:01:13 +00001761 }
1762 break;
1763 case '<':
1764 Char = getCharAndSize(CurPtr, SizeTmp);
1765 if (ParsingFilename) {
Chris Lattner9cb51ce2009-04-17 23:56:52 +00001766 return LexAngledStringLiteral(Result, CurPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001767 } else if (Char == '<' &&
1768 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001769 Kind = tok::lesslessequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001770 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1771 SizeTmp2, Result);
1772 } else if (Char == '<') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001773 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001774 Kind = tok::lessless;
Reid Spencer5f016e22007-07-11 17:01:13 +00001775 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001776 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001777 Kind = tok::lessequal;
1778 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Reid Spencer5f016e22007-07-11 17:01:13 +00001779 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001780 Kind = tok::l_square;
1781 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Reid Spencer5f016e22007-07-11 17:01:13 +00001782 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001783 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001784 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001785 Kind = tok::less;
Reid Spencer5f016e22007-07-11 17:01:13 +00001786 }
1787 break;
1788 case '>':
1789 Char = getCharAndSize(CurPtr, SizeTmp);
1790 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001791 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001792 Kind = tok::greaterequal;
Mike Stump1eb44332009-09-09 15:08:12 +00001793 } else if (Char == '>' &&
Reid Spencer5f016e22007-07-11 17:01:13 +00001794 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001795 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1796 SizeTmp2, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001797 Kind = tok::greatergreaterequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001798 } else if (Char == '>') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001799 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001800 Kind = tok::greatergreater;
Reid Spencer5f016e22007-07-11 17:01:13 +00001801 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001802 Kind = tok::greater;
Reid Spencer5f016e22007-07-11 17:01:13 +00001803 }
1804 break;
1805 case '^':
1806 Char = getCharAndSize(CurPtr, SizeTmp);
1807 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001808 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001809 Kind = tok::caretequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001810 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001811 Kind = tok::caret;
Reid Spencer5f016e22007-07-11 17:01:13 +00001812 }
1813 break;
1814 case '|':
1815 Char = getCharAndSize(CurPtr, SizeTmp);
1816 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001817 Kind = tok::pipeequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001818 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1819 } else if (Char == '|') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001820 Kind = tok::pipepipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00001821 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1822 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001823 Kind = tok::pipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00001824 }
1825 break;
1826 case ':':
1827 Char = getCharAndSize(CurPtr, SizeTmp);
1828 if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001829 Kind = tok::r_square; // ':>' -> ']'
Reid Spencer5f016e22007-07-11 17:01:13 +00001830 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1831 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001832 Kind = tok::coloncolon;
Reid Spencer5f016e22007-07-11 17:01:13 +00001833 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump1eb44332009-09-09 15:08:12 +00001834 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001835 Kind = tok::colon;
Reid Spencer5f016e22007-07-11 17:01:13 +00001836 }
1837 break;
1838 case ';':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001839 Kind = tok::semi;
Reid Spencer5f016e22007-07-11 17:01:13 +00001840 break;
1841 case '=':
1842 Char = getCharAndSize(CurPtr, SizeTmp);
1843 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001844 Kind = tok::equalequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001845 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump1eb44332009-09-09 15:08:12 +00001846 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001847 Kind = tok::equal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001848 }
1849 break;
1850 case ',':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001851 Kind = tok::comma;
Reid Spencer5f016e22007-07-11 17:01:13 +00001852 break;
1853 case '#':
1854 Char = getCharAndSize(CurPtr, SizeTmp);
1855 if (Char == '#') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001856 Kind = tok::hashhash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001857 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1858 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner9e6293d2008-10-12 04:51:35 +00001859 Kind = tok::hashat;
Chris Lattner74d15df2008-11-22 02:02:22 +00001860 if (!isLexingRawMode())
1861 Diag(BufferPtr, diag::charize_microsoft_ext);
Reid Spencer5f016e22007-07-11 17:01:13 +00001862 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1863 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +00001864 // We parsed a # character. If this occurs at the start of the line,
1865 // it's actually the start of a preprocessing directive. Callback to
1866 // the preprocessor to handle it.
1867 // FIXME: -fpreprocessed mode??
Chris Lattner766703b2009-05-13 06:10:29 +00001868 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattnere91e9322009-03-18 20:58:27 +00001869 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner168ae2d2007-10-17 20:41:00 +00001870 PP->HandleDirective(Result);
Mike Stump1eb44332009-09-09 15:08:12 +00001871
Reid Spencer5f016e22007-07-11 17:01:13 +00001872 // As an optimization, if the preprocessor didn't switch lexers, tail
1873 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001874 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001875 // Start a new token. If this is a #include or something, the PP may
1876 // want us starting at the beginning of the line again. If so, set
1877 // the StartOfLine flag.
1878 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001879 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001880 IsAtStartOfLine = false;
1881 }
1882 goto LexNextToken; // GCC isn't tail call eliminating.
1883 }
Chris Lattner168ae2d2007-10-17 20:41:00 +00001884 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001885 }
Mike Stump1eb44332009-09-09 15:08:12 +00001886
Chris Lattnere91e9322009-03-18 20:58:27 +00001887 Kind = tok::hash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001888 }
1889 break;
1890
Chris Lattner3a570772008-01-03 17:58:54 +00001891 case '@':
1892 // Objective C support.
1893 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattner9e6293d2008-10-12 04:51:35 +00001894 Kind = tok::at;
Chris Lattner3a570772008-01-03 17:58:54 +00001895 else
Chris Lattner9e6293d2008-10-12 04:51:35 +00001896 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00001897 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001898
Reid Spencer5f016e22007-07-11 17:01:13 +00001899 case '\\':
1900 // FIXME: UCN's.
1901 // FALL THROUGH.
1902 default:
Chris Lattner9e6293d2008-10-12 04:51:35 +00001903 Kind = tok::unknown;
Reid Spencer5f016e22007-07-11 17:01:13 +00001904 break;
1905 }
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Reid Spencer5f016e22007-07-11 17:01:13 +00001907 // Notify MIOpt that we read a non-whitespace/non-comment token.
1908 MIOpt.ReadToken();
1909
1910 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00001911 FormTokenWithChars(Result, CurPtr, Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001912}