summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--compilerplugins/clang/salunicodeliteral.cxx86
-rw-r--r--compilerplugins/clang/test/salunicodeliteral.cxx39
-rw-r--r--compilerplugins/clang/test/salunicodeliteral.hxx17
-rw-r--r--solenv/CompilerTest_compilerplugins_clang.mk1
4 files changed, 143 insertions, 0 deletions
diff --git a/compilerplugins/clang/salunicodeliteral.cxx b/compilerplugins/clang/salunicodeliteral.cxx
new file mode 100644
index 000000000000..6b03156b55e7
--- /dev/null
+++ b/compilerplugins/clang/salunicodeliteral.cxx
@@ -0,0 +1,86 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include "check.hxx"
+#include "plugin.hxx"
+
+namespace {
+
+bool isAsciiCharacterLiteral(Expr const * expr) {
+ if (auto const e = dyn_cast<CharacterLiteral>(expr)) {
+ return e->getKind() == CharacterLiteral::Ascii;
+ }
+ return false;
+}
+
+class Visitor final:
+ public RecursiveASTVisitor<Visitor>, public loplugin::Plugin
+{
+public:
+ explicit Visitor(InstantiationData const & data): Plugin(data) {}
+
+ bool VisitCXXStaticCastExpr(CXXStaticCastExpr const * expr) {
+ check(expr);
+ return true;
+ }
+
+ bool VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr const * expr) {
+ check(expr);
+ return true;
+ }
+
+ bool VisitCStyleCastExpr(CStyleCastExpr const * expr) {
+ check(expr);
+ return true;
+ }
+
+private:
+ void run() override {
+ if (compiler.getLangOpts().CPlusPlus
+ && compiler.getPreprocessor().getIdentifierInfo(
+ "LIBO_INTERNAL_ONLY")->hasMacroDefinition())
+ {
+ TraverseDecl(compiler.getASTContext().getTranslationUnitDecl());
+ }
+ }
+
+ void check(ExplicitCastExpr const * expr) {
+ if (ignoreLocation(expr)
+ || isInUnoIncludeFile(expr->getExprLoc())
+ //TODO: '#ifdef LIBO_INTERNAL_ONLY' within UNO include files
+ || !(loplugin::TypeCheck(expr->getTypeAsWritten())
+ .Typedef("sal_Unicode").GlobalNamespace()))
+ {
+ return;
+ }
+ auto const e1 = expr->getSubExprAsWritten();
+ auto const loc = e1->getLocStart();
+ if (loc.isMacroID()
+ && compiler.getSourceManager().isAtStartOfImmediateMacroExpansion(
+ loc))
+ {
+ return;
+ }
+ auto const e2 = e1->IgnoreParenImpCasts();
+ if (isAsciiCharacterLiteral(e2) || isa<IntegerLiteral>(e2)) {
+ report(
+ DiagnosticsEngine::Warning,
+ ("in LIBO_INTERNAL_ONLY code, replace literal cast to %0 with a"
+ " u'...' char16_t character literal"),
+ e2->getExprLoc())
+ << expr->getTypeAsWritten() << expr->getSourceRange();
+ }
+ }
+};
+
+static loplugin::Plugin::Registration<Visitor> reg("salunicodeliteral");
+
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/compilerplugins/clang/test/salunicodeliteral.cxx b/compilerplugins/clang/test/salunicodeliteral.cxx
new file mode 100644
index 000000000000..1daf9df0acb9
--- /dev/null
+++ b/compilerplugins/clang/test/salunicodeliteral.cxx
@@ -0,0 +1,39 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#include "sal/config.h"
+
+#include "sal/types.h"
+
+#include "salunicodeliteral.hxx"
+
+#define TEST1 'x'
+#define TEST2 sal_Unicode('x')
+
+namespace {
+
+void f(sal_Unicode) {}
+
+}
+
+void test() {
+ f(sal_Unicode('x')); // expected-error {{in LIBO_INTERNAL_ONLY code, replace literal cast to 'sal_Unicode' (aka 'char16_t') with a u'...' char16_t character literal [loplugin:salunicodeliteral]}}
+ f(static_cast<sal_Unicode>('x')); // expected-error {{in LIBO_INTERNAL_ONLY code, replace literal cast to 'sal_Unicode' (aka 'char16_t') with a u'...' char16_t character literal [loplugin:salunicodeliteral]}}
+ f(static_cast<sal_Unicode const>('x')); // expected-error {{in LIBO_INTERNAL_ONLY code, replace literal cast to 'const sal_Unicode' (aka 'const char16_t') with a u'...' char16_t character literal [loplugin:salunicodeliteral]}}
+ f((sal_Unicode) 'x'); // expected-error {{in LIBO_INTERNAL_ONLY code, replace literal cast to 'sal_Unicode' (aka 'char16_t') with a u'...' char16_t character literal [loplugin:salunicodeliteral]}}
+ f(sal_Unicode(('x'))); // expected-error {{in LIBO_INTERNAL_ONLY code, replace literal cast to 'sal_Unicode' (aka 'char16_t') with a u'...' char16_t character literal [loplugin:salunicodeliteral]}}
+ f(sal_Unicode(120)); // expected-error {{in LIBO_INTERNAL_ONLY code, replace literal cast to 'sal_Unicode' (aka 'char16_t') with a u'...' char16_t character literal [loplugin:salunicodeliteral]}}
+ f(sal_Unicode(TEST1));
+ f(TEST2); // expected-error {{in LIBO_INTERNAL_ONLY code, replace literal cast to 'sal_Unicode' (aka 'char16_t') with a u'...' char16_t character literal [loplugin:salunicodeliteral]}}
+ char c = 'x';
+ f(sal_Unicode(c));
+ f(char16_t('x'));
+}
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/compilerplugins/clang/test/salunicodeliteral.hxx b/compilerplugins/clang/test/salunicodeliteral.hxx
new file mode 100644
index 000000000000..f4491c72397f
--- /dev/null
+++ b/compilerplugins/clang/test/salunicodeliteral.hxx
@@ -0,0 +1,17 @@
+/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
+/*
+ * This file is part of the LibreOffice project.
+ *
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/.
+ */
+
+#ifndef INCLUDED_COMPILERPLUGINS_CLANG_TEST_SALUNICODELITERAL_HXX
+#define INCLUDED_COMPILERPLUGINS_CLANG_TEST_SALUNICODELITERAL_HXX
+
+void test();
+
+#endif
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
diff --git a/solenv/CompilerTest_compilerplugins_clang.mk b/solenv/CompilerTest_compilerplugins_clang.mk
index 9f8810eb4057..a4de36ee090a 100644
--- a/solenv/CompilerTest_compilerplugins_clang.mk
+++ b/solenv/CompilerTest_compilerplugins_clang.mk
@@ -22,6 +22,7 @@ $(eval $(call gb_CompilerTest_add_exception_objects,compilerplugins_clang, \
compilerplugins/clang/test/redundantcast \
compilerplugins/clang/test/redundantinline \
compilerplugins/clang/test/salbool \
+ compilerplugins/clang/test/salunicodeliteral \
compilerplugins/clang/test/stringconstant \
compilerplugins/clang/test/unnecessaryoverride-dtor \
compilerplugins/clang/test/unoany \