From e8b5ec6590bbde63f3bbe50d049a3307d9d5682c Mon Sep 17 00:00:00 2001 From: Noel Grandin Date: Wed, 20 Sep 2017 16:22:27 +0200 Subject: loplugin:unusedfields, fix var taking ref Change-Id: I0ea1f0c7488c140fca9f64de326c6ac588ece925 --- compilerplugins/clang/test/unusedfields.cxx | 18 ++++++++++++++---- compilerplugins/clang/unusedfields.cxx | 13 +++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) (limited to 'compilerplugins') diff --git a/compilerplugins/clang/test/unusedfields.cxx b/compilerplugins/clang/test/unusedfields.cxx index 71489b018843..21e59fcbbdd1 100644 --- a/compilerplugins/clang/test/unusedfields.cxx +++ b/compilerplugins/clang/test/unusedfields.cxx @@ -116,16 +116,19 @@ struct ReadOnlyAnalysis // expected-error@-2 {{read m_f3 [loplugin:unusedfields]}} // expected-error@-3 {{read m_f4 [loplugin:unusedfields]}} // expected-error@-4 {{read m_f5 [loplugin:unusedfields]}} -// expected-error@-5 {{write m_f2 [loplugin:unusedfields]}} -// expected-error@-6 {{write m_f3 [loplugin:unusedfields]}} -// expected-error@-7 {{write m_f4 [loplugin:unusedfields]}} -// expected-error@-8 {{write m_f5 [loplugin:unusedfields]}} +// expected-error@-5 {{read m_f6 [loplugin:unusedfields]}} +// expected-error@-6 {{write m_f2 [loplugin:unusedfields]}} +// expected-error@-7 {{write m_f3 [loplugin:unusedfields]}} +// expected-error@-8 {{write m_f4 [loplugin:unusedfields]}} +// expected-error@-9 {{write m_f5 [loplugin:unusedfields]}} +// expected-error@-10 {{write m_f6 [loplugin:unusedfields]}} { int m_f1; int m_f2; int m_f3; std::vector m_f4; int m_f5; + int m_f6; // check that we dont see a write of m_f1 ReadOnlyAnalysis() : m_f1(0) {} @@ -141,6 +144,13 @@ struct ReadOnlyAnalysis // check that we see a write when we pass by non-const ref void method5() { ReadOnly1 a(m_f5); } + + // check that we see a write when we pass by non-const ref + void method6() + { + int& r = m_f6; + r = 1; + } }; struct ReadOnlyAnalysis2 diff --git a/compilerplugins/clang/unusedfields.cxx b/compilerplugins/clang/unusedfields.cxx index 69ea4be9c8b2..af2be7ba6076 100644 --- a/compilerplugins/clang/unusedfields.cxx +++ b/compilerplugins/clang/unusedfields.cxx @@ -611,6 +611,19 @@ void UnusedFields::checkReadOnly(const FieldDecl* fieldDecl, const Expr* memberE { if (!parent) { + // check if we have an expression like + // int& r = m_field; + auto parentsRange = compiler.getASTContext().getParents(*child); + if (parentsRange.begin() != parentsRange.end()) + { + auto varDecl = dyn_cast_or_null(parentsRange.begin()->get()); + // The isImplicit() call is to avoid triggering when we see the vardecl which is part of a for-range statement, + // which is of type 'T&&' and also an l-value-ref ? + if (varDecl && !varDecl->isImplicit() && loplugin::TypeCheck(varDecl->getType()).LvalueReference().NonConst()) + { + bPotentiallyWrittenTo = true; + } + } break; } if (isa(parent)) -- cgit