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/.
15 // In C++, find implicit conversions from char16_t (aka sal_Unicode) to char.
16 // Such places are probably meant to properly work on char16_t instead.
20 class UnicodeToChar final
:
21 public loplugin::FilteringPlugin
<UnicodeToChar
>
24 explicit UnicodeToChar(loplugin::InstantiationData
const & data
):
25 FilteringPlugin(data
) {}
27 bool TraverseCStyleCastExpr(CStyleCastExpr
* expr
) {
28 subExprs_
.push(expr
->getSubExpr());
29 bool ret
= RecursiveASTVisitor::TraverseCStyleCastExpr(expr
);
34 bool TraverseCXXStaticCastExpr(CXXStaticCastExpr
* expr
) {
35 subExprs_
.push(expr
->getSubExpr());
36 bool ret
= RecursiveASTVisitor::TraverseCXXStaticCastExpr(expr
);
41 bool TraverseCXXFunctionalCastExpr(CXXFunctionalCastExpr
* expr
) {
42 subExprs_
.push(expr
->getSubExpr());
43 bool ret
= RecursiveASTVisitor::TraverseCXXFunctionalCastExpr(expr
);
48 bool VisitImplicitCastExpr(ImplicitCastExpr
const * expr
) {
49 if ((!subExprs_
.empty() && expr
== subExprs_
.top())
50 || ignoreLocation(expr
))
54 if (!(loplugin::TypeCheck(expr
->getType()).Char()
55 && expr
->getSubExpr()->getType()->isSpecificBuiltinType(
56 clang::BuiltinType::Char16
)))
61 if (compat::EvaluateAsInt(expr
->getSubExpr(), res
, compiler
.getASTContext())
62 && res
>= 0 && res
<= 0x7F)
67 DiagnosticsEngine::Warning
,
68 "suspicious implicit cast from %0 to %1",
70 << expr
->getSubExpr()->getType() << expr
->getType()
71 << expr
->getSourceRange();
77 if (compiler
.getLangOpts().CPlusPlus
) {
78 TraverseDecl(compiler
.getASTContext().getTranslationUnitDecl());
82 std::stack
<Expr
const *> subExprs_
;
85 static loplugin::Plugin::Registration
<UnicodeToChar
> reg("unicodetochar");
89 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */