Skip to content

[lldb][test] Add libcxx-simulators test for std::optional #111133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 7, 2024

Conversation

Michael137
Copy link
Member

Follow-up to the LLDB std::optional data-formatter test failure caused by #110355.

Two formats are supported:

  1. __val_ has type value_type
  2. __val_'s type is wrapped in std::remove_cv_t

Follow-up to the LLDB std::optional data-formatter test
failure caused by llvm#110355.

Two formats are supported:
1. `__val_` has type `value_type`
2. `__val_`'s type is wrapped in `std::remove_cv_t`
@Michael137 Michael137 requested a review from labath October 4, 2024 10:59
@llvmbot llvmbot added the lldb label Oct 4, 2024
@llvmbot
Copy link
Member

llvmbot commented Oct 4, 2024

@llvm/pr-subscribers-lldb

Author: Michael Buch (Michael137)

Changes

Follow-up to the LLDB std::optional data-formatter test failure caused by #110355.

Two formats are supported:

  1. __val_ has type value_type
  2. __val_'s type is wrapped in std::remove_cv_t

Full diff: https://github.com/llvm/llvm-project/pull/111133.diff

3 Files Affected:

  • (added) lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx-simulators/optional/Makefile (+3)
  • (added) lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx-simulators/optional/TestDataFormatterLibcxxOptionalSimulator.py (+40)
  • (added) lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx-simulators/optional/main.cpp (+103)
