about summary refs log tree commit diff
path: root/compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp
diff options
context:
space:
mode:
authorZalathar <Zalathar@users.noreply.github.com>2023-05-08 13:57:20 +1000
committerZalathar <Zalathar@users.noreply.github.com>2023-05-09 18:08:32 +1000
commit7cab196e7cccb68454e0ee5e667628387ad06463 (patch)
tree7b0bc611438a4d344c9ba44548cb60ad96d5eaa2 /compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp
parent33a01e2e93bfdd0c37649c6f82172644b8315b55 (diff)
downloadrust-7cab196e7cccb68454e0ee5e667628387ad06463.tar.gz
rust-7cab196e7cccb68454e0ee5e667628387ad06463.zip
Isolate coverage FFI type layouts from their underlying LLVM C++ types
Diffstat (limited to 'compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp')
-rw-r--r--compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp107
1 files changed, 100 insertions, 7 deletions
diff --git a/compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp b/compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp
index 03e6d2149e9..38fcd0527da 100644
--- a/compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp
+++ b/compiler/rustc_llvm/llvm-wrapper/CoverageMappingWrapper.cpp
@@ -8,18 +8,100 @@
 
 using namespace llvm;
 
+// FFI equivalent of enum `llvm::coverage::Counter::CounterKind`
+// https://github.com/rust-lang/llvm-project/blob/ea6fa9c2/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L97-L99
+enum class LLVMRustCounterKind {
+  Zero = 0,
+  CounterValueReference = 1,
+  Expression = 2,
+};
+
+// FFI equivalent of struct `llvm::coverage::Counter`
+// https://github.com/rust-lang/llvm-project/blob/ea6fa9c2/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L94-L149
+struct LLVMRustCounter {
+  LLVMRustCounterKind CounterKind;
+  uint32_t ID;
+};
+
+static coverage::Counter fromRust(LLVMRustCounter Counter) {
+  switch (Counter.CounterKind) {
+  case LLVMRustCounterKind::Zero:
+    return coverage::Counter::getZero();
+  case LLVMRustCounterKind::CounterValueReference:
+    return coverage::Counter::getCounter(Counter.ID);
+  case LLVMRustCounterKind::Expression:
+    return coverage::Counter::getExpression(Counter.ID);
+  }
+  report_fatal_error("Bad LLVMRustCounterKind!");
+}
+
+// FFI equivalent of enum `llvm::coverage::CounterMappingRegion::RegionKind`
+// https://github.com/rust-lang/llvm-project/blob/ea6fa9c2/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L213-L234
+enum class LLVMRustCounterMappingRegionKind {
+  CodeRegion = 0,
+  ExpansionRegion = 1,
+  SkippedRegion = 2,
+  GapRegion = 3,
+  BranchRegion = 4,
+};
+
+static coverage::CounterMappingRegion::RegionKind
+fromRust(LLVMRustCounterMappingRegionKind Kind) {
+  switch (Kind) {
+  case LLVMRustCounterMappingRegionKind::CodeRegion:
+    return coverage::CounterMappingRegion::CodeRegion;
+  case LLVMRustCounterMappingRegionKind::ExpansionRegion:
+    return coverage::CounterMappingRegion::ExpansionRegion;
+  case LLVMRustCounterMappingRegionKind::SkippedRegion:
+    return coverage::CounterMappingRegion::SkippedRegion;
+  case LLVMRustCounterMappingRegionKind::GapRegion:
+    return coverage::CounterMappingRegion::GapRegion;
+  case LLVMRustCounterMappingRegionKind::BranchRegion:
+    return coverage::CounterMappingRegion::BranchRegion;
+  }
+  report_fatal_error("Bad LLVMRustCounterMappingRegionKind!");
+}
+
+// FFI equivalent of struct `llvm::coverage::CounterMappingRegion`
+// https://github.com/rust-lang/llvm-project/blob/ea6fa9c2/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L211-L304
 struct LLVMRustCounterMappingRegion {
-  coverage::Counter Count;
-  coverage::Counter FalseCount;
+  LLVMRustCounter Count;
+  LLVMRustCounter FalseCount;
   uint32_t FileID;
   uint32_t ExpandedFileID;
   uint32_t LineStart;
   uint32_t ColumnStart;
   uint32_t LineEnd;
   uint32_t ColumnEnd;
-  coverage::CounterMappingRegion::RegionKind Kind;
+  LLVMRustCounterMappingRegionKind Kind;
+};
+
+// FFI equivalent of enum `llvm::coverage::CounterExpression::ExprKind`
+// https://github.com/rust-lang/llvm-project/blob/ea6fa9c2/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L154
+enum class LLVMRustCounterExprKind {
+  Subtract = 0,
+  Add = 1,
 };
 
+// FFI equivalent of struct `llvm::coverage::CounterExpression`
+// https://github.com/rust-lang/llvm-project/blob/ea6fa9c2/llvm/include/llvm/ProfileData/Coverage/CoverageMapping.h#L151-L160
+struct LLVMRustCounterExpression {
+  LLVMRustCounterExprKind Kind;
+  LLVMRustCounter LHS;
+  LLVMRustCounter RHS;
+};
+
+static coverage::CounterExpression::ExprKind
+fromRust(LLVMRustCounterExprKind Kind) {
+  switch (Kind) {
+  case LLVMRustCounterExprKind::Subtract:
+    return coverage::CounterExpression::Subtract;
+  case LLVMRustCounterExprKind::Add:
+    return coverage::CounterExpression::Add;
+  }
+  report_fatal_error("Bad LLVMRustCounterExprKind!");
+}
+
 extern "C" void LLVMRustCoverageWriteFilenamesSectionToBuffer(
     const char* const Filenames[],
     size_t FilenamesLen,
@@ -37,7 +119,7 @@ extern "C" void LLVMRustCoverageWriteFilenamesSectionToBuffer(
 extern "C" void LLVMRustCoverageWriteMappingToBuffer(
     const unsigned *VirtualFileMappingIDs,
     unsigned NumVirtualFileMappingIDs,
-    const coverage::CounterExpression *Expressions,
+    const LLVMRustCounterExpression *RustExpressions,
     unsigned NumExpressions,
     LLVMRustCounterMappingRegion *RustMappingRegions,
     unsigned NumMappingRegions,
@@ -48,13 +130,24 @@ extern "C" void LLVMRustCoverageWriteMappingToBuffer(
   for (const auto &Region : ArrayRef<LLVMRustCounterMappingRegion>(
            RustMappingRegions, NumMappingRegions)) {
     MappingRegions.emplace_back(
-        Region.Count, Region.FalseCount, Region.FileID, Region.ExpandedFileID,
+        fromRust(Region.Count), fromRust(Region.FalseCount),
+        Region.FileID, Region.ExpandedFileID,
         Region.LineStart, Region.ColumnStart, Region.LineEnd, Region.ColumnEnd,
-        Region.Kind);
+        fromRust(Region.Kind));
   }
+
+  std::vector<coverage::CounterExpression> Expressions;
+  Expressions.reserve(NumExpressions);
+  for (const auto &Expression :
+       ArrayRef<LLVMRustCounterExpression>(RustExpressions, NumExpressions)) {
+    Expressions.emplace_back(fromRust(Expression.Kind),
+                             fromRust(Expression.LHS),
+                             fromRust(Expression.RHS));
+  }
+
   auto CoverageMappingWriter = coverage::CoverageMappingWriter(
       ArrayRef<unsigned>(VirtualFileMappingIDs, NumVirtualFileMappingIDs),
-      ArrayRef<coverage::CounterExpression>(Expressions, NumExpressions),
+      Expressions,
       MappingRegions);
   RawRustStringOstream OS(BufferOut);
   CoverageMappingWriter.write(OS);