Skip to content

Fix #178#179

Closed
ryan-moreno wants to merge 4 commits intodotnet:mainfrom
ryan-moreno:main
Closed

Fix #178#179
ryan-moreno wants to merge 4 commits intodotnet:mainfrom
ryan-moreno:main

Conversation

@ryan-moreno
Copy link
Copy Markdown
Contributor

Fixes the bug where the LLVMModuleFlagBehavior enum values are all off by 1 (#178 (comment)). Also creates a unit test to make sure that the enums aren't off by 1 by checking that LLVM.ModuleFlagBehaviorWarning doesn't cause an Error (which does happen in the existing version of LLVMSharp). There is a comment at the bottom of the unit test describing a way to improve the test if someone else knows how to implement this.

@ryan-moreno ryan-moreno changed the title Fix for Issue 1051246758 Fix #1051246758 Nov 11, 2021
@ryan-moreno ryan-moreno changed the title Fix #1051246758 Fix #179 Nov 11, 2021
@ryan-moreno ryan-moreno changed the title Fix #179 Fix #178 Nov 11, 2021
Copy link
Copy Markdown
Member

@tannergooding tannergooding left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As per #178 (comment), there is no bug in the LLVMModuleFlagBehavior definition.

This enum matches exactly with the underlying enum defined in the LLVM-C source code.

The bug here is assuming that the C and C++ versions of the enum are identical and are 1-to-1 blittable to eachother.

Comment on lines +36 to +66
[Test(Description = "Tests that LLVMModuleFlagBehavior is properly defined as a 0-based enum for use in AddModuleFlag")]
/// <summary>Verifies that <see cref="LLVMModuleFlagBehavior"/> is defined as expected by <see cref="AddModuleFlag"/>
/// so that there isn't unexpected behavior when modules with conflicting flags are linked.
/// This test is relevant because AddModuleFlag expects a 0-based behavior enum (where 1 defines warning),
/// while the other method of adding a module flag (AddNamedMetadataOperand) expects a 1-based behavior
/// (where 1 defines error).</summary>
public static void TestBehaviorOfLinkingConflictingModules()
{
// set up two modules
LLVMModuleRef module1 = LLVMModuleRef.CreateWithName("module1");
LLVMModuleRef module2 = LLVMModuleRef.CreateWithName("module2");

// Add conflicting module flags to module1 and module2 using LLVMModuleFlagBehavior.LLVMModuleFlagBehaviorWarning
// which should only cause a warning, and not an error
string flagName = "madeUpFlagName";
uint value1 = 1;
uint value2 = 2;

module1.AddModuleFlag(flagName, LLVMModuleFlagBehavior.LLVMModuleFlagBehaviorWarning, value1);
module2.AddModuleFlag(flagName, LLVMModuleFlagBehavior.LLVMModuleFlagBehaviorWarning, value2);

// linking the modules should cause a warning, not an error
try
{
LLVM.LinkModules2(module1, module2);
}
catch (AccessViolationException)
{
// Fail doesn't actually get called. The test will simply be aborted if we reach this point, but the catch block cleans up the error output
Assert.Fail("Conflicting module flags that were defined with LLVMModuleFlagBehaviorWarning should not have caused an error.");
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test needs to be put in an LLVMOpaqueModuleTests.Manual.cs file. The main file is autogenerated and so this test will get trashed every time bindings get regenerated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants