summaryrefslogtreecommitdiffstats
path: root/compilerplugins/clang/stringview.cxx
blob: 1be16fb89a4f1faefc104237d5e2ce77a00c7847 (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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
/* -*- 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 "config_clang.h"
#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
    {
        auto const fn = handler.getMainFileName();
        return !(
            loplugin::isSamePathname(fn, SRCDIR "/sal/qa/OStringBuffer/rtl_OStringBuffer.cxx")
            || loplugin::isSamePathname(fn, SRCDIR "/sal/qa/rtl/strings/test_ostring_concat.cxx")
            || loplugin::isSamePathname(fn, SRCDIR "/sal/qa/rtl/strings/test_oustring_concat.cxx")
            || loplugin::isSamePathname(fn, SRCDIR "/sal/qa/rtl/oustring/rtl_OUString2.cxx")
            || loplugin::isSamePathname(fn, SRCDIR "/sal/qa/rtl/strings/test_oustring_compare.cxx")
            || loplugin::isSamePathname(fn,
                                        SRCDIR "/sal/qa/rtl/strings/test_oustring_startswith.cxx")
            || loplugin::isSamePathname(fn, SRCDIR
                                        "/sal/qa/rtl/strings/test_strings_defaultstringview.cxx"));
    }

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

    bool VisitFunctionDecl(FunctionDecl const*);
    bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr const*);
    bool VisitImplicitCastExpr(ImplicitCastExpr const*);
    bool VisitCXXMemberCallExpr(CXXMemberCallExpr const*);
    bool VisitCXXConstructExpr(CXXConstructExpr const*);

private:
    void handleSubExprThatCouldBeView(Expr const* expr);
    void handleCXXConstructExpr(CXXConstructExpr const* expr);
    void handleCXXMemberCallExpr(CXXMemberCallExpr const* expr);
};

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

    auto op = cxxOperatorCallExpr->getOperator();
    if (op == OO_Plus && cxxOperatorCallExpr->getNumArgs() == 2)
    {
        handleSubExprThatCouldBeView(cxxOperatorCallExpr->getArg(0));
        handleSubExprThatCouldBeView(cxxOperatorCallExpr->getArg(1));
    }
    if (cxxOperatorCallExpr->isComparisonOp())
    {
        handleSubExprThatCouldBeView(cxxOperatorCallExpr->getArg(0));
        handleSubExprThatCouldBeView(cxxOperatorCallExpr->getArg(1));
    }
    else if (op == OO_PlusEqual)
        handleSubExprThatCouldBeView(cxxOperatorCallExpr->getArg(1));
    else if (op == OO_Subscript)
        handleSubExprThatCouldBeView(cxxOperatorCallExpr->getArg(0));
    else if (op == OO_Equal)
    {
        if (loplugin::TypeCheck(cxxOperatorCallExpr->getType())
                .Class("OUStringBuffer")
                .Namespace("rtl")
                .GlobalNamespace()
            || loplugin::TypeCheck(cxxOperatorCallExpr->getType())
                   .Class("OStringBuffer")
                   .Namespace("rtl")
                   .GlobalNamespace())
        {
            handleSubExprThatCouldBeView(cxxOperatorCallExpr->getArg(1));
        }
    }
    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::VisitImplicitCastExpr(ImplicitCastExpr const* expr)
{
    if (ignoreLocation(expr))
    {
        return true;
    }
    if (!loplugin::TypeCheck(expr->getType()).ClassOrStruct("basic_string_view").StdNamespace())
    {
        return true;
    }
    handleSubExprThatCouldBeView(expr->getSubExprAsWritten());
    return true;
}

void StringView::handleSubExprThatCouldBeView(Expr const* subExpr)
{
    auto const e0 = subExpr->IgnoreImplicit();
    auto const e = e0->IgnoreParens();
    auto const tc = loplugin::TypeCheck(e->getType());
    if (!(tc.Class("OString").Namespace("rtl").GlobalNamespace()
          || tc.Class("OUString").Namespace("rtl").GlobalNamespace()
          || tc.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace()))
    {
        return;
    }
    if (auto const e1 = dyn_cast<CXXConstructExpr>(e))
    {
        if (e0 == subExpr)
        {
            handleCXXConstructExpr(e1);
        }
    }
    else if (auto const e2 = dyn_cast<CXXFunctionalCastExpr>(e))
    {
        auto e3 = e2->getSubExpr();
        if (auto const e4 = dyn_cast<CXXBindTemporaryExpr>(e3))
        {
            e3 = e4->getSubExpr();
        }
        if (auto const e4 = dyn_cast<CXXConstructExpr>(e3))
        {
            handleCXXConstructExpr(e4);
        }
    }
    else if (auto const e3 = dyn_cast<CXXMemberCallExpr>(e))
    {
        handleCXXMemberCallExpr(e3);
    }
}

void StringView::handleCXXConstructExpr(CXXConstructExpr const* expr)
{
    QualType argType;
    enum
    {
        None,
        OrChar,
        ViaConcatenation
    } extra
        = None;
    auto const d = expr->getConstructor();
    switch (d->getNumParams())
    {
        case 0:
            break;
        case 1:
        {
            auto const t = d->getParamDecl(0)->getType();
            if (t->isAnyCharacterType())
            {
                argType = expr->getArg(0)->IgnoreImplicit()->getType();
                extra = OrChar;
                break;
            }
            loplugin::TypeCheck tc(t);
            if (tc.LvalueReference()
                    .Const()
                    .Class("OStringLiteral")
                    .Namespace("rtl")
                    .GlobalNamespace()
                || tc.LvalueReference()
                       .Const()
                       .Class("OUStringLiteral")
                       .Namespace("rtl")
                       .GlobalNamespace()
                || tc.RvalueReference().Struct("OStringNumber").Namespace("rtl").GlobalNamespace()
                || tc.RvalueReference().Struct("OUStringNumber").Namespace("rtl").GlobalNamespace()
                || tc.ClassOrStruct("basic_string_view").StdNamespace())
            {
                argType = expr->getArg(0)->IgnoreImplicit()->getType();
                break;
            }
            if (tc.RvalueReference().Struct("OStringConcat").Namespace("rtl").GlobalNamespace()
                || tc.RvalueReference().Struct("OUStringConcat").Namespace("rtl").GlobalNamespace())
            {
                argType = expr->getArg(0)->IgnoreImplicit()->getType();
                extra = ViaConcatenation;
                break;
            }
            return;
        }
        case 2:
        {
            auto const t0 = d->getParamDecl(0)->getType();
            if (t0->isPointerType() && t0->getPointeeType()->isAnyCharacterType())
            {
                auto const t = d->getParamDecl(1)->getType();
                if (t->isIntegralType(compiler.getASTContext())
                    && !(t->isBooleanType() || t->isAnyCharacterType()))
                {
                    auto const arg = expr->getArg(1);
                    if (!arg->isValueDependent())
                    {
                        if (auto const val = arg->getIntegerConstantExpr(compiler.getASTContext()))
                        {
                            if (val->getExtValue() == 1)
                            {
                                extra = OrChar;
                            }
                        }
                    }
                    argType = expr->getArg(0)->IgnoreImplicit()->getType();
                    break;
                }
            }
            if (loplugin::TypeCheck(d->getParamDecl(1)->getType())
                    .Struct("Dummy")
                    .Namespace("libreoffice_internal")
                    .Namespace("rtl")
                    .GlobalNamespace())
            {
                argType = expr->getArg(0)->IgnoreImplicit()->getType();
                break;
            }
            return;
        }
        default:
            return;
    }
    report(DiagnosticsEngine::Warning,
           "instead of an %0%select{| constructed from a %2}1, pass a"
           " '%select{std::string_view|std::u16string_view}3'"
           "%select{| (or an '%select{rtl::OStringChar|rtl::OUStringChar}3')|"
           " via '%select{rtl::OStringConcatenation|rtl::OUStringConcatenation}3'}4",
           expr->getExprLoc())
        << expr->getType() << (argType.isNull() ? 0 : 1) << argType
        << (loplugin::TypeCheck(expr->getType()).Class("OString").Namespace("rtl").GlobalNamespace()
                ? 0
                : 1)
        << extra << expr->getSourceRange();
}

void StringView::handleCXXMemberCallExpr(CXXMemberCallExpr const* expr)
{
    auto const dc1 = loplugin::DeclCheck(expr->getMethodDecl());
    if (auto const dc2 = dc1.Function("copy"))
    {
        if (dc2.Class("OString").Namespace("rtl").GlobalNamespace()
            || dc2.Class("OUString").Namespace("rtl").GlobalNamespace()
            || dc2.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace())
        {
            report(DiagnosticsEngine::Warning, "rather than copy, pass with a view using subView()",
                   expr->getExprLoc())
                << expr->getSourceRange();
        }
        return;
    }
    if (auto const dc2 = dc1.Function("getToken"))
    {
        if (dc2.Class("OString").Namespace("rtl").GlobalNamespace()
            || dc2.Class("OUString").Namespace("rtl").GlobalNamespace()
            || dc2.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace())
        {
            report(DiagnosticsEngine::Warning,
                   "rather than getToken, pass with a view using o3tl::getToken()",
                   expr->getExprLoc())
                << expr->getSourceRange();
        }
        return;
    }
    if (auto const dc2 = dc1.Function("trim"))
    {
        if (dc2.Class("OString").Namespace("rtl").GlobalNamespace()
            || dc2.Class("OUString").Namespace("rtl").GlobalNamespace()
            || dc2.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace())
        {
            report(DiagnosticsEngine::Warning,
                   "rather than trim, pass with a view using o3tl::trim()", expr->getExprLoc())
                << expr->getSourceRange();
        }
        return;
    }
    if (auto const dc2 = dc1.Function("makeStringAndClear"))
    {
        if (dc2.Class("OStringBuffer").Namespace("rtl").GlobalNamespace()
            || dc2.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace())
        {
            auto const obj = expr->getImplicitObjectArgument();
            if (!(obj->isLValue() || obj->getType()->isPointerType()))
            {
                report(DiagnosticsEngine::Warning,
                       "rather than call makeStringAndClear on an rvalue, pass with a view",
                       expr->getExprLoc())
                    << expr->getSourceRange();
            }
        }
        return;
    }
    if (auto const dc2 = dc1.Function("toString"))
    {
        if (dc2.Class("OStringBuffer").Namespace("rtl").GlobalNamespace()
            || dc2.Class("OUStringBuffer").Namespace("rtl").GlobalNamespace())
        {
            report(DiagnosticsEngine::Warning, "rather than call toString, pass with a view",
                   expr->getExprLoc())
                << expr->getSourceRange();
        }
        return;
    }
}

bool StringView::VisitCXXMemberCallExpr(CXXMemberCallExpr const* expr)
{
    if (ignoreLocation(expr))
    {
        return true;
    }
    /** check for calls to O[U]StringBuffer::append that could be passed as a
        std::u16string_view */
    if (loplugin::TypeCheck(expr->getType())
            .Class("OUStringBuffer")
            .Namespace("rtl")
            .GlobalNamespace()
        || loplugin::TypeCheck(expr->getType())
               .Class("OStringBuffer")
               .Namespace("rtl")
               .GlobalNamespace())
    {
        auto const dc = loplugin::DeclCheck(expr->getMethodDecl());
        if (dc.Function("append") || dc.Function("indexOf") || dc.Function("lastIndexOf"))
        {
            handleSubExprThatCouldBeView(expr->getArg(0));
        }
        else if (dc.Function("insert"))
        {
            handleSubExprThatCouldBeView(expr->getArg(1));
        }
    }

    // rather than getToken...toInt32, use o3tl::toInt(o3tl::getToken(...)
    auto tc = loplugin::TypeCheck(expr->getImplicitObjectArgument()->getType());
    if (tc.Class("OUString").Namespace("rtl").GlobalNamespace()
        || tc.Class("OString").Namespace("rtl").GlobalNamespace())
    {
        auto const dc = loplugin::DeclCheck(expr->getMethodDecl());
        if (dc.Function("toInt32") || dc.Function("toUInt32") || dc.Function("toInt64")
            || dc.Function("toDouble") || dc.Function("equalsAscii")
            || dc.Function("equalsIgnoreAsciiCase") || dc.Function("compareToIgnoreAsciiCase")
            || dc.Function("matchIgnoreAsciiCase") || dc.Function("trim")
            || dc.Function("startsWith") || dc.Function("endsWith") || dc.Function("match"))
        {
            handleSubExprThatCouldBeView(expr->getImplicitObjectArgument());
        }
    }
    return true;
}

/** check for calls to O[U]StringBuffer constructor that could be passed as a
    std::u16string_view */
bool StringView::VisitCXXConstructExpr(CXXConstructExpr const* expr)
{
    if (ignoreLocation(expr))
    {
        return true;
    }
    if (!loplugin::TypeCheck(expr->getType())
             .Class("OUStringBuffer")
             .Namespace("rtl")
             .GlobalNamespace()
        && !loplugin::TypeCheck(expr->getType())
                .Class("OStringBuffer")
                .Namespace("rtl")
                .GlobalNamespace())
    {
        return true;
    }
    if (!compiler.getLangOpts().CPlusPlus17 && expr->isElidable()) // external C++03 code
    {
        return true;
    }
    if (expr->getNumArgs() > 0)
        handleSubExprThatCouldBeView(expr->getArg(0));
    return true;
}

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

#endif // LO_CLANG_SHARED_PLUGINS

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