diff --git a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx-simulators/optional/Makefile b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx-simulators/optional/Makefile
new file mode 100644
index 00000000000000..38cfa81053488c
--- /dev/null
+++ b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx-simulators/optional/Makefile
@@ -0,0 +1,3 @@
+CXX_SOURCES := main.cpp
+override CXXFLAGS_EXTRAS += -std=c++14
+include Makefile.rules
diff --git a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx-simulators/optional/TestDataFormatterLibcxxOptionalSimulator.py b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx-simulators/optional/TestDataFormatterLibcxxOptionalSimulator.py
new file mode 100644
index 00000000000000..4fa67e8b2bf96a
--- /dev/null
+++ b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx-simulators/optional/TestDataFormatterLibcxxOptionalSimulator.py
@@ -0,0 +1,40 @@
+"""
+Test we can understand various layouts of the libc++'s std::optional
+"""
+
+
+import lldb
+from lldbsuite.test.decorators import *
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+import functools
+
+
+class LibcxxOptionalDataFormatterSimulatorTestCase(TestBase):
+    NO_DEBUG_INFO_TESTCASE = True
+
+    def _run_test(self, defines):
+        cxxflags_extras = " ".join(["-D%s" % d for d in defines])
+        self.build(dictionary=dict(CXXFLAGS_EXTRAS=cxxflags_extras))
+        lldbutil.run_to_source_breakpoint(
+            self, "Break here", lldb.SBFileSpec("main.cpp")
+        )
+        self.expect_var_path("maybe_int", summary=' Has Value=true ',
+                             children=[ValueCheck(name="Value", summary=None, value="42")])
+        self.expect_var_path("maybe_string", summary=' Has Value=true ',
+                             children=[ValueCheck(name="Value", summary='"Hello"')])
+
+        self.expect_expr("maybe_int", result_summary=' Has Value=true ',
+                         result_children=[ValueCheck(name="Value", summary=None, value="42")])
+
+        self.expect_expr("maybe_string", result_summary=' Has Value=true ',
+                         result_children=[ValueCheck(name="Value", summary='"Hello"')])
+
+
+for r in range(2):
+    name = f'test_r{r}'
+    defines = [f'REVISION={r}']
+    f = functools.partialmethod(
+        LibcxxOptionalDataFormatterSimulatorTestCase._run_test, defines
+    )
+    setattr(LibcxxOptionalDataFormatterSimulatorTestCase, name, f)
diff --git a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx-simulators/optional/main.cpp b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx-simulators/optional/main.cpp
new file mode 100644
index 00000000000000..fd041b7fee7ee7
--- /dev/null
+++ b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx-simulators/optional/main.cpp
@@ -0,0 +1,103 @@
+#include <type_traits>
+
+#if REVISION == 0
+// Pre-a3942b3 layout.
+#define HAS_REMOVE_CV
+#endif
+// REVISION == 1: current layout
+
+namespace std {
+namespace __lldb {
+
+struct in_place_t {
+  explicit in_place_t() = default;
+};
+constexpr in_place_t in_place{};
+
+template <class _Tp, bool = is_trivially_destructible<_Tp>::value>
+struct __optional_destruct_base {
+  typedef _Tp value_type;
+  union {
+    char __null_state_;
+#ifdef HAS_REMOVE_CV
+    remove_cv_t<value_type> __val_;
+#else // !HAS_REMOVE_CV
+    value_type __val_;
+#endif
+  };
+  bool __engaged_;
+
+  template <class... _Args>
+  constexpr explicit __optional_destruct_base(in_place_t, _Args &&...__args)
+      : __val_(std::forward<_Args>(__args)...), __engaged_(true) {}
+};
+
+template <class _Tp, bool = is_reference<_Tp>::value>
+struct __optional_storage_base : __optional_destruct_base<_Tp> {
+  using __base = __optional_destruct_base<_Tp>;
+  using value_type = _Tp;
+  using __base::__base;
+};
+
+template <class _Tp, bool = is_trivially_copy_constructible<_Tp>::value>
+struct __optional_copy_base : __optional_storage_base<_Tp> {
+  using __optional_storage_base<_Tp>::__optional_storage_base;
+};
+
+template <class _Tp, bool = is_trivially_move_constructible<_Tp>::value>
+struct __optional_move_base : __optional_copy_base<_Tp> {
+  using __optional_copy_base<_Tp>::__optional_copy_base;
+};
+
+template <class _Tp, bool = is_trivially_destructible<_Tp>::value &&
+                            is_trivially_copy_constructible<_Tp>::value &&
+                            is_trivially_copy_assignable<_Tp>::value>
+struct __optional_copy_assign_base : __optional_move_base<_Tp> {
+  using __optional_move_base<_Tp>::__optional_move_base;
+};
+
+template <class _Tp, bool = is_trivially_destructible<_Tp>::value &&
+                            is_trivially_move_constructible<_Tp>::value &&
+                            is_trivially_move_assignable<_Tp>::value>
+struct __optional_move_assign_base : __optional_copy_assign_base<_Tp> {
+  using __optional_copy_assign_base<_Tp>::__optional_copy_assign_base;
+};
+
+template <bool _CanCopy, bool _CanMove> struct __sfinae_ctor_base {};
+
+template <class _Tp>
+using __optional_sfinae_ctor_base_t =
+    __sfinae_ctor_base<is_copy_constructible<_Tp>::value,
+                       is_move_constructible<_Tp>::value>;
+
+template <bool _CanCopy, bool _CanMove> struct __sfinae_assign_base {};
+
+template <class _Tp>
+using __optional_sfinae_assign_base_t = __sfinae_assign_base<
+    (is_copy_constructible<_Tp>::value && is_copy_assignable<_Tp>::value),
+    (is_move_constructible<_Tp>::value && is_move_assignable<_Tp>::value)>;
+
+template <class _Tp>
+class optional : private __optional_move_assign_base<_Tp>,
+                 private __optional_sfinae_ctor_base_t<_Tp>,
+                 private __optional_sfinae_assign_base_t<_Tp> {
+  using __base = __optional_move_assign_base<_Tp>;
+
+public:
+  using value_type = _Tp;
+
+public:
+  template <class _Up = value_type>
+  constexpr explicit optional(_Up &&__v)
+      : __base(in_place, std::forward<_Up>(__v)) {}
+};
+
+} // namespace __lldb
+} // namespace std
+
+int main() {
+  std::__lldb::optional<char const *> maybe_string{"Hello"};
+  std::__lldb::optional<int> maybe_int{42};
+  __builtin_printf("Break here\n");
+  return 0;
+}

Copy link

github-actions bot commented Oct 4, 2024

✅ With the latest revision this PR passed the Python code formatter.

@Michael137 Michael137 merged commit 66713a0 into llvm:main Oct 7, 2024
6 of 7 checks passed
@Michael137 Michael137 deleted the lldb/simulate-optional branch October 7, 2024 10:02
@Michael137
Copy link
Member Author

looks like we dont deal with __builtin_printf on Windows very well. Will fix

