================
@@ -366,6 +389,101 @@ getFunctionSourceCode(const FunctionDecl *FD, const 
DeclContext *TargetContext,
       DelKeyword(tok::kw_explicit, {FD->getBeginLoc(), FD->getLocation()});
   }
 
+  // Drop attributes that the out-of-line definition inherits from the
+  // declaration. Ones we can't safely locate are left alone.
+  // FIXME: Attributes coing from macros and attributes unknown to clang (which
+  // are not part of the AST) are not dropped.
+  for (Attr *A : FD->attrs()) {
+    if (A->isImplicit() || A->isInherited())
+      continue;
+
+    // Get the delimiter for the attribute type
+    llvm::SmallVector<tok::TokenKind> LeftDelimiter, RightDelimiter;
+    switch (A->getSyntax()) {
+    case AttributeCommonInfo::AS_GNU:
+      // Delimited by __attribute__(( and ))
+      LeftDelimiter.assign({tok::kw___attribute, tok::l_paren, tok::l_paren});
+      RightDelimiter.assign({tok::r_paren, tok::r_paren});
+      break;
+    case AttributeCommonInfo::AS_CXX11:
+    case AttributeCommonInfo::AS_C23:
+      // Delimited by [[ and ]]
+      LeftDelimiter.assign({tok::l_square, tok::l_square});
+      RightDelimiter.assign({tok::r_square, tok::r_square});
+      break;
+    case AttributeCommonInfo::AS_Declspec:
+      // Delimited by __declspec( and )
+      LeftDelimiter.assign({tok::kw___declspec, tok::l_paren});
+      RightDelimiter.assign({tok::r_paren});
+      break;
+    default:
+      // All other syntaxes are not relevant here
+      break;
+    }
+
+    if (LeftDelimiter.empty() || RightDelimiter.empty())
+      continue;
+
+    auto FDTokens = TokBuf.spelledForExpanded(TokBuf.expandedTokens(
+        {FD->getBeginLoc(), FD->getBody()->getBeginLoc()}));
+    auto AttrTokens =
+        TokBuf.spelledForExpanded(TokBuf.expandedTokens(A->getRange()));
+
+    if (!FDTokens.has_value() || !AttrTokens.has_value() ||
+        !SM.isPointWithin(A->getLocation(), FDTokens->front().location(),
+                          FDTokens->back().location()))
+      continue;
+
+    CharSourceRange DelRange;
+
+    // Find position of the right delimiter
+    for (auto It = AttrTokens->end(), E = FDTokens->end(); It != E; ++It) {
+
+      // There might be a further attribute with argument delimited by parens
+      // in the attribute list. The closing paren might be recognized as part
+      // of the attribute delimiter. So skip tokens until the closing paren.
+      if (It->kind() == tok::l_paren) {
+        It = skipTokenPair(It, E, tok::l_paren, tok::r_paren);
+        if (It != E)
+          continue;
+      }
+
+      if (std::distance(It, E) < static_cast<long>(RightDelimiter.size()))
+        // No chance to find the right delimiter. Give up.
+        break;
+
+      llvm::ArrayRef<syntax::Token> Candidate(It, RightDelimiter.size());
+
+      if (llvm::equal(Candidate, RightDelimiter, [](auto Tok, auto TokKindDel) 
{
+            return Tok.kind() == TokKindDel;
+          })) {
+        DelRange.setEnd(Candidate.end()->location());
----------------
ckandeler wrote:

**[LLM-generated]** Two related issues with ending the range here:

1. `Candidate.end()` points one past the `ArrayRef`, so this dereferences a 
token outside the matched delimiter. It is only safe because `FDTokens` happens 
to always end with the body's `{`.
2. Because the deleted char range extends up to the start of the next token, 
comments in between are removed too:
   ```cpp
   struct A { [[nodiscard]] /*keep me*/ int ^foo() { return 0; } };
   // -> int A::foo() { return 0; }   (comment lost)
   ```

Ending the range at the closing delimiter (e.g. 
`Candidate.back().endLocation()`), and then dealing with a following whitespace 
character if desired, would avoid both.

https://github.com/llvm/llvm-project/pull/242635
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to