[flang] [llvm] [flang/flang-rt] Implement show_descriptor intrinsic, a non-standard extension. (PR #170389)

Slava Zakharin via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 2 15:27:15 PST 2025


================
@@ -158,3 +159,125 @@ TEST(Descriptor, FixedStride) {
   EXPECT_TRUE(descriptor.IsContiguous());
   EXPECT_EQ(descriptor.FixedStride().value_or(-666), 0);
 }
+
+// The test below uses file operations that have nuances across multiple
+// platforms. Hence limit coverage by linux only unless wider coverage
+// should be required.
+#if defined(__linux__) && !defined(__ANDROID__)
+TEST(Descriptor, Dump) {
+  StaticDescriptor<4> staticDesc[2];
+  Descriptor &descriptor{staticDesc[0].descriptor()};
+  using Type = std::int32_t;
+  Type data[8][8][8];
+  constexpr int four{static_cast<int>(sizeof data[0][0][0])};
+  TypeCode integer{TypeCategory::Integer, four};
+  // Scalar
+  descriptor.Establish(integer, four, data, 0);
+  FILE *tmpf = nullptr;
+  tmpf = tmpfile();
+  ASSERT_TRUE(tmpf) << "tmpfile returned NULL";
+  auto resetTmpFile = [tmpf]() {
+    fflush(tmpf);
+    rewind(tmpf);
+    ftruncate(fileno(tmpf), 0);
+  };
+
+  auto getAddrFilteredContent = [tmpf]() -> std::string {
+    rewind(tmpf);
+    std::ostringstream content;
+    char buffer[1024];
+    size_t bytes_read;
+    while ((bytes_read = fread(buffer, 1, sizeof(buffer), tmpf)) > 0) {
+      content.write(buffer, bytes_read);
+    }
+
+    return std::regex_replace(
+        std::regex_replace(content.str(), std::regex("Descriptor @.*:"),
+            "Descriptor @ [addr]:"),
+        std::regex("base_addr .*"), "base_addr [addr]");
+  };
+
+  descriptor.Dump(tmpf, /*dumpRawType=*/false);
+  // also dump as CFI type
+  descriptor.Dump(tmpf, /*dumpRawType=*/true);
+  std::string output = getAddrFilteredContent();
----------------
vzakhari wrote:

```suggestion
  std::string output{getAddrFilteredContent()};
```

https://github.com/llvm/llvm-project/pull/170389


More information about the llvm-commits mailing list