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. 4 * 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 10 11 #include "check.hxx" 12 #include "plugin.hxx" 13 14 namespace { 15 isAsciiCharacterLiteral(Expr const * expr)16bool isAsciiCharacterLiteral(Expr const * expr) { 17 if (auto const e = dyn_cast<CharacterLiteral>(expr)) { 18 return e->getKind() == CharacterLiteral::Ascii; 19 } 20 return false; 21 } 22 23 class SalUnicodeLiteral final: 24 public loplugin::FilteringPlugin<SalUnicodeLiteral> 25 { 26 public: SalUnicodeLiteral(loplugin::InstantiationData const & data)27 explicit SalUnicodeLiteral(loplugin::InstantiationData const & data): 28 FilteringPlugin(data) {} 29 VisitCXXStaticCastExpr(CXXStaticCastExpr const * expr)30 bool VisitCXXStaticCastExpr(CXXStaticCastExpr const * expr) { 31 check(expr); 32 return true; 33 } 34 VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr const * expr)35 bool VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr const * expr) { 36 check(expr); 37 return true; 38 } 39 VisitCStyleCastExpr(CStyleCastExpr const * expr)40 bool VisitCStyleCastExpr(CStyleCastExpr const * expr) { 41 check(expr); 42 return true; 43 } 44 preRun()45 bool preRun() override { 46 return compiler.getLangOpts().CPlusPlus 47 && compiler.getPreprocessor().getIdentifierInfo( 48 "LIBO_INTERNAL_ONLY")->hasMacroDefinition(); 49 } 50 run()51 void run() override { 52 if (preRun()) 53 TraverseDecl(compiler.getASTContext().getTranslationUnitDecl()); 54 } 55 56 private: check(ExplicitCastExpr const * expr)57 void check(ExplicitCastExpr const * expr) { 58 if (ignoreLocation(expr) 59 || isInUnoIncludeFile(expr->getExprLoc())) 60 //TODO: '#ifdef LIBO_INTERNAL_ONLY' within UNO include files 61 { 62 return; 63 } 64 for (auto t = expr->getTypeAsWritten();;) { 65 auto const tt = t->getAs<TypedefType>(); 66 if (tt == nullptr) { 67 return; 68 } 69 if (loplugin::TypeCheck(t).Typedef("sal_Unicode") 70 .GlobalNamespace()) 71 { 72 break; 73 } 74 t = tt->desugar(); 75 } 76 auto const e1 = expr->getSubExprAsWritten(); 77 auto const loc = compat::getBeginLoc(e1); 78 if (loc.isMacroID() 79 && compiler.getSourceManager().isAtStartOfImmediateMacroExpansion( 80 loc)) 81 { 82 return; 83 } 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(); 92 } 93 } 94 }; 95 96 static loplugin::Plugin::Registration<SalUnicodeLiteral> salunicodeliteral("salunicodeliteral"); 97 98 } // namespace 99 100 #endif // LO_CLANG_SHARED_PLUGINS 101 102 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ 103