Skip to content

Commit 7ded321

Browse files
authored
Added support for generating a warning when spec handles an event but does not add it in its observes list (#716)
1 parent 59e3312 commit 7ded321

File tree

5 files changed

+44
-2
lines changed

5 files changed

+44
-2
lines changed

Src/PCompiler/CompilerCore/DefaultTranslationErrorHandler.cs

+5
Original file line numberDiff line numberDiff line change
@@ -334,5 +334,10 @@ private string DeclarationName(IPDecl method)
334334
{
335335
return method.Name.Length > 0 ? method.Name : $"at {locationResolver.GetLocation(method.SourceLocation)}";
336336
}
337+
338+
public string SpecObservesSetIncompleteWarning(ParserRuleContext ctx, PEvent ev, Machine machine)
339+
{
340+
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!]";
341+
}
337342
}
338343
}

Src/PCompiler/CompilerCore/ITranslationErrorHandler.cs

+2
Original file line numberDiff line numberDiff line change
@@ -117,5 +117,7 @@ Exception DuplicateStartState(
117117

118118
Exception IllegalChooseSubExprType(PParser.ChooseExprContext context, PLanguageType subExprType);
119119
Exception IllegalFunctionUsedInSpecMachine(Function function, Machine callerOwner);
120+
121+
String SpecObservesSetIncompleteWarning(ParserRuleContext loc, PEvent ev, Machine machine);
120122
}
121123
}

Src/PCompiler/CompilerCore/TypeChecker/Analyzer.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static Scope AnalyzeCompilationUnit(ICompilerConfiguration config,
2020
// Step 2: Validate machine specifications
2121
foreach (var machine in globalScope.Machines)
2222
{
23-
MachineChecker.Validate(handler, machine);
23+
MachineChecker.Validate(handler, machine, config);
2424
}
2525

2626
// Step 3: Fill function bodies

Src/PCompiler/CompilerCore/TypeChecker/MachineChecker.cs

+22-1
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,34 @@ namespace Plang.Compiler.TypeChecker
1111
{
1212
public static class MachineChecker
1313
{
14-
public static void Validate(ITranslationErrorHandler handler, Machine machine)
14+
public static void Validate(ITranslationErrorHandler handler, Machine machine, ICompilerConfiguration job)
1515
{
1616
var startState = FindStartState(machine, handler);
1717
var startStatePayloadType = GetStatePayload(startState);
1818
Debug.Assert(startStatePayloadType.IsSameTypeAs(machine.PayloadType));
1919
ValidateHandlers(handler, machine);
2020
ValidateTransitions(handler, machine);
21+
// special validation for monitors:
22+
// ensure that each eventhandler is in the observe set.
23+
ValidateSpecObservesList(handler, machine, job);
24+
}
25+
26+
private static void ValidateSpecObservesList(ITranslationErrorHandler handler, Machine machine, ICompilerConfiguration job)
27+
{
28+
if (machine.IsSpec)
29+
{
30+
foreach (var state in machine.AllStates())
31+
{
32+
foreach (var pair in state.AllEventHandlers)
33+
{
34+
if (!machine.Observes.Events.Contains(pair.Key))
35+
{
36+
job.Output.WriteWarning(
37+
handler.SpecObservesSetIncompleteWarning(pair.Value.SourceLocation, pair.Key, machine));
38+
}
39+
}
40+
}
41+
}
2142
}
2243

2344
private static void ValidateHandlers(ITranslationErrorHandler handler, Machine machine)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
event e1;
2+
event e2;
3+
4+
spec Invariant1 observes e1 {
5+
start state Init {
6+
on e2 goto Init;
7+
}
8+
}
9+
10+
machine Main {
11+
start state Init {
12+
13+
}
14+
}

0 commit comments

Comments
 (0)