Skip to content

Commit a144743

Browse files
committed
Allow constexpr macro definitions to be evaluated as constant
1 parent d4b11de commit a144743

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitDecl.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3406,6 +3406,26 @@ private bool IsConstant(string targetTypeName, Expr initExpr)
34063406

34073407
case CX_StmtClass.CX_StmtClass_CallExpr:
34083408
{
3409+
var callExpr = (CallExpr)initExpr;
3410+
3411+
if (callExpr.DirectCallee.IsInlined)
3412+
{
3413+
var evaluateResult = callExpr.Handle.Evaluate;
3414+
3415+
switch (evaluateResult.Kind)
3416+
{
3417+
case CXEvalResultKind.CXEval_Int:
3418+
{
3419+
return true;
3420+
}
3421+
3422+
case CXEvalResultKind.CXEval_Float:
3423+
{
3424+
return true;
3425+
}
3426+
}
3427+
}
3428+
34093429
return false;
34103430
}
34113431

sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.VisitStmt.cs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
using System;
44
using System.Collections.Generic;
5+
using System.Diagnostics;
56
using System.Linq;
67
using System.Runtime.CompilerServices;
78
using System.Text;
@@ -68,6 +69,51 @@ private void VisitCallExpr(CallExpr callExpr)
6869
var outputBuilder = StartCSharpCode();
6970
var calleeDecl = callExpr.CalleeDecl;
7071

72+
if (callExpr.DirectCallee.IsInlined)
73+
{
74+
var evalResult = callExpr.Handle.Evaluate;
75+
var canonicalType = callExpr.Type.CanonicalType;
76+
77+
switch (evalResult.Kind)
78+
{
79+
case CXEvalResultKind.CXEval_Int:
80+
{
81+
if (canonicalType.Handle.IsUnsigned)
82+
{
83+
outputBuilder.Write(evalResult.AsUnsigned);
84+
}
85+
else
86+
{
87+
outputBuilder.Write(evalResult.AsLongLong);
88+
}
89+
90+
StopCSharpCode();
91+
return;
92+
}
93+
94+
case CXEvalResultKind.CXEval_Float:
95+
{
96+
if (canonicalType.Kind == CXTypeKind.CXType_Float)
97+
{
98+
outputBuilder.Write((float)evalResult.AsDouble);
99+
}
100+
else
101+
{
102+
outputBuilder.Write(evalResult.AsDouble);
103+
}
104+
105+
StopCSharpCode();
106+
return;
107+
}
108+
109+
case CXEvalResultKind.CXEval_StrLiteral:
110+
{
111+
AddDiagnostic(DiagnosticLevel.Info, "Possible string constant");
112+
break;
113+
}
114+
}
115+
}
116+
71117
if (calleeDecl is null)
72118
{
73119
Visit(callExpr.Callee);

0 commit comments

Comments
 (0)