[clang-tidy] Fix duplicated struct body in modernize-use-using #247637
<!--LLVM NEW CONTRIBUTOR COMMENT-->
Hello @Neeraj3008 :wave:
Thank you for submitting a Pull Request (PR) to the LLVM Project. Since this is your first PR, here are a few useful links covering our main contribution policies and review practices.
All contributions to LLVM must follow our LLVM AI Tool Use Policy. In particular, if you used AI while working on this PR, remember to add a note to the PR description.
The LLVM Code-Review Policy and Practices document contains practical information about the PR process, including how patches are reviewed and accepted, and who can review a PR.
Please reply to this message to confirm that you have read these policies, especially the LLVM AI Tool Use Policy, and that any AI tool usage has been noted in the PR description.
---
Frequently asked questions
How do I add reviewers?
This PR will be automatically labeled, and the relevant teams will be notified. For some parts of the project, reviewers may also be added automatically.
You can also add reviewers manually using the Reviewers section on this page. If you cannot use that section, it is probably because you do not have write permissions for the repository. In that case, you can request a review by tagging reviewers in a comment using @ followed by their GitHub username.
What if there are no comments?
If you have not received any comments on your PR after a week, you can request a review by pinging the PR with a comment such as “Ping”. The common courtesy ping rate is once a week. Please remember that you are asking for volunteer time from other developers.
Are any special GitHub settings required to contribute to LLVM?
We only require contributors to have a public email address associated with their GitHub commits, see this section of LLVM Developer Policy for details.
---
If you have questions, feel free to leave a comment on this PR, or ask on LLVM Discord or LLVM Discourse.
Thank you,
The LLVM Community
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-tidy
@llvm/pr-subscribers-clang-tools-extra
Author: Neeraj3008
<details>
<summary>Changes</summary>
Fixes the duplicated struct body produced by modernize-use-using when converting typedefs containing anonymous struct declarations with comments.
The suffix text was previously extracted before accounting for the complete tag declaration. When a comment was present, this caused the struct body to be retained in the suffix and duplicated in the generated alias.
Recalculate the suffix text starting immediately after the complete tag declaration. This preserves comments without including the struct body twice.
Add regression coverage for anonymous structs containing both leading and trailing member comments.
Testing:
use-using.cpp lit test passed.modernize test directory: 164 passed, 1 unsupported, 0 failed.Fixes #<!-- -->246778
---
Full diff: https://github.com/llvm/llvm-project/pull/247637.diff
2 Files Affected:
````````````diff --git a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
index 504a2c40bcfff8..c2359276358529 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseUsingCheck.cpp
@@ -439,18 +439,6 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
if (!IsFirstTypedefInGroup)
stripLeadingComma(SuffixTextInfo);
- const bool SuffixHasComment = SuffixTextInfo.Tokens.HasComment;
- std::string SuffixText;
- if (SuffixHasComment) {
- SuffixText = SuffixTextInfo.Text;
- } else if (QualifierStr.empty() &&
- findFirstNonWhitespace(SuffixTextInfo.Text) != StringRef::npos &&
- SuffixTextInfo.Tokens.HasPointerOrRef &&
- !SuffixTextInfo.Tokens.HasIdentifier) {
- SuffixText = SuffixTextInfo.Text;
- }
- const std::string QualifierText = SuffixHasComment ? "" : QualifierStr;
-
if (!ReplaceRange.getEnd().isMacroID()) {
const SourceLocation::IntTy Offset = FunctionPointerCase ? 0 : Name.size();
LastReplacementEnd = ReplaceRange.getEnd().getLocWithOffset(Offset);
@@ -467,9 +455,28 @@ void UseUsingCheck::check(const MatchFinder::MatchResult &Result) {
CharSourceRange::getTokenRange(LastTagDeclRange->second), SM, LO);
if (!TagType)
return;
+
Type = *TagType;
+
+ const SourceLocation AfterTag = Lexer::getLocForEndOfToken(
+ LastTagDeclRange->second.getEnd(), 0, SM, LO);
+
+ SuffixTextInfo = getRangeTextInfo(AfterTag, NameLoc, SM, LO);
+ }
+
+ const bool SuffixHasComment = SuffixTextInfo.Tokens.HasComment;
+ std::string SuffixText;
+
+ if (SuffixHasComment) {
+ SuffixText = SuffixTextInfo.Text;
+ } else if (QualifierStr.empty() &&
+ findFirstNonWhitespace(SuffixTextInfo.Text) != StringRef::npos &&
+ SuffixTextInfo.Tokens.HasPointerOrRef &&
+ !SuffixTextInfo.Tokens.HasIdentifier) {
+ SuffixText = SuffixTextInfo.Text;
}
+ const std::string QualifierText = SuffixHasComment ? "" : QualifierStr;
std::string TypeExpr =
LeadingTextInfo.Text + Type + QualifierText + SuffixText;
TypeExpr = StringRef(TypeExpr).rtrim(" \t").str();
diff --git a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
index 36dc93faca4615..22c137ca0db035 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize/use-using.cpp
@@ -144,6 +144,15 @@ typedef struct Q1 { int a; } S1;
typedef struct { int b; } S2;
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
// CHECK-FIXES: using S2 = struct { int b; };
+typedef struct {
+ /// comment
+ int n;
+} S2LeadingComment;
+// CHECK-MESSAGES: :[[@LINE-4]]:1: warning: use 'using' instead of 'typedef'
+// CHECK-FIXES: using S2LeadingComment = struct {
+// CHECK-FIXES-NEXT: /// comment
+// CHECK-FIXES-NEXT: int n;
+// CHECK-FIXES-NEXT: };
struct Q2 { int c; } typedef S3;
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use 'using' instead of 'typedef'
// CHECK-FIXES: using S3 = struct Q2 { int c; };
</details>
I have read the LLVM AI Tool Use Policy, Code-Review Policy and Practices, and Developer Policy. I have also updated the PR description to disclose my AI tool usage. I understand that I am responsible for reviewing and explaining all submitted changes.
Please add a Release Note entry :)
@zeyi2 I've added the requested release note and resolved the merge conflicts by rebasing my branch onto the latest upstream main.
i noticed macOS arm64 ci check is failing during cmake configurations because the installed cmake binary requires a new macOS version than the runner provides. is this a know environment issue or is there something i should investigate
zeyi2
Neeraj3008
Fixes the duplicated struct body produced by
modernize-use-usingwhen converting typedefs containing anonymous struct declarations with comments.The suffix text was previously extracted before accounting for the complete tag declaration. When a comment was present, this caused the struct body to be retained in the suffix and duplicated in the generated alias.
Recalculate the suffix text starting immediately after the complete tag declaration. This preserves comments without including the struct body twice.
Add regression coverage for anonymous structs containing both leading and trailing member comments.
Testing:
use-using.cpplit test passed.modernizetest directory: 164 passed, 1 unsupported, 0 failed.Fixes #246778
AI assistance: ChatGPT was used to assist with debugging, understanding the relevant code, and developing the implementation and regression tests. I reviewed and tested the final changes and take responsibility for this contribution.