blob: 74ac55cf2eb451860e73ad0a123dca0571ef5675 [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);
Mike Stump1eb44332009-09-09 15:08:12 +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());
110}
Chris Lattnerdbf388b2007-10-07 08:47:24 +0000111
Chris Lattner168ae2d2007-10-17 20:41:00 +0000112/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner590f0cc2008-10-12 01:15:46 +0000113/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
114/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000115Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattnerde96c0f2009-01-17 07:42:27 +0000116 const char *BufStart, const char *BufPtr, const char *BufEnd)
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000117 : FileLoc(fileloc), Features(features) {
Chris Lattner22d91ca2009-01-17 06:55:17 +0000118
Chris Lattner22d91ca2009-01-17 06:55:17 +0000119 InitLexer(BufStart, BufPtr, BufEnd);
Mike Stump1eb44332009-09-09 15:08:12 +0000120
Chris Lattner168ae2d2007-10-17 20:41:00 +0000121 // We *are* in raw mode.
122 LexingRawMode = true;
Chris Lattner168ae2d2007-10-17 20:41:00 +0000123}
124
Chris Lattner025c3a62009-01-17 07:35:14 +0000125/// Lexer constructor - Create a new raw lexer object. This object is only
126/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
127/// range will outlive it, so it doesn't take ownership of it.
128Lexer::Lexer(FileID FID, const SourceManager &SM, const LangOptions &features)
129 : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
130 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
131
Mike Stump1eb44332009-09-09 15:08:12 +0000132 InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
Chris Lattner025c3a62009-01-17 07:35:14 +0000133 FromFile->getBufferEnd());
Mike Stump1eb44332009-09-09 15:08:12 +0000134
Chris Lattner025c3a62009-01-17 07:35:14 +0000135 // We *are* in raw mode.
136 LexingRawMode = true;
137}
138
Chris Lattner42e00d12009-01-17 08:27:52 +0000139/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
140/// _Pragma expansion. This has a variety of magic semantics that this method
141/// sets up. It returns a new'd Lexer that must be delete'd when done.
142///
143/// On entrance to this routine, TokStartLoc is a macro location which has a
144/// spelling loc that indicates the bytes to be lexed for the token and an
145/// instantiation location that indicates where all lexed tokens should be
146/// "expanded from".
147///
148/// FIXME: It would really be nice to make _Pragma just be a wrapper around a
149/// normal lexer that remaps tokens as they fly by. This would require making
150/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer
151/// interface that could handle this stuff. This would pull GetMappedTokenLoc
152/// out of the critical path of the lexer!
153///
Mike Stump1eb44332009-09-09 15:08:12 +0000154Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000155 SourceLocation InstantiationLocStart,
156 SourceLocation InstantiationLocEnd,
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000157 unsigned TokLen, Preprocessor &PP) {
Chris Lattner42e00d12009-01-17 08:27:52 +0000158 SourceManager &SM = PP.getSourceManager();
Chris Lattner42e00d12009-01-17 08:27:52 +0000159
160 // Create the lexer as if we were going to lex the file normally.
Chris Lattnera11d6172009-01-19 07:46:45 +0000161 FileID SpellingFID = SM.getFileID(SpellingLoc);
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000162 Lexer *L = new Lexer(SpellingFID, PP);
Mike Stump1eb44332009-09-09 15:08:12 +0000163
Chris Lattner42e00d12009-01-17 08:27:52 +0000164 // Now that the lexer is created, change the start/end locations so that we
165 // just lex the subsection of the file that we want. This is lexing from a
166 // scratch buffer.
167 const char *StrData = SM.getCharacterData(SpellingLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000168
Chris Lattner42e00d12009-01-17 08:27:52 +0000169 L->BufferPtr = StrData;
170 L->BufferEnd = StrData+TokLen;
Chris Lattner1fa49532009-03-08 08:08:45 +0000171 assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
Chris Lattner42e00d12009-01-17 08:27:52 +0000172
173 // Set the SourceLocation with the remapping information. This ensures that
174 // GetMappedTokenLoc will remap the tokens as they are lexed.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000175 L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
Chris Lattnere7fb4842009-02-15 20:52:18 +0000176 InstantiationLocStart,
177 InstantiationLocEnd, TokLen);
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattner42e00d12009-01-17 08:27:52 +0000179 // Ensure that the lexer thinks it is inside a directive, so that end \n will
180 // return an EOM token.
181 L->ParsingPreprocessorDirective = true;
Mike Stump1eb44332009-09-09 15:08:12 +0000182
Chris Lattner42e00d12009-01-17 08:27:52 +0000183 // This lexer really is for _Pragma.
184 L->Is_PragmaLexer = true;
185 return L;
186}
187
Chris Lattner168ae2d2007-10-17 20:41:00 +0000188
Reid Spencer5f016e22007-07-11 17:01:13 +0000189/// Stringify - Convert the specified string into a C string, with surrounding
190/// ""'s, and with escaped \ and " characters.
191std::string Lexer::Stringify(const std::string &Str, bool Charify) {
192 std::string Result = Str;
193 char Quote = Charify ? '\'' : '"';
194 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
195 if (Result[i] == '\\' || Result[i] == Quote) {
196 Result.insert(Result.begin()+i, '\\');
197 ++i; ++e;
198 }
199 }
200 return Result;
201}
202
Chris Lattnerd8e30832007-07-24 06:57:14 +0000203/// Stringify - Convert the specified string into a C string by escaping '\'
204/// and " characters. This does not add surrounding ""'s to the string.
205void Lexer::Stringify(llvm::SmallVectorImpl<char> &Str) {
206 for (unsigned i = 0, e = Str.size(); i != e; ++i) {
207 if (Str[i] == '\\' || Str[i] == '"') {
208 Str.insert(Str.begin()+i, '\\');
209 ++i; ++e;
210 }
211 }
212}
213
Reid Spencer5f016e22007-07-11 17:01:13 +0000214
Chris Lattner9a611942007-10-17 21:18:47 +0000215/// MeasureTokenLength - Relex the token at the specified location and return
216/// its length in bytes in the input file. If the token needs cleaning (e.g.
217/// includes a trigraph or an escaped newline) then this count includes bytes
218/// that are part of that.
219unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
Chris Lattner2c78b872009-04-14 23:22:57 +0000220 const SourceManager &SM,
221 const LangOptions &LangOpts) {
Chris Lattner9a611942007-10-17 21:18:47 +0000222 // TODO: this could be special cased for common tokens like identifiers, ')',
223 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
Mike Stump1eb44332009-09-09 15:08:12 +0000224 // all obviously single-char tokens. This could use
Chris Lattner9a611942007-10-17 21:18:47 +0000225 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
226 // something.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000227
228 // If this comes from a macro expansion, we really do want the macro name, not
229 // the token this macro expanded to.
Chris Lattner363fdc22009-01-26 22:24:27 +0000230 Loc = SM.getInstantiationLoc(Loc);
231 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
Chris Lattner83503942009-01-17 08:30:10 +0000232 std::pair<const char *,const char *> Buffer = SM.getBufferData(LocInfo.first);
233 const char *StrData = Buffer.first+LocInfo.second;
234
Chris Lattner9a611942007-10-17 21:18:47 +0000235 // Create a lexer starting at the beginning of this token.
Chris Lattnerde96c0f2009-01-17 07:42:27 +0000236 Lexer TheLexer(Loc, LangOpts, Buffer.first, StrData, Buffer.second);
Chris Lattner9a611942007-10-17 21:18:47 +0000237 Token TheTok;
Chris Lattner590f0cc2008-10-12 01:15:46 +0000238 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner9a611942007-10-17 21:18:47 +0000239 return TheTok.getLength();
240}
241
Reid Spencer5f016e22007-07-11 17:01:13 +0000242//===----------------------------------------------------------------------===//
243// Character information.
244//===----------------------------------------------------------------------===//
245
Reid Spencer5f016e22007-07-11 17:01:13 +0000246enum {
247 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
248 CHAR_VERT_WS = 0x02, // '\r', '\n'
249 CHAR_LETTER = 0x04, // a-z,A-Z
250 CHAR_NUMBER = 0x08, // 0-9
251 CHAR_UNDER = 0x10, // _
252 CHAR_PERIOD = 0x20 // .
253};
254
Chris Lattner03b98662009-07-07 17:09:54 +0000255// Statically initialize CharInfo table based on ASCII character set
256// Reference: FreeBSD 7.2 /usr/share/misc/ascii
257static const unsigned char CharInfo[256] =
258{
259// 0 NUL 1 SOH 2 STX 3 ETX
260// 4 EOT 5 ENQ 6 ACK 7 BEL
261 0 , 0 , 0 , 0 ,
262 0 , 0 , 0 , 0 ,
263// 8 BS 9 HT 10 NL 11 VT
264//12 NP 13 CR 14 SO 15 SI
265 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS,
266 CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 ,
267//16 DLE 17 DC1 18 DC2 19 DC3
268//20 DC4 21 NAK 22 SYN 23 ETB
269 0 , 0 , 0 , 0 ,
270 0 , 0 , 0 , 0 ,
271//24 CAN 25 EM 26 SUB 27 ESC
272//28 FS 29 GS 30 RS 31 US
273 0 , 0 , 0 , 0 ,
274 0 , 0 , 0 , 0 ,
275//32 SP 33 ! 34 " 35 #
276//36 $ 37 % 38 & 39 '
277 CHAR_HORZ_WS, 0 , 0 , 0 ,
278 0 , 0 , 0 , 0 ,
279//40 ( 41 ) 42 * 43 +
280//44 , 45 - 46 . 47 /
281 0 , 0 , 0 , 0 ,
282 0 , 0 , CHAR_PERIOD , 0 ,
283//48 0 49 1 50 2 51 3
284//52 4 53 5 54 6 55 7
285 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
286 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
287//56 8 57 9 58 : 59 ;
288//60 < 61 = 62 > 63 ?
289 CHAR_NUMBER , CHAR_NUMBER , 0 , 0 ,
290 0 , 0 , 0 , 0 ,
291//64 @ 65 A 66 B 67 C
292//68 D 69 E 70 F 71 G
293 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
294 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
295//72 H 73 I 74 J 75 K
296//76 L 77 M 78 N 79 O
297 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
298 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
299//80 P 81 Q 82 R 83 S
300//84 T 85 U 86 V 87 W
301 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
302 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
303//88 X 89 Y 90 Z 91 [
304//92 \ 93 ] 94 ^ 95 _
305 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
306 0 , 0 , 0 , CHAR_UNDER ,
307//96 ` 97 a 98 b 99 c
308//100 d 101 e 102 f 103 g
309 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
310 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
311//104 h 105 i 106 j 107 k
312//108 l 109 m 110 n 111 o
313 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
314 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
315//112 p 113 q 114 r 115 s
316//116 t 117 u 118 v 119 w
317 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
318 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
319//120 x 121 y 122 z 123 {
320//124 | 125 } 126 ~ 127 DEL
321 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
322 0 , 0 , 0 , 0
323};
324
Reid Spencer5f016e22007-07-11 17:01:13 +0000325static void InitCharacterInfo() {
326 static bool isInited = false;
327 if (isInited) return;
Chris Lattner03b98662009-07-07 17:09:54 +0000328 // check the statically-initialized CharInfo table
329 assert(CHAR_HORZ_WS == CharInfo[(int)' ']);
330 assert(CHAR_HORZ_WS == CharInfo[(int)'\t']);
331 assert(CHAR_HORZ_WS == CharInfo[(int)'\f']);
332 assert(CHAR_HORZ_WS == CharInfo[(int)'\v']);
333 assert(CHAR_VERT_WS == CharInfo[(int)'\n']);
334 assert(CHAR_VERT_WS == CharInfo[(int)'\r']);
335 assert(CHAR_UNDER == CharInfo[(int)'_']);
336 assert(CHAR_PERIOD == CharInfo[(int)'.']);
337 for (unsigned i = 'a'; i <= 'z'; ++i) {
338 assert(CHAR_LETTER == CharInfo[i]);
339 assert(CHAR_LETTER == CharInfo[i+'A'-'a']);
340 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000341 for (unsigned i = '0'; i <= '9'; ++i)
Chris Lattner03b98662009-07-07 17:09:54 +0000342 assert(CHAR_NUMBER == CharInfo[i]);
343 isInited = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000344}
345
Chris Lattner03b98662009-07-07 17:09:54 +0000346
Reid Spencer5f016e22007-07-11 17:01:13 +0000347/// isIdentifierBody - Return true if this is the body character of an
348/// identifier, which is [a-zA-Z0-9_].
349static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000350 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000351}
352
353/// isHorizontalWhitespace - Return true if this character is horizontal
354/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
355static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000356 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000357}
358
359/// isWhitespace - Return true if this character is horizontal or vertical
360/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
361/// for '\0'.
362static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000363 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000364}
365
366/// isNumberBody - Return true if this is the body character of an
367/// preprocessing number, which is [a-zA-Z0-9_.].
368static inline bool isNumberBody(unsigned char c) {
Mike Stump1eb44332009-09-09 15:08:12 +0000369 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
Hartmut Kaiser95c062b2007-10-18 12:47:01 +0000370 true : false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000371}
372
373
374//===----------------------------------------------------------------------===//
375// Diagnostics forwarding code.
376//===----------------------------------------------------------------------===//
377
Chris Lattner409a0362007-07-22 18:38:25 +0000378/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
379/// lexer buffer was all instantiated at a single point, perform the mapping.
380/// This is currently only used for _Pragma implementation, so it is the slow
381/// path of the hot getSourceLocation method. Do not allow it to be inlined.
382static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
383 SourceLocation FileLoc,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000384 unsigned CharNo,
385 unsigned TokLen) DISABLE_INLINE;
Chris Lattner409a0362007-07-22 18:38:25 +0000386static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
387 SourceLocation FileLoc,
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000388 unsigned CharNo, unsigned TokLen) {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000389 assert(FileLoc.isMacroID() && "Must be an instantiation");
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Chris Lattner409a0362007-07-22 18:38:25 +0000391 // Otherwise, we're lexing "mapped tokens". This is used for things like
392 // _Pragma handling. Combine the instantiation location of FileLoc with the
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000393 // spelling location.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000394 SourceManager &SM = PP.getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000395
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000396 // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000397 // characters come from spelling(FileLoc)+Offset.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000398 SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000399 SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000400
Chris Lattnere7fb4842009-02-15 20:52:18 +0000401 // Figure out the expansion loc range, which is the range covered by the
402 // original _Pragma(...) sequence.
403 std::pair<SourceLocation,SourceLocation> II =
404 SM.getImmediateInstantiationRange(FileLoc);
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Chris Lattnere7fb4842009-02-15 20:52:18 +0000406 return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
Chris Lattner409a0362007-07-22 18:38:25 +0000407}
408
Reid Spencer5f016e22007-07-11 17:01:13 +0000409/// getSourceLocation - Return a source location identifier for the specified
410/// offset in the current file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000411SourceLocation Lexer::getSourceLocation(const char *Loc,
412 unsigned TokLen) const {
Chris Lattner448cec42007-07-22 18:44:36 +0000413 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Reid Spencer5f016e22007-07-11 17:01:13 +0000414 "Location out of range for this buffer!");
Chris Lattner9dc1f532007-07-20 16:37:10 +0000415
416 // In the normal case, we're just lexing from a simple file buffer, return
417 // the file id from FileLoc with the offset specified.
Chris Lattner448cec42007-07-22 18:44:36 +0000418 unsigned CharNo = Loc-BufferStart;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000419 if (FileLoc.isFileID())
Chris Lattnerbcc2a672009-01-19 06:46:35 +0000420 return FileLoc.getFileLocWithOffset(CharNo);
Mike Stump1eb44332009-09-09 15:08:12 +0000421
Chris Lattner2b2453a2009-01-17 06:22:33 +0000422 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
423 // tokens are lexed from where the _Pragma was defined.
Chris Lattner168ae2d2007-10-17 20:41:00 +0000424 assert(PP && "This doesn't work on raw lexers");
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000425 return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
Reid Spencer5f016e22007-07-11 17:01:13 +0000426}
427
Reid Spencer5f016e22007-07-11 17:01:13 +0000428/// Diag - Forwarding function for diagnostics. This translate a source
429/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner3cbfe2c2008-11-22 00:59:29 +0000430DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner3692b092008-11-18 07:59:24 +0000431 return PP->Diag(getSourceLocation(Loc), DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +0000432}
Reid Spencer5f016e22007-07-11 17:01:13 +0000433
434//===----------------------------------------------------------------------===//
435// Trigraph and Escaped Newline Handling Code.
436//===----------------------------------------------------------------------===//
437
438/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
439/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
440static char GetTrigraphCharForLetter(char Letter) {
441 switch (Letter) {
442 default: return 0;
443 case '=': return '#';
444 case ')': return ']';
445 case '(': return '[';
446 case '!': return '|';
447 case '\'': return '^';
448 case '>': return '}';
449 case '/': return '\\';
450 case '<': return '{';
451 case '-': return '~';
452 }
453}
454
455/// DecodeTrigraphChar - If the specified character is a legal trigraph when
456/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
457/// return the result character. Finally, emit a warning about trigraph use
458/// whether trigraphs are enabled or not.
459static char DecodeTrigraphChar(const char *CP, Lexer *L) {
460 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner3692b092008-11-18 07:59:24 +0000461 if (!Res || !L) return Res;
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Chris Lattner3692b092008-11-18 07:59:24 +0000463 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +0000464 if (!L->isLexingRawMode())
465 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner3692b092008-11-18 07:59:24 +0000466 return 0;
Reid Spencer5f016e22007-07-11 17:01:13 +0000467 }
Mike Stump1eb44332009-09-09 15:08:12 +0000468
Chris Lattner74d15df2008-11-22 02:02:22 +0000469 if (!L->isLexingRawMode())
470 L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
Reid Spencer5f016e22007-07-11 17:01:13 +0000471 return Res;
472}
473
Chris Lattner24f0e482009-04-18 22:05:41 +0000474/// getEscapedNewLineSize - Return the size of the specified escaped newline,
475/// 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 +0000476/// trigraph equivalent on entry to this function.
Chris Lattner24f0e482009-04-18 22:05:41 +0000477unsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
478 unsigned Size = 0;
479 while (isWhitespace(Ptr[Size])) {
480 ++Size;
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Chris Lattner24f0e482009-04-18 22:05:41 +0000482 if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
483 continue;
484
485 // If this is a \r\n or \n\r, skip the other half.
486 if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
487 Ptr[Size-1] != Ptr[Size])
488 ++Size;
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Chris Lattner24f0e482009-04-18 22:05:41 +0000490 return Size;
Mike Stump1eb44332009-09-09 15:08:12 +0000491 }
492
Chris Lattner24f0e482009-04-18 22:05:41 +0000493 // Not an escaped newline, must be a \t or something else.
494 return 0;
495}
496
Chris Lattner03374952009-04-18 22:27:02 +0000497/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
498/// them), skip over them and return the first non-escaped-newline found,
499/// otherwise return P.
500const char *Lexer::SkipEscapedNewLines(const char *P) {
501 while (1) {
502 const char *AfterEscape;
503 if (*P == '\\') {
504 AfterEscape = P+1;
505 } else if (*P == '?') {
506 // If not a trigraph for escape, bail out.
507 if (P[1] != '?' || P[2] != '/')
508 return P;
509 AfterEscape = P+3;
510 } else {
511 return P;
512 }
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chris Lattner03374952009-04-18 22:27:02 +0000514 unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
515 if (NewLineSize == 0) return P;
516 P = AfterEscape+NewLineSize;
517 }
518}
519
Chris Lattner24f0e482009-04-18 22:05:41 +0000520
Reid Spencer5f016e22007-07-11 17:01:13 +0000521/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
522/// get its size, and return it. This is tricky in several cases:
523/// 1. If currently at the start of a trigraph, we warn about the trigraph,
524/// then either return the trigraph (skipping 3 chars) or the '?',
525/// depending on whether trigraphs are enabled or not.
526/// 2. If this is an escaped newline (potentially with whitespace between
527/// the backslash and newline), implicitly skip the newline and return
528/// the char after it.
529/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
530///
531/// This handles the slow/uncommon case of the getCharAndSize method. Here we
532/// know that we can accumulate into Size, and that we have already incremented
533/// Ptr by Size bytes.
534///
535/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
536/// be updated to match.
537///
538char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattnerd2177732007-07-20 16:59:19 +0000539 Token *Tok) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000540 // If we have a slash, look for an escaped newline.
541 if (Ptr[0] == '\\') {
542 ++Size;
543 ++Ptr;
544Slash:
545 // Common case, backslash-char where the char is not whitespace.
546 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattner5636a3b2009-06-23 05:15:06 +0000548 // See if we have optional whitespace characters between the slash and
549 // newline.
Chris Lattner24f0e482009-04-18 22:05:41 +0000550 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
551 // Remember that this token needs to be cleaned.
552 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000553
Chris Lattner24f0e482009-04-18 22:05:41 +0000554 // Warn if there was whitespace between the backslash and newline.
Chris Lattner5636a3b2009-06-23 05:15:06 +0000555 if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
Chris Lattner24f0e482009-04-18 22:05:41 +0000556 Diag(Ptr, diag::backslash_newline_space);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattner24f0e482009-04-18 22:05:41 +0000558 // Found backslash<whitespace><newline>. Parse the char after it.
559 Size += EscapedNewLineSize;
560 Ptr += EscapedNewLineSize;
561 // Use slow version to accumulate a correct size field.
562 return getCharAndSizeSlow(Ptr, Size, Tok);
563 }
Mike Stump1eb44332009-09-09 15:08:12 +0000564
Reid Spencer5f016e22007-07-11 17:01:13 +0000565 // Otherwise, this is not an escaped newline, just return the slash.
566 return '\\';
567 }
Mike Stump1eb44332009-09-09 15:08:12 +0000568
Reid Spencer5f016e22007-07-11 17:01:13 +0000569 // If this is a trigraph, process it.
570 if (Ptr[0] == '?' && Ptr[1] == '?') {
571 // If this is actually a legal trigraph (not something like "??x"), emit
572 // a trigraph warning. If so, and if trigraphs are enabled, return it.
573 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
574 // Remember that this token needs to be cleaned.
Chris Lattnerd2177732007-07-20 16:59:19 +0000575 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +0000576
577 Ptr += 3;
578 Size += 3;
579 if (C == '\\') goto Slash;
580 return C;
581 }
582 }
Mike Stump1eb44332009-09-09 15:08:12 +0000583
Reid Spencer5f016e22007-07-11 17:01:13 +0000584 // If this is neither, return a single character.
585 ++Size;
586 return *Ptr;
587}
588
589
590/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
591/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
592/// and that we have already incremented Ptr by Size bytes.
593///
594/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
595/// be updated to match.
596char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
597 const LangOptions &Features) {
598 // If we have a slash, look for an escaped newline.
599 if (Ptr[0] == '\\') {
600 ++Size;
601 ++Ptr;
602Slash:
603 // Common case, backslash-char where the char is not whitespace.
604 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump1eb44332009-09-09 15:08:12 +0000605
Reid Spencer5f016e22007-07-11 17:01:13 +0000606 // See if we have optional whitespace characters followed by a newline.
Chris Lattner24f0e482009-04-18 22:05:41 +0000607 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
608 // Found backslash<whitespace><newline>. Parse the char after it.
609 Size += EscapedNewLineSize;
610 Ptr += EscapedNewLineSize;
Mike Stump1eb44332009-09-09 15:08:12 +0000611
Chris Lattner24f0e482009-04-18 22:05:41 +0000612 // Use slow version to accumulate a correct size field.
613 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
614 }
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 // Otherwise, this is not an escaped newline, just return the slash.
617 return '\\';
618 }
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Reid Spencer5f016e22007-07-11 17:01:13 +0000620 // If this is a trigraph, process it.
621 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
622 // If this is actually a legal trigraph (not something like "??x"), return
623 // it.
624 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
625 Ptr += 3;
626 Size += 3;
627 if (C == '\\') goto Slash;
628 return C;
629 }
630 }
Mike Stump1eb44332009-09-09 15:08:12 +0000631
Reid Spencer5f016e22007-07-11 17:01:13 +0000632 // If this is neither, return a single character.
633 ++Size;
634 return *Ptr;
635}
636
637//===----------------------------------------------------------------------===//
638// Helper methods for lexing.
639//===----------------------------------------------------------------------===//
640
Chris Lattnerd2177732007-07-20 16:59:19 +0000641void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000642 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
643 unsigned Size;
644 unsigned char C = *CurPtr++;
645 while (isIdentifierBody(C)) {
646 C = *CurPtr++;
647 }
648 --CurPtr; // Back up over the skipped character.
649
650 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
651 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
652 // FIXME: UCNs.
653 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
654FinishIdentifier:
655 const char *IdStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000656 FormTokenWithChars(Result, CurPtr, tok::identifier);
Mike Stump1eb44332009-09-09 15:08:12 +0000657
Reid Spencer5f016e22007-07-11 17:01:13 +0000658 // If we are in raw mode, return this identifier raw. There is no need to
659 // look up identifier information or attempt to macro expand it.
660 if (LexingRawMode) return;
Mike Stump1eb44332009-09-09 15:08:12 +0000661
Reid Spencer5f016e22007-07-11 17:01:13 +0000662 // Fill in Result.IdentifierInfo, looking up the identifier in the
663 // identifier table.
Chris Lattnerd1186fa2009-01-21 07:45:14 +0000664 IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
Mike Stump1eb44332009-09-09 15:08:12 +0000665
Chris Lattner863c4862009-01-23 18:35:48 +0000666 // Change the kind of this identifier to the appropriate token kind, e.g.
667 // turning "for" into a keyword.
668 Result.setKind(II->getTokenID());
Mike Stump1eb44332009-09-09 15:08:12 +0000669
Reid Spencer5f016e22007-07-11 17:01:13 +0000670 // Finally, now that we know we have an identifier, pass this off to the
671 // preprocessor, which may macro expand it or something.
Chris Lattnerd1186fa2009-01-21 07:45:14 +0000672 if (II->isHandleIdentifierCase())
Chris Lattner6a170eb2009-01-21 07:43:11 +0000673 PP->HandleIdentifier(Result);
674 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000675 }
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Reid Spencer5f016e22007-07-11 17:01:13 +0000677 // Otherwise, $,\,? in identifier found. Enter slower path.
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Reid Spencer5f016e22007-07-11 17:01:13 +0000679 C = getCharAndSize(CurPtr, Size);
680 while (1) {
681 if (C == '$') {
682 // If we hit a $ and they are not supported in identifiers, we are done.
683 if (!Features.DollarIdents) goto FinishIdentifier;
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Reid Spencer5f016e22007-07-11 17:01:13 +0000685 // Otherwise, emit a diagnostic and continue.
Chris Lattner74d15df2008-11-22 02:02:22 +0000686 if (!isLexingRawMode())
687 Diag(CurPtr, diag::ext_dollar_in_identifier);
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 CurPtr = ConsumeChar(CurPtr, Size, Result);
689 C = getCharAndSize(CurPtr, Size);
690 continue;
691 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
692 // Found end of identifier.
693 goto FinishIdentifier;
694 }
695
696 // Otherwise, this character is good, consume it.
697 CurPtr = ConsumeChar(CurPtr, Size, Result);
698
699 C = getCharAndSize(CurPtr, Size);
700 while (isIdentifierBody(C)) { // FIXME: UCNs.
701 CurPtr = ConsumeChar(CurPtr, Size, Result);
702 C = getCharAndSize(CurPtr, Size);
703 }
704 }
705}
706
707
Nate Begeman5253c7f2008-04-14 02:26:39 +0000708/// LexNumericConstant - Lex the remainder of a integer or floating point
Reid Spencer5f016e22007-07-11 17:01:13 +0000709/// constant. From[-1] is the first character lexed. Return the end of the
710/// constant.
Chris Lattnerd2177732007-07-20 16:59:19 +0000711void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000712 unsigned Size;
713 char C = getCharAndSize(CurPtr, Size);
714 char PrevCh = 0;
715 while (isNumberBody(C)) { // FIXME: UCNs?
716 CurPtr = ConsumeChar(CurPtr, Size, Result);
717 PrevCh = C;
718 C = getCharAndSize(CurPtr, Size);
719 }
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Reid Spencer5f016e22007-07-11 17:01:13 +0000721 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
722 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
723 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
724
725 // If we have a hex FP constant, continue.
Eli Friedmanf01fdff2009-04-28 00:51:18 +0000726 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p'))
Reid Spencer5f016e22007-07-11 17:01:13 +0000727 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
Mike Stump1eb44332009-09-09 15:08:12 +0000728
Reid Spencer5f016e22007-07-11 17:01:13 +0000729 // Update the location of token as well as BufferPtr.
Chris Lattner47246be2009-01-26 19:29:26 +0000730 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000731 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Chris Lattner47246be2009-01-26 19:29:26 +0000732 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000733}
734
735/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
736/// either " or L".
Chris Lattnerd88dc482008-10-12 04:05:48 +0000737void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000738 const char *NulCharacter = 0; // Does this string contain the \0 character?
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Reid Spencer5f016e22007-07-11 17:01:13 +0000740 char C = getAndAdvanceChar(CurPtr, Result);
741 while (C != '"') {
742 // Skip escaped characters.
743 if (C == '\\') {
744 // Skip the escaped character.
745 C = getAndAdvanceChar(CurPtr, Result);
746 } else if (C == '\n' || C == '\r' || // Newline.
747 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner33ab3f62009-03-18 21:10:12 +0000748 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner74d15df2008-11-22 02:02:22 +0000749 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000750 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000751 return;
752 } else if (C == 0) {
753 NulCharacter = CurPtr-1;
754 }
755 C = getAndAdvanceChar(CurPtr, Result);
756 }
Mike Stump1eb44332009-09-09 15:08:12 +0000757
Reid Spencer5f016e22007-07-11 17:01:13 +0000758 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000759 if (NulCharacter && !isLexingRawMode())
760 Diag(NulCharacter, diag::null_in_string);
Reid Spencer5f016e22007-07-11 17:01:13 +0000761
Reid Spencer5f016e22007-07-11 17:01:13 +0000762 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner47246be2009-01-26 19:29:26 +0000763 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000764 FormTokenWithChars(Result, CurPtr,
765 Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +0000766 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000767}
768
769/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
770/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattnerd2177732007-07-20 16:59:19 +0000771void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000772 const char *NulCharacter = 0; // Does this string contain the \0 character?
Chris Lattner9cb51ce2009-04-17 23:56:52 +0000773 const char *AfterLessPos = CurPtr;
Reid Spencer5f016e22007-07-11 17:01:13 +0000774 char C = getAndAdvanceChar(CurPtr, Result);
775 while (C != '>') {
776 // Skip escaped characters.
777 if (C == '\\') {
778 // Skip the escaped character.
779 C = getAndAdvanceChar(CurPtr, Result);
780 } else if (C == '\n' || C == '\r' || // Newline.
781 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner9cb51ce2009-04-17 23:56:52 +0000782 // If the filename is unterminated, then it must just be a lone <
783 // character. Return this as such.
784 FormTokenWithChars(Result, AfterLessPos, tok::less);
Reid Spencer5f016e22007-07-11 17:01:13 +0000785 return;
786 } else if (C == 0) {
787 NulCharacter = CurPtr-1;
788 }
789 C = getAndAdvanceChar(CurPtr, Result);
790 }
Mike Stump1eb44332009-09-09 15:08:12 +0000791
Reid Spencer5f016e22007-07-11 17:01:13 +0000792 // If a nul character existed in the string, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +0000793 if (NulCharacter && !isLexingRawMode())
794 Diag(NulCharacter, diag::null_in_string);
Mike Stump1eb44332009-09-09 15:08:12 +0000795
Reid Spencer5f016e22007-07-11 17:01:13 +0000796 // Update the location of token as well as BufferPtr.
Chris Lattner47246be2009-01-26 19:29:26 +0000797 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000798 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Chris Lattner47246be2009-01-26 19:29:26 +0000799 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000800}
801
802
803/// LexCharConstant - Lex the remainder of a character constant, after having
804/// lexed either ' or L'.
Chris Lattnerd2177732007-07-20 16:59:19 +0000805void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000806 const char *NulCharacter = 0; // Does this character contain the \0 character?
807
808 // Handle the common case of 'x' and '\y' efficiently.
809 char C = getAndAdvanceChar(CurPtr, Result);
810 if (C == '\'') {
Chris Lattner33ab3f62009-03-18 21:10:12 +0000811 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner74d15df2008-11-22 02:02:22 +0000812 Diag(BufferPtr, diag::err_empty_character);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000813 FormTokenWithChars(Result, CurPtr, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000814 return;
815 } else if (C == '\\') {
816 // Skip the escaped character.
817 // FIXME: UCN's.
818 C = getAndAdvanceChar(CurPtr, Result);
819 }
Mike Stump1eb44332009-09-09 15:08:12 +0000820
Reid Spencer5f016e22007-07-11 17:01:13 +0000821 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
822 ++CurPtr;
823 } else {
824 // Fall back on generic code for embedded nulls, newlines, wide chars.
825 do {
826 // Skip escaped characters.
827 if (C == '\\') {
828 // Skip the escaped character.
829 C = getAndAdvanceChar(CurPtr, Result);
830 } else if (C == '\n' || C == '\r' || // Newline.
831 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattner33ab3f62009-03-18 21:10:12 +0000832 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner74d15df2008-11-22 02:02:22 +0000833 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattner9e6293d2008-10-12 04:51:35 +0000834 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Reid Spencer5f016e22007-07-11 17:01:13 +0000835 return;
836 } else if (C == 0) {
837 NulCharacter = CurPtr-1;
838 }
839 C = getAndAdvanceChar(CurPtr, Result);
840 } while (C != '\'');
841 }
Mike Stump1eb44332009-09-09 15:08:12 +0000842
Chris Lattner74d15df2008-11-22 02:02:22 +0000843 if (NulCharacter && !isLexingRawMode())
844 Diag(NulCharacter, diag::null_in_char);
Reid Spencer5f016e22007-07-11 17:01:13 +0000845
Reid Spencer5f016e22007-07-11 17:01:13 +0000846 // Update the location of token as well as BufferPtr.
Chris Lattner47246be2009-01-26 19:29:26 +0000847 const char *TokStart = BufferPtr;
Chris Lattner9e6293d2008-10-12 04:51:35 +0000848 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Chris Lattner47246be2009-01-26 19:29:26 +0000849 Result.setLiteralData(TokStart);
Reid Spencer5f016e22007-07-11 17:01:13 +0000850}
851
852/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
853/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattnerd88dc482008-10-12 04:05:48 +0000854///
855/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
856///
857bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000858 // Whitespace - Skip it, then return the token after the whitespace.
859 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
860 while (1) {
861 // Skip horizontal whitespace very aggressively.
862 while (isHorizontalWhitespace(Char))
863 Char = *++CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Daniel Dunbarddd3e8b2008-11-25 00:20:22 +0000865 // Otherwise if we have something other than whitespace, we're done.
Reid Spencer5f016e22007-07-11 17:01:13 +0000866 if (Char != '\n' && Char != '\r')
867 break;
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Reid Spencer5f016e22007-07-11 17:01:13 +0000869 if (ParsingPreprocessorDirective) {
870 // End of preprocessor directive line, let LexTokenInternal handle this.
871 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +0000872 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000873 }
Mike Stump1eb44332009-09-09 15:08:12 +0000874
Reid Spencer5f016e22007-07-11 17:01:13 +0000875 // ok, but handle newline.
876 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +0000877 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +0000878 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +0000879 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000880 Char = *++CurPtr;
881 }
882
883 // If this isn't immediately after a newline, there is leading space.
884 char PrevChar = CurPtr[-1];
885 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattnerd2177732007-07-20 16:59:19 +0000886 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +0000887
Chris Lattnerd88dc482008-10-12 04:05:48 +0000888 // If the client wants us to return whitespace, return it now.
889 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +0000890 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +0000891 return true;
892 }
Mike Stump1eb44332009-09-09 15:08:12 +0000893
Reid Spencer5f016e22007-07-11 17:01:13 +0000894 BufferPtr = CurPtr;
Chris Lattnerd88dc482008-10-12 04:05:48 +0000895 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000896}
897
898// SkipBCPLComment - We have just read the // characters from input. Skip until
899// we find the newline character thats terminate the comment. Then update
Chris Lattner2d381892008-10-12 04:15:42 +0000900/// BufferPtr and return. If we're in KeepCommentMode, this will form the token
901/// and return true.
Chris Lattnerd2177732007-07-20 16:59:19 +0000902bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000903 // If BCPL comments aren't explicitly enabled for this language, emit an
904 // extension warning.
Chris Lattner74d15df2008-11-22 02:02:22 +0000905 if (!Features.BCPLComment && !isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000906 Diag(BufferPtr, diag::ext_bcpl_comment);
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Reid Spencer5f016e22007-07-11 17:01:13 +0000908 // Mark them enabled so we only emit one warning for this translation
909 // unit.
910 Features.BCPLComment = true;
911 }
Mike Stump1eb44332009-09-09 15:08:12 +0000912
Reid Spencer5f016e22007-07-11 17:01:13 +0000913 // Scan over the body of the comment. The common case, when scanning, is that
914 // the comment contains normal ascii characters with nothing interesting in
915 // them. As such, optimize for this case with the inner loop.
916 char C;
917 do {
918 C = *CurPtr;
919 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
920 // If we find a \n character, scan backwards, checking to see if it's an
921 // escaped newline, like we do for block comments.
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Reid Spencer5f016e22007-07-11 17:01:13 +0000923 // Skip over characters in the fast loop.
924 while (C != 0 && // Potentially EOF.
925 C != '\\' && // Potentially escaped newline.
926 C != '?' && // Potentially trigraph.
927 C != '\n' && C != '\r') // Newline or DOS-style newline.
928 C = *++CurPtr;
929
930 // If this is a newline, we're done.
931 if (C == '\n' || C == '\r')
932 break; // Found the newline? Break out!
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Reid Spencer5f016e22007-07-11 17:01:13 +0000934 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000935 // properly decode the character. Read it in raw mode to avoid emitting
936 // diagnostics about things like trigraphs. If we see an escaped newline,
937 // we'll handle it below.
Reid Spencer5f016e22007-07-11 17:01:13 +0000938 const char *OldPtr = CurPtr;
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000939 bool OldRawMode = isLexingRawMode();
940 LexingRawMode = true;
Reid Spencer5f016e22007-07-11 17:01:13 +0000941 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerbc3e9842008-12-12 07:34:39 +0000942 LexingRawMode = OldRawMode;
Chris Lattneread616c2009-04-05 00:26:41 +0000943
944 // If the char that we finally got was a \n, then we must have had something
945 // like \<newline><newline>. We don't want to have consumed the second
946 // newline, we want CurPtr, to end up pointing to it down below.
947 if (C == '\n' || C == '\r') {
948 --CurPtr;
949 C = 'x'; // doesn't matter what this is.
950 }
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 // If we read multiple characters, and one of those characters was a \r or
953 // \n, then we had an escaped newline within the comment. Emit diagnostic
954 // unless the next line is also a // comment.
955 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
956 for (; OldPtr != CurPtr; ++OldPtr)
957 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
958 // Okay, we found a // comment that ends in a newline, if the next
959 // line is also a // comment, but has spaces, don't emit a diagnostic.
960 if (isspace(C)) {
961 const char *ForwardPtr = CurPtr;
962 while (isspace(*ForwardPtr)) // Skip whitespace.
963 ++ForwardPtr;
964 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
965 break;
966 }
Mike Stump1eb44332009-09-09 15:08:12 +0000967
Chris Lattner74d15df2008-11-22 02:02:22 +0000968 if (!isLexingRawMode())
969 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +0000970 break;
971 }
972 }
Mike Stump1eb44332009-09-09 15:08:12 +0000973
Reid Spencer5f016e22007-07-11 17:01:13 +0000974 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
975 } while (C != '\n' && C != '\r');
976
977 // Found but did not consume the newline.
Douglas Gregor2e222532009-07-02 17:08:52 +0000978 if (PP)
Mike Stump1eb44332009-09-09 15:08:12 +0000979 PP->HandleComment(SourceRange(getSourceLocation(BufferPtr),
Douglas Gregor2e222532009-07-02 17:08:52 +0000980 getSourceLocation(CurPtr)));
Mike Stump1eb44332009-09-09 15:08:12 +0000981
Reid Spencer5f016e22007-07-11 17:01:13 +0000982 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +0000983 if (inKeepCommentMode())
Reid Spencer5f016e22007-07-11 17:01:13 +0000984 return SaveBCPLComment(Result, CurPtr);
985
986 // If we are inside a preprocessor directive and we see the end of line,
987 // return immediately, so that the lexer can return this as an EOM token.
988 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
989 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +0000990 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +0000991 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner7a4f0042008-10-12 00:23:07 +0000994 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattnerd88dc482008-10-12 04:05:48 +0000995 // contribute to another token), it isn't needed for correctness. Note that
996 // this is ok even in KeepWhitespaceMode, because we would have returned the
997 /// comment above in that mode.
Reid Spencer5f016e22007-07-11 17:01:13 +0000998 ++CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +0000999
Reid Spencer5f016e22007-07-11 17:01:13 +00001000 // The next returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +00001001 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001002 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +00001003 Result.clearFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001004 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001005 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001006}
1007
1008/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
1009/// an appropriate way and return it.
Chris Lattnerd2177732007-07-20 16:59:19 +00001010bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001011 // If we're not in a preprocessor directive, just return the // comment
1012 // directly.
1013 FormTokenWithChars(Result, CurPtr, tok::comment);
Mike Stump1eb44332009-09-09 15:08:12 +00001014
Chris Lattner9e6293d2008-10-12 04:51:35 +00001015 if (!ParsingPreprocessorDirective)
1016 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00001017
Chris Lattner9e6293d2008-10-12 04:51:35 +00001018 // If this BCPL-style comment is in a macro definition, transmogrify it into
1019 // a C-style block comment.
1020 std::string Spelling = PP->getSpelling(Result);
1021 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
1022 Spelling[1] = '*'; // Change prefix to "/*".
1023 Spelling += "*/"; // add suffix.
Mike Stump1eb44332009-09-09 15:08:12 +00001024
Chris Lattner9e6293d2008-10-12 04:51:35 +00001025 Result.setKind(tok::comment);
Chris Lattner47246be2009-01-26 19:29:26 +00001026 PP->CreateString(&Spelling[0], Spelling.size(), Result,
1027 Result.getLocation());
Chris Lattner2d381892008-10-12 04:15:42 +00001028 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001029}
1030
1031/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
1032/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner47a2b402008-12-12 07:14:34 +00001033/// diagnostic if so. We know that the newline is inside of a block comment.
Mike Stump1eb44332009-09-09 15:08:12 +00001034static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
Reid Spencer5f016e22007-07-11 17:01:13 +00001035 Lexer *L) {
1036 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Reid Spencer5f016e22007-07-11 17:01:13 +00001038 // Back up off the newline.
1039 --CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001040
Reid Spencer5f016e22007-07-11 17:01:13 +00001041 // If this is a two-character newline sequence, skip the other character.
1042 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
1043 // \n\n or \r\r -> not escaped newline.
1044 if (CurPtr[0] == CurPtr[1])
1045 return false;
1046 // \n\r or \r\n -> skip the newline.
1047 --CurPtr;
1048 }
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Reid Spencer5f016e22007-07-11 17:01:13 +00001050 // If we have horizontal whitespace, skip over it. We allow whitespace
1051 // between the slash and newline.
1052 bool HasSpace = false;
1053 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
1054 --CurPtr;
1055 HasSpace = true;
1056 }
Mike Stump1eb44332009-09-09 15:08:12 +00001057
Reid Spencer5f016e22007-07-11 17:01:13 +00001058 // If we have a slash, we know this is an escaped newline.
1059 if (*CurPtr == '\\') {
1060 if (CurPtr[-1] != '*') return false;
1061 } else {
1062 // It isn't a slash, is it the ?? / trigraph?
1063 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
1064 CurPtr[-3] != '*')
1065 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Reid Spencer5f016e22007-07-11 17:01:13 +00001067 // This is the trigraph ending the comment. Emit a stern warning!
1068 CurPtr -= 2;
1069
1070 // If no trigraphs are enabled, warn that we ignored this trigraph and
1071 // ignore this * character.
1072 if (!L->getFeatures().Trigraphs) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001073 if (!L->isLexingRawMode())
1074 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001075 return false;
1076 }
Chris Lattner74d15df2008-11-22 02:02:22 +00001077 if (!L->isLexingRawMode())
1078 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001079 }
Mike Stump1eb44332009-09-09 15:08:12 +00001080
Reid Spencer5f016e22007-07-11 17:01:13 +00001081 // Warn about having an escaped newline between the */ characters.
Chris Lattner74d15df2008-11-22 02:02:22 +00001082 if (!L->isLexingRawMode())
1083 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Reid Spencer5f016e22007-07-11 17:01:13 +00001085 // If there was space between the backslash and newline, warn about it.
Chris Lattner74d15df2008-11-22 02:02:22 +00001086 if (HasSpace && !L->isLexingRawMode())
1087 L->Diag(CurPtr, diag::backslash_newline_space);
Mike Stump1eb44332009-09-09 15:08:12 +00001088
Reid Spencer5f016e22007-07-11 17:01:13 +00001089 return true;
1090}
1091
1092#ifdef __SSE2__
1093#include <emmintrin.h>
1094#elif __ALTIVEC__
1095#include <altivec.h>
1096#undef bool
1097#endif
1098
1099/// SkipBlockComment - We have just read the /* characters from input. Read
1100/// until we find the */ characters that terminate the comment. Note that we
1101/// don't bother decoding trigraphs or escaped newlines in block comments,
1102/// because they cannot cause the comment to end. The only thing that can
1103/// happen is the comment could end with an escaped newline between the */ end
1104/// of comment.
Chris Lattner2d381892008-10-12 04:15:42 +00001105///
1106/// If KeepCommentMode is enabled, this forms a token from the comment and
1107/// returns true.
Chris Lattnerd2177732007-07-20 16:59:19 +00001108bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001109 // Scan one character past where we should, looking for a '/' character. Once
1110 // we find it, check to see if it was preceeded by a *. This common
1111 // optimization helps people who like to put a lot of * characters in their
1112 // comments.
Chris Lattner8146b682007-07-21 23:43:37 +00001113
1114 // The first character we get with newlines and trigraphs skipped to handle
1115 // the degenerate /*/ case below correctly if the * has an escaped newline
1116 // after it.
1117 unsigned CharSize;
1118 unsigned char C = getCharAndSize(CurPtr, CharSize);
1119 CurPtr += CharSize;
Reid Spencer5f016e22007-07-11 17:01:13 +00001120 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001121 if (!isLexingRawMode())
Chris Lattner0af57422008-10-12 01:31:51 +00001122 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001123 --CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001124
Chris Lattner31f0eca2008-10-12 04:19:49 +00001125 // KeepWhitespaceMode should return this broken comment as a token. Since
1126 // it isn't a well formed comment, just return it as an 'unknown' token.
1127 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001128 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001129 return true;
1130 }
Mike Stump1eb44332009-09-09 15:08:12 +00001131
Chris Lattner31f0eca2008-10-12 04:19:49 +00001132 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001133 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001134 }
Mike Stump1eb44332009-09-09 15:08:12 +00001135
Chris Lattner8146b682007-07-21 23:43:37 +00001136 // Check to see if the first character after the '/*' is another /. If so,
1137 // then this slash does not end the block comment, it is part of it.
1138 if (C == '/')
1139 C = *CurPtr++;
Mike Stump1eb44332009-09-09 15:08:12 +00001140
Reid Spencer5f016e22007-07-11 17:01:13 +00001141 while (1) {
1142 // Skip over all non-interesting characters until we find end of buffer or a
1143 // (probably ending) '/' character.
1144 if (CurPtr + 24 < BufferEnd) {
1145 // While not aligned to a 16-byte boundary.
1146 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1147 C = *CurPtr++;
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Reid Spencer5f016e22007-07-11 17:01:13 +00001149 if (C == '/') goto FoundSlash;
1150
1151#ifdef __SSE2__
1152 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1153 '/', '/', '/', '/', '/', '/', '/', '/');
1154 while (CurPtr+16 <= BufferEnd &&
1155 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1156 CurPtr += 16;
1157#elif __ALTIVEC__
1158 __vector unsigned char Slashes = {
Mike Stump1eb44332009-09-09 15:08:12 +00001159 '/', '/', '/', '/', '/', '/', '/', '/',
Reid Spencer5f016e22007-07-11 17:01:13 +00001160 '/', '/', '/', '/', '/', '/', '/', '/'
1161 };
1162 while (CurPtr+16 <= BufferEnd &&
1163 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1164 CurPtr += 16;
Mike Stump1eb44332009-09-09 15:08:12 +00001165#else
Reid Spencer5f016e22007-07-11 17:01:13 +00001166 // Scan for '/' quickly. Many block comments are very large.
1167 while (CurPtr[0] != '/' &&
1168 CurPtr[1] != '/' &&
1169 CurPtr[2] != '/' &&
1170 CurPtr[3] != '/' &&
1171 CurPtr+4 < BufferEnd) {
1172 CurPtr += 4;
1173 }
1174#endif
Mike Stump1eb44332009-09-09 15:08:12 +00001175
Reid Spencer5f016e22007-07-11 17:01:13 +00001176 // It has to be one of the bytes scanned, increment to it and read one.
1177 C = *CurPtr++;
1178 }
Mike Stump1eb44332009-09-09 15:08:12 +00001179
Reid Spencer5f016e22007-07-11 17:01:13 +00001180 // Loop to scan the remainder.
1181 while (C != '/' && C != '\0')
1182 C = *CurPtr++;
Mike Stump1eb44332009-09-09 15:08:12 +00001183
Reid Spencer5f016e22007-07-11 17:01:13 +00001184 FoundSlash:
1185 if (C == '/') {
1186 if (CurPtr[-2] == '*') // We found the final */. We're done!
1187 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Reid Spencer5f016e22007-07-11 17:01:13 +00001189 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
1190 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
1191 // We found the final */, though it had an escaped newline between the
1192 // * and /. We're done!
1193 break;
1194 }
1195 }
1196 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1197 // If this is a /* inside of the comment, emit a warning. Don't do this
1198 // if this is a /*/, which will end the comment. This misses cases with
1199 // embedded escaped newlines, but oh well.
Chris Lattner74d15df2008-11-22 02:02:22 +00001200 if (!isLexingRawMode())
1201 Diag(CurPtr-1, diag::warn_nested_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001202 }
1203 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001204 if (!isLexingRawMode())
1205 Diag(BufferPtr, diag::err_unterminated_block_comment);
Reid Spencer5f016e22007-07-11 17:01:13 +00001206 // Note: the user probably forgot a */. We could continue immediately
1207 // after the /*, but this would involve lexing a lot of what really is the
1208 // comment, which surely would confuse the parser.
Chris Lattner31f0eca2008-10-12 04:19:49 +00001209 --CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Chris Lattner31f0eca2008-10-12 04:19:49 +00001211 // KeepWhitespaceMode should return this broken comment as a token. Since
1212 // it isn't a well formed comment, just return it as an 'unknown' token.
1213 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001214 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner31f0eca2008-10-12 04:19:49 +00001215 return true;
1216 }
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Chris Lattner31f0eca2008-10-12 04:19:49 +00001218 BufferPtr = CurPtr;
Chris Lattner2d381892008-10-12 04:15:42 +00001219 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001220 }
1221 C = *CurPtr++;
1222 }
Mike Stump1eb44332009-09-09 15:08:12 +00001223
1224 if (PP)
1225 PP->HandleComment(SourceRange(getSourceLocation(BufferPtr),
Douglas Gregor2e222532009-07-02 17:08:52 +00001226 getSourceLocation(CurPtr)));
1227
Reid Spencer5f016e22007-07-11 17:01:13 +00001228 // If we are returning comments as tokens, return this comment as a token.
Chris Lattnerfa95a012008-10-12 03:22:02 +00001229 if (inKeepCommentMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001230 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattner2d381892008-10-12 04:15:42 +00001231 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00001232 }
1233
1234 // It is common for the tokens immediately after a /**/ comment to be
1235 // whitespace. Instead of going through the big switch, handle it
Chris Lattnerd88dc482008-10-12 04:05:48 +00001236 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1237 // have already returned above with the comment as a token.
Reid Spencer5f016e22007-07-11 17:01:13 +00001238 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001239 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001240 SkipWhitespace(Result, CurPtr+1);
Chris Lattner2d381892008-10-12 04:15:42 +00001241 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001242 }
1243
1244 // Otherwise, just return so that the next character will be lexed as a token.
1245 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001246 Result.setFlag(Token::LeadingSpace);
Chris Lattner2d381892008-10-12 04:15:42 +00001247 return false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001248}
1249
1250//===----------------------------------------------------------------------===//
1251// Primary Lexing Entry Points
1252//===----------------------------------------------------------------------===//
1253
Reid Spencer5f016e22007-07-11 17:01:13 +00001254/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1255/// uninterpreted string. This switches the lexer out of directive mode.
1256std::string Lexer::ReadToEndOfLine() {
1257 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1258 "Must be in a preprocessing directive!");
1259 std::string Result;
Chris Lattnerd2177732007-07-20 16:59:19 +00001260 Token Tmp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001261
1262 // CurPtr - Cache BufferPtr in an automatic variable.
1263 const char *CurPtr = BufferPtr;
1264 while (1) {
1265 char Char = getAndAdvanceChar(CurPtr, Tmp);
1266 switch (Char) {
1267 default:
1268 Result += Char;
1269 break;
1270 case 0: // Null.
1271 // Found end of file?
1272 if (CurPtr-1 != BufferEnd) {
1273 // Nope, normal character, continue.
1274 Result += Char;
1275 break;
1276 }
1277 // FALL THROUGH.
1278 case '\r':
1279 case '\n':
1280 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1281 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1282 BufferPtr = CurPtr-1;
Mike Stump1eb44332009-09-09 15:08:12 +00001283
Reid Spencer5f016e22007-07-11 17:01:13 +00001284 // Next, lex the character, which should handle the EOM transition.
1285 Lex(Tmp);
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001286 assert(Tmp.is(tok::eom) && "Unexpected token!");
Mike Stump1eb44332009-09-09 15:08:12 +00001287
Reid Spencer5f016e22007-07-11 17:01:13 +00001288 // Finally, we're done, return the string we found.
1289 return Result;
1290 }
1291 }
1292}
1293
1294/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1295/// condition, reporting diagnostics and handling other edge cases as required.
1296/// This returns true if Result contains a token, false if PP.Lex should be
1297/// called again.
Chris Lattnerd2177732007-07-20 16:59:19 +00001298bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001299 // If we hit the end of the file while parsing a preprocessor directive,
1300 // end the preprocessor directive first. The next token returned will
1301 // then be the end of file.
1302 if (ParsingPreprocessorDirective) {
1303 // Done parsing the "line".
1304 ParsingPreprocessorDirective = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00001305 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00001306 FormTokenWithChars(Result, CurPtr, tok::eom);
Mike Stump1eb44332009-09-09 15:08:12 +00001307
Reid Spencer5f016e22007-07-11 17:01:13 +00001308 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001309 SetCommentRetentionState(PP->getCommentRetentionState());
Reid Spencer5f016e22007-07-11 17:01:13 +00001310 return true; // Have a token.
Mike Stump1eb44332009-09-09 15:08:12 +00001311 }
Douglas Gregor86d9a522009-09-21 16:56:56 +00001312
Reid Spencer5f016e22007-07-11 17:01:13 +00001313 // If we are in raw mode, return this event as an EOF token. Let the caller
1314 // that put us in raw mode handle the event.
Chris Lattner74d15df2008-11-22 02:02:22 +00001315 if (isLexingRawMode()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001316 Result.startToken();
1317 BufferPtr = BufferEnd;
Chris Lattner9e6293d2008-10-12 04:51:35 +00001318 FormTokenWithChars(Result, BufferEnd, tok::eof);
Reid Spencer5f016e22007-07-11 17:01:13 +00001319 return true;
1320 }
Mike Stump1eb44332009-09-09 15:08:12 +00001321
Douglas Gregor86d9a522009-09-21 16:56:56 +00001322 // Otherwise, check if we are code-completing, then issue diagnostics for
1323 // unterminated #if and missing newline.
Reid Spencer5f016e22007-07-11 17:01:13 +00001324
Douglas Gregor86d9a522009-09-21 16:56:56 +00001325 if (IsEofCodeCompletion) {
1326 // We're at the end of the file, but we've been asked to conside the
1327 // end of the file to be a code-completion token. Return the
1328 // code-completion token.
1329 Result.startToken();
1330 FormTokenWithChars(Result, CurPtr, tok::code_completion);
1331
1332 // Only do the eof -> code_completion translation once.
1333 IsEofCodeCompletion = false;
1334 return true;
1335 }
1336
Reid Spencer5f016e22007-07-11 17:01:13 +00001337 // If we are in a #if directive, emit an error.
1338 while (!ConditionalStack.empty()) {
Chris Lattner30c64762008-11-22 06:22:39 +00001339 PP->Diag(ConditionalStack.back().IfLoc,
1340 diag::err_pp_unterminated_conditional);
Reid Spencer5f016e22007-07-11 17:01:13 +00001341 ConditionalStack.pop_back();
1342 }
Mike Stump1eb44332009-09-09 15:08:12 +00001343
Chris Lattnerb25e5d72008-04-12 05:54:25 +00001344 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1345 // a pedwarn.
1346 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Mike Stump20d0ee52009-04-02 02:29:42 +00001347 Diag(BufferEnd, diag::ext_no_newline_eof)
1348 << CodeModificationHint::CreateInsertion(getSourceLocation(BufferEnd),
1349 "\n");
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Reid Spencer5f016e22007-07-11 17:01:13 +00001351 BufferPtr = CurPtr;
1352
1353 // Finally, let the preprocessor handle this.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001354 return PP->HandleEndOfFile(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001355}
1356
1357/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1358/// the specified lexer will return a tok::l_paren token, 0 if it is something
1359/// else and 2 if there are no more tokens in the buffer controlled by the
1360/// lexer.
1361unsigned Lexer::isNextPPTokenLParen() {
1362 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
Mike Stump1eb44332009-09-09 15:08:12 +00001363
Reid Spencer5f016e22007-07-11 17:01:13 +00001364 // Switch to 'skipping' mode. This will ensure that we can lex a token
1365 // without emitting diagnostics, disables macro expansion, and will cause EOF
1366 // to return an EOF token instead of popping the include stack.
1367 LexingRawMode = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001368
Reid Spencer5f016e22007-07-11 17:01:13 +00001369 // Save state that can be changed while lexing so that we can restore it.
1370 const char *TmpBufferPtr = BufferPtr;
Chris Lattnera864cf72009-04-24 07:15:46 +00001371 bool inPPDirectiveMode = ParsingPreprocessorDirective;
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Chris Lattnerd2177732007-07-20 16:59:19 +00001373 Token Tok;
Reid Spencer5f016e22007-07-11 17:01:13 +00001374 Tok.startToken();
1375 LexTokenInternal(Tok);
Mike Stump1eb44332009-09-09 15:08:12 +00001376
Reid Spencer5f016e22007-07-11 17:01:13 +00001377 // Restore state that may have changed.
1378 BufferPtr = TmpBufferPtr;
Chris Lattnera864cf72009-04-24 07:15:46 +00001379 ParsingPreprocessorDirective = inPPDirectiveMode;
Mike Stump1eb44332009-09-09 15:08:12 +00001380
Reid Spencer5f016e22007-07-11 17:01:13 +00001381 // Restore the lexer back to non-skipping mode.
1382 LexingRawMode = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001383
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001384 if (Tok.is(tok::eof))
Reid Spencer5f016e22007-07-11 17:01:13 +00001385 return 2;
Chris Lattner22f6bbc2007-10-09 18:02:16 +00001386 return Tok.is(tok::l_paren);
Reid Spencer5f016e22007-07-11 17:01:13 +00001387}
1388
1389
1390/// LexTokenInternal - This implements a simple C family lexer. It is an
1391/// extremely performance critical piece of code. This assumes that the buffer
Chris Lattnerefb173d2009-07-07 05:05:42 +00001392/// has a null character at the end of the file. This returns a preprocessing
1393/// token, not a normal token, as such, it is an internal interface. It assumes
1394/// that the Flags of result have been cleared before calling this.
Chris Lattnerd2177732007-07-20 16:59:19 +00001395void Lexer::LexTokenInternal(Token &Result) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001396LexNextToken:
1397 // New token, can't need cleaning yet.
Chris Lattnerd2177732007-07-20 16:59:19 +00001398 Result.clearFlag(Token::NeedsCleaning);
Reid Spencer5f016e22007-07-11 17:01:13 +00001399 Result.setIdentifierInfo(0);
Mike Stump1eb44332009-09-09 15:08:12 +00001400
Reid Spencer5f016e22007-07-11 17:01:13 +00001401 // CurPtr - Cache BufferPtr in an automatic variable.
1402 const char *CurPtr = BufferPtr;
1403
1404 // Small amounts of horizontal whitespace is very common between tokens.
1405 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1406 ++CurPtr;
1407 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1408 ++CurPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001409
Chris Lattnerd88dc482008-10-12 04:05:48 +00001410 // If we are keeping whitespace and other tokens, just return what we just
1411 // skipped. The next lexer invocation will return the token after the
1412 // whitespace.
1413 if (isKeepWhitespaceMode()) {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001414 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001415 return;
1416 }
Mike Stump1eb44332009-09-09 15:08:12 +00001417
Reid Spencer5f016e22007-07-11 17:01:13 +00001418 BufferPtr = CurPtr;
Chris Lattnerd2177732007-07-20 16:59:19 +00001419 Result.setFlag(Token::LeadingSpace);
Reid Spencer5f016e22007-07-11 17:01:13 +00001420 }
Mike Stump1eb44332009-09-09 15:08:12 +00001421
Reid Spencer5f016e22007-07-11 17:01:13 +00001422 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
Mike Stump1eb44332009-09-09 15:08:12 +00001423
Reid Spencer5f016e22007-07-11 17:01:13 +00001424 // Read a character, advancing over it.
1425 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001426 tok::TokenKind Kind;
Mike Stump1eb44332009-09-09 15:08:12 +00001427
Reid Spencer5f016e22007-07-11 17:01:13 +00001428 switch (Char) {
1429 case 0: // Null.
1430 // Found end of file?
1431 if (CurPtr-1 == BufferEnd) {
1432 // Read the PP instance variable into an automatic variable, because
1433 // LexEndOfFile will often delete 'this'.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001434 Preprocessor *PPCache = PP;
Reid Spencer5f016e22007-07-11 17:01:13 +00001435 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1436 return; // Got a token to return.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001437 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1438 return PPCache->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001439 }
Mike Stump1eb44332009-09-09 15:08:12 +00001440
Chris Lattner74d15df2008-11-22 02:02:22 +00001441 if (!isLexingRawMode())
1442 Diag(CurPtr-1, diag::null_in_file);
Chris Lattnerd2177732007-07-20 16:59:19 +00001443 Result.setFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001444 if (SkipWhitespace(Result, CurPtr))
1445 return; // KeepWhitespaceMode
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Reid Spencer5f016e22007-07-11 17:01:13 +00001447 goto LexNextToken; // GCC isn't tail call eliminating.
1448 case '\n':
1449 case '\r':
1450 // If we are inside a preprocessor directive and we see the end of line,
1451 // we know we are done with the directive, so return an EOM token.
1452 if (ParsingPreprocessorDirective) {
1453 // Done parsing the "line".
1454 ParsingPreprocessorDirective = false;
Mike Stump1eb44332009-09-09 15:08:12 +00001455
Reid Spencer5f016e22007-07-11 17:01:13 +00001456 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattnerf744d132008-10-12 03:27:19 +00001457 SetCommentRetentionState(PP->getCommentRetentionState());
Mike Stump1eb44332009-09-09 15:08:12 +00001458
Reid Spencer5f016e22007-07-11 17:01:13 +00001459 // Since we consumed a newline, we are back at the start of a line.
1460 IsAtStartOfLine = true;
Mike Stump1eb44332009-09-09 15:08:12 +00001461
Chris Lattner9e6293d2008-10-12 04:51:35 +00001462 Kind = tok::eom;
Reid Spencer5f016e22007-07-11 17:01:13 +00001463 break;
1464 }
1465 // The returned token is at the start of the line.
Chris Lattnerd2177732007-07-20 16:59:19 +00001466 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001467 // No leading whitespace seen so far.
Chris Lattnerd2177732007-07-20 16:59:19 +00001468 Result.clearFlag(Token::LeadingSpace);
Mike Stump1eb44332009-09-09 15:08:12 +00001469
Chris Lattnerd88dc482008-10-12 04:05:48 +00001470 if (SkipWhitespace(Result, CurPtr))
1471 return; // KeepWhitespaceMode
Reid Spencer5f016e22007-07-11 17:01:13 +00001472 goto LexNextToken; // GCC isn't tail call eliminating.
1473 case ' ':
1474 case '\t':
1475 case '\f':
1476 case '\v':
Chris Lattner8133cfc2007-07-22 06:29:05 +00001477 SkipHorizontalWhitespace:
Chris Lattnerd2177732007-07-20 16:59:19 +00001478 Result.setFlag(Token::LeadingSpace);
Chris Lattnerd88dc482008-10-12 04:05:48 +00001479 if (SkipWhitespace(Result, CurPtr))
1480 return; // KeepWhitespaceMode
Chris Lattner8133cfc2007-07-22 06:29:05 +00001481
1482 SkipIgnoredUnits:
1483 CurPtr = BufferPtr;
Mike Stump1eb44332009-09-09 15:08:12 +00001484
Chris Lattner8133cfc2007-07-22 06:29:05 +00001485 // If the next token is obviously a // or /* */ comment, skip it efficiently
1486 // too (without going through the big switch stmt).
Chris Lattner8402c732009-01-16 22:39:25 +00001487 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1488 Features.BCPLComment) {
Chris Lattner8133cfc2007-07-22 06:29:05 +00001489 SkipBCPLComment(Result, CurPtr+2);
1490 goto SkipIgnoredUnits;
Chris Lattnerfa95a012008-10-12 03:22:02 +00001491 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner8133cfc2007-07-22 06:29:05 +00001492 SkipBlockComment(Result, CurPtr+2);
1493 goto SkipIgnoredUnits;
1494 } else if (isHorizontalWhitespace(*CurPtr)) {
1495 goto SkipHorizontalWhitespace;
1496 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001497 goto LexNextToken; // GCC isn't tail call eliminating.
1498
Chris Lattner3a570772008-01-03 17:58:54 +00001499 // C99 6.4.4.1: Integer Constants.
1500 // C99 6.4.4.2: Floating Constants.
1501 case '0': case '1': case '2': case '3': case '4':
1502 case '5': case '6': case '7': case '8': case '9':
1503 // Notify MIOpt that we read a non-whitespace/non-comment token.
1504 MIOpt.ReadToken();
1505 return LexNumericConstant(Result, CurPtr);
Mike Stump1eb44332009-09-09 15:08:12 +00001506
Chris Lattner3a570772008-01-03 17:58:54 +00001507 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Reid Spencer5f016e22007-07-11 17:01:13 +00001508 // Notify MIOpt that we read a non-whitespace/non-comment token.
1509 MIOpt.ReadToken();
1510 Char = getCharAndSize(CurPtr, SizeTmp);
1511
1512 // Wide string literal.
1513 if (Char == '"')
1514 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1515 true);
1516
1517 // Wide character constant.
1518 if (Char == '\'')
1519 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1520 // FALL THROUGH, treating L like the start of an identifier.
Mike Stump1eb44332009-09-09 15:08:12 +00001521
Reid Spencer5f016e22007-07-11 17:01:13 +00001522 // C99 6.4.2: Identifiers.
1523 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1524 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1525 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1526 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1527 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1528 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1529 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1530 case 'v': case 'w': case 'x': case 'y': case 'z':
1531 case '_':
1532 // Notify MIOpt that we read a non-whitespace/non-comment token.
1533 MIOpt.ReadToken();
1534 return LexIdentifier(Result, CurPtr);
Chris Lattner3a570772008-01-03 17:58:54 +00001535
1536 case '$': // $ in identifiers.
1537 if (Features.DollarIdents) {
Chris Lattner74d15df2008-11-22 02:02:22 +00001538 if (!isLexingRawMode())
1539 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner3a570772008-01-03 17:58:54 +00001540 // Notify MIOpt that we read a non-whitespace/non-comment token.
1541 MIOpt.ReadToken();
1542 return LexIdentifier(Result, CurPtr);
1543 }
Mike Stump1eb44332009-09-09 15:08:12 +00001544
Chris Lattner9e6293d2008-10-12 04:51:35 +00001545 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00001546 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001547
Reid Spencer5f016e22007-07-11 17:01:13 +00001548 // C99 6.4.4: Character Constants.
1549 case '\'':
1550 // Notify MIOpt that we read a non-whitespace/non-comment token.
1551 MIOpt.ReadToken();
1552 return LexCharConstant(Result, CurPtr);
1553
1554 // C99 6.4.5: String Literals.
1555 case '"':
1556 // Notify MIOpt that we read a non-whitespace/non-comment token.
1557 MIOpt.ReadToken();
1558 return LexStringLiteral(Result, CurPtr, false);
1559
1560 // C99 6.4.6: Punctuators.
1561 case '?':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001562 Kind = tok::question;
Reid Spencer5f016e22007-07-11 17:01:13 +00001563 break;
1564 case '[':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001565 Kind = tok::l_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001566 break;
1567 case ']':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001568 Kind = tok::r_square;
Reid Spencer5f016e22007-07-11 17:01:13 +00001569 break;
1570 case '(':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001571 Kind = tok::l_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001572 break;
1573 case ')':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001574 Kind = tok::r_paren;
Reid Spencer5f016e22007-07-11 17:01:13 +00001575 break;
1576 case '{':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001577 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001578 break;
1579 case '}':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001580 Kind = tok::r_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001581 break;
1582 case '.':
1583 Char = getCharAndSize(CurPtr, SizeTmp);
1584 if (Char >= '0' && Char <= '9') {
1585 // Notify MIOpt that we read a non-whitespace/non-comment token.
1586 MIOpt.ReadToken();
1587
1588 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1589 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001590 Kind = tok::periodstar;
Reid Spencer5f016e22007-07-11 17:01:13 +00001591 CurPtr += SizeTmp;
1592 } else if (Char == '.' &&
1593 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001594 Kind = tok::ellipsis;
Reid Spencer5f016e22007-07-11 17:01:13 +00001595 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1596 SizeTmp2, Result);
1597 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001598 Kind = tok::period;
Reid Spencer5f016e22007-07-11 17:01:13 +00001599 }
1600 break;
1601 case '&':
1602 Char = getCharAndSize(CurPtr, SizeTmp);
1603 if (Char == '&') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001604 Kind = tok::ampamp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001605 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1606 } else if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001607 Kind = tok::ampequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001608 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1609 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001610 Kind = tok::amp;
Reid Spencer5f016e22007-07-11 17:01:13 +00001611 }
1612 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001613 case '*':
Reid Spencer5f016e22007-07-11 17:01:13 +00001614 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001615 Kind = tok::starequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001616 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1617 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001618 Kind = tok::star;
Reid Spencer5f016e22007-07-11 17:01:13 +00001619 }
1620 break;
1621 case '+':
1622 Char = getCharAndSize(CurPtr, SizeTmp);
1623 if (Char == '+') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001624 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001625 Kind = tok::plusplus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001626 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001627 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001628 Kind = tok::plusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001629 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001630 Kind = tok::plus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001631 }
1632 break;
1633 case '-':
1634 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001635 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::minusminus;
Mike Stump1eb44332009-09-09 15:08:12 +00001638 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattner9e6293d2008-10-12 04:51:35 +00001639 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Reid Spencer5f016e22007-07-11 17:01:13 +00001640 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1641 SizeTmp2, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001642 Kind = tok::arrowstar;
1643 } else if (Char == '>') { // ->
Reid Spencer5f016e22007-07-11 17:01:13 +00001644 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001645 Kind = tok::arrow;
1646 } else if (Char == '=') { // -=
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001648 Kind = tok::minusequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001649 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001650 Kind = tok::minus;
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 }
1652 break;
1653 case '~':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001654 Kind = tok::tilde;
Reid Spencer5f016e22007-07-11 17:01:13 +00001655 break;
1656 case '!':
1657 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001658 Kind = tok::exclaimequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001659 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1660 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001661 Kind = tok::exclaim;
Reid Spencer5f016e22007-07-11 17:01:13 +00001662 }
1663 break;
1664 case '/':
1665 // 6.4.9: Comments
1666 Char = getCharAndSize(CurPtr, SizeTmp);
1667 if (Char == '/') { // BCPL comment.
Chris Lattner8402c732009-01-16 22:39:25 +00001668 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
1669 // want to lex this as a comment. There is one problem with this though,
1670 // that in one particular corner case, this can change the behavior of the
1671 // resultant program. For example, In "foo //**/ bar", C89 would lex
1672 // this as "foo / bar" and langauges with BCPL comments would lex it as
1673 // "foo". Check to see if the character after the second slash is a '*'.
1674 // If so, we will lex that as a "/" instead of the start of a comment.
1675 if (Features.BCPLComment ||
1676 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
1677 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
1678 return; // KeepCommentMode
Mike Stump1eb44332009-09-09 15:08:12 +00001679
Chris Lattner8402c732009-01-16 22:39:25 +00001680 // It is common for the tokens immediately after a // comment to be
1681 // whitespace (indentation for the next line). Instead of going through
1682 // the big switch, handle it efficiently now.
1683 goto SkipIgnoredUnits;
1684 }
1685 }
Mike Stump1eb44332009-09-09 15:08:12 +00001686
Chris Lattner8402c732009-01-16 22:39:25 +00001687 if (Char == '*') { // /**/ comment.
Reid Spencer5f016e22007-07-11 17:01:13 +00001688 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner2d381892008-10-12 04:15:42 +00001689 return; // KeepCommentMode
1690 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner8402c732009-01-16 22:39:25 +00001691 }
Mike Stump1eb44332009-09-09 15:08:12 +00001692
Chris Lattner8402c732009-01-16 22:39:25 +00001693 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001694 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001695 Kind = tok::slashequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001696 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001697 Kind = tok::slash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001698 }
1699 break;
1700 case '%':
1701 Char = getCharAndSize(CurPtr, SizeTmp);
1702 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001703 Kind = tok::percentequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001704 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1705 } else if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001706 Kind = tok::r_brace; // '%>' -> '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00001707 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1708 } else if (Features.Digraphs && Char == ':') {
1709 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1710 Char = getCharAndSize(CurPtr, SizeTmp);
1711 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001712 Kind = tok::hashhash; // '%:%:' -> '##'
Reid Spencer5f016e22007-07-11 17:01:13 +00001713 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1714 SizeTmp2, Result);
1715 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Reid Spencer5f016e22007-07-11 17:01:13 +00001716 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner74d15df2008-11-22 02:02:22 +00001717 if (!isLexingRawMode())
1718 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001719 Kind = tok::hashat;
Chris Lattnere91e9322009-03-18 20:58:27 +00001720 } else { // '%:' -> '#'
Reid Spencer5f016e22007-07-11 17:01:13 +00001721 // We parsed a # character. If this occurs at the start of the line,
1722 // it's actually the start of a preprocessing directive. Callback to
1723 // the preprocessor to handle it.
1724 // FIXME: -fpreprocessed mode??
Chris Lattner766703b2009-05-13 06:10:29 +00001725 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattnere91e9322009-03-18 20:58:27 +00001726 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner168ae2d2007-10-17 20:41:00 +00001727 PP->HandleDirective(Result);
Mike Stump1eb44332009-09-09 15:08:12 +00001728
Reid Spencer5f016e22007-07-11 17:01:13 +00001729 // As an optimization, if the preprocessor didn't switch lexers, tail
1730 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001731 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001732 // Start a new token. If this is a #include or something, the PP may
1733 // want us starting at the beginning of the line again. If so, set
1734 // the StartOfLine flag.
1735 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001736 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001737 IsAtStartOfLine = false;
1738 }
1739 goto LexNextToken; // GCC isn't tail call eliminating.
1740 }
Mike Stump1eb44332009-09-09 15:08:12 +00001741
Chris Lattner168ae2d2007-10-17 20:41:00 +00001742 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001743 }
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Chris Lattnere91e9322009-03-18 20:58:27 +00001745 Kind = tok::hash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001746 }
1747 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001748 Kind = tok::percent;
Reid Spencer5f016e22007-07-11 17:01:13 +00001749 }
1750 break;
1751 case '<':
1752 Char = getCharAndSize(CurPtr, SizeTmp);
1753 if (ParsingFilename) {
Chris Lattner9cb51ce2009-04-17 23:56:52 +00001754 return LexAngledStringLiteral(Result, CurPtr);
Reid Spencer5f016e22007-07-11 17:01:13 +00001755 } else if (Char == '<' &&
1756 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001757 Kind = tok::lesslessequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001758 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1759 SizeTmp2, Result);
1760 } else if (Char == '<') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001761 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001762 Kind = tok::lessless;
Reid Spencer5f016e22007-07-11 17:01:13 +00001763 } else if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001764 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001765 Kind = tok::lessequal;
1766 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Reid Spencer5f016e22007-07-11 17:01:13 +00001767 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001768 Kind = tok::l_square;
1769 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Reid Spencer5f016e22007-07-11 17:01:13 +00001770 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001771 Kind = tok::l_brace;
Reid Spencer5f016e22007-07-11 17:01:13 +00001772 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001773 Kind = tok::less;
Reid Spencer5f016e22007-07-11 17:01:13 +00001774 }
1775 break;
1776 case '>':
1777 Char = getCharAndSize(CurPtr, SizeTmp);
1778 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001779 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001780 Kind = tok::greaterequal;
Mike Stump1eb44332009-09-09 15:08:12 +00001781 } else if (Char == '>' &&
Reid Spencer5f016e22007-07-11 17:01:13 +00001782 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001783 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1784 SizeTmp2, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001785 Kind = tok::greatergreaterequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001786 } else if (Char == '>') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001787 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001788 Kind = tok::greatergreater;
Reid Spencer5f016e22007-07-11 17:01:13 +00001789 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001790 Kind = tok::greater;
Reid Spencer5f016e22007-07-11 17:01:13 +00001791 }
1792 break;
1793 case '^':
1794 Char = getCharAndSize(CurPtr, SizeTmp);
1795 if (Char == '=') {
Reid Spencer5f016e22007-07-11 17:01:13 +00001796 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner9e6293d2008-10-12 04:51:35 +00001797 Kind = tok::caretequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001798 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001799 Kind = tok::caret;
Reid Spencer5f016e22007-07-11 17:01:13 +00001800 }
1801 break;
1802 case '|':
1803 Char = getCharAndSize(CurPtr, SizeTmp);
1804 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001805 Kind = tok::pipeequal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001806 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1807 } else if (Char == '|') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001808 Kind = tok::pipepipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00001809 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1810 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001811 Kind = tok::pipe;
Reid Spencer5f016e22007-07-11 17:01:13 +00001812 }
1813 break;
1814 case ':':
1815 Char = getCharAndSize(CurPtr, SizeTmp);
1816 if (Features.Digraphs && Char == '>') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001817 Kind = tok::r_square; // ':>' -> ']'
Reid Spencer5f016e22007-07-11 17:01:13 +00001818 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1819 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001820 Kind = tok::coloncolon;
Reid Spencer5f016e22007-07-11 17:01:13 +00001821 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump1eb44332009-09-09 15:08:12 +00001822 } else {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001823 Kind = tok::colon;
Reid Spencer5f016e22007-07-11 17:01:13 +00001824 }
1825 break;
1826 case ';':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001827 Kind = tok::semi;
Reid Spencer5f016e22007-07-11 17:01:13 +00001828 break;
1829 case '=':
1830 Char = getCharAndSize(CurPtr, SizeTmp);
1831 if (Char == '=') {
Chris Lattner9e6293d2008-10-12 04:51:35 +00001832 Kind = tok::equalequal;
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::equal;
Reid Spencer5f016e22007-07-11 17:01:13 +00001836 }
1837 break;
1838 case ',':
Chris Lattner9e6293d2008-10-12 04:51:35 +00001839 Kind = tok::comma;
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::hashhash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001845 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1846 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattner9e6293d2008-10-12 04:51:35 +00001847 Kind = tok::hashat;
Chris Lattner74d15df2008-11-22 02:02:22 +00001848 if (!isLexingRawMode())
1849 Diag(BufferPtr, diag::charize_microsoft_ext);
Reid Spencer5f016e22007-07-11 17:01:13 +00001850 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1851 } else {
Reid Spencer5f016e22007-07-11 17:01:13 +00001852 // We parsed a # character. If this occurs at the start of the line,
1853 // it's actually the start of a preprocessing directive. Callback to
1854 // the preprocessor to handle it.
1855 // FIXME: -fpreprocessed mode??
Chris Lattner766703b2009-05-13 06:10:29 +00001856 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattnere91e9322009-03-18 20:58:27 +00001857 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner168ae2d2007-10-17 20:41:00 +00001858 PP->HandleDirective(Result);
Mike Stump1eb44332009-09-09 15:08:12 +00001859
Reid Spencer5f016e22007-07-11 17:01:13 +00001860 // As an optimization, if the preprocessor didn't switch lexers, tail
1861 // recurse.
Chris Lattner168ae2d2007-10-17 20:41:00 +00001862 if (PP->isCurrentLexer(this)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001863 // Start a new token. If this is a #include or something, the PP may
1864 // want us starting at the beginning of the line again. If so, set
1865 // the StartOfLine flag.
1866 if (IsAtStartOfLine) {
Chris Lattnerd2177732007-07-20 16:59:19 +00001867 Result.setFlag(Token::StartOfLine);
Reid Spencer5f016e22007-07-11 17:01:13 +00001868 IsAtStartOfLine = false;
1869 }
1870 goto LexNextToken; // GCC isn't tail call eliminating.
1871 }
Chris Lattner168ae2d2007-10-17 20:41:00 +00001872 return PP->Lex(Result);
Reid Spencer5f016e22007-07-11 17:01:13 +00001873 }
Mike Stump1eb44332009-09-09 15:08:12 +00001874
Chris Lattnere91e9322009-03-18 20:58:27 +00001875 Kind = tok::hash;
Reid Spencer5f016e22007-07-11 17:01:13 +00001876 }
1877 break;
1878
Chris Lattner3a570772008-01-03 17:58:54 +00001879 case '@':
1880 // Objective C support.
1881 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattner9e6293d2008-10-12 04:51:35 +00001882 Kind = tok::at;
Chris Lattner3a570772008-01-03 17:58:54 +00001883 else
Chris Lattner9e6293d2008-10-12 04:51:35 +00001884 Kind = tok::unknown;
Chris Lattner3a570772008-01-03 17:58:54 +00001885 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001886
Reid Spencer5f016e22007-07-11 17:01:13 +00001887 case '\\':
1888 // FIXME: UCN's.
1889 // FALL THROUGH.
1890 default:
Chris Lattner9e6293d2008-10-12 04:51:35 +00001891 Kind = tok::unknown;
Reid Spencer5f016e22007-07-11 17:01:13 +00001892 break;
1893 }
Mike Stump1eb44332009-09-09 15:08:12 +00001894
Reid Spencer5f016e22007-07-11 17:01:13 +00001895 // Notify MIOpt that we read a non-whitespace/non-comment token.
1896 MIOpt.ReadToken();
1897
1898 // Update the location of token as well as BufferPtr.
Chris Lattner9e6293d2008-10-12 04:51:35 +00001899 FormTokenWithChars(Result, CurPtr, Kind);
Reid Spencer5f016e22007-07-11 17:01:13 +00001900}