enum-flags-selftests.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /* Self tests for enum-flags for GDB, the GNU debugger.
  2. Copyright (C) 2016-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #include "defs.h"
  15. #include "gdbsupport/enum-flags.h"
  16. #include "gdbsupport/valid-expr.h"
  17. #include "gdbsupport/selftest.h"
  18. namespace selftests {
  19. namespace enum_flags_tests {
  20. /* The (real) enum types used in CHECK_VALID. Their names match the
  21. template parameter names of the templates defined by CHECK_VALID to
  22. make it simpler to use. They could be named differently. */
  23. /* A "real enum". */
  24. enum RE
  25. {
  26. RE_FLAG1 = 1 << 1,
  27. RE_FLAG2 = 1 << 2,
  28. };
  29. /* Another "real enum". */
  30. enum RE2
  31. {
  32. RE2_FLAG1 = 1 << 1,
  33. RE2_FLAG2 = 1 << 2,
  34. };
  35. /* An unsigned "real enum". */
  36. enum URE : unsigned
  37. {
  38. URE_FLAG1 = 1 << 1,
  39. URE_FLAG2 = 1 << 2,
  40. URE_FLAG3 = 0xffffffff,
  41. };
  42. /* A non-flags enum. */
  43. enum NF
  44. {
  45. NF_FLAG1 = 1 << 1,
  46. NF_FLAG2 = 1 << 2,
  47. };
  48. /* The corresponding "enum flags" types. */
  49. DEF_ENUM_FLAGS_TYPE (RE, EF);
  50. DEF_ENUM_FLAGS_TYPE (RE2, EF2);
  51. DEF_ENUM_FLAGS_TYPE (URE, UEF);
  52. #if HAVE_IS_TRIVIALLY_COPYABLE
  53. /* So that std::vectors of types that have enum_flags fields can
  54. reallocate efficiently memcpy. */
  55. gdb_static_assert (std::is_trivially_copyable<EF>::value);
  56. #endif
  57. /* A couple globals used as lvalues in the CHECK_VALID expressions
  58. below. Their names (and types) match the uppercase type names
  59. exposed by CHECK_VALID just to make the expressions easier to
  60. follow. */
  61. static RE re ATTRIBUTE_UNUSED;
  62. static EF ef ATTRIBUTE_UNUSED;
  63. /* First, compile-time tests that:
  64. - make sure that incorrect operations with mismatching enum types
  65. are caught at compile time.
  66. - make sure that the same operations but involving the right enum
  67. types do compile and that they return the correct type.
  68. */
  69. #define CHECK_VALID(VALID, EXPR_TYPE, EXPR) \
  70. CHECK_VALID_EXPR_6 (EF, RE, EF2, RE2, UEF, URE, VALID, EXPR_TYPE, EXPR)
  71. typedef std::underlying_type<RE>::type und;
  72. /* Test construction / conversion from/to different types. */
  73. /* RE/EF -> underlying (explicit) */
  74. CHECK_VALID (true, und, und (RE ()))
  75. CHECK_VALID (true, und, und (EF ()))
  76. /* RE/EF -> int (explicit) */
  77. CHECK_VALID (true, int, int (RE ()))
  78. CHECK_VALID (true, int, int (EF ()))
  79. /* other -> RE */
  80. /* You can construct a raw enum value from an int explicitly to punch
  81. a hole in the type system if need to. */
  82. CHECK_VALID (true, RE, RE (1))
  83. CHECK_VALID (true, RE, RE (RE2 ()))
  84. CHECK_VALID (false, void, RE (EF2 ()))
  85. CHECK_VALID (true, RE, RE (RE ()))
  86. CHECK_VALID (false, void, RE (EF ()))
  87. /* other -> EF. */
  88. /* As expected, enum-flags is a stronger type than the backing raw
  89. enum. Unlike with raw enums, you can't construct an enum flags
  90. from an integer nor from an unrelated enum type explicitly. Add an
  91. intermediate conversion via the raw enum if you really need it. */
  92. CHECK_VALID (false, void, EF (1))
  93. CHECK_VALID (false, void, EF (1u))
  94. CHECK_VALID (false, void, EF (RE2 ()))
  95. CHECK_VALID (false, void, EF (EF2 ()))
  96. CHECK_VALID (true, EF, EF (RE ()))
  97. CHECK_VALID (true, EF, EF (EF ()))
  98. /* Test operators. */
  99. /* operator OP (raw_enum, int) */
  100. CHECK_VALID (false, void, RE () | 1)
  101. CHECK_VALID (false, void, RE () & 1)
  102. CHECK_VALID (false, void, RE () ^ 1)
  103. /* operator OP (int, raw_enum) */
  104. CHECK_VALID (false, void, 1 | RE ())
  105. CHECK_VALID (false, void, 1 & RE ())
  106. CHECK_VALID (false, void, 1 ^ RE ())
  107. /* operator OP (enum_flags, int) */
  108. CHECK_VALID (false, void, EF () | 1)
  109. CHECK_VALID (false, void, EF () & 1)
  110. CHECK_VALID (false, void, EF () ^ 1)
  111. /* operator OP (int, enum_flags) */
  112. CHECK_VALID (false, void, 1 | EF ())
  113. CHECK_VALID (false, void, 1 & EF ())
  114. CHECK_VALID (false, void, 1 ^ EF ())
  115. /* operator OP (raw_enum, raw_enum) */
  116. CHECK_VALID (false, void, RE () | RE2 ())
  117. CHECK_VALID (false, void, RE () & RE2 ())
  118. CHECK_VALID (false, void, RE () ^ RE2 ())
  119. CHECK_VALID (true, RE, RE () | RE ())
  120. CHECK_VALID (true, RE, RE () & RE ())
  121. CHECK_VALID (true, RE, RE () ^ RE ())
  122. /* operator OP (enum_flags, raw_enum) */
  123. CHECK_VALID (false, void, EF () | RE2 ())
  124. CHECK_VALID (false, void, EF () & RE2 ())
  125. CHECK_VALID (false, void, EF () ^ RE2 ())
  126. CHECK_VALID (true, EF, EF () | RE ())
  127. CHECK_VALID (true, EF, EF () & RE ())
  128. CHECK_VALID (true, EF, EF () ^ RE ())
  129. /* operator OP= (raw_enum, raw_enum), rvalue ref on the lhs. */
  130. CHECK_VALID (false, void, RE () |= RE2 ())
  131. CHECK_VALID (false, void, RE () &= RE2 ())
  132. CHECK_VALID (false, void, RE () ^= RE2 ())
  133. CHECK_VALID (false, void, RE () |= RE ())
  134. CHECK_VALID (false, void, RE () &= RE ())
  135. CHECK_VALID (false, void, RE () ^= RE ())
  136. /* operator OP= (raw_enum, raw_enum), lvalue ref on the lhs. */
  137. CHECK_VALID (false, void, re |= RE2 ())
  138. CHECK_VALID (false, void, re &= RE2 ())
  139. CHECK_VALID (false, void, re ^= RE2 ())
  140. CHECK_VALID (true, RE&, re |= RE ())
  141. CHECK_VALID (true, RE&, re &= RE ())
  142. CHECK_VALID (true, RE&, re ^= RE ())
  143. /* operator OP= (enum_flags, raw_enum), rvalue ref on the lhs. */
  144. CHECK_VALID (false, void, EF () |= RE2 ())
  145. CHECK_VALID (false, void, EF () &= RE2 ())
  146. CHECK_VALID (false, void, EF () ^= RE2 ())
  147. CHECK_VALID (false, void, EF () |= RE ())
  148. CHECK_VALID (false, void, EF () &= RE ())
  149. CHECK_VALID (false, void, EF () ^= RE ())
  150. /* operator OP= (enum_flags, raw_enum), lvalue ref on the lhs. */
  151. CHECK_VALID (false, void, ef |= RE2 ())
  152. CHECK_VALID (false, void, ef &= RE2 ())
  153. CHECK_VALID (false, void, ef ^= RE2 ())
  154. CHECK_VALID (true, EF&, ef |= EF ())
  155. CHECK_VALID (true, EF&, ef &= EF ())
  156. CHECK_VALID (true, EF&, ef ^= EF ())
  157. /* operator OP= (enum_flags, enum_flags), rvalue ref on the lhs. */
  158. CHECK_VALID (false, void, EF () |= EF2 ())
  159. CHECK_VALID (false, void, EF () &= EF2 ())
  160. CHECK_VALID (false, void, EF () ^= EF2 ())
  161. CHECK_VALID (false, void, EF () |= EF ())
  162. CHECK_VALID (false, void, EF () &= EF ())
  163. CHECK_VALID (false, void, EF () ^= EF ())
  164. /* operator OP= (enum_flags, enum_flags), lvalue ref on the lhs. */
  165. CHECK_VALID (false, void, ef |= EF2 ())
  166. CHECK_VALID (false, void, ef &= EF2 ())
  167. CHECK_VALID (false, void, ef ^= EF2 ())
  168. CHECK_VALID (true, EF&, ef |= EF ())
  169. CHECK_VALID (true, EF&, ef &= EF ())
  170. CHECK_VALID (true, EF&, ef ^= EF ())
  171. /* operator~ (raw_enum) */
  172. CHECK_VALID (false, void, ~RE ())
  173. CHECK_VALID (true, URE, ~URE ())
  174. /* operator~ (enum_flags) */
  175. CHECK_VALID (false, void, ~EF ())
  176. CHECK_VALID (true, UEF, ~UEF ())
  177. /* Check ternary operator. This exercises implicit conversions. */
  178. CHECK_VALID (true, EF, true ? EF () : RE ())
  179. CHECK_VALID (true, EF, true ? RE () : EF ())
  180. /* These are valid, but it's not a big deal since you won't be able to
  181. assign the resulting integer to an enum or an enum_flags without a
  182. cast.
  183. The latter two tests are disabled on older GCCs because they
  184. incorrectly fail with gcc 4.8 and 4.9 at least. Running the test
  185. outside a SFINAE context shows:
  186. invalid user-defined conversion from ‘EF’ to ‘RE2’
  187. They've been confirmed to compile/pass with gcc 5.3, gcc 7.1 and
  188. clang 3.7. */
  189. CHECK_VALID (true, int, true ? EF () : EF2 ())
  190. CHECK_VALID (true, int, true ? EF2 () : EF ())
  191. #if GCC_VERSION >= 5003 || defined __clang__
  192. CHECK_VALID (true, int, true ? EF () : RE2 ())
  193. CHECK_VALID (true, int, true ? RE2 () : EF ())
  194. #endif
  195. /* Same, but with an unsigned enum. */
  196. typedef unsigned int uns;
  197. CHECK_VALID (true, uns, true ? EF () : UEF ())
  198. CHECK_VALID (true, uns, true ? UEF () : EF ())
  199. #if GCC_VERSION >= 5003 || defined __clang__
  200. CHECK_VALID (true, uns, true ? EF () : URE ())
  201. CHECK_VALID (true, uns, true ? URE () : EF ())
  202. #endif
  203. /* Unfortunately this can't work due to the way C++ computes the
  204. return type of the ternary conditional operator. int isn't
  205. implicitly convertible to the raw enum type, so the type of the
  206. expression is int. And then int is not implicitly convertible to
  207. enum_flags.
  208. GCC 4.8 fails to compile this test with:
  209. error: operands to ?: have different types ‘enum_flags<RE>’ and ‘int’
  210. Confirmed to work with gcc 4.9, 5.3 and clang 3.7.
  211. */
  212. #if GCC_VERSION >= 4009 || defined __clang__
  213. CHECK_VALID (false, void, true ? EF () : 0)
  214. CHECK_VALID (false, void, true ? 0 : EF ())
  215. #endif
  216. /* Check that the ++/--/<</>>/<<=/>>= operators are deleted. */
  217. CHECK_VALID (false, void, RE ()++)
  218. CHECK_VALID (false, void, ++RE ())
  219. CHECK_VALID (false, void, --RE ())
  220. CHECK_VALID (false, void, RE ()--)
  221. CHECK_VALID (false, void, RE () << 1)
  222. CHECK_VALID (false, void, RE () >> 1)
  223. CHECK_VALID (false, void, EF () << 1)
  224. CHECK_VALID (false, void, EF () >> 1)
  225. CHECK_VALID (false, void, RE () <<= 1)
  226. CHECK_VALID (false, void, RE () >>= 1)
  227. CHECK_VALID (false, void, EF () <<= 1)
  228. CHECK_VALID (false, void, EF () >>= 1)
  229. /* Test comparison operators. */
  230. CHECK_VALID (false, void, EF () == EF2 ())
  231. CHECK_VALID (false, void, EF () == RE2 ())
  232. CHECK_VALID (false, void, RE () == EF2 ())
  233. CHECK_VALID (true, bool, EF (RE (1)) == EF (RE (1)))
  234. CHECK_VALID (true, bool, EF (RE (1)) == RE (1))
  235. CHECK_VALID (true, bool, RE (1) == EF (RE (1)))
  236. CHECK_VALID (false, void, EF () != EF2 ())
  237. CHECK_VALID (false, void, EF () != RE2 ())
  238. CHECK_VALID (false, void, RE () != EF2 ())
  239. /* Disable -Wenum-compare due to:
  240. Clang:
  241. "error: comparison of two values with different enumeration types
  242. [-Werror,-Wenum-compare]"
  243. GCC:
  244. "error: comparison between ‘enum selftests::enum_flags_tests::RE’
  245. and ‘enum selftests::enum_flags_tests::RE2’
  246. [-Werror=enum-compare]"
  247. Not a big deal since misuses like these in GDB will be caught by
  248. -Werror anyway. This check is here mainly for completeness. */
  249. #if defined __GNUC__
  250. # pragma GCC diagnostic push
  251. # pragma GCC diagnostic ignored "-Wenum-compare"
  252. #endif
  253. CHECK_VALID (true, bool, RE () == RE2 ())
  254. CHECK_VALID (true, bool, RE () != RE2 ())
  255. #if defined __GNUC__
  256. # pragma GCC diagnostic pop
  257. #endif
  258. CHECK_VALID (true, bool, EF (RE (1)) != EF (RE (2)))
  259. CHECK_VALID (true, bool, EF (RE (1)) != RE (2))
  260. CHECK_VALID (true, bool, RE (1) != EF (RE (2)))
  261. CHECK_VALID (true, bool, EF () == 0)
  262. /* Check we didn't disable/delete comparison between non-flags enums
  263. and unrelated types by mistake. */
  264. CHECK_VALID (true, bool, NF (1) == NF (1))
  265. CHECK_VALID (true, bool, NF (1) == int (1))
  266. CHECK_VALID (true, bool, NF (1) == char (1))
  267. /* -------------------------------------------------------------------- */
  268. /* Follows misc tests that exercise the API. Some are compile time,
  269. when possible, others are run time. */
  270. enum test_flag
  271. {
  272. FLAG1 = 1 << 1,
  273. FLAG2 = 1 << 2,
  274. FLAG3 = 1 << 3,
  275. };
  276. enum test_uflag : unsigned
  277. {
  278. UFLAG1 = 1 << 1,
  279. UFLAG2 = 1 << 2,
  280. UFLAG3 = 1 << 3,
  281. };
  282. DEF_ENUM_FLAGS_TYPE (test_flag, test_flags);
  283. DEF_ENUM_FLAGS_TYPE (test_uflag, test_uflags);
  284. static void
  285. self_test ()
  286. {
  287. /* Check that default construction works. */
  288. {
  289. constexpr test_flags f;
  290. gdb_static_assert (f == 0);
  291. }
  292. /* Check that assignment from zero works. */
  293. {
  294. test_flags f (FLAG1);
  295. SELF_CHECK (f == FLAG1);
  296. f = 0;
  297. SELF_CHECK (f == 0);
  298. }
  299. /* Check that construction from zero works. */
  300. {
  301. constexpr test_flags zero1 = 0;
  302. constexpr test_flags zero2 (0);
  303. constexpr test_flags zero3 {0};
  304. constexpr test_flags zero4 = {0};
  305. gdb_static_assert (zero1 == 0);
  306. gdb_static_assert (zero2 == 0);
  307. gdb_static_assert (zero3 == 0);
  308. gdb_static_assert (zero4 == 0);
  309. }
  310. /* Check construction from enum value. */
  311. {
  312. gdb_static_assert (test_flags (FLAG1) == FLAG1);
  313. gdb_static_assert (test_flags (FLAG2) != FLAG1);
  314. }
  315. /* Check copy/assignment. */
  316. {
  317. constexpr test_flags src = FLAG1;
  318. constexpr test_flags f1 = src;
  319. constexpr test_flags f2 (src);
  320. constexpr test_flags f3 {src};
  321. constexpr test_flags f4 = {src};
  322. gdb_static_assert (f1 == FLAG1);
  323. gdb_static_assert (f2 == FLAG1);
  324. gdb_static_assert (f3 == FLAG1);
  325. gdb_static_assert (f4 == FLAG1);
  326. }
  327. /* Check moving. */
  328. {
  329. test_flags src = FLAG1;
  330. test_flags dst = 0;
  331. dst = std::move (src);
  332. SELF_CHECK (dst == FLAG1);
  333. }
  334. /* Check construction from an 'or' of multiple bits. For this to
  335. work, operator| must be overridden to return an enum type. The
  336. builtin version would return int instead and then the conversion
  337. to test_flags would fail. */
  338. {
  339. constexpr test_flags f = FLAG1 | FLAG2;
  340. gdb_static_assert (f == (FLAG1 | FLAG2));
  341. }
  342. /* Similarly, check that "FLAG1 | FLAG2" on the rhs of an assignment
  343. operator works. */
  344. {
  345. test_flags f = 0;
  346. f |= FLAG1 | FLAG2;
  347. SELF_CHECK (f == (FLAG1 | FLAG2));
  348. f &= FLAG1 | FLAG2;
  349. SELF_CHECK (f == (FLAG1 | FLAG2));
  350. f ^= FLAG1 | FLAG2;
  351. SELF_CHECK (f == 0);
  352. }
  353. /* Check explicit conversion to int works. */
  354. {
  355. constexpr int some_bits (FLAG1 | FLAG2);
  356. /* And comparison with int works too. */
  357. gdb_static_assert (some_bits == (FLAG1 | FLAG2));
  358. gdb_static_assert (some_bits == test_flags (FLAG1 | FLAG2));
  359. }
  360. /* Check operator| and operator|=. Particularly interesting is
  361. making sure that putting the enum value on the lhs side of the
  362. expression works (FLAG | f). */
  363. {
  364. test_flags f = FLAG1;
  365. f |= FLAG2;
  366. SELF_CHECK (f == (FLAG1 | FLAG2));
  367. }
  368. {
  369. test_flags f = FLAG1;
  370. f = f | FLAG2;
  371. SELF_CHECK (f == (FLAG1 | FLAG2));
  372. }
  373. {
  374. test_flags f = FLAG1;
  375. f = FLAG2 | f;
  376. SELF_CHECK (f == (FLAG1 | FLAG2));
  377. }
  378. /* Check the &/&= operators. */
  379. {
  380. test_flags f = FLAG1 & FLAG2;
  381. SELF_CHECK (f == 0);
  382. f = FLAG1 | FLAG2;
  383. f &= FLAG2;
  384. SELF_CHECK (f == FLAG2);
  385. f = FLAG1 | FLAG2;
  386. f = f & FLAG2;
  387. SELF_CHECK (f == FLAG2);
  388. f = FLAG1 | FLAG2;
  389. f = FLAG2 & f;
  390. SELF_CHECK (f == FLAG2);
  391. }
  392. /* Check the ^/^= operators. */
  393. {
  394. constexpr test_flags f = FLAG1 ^ FLAG2;
  395. gdb_static_assert (f == (FLAG1 ^ FLAG2));
  396. }
  397. {
  398. test_flags f = FLAG1 ^ FLAG2;
  399. f ^= FLAG3;
  400. SELF_CHECK (f == (FLAG1 | FLAG2 | FLAG3));
  401. f = f ^ FLAG3;
  402. SELF_CHECK (f == (FLAG1 | FLAG2));
  403. f = FLAG3 ^ f;
  404. SELF_CHECK (f == (FLAG1 | FLAG2 | FLAG3));
  405. }
  406. /* Check operator~. Note this only compiles with unsigned
  407. flags. */
  408. {
  409. constexpr test_uflags f1 = ~UFLAG1;
  410. constexpr test_uflags f2 = ~f1;
  411. gdb_static_assert (f2 == UFLAG1);
  412. }
  413. /* Check the ternary operator. */
  414. {
  415. /* raw enum, raw enum */
  416. constexpr test_flags f1 = true ? FLAG1 : FLAG2;
  417. gdb_static_assert (f1 == FLAG1);
  418. constexpr test_flags f2 = false ? FLAG1 : FLAG2;
  419. gdb_static_assert (f2 == FLAG2);
  420. }
  421. {
  422. /* enum flags, raw enum */
  423. constexpr test_flags src = FLAG1;
  424. constexpr test_flags f1 = true ? src : FLAG2;
  425. gdb_static_assert (f1 == FLAG1);
  426. constexpr test_flags f2 = false ? src : FLAG2;
  427. gdb_static_assert (f2 == FLAG2);
  428. }
  429. {
  430. /* enum flags, enum flags */
  431. constexpr test_flags src1 = FLAG1;
  432. constexpr test_flags src2 = FLAG2;
  433. constexpr test_flags f1 = true ? src1 : src2;
  434. gdb_static_assert (f1 == src1);
  435. constexpr test_flags f2 = false ? src1 : src2;
  436. gdb_static_assert (f2 == src2);
  437. }
  438. /* Check that we can use flags in switch expressions (requires
  439. unambiguous conversion to integer). Also check that we can use
  440. operator| in switch cases, where only constants are allowed.
  441. This should work because operator| is constexpr. */
  442. {
  443. test_flags f = FLAG1 | FLAG2;
  444. bool ok = false;
  445. switch (f)
  446. {
  447. case FLAG1:
  448. break;
  449. case FLAG2:
  450. break;
  451. case FLAG1 | FLAG2:
  452. ok = true;
  453. break;
  454. }
  455. SELF_CHECK (ok);
  456. }
  457. }
  458. } /* namespace enum_flags_tests */
  459. } /* namespace selftests */
  460. void _initialize_enum_flags_selftests ();
  461. void
  462. _initialize_enum_flags_selftests ()
  463. {
  464. selftests::register_test ("enum-flags",
  465. selftests::enum_flags_tests::self_test);
  466. }