From 783419657cda0565716d363928c8cf5ac5035f8c Mon Sep 17 00:00:00 2001 From: Stephan Bergmann Date: Thu, 21 Apr 2016 17:29:40 +0200 Subject: loplugin:salbool: sal_Bool[] -> bool[] Change-Id: I3c5bf7a53c9ae173f8fce885ecf022f092aa43a9 --- compilerplugins/clang/salbool.cxx | 89 ++++++++++++++++++++++++++++++++++----- 1 file changed, 78 insertions(+), 11 deletions(-) (limited to 'compilerplugins') diff --git a/compilerplugins/clang/salbool.cxx b/compilerplugins/clang/salbool.cxx index 6b3d8df0fac5..f70135805fa7 100644 --- a/compilerplugins/clang/salbool.cxx +++ b/compilerplugins/clang/salbool.cxx @@ -24,6 +24,13 @@ bool isSalBool(QualType type) { return t != nullptr && t->getDecl()->getNameAsString() == "sal_Bool"; } +bool isSalBoolArray(QualType type) { + auto t = type->getAsArrayTypeUnsafe(); + return t != nullptr + && (isSalBool(t->getElementType()) + || isSalBoolArray(t->getElementType())); +} + // Clang 3.2 FunctionDecl::isInlined doesn't work as advertised ("Determine // whether this function should be inlined, because it is either marked 'inline' // or 'constexpr' or is a member function of a class that was defined in the @@ -136,6 +143,8 @@ public: bool VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr * expr); + bool VisitReturnStmt(ReturnStmt const * stmt); + bool WalkUpFromParmVarDecl(ParmVarDecl const * decl); bool VisitParmVarDecl(ParmVarDecl const * decl); @@ -248,17 +257,27 @@ bool SalBool::VisitCallExpr(CallExpr * expr) { if (ft != nullptr) { for (unsigned i = 0; i != compat::getNumParams(*ft); ++i) { QualType t(compat::getParamType(*ft, i)); + bool b = false; if (t->isLValueReferenceType()) { t = t.getNonReferenceType(); - if (!t.isConstQualified() && isSalBool(t) - && i < expr->getNumArgs()) - { - DeclRefExpr * ref = dyn_cast(expr->getArg(i)); - if (ref != nullptr) { - VarDecl const * d = dyn_cast(ref->getDecl()); - if (d != nullptr) { - varDecls_.erase(d); - } + b = !t.isConstQualified() && isSalBool(t); + } else if (t->isPointerType()) { + for (;;) { + auto t2 = t->getAs(); + if (t2 == nullptr) { + break; + } + t = t2->getPointeeType(); + } + b = isSalBool(t); + } + if (b && i < expr->getNumArgs()) { + DeclRefExpr * ref = dyn_cast( + expr->getArg(i)->IgnoreParenImpCasts()); + if (ref != nullptr) { + VarDecl const * d = dyn_cast(ref->getDecl()); + if (d != nullptr) { + varDecls_.erase(d); } } } @@ -363,6 +382,53 @@ bool SalBool::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr * expr) { return true; } +bool SalBool::VisitReturnStmt(ReturnStmt const * stmt) { + // Just enough to avoid warnings in rtl_getUriCharClass (sal/rtl/uri.cxx), + // which has + // + // static sal_Bool const aCharClass[][nCharClassSize] = ...; + // + // and + // + // return aCharClass[eCharClass]; + // + if (ignoreLocation(stmt)) { + return true; + } + auto e = stmt->getRetValue(); + if (e == nullptr) { + return true; + } + auto t = e->getType(); + if (!t->isPointerType()) { + return true; + } + for (;;) { + auto t2 = t->getAs(); + if (t2 == nullptr) { + break; + } + t = t2->getPointeeType(); + } + if (!isSalBool(t)) { + return true; + } + auto e2 = dyn_cast(e->IgnoreParenImpCasts()); + if (e2 == nullptr) { + return true; + } + auto e3 = dyn_cast(e2->getBase()->IgnoreParenImpCasts()); + if (e3 == nullptr) { + return true; + } + auto d = dyn_cast(e3->getDecl()); + if (d == nullptr) { + return true; + } + varDecls_.erase(d); + return true; +} + bool SalBool::WalkUpFromParmVarDecl(ParmVarDecl const * decl) { return VisitParmVarDecl(decl); } @@ -467,7 +533,8 @@ bool SalBool::VisitVarDecl(VarDecl const * decl) { if (ignoreLocation(decl)) { return true; } - if (!decl->isExternC() && isSalBool(decl->getType()) + if (!decl->isExternC() + && (isSalBool(decl->getType()) || isSalBoolArray(decl->getType())) && !isInSpecialMainFile( compiler.getSourceManager().getSpellingLoc(decl->getLocStart()))) { @@ -484,7 +551,7 @@ bool SalBool::VisitFieldDecl(FieldDecl const * decl) { if (ignoreLocation(decl)) { return true; } - if (isSalBool(decl->getType()) + if ((isSalBool(decl->getType()) || isSalBoolArray(decl->getType())) && !isInSpecialMainFile( compiler.getSourceManager().getSpellingLoc(decl->getLocStart()))) { -- cgit