bump product version to 6.3.0.0.beta1
[LibreOffice.git] / compilerplugins / clang / unicodetochar.cxx
blobe1e381ad2c71c822668492800e3433387c508522
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
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/.
8 */
10 #include <stack>
12 #include "check.hxx"
13 #include "plugin.hxx"
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.
18 namespace {
20 class UnicodeToChar final:
21 public loplugin::FilteringPlugin<UnicodeToChar>
23 public:
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);
30 subExprs_.pop();
31 return ret;
34 bool TraverseCXXStaticCastExpr(CXXStaticCastExpr * expr) {
35 subExprs_.push(expr->getSubExpr());
36 bool ret = RecursiveASTVisitor::TraverseCXXStaticCastExpr(expr);
37 subExprs_.pop();
38 return ret;
41 bool TraverseCXXFunctionalCastExpr(CXXFunctionalCastExpr * expr) {
42 subExprs_.push(expr->getSubExpr());
43 bool ret = RecursiveASTVisitor::TraverseCXXFunctionalCastExpr(expr);
44 subExprs_.pop();
45 return ret;
48 bool VisitImplicitCastExpr(ImplicitCastExpr const * expr) {
49 if ((!subExprs_.empty() && expr == subExprs_.top())
50 || ignoreLocation(expr))
52 return true;
54 if (!(loplugin::TypeCheck(expr->getType()).Char()
55 && expr->getSubExpr()->getType()->isSpecificBuiltinType(
56 clang::BuiltinType::Char16)))
58 return true;
60 APSInt res;
61 if (compat::EvaluateAsInt(expr->getSubExpr(), res, compiler.getASTContext())
62 && res >= 0 && res <= 0x7F)
64 return true;
66 report(
67 DiagnosticsEngine::Warning,
68 "suspicious implicit cast from %0 to %1",
69 expr->getExprLoc())
70 << expr->getSubExpr()->getType() << expr->getType()
71 << expr->getSourceRange();
72 return true;
75 private:
76 void run() override {
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: */