diff options
author | Sergio Martins <smartins@kde.org> | 2019-05-11 23:45:09 +0100 |
---|---|---|
committer | Sergio Martins <smartins@kde.org> | 2019-05-11 23:45:09 +0100 |
commit | 2ea365b34208e6c7c7349cfd1b0063f75dd48af0 (patch) | |
tree | f6948c292efbd4260491f769b5963563ae1208f7 | |
parent | df640cb267a4c2980d18f3baed212f87e8a998fd (diff) |
TypeUtils: Make it optional to pass the VarDecl
So we can test generic QualTypes too
-rw-r--r-- | src/TypeUtils.cpp | 21 | ||||
-rw-r--r-- | src/TypeUtils.h | 2 | ||||
-rw-r--r-- | src/checks/level1/foreach.cpp | 2 | ||||
-rw-r--r-- | src/checks/level1/range-loop.cpp | 2 | ||||
-rw-r--r-- | src/checks/level2/function-args-by-ref.cpp | 4 | ||||
-rw-r--r-- | src/checks/level2/function-args-by-value.cpp | 4 |
6 files changed, 17 insertions, 18 deletions
diff --git a/src/TypeUtils.cpp b/src/TypeUtils.cpp index 03a699ef..78987794 100644 --- a/src/TypeUtils.cpp +++ b/src/TypeUtils.cpp @@ -36,28 +36,27 @@ using namespace clang; -bool TypeUtils::classifyQualType(const ClazyContext *context, const VarDecl *varDecl, QualTypeClassification &classif, clang::Stmt *body) +bool TypeUtils::classifyQualType(const ClazyContext *context, clang::QualType qualType, + const VarDecl *varDecl, QualTypeClassification &classif, + clang::Stmt *body) { - if (!varDecl) - return false; - - QualType qualType = TypeUtils::unrefQualType(varDecl->getType()); - const Type *paramType = qualType.getTypePtrOrNull(); + QualType unrefQualType = TypeUtils::unrefQualType(qualType); + const Type *paramType = unrefQualType.getTypePtrOrNull(); if (!paramType || paramType->isIncompleteType()) return false; if (isUndeducibleAuto(paramType)) return false; - classif.size_of_T = context->astContext.getTypeSize(qualType) / 8; + classif.size_of_T = context->astContext.getTypeSize(unrefQualType) / 8; classif.isBig = classif.size_of_T > 16; CXXRecordDecl *recordDecl = paramType->getAsCXXRecordDecl(); CXXMethodDecl *copyCtor = recordDecl ? Utils::copyCtor(recordDecl) : nullptr; classif.isNonTriviallyCopyable = recordDecl && (recordDecl->hasNonTrivialCopyConstructor() || recordDecl->hasNonTrivialDestructor() || (copyCtor && copyCtor->isDeleted())); - classif.isReference = varDecl->getType()->isLValueReferenceType(); - classif.isConst = qualType.isConstQualified(); + classif.isReference = qualType->isLValueReferenceType(); + classif.isConst = unrefQualType.isConstQualified(); - if (varDecl->getType()->isRValueReferenceType()) // && ref, nothing to do here + if (qualType->isRValueReferenceType()) // && ref, nothing to do here return true; if (classif.isConst && !classif.isReference) { @@ -67,7 +66,7 @@ bool TypeUtils::classifyQualType(const ClazyContext *context, const VarDecl *var } } else if (classif.isConst && classif.isReference && !classif.isNonTriviallyCopyable && !classif.isBig) { classif.passSmallTrivialByValue = true; - } else if (!classif.isConst && !classif.isReference && (classif.isBig || classif.isNonTriviallyCopyable)) { + } else if (varDecl && !classif.isConst && !classif.isReference && (classif.isBig || classif.isNonTriviallyCopyable)) { if (body && (Utils::containsNonConstMemberCall(context->parentMap, body, varDecl) || Utils::isPassedToFunction(StmtBodyRange(body), varDecl, /*byrefonly=*/ true))) return true; diff --git a/src/TypeUtils.h b/src/TypeUtils.h index 9c98aaf7..f1f7d6b5 100644 --- a/src/TypeUtils.h +++ b/src/TypeUtils.h @@ -78,7 +78,7 @@ struct QualTypeClassification { * The optional parameter body is in order to advise non-const-ref -> value, since the body * needs to be inspected to see if we that would compile. */ -bool classifyQualType(const ClazyContext *context, const clang::VarDecl *varDecl, +bool classifyQualType(const ClazyContext *context, clang::QualType qualType, const clang::VarDecl *varDecl, QualTypeClassification &classification, clang::Stmt *body = nullptr); diff --git a/src/checks/level1/foreach.cpp b/src/checks/level1/foreach.cpp index 7e00f058..702ba2f1 100644 --- a/src/checks/level1/foreach.cpp +++ b/src/checks/level1/foreach.cpp @@ -157,7 +157,7 @@ void Foreach::checkBigTypeMissingRef() return; TypeUtils::QualTypeClassification classif; - bool success = TypeUtils::classifyQualType(m_context, varDecl, /*by-ref*/ classif, forStatements.at(0)); + bool success = TypeUtils::classifyQualType(m_context, varDecl->getType(), varDecl, /*by-ref*/ classif, forStatements.at(0)); if (!success) return; diff --git a/src/checks/level1/range-loop.cpp b/src/checks/level1/range-loop.cpp index a72fefb0..1a9247e1 100644 --- a/src/checks/level1/range-loop.cpp +++ b/src/checks/level1/range-loop.cpp @@ -135,7 +135,7 @@ void RangeLoop::checkPassByConstRefCorrectness(CXXForRangeStmt *rangeLoop) { TypeUtils::QualTypeClassification classif; auto varDecl = rangeLoop->getLoopVariable(); - bool success = TypeUtils::classifyQualType(m_context, varDecl, /*by-ref*/ classif, rangeLoop); + bool success = varDecl && TypeUtils::classifyQualType(m_context, varDecl->getType(), varDecl, /*by-ref*/ classif, rangeLoop); if (!success) return; diff --git a/src/checks/level2/function-args-by-ref.cpp b/src/checks/level2/function-args-by-ref.cpp index 6b7b5cb3..fdb88b11 100644 --- a/src/checks/level2/function-args-by-ref.cpp +++ b/src/checks/level2/function-args-by-ref.cpp @@ -125,7 +125,7 @@ void FunctionArgsByRef::processFunction(FunctionDecl *func) auto funcParams = Utils::functionParameters(func); for (unsigned int i = 0; i < funcParams.size(); ++i) { ParmVarDecl* param = funcParams[i]; - QualType paramQt = TypeUtils::unrefQualType(param->getType()); + const QualType paramQt = TypeUtils::unrefQualType(param->getType()); const Type *paramType = paramQt.getTypePtrOrNull(); if (!paramType || paramType->isIncompleteType() || paramType->isDependentType()) continue; @@ -134,7 +134,7 @@ void FunctionArgsByRef::processFunction(FunctionDecl *func) continue; TypeUtils::QualTypeClassification classif; - bool success = TypeUtils::classifyQualType(m_context, param, classif, body); + bool success = TypeUtils::classifyQualType(m_context, param->getType(), param, classif, body); if (!success) continue; diff --git a/src/checks/level2/function-args-by-value.cpp b/src/checks/level2/function-args-by-value.cpp index 0bd270ad..b302f876 100644 --- a/src/checks/level2/function-args-by-value.cpp +++ b/src/checks/level2/function-args-by-value.cpp @@ -145,7 +145,7 @@ void FunctionArgsByValue::processFunction(FunctionDecl *func) int i = -1; for (auto param : Utils::functionParameters(func)) { i++; - QualType paramQt = TypeUtils::unrefQualType(param->getType()); + const QualType paramQt = TypeUtils::unrefQualType(param->getType()); const Type *paramType = paramQt.getTypePtrOrNull(); if (!paramType || paramType->isIncompleteType() || paramType->isDependentType()) continue; @@ -154,7 +154,7 @@ void FunctionArgsByValue::processFunction(FunctionDecl *func) continue; TypeUtils::QualTypeClassification classif; - bool success = TypeUtils::classifyQualType(m_context, param, classif, body); + bool success = TypeUtils::classifyQualType(m_context, param->getType(), param, classif, body); if (!success) continue; |