[clang] [clang] Prioritze decl comments from macro expansion site (PR #65481)

Ben Barham via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 6 10:46:21 PDT 2023


================
@@ -179,103 +179,38 @@ static SourceLocation getDeclLocForCommentSearch(const Decl *D,
       isa<ClassTemplateSpecializationDecl>(D) ||
       // Allow association with Y across {} in `typedef struct X {} Y`.
       isa<TypedefDecl>(D))
-    return D->getBeginLoc();
+    return {D->getBeginLoc()};
----------------
bnbarham wrote:

Seems like this should also use the macro logic. In particular this has a `|| isa<TypedefDecl>(D)` and so the `TypedefDecl` case in `DeclLoc.isMacroID()` isn't actually used. Missing tests?

As far as I understand, the cases here are:
  - Use the beginning of the decl vs the identifier
  - Expansion + spelling of a macro for either of those locations if we're in a macro
  - Possibly something special for the NS_ENUM-like cases?

So we could instead do something like:
```
SmallVector<SourceLocation, 2> locations;
SourceLocation baseLocation;
if (isa<...>(D) || ...) {
  baseLocation = D->getBeginLoc();
} else {
  baseLocation = D->getLocation();
}

if (!DeclLoc.isMacroID()) {
  locations.emplace_back(baseLocation);
} else {
  // Maybe something special for the NS_ENUM case?
  locations.emplace_back(SourceMgr.getExpansionLoc(baseLocation), SourceMgr.getSpellingLoc(baseLocation));
}
```


https://github.com/llvm/llvm-project/pull/65481


More information about the cfe-commits mailing list