Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .nuke/build.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"TestAll",
"TestCodeGen",
"TestCompiler",
"TestCore",
"TestIntegration",
"TestParser",
"TestRuntime",
Expand Down
8 changes: 6 additions & 2 deletions Cesium.CodeGen/CompilationOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: MIT

using Cesium.Core.Warnings;
using Mono.Cecil;
using TruePath;

Expand All @@ -19,7 +20,8 @@ public record CompilationOptions(
IList<string> DefineConstants,
IList<LocalPath> AdditionalIncludeDirectories,
bool ProducePreprocessedFile,
bool ProduceAstFile)
bool ProduceAstFile,
WarningsSet WarningSet = WarningsSet.None)
{
public virtual bool Equals(CompilationOptions? other)
{
Expand All @@ -36,7 +38,8 @@ public virtual bool Equals(CompilationOptions? other)
&& DefineConstants.SequenceEqual(other.DefineConstants)
&& AdditionalIncludeDirectories.SequenceEqual(other.AdditionalIncludeDirectories)
&& ProducePreprocessedFile == other.ProducePreprocessedFile
&& ProduceAstFile == other.ProduceAstFile;
&& ProduceAstFile == other.ProduceAstFile
&& WarningSet == other.WarningSet;
}

public override int GetHashCode()
Expand All @@ -63,6 +66,7 @@ public override int GetHashCode()
}
hashCode.Add(ProducePreprocessedFile);
hashCode.Add(ProduceAstFile);
hashCode.Add(WarningSet);
return hashCode.ToHashCode();
}
}
17 changes: 17 additions & 0 deletions Cesium.CodeGen/CompilerWarningProcessor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-FileCopyrightText: 2026 Cesium contributors <https://github.com/ForNeVeR/Cesium>
//
// SPDX-License-Identifier: MIT

using Cesium.Core;
using Cesium.Core.Warnings;

namespace Cesium.CodeGen;

public class CompilerWarningProcessor(WarningsSet set) : IWarningProcessor<CompilerWarning>
{
public void EmitWarning(CompilerWarning warning)
{
if (set.HasFlag(warning.Set))
Console.Error.WriteLine($"Warning: {warning.Message} [-W{warning.Set.ToString().FromCamelToKebab()}]");
}
}
3 changes: 3 additions & 0 deletions Cesium.CodeGen/Contexts/FunctionScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Cesium.CodeGen.Ir.Expressions;
using Cesium.CodeGen.Ir.Types;
using Cesium.Core;
using Cesium.Core.Warnings;
using Mono.Cecil;
using Mono.Cecil.Cil;
using PointerType = Cesium.CodeGen.Ir.Types.PointerType;
Expand All @@ -19,7 +20,9 @@ internal record FunctionScope(TranslationUnitContext Context, FunctionInfo Funct
{
public AssemblyContext AssemblyContext => Context.AssemblyContext;
public ModuleDefinition Module => Context.Module;
public IWarningProcessor<CompilerWarning> WarningProcessor => Context.WarningProcessor;
public TargetArchitectureSet ArchitectureSet => AssemblyContext.ArchitectureSet;

public FunctionInfo? GetFunctionInfo(string identifier) =>
Context.GetFunctionInfo(identifier);

Expand Down
4 changes: 4 additions & 0 deletions Cesium.CodeGen/Contexts/TranslationUnitContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using System.Collections.Immutable;
using System.Diagnostics;
using System.Security.AccessControl;
using Cesium.Core.Warnings;
using PointerType = Cesium.CodeGen.Ir.Types.PointerType;

namespace Cesium.CodeGen.Contexts;
Expand All @@ -27,6 +28,8 @@ public class TranslationUnitContext
public TypeDefinition ModuleType => Module.GetType("<Module>");
public TypeDefinition GlobalType => AssemblyContext.GlobalType;

public IWarningProcessor<CompilerWarning> WarningProcessor { get; }

private TypeDefinition? _translationUnitLevelType;

private Dictionary<string, FunctionInfo> Functions { get; } = new();
Expand All @@ -39,6 +42,7 @@ public TranslationUnitContext(AssemblyContext assemblyContext, string name)
{
AssemblyContext = assemblyContext;
Name = name;
WarningProcessor = new CompilerWarningProcessor(assemblyContext.CompilationOptions.WarningSet);
}

/// <remarks>
Expand Down
4 changes: 3 additions & 1 deletion Cesium.Compiler.Tests/JsonObjectFileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// SPDX-License-Identifier: MIT

using Cesium.CodeGen;
using Cesium.Core.Warnings;
using Cesium.TestFramework;
using Mono.Cecil;
using TruePath;
Expand Down Expand Up @@ -41,7 +42,8 @@ public void SupportedExtensions(string fileName, bool result) =>
new("/nonexistent-folder/include")
],
ProducePreprocessedFile: false,
ProduceAstFile: true
ProduceAstFile: true,
WarningsSet.All
);

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"/nonexistent-folder/include"
],
"ProducePreprocessedFile": false,
"ProduceAstFile": true
"ProduceAstFile": true,
"WarningSet": "All"
}
}
}
2 changes: 1 addition & 1 deletion Cesium.Compiler/Arguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public class Arguments
public int OptimizationLevel { get; init; } = 0;

