[clang-tidy] `bugprone-unchecked-optional-access`: handle inheritance from `BloombergLP::bslstl::Optional_Base` to prevent false-positives for allocator-aware BDE types (#168863)

### Problem

`bugprone-unchecked-optional-access` produces a lot of false positives
if type inside of `bsl::optional` or `bdlb::NullableValue` is
**allocator-aware**.

This is a very common pattern, especially due to frequent use of
`bsl::string`.

[Compiler explorer example to showcase false-positives with BDE
library](https://compiler-explorer.com/z/P4zh7KbGx)

### Context

https://github.com/llvm/llvm-project/pull/101450 added support for
analysing `bsl::optional` access patterns.

However, mock `bsl::optional` type has been very simplified for testing
purposes which lead to missing false-positives related to _inheritance_
logic for this type.

[According to this
article](https://bloomberg.github.io/bde/articles/bsl_optional.html#interoperability-between-bsl-optional-and-bdlb-nullablevalue),
there are two ways of inheritance for `bsl::optional` and
`bdlb::NullableValue`:
1. C++17 non-allocator-aware type
` bdlb::NullableValue<T> -> bsl::optional<T> -> std::optional<T>`
2.  C++17 **Allocator-Aware**, and pre-C++17
`bdlb::NullableValue<T> -> bsl::optional<T>`

But this is not a full picture :(

In practice, there is an additional layer in the inheritance chain:
`BloombergLP::bslstl::Optional_Base`.

Thus, the actual inheritance structure is:
1. C++17 non-allocator-aware
`bdlb::NullableValue<T> -> bsl::optional<T> ->
BloombergLP::bslstl::Optional_Base<T, false> -> std::optional<T>`
2.  C++17 **Allocator-Aware**, and pre-C++17
`bdlb::NullableValue<T> -> bsl::optional<T> ->
BloombergLP::bslstl::Optional_Base<T, true>`

[Source code to show this
inheritance](https://github.com/bloomberg/bde/blob/f8b09a9298a5a76741c0820344c8850bf0b2e177/groups/bsl/bslstl/bslstl_optional.h#L1851)

### Root cause

IIUC, because of this inheritance logic, function calls to
`bsl::optional::has_value()` are processed like:
1. `std::optional::has_value()` for non-allocator-aware type.
2. `BloombergLP::bslstl::Optional_Base::has_value()` for allocator-aware
type.

Obviously, similar conversion are true for other common methods like
`.value()`

**This PR tries to solve this issue by improving mocks and adding
`BloombergLP::bslstl::Optional_Base<T>` to list of supported optional
types**
5 files changed
tree: 42021bf5f74ea5ac1156fc7d12b89d5df31871cb
  1. .ci/
  2. .github/
  3. bolt/
  4. clang/
  5. clang-tools-extra/
  6. cmake/
  7. compiler-rt/
  8. cross-project-tests/
  9. flang/
  10. flang-rt/
  11. libc/
  12. libclc/
  13. libcxx/
  14. libcxxabi/
  15. libsycl/
  16. libunwind/
  17. lld/
  18. lldb/
  19. llvm/
  20. llvm-libgcc/
  21. mlir/
  22. offload/
  23. openmp/
  24. orc-rt/
  25. polly/
  26. runtimes/
  27. third-party/
  28. utils/
  29. .clang-format
  30. .clang-format-ignore
  31. .clang-tidy
  32. .git-blame-ignore-revs
  33. .gitattributes
  34. .gitignore
  35. .mailmap
  36. CODE_OF_CONDUCT.md
  37. CONTRIBUTING.md
  38. LICENSE.TXT
  39. pyproject.toml
  40. README.md
  41. SECURITY.md
README.md

The LLVM Compiler Infrastructure

OpenSSF Scorecard OpenSSF Best Practices libc++

Welcome to the LLVM project!

This repository contains the source code for LLVM, a toolkit for the construction of highly optimized compilers, optimizers, and run-time environments.

The LLVM project has multiple components. The core of the project is itself called “LLVM”. This contains all of the tools, libraries, and header files needed to process intermediate representations and convert them into object files. Tools include an assembler, disassembler, bitcode analyzer, and bitcode optimizer.

C-like languages use the Clang frontend. This component compiles C, C++, Objective-C, and Objective-C++ code into LLVM bitcode -- and from there into object files, using LLVM.

Other components include: the libc++ C++ standard library, the LLD linker, and more.

Getting the Source Code and Building LLVM

Consult the Getting Started with LLVM page for information on building and running LLVM.

For information on how to contribute to the LLVM project, please take a look at the Contributing to LLVM guide.

Getting in touch

Join the LLVM Discourse forums, Discord chat, LLVM Office Hours or Regular sync-ups.

The LLVM project has adopted a code of conduct for participants to all modes of communication within the project.