blob: 60a1707fb396665b990ae54f4321d77375a4d744 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- Lexer.cpp - C Language Family Lexer ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner22eb9722006-06-18 05:43:12 +00007//
8//===----------------------------------------------------------------------===//
9//
Chris Lattner146762e2007-07-20 16:59:19 +000010// This file implements the Lexer and Token interfaces.
Chris Lattner22eb9722006-06-18 05:43:12 +000011//
12//===----------------------------------------------------------------------===//
13//
14// TODO: GCC Diagnostics emitted by the lexer:
15// PEDWARN: (form feed|vertical tab) in preprocessing directive
16//
17// Universal characters, unicode, char mapping:
18// WARNING: `%.*s' is not in NFKC
19// WARNING: `%.*s' is not in NFC
20//
21// Other:
Chris Lattner22eb9722006-06-18 05:43:12 +000022// TODO: Options to support:
23// -fexec-charset,-fwide-exec-charset
24//
25//===----------------------------------------------------------------------===//
26
27#include "clang/Lex/Lexer.h"
28#include "clang/Lex/Preprocessor.h"
Chris Lattner60f36222009-01-29 05:15:15 +000029#include "clang/Lex/LexDiagnostic.h"
Chris Lattnerdc5c0552007-07-20 16:37:10 +000030#include "clang/Basic/SourceManager.h"
Chris Lattner619c1742007-07-22 18:38:25 +000031#include "llvm/Support/Compiler.h"
Chris Lattner739e7392007-04-29 07:12:06 +000032#include "llvm/Support/MemoryBuffer.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000033#include <cctype>
Chris Lattner22eb9722006-06-18 05:43:12 +000034using namespace clang;
35
Chris Lattner3dfff972009-12-17 05:29:40 +000036static void InitCharacterInfo();
Chris Lattner22eb9722006-06-18 05:43:12 +000037
Chris Lattner4894f482007-10-07 08:47:24 +000038//===----------------------------------------------------------------------===//
39// Token Class Implementation
40//===----------------------------------------------------------------------===//
41
Mike Stump11289f42009-09-09 15:08:12 +000042/// isObjCAtKeyword - Return true if we have an ObjC keyword identifier.
Chris Lattner4894f482007-10-07 08:47:24 +000043bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const {
Douglas Gregor90abb6d2008-12-01 21:46:47 +000044 if (IdentifierInfo *II = getIdentifierInfo())
45 return II->getObjCKeywordID() == objcKey;
46 return false;
Chris Lattner4894f482007-10-07 08:47:24 +000047}
48
49/// getObjCKeywordID - Return the ObjC keyword kind.
50tok::ObjCKeywordKind Token::getObjCKeywordID() const {
51 IdentifierInfo *specId = getIdentifierInfo();
52 return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword;
53}
54
Chris Lattner67671ed2007-12-13 01:59:49 +000055
Chris Lattner4894f482007-10-07 08:47:24 +000056//===----------------------------------------------------------------------===//
57// Lexer Class Implementation
58//===----------------------------------------------------------------------===//
59
Mike Stump11289f42009-09-09 15:08:12 +000060void Lexer::InitLexer(const char *BufStart, const char *BufPtr,
Chris Lattnerf76b9202009-01-17 06:55:17 +000061 const char *BufEnd) {
Chris Lattner3dfff972009-12-17 05:29:40 +000062 InitCharacterInfo();
Mike Stump11289f42009-09-09 15:08:12 +000063
Chris Lattnerf76b9202009-01-17 06:55:17 +000064 BufferStart = BufStart;
65 BufferPtr = BufPtr;
66 BufferEnd = BufEnd;
Mike Stump11289f42009-09-09 15:08:12 +000067
Chris Lattnerf76b9202009-01-17 06:55:17 +000068 assert(BufEnd[0] == 0 &&
69 "We assume that the input buffer has a null character at the end"
70 " to simplify lexing!");
Mike Stump11289f42009-09-09 15:08:12 +000071
Chris Lattnerf76b9202009-01-17 06:55:17 +000072 Is_PragmaLexer = false;
Chris Lattner7c027ee2009-12-14 06:16:57 +000073 IsInConflictMarker = false;
Douglas Gregor2436e712009-09-17 21:32:03 +000074
Chris Lattnerf76b9202009-01-17 06:55:17 +000075 // Start of the file is a start of line.
76 IsAtStartOfLine = true;
Mike Stump11289f42009-09-09 15:08:12 +000077
Chris Lattnerf76b9202009-01-17 06:55:17 +000078 // We are not after parsing a #.
79 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +000080
Chris Lattnerf76b9202009-01-17 06:55:17 +000081 // We are not after parsing #include.
82 ParsingFilename = false;
Mike Stump11289f42009-09-09 15:08:12 +000083
Chris Lattnerf76b9202009-01-17 06:55:17 +000084 // We are not in raw mode. Raw mode disables diagnostics and interpretation
85 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used
86 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block
87 // or otherwise skipping over tokens.
88 LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +000089
Chris Lattnerf76b9202009-01-17 06:55:17 +000090 // Default to not keeping comments.
91 ExtendedTokenMode = 0;
92}
93
Chris Lattner5965a282009-01-17 07:56:59 +000094/// Lexer constructor - Create a new lexer object for the specified buffer
95/// with the specified preprocessor managing the lexing process. This lexer
96/// assumes that the associated file buffer and Preprocessor objects will
97/// outlive it, so it doesn't take ownership of either of them.
Chris Lattner710bb872009-11-30 04:18:44 +000098Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP)
Chris Lattnerc8090892009-01-17 08:03:42 +000099 : PreprocessorLexer(&PP, FID),
100 FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)),
101 Features(PP.getLangOptions()) {
Mike Stump11289f42009-09-09 15:08:12 +0000102
Chris Lattner5965a282009-01-17 07:56:59 +0000103 InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(),
104 InputFile->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000105
Chris Lattner5965a282009-01-17 07:56:59 +0000106 // Default to keeping comments if the preprocessor wants them.
107 SetCommentRetentionState(PP.getCommentRetentionState());
108}
Chris Lattner4894f482007-10-07 08:47:24 +0000109
Chris Lattner02b436a2007-10-17 20:41:00 +0000110/// Lexer constructor - Create a new raw lexer object. This object is only
Chris Lattner50c90502008-10-12 01:15:46 +0000111/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
112/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner02b436a2007-10-17 20:41:00 +0000113Lexer::Lexer(SourceLocation fileloc, const LangOptions &features,
Chris Lattnerfcf64522009-01-17 07:42:27 +0000114 const char *BufStart, const char *BufPtr, const char *BufEnd)
Chris Lattner1abd2092009-01-17 03:48:08 +0000115 : FileLoc(fileloc), Features(features) {
Chris Lattnerf76b9202009-01-17 06:55:17 +0000116
Chris Lattnerf76b9202009-01-17 06:55:17 +0000117 InitLexer(BufStart, BufPtr, BufEnd);
Mike Stump11289f42009-09-09 15:08:12 +0000118
Chris Lattner02b436a2007-10-17 20:41:00 +0000119 // We *are* in raw mode.
120 LexingRawMode = true;
Chris Lattner02b436a2007-10-17 20:41:00 +0000121}
122
Chris Lattner08354fe2009-01-17 07:35:14 +0000123/// Lexer constructor - Create a new raw lexer object. This object is only
124/// suitable for calls to 'LexRawToken'. This lexer assumes that the text
125/// range will outlive it, so it doesn't take ownership of it.
Chris Lattner710bb872009-11-30 04:18:44 +0000126Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile,
127 const SourceManager &SM, const LangOptions &features)
Chris Lattner08354fe2009-01-17 07:35:14 +0000128 : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) {
Chris Lattner08354fe2009-01-17 07:35:14 +0000129
Mike Stump11289f42009-09-09 15:08:12 +0000130 InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(),
Chris Lattner08354fe2009-01-17 07:35:14 +0000131 FromFile->getBufferEnd());
Mike Stump11289f42009-09-09 15:08:12 +0000132
Chris Lattner08354fe2009-01-17 07:35:14 +0000133 // We *are* in raw mode.
134 LexingRawMode = true;
135}
136
Chris Lattner757169b2009-01-17 08:27:52 +0000137/// Create_PragmaLexer: Lexer constructor - Create a new lexer object for
138/// _Pragma expansion. This has a variety of magic semantics that this method
139/// sets up. It returns a new'd Lexer that must be delete'd when done.
140///
141/// On entrance to this routine, TokStartLoc is a macro location which has a
142/// spelling loc that indicates the bytes to be lexed for the token and an
143/// instantiation location that indicates where all lexed tokens should be
144/// "expanded from".
145///
146/// FIXME: It would really be nice to make _Pragma just be a wrapper around a
147/// normal lexer that remaps tokens as they fly by. This would require making
148/// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer
149/// interface that could handle this stuff. This would pull GetMappedTokenLoc
150/// out of the critical path of the lexer!
151///
Mike Stump11289f42009-09-09 15:08:12 +0000152Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc,
Chris Lattner9dc9c202009-02-15 20:52:18 +0000153 SourceLocation InstantiationLocStart,
154 SourceLocation InstantiationLocEnd,
Chris Lattner29a2a192009-01-19 06:46:35 +0000155 unsigned TokLen, Preprocessor &PP) {
Chris Lattner757169b2009-01-17 08:27:52 +0000156 SourceManager &SM = PP.getSourceManager();
Chris Lattner757169b2009-01-17 08:27:52 +0000157
158 // Create the lexer as if we were going to lex the file normally.
Chris Lattnercbc35ecb2009-01-19 07:46:45 +0000159 FileID SpellingFID = SM.getFileID(SpellingLoc);
Chris Lattner710bb872009-11-30 04:18:44 +0000160 const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID);
161 Lexer *L = new Lexer(SpellingFID, InputFile, PP);
Mike Stump11289f42009-09-09 15:08:12 +0000162
Chris Lattner757169b2009-01-17 08:27:52 +0000163 // Now that the lexer is created, change the start/end locations so that we
164 // just lex the subsection of the file that we want. This is lexing from a
165 // scratch buffer.
Douglas Gregor42fe8582010-03-16 20:46:42 +0000166 bool Invalid = false;
Chris Lattner757169b2009-01-17 08:27:52 +0000167 const char *StrData = SM.getCharacterData(SpellingLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000168
Chris Lattner757169b2009-01-17 08:27:52 +0000169 L->BufferPtr = StrData;
170 L->BufferEnd = StrData+TokLen;
Chris Lattnerfa217bd2009-03-08 08:08:45 +0000171 assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!");
Chris Lattner757169b2009-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 Lattner4fa23622009-01-26 00:43:02 +0000175 L->FileLoc = SM.createInstantiationLoc(SM.getLocForStartOfFile(SpellingFID),
Chris Lattner9dc9c202009-02-15 20:52:18 +0000176 InstantiationLocStart,
177 InstantiationLocEnd, TokLen);
Mike Stump11289f42009-09-09 15:08:12 +0000178
Chris Lattner757169b2009-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 Stump11289f42009-09-09 15:08:12 +0000182
Chris Lattner757169b2009-01-17 08:27:52 +0000183 // This lexer really is for _Pragma.
184 L->Is_PragmaLexer = true;
185 return L;
186}
187
Chris Lattner02b436a2007-10-17 20:41:00 +0000188
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000189/// Stringify - Convert the specified string into a C string, with surrounding
190/// ""'s, and with escaped \ and " characters.
Chris Lattnerecc39e92006-07-15 05:23:31 +0000191std::string Lexer::Stringify(const std::string &Str, bool Charify) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000192 std::string Result = Str;
Chris Lattnerecc39e92006-07-15 05:23:31 +0000193 char Quote = Charify ? '\'' : '"';
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000194 for (unsigned i = 0, e = Result.size(); i != e; ++i) {
Chris Lattnerecc39e92006-07-15 05:23:31 +0000195 if (Result[i] == '\\' || Result[i] == Quote) {
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000196 Result.insert(Result.begin()+i, '\\');
197 ++i; ++e;
198 }
199 }
Chris Lattnerecc39e92006-07-15 05:23:31 +0000200 return Result;
Chris Lattnere3e81ea2006-07-03 01:13:26 +0000201}
202
Chris Lattner4c4a2452007-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
Douglas Gregor562c1f92010-01-22 19:49:59 +0000214static bool isWhitespace(unsigned char c);
Chris Lattner22eb9722006-06-18 05:43:12 +0000215
Chris Lattner8e129c22007-10-17 21:18:47 +0000216/// MeasureTokenLength - Relex the token at the specified location and return
217/// its length in bytes in the input file. If the token needs cleaning (e.g.
218/// includes a trigraph or an escaped newline) then this count includes bytes
219/// that are part of that.
220unsigned Lexer::MeasureTokenLength(SourceLocation Loc,
Chris Lattner184e65d2009-04-14 23:22:57 +0000221 const SourceManager &SM,
222 const LangOptions &LangOpts) {
Chris Lattner8e129c22007-10-17 21:18:47 +0000223 // TODO: this could be special cased for common tokens like identifiers, ')',
224 // etc to make this faster, if it mattered. Just look at StrData[0] to handle
Mike Stump11289f42009-09-09 15:08:12 +0000225 // all obviously single-char tokens. This could use
Chris Lattner8e129c22007-10-17 21:18:47 +0000226 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or
227 // something.
Chris Lattner4fa23622009-01-26 00:43:02 +0000228
229 // If this comes from a macro expansion, we really do want the macro name, not
230 // the token this macro expanded to.
Chris Lattnerd3817212009-01-26 22:24:27 +0000231 Loc = SM.getInstantiationLoc(Loc);
232 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc);
Douglas Gregore0fbb832010-03-16 00:06:06 +0000233 bool Invalid = false;
Benjamin Kramereb92dc02010-03-16 14:14:31 +0000234 llvm::StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid);
Douglas Gregore0fbb832010-03-16 00:06:06 +0000235 if (Invalid)
Douglas Gregor802b7762010-03-15 22:54:52 +0000236 return 0;
Benjamin Kramereb92dc02010-03-16 14:14:31 +0000237
238 const char *StrData = Buffer.data()+LocInfo.second;
Chris Lattner5509d532009-01-17 08:30:10 +0000239
Douglas Gregor562c1f92010-01-22 19:49:59 +0000240 if (isWhitespace(StrData[0]))
241 return 0;
242
Chris Lattner8e129c22007-10-17 21:18:47 +0000243 // Create a lexer starting at the beginning of this token.
Benjamin Kramereb92dc02010-03-16 14:14:31 +0000244 Lexer TheLexer(Loc, LangOpts, Buffer.begin(), StrData, Buffer.end());
Chris Lattnera3d4f162009-10-14 15:04:18 +0000245 TheLexer.SetCommentRetentionState(true);
Chris Lattner8e129c22007-10-17 21:18:47 +0000246 Token TheTok;
Chris Lattner50c90502008-10-12 01:15:46 +0000247 TheLexer.LexFromRawLexer(TheTok);
Chris Lattner8e129c22007-10-17 21:18:47 +0000248 return TheTok.getLength();
249}
250
Chris Lattner22eb9722006-06-18 05:43:12 +0000251//===----------------------------------------------------------------------===//
252// Character information.
253//===----------------------------------------------------------------------===//
254
Chris Lattner22eb9722006-06-18 05:43:12 +0000255enum {
256 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0'
257 CHAR_VERT_WS = 0x02, // '\r', '\n'
258 CHAR_LETTER = 0x04, // a-z,A-Z
259 CHAR_NUMBER = 0x08, // 0-9
260 CHAR_UNDER = 0x10, // _
261 CHAR_PERIOD = 0x20 // .
262};
263
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000264// Statically initialize CharInfo table based on ASCII character set
265// Reference: FreeBSD 7.2 /usr/share/misc/ascii
Chris Lattner3dfff972009-12-17 05:29:40 +0000266static const unsigned char CharInfo[256] =
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000267{
268// 0 NUL 1 SOH 2 STX 3 ETX
269// 4 EOT 5 ENQ 6 ACK 7 BEL
270 0 , 0 , 0 , 0 ,
271 0 , 0 , 0 , 0 ,
272// 8 BS 9 HT 10 NL 11 VT
273//12 NP 13 CR 14 SO 15 SI
274 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS,
275 CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 ,
276//16 DLE 17 DC1 18 DC2 19 DC3
277//20 DC4 21 NAK 22 SYN 23 ETB
278 0 , 0 , 0 , 0 ,
279 0 , 0 , 0 , 0 ,
280//24 CAN 25 EM 26 SUB 27 ESC
281//28 FS 29 GS 30 RS 31 US
282 0 , 0 , 0 , 0 ,
283 0 , 0 , 0 , 0 ,
284//32 SP 33 ! 34 " 35 #
285//36 $ 37 % 38 & 39 '
286 CHAR_HORZ_WS, 0 , 0 , 0 ,
287 0 , 0 , 0 , 0 ,
288//40 ( 41 ) 42 * 43 +
289//44 , 45 - 46 . 47 /
290 0 , 0 , 0 , 0 ,
291 0 , 0 , CHAR_PERIOD , 0 ,
292//48 0 49 1 50 2 51 3
293//52 4 53 5 54 6 55 7
294 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
295 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER ,
296//56 8 57 9 58 : 59 ;
297//60 < 61 = 62 > 63 ?
298 CHAR_NUMBER , CHAR_NUMBER , 0 , 0 ,
299 0 , 0 , 0 , 0 ,
300//64 @ 65 A 66 B 67 C
301//68 D 69 E 70 F 71 G
302 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
303 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
304//72 H 73 I 74 J 75 K
305//76 L 77 M 78 N 79 O
306 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
307 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
308//80 P 81 Q 82 R 83 S
309//84 T 85 U 86 V 87 W
310 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
311 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
312//88 X 89 Y 90 Z 91 [
313//92 \ 93 ] 94 ^ 95 _
314 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
315 0 , 0 , 0 , CHAR_UNDER ,
316//96 ` 97 a 98 b 99 c
317//100 d 101 e 102 f 103 g
318 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
319 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
320//104 h 105 i 106 j 107 k
321//108 l 109 m 110 n 111 o
322 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
323 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
324//112 p 113 q 114 r 115 s
325//116 t 117 u 118 v 119 w
326 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
327 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER ,
328//120 x 121 y 122 z 123 {
329//124 | 125 } 126 ~ 127 DEL
330 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 0 ,
331 0 , 0 , 0 , 0
332};
333
Chris Lattner3dfff972009-12-17 05:29:40 +0000334static void InitCharacterInfo() {
Chris Lattner22eb9722006-06-18 05:43:12 +0000335 static bool isInited = false;
336 if (isInited) return;
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000337 // check the statically-initialized CharInfo table
338 assert(CHAR_HORZ_WS == CharInfo[(int)' ']);
339 assert(CHAR_HORZ_WS == CharInfo[(int)'\t']);
340 assert(CHAR_HORZ_WS == CharInfo[(int)'\f']);
341 assert(CHAR_HORZ_WS == CharInfo[(int)'\v']);
342 assert(CHAR_VERT_WS == CharInfo[(int)'\n']);
343 assert(CHAR_VERT_WS == CharInfo[(int)'\r']);
344 assert(CHAR_UNDER == CharInfo[(int)'_']);
345 assert(CHAR_PERIOD == CharInfo[(int)'.']);
346 for (unsigned i = 'a'; i <= 'z'; ++i) {
347 assert(CHAR_LETTER == CharInfo[i]);
348 assert(CHAR_LETTER == CharInfo[i+'A'-'a']);
349 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000350 for (unsigned i = '0'; i <= '9'; ++i)
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000351 assert(CHAR_NUMBER == CharInfo[i]);
Steve Naroff04bc0182009-12-08 16:38:12 +0000352
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000353 isInited = true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000354}
355
Chris Lattnerde50a0c2009-07-07 17:09:54 +0000356
Chris Lattner22eb9722006-06-18 05:43:12 +0000357/// isIdentifierBody - Return true if this is the body character of an
358/// identifier, which is [a-zA-Z0-9_].
359static inline bool isIdentifierBody(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000360 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000361}
362
363/// isHorizontalWhitespace - Return true if this character is horizontal
364/// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'.
365static inline bool isHorizontalWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000366 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000367}
368
369/// isWhitespace - Return true if this character is horizontal or vertical
370/// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false
371/// for '\0'.
372static inline bool isWhitespace(unsigned char c) {
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000373 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000374}
375
376/// isNumberBody - Return true if this is the body character of an
377/// preprocessing number, which is [a-zA-Z0-9_.].
378static inline bool isNumberBody(unsigned char c) {
Mike Stump11289f42009-09-09 15:08:12 +0000379 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ?
Hartmut Kaiserc8107e52007-10-18 12:47:01 +0000380 true : false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000381}
382
Chris Lattnerd01e2912006-06-18 16:22:51 +0000383
Chris Lattner22eb9722006-06-18 05:43:12 +0000384//===----------------------------------------------------------------------===//
385// Diagnostics forwarding code.
386//===----------------------------------------------------------------------===//
387
Chris Lattner619c1742007-07-22 18:38:25 +0000388/// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the
389/// lexer buffer was all instantiated at a single point, perform the mapping.
390/// This is currently only used for _Pragma implementation, so it is the slow
391/// path of the hot getSourceLocation method. Do not allow it to be inlined.
Benjamin Kramer5e738282009-11-14 16:36:57 +0000392static DISABLE_INLINE SourceLocation GetMappedTokenLoc(Preprocessor &PP,
393 SourceLocation FileLoc,
394 unsigned CharNo,
395 unsigned TokLen);
Chris Lattner619c1742007-07-22 18:38:25 +0000396static SourceLocation GetMappedTokenLoc(Preprocessor &PP,
397 SourceLocation FileLoc,
Chris Lattner4fa23622009-01-26 00:43:02 +0000398 unsigned CharNo, unsigned TokLen) {
Chris Lattner9dc9c202009-02-15 20:52:18 +0000399 assert(FileLoc.isMacroID() && "Must be an instantiation");
Mike Stump11289f42009-09-09 15:08:12 +0000400
Chris Lattner619c1742007-07-22 18:38:25 +0000401 // Otherwise, we're lexing "mapped tokens". This is used for things like
402 // _Pragma handling. Combine the instantiation location of FileLoc with the
Chris Lattner53e384f2009-01-16 07:00:02 +0000403 // spelling location.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000404 SourceManager &SM = PP.getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +0000405
Chris Lattner8a425862009-01-16 07:36:28 +0000406 // Create a new SLoc which is expanded from Instantiation(FileLoc) but whose
Chris Lattner53e384f2009-01-16 07:00:02 +0000407 // characters come from spelling(FileLoc)+Offset.
Chris Lattner9dc9c202009-02-15 20:52:18 +0000408 SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc);
Chris Lattner29a2a192009-01-19 06:46:35 +0000409 SpellingLoc = SpellingLoc.getFileLocWithOffset(CharNo);
Mike Stump11289f42009-09-09 15:08:12 +0000410
Chris Lattner9dc9c202009-02-15 20:52:18 +0000411 // Figure out the expansion loc range, which is the range covered by the
412 // original _Pragma(...) sequence.
413 std::pair<SourceLocation,SourceLocation> II =
414 SM.getImmediateInstantiationRange(FileLoc);
Mike Stump11289f42009-09-09 15:08:12 +0000415
Chris Lattner9dc9c202009-02-15 20:52:18 +0000416 return SM.createInstantiationLoc(SpellingLoc, II.first, II.second, TokLen);
Chris Lattner619c1742007-07-22 18:38:25 +0000417}
418
Chris Lattner22eb9722006-06-18 05:43:12 +0000419/// getSourceLocation - Return a source location identifier for the specified
420/// offset in the current file.
Chris Lattner4fa23622009-01-26 00:43:02 +0000421SourceLocation Lexer::getSourceLocation(const char *Loc,
422 unsigned TokLen) const {
Chris Lattner5d1c0272007-07-22 18:44:36 +0000423 assert(Loc >= BufferStart && Loc <= BufferEnd &&
Chris Lattner4cca5ba2006-07-02 20:05:54 +0000424 "Location out of range for this buffer!");
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000425
426 // In the normal case, we're just lexing from a simple file buffer, return
427 // the file id from FileLoc with the offset specified.
Chris Lattner5d1c0272007-07-22 18:44:36 +0000428 unsigned CharNo = Loc-BufferStart;
Chris Lattnerdc5c0552007-07-20 16:37:10 +0000429 if (FileLoc.isFileID())
Chris Lattner29a2a192009-01-19 06:46:35 +0000430 return FileLoc.getFileLocWithOffset(CharNo);
Mike Stump11289f42009-09-09 15:08:12 +0000431
Chris Lattnerd32480d2009-01-17 06:22:33 +0000432 // Otherwise, this is the _Pragma lexer case, which pretends that all of the
433 // tokens are lexed from where the _Pragma was defined.
Chris Lattner02b436a2007-10-17 20:41:00 +0000434 assert(PP && "This doesn't work on raw lexers");
Chris Lattner4fa23622009-01-26 00:43:02 +0000435 return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen);
Chris Lattner22eb9722006-06-18 05:43:12 +0000436}
437
Chris Lattner22eb9722006-06-18 05:43:12 +0000438/// Diag - Forwarding function for diagnostics. This translate a source
439/// position in the current buffer into a SourceLocation object for rendering.
Chris Lattner427c9c12008-11-22 00:59:29 +0000440DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const {
Chris Lattner907dfe92008-11-18 07:59:24 +0000441 return PP->Diag(getSourceLocation(Loc), DiagID);
Chris Lattner22eb9722006-06-18 05:43:12 +0000442}
443
444//===----------------------------------------------------------------------===//
445// Trigraph and Escaped Newline Handling Code.
446//===----------------------------------------------------------------------===//
447
448/// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair,
449/// return the decoded trigraph letter it corresponds to, or '\0' if nothing.
450static char GetTrigraphCharForLetter(char Letter) {
451 switch (Letter) {
452 default: return 0;
453 case '=': return '#';
454 case ')': return ']';
455 case '(': return '[';
456 case '!': return '|';
457 case '\'': return '^';
458 case '>': return '}';
459 case '/': return '\\';
460 case '<': return '{';
461 case '-': return '~';
462 }
463}
464
465/// DecodeTrigraphChar - If the specified character is a legal trigraph when
466/// prefixed with ??, emit a trigraph warning. If trigraphs are enabled,
467/// return the result character. Finally, emit a warning about trigraph use
468/// whether trigraphs are enabled or not.
469static char DecodeTrigraphChar(const char *CP, Lexer *L) {
470 char Res = GetTrigraphCharForLetter(*CP);
Chris Lattner907dfe92008-11-18 07:59:24 +0000471 if (!Res || !L) return Res;
Mike Stump11289f42009-09-09 15:08:12 +0000472
Chris Lattner907dfe92008-11-18 07:59:24 +0000473 if (!L->getFeatures().Trigraphs) {
Chris Lattner6d27a162008-11-22 02:02:22 +0000474 if (!L->isLexingRawMode())
475 L->Diag(CP-2, diag::trigraph_ignored);
Chris Lattner907dfe92008-11-18 07:59:24 +0000476 return 0;
Chris Lattner22eb9722006-06-18 05:43:12 +0000477 }
Mike Stump11289f42009-09-09 15:08:12 +0000478
Chris Lattner6d27a162008-11-22 02:02:22 +0000479 if (!L->isLexingRawMode())
480 L->Diag(CP-2, diag::trigraph_converted) << std::string()+Res;
Chris Lattner22eb9722006-06-18 05:43:12 +0000481 return Res;
482}
483
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000484/// getEscapedNewLineSize - Return the size of the specified escaped newline,
485/// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a
Mike Stump11289f42009-09-09 15:08:12 +0000486/// trigraph equivalent on entry to this function.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000487unsigned Lexer::getEscapedNewLineSize(const char *Ptr) {
488 unsigned Size = 0;
489 while (isWhitespace(Ptr[Size])) {
490 ++Size;
Mike Stump11289f42009-09-09 15:08:12 +0000491
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000492 if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r')
493 continue;
494
495 // If this is a \r\n or \n\r, skip the other half.
496 if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') &&
497 Ptr[Size-1] != Ptr[Size])
498 ++Size;
Mike Stump11289f42009-09-09 15:08:12 +0000499
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000500 return Size;
Mike Stump11289f42009-09-09 15:08:12 +0000501 }
502
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000503 // Not an escaped newline, must be a \t or something else.
504 return 0;
505}
506
Chris Lattner38b2cde2009-04-18 22:27:02 +0000507/// SkipEscapedNewLines - If P points to an escaped newline (or a series of
508/// them), skip over them and return the first non-escaped-newline found,
509/// otherwise return P.
510const char *Lexer::SkipEscapedNewLines(const char *P) {
511 while (1) {
512 const char *AfterEscape;
513 if (*P == '\\') {
514 AfterEscape = P+1;
515 } else if (*P == '?') {
516 // If not a trigraph for escape, bail out.
517 if (P[1] != '?' || P[2] != '/')
518 return P;
519 AfterEscape = P+3;
520 } else {
521 return P;
522 }
Mike Stump11289f42009-09-09 15:08:12 +0000523
Chris Lattner38b2cde2009-04-18 22:27:02 +0000524 unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape);
525 if (NewLineSize == 0) return P;
526 P = AfterEscape+NewLineSize;
527 }
528}
529
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000530
Chris Lattner22eb9722006-06-18 05:43:12 +0000531/// getCharAndSizeSlow - Peek a single 'character' from the specified buffer,
532/// get its size, and return it. This is tricky in several cases:
533/// 1. If currently at the start of a trigraph, we warn about the trigraph,
534/// then either return the trigraph (skipping 3 chars) or the '?',
535/// depending on whether trigraphs are enabled or not.
536/// 2. If this is an escaped newline (potentially with whitespace between
537/// the backslash and newline), implicitly skip the newline and return
538/// the char after it.
Chris Lattner505c5472006-07-03 00:55:48 +0000539/// 3. If this is a UCN, return it. FIXME: C++ UCN's?
Chris Lattner22eb9722006-06-18 05:43:12 +0000540///
541/// This handles the slow/uncommon case of the getCharAndSize method. Here we
542/// know that we can accumulate into Size, and that we have already incremented
543/// Ptr by Size bytes.
544///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000545/// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should
546/// be updated to match.
Chris Lattner22eb9722006-06-18 05:43:12 +0000547///
548char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size,
Chris Lattner146762e2007-07-20 16:59:19 +0000549 Token *Tok) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000550 // If we have a slash, look for an escaped newline.
551 if (Ptr[0] == '\\') {
552 ++Size;
553 ++Ptr;
554Slash:
555 // Common case, backslash-char where the char is not whitespace.
556 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump11289f42009-09-09 15:08:12 +0000557
Chris Lattnerc1835952009-06-23 05:15:06 +0000558 // See if we have optional whitespace characters between the slash and
559 // newline.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000560 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
561 // Remember that this token needs to be cleaned.
562 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000563
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000564 // Warn if there was whitespace between the backslash and newline.
Chris Lattnerc1835952009-06-23 05:15:06 +0000565 if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode())
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000566 Diag(Ptr, diag::backslash_newline_space);
Mike Stump11289f42009-09-09 15:08:12 +0000567
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000568 // Found backslash<whitespace><newline>. Parse the char after it.
569 Size += EscapedNewLineSize;
570 Ptr += EscapedNewLineSize;
571 // Use slow version to accumulate a correct size field.
572 return getCharAndSizeSlow(Ptr, Size, Tok);
573 }
Mike Stump11289f42009-09-09 15:08:12 +0000574
Chris Lattner22eb9722006-06-18 05:43:12 +0000575 // Otherwise, this is not an escaped newline, just return the slash.
576 return '\\';
577 }
Mike Stump11289f42009-09-09 15:08:12 +0000578
Chris Lattner22eb9722006-06-18 05:43:12 +0000579 // If this is a trigraph, process it.
580 if (Ptr[0] == '?' && Ptr[1] == '?') {
581 // If this is actually a legal trigraph (not something like "??x"), emit
582 // a trigraph warning. If so, and if trigraphs are enabled, return it.
583 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) {
584 // Remember that this token needs to be cleaned.
Chris Lattner146762e2007-07-20 16:59:19 +0000585 if (Tok) Tok->setFlag(Token::NeedsCleaning);
Chris Lattner22eb9722006-06-18 05:43:12 +0000586
587 Ptr += 3;
588 Size += 3;
589 if (C == '\\') goto Slash;
590 return C;
591 }
592 }
Mike Stump11289f42009-09-09 15:08:12 +0000593
Chris Lattner22eb9722006-06-18 05:43:12 +0000594 // If this is neither, return a single character.
595 ++Size;
596 return *Ptr;
597}
598
Chris Lattnerd01e2912006-06-18 16:22:51 +0000599
Chris Lattner22eb9722006-06-18 05:43:12 +0000600/// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the
601/// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size,
602/// and that we have already incremented Ptr by Size bytes.
603///
Chris Lattnerd01e2912006-06-18 16:22:51 +0000604/// NOTE: When this method is updated, getCharAndSizeSlow (above) should
605/// be updated to match.
606char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size,
Chris Lattner22eb9722006-06-18 05:43:12 +0000607 const LangOptions &Features) {
608 // If we have a slash, look for an escaped newline.
609 if (Ptr[0] == '\\') {
610 ++Size;
611 ++Ptr;
612Slash:
613 // Common case, backslash-char where the char is not whitespace.
614 if (!isWhitespace(Ptr[0])) return '\\';
Mike Stump11289f42009-09-09 15:08:12 +0000615
Chris Lattner22eb9722006-06-18 05:43:12 +0000616 // See if we have optional whitespace characters followed by a newline.
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000617 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) {
618 // Found backslash<whitespace><newline>. Parse the char after it.
619 Size += EscapedNewLineSize;
620 Ptr += EscapedNewLineSize;
Mike Stump11289f42009-09-09 15:08:12 +0000621
Chris Lattnerfbce7aa2009-04-18 22:05:41 +0000622 // Use slow version to accumulate a correct size field.
623 return getCharAndSizeSlowNoWarn(Ptr, Size, Features);
624 }
Mike Stump11289f42009-09-09 15:08:12 +0000625
Chris Lattner22eb9722006-06-18 05:43:12 +0000626 // Otherwise, this is not an escaped newline, just return the slash.
627 return '\\';
628 }
Mike Stump11289f42009-09-09 15:08:12 +0000629
Chris Lattner22eb9722006-06-18 05:43:12 +0000630 // If this is a trigraph, process it.
631 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') {
632 // If this is actually a legal trigraph (not something like "??x"), return
633 // it.
634 if (char C = GetTrigraphCharForLetter(Ptr[2])) {
635 Ptr += 3;
636 Size += 3;
637 if (C == '\\') goto Slash;
638 return C;
639 }
640 }
Mike Stump11289f42009-09-09 15:08:12 +0000641
Chris Lattner22eb9722006-06-18 05:43:12 +0000642 // If this is neither, return a single character.
643 ++Size;
644 return *Ptr;
645}
646
Chris Lattner22eb9722006-06-18 05:43:12 +0000647//===----------------------------------------------------------------------===//
648// Helper methods for lexing.
649//===----------------------------------------------------------------------===//
650
Chris Lattner146762e2007-07-20 16:59:19 +0000651void Lexer::LexIdentifier(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000652 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$]
653 unsigned Size;
654 unsigned char C = *CurPtr++;
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000655 while (isIdentifierBody(C))
Chris Lattner22eb9722006-06-18 05:43:12 +0000656 C = *CurPtr++;
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000657
Chris Lattner22eb9722006-06-18 05:43:12 +0000658 --CurPtr; // Back up over the skipped character.
659
660 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline
661 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN.
Chris Lattner505c5472006-07-03 00:55:48 +0000662 // FIXME: UCNs.
Chris Lattner21d9b9a2010-01-11 02:38:50 +0000663 //
664 // TODO: Could merge these checks into a CharInfo flag to make the comparison
665 // cheaper
Chris Lattner22eb9722006-06-18 05:43:12 +0000666 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) {
667FinishIdentifier:
Chris Lattnercefc7682006-07-08 08:28:12 +0000668 const char *IdStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000669 FormTokenWithChars(Result, CurPtr, tok::identifier);
Mike Stump11289f42009-09-09 15:08:12 +0000670
Chris Lattner0f1f5052006-07-20 04:16:23 +0000671 // If we are in raw mode, return this identifier raw. There is no need to
672 // look up identifier information or attempt to macro expand it.
673 if (LexingRawMode) return;
Mike Stump11289f42009-09-09 15:08:12 +0000674
Chris Lattnercefc7682006-07-08 08:28:12 +0000675 // Fill in Result.IdentifierInfo, looking up the identifier in the
676 // identifier table.
Chris Lattner8256b972009-01-21 07:45:14 +0000677 IdentifierInfo *II = PP->LookUpIdentifierInfo(Result, IdStart);
Mike Stump11289f42009-09-09 15:08:12 +0000678
Chris Lattner1f6c7fe2009-01-23 18:35:48 +0000679 // Change the kind of this identifier to the appropriate token kind, e.g.
680 // turning "for" into a keyword.
681 Result.setKind(II->getTokenID());
Mike Stump11289f42009-09-09 15:08:12 +0000682
Chris Lattnerc5a00062006-06-18 16:41:01 +0000683 // Finally, now that we know we have an identifier, pass this off to the
684 // preprocessor, which may macro expand it or something.
Chris Lattner8256b972009-01-21 07:45:14 +0000685 if (II->isHandleIdentifierCase())
Chris Lattnerad89ec02009-01-21 07:43:11 +0000686 PP->HandleIdentifier(Result);
687 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000688 }
Mike Stump11289f42009-09-09 15:08:12 +0000689
Chris Lattner22eb9722006-06-18 05:43:12 +0000690 // Otherwise, $,\,? in identifier found. Enter slower path.
Mike Stump11289f42009-09-09 15:08:12 +0000691
Chris Lattner22eb9722006-06-18 05:43:12 +0000692 C = getCharAndSize(CurPtr, Size);
693 while (1) {
694 if (C == '$') {
695 // If we hit a $ and they are not supported in identifiers, we are done.
696 if (!Features.DollarIdents) goto FinishIdentifier;
Mike Stump11289f42009-09-09 15:08:12 +0000697
Chris Lattner22eb9722006-06-18 05:43:12 +0000698 // Otherwise, emit a diagnostic and continue.
Chris Lattner6d27a162008-11-22 02:02:22 +0000699 if (!isLexingRawMode())
700 Diag(CurPtr, diag::ext_dollar_in_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +0000701 CurPtr = ConsumeChar(CurPtr, Size, Result);
702 C = getCharAndSize(CurPtr, Size);
703 continue;
Chris Lattner505c5472006-07-03 00:55:48 +0000704 } else if (!isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000705 // Found end of identifier.
706 goto FinishIdentifier;
707 }
708
709 // Otherwise, this character is good, consume it.
710 CurPtr = ConsumeChar(CurPtr, Size, Result);
711
712 C = getCharAndSize(CurPtr, Size);
Chris Lattner505c5472006-07-03 00:55:48 +0000713 while (isIdentifierBody(C)) { // FIXME: UCNs.
Chris Lattner22eb9722006-06-18 05:43:12 +0000714 CurPtr = ConsumeChar(CurPtr, Size, Result);
715 C = getCharAndSize(CurPtr, Size);
716 }
717 }
718}
719
720
Nate Begeman5eee9332008-04-14 02:26:39 +0000721/// LexNumericConstant - Lex the remainder of a integer or floating point
Chris Lattner22eb9722006-06-18 05:43:12 +0000722/// constant. From[-1] is the first character lexed. Return the end of the
723/// constant.
Chris Lattner146762e2007-07-20 16:59:19 +0000724void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000725 unsigned Size;
726 char C = getCharAndSize(CurPtr, Size);
727 char PrevCh = 0;
Chris Lattner505c5472006-07-03 00:55:48 +0000728 while (isNumberBody(C)) { // FIXME: UCNs?
Chris Lattner22eb9722006-06-18 05:43:12 +0000729 CurPtr = ConsumeChar(CurPtr, Size, Result);
730 PrevCh = C;
731 C = getCharAndSize(CurPtr, Size);
732 }
Mike Stump11289f42009-09-09 15:08:12 +0000733
Chris Lattner22eb9722006-06-18 05:43:12 +0000734 // If we fell out, check for a sign, due to 1e+12. If we have one, continue.
735 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e'))
736 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
737
738 // If we have a hex FP constant, continue.
Alexis Hunt91b78382010-01-10 23:37:56 +0000739 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p') &&
740 (!PP || !PP->getLangOptions().CPlusPlus0x))
Chris Lattner22eb9722006-06-18 05:43:12 +0000741 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result));
Mike Stump11289f42009-09-09 15:08:12 +0000742
Chris Lattnerd01e2912006-06-18 16:22:51 +0000743 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000744 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000745 FormTokenWithChars(Result, CurPtr, tok::numeric_constant);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000746 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000747}
748
749/// LexStringLiteral - Lex the remainder of a string literal, after having lexed
750/// either " or L".
Chris Lattner4d963442008-10-12 04:05:48 +0000751void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, bool Wide) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000752 const char *NulCharacter = 0; // Does this string contain the \0 character?
Mike Stump11289f42009-09-09 15:08:12 +0000753
Chris Lattner22eb9722006-06-18 05:43:12 +0000754 char C = getAndAdvanceChar(CurPtr, Result);
755 while (C != '"') {
756 // Skip escaped characters.
757 if (C == '\\') {
758 // Skip the escaped character.
759 C = getAndAdvanceChar(CurPtr, Result);
760 } else if (C == '\n' || C == '\r' || // Newline.
761 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerd14705b2009-03-18 21:10:12 +0000762 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +0000763 Diag(BufferPtr, diag::err_unterminated_string);
Chris Lattnerb11c3232008-10-12 04:51:35 +0000764 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000765 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000766 } else if (C == 0) {
767 NulCharacter = CurPtr-1;
768 }
769 C = getAndAdvanceChar(CurPtr, Result);
770 }
Mike Stump11289f42009-09-09 15:08:12 +0000771
Chris Lattner5a78a022006-07-20 06:02:19 +0000772 // If a nul character existed in the string, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +0000773 if (NulCharacter && !isLexingRawMode())
774 Diag(NulCharacter, diag::null_in_string);
Chris Lattner22eb9722006-06-18 05:43:12 +0000775
Chris Lattnerd01e2912006-06-18 16:22:51 +0000776 // Update the location of the token as well as the BufferPtr instance var.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000777 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000778 FormTokenWithChars(Result, CurPtr,
779 Wide ? tok::wide_string_literal : tok::string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000780 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000781}
782
783/// LexAngledStringLiteral - Lex the remainder of an angled string literal,
784/// after having lexed the '<' character. This is used for #include filenames.
Chris Lattner146762e2007-07-20 16:59:19 +0000785void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000786 const char *NulCharacter = 0; // Does this string contain the \0 character?
Chris Lattnerb40289b2009-04-17 23:56:52 +0000787 const char *AfterLessPos = CurPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +0000788 char C = getAndAdvanceChar(CurPtr, Result);
789 while (C != '>') {
790 // Skip escaped characters.
791 if (C == '\\') {
792 // Skip the escaped character.
793 C = getAndAdvanceChar(CurPtr, Result);
794 } else if (C == '\n' || C == '\r' || // Newline.
795 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerb40289b2009-04-17 23:56:52 +0000796 // If the filename is unterminated, then it must just be a lone <
797 // character. Return this as such.
798 FormTokenWithChars(Result, AfterLessPos, tok::less);
Chris Lattner5a78a022006-07-20 06:02:19 +0000799 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000800 } else if (C == 0) {
801 NulCharacter = CurPtr-1;
802 }
803 C = getAndAdvanceChar(CurPtr, Result);
804 }
Mike Stump11289f42009-09-09 15:08:12 +0000805
Chris Lattner5a78a022006-07-20 06:02:19 +0000806 // If a nul character existed in the string, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +0000807 if (NulCharacter && !isLexingRawMode())
808 Diag(NulCharacter, diag::null_in_string);
Mike Stump11289f42009-09-09 15:08:12 +0000809
Chris Lattnerd01e2912006-06-18 16:22:51 +0000810 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000811 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000812 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000813 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000814}
815
816
817/// LexCharConstant - Lex the remainder of a character constant, after having
818/// lexed either ' or L'.
Chris Lattner146762e2007-07-20 16:59:19 +0000819void Lexer::LexCharConstant(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000820 const char *NulCharacter = 0; // Does this character contain the \0 character?
821
822 // Handle the common case of 'x' and '\y' efficiently.
823 char C = getAndAdvanceChar(CurPtr, Result);
824 if (C == '\'') {
Chris Lattnerd14705b2009-03-18 21:10:12 +0000825 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +0000826 Diag(BufferPtr, diag::err_empty_character);
Chris Lattnerb11c3232008-10-12 04:51:35 +0000827 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000828 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000829 } else if (C == '\\') {
830 // Skip the escaped character.
831 // FIXME: UCN's.
832 C = getAndAdvanceChar(CurPtr, Result);
833 }
Mike Stump11289f42009-09-09 15:08:12 +0000834
Chris Lattner22eb9722006-06-18 05:43:12 +0000835 if (C && C != '\n' && C != '\r' && CurPtr[0] == '\'') {
836 ++CurPtr;
837 } else {
838 // Fall back on generic code for embedded nulls, newlines, wide chars.
839 do {
840 // Skip escaped characters.
841 if (C == '\\') {
842 // Skip the escaped character.
843 C = getAndAdvanceChar(CurPtr, Result);
844 } else if (C == '\n' || C == '\r' || // Newline.
845 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file.
Chris Lattnerd14705b2009-03-18 21:10:12 +0000846 if (!isLexingRawMode() && !Features.AsmPreprocessor)
Chris Lattner6d27a162008-11-22 02:02:22 +0000847 Diag(BufferPtr, diag::err_unterminated_char);
Chris Lattnerb11c3232008-10-12 04:51:35 +0000848 FormTokenWithChars(Result, CurPtr-1, tok::unknown);
Chris Lattner5a78a022006-07-20 06:02:19 +0000849 return;
Chris Lattner22eb9722006-06-18 05:43:12 +0000850 } else if (C == 0) {
851 NulCharacter = CurPtr-1;
852 }
853 C = getAndAdvanceChar(CurPtr, Result);
854 } while (C != '\'');
855 }
Mike Stump11289f42009-09-09 15:08:12 +0000856
Chris Lattner6d27a162008-11-22 02:02:22 +0000857 if (NulCharacter && !isLexingRawMode())
858 Diag(NulCharacter, diag::null_in_char);
Chris Lattner22eb9722006-06-18 05:43:12 +0000859
Chris Lattnerd01e2912006-06-18 16:22:51 +0000860 // Update the location of token as well as BufferPtr.
Chris Lattner5a7971e2009-01-26 19:29:26 +0000861 const char *TokStart = BufferPtr;
Chris Lattnerb11c3232008-10-12 04:51:35 +0000862 FormTokenWithChars(Result, CurPtr, tok::char_constant);
Chris Lattner5a7971e2009-01-26 19:29:26 +0000863 Result.setLiteralData(TokStart);
Chris Lattner22eb9722006-06-18 05:43:12 +0000864}
865
866/// SkipWhitespace - Efficiently skip over a series of whitespace characters.
867/// Update BufferPtr to point to the next non-whitespace character and return.
Chris Lattner4d963442008-10-12 04:05:48 +0000868///
869/// This method forms a token and returns true if KeepWhitespaceMode is enabled.
870///
871bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000872 // Whitespace - Skip it, then return the token after the whitespace.
873 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently.
874 while (1) {
875 // Skip horizontal whitespace very aggressively.
876 while (isHorizontalWhitespace(Char))
877 Char = *++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +0000878
Daniel Dunbar5c4cc092008-11-25 00:20:22 +0000879 // Otherwise if we have something other than whitespace, we're done.
Chris Lattner22eb9722006-06-18 05:43:12 +0000880 if (Char != '\n' && Char != '\r')
881 break;
Mike Stump11289f42009-09-09 15:08:12 +0000882
Chris Lattner22eb9722006-06-18 05:43:12 +0000883 if (ParsingPreprocessorDirective) {
884 // End of preprocessor directive line, let LexTokenInternal handle this.
885 BufferPtr = CurPtr;
Chris Lattner4d963442008-10-12 04:05:48 +0000886 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000887 }
Mike Stump11289f42009-09-09 15:08:12 +0000888
Chris Lattner22eb9722006-06-18 05:43:12 +0000889 // ok, but handle newline.
890 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +0000891 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +0000892 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +0000893 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000894 Char = *++CurPtr;
895 }
896
897 // If this isn't immediately after a newline, there is leading space.
898 char PrevChar = CurPtr[-1];
899 if (PrevChar != '\n' && PrevChar != '\r')
Chris Lattner146762e2007-07-20 16:59:19 +0000900 Result.setFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +0000901
Chris Lattner4d963442008-10-12 04:05:48 +0000902 // If the client wants us to return whitespace, return it now.
903 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +0000904 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4d963442008-10-12 04:05:48 +0000905 return true;
906 }
Mike Stump11289f42009-09-09 15:08:12 +0000907
Chris Lattner22eb9722006-06-18 05:43:12 +0000908 BufferPtr = CurPtr;
Chris Lattner4d963442008-10-12 04:05:48 +0000909 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000910}
911
912// SkipBCPLComment - We have just read the // characters from input. Skip until
913// we find the newline character thats terminate the comment. Then update
Chris Lattner87d02082010-01-18 22:35:47 +0000914/// BufferPtr and return.
915///
916/// If we're in KeepCommentMode or any CommentHandler has inserted
917/// some tokens, this will store the first token and return true.
Chris Lattner146762e2007-07-20 16:59:19 +0000918bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000919 // If BCPL comments aren't explicitly enabled for this language, emit an
920 // extension warning.
Chris Lattner6d27a162008-11-22 02:02:22 +0000921 if (!Features.BCPLComment && !isLexingRawMode()) {
Chris Lattnerd01e2912006-06-18 16:22:51 +0000922 Diag(BufferPtr, diag::ext_bcpl_comment);
Mike Stump11289f42009-09-09 15:08:12 +0000923
Chris Lattner22eb9722006-06-18 05:43:12 +0000924 // Mark them enabled so we only emit one warning for this translation
925 // unit.
926 Features.BCPLComment = true;
927 }
Mike Stump11289f42009-09-09 15:08:12 +0000928
Chris Lattner22eb9722006-06-18 05:43:12 +0000929 // Scan over the body of the comment. The common case, when scanning, is that
930 // the comment contains normal ascii characters with nothing interesting in
931 // them. As such, optimize for this case with the inner loop.
932 char C;
933 do {
934 C = *CurPtr;
Chris Lattner505c5472006-07-03 00:55:48 +0000935 // FIXME: Speedup BCPL comment lexing. Just scan for a \n or \r character.
936 // If we find a \n character, scan backwards, checking to see if it's an
937 // escaped newline, like we do for block comments.
Mike Stump11289f42009-09-09 15:08:12 +0000938
Chris Lattner22eb9722006-06-18 05:43:12 +0000939 // Skip over characters in the fast loop.
940 while (C != 0 && // Potentially EOF.
941 C != '\\' && // Potentially escaped newline.
942 C != '?' && // Potentially trigraph.
943 C != '\n' && C != '\r') // Newline or DOS-style newline.
944 C = *++CurPtr;
945
946 // If this is a newline, we're done.
947 if (C == '\n' || C == '\r')
948 break; // Found the newline? Break out!
Mike Stump11289f42009-09-09 15:08:12 +0000949
Chris Lattner22eb9722006-06-18 05:43:12 +0000950 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to
Chris Lattnere141a9e2008-12-12 07:34:39 +0000951 // properly decode the character. Read it in raw mode to avoid emitting
952 // diagnostics about things like trigraphs. If we see an escaped newline,
953 // we'll handle it below.
Chris Lattner22eb9722006-06-18 05:43:12 +0000954 const char *OldPtr = CurPtr;
Chris Lattnere141a9e2008-12-12 07:34:39 +0000955 bool OldRawMode = isLexingRawMode();
956 LexingRawMode = true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000957 C = getAndAdvanceChar(CurPtr, Result);
Chris Lattnere141a9e2008-12-12 07:34:39 +0000958 LexingRawMode = OldRawMode;
Chris Lattnerecdaf402009-04-05 00:26:41 +0000959
960 // If the char that we finally got was a \n, then we must have had something
961 // like \<newline><newline>. We don't want to have consumed the second
962 // newline, we want CurPtr, to end up pointing to it down below.
963 if (C == '\n' || C == '\r') {
964 --CurPtr;
965 C = 'x'; // doesn't matter what this is.
966 }
Mike Stump11289f42009-09-09 15:08:12 +0000967
Chris Lattner22eb9722006-06-18 05:43:12 +0000968 // If we read multiple characters, and one of those characters was a \r or
Chris Lattnerff591e22007-06-09 06:07:22 +0000969 // \n, then we had an escaped newline within the comment. Emit diagnostic
970 // unless the next line is also a // comment.
971 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +0000972 for (; OldPtr != CurPtr; ++OldPtr)
973 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') {
Chris Lattnerff591e22007-06-09 06:07:22 +0000974 // Okay, we found a // comment that ends in a newline, if the next
975 // line is also a // comment, but has spaces, don't emit a diagnostic.
976 if (isspace(C)) {
977 const char *ForwardPtr = CurPtr;
978 while (isspace(*ForwardPtr)) // Skip whitespace.
979 ++ForwardPtr;
980 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/')
981 break;
982 }
Mike Stump11289f42009-09-09 15:08:12 +0000983
Chris Lattner6d27a162008-11-22 02:02:22 +0000984 if (!isLexingRawMode())
985 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment);
Chris Lattnercb283342006-06-18 06:48:37 +0000986 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000987 }
988 }
Mike Stump11289f42009-09-09 15:08:12 +0000989
Chris Lattner457fc152006-07-29 06:30:25 +0000990 if (CurPtr == BufferEnd+1) { --CurPtr; break; }
Chris Lattner22eb9722006-06-18 05:43:12 +0000991 } while (C != '\n' && C != '\r');
992
Chris Lattner93ddf802010-02-03 21:06:21 +0000993 // Found but did not consume the newline. Notify comment handlers about the
994 // comment unless we're in a #if 0 block.
995 if (PP && !isLexingRawMode() &&
996 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
997 getSourceLocation(CurPtr)))) {
Chris Lattner87d02082010-01-18 22:35:47 +0000998 BufferPtr = CurPtr;
999 return true; // A token has to be returned.
1000 }
Mike Stump11289f42009-09-09 15:08:12 +00001001
Chris Lattner457fc152006-07-29 06:30:25 +00001002 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +00001003 if (inKeepCommentMode())
Chris Lattner457fc152006-07-29 06:30:25 +00001004 return SaveBCPLComment(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001005
1006 // If we are inside a preprocessor directive and we see the end of line,
1007 // return immediately, so that the lexer can return this as an EOM token.
Chris Lattner457fc152006-07-29 06:30:25 +00001008 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001009 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001010 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001011 }
Mike Stump11289f42009-09-09 15:08:12 +00001012
Chris Lattner22eb9722006-06-18 05:43:12 +00001013 // Otherwise, eat the \n character. We don't care if this is a \n\r or
Chris Lattner87e97ea2008-10-12 00:23:07 +00001014 // \r\n sequence. This is an efficiency hack (because we know the \n can't
Chris Lattner4d963442008-10-12 04:05:48 +00001015 // contribute to another token), it isn't needed for correctness. Note that
1016 // this is ok even in KeepWhitespaceMode, because we would have returned the
1017 /// comment above in that mode.
Chris Lattner22eb9722006-06-18 05:43:12 +00001018 ++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001019
Chris Lattner22eb9722006-06-18 05:43:12 +00001020 // The next returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001021 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001022 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001023 Result.clearFlag(Token::LeadingSpace);
Chris Lattner22eb9722006-06-18 05:43:12 +00001024 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001025 return false;
Chris Lattner457fc152006-07-29 06:30:25 +00001026}
Chris Lattner22eb9722006-06-18 05:43:12 +00001027
Chris Lattner457fc152006-07-29 06:30:25 +00001028/// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in
1029/// an appropriate way and return it.
Chris Lattner146762e2007-07-20 16:59:19 +00001030bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001031 // If we're not in a preprocessor directive, just return the // comment
1032 // directly.
1033 FormTokenWithChars(Result, CurPtr, tok::comment);
Mike Stump11289f42009-09-09 15:08:12 +00001034
Chris Lattnerb11c3232008-10-12 04:51:35 +00001035 if (!ParsingPreprocessorDirective)
1036 return true;
Mike Stump11289f42009-09-09 15:08:12 +00001037
Chris Lattnerb11c3232008-10-12 04:51:35 +00001038 // If this BCPL-style comment is in a macro definition, transmogrify it into
1039 // a C-style block comment.
Douglas Gregordc970f02010-03-16 22:30:13 +00001040 bool Invalid = false;
1041 std::string Spelling = PP->getSpelling(Result, &Invalid);
1042 if (Invalid)
1043 return true;
1044
Chris Lattnerb11c3232008-10-12 04:51:35 +00001045 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?");
1046 Spelling[1] = '*'; // Change prefix to "/*".
1047 Spelling += "*/"; // add suffix.
Mike Stump11289f42009-09-09 15:08:12 +00001048
Chris Lattnerb11c3232008-10-12 04:51:35 +00001049 Result.setKind(tok::comment);
Chris Lattner5a7971e2009-01-26 19:29:26 +00001050 PP->CreateString(&Spelling[0], Spelling.size(), Result,
1051 Result.getLocation());
Chris Lattnere01e7582008-10-12 04:15:42 +00001052 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001053}
1054
Chris Lattnercb283342006-06-18 06:48:37 +00001055/// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline
1056/// character (either \n or \r) is part of an escaped newline sequence. Issue a
Chris Lattner89770572008-12-12 07:14:34 +00001057/// diagnostic if so. We know that the newline is inside of a block comment.
Mike Stump11289f42009-09-09 15:08:12 +00001058static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr,
Chris Lattner1f583052006-06-18 06:53:56 +00001059 Lexer *L) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001060 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r');
Mike Stump11289f42009-09-09 15:08:12 +00001061
Chris Lattner22eb9722006-06-18 05:43:12 +00001062 // Back up off the newline.
1063 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001064
Chris Lattner22eb9722006-06-18 05:43:12 +00001065 // If this is a two-character newline sequence, skip the other character.
1066 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') {
1067 // \n\n or \r\r -> not escaped newline.
1068 if (CurPtr[0] == CurPtr[1])
1069 return false;
1070 // \n\r or \r\n -> skip the newline.
1071 --CurPtr;
1072 }
Mike Stump11289f42009-09-09 15:08:12 +00001073
Chris Lattner22eb9722006-06-18 05:43:12 +00001074 // If we have horizontal whitespace, skip over it. We allow whitespace
1075 // between the slash and newline.
1076 bool HasSpace = false;
1077 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) {
1078 --CurPtr;
1079 HasSpace = true;
1080 }
Mike Stump11289f42009-09-09 15:08:12 +00001081
Chris Lattner22eb9722006-06-18 05:43:12 +00001082 // If we have a slash, we know this is an escaped newline.
1083 if (*CurPtr == '\\') {
Chris Lattnercb283342006-06-18 06:48:37 +00001084 if (CurPtr[-1] != '*') return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001085 } else {
1086 // It isn't a slash, is it the ?? / trigraph?
Chris Lattnercb283342006-06-18 06:48:37 +00001087 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' ||
1088 CurPtr[-3] != '*')
Chris Lattner22eb9722006-06-18 05:43:12 +00001089 return false;
Mike Stump11289f42009-09-09 15:08:12 +00001090
Chris Lattnercb283342006-06-18 06:48:37 +00001091 // This is the trigraph ending the comment. Emit a stern warning!
Chris Lattner22eb9722006-06-18 05:43:12 +00001092 CurPtr -= 2;
1093
1094 // If no trigraphs are enabled, warn that we ignored this trigraph and
1095 // ignore this * character.
Chris Lattner1f583052006-06-18 06:53:56 +00001096 if (!L->getFeatures().Trigraphs) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001097 if (!L->isLexingRawMode())
1098 L->Diag(CurPtr, diag::trigraph_ignored_block_comment);
Chris Lattnercb283342006-06-18 06:48:37 +00001099 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001100 }
Chris Lattner6d27a162008-11-22 02:02:22 +00001101 if (!L->isLexingRawMode())
1102 L->Diag(CurPtr, diag::trigraph_ends_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001103 }
Mike Stump11289f42009-09-09 15:08:12 +00001104
Chris Lattner22eb9722006-06-18 05:43:12 +00001105 // Warn about having an escaped newline between the */ characters.
Chris Lattner6d27a162008-11-22 02:02:22 +00001106 if (!L->isLexingRawMode())
1107 L->Diag(CurPtr, diag::escaped_newline_block_comment_end);
Mike Stump11289f42009-09-09 15:08:12 +00001108
Chris Lattner22eb9722006-06-18 05:43:12 +00001109 // If there was space between the backslash and newline, warn about it.
Chris Lattner6d27a162008-11-22 02:02:22 +00001110 if (HasSpace && !L->isLexingRawMode())
1111 L->Diag(CurPtr, diag::backslash_newline_space);
Mike Stump11289f42009-09-09 15:08:12 +00001112
Chris Lattnercb283342006-06-18 06:48:37 +00001113 return true;
Chris Lattner22eb9722006-06-18 05:43:12 +00001114}
1115
Chris Lattneraded4a92006-10-27 04:42:31 +00001116#ifdef __SSE2__
1117#include <emmintrin.h>
Chris Lattner9f6604f2006-10-30 20:01:22 +00001118#elif __ALTIVEC__
1119#include <altivec.h>
1120#undef bool
Chris Lattneraded4a92006-10-27 04:42:31 +00001121#endif
1122
Chris Lattner22eb9722006-06-18 05:43:12 +00001123/// SkipBlockComment - We have just read the /* characters from input. Read
1124/// until we find the */ characters that terminate the comment. Note that we
1125/// don't bother decoding trigraphs or escaped newlines in block comments,
1126/// because they cannot cause the comment to end. The only thing that can
1127/// happen is the comment could end with an escaped newline between the */ end
1128/// of comment.
Chris Lattnere01e7582008-10-12 04:15:42 +00001129///
Chris Lattner87d02082010-01-18 22:35:47 +00001130/// If we're in KeepCommentMode or any CommentHandler has inserted
1131/// some tokens, this will store the first token and return true.
Chris Lattner146762e2007-07-20 16:59:19 +00001132bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001133 // Scan one character past where we should, looking for a '/' character. Once
1134 // we find it, check to see if it was preceeded by a *. This common
1135 // optimization helps people who like to put a lot of * characters in their
1136 // comments.
Chris Lattnerc850ad62007-07-21 23:43:37 +00001137
1138 // The first character we get with newlines and trigraphs skipped to handle
1139 // the degenerate /*/ case below correctly if the * has an escaped newline
1140 // after it.
1141 unsigned CharSize;
1142 unsigned char C = getCharAndSize(CurPtr, CharSize);
1143 CurPtr += CharSize;
Chris Lattner22eb9722006-06-18 05:43:12 +00001144 if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001145 if (!isLexingRawMode())
Chris Lattner7c2e9802008-10-12 01:31:51 +00001146 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner99e7d232008-10-12 04:19:49 +00001147 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001148
Chris Lattner99e7d232008-10-12 04:19:49 +00001149 // KeepWhitespaceMode should return this broken comment as a token. Since
1150 // it isn't a well formed comment, just return it as an 'unknown' token.
1151 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001152 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner99e7d232008-10-12 04:19:49 +00001153 return true;
1154 }
Mike Stump11289f42009-09-09 15:08:12 +00001155
Chris Lattner99e7d232008-10-12 04:19:49 +00001156 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001157 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001158 }
Mike Stump11289f42009-09-09 15:08:12 +00001159
Chris Lattnerc850ad62007-07-21 23:43:37 +00001160 // Check to see if the first character after the '/*' is another /. If so,
1161 // then this slash does not end the block comment, it is part of it.
1162 if (C == '/')
1163 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001164
Chris Lattner22eb9722006-06-18 05:43:12 +00001165 while (1) {
Chris Lattner6cc3e362006-10-27 04:12:35 +00001166 // Skip over all non-interesting characters until we find end of buffer or a
1167 // (probably ending) '/' character.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001168 if (CurPtr + 24 < BufferEnd) {
1169 // While not aligned to a 16-byte boundary.
1170 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0)
1171 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001172
Chris Lattner6cc3e362006-10-27 04:12:35 +00001173 if (C == '/') goto FoundSlash;
Chris Lattneraded4a92006-10-27 04:42:31 +00001174
1175#ifdef __SSE2__
1176 __m128i Slashes = _mm_set_epi8('/', '/', '/', '/', '/', '/', '/', '/',
1177 '/', '/', '/', '/', '/', '/', '/', '/');
1178 while (CurPtr+16 <= BufferEnd &&
1179 _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)) == 0)
1180 CurPtr += 16;
Chris Lattner9f6604f2006-10-30 20:01:22 +00001181#elif __ALTIVEC__
1182 __vector unsigned char Slashes = {
Mike Stump11289f42009-09-09 15:08:12 +00001183 '/', '/', '/', '/', '/', '/', '/', '/',
Chris Lattner9f6604f2006-10-30 20:01:22 +00001184 '/', '/', '/', '/', '/', '/', '/', '/'
1185 };
1186 while (CurPtr+16 <= BufferEnd &&
1187 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes))
1188 CurPtr += 16;
Mike Stump11289f42009-09-09 15:08:12 +00001189#else
Chris Lattneraded4a92006-10-27 04:42:31 +00001190 // Scan for '/' quickly. Many block comments are very large.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001191 while (CurPtr[0] != '/' &&
1192 CurPtr[1] != '/' &&
1193 CurPtr[2] != '/' &&
1194 CurPtr[3] != '/' &&
1195 CurPtr+4 < BufferEnd) {
1196 CurPtr += 4;
1197 }
Chris Lattneraded4a92006-10-27 04:42:31 +00001198#endif
Mike Stump11289f42009-09-09 15:08:12 +00001199
Chris Lattneraded4a92006-10-27 04:42:31 +00001200 // It has to be one of the bytes scanned, increment to it and read one.
Chris Lattner6cc3e362006-10-27 04:12:35 +00001201 C = *CurPtr++;
1202 }
Mike Stump11289f42009-09-09 15:08:12 +00001203
Chris Lattneraded4a92006-10-27 04:42:31 +00001204 // Loop to scan the remainder.
Chris Lattner22eb9722006-06-18 05:43:12 +00001205 while (C != '/' && C != '\0')
1206 C = *CurPtr++;
Mike Stump11289f42009-09-09 15:08:12 +00001207
Chris Lattner6cc3e362006-10-27 04:12:35 +00001208 FoundSlash:
Chris Lattner22eb9722006-06-18 05:43:12 +00001209 if (C == '/') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001210 if (CurPtr[-2] == '*') // We found the final */. We're done!
1211 break;
Mike Stump11289f42009-09-09 15:08:12 +00001212
Chris Lattner22eb9722006-06-18 05:43:12 +00001213 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) {
Chris Lattner1f583052006-06-18 06:53:56 +00001214 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001215 // We found the final */, though it had an escaped newline between the
1216 // * and /. We're done!
1217 break;
1218 }
1219 }
1220 if (CurPtr[0] == '*' && CurPtr[1] != '/') {
1221 // If this is a /* inside of the comment, emit a warning. Don't do this
1222 // if this is a /*/, which will end the comment. This misses cases with
1223 // embedded escaped newlines, but oh well.
Chris Lattner6d27a162008-11-22 02:02:22 +00001224 if (!isLexingRawMode())
1225 Diag(CurPtr-1, diag::warn_nested_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001226 }
1227 } else if (C == 0 && CurPtr == BufferEnd+1) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001228 if (!isLexingRawMode())
1229 Diag(BufferPtr, diag::err_unterminated_block_comment);
Chris Lattner22eb9722006-06-18 05:43:12 +00001230 // Note: the user probably forgot a */. We could continue immediately
1231 // after the /*, but this would involve lexing a lot of what really is the
1232 // comment, which surely would confuse the parser.
Chris Lattner99e7d232008-10-12 04:19:49 +00001233 --CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001234
Chris Lattner99e7d232008-10-12 04:19:49 +00001235 // KeepWhitespaceMode should return this broken comment as a token. Since
1236 // it isn't a well formed comment, just return it as an 'unknown' token.
1237 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001238 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner99e7d232008-10-12 04:19:49 +00001239 return true;
1240 }
Mike Stump11289f42009-09-09 15:08:12 +00001241
Chris Lattner99e7d232008-10-12 04:19:49 +00001242 BufferPtr = CurPtr;
Chris Lattnere01e7582008-10-12 04:15:42 +00001243 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001244 }
1245 C = *CurPtr++;
1246 }
Mike Stump11289f42009-09-09 15:08:12 +00001247
Chris Lattner93ddf802010-02-03 21:06:21 +00001248 // Notify comment handlers about the comment unless we're in a #if 0 block.
1249 if (PP && !isLexingRawMode() &&
1250 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr),
1251 getSourceLocation(CurPtr)))) {
Chris Lattner87d02082010-01-18 22:35:47 +00001252 BufferPtr = CurPtr;
1253 return true; // A token has to be returned.
1254 }
Douglas Gregorc6d5edd2009-07-02 17:08:52 +00001255
Chris Lattner457fc152006-07-29 06:30:25 +00001256 // If we are returning comments as tokens, return this comment as a token.
Chris Lattner8637abd2008-10-12 03:22:02 +00001257 if (inKeepCommentMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001258 FormTokenWithChars(Result, CurPtr, tok::comment);
Chris Lattnere01e7582008-10-12 04:15:42 +00001259 return true;
Chris Lattner457fc152006-07-29 06:30:25 +00001260 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001261
1262 // It is common for the tokens immediately after a /**/ comment to be
1263 // whitespace. Instead of going through the big switch, handle it
Chris Lattner4d963442008-10-12 04:05:48 +00001264 // efficiently now. This is safe even in KeepWhitespaceMode because we would
1265 // have already returned above with the comment as a token.
Chris Lattner22eb9722006-06-18 05:43:12 +00001266 if (isHorizontalWhitespace(*CurPtr)) {
Chris Lattner146762e2007-07-20 16:59:19 +00001267 Result.setFlag(Token::LeadingSpace);
Chris Lattner457fc152006-07-29 06:30:25 +00001268 SkipWhitespace(Result, CurPtr+1);
Chris Lattnere01e7582008-10-12 04:15:42 +00001269 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001270 }
1271
1272 // Otherwise, just return so that the next character will be lexed as a token.
1273 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001274 Result.setFlag(Token::LeadingSpace);
Chris Lattnere01e7582008-10-12 04:15:42 +00001275 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +00001276}
1277
1278//===----------------------------------------------------------------------===//
1279// Primary Lexing Entry Points
1280//===----------------------------------------------------------------------===//
1281
Chris Lattner22eb9722006-06-18 05:43:12 +00001282/// ReadToEndOfLine - Read the rest of the current preprocessor line as an
1283/// uninterpreted string. This switches the lexer out of directive mode.
1284std::string Lexer::ReadToEndOfLine() {
1285 assert(ParsingPreprocessorDirective && ParsingFilename == false &&
1286 "Must be in a preprocessing directive!");
1287 std::string Result;
Chris Lattner146762e2007-07-20 16:59:19 +00001288 Token Tmp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001289
1290 // CurPtr - Cache BufferPtr in an automatic variable.
1291 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001292 while (1) {
1293 char Char = getAndAdvanceChar(CurPtr, Tmp);
1294 switch (Char) {
1295 default:
1296 Result += Char;
1297 break;
1298 case 0: // Null.
1299 // Found end of file?
1300 if (CurPtr-1 != BufferEnd) {
1301 // Nope, normal character, continue.
1302 Result += Char;
1303 break;
1304 }
1305 // FALL THROUGH.
1306 case '\r':
1307 case '\n':
1308 // Okay, we found the end of the line. First, back up past the \0, \r, \n.
1309 assert(CurPtr[-1] == Char && "Trigraphs for newline?");
1310 BufferPtr = CurPtr-1;
Mike Stump11289f42009-09-09 15:08:12 +00001311
Chris Lattner22eb9722006-06-18 05:43:12 +00001312 // Next, lex the character, which should handle the EOM transition.
Chris Lattnercb283342006-06-18 06:48:37 +00001313 Lex(Tmp);
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001314 assert(Tmp.is(tok::eom) && "Unexpected token!");
Mike Stump11289f42009-09-09 15:08:12 +00001315
Chris Lattner22eb9722006-06-18 05:43:12 +00001316 // Finally, we're done, return the string we found.
1317 return Result;
1318 }
1319 }
1320}
1321
1322/// LexEndOfFile - CurPtr points to the end of this file. Handle this
1323/// condition, reporting diagnostics and handling other edge cases as required.
Chris Lattner2183a6e2006-07-18 06:36:12 +00001324/// This returns true if Result contains a token, false if PP.Lex should be
1325/// called again.
Chris Lattner146762e2007-07-20 16:59:19 +00001326bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001327 // If we hit the end of the file while parsing a preprocessor directive,
1328 // end the preprocessor directive first. The next token returned will
1329 // then be the end of file.
1330 if (ParsingPreprocessorDirective) {
1331 // Done parsing the "line".
1332 ParsingPreprocessorDirective = false;
Chris Lattnerd01e2912006-06-18 16:22:51 +00001333 // Update the location of token as well as BufferPtr.
Chris Lattnerb11c3232008-10-12 04:51:35 +00001334 FormTokenWithChars(Result, CurPtr, tok::eom);
Mike Stump11289f42009-09-09 15:08:12 +00001335
Chris Lattner457fc152006-07-29 06:30:25 +00001336 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001337 SetCommentRetentionState(PP->getCommentRetentionState());
Chris Lattner2183a6e2006-07-18 06:36:12 +00001338 return true; // Have a token.
Mike Stump11289f42009-09-09 15:08:12 +00001339 }
Douglas Gregor3545ff42009-09-21 16:56:56 +00001340
Chris Lattner30a2fa12006-07-19 06:31:49 +00001341 // If we are in raw mode, return this event as an EOF token. Let the caller
1342 // that put us in raw mode handle the event.
Chris Lattner6d27a162008-11-22 02:02:22 +00001343 if (isLexingRawMode()) {
Chris Lattner8c204872006-10-14 05:19:21 +00001344 Result.startToken();
Chris Lattner30a2fa12006-07-19 06:31:49 +00001345 BufferPtr = BufferEnd;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001346 FormTokenWithChars(Result, BufferEnd, tok::eof);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001347 return true;
Chris Lattnerd8aee0e2006-07-11 05:04:55 +00001348 }
Mike Stump11289f42009-09-09 15:08:12 +00001349
Douglas Gregor3545ff42009-09-21 16:56:56 +00001350 // Otherwise, check if we are code-completing, then issue diagnostics for
1351 // unterminated #if and missing newline.
Chris Lattner30a2fa12006-07-19 06:31:49 +00001352
Douglas Gregor53ad6b92009-12-02 06:49:09 +00001353 if (PP && PP->isCodeCompletionFile(FileLoc)) {
1354 // We're at the end of the file, but we've been asked to consider the
1355 // end of the file to be a code-completion token. Return the
1356 // code-completion token.
1357 Result.startToken();
1358 FormTokenWithChars(Result, CurPtr, tok::code_completion);
Douglas Gregor3545ff42009-09-21 16:56:56 +00001359
Douglas Gregor53ad6b92009-12-02 06:49:09 +00001360 // Only do the eof -> code_completion translation once.
1361 PP->SetCodeCompletionPoint(0, 0, 0);
1362 return true;
Douglas Gregor3545ff42009-09-21 16:56:56 +00001363 }
1364
Chris Lattner30a2fa12006-07-19 06:31:49 +00001365 // If we are in a #if directive, emit an error.
1366 while (!ConditionalStack.empty()) {
Chris Lattner014156e2008-11-22 06:22:39 +00001367 PP->Diag(ConditionalStack.back().IfLoc,
1368 diag::err_pp_unterminated_conditional);
Chris Lattner30a2fa12006-07-19 06:31:49 +00001369 ConditionalStack.pop_back();
1370 }
Mike Stump11289f42009-09-09 15:08:12 +00001371
Chris Lattner8f96d042008-04-12 05:54:25 +00001372 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue
1373 // a pedwarn.
1374 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r'))
Mike Stump0be88752009-04-02 02:29:42 +00001375 Diag(BufferEnd, diag::ext_no_newline_eof)
1376 << CodeModificationHint::CreateInsertion(getSourceLocation(BufferEnd),
1377 "\n");
Mike Stump11289f42009-09-09 15:08:12 +00001378
Chris Lattner22eb9722006-06-18 05:43:12 +00001379 BufferPtr = CurPtr;
Chris Lattner30a2fa12006-07-19 06:31:49 +00001380
1381 // Finally, let the preprocessor handle this.
Chris Lattner02b436a2007-10-17 20:41:00 +00001382 return PP->HandleEndOfFile(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001383}
1384
Chris Lattner678c8802006-07-11 05:46:12 +00001385/// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from
1386/// the specified lexer will return a tok::l_paren token, 0 if it is something
1387/// else and 2 if there are no more tokens in the buffer controlled by the
1388/// lexer.
1389unsigned Lexer::isNextPPTokenLParen() {
1390 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?");
Mike Stump11289f42009-09-09 15:08:12 +00001391
Chris Lattner678c8802006-07-11 05:46:12 +00001392 // Switch to 'skipping' mode. This will ensure that we can lex a token
1393 // without emitting diagnostics, disables macro expansion, and will cause EOF
1394 // to return an EOF token instead of popping the include stack.
1395 LexingRawMode = true;
Mike Stump11289f42009-09-09 15:08:12 +00001396
Chris Lattner678c8802006-07-11 05:46:12 +00001397 // Save state that can be changed while lexing so that we can restore it.
1398 const char *TmpBufferPtr = BufferPtr;
Chris Lattner40493eb2009-04-24 07:15:46 +00001399 bool inPPDirectiveMode = ParsingPreprocessorDirective;
Mike Stump11289f42009-09-09 15:08:12 +00001400
Chris Lattner146762e2007-07-20 16:59:19 +00001401 Token Tok;
Chris Lattner8c204872006-10-14 05:19:21 +00001402 Tok.startToken();
Chris Lattner678c8802006-07-11 05:46:12 +00001403 LexTokenInternal(Tok);
Mike Stump11289f42009-09-09 15:08:12 +00001404
Chris Lattner678c8802006-07-11 05:46:12 +00001405 // Restore state that may have changed.
1406 BufferPtr = TmpBufferPtr;
Chris Lattner40493eb2009-04-24 07:15:46 +00001407 ParsingPreprocessorDirective = inPPDirectiveMode;
Mike Stump11289f42009-09-09 15:08:12 +00001408
Chris Lattner678c8802006-07-11 05:46:12 +00001409 // Restore the lexer back to non-skipping mode.
1410 LexingRawMode = false;
Mike Stump11289f42009-09-09 15:08:12 +00001411
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001412 if (Tok.is(tok::eof))
Chris Lattner678c8802006-07-11 05:46:12 +00001413 return 2;
Chris Lattner98c1f7c2007-10-09 18:02:16 +00001414 return Tok.is(tok::l_paren);
Chris Lattner678c8802006-07-11 05:46:12 +00001415}
1416
Chris Lattner7c027ee2009-12-14 06:16:57 +00001417/// FindConflictEnd - Find the end of a version control conflict marker.
1418static const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd) {
1419 llvm::StringRef RestOfBuffer(CurPtr+7, BufferEnd-CurPtr-7);
1420 size_t Pos = RestOfBuffer.find(">>>>>>>");
1421 while (Pos != llvm::StringRef::npos) {
1422 // Must occur at start of line.
1423 if (RestOfBuffer[Pos-1] != '\r' &&
1424 RestOfBuffer[Pos-1] != '\n') {
1425 RestOfBuffer = RestOfBuffer.substr(Pos+7);
1426 continue;
1427 }
1428 return RestOfBuffer.data()+Pos;
1429 }
1430 return 0;
1431}
1432
1433/// IsStartOfConflictMarker - If the specified pointer is the start of a version
1434/// control conflict marker like '<<<<<<<', recognize it as such, emit an error
1435/// and recover nicely. This returns true if it is a conflict marker and false
1436/// if not.
1437bool Lexer::IsStartOfConflictMarker(const char *CurPtr) {
1438 // Only a conflict marker if it starts at the beginning of a line.
1439 if (CurPtr != BufferStart &&
1440 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1441 return false;
1442
1443 // Check to see if we have <<<<<<<.
1444 if (BufferEnd-CurPtr < 8 ||
1445 llvm::StringRef(CurPtr, 7) != "<<<<<<<")
1446 return false;
1447
1448 // If we have a situation where we don't care about conflict markers, ignore
1449 // it.
1450 if (IsInConflictMarker || isLexingRawMode())
1451 return false;
1452
1453 // Check to see if there is a >>>>>>> somewhere in the buffer at the start of
1454 // a line to terminate this conflict marker.
1455 if (FindConflictEnd(CurPtr+7, BufferEnd)) {
1456 // We found a match. We are really in a conflict marker.
1457 // Diagnose this, and ignore to the end of line.
1458 Diag(CurPtr, diag::err_conflict_marker);
1459 IsInConflictMarker = true;
1460
1461 // Skip ahead to the end of line. We know this exists because the
1462 // end-of-conflict marker starts with \r or \n.
1463 while (*CurPtr != '\r' && *CurPtr != '\n') {
1464 assert(CurPtr != BufferEnd && "Didn't find end of line");
1465 ++CurPtr;
1466 }
1467 BufferPtr = CurPtr;
1468 return true;
1469 }
1470
1471 // No end of conflict marker found.
1472 return false;
1473}
1474
1475
1476/// HandleEndOfConflictMarker - If this is a '=======' or '|||||||' or '>>>>>>>'
1477/// marker, then it is the end of a conflict marker. Handle it by ignoring up
1478/// until the end of the line. This returns true if it is a conflict marker and
1479/// false if not.
1480bool Lexer::HandleEndOfConflictMarker(const char *CurPtr) {
1481 // Only a conflict marker if it starts at the beginning of a line.
1482 if (CurPtr != BufferStart &&
1483 CurPtr[-1] != '\n' && CurPtr[-1] != '\r')
1484 return false;
1485
1486 // If we have a situation where we don't care about conflict markers, ignore
1487 // it.
1488 if (!IsInConflictMarker || isLexingRawMode())
1489 return false;
1490
1491 // Check to see if we have the marker (7 characters in a row).
1492 for (unsigned i = 1; i != 7; ++i)
1493 if (CurPtr[i] != CurPtr[0])
1494 return false;
1495
1496 // If we do have it, search for the end of the conflict marker. This could
1497 // fail if it got skipped with a '#if 0' or something. Note that CurPtr might
1498 // be the end of conflict marker.
1499 if (const char *End = FindConflictEnd(CurPtr, BufferEnd)) {
1500 CurPtr = End;
1501
1502 // Skip ahead to the end of line.
1503 while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n')
1504 ++CurPtr;
1505
1506 BufferPtr = CurPtr;
1507
1508 // No longer in the conflict marker.
1509 IsInConflictMarker = false;
1510 return true;
1511 }
1512
1513 return false;
1514}
1515
Chris Lattner22eb9722006-06-18 05:43:12 +00001516
1517/// LexTokenInternal - This implements a simple C family lexer. It is an
1518/// extremely performance critical piece of code. This assumes that the buffer
Chris Lattner5c349382009-07-07 05:05:42 +00001519/// has a null character at the end of the file. This returns a preprocessing
1520/// token, not a normal token, as such, it is an internal interface. It assumes
1521/// that the Flags of result have been cleared before calling this.
Chris Lattner146762e2007-07-20 16:59:19 +00001522void Lexer::LexTokenInternal(Token &Result) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001523LexNextToken:
1524 // New token, can't need cleaning yet.
Chris Lattner146762e2007-07-20 16:59:19 +00001525 Result.clearFlag(Token::NeedsCleaning);
Chris Lattner8c204872006-10-14 05:19:21 +00001526 Result.setIdentifierInfo(0);
Mike Stump11289f42009-09-09 15:08:12 +00001527
Chris Lattner22eb9722006-06-18 05:43:12 +00001528 // CurPtr - Cache BufferPtr in an automatic variable.
1529 const char *CurPtr = BufferPtr;
Chris Lattner22eb9722006-06-18 05:43:12 +00001530
Chris Lattnereb54b592006-07-10 06:34:27 +00001531 // Small amounts of horizontal whitespace is very common between tokens.
1532 if ((*CurPtr == ' ') || (*CurPtr == '\t')) {
1533 ++CurPtr;
1534 while ((*CurPtr == ' ') || (*CurPtr == '\t'))
1535 ++CurPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001536
Chris Lattner4d963442008-10-12 04:05:48 +00001537 // If we are keeping whitespace and other tokens, just return what we just
1538 // skipped. The next lexer invocation will return the token after the
1539 // whitespace.
1540 if (isKeepWhitespaceMode()) {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001541 FormTokenWithChars(Result, CurPtr, tok::unknown);
Chris Lattner4d963442008-10-12 04:05:48 +00001542 return;
1543 }
Mike Stump11289f42009-09-09 15:08:12 +00001544
Chris Lattnereb54b592006-07-10 06:34:27 +00001545 BufferPtr = CurPtr;
Chris Lattner146762e2007-07-20 16:59:19 +00001546 Result.setFlag(Token::LeadingSpace);
Chris Lattnereb54b592006-07-10 06:34:27 +00001547 }
Mike Stump11289f42009-09-09 15:08:12 +00001548
Chris Lattner22eb9722006-06-18 05:43:12 +00001549 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below.
Mike Stump11289f42009-09-09 15:08:12 +00001550
Chris Lattner22eb9722006-06-18 05:43:12 +00001551 // Read a character, advancing over it.
1552 char Char = getAndAdvanceChar(CurPtr, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001553 tok::TokenKind Kind;
Mike Stump11289f42009-09-09 15:08:12 +00001554
Chris Lattner22eb9722006-06-18 05:43:12 +00001555 switch (Char) {
1556 case 0: // Null.
1557 // Found end of file?
Chris Lattner2183a6e2006-07-18 06:36:12 +00001558 if (CurPtr-1 == BufferEnd) {
1559 // Read the PP instance variable into an automatic variable, because
1560 // LexEndOfFile will often delete 'this'.
Chris Lattner02b436a2007-10-17 20:41:00 +00001561 Preprocessor *PPCache = PP;
Chris Lattner2183a6e2006-07-18 06:36:12 +00001562 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1563 return; // Got a token to return.
Chris Lattner02b436a2007-10-17 20:41:00 +00001564 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1565 return PPCache->Lex(Result);
Chris Lattner2183a6e2006-07-18 06:36:12 +00001566 }
Mike Stump11289f42009-09-09 15:08:12 +00001567
Chris Lattner6d27a162008-11-22 02:02:22 +00001568 if (!isLexingRawMode())
1569 Diag(CurPtr-1, diag::null_in_file);
Chris Lattner146762e2007-07-20 16:59:19 +00001570 Result.setFlag(Token::LeadingSpace);
Chris Lattner4d963442008-10-12 04:05:48 +00001571 if (SkipWhitespace(Result, CurPtr))
1572 return; // KeepWhitespaceMode
Mike Stump11289f42009-09-09 15:08:12 +00001573
Chris Lattner22eb9722006-06-18 05:43:12 +00001574 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner3dfff972009-12-17 05:29:40 +00001575
1576 case 26: // DOS & CP/M EOF: "^Z".
1577 // If we're in Microsoft extensions mode, treat this as end of file.
1578 if (Features.Microsoft) {
1579 // Read the PP instance variable into an automatic variable, because
1580 // LexEndOfFile will often delete 'this'.
1581 Preprocessor *PPCache = PP;
1582 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file.
1583 return; // Got a token to return.
1584 assert(PPCache && "Raw buffer::LexEndOfFile should return a token");
1585 return PPCache->Lex(Result);
1586 }
1587 // If Microsoft extensions are disabled, this is just random garbage.
1588 Kind = tok::unknown;
1589 break;
1590
Chris Lattner22eb9722006-06-18 05:43:12 +00001591 case '\n':
1592 case '\r':
1593 // If we are inside a preprocessor directive and we see the end of line,
1594 // we know we are done with the directive, so return an EOM token.
1595 if (ParsingPreprocessorDirective) {
1596 // Done parsing the "line".
1597 ParsingPreprocessorDirective = false;
Mike Stump11289f42009-09-09 15:08:12 +00001598
Chris Lattner457fc152006-07-29 06:30:25 +00001599 // Restore comment saving mode, in case it was disabled for directive.
Chris Lattner097a8b82008-10-12 03:27:19 +00001600 SetCommentRetentionState(PP->getCommentRetentionState());
Mike Stump11289f42009-09-09 15:08:12 +00001601
Chris Lattner22eb9722006-06-18 05:43:12 +00001602 // Since we consumed a newline, we are back at the start of a line.
1603 IsAtStartOfLine = true;
Mike Stump11289f42009-09-09 15:08:12 +00001604
Chris Lattnerb11c3232008-10-12 04:51:35 +00001605 Kind = tok::eom;
Chris Lattner22eb9722006-06-18 05:43:12 +00001606 break;
1607 }
1608 // The returned token is at the start of the line.
Chris Lattner146762e2007-07-20 16:59:19 +00001609 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001610 // No leading whitespace seen so far.
Chris Lattner146762e2007-07-20 16:59:19 +00001611 Result.clearFlag(Token::LeadingSpace);
Mike Stump11289f42009-09-09 15:08:12 +00001612
Chris Lattner4d963442008-10-12 04:05:48 +00001613 if (SkipWhitespace(Result, CurPtr))
1614 return; // KeepWhitespaceMode
Chris Lattner22eb9722006-06-18 05:43:12 +00001615 goto LexNextToken; // GCC isn't tail call eliminating.
1616 case ' ':
1617 case '\t':
1618 case '\f':
1619 case '\v':
Chris Lattnerb9b85972007-07-22 06:29:05 +00001620 SkipHorizontalWhitespace:
Chris Lattner146762e2007-07-20 16:59:19 +00001621 Result.setFlag(Token::LeadingSpace);
Chris Lattner4d963442008-10-12 04:05:48 +00001622 if (SkipWhitespace(Result, CurPtr))
1623 return; // KeepWhitespaceMode
Chris Lattnerb9b85972007-07-22 06:29:05 +00001624
1625 SkipIgnoredUnits:
1626 CurPtr = BufferPtr;
Mike Stump11289f42009-09-09 15:08:12 +00001627
Chris Lattnerb9b85972007-07-22 06:29:05 +00001628 // If the next token is obviously a // or /* */ comment, skip it efficiently
1629 // too (without going through the big switch stmt).
Chris Lattner58827712009-01-16 22:39:25 +00001630 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() &&
1631 Features.BCPLComment) {
Chris Lattner87d02082010-01-18 22:35:47 +00001632 if (SkipBCPLComment(Result, CurPtr+2))
1633 return; // There is a token to return.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001634 goto SkipIgnoredUnits;
Chris Lattner8637abd2008-10-12 03:22:02 +00001635 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) {
Chris Lattner87d02082010-01-18 22:35:47 +00001636 if (SkipBlockComment(Result, CurPtr+2))
1637 return; // There is a token to return.
Chris Lattnerb9b85972007-07-22 06:29:05 +00001638 goto SkipIgnoredUnits;
1639 } else if (isHorizontalWhitespace(*CurPtr)) {
1640 goto SkipHorizontalWhitespace;
1641 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001642 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner3dfff972009-12-17 05:29:40 +00001643
Chris Lattner2b15cf72008-01-03 17:58:54 +00001644 // C99 6.4.4.1: Integer Constants.
1645 // C99 6.4.4.2: Floating Constants.
1646 case '0': case '1': case '2': case '3': case '4':
1647 case '5': case '6': case '7': case '8': case '9':
1648 // Notify MIOpt that we read a non-whitespace/non-comment token.
1649 MIOpt.ReadToken();
1650 return LexNumericConstant(Result, CurPtr);
Mike Stump11289f42009-09-09 15:08:12 +00001651
Chris Lattner2b15cf72008-01-03 17:58:54 +00001652 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz").
Chris Lattner371ac8a2006-07-04 07:11:10 +00001653 // Notify MIOpt that we read a non-whitespace/non-comment token.
1654 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001655 Char = getCharAndSize(CurPtr, SizeTmp);
1656
1657 // Wide string literal.
1658 if (Char == '"')
Chris Lattnerd3e98952006-10-06 05:22:26 +00001659 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result),
1660 true);
Chris Lattner22eb9722006-06-18 05:43:12 +00001661
1662 // Wide character constant.
1663 if (Char == '\'')
1664 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1665 // FALL THROUGH, treating L like the start of an identifier.
Mike Stump11289f42009-09-09 15:08:12 +00001666
Chris Lattner22eb9722006-06-18 05:43:12 +00001667 // C99 6.4.2: Identifiers.
1668 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G':
1669 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N':
1670 case 'O': case 'P': case 'Q': case 'R': case 'S': case 'T': case 'U':
1671 case 'V': case 'W': case 'X': case 'Y': case 'Z':
1672 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g':
1673 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n':
1674 case 'o': case 'p': case 'q': case 'r': case 's': case 't': case 'u':
1675 case 'v': case 'w': case 'x': case 'y': case 'z':
1676 case '_':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001677 // Notify MIOpt that we read a non-whitespace/non-comment token.
1678 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001679 return LexIdentifier(Result, CurPtr);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001680
1681 case '$': // $ in identifiers.
1682 if (Features.DollarIdents) {
Chris Lattner6d27a162008-11-22 02:02:22 +00001683 if (!isLexingRawMode())
1684 Diag(CurPtr-1, diag::ext_dollar_in_identifier);
Chris Lattner2b15cf72008-01-03 17:58:54 +00001685 // Notify MIOpt that we read a non-whitespace/non-comment token.
1686 MIOpt.ReadToken();
1687 return LexIdentifier(Result, CurPtr);
1688 }
Mike Stump11289f42009-09-09 15:08:12 +00001689
Chris Lattnerb11c3232008-10-12 04:51:35 +00001690 Kind = tok::unknown;
Chris Lattner2b15cf72008-01-03 17:58:54 +00001691 break;
Mike Stump11289f42009-09-09 15:08:12 +00001692
Chris Lattner22eb9722006-06-18 05:43:12 +00001693 // C99 6.4.4: Character Constants.
1694 case '\'':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001695 // Notify MIOpt that we read a non-whitespace/non-comment token.
1696 MIOpt.ReadToken();
Chris Lattner22eb9722006-06-18 05:43:12 +00001697 return LexCharConstant(Result, CurPtr);
1698
1699 // C99 6.4.5: String Literals.
1700 case '"':
Chris Lattner371ac8a2006-07-04 07:11:10 +00001701 // Notify MIOpt that we read a non-whitespace/non-comment token.
1702 MIOpt.ReadToken();
Chris Lattnerd3e98952006-10-06 05:22:26 +00001703 return LexStringLiteral(Result, CurPtr, false);
Chris Lattner22eb9722006-06-18 05:43:12 +00001704
1705 // C99 6.4.6: Punctuators.
1706 case '?':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001707 Kind = tok::question;
Chris Lattner22eb9722006-06-18 05:43:12 +00001708 break;
1709 case '[':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001710 Kind = tok::l_square;
Chris Lattner22eb9722006-06-18 05:43:12 +00001711 break;
1712 case ']':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001713 Kind = tok::r_square;
Chris Lattner22eb9722006-06-18 05:43:12 +00001714 break;
1715 case '(':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001716 Kind = tok::l_paren;
Chris Lattner22eb9722006-06-18 05:43:12 +00001717 break;
1718 case ')':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001719 Kind = tok::r_paren;
Chris Lattner22eb9722006-06-18 05:43:12 +00001720 break;
1721 case '{':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001722 Kind = tok::l_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001723 break;
1724 case '}':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001725 Kind = tok::r_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001726 break;
1727 case '.':
1728 Char = getCharAndSize(CurPtr, SizeTmp);
1729 if (Char >= '0' && Char <= '9') {
Chris Lattner371ac8a2006-07-04 07:11:10 +00001730 // Notify MIOpt that we read a non-whitespace/non-comment token.
1731 MIOpt.ReadToken();
1732
Chris Lattner22eb9722006-06-18 05:43:12 +00001733 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result));
1734 } else if (Features.CPlusPlus && Char == '*') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001735 Kind = tok::periodstar;
Chris Lattner22eb9722006-06-18 05:43:12 +00001736 CurPtr += SizeTmp;
1737 } else if (Char == '.' &&
1738 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001739 Kind = tok::ellipsis;
Chris Lattner22eb9722006-06-18 05:43:12 +00001740 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1741 SizeTmp2, Result);
1742 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001743 Kind = tok::period;
Chris Lattner22eb9722006-06-18 05:43:12 +00001744 }
1745 break;
1746 case '&':
1747 Char = getCharAndSize(CurPtr, SizeTmp);
1748 if (Char == '&') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001749 Kind = tok::ampamp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001750 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1751 } else if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001752 Kind = tok::ampequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001753 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1754 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001755 Kind = tok::amp;
Chris Lattner22eb9722006-06-18 05:43:12 +00001756 }
1757 break;
Mike Stump11289f42009-09-09 15:08:12 +00001758 case '*':
Chris Lattner22eb9722006-06-18 05:43:12 +00001759 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001760 Kind = tok::starequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001761 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1762 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001763 Kind = tok::star;
Chris Lattner22eb9722006-06-18 05:43:12 +00001764 }
1765 break;
1766 case '+':
1767 Char = getCharAndSize(CurPtr, SizeTmp);
1768 if (Char == '+') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001769 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001770 Kind = tok::plusplus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001771 } else if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001772 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001773 Kind = tok::plusequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001774 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001775 Kind = tok::plus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001776 }
1777 break;
1778 case '-':
1779 Char = getCharAndSize(CurPtr, SizeTmp);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001780 if (Char == '-') { // --
Chris Lattner22eb9722006-06-18 05:43:12 +00001781 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001782 Kind = tok::minusminus;
Mike Stump11289f42009-09-09 15:08:12 +00001783 } else if (Char == '>' && Features.CPlusPlus &&
Chris Lattnerb11c3232008-10-12 04:51:35 +00001784 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->*
Chris Lattner22eb9722006-06-18 05:43:12 +00001785 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1786 SizeTmp2, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001787 Kind = tok::arrowstar;
1788 } else if (Char == '>') { // ->
Chris Lattner22eb9722006-06-18 05:43:12 +00001789 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001790 Kind = tok::arrow;
1791 } else if (Char == '=') { // -=
Chris Lattner22eb9722006-06-18 05:43:12 +00001792 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001793 Kind = tok::minusequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001794 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001795 Kind = tok::minus;
Chris Lattner22eb9722006-06-18 05:43:12 +00001796 }
1797 break;
1798 case '~':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001799 Kind = tok::tilde;
Chris Lattner22eb9722006-06-18 05:43:12 +00001800 break;
1801 case '!':
1802 if (getCharAndSize(CurPtr, SizeTmp) == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001803 Kind = tok::exclaimequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001804 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1805 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001806 Kind = tok::exclaim;
Chris Lattner22eb9722006-06-18 05:43:12 +00001807 }
1808 break;
1809 case '/':
1810 // 6.4.9: Comments
1811 Char = getCharAndSize(CurPtr, SizeTmp);
1812 if (Char == '/') { // BCPL comment.
Chris Lattner58827712009-01-16 22:39:25 +00001813 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally
1814 // want to lex this as a comment. There is one problem with this though,
1815 // that in one particular corner case, this can change the behavior of the
1816 // resultant program. For example, In "foo //**/ bar", C89 would lex
1817 // this as "foo / bar" and langauges with BCPL comments would lex it as
1818 // "foo". Check to see if the character after the second slash is a '*'.
1819 // If so, we will lex that as a "/" instead of the start of a comment.
1820 if (Features.BCPLComment ||
1821 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') {
1822 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner87d02082010-01-18 22:35:47 +00001823 return; // There is a token to return.
Mike Stump11289f42009-09-09 15:08:12 +00001824
Chris Lattner58827712009-01-16 22:39:25 +00001825 // It is common for the tokens immediately after a // comment to be
1826 // whitespace (indentation for the next line). Instead of going through
1827 // the big switch, handle it efficiently now.
1828 goto SkipIgnoredUnits;
1829 }
1830 }
Mike Stump11289f42009-09-09 15:08:12 +00001831
Chris Lattner58827712009-01-16 22:39:25 +00001832 if (Char == '*') { // /**/ comment.
Chris Lattner457fc152006-07-29 06:30:25 +00001833 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result)))
Chris Lattner87d02082010-01-18 22:35:47 +00001834 return; // There is a token to return.
Chris Lattnere01e7582008-10-12 04:15:42 +00001835 goto LexNextToken; // GCC isn't tail call eliminating.
Chris Lattner58827712009-01-16 22:39:25 +00001836 }
Mike Stump11289f42009-09-09 15:08:12 +00001837
Chris Lattner58827712009-01-16 22:39:25 +00001838 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001839 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001840 Kind = tok::slashequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001841 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001842 Kind = tok::slash;
Chris Lattner22eb9722006-06-18 05:43:12 +00001843 }
1844 break;
1845 case '%':
1846 Char = getCharAndSize(CurPtr, SizeTmp);
1847 if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001848 Kind = tok::percentequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001849 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1850 } else if (Features.Digraphs && Char == '>') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001851 Kind = tok::r_brace; // '%>' -> '}'
Chris Lattner22eb9722006-06-18 05:43:12 +00001852 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1853 } else if (Features.Digraphs && Char == ':') {
1854 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001855 Char = getCharAndSize(CurPtr, SizeTmp);
1856 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001857 Kind = tok::hashhash; // '%:%:' -> '##'
Chris Lattner22eb9722006-06-18 05:43:12 +00001858 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1859 SizeTmp2, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00001860 } else if (Char == '@' && Features.Microsoft) { // %:@ -> #@ -> Charize
Chris Lattner2b271db2006-07-15 05:41:09 +00001861 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner6d27a162008-11-22 02:02:22 +00001862 if (!isLexingRawMode())
1863 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001864 Kind = tok::hashat;
Chris Lattner2534324a2009-03-18 20:58:27 +00001865 } else { // '%:' -> '#'
Chris Lattner22eb9722006-06-18 05:43:12 +00001866 // We parsed a # character. If this occurs at the start of the line,
1867 // it's actually the start of a preprocessing directive. Callback to
1868 // the preprocessor to handle it.
1869 // FIXME: -fpreprocessed mode??
Chris Lattnerff96dd02009-05-13 06:10:29 +00001870 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattner2534324a2009-03-18 20:58:27 +00001871 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner02b436a2007-10-17 20:41:00 +00001872 PP->HandleDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +00001873
Chris Lattner22eb9722006-06-18 05:43:12 +00001874 // As an optimization, if the preprocessor didn't switch lexers, tail
1875 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00001876 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00001877 // Start a new token. If this is a #include or something, the PP may
1878 // want us starting at the beginning of the line again. If so, set
1879 // the StartOfLine flag.
1880 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00001881 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00001882 IsAtStartOfLine = false;
1883 }
1884 goto LexNextToken; // GCC isn't tail call eliminating.
1885 }
Mike Stump11289f42009-09-09 15:08:12 +00001886
Chris Lattner02b436a2007-10-17 20:41:00 +00001887 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00001888 }
Mike Stump11289f42009-09-09 15:08:12 +00001889
Chris Lattner2534324a2009-03-18 20:58:27 +00001890 Kind = tok::hash;
Chris Lattner22eb9722006-06-18 05:43:12 +00001891 }
1892 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001893 Kind = tok::percent;
Chris Lattner22eb9722006-06-18 05:43:12 +00001894 }
1895 break;
1896 case '<':
1897 Char = getCharAndSize(CurPtr, SizeTmp);
1898 if (ParsingFilename) {
Chris Lattnerb40289b2009-04-17 23:56:52 +00001899 return LexAngledStringLiteral(Result, CurPtr);
Chris Lattner22eb9722006-06-18 05:43:12 +00001900 } else if (Char == '<') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001901 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
1902 if (After == '=') {
1903 Kind = tok::lesslessequal;
1904 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1905 SizeTmp2, Result);
1906 } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) {
1907 // If this is actually a '<<<<<<<' version control conflict marker,
1908 // recognize it as such and recover nicely.
1909 goto LexNextToken;
1910 } else {
1911 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1912 Kind = tok::lessless;
1913 }
Chris Lattner22eb9722006-06-18 05:43:12 +00001914 } else if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001915 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001916 Kind = tok::lessequal;
1917 } else if (Features.Digraphs && Char == ':') { // '<:' -> '['
Chris Lattner22eb9722006-06-18 05:43:12 +00001918 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001919 Kind = tok::l_square;
1920 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{'
Chris Lattner22eb9722006-06-18 05:43:12 +00001921 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001922 Kind = tok::l_brace;
Chris Lattner22eb9722006-06-18 05:43:12 +00001923 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001924 Kind = tok::less;
Chris Lattner22eb9722006-06-18 05:43:12 +00001925 }
1926 break;
1927 case '>':
1928 Char = getCharAndSize(CurPtr, SizeTmp);
1929 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001930 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001931 Kind = tok::greaterequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001932 } else if (Char == '>') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001933 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2);
1934 if (After == '=') {
1935 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result),
1936 SizeTmp2, Result);
1937 Kind = tok::greatergreaterequal;
1938 } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) {
1939 // If this is '>>>>>>>' and we're in a conflict marker, ignore it.
1940 goto LexNextToken;
1941 } else {
1942 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1943 Kind = tok::greatergreater;
1944 }
1945
Chris Lattner22eb9722006-06-18 05:43:12 +00001946 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001947 Kind = tok::greater;
Chris Lattner22eb9722006-06-18 05:43:12 +00001948 }
1949 break;
1950 case '^':
1951 Char = getCharAndSize(CurPtr, SizeTmp);
1952 if (Char == '=') {
Chris Lattner22eb9722006-06-18 05:43:12 +00001953 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattnerb11c3232008-10-12 04:51:35 +00001954 Kind = tok::caretequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001955 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001956 Kind = tok::caret;
Chris Lattner22eb9722006-06-18 05:43:12 +00001957 }
1958 break;
1959 case '|':
1960 Char = getCharAndSize(CurPtr, SizeTmp);
1961 if (Char == '=') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001962 Kind = tok::pipeequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001963 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1964 } else if (Char == '|') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001965 // If this is '|||||||' and we're in a conflict marker, ignore it.
1966 if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1))
1967 goto LexNextToken;
Chris Lattnerb11c3232008-10-12 04:51:35 +00001968 Kind = tok::pipepipe;
Chris Lattner22eb9722006-06-18 05:43:12 +00001969 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1970 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001971 Kind = tok::pipe;
Chris Lattner22eb9722006-06-18 05:43:12 +00001972 }
1973 break;
1974 case ':':
1975 Char = getCharAndSize(CurPtr, SizeTmp);
1976 if (Features.Digraphs && Char == '>') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001977 Kind = tok::r_square; // ':>' -> ']'
Chris Lattner22eb9722006-06-18 05:43:12 +00001978 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
1979 } else if (Features.CPlusPlus && Char == ':') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001980 Kind = tok::coloncolon;
Chris Lattner22eb9722006-06-18 05:43:12 +00001981 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump11289f42009-09-09 15:08:12 +00001982 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001983 Kind = tok::colon;
Chris Lattner22eb9722006-06-18 05:43:12 +00001984 }
1985 break;
1986 case ';':
Chris Lattnerb11c3232008-10-12 04:51:35 +00001987 Kind = tok::semi;
Chris Lattner22eb9722006-06-18 05:43:12 +00001988 break;
1989 case '=':
1990 Char = getCharAndSize(CurPtr, SizeTmp);
1991 if (Char == '=') {
Chris Lattner7c027ee2009-12-14 06:16:57 +00001992 // If this is '=======' and we're in a conflict marker, ignore it.
1993 if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1))
1994 goto LexNextToken;
1995
Chris Lattnerb11c3232008-10-12 04:51:35 +00001996 Kind = tok::equalequal;
Chris Lattner22eb9722006-06-18 05:43:12 +00001997 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Mike Stump11289f42009-09-09 15:08:12 +00001998 } else {
Chris Lattnerb11c3232008-10-12 04:51:35 +00001999 Kind = tok::equal;
Chris Lattner22eb9722006-06-18 05:43:12 +00002000 }
2001 break;
2002 case ',':
Chris Lattnerb11c3232008-10-12 04:51:35 +00002003 Kind = tok::comma;
Chris Lattner22eb9722006-06-18 05:43:12 +00002004 break;
2005 case '#':
2006 Char = getCharAndSize(CurPtr, SizeTmp);
2007 if (Char == '#') {
Chris Lattnerb11c3232008-10-12 04:51:35 +00002008 Kind = tok::hashhash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002009 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner2b271db2006-07-15 05:41:09 +00002010 } else if (Char == '@' && Features.Microsoft) { // #@ -> Charize
Chris Lattnerb11c3232008-10-12 04:51:35 +00002011 Kind = tok::hashat;
Chris Lattner6d27a162008-11-22 02:02:22 +00002012 if (!isLexingRawMode())
2013 Diag(BufferPtr, diag::charize_microsoft_ext);
Chris Lattner2b271db2006-07-15 05:41:09 +00002014 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002015 } else {
Chris Lattner22eb9722006-06-18 05:43:12 +00002016 // We parsed a # character. If this occurs at the start of the line,
2017 // it's actually the start of a preprocessing directive. Callback to
2018 // the preprocessor to handle it.
Chris Lattner505c5472006-07-03 00:55:48 +00002019 // FIXME: -fpreprocessed mode??
Chris Lattnerff96dd02009-05-13 06:10:29 +00002020 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) {
Chris Lattner2534324a2009-03-18 20:58:27 +00002021 FormTokenWithChars(Result, CurPtr, tok::hash);
Chris Lattner02b436a2007-10-17 20:41:00 +00002022 PP->HandleDirective(Result);
Mike Stump11289f42009-09-09 15:08:12 +00002023
Chris Lattner22eb9722006-06-18 05:43:12 +00002024 // As an optimization, if the preprocessor didn't switch lexers, tail
2025 // recurse.
Chris Lattner02b436a2007-10-17 20:41:00 +00002026 if (PP->isCurrentLexer(this)) {
Chris Lattner22eb9722006-06-18 05:43:12 +00002027 // Start a new token. If this is a #include or something, the PP may
2028 // want us starting at the beginning of the line again. If so, set
2029 // the StartOfLine flag.
2030 if (IsAtStartOfLine) {
Chris Lattner146762e2007-07-20 16:59:19 +00002031 Result.setFlag(Token::StartOfLine);
Chris Lattner22eb9722006-06-18 05:43:12 +00002032 IsAtStartOfLine = false;
2033 }
2034 goto LexNextToken; // GCC isn't tail call eliminating.
2035 }
Chris Lattner02b436a2007-10-17 20:41:00 +00002036 return PP->Lex(Result);
Chris Lattner22eb9722006-06-18 05:43:12 +00002037 }
Mike Stump11289f42009-09-09 15:08:12 +00002038
Chris Lattner2534324a2009-03-18 20:58:27 +00002039 Kind = tok::hash;
Chris Lattner22eb9722006-06-18 05:43:12 +00002040 }
2041 break;
2042
Chris Lattner2b15cf72008-01-03 17:58:54 +00002043 case '@':
2044 // Objective C support.
2045 if (CurPtr[-1] == '@' && Features.ObjC1)
Chris Lattnerb11c3232008-10-12 04:51:35 +00002046 Kind = tok::at;
Chris Lattner2b15cf72008-01-03 17:58:54 +00002047 else
Chris Lattnerb11c3232008-10-12 04:51:35 +00002048 Kind = tok::unknown;
Chris Lattner2b15cf72008-01-03 17:58:54 +00002049 break;
Mike Stump11289f42009-09-09 15:08:12 +00002050
Chris Lattner22eb9722006-06-18 05:43:12 +00002051 case '\\':
Chris Lattner505c5472006-07-03 00:55:48 +00002052 // FIXME: UCN's.
Chris Lattner22eb9722006-06-18 05:43:12 +00002053 // FALL THROUGH.
2054 default:
Chris Lattnerb11c3232008-10-12 04:51:35 +00002055 Kind = tok::unknown;
Chris Lattner041bef82006-07-11 05:52:53 +00002056 break;
Chris Lattner22eb9722006-06-18 05:43:12 +00002057 }
Mike Stump11289f42009-09-09 15:08:12 +00002058
Chris Lattner371ac8a2006-07-04 07:11:10 +00002059 // Notify MIOpt that we read a non-whitespace/non-comment token.
2060 MIOpt.ReadToken();
2061
Chris Lattnerd01e2912006-06-18 16:22:51 +00002062 // Update the location of token as well as BufferPtr.
Chris Lattnerb11c3232008-10-12 04:51:35 +00002063 FormTokenWithChars(Result, CurPtr, Kind);
Chris Lattner22eb9722006-06-18 05:43:12 +00002064}