Skip to content

Commit

Permalink
PR cleanup (#723)
Browse files Browse the repository at this point in the history
* Added support for generating a warning when spec handles an event but does not add it in its observes list (#716)

* Create custom converter for JSON serialization in .NET8 (#717)

* Create custom converter for JSON serialization in .NET8

* Add check for different dictionary type for null replacement

---------

Co-authored-by: Eric Hua <[email protected]>

---------

Co-authored-by: Ankush Desai <[email protected]>
Co-authored-by: Eric Hua <[email protected]>
Co-authored-by: Eric Hua <[email protected]>
  • Loading branch information
4 people authored Apr 25, 2024
1 parent c6f74dc commit 3f4781a
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 9 deletions.
36 changes: 29 additions & 7 deletions Src/PChecker/CheckerCore/SystematicTesting/TestingEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,25 @@ public class TestingEngine
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
WriteIndented = true,
Converters = { new EncodingConverter() }
};

internal class EncodingConverter : JsonConverter<Encoding>
{
public override Encoding Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var name = reader.GetString();
if (name == null)
return null;
return Encoding.GetEncoding(name);
}
public override void Write(Utf8JsonWriter writer, Encoding value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.WebName);
}
}

/// <summary>
/// The profiler.
/// </summary>
Expand Down Expand Up @@ -751,12 +767,18 @@ public object RecursivelyReplaceNullWithString(object obj)
{
return "null";
}

if (obj is Dictionary<string, object> dictionary)
{
if (obj is Dictionary<string, object> dictionaryStr) {
var newDictionary = new Dictionary<string, object>();
foreach (var item in dictionary)
{
foreach (var item in dictionaryStr) {
var newVal = RecursivelyReplaceNullWithString(item.Value);
if (newVal != null)
newDictionary[item.Key] = newVal;
}
return newDictionary;
}
else if (obj is Dictionary<int, object> dictionaryInt) {
var newDictionary = new Dictionary<int, object>();
foreach (var item in dictionaryInt) {
var newVal = RecursivelyReplaceNullWithString(item.Value);
if (newVal != null)
newDictionary[item.Key] = newVal;
Expand Down Expand Up @@ -1156,4 +1178,4 @@ public void SetLogger(TextWriter logger)
ErrorReporter.Logger = logger;
}
}
}
}
5 changes: 5 additions & 0 deletions Src/PCompiler/CompilerCore/DefaultTranslationErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -334,5 +334,10 @@ private string DeclarationName(IPDecl method)
{
return method.Name.Length > 0 ? method.Name : $"at {locationResolver.GetLocation(method.SourceLocation)}";
}

public string SpecObservesSetIncompleteWarning(ParserRuleContext ctx, PEvent ev, Machine machine)
{
return $"[!Warning!]\n[{locationResolver.GetLocation(ctx, ctx.start)}] Event {ev.Name} is not in the observes list of the spec machine {machine.Name}. The event-handler is never triggered as the event is not observed by the spec.\n[!Warning!]";
}
}
}
2 changes: 2 additions & 0 deletions Src/PCompiler/CompilerCore/ITranslationErrorHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,5 +117,7 @@ Exception DuplicateStartState(

Exception IllegalChooseSubExprType(PParser.ChooseExprContext context, PLanguageType subExprType);
Exception IllegalFunctionUsedInSpecMachine(Function function, Machine callerOwner);

String SpecObservesSetIncompleteWarning(ParserRuleContext loc, PEvent ev, Machine machine);
}
}
2 changes: 1 addition & 1 deletion Src/PCompiler/CompilerCore/TypeChecker/Analyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static Scope AnalyzeCompilationUnit(ICompilerConfiguration config,
// Step 2: Validate machine specifications
foreach (var machine in globalScope.Machines)
{
MachineChecker.Validate(handler, machine);
MachineChecker.Validate(handler, machine, config);
}

// Step 3: Fill function bodies
Expand Down
23 changes: 22 additions & 1 deletion Src/PCompiler/CompilerCore/TypeChecker/MachineChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,34 @@ namespace Plang.Compiler.TypeChecker
{
public static class MachineChecker
{
public static void Validate(ITranslationErrorHandler handler, Machine machine)
public static void Validate(ITranslationErrorHandler handler, Machine machine, ICompilerConfiguration job)
{
var startState = FindStartState(machine, handler);
var startStatePayloadType = GetStatePayload(startState);
Debug.Assert(startStatePayloadType.IsSameTypeAs(machine.PayloadType));
ValidateHandlers(handler, machine);
ValidateTransitions(handler, machine);
// special validation for monitors:
// ensure that each eventhandler is in the observe set.
ValidateSpecObservesList(handler, machine, job);
}

private static void ValidateSpecObservesList(ITranslationErrorHandler handler, Machine machine, ICompilerConfiguration job)
{
if (machine.IsSpec)
{
foreach (var state in machine.AllStates())
{
foreach (var pair in state.AllEventHandlers)
{
if (!machine.Observes.Events.Contains(pair.Key))
{
job.Output.WriteWarning(
handler.SpecObservesSetIncompleteWarning(pair.Value.SourceLocation, pair.Key, machine));
}
}
}
}
}

private static void ValidateHandlers(ITranslationErrorHandler handler, Machine machine)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
event e1;
event e2;

spec Invariant1 observes e1 {
start state Init {
on e2 goto Init;
}
}

machine Main {
start state Init {

}
}

0 comments on commit 3f4781a

Please sign in to comment.