mirror of
https://github.com/github/codeql.git
synced 2026-04-26 09:15:12 +02:00
C#: Add compile time constants for some types of fields in the stub generator.
This commit is contained in:
@@ -296,6 +296,35 @@ internal sealed class StubVisitor : SymbolVisitor
|
||||
private static string EscapeIdentifier(string identifier) =>
|
||||
keywords.Contains(identifier) ? "@" + identifier : identifier;
|
||||
|
||||
private static bool TryGetConstantValue(IFieldSymbol symbol, out string value)
|
||||
{
|
||||
value = "";
|
||||
if (!symbol.HasConstantValue)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
var v = symbol.ConstantValue;
|
||||
switch (symbol.Type.SpecialType)
|
||||
{
|
||||
case SpecialType.System_Boolean:
|
||||
value = (bool)v ? "true" : "false";
|
||||
return true;
|
||||
case SpecialType.System_Byte:
|
||||
case SpecialType.System_SByte:
|
||||
case SpecialType.System_UInt16:
|
||||
case SpecialType.System_UInt32:
|
||||
case SpecialType.System_UInt64:
|
||||
case SpecialType.System_Int16:
|
||||
case SpecialType.System_Int32:
|
||||
case SpecialType.System_Int64:
|
||||
value = v.ToString()!;
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public override void VisitField(IFieldSymbol symbol)
|
||||
{
|
||||
if (IsNotPublic(symbol.DeclaredAccessibility))
|
||||
@@ -325,7 +354,8 @@ internal sealed class StubVisitor : SymbolVisitor
|
||||
stubWriter.Write(EscapeIdentifier(symbol.Name));
|
||||
if (symbol.IsConst)
|
||||
{
|
||||
stubWriter.Write(" = default");
|
||||
var v = TryGetConstantValue(symbol, out var value) ? value : "default";
|
||||
stubWriter.Write($" = {v}");
|
||||
}
|
||||
stubWriter.WriteLine(";");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user