The key part of getRawCommentForDecl() required to find a comment
is determining where to look for it. The location of the decl
itself is usually right, except when macros get involved. The
comment in the macro is stored in RawCommentList at the spelling
location of the decl, not at the place where the decl comes into
being as the macro is instantiated.
getDeclLocForCommentSearch() already contained to branches to try
handle comments inside macros, and we are able to replace them
and handle more cases as well, by returning the spelling location
of the decl's begin location. That is:
SourceMgr.getSpellingLoc(D->getBeginLoc())
What was the original motivation for the PR here? Was it the DECLARE_FUNCTIONS case above?
For this case, personally I would expect that something like:
#define DEFINE_TAG(name) \ <anytagdecl> name { .... }; /// Comment attached to foo DEFINE_TAG(foo) /// Comment attached to bar DEFINE_TAG(bar)Is far more common than the test case here. To put another way, the previous
if (const auto *TD = dyn_cast<TagDecl>(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()) return SourceMgr.getExpansionLoc(DeclLoc); }Seems more reasonable to me than the hardcoded macro-name check. Even NS_*-wise there's also NS_ERROR_ENUM, NS_TYPED_ENUM, NS_TYPED_EXTENSIBLE_ENUM, NS_CLOSED_ENUM.