|
| 1 | +using System; |
| 2 | +using System.IO; |
| 3 | +using LLVMSharp.Interop; |
| 4 | +using NUnit.Framework; |
| 5 | + |
| 6 | +namespace LLVMSharp.UnitTests |
| 7 | +{ |
| 8 | + public class DIBuilder |
| 9 | + { |
| 10 | + [Test(Description = "Exercises some DIBuilder functions, does not test the actual debug information is correct")] |
| 11 | + public void CreateDebugLocation() |
| 12 | + { |
| 13 | + string fileName = Path.GetFileName("DIBuilder.c"); |
| 14 | + string directory = Path.GetDirectoryName("."); |
| 15 | + LLVMModuleRef module = LLVMModuleRef.CreateWithName("netscripten"); |
| 16 | + module.Target = "asmjs-unknown-emscripten"; |
| 17 | + var dIBuilder = module.CreateDIBuilder(); |
| 18 | + var builder = module.Context.CreateBuilder(); |
| 19 | + LLVMMetadataRef fileMetadata = dIBuilder.CreateFile(fileName, directory); |
| 20 | + |
| 21 | + LLVMMetadataRef compileUnitMetadata = dIBuilder.CreateCompileUnit( |
| 22 | + LLVMDWARFSourceLanguage.LLVMDWARFSourceLanguageC, |
| 23 | + fileMetadata, "ILC", 0 /* Optimized */, String.Empty, 1, String.Empty, |
| 24 | + LLVMDWARFEmissionKind.LLVMDWARFEmissionFull, 0, 0, 0); |
| 25 | + module.AddNamedMetadataOperand("llvm.dbg.cu", compileUnitMetadata); |
| 26 | + |
| 27 | + LLVMMetadataRef functionMetaType = dIBuilder.CreateSubroutineType(fileMetadata, |
| 28 | + ReadOnlySpan<LLVMMetadataRef>.Empty, LLVMDIFlags.LLVMDIFlagZero); |
| 29 | + |
| 30 | + uint lineNumber = 1; |
| 31 | + var debugFunction = dIBuilder.CreateFunction(fileMetadata, "CreateDebugLocation", "CreateDebugLocation", |
| 32 | + fileMetadata, |
| 33 | + lineNumber, functionMetaType, 1, 1, lineNumber, 0, 0); |
| 34 | + LLVMMetadataRef currentLine = |
| 35 | + module.Context.CreateDebugLocation(lineNumber, 0, debugFunction, default(LLVMMetadataRef)); |
| 36 | + |
| 37 | + LLVMTypeRef[] FooParamTys = {LLVMTypeRef.Int64, LLVMTypeRef.Int64,}; |
| 38 | + LLVMTypeRef FooFuncTy = LLVMTypeRef.CreateFunction(LLVMTypeRef.Int64, FooParamTys); |
| 39 | + LLVMValueRef FooFunction = module.AddFunction("foo", FooFuncTy); |
| 40 | + |
| 41 | + var funcBlock = module.Context.AppendBasicBlock(FooFunction, "foo"); |
| 42 | + builder.PositionAtEnd(funcBlock); |
| 43 | + builder.BuildRet(LLVMValueRef.CreateConstInt(LLVMTypeRef.Int64, 0)); |
| 44 | + builder.CurrentDebugLocation = module.Context.MetadataAsValue(currentLine); |
| 45 | + var dwarfVersion = LLVMValueRef.CreateMDNode(new[] |
| 46 | + { |
| 47 | + LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, 2), module.Context.GetMDString("Dwarf Version", 13), |
| 48 | + LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, 4) |
| 49 | + }); |
| 50 | + var dwarfSchemaVersion = LLVMValueRef.CreateMDNode(new[] |
| 51 | + { |
| 52 | + LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, 2), |
| 53 | + module.Context.GetMDString("Debug Info Version", 18), |
| 54 | + LLVMValueRef.CreateConstInt(LLVMTypeRef.Int32, 3) |
| 55 | + }); |
| 56 | + module.AddNamedMetadataOperand("llvm.module.flags", dwarfVersion); |
| 57 | + module.AddNamedMetadataOperand("llvm.module.flags", dwarfSchemaVersion); |
| 58 | + dIBuilder.DIBuilderFinalize(); |
| 59 | + |
| 60 | + module.TryVerify(LLVMVerifierFailureAction.LLVMPrintMessageAction, out string message); |
| 61 | + |
| 62 | + Assert.AreEqual("", message); |
| 63 | + } |
| 64 | + } |
| 65 | +} |
0 commit comments