summaryrefslogtreecommitdiff
path: root/compilerplugins
diff options
context:
space:
mode:
authorNoel Grandin <noel.grandin@collabora.co.uk>2017-09-20 16:22:27 +0200
committerNoel Grandin <noel.grandin@collabora.co.uk>2017-09-21 10:01:43 +0200
commite8b5ec6590bbde63f3bbe50d049a3307d9d5682c (patch)
tree1ef1d60c4ff7d0a952dd93cf8b6b3c3bc71f4cb9 /compilerplugins
parentd6e8e522a396f97f71a92d9b5d7e03947a4ad803 (diff)
loplugin:unusedfields, fix var taking ref
Change-Id: I0ea1f0c7488c140fca9f64de326c6ac588ece925
Diffstat (limited to 'compilerplugins')
-rw-r--r--compilerplugins/clang/test/unusedfields.cxx18
-rw-r--r--compilerplugins/clang/unusedfields.cxx13
2 files changed, 27 insertions, 4 deletions
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<int> 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<VarDecl>(parentsRange.begin()->get<Decl>());
+ // 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<CXXReinterpretCastExpr>(parent))