From: Dmitri Gribenko Date: Thu, 27 Mar 2014 16:40:51 +0000 (+0000) Subject: Comment parsing: attach comments to enums declared using the NS_ENUM macro X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b29c030ef958b2cb942bbd37c1f62e050f85fe81;p=clang Comment parsing: attach comments to enums declared using the NS_ENUM macro Previously we would only attach comments to the typedef. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@204942 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/ASTContext.cpp b/lib/AST/ASTContext.cpp index 1e55521b87..110ae38901 100644 --- a/lib/AST/ASTContext.cpp +++ b/lib/AST/ASTContext.cpp @@ -145,11 +145,23 @@ RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { DeclLoc = D->getLocStart(); else { DeclLoc = D->getLocation(); - // If location of the typedef name is in a macro, it is because being - // declared via a macro. Try using declaration's starting location - // as the "declaration location". - if (DeclLoc.isMacroID() && isa(D)) - DeclLoc = D->getLocStart(); + if (DeclLoc.isMacroID()) { + if (isa(D)) { + // If location of the typedef name is in a macro, it is because being + // declared via a macro. Try using declaration's starting location as + // the "declaration location". + DeclLoc = D->getLocStart(); + } else if (const TagDecl *TD = dyn_cast(D)) { + // If location of the tag decl is inside a macro, but the spelling of + // the tag name comes from a macro argument, it looks like a special + // macro like NS_ENUM is being used to define the tag decl. In that + // case, adjust the source location to the expansion loc so that we can + // attach the comment to the tag decl. + if (SourceMgr.isMacroArgExpansion(DeclLoc) && + TD->isCompleteDefinition()) + DeclLoc = SourceMgr.getExpansionLoc(DeclLoc); + } + } } // If the declaration doesn't map directly to a location in a file, we diff --git a/test/Index/annotate-comments-objc.m b/test/Index/annotate-comments-objc.m index bd32445f89..9fb4b4e943 100644 --- a/test/Index/annotate-comments-objc.m +++ b/test/Index/annotate-comments-objc.m @@ -32,6 +32,32 @@ void functionBeforeImports(void); - (void)method1_isdoxy4; /*!< method1_isdoxy4 IS_DOXYGEN_SINGLE */ @end +//===--- +// rdar://14348912 +// Check that we attach comments to enums declared using the NS_ENUM macro. +//===--- + +#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type + +/// An_NS_ENUM_isdoxy1 IS_DOXYGEN_SINGLE +typedef NS_ENUM(int, An_NS_ENUM_isdoxy1) { Red, Green, Blue }; + +// In the implementation of attaching comments to enums declared using the +// NS_ENUM macro, it is tempting to use the fact that enum decl is embedded in +// the typedef. Make sure that the heuristic is strong enough that it does not +// attach unrelated comments in the following cases where tag decls are +// embedded in declarators. + +#define DECLARE_FUNCTION() \ + void functionFromMacro() { \ + typedef struct Struct_notdoxy Struct_notdoxy; \ + } + +/// IS_DOXYGEN_NOT_ATTACHED +DECLARE_FUNCTION() + +/// typedef_isdoxy1 IS_DOXYGEN_SINGLE +typedef struct Struct_notdoxy *typedef_isdoxy1; #endif @@ -91,4 +117,8 @@ void functionBeforeImports(void); // CHECK: annotate-comments-objc.m:30:9: ObjCInstanceMethodDecl=method1_isdoxy2:{{.*}} method1_isdoxy2 IS_DOXYGEN_SINGLE // CHECK: annotate-comments-objc.m:31:9: ObjCInstanceMethodDecl=method1_isdoxy3:{{.*}} method1_isdoxy3 IS_DOXYGEN_SINGLE // CHECK: annotate-comments-objc.m:32:9: ObjCInstanceMethodDecl=method1_isdoxy4:{{.*}} method1_isdoxy4 IS_DOXYGEN_SINGLE +// CHECK: annotate-comments-objc.m:43:22: EnumDecl=An_NS_ENUM_isdoxy1:{{.*}} An_NS_ENUM_isdoxy1 IS_DOXYGEN_SINGLE +// CHECK: annotate-comments-objc.m:43:22: TypedefDecl=An_NS_ENUM_isdoxy1:{{.*}} An_NS_ENUM_isdoxy1 IS_DOXYGEN_SINGLE +// CHECK: annotate-comments-objc.m:43:22: EnumDecl=An_NS_ENUM_isdoxy1:{{.*}} An_NS_ENUM_isdoxy1 IS_DOXYGEN_SINGLE +// CHECK: annotate-comments-objc.m:60:32: TypedefDecl=typedef_isdoxy1:{{.*}} typedef_isdoxy1 IS_DOXYGEN_SINGLE diff --git a/test/Index/comment-to-html-xml-conversion.cpp b/test/Index/comment-to-html-xml-conversion.cpp index a824ec48b4..327fa64483 100644 --- a/test/Index/comment-to-html-xml-conversion.cpp +++ b/test/Index/comment-to-html-xml-conversion.cpp @@ -1015,17 +1015,5 @@ void comment_to_xml_conversion_exceptions_5(); // CHECK-NEXT: (CXComment_Paragraph // CHECK-NEXT: (CXComment_Text Text=[ Ccc.]))))] - -// rdar://14348912 -#define NS_ENUM(_type, _name) enum _name : _type _name; enum _name : _type - -/**! Documentation comment */ -typedef NS_ENUM(int, Color) { Red, Green, Blue }; -// CHECK: comment-to-html-xml-conversion.cpp:[[@LINE-1]]:22: TypedefDecl=Color:[[@LINE-1]]:22 -// CHECK-NEXT: CommentAST=[ -// CHECK-NEXT: (CXComment_FullComment -// CHECK-NEXT: (CXComment_Paragraph -// CHECK-NEXT: (CXComment_Text Text=[! Documentation comment ])))] - #endif