Skip to content
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

Fix a crash with invalid FILE_DIR defines #2197

Merged
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
1 change: 1 addition & 0 deletions DMCompiler/Compiler/CompilerError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public enum WarningCode {
FileAlreadyIncluded = 1000,
MissingIncludedFile = 1001,
InvalidWarningCode = 1002,
InvalidFileDirDefine = 1003,
MisplacedDirective = 1100,
UndefineMissingDirective = 1101,
DefinedMissingParen = 1150,
Expand Down
2 changes: 1 addition & 1 deletion DMCompiler/Compiler/DMPreprocessor/DMPreprocessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ private void HandleDefineDirective(Token defineToken) {

DMPreprocessorLexer currentLexer = _lexerStack.Peek();
string dir = Path.Combine(currentLexer.IncludeDirectory, dirTokenValue);
compiler.AddResourceDirectory(dir);
compiler.AddResourceDirectory(dir, dirToken.Location);

// In BYOND it goes on to set the FILE_DIR macro's value to the added directory
// I don't see any reason to do that
Expand Down
15 changes: 12 additions & 3 deletions DMCompiler/DMCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ namespace DMCompiler;
public class DMCompiler {
public int ErrorCount;
public int WarningCount;
public HashSet<WarningCode> UniqueEmissions = new();
public readonly HashSet<WarningCode> UniqueEmissions = new();
public DMCompilerSettings Settings;
public IReadOnlyList<string> ResourceDirectories => _resourceDirectories;

private readonly DMCompilerConfiguration Config = new();
private readonly List<string> _resourceDirectories = new();
private string _codeDirectory;
private DateTime _compileStartTime;

internal readonly DMCodeTree DMCodeTree;
Expand Down Expand Up @@ -97,8 +98,13 @@ public bool Compile(DMCompilerSettings settings) {
return successfulCompile;
}

public void AddResourceDirectory(string dir) {
public void AddResourceDirectory(string dir, Location loc) {
dir = dir.Replace('\\', Path.DirectorySeparatorChar);
if (!Directory.Exists(dir)) {
Emit(WarningCode.InvalidFileDirDefine, loc,
$"Folder \"{Path.GetRelativePath(_codeDirectory, dir)}\" does not exist");
return;
}

_resourceDirectories.Add(dir);
}
Expand Down Expand Up @@ -129,7 +135,10 @@ public void AddResourceDirectory(string dir) {
}

// Adds the root of the DM project to FILE_DIR
compiler.AddResourceDirectory(Path.GetDirectoryName(files[0]) ?? "/");
_codeDirectory = Path.GetDirectoryName(files[0]) ?? "";
if (string.IsNullOrWhiteSpace(_codeDirectory))
_codeDirectory = Path.GetFullPath(".");
compiler.AddResourceDirectory(_codeDirectory, Location.Internal);

string compilerDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty;
string dmStandardDirectory = Path.Join(compilerDirectory, "DMStandard");
Expand Down
1 change: 1 addition & 0 deletions DMCompiler/DMStandard/DefaultPragmaConfig.dm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma FileAlreadyIncluded warning
#pragma MissingIncludedFile error
#pragma InvalidWarningCode warning
#pragma InvalidFileDirDefine warning
#pragma MisplacedDirective error
#pragma UndefineMissingDirective warning
#pragma DefinedMissingParen error
Expand Down
Loading