aboutsummaryrefslogtreecommitdiffstats
path: root/src/checks/level0/qstring-arg.cpp
blob: e66e0eb4595ef995e6cd7c62f2c0f0dbab894285 (plain)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
   This file is part of the clazy static checker.

  Copyright (C) 2015 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 "qstring-arg.h"
#include "Utils.h"
#include "StringUtils.h"
#include "HierarchyUtils.h"

#include <clang/AST/AST.h>
#include <vector>

using namespace clang;
using namespace std;

QStringArg::QStringArg(const std::string &name, ClazyContext *context)
    : CheckBase(name, context, Option_CanIgnoreIncludes)
{
    m_filesToIgnore = { "qstring.h" };
}

static string variableNameFromArg(Expr *arg)
{
    vector<DeclRefExpr*> declRefs;
    clazy::getChilds<DeclRefExpr>(arg, declRefs);
    if (declRefs.size() == 1) {
        ValueDecl *decl = declRefs.at(0)->getDecl();
        return decl ? decl->getNameAsString() : string();
    }

    return {};
}

static CXXMethodDecl* isArgMethod(FunctionDecl *func)
{
    if (!func)
        return nullptr;

    auto method = dyn_cast<CXXMethodDecl>(func);
    if (!method || clazy::name(method) != "arg")
        return nullptr;

    CXXRecordDecl *record = method->getParent();
    if (!record || clazy::name(record) != "QString")
        return nullptr;

    return method;
}

static bool isArgFuncWithOnlyQString(CallExpr *callExpr)
{
    if (!callExpr)
        return false;

    CXXMethodDecl *method = isArgMethod(callExpr->getDirectCallee());
    if (!method)
        return false;

    ParmVarDecl *secondParam = method->getParamDecl(1);
    if (clazy::classNameFor(secondParam) == "QString")
        return true;

    ParmVarDecl *firstParam = method->getParamDecl(0);
    if (clazy::classNameFor(firstParam) != "QString")
        return false;

    // This is a arg(QString, int, QChar) call, it's good if the second parameter is a default param
    return isa<CXXDefaultArgExpr>(callExpr->getArg(1));
}

bool QStringArg::checkMultiArgWarningCase(const vector<clang::CallExpr *> &calls)
{
    const int size = calls.size();
    for (int i = 1; i < size; ++i) {
        auto call = calls.at(i);
        if (calls.at(i - 1)->getNumArgs() + call->getNumArgs() <= 9) {
            emitWarning(getLocEnd(call), "Use multi-arg instead");
            return true;
        }
    }

    return false;
}

void QStringArg::checkForMultiArgOpportunities(CXXMemberCallExpr *memberCall)
{
    if (!isArgFuncWithOnlyQString(memberCall))
        return;

    if (getLocStart(memberCall).isMacroID()) {
        auto macroName = Lexer::getImmediateMacroName(getLocStart(memberCall), sm(), lo());
        if (macroName == "QT_REQUIRE_VERSION") // bug #391851
            return;
    }

    vector<clang::CallExpr *> callExprs = Utils::callListForChain(memberCall);
    vector<clang::CallExpr *> argCalls;
    for (auto call : callExprs) {
        if (!clazy::contains(m_alreadyProcessedChainedCalls, call) && isArgFuncWithOnlyQString(call)) {
            argCalls.push_back(call);
            m_alreadyProcessedChainedCalls.push_back(call);
        } else {
            if (checkMultiArgWarningCase(argCalls))
                return;
            argCalls.clear();
        }
    }

    checkMultiArgWarningCase(argCalls);
}

void QStringArg::VisitStmt(clang::Stmt *stmt)
{
    auto memberCall = dyn_cast<CXXMemberCallExpr>(stmt);
    if (!memberCall)
        return;

    if (shouldIgnoreFile(getLocStart(stmt)))
        return;

    checkForMultiArgOpportunities(memberCall);

    if (!isOptionSet("fillChar-overloads"))
        return;

    CXXMethodDecl *method = isArgMethod(memberCall->getDirectCallee());
    if (!method)
        return;

    if (clazy::simpleArgTypeName(method, method->getNumParams() - 1, lo()) == "QChar") {
        // The second arg wasn't passed, so this is a safe and unambiguous use, like .arg(1)
        if (isa<CXXDefaultArgExpr>(memberCall->getArg(1)))
            return;

        ParmVarDecl *p = method->getParamDecl(2);
        if (p && clazy::name(p) == "base") {
            // User went through the trouble specifying a base, lets allow it if it's a literal.
            vector<IntegerLiteral*> literals;
            clazy::getChilds<IntegerLiteral>(memberCall->getArg(2), literals);
            if (!literals.empty())
                return;

            string variableName = clazy::toLower(variableNameFromArg(memberCall->getArg(2)));
            if (clazy::contains(variableName, "base"))
                return;
        }

        p = method->getParamDecl(1);
        if (p && clazy::name(p) == "fieldWidth") {
            // He specified a literal, so he knows what he's doing, otherwise he would have put it directly in the string
            vector<IntegerLiteral*> literals;
            clazy::getChilds<IntegerLiteral>(memberCall->getArg(1), literals);
            if (!literals.empty())
                return;

            // the variable is named "width", user knows what he's doing
            string variableName = clazy::toLower(variableNameFromArg(memberCall->getArg(1)));
            if (clazy::contains(variableName, "width"))
                return;
        }

        emitWarning(getLocStart(stmt), "Using QString::arg() with fillChar overload");
    }
}