aboutsummaryrefslogtreecommitdiffstats
path: root/src/checks/level1/foreach.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/checks/level1/foreach.cpp')
-rw-r--r--src/checks/level1/foreach.cpp40
1 files changed, 19 insertions, 21 deletions
diff --git a/src/checks/level1/foreach.cpp b/src/checks/level1/foreach.cpp
index 881eea47..8f00b8ef 100644
--- a/src/checks/level1/foreach.cpp
+++ b/src/checks/level1/foreach.cpp
@@ -28,8 +28,8 @@
#include "HierarchyUtils.h"
#include "QtUtils.h"
#include "TypeUtils.h"
-#include "checkmanager.h"
#include "PreProcessorVisitor.h"
+#include "StringUtils.h"
#include <clang/AST/AST.h>
@@ -37,7 +37,7 @@ using namespace clang;
using namespace std;
Foreach::Foreach(const std::string &name, ClazyContext *context)
- : CheckBase(name, context)
+ : CheckBase(name, context, Option_CanIgnoreIncludes)
{
context->enablePreprocessorVisitor();
}
@@ -60,22 +60,22 @@ void Foreach::VisitStmt(clang::Stmt *stmt)
if (!m_lastForStmt)
return;
- CXXConstructExpr *constructExpr = dyn_cast<CXXConstructExpr>(stmt);
+ auto constructExpr = dyn_cast<CXXConstructExpr>(stmt);
if (!constructExpr || constructExpr->getNumArgs() < 1)
return;
CXXConstructorDecl *constructorDecl = constructExpr->getConstructor();
- if (!constructorDecl || constructorDecl->getNameAsString() != "QForeachContainer")
+ if (!constructorDecl || clazy::name(constructorDecl) != "QForeachContainer")
return;
vector<DeclRefExpr*> declRefExprs;
- HierarchyUtils::getChilds<DeclRefExpr>(constructExpr, declRefExprs);
+ clazy::getChilds<DeclRefExpr>(constructExpr, declRefExprs);
if (declRefExprs.empty())
return;
// Get the container value declaration
DeclRefExpr *declRefExpr = declRefExprs.front();
- ValueDecl *valueDecl = dyn_cast<ValueDecl>(declRefExpr->getDecl());
+ auto valueDecl = dyn_cast<ValueDecl>(declRefExpr->getDecl());
if (!valueDecl)
return;
@@ -89,17 +89,17 @@ void Foreach::VisitStmt(clang::Stmt *stmt)
return;
auto rootBaseClass = Utils::rootBaseClass(containerRecord);
- const string containerClassName = rootBaseClass->getNameAsString();
- const bool isQtContainer = QtUtils::isQtIterableClass(containerClassName);
+ StringRef containerClassName = clazy::name(rootBaseClass);
+ const bool isQtContainer = clazy::isQtIterableClass(containerClassName);
if (containerClassName.empty()) {
- emitWarning(stmt->getLocStart(), "internal error, couldn't get class name of foreach container, please report a bug");
+ emitWarning(getLocStart(stmt), "internal error, couldn't get class name of foreach container, please report a bug");
return;
} else {
if (!isQtContainer) {
- emitWarning(stmt->getLocStart(), "foreach with STL container causes deep-copy (" + rootBaseClass->getQualifiedNameAsString() + ')');
+ emitWarning(getLocStart(stmt), "foreach with STL container causes deep-copy (" + rootBaseClass->getQualifiedNameAsString() + ')');
return;
} else if (containerClassName == "QVarLengthArray") {
- emitWarning(stmt->getLocStart(), "foreach with QVarLengthArray causes deep-copy");
+ emitWarning(getLocStart(stmt), "foreach with QVarLengthArray causes deep-copy");
return;
}
}
@@ -115,7 +115,7 @@ void Foreach::VisitStmt(clang::Stmt *stmt)
// Now look inside the for statement for detachments
if (containsDetachments(m_lastForStmt, valueDecl)) {
- emitWarning(stmt->getLocStart(), "foreach container detached");
+ emitWarning(getLocStart(stmt), "foreach container detached");
}
}
@@ -123,13 +123,13 @@ void Foreach::checkBigTypeMissingRef()
{
// Get the inner forstm
vector<ForStmt*> forStatements;
- HierarchyUtils::getChilds<ForStmt>(m_lastForStmt->getBody(), forStatements);
+ clazy::getChilds<ForStmt>(m_lastForStmt->getBody(), forStatements);
if (forStatements.empty())
return;
// Get the variable declaration (lhs of foreach)
vector<DeclStmt*> varDecls;
- HierarchyUtils::getChilds<DeclStmt>(forStatements.at(0), varDecls);
+ clazy::getChilds<DeclStmt>(forStatements.at(0), varDecls);
if (varDecls.empty())
return;
@@ -157,7 +157,7 @@ void Foreach::checkBigTypeMissingRef()
return;
}
- emitWarning(varDecl->getLocStart(), error.c_str());
+ emitWarning(getLocStart(varDecl), error.c_str());
}
}
@@ -174,17 +174,17 @@ bool Foreach::containsDetachments(Stmt *stm, clang::ValueDecl *containerValueDec
auto recordDecl = dyn_cast<CXXRecordDecl>(declContext);
if (recordDecl) {
const std::string className = Utils::rootBaseClass(recordDecl)->getQualifiedNameAsString();
- const std::unordered_map<string, std::vector<string> > &detachingMethodsMap = QtUtils::detachingMethods();
+ const std::unordered_map<string, std::vector<StringRef> > &detachingMethodsMap = clazy::detachingMethods();
if (detachingMethodsMap.find(className) != detachingMethodsMap.end()) {
const std::string functionName = valDecl->getNameAsString();
const auto &allowedFunctions = detachingMethodsMap.at(className);
- if (clazy_std::contains(allowedFunctions, functionName)) {
+ if (clazy::contains(allowedFunctions, functionName)) {
Expr *expr = memberExpr->getBase();
if (expr) {
DeclRefExpr *refExpr = dyn_cast<DeclRefExpr>(expr);
if (!refExpr) {
- auto s = HierarchyUtils::getFirstChildAtDepth(expr, 1);
+ auto s = clazy::getFirstChildAtDepth(expr, 1);
refExpr = dyn_cast<DeclRefExpr>(s);
if (refExpr) {
if (refExpr->getDecl() == containerValueDecl) { // Finally, check if this non-const member call is on the same container we're iterating
@@ -199,9 +199,7 @@ bool Foreach::containsDetachments(Stmt *stm, clang::ValueDecl *containerValueDec
}
}
- return clazy_std::any_of(stm->children(), [this, containerValueDecl](Stmt *child) {
+ return clazy::any_of(stm->children(), [this, containerValueDecl](Stmt *child) {
return this->containsDetachments(child, containerValueDecl);
});
}
-
-REGISTER_CHECK("foreach", Foreach, CheckLevel1)