[clang] 0cb0809 - [Clang][Comments] Parse `<img src=""/>` in doc comments correctly

Egor Zhdan via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 1 05:17:53 PDT 2022


Author: Egor Zhdan
Date: 2022-09-01T13:17:42+01:00
New Revision: 0cb08093365478224f2a58cece42527fb8e1199e

URL: https://github.com/llvm/llvm-project/commit/0cb08093365478224f2a58cece42527fb8e1199e
DIFF: https://github.com/llvm/llvm-project/commit/0cb08093365478224f2a58cece42527fb8e1199e.diff

LOG: [Clang][Comments] Parse `<img src=""/>` in doc comments correctly

This is a valid HTML5 tag. Previously it triggered a Clang error (`HTML start tag prematurely ended, expected attribute name or '>'`) since Clang was treating `/>` as a text token. This was happening because after lexing the closing quote (`"`) the lexer state was reset to "Normal" while the tag was not actually closed yet: `>` was not yet parsed at that point.

rdar://91464292

Differential Revision: https://reviews.llvm.org/D132932

Added: 
    

Modified: 
    clang/lib/AST/CommentLexer.cpp
    clang/test/Index/comment-to-html-xml-conversion.cpp
    clang/test/Sema/warn-documentation.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/CommentLexer.cpp b/clang/lib/AST/CommentLexer.cpp
