[Lldb-commits] [lldb] [lldb][Clike] Add LanguageOpts class (PR #223701)

Raphael Isemann via lldb-commits lldb-commits at lists.llvm.org
Fri Oct 9 05:59:16 PDT 2026


https://github.com/Teemperor updated https://github.com/llvm/llvm-project/pull/223701

>From fbaa5182becfb1ba6bb72d57402cf216a22c84d6 Mon Sep 17 00:00:00 2001
From: Raphael Isemann <rise at apple.com>
Date: Tue, 8 Sep 2026 12:01:32 +0100
Subject: [PATCH] [lldb][Clike] Add LanguageOpts class

TypeSystemClike needs to know about the sizes of various language
builtins on the current target. The LanguageOpts class provides this
information by querying Clang. The implementation of
GetFloatTypeSemantics and GetBitIntByteSize mirrors the logic in
TypeSystemClang.
---
 .../Plugins/TypeSystem/Clike/CMakeLists.txt   |   3 +
 .../Plugins/TypeSystem/Clike/LanguageOpts.cpp | 109 ++++++++++++++++++
 .../Plugins/TypeSystem/Clike/LanguageOpts.h   |  89 ++++++++++++++
 .../unittests/TypeSystem/Clike/CMakeLists.txt |   1 +
 .../TypeSystem/Clike/LanguageOptsTest.cpp     |  80 +++++++++++++
 5 files changed, 282 insertions(+)
 create mode 100644 lldb/source/Plugins/TypeSystem/Clike/LanguageOpts.cpp
 create mode 100644 lldb/source/Plugins/TypeSystem/Clike/LanguageOpts.h
 create mode 100644 lldb/unittests/TypeSystem/Clike/LanguageOptsTest.cpp

diff --git a/lldb/source/Plugins/TypeSystem/Clike/CMakeLists.txt b/lldb/source/Plugins/TypeSystem/Clike/CMakeLists.txt
index 3f8e742165cfd3..ea52806cda68bd 100644
--- a/lldb/source/Plugins/TypeSystem/Clike/CMakeLists.txt
+++ b/lldb/source/Plugins/TypeSystem/Clike/CMakeLists.txt
@@ -1,5 +1,6 @@
 add_lldb_library(lldbPluginTypeSystemClike PLUGIN
   Identifier.cpp
+  LanguageOpts.cpp
   TypeSystemClike.cpp
 
   LINK_COMPONENTS
@@ -8,4 +9,6 @@ add_lldb_library(lldbPluginTypeSystemClike PLUGIN
     lldbCore
     lldbSymbol
     lldbUtility
+  CLANG_LIBS
+    clangBasic
 )
diff --git a/lldb/source/Plugins/TypeSystem/Clike/LanguageOpts.cpp b/lldb/source/Plugins/TypeSystem/Clike/LanguageOpts.cpp
new file mode 100644
index 00000000000000..cb0bc6772180eb
--- /dev/null
+++ b/lldb/source/Plugins/TypeSystem/Clike/LanguageOpts.cpp
@@ -0,0 +1,109 @@
+//===-- LanguageOpts.cpp --------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "LanguageOpts.h"
+
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/DiagnosticIDs.h"
+#include "clang/Basic/DiagnosticOptions.h"
+#include "clang/Basic/TargetInfo.h"
+#include "clang/Basic/TargetOptions.h"
+
+#include "llvm/ADT/APFloat.h"
+#include "llvm/Support/ErrorExtras.h"
+#include "llvm/Support/MathExtras.h"
+
+#include <memory>
+#include <utility>
+
+using namespace lldb_private;
+using namespace lldb_private::clike;
+
+/// Build a TargetInfo for \p triple, or null if Clang doesn't know the target.
+static std::unique_ptr<clang::TargetInfo>
+CreateTargetInfo(clang::TargetOptions target_opts) {
+  clang::DiagnosticOptions diag_opts;
+  clang::DiagnosticsEngine diags(
+      llvm::makeIntrusiveRefCnt<clang::DiagnosticIDs>(), diag_opts,
+      new clang::IgnoringDiagConsumer());
+
+  return std::unique_ptr<clang::TargetInfo>(
+      clang::TargetInfo::CreateTargetInfo(diags, target_opts));
+}
+
+LanguageOpts::LanguageOpts(llvm::Triple triple,
+                           std::unique_ptr<clang::TargetInfo> target,
+                           BuiltinSizes sizes, FloatSemantics semantics)
+    : m_triple(std::move(triple)), m_target(std::move(target)),
+      m_builtin_sizes(sizes), m_semantics(semantics) {}
+
+llvm::Expected<LanguageOpts> LanguageOpts::Create(llvm::Triple triple) {
+  clang::TargetOptions target_opts;
+  target_opts.Triple = triple.str();
+  std::unique_ptr<clang::TargetInfo> target = CreateTargetInfo(target_opts);
+  if (!target)
+    return llvm::createStringErrorV("Unknown Clang target: '{0}'",
+                                    triple.str());
+
+  auto bytes = [](unsigned bits) -> uint32_t { return bits / 8; };
+  BuiltinSizes sizes;
+  sizes.bool_size = bytes(target->getBoolWidth());
+  sizes.short_size = bytes(target->getShortWidth());
+  sizes.int_size = bytes(target->getIntWidth());
+  sizes.long_size = bytes(target->getLongWidth());
+  sizes.long_long_size = bytes(target->getLongLongWidth());
+  sizes.wchar_size = bytes(target->getWCharWidth());
+  sizes.char16_size = bytes(target->getChar16Width());
+  sizes.char32_size = bytes(target->getChar32Width());
+  sizes.float_size = bytes(target->getFloatWidth());
+  sizes.double_size = bytes(target->getDoubleWidth());
+  sizes.long_double_size = bytes(target->getLongDoubleWidth());
+  sizes.pointer_size = bytes(target->getPointerWidth(clang::LangAS::Default));
+
+  FloatSemantics semantics;
+  semantics.half = &target->getHalfFormat();
+  semantics.single = &target->getFloatFormat();
+  semantics.double_ = &target->getDoubleFormat();
+  semantics.long_double = &target->getLongDoubleFormat();
+  semantics.float128 = &target->getFloat128Format();
+
+  return LanguageOpts(std::move(triple), std::move(target), sizes, semantics);
+}
+
+const llvm::fltSemantics &
+LanguageOpts::GetFloatTypeSemantics(const size_t byte_size,
+                                    const lldb::Format format) const {
+  const size_t bit_size = byte_size * 8;
+  if (byte_size == m_builtin_sizes.float_size)
+    return *m_semantics.single;
+  if (byte_size == m_builtin_sizes.double_size)
+    return *m_semantics.double_;
+  if (format == lldb::eFormatFloat128 && bit_size == 128)
+    return *m_semantics.float128;
+  if (byte_size == m_builtin_sizes.long_double_size ||
+      bit_size == llvm::APFloat::semanticsSizeInBits(*m_semantics.long_double))
+    return *m_semantics.long_double;
+  if (bit_size == 16)
+    return *m_semantics.half;
+  if (bit_size == 128)
+    return *m_semantics.float128;
+  return llvm::APFloat::Bogus();
+}
+
+std::optional<uint64_t> LanguageOpts::GetBitIntByteSize(unsigned bits) const {
+  if (bits == 0)
+    return std::nullopt;
+
+  if (bits > m_target->getMaxBitIntWidth())
+    return std::nullopt;
+
+  // clang lays out a `_BitInt` by rounding its width up to its ABI alignment.
+  unsigned align_bits = m_target->getBitIntAlign(bits);
+  uint64_t size_bits = llvm::alignTo(bits, align_bits);
+  return size_bits / 8;
+}
diff --git a/lldb/source/Plugins/TypeSystem/Clike/LanguageOpts.h b/lldb/source/Plugins/TypeSystem/Clike/LanguageOpts.h
new file mode 100644
index 00000000000000..c594c50d0d6abe
--- /dev/null
+++ b/lldb/source/Plugins/TypeSystem/Clike/LanguageOpts.h
@@ -0,0 +1,89 @@
+//===-- LanguageOpts.h ----------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLDB_SOURCE_PLUGINS_TYPESYSTEM_CLIKE_LANGUAGEOPTS_H
+#define LLDB_SOURCE_PLUGINS_TYPESYSTEM_CLIKE_LANGUAGEOPTS_H
+
+#include "clang/Basic/TargetInfo.h"
+
+#include "llvm/Support/Error.h"
+#include "llvm/TargetParser/Triple.h"
+
+#include "lldb/lldb-enumerations.h"
+
+#include <cstdint>
+#include <memory>
+#include <optional>
+
+namespace llvm {
+struct fltSemantics;
+} // namespace llvm
+
+namespace lldb_private {
+namespace clike {
+
+/// Describes the target/language configuration that determines properties of
+/// the type system, such as builtin type sizes and encodings.
+class LanguageOpts {
+public:
+  /// Sizes (in bytes) of the target-dependent builtin types.
+  struct BuiltinSizes {
+    uint32_t bool_size = 0;
+    uint32_t short_size = 0;
+    uint32_t int_size = 0;
+    uint32_t long_size = 0;
+    uint32_t long_long_size = 0;
+    uint32_t wchar_size = 0;
+    uint32_t char16_size = 0;
+    uint32_t char32_size = 0;
+    uint32_t float_size = 0;
+    uint32_t double_size = 0;
+    uint32_t long_double_size = 0;
+    uint32_t pointer_size = 0;
+  };
+
+  /// The language options for \p triple, or an error if Clang cannot describe
+  /// the triple.
+  static llvm::Expected<LanguageOpts> Create(llvm::Triple triple);
+
+  const llvm::Triple &GetTriple() const { return m_triple; }
+  const BuiltinSizes &GetBuiltinSizes() const { return m_builtin_sizes; }
+
+  /// The floating point semantics for a float of the given storage size.
+  const llvm::fltSemantics &
+  GetFloatTypeSemantics(const size_t byte_size,
+                        const lldb::Format format) const;
+
+  /// The storage size (in bytes) of a `_BitInt(bits)` on this target.
+  ///
+  /// Returns std::nullopt if the bit width is invalid for the target.
+  std::optional<uint64_t> GetBitIntByteSize(unsigned bits) const;
+
+private:
+  /// The target's float formats.
+  struct FloatSemantics {
+    const llvm::fltSemantics *half = nullptr;
+    const llvm::fltSemantics *single = nullptr;
+    const llvm::fltSemantics *double_ = nullptr;
+    const llvm::fltSemantics *long_double = nullptr;
+    const llvm::fltSemantics *float128 = nullptr;
+  };
+
+  LanguageOpts(llvm::Triple triple, std::unique_ptr<clang::TargetInfo> target,
+               BuiltinSizes sizes, FloatSemantics semantics);
+
+  llvm::Triple m_triple;
+  std::unique_ptr<clang::TargetInfo> m_target;
+  BuiltinSizes m_builtin_sizes;
+  FloatSemantics m_semantics;
+};
+
+} // namespace clike
+} // namespace lldb_private
+
+#endif // LLDB_SOURCE_PLUGINS_TYPESYSTEM_CLIKE_LANGUAGEOPTS_H
diff --git a/lldb/unittests/TypeSystem/Clike/CMakeLists.txt b/lldb/unittests/TypeSystem/Clike/CMakeLists.txt
index c74cc7d651de1e..33b8ad9ad72043 100644
--- a/lldb/unittests/TypeSystem/Clike/CMakeLists.txt
+++ b/lldb/unittests/TypeSystem/Clike/CMakeLists.txt
@@ -1,5 +1,6 @@
 add_lldb_unittest(TypeSystemClikeUnitTests
   IdentifierTest.cpp
+  LanguageOptsTest.cpp
 
   LINK_LIBS
     lldbPluginTypeSystemClike
diff --git a/lldb/unittests/TypeSystem/Clike/LanguageOptsTest.cpp b/lldb/unittests/TypeSystem/Clike/LanguageOptsTest.cpp
new file mode 100644
index 00000000000000..a70c9b0ee4a4d1
--- /dev/null
+++ b/lldb/unittests/TypeSystem/Clike/LanguageOptsTest.cpp
@@ -0,0 +1,80 @@
+//===-- LanguageOptsTest.cpp ----------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "Plugins/TypeSystem/Clike/LanguageOpts.h"
+
+#include "llvm/ADT/APFloat.h"
+#include "llvm/Support/Error.h"
+#include "llvm/TargetParser/Triple.h"
+
+#include "gtest/gtest.h"
+
+using namespace lldb_private::clike;
+
+static LanguageOpts OptsFor(const char *triple) {
+  return llvm::cantFail(LanguageOpts::Create(llvm::Triple(triple)));
+}
+
+TEST(LanguageOptsTest, X86_64Sizes) {
+  LanguageOpts opts = OptsFor("x86_64-pc-linux-gnu");
+  const LanguageOpts::BuiltinSizes &sizes = opts.GetBuiltinSizes();
+  EXPECT_EQ(sizes.bool_size, 1u);
+  EXPECT_EQ(sizes.short_size, 2u);
+  EXPECT_EQ(sizes.int_size, 4u);
+  EXPECT_EQ(sizes.long_size, 8u);
+  EXPECT_EQ(sizes.long_long_size, 8u);
+  EXPECT_EQ(sizes.float_size, 4u);
+  EXPECT_EQ(sizes.double_size, 8u);
+  EXPECT_EQ(sizes.pointer_size, 8u);
+}
+
+TEST(LanguageOptsTest, I386Sizes) {
+  LanguageOpts opts = OptsFor("i386-pc-linux-gnu");
+  const LanguageOpts::BuiltinSizes &sizes = opts.GetBuiltinSizes();
+  EXPECT_EQ(sizes.long_size, 4u);
+  EXPECT_EQ(sizes.pointer_size, 4u);
+}
+
+TEST(LanguageOptsTest, CreateFailsForUnknownTriple) {
+  llvm::Expected<LanguageOpts> opts =
+      LanguageOpts::Create(llvm::Triple("totally-bogus-triple-value"));
+  EXPECT_FALSE(static_cast<bool>(opts));
+  llvm::consumeError(opts.takeError());
+}
+
+TEST(LanguageOptsTest, FloatTypeSemanticsBySize) {
+  LanguageOpts opts = OptsFor("x86_64-pc-linux-gnu");
+  EXPECT_EQ(&opts.GetFloatTypeSemantics(4, lldb::eFormatFloat),
+            &llvm::APFloat::IEEEsingle());
+  EXPECT_EQ(&opts.GetFloatTypeSemantics(8, lldb::eFormatFloat),
+            &llvm::APFloat::IEEEdouble());
+}
+
+TEST(LanguageOptsTest, FloatTypeSemanticsUnknownSizeIsBogus) {
+  LanguageOpts opts = OptsFor("x86_64-pc-linux-gnu");
+  EXPECT_EQ(&opts.GetFloatTypeSemantics(3, lldb::eFormatFloat),
+            &llvm::APFloat::Bogus());
+}
+
+TEST(LanguageOptsTest, BitIntByteSizeRoundsUpToAlignment) {
+  LanguageOpts opts = OptsFor("x86_64-pc-linux-gnu");
+  std::optional<uint64_t> size = opts.GetBitIntByteSize(1);
+  ASSERT_TRUE(size.has_value());
+  // A 1-bit _BitInt still occupies at least one byte.
+  EXPECT_GE(*size, 1u);
+
+  std::optional<uint64_t> size9 = opts.GetBitIntByteSize(9);
+  ASSERT_TRUE(size9.has_value());
+  // 9 bits need more than 1 byte of storage.
+  EXPECT_GT(*size9, 1u);
+}
+
+TEST(LanguageOptsTest, BitIntByteSizeZeroIsInvalid) {
+  LanguageOpts opts = OptsFor("x86_64-pc-linux-gnu");
+  EXPECT_FALSE(opts.GetBitIntByteSize(0).has_value());
+}



More information about the lldb-commits mailing list