Kyvangka1610 added a commit to Kyvangka1610/llvm-project that referenced this pull request Oct 7, 2024
* commit 'FETCH_HEAD':
  [X86] getIntImmCostInst - pull out repeated Imm.getBitWidth() calls. NFC.
  [X86] Add test coverage for llvm#111323
  [Driver] Use empty multilib file in another test (llvm#111352)
  [clang][OpenMP][test] Use x86_64-linux-gnu triple for test referencing avx512f feature (llvm#111337)
  [doc] Fix Kaleidoscope tutorial chapter 3 code snippet and full listing discrepancies (llvm#111289)
  [Flang][OpenMP] Improve entry block argument creation and binding (llvm#110267)
  [x86] combineMul - handle 0/-1 KnownBits cases before MUL_IMM logic (REAPPLIED)
  [llvm-dis] Fix non-deterministic disassembly across multiple inputs (llvm#110988)
  [lldb][test] TestDataFormatterLibcxxOptionalSimulator.py: change order of ifdefs
  [lldb][test] Add libcxx-simulators test for std::optional (llvm#111133)
  [x86] combineMul - use computeKnownBits directly to find MUL_IMM constant splat. (REAPPLIED)
  Reland "[lldb][test] TestDataFormatterLibcxxStringSimulator.py: add new padding layout" (llvm#111123)
  Revert "[x86] combineMul - use computeKnownBits directly to find MUL_IMM constant splat."
  update_test_checks: fix a simple regression  (llvm#111347)
  [LegalizeVectorTypes] Always widen fabs (llvm#111298)
  [lsan] Make ReportUnsuspendedThreads return bool also for Fuchsia
  [mlir][vector] Add more tests for ConvertVectorToLLVM (6/n) (llvm#111121)
  [bazel] port 9144fed
  [SystemZ] Remove inlining threshold multiplier. (llvm#106058)
  [LegalizeVectorTypes] When widening don't check for libcalls if promoted (llvm#111297)
  [clang][Driver] Improve multilib custom error reporting (llvm#110804)
  [clang][Driver] Rename "FatalError" key to "Error" in multilib.yaml (llvm#110804)
  [LLVM][Maintainers] Update release managers (llvm#111164)
  [Clang][Driver] Add option to provide path for multilib's YAML config file (llvm#109640)
  [LoopVectorize] Remove redundant code in emitSCEVChecks (llvm#111132)
  [AMDGPU] Only emit SCOPE_SYS global_wb (llvm#110636)
  [ELF] Change Ctx::target to unique_ptr (llvm#111260)
  [ELF] Pass Ctx & to some free functions
  [RISCV] Only disassemble fcvtmod.w.d if the rounding mode is rtz. (llvm#111308)
  [Clang] Remove the special-casing for RequiresExprBodyDecl in BuildResolvedCallExpr() after fd87d76 (llvm#111277)
  [ELF] Pass Ctx & to InputFile
  [clang-format] Add AlignFunctionDeclarations to AlignConsecutiveDeclarations (llvm#108241)
  [AMDGPU] Support preloading hidden kernel arguments (llvm#98861)
  [ELF] Move static nextGroupId isInGroup to LinkerDriver
  [clangd] Add ArgumentLists config option under Completion (llvm#111322)
  [ELF] Pass Ctx & to SyntheticSections
  [ELF] Pass Ctx & to Symbols
  [ELF] Pass Ctx & to Symbols
  [ELF] getRelocTargetVA: pass Ctx and Relocation. NFC
  [clang-tidy] Avoid capturing a local variable in a static lambda in UseRangesCheck (llvm#111282)
  [VPlan] Use pointer to member 0 as VPInterleaveRecipe's pointer arg. (llvm#106431)
  [clangd] Simplify ternary expressions with std::optional::value_or (NFC) (llvm#111309)
  [libc++][format][2/3] Optimizes c-string arguments. (llvm#101805)
  [RISCV] Combine RVBUnary and RVKUnary into classes that are more similar to ALU(W)_r(r/i). NFC (llvm#111279)
  [ELF] Pass Ctx & to InputFiles
  [libc] GPU RPC interface: add return value to `rpc_host_call` (llvm#111288)

Signed-off-by: kyvangka1610 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants