lok: getSlideShowInfo: interactions: check that properties are available
[LibreOffice.git] / compilerplugins / clang / unicodetochar.cxx
blobe404211ffed1feedcba099919e6259489ad4f903
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 #ifndef LO_CLANG_SHARED_PLUGINS
12 #include <stack>
14 #include "check.hxx"
15 #include "compat.hxx"
16 #include "plugin.hxx"
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.
21 namespace {
23 class UnicodeToChar final:
24 public loplugin::FilteringPlugin<UnicodeToChar>
26 public:
27 explicit UnicodeToChar(loplugin::InstantiationData const & data):
28 FilteringPlugin(data) {}
30 bool PreTraverseCStyleCastExpr(CStyleCastExpr * expr) {
31 subExprs_.push(expr->getSubExpr());
32 return true;
34 bool PostTraverseCStyleCastExpr(CStyleCastExpr *, bool ) {
35 subExprs_.pop();
36 return true;
38 bool TraverseCStyleCastExpr(CStyleCastExpr * expr) {
39 PreTraverseCStyleCastExpr(expr);
40 bool ret = RecursiveASTVisitor::TraverseCStyleCastExpr(expr);
41 PostTraverseCStyleCastExpr(expr, ret);
42 return ret;
45 bool PreTraverseCXXStaticCastExpr(CXXStaticCastExpr * expr) {
46 subExprs_.push(expr->getSubExpr());
47 return true;
49 bool PostTraverseCXXStaticCastExpr(CXXStaticCastExpr *, bool) {
50 subExprs_.pop();
51 return true;
53 bool TraverseCXXStaticCastExpr(CXXStaticCastExpr * expr) {
54 PreTraverseCXXStaticCastExpr(expr);
55 bool ret = RecursiveASTVisitor::TraverseCXXStaticCastExpr(expr);
56 PostTraverseCXXStaticCastExpr(expr, ret);
57 return ret;
60 bool PreTraverseCXXFunctionalCastExpr(CXXFunctionalCastExpr * expr) {
61 subExprs_.push(expr->getSubExpr());
62 return true;
64 bool PostTraverseCXXFunctionalCastExpr(CXXFunctionalCastExpr *, bool) {
65 subExprs_.pop();
66 return true;
68 bool TraverseCXXFunctionalCastExpr(CXXFunctionalCastExpr * expr) {
69 PreTraverseCXXFunctionalCastExpr(expr);
70 bool ret = RecursiveASTVisitor::TraverseCXXFunctionalCastExpr(expr);
71 PostTraverseCXXFunctionalCastExpr(expr, ret);
72 return ret;
75 bool VisitImplicitCastExpr(ImplicitCastExpr const * expr) {
76 if ((!subExprs_.empty() && expr == subExprs_.top())
77 || ignoreLocation(expr))
79 return true;
81 if (!(loplugin::TypeCheck(expr->getType()).Char()
82 && expr->getSubExpr()->getType()->isSpecificBuiltinType(
83 clang::BuiltinType::Char16)))
85 return true;
87 APSInt res;
88 if (compat::EvaluateAsInt(expr->getSubExpr(), res, compiler.getASTContext())
89 && res >= 0 && res <= 0x7F)
91 return true;
93 report(
94 DiagnosticsEngine::Warning,
95 "suspicious implicit cast from %0 to %1",
96 expr->getExprLoc())
97 << expr->getSubExpr()->getType() << expr->getType()
98 << expr->getSourceRange();
99 return true;
102 bool preRun() override {
103 return compiler.getLangOpts().CPlusPlus;
106 void run() override {
107 if (preRun()) {
108 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
112 private:
113 std::stack<Expr const *> subExprs_;
116 static loplugin::Plugin::Registration<UnicodeToChar> unicodetochar("unicodetochar");
118 } // namespace
120 #endif // LO_CLANG_SHARED_PLUGINS
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */