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 bool isAsciiCharacterLiteral(Expr
const * expr
) {
16 if (auto const e
= dyn_cast
<CharacterLiteral
>(expr
)) {
17 return e
->getKind() == CharacterLiteral::Ascii
;
22 class SalUnicodeLiteral final
:
23 public RecursiveASTVisitor
<SalUnicodeLiteral
>, public loplugin::Plugin
26 explicit SalUnicodeLiteral(loplugin::InstantiationData
const & data
):
29 bool VisitCXXStaticCastExpr(CXXStaticCastExpr
const * expr
) {
34 bool VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr
const * expr
) {
39 bool VisitCStyleCastExpr(CStyleCastExpr
const * expr
) {
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
61 for (auto t
= expr
->getTypeAsWritten();;) {
62 auto const tt
= t
->getAs
<TypedefType
>();
66 if (loplugin::TypeCheck(t
).Typedef("sal_Unicode")
73 auto const e1
= expr
->getSubExprAsWritten();
74 auto const loc
= e1
->getLocStart();
76 && compiler
.getSourceManager().isAtStartOfImmediateMacroExpansion(
81 auto const e2
= e1
->IgnoreParenImpCasts();
82 if (isAsciiCharacterLiteral(e2
) || isa
<IntegerLiteral
>(e2
)) {
84 DiagnosticsEngine::Warning
,
85 ("in LIBO_INTERNAL_ONLY code, replace literal cast to %0 with a"
86 " u'...' char16_t character literal"),
88 << expr
->getTypeAsWritten() << expr
->getSourceRange();
93 static loplugin::Plugin::Registration
<SalUnicodeLiteral
> reg(
98 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */