summaryrefslogtreecommitdiffstats
path: root/compilerplugins/clang/stringview.cxx
blob: 5a70d01de841e6a7dcf48bcc4632dc6322d9b799 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
#ifndef LO_CLANG_SHARED_PLUGINS

#include <cassert>
#include <string>
#include <iostream>
#include <unordered_map>
#include <unordered_set>

#include "plugin.hxx"
#include "check.hxx"
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/StmtVisitor.h"

/**
    Look for places where we are making a substring copy of an OUString, and then passing it to a
    function that takes a u16string_view, in which case it is more efficient to pass a view
    of the OUString, rather than making a copy.

    TODO currently does not check if there is some other visible overload of the callee, that can take
    a string_view.
    TODO handle OUStringBuffer/OStringBuffer similarly
*/

namespace
{
class StringView : public loplugin::FilteringPlugin<StringView>
{
public:
    explicit StringView(loplugin::InstantiationData const& data)
        : FilteringPlugin(data)
    {
    }

    bool preRun() override { return true; }

    virtual void run() override
    {
        if (!preRun())
            return;
        TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
    }

    bool VisitCallExpr(CallExpr const*);
    bool VisitCXXConstructExpr(CXXConstructExpr const*);
    bool VisitFunctionDecl(FunctionDecl const*);
    bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr const*);
    bool VisitImplicitCastExpr(ImplicitCastExpr const*);
};

bool StringView::VisitCallExpr(CallExpr const* callExpr)
{
    if (ignoreLocation(callExpr))
        return true;

    const FunctionDecl* functionDecl;
    if (isa<CXXMemberCallExpr>(callExpr))
        functionDecl = dyn_cast<CXXMemberCallExpr>(callExpr)->getMethodDecl();
    else
        functionDecl = callExpr->getDirectCallee();
    if (!functionDecl)
        return true;

    unsigned len = std::min(callExpr->getNumArgs(), functionDecl->getNumParams());
    for (unsigned i = 0; i < len; ++i)
    {
        const Expr* argExpr = callExpr->getArg(i);
        auto paramDecl = functionDecl->getParamDecl(i);
        auto paramRecordDecl = dyn_cast_or_null<RecordDecl>(
            paramDecl->getType()->getUnqualifiedDesugaredType()->getAsTagDecl());
        if (!paramRecordDecl || !paramRecordDecl->getIdentifier()
            || paramRecordDecl->getName() != "basic_string_view")
            continue;
        // unwrap the operator that converts to std::u16string_view
        auto memberCallExpr = dyn_cast<CXXMemberCallExpr>(argExpr->IgnoreImpCasts());
        if (!memberCallExpr)
            continue;
        // unwrap the call to copy()
        auto memberCallExpr2 = dyn_cast<CXXMemberCallExpr>(
            compat::IgnoreImplicit(memberCallExpr->getImplicitObjectArgument()));
        if (!memberCallExpr2)
            continue;
        auto methodDecl = memberCallExpr2->getMethodDecl();
        if (!methodDecl->getIdentifier() || methodDecl->getName() != "copy")
            continue;
        report(DiagnosticsEngine::Warning, "rather than copy, pass with a view using subView()",
               compat::getBeginLoc(argExpr))
            << argExpr->getSourceRange();
    }

    return true;
}

bool StringView::VisitCXXOperatorCallExpr(CXXOperatorCallExpr const* cxxOperatorCallExpr)
{
    if (ignoreLocation(cxxOperatorCallExpr))
        return true;

    auto check = [&](const Expr* expr) -> void {
        auto memberCallExpr = dyn_cast<CXXMemberCallExpr>(compat::IgnoreImplicit(expr));
        if (!memberCallExpr)
            return;
        auto methodDecl = memberCallExpr->getMethodDecl();
        if (!methodDecl->getIdentifier() || methodDecl->getName() != "copy")
            return;
        report(DiagnosticsEngine::Warning, "rather than copy, pass with a view using subView()",
               compat::getBeginLoc(expr))
            << expr->getSourceRange();
    };
    auto op = cxxOperatorCallExpr->getOperator();
    if (op == OO_Plus && cxxOperatorCallExpr->getNumArgs() == 2)
    {
        check(cxxOperatorCallExpr->getArg(0));
        check(cxxOperatorCallExpr->getArg(1));
    }
    if (compat::isComparisonOp(cxxOperatorCallExpr))
    {
        check(cxxOperatorCallExpr->getArg(0));
        check(cxxOperatorCallExpr->getArg(1));
    }
    else if (op == OO_PlusEqual)
        check(cxxOperatorCallExpr->getArg(1));
    else if (op == OO_Subscript)
        check(cxxOperatorCallExpr->getArg(0));
    return true;
}

