diff options
Diffstat (limited to 'compilerplugins/clang/rangedforcopy.cxx')
-rw-r--r-- | compilerplugins/clang/rangedforcopy.cxx | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/compilerplugins/clang/rangedforcopy.cxx b/compilerplugins/clang/rangedforcopy.cxx index 68004a1708ae..16e7746afdcb 100644 --- a/compilerplugins/clang/rangedforcopy.cxx +++ b/compilerplugins/clang/rangedforcopy.cxx @@ -12,6 +12,7 @@ #include <string> #include <iostream> +#include "check.hxx" #include "plugin.hxx" #include "clang/AST/CXXInheritance.h" @@ -45,18 +46,43 @@ bool RangedForCopy::VisitCXXForRangeStmt( const CXXForRangeStmt* stmt ) const VarDecl* varDecl = stmt->getLoopVariable(); if (!varDecl) return true; + if (isa<DecompositionDecl>(varDecl)) + { + // Assume that use of a non-reference structured binding is intentional: + return true; + } const QualType type = varDecl->getType(); - if (type->isRecordType() && !type->isReferenceType() && !type->isPointerType()) + if (!type->isRecordType() || type->isReferenceType() || type->isPointerType()) + return true; + + if (auto exprWithCleanups = dyn_cast<ExprWithCleanups>(varDecl->getInit())) + if (dyn_cast<CXXBindTemporaryExpr>(exprWithCleanups->getSubExpr()->IgnoreImpCasts())) + return true; + + if (loplugin::TypeCheck(type).Class("__bit_const_reference").StdNamespace()) { - std::string name = type.getAsString(); - report( - DiagnosticsEngine::Warning, - "Loop variable passed by value, pass by reference instead, e.g. 'const %0&'", - compat::getBeginLoc(varDecl)) - << name << varDecl->getSourceRange(); + // With libc++ without _LIBCPP_ABI_BITSET_VECTOR_BOOL_CONST_SUBSCRIPT_RETURN_BOOL, + // iterating over a const std::vector<bool> non-compliantly uses a variable of some + // internal __bit_const_reference class type, rather than of type bool (see + // <https://reviews.llvm.org/D123851> "[libc++] Change + // vector<bool>::const_iterator::reference to bool in ABIv2"): + return true; } + // trivial class, ignore it + if (loplugin::TypeCheck(type).Class("TypedWhichId").GlobalNamespace()) + return true; + if (loplugin::TypeCheck(type).Typedef("OUStringChar").Namespace("rtl")) + return true; + + std::string name = type.getAsString(); + report( + DiagnosticsEngine::Warning, + "Loop variable passed by value, pass by reference instead, e.g. 'const %0&'", + varDecl->getBeginLoc()) + << name << varDecl->getSourceRange(); + return true; } |