Skip to content

Commit

Permalink
Remove boundary/break error handling... (#324)
Browse files Browse the repository at this point in the history
Something weird is happening - unhandled CPP exception being thrown
  • Loading branch information
keynmol authored Jan 5, 2025
1 parent 047da98 commit 6068826
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 104 deletions.
7 changes: 6 additions & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import scala.scalanative.build.LTO
import commandmatrix.extra.*
import com.indoorvivants.detective.Platform
import Platform.*
import scala.scalanative.build.*

import _root_.bindgen.interface.*
import ArtifactNames.*
Expand Down Expand Up @@ -118,7 +119,10 @@ lazy val bindgen = project
.settings(nativeCommon)
.settings(Compile / nativeConfig ~= environmentConfiguration)
.settings(nativeConfig ~= usesLibClang)
.settings(nativeConfig ~= (_.withIncrementalCompilation(true)))
.settings(
nativeConfig ~= (_.withIncrementalCompilation(true)
.withSourceLevelDebuggingConfig(SourceLevelDebuggingConfig.enabled))
)
.settings(remoteCacheSettings)
.settings(
buildInfoPackage := "bindgen",
Expand Down Expand Up @@ -798,6 +802,7 @@ val scalafixRules = Seq(
).mkString(" ")

val PrepareCICommands = Seq(
"scalafixEnable",
s"scalafix --rules $scalafixRules",
"scalafmtAll",
"scalafmtSbt"
Expand Down
125 changes: 61 additions & 64 deletions modules/bindgen/src/main/scala/analysis/BindingGenerator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,78 +6,75 @@ import scala.util.boundary

import fluent.*
import scalanative.unsafe.*
import boundary.Label

object BindingGenerator:
def run(context: Context, environment: ConfiguredEnvironment)(using Zone)(
using config: Config
): Either[BindingError, Binding] =
boundary:
val index = clang_createIndex(0, 0)

val index = clang_createIndex(0, 0)
ClangTranslationUnit
.create(index, context.headerFile.value, environment.clang)
.flatMap: translationUnit =>

val (cxClientData, memory) =
Captured.unsafe(environment.systemHeaderDetector -> BindingBuilder())

val unit =
ClangTranslationUnit
.create(index, context.headerFile.value, environment.clang)
.?

val (cxClientData, memory) =
Captured.unsafe(environment.systemHeaderDetector -> BindingBuilder())

val translationUnitCursor = ClangTranslationUnit.getCursor(unit).?

val visitor = ClangVisitor.visitor

try
libclang.fluent.clang_visitChildren(
translationUnitCursor,
visitor,
CXClientData.wrap(cxClientData)
)

val binding: BindingBuilder = (!cxClientData)._1._2

trace(
"Binding information before adding system aliases:",
binding.named.toList.sortBy(_._1.n).map { case (k, v) =>
k.toString -> v
}
)

addBuiltInAliases(binding)

trace(
"Binding information after adding system alises:",
binding.named.toList.sortBy(_._1.n).map { case (k, v) =>
k.toString -> v
}
)
val closure = computeClosure(binding.named.filter { n =>
val name = n._1.n

if config.exclusivePrefix.isEmpty then true
else config.exclusivePrefix.exists(ep => name.startsWith(ep.value))
}.toMap)

trace(s"Defined or used in main file: ${closure}")

binding.named.filterInPlace((k, _) => closure.contains(k.n))
binding.unnamed.filterInPlace(_.location.shouldBeIncluded)

trace(
"Binding information after computing closure:",
binding.named.toList.sortBy(_._1.n).map { case (k, v) =>
k.toString -> v
}
)

Right(binding.build)
finally
memory.deallocate()
clang_disposeTranslationUnit(unit)
clang_disposeIndex(index)

end try
.getCursor(translationUnit)
.flatMap: translationUnitCursor =>
val visitor = ClangVisitor.visitor

try
libclang.fluent.clang_visitChildren(
translationUnitCursor,
visitor,
CXClientData.wrap(cxClientData)
)

val binding: BindingBuilder = (!cxClientData)._1._2

trace(
"Binding information before adding system aliases:",
binding.named.toList.sortBy(_._1.n).map { case (k, v) =>
k.toString -> v
}
)

addBuiltInAliases(binding)

trace(
"Binding information after adding system alises:",
binding.named.toList.sortBy(_._1.n).map { case (k, v) =>
k.toString -> v
}
)
val closure = computeClosure(binding.named.filter { n =>
val name = n._1.n

if config.exclusivePrefix.isEmpty then true
else
config.exclusivePrefix.exists(ep => name.startsWith(ep.value))
}.toMap)

trace(s"Defined or used in main file: ${closure}")

binding.named.filterInPlace((k, _) => closure.contains(k.n))
binding.unnamed.filterInPlace(_.location.shouldBeIncluded)

trace(
"Binding information after computing closure:",
binding.named.toList.sortBy(_._1.n).map { case (k, v) =>
k.toString -> v
}
)

Right(binding.build)
finally
memory.deallocate()
clang_disposeTranslationUnit(translationUnit)
clang_disposeIndex(index)
end try

end run
end BindingGenerator
16 changes: 6 additions & 10 deletions modules/bindgen/src/main/scala/analysis/ClangTranslationUnit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import scala.util.boundary

import scalanative.unsafe.*
import scalanative.unsigned.*
import boundary.break
import boundary.Label

object ClangTranslationUnit:
def create(
Expand Down Expand Up @@ -49,9 +47,9 @@ object ClangTranslationUnit:
CXTranslationUnit_Flags.CXTranslationUnit_None.uint
)

boundary:
if unit == null.asInstanceOf[CXTranslationUnit] then
break(Left(BindingError.FailedToCreateTranslationUnit))
if unit == null.asInstanceOf[CXTranslationUnit] then
Left(BindingError.FailedToCreateTranslationUnit)
else

trace("Successfully created a translation unit")

Expand All @@ -69,13 +67,11 @@ object ClangTranslationUnit:

if diag.severity == ClangSeverity.Error || diag.severity == ClangSeverity.Fatal
then errors += 1

}

if errors != 0 then
break(Left(BindingError.ClangErrors(diagnostics.toList)))

Right(unit)
if errors != 0 then Left(BindingError.ClangErrors(diagnostics.toList))
else Right(unit)
end if
end create

def getCursor(cursor: CXTranslationUnit)(using Zone) =
Expand Down
28 changes: 11 additions & 17 deletions modules/bindgen/src/main/scala/analysis/InteractiveDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import bindgen.rendering.{RenderedOutput, renderBinding}
import scala.scalanative.unsafe.Zone
import scala.util.boundary

import boundary.Label

case class ConfiguredEnvironment(
clang: ClangInfo,
systemHeaderDetector: SystemHeaderDetector
Expand All @@ -21,25 +19,21 @@ class InteractiveDriver(config: Config, environment: ConfiguredEnvironment):
def createBinding(context: Context, outputMode: OutputMode)(using
Zone
): Either[BindingError, RenderedOutput] =
boundary:
val binding = analyse(context).?
analyse(context).map: binding =>
given Context = context
Right(renderBinding(binding, outputMode))
renderBinding(binding, outputMode)
end InteractiveDriver

object InteractiveDriver:
def init(using Config, Zone): Either[BindingError, InteractiveDriver] =
boundary:
val config = summon[Config]
val clang = clangInfo(config.systemPathDetection).?

Right(
InteractiveDriver(
config,
ConfiguredEnvironment(
clang,
SystemHeaderDetector(clang, config.excludeSystemPaths)
)
)
val config = summon[Config]
for clang <- clangInfo(config.systemPathDetection)
yield InteractiveDriver(
config,
ConfiguredEnvironment(
clang,
SystemHeaderDetector(clang, config.excludeSystemPaths)
)
)
end init
end InteractiveDriver
12 changes: 0 additions & 12 deletions modules/bindgen/src/main/scala/analysis/syntax.scala

This file was deleted.

0 comments on commit 6068826

Please sign in to comment.