1 //===--- CommentLexer.cpp -------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "clang/AST/CommentLexer.h" 10 #include "clang/AST/CommentCommandTraits.h" 11 #include "clang/AST/CommentDiagnostic.h" 12 #include "clang/Basic/CharInfo.h" 13 #include "llvm/ADT/StringExtras.h" 14 #include "llvm/ADT/StringSwitch.h" 15 #include "llvm/Support/ConvertUTF.h" 16 #include "llvm/Support/ErrorHandling.h" 17 18 namespace clang { 19 namespace comments { 20 21 void Token::dump(const Lexer &L, const SourceManager &SM) const { 22 llvm::errs() << "comments::Token Kind=" << Kind << " "; 23 Loc.print(llvm::errs(), SM); 24 llvm::errs() << " " << Length << " \"" << L.getSpelling(*this, SM) << "\"\n"; 25 } 26 27 static inline bool isHTMLNamedCharacterReferenceCharacter(char C) { 28 return isLetter(C); 29 } 30 31 static inline bool isHTMLDecimalCharacterReferenceCharacter(char C) { 32 return isDigit(C); 33 } 34 35 static inline bool isHTMLHexCharacterReferenceCharacter(char C) { 36 return isHexDigit(C); 37 } 38 39 static inline StringRef convertCodePointToUTF8( 40 llvm::BumpPtrAllocator &Allocator, 41 unsigned CodePoint) { 42 char *Resolved = Allocator.Allocate<char>(UNI_MAX_UTF8_BYTES_PER_CODE_POINT); 43 char *ResolvedPtr = Resolved; 44 if (llvm::ConvertCodePointToUTF8(CodePoint, ResolvedPtr)) 45 return StringRef(Resolved, ResolvedPtr - Resolved); 46 else 47 return StringRef(); 48 } 49 50 namespace { 51 52 #include "clang/AST/CommentHTMLTags.inc" 53 #include "clang/AST/CommentHTMLNamedCharacterReferences.inc" 54 55 } // end anonymous namespace 56 57 StringRef Lexer::resolveHTMLNamedCharacterReference(StringRef Name) const { 58 // Fast path, first check a few most widely used named character references. 59 return llvm::StringSwitch<StringRef>(Name) 60 .Case("amp", "&") 61 .Case("lt", "<") 62 .Case("gt", ">") 63 .Case("quot", "\"") 64 .Case("apos", "\'") 65 // Slow path. 66 .Default(translateHTMLNamedCharacterReferenceToUTF8(Name)); 67 } 68 69 StringRef Lexer::resolveHTMLDecimalCharacterReference(StringRef Name) const { 70 unsigned CodePoint = 0; 71 for (unsigned i = 0, e = Name.size(); i != e; ++i) { 72 assert(isHTMLDecimalCharacterReferenceCharacter(Name[i])); 73 CodePoint *= 10; 74 CodePoint += Name[i] - '0'; 75 } 76 return convertCodePointToUTF8(Allocator, CodePoint); 77 } 78 79 StringRef Lexer::resolveHTMLHexCharacterReference(StringRef Name) const { 80 unsigned CodePoint = 0; 81 for (unsigned i = 0, e = Name.size(); i != e; ++i) { 82 CodePoint *= 16; 83 const char C = Name[i]; 84 assert(isHTMLHexCharacterReferenceCharacter(C)); 85 CodePoint += llvm::hexDigitValue(C); 86 } 87 return convertCodePointToUTF8(Allocator, CodePoint); 88 } 89 90 void Lexer::skipLineStartingDecorations() { 91 // This function should be called only for C comments 92 assert(CommentState == LCS_InsideCComment); 93 94 if (BufferPtr == CommentEnd) 95 return; 96 97 switch (*BufferPtr) { 98 case ' ': 99 case '\t': 100 case '\f': 101 case '\v': { 102 const char *NewBufferPtr = BufferPtr; 103 NewBufferPtr++; 104 if (NewBufferPtr == CommentEnd) 105 return; 106 107 char C = *NewBufferPtr; 108 while (isHorizontalWhitespace(C)) { 109 NewBufferPtr++; 110 if (NewBufferPtr == CommentEnd) 111 return; 112 C = *NewBufferPtr; 113 } 114 if (C == '*') 115 BufferPtr = NewBufferPtr + 1; 116 break; 117 } 118 case '*': 119 BufferPtr++; 120 break; 121 } 122 } 123 124 namespace { 125 /// Returns pointer to the first newline character in the string. 126 const char *findNewline(const char *BufferPtr, const char *BufferEnd) { 127 for ( ; BufferPtr != BufferEnd; ++BufferPtr) { 128 if (isVerticalWhitespace(*BufferPtr)) 129 return BufferPtr; 130 } 131 return BufferEnd; 132 } 133 134 const char *skipNewline(const char *BufferPtr, const char *BufferEnd) { 135 if (BufferPtr == BufferEnd) 136 return BufferPtr; 137 138 if (*BufferPtr == '\n') 139 BufferPtr++; 140 else { 141 assert(*BufferPtr == '\r'); 142 BufferPtr++; 143 if (BufferPtr != BufferEnd && *BufferPtr == '\n') 144 BufferPtr++; 145 } 146 return BufferPtr; 147 } 148 149 const char *skipNamedCharacterReference(const char *BufferPtr, 150 const char *BufferEnd) { 151 for ( ; BufferPtr != BufferEnd; ++BufferPtr) { 152 if (!isHTMLNamedCharacterReferenceCharacter(*BufferPtr)) 153 return BufferPtr; 154 } 155 return BufferEnd; 156 } 157 158 const char *skipDecimalCharacterReference(const char *BufferPtr, 159 const char *BufferEnd) { 160 for ( ; BufferPtr != BufferEnd; ++BufferPtr) { 161 if (!isHTMLDecimalCharacterReferenceCharacter(*BufferPtr)) 162 return BufferPtr; 163 } 164 return BufferEnd; 165 } 166 167 const char *skipHexCharacterReference(const char *BufferPtr, 168 const char *BufferEnd) { 169 for ( ; BufferPtr != BufferEnd; ++BufferPtr) { 170 if (!isHTMLHexCharacterReferenceCharacter(*BufferPtr)) 171 return BufferPtr; 172 } 173 return BufferEnd; 174 } 175 176 bool isHTMLIdentifierStartingCharacter(char C) { 177 return isLetter(C); 178 } 179 180 bool isHTMLIdentifierCharacter(char C) { 181 return isAlphanumeric(C); 182 } 183 184 const char *skipHTMLIdentifier(const char *BufferPtr, const char *BufferEnd) { 185 for ( ; BufferPtr != BufferEnd; ++BufferPtr) { 186 if (!isHTMLIdentifierCharacter(*BufferPtr)) 187 return BufferPtr; 188 } 189 return BufferEnd; 190 } 191 192 /// Skip HTML string quoted in single or double quotes. Escaping quotes inside 193 /// string allowed. 194 /// 195 /// Returns pointer to closing quote. 196 const char *skipHTMLQuotedString(const char *BufferPtr, const char *BufferEnd) 197 { 198 const char Quote = *BufferPtr; 199 assert(Quote == '\"' || Quote == '\''); 200 201 BufferPtr++; 202 for ( ; BufferPtr != BufferEnd; ++BufferPtr) { 203 const char C = *BufferPtr; 204 if (C == Quote && BufferPtr[-1] != '\\') 205 return BufferPtr; 206 } 207 return BufferEnd; 208 } 209 210 const char *skipWhitespace(const char *BufferPtr, const char *BufferEnd) { 211 for ( ; BufferPtr != BufferEnd; ++BufferPtr) { 212 if (!isWhitespace(*BufferPtr)) 213 return BufferPtr; 214 } 215 return BufferEnd; 216 } 217 218 bool isWhitespace(const char *BufferPtr, const char *BufferEnd) { 219 return skipWhitespace(BufferPtr, BufferEnd) == BufferEnd; 220 } 221 222 bool isCommandNameStartCharacter(char C) { 223 return isLetter(C); 224 } 225 226 bool isCommandNameCharacter(char C) { 227 return isAlphanumeric(C); 228 } 229 230 const char *skipCommandName(const char *BufferPtr, const char *BufferEnd) { 231 for ( ; BufferPtr != BufferEnd; ++BufferPtr) { 232 if (!isCommandNameCharacter(*BufferPtr)) 233 return BufferPtr; 234 } 235 return BufferEnd; 236 } 237 238 /// Return the one past end pointer for BCPL comments. 239 /// Handles newlines escaped with backslash or trigraph for backslahs. 240 const char *findBCPLCommentEnd(const char *BufferPtr, const char *BufferEnd) { 241 const char *CurPtr = BufferPtr; 242 while (CurPtr != BufferEnd) { 243 while (!isVerticalWhitespace(*CurPtr)) { 244 CurPtr++; 245 if (CurPtr == BufferEnd) 246 return BufferEnd; 247 } 248 // We found a newline, check if it is escaped. 249 const char *EscapePtr = CurPtr - 1; 250 while(isHorizontalWhitespace(*EscapePtr)) 251 EscapePtr--; 252 253 if (*EscapePtr == '\\' || 254 (EscapePtr - 2 >= BufferPtr && EscapePtr[0] == '/' && 255 EscapePtr[-1] == '?' && EscapePtr[-2] == '?')) { 256 // We found an escaped newline. 257 CurPtr = skipNewline(CurPtr, BufferEnd); 258 } else 259 return CurPtr; // Not an escaped newline. 260 } 261 return BufferEnd; 262 } 263 264 /// Return the one past end pointer for C comments. 265 /// Very dumb, does not handle escaped newlines or trigraphs. 266 const char *findCCommentEnd(const char *BufferPtr, const char *BufferEnd) { 267 for ( ; BufferPtr != BufferEnd; ++BufferPtr) { 268 if (*BufferPtr == '*') { 269 assert(BufferPtr + 1 != BufferEnd); 270 if (*(BufferPtr + 1) == '/') 271 return BufferPtr; 272 } 273 } 274 llvm_unreachable("buffer end hit before '*/' was seen"); 275 } 276 277 } // end anonymous namespace 278 279 void Lexer::formTokenWithChars(Token &Result, const char *TokEnd, 280 tok::TokenKind Kind) { 281 const unsigned TokLen = TokEnd - BufferPtr; 282 Result.setLocation(getSourceLocation(BufferPtr)); 283 Result.setKind(Kind); 284 Result.setLength(TokLen); 285 #ifndef NDEBUG 286 Result.TextPtr = "<UNSET>"; 287 Result.IntVal = 7; 288 #endif 289 BufferPtr = TokEnd; 290 } 291 292 void Lexer::lexCommentText(Token &T) { 293 assert(CommentState == LCS_InsideBCPLComment || 294 CommentState == LCS_InsideCComment); 295 296 // Handles lexing non-command text, i.e. text and newline. 297 auto HandleNonCommandToken = [&]() -> void { 298 assert(State == LS_Normal); 299 300 const char *TokenPtr = BufferPtr; 301 assert(TokenPtr < CommentEnd); 302 switch (*TokenPtr) { 303 case '\n': 304 case '\r': 305 TokenPtr = skipNewline(TokenPtr, CommentEnd); 306 formTokenWithChars(T, TokenPtr, tok::newline); 307 308 if (CommentState == LCS_InsideCComment) 309 skipLineStartingDecorations(); 310 return; 311 312 default: { 313 StringRef TokStartSymbols = ParseCommands ? "\n\r\\@&<" : "\n\r"; 314 size_t End = StringRef(TokenPtr, CommentEnd - TokenPtr) 315 .find_first_of(TokStartSymbols); 316 if (End != StringRef::npos) 317 TokenPtr += End; 318 else 319 TokenPtr = CommentEnd; 320 formTextToken(T, TokenPtr); 321 return; 322 } 323 } 324 }; 325 326 if (!ParseCommands) 327 return HandleNonCommandToken(); 328 329 switch (State) { 330 case LS_Normal: 331 break; 332 case LS_VerbatimBlockFirstLine: 333 lexVerbatimBlockFirstLine(T); 334 return; 335 case LS_VerbatimBlockBody: 336 lexVerbatimBlockBody(T); 337 return; 338 case LS_VerbatimLineText: 339 lexVerbatimLineText(T); 340 return; 341 case LS_HTMLStartTag: 342 lexHTMLStartTag(T); 343 return; 344 case LS_HTMLEndTag: 345 lexHTMLEndTag(T); 346 return; 347 } 348 349 assert(State == LS_Normal); 350 const char *TokenPtr = BufferPtr; 351 assert(TokenPtr < CommentEnd); 352 switch(*TokenPtr) { 353 case '\\': 354 case '@': { 355 // Commands that start with a backslash and commands that start with 356 // 'at' have equivalent semantics. But we keep information about the 357 // exact syntax in AST for comments. 358 tok::TokenKind CommandKind = 359 (*TokenPtr == '@') ? tok::at_command : tok::backslash_command; 360 TokenPtr++; 361 if (TokenPtr == CommentEnd) { 362 formTextToken(T, TokenPtr); 363 return; 364 } 365 char C = *TokenPtr; 366 switch (C) { 367 default: 368 break; 369 370 case '\\': case '@': case '&': case '$': 371 case '#': case '<': case '>': case '%': 372 case '\"': case '.': case ':': 373 // This is one of \\ \@ \& \$ etc escape sequences. 374 TokenPtr++; 375 if (C == ':' && TokenPtr != CommentEnd && *TokenPtr == ':') { 376 // This is the \:: escape sequence. 377 TokenPtr++; 378 } 379 StringRef UnescapedText(BufferPtr + 1, TokenPtr - (BufferPtr + 1)); 380 formTokenWithChars(T, TokenPtr, tok::text); 381 T.setText(UnescapedText); 382 return; 383 } 384 385 // Don't make zero-length commands. 386 if (!isCommandNameStartCharacter(*TokenPtr)) { 387 formTextToken(T, TokenPtr); 388 return; 389 } 390 391 TokenPtr = skipCommandName(TokenPtr, CommentEnd); 392 unsigned Length = TokenPtr - (BufferPtr + 1); 393 394 // Hardcoded support for lexing LaTeX formula commands 395 // \f$ \f( \f) \f[ \f] \f{ \f} as a single command. 396 if (Length == 1 && TokenPtr[-1] == 'f' && TokenPtr != CommentEnd) { 397 C = *TokenPtr; 398 if (C == '$' || C == '(' || C == ')' || C == '[' || C == ']' || 399 C == '{' || C == '}') { 400 TokenPtr++; 401 Length++; 402 } 403 } 404 405 StringRef CommandName(BufferPtr + 1, Length); 406 407 const CommandInfo *Info = Traits.getCommandInfoOrNULL(CommandName); 408 if (!Info) { 409 if ((Info = Traits.getTypoCorrectCommandInfo(CommandName))) { 410 StringRef CorrectedName = Info->Name; 411 SourceLocation Loc = getSourceLocation(BufferPtr); 412 SourceLocation EndLoc = getSourceLocation(TokenPtr); 413 SourceRange FullRange = SourceRange(Loc, EndLoc); 414 SourceRange CommandRange(Loc.getLocWithOffset(1), EndLoc); 415 Diag(Loc, diag::warn_correct_comment_command_name) 416 << FullRange << CommandName << CorrectedName 417 << FixItHint::CreateReplacement(CommandRange, CorrectedName); 418 } else { 419 formTokenWithChars(T, TokenPtr, tok::unknown_command); 420 T.setUnknownCommandName(CommandName); 421 Diag(T.getLocation(), diag::warn_unknown_comment_command_name) 422 << SourceRange(T.getLocation(), T.getEndLocation()); 423 return; 424 } 425 } 426 if (Info->IsVerbatimBlockCommand) { 427 setupAndLexVerbatimBlock(T, TokenPtr, *BufferPtr, Info); 428 return; 429 } 430 if (Info->IsVerbatimLineCommand) { 431 setupAndLexVerbatimLine(T, TokenPtr, Info); 432 return; 433 } 434 formTokenWithChars(T, TokenPtr, CommandKind); 435 T.setCommandID(Info->getID()); 436 return; 437 } 438 439 case '&': 440 lexHTMLCharacterReference(T); 441 return; 442 443 case '<': { 444 TokenPtr++; 445 if (TokenPtr == CommentEnd) { 446 formTextToken(T, TokenPtr); 447 return; 448 } 449 const char C = *TokenPtr; 450 if (isHTMLIdentifierStartingCharacter(C)) 451 setupAndLexHTMLStartTag(T); 452 else if (C == '/') 453 setupAndLexHTMLEndTag(T); 454 else 455 formTextToken(T, TokenPtr); 456 return; 457 } 458 459 default: 460 return HandleNonCommandToken(); 461 } 462 } 463 464 void Lexer::setupAndLexVerbatimBlock(Token &T, 465 const char *TextBegin, 466 char Marker, const CommandInfo *Info) { 467 assert(Info->IsVerbatimBlockCommand); 468 469 VerbatimBlockEndCommandName.clear(); 470 VerbatimBlockEndCommandName.append(Marker == '\\' ? "\\" : "@"); 471 VerbatimBlockEndCommandName.append(Info->EndCommandName); 472 473 formTokenWithChars(T, TextBegin, tok::verbatim_block_begin); 474 T.setVerbatimBlockID(Info->getID()); 475 476 // If there is a newline following the verbatim opening command, skip the 477 // newline so that we don't create an tok::verbatim_block_line with empty 478 // text content. 479 if (BufferPtr != CommentEnd && 480 isVerticalWhitespace(*BufferPtr)) { 481 BufferPtr = skipNewline(BufferPtr, CommentEnd); 482 State = LS_VerbatimBlockBody; 483 return; 484 } 485 486 State = LS_VerbatimBlockFirstLine; 487 } 488 489 void Lexer::lexVerbatimBlockFirstLine(Token &T) { 490 again: 491 assert(BufferPtr < CommentEnd); 492 493 // FIXME: It would be better to scan the text once, finding either the block 494 // end command or newline. 495 // 496 // Extract current line. 497 const char *Newline = findNewline(BufferPtr, CommentEnd); 498 StringRef Line(BufferPtr, Newline - BufferPtr); 499 500 // Look for end command in current line. 501 size_t Pos = Line.find(VerbatimBlockEndCommandName); 502 const char *TextEnd; 503 const char *NextLine; 504 if (Pos == StringRef::npos) { 505 // Current line is completely verbatim. 506 TextEnd = Newline; 507 NextLine = skipNewline(Newline, CommentEnd); 508 } else if (Pos == 0) { 509 // Current line contains just an end command. 510 const char *End = BufferPtr + VerbatimBlockEndCommandName.size(); 511 StringRef Name(BufferPtr + 1, End - (BufferPtr + 1)); 512 formTokenWithChars(T, End, tok::verbatim_block_end); 513 T.setVerbatimBlockID(Traits.getCommandInfo(Name)->getID()); 514 State = LS_Normal; 515 return; 516 } else { 517 // There is some text, followed by end command. Extract text first. 518 TextEnd = BufferPtr + Pos; 519 NextLine = TextEnd; 520 // If there is only whitespace before end command, skip whitespace. 521 if (isWhitespace(BufferPtr, TextEnd)) { 522 BufferPtr = TextEnd; 523 goto again; 524 } 525 } 526 527 StringRef Text(BufferPtr, TextEnd - BufferPtr); 528 formTokenWithChars(T, NextLine, tok::verbatim_block_line); 529 T.setVerbatimBlockText(Text); 530 531 State = LS_VerbatimBlockBody; 532 } 533 534 void Lexer::lexVerbatimBlockBody(Token &T) { 535 assert(State == LS_VerbatimBlockBody); 536 537 if (CommentState == LCS_InsideCComment) 538 skipLineStartingDecorations(); 539 540 if (BufferPtr == CommentEnd) { 541 formTokenWithChars(T, BufferPtr, tok::verbatim_block_line); 542 T.setVerbatimBlockText(""); 543 return; 544 } 545 546 lexVerbatimBlockFirstLine(T); 547 } 548 549 void Lexer::setupAndLexVerbatimLine(Token &T, const char *TextBegin, 550 const CommandInfo *Info) { 551 assert(Info->IsVerbatimLineCommand); 552 formTokenWithChars(T, TextBegin, tok::verbatim_line_name); 553 T.setVerbatimLineID(Info->getID()); 554 555 State = LS_VerbatimLineText; 556 } 557 558 void Lexer::lexVerbatimLineText(Token &T) { 559 assert(State == LS_VerbatimLineText); 560 561 // Extract current line. 562 const char *Newline = findNewline(BufferPtr, CommentEnd); 563 StringRef Text(BufferPtr, Newline - BufferPtr); 564 formTokenWithChars(T, Newline, tok::verbatim_line_text); 565 T.setVerbatimLineText(Text); 566 567 State = LS_Normal; 568 } 569 570 void Lexer::lexHTMLCharacterReference(Token &T) { 571 const char *TokenPtr = BufferPtr; 572 assert(*TokenPtr == '&'); 573 TokenPtr++; 574 if (TokenPtr == CommentEnd) { 575 formTextToken(T, TokenPtr); 576 return; 577 } 578 const char *NamePtr; 579 bool isNamed = false; 580 bool isDecimal = false; 581 char C = *TokenPtr; 582 if (isHTMLNamedCharacterReferenceCharacter(C)) { 583 NamePtr = TokenPtr; 584 TokenPtr = skipNamedCharacterReference(TokenPtr, CommentEnd); 585 isNamed = true; 586 } else if (C == '#') { 587 TokenPtr++; 588 if (TokenPtr == CommentEnd) { 589 formTextToken(T, TokenPtr); 590 return; 591 } 592 C = *TokenPtr; 593 if (isHTMLDecimalCharacterReferenceCharacter(C)) { 594 NamePtr = TokenPtr; 595 TokenPtr = skipDecimalCharacterReference(TokenPtr, CommentEnd); 596 isDecimal = true; 597 } else if (C == 'x' || C == 'X') { 598 TokenPtr++; 599 NamePtr = TokenPtr; 600 TokenPtr = skipHexCharacterReference(TokenPtr, CommentEnd); 601 } else { 602 formTextToken(T, TokenPtr); 603 return; 604 } 605 } else { 606 formTextToken(T, TokenPtr); 607 return; 608 } 609 if (NamePtr == TokenPtr || TokenPtr == CommentEnd || 610 *TokenPtr != ';') { 611 formTextToken(T, TokenPtr); 612 return; 613 } 614 StringRef Name(NamePtr, TokenPtr - NamePtr); 615 TokenPtr++; // Skip semicolon. 616 StringRef Resolved; 617 if (isNamed) 618 Resolved = resolveHTMLNamedCharacterReference(Name); 619 else if (isDecimal) 620 Resolved = resolveHTMLDecimalCharacterReference(Name); 621 else 622 Resolved = resolveHTMLHexCharacterReference(Name); 623 624 if (Resolved.empty()) { 625 formTextToken(T, TokenPtr); 626 return; 627 } 628 formTokenWithChars(T, TokenPtr, tok::text); 629 T.setText(Resolved); 630 } 631 632 void Lexer::setupAndLexHTMLStartTag(Token &T) { 633 assert(BufferPtr[0] == '<' && 634 isHTMLIdentifierStartingCharacter(BufferPtr[1])); 635 const char *TagNameEnd = skipHTMLIdentifier(BufferPtr + 2, CommentEnd); 636 StringRef Name(BufferPtr + 1, TagNameEnd - (BufferPtr + 1)); 637 if (!isHTMLTagName(Name)) { 638 formTextToken(T, TagNameEnd); 639 return; 640 } 641 642 formTokenWithChars(T, TagNameEnd, tok::html_start_tag); 643 T.setHTMLTagStartName(Name); 644 645 BufferPtr = skipWhitespace(BufferPtr, CommentEnd); 646 647 const char C = *BufferPtr; 648 if (BufferPtr != CommentEnd && 649 (C == '>' || C == '/' || isHTMLIdentifierStartingCharacter(C))) 650 State = LS_HTMLStartTag; 651 } 652 653 void Lexer::lexHTMLStartTag(Token &T) { 654 assert(State == LS_HTMLStartTag); 655 656 const char *TokenPtr = BufferPtr; 657 char C = *TokenPtr; 658 if (isHTMLIdentifierCharacter(C)) { 659 TokenPtr = skipHTMLIdentifier(TokenPtr, CommentEnd); 660 StringRef Ident(BufferPtr, TokenPtr - BufferPtr); 661 formTokenWithChars(T, TokenPtr, tok::html_ident); 662 T.setHTMLIdent(Ident); 663 } else { 664 switch (C) { 665 case '=': 666 TokenPtr++; 667 formTokenWithChars(T, TokenPtr, tok::html_equals); 668 break; 669 case '\"': 670 case '\'': { 671 const char *OpenQuote = TokenPtr; 672 TokenPtr = skipHTMLQuotedString(TokenPtr, CommentEnd); 673 const char *ClosingQuote = TokenPtr; 674 if (TokenPtr != CommentEnd) // Skip closing quote. 675 TokenPtr++; 676 formTokenWithChars(T, TokenPtr, tok::html_quoted_string); 677 T.setHTMLQuotedString(StringRef(OpenQuote + 1, 678 ClosingQuote - (OpenQuote + 1))); 679 break; 680 } 681 case '>': 682 TokenPtr++; 683 formTokenWithChars(T, TokenPtr, tok::html_greater); 684 State = LS_Normal; 685 return; 686 case '/': 687 TokenPtr++; 688 if (TokenPtr != CommentEnd && *TokenPtr == '>') { 689 TokenPtr++; 690 formTokenWithChars(T, TokenPtr, tok::html_slash_greater); 691 } else 692 formTextToken(T, TokenPtr); 693 694 State = LS_Normal; 695 return; 696 } 697 } 698 699 // Now look ahead and return to normal state if we don't see any HTML tokens 700 // ahead. 701 BufferPtr = skipWhitespace(BufferPtr, CommentEnd); 702 if (BufferPtr == CommentEnd) { 703 State = LS_Normal; 704 return; 705 } 706 707 C = *BufferPtr; 708 if (!isHTMLIdentifierStartingCharacter(C) && 709 C != '=' && C != '\"' && C != '\'' && C != '>') { 710 State = LS_Normal; 711 return; 712 } 713 } 714 715 void Lexer::setupAndLexHTMLEndTag(Token &T) { 716 assert(BufferPtr[0] == '<' && BufferPtr[1] == '/'); 717 718 const char *TagNameBegin = skipWhitespace(BufferPtr + 2, CommentEnd); 719 const char *TagNameEnd = skipHTMLIdentifier(TagNameBegin, CommentEnd); 720 StringRef Name(TagNameBegin, TagNameEnd - TagNameBegin); 721 if (!isHTMLTagName(Name)) { 722 formTextToken(T, TagNameEnd); 723 return; 724 } 725 726 const char *End = skipWhitespace(TagNameEnd, CommentEnd); 727 728 formTokenWithChars(T, End, tok::html_end_tag); 729 T.setHTMLTagEndName(Name); 730 731 if (BufferPtr != CommentEnd && *BufferPtr == '>') 732 State = LS_HTMLEndTag; 733 } 734 735 void Lexer::lexHTMLEndTag(Token &T) { 736 assert(BufferPtr != CommentEnd && *BufferPtr == '>'); 737 738 formTokenWithChars(T, BufferPtr + 1, tok::html_greater); 739 State = LS_Normal; 740 } 741 742 Lexer::Lexer(llvm::BumpPtrAllocator &Allocator, DiagnosticsEngine &Diags, 743 const CommandTraits &Traits, SourceLocation FileLoc, 744 const char *BufferStart, const char *BufferEnd, bool ParseCommands) 745 : Allocator(Allocator), Diags(Diags), Traits(Traits), 746 BufferStart(BufferStart), BufferEnd(BufferEnd), BufferPtr(BufferStart), 747 FileLoc(FileLoc), ParseCommands(ParseCommands), 748 CommentState(LCS_BeforeComment), State(LS_Normal) {} 749 750 void Lexer::lex(Token &T) { 751 again: 752 switch (CommentState) { 753 case LCS_BeforeComment: 754 if (BufferPtr == BufferEnd) { 755 formTokenWithChars(T, BufferPtr, tok::eof); 756 return; 757 } 758 759 assert(*BufferPtr == '/'); 760 BufferPtr++; // Skip first slash. 761 switch(*BufferPtr) { 762 case '/': { // BCPL comment. 763 BufferPtr++; // Skip second slash. 764 765 if (BufferPtr != BufferEnd) { 766 // Skip Doxygen magic marker, if it is present. 767 // It might be missing because of a typo //< or /*<, or because we 768 // merged this non-Doxygen comment into a bunch of Doxygen comments 769 // around it: /** ... */ /* ... */ /** ... */ 770 const char C = *BufferPtr; 771 if (C == '/' || C == '!') 772 BufferPtr++; 773 } 774 775 // Skip less-than symbol that marks trailing comments. 776 // Skip it even if the comment is not a Doxygen one, because //< and /*< 777 // are frequent typos. 778 if (BufferPtr != BufferEnd && *BufferPtr == '<') 779 BufferPtr++; 780 781 CommentState = LCS_InsideBCPLComment; 782 if (State != LS_VerbatimBlockBody && State != LS_VerbatimBlockFirstLine) 783 State = LS_Normal; 784 CommentEnd = findBCPLCommentEnd(BufferPtr, BufferEnd); 785 goto again; 786 } 787 case '*': { // C comment. 788 BufferPtr++; // Skip star. 789 790 // Skip Doxygen magic marker. 791 const char C = *BufferPtr; 792 if ((C == '*' && *(BufferPtr + 1) != '/') || C == '!') 793 BufferPtr++; 794 795 // Skip less-than symbol that marks trailing comments. 796 if (BufferPtr != BufferEnd && *BufferPtr == '<') 797 BufferPtr++; 798 799 CommentState = LCS_InsideCComment; 800 State = LS_Normal; 801 CommentEnd = findCCommentEnd(BufferPtr, BufferEnd); 802 goto again; 803 } 804 default: 805 llvm_unreachable("second character of comment should be '/' or '*'"); 806 } 807 808 case LCS_BetweenComments: { 809 // Consecutive comments are extracted only if there is only whitespace 810 // between them. So we can search for the start of the next comment. 811 const char *EndWhitespace = BufferPtr; 812 while(EndWhitespace != BufferEnd && *EndWhitespace != '/') 813 EndWhitespace++; 814 815 // Turn any whitespace between comments (and there is only whitespace 816 // between them -- guaranteed by comment extraction) into a newline. We 817 // have two newlines between C comments in total (first one was synthesized 818 // after a comment). 819 formTokenWithChars(T, EndWhitespace, tok::newline); 820 821 CommentState = LCS_BeforeComment; 822 break; 823 } 824 825 case LCS_InsideBCPLComment: 826 case LCS_InsideCComment: 827 if (BufferPtr != CommentEnd) { 828 lexCommentText(T); 829 break; 830 } else { 831 // Skip C comment closing sequence. 832 if (CommentState == LCS_InsideCComment) { 833 assert(BufferPtr[0] == '*' && BufferPtr[1] == '/'); 834 BufferPtr += 2; 835 assert(BufferPtr <= BufferEnd); 836 837 // Synthenize newline just after the C comment, regardless if there is 838 // actually a newline. 839 formTokenWithChars(T, BufferPtr, tok::newline); 840 841 CommentState = LCS_BetweenComments; 842 break; 843 } else { 844 // Don't synthesized a newline after BCPL comment. 845 CommentState = LCS_BetweenComments; 846 goto again; 847 } 848 } 849 } 850 } 851 852 StringRef Lexer::getSpelling(const Token &Tok, 853 const SourceManager &SourceMgr) const { 854 SourceLocation Loc = Tok.getLocation(); 855 std::pair<FileID, unsigned> LocInfo = SourceMgr.getDecomposedLoc(Loc); 856 857 bool InvalidTemp = false; 858 StringRef File = SourceMgr.getBufferData(LocInfo.first, &InvalidTemp); 859 if (InvalidTemp) 860 return StringRef(); 861 862 const char *Begin = File.data() + LocInfo.second; 863 return StringRef(Begin, Tok.getLength()); 864 } 865 866 } // end namespace comments 867 } // end namespace clang 868