| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2017 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "qhash-namespace.h" |
| 8 |
#include "ClazyContext.h" |
| 9 |
#include "ContextUtils.h" |
| 10 |
#include "PreProcessorVisitor.h" |
| 11 |
#include "StringUtils.h" |
| 12 |
|
| 13 |
#include <clang/AST/Decl.h> |
| 14 |
#include <clang/AST/DeclCXX.h> |
| 15 |
#include <clang/Basic/LLVM.h> |
| 16 |
#include <llvm/ADT/StringRef.h> |
| 17 |
#include <llvm/Support/Casting.h> |
| 18 |
|
| 19 |
using namespace clang; |
| 20 |
|
| 21 |
std::string QHashNamespace::fullyQualifiedParamName(clang::ParmVarDecl *firstArg) |
| 22 |
{ |
| 23 |
if (auto *typePtr = firstArg->getType().getTypePtr()->getAsTagDecl()) { |
| 24 |
return typePtr->getQualifiedNameAsString(); |
| 25 |
} |
| 26 |
// Fallback, should not happen |
| 27 |
return firstArg->getType().getAsString(); |
| 28 |
} |
| 29 |
|
| 30 |
void QHashNamespace::VisitDecl(clang::Decl *decl) |
| 31 |
{ |
| 32 |
auto *func = dyn_cast<FunctionDecl>(decl); |
| 33 |
if (!func || isa<CXXMethodDecl>(func) || func->getNumParams() == 0 || clazy::name(func) != "qHash") { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
ParmVarDecl *firstArg = func->getParamDecl(0); |
| 38 |
NamespaceDecl *argumentNS = clazy::namespaceForType(firstArg->getType()); |
| 39 |
NamespaceDecl *qHashNS = clazy::namespaceForFunction(func); |
| 40 |
|
| 41 |
std::string msg; |
| 42 |
if (qHashNS && argumentNS) { |
| 43 |
const std::string argumentNSstr = argumentNS->getQualifiedNameAsString(); |
| 44 |
const std::string qhashNSstr = qHashNS->getQualifiedNameAsString(); |
| 45 |
if (argumentNSstr != qhashNSstr) { |
| 46 |
msg = "Move qHash(" + fullyQualifiedParamName(firstArg) + ") to " + argumentNSstr + " namespace for ADL lookup"; |
| 47 |
} |
| 48 |
} else if (qHashNS && !argumentNS) { |
| 49 |
msg = "Move qHash(" + fullyQualifiedParamName(firstArg) + ") out of namespace " + qHashNS->getQualifiedNameAsString(); |
| 50 |
} else if (!qHashNS && argumentNS) { |
| 51 |
msg = "Move qHash(" + fullyQualifiedParamName(firstArg) + ") into " + argumentNS->getQualifiedNameAsString() + " namespace for ADL lookup"; |
| 52 |
} |
| 53 |
|
| 54 |
if (!msg.empty()) { |
| 55 |
emitWarning(decl, msg); |
| 56 |
} |
| 57 |
|
| 58 |
if (m_context->isQtDeveloper()) { |
| 59 |
PreProcessorVisitor *preProcessorVisitor = m_context->preprocessorVisitor; |
| 60 |
if (preProcessorVisitor && !preProcessorVisitor->isBetweenQtNamespaceMacros(func->getBeginLoc())) { |
| 61 |
emitWarning(decl, "qHash(" + clazy::simpleTypeName(firstArg->getType(), lo()) + ") must be declared before QT_END_NAMESPACE"); |
| 62 |
} |
| 63 |
} |
| 64 |
} |