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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* 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/.
*/
// Find uses of dynamic_cast that cast between unrelated classes, which is suspicious and might
// indicate a bug. The heuristic used to consider two classes unrelated is that neither derives
// from the other (directly or indirectly) and they do not both virtually derive (directly or
// indirectly) from a common third class. Additionally, class definitions can be attributed with
// SAL_LOPLUGIN_ANNOTATE("crosscast") (from sal/types.h) to suppress false warnings about known-good
// cases casting from or to such a class.
#ifndef LO_CLANG_SHARED_PLUGINS
#include <cassert>
#include <set>
#include "compat.hxx"
#include "plugin.hxx"
namespace
{
void computeVirtualBases(CXXRecordDecl const* decl, std::set<CXXRecordDecl const*>* vbases)
{
assert(vbases != nullptr);
for (auto const& i : decl->bases())
{
auto const d = i.getType()->getAsCXXRecordDecl();
if (d == nullptr)
{
continue;
}
if (i.isVirtual())
{
if (!vbases->insert(d->getCanonicalDecl()).second)
{
// As we track the already computed virtual bases in vbases anyway, we can cheaply
// optimize the case where we see a virtual base again, even though we don't bother
// to optimize the case where we see a non-virtual base multiple times:
continue;
}
}
computeVirtualBases(d, vbases);
}
}
bool compareVirtualBases(CXXRecordDecl const* decl, std::set<CXXRecordDecl const*>& vbases)
{
for (auto const& i : decl->bases())
{
auto const d = i.getType()->getAsCXXRecordDecl();
if (d == nullptr)
{
continue;
}
if (i.isVirtual() && vbases.count(d->getCanonicalDecl()) == 1)
{
return true;
}
if (compareVirtualBases(d, vbases))
{
return true;
}
}
return false;
}
bool haveCommonVirtualBase(CXXRecordDecl const* decl1, CXXRecordDecl const* decl2)
{
std::set<CXXRecordDecl const*> vbases;
computeVirtualBases(decl1, &vbases);
return compareVirtualBases(decl2, vbases);
}
bool isAllowedInCrossCasts(CXXRecordDecl const* decl)
{
auto const def = decl->getDefinition();
if (def == nullptr)
{
return false;
}
for (auto const attr : def->specific_attrs<AnnotateAttr>())
{
if (attr->getAnnotation() == "loplugin:crosscast")
{
return true;
}
}
return false;
}
class CrossCast final : public loplugin::FilteringPlugin<CrossCast>
{
public:
explicit CrossCast(loplugin::InstantiationData const& data)
: FilteringPlugin(data)
{
}
bool preRun() override { return compiler.getLangOpts().CPlusPlus; }
void run() override
{
if (preRun())
{
TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
}
}
bool VisitCXXDynamicCastExpr(CXXDynamicCastExpr const* expr)
{
if (ignoreLocation(expr))
{
return true;
}
auto const t2 = expr->getTypeAsWritten();
if (t2->isVoidPointerType())
{
return true;
}
auto const d2 = t2->getPointeeCXXRecordDecl();
if (d2 == nullptr)
{
return true;
}
auto const t1 = compat::getSubExprAsWritten(expr)->getType();
auto t1a = t1;
if (auto const t = t1a->getAs<clang::PointerType>())
{
t1a = t->getPointeeType();
}
auto const d1 = t1a->getAsCXXRecordDecl();
if (d1 == nullptr)
{
return true;
}
if (d1->getCanonicalDecl() == d2->getCanonicalDecl() || d1->isDerivedFrom(d2)
|| d2->isDerivedFrom(d1) || haveCommonVirtualBase(d1, d2))
{
return true;
}
if (isAllowedInCrossCasts(d1) || isAllowedInCrossCasts(d2))
{
return true;
}
if (suppressWarningAt(expr->getBeginLoc()))
{
return true;
}
report(DiagnosticsEngine::Warning, "suspicious dynamic cross cast from %0 to %1",
expr->getExprLoc())
<< t1 << t2 << expr->getSourceRange();
return true;
}
};
loplugin::Plugin::Registration<CrossCast> crosscast("crosscast");
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
|