Skip to content

Commit 3b3106a

Browse files
authored
Add parameter checks and tests for IntructionBuilder.IntToPointer (#391)
Fixes #384 * Updated types to add parameter checks and report problems early * Updated .editorconfig to describe problems with Broken GUI editor
1 parent 56f9b2f commit 3b3106a

File tree

3 files changed

+79
-6
lines changed

3 files changed

+79
-6
lines changed

.editorconfig

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,20 @@
11
#Primary settings apply to all files unless overridden below
2+
# NOTE: Just opening this file in the VS UI editor will make changes that are
3+
# not intended.
4+
#
5+
# DO-NOT allow such changes!
6+
#
7+
# Unfortunately, this requires a great deal of discipline and manual checking
8+
# of differences to ensure the changes made ONLY contains intentional changes
9+
# that really apply to all users of this repository.
10+
#
11+
# see: https://github.com/dotnet/roslyn/issues/59325
12+
#
13+
# Unfortunately, that has remained open for nearly 4 years, so not likely to
14+
# get a fix any time soon...
15+
16+
# Analysis and refactoring rules for Ubiquity.NET
17+
# Description: Code analysis rules for Ubiquity.NET projects
218
root = true
319

420
[*]
@@ -108,11 +124,6 @@ csharp_style_prefer_local_over_anonymous_function = true:error
108124
csharp_style_prefer_index_operator = true:error
109125
csharp_style_prefer_range_operator = true:error
110126

111-
# Analysis and refactoring rules for Ubiquity.NET
112-
# Description: Code analysis rules for Ubiquity.NET projects
113-
114-
# NOTE: Requires **VS2019 16.3** or later
115-
116127
# Code files
117128
[*.{cs,vb}]
118129

@@ -1616,6 +1627,5 @@ dotnet_diagnostic.SA1652.severity = warning
16161627

16171628
dotnet_diagnostic.SX1101.severity = error
16181629

1619-
16201630
# IDE0045: Convert to conditional expression
16211631
dotnet_diagnostic.IDE0045.severity = suggestion
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright (c) Ubiquity.NET Contributors. All rights reserved.
2+
// Licensed under the Apache-2.0 WITH LLVM-exception license. See the LICENSE.md file in the project root for full license information.
3+
4+
using System;
5+
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
8+
using Ubiquity.NET.Llvm.Instructions;
9+
using Ubiquity.NET.Llvm.Values;
10+
11+
namespace Ubiquity.NET.Llvm.UT.Instructions
12+
{
13+
[TestClass]
14+
public class PointerCasts
15+
{
16+
#pragma warning disable CS8625 // Cannot convert null literal to non-nullable reference type.
17+
[TestMethod]
18+
public void IntToPointer_throws_with_invalid_input( )
19+
{
20+
using var ctx = new Context();
21+
using var module = ctx.CreateBitcodeModule("test"u8);
22+
var doulbeFuncType = ctx.GetFunctionType(ctx.DoubleType);
23+
var doubleFunc = module.CreateFunction("test"u8, doulbeFuncType);
24+
var block = ctx.CreateBasicBlock("testBlock"u8);
25+
26+
using var irBuilder = new InstructionBuilder(block);
27+
Value nonConstValue = irBuilder.Call(doubleFunc);
28+
Value nonIntConstantValue = ctx.CreateConstant(1.23);
29+
30+
var ptrBoolType = ctx.BoolType.CreatePointerType();
31+
var constInt = ctx.CreateConstant(0x1234u);
32+
var argNullEx = Assert.ThrowsExactly<ArgumentNullException>(()=>
33+
{
34+
_ = irBuilder.IntToPointer( null, ptrBoolType );
35+
});
36+
Assert.AreEqual( "intValue", argNullEx.ParamName);
37+
38+
argNullEx = Assert.ThrowsExactly<ArgumentNullException>(()=>
39+
{
40+
_ = irBuilder.IntToPointer( constInt, null );
41+
});
42+
Assert.AreEqual( "ptrType", argNullEx.ParamName );
43+
44+
var argEx = Assert.ThrowsExactly<ArgumentException>( ( ) =>
45+
{
46+
_ = irBuilder.IntToPointer( nonIntConstantValue, ptrBoolType );
47+
} );
48+
Assert.AreEqual( "intValue", argEx.ParamName );
49+
50+
argEx = Assert.ThrowsExactly<ArgumentException>( ( ) =>
51+
{
52+
_ = irBuilder.IntToPointer( nonConstValue, ptrBoolType );
53+
} );
54+
Assert.AreEqual( "intValue", argEx.ParamName );
55+
}
56+
#pragma warning restore CS8625 // Cannot convert null literal to non-nullable reference type.
57+
}
58+
}

src/Ubiquity.NET.Llvm/Instructions/InstructionBuilder.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,11 @@ public Value IntToPointer( Value intValue, IPointerType ptrType )
920920
ArgumentNullException.ThrowIfNull( intValue );
921921
ArgumentNullException.ThrowIfNull( ptrType );
922922

923+
if(!intValue.NativeType.IsInteger)
924+
{
925+
throw new ArgumentException( Resources.Expecting_an_integer_type, nameof( intValue ) );
926+
}
927+
923928
var handle = (intValue is Constant)
924929
? LLVMConstIntToPtr( intValue.Handle, ptrType.GetTypeRef( ) )
925930
: LLVMBuildIntToPtr( Handle, intValue.Handle, ptrType.GetTypeRef( ), LazyEncodedString.Empty );

0 commit comments

Comments
 (0)