[Option('W', HelpText = "Enable warnings set")]
public string WarningsSet { get; init; } = "";
public IEnumerable<string> WarningsSet { get; init; } = Array.Empty<string>();

[Option('E', HelpText = "Produce preprocessed file")]
public bool ProducePreprocessedFile { get; init; } = false;
Expand Down
16 changes: 15 additions & 1 deletion Cesium.Compiler/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Diagnostics.CodeAnalysis;
using Cesium.CodeGen;
using Cesium.Core;
using Cesium.Core.Warnings;
using Mono.Cecil;
using TruePath;

Expand Down Expand Up @@ -36,6 +37,18 @@ public static async Task<int> Main(string[] args)
".dll" => ModuleKind.Dll,
var o => throw new CompilationException($"Unknown file extension: {o}. \"modulekind\" is not specified.")
};

var warningsSet = options.WarningsSet.Aggregate(WarningsSet.None, (set, s) =>
{
var warn = s.FromKebabToCamel();
if (Enum.TryParse<WarningsSet>(warn, true, out var parsed))
{
return set | parsed;
}

throw new CompilationException($"Unknown warning: {warn}");
});

var compilationOptions = new CompilationOptions(
targetRuntime,
targetArchitectureSet,
Expand All @@ -48,7 +61,8 @@ public static async Task<int> Main(string[] args)
options.DefineConstant.ToList(),
options.IncludeDirectories.Select(x => new LocalPath(x)).ToList(),
options.ProducePreprocessedFile,
options.DumpAst);
options.DumpAst,
warningsSet);

