summaryrefslogtreecommitdiffstats
path: root/compilerplugins/clang/test/unusedenumconstants.cxx
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 09:06:44 +0000
commited5640d8b587fbcfed7dd7967f3de04b37a76f26 (patch)
tree7a5f7c6c9d02226d7471cb3cc8fbbf631b415303 /compilerplugins/clang/test/unusedenumconstants.cxx
parentInitial commit. (diff)
downloadlibreoffice-cb75148ebd0135178ff46f89a30139c44f8d2040.tar.xz
libreoffice-cb75148ebd0135178ff46f89a30139c44f8d2040.zip
Adding upstream version 4:7.4.7.upstream/4%7.4.7upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'compilerplugins/clang/test/unusedenumconstants.cxx')
-rw-r--r--compilerplugins/clang/test/unusedenumconstants.cxx120
1 files changed, 120 insertions, 0 deletions
diff --git a/compilerplugins/clang/test/unusedenumconstants.cxx b/compilerplugins/clang/test/unusedenumconstants.cxx
new file mode 100644
index 000000000..3f69e8993
--- /dev/null
+++ b/compilerplugins/clang/test/unusedenumconstants.cxx
@@ -0,0 +1,120 @@
+/* -*- 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 <o3tl/typed_flags_set.hxx>
+
+namespace test1
+{
+void test1()
+{
+ enum NameClashMode
+ {
+ NONE,
+ NO_CLASH // expected-error {{write NO_CLASH [loplugin:unusedenumconstants]}}
+ };
+ NameClashMode eNameClashMode = NO_CLASH;
+ (void)eNameClashMode;
+}
+};
+
+enum class BrowseMode
+{
+ Modules = 0x01, // expected-error {{read Modules [loplugin:unusedenumconstants]}}
+ Top = 0x02, // expected-error {{write Top [loplugin:unusedenumconstants]}}
+ Bottom = 0x04, // expected-error {{read Bottom [loplugin:unusedenumconstants]}}
+ Left = 0x08, // expected-error {{write Left [loplugin:unusedenumconstants]}}
+};
+namespace o3tl
+{
+template <> struct typed_flags<BrowseMode> : is_typed_flags<BrowseMode, 0xf>
+{
+};
+}
+BrowseMode g_flags;
+int test2(BrowseMode nMode)
+{
+ if (nMode & BrowseMode::Modules)
+ return 1;
+ g_flags |= BrowseMode::Top;
+ return 0;
+}
+bool test2b(BrowseMode nMode) { return bool(nMode & BrowseMode::Bottom); }
+BrowseMode test2c() { return BrowseMode::Left; }
+
+enum class Enum3
+{
+ One = 0x01, // expected-error {{write One [loplugin:unusedenumconstants]}}
+ Two = 0x02 // expected-error {{write Two [loplugin:unusedenumconstants]}}
+};
+namespace o3tl
+{
+template <> struct typed_flags<Enum3> : is_typed_flags<Enum3, 0x3>
+{
+};
+}
+void test3_foo(Enum3);
+void test3() { test3_foo(Enum3::One | Enum3::Two); }
+
+namespace test4
+{
+enum Enum4
+{
+ ONE, // expected-error {{write ONE [loplugin:unusedenumconstants]}}
+ TWO
+};
+struct Test4Base
+{
+ Test4Base(Enum4) {}
+};
+struct Test4 : public Test4Base
+{
+ Test4()
+ : Test4Base(Enum4::ONE)
+ {
+ }
+};
+};
+
+//-----------------------------------------------------------------------------------
+
+// check that conditional operator walks up the tree
+namespace test5
+{
+enum Enum
+{
+ ONE, // expected-error {{write ONE [loplugin:unusedenumconstants]}}
+ TWO // expected-error {{write TWO [loplugin:unusedenumconstants]}}
+};
+
+Enum foo(int x) { return x == 1 ? Enum::ONE : Enum::TWO; }
+};
+
+//-----------------------------------------------------------------------------------
+// Ignore a common pattern that does not introduce any new information, merely removes
+// information.
+enum class Enum6
+{
+ Modules = 0x01, // expected-error {{write Modules [loplugin:unusedenumconstants]}}
+ Top = 0x02,
+};
+namespace o3tl
+{
+template <> struct typed_flags<Enum6> : is_typed_flags<Enum6, 0x03>
+{
+};
+}
+void test6()
+{
+ Enum6 foo = Enum6::Modules;
+ foo &= ~Enum6::Top;
+ foo &= (~Enum6::Top);
+ (void)foo;
+};
+
+/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */