Skip to content

Commit

Permalink
Rip out FileCache and replace with FileManager
Browse files Browse the repository at this point in the history
  • Loading branch information
ellie-idb committed Oct 26, 2021
1 parent 0126df6 commit 97c5a0b
Show file tree
Hide file tree
Showing 12 changed files with 448 additions and 294 deletions.
4 changes: 2 additions & 2 deletions dub.sdl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ subPackage {
"src/dmd/console.d" \
"src/dmd/entity.d" \
"src/dmd/errors.d" \
"src/dmd/filecache.d" \
"src/dmd/file_manager.d" \
"src/dmd/globals.d" \
"src/dmd/id.d" \
"src/dmd/identifier.d" \
Expand Down Expand Up @@ -92,7 +92,7 @@ subPackage {
console,\
entity,\
errors,\
filecache,\
file_manager,\
globals,\
id,\
identifier,\
Expand Down
2 changes: 1 addition & 1 deletion src/build.d
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,7 @@ auto sourceFiles()
statement.h staticassert.h target.h template.h tokens.h version.h visitor.h
"),
lexer: fileArray(env["D"], "
console.d entity.d errors.d filecache.d globals.d id.d identifier.d lexer.d tokens.d utf.d
console.d entity.d errors.d file_manager.d globals.d id.d identifier.d lexer.d tokens.d utf.d
") ~ fileArray(env["ROOT"], "
array.d bitarray.d ctfloat.d file.d filename.d hash.d port.d region.d rmem.d
rootobject.d stringtable.d
Expand Down
16 changes: 8 additions & 8 deletions src/dmd/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,14 +245,14 @@ Note that these groups have no strict meaning, the category assignments are a bi

Note: many other utilities are in [dmd/root](https://github.com/dlang/dmd/tree/master/src/dmd/root).

| File | Purpose |
|-----------------------------------------------------------------------------|---------------------------------------------------|
| [env.d](https://github.com/dlang/dmd/blob/master/src/dmd/env.d) | Modify environment variables |
| [console.d](https://github.com/dlang/dmd/blob/master/src/dmd/console.d) | Print error messages in color |
| [utf.d](https://github.com/dlang/dmd/blob/master/src/dmd/utf.d) | Encoding/decoding Unicode text |
| [filecache.d](https://github.com/dlang/dmd/blob/master/src/dmd/filecache.d) | Keep file contents in memory |
| [utils.d](https://github.com/dlang/dmd/blob/master/src/dmd/utils.d) | Utility functions related to files and file paths |
| [complex.d](https://github.com/dlang/dmd/blob/master/src/dmd/complex.d) | A complex number type |
| File | Purpose |
|-----------------------------------------------------------------------------------|---------------------------------------------------|
| [env.d](https://github.com/dlang/dmd/blob/master/src/dmd/env.d) | Modify environment variables |
| [console.d](https://github.com/dlang/dmd/blob/master/src/dmd/console.d) | Print error messages in color |
| [utf.d](https://github.com/dlang/dmd/blob/master/src/dmd/utf.d) | Encoding/decoding Unicode text |
| [file_manager.d](https://github.com/dlang/dmd/blob/master/src/dmd/file_manager.d) | Keep file contents in memory |
| [utils.d](https://github.com/dlang/dmd/blob/master/src/dmd/utils.d) | Utility functions related to files and file paths |
| [complex.d](https://github.com/dlang/dmd/blob/master/src/dmd/complex.d) | A complex number type |

| File | Purpose |
|---------------------------------------------------------------------------------|---------------------------------------------------------------|
Expand Down
127 changes: 16 additions & 111 deletions src/dmd/dmodule.d
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import dmd.dsymbolsem;
import dmd.errors;
import dmd.expression;
import dmd.expressionsem;
import dmd.file_manager;
import dmd.globals;
import dmd.id;
import dmd.identifier;
Expand All @@ -50,113 +51,6 @@ import dmd.target;
import dmd.utils;
import dmd.visitor;

enum package_d = "package." ~ mars_ext;
enum package_di = "package." ~ hdr_ext;

/********************************************
* Look for the source file if it's different from filename.
* Look for .di, .d, directory, and along global.path.
* Does not open the file.
* Params:
* filename = as supplied by the user
* path = path to look for filename
* Returns:
* the found file name or
* `null` if it is not different from filename.
*/
private const(char)[] lookForSourceFile(const char[] filename, const char*[] path)
{
//printf("lookForSourceFile(`%.*s`)\n", cast(int)filename.length, filename.ptr);
/* Search along path[] for .di file, then .d file, then .i file, then .c file.
*/
const sdi = FileName.forceExt(filename, hdr_ext);
if (FileName.exists(sdi) == 1)
return sdi;
scope(exit) FileName.free(sdi.ptr);

const sd = FileName.forceExt(filename, mars_ext);
if (FileName.exists(sd) == 1)
return sd;
scope(exit) FileName.free(sd.ptr);

const si = FileName.forceExt(filename, i_ext);
if (FileName.exists(si) == 1)
return si;
scope(exit) FileName.free(si.ptr);

const sc = FileName.forceExt(filename, c_ext);
if (FileName.exists(sc) == 1)
return sc;
scope(exit) FileName.free(sc.ptr);

if (FileName.exists(filename) == 2)
{
/* The filename exists and it's a directory.
* Therefore, the result should be: filename/package.d
* iff filename/package.d is a file
*/
const ni = FileName.combine(filename, package_di);
if (FileName.exists(ni) == 1)
return ni;
FileName.free(ni.ptr);

const n = FileName.combine(filename, package_d);
if (FileName.exists(n) == 1)
return n;
FileName.free(n.ptr);
}
if (FileName.absolute(filename))
return null;
if (!path.length)
return null;
foreach (entry; path)
{
const p = entry.toDString();

const(char)[] n = FileName.combine(p, sdi);
if (FileName.exists(n) == 1) {
return n;
}
FileName.free(n.ptr);

n = FileName.combine(p, sd);
if (FileName.exists(n) == 1) {
return n;
}
FileName.free(n.ptr);

n = FileName.combine(p, si);
if (FileName.exists(n) == 1) {
return n;
}
FileName.free(n.ptr);

n = FileName.combine(p, sc);
if (FileName.exists(n) == 1) {
return n;
}
FileName.free(n.ptr);

const b = FileName.removeExt(filename);
n = FileName.combine(p, b);
FileName.free(b.ptr);
if (FileName.exists(n) == 2)
{
const n2i = FileName.combine(n, package_di);
if (FileName.exists(n2i) == 1)
return n2i;
FileName.free(n2i.ptr);
const n2 = FileName.combine(n, package_d);
if (FileName.exists(n2) == 1) {
return n2;
}
FileName.free(n2.ptr);
}
FileName.free(n.ptr);
}
return null;
}

// function used to call semantic3 on a module's dependencies
void semantic3OnDependencies(Module m)
{
Expand Down Expand Up @@ -414,7 +308,7 @@ extern (C++) class Package : ScopeDsymbol
packages ~= s.ident;
reverse(packages);

if (lookForSourceFile(getFilename(packages, ident), global.path ? (*global.path)[] : null))
if (FileManager.lookForSourceFile(getFilename(packages, ident), global.path ? (*global.path)[] : null))
Module.load(Loc.initial, packages, this.ident);
else
isPkgMod = PKG.package_;
Expand Down Expand Up @@ -612,7 +506,7 @@ extern (C++) final class Module : Package
// foo\bar\baz
const(char)[] filename = getFilename(packages, ident);
// Look for the source file
if (const result = lookForSourceFile(filename, global.path ? (*global.path)[] : null))
if (const result = FileManager.lookForSourceFile(filename, global.path ? (*global.path)[] : null))
filename = result; // leaks

auto m = new Module(loc, filename, ident, 0, 0);
Expand Down Expand Up @@ -781,14 +675,25 @@ extern (C++) final class Module : Package
return true; // already read

//printf("Module::read('%s') file '%s'\n", toChars(), srcfile.toChars());
auto readResult = File.read(srcfile.toChars());

if (global.params.emitMakeDeps)
{
global.params.makeDeps.push(srcfile.toChars());
}

return loadSourceBuffer(loc, readResult);
if (auto readResult = FileManager.fileManager.lookup(srcfile))
{
srcBuffer = readResult;
return true;
}

auto readResult = File.read(srcfile.toChars());
if (loadSourceBuffer(loc, readResult))
{
FileManager.fileManager.add(srcfile, srcBuffer);
return true;
}
return false;
}

/// syntactic parse
Expand Down
42 changes: 23 additions & 19 deletions src/dmd/errors.d
Original file line number Diff line number Diff line change
Expand Up @@ -387,29 +387,33 @@ private void verrorPrint(const ref Loc loc, Color headerColor, const(char)* head
!loc.filename.strstr(".d-mixin-") &&
!global.params.mixinOut)
{
import dmd.filecache : FileCache;
auto fllines = FileCache.fileCache.addOrGetFile(loc.filename.toDString());

if (loc.linnum - 1 < fllines.lines.length)
import dmd.file_manager : FileManager;
import dmd.root.filename : FileName;
const fileName = FileName(loc.filename.toDString);
if (auto file = FileManager.fileManager.lookup(fileName))
{
auto line = fllines.lines[loc.linnum - 1];
if (loc.charnum < line.length)
const(char)[][] lines = FileManager.fileManager.getLines(fileName);
if (loc.linnum - 1 < lines.length)
{
fprintf(stderr, "%.*s\n", cast(int)line.length, line.ptr);
// The number of column bytes and the number of display columns
// occupied by a character are not the same for non-ASCII charaters.
// https://issues.dlang.org/show_bug.cgi?id=21849
size_t c = 0;
while (c < loc.charnum - 1)
auto line = lines[loc.linnum - 1];
if (loc.charnum < line.length)
{
import dmd.utf : utf_decodeChar;
dchar u;
const msg = utf_decodeChar(line, c, u);
assert(msg is null, msg);
fputc(' ', stderr);
fprintf(stderr, "%.*s\n", cast(int)line.length, line.ptr);
// The number of column bytes and the number of display columns
// occupied by a character are not the same for non-ASCII charaters.
// https://issues.dlang.org/show_bug.cgi?id=21849
size_t c = 0;
while (c < loc.charnum - 1)
{
import dmd.utf : utf_decodeChar;
dchar u;
const msg = utf_decodeChar(line, c, u);
assert(msg is null, msg);
fputc(' ', stderr);
}
fputc('^', stderr);
fputc('\n', stderr);
}
fputc('^', stderr);
fputc('\n', stderr);
}
}
}
Expand Down
27 changes: 20 additions & 7 deletions src/dmd/expressionsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import dmd.dtemplate;
import dmd.errors;
import dmd.escape;
import dmd.expression;
import dmd.file_manager;
import dmd.func;
import dmd.globals;
import dmd.hdrgen;
Expand Down Expand Up @@ -6051,17 +6052,29 @@ private extern (C++) final class ExpressionSemanticVisitor : Visitor
}

{
auto readResult = File.read(name);
if (!readResult.success)
auto fileName = FileName(name.toDString);
if (auto fmResult = FileManager.fileManager.lookup(fileName))
{
e.error("cannot read file `%s`", name);
return setError();
se = new StringExp(e.loc, fmResult.data);
}
else
{
// take ownership of buffer (probably leaking)
auto data = readResult.extractSlice();
se = new StringExp(e.loc, data);
auto readResult = File.read(name);
if (!readResult.success)
{
e.error("cannot read file `%s`", name);
return setError();
}
else
{
// take ownership of buffer (probably leaking)
auto data = readResult.extractSlice();
se = new StringExp(e.loc, data);

FileBuffer* fileBuffer = FileBuffer.create();
fileBuffer.data = data;
FileManager.fileManager.add(fileName, fileBuffer);
}
}
}
result = se.expressionSemantic(sc);
Expand Down
Loading

0 comments on commit 97c5a0b

Please sign in to comment.