Skip to content

Commit

Permalink
Merge pull request #18 from atilaneves/appveyor-get-temp-file-name
Browse files Browse the repository at this point in the history
Appveyor get temp file name
  • Loading branch information
atilaneves committed Oct 31, 2019
2 parents 34ed05b + ac59808 commit c8a02b0
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 54 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Expand Up @@ -3,12 +3,12 @@ sudo: false

matrix:
include:
- d: dmd-2.088.0
- d: dmd-2.088.1
- d: dmd-2.087.1
- d: dmd-2.086.0
- d: ldc-1.18.0
- d: ldc-1.17.0
- d: ldc-1.16.0
- d: ldc-1.15.0

addons:
apt:
Expand Down
2 changes: 1 addition & 1 deletion dub.selections.json
@@ -1,6 +1,6 @@
{
"fileVersion": 1,
"versions": {
"unit-threaded": "0.10.3"
"unit-threaded": "0.10.4"
}
}
52 changes: 1 addition & 51 deletions source/clang/package.d
Expand Up @@ -5,57 +5,6 @@ import clang.c.index;
import clang.c.util: EnumD;


immutable bool[string] gPredefinedCursors;

version (Windows)
extern(C) private int _mktemp_s(char* nameTemplate, size_t sizeInChars) nothrow @safe @nogc;

shared static this() nothrow {
try {

const fileName = getTempFileName;
{
// create an empty file
import std.stdio: File;
auto f = File(fileName, "w");
f.writeln;
f.flush;
f.detach;
}

auto tu = parse(fileName,
["-xc"],
TranslationUnitFlags.DetailedPreprocessingRecord);
foreach(cursor; tu.cursor.children) {
gPredefinedCursors[cursor.spelling] = true;
}

} catch(Exception e) {
import std.stdio: stderr;
try
stderr.writeln("Error initialising libclang: ", e);
catch(Exception _) {}
}
}


string getTempFileName() @trusted {
import std.file: tempDir;
import std.path: buildPath;
import std.string: fromStringz;

char[] tmpnamBuf = buildPath(tempDir, "libclangXXXXXX\0").dup;

version (Posix) {
import core.sys.posix.stdlib: mkstemp;
mkstemp(&tmpnamBuf[0]);
}
else version (Windows)
_mktemp_s(&tmpnamBuf[0], tmpnamBuf.length);

return tmpnamBuf.idup;
}


mixin EnumD!("TranslationUnitFlags", CXTranslationUnit_Flags, "CXTranslationUnit_");
mixin EnumD!("Language", CXLanguageKind, "CXLanguage_");
Expand Down Expand Up @@ -354,6 +303,7 @@ struct Cursor {
}

bool isPredefined() @safe pure nothrow const {
import clang.static_: gPredefinedCursors;
return (spelling in gPredefinedCursors) !is null;
}

Expand Down
39 changes: 39 additions & 0 deletions source/clang/static_.d
@@ -0,0 +1,39 @@
/**
Static constructors
*/
module clang.static_;


immutable bool[string] gPredefinedCursors;


shared static this() nothrow {
try {

import clang: parse, TranslationUnitFlags;
import clang.util: getTempFileName;

const fileName = getTempFileName;
{
// create an empty file
import std.stdio: File;
auto f = File(fileName, "w");
f.writeln;
f.flush;
f.detach;
}

auto tu = parse(fileName,
["-xc"],
TranslationUnitFlags.DetailedPreprocessingRecord);
foreach(cursor; tu.cursor.children) {
gPredefinedCursors[cursor.spelling] = true;
}

} catch(Exception e) {
import std.stdio: stderr;
try
stderr.writeln("Error initialising libclang: ", e);
catch(Exception _) {}
}
}
31 changes: 31 additions & 0 deletions source/clang/util.d
@@ -1,6 +1,11 @@
module clang.util;


version (Windows) {
extern(C) private int _mktemp_s(char* nameTemplate, size_t sizeInChars) nothrow @safe @nogc;
}


/**
Makes a member variable lazy so that it's only computed if necessary.
Very hacky, uses undefined behaviour by casting const away as if
Expand Down Expand Up @@ -38,3 +43,29 @@ mixin template Lazy(alias memberVariable) {
//pragma(msg, str);
mixin(str);
}


/**
Returns a suitable temporary file name
*/
string getTempFileName() @trusted {
import std.file: tempDir;
import std.path: buildPath;
import std.string: fromStringz;
import std.process: environment;

const dir = environment.get("APPVEYOR", null) is null
? tempDir
: ".";

char[] tmpnamBuf = buildPath(dir, "libclangXXXXXX\0").dup;

version (Posix) {
import core.sys.posix.stdlib: mkstemp;
mkstemp(&tmpnamBuf[0]);
}
else version (Windows)
_mktemp_s(&tmpnamBuf[0], tmpnamBuf.length);

return tmpnamBuf.idup;
}

0 comments on commit c8a02b0

Please sign in to comment.