[clang] [AST] Iterate redecls starting from the canonical one in getRawCommentsForAnyRedecl() (PR #108475)

Younan Zhang via cfe-commits cfe-commits at lists.llvm.org
Wed Sep 18 00:47:08 PDT 2024


================
@@ -444,7 +444,7 @@ const RawComment *ASTContext::getRawCommentForAnyRedecl(
     return CommentlessRedeclChains.lookup(CanonicalD);
   }();
 
-  for (const auto Redecl : D->redecls()) {
+  for (const auto Redecl : CanonicalD->redecls()) {
----------------
zyn0217 wrote:

I reread your analysis and I think the only safe usage of `getRawCommentForAnyRedecl()` previously was that, in the second call to the function, only those `D`s that live in between the `CanonicalDecl` and `LastCheckedRedecl` are expected to be used as the starting point of the traversal. Otherwise, the presence of `LastCheckedRedecl` would result in skipping over all declarations, including those whose associated comments were unparsed yet.

So I wonder if we could make use of LastCheckedRedecl opt-in. To be clear, we first check if `D` is in the path `Canonical` -> `LastCheckedRedecl`. If so, we use the mechanism that skips past every visited declaration; otherwise, we ignore it.

```cpp
// Any redeclarations of D that we haven't checked for comments yet?
const Decl *LastCheckedRedecl = CommentlessRedeclChains.lookup(CanonicalD);
bool CanUseCommentlessCache = false;
if (LastCheckedRedecl) {
  for (auto *Redecl : CanonicalD->redecls()) {
    if (Redecl == D) {
      CanUseCommentlessCache = true;
      break;
    }
    if (Redecl == LastCheckedRedecl)
      break;
   }
}
```

```cpp
// Skip all redeclarations that have been checked previously.
if (CanUseCommentlessCache && LastCheckedRedecl)
  ...
```

The solution could be improved to pick up the `LastCheckedRedecl` again after visiting the `CanonicalDecl`, though.

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


More information about the cfe-commits mailing list