Skip to content

Added support for generating a warning when spec handles an event but… #716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2024
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
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 {

}
}