[clang] a433e71 - [AST] Attach comment in `/** doc */ typedef struct A {} B` to B as well as A.

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 18 02:09:53 PST 2019


Author: Sam McCall
Date: 2019-11-18T11:09:23+01:00
New Revision: a433e7141fb3f697e6430437ee73b19076603c1b

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

LOG: [AST] Attach comment in `/** doc */ typedef struct A {} B` to B as well as A.

Summary:
Semantically they're the same thing, and it's important when the underlying
struct is anonymous.

There doesn't seem to be a problem attaching the same comment to multiple things
as it already happens with `/** doc */ int a, b;`

This affects an Index test but the results look better (name present, USR points
to the typedef).

Fixes https://github.com/clangd/clangd/issues/189

Reviewers: kadircet, lh123

Subscribers: ilya-biryukov, jkorous, arphaman, usaxena95, cfe-commits

Tags: #clang

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

Added: 
    

Modified: 
    clang-tools-extra/clangd/unittests/XRefsTests.cpp
    clang/lib/AST/ASTContext.cpp
    clang/test/Index/annotate-comments-typedef.m
    clang/test/Sema/warn-documentation.cpp

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clangd/unittests/XRefsTests.cpp b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
index cbc81805fd73..cb34dac7ec00 100644
--- a/clang-tools-extra/clangd/unittests/XRefsTests.cpp
+++ b/clang-tools-extra/clangd/unittests/XRefsTests.cpp
@@ -1299,6 +1299,19 @@ TEST(Hover, All) {
           "]\n"
           "text[Typedef]",
       },
+      {
+          R"cpp(// Typedef with embedded definition
+            typedef struct Bar {} Foo;
+            int main() {
+              ^Foo bar;
+            }
+          )cpp",
+          "text[Declared in]code[global namespace]\n"
+          "codeblock(cpp) [\n"
+          "typedef struct Bar Foo\n"
+          "]\n"
+          "text[Typedef with embedded definition]",
+      },
       {
           R"cpp(// Namespace
             namespace ns {

diff  --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index cda51ec755a8..6a15f99eb7d3 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -163,7 +163,9 @@ static SourceLocation getDeclLocForCommentSearch(const Decl *D,
   if (isa<ObjCMethodDecl>(D) || isa<ObjCContainerDecl>(D) ||
       isa<ObjCPropertyDecl>(D) ||
       isa<RedeclarableTemplateDecl>(D) ||
-      isa<ClassTemplateSpecializationDecl>(D))
+      isa<ClassTemplateSpecializationDecl>(D) ||
+      // Allow association with Y across {} in `typedef struct X {} Y`.
+      isa<TypedefDecl>(D))
     return D->getBeginLoc();
   else {
     const SourceLocation DeclLoc = D->getLocation();

diff  --git a/clang/test/Index/annotate-comments-typedef.m b/clang/test/Index/annotate-comments-typedef.m
index 751cfaab4931..77c4d631850d 100644
--- a/clang/test/Index/annotate-comments-typedef.m
+++ b/clang/test/Index/annotate-comments-typedef.m
@@ -17,7 +17,7 @@
         MyEnumBar, /**< value Bar */
         MyEnumBaz, /**< value Baz */
 } MyEnum;
-// CHECK: TypedefDecl=MyEnum:[[@LINE-1]]:3 (Definition) FullCommentAsHTML=[<p class="para-brief"> Documentation for MyEnum </p>] FullCommentAsXML=[<Typedef file="{{[^"]+}}annotate-comments-typedef.m" line="[[@LINE-1]]" column="3"><Name><anonymous></Name><USR>c:@EA at MyEnum</USR><Declaration>typedef enum MyEnum MyEnum</Declaration><Abstract><Para> Documentation for MyEnum </Para></Abstract></Typedef>]
+// CHECK: TypedefDecl=MyEnum:[[@LINE-1]]:3 (Definition) {{.*}} FullCommentAsHTML=[<p class="para-brief"> Documentation for MyEnum </p>] FullCommentAsXML=[<Typedef file="{{[^"]+}}annotate-comments-typedef.m" line="[[@LINE-1]]" column="3"><Name>MyEnum</Name><USR>c:@T at MyEnum</USR><Declaration>typedef enum MyEnum MyEnum</Declaration><Abstract><Para> Documentation for MyEnum </Para></Abstract></Typedef>]
 
 
 /** Documentation for E */
@@ -35,7 +35,7 @@
 typedef struct {
          int iii;
         } Foo;
-// CHECK: TypedefDecl=Foo:[[@LINE-1]]:11 (Definition) FullCommentAsHTML=[<p class="para-brief"> Comment about Foo </p>] FullCommentAsXML=[<Typedef file="{{[^"]+}}annotate-comments-typedef.m" line="[[@LINE-1]]" column="11"><Name><anonymous></Name><USR>c:@SA at Foo</USR><Declaration>typedef struct Foo Foo</Declaration><Abstract><Para> Comment about Foo </Para></Abstract></Typedef>]
+// CHECK: TypedefDecl=Foo:[[@LINE-1]]:11 (Definition) {{.*}} FullCommentAsHTML=[<p class="para-brief"> Comment about Foo </p>] FullCommentAsXML=[<Typedef file="{{[^"]+}}annotate-comments-typedef.m" line="[[@LINE-1]]" column="11"><Name>Foo</Name><USR>c:@T at Foo</USR><Declaration>typedef struct Foo Foo</Declaration><Abstract><Para> Comment about Foo </Para></Abstract></Typedef>]
 // CHECK: StructDecl=:[[@LINE-4]]:9 (Definition) {{.*}} BriefComment=[Comment about Foo] FullCommentAsHTML=[<p class="para-brief"> Comment about Foo </p>] FullCommentAsXML=[<Class file="{{[^"]+}}annotate-comments-typedef.m" line="[[@LINE-4]]" column="9"><Name><anonymous></Name><USR>c:@SA at Foo</USR><Declaration>struct {}</Declaration><Abstract><Para> Comment about Foo </Para></Abstract></Class>]
 
 

diff  --git a/clang/test/Sema/warn-documentation.cpp b/clang/test/Sema/warn-documentation.cpp
index 66f02efdc40d..93273559a905 100644
--- a/clang/test/Sema/warn-documentation.cpp
+++ b/clang/test/Sema/warn-documentation.cpp
@@ -868,7 +868,7 @@ struct test_noattach12 *test_attach13;
 /// \brief\author Aaa
 typedef struct test_noattach14 *test_attach15;
 
-// expected-warning at +1 {{empty paragraph passed to '\brief' command}}
+// expected-warning at +1 + {{empty paragraph passed to '\brief' command}}
 /// \brief\author Aaa
 typedef struct test_attach16 { int a; } test_attach17;
 
@@ -886,7 +886,7 @@ typedef struct S *test_attach19;
 /// \brief\author Aaa
 struct test_attach20;
 
-// expected-warning at +1 {{empty paragraph passed to '\brief' command}}
+// expected-warning at +1 + {{empty paragraph passed to '\brief' command}}
 /// \brief\author Aaa
 typedef struct test_attach21 {
   // expected-warning at +1 {{empty paragraph passed to '\brief' command}}


        


More information about the cfe-commits mailing list