if (options.ProduceObjectFileImitation)
{
Expand Down
4 changes: 2 additions & 2 deletions Cesium.Compiler/WarningProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@

namespace Cesium.Compiler;

public class WarningProcessor : IWarningProcessor
public class WarningProcessor : IWarningProcessor<DiagnosticWarning>
{
public void EmitWarning(PreprocessorWarning warning)
public void EmitWarning(DiagnosticWarning warning)
{
Console.Error.WriteLine($"{warning.Location}: warning: {warning.Message}");
}
Expand Down
31 changes: 31 additions & 0 deletions Cesium.Core.Tests/Cesium.Core.Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!--
SPDX-FileCopyrightText: 2025-2026 Cesium contributors <https://github.com/ForNeVeR/Cesium>

SPDX-License-Identifier: MIT
-->

<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<Using Include="Xunit"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Cesium.Core\Cesium.Core.csproj" />
</ItemGroup>

</Project>
41 changes: 41 additions & 0 deletions Cesium.Core.Tests/StringFormatExtensionsTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-FileCopyrightText: 2026 Cesium contributors <https://github.com/ForNeVeR/Cesium>
//
// SPDX-License-Identifier: MIT

namespace Cesium.Core.Tests;

public class StringFormatExtensionsTest
{
[Theory]
[InlineData("", "")]
[InlineData("hello", "Hello")]
[InlineData("hello-world", "HelloWorld")]
[InlineData("-hello", "Hello")]
[InlineData("hello-", "Hello")]
[InlineData("hello--world", "HelloWorld")]
[InlineData("---", "")]
[InlineData("hello-123", "Hello123")]
[InlineData("a-b-123", "AB123")]
[InlineData("HELLO-WORLD", "HELLOWORLD")]
[InlineData("a", "A")]
[InlineData("a-b", "AB")]
public void StringFromKebabToCamelTest(string input, string expected)
=> Assert.Equal(expected, input.FromKebabToCamel());

[Theory]
[InlineData("", "")]
[InlineData("Hello", "hello")]
[InlineData("HelloWorld", "hello-world")]
[InlineData("hello", "hello")]
[InlineData("HELLO", "hello")]
[InlineData("XMLHttpRequest", "xml-http-request")]
[InlineData("Hello123", "hello-123")]
[InlineData("Hello123World", "hello-123-world")]
[InlineData("A", "a")]
[InlineData("a", "a")]
[InlineData("123", "123")]
[InlineData("A1B2", "a-1-b-2")]
[InlineData("AbcDef", "abc-def")]
public void StringFromCamelToKebabTest(string input, string expected)
=> Assert.Equal(expected, input.FromCamelToKebab());
}
60 changes: 60 additions & 0 deletions Cesium.Core/StringFormatExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// SPDX-FileCopyrightText: 2026 Cesium contributors <https://github.com/ForNeVeR/Cesium>
//
// SPDX-License-Identifier: MIT

using System.Text;

namespace Cesium.Core;

public static class StringFormatExtensions
{
extension(ReadOnlySpan<char> str)
Comment thread
evgTSV marked this conversation as resolved.
{
public string FromKebabToCamel()
{
if (str.IsEmpty) return string.Empty;

var sb = new StringBuilder();

foreach (var part in str.Split('-'))
{
if (part.Start.Value == part.End.Value)
continue;

sb.Append(char.ToUpper(str[part.Start]));

if (part.Start.Value != part.End.Value)
sb.Append(str[(part.Start.Value + 1)..part.End]);
}

return sb.ToString();
}

public string FromCamelToKebab()
{
if (str.IsEmpty) return string.Empty;

var sb = new StringBuilder();

for (int i = 0; i < str.Length; i++)
{
if (char.IsLower(str[i]))
sb.Append(str[i]);
else if (i == 0)
sb.Append(char.ToLower(str[i]));
else if (char.IsDigit(str[i]) && !char.IsDigit(str[i - 1]))
sb.Append('-').Append(str[i]);
else if (char.IsDigit(str[i]))
sb.Append(str[i]);
else if (char.IsLower(str[i - 1]))
sb.Append('-').Append(char.ToLower(str[i]));
else if (i + 1 == str.Length || char.IsUpper(str[i + 1]))
sb.Append(char.ToLower(str[i]));
else
sb.Append('-').Append(char.ToLower(str[i]));
}

return sb.ToString();
}
}
}
9 changes: 9 additions & 0 deletions Cesium.Core/Warnings/CompilerWarning.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-FileCopyrightText: 2026 Cesium contributors <https://github.com/ForNeVeR/Cesium>
//
// SPDX-License-Identifier: MIT

namespace Cesium.Core.Warnings;

public record CompilerWarning(string Message, WarningsSet Set)
: DiagnosticWarning(new SourceLocationInfo(string.Empty, 0, 0), Message);
Comment thread
ForNeVeR marked this conversation as resolved.
// TODO: In future we have to determine a location where a warning is triggered
7 changes: 7 additions & 0 deletions Cesium.Core/Warnings/DiagnosticWarning.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// SPDX-FileCopyrightText: 2026 Cesium contributors <https://github.com/ForNeVeR/Cesium>
//
// SPDX-License-Identifier: MIT

namespace Cesium.Core.Warnings;

public record DiagnosticWarning(SourceLocationInfo Location, string Message);
6 changes: 3 additions & 3 deletions Cesium.Core/Warnings/IWarningProcessor.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// SPDX-FileCopyrightText: 2025 Cesium contributors <https://github.com/ForNeVeR/Cesium>
// SPDX-FileCopyrightText: 2025-2026 Cesium contributors <https://github.com/ForNeVeR/Cesium>
//
// SPDX-License-Identifier: MIT

namespace Cesium.Core.Warnings;

public interface IWarningProcessor
public interface IWarningProcessor<in T> where T : DiagnosticWarning
{
public void EmitWarning(PreprocessorWarning warning);
public void EmitWarning(T warning);
}
Comment thread
ForNeVeR marked this conversation as resolved.
3 changes: 2 additions & 1 deletion Cesium.Core/Warnings/PreprocessorWarning.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@

namespace Cesium.Core.Warnings;

public record PreprocessorWarning(SourceLocationInfo Location, string Message);
public record PreprocessorWarning(SourceLocationInfo Location, string Message)
: DiagnosticWarning(Location, Message);
Loading
Loading