diff options
Diffstat (limited to 'src/checks/level2/function-args-by-ref.cpp')
-rw-r--r-- | src/checks/level2/function-args-by-ref.cpp | 52 |
1 files changed, 28 insertions, 24 deletions
diff --git a/src/checks/level2/function-args-by-ref.cpp b/src/checks/level2/function-args-by-ref.cpp index a1fa94b7..712ca088 100644 --- a/src/checks/level2/function-args-by-ref.cpp +++ b/src/checks/level2/function-args-by-ref.cpp @@ -4,7 +4,7 @@ Copyright (C) 2015 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com Author: Sérgio Martins <sergio.martins@kdab.com> - Copyright (C) 2015 Sergio Martins <smartins@kde.org> + Copyright (C) 2015,2018 Sergio Martins <smartins@kde.org> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -26,7 +26,8 @@ #include "Utils.h" #include "FixItUtils.h" #include "TypeUtils.h" -#include "checkmanager.h" +#include "ClazyContext.h" +#include "StringUtils.h" #include <clang/AST/AST.h> #include <clang/Lex/Lexer.h> @@ -34,11 +35,6 @@ using namespace clang; using namespace std; -enum Fixit { - FixitNone = 0, - FixitAll = 0x1 // More granularity isn't needed I guess -}; - static bool shouldIgnoreClass(CXXRecordDecl *record) { if (!record) @@ -59,13 +55,19 @@ static bool shouldIgnoreClass(CXXRecordDecl *record) "QVariantComparisonHelper", "QHashDummyValue", "QCharRef", "QString::Null" }; - return clazy_std::contains(ignoreList, record->getQualifiedNameAsString()); + return clazy::contains(ignoreList, record->getQualifiedNameAsString()); } -static bool shouldIgnoreFunction(clang::FunctionDecl *function) +static bool shouldIgnoreOperator(FunctionDecl *function) { // Too many warnings in operator<< - static const vector<string> ignoreList = {"operator<<"}; + static const vector<StringRef> ignoreList = { "operator<<" }; + + return clazy::contains(ignoreList, clazy::name(function)); +} + +static bool shouldIgnoreFunction(clang::FunctionDecl *function) +{ static const vector<string> qualifiedIgnoreList = {"QDBusMessage::createErrorReply", // Fixed in Qt6 "QMenu::exec", // Fixed in Qt6 "QTextFrame::iterator", // Fixed in Qt6 @@ -77,29 +79,35 @@ static bool shouldIgnoreFunction(clang::FunctionDecl *function) "QSslCertificate::verify", // Fixed in Qt6 "QSslConfiguration::setAllowedNextProtocols" // Fixed in Qt6 }; - if (clazy_std::contains(ignoreList, function->getNameAsString())) - return true; - return clazy_std::contains(qualifiedIgnoreList, function->getQualifiedNameAsString()); + return clazy::contains(qualifiedIgnoreList, function->getQualifiedNameAsString()); } FunctionArgsByRef::FunctionArgsByRef(const std::string &name, ClazyContext *context) - : CheckBase(name, context) + : CheckBase(name, context, Option_CanIgnoreIncludes) { } static std::string warningMsgForSmallType(int sizeOf, const std::string &typeName) { std::string sizeStr = std::to_string(sizeOf); - return "Missing reference on large type sizeof " + typeName + " is " + sizeStr + " bytes)"; + return "Missing reference on large type (sizeof " + typeName + " is " + sizeStr + " bytes)"; } void FunctionArgsByRef::processFunction(FunctionDecl *func) { - if (!func || shouldIgnoreFunction(func) || - !func->isThisDeclarationADefinition() || func->isDeleted()) + if (!func || !func->isThisDeclarationADefinition() || func->isDeleted() || shouldIgnoreOperator(func)) + return; + + if (m_context->isQtDeveloper() && shouldIgnoreFunction(func)) return; + const bool warnForOverriddenMethods = isOptionSet("warn-for-overridden-methods"); + if (!warnForOverriddenMethods && Utils::methodOverrides(dyn_cast<CXXMethodDecl>(func))) { + // When overriding you can't change the signature. You should fix the base classes first + return; + } + Stmt *body = func->getBody(); int i = -1; @@ -132,7 +140,7 @@ void FunctionArgsByRef::processFunction(FunctionDecl *func) error = "Missing reference on non-trivial type (" + paramStr + ')'; } - emitWarning(param->getLocStart(), error.c_str(), fixits); + emitWarning(getLocStart(param), error.c_str(), fixits); } } } @@ -144,8 +152,8 @@ void FunctionArgsByRef::VisitDecl(Decl *decl) void FunctionArgsByRef::VisitStmt(Stmt *stmt) { - if (LambdaExpr *lambda = dyn_cast<LambdaExpr>(stmt)) { - if (!shouldIgnoreFile(stmt->getLocStart())) + if (auto lambda = dyn_cast<LambdaExpr>(stmt)) { + if (!shouldIgnoreFile(getLocStart(stmt))) processFunction(lambda->getCallOperator()); } } @@ -155,7 +163,3 @@ clang::FixItHint FunctionArgsByRef::fixit(const ParmVarDecl *, TypeUtils::QualTy FixItHint fixit; return fixit; } - -const char *const s_checkName = "function-args-by-ref"; -REGISTER_CHECK(s_checkName, FunctionArgsByRef, CheckLevel2) -// REGISTER_FIXIT(FixitAll, "fix-func-args", s_checkName) |