/* SPDX-FileCopyrightText: 2017 Sergio Martins SPDX-License-Identifier: LGPL-2.0-or-later */ #include "qhash-namespace.h" #include "ClazyContext.h" #include "ContextUtils.h" #include "PreProcessorVisitor.h" #include "StringUtils.h" #include #include #include #include #include using namespace clang; std::string QHashNamespace::fullyQualifiedParamName(clang::ParmVarDecl *firstArg) { if (auto *typePtr = firstArg->getType().getTypePtr()->getAsTagDecl()) { return typePtr->getQualifiedNameAsString(); } // Fallback, should not happen return firstArg->getType().getAsString(); } void QHashNamespace::VisitDecl(clang::Decl *decl) { auto *func = dyn_cast(decl); if (!func || isa(func) || func->getNumParams() == 0 || clazy::name(func) != "qHash") { return; } ParmVarDecl *firstArg = func->getParamDecl(0); NamespaceDecl *argumentNS = clazy::namespaceForType(firstArg->getType()); NamespaceDecl *qHashNS = clazy::namespaceForFunction(func); std::string msg; if (qHashNS && argumentNS) { const std::string argumentNSstr = argumentNS->getQualifiedNameAsString(); const std::string qhashNSstr = qHashNS->getQualifiedNameAsString(); if (argumentNSstr != qhashNSstr) { msg = "Move qHash(" + fullyQualifiedParamName(firstArg) + ") to " + argumentNSstr + " namespace for ADL lookup"; } } else if (qHashNS && !argumentNS) { msg = "Move qHash(" + fullyQualifiedParamName(firstArg) + ") out of namespace " + qHashNS->getQualifiedNameAsString(); } else if (!qHashNS && argumentNS) { msg = "Move qHash(" + fullyQualifiedParamName(firstArg) + ") into " + argumentNS->getQualifiedNameAsString() + " namespace for ADL lookup"; } if (!msg.empty()) { emitWarning(decl, msg); } if (m_context->isQtDeveloper()) { PreProcessorVisitor *preProcessorVisitor = m_context->preprocessorVisitor; if (preProcessorVisitor && !preProcessorVisitor->isBetweenQtNamespaceMacros(func->getBeginLoc())) { emitWarning(decl, "qHash(" + clazy::simpleTypeName(firstArg->getType(), lo()) + ") must be declared before QT_END_NAMESPACE"); } } }