Skip to content

Commit 4287861

Browse files
authored
Fix some DMCompiler warnings (#2460)
1 parent f18e967 commit 4287861

File tree

8 files changed

+35
-23
lines changed

8 files changed

+35
-23
lines changed

DMCompiler/Compiler/DM/AST/DMAST.ObjectStatements.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public DMASTProcDefinition(Location location, DreamPath path, DMASTDefinitionPar
5959
}
6060

6161
ObjectPath = (path.Elements.Length > 1) ? path.FromElements(0, -2) : DreamPath.Root;
62-
Name = path.LastElement;
62+
Name = path.LastElement ?? throw new ArgumentException($"Proc path \"{path}\" is missing a name", nameof(path));
6363
Parameters = parameters;
6464
Body = body;
6565
ReturnTypes = returnType;
@@ -103,7 +103,7 @@ public sealed class DMASTObjectVarOverride : DMASTStatement {
103103

104104
public DMASTObjectVarOverride(Location location, DreamPath path, DMASTExpression value) : base(location) {
105105
ObjectPath = path.FromElements(0, -2);
106-
VarName = path.LastElement;
106+
VarName = path.LastElement ?? throw new ArgumentException($"Var override path \"{path}\" is missing a name", nameof(path));
107107
Value = value;
108108
}
109109
}

DMCompiler/Compiler/DM/DMParser.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics.CodeAnalysis;
12
using DMCompiler.Compiler.DMPreprocessor;
23
using System.Linq;
34
using DMCompiler.Compiler.DM.AST;
@@ -506,6 +507,7 @@ public DMASTFile File() {
506507
return null;
507508
}
508509

510+
[return: NotNullIfNotNull(nameof(expression))]
509511
private DMASTExpression? ParseScopeIdentifier(DMASTExpression? expression) {
510512
do {
511513
var identifier = Identifier();
@@ -1265,6 +1267,7 @@ private DMASTProcStatement For() {
12651267
if (Check(TokenType.DM_In)) {
12661268
Whitespace();
12671269
DMASTExpression? listExpr = Expression();
1270+
RequireExpression(ref listExpr);
12681271
Whitespace();
12691272
Consume(TokenType.DM_RightParenthesis, "Expected ')' in for after expression 2");
12701273
ExtraColonPeriod();

DMCompiler/Compiler/DMPreprocessor/DMPreprocessor.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,8 @@ public void DefineMacro(string key, string value) {
197197

198198
// NB: Pushes files to a stack, so call in reverse order if you are
199199
// including multiple files.
200-
public void IncludeFile(string includeDir, string file, bool isDMStandard, Location? includedFrom = null) {
201-
string filePath = Path.Combine(includeDir, file);
200+
public void IncludeFile(string? includeDir, string file, bool isDMStandard, Location? includedFrom = null) {
201+
string filePath = Path.Combine(includeDir ?? string.Empty, file);
202202
filePath = filePath.Replace('\\', Path.DirectorySeparatorChar);
203203
filePath = Path.GetFullPath(filePath); // Strips out path operators
204204

@@ -243,7 +243,7 @@ public void IncludeFile(string includeDir, string file, bool isDMStandard, Locat
243243
}
244244
}
245245

246-
public void PreprocessFile(string includeDir, string file, bool isDMStandard) {
246+
public void PreprocessFile(string? includeDir, string file, bool isDMStandard) {
247247
file = file.Replace('\\', '/');
248248

249249
_lexerStack.Push(new DMPreprocessorLexer(compiler, includeDir, file, isDMStandard));
@@ -275,7 +275,7 @@ private void HandleIncludeDirective(Token includeToken) {
275275

276276
DMPreprocessorLexer currentLexer = _lexerStack.Peek();
277277
string file = Path.Combine(Path.GetDirectoryName(currentLexer.File.Replace('\\', Path.DirectorySeparatorChar)), includedFileToken.ValueAsString());
278-
string directory = currentLexer.IncludeDirectory;
278+
string? directory = currentLexer.IncludeDirectory;
279279

280280
IncludeFile(directory, file, includeToken.Location.InDMStandard, includedFrom: includeToken.Location);
281281
}
@@ -307,7 +307,7 @@ private void HandleDefineDirective(Token defineToken) {
307307
}
308308

309309
DMPreprocessorLexer currentLexer = _lexerStack.Peek();
310-
string dir = Path.Combine(currentLexer.IncludeDirectory, dirTokenValue);
310+
string dir = Path.Combine(currentLexer.IncludeDirectory ?? string.Empty, dirTokenValue);
311311
compiler.AddResourceDirectory(dir, dirToken.Location);
312312

313313
// In BYOND it goes on to set the FILE_DIR macro's value to the added directory

DMCompiler/Compiler/DMPreprocessor/DMPreprocessorLexer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace DMCompiler.Compiler.DMPreprocessor;
1212
internal sealed class DMPreprocessorLexer {
1313
private static readonly StringBuilder TokenTextBuilder = new();
1414

15-
public readonly string IncludeDirectory;
15+
public readonly string? IncludeDirectory;
1616
public readonly string File;
1717

1818
private readonly DMCompiler _compiler;
@@ -23,7 +23,7 @@ internal sealed class DMPreprocessorLexer {
2323
private int _previousLine = 1, _previousColumn;
2424
private readonly Queue<Token> _pendingTokenQueue = new(); // TODO: Possible to remove this?
2525

26-
public DMPreprocessorLexer(DMCompiler compiler, string includeDirectory, string file, string source) {
26+
public DMPreprocessorLexer(DMCompiler compiler, string? includeDirectory, string file, string source) {
2727
_compiler = compiler;
2828
IncludeDirectory = includeDirectory;
2929
File = file;
@@ -32,12 +32,12 @@ public DMPreprocessorLexer(DMCompiler compiler, string includeDirectory, string
3232
Advance();
3333
}
3434

35-
public DMPreprocessorLexer(DMCompiler compiler, string includeDirectory, string file, bool isDMStandard) {
35+
public DMPreprocessorLexer(DMCompiler compiler, string? includeDirectory, string file, bool isDMStandard) {
3636
_compiler = compiler;
3737
IncludeDirectory = includeDirectory;
3838
File = file;
3939

40-
_source = new StreamReader(Path.Combine(includeDirectory, file), Encoding.UTF8);
40+
_source = new StreamReader(Path.Combine(includeDirectory ?? string.Empty, file), Encoding.UTF8);
4141
_isDMStandard = isDMStandard;
4242
Advance();
4343
}

DMCompiler/DM/Builders/DMASTFolder.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Diagnostics.CodeAnalysis;
12
using DMCompiler.Compiler.DM.AST;
23

34
namespace DMCompiler.DM.Builders;
@@ -122,7 +123,8 @@ public void FoldAst(DMASTNode? ast) {
122123
}
123124
}
124125

125-
private DMASTExpression FoldExpression(DMASTExpression? expression) {
126+
[return: NotNullIfNotNull(nameof(expression))]
127+
private DMASTExpression? FoldExpression(DMASTExpression? expression) {
126128
if (expression is DMASTUnary unary) {
127129
unary.Value = FoldExpression(unary.Value);
128130
} else if (expression is DMASTBinary binary) {

DMCompiler/DM/Builders/DMProcBuilder.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public void ProcessProcDefinition(DMASTProcDefinition procDefinition) {
1919

2020
if (parameter.Value != null) { //Parameter has a default value
2121
string afterDefaultValueCheck = proc.NewLabelName();
22-
DMReference parameterRef = proc.GetLocalVariableReference(parameterName);
22+
DMReference parameterRef = proc.GetLocalVariableReference(parameterName, parameter.Location);
2323

2424
//Don't set parameter to default if not null
2525
proc.PushReferenceValue(parameterRef);
@@ -197,7 +197,7 @@ private void ProcessStatementVarDeclaration(DMASTProcStatementVarDeclaration var
197197
}
198198

199199
value.EmitPushValue(ExprContext);
200-
proc.Assign(proc.GetLocalVariableReference(varDeclaration.Name));
200+
proc.Assign(proc.GetLocalVariableReference(varDeclaration.Name, varDeclaration.Location));
201201
proc.Pop();
202202
}
203203

@@ -396,10 +396,11 @@ private void ProcessStatementFor(DMASTProcStatementFor statementFor) {
396396
}
397397
proc.EndScope();
398398

399-
IEnumerable<DMASTVarDeclExpression> FindVarDecls(DMASTExpression expr) {
400-
if (expr is DMASTVarDeclExpression p) {
399+
IEnumerable<DMASTVarDeclExpression> FindVarDecls(DMASTExpression? expr) {
400+
if (expr is null)
401+
yield break;
402+
if (expr is DMASTVarDeclExpression p)
401403
yield return p;
402-
}
403404

404405
foreach (var leaf in expr.Leaves()) {
405406
foreach(var decl in FindVarDecls(leaf)) {
@@ -863,7 +864,7 @@ private void ProcessStatementTryCatch(DMASTProcStatementTryCatch tryCatch) {
863864
compiler.Emit(WarningCode.DuplicateVariable, param.Location, $"Duplicate var {param.Name}");
864865
}
865866

866-
proc.StartTry(catchLabel, proc.GetLocalVariableReference(param.Name));
867+
proc.StartTry(catchLabel, proc.GetLocalVariableReference(param.Name, param.Location));
867868
} else {
868869
if (tryCatch.CatchParameter != null)
869870
compiler.Emit(WarningCode.InvalidVarDefinition, tryCatch.CatchParameter.Location,

DMCompiler/DM/DMProc.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -532,9 +532,14 @@ public bool TryAddLocalConstVariable(string name, DreamPath? type, Constant valu
532532
return null;
533533
}
534534

535-
public DMReference GetLocalVariableReference(string name) {
535+
public DMReference GetLocalVariableReference(string name, Location loc) {
536536
LocalVariable? local = GetLocalVariable(name);
537537

538+
if (local is null) {
539+
_compiler.Emit(WarningCode.InvalidReference, loc, $"Attempted to reference invalid local var \"{name}\"");
540+
return DMReference.Invalid;
541+
}
542+
538543
return local.IsParameter ? DMReference.CreateArgument(local.Id) : DMReference.CreateLocal(local.Id);
539544
}
540545

DMCompiler/DMCompiler.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,12 @@ public void AddResourceDirectory(string dir, Location loc) {
127127
return null;
128128
}
129129

130-
string includeDir = Path.GetDirectoryName(files[i]);
130+
string? includeDir = Path.GetDirectoryName(files[i]);
131131
string fileName = Path.GetFileName(files[i]);
132132

133133
preproc.IncludeFile(includeDir, fileName, false);
134-
compiler.AddResourceDirectory(includeDir, Location.Internal);
134+
if (includeDir is not null)
135+
compiler.AddResourceDirectory(includeDir, Location.Internal);
135136
}
136137

137138
// Adds the root of the DM project to FILE_DIR
@@ -160,8 +161,8 @@ public void AddResourceDirectory(string dir, Location loc) {
160161
result.Append(t.Text);
161162
}
162163

163-
string outputDir = Path.GetDirectoryName(Settings.Files[0]);
164-
string outputPath = Path.Combine(outputDir, "preprocessor_dump.dm");
164+
string? outputDir = Path.GetDirectoryName(Settings.Files[0]);
165+
string outputPath = Path.Combine(outputDir ?? string.Empty, "preprocessor_dump.dm");
165166

166167
File.WriteAllText(outputPath, result.ToString());
167168
Console.WriteLine($"Preprocessor output dumped to {outputPath}");

0 commit comments

Comments
 (0)