Skip to content

Commit

Permalink
Merge pull request #7798 from marler8997/bug18322
Browse files Browse the repository at this point in the history
Fix issue 18322: __FILE_FULL_PATH__ doesn't work as a template parameter
merged-on-behalf-of: Mike Franklin <JinShil@users.noreply.github.com>
  • Loading branch information
dlang-bot authored Feb 11, 2018
2 parents 9691eba + 1927521 commit 6a2db25
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 41 deletions.
1 change: 0 additions & 1 deletion src/dmd/astbase.d
Original file line number Diff line number Diff line change
Expand Up @@ -1432,7 +1432,6 @@ struct ASTBase

File* srcfile;
const(char)* arg;
const(char)* srcfilePath;

extern (D) this(const(char)* filename, Identifier ident, int doDocComment, int doHdrGen)
{
Expand Down
23 changes: 3 additions & 20 deletions src/dmd/dmodule.d
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,14 @@ version(Windows) {
* 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.
* Output:
* path the path where the file was found if it was not the current directory
* Input:
* filename as supplied by the user
* global.path
* Returns:
* NULL if it's not different from filename.
*/
private const(char)* lookForSourceFile(const(char)** path, const(char)* filename)
private const(char)* lookForSourceFile(const(char)* filename)
{
*path = null;
/* Search along global.path for .di file, then .d file.
*/
const(char)* sdi = FileName.forceExt(filename, global.hdr_ext);
Expand Down Expand Up @@ -94,13 +91,11 @@ private const(char)* lookForSourceFile(const(char)** path, const(char)* filename
const(char)* p = (*global.path)[i];
const(char)* n = FileName.combine(p, sdi);
if (FileName.exists(n) == 1) {
*path = p;
return n;
}
FileName.free(n);
n = FileName.combine(p, sd);
if (FileName.exists(n) == 1) {
*path = p;
return n;
}
FileName.free(n);
Expand All @@ -115,7 +110,6 @@ private const(char)* lookForSourceFile(const(char)** path, const(char)* filename
FileName.free(n2i);
const(char)* n2 = FileName.combine(n, "package.d");
if (FileName.exists(n2) == 1) {
*path = p;
return n2;
}
FileName.free(n2);
Expand Down Expand Up @@ -314,7 +308,6 @@ extern (C++) final class Module : Package
const(char)* arg; // original argument name
ModuleDeclaration* md; // if !=null, the contents of the ModuleDeclaration declaration
File* srcfile; // input source file
const(char)* srcfilePath; // the path prefix to the srcfile if it applies
File* objfile; // output .obj file
File* hdrfile; // 'header' file
File* docfile; // output documentation file
Expand Down Expand Up @@ -421,9 +414,6 @@ extern (C++) final class Module : Package
fatal();
}
srcfile = new File(srcfilename);
if(!FileName.absolute(srcfilename)) {
srcfilePath = getcwd(null, 0);
}
objfile = setOutfile(global.params.objname, global.params.objdir, filename, global.obj_ext);
if (doDocComment)
setDocfile();
Expand Down Expand Up @@ -503,17 +493,10 @@ extern (C++) final class Module : Package
m.loc = loc;
/* Look for the source file
*/
const(char)* path;
const(char)* result = lookForSourceFile(&path, filename);
const(char)* result = lookForSourceFile(filename);
if (result)
{
m.srcfile = new File(result);
if(path) {
m.srcfilePath = path;
} else if(!FileName.absolute(result)) {
m.srcfilePath = getcwd(null, 0);
}
}

if (!m.read(loc))
return null;
if (global.params.verbose)
Expand Down
9 changes: 6 additions & 3 deletions src/dmd/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -1823,7 +1823,7 @@ extern (C++) abstract class Expression : RootObject
}

/****************************************
* Resolve __FILE__, __LINE__, __MODULE__, __FUNCTION__, __PRETTY_FUNCTION__ to loc.
* Resolve __FILE__, __LINE__, __MODULE__, __FUNCTION__, __PRETTY_FUNCTION__, __FILE_FULL_PATH__ to loc.
*/
Expression resolveLoc(const ref Loc loc, Scope* sc)
{
Expand Down Expand Up @@ -7081,9 +7081,12 @@ extern (C++) final class FileInitExp : DefaultInitExp
override Expression resolveLoc(const ref Loc loc, Scope* sc)
{
//printf("FileInitExp::resolve() %s\n", toChars());
const(char)* s = loc.isValid() ? loc.filename : sc._module.ident.toChars();
const(char)* s;
if (subop == TOK.fileFullPath)
s = FileName.combine(sc._module.srcfilePath, s);
s = FileName.toAbsolute(loc.isValid() ? loc.filename : sc._module.srcfile.name.toChars());
else
s = loc.isValid() ? loc.filename : sc._module.ident.toChars();

Expression e = new StringExp(loc, cast(char*)s);
e = e.expressionSemantic(sc);
e = e.castTo(sc, type);
Expand Down
2 changes: 1 addition & 1 deletion src/dmd/globals.d
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ alias d_uns64 = uint64_t;
// file location
struct Loc
{
const(char)* filename;
const(char)* filename; // either absolute or relative to cwd
uint linnum;
uint charnum;

Expand Down
2 changes: 1 addition & 1 deletion src/dmd/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ struct DArray
// file location
struct Loc
{
const char *filename;
const char *filename; // either absolute or relative to cwd
unsigned linnum;
unsigned charnum;

Expand Down
17 changes: 5 additions & 12 deletions src/dmd/parse.d
Original file line number Diff line number Diff line change
Expand Up @@ -7267,18 +7267,11 @@ final class Parser(AST) : Lexer
break;
}
case TOK.fileFullPath:
{
const(char)* srcfile = mod.srcfile.name.toChars();
const(char)* s;
if(loc.filename && !FileName.equals(loc.filename, srcfile)) {
s = loc.filename;
} else {
s = FileName.combine(mod.srcfilePath, srcfile);
}
e = new AST.StringExp(loc, cast(char*)s);
nextToken();
break;
}
assert(loc.isValid(), "__FILE_FULL_PATH__ does not work with an invalid location");
e = new AST.StringExp(loc, cast(char*)FileName.toAbsolute(loc.filename));
nextToken();
break;

case TOK.line:
e = new AST.IntegerExp(loc, loc.linnum, AST.Type.tint32);
nextToken();
Expand Down
17 changes: 16 additions & 1 deletion src/dmd/root/filename.d
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ nothrow
version (Windows) extern (C) int stricmp(const char*, const char*) pure;
version (Windows) extern (Windows) DWORD GetFullPathNameW(LPCWSTR, DWORD, LPWSTR, LPWSTR*) @nogc;
version (Windows) extern (Windows) void SetLastError(DWORD) @nogc;
version (Windows) extern (C) char* getcwd(char* buffer, size_t maxlen);
version (Posix) extern (C) char* canonicalize_file_name(const char*);
version (Posix) import core.sys.posix.unistd : getcwd;
}

alias Strings = Array!(const(char)*);
alias Files = Array!(File*);

Expand Down Expand Up @@ -98,6 +99,20 @@ nothrow:
}
}

/**
Return the given name as an absolute path
Params:
name = path
base = the absolute base to prefix name with if it is relative
Returns: name as an absolute path relative to base
*/
extern (C++) static const(char)* toAbsolute(const(char)* name, const(char)* base = null)
{
return absolute(name) ? name : combine(base ? base : getcwd(null, 0), name);
}

/********************************
* Determine file name extension as slice of input.
* Params:
Expand Down
1 change: 1 addition & 0 deletions src/dmd/root/filename.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct FileName
int compare(RootObject *obj);
static int compare(const char *name1, const char *name2);
static bool absolute(const char *name);
static const char *toAbsolute(const char *name, const char *base = NULL);
static const char *ext(const char *);
const char *ext();
static const char *removeExt(const char *str);
Expand Down
4 changes: 2 additions & 2 deletions test/compilable/line.d
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ static assert(__FILE_FULL_PATH__[$-__FILE__.length..$] == __FILE__);

static assert(__LINE__ == 101);
static assert(__FILE__ == "newfile.d");
static assert(__FILE_FULL_PATH__ == "newfile.d");
static assert(__FILE_FULL_PATH__[$ - 9 .. $] == "newfile.d");

# line 200

static assert(__LINE__ == 201);
static assert(__FILE__ == "newfile.d");
static assert(__FILE_FULL_PATH__ == "newfile.d");
static assert(__FILE_FULL_PATH__[$ - 9 .. $] == "newfile.d");


14 changes: 14 additions & 0 deletions test/runnable/imports/test18322import.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module test18322import;
void fun(string templateFileFullPath = __FILE_FULL_PATH__,
string templateFile = __FILE__)(string expectedFilename, string fileFullPath = __FILE_FULL_PATH__)
{
// make sure it is an absolute path
version(Windows)
assert(fileFullPath[1..3] == ":\\");
else
assert(fileFullPath[0] == '/');

assert(templateFileFullPath == fileFullPath);
assert(fileFullPath[$ - expectedFilename.length .. $] == expectedFilename);
assert(fileFullPath[$ - templateFile.length .. $] == templateFile);
}
21 changes: 21 additions & 0 deletions test/runnable/test18322.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
REQUIRED_ARGS: -Irunnable/imports
COMPILED_IMPORTS: imports/test18322import.d
PERMUTE_ARGS:
*/
import test18322import;
void main(){
version(Windows)
auto sep = "\\";
else
auto sep = "/";

auto filename = "runnable" ~ sep ~ "test18322.d";

fun(filename);
mixin(`fun(filename ~ "-mixin-16");`);

#line 100 "poundlinefile.d"
fun("poundlinefile.d");
mixin(`fun("poundlinefile.d-mixin-101");`);
}

0 comments on commit 6a2db25

Please sign in to comment.