Skip to content

Commit

Permalink
Mark ternary macros that can be evaluated as constants as const (#221)
Browse files Browse the repository at this point in the history
Previously, all conditionals were marked as not constant. However, if the Cond, LHS and RHS are all constant, the ternary can be marked constant as well
  • Loading branch information
ThadHouse authored Apr 8, 2021
1 parent dd684be commit cbe1fb3
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2848,7 +2848,8 @@ private bool IsConstant(Expr initExpr)

case CX_StmtClass.CX_StmtClass_ConditionalOperator:
{
return false;
var conditionalOperator = (ConditionalOperator)initExpr;
return IsConstant(conditionalOperator.Cond) && IsConstant(conditionalOperator.LHS) && IsConstant(conditionalOperator.RHS);
}

// case CX_StmtClass.CX_StmtClass_AddrLabelExpr:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,8 @@ public abstract class VarDeclarationTest : PInvokeGeneratorTest

[Fact]
public abstract Task MultidimensionlArrayTest();

[Fact]
public abstract Task ConditionalDefineConstTest();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -298,5 +298,25 @@ public static partial class Methods

return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents);
}

public override Task ConditionalDefineConstTest()
{
var inputContents = @"typedef int TESTRESULT;
#define TESTRESULT_FROM_WIN32(x) ((TESTRESULT)(x) <= 0 ? ((TESTRESULT)(x)) : ((TESTRESULT) (((x) & 0x0000FFFF) | (7 << 16) | 0x80000000)))
#define ADDRESS_IN_USE TESTRESULT_FROM_WIN32(10048)";

var expectedOutputContents = $@"namespace ClangSharp.Test
{{
public static partial class Methods
{{
[NativeTypeName(""#define ADDRESS_IN_USE TESTRESULT_FROM_WIN32(10048)"")]
public const int ADDRESS_IN_USE = unchecked((int)(10048) <= 0 ? ((int)(10048)) : ((int)(((10048) & 0x0000FFFF) | (7 << 16) | 0x80000000)));
}}
}}
";
var diagnostics = new Diagnostic[] { new Diagnostic(DiagnosticLevel.Warning, "Function like macro definition records are not supported: 'TESTRESULT_FROM_WIN32'. Generated bindings may be incomplete.", "Line 2, Column 9 in ClangUnsavedFile.h") };

return ValidateGeneratedCSharpCompatibleUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -307,5 +307,25 @@ public static partial class Methods

return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents);
}

public override Task ConditionalDefineConstTest()
{
var inputContents = @"typedef int TESTRESULT;
#define TESTRESULT_FROM_WIN32(x) ((TESTRESULT)(x) <= 0 ? ((TESTRESULT)(x)) : ((TESTRESULT) (((x) & 0x0000FFFF) | (7 << 16) | 0x80000000)))
#define ADDRESS_IN_USE TESTRESULT_FROM_WIN32(10048)";

var expectedOutputContents = $@"namespace ClangSharp.Test
{{
public static partial class Methods
{{
[NativeTypeName(""#define ADDRESS_IN_USE TESTRESULT_FROM_WIN32(10048)"")]
public const int ADDRESS_IN_USE = unchecked((int)(10048) <= 0 ? ((int)(10048)) : ((int)(((10048) & 0x0000FFFF) | (7 << 16) | 0x80000000)));
}}
}}
";
var diagnostics = new Diagnostic[] { new Diagnostic(DiagnosticLevel.Warning, "Function like macro definition records are not supported: 'TESTRESULT_FROM_WIN32'. Generated bindings may be incomplete.", "Line 2, Column 9 in ClangUnsavedFile.h") };

return ValidateGeneratedCSharpCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -294,5 +294,25 @@ public static partial class Methods

return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents);
}

public override Task ConditionalDefineConstTest()
{
var inputContents = @"typedef int TESTRESULT;
#define TESTRESULT_FROM_WIN32(x) ((TESTRESULT)(x) <= 0 ? ((TESTRESULT)(x)) : ((TESTRESULT) (((x) & 0x0000FFFF) | (7 << 16) | 0x80000000)))
#define ADDRESS_IN_USE TESTRESULT_FROM_WIN32(10048)";

var expectedOutputContents = $@"namespace ClangSharp.Test
{{
public static partial class Methods
{{
[NativeTypeName(""#define ADDRESS_IN_USE TESTRESULT_FROM_WIN32(10048)"")]
public const int ADDRESS_IN_USE = unchecked((int)(10048) <= 0 ? ((int)(10048)) : ((int)(((10048) & 0x0000FFFF) | (7 << 16) | 0x80000000)));
}}
}}
";
var diagnostics = new Diagnostic[] { new Diagnostic(DiagnosticLevel.Warning, "Function like macro definition records are not supported: 'TESTRESULT_FROM_WIN32'. Generated bindings may be incomplete.", "Line 2, Column 9 in ClangUnsavedFile.h") };

return ValidateGeneratedCSharpLatestUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -307,5 +307,25 @@ public static partial class Methods

return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents);
}

public override Task ConditionalDefineConstTest()
{
var inputContents = @"typedef int TESTRESULT;
#define TESTRESULT_FROM_WIN32(x) ((TESTRESULT)(x) <= 0 ? ((TESTRESULT)(x)) : ((TESTRESULT) (((x) & 0x0000FFFF) | (7 << 16) | 0x80000000)))
#define ADDRESS_IN_USE TESTRESULT_FROM_WIN32(10048)";

var expectedOutputContents = $@"namespace ClangSharp.Test
{{
public static partial class Methods
{{
[NativeTypeName(""#define ADDRESS_IN_USE TESTRESULT_FROM_WIN32(10048)"")]
public const int ADDRESS_IN_USE = unchecked((int)(10048) <= 0 ? ((int)(10048)) : ((int)(((10048) & 0x0000FFFF) | (7 << 16) | 0x80000000)));
}}
}}
";
var diagnostics = new Diagnostic[] { new Diagnostic(DiagnosticLevel.Warning, "Function like macro definition records are not supported: 'TESTRESULT_FROM_WIN32'. Generated bindings may be incomplete.", "Line 2, Column 9 in ClangUnsavedFile.h") };

return ValidateGeneratedCSharpLatestWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -381,5 +381,32 @@ public override Task MultidimensionlArrayTest()

return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents);
}

public override Task ConditionalDefineConstTest()
{
var inputContents = @"typedef int TESTRESULT;
#define TESTRESULT_FROM_WIN32(x) ((TESTRESULT)(x) <= 0 ? ((TESTRESULT)(x)) : ((TESTRESULT) (((x) & 0x0000FFFF) | (7 << 16) | 0x80000000)))
#define ADDRESS_IN_USE TESTRESULT_FROM_WIN32(10048)";

var expectedOutputContents = $@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>
<bindings>
<namespace name=""ClangSharp.Test"">
<class name=""Methods"" access=""public"" static=""true"">
<constant name=""ADDRESS_IN_USE"" access=""public"">
<type primitive=""True"">int</type>
<value>
<unchecked>
<code>((int)(10048) &lt;= 0 ? ((int)(10048)) : ((int)(((10048) &amp; 0x0000FFFF) | (7 &lt;&lt; 16) | 0x80000000)))</code>
</unchecked>
</value>
</constant>
</class>
</namespace>
</bindings>
";
var diagnostics = new Diagnostic[] { new Diagnostic(DiagnosticLevel.Warning, "Function like macro definition records are not supported: 'TESTRESULT_FROM_WIN32'. Generated bindings may be incomplete.", "Line 2, Column 9 in ClangUnsavedFile.h") };

return ValidateGeneratedXmlCompatibleUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -403,5 +403,32 @@ public override Task MultidimensionlArrayTest()

return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents);
}

public override Task ConditionalDefineConstTest()
{
var inputContents = @"typedef int TESTRESULT;
#define TESTRESULT_FROM_WIN32(x) ((TESTRESULT)(x) <= 0 ? ((TESTRESULT)(x)) : ((TESTRESULT) (((x) & 0x0000FFFF) | (7 << 16) | 0x80000000)))
#define ADDRESS_IN_USE TESTRESULT_FROM_WIN32(10048)";

var expectedOutputContents = $@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>
<bindings>
<namespace name=""ClangSharp.Test"">
<class name=""Methods"" access=""public"" static=""true"">
<constant name=""ADDRESS_IN_USE"" access=""public"">
<type primitive=""True"">int</type>
<value>
<unchecked>
<code>((int)(10048) &lt;= 0 ? ((int)(10048)) : ((int)(((10048) &amp; 0x0000FFFF) | (7 &lt;&lt; 16) | 0x80000000)))</code>
</unchecked>
</value>
</constant>
</class>
</namespace>
</bindings>
";
var diagnostics = new Diagnostic[] { new Diagnostic(DiagnosticLevel.Warning, "Function like macro definition records are not supported: 'TESTRESULT_FROM_WIN32'. Generated bindings may be incomplete.", "Line 2, Column 9 in ClangUnsavedFile.h") };

return ValidateGeneratedXmlCompatibleWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -385,5 +385,32 @@ public override Task MultidimensionlArrayTest()

return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents);
}

public override Task ConditionalDefineConstTest()
{
var inputContents = @"typedef int TESTRESULT;
#define TESTRESULT_FROM_WIN32(x) ((TESTRESULT)(x) <= 0 ? ((TESTRESULT)(x)) : ((TESTRESULT) (((x) & 0x0000FFFF) | (7 << 16) | 0x80000000)))
#define ADDRESS_IN_USE TESTRESULT_FROM_WIN32(10048)";

var expectedOutputContents = $@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>
<bindings>
<namespace name=""ClangSharp.Test"">
<class name=""Methods"" access=""public"" static=""true"">
<constant name=""ADDRESS_IN_USE"" access=""public"">
<type primitive=""True"">int</type>
<value>
<unchecked>
<code>((int)(10048) &lt;= 0 ? ((int)(10048)) : ((int)(((10048) &amp; 0x0000FFFF) | (7 &lt;&lt; 16) | 0x80000000)))</code>
</unchecked>
</value>
</constant>
</class>
</namespace>
</bindings>
";
var diagnostics = new Diagnostic[] { new Diagnostic(DiagnosticLevel.Warning, "Function like macro definition records are not supported: 'TESTRESULT_FROM_WIN32'. Generated bindings may be incomplete.", "Line 2, Column 9 in ClangUnsavedFile.h") };

return ValidateGeneratedXmlLatestUnixBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -403,5 +403,32 @@ public override Task MultidimensionlArrayTest()

return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents);
}

public override Task ConditionalDefineConstTest()
{
var inputContents = @"typedef int TESTRESULT;
#define TESTRESULT_FROM_WIN32(x) ((TESTRESULT)(x) <= 0 ? ((TESTRESULT)(x)) : ((TESTRESULT) (((x) & 0x0000FFFF) | (7 << 16) | 0x80000000)))
#define ADDRESS_IN_USE TESTRESULT_FROM_WIN32(10048)";

var expectedOutputContents = $@"<?xml version=""1.0"" encoding=""UTF-8"" standalone=""yes"" ?>
<bindings>
<namespace name=""ClangSharp.Test"">
<class name=""Methods"" access=""public"" static=""true"">
<constant name=""ADDRESS_IN_USE"" access=""public"">
<type primitive=""True"">int</type>
<value>
<unchecked>
<code>((int)(10048) &lt;= 0 ? ((int)(10048)) : ((int)(((10048) &amp; 0x0000FFFF) | (7 &lt;&lt; 16) | 0x80000000)))</code>
</unchecked>
</value>
</constant>
</class>
</namespace>
</bindings>
";
var diagnostics = new Diagnostic[] { new Diagnostic(DiagnosticLevel.Warning, "Function like macro definition records are not supported: 'TESTRESULT_FROM_WIN32'. Generated bindings may be incomplete.", "Line 2, Column 9 in ClangUnsavedFile.h") };

return ValidateGeneratedXmlLatestWindowsBindingsAsync(inputContents, expectedOutputContents, expectedDiagnostics: diagnostics);
}
}
}

0 comments on commit cbe1fb3

Please sign in to comment.