bool StringView::VisitFunctionDecl(FunctionDecl const* functionDecl)
{
    if (ignoreLocation(functionDecl))
        return true;
    // debugging
    // if (functionDecl->getIdentifier() && functionDecl->getName() == "f1")
    //     functionDecl->dump();
    return true;
}

bool StringView::VisitCXXConstructExpr(CXXConstructExpr const* constructExpr)
{
    if (ignoreLocation(constructExpr))
        return true;

    const CXXConstructorDecl* constructorDecl = constructExpr->getConstructor();
    if (!constructorDecl)
        return true;

    unsigned len = std::min(constructExpr->getNumArgs(), constructorDecl->getNumParams());
    for (unsigned i = 0; i < len; ++i)
    {
        const Expr* argExpr = constructExpr->getArg(i);
        auto paramDecl = constructorDecl->getParamDecl(i);
        auto paramRecordDecl = dyn_cast_or_null<RecordDecl>(
            paramDecl->getType()->getUnqualifiedDesugaredType()->getAsTagDecl());
        if (!paramRecordDecl || !paramRecordDecl->getIdentifier()
            || paramRecordDecl->getName() != "basic_string_view")
            continue;
        // unwrap the operator that converts to std::u16string_view
        auto memberCallExpr = dyn_cast<CXXMemberCallExpr>(argExpr->IgnoreImpCasts());
        if (!memberCallExpr)
            continue;
        // unwrap the call to copy()
        auto memberCallExpr2 = dyn_cast<CXXMemberCallExpr>(
            compat::IgnoreImplicit(memberCallExpr->getImplicitObjectArgument()));
        if (!memberCallExpr2)
            continue;
        auto methodDecl = memberCallExpr2->getMethodDecl();
        if (!methodDecl->getIdentifier() || methodDecl->getName() != "copy")
            continue;
        report(DiagnosticsEngine::Warning, "rather than copy, pass with a view using subView()",
               compat::getBeginLoc(argExpr))
            << argExpr->getSourceRange();
    }

    return true;
}

bool StringView::VisitImplicitCastExpr(ImplicitCastExpr const* expr)
{
    if (ignoreLocation(expr))
    {
        return true;
    }
    if (!loplugin::TypeCheck(expr->getType()).ClassOrStruct("basic_string_view").StdNamespace())
    {
        return true;
    }
    auto const e = dyn_cast<CXXConstructExpr>(expr->getSubExprAsWritten()->IgnoreParens());
    if (e == nullptr)
    {
        return true;
    }
    if (e->getNumArgs() != 0)
    {
        return true;
    }
    auto const tc = loplugin::TypeCheck(e->getType());
    if (!(tc.Class("OString").Namespace("rtl").GlobalNamespace()
          || tc.Class("OUString").Namespace("rtl").GlobalNamespace()))
    {
        return true;
    }
    report(DiagnosticsEngine::Warning,
           "instead of an empty %0, pass an empty '%select{std::string_view|std::u16string_view}1'",
           e->getLocation())
        << e->getType() << (tc.Class("OString").Namespace("rtl").GlobalNamespace() ? 0 : 1)
        << e->getSourceRange();
    return true;
}

loplugin::Plugin::Registration<StringView> stringview("stringview");
}

#endif // LO_CLANG_SHARED_PLUGINS

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */