1 #include "clang/AST/CommentLexer.h"
2 #include "clang/AST/CommentCommandTraits.h"
3 #include "clang/Basic/ConvertUTF.h"
4 #include "llvm/ADT/StringSwitch.h"
5 #include "llvm/Support/ErrorHandling.h"
10 void Token::dump(const Lexer &L, const SourceManager &SM) const {
11 llvm::errs() << "comments::Token Kind=" << Kind << " ";
13 llvm::errs() << " " << Length << " \"" << L.getSpelling(*this, SM) << "\"\n";
17 bool isHTMLNamedCharacterReferenceCharacter(char C) {
18 return (C >= 'a' && C <= 'z') ||
19 (C >= 'A' && C <= 'Z');
22 bool isHTMLDecimalCharacterReferenceCharacter(char C) {
23 return C >= '0' && C <= '9';
26 bool isHTMLHexCharacterReferenceCharacter(char C) {
27 return (C >= '0' && C <= '9') ||
28 (C >= 'a' && C <= 'f') ||
29 (C >= 'A' && C <= 'F');
31 } // unnamed namespace
33 StringRef Lexer::resolveHTMLNamedCharacterReference(StringRef Name) const {
34 return llvm::StringSwitch<StringRef>(Name)
43 StringRef Lexer::resolveHTMLDecimalCharacterReference(StringRef Name) const {
44 unsigned CodePoint = 0;
45 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
46 assert(isHTMLDecimalCharacterReferenceCharacter(Name[i]));
48 CodePoint += Name[i] - '0';
51 char *Resolved = Allocator.Allocate<char>(UNI_MAX_UTF8_BYTES_PER_CODE_POINT);
52 char *ResolvedPtr = Resolved;
53 if (ConvertCodePointToUTF8(CodePoint, ResolvedPtr))
54 return StringRef(Resolved, ResolvedPtr - Resolved);
59 StringRef Lexer::resolveHTMLHexCharacterReference(StringRef Name) const {
60 unsigned CodePoint = 0;
61 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
63 const char C = Name[i];
64 assert(isHTMLHexCharacterReferenceCharacter(C));
65 if (C >= '0' && C <= '9')
66 CodePoint += Name[i] - '0';
67 else if (C >= 'a' && C <= 'f')
68 CodePoint += Name[i] - 'a' + 10;
70 CodePoint += Name[i] - 'A' + 10;
73 char *Resolved = Allocator.Allocate<char>(UNI_MAX_UTF8_BYTES_PER_CODE_POINT);
74 char *ResolvedPtr = Resolved;
75 if (ConvertCodePointToUTF8(CodePoint, ResolvedPtr))
76 return StringRef(Resolved, ResolvedPtr - Resolved);
81 void Lexer::skipLineStartingDecorations() {
82 // This function should be called only for C comments
83 assert(CommentState == LCS_InsideCComment);
85 if (BufferPtr == CommentEnd)
93 const char *NewBufferPtr = BufferPtr;
95 if (NewBufferPtr == CommentEnd)
98 char C = *NewBufferPtr;
99 while (C == ' ' || C == '\t' || C == '\f' || C == '\v') {
101 if (NewBufferPtr == CommentEnd)
106 BufferPtr = NewBufferPtr + 1;
116 /// Returns pointer to the first newline character in the string.
117 const char *findNewline(const char *BufferPtr, const char *BufferEnd) {
118 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
119 const char C = *BufferPtr;
120 if (C == '\n' || C == '\r')
126 const char *skipNewline(const char *BufferPtr, const char *BufferEnd) {
127 if (BufferPtr == BufferEnd)
130 if (*BufferPtr == '\n')
133 assert(*BufferPtr == '\r');
135 if (BufferPtr != BufferEnd && *BufferPtr == '\n')
141 const char *skipNamedCharacterReference(const char *BufferPtr,
142 const char *BufferEnd) {
143 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
144 if (!isHTMLNamedCharacterReferenceCharacter(*BufferPtr))
150 const char *skipDecimalCharacterReference(const char *BufferPtr,
151 const char *BufferEnd) {
152 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
153 if (!isHTMLDecimalCharacterReferenceCharacter(*BufferPtr))
159 const char *skipHexCharacterReference(const char *BufferPtr,
160 const char *BufferEnd) {
161 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
162 if (!isHTMLHexCharacterReferenceCharacter(*BufferPtr))
168 bool isHTMLIdentifierStartingCharacter(char C) {
169 return (C >= 'a' && C <= 'z') ||
170 (C >= 'A' && C <= 'Z');
173 bool isHTMLIdentifierCharacter(char C) {
174 return (C >= 'a' && C <= 'z') ||
175 (C >= 'A' && C <= 'Z') ||
176 (C >= '0' && C <= '9');
179 const char *skipHTMLIdentifier(const char *BufferPtr, const char *BufferEnd) {
180 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
181 if (!isHTMLIdentifierCharacter(*BufferPtr))
187 /// Skip HTML string quoted in single or double quotes. Escaping quotes inside
190 /// Returns pointer to closing quote.
191 const char *skipHTMLQuotedString(const char *BufferPtr, const char *BufferEnd)
193 const char Quote = *BufferPtr;
194 assert(Quote == '\"' || Quote == '\'');
197 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
198 const char C = *BufferPtr;
199 if (C == Quote && BufferPtr[-1] != '\\')
205 bool isHorizontalWhitespace(char C) {
206 return C == ' ' || C == '\t' || C == '\f' || C == '\v';
209 bool isWhitespace(char C) {
210 return C == ' ' || C == '\n' || C == '\r' ||
211 C == '\t' || C == '\f' || C == '\v';
214 const char *skipWhitespace(const char *BufferPtr, const char *BufferEnd) {
215 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
216 if (!isWhitespace(*BufferPtr))
222 bool isWhitespace(const char *BufferPtr, const char *BufferEnd) {
223 return skipWhitespace(BufferPtr, BufferEnd) == BufferEnd;
226 bool isCommandNameCharacter(char C) {
227 return (C >= 'a' && C <= 'z') ||
228 (C >= 'A' && C <= 'Z') ||
229 (C >= '0' && C <= '9');
232 const char *skipCommandName(const char *BufferPtr, const char *BufferEnd) {
233 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
234 if (!isCommandNameCharacter(*BufferPtr))
240 /// Return the one past end pointer for BCPL comments.
241 /// Handles newlines escaped with backslash or trigraph for backslahs.
242 const char *findBCPLCommentEnd(const char *BufferPtr, const char *BufferEnd) {
243 const char *CurPtr = BufferPtr;
244 while (CurPtr != BufferEnd) {
246 while (C != '\n' && C != '\r') {
248 if (CurPtr == BufferEnd)
252 // We found a newline, check if it is escaped.
253 const char *EscapePtr = CurPtr - 1;
254 while(isHorizontalWhitespace(*EscapePtr))
257 if (*EscapePtr == '\\' ||
258 (EscapePtr - 2 >= BufferPtr && EscapePtr[0] == '/' &&
259 EscapePtr[-1] == '?' && EscapePtr[-2] == '?')) {
260 // We found an escaped newline.
261 CurPtr = skipNewline(CurPtr, BufferEnd);
263 return CurPtr; // Not an escaped newline.
268 /// Return the one past end pointer for C comments.
269 /// Very dumb, does not handle escaped newlines or trigraphs.
270 const char *findCCommentEnd(const char *BufferPtr, const char *BufferEnd) {
271 for ( ; BufferPtr != BufferEnd; ++BufferPtr) {
272 if (*BufferPtr == '*') {
273 assert(BufferPtr + 1 != BufferEnd);
274 if (*(BufferPtr + 1) == '/')
278 llvm_unreachable("buffer end hit before '*/' was seen");
280 } // unnamed namespace
282 void Lexer::lexCommentText(Token &T) {
283 assert(CommentState == LCS_InsideBCPLComment ||
284 CommentState == LCS_InsideCComment);
289 case LS_VerbatimBlockFirstLine:
290 lexVerbatimBlockFirstLine(T);
292 case LS_VerbatimBlockBody:
293 lexVerbatimBlockBody(T);
295 case LS_VerbatimLineText:
296 lexVerbatimLineText(T);
298 case LS_HTMLStartTag:
306 assert(State == LS_Normal);
308 const char *TokenPtr = BufferPtr;
309 assert(TokenPtr < CommentEnd);
310 while (TokenPtr != CommentEnd) {
315 if (TokenPtr == CommentEnd) {
316 formTextToken(T, TokenPtr);
324 case '\\': case '@': case '&': case '$':
325 case '#': case '<': case '>': case '%':
326 case '\"': case '.': case ':':
327 // This is one of \\ \@ \& \$ etc escape sequences.
329 if (C == ':' && TokenPtr != CommentEnd && *TokenPtr == ':') {
330 // This is the \:: escape sequence.
333 StringRef UnescapedText(BufferPtr + 1, TokenPtr - (BufferPtr + 1));
334 formTokenWithChars(T, TokenPtr, tok::text);
335 T.setText(UnescapedText);
339 // Don't make zero-length commands.
340 if (!isCommandNameCharacter(*TokenPtr)) {
341 formTextToken(T, TokenPtr);
345 TokenPtr = skipCommandName(TokenPtr, CommentEnd);
346 unsigned Length = TokenPtr - (BufferPtr + 1);
348 // Hardcoded support for lexing LaTeX formula commands
349 // \f$ \f[ \f] \f{ \f} as a single command.
350 if (Length == 1 && TokenPtr[-1] == 'f' && TokenPtr != CommentEnd) {
352 if (C == '$' || C == '[' || C == ']' || C == '{' || C == '}') {
358 const StringRef CommandName(BufferPtr + 1, Length);
361 if (Traits.isVerbatimBlockCommand(CommandName, EndName)) {
362 setupAndLexVerbatimBlock(T, TokenPtr, *BufferPtr, EndName);
365 if (Traits.isVerbatimLineCommand(CommandName)) {
366 setupAndLexVerbatimLine(T, TokenPtr);
369 formTokenWithChars(T, TokenPtr, tok::command);
370 T.setCommandName(CommandName);
375 lexHTMLCharacterReference(T);
380 if (TokenPtr == CommentEnd) {
381 formTextToken(T, TokenPtr);
384 const char C = *TokenPtr;
385 if (isHTMLIdentifierStartingCharacter(C))
386 setupAndLexHTMLStartTag(T);
388 setupAndLexHTMLEndTag(T);
390 formTextToken(T, TokenPtr);
397 TokenPtr = skipNewline(TokenPtr, CommentEnd);
398 formTokenWithChars(T, TokenPtr, tok::newline);
400 if (CommentState == LCS_InsideCComment)
401 skipLineStartingDecorations();
407 if (TokenPtr == CommentEnd)
409 const char C = *TokenPtr;
410 if(C == '\n' || C == '\r' ||
411 C == '\\' || C == '@' || C == '&' || C == '<')
414 formTextToken(T, TokenPtr);
421 void Lexer::setupAndLexVerbatimBlock(Token &T,
422 const char *TextBegin,
423 char Marker, StringRef EndName) {
424 VerbatimBlockEndCommandName.clear();
425 VerbatimBlockEndCommandName.append(Marker == '\\' ? "\\" : "@");
426 VerbatimBlockEndCommandName.append(EndName);
428 StringRef Name(BufferPtr + 1, TextBegin - (BufferPtr + 1));
429 formTokenWithChars(T, TextBegin, tok::verbatim_block_begin);
430 T.setVerbatimBlockName(Name);
432 // If there is a newline following the verbatim opening command, skip the
433 // newline so that we don't create an tok::verbatim_block_line with empty
435 if (BufferPtr != CommentEnd) {
436 const char C = *BufferPtr;
437 if (C == '\n' || C == '\r') {
438 BufferPtr = skipNewline(BufferPtr, CommentEnd);
439 State = LS_VerbatimBlockBody;
444 State = LS_VerbatimBlockFirstLine;
447 void Lexer::lexVerbatimBlockFirstLine(Token &T) {
449 assert(BufferPtr < CommentEnd);
451 // FIXME: It would be better to scan the text once, finding either the block
452 // end command or newline.
454 // Extract current line.
455 const char *Newline = findNewline(BufferPtr, CommentEnd);
456 StringRef Line(BufferPtr, Newline - BufferPtr);
458 // Look for end command in current line.
459 size_t Pos = Line.find(VerbatimBlockEndCommandName);
461 const char *NextLine;
462 if (Pos == StringRef::npos) {
463 // Current line is completely verbatim.
465 NextLine = skipNewline(Newline, CommentEnd);
466 } else if (Pos == 0) {
467 // Current line contains just an end command.
468 const char *End = BufferPtr + VerbatimBlockEndCommandName.size();
469 StringRef Name(BufferPtr + 1, End - (BufferPtr + 1));
470 formTokenWithChars(T, End, tok::verbatim_block_end);
471 T.setVerbatimBlockName(Name);
475 // There is some text, followed by end command. Extract text first.
476 TextEnd = BufferPtr + Pos;
478 // If there is only whitespace before end command, skip whitespace.
479 if (isWhitespace(BufferPtr, TextEnd)) {
485 StringRef Text(BufferPtr, TextEnd - BufferPtr);
486 formTokenWithChars(T, NextLine, tok::verbatim_block_line);
487 T.setVerbatimBlockText(Text);
489 State = LS_VerbatimBlockBody;
492 void Lexer::lexVerbatimBlockBody(Token &T) {
493 assert(State == LS_VerbatimBlockBody);
495 if (CommentState == LCS_InsideCComment)
496 skipLineStartingDecorations();
498 lexVerbatimBlockFirstLine(T);
501 void Lexer::setupAndLexVerbatimLine(Token &T, const char *TextBegin) {
502 const StringRef Name(BufferPtr + 1, TextBegin - BufferPtr - 1);
503 formTokenWithChars(T, TextBegin, tok::verbatim_line_name);
504 T.setVerbatimLineName(Name);
506 State = LS_VerbatimLineText;
509 void Lexer::lexVerbatimLineText(Token &T) {
510 assert(State == LS_VerbatimLineText);
512 // Extract current line.
513 const char *Newline = findNewline(BufferPtr, CommentEnd);
514 const StringRef Text(BufferPtr, Newline - BufferPtr);
515 formTokenWithChars(T, Newline, tok::verbatim_line_text);
516 T.setVerbatimLineText(Text);
521 void Lexer::lexHTMLCharacterReference(Token &T) {
522 const char *TokenPtr = BufferPtr;
523 assert(*TokenPtr == '&');
525 if (TokenPtr == CommentEnd) {
526 formTextToken(T, TokenPtr);
530 bool isNamed = false;
531 bool isDecimal = false;
533 if (isHTMLNamedCharacterReferenceCharacter(C)) {
535 TokenPtr = skipNamedCharacterReference(TokenPtr, CommentEnd);
537 } else if (C == '#') {
539 if (TokenPtr == CommentEnd) {
540 formTextToken(T, TokenPtr);
544 if (isHTMLDecimalCharacterReferenceCharacter(C)) {
546 TokenPtr = skipDecimalCharacterReference(TokenPtr, CommentEnd);
548 } else if (C == 'x' || C == 'X') {
551 TokenPtr = skipHexCharacterReference(TokenPtr, CommentEnd);
553 formTextToken(T, TokenPtr);
557 formTextToken(T, TokenPtr);
560 if (NamePtr == TokenPtr || TokenPtr == CommentEnd ||
562 formTextToken(T, TokenPtr);
565 StringRef Name(NamePtr, TokenPtr - NamePtr);
566 TokenPtr++; // Skip semicolon.
569 Resolved = resolveHTMLNamedCharacterReference(Name);
571 Resolved = resolveHTMLDecimalCharacterReference(Name);
573 Resolved = resolveHTMLHexCharacterReference(Name);
575 if (Resolved.empty()) {
576 formTextToken(T, TokenPtr);
579 formTokenWithChars(T, TokenPtr, tok::text);
584 void Lexer::setupAndLexHTMLStartTag(Token &T) {
585 assert(BufferPtr[0] == '<' &&
586 isHTMLIdentifierStartingCharacter(BufferPtr[1]));
587 const char *TagNameEnd = skipHTMLIdentifier(BufferPtr + 2, CommentEnd);
589 StringRef Name(BufferPtr + 1, TagNameEnd - (BufferPtr + 1));
590 formTokenWithChars(T, TagNameEnd, tok::html_start_tag);
591 T.setHTMLTagStartName(Name);
593 BufferPtr = skipWhitespace(BufferPtr, CommentEnd);
595 const char C = *BufferPtr;
596 if (BufferPtr != CommentEnd &&
597 (C == '>' || C == '/' || isHTMLIdentifierStartingCharacter(C)))
598 State = LS_HTMLStartTag;
601 void Lexer::lexHTMLStartTag(Token &T) {
602 assert(State == LS_HTMLStartTag);
604 const char *TokenPtr = BufferPtr;
606 if (isHTMLIdentifierCharacter(C)) {
607 TokenPtr = skipHTMLIdentifier(TokenPtr, CommentEnd);
608 StringRef Ident(BufferPtr, TokenPtr - BufferPtr);
609 formTokenWithChars(T, TokenPtr, tok::html_ident);
610 T.setHTMLIdent(Ident);
615 formTokenWithChars(T, TokenPtr, tok::html_equals);
619 const char *OpenQuote = TokenPtr;
620 TokenPtr = skipHTMLQuotedString(TokenPtr, CommentEnd);
621 const char *ClosingQuote = TokenPtr;
622 if (TokenPtr != CommentEnd) // Skip closing quote.
624 formTokenWithChars(T, TokenPtr, tok::html_quoted_string);
625 T.setHTMLQuotedString(StringRef(OpenQuote + 1,
626 ClosingQuote - (OpenQuote + 1)));
631 formTokenWithChars(T, TokenPtr, tok::html_greater);
636 if (TokenPtr != CommentEnd && *TokenPtr == '>') {
638 formTokenWithChars(T, TokenPtr, tok::html_slash_greater);
640 formTextToken(T, TokenPtr);
647 // Now look ahead and return to normal state if we don't see any HTML tokens
649 BufferPtr = skipWhitespace(BufferPtr, CommentEnd);
650 if (BufferPtr == CommentEnd) {
656 if (!isHTMLIdentifierStartingCharacter(C) &&
657 C != '=' && C != '\"' && C != '\'' && C != '>') {
663 void Lexer::setupAndLexHTMLEndTag(Token &T) {
664 assert(BufferPtr[0] == '<' && BufferPtr[1] == '/');
666 const char *TagNameBegin = skipWhitespace(BufferPtr + 2, CommentEnd);
667 const char *TagNameEnd = skipHTMLIdentifier(TagNameBegin, CommentEnd);
669 const char *End = skipWhitespace(TagNameEnd, CommentEnd);
671 formTokenWithChars(T, End, tok::html_end_tag);
672 T.setHTMLTagEndName(StringRef(TagNameBegin, TagNameEnd - TagNameBegin));
674 if (BufferPtr != CommentEnd && *BufferPtr == '>')
675 State = LS_HTMLEndTag;
678 void Lexer::lexHTMLEndTag(Token &T) {
679 assert(BufferPtr != CommentEnd && *BufferPtr == '>');
681 formTokenWithChars(T, BufferPtr + 1, tok::html_greater);
685 Lexer::Lexer(llvm::BumpPtrAllocator &Allocator, const CommandTraits &Traits,
686 SourceLocation FileLoc, const CommentOptions &CommOpts,
687 const char *BufferStart, const char *BufferEnd):
688 Allocator(Allocator), Traits(Traits),
689 BufferStart(BufferStart), BufferEnd(BufferEnd),
690 FileLoc(FileLoc), CommOpts(CommOpts), BufferPtr(BufferStart),
691 CommentState(LCS_BeforeComment), State(LS_Normal) {
694 void Lexer::lex(Token &T) {
696 switch (CommentState) {
697 case LCS_BeforeComment:
698 if (BufferPtr == BufferEnd) {
699 formTokenWithChars(T, BufferPtr, tok::eof);
703 assert(*BufferPtr == '/');
704 BufferPtr++; // Skip first slash.
706 case '/': { // BCPL comment.
707 BufferPtr++; // Skip second slash.
709 if (BufferPtr != BufferEnd) {
710 // Skip Doxygen magic marker, if it is present.
711 // It might be missing because of a typo //< or /*<, or because we
712 // merged this non-Doxygen comment into a bunch of Doxygen comments
713 // around it: /** ... */ /* ... */ /** ... */
714 const char C = *BufferPtr;
715 if (C == '/' || C == '!')
719 // Skip less-than symbol that marks trailing comments.
720 // Skip it even if the comment is not a Doxygen one, because //< and /*<
721 // are frequent typos.
722 if (BufferPtr != BufferEnd && *BufferPtr == '<')
725 CommentState = LCS_InsideBCPLComment;
726 if (State != LS_VerbatimBlockBody && State != LS_VerbatimBlockFirstLine)
728 CommentEnd = findBCPLCommentEnd(BufferPtr, BufferEnd);
731 case '*': { // C comment.
732 BufferPtr++; // Skip star.
734 // Skip Doxygen magic marker.
735 const char C = *BufferPtr;
736 if ((C == '*' && *(BufferPtr + 1) != '/') || C == '!')
739 // Skip less-than symbol that marks trailing comments.
740 if (BufferPtr != BufferEnd && *BufferPtr == '<')
743 CommentState = LCS_InsideCComment;
745 CommentEnd = findCCommentEnd(BufferPtr, BufferEnd);
749 llvm_unreachable("second character of comment should be '/' or '*'");
752 case LCS_BetweenComments: {
753 // Consecutive comments are extracted only if there is only whitespace
754 // between them. So we can search for the start of the next comment.
755 const char *EndWhitespace = BufferPtr;
756 while(EndWhitespace != BufferEnd && *EndWhitespace != '/')
759 // Turn any whitespace between comments (and there is only whitespace
760 // between them -- guaranteed by comment extraction) into a newline. We
761 // have two newlines between C comments in total (first one was synthesized
763 formTokenWithChars(T, EndWhitespace, tok::newline);
765 CommentState = LCS_BeforeComment;
769 case LCS_InsideBCPLComment:
770 case LCS_InsideCComment:
771 if (BufferPtr != CommentEnd) {
775 // Skip C comment closing sequence.
776 if (CommentState == LCS_InsideCComment) {
777 assert(BufferPtr[0] == '*' && BufferPtr[1] == '/');
779 assert(BufferPtr <= BufferEnd);
781 // Synthenize newline just after the C comment, regardless if there is
782 // actually a newline.
783 formTokenWithChars(T, BufferPtr, tok::newline);
785 CommentState = LCS_BetweenComments;
788 // Don't synthesized a newline after BCPL comment.
789 CommentState = LCS_BetweenComments;
796 StringRef Lexer::getSpelling(const Token &Tok,
797 const SourceManager &SourceMgr,
798 bool *Invalid) const {
799 SourceLocation Loc = Tok.getLocation();
800 std::pair<FileID, unsigned> LocInfo = SourceMgr.getDecomposedLoc(Loc);
802 bool InvalidTemp = false;
803 StringRef File = SourceMgr.getBufferData(LocInfo.first, &InvalidTemp);
809 const char *Begin = File.data() + LocInfo.second;
810 return StringRef(Begin, Tok.getLength());
813 } // end namespace comments
814 } // end namespace clang