loplugin:changerectanglegetref also fix Point and Size

Change-Id: I373af0a62e3785c4abc2d27b0b31121c9d596ca3
This commit is contained in:
Noel Grandin
2018-02-12 10:24:44 +02:00
parent fc80919370
commit 78bd5d5fb4

View File

@@ -36,11 +36,12 @@ public:
bool VisitCXXMemberCallExpr(CXXMemberCallExpr const* call); bool VisitCXXMemberCallExpr(CXXMemberCallExpr const* call);
private: private:
bool ChangeAssignment(Stmt const* parent, std::string const& methodName); bool ChangeAssignment(Stmt const* parent, std::string const& methodName,
std::string const& setPrefix);
bool ChangeBinaryOperator(BinaryOperator const* parent, CXXMemberCallExpr const* call, bool ChangeBinaryOperator(BinaryOperator const* parent, CXXMemberCallExpr const* call,
std::string const& methodName); std::string const& methodName, std::string const& setPrefix);
bool ChangeUnaryOperator(UnaryOperator const* parent, CXXMemberCallExpr const* call, bool ChangeUnaryOperator(UnaryOperator const* parent, CXXMemberCallExpr const* call,
std::string const& methodName); std::string const& methodName, std::string const& setPrefix);
std::string extractCode(SourceLocation startLoc, SourceLocation endLoc); std::string extractCode(SourceLocation startLoc, SourceLocation endLoc);
}; };
@@ -60,14 +61,47 @@ bool ChangeRectangleGetRef::VisitCXXMemberCallExpr(CXXMemberCallExpr const* call
return true; return true;
auto dc = loplugin::DeclCheck(func); auto dc = loplugin::DeclCheck(func);
std::string methodName; std::string methodName;
std::string setPrefix;
if (dc.Function("Top").Class("Rectangle").Namespace("tools").GlobalNamespace()) if (dc.Function("Top").Class("Rectangle").Namespace("tools").GlobalNamespace())
{
methodName = "Top"; methodName = "Top";
setPrefix = "Set";
}
else if (dc.Function("Bottom").Class("Rectangle").Namespace("tools").GlobalNamespace()) else if (dc.Function("Bottom").Class("Rectangle").Namespace("tools").GlobalNamespace())
{
methodName = "Bottom"; methodName = "Bottom";
setPrefix = "Set";
}
else if (dc.Function("Left").Class("Rectangle").Namespace("tools").GlobalNamespace()) else if (dc.Function("Left").Class("Rectangle").Namespace("tools").GlobalNamespace())
{
methodName = "Left"; methodName = "Left";
setPrefix = "Set";
}
else if (dc.Function("Right").Class("Rectangle").Namespace("tools").GlobalNamespace()) else if (dc.Function("Right").Class("Rectangle").Namespace("tools").GlobalNamespace())
{
methodName = "Right"; methodName = "Right";
setPrefix = "Set";
}
else if (dc.Function("X").Class("Point").GlobalNamespace())
{
methodName = "X";
setPrefix = "set";
}
else if (dc.Function("Y").Class("Point").GlobalNamespace())
{
methodName = "Y";
setPrefix = "set";
}
else if (dc.Function("Width").Class("Size").GlobalNamespace())
{
methodName = "Width";
setPrefix = "set";
}
else if (dc.Function("Height").Class("Size").GlobalNamespace())
{
methodName = "Height";
setPrefix = "set";
}
else else
return true; return true;
if (!loplugin::TypeCheck(func->getReturnType()).LvalueReference()) if (!loplugin::TypeCheck(func->getReturnType()).LvalueReference())
@@ -78,7 +112,7 @@ bool ChangeRectangleGetRef::VisitCXXMemberCallExpr(CXXMemberCallExpr const* call
return true; return true;
if (auto unaryOp = dyn_cast<UnaryOperator>(parent)) if (auto unaryOp = dyn_cast<UnaryOperator>(parent))
{ {
if (!ChangeUnaryOperator(unaryOp, call, methodName)) if (!ChangeUnaryOperator(unaryOp, call, methodName, setPrefix))
report(DiagnosticsEngine::Warning, "Could not fix this one1", call->getLocStart()); report(DiagnosticsEngine::Warning, "Could not fix this one1", call->getLocStart());
return true; return true;
} }
@@ -92,21 +126,22 @@ bool ChangeRectangleGetRef::VisitCXXMemberCallExpr(CXXMemberCallExpr const* call
auto opcode = binaryOp->getOpcode(); auto opcode = binaryOp->getOpcode();
if (opcode == BO_Assign) if (opcode == BO_Assign)
{ {
if (!ChangeAssignment(parent, methodName)) if (!ChangeAssignment(parent, methodName, setPrefix))
report(DiagnosticsEngine::Warning, "Could not fix this one4", call->getLocStart()); report(DiagnosticsEngine::Warning, "Could not fix this one4", call->getLocStart());
return true; return true;
} }
if (opcode == BO_RemAssign || opcode == BO_AddAssign || opcode == BO_SubAssign if (opcode == BO_RemAssign || opcode == BO_AddAssign || opcode == BO_SubAssign
|| opcode == BO_MulAssign || opcode == BO_DivAssign) || opcode == BO_MulAssign || opcode == BO_DivAssign)
{ {
if (!ChangeBinaryOperator(binaryOp, call, methodName)) if (!ChangeBinaryOperator(binaryOp, call, methodName, setPrefix))
report(DiagnosticsEngine::Warning, "Could not fix this one5", call->getLocStart()); report(DiagnosticsEngine::Warning, "Could not fix this one5", call->getLocStart());
return true; return true;
} }
return true; return true;
} }
bool ChangeRectangleGetRef::ChangeAssignment(Stmt const* parent, std::string const& methodName) bool ChangeRectangleGetRef::ChangeAssignment(Stmt const* parent, std::string const& methodName,
std::string const& setPrefix)
{ {
// Look for expressions like // Look for expressions like
// aRect.Left() = ...; // aRect.Left() = ...;
@@ -124,7 +159,7 @@ bool ChangeRectangleGetRef::ChangeAssignment(Stmt const* parent, std::string con
auto originalLength = callText.size(); auto originalLength = callText.size();
auto newText = std::regex_replace(callText, std::regex(methodName + "\\(\\) *="), auto newText = std::regex_replace(callText, std::regex(methodName + "\\(\\) *="),
"Set" + methodName + "("); setPrefix + methodName + "(");
if (newText == callText) if (newText == callText)
return false; return false;
newText += " )"; newText += " )";
@@ -134,7 +169,8 @@ bool ChangeRectangleGetRef::ChangeAssignment(Stmt const* parent, std::string con
bool ChangeRectangleGetRef::ChangeBinaryOperator(BinaryOperator const* binaryOp, bool ChangeRectangleGetRef::ChangeBinaryOperator(BinaryOperator const* binaryOp,
CXXMemberCallExpr const* call, CXXMemberCallExpr const* call,
std::string const& methodName) std::string const& methodName,
std::string const& setPrefix)
{ {
// Look for expressions like // Look for expressions like
// aRect.Left() += ...; // aRect.Left() += ...;
@@ -182,14 +218,14 @@ bool ChangeRectangleGetRef::ChangeBinaryOperator(BinaryOperator const* binaryOp,
auto implicitObjectText = extractCode(call->getImplicitObjectArgument()->getExprLoc(), auto implicitObjectText = extractCode(call->getImplicitObjectArgument()->getExprLoc(),
call->getImplicitObjectArgument()->getExprLoc()); call->getImplicitObjectArgument()->getExprLoc());
auto newText = std::regex_replace(callText, std::regex(methodName + "\\(\\) *" + regexOpname), auto newText = std::regex_replace(callText, std::regex(methodName + "\\(\\) *" + regexOpname),
"Set" + methodName + "( " + implicitObjectText + ".Get" setPrefix + methodName + "( " + implicitObjectText + "."
+ methodName + "() " + replaceOpname + " "); + methodName + "() " + replaceOpname + " ");
if (newText == callText) if (newText == callText)
return false; return false;
// sometimes we end up with duplicate spaces after the opname // sometimes we end up with duplicate spaces after the opname
newText = std::regex_replace( newText
newText, std::regex("Get" + methodName + "\\(\\) \\" + replaceOpname + " "), = std::regex_replace(newText, std::regex(methodName + "\\(\\) \\" + replaceOpname + " "),
"Get" + methodName + "() " + replaceOpname + " "); methodName + "() " + replaceOpname + " ");
newText += " )"; newText += " )";
return replaceText(startLoc, originalLength, newText); return replaceText(startLoc, originalLength, newText);
@@ -197,7 +233,8 @@ bool ChangeRectangleGetRef::ChangeBinaryOperator(BinaryOperator const* binaryOp,
bool ChangeRectangleGetRef::ChangeUnaryOperator(UnaryOperator const* unaryOp, bool ChangeRectangleGetRef::ChangeUnaryOperator(UnaryOperator const* unaryOp,
CXXMemberCallExpr const* call, CXXMemberCallExpr const* call,
std::string const& methodName) std::string const& methodName,
std::string const& setPrefix)
{ {
// Look for expressions like // Look for expressions like
// aRect.Left()++; // aRect.Left()++;
@@ -238,16 +275,16 @@ bool ChangeRectangleGetRef::ChangeUnaryOperator(UnaryOperator const* unaryOp,
{ {
auto newText auto newText
= std::regex_replace(callText, std::regex(methodName + "\\(\\) *" + regexOpname), = std::regex_replace(callText, std::regex(methodName + "\\(\\) *" + regexOpname),
"Set" + methodName + "( " + replaceOpname + implicitObjectText setPrefix + methodName + "( " + replaceOpname + implicitObjectText
+ ".Get" + methodName + "()"); + "." + methodName + "()");
return replaceText(startLoc, originalLength, newText); return replaceText(startLoc, originalLength, newText);
} }
else else
{ {
auto newText auto newText
= std::regex_replace(callText, std::regex(regexOpname + " *" + methodName + "\\(\\)"), = std::regex_replace(callText, std::regex(regexOpname + " *" + methodName + "\\(\\)"),
"Set" + methodName + "( " + replaceOpname + implicitObjectText setPrefix + methodName + "( " + replaceOpname + implicitObjectText
+ ".Get" + methodName + "()"); + "." + methodName + "()");
return replaceText(startLoc, originalLength, newText); return replaceText(startLoc, originalLength, newText);
} }
} }