| 1 |
/* |
| 2 |
SPDX-FileCopyrightText: 2016 Sergio Martins <[email protected]> |
| 3 |
|
| 4 |
SPDX-License-Identifier: LGPL-2.0-or-later |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "incorrect-emit.h" |
| 8 |
#include "AccessSpecifierManager.h" |
| 9 |
#include "ClazyContext.h" |
| 10 |
#include "HierarchyUtils.h" |
| 11 |
#include "Utils.h" |
| 12 |
|
| 13 |
#include <clang/AST/DeclCXX.h> |
| 14 |
#include <clang/AST/Expr.h> |
| 15 |
#include <clang/AST/ExprCXX.h> |
| 16 |
#include <clang/AST/Stmt.h> |
| 17 |
#include <clang/Basic/IdentifierTable.h> |
| 18 |
#include <clang/Basic/LLVM.h> |
| 19 |
#include <clang/Basic/SourceManager.h> |
| 20 |
#include <clang/Lex/Token.h> |
| 21 |
#include <llvm/ADT/StringRef.h> |
| 22 |
#include <llvm/Support/Casting.h> |
| 23 |
|
| 24 |
using namespace clang; |
| 25 |
|
| 26 |
IncorrectEmit::IncorrectEmit(const std::string &name, Options options) |
| 27 |
: CheckBase(name, options) |
| 28 |
{ |
| 29 |
m_emitLocations.reserve(30); // bootstrap it |
| 30 |
m_filesToIgnore = {"moc_", ".moc"}; |
| 31 |
} |
| 32 |
|
| 33 |
size_t countUntilNonSpaceOrParen(const char *start, size_t prefixLen) |
| 34 |
{ |
| 35 |
size_t count = prefixLen; |
| 36 |
start += prefixLen; |
| 37 |
// Skip spaces and '(' characters |
| 38 |
while (*start && (isspace(*start) || *start == '(')) { |
| 39 |
++count; |
| 40 |
++start; |
| 41 |
} |
| 42 |
return count; |
| 43 |
} |
| 44 |
|
| 45 |
void IncorrectEmit::VisitMacroExpands(const Token &MacroNameTok, const SourceRange &range, const MacroInfo *info) |
| 46 |
{ |
| 47 |
IdentifierInfo *ii = MacroNameTok.getIdentifierInfo(); |
| 48 |
if (!ii) { |
| 49 |
return; |
| 50 |
} |
| 51 |
const bool isEmit = ii->getName() == "emit"; |
| 52 |
const bool isQEmit = ii->getName() == "Q_EMIT"; |
| 53 |
if (isEmit || isQEmit) { |
| 54 |
const char *start = sm().getCharacterData(range.getBegin()); |
| 55 |
const size_t char_offset = countUntilNonSpaceOrParen(start, isEmit ? strlen("emit") : strlen("Q_EMIT")); |
| 56 |
SourceLocation newLocation = range.getBegin().getLocWithOffset(char_offset); |
| 57 |
m_emitLocations.push_back(newLocation); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
void IncorrectEmit::VisitStmt(Stmt *stmt) |
| 62 |
{ |
| 63 |
auto *methodCall = dyn_cast<CXXMemberCallExpr>(stmt); |
| 64 |
if (!methodCall || !methodCall->getCalleeDecl()) { |
| 65 |
return; |
| 66 |
} |
| 67 |
|
| 68 |
const AccessSpecifierManager *accessSpecifierManager = m_context->accessSpecifierManager; |
| 69 |
auto *method = dyn_cast<CXXMethodDecl>(methodCall->getCalleeDecl()); |
| 70 |
if (!method || !accessSpecifierManager) { |
| 71 |
return; |
| 72 |
} |
| 73 |
|
| 74 |
if (shouldIgnoreFile(stmt->getBeginLoc())) { |
| 75 |
return; |
| 76 |
} |
| 77 |
|
| 78 |
if (Stmt *parent = clazy::parent(m_context->parentMap, methodCall)) { |
| 79 |
// Check if we're inside a chained call, such as: emit d_func()->mySignal() |
| 80 |
// We're not interested in the d_func() call, so skip it |
| 81 |
if (clazy::getFirstParentOfType<CXXMemberCallExpr>(m_context->parentMap, parent)) { |
| 82 |
return; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
const QtAccessSpecifierType type = accessSpecifierManager->qtAccessSpecifierType(method); |
| 87 |
if (type == QtAccessSpecifier_Unknown) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
const bool hasEmit = hasEmitKeyboard(methodCall); |
| 92 |
const std::string methodName = method->getQualifiedNameAsString(); |
| 93 |
const bool isSignal = type == QtAccessSpecifier_Signal; |
| 94 |
if (isSignal && !hasEmit) { |
| 95 |
emitWarning(stmt, "Missing emit keyword on signal call " + methodName); |
| 96 |
} else if (!isSignal && hasEmit) { |
| 97 |
emitWarning(stmt, "Emit keyword being used with non-signal " + methodName); |
| 98 |
} |
| 99 |
|
| 100 |
if (isSignal) { |
| 101 |
checkCallSignalInsideCTOR(methodCall); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
void IncorrectEmit::checkCallSignalInsideCTOR(CXXMemberCallExpr *callExpr) |
| 106 |
{ |
| 107 |
if (!m_context->lastMethodDecl) { |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
auto *ctorDecl = dyn_cast<CXXConstructorDecl>(m_context->lastMethodDecl); |
| 112 |
if (!ctorDecl) { |
| 113 |
return; |
| 114 |
} |
| 115 |
|
| 116 |
Expr *implicitArg = callExpr->getImplicitObjectArgument(); |
| 117 |
if (!implicitArg || !isa<CXXThisExpr>(implicitArg)) { // emit other->sig() is ok |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
if (clazy::getFirstParentOfType<LambdaExpr>(m_context->parentMap, callExpr) != nullptr) { |
| 122 |
return; // Emit is inside a lambda, it's fine |
| 123 |
} |
| 124 |
|
| 125 |
emitWarning(callExpr->getBeginLoc(), "Emitting inside constructor probably has no effect"); |
| 126 |
} |
| 127 |
|
| 128 |
bool IncorrectEmit::hasEmitKeyboard(CXXMemberCallExpr *call) const |
| 129 |
{ |
| 130 |
SourceLocation callLoc = call->getBeginLoc(); |
| 131 |
if (callLoc.isMacroID()) { |
| 132 |
callLoc = sm().getFileLoc(callLoc); |
| 133 |
} |
| 134 |
|
| 135 |
return std::any_of(m_emitLocations.begin(), m_emitLocations.end(), [&callLoc](const SourceLocation &emitLoc) { |
| 136 |
return callLoc == emitLoc; |
| 137 |
}); |
| 138 |
} |