How to read serialized diagnostics in Swift

By setting the -serialize-diagnostics-path option to the compiler, I can get a binary file containing serialized diagnostics. I would like to programmatically process the content using Swift code, but don't know where to start.

I tried reading lib/Frontend/SerializedDiagnosticConsumer.cpp in order to find the data written to the diagnostics file. I somewhat understand what is in the output, but I can't figure out the structure of it. I'm quite bad at reading C++. :(

Could someone point me to the right direction and help me understand it?

1 Like

.dia files use the LLVM bitstream container format, which is described here: LLVM Bitcode File Format β€” LLVM 16.0.0git documentation

swiftdeps files also use the bitstream format, so swift-tools-support core recently got support for reading it, you can check out Bitstream.swift in TSCUtility. TSCUtility also has SerializedDiagnostics.swift which builds on it to read the diagnostics format, but that part isn’t as widely used yet so might not be as reliable.

3 Likes

@wowbagger have you had any luck with parsing?

I've had success reading .dia files generated by swift build -Xswiftc -serialize-diagnostics using the SerializedDiagnostics struct that @owenv mentioned. I was able to load my .dia file as a ByteString with this other method from TSC.

I was not able to read .dia files generated by building an xcode project. I just get badMagic error.