Skip to content

Commit 90a760a

Browse files
put the source text of the 'MemberExpr' in the diag message.
1 parent 32cb572 commit 90a760a

File tree

3 files changed

+27
-23
lines changed

3 files changed

+27
-23
lines changed

clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,8 @@ void UnnecessaryCopyInitialization::registerMatchers(MatchFinder *Finder) {
282282
LocalVarCopiedFrom(
283283
memberExpr(hasObjectExpression(anyOf(hasDescendant(DeclRefToConstVar),
284284
DeclRefToConstVar)),
285-
member(fieldDecl().bind("fieldDecl")))),
285+
member(fieldDecl()))
286+
.bind("memExpr")),
286287
this);
287288
}
288289

@@ -305,7 +306,7 @@ void UnnecessaryCopyInitialization::check(
305306
IssueFix, IsVarUnused, IsVarOnlyUsedAsConst};
306307
const auto *OldVar = Result.Nodes.getNodeAs<VarDecl>(OldVarDeclId);
307308
const auto *ObjectArg = Result.Nodes.getNodeAs<VarDecl>(ObjectArgId);
308-
const auto *FD = Result.Nodes.getNodeAs<FieldDecl>("fieldDecl");
309+
const auto *ME = Result.Nodes.getNodeAs<MemberExpr>("memExpr");
309310
const auto *CtorCall = Result.Nodes.getNodeAs<CXXConstructExpr>("ctorCall");
310311

311312
TraversalKindScope RAII(*Result.Context, TK_AsIs);
@@ -330,12 +331,12 @@ void UnnecessaryCopyInitialization::check(
330331
if (OldVar == nullptr) {
331332
// `auto NewVar = functionCall();`
332333
handleCopyFromMethodReturn(Context, ObjectArg);
333-
} else if (FD == nullptr) {
334+
} else if (ME == nullptr) {
334335
// `auto NewVar = OldVar;`
335336
handleCopyFromLocalVar(Context, *OldVar);
336337
} else {
337338
// `auto NewVar = OldVar.FD;`
338-
handleCopyFromConstLocalVarMember(Context, *OldVar, *FD);
339+
handleCopyFromConstVarMember(Context, *OldVar, *ME);
339340
}
340341
}
341342

@@ -360,9 +361,9 @@ void UnnecessaryCopyInitialization::handleCopyFromLocalVar(
360361
diagnoseCopyFromLocalVar(Ctx, OldVar);
361362
}
362363

