Skip to content

Commit

Permalink
usage of 'this' in member methods cancels static suggestion
Browse files Browse the repository at this point in the history
  • Loading branch information
rizsotto committed Oct 9, 2012
1 parent bcb635e commit a5e8555
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 33 deletions.
39 changes: 39 additions & 0 deletions sources/IsCXXThisExpr.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2012 by Laszlo Nagy [see file MIT-LICENSE]

#ifndef _IsCXXThisExpr_hpp_
#define _IsCXXThisExpr_hpp_

#include <boost/noncopyable.hpp>

#include <clang/AST/AST.h>
#include <clang/AST/RecursiveASTVisitor.h>

// These are helper struct/method to figure out was it a member
// method call or a call on a variable.
class IsCXXThisExpr
: public boost::noncopyable
, public clang::RecursiveASTVisitor<IsCXXThisExpr> {
public:
static bool Check(clang::Stmt const * const Stmt) {
IsCXXThisExpr V;
V.TraverseStmt(const_cast<clang::Stmt*>(Stmt));
return V.Found;
}

// public visitor method.
bool VisitCXXThisExpr(clang::CXXThisExpr const *) {
Found = true;
return true;
}

private:
IsCXXThisExpr()
: boost::noncopyable()
, clang::RecursiveASTVisitor<IsCXXThisExpr>()
, Found(false)
{ }

bool Found;
};

#endif // _IsCXXThisExpr_hpp_
4 changes: 3 additions & 1 deletion sources/ModuleAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "ModuleAnalysis.hpp"
#include "ScopeAnalysis.hpp"
#include "IsCXXThisExpr.hpp"

#include <iterator>
#include <map>
Expand Down Expand Up @@ -339,7 +340,8 @@ class AnalyseVariableUsage
MemberFunctions |
boost::adaptors::filtered(IsMemberMethod()),
boost::bind(&ScopeAnalysis::WasReferenced, &Analysis, _1));
if ((0 == MemberAccess) && (0 == FunctionAccess)) {
if ((0 == MemberAccess) && (0 == FunctionAccess)
&& (! IsCXXThisExpr::Check(F->getBody()))) {
StaticCandidates.insert(F);
} else if (! F->isConst()) {
ConstCandidates.insert(F);
Expand Down
35 changes: 3 additions & 32 deletions sources/ScopeAnalysis.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright 2012 by Laszlo Nagy [see file MIT-LICENSE]

#include "ScopeAnalysis.hpp"
#include "IsCXXThisExpr.hpp"

#include <boost/noncopyable.hpp>
#include <boost/bind.hpp>
#include <boost/range.hpp>
#include <boost/range/algorithm/for_each.hpp>
Expand All @@ -11,35 +11,6 @@

namespace {

// These are helper struct/method to figure out was it a member
// method call or a call on a variable.
class IsCXXThisExpr
: public boost::noncopyable
, public clang::RecursiveASTVisitor<IsCXXThisExpr> {
public:
static bool Check(clang::Expr const * const E) {
IsCXXThisExpr V;
clang::Stmt const * const Stmt = E;
V.TraverseStmt(const_cast<clang::Stmt*>(Stmt));
return V.Found;
}

// public visitor method.
bool VisitCXXThisExpr(clang::CXXThisExpr const *) {
Found = true;
return true;
}

private:
IsCXXThisExpr()
: boost::noncopyable()
, clang::RecursiveASTVisitor<IsCXXThisExpr>()
, Found(false)
{ }

bool Found;
};

// Usage extract method implemented in visitor style.
class UsageExtractor
: public clang::RecursiveASTVisitor<UsageExtractor> {
Expand Down Expand Up @@ -147,7 +118,7 @@ class UsageRefCollector {
if (clang::DeclaratorDecl const * const VD = U.first) {
ScopeAnalysis::UsageRefsMap::iterator It = Results.find(VD);
if (Results.end() == It) {
std::pair<ScopeAnalysis::UsageRefsMap::iterator, bool> R =
std::pair<ScopeAnalysis::UsageRefsMap::iterator, bool> const R =
Results.insert(ScopeAnalysis::UsageRefsMap::value_type(VD, ScopeAnalysis::UsageRefs()));
It = R.first;
}
Expand All @@ -169,7 +140,7 @@ class UsageRefCollector {
unsigned const Id = DE.getCustomDiagID(clang::DiagnosticsEngine::Note, Message);
ScopeAnalysis::UsageRefs const & Ls = Var.second;
for (ScopeAnalysis::UsageRefs::const_iterator It(Ls.begin()), End(Ls.end()); It != End; ++It) {
clang::DiagnosticBuilder DB = DE.Report(It->second.getBegin(), Id);
clang::DiagnosticBuilder const DB = DE.Report(It->second.getBegin(), Id);
DB << Var.first->getNameAsString();
DB << It->first.getAsString();
DB.setForceEmit();
Expand Down
7 changes: 7 additions & 0 deletions test/PseudoConstAnalysis/MethodConstness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ struct BaseOne {

int f4() const;
static int sf1();

int f5() const;
};

struct BaseTwo {
Expand Down Expand Up @@ -62,6 +64,11 @@ int BaseOne::f4() const { // expected-warning {{function 'f4' could be declared
return 8;
}

int BaseOne::f5() const {
BaseOne const Value = *this;
return 8;
}

int BaseOne::sf1() {
return 8;
}
Expand Down

0 comments on commit a5e8555

Please sign in to comment.