index 61ce8979f13f5..f0250fc9fd55e 100644
--- a/clang/lib/AST/CommentLexer.cpp
+++ b/clang/lib/AST/CommentLexer.cpp
@@ -701,7 +701,7 @@ void Lexer::lexHTMLStartTag(Token &T) {
 
   C = *BufferPtr;
   if (!isHTMLIdentifierStartingCharacter(C) &&
-      C != '=' && C != '\"' && C != '\'' && C != '>') {
+      C != '=' && C != '\"' && C != '\'' && C != '>' && C != '/') {
     State = LS_Normal;
     return;
   }

diff  --git a/clang/test/Index/comment-to-html-xml-conversion.cpp b/clang/test/Index/comment-to-html-xml-conversion.cpp
index 1fedd382365cf..ec49e5af31da1 100644
--- a/clang/test/Index/comment-to-html-xml-conversion.cpp
+++ b/clang/test/Index/comment-to-html-xml-conversion.cpp
@@ -744,6 +744,26 @@ void comment_to_html_conversion_37();
 // CHECK-NEXT:         (CXComment_Text Text=[ ] IsWhitespace)
 // CHECK-NEXT:         (CXComment_InlineCommand CommandName=[anchor] RenderAnchor Arg[0]=A)))]
 
+/// Aaa bbb<img/>
+void comment_to_html_conversion_38();
+
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:6: FunctionDecl=comment_to_html_conversion_38:{{.*}} FullCommentAsHTML=[<p class="para-brief"> Aaa bbb<img/></p>] FullCommentAsXML=[<Function file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="6"><Name>comment_to_html_conversion_38</Name><USR>c:@F at comment_to_html_conversion_38#</USR><Declaration>void comment_to_html_conversion_38()</Declaration><Abstract><Para> Aaa bbb<rawHTML><![CDATA[<img/>]]></rawHTML></Para></Abstract></Function>]
+// CHECK-NEXT:  CommentAST=[
+// CHECK-NEXT:    (CXComment_FullComment
+// CHECK-NEXT:       (CXComment_Paragraph
+// CHECK-NEXT:         (CXComment_Text Text=[ Aaa bbb])
+// CHECK-NEXT:         (CXComment_HTMLStartTag Name=[img] SelfClosing)
+
+/// Aaa ccc<img />
+void comment_to_html_conversion_39();
+
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:6: FunctionDecl=comment_to_html_conversion_39:{{.*}} FullCommentAsHTML=[<p class="para-brief"> Aaa ccc<img/></p>] FullCommentAsXML=[<Function file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="6"><Name>comment_to_html_conversion_39</Name><USR>c:@F at comment_to_html_conversion_39#</USR><Declaration>void comment_to_html_conversion_39()</Declaration><Abstract><Para> Aaa ccc<rawHTML><![CDATA[<img/>]]></rawHTML></Para></Abstract></Function>]
+// CHECK-NEXT:  CommentAST=[
+// CHECK-NEXT:    (CXComment_FullComment
+// CHECK-NEXT:       (CXComment_Paragraph
+// CHECK-NEXT:         (CXComment_Text Text=[ Aaa ccc])
+// CHECK-NEXT:         (CXComment_HTMLStartTag Name=[img] SelfClosing)
+
 /// Aaa ccc<img src="">
 void comment_to_html_conversion_40();
 
@@ -754,6 +774,36 @@ void comment_to_html_conversion_40();
 // CHECK-NEXT:         (CXComment_Text Text=[ Aaa ccc])
 // CHECK-NEXT:         (CXComment_HTMLStartTag Name=[img] Attrs: src=)
 
+/// Aaa ccc<img src="path">
+void comment_to_html_conversion_41();
+
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:6: FunctionDecl=comment_to_html_conversion_41:{{.*}} FullCommentAsHTML=[<p class="para-brief"> Aaa ccc<img src="path"></p>] FullCommentAsXML=[<Function file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="6"><Name>comment_to_html_conversion_41</Name><USR>c:@F at comment_to_html_conversion_41#</USR><Declaration>void comment_to_html_conversion_41()</Declaration><Abstract><Para> Aaa ccc<rawHTML><![CDATA[<img src="path">]]></rawHTML></Para></Abstract></Function>]
+// CHECK-NEXT:  CommentAST=[
+// CHECK-NEXT:    (CXComment_FullComment
+// CHECK-NEXT:       (CXComment_Paragraph
+// CHECK-NEXT:         (CXComment_Text Text=[ Aaa ccc])
+// CHECK-NEXT:         (CXComment_HTMLStartTag Name=[img] Attrs: src=path)
+
+/// Aaa ccc<img src="path"/>
+void comment_to_html_conversion_42();
+
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:6: FunctionDecl=comment_to_html_conversion_42:{{.*}} FullCommentAsHTML=[<p class="para-brief"> Aaa ccc<img src="path"/></p>] FullCommentAsXML=[<Function file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="6"><Name>comment_to_html_conversion_42</Name><USR>c:@F at comment_to_html_conversion_42#</USR><Declaration>void comment_to_html_conversion_42()</Declaration><Abstract><Para> Aaa ccc<rawHTML><![CDATA[<img src="path"/>]]></rawHTML></Para></Abstract></Function>]
+// CHECK-NEXT:  CommentAST=[
+// CHECK-NEXT:    (CXComment_FullComment
+// CHECK-NEXT:       (CXComment_Paragraph
+// CHECK-NEXT:         (CXComment_Text Text=[ Aaa ccc])
+// CHECK-NEXT:         (CXComment_HTMLStartTag Name=[img] Attrs: src=path SelfClosing)
+
+/// Aaa ddd<img src=""/>
+void comment_to_html_conversion_43();
+
+// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-2]]:6: FunctionDecl=comment_to_html_conversion_43:{{.*}} FullCommentAsHTML=[<p class="para-brief"> Aaa ddd<img src/></p>] FullCommentAsXML=[<Function file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-2]]" column="6"><Name>comment_to_html_conversion_43</Name><USR>c:@F at comment_to_html_conversion_43#</USR><Declaration>void comment_to_html_conversion_43()</Declaration><Abstract><Para> Aaa ddd<rawHTML><![CDATA[<img src/>]]></rawHTML></Para></Abstract></Function>]
+// CHECK-NEXT:  CommentAST=[
+// CHECK-NEXT:    (CXComment_FullComment
+// CHECK-NEXT:       (CXComment_Paragraph
+// CHECK-NEXT:         (CXComment_Text Text=[ Aaa ddd])
+// CHECK-NEXT:         (CXComment_HTMLStartTag Name=[img] Attrs: src= SelfClosing)
+
 /// Aaa.
 class comment_to_xml_conversion_01 {
 // CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-1]]:7: ClassDecl=comment_to_xml_conversion_01:{{.*}} FullCommentAsXML=[<Class file="{{[^"]+}}comment-to-html-xml-conversion.cpp" line="[[@LINE-1]]" column="7"><Name>comment_to_xml_conversion_01</Name><USR>c:@S at comment_to_xml_conversion_01</USR><Declaration>class comment_to_xml_conversion_01 {}</Declaration><Abstract><Para> Aaa.</Para></Abstract></Class>]

diff  --git a/clang/test/Sema/warn-documentation.cpp b/clang/test/Sema/warn-documentation.cpp
index 431c0e078dd98..284ae97b0f88f 100644
--- a/clang/test/Sema/warn-documentation.cpp
+++ b/clang/test/Sema/warn-documentation.cpp
@@ -62,6 +62,21 @@ int test_html10(int);
 /// <br></br>
 int test_html11(int);
 
+/// Aaa bbb<img/>
+int test_html12(int);
+
+/// Aaa bbb<img />
+int test_html13(int);
+
+/// Aaa bbb<img src="">
+int test_html14(int);
+
+/// Aaa bbb<img src=""/>
+int test_html15(int);
+
+/// Aaa bbb<img src="" />
+int test_html16(int);
+
 /// <blockquote>Meow</blockquote>
 int test_html_nesting1(int);
 


        


More information about the cfe-commits mailing list