1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #ifndef LO_CLANG_SHARED_PLUGINS
18 // In C++, find implicit conversions from char16_t (aka sal_Unicode) to char.
19 // Such places are probably meant to properly work on char16_t instead.
23 class UnicodeToChar final
:
24 public loplugin::FilteringPlugin
<UnicodeToChar
>
27 explicit UnicodeToChar(loplugin::InstantiationData
const & data
):
28 FilteringPlugin(data
) {}
30 bool PreTraverseCStyleCastExpr(CStyleCastExpr
* expr
) {
31 subExprs_
.push(expr
->getSubExpr());
34 bool PostTraverseCStyleCastExpr(CStyleCastExpr
*, bool ) {
38 bool TraverseCStyleCastExpr(CStyleCastExpr
* expr
) {
39 PreTraverseCStyleCastExpr(expr
);
40 bool ret
= RecursiveASTVisitor::TraverseCStyleCastExpr(expr
);
41 PostTraverseCStyleCastExpr(expr
, ret
);
45 bool PreTraverseCXXStaticCastExpr(CXXStaticCastExpr
* expr
) {
46 subExprs_
.push(expr
->getSubExpr());
49 bool PostTraverseCXXStaticCastExpr(CXXStaticCastExpr
*, bool) {
53 bool TraverseCXXStaticCastExpr(CXXStaticCastExpr
* expr
) {
54 PreTraverseCXXStaticCastExpr(expr
);
55 bool ret
= RecursiveASTVisitor::TraverseCXXStaticCastExpr(expr
);
56 PostTraverseCXXStaticCastExpr(expr
, ret
);
60 bool PreTraverseCXXFunctionalCastExpr(CXXFunctionalCastExpr
* expr
) {
61 subExprs_
.push(expr
->getSubExpr());
64 bool PostTraverseCXXFunctionalCastExpr(CXXFunctionalCastExpr
*, bool) {
68 bool TraverseCXXFunctionalCastExpr(CXXFunctionalCastExpr
* expr
) {
69 PreTraverseCXXFunctionalCastExpr(expr
);
70 bool ret
= RecursiveASTVisitor::TraverseCXXFunctionalCastExpr(expr
);
71 PostTraverseCXXFunctionalCastExpr(expr
, ret
);
75 bool VisitImplicitCastExpr(ImplicitCastExpr
const * expr
) {
76 if ((!subExprs_
.empty() && expr
== subExprs_
.top())
77 || ignoreLocation(expr
))
81 if (!(loplugin::TypeCheck(expr
->getType()).Char()
82 && expr
->getSubExpr()->getType()->isSpecificBuiltinType(
83 clang::BuiltinType::Char16
)))
88 if (compat::EvaluateAsInt(expr
->getSubExpr(), res
, compiler
.getASTContext())
89 && res
>= 0 && res
<= 0x7F)
94 DiagnosticsEngine::Warning
,
95 "suspicious implicit cast from %0 to %1",
97 << expr
->getSubExpr()->getType() << expr
->getType()
98 << expr
->getSourceRange();
102 bool preRun() override
{
103 return compiler
.getLangOpts().CPlusPlus
;
106 void run() override
{
108 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
113 std::stack
<Expr
const *> subExprs_
;
116 static loplugin::Plugin::Registration
<UnicodeToChar
> unicodetochar("unicodetochar");
120 #endif // LO_CLANG_SHARED_PLUGINS
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */