summaryrefslogtreecommitdiffstats
path: root/compilerplugins/clang/stringviewdangle.cxx
blob: 99cb852d03b6762149de90631b25ced3c8fcd84d (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
/* -*- 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 "compat.hxx"
#include "config_clang.h"
#include "clang/AST/CXXInheritance.h"
#include "clang/AST/StmtVisitor.h"

/**
Look for places where we are assigning a temporary O[U]String to a std::*string_view, which leads
to a view pointing to freed memory.
*/

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

    bool preRun() override { return true; }

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

    bool VisitCXXOperatorCallExpr(CXXOperatorCallExpr const*);
    bool VisitVarDecl(VarDecl const*);
};

static const Expr* IgnoreImplicitAndConversionOperator(const Expr* expr)
{
    expr = expr->IgnoreImplicit();
    if (auto memberCall = dyn_cast<CXXMemberCallExpr>(expr))
    {
        if (auto conversionDecl = dyn_cast_or_null<CXXConversionDecl>(memberCall->getMethodDecl()))
        {
            if (!conversionDecl->isExplicit())
                expr = memberCall->getImplicitObjectArgument()->IgnoreImpCasts();
        }
    }
    return expr;
}

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

    auto op = cxxOperatorCallExpr->getOperator();
    if (op != OO_Equal)
        return true;
    if (!loplugin::TypeCheck(cxxOperatorCallExpr->getType())
             .ClassOrStruct("basic_string_view")
             .StdNamespace())
        return true;
    auto expr = IgnoreImplicitAndConversionOperator(cxxOperatorCallExpr->getArg(1));
    auto tc = loplugin::TypeCheck(expr->getType());
    if (!tc.Class("OUString").Namespace("rtl").GlobalNamespace()
        && !tc.Class("OString").Namespace("rtl").GlobalNamespace())
        return true;
    if (!isa<MaterializeTemporaryExpr>(expr))
        return true;
    report(DiagnosticsEngine::Warning, "view pointing into temporary i.e. dangling",
           cxxOperatorCallExpr->getExprLoc())
        << cxxOperatorCallExpr->getSourceRange();
    return true;
}

bool StringViewDangle::VisitVarDecl(VarDecl const* varDecl)
{
    if (ignoreLocation(varDecl))
        return true;
    if (!loplugin::TypeCheck(varDecl->getType()).ClassOrStruct("basic_string_view").StdNamespace())
        return true;
    if (!varDecl->hasInit())
        return true;
    auto expr = IgnoreImplicitAndConversionOperator(varDecl->getInit());
    auto tc = loplugin::TypeCheck(expr->getType());
    if (!tc.Class("OUString").Namespace("rtl").GlobalNamespace()
        && !tc.Class("OString").Namespace("rtl").GlobalNamespace())
        return true;
    if (!isa<MaterializeTemporaryExpr>(expr))
        return true;
    report(DiagnosticsEngine::Warning, "view pointing into temporary i.e. dangling",
           varDecl->getLocation())
        << varDecl->getSourceRange();
    return true;
}

loplugin::Plugin::Registration<StringViewDangle> stringviewdangle("stringviewdangle");
}

#endif // LO_CLANG_SHARED_PLUGINS

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