1 //===--- Lexer.cpp - C Language Family Lexer ------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the Lexer and Token interfaces. 11 // 12 //===----------------------------------------------------------------------===// 13 // 14 // TODO: GCC Diagnostics emitted by the lexer: 15 // PEDWARN: (form feed|vertical tab) in preprocessing directive 16 // 17 // Universal characters, unicode, char mapping: 18 // WARNING: `%.*s' is not in NFKC 19 // WARNING: `%.*s' is not in NFC 20 // 21 // Other: 22 // TODO: Options to support: 23 // -fexec-charset,-fwide-exec-charset 24 // 25 //===----------------------------------------------------------------------===// 26 27 #include "clang/Lex/Lexer.h" 28 #include "clang/Lex/Preprocessor.h" 29 #include "clang/Lex/LexDiagnostic.h" 30 #include "clang/Lex/CodeCompletionHandler.h" 31 #include "clang/Basic/SourceManager.h" 32 #include "llvm/ADT/StringSwitch.h" 33 #include "llvm/ADT/STLExtras.h" 34 #include "llvm/Support/Compiler.h" 35 #include "llvm/Support/MemoryBuffer.h" 36 #include <cstring> 37 using namespace clang; 38 39 static void InitCharacterInfo(); 40 41 //===----------------------------------------------------------------------===// 42 // Token Class Implementation 43 //===----------------------------------------------------------------------===// 44 45 /// isObjCAtKeyword - Return true if we have an ObjC keyword identifier. 46 bool Token::isObjCAtKeyword(tok::ObjCKeywordKind objcKey) const { 47 if (IdentifierInfo *II = getIdentifierInfo()) 48 return II->getObjCKeywordID() == objcKey; 49 return false; 50 } 51 52 /// getObjCKeywordID - Return the ObjC keyword kind. 53 tok::ObjCKeywordKind Token::getObjCKeywordID() const { 54 IdentifierInfo *specId = getIdentifierInfo(); 55 return specId ? specId->getObjCKeywordID() : tok::objc_not_keyword; 56 } 57 58 59 //===----------------------------------------------------------------------===// 60 // Lexer Class Implementation 61 //===----------------------------------------------------------------------===// 62 63 void Lexer::anchor() { } 64 65 void Lexer::InitLexer(const char *BufStart, const char *BufPtr, 66 const char *BufEnd) { 67 InitCharacterInfo(); 68 69 BufferStart = BufStart; 70 BufferPtr = BufPtr; 71 BufferEnd = BufEnd; 72 73 assert(BufEnd[0] == 0 && 74 "We assume that the input buffer has a null character at the end" 75 " to simplify lexing!"); 76 77 // Check whether we have a BOM in the beginning of the buffer. If yes - act 78 // accordingly. Right now we support only UTF-8 with and without BOM, so, just 79 // skip the UTF-8 BOM if it's present. 80 if (BufferStart == BufferPtr) { 81 // Determine the size of the BOM. 82 StringRef Buf(BufferStart, BufferEnd - BufferStart); 83 size_t BOMLength = llvm::StringSwitch<size_t>(Buf) 84 .StartsWith("\xEF\xBB\xBF", 3) // UTF-8 BOM 85 .Default(0); 86 87 // Skip the BOM. 88 BufferPtr += BOMLength; 89 } 90 91 Is_PragmaLexer = false; 92 CurrentConflictMarkerState = CMK_None; 93 94 // Start of the file is a start of line. 95 IsAtStartOfLine = true; 96 97 // We are not after parsing a #. 98 ParsingPreprocessorDirective = false; 99 100 // We are not after parsing #include. 101 ParsingFilename = false; 102 103 // We are not in raw mode. Raw mode disables diagnostics and interpretation 104 // of tokens (e.g. identifiers, thus disabling macro expansion). It is used 105 // to quickly lex the tokens of the buffer, e.g. when handling a "#if 0" block 106 // or otherwise skipping over tokens. 107 LexingRawMode = false; 108 109 // Default to not keeping comments. 110 ExtendedTokenMode = 0; 111 } 112 113 /// Lexer constructor - Create a new lexer object for the specified buffer 114 /// with the specified preprocessor managing the lexing process. This lexer 115 /// assumes that the associated file buffer and Preprocessor objects will 116 /// outlive it, so it doesn't take ownership of either of them. 117 Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *InputFile, Preprocessor &PP) 118 : PreprocessorLexer(&PP, FID), 119 FileLoc(PP.getSourceManager().getLocForStartOfFile(FID)), 120 Features(PP.getLangOptions()) { 121 122 InitLexer(InputFile->getBufferStart(), InputFile->getBufferStart(), 123 InputFile->getBufferEnd()); 124 125 // Default to keeping comments if the preprocessor wants them. 126 SetCommentRetentionState(PP.getCommentRetentionState()); 127 } 128 129 /// Lexer constructor - Create a new raw lexer object. This object is only 130 /// suitable for calls to 'LexRawToken'. This lexer assumes that the text 131 /// range will outlive it, so it doesn't take ownership of it. 132 Lexer::Lexer(SourceLocation fileloc, const LangOptions &features, 133 const char *BufStart, const char *BufPtr, const char *BufEnd) 134 : FileLoc(fileloc), Features(features) { 135 136 InitLexer(BufStart, BufPtr, BufEnd); 137 138 // We *are* in raw mode. 139 LexingRawMode = true; 140 } 141 142 /// Lexer constructor - Create a new raw lexer object. This object is only 143 /// suitable for calls to 'LexRawToken'. This lexer assumes that the text 144 /// range will outlive it, so it doesn't take ownership of it. 145 Lexer::Lexer(FileID FID, const llvm::MemoryBuffer *FromFile, 146 const SourceManager &SM, const LangOptions &features) 147 : FileLoc(SM.getLocForStartOfFile(FID)), Features(features) { 148 149 InitLexer(FromFile->getBufferStart(), FromFile->getBufferStart(), 150 FromFile->getBufferEnd()); 151 152 // We *are* in raw mode. 153 LexingRawMode = true; 154 } 155 156 /// Create_PragmaLexer: Lexer constructor - Create a new lexer object for 157 /// _Pragma expansion. This has a variety of magic semantics that this method 158 /// sets up. It returns a new'd Lexer that must be delete'd when done. 159 /// 160 /// On entrance to this routine, TokStartLoc is a macro location which has a 161 /// spelling loc that indicates the bytes to be lexed for the token and an 162 /// expansion location that indicates where all lexed tokens should be 163 /// "expanded from". 164 /// 165 /// FIXME: It would really be nice to make _Pragma just be a wrapper around a 166 /// normal lexer that remaps tokens as they fly by. This would require making 167 /// Preprocessor::Lex virtual. Given that, we could just dump in a magic lexer 168 /// interface that could handle this stuff. This would pull GetMappedTokenLoc 169 /// out of the critical path of the lexer! 170 /// 171 Lexer *Lexer::Create_PragmaLexer(SourceLocation SpellingLoc, 172 SourceLocation ExpansionLocStart, 173 SourceLocation ExpansionLocEnd, 174 unsigned TokLen, Preprocessor &PP) { 175 SourceManager &SM = PP.getSourceManager(); 176 177 // Create the lexer as if we were going to lex the file normally. 178 FileID SpellingFID = SM.getFileID(SpellingLoc); 179 const llvm::MemoryBuffer *InputFile = SM.getBuffer(SpellingFID); 180 Lexer *L = new Lexer(SpellingFID, InputFile, PP); 181 182 // Now that the lexer is created, change the start/end locations so that we 183 // just lex the subsection of the file that we want. This is lexing from a 184 // scratch buffer. 185 const char *StrData = SM.getCharacterData(SpellingLoc); 186 187 L->BufferPtr = StrData; 188 L->BufferEnd = StrData+TokLen; 189 assert(L->BufferEnd[0] == 0 && "Buffer is not nul terminated!"); 190 191 // Set the SourceLocation with the remapping information. This ensures that 192 // GetMappedTokenLoc will remap the tokens as they are lexed. 193 L->FileLoc = SM.createExpansionLoc(SM.getLocForStartOfFile(SpellingFID), 194 ExpansionLocStart, 195 ExpansionLocEnd, TokLen); 196 197 // Ensure that the lexer thinks it is inside a directive, so that end \n will 198 // return an EOD token. 199 L->ParsingPreprocessorDirective = true; 200 201 // This lexer really is for _Pragma. 202 L->Is_PragmaLexer = true; 203 return L; 204 } 205 206 207 /// Stringify - Convert the specified string into a C string, with surrounding 208 /// ""'s, and with escaped \ and " characters. 209 std::string Lexer::Stringify(const std::string &Str, bool Charify) { 210 std::string Result = Str; 211 char Quote = Charify ? '\'' : '"'; 212 for (unsigned i = 0, e = Result.size(); i != e; ++i) { 213 if (Result[i] == '\\' || Result[i] == Quote) { 214 Result.insert(Result.begin()+i, '\\'); 215 ++i; ++e; 216 } 217 } 218 return Result; 219 } 220 221 /// Stringify - Convert the specified string into a C string by escaping '\' 222 /// and " characters. This does not add surrounding ""'s to the string. 223 void Lexer::Stringify(SmallVectorImpl<char> &Str) { 224 for (unsigned i = 0, e = Str.size(); i != e; ++i) { 225 if (Str[i] == '\\' || Str[i] == '"') { 226 Str.insert(Str.begin()+i, '\\'); 227 ++i; ++e; 228 } 229 } 230 } 231 232 //===----------------------------------------------------------------------===// 233 // Token Spelling 234 //===----------------------------------------------------------------------===// 235 236 /// getSpelling() - Return the 'spelling' of this token. The spelling of a 237 /// token are the characters used to represent the token in the source file 238 /// after trigraph expansion and escaped-newline folding. In particular, this 239 /// wants to get the true, uncanonicalized, spelling of things like digraphs 240 /// UCNs, etc. 241 StringRef Lexer::getSpelling(SourceLocation loc, 242 SmallVectorImpl<char> &buffer, 243 const SourceManager &SM, 244 const LangOptions &options, 245 bool *invalid) { 246 // Break down the source location. 247 std::pair<FileID, unsigned> locInfo = SM.getDecomposedLoc(loc); 248 249 // Try to the load the file buffer. 250 bool invalidTemp = false; 251 StringRef file = SM.getBufferData(locInfo.first, &invalidTemp); 252 if (invalidTemp) { 253 if (invalid) *invalid = true; 254 return StringRef(); 255 } 256 257 const char *tokenBegin = file.data() + locInfo.second; 258 259 // Lex from the start of the given location. 260 Lexer lexer(SM.getLocForStartOfFile(locInfo.first), options, 261 file.begin(), tokenBegin, file.end()); 262 Token token; 263 lexer.LexFromRawLexer(token); 264 265 unsigned length = token.getLength(); 266 267 // Common case: no need for cleaning. 268 if (!token.needsCleaning()) 269 return StringRef(tokenBegin, length); 270 271 // Hard case, we need to relex the characters into the string. 272 buffer.clear(); 273 buffer.reserve(length); 274 275 for (const char *ti = tokenBegin, *te = ti + length; ti != te; ) { 276 unsigned charSize; 277 buffer.push_back(Lexer::getCharAndSizeNoWarn(ti, charSize, options)); 278 ti += charSize; 279 } 280 281 return StringRef(buffer.data(), buffer.size()); 282 } 283 284 /// getSpelling() - Return the 'spelling' of this token. The spelling of a 285 /// token are the characters used to represent the token in the source file 286 /// after trigraph expansion and escaped-newline folding. In particular, this 287 /// wants to get the true, uncanonicalized, spelling of things like digraphs 288 /// UCNs, etc. 289 std::string Lexer::getSpelling(const Token &Tok, const SourceManager &SourceMgr, 290 const LangOptions &Features, bool *Invalid) { 291 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); 292 293 // If this token contains nothing interesting, return it directly. 294 bool CharDataInvalid = false; 295 const char* TokStart = SourceMgr.getCharacterData(Tok.getLocation(), 296 &CharDataInvalid); 297 if (Invalid) 298 *Invalid = CharDataInvalid; 299 if (CharDataInvalid) 300 return std::string(); 301 302 if (!Tok.needsCleaning()) 303 return std::string(TokStart, TokStart+Tok.getLength()); 304 305 std::string Result; 306 Result.reserve(Tok.getLength()); 307 308 // Otherwise, hard case, relex the characters into the string. 309 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); 310 Ptr != End; ) { 311 unsigned CharSize; 312 Result.push_back(Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features)); 313 Ptr += CharSize; 314 } 315 assert(Result.size() != unsigned(Tok.getLength()) && 316 "NeedsCleaning flag set on something that didn't need cleaning!"); 317 return Result; 318 } 319 320 /// getSpelling - This method is used to get the spelling of a token into a 321 /// preallocated buffer, instead of as an std::string. The caller is required 322 /// to allocate enough space for the token, which is guaranteed to be at least 323 /// Tok.getLength() bytes long. The actual length of the token is returned. 324 /// 325 /// Note that this method may do two possible things: it may either fill in 326 /// the buffer specified with characters, or it may *change the input pointer* 327 /// to point to a constant buffer with the data already in it (avoiding a 328 /// copy). The caller is not allowed to modify the returned buffer pointer 329 /// if an internal buffer is returned. 330 unsigned Lexer::getSpelling(const Token &Tok, const char *&Buffer, 331 const SourceManager &SourceMgr, 332 const LangOptions &Features, bool *Invalid) { 333 assert((int)Tok.getLength() >= 0 && "Token character range is bogus!"); 334 335 const char *TokStart = 0; 336 // NOTE: this has to be checked *before* testing for an IdentifierInfo. 337 if (Tok.is(tok::raw_identifier)) 338 TokStart = Tok.getRawIdentifierData(); 339 else if (const IdentifierInfo *II = Tok.getIdentifierInfo()) { 340 // Just return the string from the identifier table, which is very quick. 341 Buffer = II->getNameStart(); 342 return II->getLength(); 343 } 344 345 // NOTE: this can be checked even after testing for an IdentifierInfo. 346 if (Tok.isLiteral()) 347 TokStart = Tok.getLiteralData(); 348 349 if (TokStart == 0) { 350 // Compute the start of the token in the input lexer buffer. 351 bool CharDataInvalid = false; 352 TokStart = SourceMgr.getCharacterData(Tok.getLocation(), &CharDataInvalid); 353 if (Invalid) 354 *Invalid = CharDataInvalid; 355 if (CharDataInvalid) { 356 Buffer = ""; 357 return 0; 358 } 359 } 360 361 // If this token contains nothing interesting, return it directly. 362 if (!Tok.needsCleaning()) { 363 Buffer = TokStart; 364 return Tok.getLength(); 365 } 366 367 // Otherwise, hard case, relex the characters into the string. 368 char *OutBuf = const_cast<char*>(Buffer); 369 for (const char *Ptr = TokStart, *End = TokStart+Tok.getLength(); 370 Ptr != End; ) { 371 unsigned CharSize; 372 *OutBuf++ = Lexer::getCharAndSizeNoWarn(Ptr, CharSize, Features); 373 Ptr += CharSize; 374 } 375 assert(unsigned(OutBuf-Buffer) != Tok.getLength() && 376 "NeedsCleaning flag set on something that didn't need cleaning!"); 377 378 return OutBuf-Buffer; 379 } 380 381 382 383 static bool isWhitespace(unsigned char c); 384 385 /// MeasureTokenLength - Relex the token at the specified location and return 386 /// its length in bytes in the input file. If the token needs cleaning (e.g. 387 /// includes a trigraph or an escaped newline) then this count includes bytes 388 /// that are part of that. 389 unsigned Lexer::MeasureTokenLength(SourceLocation Loc, 390 const SourceManager &SM, 391 const LangOptions &LangOpts) { 392 // TODO: this could be special cased for common tokens like identifiers, ')', 393 // etc to make this faster, if it mattered. Just look at StrData[0] to handle 394 // all obviously single-char tokens. This could use 395 // Lexer::isObviouslySimpleCharacter for example to handle identifiers or 396 // something. 397 398 // If this comes from a macro expansion, we really do want the macro name, not 399 // the token this macro expanded to. 400 Loc = SM.getExpansionLoc(Loc); 401 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 402 bool Invalid = false; 403 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); 404 if (Invalid) 405 return 0; 406 407 const char *StrData = Buffer.data()+LocInfo.second; 408 409 if (isWhitespace(StrData[0])) 410 return 0; 411 412 // Create a lexer starting at the beginning of this token. 413 Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, 414 Buffer.begin(), StrData, Buffer.end()); 415 TheLexer.SetCommentRetentionState(true); 416 Token TheTok; 417 TheLexer.LexFromRawLexer(TheTok); 418 return TheTok.getLength(); 419 } 420 421 static SourceLocation getBeginningOfFileToken(SourceLocation Loc, 422 const SourceManager &SM, 423 const LangOptions &LangOpts) { 424 assert(Loc.isFileID()); 425 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 426 if (LocInfo.first.isInvalid()) 427 return Loc; 428 429 bool Invalid = false; 430 StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); 431 if (Invalid) 432 return Loc; 433 434 // Back up from the current location until we hit the beginning of a line 435 // (or the buffer). We'll relex from that point. 436 const char *BufStart = Buffer.data(); 437 if (LocInfo.second >= Buffer.size()) 438 return Loc; 439 440 const char *StrData = BufStart+LocInfo.second; 441 if (StrData[0] == '\n' || StrData[0] == '\r') 442 return Loc; 443 444 const char *LexStart = StrData; 445 while (LexStart != BufStart) { 446 if (LexStart[0] == '\n' || LexStart[0] == '\r') { 447 ++LexStart; 448 break; 449 } 450 451 --LexStart; 452 } 453 454 // Create a lexer starting at the beginning of this token. 455 SourceLocation LexerStartLoc = Loc.getLocWithOffset(-LocInfo.second); 456 Lexer TheLexer(LexerStartLoc, LangOpts, BufStart, LexStart, Buffer.end()); 457 TheLexer.SetCommentRetentionState(true); 458 459 // Lex tokens until we find the token that contains the source location. 460 Token TheTok; 461 do { 462 TheLexer.LexFromRawLexer(TheTok); 463 464 if (TheLexer.getBufferLocation() > StrData) { 465 // Lexing this token has taken the lexer past the source location we're 466 // looking for. If the current token encompasses our source location, 467 // return the beginning of that token. 468 if (TheLexer.getBufferLocation() - TheTok.getLength() <= StrData) 469 return TheTok.getLocation(); 470 471 // We ended up skipping over the source location entirely, which means 472 // that it points into whitespace. We're done here. 473 break; 474 } 475 } while (TheTok.getKind() != tok::eof); 476 477 // We've passed our source location; just return the original source location. 478 return Loc; 479 } 480 481 SourceLocation Lexer::GetBeginningOfToken(SourceLocation Loc, 482 const SourceManager &SM, 483 const LangOptions &LangOpts) { 484 if (Loc.isFileID()) 485 return getBeginningOfFileToken(Loc, SM, LangOpts); 486 487 if (!SM.isMacroArgExpansion(Loc)) 488 return Loc; 489 490 SourceLocation FileLoc = SM.getSpellingLoc(Loc); 491 SourceLocation BeginFileLoc = getBeginningOfFileToken(FileLoc, SM, LangOpts); 492 std::pair<FileID, unsigned> FileLocInfo = SM.getDecomposedLoc(FileLoc); 493 std::pair<FileID, unsigned> BeginFileLocInfo 494 = SM.getDecomposedLoc(BeginFileLoc); 495 assert(FileLocInfo.first == BeginFileLocInfo.first && 496 FileLocInfo.second >= BeginFileLocInfo.second); 497 return Loc.getLocWithOffset(BeginFileLocInfo.second - FileLocInfo.second); 498 } 499 500 namespace { 501 enum PreambleDirectiveKind { 502 PDK_Skipped, 503 PDK_StartIf, 504 PDK_EndIf, 505 PDK_Unknown 506 }; 507 } 508 509 std::pair<unsigned, bool> 510 Lexer::ComputePreamble(const llvm::MemoryBuffer *Buffer, 511 const LangOptions &Features, unsigned MaxLines) { 512 // Create a lexer starting at the beginning of the file. Note that we use a 513 // "fake" file source location at offset 1 so that the lexer will track our 514 // position within the file. 515 const unsigned StartOffset = 1; 516 SourceLocation StartLoc = SourceLocation::getFromRawEncoding(StartOffset); 517 Lexer TheLexer(StartLoc, Features, Buffer->getBufferStart(), 518 Buffer->getBufferStart(), Buffer->getBufferEnd()); 519 520 bool InPreprocessorDirective = false; 521 Token TheTok; 522 Token IfStartTok; 523 unsigned IfCount = 0; 524 525 unsigned MaxLineOffset = 0; 526 if (MaxLines) { 527 const char *CurPtr = Buffer->getBufferStart(); 528 unsigned CurLine = 0; 529 while (CurPtr != Buffer->getBufferEnd()) { 530 char ch = *CurPtr++; 531 if (ch == '\n') { 532 ++CurLine; 533 if (CurLine == MaxLines) 534 break; 535 } 536 } 537 if (CurPtr != Buffer->getBufferEnd()) 538 MaxLineOffset = CurPtr - Buffer->getBufferStart(); 539 } 540 541 do { 542 TheLexer.LexFromRawLexer(TheTok); 543 544 if (InPreprocessorDirective) { 545 // If we've hit the end of the file, we're done. 546 if (TheTok.getKind() == tok::eof) { 547 InPreprocessorDirective = false; 548 break; 549 } 550 551 // If we haven't hit the end of the preprocessor directive, skip this 552 // token. 553 if (!TheTok.isAtStartOfLine()) 554 continue; 555 556 // We've passed the end of the preprocessor directive, and will look 557 // at this token again below. 558 InPreprocessorDirective = false; 559 } 560 561 // Keep track of the # of lines in the preamble. 562 if (TheTok.isAtStartOfLine()) { 563 unsigned TokOffset = TheTok.getLocation().getRawEncoding() - StartOffset; 564 565 // If we were asked to limit the number of lines in the preamble, 566 // and we're about to exceed that limit, we're done. 567 if (MaxLineOffset && TokOffset >= MaxLineOffset) 568 break; 569 } 570 571 // Comments are okay; skip over them. 572 if (TheTok.getKind() == tok::comment) 573 continue; 574 575 if (TheTok.isAtStartOfLine() && TheTok.getKind() == tok::hash) { 576 // This is the start of a preprocessor directive. 577 Token HashTok = TheTok; 578 InPreprocessorDirective = true; 579 580 // Figure out which directive this is. Since we're lexing raw tokens, 581 // we don't have an identifier table available. Instead, just look at 582 // the raw identifier to recognize and categorize preprocessor directives. 583 TheLexer.LexFromRawLexer(TheTok); 584 if (TheTok.getKind() == tok::raw_identifier && !TheTok.needsCleaning()) { 585 StringRef Keyword(TheTok.getRawIdentifierData(), 586 TheTok.getLength()); 587 PreambleDirectiveKind PDK 588 = llvm::StringSwitch<PreambleDirectiveKind>(Keyword) 589 .Case("include", PDK_Skipped) 590 .Case("__include_macros", PDK_Skipped) 591 .Case("define", PDK_Skipped) 592 .Case("undef", PDK_Skipped) 593 .Case("line", PDK_Skipped) 594 .Case("error", PDK_Skipped) 595 .Case("pragma", PDK_Skipped) 596 .Case("import", PDK_Skipped) 597 .Case("include_next", PDK_Skipped) 598 .Case("warning", PDK_Skipped) 599 .Case("ident", PDK_Skipped) 600 .Case("sccs", PDK_Skipped) 601 .Case("assert", PDK_Skipped) 602 .Case("unassert", PDK_Skipped) 603 .Case("if", PDK_StartIf) 604 .Case("ifdef", PDK_StartIf) 605 .Case("ifndef", PDK_StartIf) 606 .Case("elif", PDK_Skipped) 607 .Case("else", PDK_Skipped) 608 .Case("endif", PDK_EndIf) 609 .Default(PDK_Unknown); 610 611 switch (PDK) { 612 case PDK_Skipped: 613 continue; 614 615 case PDK_StartIf: 616 if (IfCount == 0) 617 IfStartTok = HashTok; 618 619 ++IfCount; 620 continue; 621 622 case PDK_EndIf: 623 // Mismatched #endif. The preamble ends here. 624 if (IfCount == 0) 625 break; 626 627 --IfCount; 628 continue; 629 630 case PDK_Unknown: 631 // We don't know what this directive is; stop at the '#'. 632 break; 633 } 634 } 635 636 // We only end up here if we didn't recognize the preprocessor 637 // directive or it was one that can't occur in the preamble at this 638 // point. Roll back the current token to the location of the '#'. 639 InPreprocessorDirective = false; 640 TheTok = HashTok; 641 } 642 643 // We hit a token that we don't recognize as being in the 644 // "preprocessing only" part of the file, so we're no longer in 645 // the preamble. 646 break; 647 } while (true); 648 649 SourceLocation End = IfCount? IfStartTok.getLocation() : TheTok.getLocation(); 650 return std::make_pair(End.getRawEncoding() - StartLoc.getRawEncoding(), 651 IfCount? IfStartTok.isAtStartOfLine() 652 : TheTok.isAtStartOfLine()); 653 } 654 655 656 /// AdvanceToTokenCharacter - Given a location that specifies the start of a 657 /// token, return a new location that specifies a character within the token. 658 SourceLocation Lexer::AdvanceToTokenCharacter(SourceLocation TokStart, 659 unsigned CharNo, 660 const SourceManager &SM, 661 const LangOptions &Features) { 662 // Figure out how many physical characters away the specified expansion 663 // character is. This needs to take into consideration newlines and 664 // trigraphs. 665 bool Invalid = false; 666 const char *TokPtr = SM.getCharacterData(TokStart, &Invalid); 667 668 // If they request the first char of the token, we're trivially done. 669 if (Invalid || (CharNo == 0 && Lexer::isObviouslySimpleCharacter(*TokPtr))) 670 return TokStart; 671 672 unsigned PhysOffset = 0; 673 674 // The usual case is that tokens don't contain anything interesting. Skip 675 // over the uninteresting characters. If a token only consists of simple 676 // chars, this method is extremely fast. 677 while (Lexer::isObviouslySimpleCharacter(*TokPtr)) { 678 if (CharNo == 0) 679 return TokStart.getLocWithOffset(PhysOffset); 680 ++TokPtr, --CharNo, ++PhysOffset; 681 } 682 683 // If we have a character that may be a trigraph or escaped newline, use a 684 // lexer to parse it correctly. 685 for (; CharNo; --CharNo) { 686 unsigned Size; 687 Lexer::getCharAndSizeNoWarn(TokPtr, Size, Features); 688 TokPtr += Size; 689 PhysOffset += Size; 690 } 691 692 // Final detail: if we end up on an escaped newline, we want to return the 693 // location of the actual byte of the token. For example foo\<newline>bar 694 // advanced by 3 should return the location of b, not of \\. One compounding 695 // detail of this is that the escape may be made by a trigraph. 696 if (!Lexer::isObviouslySimpleCharacter(*TokPtr)) 697 PhysOffset += Lexer::SkipEscapedNewLines(TokPtr)-TokPtr; 698 699 return TokStart.getLocWithOffset(PhysOffset); 700 } 701 702 /// \brief Computes the source location just past the end of the 703 /// token at this source location. 704 /// 705 /// This routine can be used to produce a source location that 706 /// points just past the end of the token referenced by \p Loc, and 707 /// is generally used when a diagnostic needs to point just after a 708 /// token where it expected something different that it received. If 709 /// the returned source location would not be meaningful (e.g., if 710 /// it points into a macro), this routine returns an invalid 711 /// source location. 712 /// 713 /// \param Offset an offset from the end of the token, where the source 714 /// location should refer to. The default offset (0) produces a source 715 /// location pointing just past the end of the token; an offset of 1 produces 716 /// a source location pointing to the last character in the token, etc. 717 SourceLocation Lexer::getLocForEndOfToken(SourceLocation Loc, unsigned Offset, 718 const SourceManager &SM, 719 const LangOptions &Features) { 720 if (Loc.isInvalid()) 721 return SourceLocation(); 722 723 if (Loc.isMacroID()) { 724 if (Offset > 0 || !isAtEndOfMacroExpansion(Loc, SM, Features, &Loc)) 725 return SourceLocation(); // Points inside the macro expansion. 726 } 727 728 unsigned Len = Lexer::MeasureTokenLength(Loc, SM, Features); 729 if (Len > Offset) 730 Len = Len - Offset; 731 else 732 return Loc; 733 734 return Loc.getLocWithOffset(Len); 735 } 736 737 /// \brief Returns true if the given MacroID location points at the first 738 /// token of the macro expansion. 739 bool Lexer::isAtStartOfMacroExpansion(SourceLocation loc, 740 const SourceManager &SM, 741 const LangOptions &LangOpts, 742 SourceLocation *MacroBegin) { 743 assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); 744 745 std::pair<FileID, unsigned> infoLoc = SM.getDecomposedLoc(loc); 746 // FIXME: If the token comes from the macro token paste operator ('##') 747 // this function will always return false; 748 if (infoLoc.second > 0) 749 return false; // Does not point at the start of token. 750 751 SourceLocation expansionLoc = 752 SM.getSLocEntry(infoLoc.first).getExpansion().getExpansionLocStart(); 753 if (expansionLoc.isFileID()) { 754 // No other macro expansions, this is the first. 755 if (MacroBegin) 756 *MacroBegin = expansionLoc; 757 return true; 758 } 759 760 return isAtStartOfMacroExpansion(expansionLoc, SM, LangOpts, MacroBegin); 761 } 762 763 /// \brief Returns true if the given MacroID location points at the last 764 /// token of the macro expansion. 765 bool Lexer::isAtEndOfMacroExpansion(SourceLocation loc, 766 const SourceManager &SM, 767 const LangOptions &LangOpts, 768 SourceLocation *MacroEnd) { 769 assert(loc.isValid() && loc.isMacroID() && "Expected a valid macro loc"); 770 771 SourceLocation spellLoc = SM.getSpellingLoc(loc); 772 unsigned tokLen = MeasureTokenLength(spellLoc, SM, LangOpts); 773 if (tokLen == 0) 774 return false; 775 776 FileID FID = SM.getFileID(loc); 777 SourceLocation afterLoc = loc.getLocWithOffset(tokLen+1); 778 if (SM.isInFileID(afterLoc, FID)) 779 return false; // Still in the same FileID, does not point to the last token. 780 781 // FIXME: If the token comes from the macro token paste operator ('##') 782 // or the stringify operator ('#') this function will always return false; 783 784 SourceLocation expansionLoc = 785 SM.getSLocEntry(FID).getExpansion().getExpansionLocEnd(); 786 if (expansionLoc.isFileID()) { 787 // No other macro expansions. 788 if (MacroEnd) 789 *MacroEnd = expansionLoc; 790 return true; 791 } 792 793 return isAtEndOfMacroExpansion(expansionLoc, SM, LangOpts, MacroEnd); 794 } 795 796 static CharSourceRange makeRangeFromFileLocs(SourceLocation Begin, 797 SourceLocation End, 798 const SourceManager &SM, 799 const LangOptions &LangOpts) { 800 assert(Begin.isFileID() && End.isFileID()); 801 End = Lexer::getLocForEndOfToken(End, 0, SM,LangOpts); 802 if (End.isInvalid()) 803 return CharSourceRange(); 804 805 // Break down the source locations. 806 FileID FID; 807 unsigned BeginOffs; 808 llvm::tie(FID, BeginOffs) = SM.getDecomposedLoc(Begin); 809 if (FID.isInvalid()) 810 return CharSourceRange(); 811 812 unsigned EndOffs; 813 if (!SM.isInFileID(End, FID, &EndOffs) || 814 BeginOffs > EndOffs) 815 return CharSourceRange(); 816 817 return CharSourceRange::getCharRange(Begin, End); 818 } 819 820 /// \brief Accepts a token source range and returns a character range with 821 /// file locations. 822 /// Returns a null range if a part of the range resides inside a macro 823 /// expansion or the range does not reside on the same FileID. 824 CharSourceRange Lexer::makeFileCharRange(SourceRange TokenRange, 825 const SourceManager &SM, 826 const LangOptions &LangOpts) { 827 SourceLocation Begin = TokenRange.getBegin(); 828 SourceLocation End = TokenRange.getEnd(); 829 if (Begin.isInvalid() || End.isInvalid()) 830 return CharSourceRange(); 831 832 if (Begin.isFileID() && End.isFileID()) 833 return makeRangeFromFileLocs(Begin, End, SM, LangOpts); 834 835 if (Begin.isMacroID() && End.isFileID()) { 836 if (!isAtStartOfMacroExpansion(Begin, SM, LangOpts, &Begin)) 837 return CharSourceRange(); 838 return makeRangeFromFileLocs(Begin, End, SM, LangOpts); 839 } 840 841 if (Begin.isFileID() && End.isMacroID()) { 842 if (!isAtEndOfMacroExpansion(End, SM, LangOpts, &End)) 843 return CharSourceRange(); 844 return makeRangeFromFileLocs(Begin, End, SM, LangOpts); 845 } 846 847 assert(Begin.isMacroID() && End.isMacroID()); 848 SourceLocation MacroBegin, MacroEnd; 849 if (isAtStartOfMacroExpansion(Begin, SM, LangOpts, &MacroBegin) && 850 isAtEndOfMacroExpansion(End, SM, LangOpts, &MacroEnd)) 851 return makeRangeFromFileLocs(MacroBegin, MacroEnd, SM, LangOpts); 852 853 FileID FID; 854 unsigned BeginOffs; 855 llvm::tie(FID, BeginOffs) = SM.getDecomposedLoc(Begin); 856 if (FID.isInvalid()) 857 return CharSourceRange(); 858 859 unsigned EndOffs; 860 if (!SM.isInFileID(End, FID, &EndOffs) || 861 BeginOffs > EndOffs) 862 return CharSourceRange(); 863 864 const SrcMgr::SLocEntry *E = &SM.getSLocEntry(FID); 865 const SrcMgr::ExpansionInfo &Expansion = E->getExpansion(); 866 if (Expansion.isMacroArgExpansion() && 867 Expansion.getSpellingLoc().isFileID()) { 868 SourceLocation SpellLoc = Expansion.getSpellingLoc(); 869 return makeRangeFromFileLocs(SpellLoc.getLocWithOffset(BeginOffs), 870 SpellLoc.getLocWithOffset(EndOffs), 871 SM, LangOpts); 872 } 873 874 return CharSourceRange(); 875 } 876 877 StringRef Lexer::getSourceText(CharSourceRange Range, 878 const SourceManager &SM, 879 const LangOptions &LangOpts, 880 bool *Invalid) { 881 if (Range.isTokenRange()) 882 Range = makeFileCharRange(Range.getAsRange(), SM, LangOpts); 883 884 if (Range.isInvalid() || 885 Range.getBegin().isMacroID() || Range.getEnd().isMacroID()) { 886 if (Invalid) *Invalid = true; 887 return StringRef(); 888 } 889 890 // Break down the source location. 891 std::pair<FileID, unsigned> beginInfo = SM.getDecomposedLoc(Range.getBegin()); 892 if (beginInfo.first.isInvalid()) { 893 if (Invalid) *Invalid = true; 894 return StringRef(); 895 } 896 897 unsigned EndOffs; 898 if (!SM.isInFileID(Range.getEnd(), beginInfo.first, &EndOffs) || 899 beginInfo.second > EndOffs) { 900 if (Invalid) *Invalid = true; 901 return StringRef(); 902 } 903 904 // Try to the load the file buffer. 905 bool invalidTemp = false; 906 StringRef file = SM.getBufferData(beginInfo.first, &invalidTemp); 907 if (invalidTemp) { 908 if (Invalid) *Invalid = true; 909 return StringRef(); 910 } 911 912 if (Invalid) *Invalid = false; 913 return file.substr(beginInfo.second, EndOffs - beginInfo.second); 914 } 915 916 StringRef Lexer::getImmediateMacroName(SourceLocation Loc, 917 const SourceManager &SM, 918 const LangOptions &LangOpts) { 919 assert(Loc.isMacroID() && "Only reasonble to call this on macros"); 920 921 // Find the location of the immediate macro expansion. 922 while (1) { 923 FileID FID = SM.getFileID(Loc); 924 const SrcMgr::SLocEntry *E = &SM.getSLocEntry(FID); 925 const SrcMgr::ExpansionInfo &Expansion = E->getExpansion(); 926 Loc = Expansion.getExpansionLocStart(); 927 if (!Expansion.isMacroArgExpansion()) 928 break; 929 930 // For macro arguments we need to check that the argument did not come 931 // from an inner macro, e.g: "MAC1( MAC2(foo) )" 932 933 // Loc points to the argument id of the macro definition, move to the 934 // macro expansion. 935 Loc = SM.getImmediateExpansionRange(Loc).first; 936 SourceLocation SpellLoc = Expansion.getSpellingLoc(); 937 if (SpellLoc.isFileID()) 938 break; // No inner macro. 939 940 // If spelling location resides in the same FileID as macro expansion 941 // location, it means there is no inner macro. 942 FileID MacroFID = SM.getFileID(Loc); 943 if (SM.isInFileID(SpellLoc, MacroFID)) 944 break; 945 946 // Argument came from inner macro. 947 Loc = SpellLoc; 948 } 949 950 // Find the spelling location of the start of the non-argument expansion 951 // range. This is where the macro name was spelled in order to begin 952 // expanding this macro. 953 Loc = SM.getSpellingLoc(Loc); 954 955 // Dig out the buffer where the macro name was spelled and the extents of the 956 // name so that we can render it into the expansion note. 957 std::pair<FileID, unsigned> ExpansionInfo = SM.getDecomposedLoc(Loc); 958 unsigned MacroTokenLength = Lexer::MeasureTokenLength(Loc, SM, LangOpts); 959 StringRef ExpansionBuffer = SM.getBufferData(ExpansionInfo.first); 960 return ExpansionBuffer.substr(ExpansionInfo.second, MacroTokenLength); 961 } 962 963 //===----------------------------------------------------------------------===// 964 // Character information. 965 //===----------------------------------------------------------------------===// 966 967 enum { 968 CHAR_HORZ_WS = 0x01, // ' ', '\t', '\f', '\v'. Note, no '\0' 969 CHAR_VERT_WS = 0x02, // '\r', '\n' 970 CHAR_LETTER = 0x04, // a-z,A-Z 971 CHAR_NUMBER = 0x08, // 0-9 972 CHAR_UNDER = 0x10, // _ 973 CHAR_PERIOD = 0x20, // . 974 CHAR_RAWDEL = 0x40 // {}[]#<>%:;?*+-/^&|~!=,"' 975 }; 976 977 // Statically initialize CharInfo table based on ASCII character set 978 // Reference: FreeBSD 7.2 /usr/share/misc/ascii 979 static const unsigned char CharInfo[256] = 980 { 981 // 0 NUL 1 SOH 2 STX 3 ETX 982 // 4 EOT 5 ENQ 6 ACK 7 BEL 983 0 , 0 , 0 , 0 , 984 0 , 0 , 0 , 0 , 985 // 8 BS 9 HT 10 NL 11 VT 986 //12 NP 13 CR 14 SO 15 SI 987 0 , CHAR_HORZ_WS, CHAR_VERT_WS, CHAR_HORZ_WS, 988 CHAR_HORZ_WS, CHAR_VERT_WS, 0 , 0 , 989 //16 DLE 17 DC1 18 DC2 19 DC3 990 //20 DC4 21 NAK 22 SYN 23 ETB 991 0 , 0 , 0 , 0 , 992 0 , 0 , 0 , 0 , 993 //24 CAN 25 EM 26 SUB 27 ESC 994 //28 FS 29 GS 30 RS 31 US 995 0 , 0 , 0 , 0 , 996 0 , 0 , 0 , 0 , 997 //32 SP 33 ! 34 " 35 # 998 //36 $ 37 % 38 & 39 ' 999 CHAR_HORZ_WS, CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , 1000 0 , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , 1001 //40 ( 41 ) 42 * 43 + 1002 //44 , 45 - 46 . 47 / 1003 0 , 0 , CHAR_RAWDEL , CHAR_RAWDEL , 1004 CHAR_RAWDEL , CHAR_RAWDEL , CHAR_PERIOD , CHAR_RAWDEL , 1005 //48 0 49 1 50 2 51 3 1006 //52 4 53 5 54 6 55 7 1007 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , 1008 CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , CHAR_NUMBER , 1009 //56 8 57 9 58 : 59 ; 1010 //60 < 61 = 62 > 63 ? 1011 CHAR_NUMBER , CHAR_NUMBER , CHAR_RAWDEL , CHAR_RAWDEL , 1012 CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , 1013 //64 @ 65 A 66 B 67 C 1014 //68 D 69 E 70 F 71 G 1015 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 1016 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 1017 //72 H 73 I 74 J 75 K 1018 //76 L 77 M 78 N 79 O 1019 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 1020 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 1021 //80 P 81 Q 82 R 83 S 1022 //84 T 85 U 86 V 87 W 1023 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 1024 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 1025 //88 X 89 Y 90 Z 91 [ 1026 //92 \ 93 ] 94 ^ 95 _ 1027 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_RAWDEL , 1028 0 , CHAR_RAWDEL , CHAR_RAWDEL , CHAR_UNDER , 1029 //96 ` 97 a 98 b 99 c 1030 //100 d 101 e 102 f 103 g 1031 0 , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 1032 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 1033 //104 h 105 i 106 j 107 k 1034 //108 l 109 m 110 n 111 o 1035 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 1036 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 1037 //112 p 113 q 114 r 115 s 1038 //116 t 117 u 118 v 119 w 1039 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 1040 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , 1041 //120 x 121 y 122 z 123 { 1042 //124 | 125 } 126 ~ 127 DEL 1043 CHAR_LETTER , CHAR_LETTER , CHAR_LETTER , CHAR_RAWDEL , 1044 CHAR_RAWDEL , CHAR_RAWDEL , CHAR_RAWDEL , 0 1045 }; 1046 1047 static void InitCharacterInfo() { 1048 static bool isInited = false; 1049 if (isInited) return; 1050 // check the statically-initialized CharInfo table 1051 assert(CHAR_HORZ_WS == CharInfo[(int)' ']); 1052 assert(CHAR_HORZ_WS == CharInfo[(int)'\t']); 1053 assert(CHAR_HORZ_WS == CharInfo[(int)'\f']); 1054 assert(CHAR_HORZ_WS == CharInfo[(int)'\v']); 1055 assert(CHAR_VERT_WS == CharInfo[(int)'\n']); 1056 assert(CHAR_VERT_WS == CharInfo[(int)'\r']); 1057 assert(CHAR_UNDER == CharInfo[(int)'_']); 1058 assert(CHAR_PERIOD == CharInfo[(int)'.']); 1059 for (unsigned i = 'a'; i <= 'z'; ++i) { 1060 assert(CHAR_LETTER == CharInfo[i]); 1061 assert(CHAR_LETTER == CharInfo[i+'A'-'a']); 1062 } 1063 for (unsigned i = '0'; i <= '9'; ++i) 1064 assert(CHAR_NUMBER == CharInfo[i]); 1065 1066 isInited = true; 1067 } 1068 1069 1070 /// isIdentifierBody - Return true if this is the body character of an 1071 /// identifier, which is [a-zA-Z0-9_]. 1072 static inline bool isIdentifierBody(unsigned char c) { 1073 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER)) ? true : false; 1074 } 1075 1076 /// isHorizontalWhitespace - Return true if this character is horizontal 1077 /// whitespace: ' ', '\t', '\f', '\v'. Note that this returns false for '\0'. 1078 static inline bool isHorizontalWhitespace(unsigned char c) { 1079 return (CharInfo[c] & CHAR_HORZ_WS) ? true : false; 1080 } 1081 1082 /// isVerticalWhitespace - Return true if this character is vertical 1083 /// whitespace: '\n', '\r'. Note that this returns false for '\0'. 1084 static inline bool isVerticalWhitespace(unsigned char c) { 1085 return (CharInfo[c] & CHAR_VERT_WS) ? true : false; 1086 } 1087 1088 /// isWhitespace - Return true if this character is horizontal or vertical 1089 /// whitespace: ' ', '\t', '\f', '\v', '\n', '\r'. Note that this returns false 1090 /// for '\0'. 1091 static inline bool isWhitespace(unsigned char c) { 1092 return (CharInfo[c] & (CHAR_HORZ_WS|CHAR_VERT_WS)) ? true : false; 1093 } 1094 1095 /// isNumberBody - Return true if this is the body character of an 1096 /// preprocessing number, which is [a-zA-Z0-9_.]. 1097 static inline bool isNumberBody(unsigned char c) { 1098 return (CharInfo[c] & (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD)) ? 1099 true : false; 1100 } 1101 1102 /// isRawStringDelimBody - Return true if this is the body character of a 1103 /// raw string delimiter. 1104 static inline bool isRawStringDelimBody(unsigned char c) { 1105 return (CharInfo[c] & 1106 (CHAR_LETTER|CHAR_NUMBER|CHAR_UNDER|CHAR_PERIOD|CHAR_RAWDEL)) ? 1107 true : false; 1108 } 1109 1110 1111 //===----------------------------------------------------------------------===// 1112 // Diagnostics forwarding code. 1113 //===----------------------------------------------------------------------===// 1114 1115 /// GetMappedTokenLoc - If lexing out of a 'mapped buffer', where we pretend the 1116 /// lexer buffer was all expanded at a single point, perform the mapping. 1117 /// This is currently only used for _Pragma implementation, so it is the slow 1118 /// path of the hot getSourceLocation method. Do not allow it to be inlined. 1119 static LLVM_ATTRIBUTE_NOINLINE SourceLocation GetMappedTokenLoc( 1120 Preprocessor &PP, SourceLocation FileLoc, unsigned CharNo, unsigned TokLen); 1121 static SourceLocation GetMappedTokenLoc(Preprocessor &PP, 1122 SourceLocation FileLoc, 1123 unsigned CharNo, unsigned TokLen) { 1124 assert(FileLoc.isMacroID() && "Must be a macro expansion"); 1125 1126 // Otherwise, we're lexing "mapped tokens". This is used for things like 1127 // _Pragma handling. Combine the expansion location of FileLoc with the 1128 // spelling location. 1129 SourceManager &SM = PP.getSourceManager(); 1130 1131 // Create a new SLoc which is expanded from Expansion(FileLoc) but whose 1132 // characters come from spelling(FileLoc)+Offset. 1133 SourceLocation SpellingLoc = SM.getSpellingLoc(FileLoc); 1134 SpellingLoc = SpellingLoc.getLocWithOffset(CharNo); 1135 1136 // Figure out the expansion loc range, which is the range covered by the 1137 // original _Pragma(...) sequence. 1138 std::pair<SourceLocation,SourceLocation> II = 1139 SM.getImmediateExpansionRange(FileLoc); 1140 1141 return SM.createExpansionLoc(SpellingLoc, II.first, II.second, TokLen); 1142 } 1143 1144 /// getSourceLocation - Return a source location identifier for the specified 1145 /// offset in the current file. 1146 SourceLocation Lexer::getSourceLocation(const char *Loc, 1147 unsigned TokLen) const { 1148 assert(Loc >= BufferStart && Loc <= BufferEnd && 1149 "Location out of range for this buffer!"); 1150 1151 // In the normal case, we're just lexing from a simple file buffer, return 1152 // the file id from FileLoc with the offset specified. 1153 unsigned CharNo = Loc-BufferStart; 1154 if (FileLoc.isFileID()) 1155 return FileLoc.getLocWithOffset(CharNo); 1156 1157 // Otherwise, this is the _Pragma lexer case, which pretends that all of the 1158 // tokens are lexed from where the _Pragma was defined. 1159 assert(PP && "This doesn't work on raw lexers"); 1160 return GetMappedTokenLoc(*PP, FileLoc, CharNo, TokLen); 1161 } 1162 1163 /// Diag - Forwarding function for diagnostics. This translate a source 1164 /// position in the current buffer into a SourceLocation object for rendering. 1165 DiagnosticBuilder Lexer::Diag(const char *Loc, unsigned DiagID) const { 1166 return PP->Diag(getSourceLocation(Loc), DiagID); 1167 } 1168 1169 //===----------------------------------------------------------------------===// 1170 // Trigraph and Escaped Newline Handling Code. 1171 //===----------------------------------------------------------------------===// 1172 1173 /// GetTrigraphCharForLetter - Given a character that occurs after a ?? pair, 1174 /// return the decoded trigraph letter it corresponds to, or '\0' if nothing. 1175 static char GetTrigraphCharForLetter(char Letter) { 1176 switch (Letter) { 1177 default: return 0; 1178 case '=': return '#'; 1179 case ')': return ']'; 1180 case '(': return '['; 1181 case '!': return '|'; 1182 case '\'': return '^'; 1183 case '>': return '}'; 1184 case '/': return '\\'; 1185 case '<': return '{'; 1186 case '-': return '~'; 1187 } 1188 } 1189 1190 /// DecodeTrigraphChar - If the specified character is a legal trigraph when 1191 /// prefixed with ??, emit a trigraph warning. If trigraphs are enabled, 1192 /// return the result character. Finally, emit a warning about trigraph use 1193 /// whether trigraphs are enabled or not. 1194 static char DecodeTrigraphChar(const char *CP, Lexer *L) { 1195 char Res = GetTrigraphCharForLetter(*CP); 1196 if (!Res || !L) return Res; 1197 1198 if (!L->getFeatures().Trigraphs) { 1199 if (!L->isLexingRawMode()) 1200 L->Diag(CP-2, diag::trigraph_ignored); 1201 return 0; 1202 } 1203 1204 if (!L->isLexingRawMode()) 1205 L->Diag(CP-2, diag::trigraph_converted) << StringRef(&Res, 1); 1206 return Res; 1207 } 1208 1209 /// getEscapedNewLineSize - Return the size of the specified escaped newline, 1210 /// or 0 if it is not an escaped newline. P[-1] is known to be a "\" or a 1211 /// trigraph equivalent on entry to this function. 1212 unsigned Lexer::getEscapedNewLineSize(const char *Ptr) { 1213 unsigned Size = 0; 1214 while (isWhitespace(Ptr[Size])) { 1215 ++Size; 1216 1217 if (Ptr[Size-1] != '\n' && Ptr[Size-1] != '\r') 1218 continue; 1219 1220 // If this is a \r\n or \n\r, skip the other half. 1221 if ((Ptr[Size] == '\r' || Ptr[Size] == '\n') && 1222 Ptr[Size-1] != Ptr[Size]) 1223 ++Size; 1224 1225 return Size; 1226 } 1227 1228 // Not an escaped newline, must be a \t or something else. 1229 return 0; 1230 } 1231 1232 /// SkipEscapedNewLines - If P points to an escaped newline (or a series of 1233 /// them), skip over them and return the first non-escaped-newline found, 1234 /// otherwise return P. 1235 const char *Lexer::SkipEscapedNewLines(const char *P) { 1236 while (1) { 1237 const char *AfterEscape; 1238 if (*P == '\\') { 1239 AfterEscape = P+1; 1240 } else if (*P == '?') { 1241 // If not a trigraph for escape, bail out. 1242 if (P[1] != '?' || P[2] != '/') 1243 return P; 1244 AfterEscape = P+3; 1245 } else { 1246 return P; 1247 } 1248 1249 unsigned NewLineSize = Lexer::getEscapedNewLineSize(AfterEscape); 1250 if (NewLineSize == 0) return P; 1251 P = AfterEscape+NewLineSize; 1252 } 1253 } 1254 1255 /// \brief Checks that the given token is the first token that occurs after the 1256 /// given location (this excludes comments and whitespace). Returns the location 1257 /// immediately after the specified token. If the token is not found or the 1258 /// location is inside a macro, the returned source location will be invalid. 1259 SourceLocation Lexer::findLocationAfterToken(SourceLocation Loc, 1260 tok::TokenKind TKind, 1261 const SourceManager &SM, 1262 const LangOptions &LangOpts, 1263 bool SkipTrailingWhitespaceAndNewLine) { 1264 if (Loc.isMacroID()) { 1265 if (!Lexer::isAtEndOfMacroExpansion(Loc, SM, LangOpts, &Loc)) 1266 return SourceLocation(); 1267 } 1268 Loc = Lexer::getLocForEndOfToken(Loc, 0, SM, LangOpts); 1269 1270 // Break down the source location. 1271 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 1272 1273 // Try to load the file buffer. 1274 bool InvalidTemp = false; 1275 llvm::StringRef File = SM.getBufferData(LocInfo.first, &InvalidTemp); 1276 if (InvalidTemp) 1277 return SourceLocation(); 1278 1279 const char *TokenBegin = File.data() + LocInfo.second; 1280 1281 // Lex from the start of the given location. 1282 Lexer lexer(SM.getLocForStartOfFile(LocInfo.first), LangOpts, File.begin(), 1283 TokenBegin, File.end()); 1284 // Find the token. 1285 Token Tok; 1286 lexer.LexFromRawLexer(Tok); 1287 if (Tok.isNot(TKind)) 1288 return SourceLocation(); 1289 SourceLocation TokenLoc = Tok.getLocation(); 1290 1291 // Calculate how much whitespace needs to be skipped if any. 1292 unsigned NumWhitespaceChars = 0; 1293 if (SkipTrailingWhitespaceAndNewLine) { 1294 const char *TokenEnd = SM.getCharacterData(TokenLoc) + 1295 Tok.getLength(); 1296 unsigned char C = *TokenEnd; 1297 while (isHorizontalWhitespace(C)) { 1298 C = *(++TokenEnd); 1299 NumWhitespaceChars++; 1300 } 1301 if (isVerticalWhitespace(C)) 1302 NumWhitespaceChars++; 1303 } 1304 1305 return TokenLoc.getLocWithOffset(Tok.getLength() + NumWhitespaceChars); 1306 } 1307 1308 /// getCharAndSizeSlow - Peek a single 'character' from the specified buffer, 1309 /// get its size, and return it. This is tricky in several cases: 1310 /// 1. If currently at the start of a trigraph, we warn about the trigraph, 1311 /// then either return the trigraph (skipping 3 chars) or the '?', 1312 /// depending on whether trigraphs are enabled or not. 1313 /// 2. If this is an escaped newline (potentially with whitespace between 1314 /// the backslash and newline), implicitly skip the newline and return 1315 /// the char after it. 1316 /// 3. If this is a UCN, return it. FIXME: C++ UCN's? 1317 /// 1318 /// This handles the slow/uncommon case of the getCharAndSize method. Here we 1319 /// know that we can accumulate into Size, and that we have already incremented 1320 /// Ptr by Size bytes. 1321 /// 1322 /// NOTE: When this method is updated, getCharAndSizeSlowNoWarn (below) should 1323 /// be updated to match. 1324 /// 1325 char Lexer::getCharAndSizeSlow(const char *Ptr, unsigned &Size, 1326 Token *Tok) { 1327 // If we have a slash, look for an escaped newline. 1328 if (Ptr[0] == '\\') { 1329 ++Size; 1330 ++Ptr; 1331 Slash: 1332 // Common case, backslash-char where the char is not whitespace. 1333 if (!isWhitespace(Ptr[0])) return '\\'; 1334 1335 // See if we have optional whitespace characters between the slash and 1336 // newline. 1337 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { 1338 // Remember that this token needs to be cleaned. 1339 if (Tok) Tok->setFlag(Token::NeedsCleaning); 1340 1341 // Warn if there was whitespace between the backslash and newline. 1342 if (Ptr[0] != '\n' && Ptr[0] != '\r' && Tok && !isLexingRawMode()) 1343 Diag(Ptr, diag::backslash_newline_space); 1344 1345 // Found backslash<whitespace><newline>. Parse the char after it. 1346 Size += EscapedNewLineSize; 1347 Ptr += EscapedNewLineSize; 1348 1349 // If the char that we finally got was a \n, then we must have had 1350 // something like \<newline><newline>. We don't want to consume the 1351 // second newline. 1352 if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0') 1353 return ' '; 1354 1355 // Use slow version to accumulate a correct size field. 1356 return getCharAndSizeSlow(Ptr, Size, Tok); 1357 } 1358 1359 // Otherwise, this is not an escaped newline, just return the slash. 1360 return '\\'; 1361 } 1362 1363 // If this is a trigraph, process it. 1364 if (Ptr[0] == '?' && Ptr[1] == '?') { 1365 // If this is actually a legal trigraph (not something like "??x"), emit 1366 // a trigraph warning. If so, and if trigraphs are enabled, return it. 1367 if (char C = DecodeTrigraphChar(Ptr+2, Tok ? this : 0)) { 1368 // Remember that this token needs to be cleaned. 1369 if (Tok) Tok->setFlag(Token::NeedsCleaning); 1370 1371 Ptr += 3; 1372 Size += 3; 1373 if (C == '\\') goto Slash; 1374 return C; 1375 } 1376 } 1377 1378 // If this is neither, return a single character. 1379 ++Size; 1380 return *Ptr; 1381 } 1382 1383 1384 /// getCharAndSizeSlowNoWarn - Handle the slow/uncommon case of the 1385 /// getCharAndSizeNoWarn method. Here we know that we can accumulate into Size, 1386 /// and that we have already incremented Ptr by Size bytes. 1387 /// 1388 /// NOTE: When this method is updated, getCharAndSizeSlow (above) should 1389 /// be updated to match. 1390 char Lexer::getCharAndSizeSlowNoWarn(const char *Ptr, unsigned &Size, 1391 const LangOptions &Features) { 1392 // If we have a slash, look for an escaped newline. 1393 if (Ptr[0] == '\\') { 1394 ++Size; 1395 ++Ptr; 1396 Slash: 1397 // Common case, backslash-char where the char is not whitespace. 1398 if (!isWhitespace(Ptr[0])) return '\\'; 1399 1400 // See if we have optional whitespace characters followed by a newline. 1401 if (unsigned EscapedNewLineSize = getEscapedNewLineSize(Ptr)) { 1402 // Found backslash<whitespace><newline>. Parse the char after it. 1403 Size += EscapedNewLineSize; 1404 Ptr += EscapedNewLineSize; 1405 1406 // If the char that we finally got was a \n, then we must have had 1407 // something like \<newline><newline>. We don't want to consume the 1408 // second newline. 1409 if (*Ptr == '\n' || *Ptr == '\r' || *Ptr == '\0') 1410 return ' '; 1411 1412 // Use slow version to accumulate a correct size field. 1413 return getCharAndSizeSlowNoWarn(Ptr, Size, Features); 1414 } 1415 1416 // Otherwise, this is not an escaped newline, just return the slash. 1417 return '\\'; 1418 } 1419 1420 // If this is a trigraph, process it. 1421 if (Features.Trigraphs && Ptr[0] == '?' && Ptr[1] == '?') { 1422 // If this is actually a legal trigraph (not something like "??x"), return 1423 // it. 1424 if (char C = GetTrigraphCharForLetter(Ptr[2])) { 1425 Ptr += 3; 1426 Size += 3; 1427 if (C == '\\') goto Slash; 1428 return C; 1429 } 1430 } 1431 1432 // If this is neither, return a single character. 1433 ++Size; 1434 return *Ptr; 1435 } 1436 1437 //===----------------------------------------------------------------------===// 1438 // Helper methods for lexing. 1439 //===----------------------------------------------------------------------===// 1440 1441 /// \brief Routine that indiscriminately skips bytes in the source file. 1442 void Lexer::SkipBytes(unsigned Bytes, bool StartOfLine) { 1443 BufferPtr += Bytes; 1444 if (BufferPtr > BufferEnd) 1445 BufferPtr = BufferEnd; 1446 IsAtStartOfLine = StartOfLine; 1447 } 1448 1449 void Lexer::LexIdentifier(Token &Result, const char *CurPtr) { 1450 // Match [_A-Za-z0-9]*, we have already matched [_A-Za-z$] 1451 unsigned Size; 1452 unsigned char C = *CurPtr++; 1453 while (isIdentifierBody(C)) 1454 C = *CurPtr++; 1455 1456 --CurPtr; // Back up over the skipped character. 1457 1458 // Fast path, no $,\,? in identifier found. '\' might be an escaped newline 1459 // or UCN, and ? might be a trigraph for '\', an escaped newline or UCN. 1460 // FIXME: UCNs. 1461 // 1462 // TODO: Could merge these checks into a CharInfo flag to make the comparison 1463 // cheaper 1464 if (C != '\\' && C != '?' && (C != '$' || !Features.DollarIdents)) { 1465 FinishIdentifier: 1466 const char *IdStart = BufferPtr; 1467 FormTokenWithChars(Result, CurPtr, tok::raw_identifier); 1468 Result.setRawIdentifierData(IdStart); 1469 1470 // If we are in raw mode, return this identifier raw. There is no need to 1471 // look up identifier information or attempt to macro expand it. 1472 if (LexingRawMode) 1473 return; 1474 1475 // Fill in Result.IdentifierInfo and update the token kind, 1476 // looking up the identifier in the identifier table. 1477 IdentifierInfo *II = PP->LookUpIdentifierInfo(Result); 1478 1479 // Finally, now that we know we have an identifier, pass this off to the 1480 // preprocessor, which may macro expand it or something. 1481 if (II->isHandleIdentifierCase()) 1482 PP->HandleIdentifier(Result); 1483 1484 return; 1485 } 1486 1487 // Otherwise, $,\,? in identifier found. Enter slower path. 1488 1489 C = getCharAndSize(CurPtr, Size); 1490 while (1) { 1491 if (C == '$') { 1492 // If we hit a $ and they are not supported in identifiers, we are done. 1493 if (!Features.DollarIdents) goto FinishIdentifier; 1494 1495 // Otherwise, emit a diagnostic and continue. 1496 if (!isLexingRawMode()) 1497 Diag(CurPtr, diag::ext_dollar_in_identifier); 1498 CurPtr = ConsumeChar(CurPtr, Size, Result); 1499 C = getCharAndSize(CurPtr, Size); 1500 continue; 1501 } else if (!isIdentifierBody(C)) { // FIXME: UCNs. 1502 // Found end of identifier. 1503 goto FinishIdentifier; 1504 } 1505 1506 // Otherwise, this character is good, consume it. 1507 CurPtr = ConsumeChar(CurPtr, Size, Result); 1508 1509 C = getCharAndSize(CurPtr, Size); 1510 while (isIdentifierBody(C)) { // FIXME: UCNs. 1511 CurPtr = ConsumeChar(CurPtr, Size, Result); 1512 C = getCharAndSize(CurPtr, Size); 1513 } 1514 } 1515 } 1516 1517 /// isHexaLiteral - Return true if Start points to a hex constant. 1518 /// in microsoft mode (where this is supposed to be several different tokens). 1519 static bool isHexaLiteral(const char *Start, const LangOptions &Features) { 1520 unsigned Size; 1521 char C1 = Lexer::getCharAndSizeNoWarn(Start, Size, Features); 1522 if (C1 != '0') 1523 return false; 1524 char C2 = Lexer::getCharAndSizeNoWarn(Start + Size, Size, Features); 1525 return (C2 == 'x' || C2 == 'X'); 1526 } 1527 1528 /// LexNumericConstant - Lex the remainder of a integer or floating point 1529 /// constant. From[-1] is the first character lexed. Return the end of the 1530 /// constant. 1531 void Lexer::LexNumericConstant(Token &Result, const char *CurPtr) { 1532 unsigned Size; 1533 char C = getCharAndSize(CurPtr, Size); 1534 char PrevCh = 0; 1535 while (isNumberBody(C)) { // FIXME: UCNs? 1536 CurPtr = ConsumeChar(CurPtr, Size, Result); 1537 PrevCh = C; 1538 C = getCharAndSize(CurPtr, Size); 1539 } 1540 1541 // If we fell out, check for a sign, due to 1e+12. If we have one, continue. 1542 if ((C == '-' || C == '+') && (PrevCh == 'E' || PrevCh == 'e')) { 1543 // If we are in Microsoft mode, don't continue if the constant is hex. 1544 // For example, MSVC will accept the following as 3 tokens: 0x1234567e+1 1545 if (!Features.MicrosoftExt || !isHexaLiteral(BufferPtr, Features)) 1546 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); 1547 } 1548 1549 // If we have a hex FP constant, continue. 1550 if ((C == '-' || C == '+') && (PrevCh == 'P' || PrevCh == 'p')) 1551 return LexNumericConstant(Result, ConsumeChar(CurPtr, Size, Result)); 1552 1553 // Update the location of token as well as BufferPtr. 1554 const char *TokStart = BufferPtr; 1555 FormTokenWithChars(Result, CurPtr, tok::numeric_constant); 1556 Result.setLiteralData(TokStart); 1557 } 1558 1559 /// LexStringLiteral - Lex the remainder of a string literal, after having lexed 1560 /// either " or L" or u8" or u" or U". 1561 void Lexer::LexStringLiteral(Token &Result, const char *CurPtr, 1562 tok::TokenKind Kind) { 1563 const char *NulCharacter = 0; // Does this string contain the \0 character? 1564 1565 if (!isLexingRawMode() && 1566 (Kind == tok::utf8_string_literal || 1567 Kind == tok::utf16_string_literal || 1568 Kind == tok::utf32_string_literal)) 1569 Diag(BufferPtr, diag::warn_cxx98_compat_unicode_literal); 1570 1571 char C = getAndAdvanceChar(CurPtr, Result); 1572 while (C != '"') { 1573 // Skip escaped characters. Escaped newlines will already be processed by 1574 // getAndAdvanceChar. 1575 if (C == '\\') 1576 C = getAndAdvanceChar(CurPtr, Result); 1577 1578 if (C == '\n' || C == '\r' || // Newline. 1579 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. 1580 if (!isLexingRawMode() && !Features.AsmPreprocessor) 1581 Diag(BufferPtr, diag::warn_unterminated_string); 1582 FormTokenWithChars(Result, CurPtr-1, tok::unknown); 1583 return; 1584 } 1585 1586 if (C == 0) { 1587 if (isCodeCompletionPoint(CurPtr-1)) { 1588 PP->CodeCompleteNaturalLanguage(); 1589 FormTokenWithChars(Result, CurPtr-1, tok::unknown); 1590 return cutOffLexing(); 1591 } 1592 1593 NulCharacter = CurPtr-1; 1594 } 1595 C = getAndAdvanceChar(CurPtr, Result); 1596 } 1597 1598 // If a nul character existed in the string, warn about it. 1599 if (NulCharacter && !isLexingRawMode()) 1600 Diag(NulCharacter, diag::null_in_string); 1601 1602 // Update the location of the token as well as the BufferPtr instance var. 1603 const char *TokStart = BufferPtr; 1604 FormTokenWithChars(Result, CurPtr, Kind); 1605 Result.setLiteralData(TokStart); 1606 } 1607 1608 /// LexRawStringLiteral - Lex the remainder of a raw string literal, after 1609 /// having lexed R", LR", u8R", uR", or UR". 1610 void Lexer::LexRawStringLiteral(Token &Result, const char *CurPtr, 1611 tok::TokenKind Kind) { 1612 // This function doesn't use getAndAdvanceChar because C++0x [lex.pptoken]p3: 1613 // Between the initial and final double quote characters of the raw string, 1614 // any transformations performed in phases 1 and 2 (trigraphs, 1615 // universal-character-names, and line splicing) are reverted. 1616 1617 if (!isLexingRawMode()) 1618 Diag(BufferPtr, diag::warn_cxx98_compat_raw_string_literal); 1619 1620 unsigned PrefixLen = 0; 1621 1622 while (PrefixLen != 16 && isRawStringDelimBody(CurPtr[PrefixLen])) 1623 ++PrefixLen; 1624 1625 // If the last character was not a '(', then we didn't lex a valid delimiter. 1626 if (CurPtr[PrefixLen] != '(') { 1627 if (!isLexingRawMode()) { 1628 const char *PrefixEnd = &CurPtr[PrefixLen]; 1629 if (PrefixLen == 16) { 1630 Diag(PrefixEnd, diag::err_raw_delim_too_long); 1631 } else { 1632 Diag(PrefixEnd, diag::err_invalid_char_raw_delim) 1633 << StringRef(PrefixEnd, 1); 1634 } 1635 } 1636 1637 // Search for the next '"' in hopes of salvaging the lexer. Unfortunately, 1638 // it's possible the '"' was intended to be part of the raw string, but 1639 // there's not much we can do about that. 1640 while (1) { 1641 char C = *CurPtr++; 1642 1643 if (C == '"') 1644 break; 1645 if (C == 0 && CurPtr-1 == BufferEnd) { 1646 --CurPtr; 1647 break; 1648 } 1649 } 1650 1651 FormTokenWithChars(Result, CurPtr, tok::unknown); 1652 return; 1653 } 1654 1655 // Save prefix and move CurPtr past it 1656 const char *Prefix = CurPtr; 1657 CurPtr += PrefixLen + 1; // skip over prefix and '(' 1658 1659 while (1) { 1660 char C = *CurPtr++; 1661 1662 if (C == ')') { 1663 // Check for prefix match and closing quote. 1664 if (strncmp(CurPtr, Prefix, PrefixLen) == 0 && CurPtr[PrefixLen] == '"') { 1665 CurPtr += PrefixLen + 1; // skip over prefix and '"' 1666 break; 1667 } 1668 } else if (C == 0 && CurPtr-1 == BufferEnd) { // End of file. 1669 if (!isLexingRawMode()) 1670 Diag(BufferPtr, diag::err_unterminated_raw_string) 1671 << StringRef(Prefix, PrefixLen); 1672 FormTokenWithChars(Result, CurPtr-1, tok::unknown); 1673 return; 1674 } 1675 } 1676 1677 // Update the location of token as well as BufferPtr. 1678 const char *TokStart = BufferPtr; 1679 FormTokenWithChars(Result, CurPtr, Kind); 1680 Result.setLiteralData(TokStart); 1681 } 1682 1683 /// LexAngledStringLiteral - Lex the remainder of an angled string literal, 1684 /// after having lexed the '<' character. This is used for #include filenames. 1685 void Lexer::LexAngledStringLiteral(Token &Result, const char *CurPtr) { 1686 const char *NulCharacter = 0; // Does this string contain the \0 character? 1687 const char *AfterLessPos = CurPtr; 1688 char C = getAndAdvanceChar(CurPtr, Result); 1689 while (C != '>') { 1690 // Skip escaped characters. 1691 if (C == '\\') { 1692 // Skip the escaped character. 1693 C = getAndAdvanceChar(CurPtr, Result); 1694 } else if (C == '\n' || C == '\r' || // Newline. 1695 (C == 0 && (CurPtr-1 == BufferEnd || // End of file. 1696 isCodeCompletionPoint(CurPtr-1)))) { 1697 // If the filename is unterminated, then it must just be a lone < 1698 // character. Return this as such. 1699 FormTokenWithChars(Result, AfterLessPos, tok::less); 1700 return; 1701 } else if (C == 0) { 1702 NulCharacter = CurPtr-1; 1703 } 1704 C = getAndAdvanceChar(CurPtr, Result); 1705 } 1706 1707 // If a nul character existed in the string, warn about it. 1708 if (NulCharacter && !isLexingRawMode()) 1709 Diag(NulCharacter, diag::null_in_string); 1710 1711 // Update the location of token as well as BufferPtr. 1712 const char *TokStart = BufferPtr; 1713 FormTokenWithChars(Result, CurPtr, tok::angle_string_literal); 1714 Result.setLiteralData(TokStart); 1715 } 1716 1717 1718 /// LexCharConstant - Lex the remainder of a character constant, after having 1719 /// lexed either ' or L' or u' or U'. 1720 void Lexer::LexCharConstant(Token &Result, const char *CurPtr, 1721 tok::TokenKind Kind) { 1722 const char *NulCharacter = 0; // Does this character contain the \0 character? 1723 1724 if (!isLexingRawMode() && 1725 (Kind == tok::utf16_char_constant || Kind == tok::utf32_char_constant)) 1726 Diag(BufferPtr, diag::warn_cxx98_compat_unicode_literal); 1727 1728 char C = getAndAdvanceChar(CurPtr, Result); 1729 if (C == '\'') { 1730 if (!isLexingRawMode() && !Features.AsmPreprocessor) 1731 Diag(BufferPtr, diag::err_empty_character); 1732 FormTokenWithChars(Result, CurPtr, tok::unknown); 1733 return; 1734 } 1735 1736 while (C != '\'') { 1737 // Skip escaped characters. 1738 if (C == '\\') { 1739 // Skip the escaped character. 1740 // FIXME: UCN's 1741 C = getAndAdvanceChar(CurPtr, Result); 1742 } else if (C == '\n' || C == '\r' || // Newline. 1743 (C == 0 && CurPtr-1 == BufferEnd)) { // End of file. 1744 if (!isLexingRawMode() && !Features.AsmPreprocessor) 1745 Diag(BufferPtr, diag::warn_unterminated_char); 1746 FormTokenWithChars(Result, CurPtr-1, tok::unknown); 1747 return; 1748 } else if (C == 0) { 1749 if (isCodeCompletionPoint(CurPtr-1)) { 1750 PP->CodeCompleteNaturalLanguage(); 1751 FormTokenWithChars(Result, CurPtr-1, tok::unknown); 1752 return cutOffLexing(); 1753 } 1754 1755 NulCharacter = CurPtr-1; 1756 } 1757 C = getAndAdvanceChar(CurPtr, Result); 1758 } 1759 1760 // If a nul character existed in the character, warn about it. 1761 if (NulCharacter && !isLexingRawMode()) 1762 Diag(NulCharacter, diag::null_in_char); 1763 1764 // Update the location of token as well as BufferPtr. 1765 const char *TokStart = BufferPtr; 1766 FormTokenWithChars(Result, CurPtr, Kind); 1767 Result.setLiteralData(TokStart); 1768 } 1769 1770 /// SkipWhitespace - Efficiently skip over a series of whitespace characters. 1771 /// Update BufferPtr to point to the next non-whitespace character and return. 1772 /// 1773 /// This method forms a token and returns true if KeepWhitespaceMode is enabled. 1774 /// 1775 bool Lexer::SkipWhitespace(Token &Result, const char *CurPtr) { 1776 // Whitespace - Skip it, then return the token after the whitespace. 1777 unsigned char Char = *CurPtr; // Skip consequtive spaces efficiently. 1778 while (1) { 1779 // Skip horizontal whitespace very aggressively. 1780 while (isHorizontalWhitespace(Char)) 1781 Char = *++CurPtr; 1782 1783 // Otherwise if we have something other than whitespace, we're done. 1784 if (Char != '\n' && Char != '\r') 1785 break; 1786 1787 if (ParsingPreprocessorDirective) { 1788 // End of preprocessor directive line, let LexTokenInternal handle this. 1789 BufferPtr = CurPtr; 1790 return false; 1791 } 1792 1793 // ok, but handle newline. 1794 // The returned token is at the start of the line. 1795 Result.setFlag(Token::StartOfLine); 1796 // No leading whitespace seen so far. 1797 Result.clearFlag(Token::LeadingSpace); 1798 Char = *++CurPtr; 1799 } 1800 1801 // If this isn't immediately after a newline, there is leading space. 1802 char PrevChar = CurPtr[-1]; 1803 if (PrevChar != '\n' && PrevChar != '\r') 1804 Result.setFlag(Token::LeadingSpace); 1805 1806 // If the client wants us to return whitespace, return it now. 1807 if (isKeepWhitespaceMode()) { 1808 FormTokenWithChars(Result, CurPtr, tok::unknown); 1809 return true; 1810 } 1811 1812 BufferPtr = CurPtr; 1813 return false; 1814 } 1815 1816 // SkipBCPLComment - We have just read the // characters from input. Skip until 1817 // we find the newline character thats terminate the comment. Then update 1818 /// BufferPtr and return. 1819 /// 1820 /// If we're in KeepCommentMode or any CommentHandler has inserted 1821 /// some tokens, this will store the first token and return true. 1822 bool Lexer::SkipBCPLComment(Token &Result, const char *CurPtr) { 1823 // If BCPL comments aren't explicitly enabled for this language, emit an 1824 // extension warning. 1825 if (!Features.BCPLComment && !isLexingRawMode()) { 1826 Diag(BufferPtr, diag::ext_bcpl_comment); 1827 1828 // Mark them enabled so we only emit one warning for this translation 1829 // unit. 1830 Features.BCPLComment = true; 1831 } 1832 1833 // Scan over the body of the comment. The common case, when scanning, is that 1834 // the comment contains normal ascii characters with nothing interesting in 1835 // them. As such, optimize for this case with the inner loop. 1836 char C; 1837 do { 1838 C = *CurPtr; 1839 // Skip over characters in the fast loop. 1840 while (C != 0 && // Potentially EOF. 1841 C != '\n' && C != '\r') // Newline or DOS-style newline. 1842 C = *++CurPtr; 1843 1844 const char *NextLine = CurPtr; 1845 if (C != 0) { 1846 // We found a newline, see if it's escaped. 1847 const char *EscapePtr = CurPtr-1; 1848 while (isHorizontalWhitespace(*EscapePtr)) // Skip whitespace. 1849 --EscapePtr; 1850 1851 if (*EscapePtr == '\\') // Escaped newline. 1852 CurPtr = EscapePtr; 1853 else if (EscapePtr[0] == '/' && EscapePtr[-1] == '?' && 1854 EscapePtr[-2] == '?') // Trigraph-escaped newline. 1855 CurPtr = EscapePtr-2; 1856 else 1857 break; // This is a newline, we're done. 1858 1859 C = *CurPtr; 1860 } 1861 1862 // Otherwise, this is a hard case. Fall back on getAndAdvanceChar to 1863 // properly decode the character. Read it in raw mode to avoid emitting 1864 // diagnostics about things like trigraphs. If we see an escaped newline, 1865 // we'll handle it below. 1866 const char *OldPtr = CurPtr; 1867 bool OldRawMode = isLexingRawMode(); 1868 LexingRawMode = true; 1869 C = getAndAdvanceChar(CurPtr, Result); 1870 LexingRawMode = OldRawMode; 1871 1872 // If we only read only one character, then no special handling is needed. 1873 // We're done and can skip forward to the newline. 1874 if (C != 0 && CurPtr == OldPtr+1) { 1875 CurPtr = NextLine; 1876 break; 1877 } 1878 1879 // If we read multiple characters, and one of those characters was a \r or 1880 // \n, then we had an escaped newline within the comment. Emit diagnostic 1881 // unless the next line is also a // comment. 1882 if (CurPtr != OldPtr+1 && C != '/' && CurPtr[0] != '/') { 1883 for (; OldPtr != CurPtr; ++OldPtr) 1884 if (OldPtr[0] == '\n' || OldPtr[0] == '\r') { 1885 // Okay, we found a // comment that ends in a newline, if the next 1886 // line is also a // comment, but has spaces, don't emit a diagnostic. 1887 if (isWhitespace(C)) { 1888 const char *ForwardPtr = CurPtr; 1889 while (isWhitespace(*ForwardPtr)) // Skip whitespace. 1890 ++ForwardPtr; 1891 if (ForwardPtr[0] == '/' && ForwardPtr[1] == '/') 1892 break; 1893 } 1894 1895 if (!isLexingRawMode()) 1896 Diag(OldPtr-1, diag::ext_multi_line_bcpl_comment); 1897 break; 1898 } 1899 } 1900 1901 if (CurPtr == BufferEnd+1) { 1902 --CurPtr; 1903 break; 1904 } 1905 1906 if (C == '\0' && isCodeCompletionPoint(CurPtr-1)) { 1907 PP->CodeCompleteNaturalLanguage(); 1908 cutOffLexing(); 1909 return false; 1910 } 1911 1912 } while (C != '\n' && C != '\r'); 1913 1914 // Found but did not consume the newline. Notify comment handlers about the 1915 // comment unless we're in a #if 0 block. 1916 if (PP && !isLexingRawMode() && 1917 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr), 1918 getSourceLocation(CurPtr)))) { 1919 BufferPtr = CurPtr; 1920 return true; // A token has to be returned. 1921 } 1922 1923 // If we are returning comments as tokens, return this comment as a token. 1924 if (inKeepCommentMode()) 1925 return SaveBCPLComment(Result, CurPtr); 1926 1927 // If we are inside a preprocessor directive and we see the end of line, 1928 // return immediately, so that the lexer can return this as an EOD token. 1929 if (ParsingPreprocessorDirective || CurPtr == BufferEnd) { 1930 BufferPtr = CurPtr; 1931 return false; 1932 } 1933 1934 // Otherwise, eat the \n character. We don't care if this is a \n\r or 1935 // \r\n sequence. This is an efficiency hack (because we know the \n can't 1936 // contribute to another token), it isn't needed for correctness. Note that 1937 // this is ok even in KeepWhitespaceMode, because we would have returned the 1938 /// comment above in that mode. 1939 ++CurPtr; 1940 1941 // The next returned token is at the start of the line. 1942 Result.setFlag(Token::StartOfLine); 1943 // No leading whitespace seen so far. 1944 Result.clearFlag(Token::LeadingSpace); 1945 BufferPtr = CurPtr; 1946 return false; 1947 } 1948 1949 /// SaveBCPLComment - If in save-comment mode, package up this BCPL comment in 1950 /// an appropriate way and return it. 1951 bool Lexer::SaveBCPLComment(Token &Result, const char *CurPtr) { 1952 // If we're not in a preprocessor directive, just return the // comment 1953 // directly. 1954 FormTokenWithChars(Result, CurPtr, tok::comment); 1955 1956 if (!ParsingPreprocessorDirective) 1957 return true; 1958 1959 // If this BCPL-style comment is in a macro definition, transmogrify it into 1960 // a C-style block comment. 1961 bool Invalid = false; 1962 std::string Spelling = PP->getSpelling(Result, &Invalid); 1963 if (Invalid) 1964 return true; 1965 1966 assert(Spelling[0] == '/' && Spelling[1] == '/' && "Not bcpl comment?"); 1967 Spelling[1] = '*'; // Change prefix to "/*". 1968 Spelling += "*/"; // add suffix. 1969 1970 Result.setKind(tok::comment); 1971 PP->CreateString(&Spelling[0], Spelling.size(), Result, 1972 Result.getLocation(), Result.getLocation()); 1973 return true; 1974 } 1975 1976 /// isBlockCommentEndOfEscapedNewLine - Return true if the specified newline 1977 /// character (either \n or \r) is part of an escaped newline sequence. Issue a 1978 /// diagnostic if so. We know that the newline is inside of a block comment. 1979 static bool isEndOfBlockCommentWithEscapedNewLine(const char *CurPtr, 1980 Lexer *L) { 1981 assert(CurPtr[0] == '\n' || CurPtr[0] == '\r'); 1982 1983 // Back up off the newline. 1984 --CurPtr; 1985 1986 // If this is a two-character newline sequence, skip the other character. 1987 if (CurPtr[0] == '\n' || CurPtr[0] == '\r') { 1988 // \n\n or \r\r -> not escaped newline. 1989 if (CurPtr[0] == CurPtr[1]) 1990 return false; 1991 // \n\r or \r\n -> skip the newline. 1992 --CurPtr; 1993 } 1994 1995 // If we have horizontal whitespace, skip over it. We allow whitespace 1996 // between the slash and newline. 1997 bool HasSpace = false; 1998 while (isHorizontalWhitespace(*CurPtr) || *CurPtr == 0) { 1999 --CurPtr; 2000 HasSpace = true; 2001 } 2002 2003 // If we have a slash, we know this is an escaped newline. 2004 if (*CurPtr == '\\') { 2005 if (CurPtr[-1] != '*') return false; 2006 } else { 2007 // It isn't a slash, is it the ?? / trigraph? 2008 if (CurPtr[0] != '/' || CurPtr[-1] != '?' || CurPtr[-2] != '?' || 2009 CurPtr[-3] != '*') 2010 return false; 2011 2012 // This is the trigraph ending the comment. Emit a stern warning! 2013 CurPtr -= 2; 2014 2015 // If no trigraphs are enabled, warn that we ignored this trigraph and 2016 // ignore this * character. 2017 if (!L->getFeatures().Trigraphs) { 2018 if (!L->isLexingRawMode()) 2019 L->Diag(CurPtr, diag::trigraph_ignored_block_comment); 2020 return false; 2021 } 2022 if (!L->isLexingRawMode()) 2023 L->Diag(CurPtr, diag::trigraph_ends_block_comment); 2024 } 2025 2026 // Warn about having an escaped newline between the */ characters. 2027 if (!L->isLexingRawMode()) 2028 L->Diag(CurPtr, diag::escaped_newline_block_comment_end); 2029 2030 // If there was space between the backslash and newline, warn about it. 2031 if (HasSpace && !L->isLexingRawMode()) 2032 L->Diag(CurPtr, diag::backslash_newline_space); 2033 2034 return true; 2035 } 2036 2037 #ifdef __SSE2__ 2038 #include <emmintrin.h> 2039 #elif __ALTIVEC__ 2040 #include <altivec.h> 2041 #undef bool 2042 #endif 2043 2044 /// SkipBlockComment - We have just read the /* characters from input. Read 2045 /// until we find the */ characters that terminate the comment. Note that we 2046 /// don't bother decoding trigraphs or escaped newlines in block comments, 2047 /// because they cannot cause the comment to end. The only thing that can 2048 /// happen is the comment could end with an escaped newline between the */ end 2049 /// of comment. 2050 /// 2051 /// If we're in KeepCommentMode or any CommentHandler has inserted 2052 /// some tokens, this will store the first token and return true. 2053 bool Lexer::SkipBlockComment(Token &Result, const char *CurPtr) { 2054 // Scan one character past where we should, looking for a '/' character. Once 2055 // we find it, check to see if it was preceded by a *. This common 2056 // optimization helps people who like to put a lot of * characters in their 2057 // comments. 2058 2059 // The first character we get with newlines and trigraphs skipped to handle 2060 // the degenerate /*/ case below correctly if the * has an escaped newline 2061 // after it. 2062 unsigned CharSize; 2063 unsigned char C = getCharAndSize(CurPtr, CharSize); 2064 CurPtr += CharSize; 2065 if (C == 0 && CurPtr == BufferEnd+1) { 2066 if (!isLexingRawMode()) 2067 Diag(BufferPtr, diag::err_unterminated_block_comment); 2068 --CurPtr; 2069 2070 // KeepWhitespaceMode should return this broken comment as a token. Since 2071 // it isn't a well formed comment, just return it as an 'unknown' token. 2072 if (isKeepWhitespaceMode()) { 2073 FormTokenWithChars(Result, CurPtr, tok::unknown); 2074 return true; 2075 } 2076 2077 BufferPtr = CurPtr; 2078 return false; 2079 } 2080 2081 // Check to see if the first character after the '/*' is another /. If so, 2082 // then this slash does not end the block comment, it is part of it. 2083 if (C == '/') 2084 C = *CurPtr++; 2085 2086 while (1) { 2087 // Skip over all non-interesting characters until we find end of buffer or a 2088 // (probably ending) '/' character. 2089 if (CurPtr + 24 < BufferEnd && 2090 // If there is a code-completion point avoid the fast scan because it 2091 // doesn't check for '\0'. 2092 !(PP && PP->getCodeCompletionFileLoc() == FileLoc)) { 2093 // While not aligned to a 16-byte boundary. 2094 while (C != '/' && ((intptr_t)CurPtr & 0x0F) != 0) 2095 C = *CurPtr++; 2096 2097 if (C == '/') goto FoundSlash; 2098 2099 #ifdef __SSE2__ 2100 __m128i Slashes = _mm_set1_epi8('/'); 2101 while (CurPtr+16 <= BufferEnd) { 2102 int cmp = _mm_movemask_epi8(_mm_cmpeq_epi8(*(__m128i*)CurPtr, Slashes)); 2103 if (cmp != 0) { 2104 // Adjust the pointer to point directly after the first slash. It's 2105 // not necessary to set C here, it will be overwritten at the end of 2106 // the outer loop. 2107 CurPtr += llvm::CountTrailingZeros_32(cmp) + 1; 2108 goto FoundSlash; 2109 } 2110 CurPtr += 16; 2111 } 2112 #elif __ALTIVEC__ 2113 __vector unsigned char Slashes = { 2114 '/', '/', '/', '/', '/', '/', '/', '/', 2115 '/', '/', '/', '/', '/', '/', '/', '/' 2116 }; 2117 while (CurPtr+16 <= BufferEnd && 2118 !vec_any_eq(*(vector unsigned char*)CurPtr, Slashes)) 2119 CurPtr += 16; 2120 #else 2121 // Scan for '/' quickly. Many block comments are very large. 2122 while (CurPtr[0] != '/' && 2123 CurPtr[1] != '/' && 2124 CurPtr[2] != '/' && 2125 CurPtr[3] != '/' && 2126 CurPtr+4 < BufferEnd) { 2127 CurPtr += 4; 2128 } 2129 #endif 2130 2131 // It has to be one of the bytes scanned, increment to it and read one. 2132 C = *CurPtr++; 2133 } 2134 2135 // Loop to scan the remainder. 2136 while (C != '/' && C != '\0') 2137 C = *CurPtr++; 2138 2139 if (C == '/') { 2140 FoundSlash: 2141 if (CurPtr[-2] == '*') // We found the final */. We're done! 2142 break; 2143 2144 if ((CurPtr[-2] == '\n' || CurPtr[-2] == '\r')) { 2145 if (isEndOfBlockCommentWithEscapedNewLine(CurPtr-2, this)) { 2146 // We found the final */, though it had an escaped newline between the 2147 // * and /. We're done! 2148 break; 2149 } 2150 } 2151 if (CurPtr[0] == '*' && CurPtr[1] != '/') { 2152 // If this is a /* inside of the comment, emit a warning. Don't do this 2153 // if this is a /*/, which will end the comment. This misses cases with 2154 // embedded escaped newlines, but oh well. 2155 if (!isLexingRawMode()) 2156 Diag(CurPtr-1, diag::warn_nested_block_comment); 2157 } 2158 } else if (C == 0 && CurPtr == BufferEnd+1) { 2159 if (!isLexingRawMode()) 2160 Diag(BufferPtr, diag::err_unterminated_block_comment); 2161 // Note: the user probably forgot a */. We could continue immediately 2162 // after the /*, but this would involve lexing a lot of what really is the 2163 // comment, which surely would confuse the parser. 2164 --CurPtr; 2165 2166 // KeepWhitespaceMode should return this broken comment as a token. Since 2167 // it isn't a well formed comment, just return it as an 'unknown' token. 2168 if (isKeepWhitespaceMode()) { 2169 FormTokenWithChars(Result, CurPtr, tok::unknown); 2170 return true; 2171 } 2172 2173 BufferPtr = CurPtr; 2174 return false; 2175 } else if (C == '\0' && isCodeCompletionPoint(CurPtr-1)) { 2176 PP->CodeCompleteNaturalLanguage(); 2177 cutOffLexing(); 2178 return false; 2179 } 2180 2181 C = *CurPtr++; 2182 } 2183 2184 // Notify comment handlers about the comment unless we're in a #if 0 block. 2185 if (PP && !isLexingRawMode() && 2186 PP->HandleComment(Result, SourceRange(getSourceLocation(BufferPtr), 2187 getSourceLocation(CurPtr)))) { 2188 BufferPtr = CurPtr; 2189 return true; // A token has to be returned. 2190 } 2191 2192 // If we are returning comments as tokens, return this comment as a token. 2193 if (inKeepCommentMode()) { 2194 FormTokenWithChars(Result, CurPtr, tok::comment); 2195 return true; 2196 } 2197 2198 // It is common for the tokens immediately after a /**/ comment to be 2199 // whitespace. Instead of going through the big switch, handle it 2200 // efficiently now. This is safe even in KeepWhitespaceMode because we would 2201 // have already returned above with the comment as a token. 2202 if (isHorizontalWhitespace(*CurPtr)) { 2203 Result.setFlag(Token::LeadingSpace); 2204 SkipWhitespace(Result, CurPtr+1); 2205 return false; 2206 } 2207 2208 // Otherwise, just return so that the next character will be lexed as a token. 2209 BufferPtr = CurPtr; 2210 Result.setFlag(Token::LeadingSpace); 2211 return false; 2212 } 2213 2214 //===----------------------------------------------------------------------===// 2215 // Primary Lexing Entry Points 2216 //===----------------------------------------------------------------------===// 2217 2218 /// ReadToEndOfLine - Read the rest of the current preprocessor line as an 2219 /// uninterpreted string. This switches the lexer out of directive mode. 2220 std::string Lexer::ReadToEndOfLine() { 2221 assert(ParsingPreprocessorDirective && ParsingFilename == false && 2222 "Must be in a preprocessing directive!"); 2223 std::string Result; 2224 Token Tmp; 2225 2226 // CurPtr - Cache BufferPtr in an automatic variable. 2227 const char *CurPtr = BufferPtr; 2228 while (1) { 2229 char Char = getAndAdvanceChar(CurPtr, Tmp); 2230 switch (Char) { 2231 default: 2232 Result += Char; 2233 break; 2234 case 0: // Null. 2235 // Found end of file? 2236 if (CurPtr-1 != BufferEnd) { 2237 if (isCodeCompletionPoint(CurPtr-1)) { 2238 PP->CodeCompleteNaturalLanguage(); 2239 cutOffLexing(); 2240 return Result; 2241 } 2242 2243 // Nope, normal character, continue. 2244 Result += Char; 2245 break; 2246 } 2247 // FALL THROUGH. 2248 case '\r': 2249 case '\n': 2250 // Okay, we found the end of the line. First, back up past the \0, \r, \n. 2251 assert(CurPtr[-1] == Char && "Trigraphs for newline?"); 2252 BufferPtr = CurPtr-1; 2253 2254 // Next, lex the character, which should handle the EOD transition. 2255 Lex(Tmp); 2256 if (Tmp.is(tok::code_completion)) { 2257 if (PP) 2258 PP->CodeCompleteNaturalLanguage(); 2259 Lex(Tmp); 2260 } 2261 assert(Tmp.is(tok::eod) && "Unexpected token!"); 2262 2263 // Finally, we're done, return the string we found. 2264 return Result; 2265 } 2266 } 2267 } 2268 2269 /// LexEndOfFile - CurPtr points to the end of this file. Handle this 2270 /// condition, reporting diagnostics and handling other edge cases as required. 2271 /// This returns true if Result contains a token, false if PP.Lex should be 2272 /// called again. 2273 bool Lexer::LexEndOfFile(Token &Result, const char *CurPtr) { 2274 // If we hit the end of the file while parsing a preprocessor directive, 2275 // end the preprocessor directive first. The next token returned will 2276 // then be the end of file. 2277 if (ParsingPreprocessorDirective) { 2278 // Done parsing the "line". 2279 ParsingPreprocessorDirective = false; 2280 // Update the location of token as well as BufferPtr. 2281 FormTokenWithChars(Result, CurPtr, tok::eod); 2282 2283 // Restore comment saving mode, in case it was disabled for directive. 2284 SetCommentRetentionState(PP->getCommentRetentionState()); 2285 return true; // Have a token. 2286 } 2287 2288 // If we are in raw mode, return this event as an EOF token. Let the caller 2289 // that put us in raw mode handle the event. 2290 if (isLexingRawMode()) { 2291 Result.startToken(); 2292 BufferPtr = BufferEnd; 2293 FormTokenWithChars(Result, BufferEnd, tok::eof); 2294 return true; 2295 } 2296 2297 // Issue diagnostics for unterminated #if and missing newline. 2298 2299 // If we are in a #if directive, emit an error. 2300 while (!ConditionalStack.empty()) { 2301 if (PP->getCodeCompletionFileLoc() != FileLoc) 2302 PP->Diag(ConditionalStack.back().IfLoc, 2303 diag::err_pp_unterminated_conditional); 2304 ConditionalStack.pop_back(); 2305 } 2306 2307 // C99 5.1.1.2p2: If the file is non-empty and didn't end in a newline, issue 2308 // a pedwarn. 2309 if (CurPtr != BufferStart && (CurPtr[-1] != '\n' && CurPtr[-1] != '\r')) 2310 Diag(BufferEnd, diag::ext_no_newline_eof) 2311 << FixItHint::CreateInsertion(getSourceLocation(BufferEnd), "\n"); 2312 2313 BufferPtr = CurPtr; 2314 2315 // Finally, let the preprocessor handle this. 2316 return PP->HandleEndOfFile(Result); 2317 } 2318 2319 /// isNextPPTokenLParen - Return 1 if the next unexpanded token lexed from 2320 /// the specified lexer will return a tok::l_paren token, 0 if it is something 2321 /// else and 2 if there are no more tokens in the buffer controlled by the 2322 /// lexer. 2323 unsigned Lexer::isNextPPTokenLParen() { 2324 assert(!LexingRawMode && "How can we expand a macro from a skipping buffer?"); 2325 2326 // Switch to 'skipping' mode. This will ensure that we can lex a token 2327 // without emitting diagnostics, disables macro expansion, and will cause EOF 2328 // to return an EOF token instead of popping the include stack. 2329 LexingRawMode = true; 2330 2331 // Save state that can be changed while lexing so that we can restore it. 2332 const char *TmpBufferPtr = BufferPtr; 2333 bool inPPDirectiveMode = ParsingPreprocessorDirective; 2334 2335 Token Tok; 2336 Tok.startToken(); 2337 LexTokenInternal(Tok); 2338 2339 // Restore state that may have changed. 2340 BufferPtr = TmpBufferPtr; 2341 ParsingPreprocessorDirective = inPPDirectiveMode; 2342 2343 // Restore the lexer back to non-skipping mode. 2344 LexingRawMode = false; 2345 2346 if (Tok.is(tok::eof)) 2347 return 2; 2348 return Tok.is(tok::l_paren); 2349 } 2350 2351 /// FindConflictEnd - Find the end of a version control conflict marker. 2352 static const char *FindConflictEnd(const char *CurPtr, const char *BufferEnd, 2353 ConflictMarkerKind CMK) { 2354 const char *Terminator = CMK == CMK_Perforce ? "<<<<\n" : ">>>>>>>"; 2355 size_t TermLen = CMK == CMK_Perforce ? 5 : 7; 2356 StringRef RestOfBuffer(CurPtr+TermLen, BufferEnd-CurPtr-TermLen); 2357 size_t Pos = RestOfBuffer.find(Terminator); 2358 while (Pos != StringRef::npos) { 2359 // Must occur at start of line. 2360 if (RestOfBuffer[Pos-1] != '\r' && 2361 RestOfBuffer[Pos-1] != '\n') { 2362 RestOfBuffer = RestOfBuffer.substr(Pos+TermLen); 2363 Pos = RestOfBuffer.find(Terminator); 2364 continue; 2365 } 2366 return RestOfBuffer.data()+Pos; 2367 } 2368 return 0; 2369 } 2370 2371 /// IsStartOfConflictMarker - If the specified pointer is the start of a version 2372 /// control conflict marker like '<<<<<<<', recognize it as such, emit an error 2373 /// and recover nicely. This returns true if it is a conflict marker and false 2374 /// if not. 2375 bool Lexer::IsStartOfConflictMarker(const char *CurPtr) { 2376 // Only a conflict marker if it starts at the beginning of a line. 2377 if (CurPtr != BufferStart && 2378 CurPtr[-1] != '\n' && CurPtr[-1] != '\r') 2379 return false; 2380 2381 // Check to see if we have <<<<<<< or >>>>. 2382 if ((BufferEnd-CurPtr < 8 || StringRef(CurPtr, 7) != "<<<<<<<") && 2383 (BufferEnd-CurPtr < 6 || StringRef(CurPtr, 5) != ">>>> ")) 2384 return false; 2385 2386 // If we have a situation where we don't care about conflict markers, ignore 2387 // it. 2388 if (CurrentConflictMarkerState || isLexingRawMode()) 2389 return false; 2390 2391 ConflictMarkerKind Kind = *CurPtr == '<' ? CMK_Normal : CMK_Perforce; 2392 2393 // Check to see if there is an ending marker somewhere in the buffer at the 2394 // start of a line to terminate this conflict marker. 2395 if (FindConflictEnd(CurPtr, BufferEnd, Kind)) { 2396 // We found a match. We are really in a conflict marker. 2397 // Diagnose this, and ignore to the end of line. 2398 Diag(CurPtr, diag::err_conflict_marker); 2399 CurrentConflictMarkerState = Kind; 2400 2401 // Skip ahead to the end of line. We know this exists because the 2402 // end-of-conflict marker starts with \r or \n. 2403 while (*CurPtr != '\r' && *CurPtr != '\n') { 2404 assert(CurPtr != BufferEnd && "Didn't find end of line"); 2405 ++CurPtr; 2406 } 2407 BufferPtr = CurPtr; 2408 return true; 2409 } 2410 2411 // No end of conflict marker found. 2412 return false; 2413 } 2414 2415 2416 /// HandleEndOfConflictMarker - If this is a '====' or '||||' or '>>>>', or if 2417 /// it is '<<<<' and the conflict marker started with a '>>>>' marker, then it 2418 /// is the end of a conflict marker. Handle it by ignoring up until the end of 2419 /// the line. This returns true if it is a conflict marker and false if not. 2420 bool Lexer::HandleEndOfConflictMarker(const char *CurPtr) { 2421 // Only a conflict marker if it starts at the beginning of a line. 2422 if (CurPtr != BufferStart && 2423 CurPtr[-1] != '\n' && CurPtr[-1] != '\r') 2424 return false; 2425 2426 // If we have a situation where we don't care about conflict markers, ignore 2427 // it. 2428 if (!CurrentConflictMarkerState || isLexingRawMode()) 2429 return false; 2430 2431 // Check to see if we have the marker (4 characters in a row). 2432 for (unsigned i = 1; i != 4; ++i) 2433 if (CurPtr[i] != CurPtr[0]) 2434 return false; 2435 2436 // If we do have it, search for the end of the conflict marker. This could 2437 // fail if it got skipped with a '#if 0' or something. Note that CurPtr might 2438 // be the end of conflict marker. 2439 if (const char *End = FindConflictEnd(CurPtr, BufferEnd, 2440 CurrentConflictMarkerState)) { 2441 CurPtr = End; 2442 2443 // Skip ahead to the end of line. 2444 while (CurPtr != BufferEnd && *CurPtr != '\r' && *CurPtr != '\n') 2445 ++CurPtr; 2446 2447 BufferPtr = CurPtr; 2448 2449 // No longer in the conflict marker. 2450 CurrentConflictMarkerState = CMK_None; 2451 return true; 2452 } 2453 2454 return false; 2455 } 2456 2457 bool Lexer::isCodeCompletionPoint(const char *CurPtr) const { 2458 if (PP && PP->isCodeCompletionEnabled()) { 2459 SourceLocation Loc = FileLoc.getLocWithOffset(CurPtr-BufferStart); 2460 return Loc == PP->getCodeCompletionLoc(); 2461 } 2462 2463 return false; 2464 } 2465 2466 2467 /// LexTokenInternal - This implements a simple C family lexer. It is an 2468 /// extremely performance critical piece of code. This assumes that the buffer 2469 /// has a null character at the end of the file. This returns a preprocessing 2470 /// token, not a normal token, as such, it is an internal interface. It assumes 2471 /// that the Flags of result have been cleared before calling this. 2472 void Lexer::LexTokenInternal(Token &Result) { 2473 LexNextToken: 2474 // New token, can't need cleaning yet. 2475 Result.clearFlag(Token::NeedsCleaning); 2476 Result.setIdentifierInfo(0); 2477 2478 // CurPtr - Cache BufferPtr in an automatic variable. 2479 const char *CurPtr = BufferPtr; 2480 2481 // Small amounts of horizontal whitespace is very common between tokens. 2482 if ((*CurPtr == ' ') || (*CurPtr == '\t')) { 2483 ++CurPtr; 2484 while ((*CurPtr == ' ') || (*CurPtr == '\t')) 2485 ++CurPtr; 2486 2487 // If we are keeping whitespace and other tokens, just return what we just 2488 // skipped. The next lexer invocation will return the token after the 2489 // whitespace. 2490 if (isKeepWhitespaceMode()) { 2491 FormTokenWithChars(Result, CurPtr, tok::unknown); 2492 return; 2493 } 2494 2495 BufferPtr = CurPtr; 2496 Result.setFlag(Token::LeadingSpace); 2497 } 2498 2499 unsigned SizeTmp, SizeTmp2; // Temporaries for use in cases below. 2500 2501 // Read a character, advancing over it. 2502 char Char = getAndAdvanceChar(CurPtr, Result); 2503 tok::TokenKind Kind; 2504 2505 switch (Char) { 2506 case 0: // Null. 2507 // Found end of file? 2508 if (CurPtr-1 == BufferEnd) { 2509 // Read the PP instance variable into an automatic variable, because 2510 // LexEndOfFile will often delete 'this'. 2511 Preprocessor *PPCache = PP; 2512 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file. 2513 return; // Got a token to return. 2514 assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); 2515 return PPCache->Lex(Result); 2516 } 2517 2518 // Check if we are performing code completion. 2519 if (isCodeCompletionPoint(CurPtr-1)) { 2520 // Return the code-completion token. 2521 Result.startToken(); 2522 FormTokenWithChars(Result, CurPtr, tok::code_completion); 2523 return; 2524 } 2525 2526 if (!isLexingRawMode()) 2527 Diag(CurPtr-1, diag::null_in_file); 2528 Result.setFlag(Token::LeadingSpace); 2529 if (SkipWhitespace(Result, CurPtr)) 2530 return; // KeepWhitespaceMode 2531 2532 goto LexNextToken; // GCC isn't tail call eliminating. 2533 2534 case 26: // DOS & CP/M EOF: "^Z". 2535 // If we're in Microsoft extensions mode, treat this as end of file. 2536 if (Features.MicrosoftExt) { 2537 // Read the PP instance variable into an automatic variable, because 2538 // LexEndOfFile will often delete 'this'. 2539 Preprocessor *PPCache = PP; 2540 if (LexEndOfFile(Result, CurPtr-1)) // Retreat back into the file. 2541 return; // Got a token to return. 2542 assert(PPCache && "Raw buffer::LexEndOfFile should return a token"); 2543 return PPCache->Lex(Result); 2544 } 2545 // If Microsoft extensions are disabled, this is just random garbage. 2546 Kind = tok::unknown; 2547 break; 2548 2549 case '\n': 2550 case '\r': 2551 // If we are inside a preprocessor directive and we see the end of line, 2552 // we know we are done with the directive, so return an EOD token. 2553 if (ParsingPreprocessorDirective) { 2554 // Done parsing the "line". 2555 ParsingPreprocessorDirective = false; 2556 2557 // Restore comment saving mode, in case it was disabled for directive. 2558 SetCommentRetentionState(PP->getCommentRetentionState()); 2559 2560 // Since we consumed a newline, we are back at the start of a line. 2561 IsAtStartOfLine = true; 2562 2563 Kind = tok::eod; 2564 break; 2565 } 2566 // The returned token is at the start of the line. 2567 Result.setFlag(Token::StartOfLine); 2568 // No leading whitespace seen so far. 2569 Result.clearFlag(Token::LeadingSpace); 2570 2571 if (SkipWhitespace(Result, CurPtr)) 2572 return; // KeepWhitespaceMode 2573 goto LexNextToken; // GCC isn't tail call eliminating. 2574 case ' ': 2575 case '\t': 2576 case '\f': 2577 case '\v': 2578 SkipHorizontalWhitespace: 2579 Result.setFlag(Token::LeadingSpace); 2580 if (SkipWhitespace(Result, CurPtr)) 2581 return; // KeepWhitespaceMode 2582 2583 SkipIgnoredUnits: 2584 CurPtr = BufferPtr; 2585 2586 // If the next token is obviously a // or /* */ comment, skip it efficiently 2587 // too (without going through the big switch stmt). 2588 if (CurPtr[0] == '/' && CurPtr[1] == '/' && !inKeepCommentMode() && 2589 Features.BCPLComment && !Features.TraditionalCPP) { 2590 if (SkipBCPLComment(Result, CurPtr+2)) 2591 return; // There is a token to return. 2592 goto SkipIgnoredUnits; 2593 } else if (CurPtr[0] == '/' && CurPtr[1] == '*' && !inKeepCommentMode()) { 2594 if (SkipBlockComment(Result, CurPtr+2)) 2595 return; // There is a token to return. 2596 goto SkipIgnoredUnits; 2597 } else if (isHorizontalWhitespace(*CurPtr)) { 2598 goto SkipHorizontalWhitespace; 2599 } 2600 goto LexNextToken; // GCC isn't tail call eliminating. 2601 2602 // C99 6.4.4.1: Integer Constants. 2603 // C99 6.4.4.2: Floating Constants. 2604 case '0': case '1': case '2': case '3': case '4': 2605 case '5': case '6': case '7': case '8': case '9': 2606 // Notify MIOpt that we read a non-whitespace/non-comment token. 2607 MIOpt.ReadToken(); 2608 return LexNumericConstant(Result, CurPtr); 2609 2610 case 'u': // Identifier (uber) or C++0x UTF-8 or UTF-16 string literal 2611 // Notify MIOpt that we read a non-whitespace/non-comment token. 2612 MIOpt.ReadToken(); 2613 2614 if (Features.CPlusPlus0x) { 2615 Char = getCharAndSize(CurPtr, SizeTmp); 2616 2617 // UTF-16 string literal 2618 if (Char == '"') 2619 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), 2620 tok::utf16_string_literal); 2621 2622 // UTF-16 character constant 2623 if (Char == '\'') 2624 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result), 2625 tok::utf16_char_constant); 2626 2627 // UTF-16 raw string literal 2628 if (Char == 'R' && getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"') 2629 return LexRawStringLiteral(Result, 2630 ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 2631 SizeTmp2, Result), 2632 tok::utf16_string_literal); 2633 2634 if (Char == '8') { 2635 char Char2 = getCharAndSize(CurPtr + SizeTmp, SizeTmp2); 2636 2637 // UTF-8 string literal 2638 if (Char2 == '"') 2639 return LexStringLiteral(Result, 2640 ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 2641 SizeTmp2, Result), 2642 tok::utf8_string_literal); 2643 2644 if (Char2 == 'R') { 2645 unsigned SizeTmp3; 2646 char Char3 = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3); 2647 // UTF-8 raw string literal 2648 if (Char3 == '"') { 2649 return LexRawStringLiteral(Result, 2650 ConsumeChar(ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 2651 SizeTmp2, Result), 2652 SizeTmp3, Result), 2653 tok::utf8_string_literal); 2654 } 2655 } 2656 } 2657 } 2658 2659 // treat u like the start of an identifier. 2660 return LexIdentifier(Result, CurPtr); 2661 2662 case 'U': // Identifier (Uber) or C++0x UTF-32 string literal 2663 // Notify MIOpt that we read a non-whitespace/non-comment token. 2664 MIOpt.ReadToken(); 2665 2666 if (Features.CPlusPlus0x) { 2667 Char = getCharAndSize(CurPtr, SizeTmp); 2668 2669 // UTF-32 string literal 2670 if (Char == '"') 2671 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), 2672 tok::utf32_string_literal); 2673 2674 // UTF-32 character constant 2675 if (Char == '\'') 2676 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result), 2677 tok::utf32_char_constant); 2678 2679 // UTF-32 raw string literal 2680 if (Char == 'R' && getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"') 2681 return LexRawStringLiteral(Result, 2682 ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 2683 SizeTmp2, Result), 2684 tok::utf32_string_literal); 2685 } 2686 2687 // treat U like the start of an identifier. 2688 return LexIdentifier(Result, CurPtr); 2689 2690 case 'R': // Identifier or C++0x raw string literal 2691 // Notify MIOpt that we read a non-whitespace/non-comment token. 2692 MIOpt.ReadToken(); 2693 2694 if (Features.CPlusPlus0x) { 2695 Char = getCharAndSize(CurPtr, SizeTmp); 2696 2697 if (Char == '"') 2698 return LexRawStringLiteral(Result, 2699 ConsumeChar(CurPtr, SizeTmp, Result), 2700 tok::string_literal); 2701 } 2702 2703 // treat R like the start of an identifier. 2704 return LexIdentifier(Result, CurPtr); 2705 2706 case 'L': // Identifier (Loony) or wide literal (L'x' or L"xyz"). 2707 // Notify MIOpt that we read a non-whitespace/non-comment token. 2708 MIOpt.ReadToken(); 2709 Char = getCharAndSize(CurPtr, SizeTmp); 2710 2711 // Wide string literal. 2712 if (Char == '"') 2713 return LexStringLiteral(Result, ConsumeChar(CurPtr, SizeTmp, Result), 2714 tok::wide_string_literal); 2715 2716 // Wide raw string literal. 2717 if (Features.CPlusPlus0x && Char == 'R' && 2718 getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == '"') 2719 return LexRawStringLiteral(Result, 2720 ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 2721 SizeTmp2, Result), 2722 tok::wide_string_literal); 2723 2724 // Wide character constant. 2725 if (Char == '\'') 2726 return LexCharConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result), 2727 tok::wide_char_constant); 2728 // FALL THROUGH, treating L like the start of an identifier. 2729 2730 // C99 6.4.2: Identifiers. 2731 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': case 'G': 2732 case 'H': case 'I': case 'J': case 'K': /*'L'*/case 'M': case 'N': 2733 case 'O': case 'P': case 'Q': /*'R'*/case 'S': case 'T': /*'U'*/ 2734 case 'V': case 'W': case 'X': case 'Y': case 'Z': 2735 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'g': 2736 case 'h': case 'i': case 'j': case 'k': case 'l': case 'm': case 'n': 2737 case 'o': case 'p': case 'q': case 'r': case 's': case 't': /*'u'*/ 2738 case 'v': case 'w': case 'x': case 'y': case 'z': 2739 case '_': 2740 // Notify MIOpt that we read a non-whitespace/non-comment token. 2741 MIOpt.ReadToken(); 2742 return LexIdentifier(Result, CurPtr); 2743 2744 case '$': // $ in identifiers. 2745 if (Features.DollarIdents) { 2746 if (!isLexingRawMode()) 2747 Diag(CurPtr-1, diag::ext_dollar_in_identifier); 2748 // Notify MIOpt that we read a non-whitespace/non-comment token. 2749 MIOpt.ReadToken(); 2750 return LexIdentifier(Result, CurPtr); 2751 } 2752 2753 Kind = tok::unknown; 2754 break; 2755 2756 // C99 6.4.4: Character Constants. 2757 case '\'': 2758 // Notify MIOpt that we read a non-whitespace/non-comment token. 2759 MIOpt.ReadToken(); 2760 return LexCharConstant(Result, CurPtr, tok::char_constant); 2761 2762 // C99 6.4.5: String Literals. 2763 case '"': 2764 // Notify MIOpt that we read a non-whitespace/non-comment token. 2765 MIOpt.ReadToken(); 2766 return LexStringLiteral(Result, CurPtr, tok::string_literal); 2767 2768 // C99 6.4.6: Punctuators. 2769 case '?': 2770 Kind = tok::question; 2771 break; 2772 case '[': 2773 Kind = tok::l_square; 2774 break; 2775 case ']': 2776 Kind = tok::r_square; 2777 break; 2778 case '(': 2779 Kind = tok::l_paren; 2780 break; 2781 case ')': 2782 Kind = tok::r_paren; 2783 break; 2784 case '{': 2785 Kind = tok::l_brace; 2786 break; 2787 case '}': 2788 Kind = tok::r_brace; 2789 break; 2790 case '.': 2791 Char = getCharAndSize(CurPtr, SizeTmp); 2792 if (Char >= '0' && Char <= '9') { 2793 // Notify MIOpt that we read a non-whitespace/non-comment token. 2794 MIOpt.ReadToken(); 2795 2796 return LexNumericConstant(Result, ConsumeChar(CurPtr, SizeTmp, Result)); 2797 } else if (Features.CPlusPlus && Char == '*') { 2798 Kind = tok::periodstar; 2799 CurPtr += SizeTmp; 2800 } else if (Char == '.' && 2801 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '.') { 2802 Kind = tok::ellipsis; 2803 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 2804 SizeTmp2, Result); 2805 } else { 2806 Kind = tok::period; 2807 } 2808 break; 2809 case '&': 2810 Char = getCharAndSize(CurPtr, SizeTmp); 2811 if (Char == '&') { 2812 Kind = tok::ampamp; 2813 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2814 } else if (Char == '=') { 2815 Kind = tok::ampequal; 2816 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2817 } else { 2818 Kind = tok::amp; 2819 } 2820 break; 2821 case '*': 2822 if (getCharAndSize(CurPtr, SizeTmp) == '=') { 2823 Kind = tok::starequal; 2824 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2825 } else { 2826 Kind = tok::star; 2827 } 2828 break; 2829 case '+': 2830 Char = getCharAndSize(CurPtr, SizeTmp); 2831 if (Char == '+') { 2832 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2833 Kind = tok::plusplus; 2834 } else if (Char == '=') { 2835 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2836 Kind = tok::plusequal; 2837 } else { 2838 Kind = tok::plus; 2839 } 2840 break; 2841 case '-': 2842 Char = getCharAndSize(CurPtr, SizeTmp); 2843 if (Char == '-') { // -- 2844 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2845 Kind = tok::minusminus; 2846 } else if (Char == '>' && Features.CPlusPlus && 2847 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == '*') { // C++ ->* 2848 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 2849 SizeTmp2, Result); 2850 Kind = tok::arrowstar; 2851 } else if (Char == '>') { // -> 2852 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2853 Kind = tok::arrow; 2854 } else if (Char == '=') { // -= 2855 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2856 Kind = tok::minusequal; 2857 } else { 2858 Kind = tok::minus; 2859 } 2860 break; 2861 case '~': 2862 Kind = tok::tilde; 2863 break; 2864 case '!': 2865 if (getCharAndSize(CurPtr, SizeTmp) == '=') { 2866 Kind = tok::exclaimequal; 2867 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2868 } else { 2869 Kind = tok::exclaim; 2870 } 2871 break; 2872 case '/': 2873 // 6.4.9: Comments 2874 Char = getCharAndSize(CurPtr, SizeTmp); 2875 if (Char == '/') { // BCPL comment. 2876 // Even if BCPL comments are disabled (e.g. in C89 mode), we generally 2877 // want to lex this as a comment. There is one problem with this though, 2878 // that in one particular corner case, this can change the behavior of the 2879 // resultant program. For example, In "foo //**/ bar", C89 would lex 2880 // this as "foo / bar" and langauges with BCPL comments would lex it as 2881 // "foo". Check to see if the character after the second slash is a '*'. 2882 // If so, we will lex that as a "/" instead of the start of a comment. 2883 // However, we never do this in -traditional-cpp mode. 2884 if ((Features.BCPLComment || 2885 getCharAndSize(CurPtr+SizeTmp, SizeTmp2) != '*') && 2886 !Features.TraditionalCPP) { 2887 if (SkipBCPLComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) 2888 return; // There is a token to return. 2889 2890 // It is common for the tokens immediately after a // comment to be 2891 // whitespace (indentation for the next line). Instead of going through 2892 // the big switch, handle it efficiently now. 2893 goto SkipIgnoredUnits; 2894 } 2895 } 2896 2897 if (Char == '*') { // /**/ comment. 2898 if (SkipBlockComment(Result, ConsumeChar(CurPtr, SizeTmp, Result))) 2899 return; // There is a token to return. 2900 goto LexNextToken; // GCC isn't tail call eliminating. 2901 } 2902 2903 if (Char == '=') { 2904 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2905 Kind = tok::slashequal; 2906 } else { 2907 Kind = tok::slash; 2908 } 2909 break; 2910 case '%': 2911 Char = getCharAndSize(CurPtr, SizeTmp); 2912 if (Char == '=') { 2913 Kind = tok::percentequal; 2914 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2915 } else if (Features.Digraphs && Char == '>') { 2916 Kind = tok::r_brace; // '%>' -> '}' 2917 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2918 } else if (Features.Digraphs && Char == ':') { 2919 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2920 Char = getCharAndSize(CurPtr, SizeTmp); 2921 if (Char == '%' && getCharAndSize(CurPtr+SizeTmp, SizeTmp2) == ':') { 2922 Kind = tok::hashhash; // '%:%:' -> '##' 2923 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 2924 SizeTmp2, Result); 2925 } else if (Char == '@' && Features.MicrosoftExt) {// %:@ -> #@ -> Charize 2926 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2927 if (!isLexingRawMode()) 2928 Diag(BufferPtr, diag::ext_charize_microsoft); 2929 Kind = tok::hashat; 2930 } else { // '%:' -> '#' 2931 // We parsed a # character. If this occurs at the start of the line, 2932 // it's actually the start of a preprocessing directive. Callback to 2933 // the preprocessor to handle it. 2934 // FIXME: -fpreprocessed mode?? 2935 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) { 2936 FormTokenWithChars(Result, CurPtr, tok::hash); 2937 PP->HandleDirective(Result); 2938 2939 // As an optimization, if the preprocessor didn't switch lexers, tail 2940 // recurse. 2941 if (PP->isCurrentLexer(this)) { 2942 // Start a new token. If this is a #include or something, the PP may 2943 // want us starting at the beginning of the line again. If so, set 2944 // the StartOfLine flag and clear LeadingSpace. 2945 if (IsAtStartOfLine) { 2946 Result.setFlag(Token::StartOfLine); 2947 Result.clearFlag(Token::LeadingSpace); 2948 IsAtStartOfLine = false; 2949 } 2950 goto LexNextToken; // GCC isn't tail call eliminating. 2951 } 2952 2953 return PP->Lex(Result); 2954 } 2955 2956 Kind = tok::hash; 2957 } 2958 } else { 2959 Kind = tok::percent; 2960 } 2961 break; 2962 case '<': 2963 Char = getCharAndSize(CurPtr, SizeTmp); 2964 if (ParsingFilename) { 2965 return LexAngledStringLiteral(Result, CurPtr); 2966 } else if (Char == '<') { 2967 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2); 2968 if (After == '=') { 2969 Kind = tok::lesslessequal; 2970 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 2971 SizeTmp2, Result); 2972 } else if (After == '<' && IsStartOfConflictMarker(CurPtr-1)) { 2973 // If this is actually a '<<<<<<<' version control conflict marker, 2974 // recognize it as such and recover nicely. 2975 goto LexNextToken; 2976 } else if (After == '<' && HandleEndOfConflictMarker(CurPtr-1)) { 2977 // If this is '<<<<' and we're in a Perforce-style conflict marker, 2978 // ignore it. 2979 goto LexNextToken; 2980 } else if (Features.CUDA && After == '<') { 2981 Kind = tok::lesslessless; 2982 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 2983 SizeTmp2, Result); 2984 } else { 2985 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2986 Kind = tok::lessless; 2987 } 2988 } else if (Char == '=') { 2989 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 2990 Kind = tok::lessequal; 2991 } else if (Features.Digraphs && Char == ':') { // '<:' -> '[' 2992 if (Features.CPlusPlus0x && 2993 getCharAndSize(CurPtr + SizeTmp, SizeTmp2) == ':') { 2994 // C++0x [lex.pptoken]p3: 2995 // Otherwise, if the next three characters are <:: and the subsequent 2996 // character is neither : nor >, the < is treated as a preprocessor 2997 // token by itself and not as the first character of the alternative 2998 // token <:. 2999 unsigned SizeTmp3; 3000 char After = getCharAndSize(CurPtr + SizeTmp + SizeTmp2, SizeTmp3); 3001 if (After != ':' && After != '>') { 3002 Kind = tok::less; 3003 if (!isLexingRawMode()) 3004 Diag(BufferPtr, diag::warn_cxx98_compat_less_colon_colon); 3005 break; 3006 } 3007 } 3008 3009 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 3010 Kind = tok::l_square; 3011 } else if (Features.Digraphs && Char == '%') { // '<%' -> '{' 3012 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 3013 Kind = tok::l_brace; 3014 } else { 3015 Kind = tok::less; 3016 } 3017 break; 3018 case '>': 3019 Char = getCharAndSize(CurPtr, SizeTmp); 3020 if (Char == '=') { 3021 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 3022 Kind = tok::greaterequal; 3023 } else if (Char == '>') { 3024 char After = getCharAndSize(CurPtr+SizeTmp, SizeTmp2); 3025 if (After == '=') { 3026 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 3027 SizeTmp2, Result); 3028 Kind = tok::greatergreaterequal; 3029 } else if (After == '>' && IsStartOfConflictMarker(CurPtr-1)) { 3030 // If this is actually a '>>>>' conflict marker, recognize it as such 3031 // and recover nicely. 3032 goto LexNextToken; 3033 } else if (After == '>' && HandleEndOfConflictMarker(CurPtr-1)) { 3034 // If this is '>>>>>>>' and we're in a conflict marker, ignore it. 3035 goto LexNextToken; 3036 } else if (Features.CUDA && After == '>') { 3037 Kind = tok::greatergreatergreater; 3038 CurPtr = ConsumeChar(ConsumeChar(CurPtr, SizeTmp, Result), 3039 SizeTmp2, Result); 3040 } else { 3041 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 3042 Kind = tok::greatergreater; 3043 } 3044 3045 } else { 3046 Kind = tok::greater; 3047 } 3048 break; 3049 case '^': 3050 Char = getCharAndSize(CurPtr, SizeTmp); 3051 if (Char == '=') { 3052 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 3053 Kind = tok::caretequal; 3054 } else { 3055 Kind = tok::caret; 3056 } 3057 break; 3058 case '|': 3059 Char = getCharAndSize(CurPtr, SizeTmp); 3060 if (Char == '=') { 3061 Kind = tok::pipeequal; 3062 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 3063 } else if (Char == '|') { 3064 // If this is '|||||||' and we're in a conflict marker, ignore it. 3065 if (CurPtr[1] == '|' && HandleEndOfConflictMarker(CurPtr-1)) 3066 goto LexNextToken; 3067 Kind = tok::pipepipe; 3068 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 3069 } else { 3070 Kind = tok::pipe; 3071 } 3072 break; 3073 case ':': 3074 Char = getCharAndSize(CurPtr, SizeTmp); 3075 if (Features.Digraphs && Char == '>') { 3076 Kind = tok::r_square; // ':>' -> ']' 3077 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 3078 } else if (Features.CPlusPlus && Char == ':') { 3079 Kind = tok::coloncolon; 3080 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 3081 } else { 3082 Kind = tok::colon; 3083 } 3084 break; 3085 case ';': 3086 Kind = tok::semi; 3087 break; 3088 case '=': 3089 Char = getCharAndSize(CurPtr, SizeTmp); 3090 if (Char == '=') { 3091 // If this is '====' and we're in a conflict marker, ignore it. 3092 if (CurPtr[1] == '=' && HandleEndOfConflictMarker(CurPtr-1)) 3093 goto LexNextToken; 3094 3095 Kind = tok::equalequal; 3096 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 3097 } else { 3098 Kind = tok::equal; 3099 } 3100 break; 3101 case ',': 3102 Kind = tok::comma; 3103 break; 3104 case '#': 3105 Char = getCharAndSize(CurPtr, SizeTmp); 3106 if (Char == '#') { 3107 Kind = tok::hashhash; 3108 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 3109 } else if (Char == '@' && Features.MicrosoftExt) { // #@ -> Charize 3110 Kind = tok::hashat; 3111 if (!isLexingRawMode()) 3112 Diag(BufferPtr, diag::ext_charize_microsoft); 3113 CurPtr = ConsumeChar(CurPtr, SizeTmp, Result); 3114 } else { 3115 // We parsed a # character. If this occurs at the start of the line, 3116 // it's actually the start of a preprocessing directive. Callback to 3117 // the preprocessor to handle it. 3118 // FIXME: -fpreprocessed mode?? 3119 if (Result.isAtStartOfLine() && !LexingRawMode && !Is_PragmaLexer) { 3120 FormTokenWithChars(Result, CurPtr, tok::hash); 3121 PP->HandleDirective(Result); 3122 3123 // As an optimization, if the preprocessor didn't switch lexers, tail 3124 // recurse. 3125 if (PP->isCurrentLexer(this)) { 3126 // Start a new token. If this is a #include or something, the PP may 3127 // want us starting at the beginning of the line again. If so, set 3128 // the StartOfLine flag and clear LeadingSpace. 3129 if (IsAtStartOfLine) { 3130 Result.setFlag(Token::StartOfLine); 3131 Result.clearFlag(Token::LeadingSpace); 3132 IsAtStartOfLine = false; 3133 } 3134 goto LexNextToken; // GCC isn't tail call eliminating. 3135 } 3136 return PP->Lex(Result); 3137 } 3138 3139 Kind = tok::hash; 3140 } 3141 break; 3142 3143 case '@': 3144 // Objective C support. 3145 if (CurPtr[-1] == '@' && Features.ObjC1) 3146 Kind = tok::at; 3147 else 3148 Kind = tok::unknown; 3149 break; 3150 3151 case '\\': 3152 // FIXME: UCN's. 3153 // FALL THROUGH. 3154 default: 3155 Kind = tok::unknown; 3156 break; 3157 } 3158 3159 // Notify MIOpt that we read a non-whitespace/non-comment token. 3160 MIOpt.ReadToken(); 3161 3162 // Update the location of token as well as BufferPtr. 3163 FormTokenWithChars(Result, CurPtr, Kind); 3164 } 3165