summaryrefslogtreecommitdiff
path: root/compilerplugins/clang
diff options
context:
space:
mode:
authorStephan Bergmann <sbergman@redhat.com>2015-12-10 17:33:54 +0100
committerStephan Bergmann <sbergman@redhat.com>2015-12-10 17:34:31 +0100
commit4a1edf626ad48b5955892e5590d75fa7ae5eaf58 (patch)
tree3ace4c2ac4da5529295780880f67fdfce09b6637 /compilerplugins/clang
parent46fe3bddebf30775ae19eaa0fefe1d8e2f78eced (diff)
More loplugin:nullptr automatic rewrite (within templates)
Change-Id: I9bc06cfb5eeb38fd7ae7fb25f876ea9f96e4a65a
Diffstat (limited to 'compilerplugins/clang')
-rw-r--r--compilerplugins/clang/store/nullptr.cxx68
1 files changed, 68 insertions, 0 deletions
diff --git a/compilerplugins/clang/store/nullptr.cxx b/compilerplugins/clang/store/nullptr.cxx
index 8bed1ce40424..6bf4e54d3f15 100644
--- a/compilerplugins/clang/store/nullptr.cxx
+++ b/compilerplugins/clang/store/nullptr.cxx
@@ -44,6 +44,12 @@ public:
bool VisitGNUNullExpr(GNUNullExpr const * expr);
+ bool VisitBinaryOperator(BinaryOperator const * expr);
+
+ bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr const * expr);
+
+ // bool shouldVisitTemplateInstantiations() const { return true; }
+
private:
bool isInLokIncludeFile(SourceLocation spellingLocation) const;
@@ -111,6 +117,68 @@ bool Nullptr::VisitGNUNullExpr(GNUNullExpr const * expr) {
return true;
}
+bool Nullptr::VisitBinaryOperator(BinaryOperator const * expr) {
+ if (ignoreLocation(expr)) {
+ return true;
+ }
+ Expr const * e;
+ switch (expr->getOpcode()) {
+ case BO_EQ:
+ case BO_NE:
+ if (expr->getRHS()->getType()->isPointerType()) {
+ e = expr->getLHS();
+ break;
+ }
+ // fall through
+ case BO_Assign:
+ if (expr->getLHS()->getType()->isPointerType()) {
+ e = expr->getRHS();
+ break;
+ }
+ // fall through
+ default:
+ return true;
+ }
+ //TODO: detect NPCK_ZeroExpression where appropriate
+ auto const lit = dyn_cast<IntegerLiteral>(e->IgnoreParenImpCasts());
+ if (lit == nullptr || lit->getValue().getBoolValue()) {
+ return true;
+ }
+ handleNull(e, nullptr, Expr::NPCK_ZeroLiteral);
+ return true;
+}
+
+bool Nullptr::VisitCXXOperatorCallExpr(CXXOperatorCallExpr const * expr) {
+ if (ignoreLocation(expr)) {
+ return true;
+ }
+ Expr const * e;
+ switch (expr->getOperator()) {
+ case OO_EqualEqual:
+ case OO_ExclaimEqual:
+ if (expr->getArg(1)->getType()->isPointerType()) {
+ e = expr->getArg(0);
+ break;
+ }
+ // fall through
+ case OO_Equal:
+ if (expr->getArg(0)->getType()->isPointerType()) {
+ e = expr->getArg(1);
+ break;
+ }
+ // fall through
+ default:
+ return true;
+ }
+ //TODO: detect NPCK_ZeroExpression where appropriate
+ auto const lit = dyn_cast<IntegerLiteral>(e->IgnoreParenImpCasts());
+ if (lit == nullptr || lit->getValue().getBoolValue()) {
+ return true;
+ }
+ handleNull(e, nullptr, Expr::NPCK_ZeroLiteral);
+ return true;
+}
+
bool Nullptr::isInLokIncludeFile(SourceLocation spellingLocation) const {
return compiler.getSourceManager().getFilename(spellingLocation)
.startswith(SRCDIR "/include/LibreOfficeKit/");