bump product version to 6.4.0.3
[LibreOffice.git] / compilerplugins / clang / salunicodeliteral.cxx
blob4c2a00669ab071ee360dddfd76a4dd72e1b1819b
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 */
9 #ifndef LO_CLANG_SHARED_PLUGINS
11 #include "check.hxx"
12 #include "plugin.hxx"
14 namespace {
16 bool isAsciiCharacterLiteral(Expr const * expr) {
17 if (auto const e = dyn_cast<CharacterLiteral>(expr)) {
18 return e->getKind() == CharacterLiteral::Ascii;
20 return false;
23 class SalUnicodeLiteral final:
24 public loplugin::FilteringPlugin<SalUnicodeLiteral>
26 public:
27 explicit SalUnicodeLiteral(loplugin::InstantiationData const & data):
28 FilteringPlugin(data) {}
30 bool VisitCXXStaticCastExpr(CXXStaticCastExpr const * expr) {
31 check(expr);
32 return true;
35 bool VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr const * expr) {
36 check(expr);
37 return true;
40 bool VisitCStyleCastExpr(CStyleCastExpr const * expr) {
41 check(expr);
42 return true;
45 bool preRun() override {
46 return compiler.getLangOpts().CPlusPlus
47 && compiler.getPreprocessor().getIdentifierInfo(
48 "LIBO_INTERNAL_ONLY")->hasMacroDefinition();
51 void run() override {
52 if (preRun())
53 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
56 private:
57 void check(ExplicitCastExpr const * expr) {
58 if (ignoreLocation(expr)
59 || isInUnoIncludeFile(expr->getExprLoc()))
60 //TODO: '#ifdef LIBO_INTERNAL_ONLY' within UNO include files
62 return;
64 for (auto t = expr->getTypeAsWritten();;) {
65 auto const tt = t->getAs<TypedefType>();
66 if (tt == nullptr) {
67 return;
69 if (loplugin::TypeCheck(t).Typedef("sal_Unicode")
70 .GlobalNamespace())
72 break;
74 t = tt->desugar();
76 auto const e1 = expr->getSubExprAsWritten();
77 auto const loc = compat::getBeginLoc(e1);
78 if (loc.isMacroID()
79 && compiler.getSourceManager().isAtStartOfImmediateMacroExpansion(
80 loc))
82 return;
84 auto const e2 = e1->IgnoreParenImpCasts();
85 if (isAsciiCharacterLiteral(e2) || isa<IntegerLiteral>(e2)) {
86 report(
87 DiagnosticsEngine::Warning,
88 ("in LIBO_INTERNAL_ONLY code, replace literal cast to %0 with a"
89 " u'...' char16_t character literal"),
90 e2->getExprLoc())
91 << expr->getTypeAsWritten() << expr->getSourceRange();
96 static loplugin::Plugin::Registration<SalUnicodeLiteral> salunicodeliteral("salunicodeliteral");
98 } // namespace
100 #endif // LO_CLANG_SHARED_PLUGINS
102 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */