Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / compilerplugins / clang / salunicodeliteral.cxx
blob529d20c970eb52b5e6d7117a354c33e96c44e80c
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 "check.hxx"
11 #include "plugin.hxx"
13 namespace {
15 bool isAsciiCharacterLiteral(Expr const * expr) {
16 if (auto const e = dyn_cast<CharacterLiteral>(expr)) {
17 return e->getKind() == CharacterLiteral::Ascii;
19 return false;
22 class SalUnicodeLiteral final:
23 public RecursiveASTVisitor<SalUnicodeLiteral>, public loplugin::Plugin
25 public:
26 explicit SalUnicodeLiteral(loplugin::InstantiationData const & data):
27 Plugin(data) {}
29 bool VisitCXXStaticCastExpr(CXXStaticCastExpr const * expr) {
30 check(expr);
31 return true;
34 bool VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr const * expr) {
35 check(expr);
36 return true;
39 bool VisitCStyleCastExpr(CStyleCastExpr const * expr) {
40 check(expr);
41 return true;
44 private:
45 void run() override {
46 if (compiler.getLangOpts().CPlusPlus
47 && compiler.getPreprocessor().getIdentifierInfo(
48 "LIBO_INTERNAL_ONLY")->hasMacroDefinition())
50 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
54 void check(ExplicitCastExpr const * expr) {
55 if (ignoreLocation(expr)
56 || isInUnoIncludeFile(expr->getExprLoc()))
57 //TODO: '#ifdef LIBO_INTERNAL_ONLY' within UNO include files
59 return;
61 for (auto t = expr->getTypeAsWritten();;) {
62 auto const tt = t->getAs<TypedefType>();
63 if (tt == nullptr) {
64 return;
66 if (loplugin::TypeCheck(t).Typedef("sal_Unicode")
67 .GlobalNamespace())
69 break;
71 t = tt->desugar();
73 auto const e1 = expr->getSubExprAsWritten();
74 auto const loc = e1->getLocStart();
75 if (loc.isMacroID()
76 && compiler.getSourceManager().isAtStartOfImmediateMacroExpansion(
77 loc))
79 return;
81 auto const e2 = e1->IgnoreParenImpCasts();
82 if (isAsciiCharacterLiteral(e2) || isa<IntegerLiteral>(e2)) {
83 report(
84 DiagnosticsEngine::Warning,
85 ("in LIBO_INTERNAL_ONLY code, replace literal cast to %0 with a"
86 " u'...' char16_t character literal"),
87 e2->getExprLoc())
88 << expr->getTypeAsWritten() << expr->getSourceRange();
93 static loplugin::Plugin::Registration<SalUnicodeLiteral> reg(
94 "salunicodeliteral");
98 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */