1 //===- UncheckedOptionalAccessModelTest.cpp -------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // FIXME: Move this to clang/unittests/Analysis/FlowSensitive/Models. 9 10 #include "clang/Analysis/FlowSensitive/Models/UncheckedOptionalAccessModel.h" 11 #include "TestingSupport.h" 12 #include "clang/AST/ASTContext.h" 13 #include "clang/ASTMatchers/ASTMatchers.h" 14 #include "clang/Analysis/FlowSensitive/TypeErasedDataflowAnalysis.h" 15 #include "clang/Basic/SourceLocation.h" 16 #include "clang/Tooling/Tooling.h" 17 #include "llvm/ADT/DenseSet.h" 18 #include "llvm/ADT/STLExtras.h" 19 #include "llvm/Support/Error.h" 20 #include "gmock/gmock.h" 21 #include "gtest/gtest.h" 22 #include <string> 23 #include <utility> 24 #include <vector> 25 26 using namespace clang; 27 using namespace dataflow; 28 using namespace test; 29 30 using ::testing::ContainerEq; 31 32 // FIXME: Move header definitions in separate file(s). 33 static constexpr char CSDtdDefHeader[] = R"( 34 #ifndef CSTDDEF_H 35 #define CSTDDEF_H 36 37 namespace std { 38 39 typedef decltype(sizeof(char)) size_t; 40 41 using nullptr_t = decltype(nullptr); 42 43 } // namespace std 44 45 #endif // CSTDDEF_H 46 )"; 47 48 static constexpr char StdTypeTraitsHeader[] = R"( 49 #ifndef STD_TYPE_TRAITS_H 50 #define STD_TYPE_TRAITS_H 51 52 #include "cstddef.h" 53 54 namespace std { 55 56 template <typename T, T V> 57 struct integral_constant { 58 static constexpr T value = V; 59 }; 60 61 using true_type = integral_constant<bool, true>; 62 using false_type = integral_constant<bool, false>; 63 64 template< class T > struct remove_reference {typedef T type;}; 65 template< class T > struct remove_reference<T&> {typedef T type;}; 66 template< class T > struct remove_reference<T&&> {typedef T type;}; 67 68 template <class T> 69 using remove_reference_t = typename remove_reference<T>::type; 70 71 template <class T> 72 struct remove_extent { 73 typedef T type; 74 }; 75 76 template <class T> 77 struct remove_extent<T[]> { 78 typedef T type; 79 }; 80 81 template <class T, size_t N> 82 struct remove_extent<T[N]> { 83 typedef T type; 84 }; 85 86 template <class T> 87 struct is_array : false_type {}; 88 89 template <class T> 90 struct is_array<T[]> : true_type {}; 91 92 template <class T, size_t N> 93 struct is_array<T[N]> : true_type {}; 94 95 template <class> 96 struct is_function : false_type {}; 97 98 template <class Ret, class... Args> 99 struct is_function<Ret(Args...)> : true_type {}; 100 101 namespace detail { 102 103 template <class T> 104 struct type_identity { 105 using type = T; 106 }; // or use type_identity (since C++20) 107 108 template <class T> 109 auto try_add_pointer(int) -> type_identity<typename remove_reference<T>::type*>; 110 template <class T> 111 auto try_add_pointer(...) -> type_identity<T>; 112 113 } // namespace detail 114 115 template <class T> 116 struct add_pointer : decltype(detail::try_add_pointer<T>(0)) {}; 117 118 template <bool B, class T, class F> 119 struct conditional { 120 typedef T type; 121 }; 122 123 template <class T, class F> 124 struct conditional<false, T, F> { 125 typedef F type; 126 }; 127 128 template <class T> 129 struct remove_cv { 130 typedef T type; 131 }; 132 template <class T> 133 struct remove_cv<const T> { 134 typedef T type; 135 }; 136 template <class T> 137 struct remove_cv<volatile T> { 138 typedef T type; 139 }; 140 template <class T> 141 struct remove_cv<const volatile T> { 142 typedef T type; 143 }; 144 145 template <class T> 146 using remove_cv_t = typename remove_cv<T>::type; 147 148 template <class T> 149 struct decay { 150 private: 151 typedef typename remove_reference<T>::type U; 152 153 public: 154 typedef typename conditional< 155 is_array<U>::value, typename remove_extent<U>::type*, 156 typename conditional<is_function<U>::value, typename add_pointer<U>::type, 157 typename remove_cv<U>::type>::type>::type type; 158 }; 159 160 template <bool B, class T = void> 161 struct enable_if {}; 162 163 template <class T> 164 struct enable_if<true, T> { 165 typedef T type; 166 }; 167 168 template <bool B, class T = void> 169 using enable_if_t = typename enable_if<B, T>::type; 170 171 template <class T, class U> 172 struct is_same : false_type {}; 173 174 template <class T> 175 struct is_same<T, T> : true_type {}; 176 177 template <class T> 178 struct is_void : is_same<void, typename remove_cv<T>::type> {}; 179 180 namespace detail { 181 182 template <class T> 183 auto try_add_lvalue_reference(int) -> type_identity<T&>; 184 template <class T> 185 auto try_add_lvalue_reference(...) -> type_identity<T>; 186 187 template <class T> 188 auto try_add_rvalue_reference(int) -> type_identity<T&&>; 189 template <class T> 190 auto try_add_rvalue_reference(...) -> type_identity<T>; 191 192 } // namespace detail 193 194 template <class T> 195 struct add_lvalue_reference : decltype(detail::try_add_lvalue_reference<T>(0)) { 196 }; 197 198 template <class T> 199 struct add_rvalue_reference : decltype(detail::try_add_rvalue_reference<T>(0)) { 200 }; 201 202 template <class T> 203 typename add_rvalue_reference<T>::type declval() noexcept; 204 205 namespace detail { 206 207 template <class T> 208 auto test_returnable(int) 209 -> decltype(void(static_cast<T (*)()>(nullptr)), true_type{}); 210 template <class> 211 auto test_returnable(...) -> false_type; 212 213 template <class From, class To> 214 auto test_implicitly_convertible(int) 215 -> decltype(void(declval<void (&)(To)>()(declval<From>())), true_type{}); 216 template <class, class> 217 auto test_implicitly_convertible(...) -> false_type; 218 219 } // namespace detail 220 221 template <class From, class To> 222 struct is_convertible 223 : integral_constant<bool, 224 (decltype(detail::test_returnable<To>(0))::value && 225 decltype(detail::test_implicitly_convertible<From, To>( 226 0))::value) || 227 (is_void<From>::value && is_void<To>::value)> {}; 228 229 template <class From, class To> 230 inline constexpr bool is_convertible_v = is_convertible<From, To>::value; 231 232 template <class...> 233 using void_t = void; 234 235 template <class, class T, class... Args> 236 struct is_constructible_ : false_type {}; 237 238 template <class T, class... Args> 239 struct is_constructible_<void_t<decltype(T(declval<Args>()...))>, T, Args...> 240 : true_type {}; 241 242 template <class T, class... Args> 243 using is_constructible = is_constructible_<void_t<>, T, Args...>; 244 245 template <class T, class... Args> 246 inline constexpr bool is_constructible_v = is_constructible<T, Args...>::value; 247 248 template <class _Tp> 249 struct __uncvref { 250 typedef typename remove_cv<typename remove_reference<_Tp>::type>::type type; 251 }; 252 253 template <class _Tp> 254 using __uncvref_t = typename __uncvref<_Tp>::type; 255 256 template <bool _Val> 257 using _BoolConstant = integral_constant<bool, _Val>; 258 259 template <class _Tp, class _Up> 260 using _IsSame = _BoolConstant<__is_same(_Tp, _Up)>; 261 262 template <class _Tp, class _Up> 263 using _IsNotSame = _BoolConstant<!__is_same(_Tp, _Up)>; 264 265 template <bool> 266 struct _MetaBase; 267 template <> 268 struct _MetaBase<true> { 269 template <class _Tp, class _Up> 270 using _SelectImpl = _Tp; 271 template <template <class...> class _FirstFn, template <class...> class, 272 class... _Args> 273 using _SelectApplyImpl = _FirstFn<_Args...>; 274 template <class _First, class...> 275 using _FirstImpl = _First; 276 template <class, class _Second, class...> 277 using _SecondImpl = _Second; 278 template <class _Result, class _First, class... _Rest> 279 using _OrImpl = 280 typename _MetaBase<_First::value != true && sizeof...(_Rest) != 0>:: 281 template _OrImpl<_First, _Rest...>; 282 }; 283 284 template <> 285 struct _MetaBase<false> { 286 template <class _Tp, class _Up> 287 using _SelectImpl = _Up; 288 template <template <class...> class, template <class...> class _SecondFn, 289 class... _Args> 290 using _SelectApplyImpl = _SecondFn<_Args...>; 291 template <class _Result, class...> 292 using _OrImpl = _Result; 293 }; 294 295 template <bool _Cond, class _IfRes, class _ElseRes> 296 using _If = typename _MetaBase<_Cond>::template _SelectImpl<_IfRes, _ElseRes>; 297 298 template <class... _Rest> 299 using _Or = typename _MetaBase<sizeof...(_Rest) != 300 0>::template _OrImpl<false_type, _Rest...>; 301 302 template <bool _Bp, class _Tp = void> 303 using __enable_if_t = typename enable_if<_Bp, _Tp>::type; 304 305 template <class...> 306 using __expand_to_true = true_type; 307 template <class... _Pred> 308 __expand_to_true<__enable_if_t<_Pred::value>...> __and_helper(int); 309 template <class...> 310 false_type __and_helper(...); 311 template <class... _Pred> 312 using _And = decltype(__and_helper<_Pred...>(0)); 313 314 template <class _Pred> 315 struct _Not : _BoolConstant<!_Pred::value> {}; 316 317 struct __check_tuple_constructor_fail { 318 static constexpr bool __enable_explicit_default() { return false; } 319 static constexpr bool __enable_implicit_default() { return false; } 320 template <class...> 321 static constexpr bool __enable_explicit() { 322 return false; 323 } 324 template <class...> 325 static constexpr bool __enable_implicit() { 326 return false; 327 } 328 }; 329 330 template <typename, typename _Tp> 331 struct __select_2nd { 332 typedef _Tp type; 333 }; 334 template <class _Tp, class _Arg> 335 typename __select_2nd<decltype((declval<_Tp>() = declval<_Arg>())), 336 true_type>::type 337 __is_assignable_test(int); 338 template <class, class> 339 false_type __is_assignable_test(...); 340 template <class _Tp, class _Arg, 341 bool = is_void<_Tp>::value || is_void<_Arg>::value> 342 struct __is_assignable_imp 343 : public decltype((__is_assignable_test<_Tp, _Arg>(0))) {}; 344 template <class _Tp, class _Arg> 345 struct __is_assignable_imp<_Tp, _Arg, true> : public false_type {}; 346 template <class _Tp, class _Arg> 347 struct is_assignable : public __is_assignable_imp<_Tp, _Arg> {}; 348 349 template <class _Tp> 350 struct __libcpp_is_integral : public false_type {}; 351 template <> 352 struct __libcpp_is_integral<bool> : public true_type {}; 353 template <> 354 struct __libcpp_is_integral<char> : public true_type {}; 355 template <> 356 struct __libcpp_is_integral<signed char> : public true_type {}; 357 template <> 358 struct __libcpp_is_integral<unsigned char> : public true_type {}; 359 template <> 360 struct __libcpp_is_integral<wchar_t> : public true_type {}; 361 template <> 362 struct __libcpp_is_integral<short> : public true_type {}; // NOLINT 363 template <> 364 struct __libcpp_is_integral<unsigned short> : public true_type {}; // NOLINT 365 template <> 366 struct __libcpp_is_integral<int> : public true_type {}; 367 template <> 368 struct __libcpp_is_integral<unsigned int> : public true_type {}; 369 template <> 370 struct __libcpp_is_integral<long> : public true_type {}; // NOLINT 371 template <> 372 struct __libcpp_is_integral<unsigned long> : public true_type {}; // NOLINT 373 template <> 374 struct __libcpp_is_integral<long long> : public true_type {}; // NOLINT 375 template <> // NOLINTNEXTLINE 376 struct __libcpp_is_integral<unsigned long long> : public true_type {}; 377 template <class _Tp> 378 struct is_integral 379 : public __libcpp_is_integral<typename remove_cv<_Tp>::type> {}; 380 381 template <class _Tp> 382 struct __libcpp_is_floating_point : public false_type {}; 383 template <> 384 struct __libcpp_is_floating_point<float> : public true_type {}; 385 template <> 386 struct __libcpp_is_floating_point<double> : public true_type {}; 387 template <> 388 struct __libcpp_is_floating_point<long double> : public true_type {}; 389 template <class _Tp> 390 struct is_floating_point 391 : public __libcpp_is_floating_point<typename remove_cv<_Tp>::type> {}; 392 393 template <class _Tp> 394 struct is_arithmetic 395 : public integral_constant<bool, is_integral<_Tp>::value || 396 is_floating_point<_Tp>::value> {}; 397 398 template <class _Tp> 399 struct __libcpp_is_pointer : public false_type {}; 400 template <class _Tp> 401 struct __libcpp_is_pointer<_Tp*> : public true_type {}; 402 template <class _Tp> 403 struct is_pointer : public __libcpp_is_pointer<typename remove_cv<_Tp>::type> { 404 }; 405 406 template <class _Tp> 407 struct __libcpp_is_member_pointer : public false_type {}; 408 template <class _Tp, class _Up> 409 struct __libcpp_is_member_pointer<_Tp _Up::*> : public true_type {}; 410 template <class _Tp> 411 struct is_member_pointer 412 : public __libcpp_is_member_pointer<typename remove_cv<_Tp>::type> {}; 413 414 template <class _Tp> 415 struct __libcpp_union : public false_type {}; 416 template <class _Tp> 417 struct is_union : public __libcpp_union<typename remove_cv<_Tp>::type> {}; 418 419 template <class T> 420 struct is_reference : false_type {}; 421 template <class T> 422 struct is_reference<T&> : true_type {}; 423 template <class T> 424 struct is_reference<T&&> : true_type {}; 425 426 template <class T> 427 inline constexpr bool is_reference_v = is_reference<T>::value; 428 429 struct __two { 430 char __lx[2]; 431 }; 432 433 namespace __is_class_imp { 434 template <class _Tp> 435 char __test(int _Tp::*); 436 template <class _Tp> 437 __two __test(...); 438 } // namespace __is_class_imp 439 template <class _Tp> 440 struct is_class 441 : public integral_constant<bool, 442 sizeof(__is_class_imp::__test<_Tp>(0)) == 1 && 443 !is_union<_Tp>::value> {}; 444 445 template <class _Tp> 446 struct __is_nullptr_t_impl : public false_type {}; 447 template <> 448 struct __is_nullptr_t_impl<nullptr_t> : public true_type {}; 449 template <class _Tp> 450 struct __is_nullptr_t 451 : public __is_nullptr_t_impl<typename remove_cv<_Tp>::type> {}; 452 template <class _Tp> 453 struct is_null_pointer 454 : public __is_nullptr_t_impl<typename remove_cv<_Tp>::type> {}; 455 456 template <class _Tp> 457 struct is_enum 458 : public integral_constant< 459 bool, !is_void<_Tp>::value && !is_integral<_Tp>::value && 460 !is_floating_point<_Tp>::value && !is_array<_Tp>::value && 461 !is_pointer<_Tp>::value && !is_reference<_Tp>::value && 462 !is_member_pointer<_Tp>::value && !is_union<_Tp>::value && 463 !is_class<_Tp>::value && !is_function<_Tp>::value> {}; 464 465 template <class _Tp> 466 struct is_scalar 467 : public integral_constant< 468 bool, is_arithmetic<_Tp>::value || is_member_pointer<_Tp>::value || 469 is_pointer<_Tp>::value || __is_nullptr_t<_Tp>::value || 470 is_enum<_Tp>::value> {}; 471 template <> 472 struct is_scalar<nullptr_t> : public true_type {}; 473 474 } // namespace std 475 476 #endif // STD_TYPE_TRAITS_H 477 )"; 478 479 static constexpr char AbslTypeTraitsHeader[] = R"( 480 #ifndef ABSL_TYPE_TRAITS_H 481 #define ABSL_TYPE_TRAITS_H 482 483 #include "std_type_traits.h" 484 485 namespace absl { 486 487 template <typename... Ts> 488 struct conjunction : std::true_type {}; 489 490 template <typename T, typename... Ts> 491 struct conjunction<T, Ts...> 492 : std::conditional<T::value, conjunction<Ts...>, T>::type {}; 493 494 template <typename T> 495 struct conjunction<T> : T {}; 496 497 template <typename T> 498 struct negation : std::integral_constant<bool, !T::value> {}; 499 500 template <bool B, typename T = void> 501 using enable_if_t = typename std::enable_if<B, T>::type; 502 503 } // namespace absl 504 505 #endif // ABSL_TYPE_TRAITS_H 506 )"; 507 508 static constexpr char StdStringHeader[] = R"( 509 #ifndef STRING_H 510 #define STRING_H 511 512 namespace std { 513 514 struct string { 515 string(const char*); 516 ~string(); 517 bool empty(); 518 }; 519 bool operator!=(const string &LHS, const char *RHS); 520 521 } // namespace std 522 523 #endif // STRING_H 524 )"; 525 526 static constexpr char StdUtilityHeader[] = R"( 527 #ifndef UTILITY_H 528 #define UTILITY_H 529 530 #include "std_type_traits.h" 531 532 namespace std { 533 534 template <typename T> 535 constexpr remove_reference_t<T>&& move(T&& x); 536 537 template <typename T> 538 void swap(T& a, T& b) noexcept; 539 540 } // namespace std 541 542 #endif // UTILITY_H 543 )"; 544 545 static constexpr char StdInitializerListHeader[] = R"( 546 #ifndef INITIALIZER_LIST_H 547 #define INITIALIZER_LIST_H 548 549 namespace std { 550 551 template <typename T> 552 class initializer_list { 553 public: 554 initializer_list() noexcept; 555 }; 556 557 } // namespace std 558 559 #endif // INITIALIZER_LIST_H 560 )"; 561 562 static constexpr char StdOptionalHeader[] = R"( 563 #include "std_initializer_list.h" 564 #include "std_type_traits.h" 565 #include "std_utility.h" 566 567 namespace std { 568 569 struct in_place_t {}; 570 constexpr in_place_t in_place; 571 572 struct nullopt_t { 573 constexpr explicit nullopt_t() {} 574 }; 575 constexpr nullopt_t nullopt; 576 577 template <class _Tp> 578 struct __optional_destruct_base { 579 constexpr void reset() noexcept; 580 }; 581 582 template <class _Tp> 583 struct __optional_storage_base : __optional_destruct_base<_Tp> { 584 constexpr bool has_value() const noexcept; 585 }; 586 587 template <typename _Tp> 588 class optional : private __optional_storage_base<_Tp> { 589 using __base = __optional_storage_base<_Tp>; 590 591 public: 592 using value_type = _Tp; 593 594 private: 595 struct _CheckOptionalArgsConstructor { 596 template <class _Up> 597 static constexpr bool __enable_implicit() { 598 return is_constructible_v<_Tp, _Up&&> && is_convertible_v<_Up&&, _Tp>; 599 } 600 601 template <class _Up> 602 static constexpr bool __enable_explicit() { 603 return is_constructible_v<_Tp, _Up&&> && !is_convertible_v<_Up&&, _Tp>; 604 } 605 }; 606 template <class _Up> 607 using _CheckOptionalArgsCtor = 608 _If<_IsNotSame<__uncvref_t<_Up>, in_place_t>::value && 609 _IsNotSame<__uncvref_t<_Up>, optional>::value, 610 _CheckOptionalArgsConstructor, __check_tuple_constructor_fail>; 611 template <class _QualUp> 612 struct _CheckOptionalLikeConstructor { 613 template <class _Up, class _Opt = optional<_Up>> 614 using __check_constructible_from_opt = 615 _Or<is_constructible<_Tp, _Opt&>, is_constructible<_Tp, _Opt const&>, 616 is_constructible<_Tp, _Opt&&>, is_constructible<_Tp, _Opt const&&>, 617 is_convertible<_Opt&, _Tp>, is_convertible<_Opt const&, _Tp>, 618 is_convertible<_Opt&&, _Tp>, is_convertible<_Opt const&&, _Tp>>; 619 template <class _Up, class _QUp = _QualUp> 620 static constexpr bool __enable_implicit() { 621 return is_convertible<_QUp, _Tp>::value && 622 !__check_constructible_from_opt<_Up>::value; 623 } 624 template <class _Up, class _QUp = _QualUp> 625 static constexpr bool __enable_explicit() { 626 return !is_convertible<_QUp, _Tp>::value && 627 !__check_constructible_from_opt<_Up>::value; 628 } 629 }; 630 631 template <class _Up, class _QualUp> 632 using _CheckOptionalLikeCtor = 633 _If<_And<_IsNotSame<_Up, _Tp>, is_constructible<_Tp, _QualUp>>::value, 634 _CheckOptionalLikeConstructor<_QualUp>, 635 __check_tuple_constructor_fail>; 636 637 638 template <class _Up, class _QualUp> 639 using _CheckOptionalLikeAssign = _If< 640 _And< 641 _IsNotSame<_Up, _Tp>, 642 is_constructible<_Tp, _QualUp>, 643 is_assignable<_Tp&, _QualUp> 644 >::value, 645 _CheckOptionalLikeConstructor<_QualUp>, 646 __check_tuple_constructor_fail 647 >; 648 649 public: 650 constexpr optional() noexcept {} 651 constexpr optional(const optional&) = default; 652 constexpr optional(optional&&) = default; 653 constexpr optional(nullopt_t) noexcept {} 654 655 template < 656 class _InPlaceT, class... _Args, 657 class = enable_if_t<_And<_IsSame<_InPlaceT, in_place_t>, 658 is_constructible<value_type, _Args...>>::value>> 659 constexpr explicit optional(_InPlaceT, _Args&&... __args); 660 661 template <class _Up, class... _Args, 662 class = enable_if_t<is_constructible_v< 663 value_type, initializer_list<_Up>&, _Args...>>> 664 constexpr explicit optional(in_place_t, initializer_list<_Up> __il, 665 _Args&&... __args); 666 667 template < 668 class _Up = value_type, 669 enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_implicit<_Up>(), 670 int> = 0> 671 constexpr optional(_Up&& __v); 672 673 template < 674 class _Up, 675 enable_if_t<_CheckOptionalArgsCtor<_Up>::template __enable_explicit<_Up>(), 676 int> = 0> 677 constexpr explicit optional(_Up&& __v); 678 679 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>:: 680 template __enable_implicit<_Up>(), 681 int> = 0> 682 constexpr optional(const optional<_Up>& __v); 683 684 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up const&>:: 685 template __enable_explicit<_Up>(), 686 int> = 0> 687 constexpr explicit optional(const optional<_Up>& __v); 688 689 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>:: 690 template __enable_implicit<_Up>(), 691 int> = 0> 692 constexpr optional(optional<_Up>&& __v); 693 694 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>:: 695 template __enable_explicit<_Up>(), 696 int> = 0> 697 constexpr explicit optional(optional<_Up>&& __v); 698 699 constexpr optional& operator=(nullopt_t) noexcept; 700 701 optional& operator=(const optional&); 702 703 optional& operator=(optional&&); 704 705 template <class _Up = value_type, 706 class = enable_if_t<_And<_IsNotSame<__uncvref_t<_Up>, optional>, 707 _Or<_IsNotSame<__uncvref_t<_Up>, value_type>, 708 _Not<is_scalar<value_type>>>, 709 is_constructible<value_type, _Up>, 710 is_assignable<value_type&, _Up>>::value>> 711 constexpr optional& operator=(_Up&& __v); 712 713 template <class _Up, enable_if_t<_CheckOptionalLikeAssign<_Up, _Up const&>:: 714 template __enable_assign<_Up>(), 715 int> = 0> 716 constexpr optional& operator=(const optional<_Up>& __v); 717 718 template <class _Up, enable_if_t<_CheckOptionalLikeCtor<_Up, _Up&&>:: 719 template __enable_assign<_Up>(), 720 int> = 0> 721 constexpr optional& operator=(optional<_Up>&& __v); 722 723 const _Tp& operator*() const&; 724 _Tp& operator*() &; 725 const _Tp&& operator*() const&&; 726 _Tp&& operator*() &&; 727 728 const _Tp* operator->() const; 729 _Tp* operator->(); 730 731 const _Tp& value() const&; 732 _Tp& value() &; 733 const _Tp&& value() const&&; 734 _Tp&& value() &&; 735 736 template <typename U> 737 constexpr _Tp value_or(U&& v) const&; 738 template <typename U> 739 _Tp value_or(U&& v) &&; 740 741 template <typename... Args> 742 _Tp& emplace(Args&&... args); 743 744 template <typename U, typename... Args> 745 _Tp& emplace(std::initializer_list<U> ilist, Args&&... args); 746 747 using __base::reset; 748 749 constexpr explicit operator bool() const noexcept; 750 using __base::has_value; 751 752 constexpr void swap(optional& __opt) noexcept; 753 }; 754 755 template <typename T> 756 constexpr optional<typename std::decay<T>::type> make_optional(T&& v); 757 758 template <typename T, typename... Args> 759 constexpr optional<T> make_optional(Args&&... args); 760 761 template <typename T, typename U, typename... Args> 762 constexpr optional<T> make_optional(std::initializer_list<U> il, 763 Args&&... args); 764 765 } // namespace std 766 )"; 767 768 static constexpr char AbslOptionalHeader[] = R"( 769 #include "absl_type_traits.h" 770 #include "std_initializer_list.h" 771 #include "std_type_traits.h" 772 #include "std_utility.h" 773 774 namespace absl { 775 776 struct nullopt_t { 777 constexpr explicit nullopt_t() {} 778 }; 779 constexpr nullopt_t nullopt; 780 781 struct in_place_t {}; 782 constexpr in_place_t in_place; 783 784 template <typename T> 785 class optional; 786 787 namespace optional_internal { 788 789 template <typename T, typename U> 790 struct is_constructible_convertible_from_optional 791 : std::integral_constant< 792 bool, std::is_constructible<T, optional<U>&>::value || 793 std::is_constructible<T, optional<U>&&>::value || 794 std::is_constructible<T, const optional<U>&>::value || 795 std::is_constructible<T, const optional<U>&&>::value || 796 std::is_convertible<optional<U>&, T>::value || 797 std::is_convertible<optional<U>&&, T>::value || 798 std::is_convertible<const optional<U>&, T>::value || 799 std::is_convertible<const optional<U>&&, T>::value> {}; 800 801 template <typename T, typename U> 802 struct is_constructible_convertible_assignable_from_optional 803 : std::integral_constant< 804 bool, is_constructible_convertible_from_optional<T, U>::value || 805 std::is_assignable<T&, optional<U>&>::value || 806 std::is_assignable<T&, optional<U>&&>::value || 807 std::is_assignable<T&, const optional<U>&>::value || 808 std::is_assignable<T&, const optional<U>&&>::value> {}; 809 810 } // namespace optional_internal 811 812 template <typename T> 813 class optional { 814 public: 815 constexpr optional() noexcept; 816 817 constexpr optional(nullopt_t) noexcept; 818 819 optional(const optional&) = default; 820 821 optional(optional&&) = default; 822 823 template <typename InPlaceT, typename... Args, 824 absl::enable_if_t<absl::conjunction< 825 std::is_same<InPlaceT, in_place_t>, 826 std::is_constructible<T, Args&&...>>::value>* = nullptr> 827 constexpr explicit optional(InPlaceT, Args&&... args); 828 829 template <typename U, typename... Args, 830 typename = typename std::enable_if<std::is_constructible< 831 T, std::initializer_list<U>&, Args&&...>::value>::type> 832 constexpr explicit optional(in_place_t, std::initializer_list<U> il, 833 Args&&... args); 834 835 template < 836 typename U = T, 837 typename std::enable_if< 838 absl::conjunction<absl::negation<std::is_same< 839 in_place_t, typename std::decay<U>::type>>, 840 absl::negation<std::is_same< 841 optional<T>, typename std::decay<U>::type>>, 842 std::is_convertible<U&&, T>, 843 std::is_constructible<T, U&&>>::value, 844 bool>::type = false> 845 constexpr optional(U&& v); 846 847 template < 848 typename U = T, 849 typename std::enable_if< 850 absl::conjunction<absl::negation<std::is_same< 851 in_place_t, typename std::decay<U>::type>>, 852 absl::negation<std::is_same< 853 optional<T>, typename std::decay<U>::type>>, 854 absl::negation<std::is_convertible<U&&, T>>, 855 std::is_constructible<T, U&&>>::value, 856 bool>::type = false> 857 explicit constexpr optional(U&& v); 858 859 template <typename U, 860 typename std::enable_if< 861 absl::conjunction< 862 absl::negation<std::is_same<T, U>>, 863 std::is_constructible<T, const U&>, 864 absl::negation< 865 optional_internal:: 866 is_constructible_convertible_from_optional<T, U>>, 867 std::is_convertible<const U&, T>>::value, 868 bool>::type = false> 869 optional(const optional<U>& rhs); 870 871 template <typename U, 872 typename std::enable_if< 873 absl::conjunction< 874 absl::negation<std::is_same<T, U>>, 875 std::is_constructible<T, const U&>, 876 absl::negation< 877 optional_internal:: 878 is_constructible_convertible_from_optional<T, U>>, 879 absl::negation<std::is_convertible<const U&, T>>>::value, 880 bool>::type = false> 881 explicit optional(const optional<U>& rhs); 882 883 template < 884 typename U, 885 typename std::enable_if< 886 absl::conjunction< 887 absl::negation<std::is_same<T, U>>, std::is_constructible<T, U&&>, 888 absl::negation< 889 optional_internal::is_constructible_convertible_from_optional< 890 T, U>>, 891 std::is_convertible<U&&, T>>::value, 892 bool>::type = false> 893 optional(optional<U>&& rhs); 894 895 template < 896 typename U, 897 typename std::enable_if< 898 absl::conjunction< 899 absl::negation<std::is_same<T, U>>, std::is_constructible<T, U&&>, 900 absl::negation< 901 optional_internal::is_constructible_convertible_from_optional< 902 T, U>>, 903 absl::negation<std::is_convertible<U&&, T>>>::value, 904 bool>::type = false> 905 explicit optional(optional<U>&& rhs); 906 907 optional& operator=(nullopt_t) noexcept; 908 909 optional& operator=(const optional& src); 910 911 optional& operator=(optional&& src); 912 913 template < 914 typename U = T, 915 typename = typename std::enable_if<absl::conjunction< 916 absl::negation< 917 std::is_same<optional<T>, typename std::decay<U>::type>>, 918 absl::negation< 919 absl::conjunction<std::is_scalar<T>, 920 std::is_same<T, typename std::decay<U>::type>>>, 921 std::is_constructible<T, U>, std::is_assignable<T&, U>>::value>::type> 922 optional& operator=(U&& v); 923 924 template < 925 typename U, 926 typename = typename std::enable_if<absl::conjunction< 927 absl::negation<std::is_same<T, U>>, 928 std::is_constructible<T, const U&>, std::is_assignable<T&, const U&>, 929 absl::negation< 930 optional_internal:: 931 is_constructible_convertible_assignable_from_optional< 932 T, U>>>::value>::type> 933 optional& operator=(const optional<U>& rhs); 934 935 template <typename U, 936 typename = typename std::enable_if<absl::conjunction< 937 absl::negation<std::is_same<T, U>>, std::is_constructible<T, U>, 938 std::is_assignable<T&, U>, 939 absl::negation< 940 optional_internal:: 941 is_constructible_convertible_assignable_from_optional< 942 T, U>>>::value>::type> 943 optional& operator=(optional<U>&& rhs); 944 945 const T& operator*() const&; 946 T& operator*() &; 947 const T&& operator*() const&&; 948 T&& operator*() &&; 949 950 const T* operator->() const; 951 T* operator->(); 952 953 const T& value() const&; 954 T& value() &; 955 const T&& value() const&&; 956 T&& value() &&; 957 958 template <typename U> 959 constexpr T value_or(U&& v) const&; 960 template <typename U> 961 T value_or(U&& v) &&; 962 963 template <typename... Args> 964 T& emplace(Args&&... args); 965 966 template <typename U, typename... Args> 967 T& emplace(std::initializer_list<U> ilist, Args&&... args); 968 969 void reset() noexcept; 970 971 constexpr explicit operator bool() const noexcept; 972 constexpr bool has_value() const noexcept; 973 974 void swap(optional& rhs) noexcept; 975 }; 976 977 template <typename T> 978 constexpr optional<typename std::decay<T>::type> make_optional(T&& v); 979 980 template <typename T, typename... Args> 981 constexpr optional<T> make_optional(Args&&... args); 982 983 template <typename T, typename U, typename... Args> 984 constexpr optional<T> make_optional(std::initializer_list<U> il, 985 Args&&... args); 986 987 } // namespace absl 988 )"; 989 990 static constexpr char BaseOptionalHeader[] = R"( 991 #include "std_initializer_list.h" 992 #include "std_type_traits.h" 993 #include "std_utility.h" 994 995 namespace base { 996 997 struct in_place_t {}; 998 constexpr in_place_t in_place; 999 1000 struct nullopt_t { 1001 constexpr explicit nullopt_t() {} 1002 }; 1003 constexpr nullopt_t nullopt; 1004 1005 template <typename T> 1006 class Optional; 1007 1008 namespace internal { 1009 1010 template <typename T> 1011 using RemoveCvRefT = std::remove_cv_t<std::remove_reference_t<T>>; 1012 1013 template <typename T, typename U> 1014 struct IsConvertibleFromOptional 1015 : std::integral_constant< 1016 bool, std::is_constructible<T, Optional<U>&>::value || 1017 std::is_constructible<T, const Optional<U>&>::value || 1018 std::is_constructible<T, Optional<U>&&>::value || 1019 std::is_constructible<T, const Optional<U>&&>::value || 1020 std::is_convertible<Optional<U>&, T>::value || 1021 std::is_convertible<const Optional<U>&, T>::value || 1022 std::is_convertible<Optional<U>&&, T>::value || 1023 std::is_convertible<const Optional<U>&&, T>::value> {}; 1024 1025 template <typename T, typename U> 1026 struct IsAssignableFromOptional 1027 : std::integral_constant< 1028 bool, IsConvertibleFromOptional<T, U>::value || 1029 std::is_assignable<T&, Optional<U>&>::value || 1030 std::is_assignable<T&, const Optional<U>&>::value || 1031 std::is_assignable<T&, Optional<U>&&>::value || 1032 std::is_assignable<T&, const Optional<U>&&>::value> {}; 1033 1034 } // namespace internal 1035 1036 template <typename T> 1037 class Optional { 1038 public: 1039 using value_type = T; 1040 1041 constexpr Optional() = default; 1042 constexpr Optional(const Optional& other) noexcept = default; 1043 constexpr Optional(Optional&& other) noexcept = default; 1044 1045 constexpr Optional(nullopt_t); 1046 1047 template <typename U, 1048 typename std::enable_if< 1049 std::is_constructible<T, const U&>::value && 1050 !internal::IsConvertibleFromOptional<T, U>::value && 1051 std::is_convertible<const U&, T>::value, 1052 bool>::type = false> 1053 Optional(const Optional<U>& other) noexcept; 1054 1055 template <typename U, 1056 typename std::enable_if< 1057 std::is_constructible<T, const U&>::value && 1058 !internal::IsConvertibleFromOptional<T, U>::value && 1059 !std::is_convertible<const U&, T>::value, 1060 bool>::type = false> 1061 explicit Optional(const Optional<U>& other) noexcept; 1062 1063 template <typename U, 1064 typename std::enable_if< 1065 std::is_constructible<T, U&&>::value && 1066 !internal::IsConvertibleFromOptional<T, U>::value && 1067 std::is_convertible<U&&, T>::value, 1068 bool>::type = false> 1069 Optional(Optional<U>&& other) noexcept; 1070 1071 template <typename U, 1072 typename std::enable_if< 1073 std::is_constructible<T, U&&>::value && 1074 !internal::IsConvertibleFromOptional<T, U>::value && 1075 !std::is_convertible<U&&, T>::value, 1076 bool>::type = false> 1077 explicit Optional(Optional<U>&& other) noexcept; 1078 1079 template <class... Args> 1080 constexpr explicit Optional(in_place_t, Args&&... args); 1081 1082 template <class U, class... Args, 1083 class = typename std::enable_if<std::is_constructible< 1084 value_type, std::initializer_list<U>&, Args...>::value>::type> 1085 constexpr explicit Optional(in_place_t, std::initializer_list<U> il, 1086 Args&&... args); 1087 1088 template < 1089 typename U = value_type, 1090 typename std::enable_if< 1091 std::is_constructible<T, U&&>::value && 1092 !std::is_same<internal::RemoveCvRefT<U>, in_place_t>::value && 1093 !std::is_same<internal::RemoveCvRefT<U>, Optional<T>>::value && 1094 std::is_convertible<U&&, T>::value, 1095 bool>::type = false> 1096 constexpr Optional(U&& value); 1097 1098 template < 1099 typename U = value_type, 1100 typename std::enable_if< 1101 std::is_constructible<T, U&&>::value && 1102 !std::is_same<internal::RemoveCvRefT<U>, in_place_t>::value && 1103 !std::is_same<internal::RemoveCvRefT<U>, Optional<T>>::value && 1104 !std::is_convertible<U&&, T>::value, 1105 bool>::type = false> 1106 constexpr explicit Optional(U&& value); 1107 1108 Optional& operator=(const Optional& other) noexcept; 1109 1110 Optional& operator=(Optional&& other) noexcept; 1111 1112 Optional& operator=(nullopt_t); 1113 1114 template <typename U> 1115 typename std::enable_if< 1116 !std::is_same<internal::RemoveCvRefT<U>, Optional<T>>::value && 1117 std::is_constructible<T, U>::value && 1118 std::is_assignable<T&, U>::value && 1119 (!std::is_scalar<T>::value || 1120 !std::is_same<typename std::decay<U>::type, T>::value), 1121 Optional&>::type 1122 operator=(U&& value) noexcept; 1123 1124 template <typename U> 1125 typename std::enable_if<!internal::IsAssignableFromOptional<T, U>::value && 1126 std::is_constructible<T, const U&>::value && 1127 std::is_assignable<T&, const U&>::value, 1128 Optional&>::type 1129 operator=(const Optional<U>& other) noexcept; 1130 1131 template <typename U> 1132 typename std::enable_if<!internal::IsAssignableFromOptional<T, U>::value && 1133 std::is_constructible<T, U>::value && 1134 std::is_assignable<T&, U>::value, 1135 Optional&>::type 1136 operator=(Optional<U>&& other) noexcept; 1137 1138 const T& operator*() const&; 1139 T& operator*() &; 1140 const T&& operator*() const&&; 1141 T&& operator*() &&; 1142 1143 const T* operator->() const; 1144 T* operator->(); 1145 1146 const T& value() const&; 1147 T& value() &; 1148 const T&& value() const&&; 1149 T&& value() &&; 1150 1151 template <typename U> 1152 constexpr T value_or(U&& v) const&; 1153 template <typename U> 1154 T value_or(U&& v) &&; 1155 1156 template <typename... Args> 1157 T& emplace(Args&&... args); 1158 1159 template <typename U, typename... Args> 1160 T& emplace(std::initializer_list<U> ilist, Args&&... args); 1161 1162 void reset() noexcept; 1163 1164 constexpr explicit operator bool() const noexcept; 1165 constexpr bool has_value() const noexcept; 1166 1167 void swap(Optional& other); 1168 }; 1169 1170 template <typename T> 1171 constexpr Optional<typename std::decay<T>::type> make_optional(T&& v); 1172 1173 template <typename T, typename... Args> 1174 constexpr Optional<T> make_optional(Args&&... args); 1175 1176 template <typename T, typename U, typename... Args> 1177 constexpr Optional<T> make_optional(std::initializer_list<U> il, 1178 Args&&... args); 1179 1180 } // namespace base 1181 )"; 1182 1183 /// Replaces all occurrences of `Pattern` in `S` with `Replacement`. 1184 static void ReplaceAllOccurrences(std::string &S, const std::string &Pattern, 1185 const std::string &Replacement) { 1186 size_t Pos = 0; 1187 while (true) { 1188 Pos = S.find(Pattern, Pos); 1189 if (Pos == std::string::npos) 1190 break; 1191 S.replace(Pos, Pattern.size(), Replacement); 1192 } 1193 } 1194 1195 struct OptionalTypeIdentifier { 1196 std::string NamespaceName; 1197 std::string TypeName; 1198 }; 1199 1200 class UncheckedOptionalAccessTest 1201 : public ::testing::TestWithParam<OptionalTypeIdentifier> { 1202 protected: 1203 void ExpectDiagnosticsFor(std::string SourceCode) { 1204 ExpectDiagnosticsFor(SourceCode, ast_matchers::hasName("target")); 1205 } 1206 1207 private: 1208 template <typename FuncDeclMatcher> 1209 void ExpectDiagnosticsFor(std::string SourceCode, 1210 FuncDeclMatcher FuncMatcher) { 1211 ReplaceAllOccurrences(SourceCode, "$ns", GetParam().NamespaceName); 1212 ReplaceAllOccurrences(SourceCode, "$optional", GetParam().TypeName); 1213 1214 std::vector<std::pair<std::string, std::string>> Headers; 1215 Headers.emplace_back("cstddef.h", CSDtdDefHeader); 1216 Headers.emplace_back("std_initializer_list.h", StdInitializerListHeader); 1217 Headers.emplace_back("std_string.h", StdStringHeader); 1218 Headers.emplace_back("std_type_traits.h", StdTypeTraitsHeader); 1219 Headers.emplace_back("std_utility.h", StdUtilityHeader); 1220 Headers.emplace_back("std_optional.h", StdOptionalHeader); 1221 Headers.emplace_back("absl_type_traits.h", AbslTypeTraitsHeader); 1222 Headers.emplace_back("absl_optional.h", AbslOptionalHeader); 1223 Headers.emplace_back("base_optional.h", BaseOptionalHeader); 1224 Headers.emplace_back("unchecked_optional_access_test.h", R"( 1225 #include "absl_optional.h" 1226 #include "base_optional.h" 1227 #include "std_initializer_list.h" 1228 #include "std_optional.h" 1229 #include "std_string.h" 1230 #include "std_utility.h" 1231 1232 template <typename T> 1233 T Make(); 1234 )"); 1235 UncheckedOptionalAccessModelOptions Options{ 1236 /*IgnoreSmartPointerDereference=*/true}; 1237 std::vector<SourceLocation> Diagnostics; 1238 llvm::Error Error = checkDataflow<UncheckedOptionalAccessModel>( 1239 AnalysisInputs<UncheckedOptionalAccessModel>( 1240 SourceCode, std::move(FuncMatcher), 1241 [Options](ASTContext &Ctx, Environment &) { 1242 return UncheckedOptionalAccessModel(Ctx, Options); 1243 }) 1244 .withPostVisitCFG( 1245 [&Diagnostics, 1246 Diagnoser = UncheckedOptionalAccessDiagnoser(Options)]( 1247 ASTContext &Ctx, const CFGElement &Elt, 1248 const TypeErasedDataflowAnalysisState &State) mutable { 1249 auto EltDiagnostics = 1250 Diagnoser.diagnose(Ctx, &Elt, State.Env); 1251 llvm::move(EltDiagnostics, std::back_inserter(Diagnostics)); 1252 }) 1253 .withASTBuildArgs( 1254 {"-fsyntax-only", "-std=c++17", "-Wno-undefined-inline"}) 1255 .withASTBuildVirtualMappedFiles( 1256 tooling::FileContentMappings(Headers.begin(), Headers.end())), 1257 /*VerifyResults=*/[&Diagnostics]( 1258 const llvm::DenseMap<unsigned, std::string> 1259 &Annotations, 1260 const AnalysisOutputs &AO) { 1261 llvm::DenseSet<unsigned> AnnotationLines; 1262 for (const auto &[Line, _] : Annotations) { 1263 AnnotationLines.insert(Line); 1264 } 1265 auto &SrcMgr = AO.ASTCtx.getSourceManager(); 1266 llvm::DenseSet<unsigned> DiagnosticLines; 1267 for (SourceLocation &Loc : Diagnostics) { 1268 DiagnosticLines.insert(SrcMgr.getPresumedLineNumber(Loc)); 1269 } 1270 1271 EXPECT_THAT(DiagnosticLines, ContainerEq(AnnotationLines)); 1272 }); 1273 if (Error) 1274 FAIL() << llvm::toString(std::move(Error)); 1275 } 1276 }; 1277 1278 INSTANTIATE_TEST_SUITE_P( 1279 UncheckedOptionalUseTestInst, UncheckedOptionalAccessTest, 1280 ::testing::Values(OptionalTypeIdentifier{"std", "optional"}, 1281 OptionalTypeIdentifier{"absl", "optional"}, 1282 OptionalTypeIdentifier{"base", "Optional"}), 1283 [](const ::testing::TestParamInfo<OptionalTypeIdentifier> &Info) { 1284 return Info.param.NamespaceName; 1285 }); 1286 1287 TEST_P(UncheckedOptionalAccessTest, EmptyFunctionBody) { 1288 ExpectDiagnosticsFor(R"( 1289 void target() { 1290 (void)0; 1291 } 1292 )"); 1293 } 1294 1295 TEST_P(UncheckedOptionalAccessTest, UnwrapUsingValueNoCheck) { 1296 ExpectDiagnosticsFor( 1297 R"( 1298 #include "unchecked_optional_access_test.h" 1299 1300 void target($ns::$optional<int> opt) { 1301 opt.value(); // [[unsafe]] 1302 } 1303 )"); 1304 1305 ExpectDiagnosticsFor( 1306 R"( 1307 #include "unchecked_optional_access_test.h" 1308 1309 void target($ns::$optional<int> opt) { 1310 std::move(opt).value(); // [[unsafe]] 1311 } 1312 )"); 1313 } 1314 1315 TEST_P(UncheckedOptionalAccessTest, UnwrapUsingOperatorStarNoCheck) { 1316 ExpectDiagnosticsFor( 1317 R"( 1318 #include "unchecked_optional_access_test.h" 1319 1320 void target($ns::$optional<int> opt) { 1321 *opt; // [[unsafe]] 1322 } 1323 )"); 1324 1325 ExpectDiagnosticsFor( 1326 R"( 1327 #include "unchecked_optional_access_test.h" 1328 1329 void target($ns::$optional<int> opt) { 1330 *std::move(opt); // [[unsafe]] 1331 } 1332 )"); 1333 } 1334 1335 TEST_P(UncheckedOptionalAccessTest, UnwrapUsingOperatorArrowNoCheck) { 1336 ExpectDiagnosticsFor( 1337 R"( 1338 #include "unchecked_optional_access_test.h" 1339 1340 struct Foo { 1341 void foo(); 1342 }; 1343 1344 void target($ns::$optional<Foo> opt) { 1345 opt->foo(); // [[unsafe]] 1346 } 1347 )"); 1348 1349 ExpectDiagnosticsFor( 1350 R"( 1351 #include "unchecked_optional_access_test.h" 1352 1353 struct Foo { 1354 void foo(); 1355 }; 1356 1357 void target($ns::$optional<Foo> opt) { 1358 std::move(opt)->foo(); // [[unsafe]] 1359 } 1360 )"); 1361 } 1362 1363 TEST_P(UncheckedOptionalAccessTest, HasValueCheck) { 1364 ExpectDiagnosticsFor(R"( 1365 #include "unchecked_optional_access_test.h" 1366 1367 void target($ns::$optional<int> opt) { 1368 if (opt.has_value()) { 1369 opt.value(); 1370 } 1371 } 1372 )"); 1373 } 1374 1375 TEST_P(UncheckedOptionalAccessTest, OperatorBoolCheck) { 1376 ExpectDiagnosticsFor(R"( 1377 #include "unchecked_optional_access_test.h" 1378 1379 void target($ns::$optional<int> opt) { 1380 if (opt) { 1381 opt.value(); 1382 } 1383 } 1384 )"); 1385 } 1386 1387 TEST_P(UncheckedOptionalAccessTest, UnwrapFunctionCallResultNoCheck) { 1388 ExpectDiagnosticsFor( 1389 R"( 1390 #include "unchecked_optional_access_test.h" 1391 1392 void target() { 1393 Make<$ns::$optional<int>>().value(); // [[unsafe]] 1394 (void)0; 1395 } 1396 )"); 1397 1398 ExpectDiagnosticsFor( 1399 R"( 1400 #include "unchecked_optional_access_test.h" 1401 1402 void target($ns::$optional<int> opt) { 1403 std::move(opt).value(); // [[unsafe]] 1404 } 1405 )"); 1406 } 1407 1408 TEST_P(UncheckedOptionalAccessTest, DefaultConstructor) { 1409 ExpectDiagnosticsFor( 1410 R"( 1411 #include "unchecked_optional_access_test.h" 1412 1413 void target() { 1414 $ns::$optional<int> opt; 1415 opt.value(); // [[unsafe]] 1416 } 1417 )"); 1418 } 1419 1420 TEST_P(UncheckedOptionalAccessTest, NulloptConstructor) { 1421 ExpectDiagnosticsFor( 1422 R"( 1423 #include "unchecked_optional_access_test.h" 1424 1425 void target() { 1426 $ns::$optional<int> opt($ns::nullopt); 1427 opt.value(); // [[unsafe]] 1428 } 1429 )"); 1430 } 1431 1432 TEST_P(UncheckedOptionalAccessTest, InPlaceConstructor) { 1433 ExpectDiagnosticsFor(R"( 1434 #include "unchecked_optional_access_test.h" 1435 1436 void target() { 1437 $ns::$optional<int> opt($ns::in_place, 3); 1438 opt.value(); 1439 } 1440 )"); 1441 1442 ExpectDiagnosticsFor(R"( 1443 #include "unchecked_optional_access_test.h" 1444 1445 struct Foo {}; 1446 1447 void target() { 1448 $ns::$optional<Foo> opt($ns::in_place); 1449 opt.value(); 1450 } 1451 )"); 1452 1453 ExpectDiagnosticsFor(R"( 1454 #include "unchecked_optional_access_test.h" 1455 1456 struct Foo { 1457 explicit Foo(int, bool); 1458 }; 1459 1460 void target() { 1461 $ns::$optional<Foo> opt($ns::in_place, 3, false); 1462 opt.value(); 1463 } 1464 )"); 1465 1466 ExpectDiagnosticsFor(R"( 1467 #include "unchecked_optional_access_test.h" 1468 1469 struct Foo { 1470 explicit Foo(std::initializer_list<int>); 1471 }; 1472 1473 void target() { 1474 $ns::$optional<Foo> opt($ns::in_place, {3}); 1475 opt.value(); 1476 } 1477 )"); 1478 } 1479 1480 TEST_P(UncheckedOptionalAccessTest, ValueConstructor) { 1481 ExpectDiagnosticsFor(R"( 1482 #include "unchecked_optional_access_test.h" 1483 1484 void target() { 1485 $ns::$optional<int> opt(21); 1486 opt.value(); 1487 } 1488 )"); 1489 1490 ExpectDiagnosticsFor(R"( 1491 #include "unchecked_optional_access_test.h" 1492 1493 void target() { 1494 $ns::$optional<int> opt = $ns::$optional<int>(21); 1495 opt.value(); 1496 } 1497 )"); 1498 ExpectDiagnosticsFor(R"( 1499 #include "unchecked_optional_access_test.h" 1500 1501 void target() { 1502 $ns::$optional<$ns::$optional<int>> opt(Make<$ns::$optional<int>>()); 1503 opt.value(); 1504 } 1505 )"); 1506 1507 ExpectDiagnosticsFor(R"( 1508 #include "unchecked_optional_access_test.h" 1509 1510 struct MyString { 1511 MyString(const char*); 1512 }; 1513 1514 void target() { 1515 $ns::$optional<MyString> opt("foo"); 1516 opt.value(); 1517 } 1518 )"); 1519 1520 ExpectDiagnosticsFor(R"( 1521 #include "unchecked_optional_access_test.h" 1522 1523 struct Foo {}; 1524 1525 struct Bar { 1526 Bar(const Foo&); 1527 }; 1528 1529 void target() { 1530 $ns::$optional<Bar> opt(Make<Foo>()); 1531 opt.value(); 1532 } 1533 )"); 1534 1535 ExpectDiagnosticsFor(R"( 1536 #include "unchecked_optional_access_test.h" 1537 1538 struct Foo { 1539 explicit Foo(int); 1540 }; 1541 1542 void target() { 1543 $ns::$optional<Foo> opt(3); 1544 opt.value(); 1545 } 1546 )"); 1547 } 1548 1549 TEST_P(UncheckedOptionalAccessTest, ConvertibleOptionalConstructor) { 1550 ExpectDiagnosticsFor( 1551 R"( 1552 #include "unchecked_optional_access_test.h" 1553 1554 struct Foo {}; 1555 1556 struct Bar { 1557 Bar(const Foo&); 1558 }; 1559 1560 void target() { 1561 $ns::$optional<Bar> opt(Make<$ns::$optional<Foo>>()); 1562 opt.value(); // [[unsafe]] 1563 } 1564 )"); 1565 1566 ExpectDiagnosticsFor( 1567 R"( 1568 #include "unchecked_optional_access_test.h" 1569 1570 struct Foo {}; 1571 1572 struct Bar { 1573 explicit Bar(const Foo&); 1574 }; 1575 1576 void target() { 1577 $ns::$optional<Bar> opt(Make<$ns::$optional<Foo>>()); 1578 opt.value(); // [[unsafe]] 1579 } 1580 )"); 1581 1582 ExpectDiagnosticsFor( 1583 R"( 1584 #include "unchecked_optional_access_test.h" 1585 1586 struct Foo {}; 1587 1588 struct Bar { 1589 Bar(const Foo&); 1590 }; 1591 1592 void target() { 1593 $ns::$optional<Foo> opt1 = $ns::nullopt; 1594 $ns::$optional<Bar> opt2(opt1); 1595 opt2.value(); // [[unsafe]] 1596 } 1597 )"); 1598 1599 ExpectDiagnosticsFor(R"( 1600 #include "unchecked_optional_access_test.h" 1601 1602 struct Foo {}; 1603 1604 struct Bar { 1605 Bar(const Foo&); 1606 }; 1607 1608 void target() { 1609 $ns::$optional<Foo> opt1(Make<Foo>()); 1610 $ns::$optional<Bar> opt2(opt1); 1611 opt2.value(); 1612 } 1613 )"); 1614 1615 ExpectDiagnosticsFor(R"( 1616 #include "unchecked_optional_access_test.h" 1617 1618 struct Foo {}; 1619 1620 struct Bar { 1621 explicit Bar(const Foo&); 1622 }; 1623 1624 void target() { 1625 $ns::$optional<Foo> opt1(Make<Foo>()); 1626 $ns::$optional<Bar> opt2(opt1); 1627 opt2.value(); 1628 } 1629 )"); 1630 } 1631 1632 TEST_P(UncheckedOptionalAccessTest, MakeOptional) { 1633 ExpectDiagnosticsFor(R"( 1634 #include "unchecked_optional_access_test.h" 1635 1636 void target() { 1637 $ns::$optional<int> opt = $ns::make_optional(0); 1638 opt.value(); 1639 } 1640 )"); 1641 1642 ExpectDiagnosticsFor(R"( 1643 #include "unchecked_optional_access_test.h" 1644 1645 struct Foo { 1646 Foo(int, int); 1647 }; 1648 1649 void target() { 1650 $ns::$optional<Foo> opt = $ns::make_optional<Foo>(21, 22); 1651 opt.value(); 1652 } 1653 )"); 1654 1655 ExpectDiagnosticsFor(R"( 1656 #include "unchecked_optional_access_test.h" 1657 1658 struct Foo { 1659 constexpr Foo(std::initializer_list<char>); 1660 }; 1661 1662 void target() { 1663 char a = 'a'; 1664 $ns::$optional<Foo> opt = $ns::make_optional<Foo>({a}); 1665 opt.value(); 1666 } 1667 )"); 1668 } 1669 1670 TEST_P(UncheckedOptionalAccessTest, ValueOr) { 1671 ExpectDiagnosticsFor(R"( 1672 #include "unchecked_optional_access_test.h" 1673 1674 void target() { 1675 $ns::$optional<int> opt; 1676 opt.value_or(0); 1677 (void)0; 1678 } 1679 )"); 1680 } 1681 1682 TEST_P(UncheckedOptionalAccessTest, ValueOrComparison) { 1683 // Pointers. 1684 ExpectDiagnosticsFor( 1685 R"code( 1686 #include "unchecked_optional_access_test.h" 1687 1688 void target($ns::$optional<int*> opt) { 1689 if (opt.value_or(nullptr) != nullptr) { 1690 opt.value(); 1691 } else { 1692 opt.value(); // [[unsafe]] 1693 } 1694 } 1695 )code"); 1696 1697 // Integers. 1698 ExpectDiagnosticsFor( 1699 R"code( 1700 #include "unchecked_optional_access_test.h" 1701 1702 void target($ns::$optional<int> opt) { 1703 if (opt.value_or(0) != 0) { 1704 opt.value(); 1705 } else { 1706 opt.value(); // [[unsafe]] 1707 } 1708 } 1709 )code"); 1710 1711 // Strings. 1712 ExpectDiagnosticsFor( 1713 R"code( 1714 #include "unchecked_optional_access_test.h" 1715 1716 void target($ns::$optional<std::string> opt) { 1717 if (!opt.value_or("").empty()) { 1718 opt.value(); 1719 } else { 1720 opt.value(); // [[unsafe]] 1721 } 1722 } 1723 )code"); 1724 1725 ExpectDiagnosticsFor( 1726 R"code( 1727 #include "unchecked_optional_access_test.h" 1728 1729 void target($ns::$optional<std::string> opt) { 1730 if (opt.value_or("") != "") { 1731 opt.value(); 1732 } else { 1733 opt.value(); // [[unsafe]] 1734 } 1735 } 1736 )code"); 1737 1738 // Pointer-to-optional. 1739 // 1740 // FIXME: make `opt` a parameter directly, once we ensure that all `optional` 1741 // values have a `has_value` property. 1742 ExpectDiagnosticsFor( 1743 R"code( 1744 #include "unchecked_optional_access_test.h" 1745 1746 void target($ns::$optional<int> p) { 1747 $ns::$optional<int> *opt = &p; 1748 if (opt->value_or(0) != 0) { 1749 opt->value(); 1750 } else { 1751 opt->value(); // [[unsafe]] 1752 } 1753 } 1754 )code"); 1755 } 1756 1757 TEST_P(UncheckedOptionalAccessTest, Emplace) { 1758 ExpectDiagnosticsFor(R"( 1759 #include "unchecked_optional_access_test.h" 1760 1761 void target() { 1762 $ns::$optional<int> opt; 1763 opt.emplace(0); 1764 opt.value(); 1765 } 1766 )"); 1767 1768 ExpectDiagnosticsFor(R"( 1769 #include "unchecked_optional_access_test.h" 1770 1771 void target($ns::$optional<int> *opt) { 1772 opt->emplace(0); 1773 opt->value(); 1774 } 1775 )"); 1776 1777 // FIXME: Add tests that call `emplace` in conditional branches: 1778 // ExpectDiagnosticsFor( 1779 // R"( 1780 // #include "unchecked_optional_access_test.h" 1781 // 1782 // void target($ns::$optional<int> opt, bool b) { 1783 // if (b) { 1784 // opt.emplace(0); 1785 // } 1786 // if (b) { 1787 // opt.value(); 1788 // } else { 1789 // opt.value(); // [[unsafe]] 1790 // } 1791 // } 1792 // )"); 1793 } 1794 1795 TEST_P(UncheckedOptionalAccessTest, Reset) { 1796 ExpectDiagnosticsFor( 1797 R"( 1798 #include "unchecked_optional_access_test.h" 1799 1800 void target() { 1801 $ns::$optional<int> opt = $ns::make_optional(0); 1802 opt.reset(); 1803 opt.value(); // [[unsafe]] 1804 } 1805 )"); 1806 1807 ExpectDiagnosticsFor( 1808 R"( 1809 #include "unchecked_optional_access_test.h" 1810 1811 void target($ns::$optional<int> &opt) { 1812 if (opt.has_value()) { 1813 opt.reset(); 1814 opt.value(); // [[unsafe]] 1815 } 1816 } 1817 )"); 1818 1819 // FIXME: Add tests that call `reset` in conditional branches: 1820 // ExpectDiagnosticsFor( 1821 // R"( 1822 // #include "unchecked_optional_access_test.h" 1823 // 1824 // void target(bool b) { 1825 // $ns::$optional<int> opt = $ns::make_optional(0); 1826 // if (b) { 1827 // opt.reset(); 1828 // } 1829 // if (b) { 1830 // opt.value(); // [[unsafe]] 1831 // } else { 1832 // opt.value(); 1833 // } 1834 // } 1835 // )"); 1836 } 1837 1838 TEST_P(UncheckedOptionalAccessTest, ValueAssignment) { 1839 ExpectDiagnosticsFor(R"( 1840 #include "unchecked_optional_access_test.h" 1841 1842 struct Foo {}; 1843 1844 void target() { 1845 $ns::$optional<Foo> opt; 1846 opt = Foo(); 1847 opt.value(); 1848 } 1849 )"); 1850 1851 ExpectDiagnosticsFor(R"( 1852 #include "unchecked_optional_access_test.h" 1853 1854 struct Foo {}; 1855 1856 void target() { 1857 $ns::$optional<Foo> opt; 1858 (opt = Foo()).value(); 1859 (void)0; 1860 } 1861 )"); 1862 1863 ExpectDiagnosticsFor(R"( 1864 #include "unchecked_optional_access_test.h" 1865 1866 struct MyString { 1867 MyString(const char*); 1868 }; 1869 1870 void target() { 1871 $ns::$optional<MyString> opt; 1872 opt = "foo"; 1873 opt.value(); 1874 } 1875 )"); 1876 1877 ExpectDiagnosticsFor(R"( 1878 #include "unchecked_optional_access_test.h" 1879 1880 struct MyString { 1881 MyString(const char*); 1882 }; 1883 1884 void target() { 1885 $ns::$optional<MyString> opt; 1886 (opt = "foo").value(); 1887 } 1888 )"); 1889 } 1890 1891 TEST_P(UncheckedOptionalAccessTest, OptionalConversionAssignment) { 1892 ExpectDiagnosticsFor( 1893 R"( 1894 #include "unchecked_optional_access_test.h" 1895 1896 struct Foo {}; 1897 1898 struct Bar { 1899 Bar(const Foo&); 1900 }; 1901 1902 void target() { 1903 $ns::$optional<Foo> opt1 = Foo(); 1904 $ns::$optional<Bar> opt2; 1905 opt2 = opt1; 1906 opt2.value(); 1907 } 1908 )"); 1909 1910 ExpectDiagnosticsFor( 1911 R"( 1912 #include "unchecked_optional_access_test.h" 1913 1914 struct Foo {}; 1915 1916 struct Bar { 1917 Bar(const Foo&); 1918 }; 1919 1920 void target() { 1921 $ns::$optional<Foo> opt1; 1922 $ns::$optional<Bar> opt2; 1923 if (opt2.has_value()) { 1924 opt2 = opt1; 1925 opt2.value(); // [[unsafe]] 1926 } 1927 } 1928 )"); 1929 1930 ExpectDiagnosticsFor( 1931 R"( 1932 #include "unchecked_optional_access_test.h" 1933 1934 struct Foo {}; 1935 1936 struct Bar { 1937 Bar(const Foo&); 1938 }; 1939 1940 void target() { 1941 $ns::$optional<Foo> opt1 = Foo(); 1942 $ns::$optional<Bar> opt2; 1943 (opt2 = opt1).value(); 1944 (void)0; 1945 } 1946 )"); 1947 } 1948 1949 TEST_P(UncheckedOptionalAccessTest, NulloptAssignment) { 1950 ExpectDiagnosticsFor( 1951 R"( 1952 #include "unchecked_optional_access_test.h" 1953 1954 void target() { 1955 $ns::$optional<int> opt = 3; 1956 opt = $ns::nullopt; 1957 opt.value(); // [[unsafe]] 1958 } 1959 )"); 1960 1961 ExpectDiagnosticsFor( 1962 R"( 1963 #include "unchecked_optional_access_test.h" 1964 1965 void target() { 1966 $ns::$optional<int> opt = 3; 1967 (opt = $ns::nullopt).value(); // [[unsafe]] 1968 } 1969 )"); 1970 } 1971 1972 TEST_P(UncheckedOptionalAccessTest, OptionalSwap) { 1973 ExpectDiagnosticsFor( 1974 R"( 1975 #include "unchecked_optional_access_test.h" 1976 1977 void target() { 1978 $ns::$optional<int> opt1 = $ns::nullopt; 1979 $ns::$optional<int> opt2 = 3; 1980 1981 opt1.swap(opt2); 1982 1983 opt1.value(); 1984 1985 opt2.value(); // [[unsafe]] 1986 } 1987 )"); 1988 1989 ExpectDiagnosticsFor( 1990 R"( 1991 #include "unchecked_optional_access_test.h" 1992 1993 void target() { 1994 $ns::$optional<int> opt1 = $ns::nullopt; 1995 $ns::$optional<int> opt2 = 3; 1996 1997 opt2.swap(opt1); 1998 1999 opt1.value(); 2000 2001 opt2.value(); // [[unsafe]] 2002 } 2003 )"); 2004 } 2005 2006 TEST_P(UncheckedOptionalAccessTest, StdSwap) { 2007 ExpectDiagnosticsFor( 2008 R"( 2009 #include "unchecked_optional_access_test.h" 2010 2011 void target() { 2012 $ns::$optional<int> opt1 = $ns::nullopt; 2013 $ns::$optional<int> opt2 = 3; 2014 2015 std::swap(opt1, opt2); 2016 2017 opt1.value(); 2018 2019 opt2.value(); // [[unsafe]] 2020 } 2021 )"); 2022 2023 ExpectDiagnosticsFor( 2024 R"( 2025 #include "unchecked_optional_access_test.h" 2026 2027 void target() { 2028 $ns::$optional<int> opt1 = $ns::nullopt; 2029 $ns::$optional<int> opt2 = 3; 2030 2031 std::swap(opt2, opt1); 2032 2033 opt1.value(); 2034 2035 opt2.value(); // [[unsafe]] 2036 } 2037 )"); 2038 } 2039 2040 TEST_P(UncheckedOptionalAccessTest, UniquePtrToStructWithOptionalField) { 2041 // We suppress diagnostics for values reachable from smart pointers (other 2042 // than `optional` itself). 2043 ExpectDiagnosticsFor( 2044 R"( 2045 #include "unchecked_optional_access_test.h" 2046 2047 template <typename T> 2048 struct smart_ptr { 2049 T& operator*() &; 2050 T* operator->(); 2051 }; 2052 2053 struct Foo { 2054 $ns::$optional<int> opt; 2055 }; 2056 2057 void target() { 2058 smart_ptr<Foo> foo; 2059 *foo->opt; 2060 *(*foo).opt; 2061 } 2062 )"); 2063 } 2064 2065 TEST_P(UncheckedOptionalAccessTest, CallReturningOptional) { 2066 ExpectDiagnosticsFor( 2067 R"( 2068 #include "unchecked_optional_access_test.h" 2069 2070 $ns::$optional<int> MakeOpt(); 2071 2072 void target() { 2073 $ns::$optional<int> opt = 0; 2074 opt = MakeOpt(); 2075 opt.value(); // [[unsafe]] 2076 } 2077 )"); 2078 ExpectDiagnosticsFor( 2079 R"( 2080 #include "unchecked_optional_access_test.h" 2081 2082 const $ns::$optional<int>& MakeOpt(); 2083 2084 void target() { 2085 $ns::$optional<int> opt = 0; 2086 opt = MakeOpt(); 2087 opt.value(); // [[unsafe]] 2088 } 2089 )"); 2090 2091 ExpectDiagnosticsFor( 2092 R"( 2093 #include "unchecked_optional_access_test.h" 2094 2095 using IntOpt = $ns::$optional<int>; 2096 IntOpt MakeOpt(); 2097 2098 void target() { 2099 IntOpt opt = 0; 2100 opt = MakeOpt(); 2101 opt.value(); // [[unsafe]] 2102 } 2103 )"); 2104 2105 ExpectDiagnosticsFor( 2106 R"( 2107 #include "unchecked_optional_access_test.h" 2108 2109 using IntOpt = $ns::$optional<int>; 2110 const IntOpt& MakeOpt(); 2111 2112 void target() { 2113 IntOpt opt = 0; 2114 opt = MakeOpt(); 2115 opt.value(); // [[unsafe]] 2116 } 2117 )"); 2118 } 2119 2120 // Verifies that the model sees through aliases. 2121 TEST_P(UncheckedOptionalAccessTest, WithAlias) { 2122 ExpectDiagnosticsFor( 2123 R"( 2124 #include "unchecked_optional_access_test.h" 2125 2126 template <typename T> 2127 using MyOptional = $ns::$optional<T>; 2128 2129 void target(MyOptional<int> opt) { 2130 opt.value(); // [[unsafe]] 2131 } 2132 )"); 2133 } 2134 2135 TEST_P(UncheckedOptionalAccessTest, OptionalValueOptional) { 2136 // Basic test that nested values are populated. We nest an optional because 2137 // its easy to use in a test, but the type of the nested value shouldn't 2138 // matter. 2139 ExpectDiagnosticsFor( 2140 R"( 2141 #include "unchecked_optional_access_test.h" 2142 2143 using Foo = $ns::$optional<std::string>; 2144 2145 void target($ns::$optional<Foo> foo) { 2146 if (foo && *foo) { 2147 foo->value(); 2148 } 2149 } 2150 )"); 2151 2152 // Mutation is supported for nested values. 2153 ExpectDiagnosticsFor( 2154 R"( 2155 #include "unchecked_optional_access_test.h" 2156 2157 using Foo = $ns::$optional<std::string>; 2158 2159 void target($ns::$optional<Foo> foo) { 2160 if (foo && *foo) { 2161 foo->reset(); 2162 foo->value(); // [[unsafe]] 2163 } 2164 } 2165 )"); 2166 } 2167 2168 // Tests that structs can be nested. We use an optional field because its easy 2169 // to use in a test, but the type of the field shouldn't matter. 2170 TEST_P(UncheckedOptionalAccessTest, OptionalValueStruct) { 2171 ExpectDiagnosticsFor( 2172 R"( 2173 #include "unchecked_optional_access_test.h" 2174 2175 struct Foo { 2176 $ns::$optional<std::string> opt; 2177 }; 2178 2179 void target($ns::$optional<Foo> foo) { 2180 if (foo && foo->opt) { 2181 foo->opt.value(); 2182 } 2183 } 2184 )"); 2185 } 2186 2187 TEST_P(UncheckedOptionalAccessTest, OptionalValueInitialization) { 2188 // FIXME: Fix when to initialize `value`. All unwrapping should be safe in 2189 // this example, but `value` initialization is done multiple times during the 2190 // fixpoint iterations and joining the environment won't correctly merge them. 2191 ExpectDiagnosticsFor( 2192 R"( 2193 #include "unchecked_optional_access_test.h" 2194 2195 using Foo = $ns::$optional<std::string>; 2196 2197 void target($ns::$optional<Foo> foo, bool b) { 2198 if (!foo.has_value()) return; 2199 if (b) { 2200 if (!foo->has_value()) return; 2201 // We have created `foo.value()`. 2202 foo->value(); 2203 } else { 2204 if (!foo->has_value()) return; 2205 // We have created `foo.value()` again, in a different environment. 2206 foo->value(); 2207 } 2208 // Now we merge the two values. UncheckedOptionalAccessModel::merge() will 2209 // throw away the "value" property. 2210 foo->value(); // [[unsafe]] 2211 } 2212 )"); 2213 } 2214 2215 TEST_P(UncheckedOptionalAccessTest, AssignThroughLvalueReferencePtr) { 2216 ExpectDiagnosticsFor( 2217 R"( 2218 #include "unchecked_optional_access_test.h" 2219 2220 template <typename T> 2221 struct smart_ptr { 2222 typename std::add_lvalue_reference<T>::type operator*() &; 2223 }; 2224 2225 void target() { 2226 smart_ptr<$ns::$optional<float>> x; 2227 *x = $ns::nullopt; 2228 (*x).value(); // [[unsafe]] 2229 } 2230 )"); 2231 } 2232 2233 TEST_P(UncheckedOptionalAccessTest, CorrelatedBranches) { 2234 ExpectDiagnosticsFor(R"code( 2235 #include "unchecked_optional_access_test.h" 2236 2237 void target(bool b, $ns::$optional<int> opt) { 2238 if (b || opt.has_value()) { 2239 if (!b) { 2240 opt.value(); 2241 } 2242 } 2243 } 2244 )code"); 2245 2246 ExpectDiagnosticsFor(R"code( 2247 #include "unchecked_optional_access_test.h" 2248 2249 void target(bool b, $ns::$optional<int> opt) { 2250 if (b && !opt.has_value()) return; 2251 if (b) { 2252 opt.value(); 2253 } 2254 } 2255 )code"); 2256 2257 ExpectDiagnosticsFor( 2258 R"code( 2259 #include "unchecked_optional_access_test.h" 2260 2261 void target(bool b, $ns::$optional<int> opt) { 2262 if (opt.has_value()) b = true; 2263 if (b) { 2264 opt.value(); // [[unsafe]] 2265 } 2266 } 2267 )code"); 2268 2269 ExpectDiagnosticsFor(R"code( 2270 #include "unchecked_optional_access_test.h" 2271 2272 void target(bool b, $ns::$optional<int> opt) { 2273 if (b) return; 2274 if (opt.has_value()) b = true; 2275 if (b) { 2276 opt.value(); 2277 } 2278 } 2279 )code"); 2280 2281 ExpectDiagnosticsFor(R"( 2282 #include "unchecked_optional_access_test.h" 2283 2284 void target(bool b, $ns::$optional<int> opt) { 2285 if (opt.has_value() == b) { 2286 if (b) { 2287 opt.value(); 2288 } 2289 } 2290 } 2291 )"); 2292 2293 ExpectDiagnosticsFor(R"( 2294 #include "unchecked_optional_access_test.h" 2295 2296 void target(bool b, $ns::$optional<int> opt) { 2297 if (opt.has_value() != b) { 2298 if (!b) { 2299 opt.value(); 2300 } 2301 } 2302 } 2303 )"); 2304 2305 ExpectDiagnosticsFor(R"( 2306 #include "unchecked_optional_access_test.h" 2307 2308 void target(bool b) { 2309 $ns::$optional<int> opt1 = $ns::nullopt; 2310 $ns::$optional<int> opt2; 2311 if (b) { 2312 opt2 = $ns::nullopt; 2313 } else { 2314 opt2 = $ns::nullopt; 2315 } 2316 if (opt2.has_value()) { 2317 opt1.value(); 2318 } 2319 } 2320 )"); 2321 2322 // FIXME: Add support for operator==. 2323 // ExpectDiagnosticsFor(R"( 2324 // #include "unchecked_optional_access_test.h" 2325 // 2326 // void target($ns::$optional<int> opt1, $ns::$optional<int> opt2) { 2327 // if (opt1 == opt2) { 2328 // if (opt1.has_value()) { 2329 // opt2.value(); 2330 // } 2331 // } 2332 // } 2333 // )"); 2334 } 2335 2336 TEST_P(UncheckedOptionalAccessTest, JoinDistinctValues) { 2337 ExpectDiagnosticsFor( 2338 R"code( 2339 #include "unchecked_optional_access_test.h" 2340 2341 void target(bool b) { 2342 $ns::$optional<int> opt; 2343 if (b) { 2344 opt = Make<$ns::$optional<int>>(); 2345 } else { 2346 opt = Make<$ns::$optional<int>>(); 2347 } 2348 if (opt.has_value()) { 2349 opt.value(); 2350 } else { 2351 opt.value(); // [[unsafe]] 2352 } 2353 } 2354 )code"); 2355 2356 ExpectDiagnosticsFor(R"code( 2357 #include "unchecked_optional_access_test.h" 2358 2359 void target(bool b) { 2360 $ns::$optional<int> opt; 2361 if (b) { 2362 opt = Make<$ns::$optional<int>>(); 2363 if (!opt.has_value()) return; 2364 } else { 2365 opt = Make<$ns::$optional<int>>(); 2366 if (!opt.has_value()) return; 2367 } 2368 opt.value(); 2369 } 2370 )code"); 2371 2372 ExpectDiagnosticsFor( 2373 R"code( 2374 #include "unchecked_optional_access_test.h" 2375 2376 void target(bool b) { 2377 $ns::$optional<int> opt; 2378 if (b) { 2379 opt = Make<$ns::$optional<int>>(); 2380 if (!opt.has_value()) return; 2381 } else { 2382 opt = Make<$ns::$optional<int>>(); 2383 } 2384 opt.value(); // [[unsafe]] 2385 } 2386 )code"); 2387 2388 ExpectDiagnosticsFor( 2389 R"code( 2390 #include "unchecked_optional_access_test.h" 2391 2392 void target(bool b) { 2393 $ns::$optional<int> opt; 2394 if (b) { 2395 opt = 1; 2396 } else { 2397 opt = 2; 2398 } 2399 opt.value(); 2400 } 2401 )code"); 2402 2403 ExpectDiagnosticsFor( 2404 R"code( 2405 #include "unchecked_optional_access_test.h" 2406 2407 void target(bool b) { 2408 $ns::$optional<int> opt; 2409 if (b) { 2410 opt = 1; 2411 } else { 2412 opt = Make<$ns::$optional<int>>(); 2413 } 2414 opt.value(); // [[unsafe]] 2415 } 2416 )code"); 2417 } 2418 2419 TEST_P(UncheckedOptionalAccessTest, ReassignValueInLoop) { 2420 ExpectDiagnosticsFor(R"( 2421 #include "unchecked_optional_access_test.h" 2422 2423 void target() { 2424 $ns::$optional<int> opt = 3; 2425 while (Make<bool>()) { 2426 opt.value(); 2427 } 2428 } 2429 )"); 2430 2431 ExpectDiagnosticsFor(R"( 2432 #include "unchecked_optional_access_test.h" 2433 2434 void target() { 2435 $ns::$optional<int> opt = 3; 2436 while (Make<bool>()) { 2437 opt.value(); 2438 2439 opt = Make<$ns::$optional<int>>(); 2440 if (!opt.has_value()) return; 2441 } 2442 } 2443 )"); 2444 2445 ExpectDiagnosticsFor( 2446 R"( 2447 #include "unchecked_optional_access_test.h" 2448 2449 void target() { 2450 $ns::$optional<int> opt = 3; 2451 while (Make<bool>()) { 2452 opt.value(); // [[unsafe]] 2453 2454 opt = Make<$ns::$optional<int>>(); 2455 } 2456 } 2457 )"); 2458 2459 ExpectDiagnosticsFor( 2460 R"( 2461 #include "unchecked_optional_access_test.h" 2462 2463 void target() { 2464 $ns::$optional<int> opt = 3; 2465 while (Make<bool>()) { 2466 opt.value(); // [[unsafe]] 2467 2468 opt = Make<$ns::$optional<int>>(); 2469 if (!opt.has_value()) continue; 2470 } 2471 } 2472 )"); 2473 } 2474 2475 // FIXME: Add support for: 2476 // - constructors (copy, move) 2477 // - assignment operators (default, copy, move) 2478 // - invalidation (passing optional by non-const reference/pointer) 2479