1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/*
This file is part of the clazy static checker.
Copyright (C) 2016 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
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "incorrect-emit.h"
#include "AccessSpecifierManager.h"
#include "ClazyContext.h"
#include "Utils.h"
#include "HierarchyUtils.h"
#include "SourceCompatibilityHelpers.h"
#include <clang/AST/DeclCXX.h>
#include <clang/AST/ExprCXX.h>
#include <clang/AST/Expr.h>
#include <clang/AST/Stmt.h>
#include <clang/Basic/IdentifierTable.h>
#include <clang/Basic/LLVM.h>
#include <clang/Basic/SourceManager.h>
#include <clang/Lex/Token.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/Support/Casting.h>
#include <utility>
namespace clang {
class MacroInfo;
} // namespace clang
using namespace clang;
using namespace std;
IncorrectEmit::IncorrectEmit(const std::string &name, ClazyContext *context)
: CheckBase(name, context, Option_CanIgnoreIncludes)
{
context->enableAccessSpecifierManager();
enablePreProcessorCallbacks();
m_emitLocations.reserve(30); // bootstrap it
m_filesToIgnore = { "moc_", ".moc" };
}
void IncorrectEmit::VisitMacroExpands(const Token &MacroNameTok, const SourceRange &range, const MacroInfo *)
{
IdentifierInfo *ii = MacroNameTok.getIdentifierInfo();
if (ii && (ii->getName() == "emit" || ii->getName() == "Q_EMIT"))
m_emitLocations.push_back(range.getBegin());
}
void IncorrectEmit::VisitStmt(Stmt *stmt)
{
auto methodCall = dyn_cast<CXXMemberCallExpr>(stmt);
if (!methodCall || !methodCall->getCalleeDecl())
return;
AccessSpecifierManager *accessSpecifierManager = m_context->accessSpecifierManager;
auto method = dyn_cast<CXXMethodDecl>(methodCall->getCalleeDecl());
if (!method || !accessSpecifierManager)
return;
if (shouldIgnoreFile(getLocStart(stmt)))
return;
if (Stmt *parent = clazy::parent(m_context->parentMap, methodCall)) {
// Check if we're inside a chained call, such as: emit d_func()->mySignal()
// We're not interested in the d_func() call, so skip it
if (clazy::getFirstParentOfType<CXXMemberCallExpr>(m_context->parentMap, parent))
return;
}
const QtAccessSpecifierType type = accessSpecifierManager->qtAccessSpecifierType(method);
if (type == QtAccessSpecifier_Unknown)
return;
const bool hasEmit = hasEmitKeyboard(methodCall);
const string methodName = method->getQualifiedNameAsString();
const bool isSignal = type == QtAccessSpecifier_Signal;
if (isSignal && !hasEmit) {
emitWarning(stmt, "Missing emit keyword on signal call " + methodName);
} else if (!isSignal && hasEmit) {
emitWarning(stmt, "Emit keyword being used with non-signal " + methodName);
}
if (isSignal)
checkCallSignalInsideCTOR(methodCall);
}
void IncorrectEmit::checkCallSignalInsideCTOR(CXXMemberCallExpr *callExpr)
{
if (!m_context->lastMethodDecl)
return;
auto ctorDecl = dyn_cast<CXXConstructorDecl>(m_context->lastMethodDecl);
if (!ctorDecl)
return;
Expr *implicitArg = callExpr->getImplicitObjectArgument();
if (!implicitArg || !isa<CXXThisExpr>(implicitArg)) // emit other->sig() is ok
return;
if (clazy::getFirstParentOfType<LambdaExpr>(m_context->parentMap, callExpr) != nullptr)
return; // Emit is inside a lambda, it's fine
emitWarning(getLocStart(callExpr), "Emitting inside constructor has no effect");
}
bool IncorrectEmit::hasEmitKeyboard(CXXMemberCallExpr *call) const
{
SourceLocation callLoc = getLocStart(call);
if (callLoc.isMacroID())
callLoc = sm().getFileLoc(callLoc);
for (const SourceLocation &emitLoc : m_emitLocations) {
// We cache the calculation of the next token because it uses the Lexer and hence expensive.
auto it = m_locationCache.find(emitLoc.getRawEncoding());
SourceLocation nextTokenLoc;
if (it == m_locationCache.end()) {
nextTokenLoc = Utils::locForNextToken(emitLoc, sm(), lo());
m_locationCache[emitLoc.getRawEncoding()] = nextTokenLoc;
} else {
nextTokenLoc = it->second;
}
if (nextTokenLoc == callLoc)
return true;
}
return false;
}
|