Skip to content

Commit

Permalink
Consistent %ANALYZER behaviour with final analyzers
Browse files Browse the repository at this point in the history
  • Loading branch information
TollyH committed May 13, 2024
1 parent e8af1fa commit 7977013
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 22 deletions.
49 changes: 34 additions & 15 deletions AssemblerWarnings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ public partial class AssemblerWarnings
public readonly HashSet<int> DisabledWarnings = new();
public readonly HashSet<int> DisabledSuggestions = new();

// Store every line where each warning has been disabled so that final warning analyzers don't return warnings
// in places that have the warning disabled when the warning was since re-enabled.
private readonly Dictionary<int, HashSet<FilePosition>> disabledNonFatalErrorPositions = new();
private readonly Dictionary<int, HashSet<FilePosition>> disabledWarningPositions = new();
private readonly Dictionary<int, HashSet<FilePosition>> disabledSuggestionPositions = new();

// Variables updated by parameters of the NextInstruction method
private byte[] newBytes = Array.Empty<byte>();
private string mnemonic = "";
Expand Down Expand Up @@ -171,6 +177,7 @@ public Warning[] NextInstruction(byte[] newBytes, string mnemonic, string[] oper
/// <param name="macroName">The name of the current macro being expanded, or <see langword="null"/> if no macro is.</param>
public void NewLabel(string labelName, FilePosition filePosition, string? macroName)
{
UpdateDisabledPositions(filePosition);
labelDefinitionPositions[labelName] = (filePosition, macroName);
}

Expand All @@ -192,27 +199,18 @@ public Warning[] Finalize(byte[] finalProgram, ulong entryPoint, HashSet<string>

foreach ((int code, FinalWarningAnalyzer finalAnalyzer) in nonFatalErrorFinalAnalyzers)
{
if (DisabledNonFatalErrors.Contains(code))
{
continue;
}
warnings.AddRange(finalAnalyzer());
warnings.AddRange(finalAnalyzer().Where(w =>
!disabledNonFatalErrorPositions.GetValueOrDefault(code)?.Contains(w.Position) ?? true));
}
foreach ((int code, FinalWarningAnalyzer finalAnalyzer) in warningFinalAnalyzers)
{
if (DisabledWarnings.Contains(code))
{
continue;
}
warnings.AddRange(finalAnalyzer());
warnings.AddRange(finalAnalyzer().Where(w =>
!disabledWarningPositions.GetValueOrDefault(code)?.Contains(w.Position) ?? true));
}
foreach ((int code, FinalWarningAnalyzer finalAnalyzer) in suggestionFinalAnalyzers)
{
if (DisabledSuggestions.Contains(code))
{
continue;
}
warnings.AddRange(finalAnalyzer());
warnings.AddRange(finalAnalyzer().Where(w =>
!disabledSuggestionPositions.GetValueOrDefault(code)?.Contains(w.Position) ?? true));
}

return warnings.ToArray();
Expand Down Expand Up @@ -335,6 +333,8 @@ public AssemblerWarnings(bool usingV1Format)

private void PreAnalyzeStateUpdate()
{
UpdateDisabledPositions(filePosition);

if (newBytes.Length > 0)
{
operandStart = 0;
Expand Down Expand Up @@ -440,6 +440,25 @@ private void PostAnalyzeStateUpdate()
lastMacroLineDepth = macroLineDepth;
}

private void UpdateDisabledPositions(FilePosition filePosition)
{
foreach (int code in DisabledNonFatalErrors)
{
_ = disabledNonFatalErrorPositions.TryAdd(code, new HashSet<FilePosition>());
disabledNonFatalErrorPositions[code].Add(filePosition);
}
foreach (int code in DisabledWarnings)
{
_ = disabledWarningPositions.TryAdd(code, new HashSet<FilePosition>());
disabledWarningPositions[code].Add(filePosition);
}
foreach (int code in DisabledSuggestions)
{
_ = disabledSuggestionPositions.TryAdd(code, new HashSet<FilePosition>());
disabledSuggestionPositions[code].Add(filePosition);
}
}

// Analyzer methods
// (Rolling = runs as each instruction is processed, Final = runs after all instructions have been processed)

Expand Down
8 changes: 1 addition & 7 deletions Test/KitchenSink.asm
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
; The exact binary output that this program should produce
; is located in KitchenSink.bin

; Disable unused label suggestion
%ANALYZER suggestion, 0018, 0
%ANALYZER suggestion, 0004, 0

MVQ rg0, 69

Expand Down Expand Up @@ -42,8 +42,6 @@ HLT

%NUM @!CURRENT_ADDRESS

%ANALYZER suggestion, 0004, 0

%DAT "Hello, world!\nEscape test complete\"Still string\0",
%DAT 0x42
%NUM 1189998819991197253
Expand All @@ -64,10 +62,6 @@ this@LineIsNowBlank()
%nUm :&NOR_DOES_THIS
%NuM :&NOR_THIS

; Final analyzers currently can't be re-enabled without them showing again
; This should hopefully be changed in a future version
; %ANALYZER suggestion, 0004, r

%MACRO CFL, ASMX_CLF
:thisLabelRemovesTheWarning
CFL
Expand Down

0 comments on commit 7977013

Please sign in to comment.