From 4d9234ec2f2bdfcff8d624897516208df37f6dfa Mon Sep 17 00:00:00 2001 From: Ankush Desai Date: Tue, 9 Apr 2024 14:05:36 -0700 Subject: [PATCH] Added support for generating a warning when spec handles an event but does not add it in its observes list --- .../DefaultTranslationErrorHandler.cs | 5 ++++ .../CompilerCore/ITranslationErrorHandler.cs | 2 ++ .../CompilerCore/TypeChecker/Analyzer.cs | 2 +- .../TypeChecker/MachineChecker.cs | 23 ++++++++++++++++++- .../Correct/monitorobserves/observes.p | 14 +++++++++++ 5 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 Tst/RegressionTests/Feature1SMLevelDecls/Correct/monitorobserves/observes.p diff --git a/Src/PCompiler/CompilerCore/DefaultTranslationErrorHandler.cs b/Src/PCompiler/CompilerCore/DefaultTranslationErrorHandler.cs index ccacf6fcfd..4aef965ae9 100644 --- a/Src/PCompiler/CompilerCore/DefaultTranslationErrorHandler.cs +++ b/Src/PCompiler/CompilerCore/DefaultTranslationErrorHandler.cs @@ -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!]"; + } } } \ No newline at end of file diff --git a/Src/PCompiler/CompilerCore/ITranslationErrorHandler.cs b/Src/PCompiler/CompilerCore/ITranslationErrorHandler.cs index 00e12c244f..402a5fc5d6 100644 --- a/Src/PCompiler/CompilerCore/ITranslationErrorHandler.cs +++ b/Src/PCompiler/CompilerCore/ITranslationErrorHandler.cs @@ -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); } } \ No newline at end of file diff --git a/Src/PCompiler/CompilerCore/TypeChecker/Analyzer.cs b/Src/PCompiler/CompilerCore/TypeChecker/Analyzer.cs index 050be18cca..97ee4dabdf 100644 --- a/Src/PCompiler/CompilerCore/TypeChecker/Analyzer.cs +++ b/Src/PCompiler/CompilerCore/TypeChecker/Analyzer.cs @@ -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 diff --git a/Src/PCompiler/CompilerCore/TypeChecker/MachineChecker.cs b/Src/PCompiler/CompilerCore/TypeChecker/MachineChecker.cs index 628d9cac50..b8f0a5cddf 100644 --- a/Src/PCompiler/CompilerCore/TypeChecker/MachineChecker.cs +++ b/Src/PCompiler/CompilerCore/TypeChecker/MachineChecker.cs @@ -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) diff --git a/Tst/RegressionTests/Feature1SMLevelDecls/Correct/monitorobserves/observes.p b/Tst/RegressionTests/Feature1SMLevelDecls/Correct/monitorobserves/observes.p new file mode 100644 index 0000000000..03855f7148 --- /dev/null +++ b/Tst/RegressionTests/Feature1SMLevelDecls/Correct/monitorobserves/observes.p @@ -0,0 +1,14 @@ +event e1; +event e2; + +spec Invariant1 observes e1 { + start state Init { + on e2 goto Init; + } +} + +machine Main { + start state Init { + + } +} \ No newline at end of file