363-
void UnnecessaryCopyInitialization::handleCopyFromConstLocalVarMember(
364-
const CheckContext &Ctx, const VarDecl &OldVar, const FieldDecl &FD) {
365-
diagnoseCopyFromConstLocalVarMember(Ctx, OldVar, FD);
364+
void UnnecessaryCopyInitialization::handleCopyFromConstVarMember(
365+
const CheckContext &Ctx, const VarDecl &OldVar, const MemberExpr &ME) {
366+
diagnoseCopyFromConstVarMember(Ctx, OldVar, ME);
366367
}
367368

368369
void UnnecessaryCopyInitialization::diagnoseCopyFromMethodReturn(
@@ -391,15 +392,18 @@ void UnnecessaryCopyInitialization::diagnoseCopyFromLocalVar(
391392
maybeIssueFixes(Ctx, Diagnostic);
392393
}
393394

394-
void UnnecessaryCopyInitialization::diagnoseCopyFromConstLocalVarMember(
395-
const CheckContext &Ctx, const VarDecl &OldVar, const FieldDecl &FD) {
395+
void UnnecessaryCopyInitialization::diagnoseCopyFromConstVarMember(
396+
const CheckContext &Ctx, const VarDecl &OldVar, const MemberExpr &ME) {
397+
std::string MEStr(Lexer::getSourceText(
398+
CharSourceRange::getTokenRange(ME.getSourceRange()),
399+
Ctx.ASTCtx.getSourceManager(), Ctx.ASTCtx.getLangOpts()));
396400
auto Diagnostic =
397401
diag(Ctx.Var.getLocation(),
398-
"local copy %0 of the field %1 of type %2 in object %3 is never "
402+
"local copy %0 of the subobject '%1' of type %2 is never "
399403
"modified%select{"
400404
"| and never used}4; consider %select{avoiding the copy|removing "
401405
"the statement}4")
402-
<< &Ctx.Var << &FD << Ctx.Var.getType() << &OldVar << Ctx.IsVarUnused;
406+
<< &Ctx.Var << MEStr << Ctx.Var.getType() << &OldVar << Ctx.IsVarUnused;
403407
maybeIssueFixes(Ctx, Diagnostic);
404408
}
405409

clang-tools-extra/clang-tidy/performance/UnnecessaryCopyInitialization.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ class UnnecessaryCopyInitialization : public ClangTidyCheck {
5050
virtual void diagnoseCopyFromMethodReturn(const CheckContext &Ctx);
5151
virtual void diagnoseCopyFromLocalVar(const CheckContext &Ctx,
5252
const VarDecl &OldVar);
53-
virtual void diagnoseCopyFromConstLocalVarMember(const CheckContext &Ctx,
53+
virtual void diagnoseCopyFromConstVarMember(const CheckContext &Ctx,
5454
const VarDecl &OldVar,
55-
const FieldDecl &FD);
55+
const MemberExpr &ME);
5656

5757
private:
5858
void handleCopyFromMethodReturn(const CheckContext &Ctx,
5959
const VarDecl *ObjectArg);
6060
void handleCopyFromLocalVar(const CheckContext &Ctx, const VarDecl &OldVar);
6161

62-
void handleCopyFromConstLocalVarMember(const CheckContext &Ctx,
62+
void handleCopyFromConstVarMember(const CheckContext &Ctx,
6363
const VarDecl &OldVar,
64-
const FieldDecl &FD);
64+
const MemberExpr &ME);
6565

6666
void maybeIssueFixes(const CheckContext &Ctx, DiagnosticBuilder &Diagnostic);
6767

clang-tools-extra/test/clang-tidy/checkers/performance/unnecessary-copy-initialization.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -941,10 +941,10 @@ template<typename T> bool OperatorWithNoDirectCallee(T t) {
941941

942942
bool CopiedFromParmVarField(const Struct &crs, const Struct cs, Struct &rs, Struct s) {
943943
const auto m1 = crs.Member;
944-
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm1' of the field 'Member' of type 'const ExpensiveToCopyType' in object 'crs' is never modified; consider avoiding the copy
944+
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm1' of the subobject 'crs.Member' of type 'const ExpensiveToCopyType' is never modified; consider avoiding the copy
945945
// CHECK-FIXES: const auto& m1 = crs.Member;
946946
const auto m2 = cs.Member;
947-
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm2' of the field 'Member' of type 'const ExpensiveToCopyType' in object 'cs' is never modified; consider avoiding the copy
947+
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm2' of the subobject 'cs.Member' of type 'const ExpensiveToCopyType' is never modified; consider avoiding the copy
948948
// CHECK-FIXES: const auto& m2 = cs.Member;
949949
const auto m3 = rs.Member;
950950
const auto m4 = s.Member;
@@ -956,11 +956,11 @@ bool CopiedFromVarField() {
956956
const Struct crs;
957957
Struct s;
958958
const auto m1 = crs.Member;
959-
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm1' of the field 'Member' of type 'const ExpensiveToCopyType' in object 'crs' is never modified; consider avoiding the copy
959+
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm1' of the subobject 'crs.Member' of type 'const ExpensiveToCopyType' is never modified; consider avoiding the copy
960960
// CHECK-FIXES: const auto& m1 = crs.Member;
961961
const auto m2 = s.Member;
962962
const auto m3 = GlobalS.Member;
963-
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm3' of the field 'Member' of type 'const ExpensiveToCopyType' in object 'GlobalS' is never modified; consider avoiding the copy
963+
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm3' of the subobject 'GlobalS.Member' of type 'const ExpensiveToCopyType' is never modified; consider avoiding the copy
964964
// CHECK-FIXES: const auto& m3 = GlobalS.Member;
965965
return m1 == m2 || m2 == m3;
966966
}
@@ -971,10 +971,10 @@ struct NestedStruct {
971971

972972
bool CopiedFromParmVarNestedField(const NestedStruct &ncrs, const NestedStruct ncs, NestedStruct &nrs, NestedStruct ns) {
973973
const auto m1 = ncrs.s.Member;
974-
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm1' of the field 'Member' of type 'const ExpensiveToCopyType' in object 'ncrs' is never modified; consider avoiding the copy
974+
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm1' of the subobject 'ncrs.s.Member' of type 'const ExpensiveToCopyType' is never modified; consider avoiding the copy
975975
// CHECK-FIXES: const auto& m1 = ncrs.s.Member;
976976
const auto m2 = ncs.s.Member;
977-
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm2' of the field 'Member' of type 'const ExpensiveToCopyType' in object 'ncs' is never modified; consider avoiding the copy
977+
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm2' of the subobject 'ncs.s.Member' of type 'const ExpensiveToCopyType' is never modified; consider avoiding the copy
978978
// CHECK-FIXES: const auto& m2 = ncs.s.Member;
979979
const auto m3 = nrs.s.Member;
980980
const auto m4 = ns.s.Member;
@@ -986,11 +986,11 @@ bool CopiedFromVarNestedField() {
986986
const NestedStruct ncrs;
987987
NestedStruct ns;
988988
const auto m1 = ncrs.s.Member;
989-
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm1' of the field 'Member' of type 'const ExpensiveToCopyType' in object 'ncrs' is never modified; consider avoiding the copy
989+
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm1' of the subobject 'ncrs.s.Member' of type 'const ExpensiveToCopyType' is never modified; consider avoiding the copy
990990
// CHECK-FIXES: const auto& m1 = ncrs.s.Member;
991991
const auto m2 = ns.s.Member;
992992
const auto m3 = GlobalNS.s.Member;
993-
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm3' of the field 'Member' of type 'const ExpensiveToCopyType' in object 'GlobalNS' is never modified; consider avoiding the copy
993+
// CHECK-MESSAGES: [[@LINE-1]]:14: warning: local copy 'm3' of the subobject 'GlobalNS.s.Member' of type 'const ExpensiveToCopyType' is never modified; consider avoiding the copy
994994
// CHECK-FIXES: const auto& m3 = GlobalNS.s.Member;
995995
return m1 == m2 || m2 == m3;
996996
}

0 commit comments

Comments
 (0)