Skip to content

Commit

Permalink
Fixes order of output file (statements are now in original 'C' order).
Browse files Browse the repository at this point in the history
Extends testing framework (adds functions that partially replaces cucumber).
  • Loading branch information
ciechowoj committed Mar 14, 2016
1 parent 8b70915 commit 951a36c
Show file tree
Hide file tree
Showing 26 changed files with 1,405 additions and 319 deletions.
99 changes: 99 additions & 0 deletions clang/Cursor.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
*/
module clang.Cursor;

import std.array : appender, Appender;

import mambo.core._;

import clang.c.Index;
import clang.Index;
import clang.File;
import clang.SourceLocation;
import clang.SourceRange;
import clang.Token;
Expand Down Expand Up @@ -50,6 +53,11 @@ struct Cursor
return SourceLocation(clang_getCursorLocation(cx));
}

@property File file () const
{
return location.spelling.file;
}

@property TokenRange tokens() const
{
import std.algorithm.mutation : stripRight;
Expand Down Expand Up @@ -140,6 +148,16 @@ struct Cursor
return app.data;
}

Cursor semanticParent() const
{
return Cursor(clang_getCursorSemanticParent(cast(CXCursor) cx));
}

Cursor lexicalParent() const
{
return Cursor(clang_getCursorLexicalParent(cast(CXCursor) cx));
}

@property CXLanguageKind language ()
{
return clang_getCursorLanguage(cx);
Expand Down Expand Up @@ -191,6 +209,87 @@ struct Cursor
{
return Cursor(clang_getCursorDefinition(cast(CXCursor) cx));
}

void dumpAST(ref Appender!string result, size_t indent, File* file)
{
import std.format;
import std.array : replicate;
import std.algorithm.comparison : min;

string stripPrefix(string x)
{
immutable string prefix = "CXCursor_";
immutable size_t prefixSize = prefix.length;
return x.startsWith(prefix) ? x[prefixSize..$] : x;
}

string prettyTokens(TokenRange tokens, size_t limit = 5)
{
string prettyToken(Token token)
{
immutable string prefix = "CXToken_";
immutable size_t prefixSize = prefix.length;
auto x = to!string(token.kind);
return "%s \"%s\"".format(
x.startsWith(prefix) ? x[prefixSize..$] : x,
token.spelling);
}

auto result = appender!string("[");

if (tokens.length != 0)
{
result.put(prettyToken(tokens[0]));

foreach (Token token; tokens[1..min($, limit)])
{
result.put(", ");
result.put(prettyToken(token));
}
}

if (tokens.length > limit)
result.put(", ..]");
else
result.put("]");

return result.data;
}

immutable size_t step = 4;

auto text = "%s \"%s\" [%d..%d] %s\n".format(
stripPrefix(to!string(kind)),
spelling,
extent.start.offset,
extent.end.offset,
prettyTokens(tokens));

result.put(" ".replicate(indent));
result.put(text);

if (file)
{
foreach (cursor, _; all)
{
if (!cursor.isPredefined() && cursor.file == *file)
cursor.dumpAST(result, indent + step);
}
}
else
{
foreach (cursor, _; all)
{
if (!cursor.isPredefined())
cursor.dumpAST(result, indent + step);
}
}
}

void dumpAST(ref Appender!string result, size_t indent)
{
dumpAST(result, indent, null);
}
}

struct ObjcCursor
Expand Down
104 changes: 103 additions & 1 deletion clang/Diagnostic.d
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
module clang.Diagnostic;

import std.typecons : RefCounted;

import clang.c.Index;
import clang.Util;

Expand All @@ -22,4 +24,104 @@ struct Diagnostic
{
return clang_getDiagnosticSeverity(cx);
}
}

@property toString()
{
return format;
}
}

struct DiagnosticSet
{
private struct Container
{
CXDiagnosticSet set;

~this()
{
if (set != null)
{
clang_disposeDiagnosticSet(set);
}
}
}

private RefCounted!Container container;
private size_t begin;
private size_t end;

private static RefCounted!Container makeContainer(
CXDiagnosticSet set)
{
RefCounted!Container result;
result.set = set;
return result;
}

private this(
RefCounted!Container container,
size_t begin,
size_t end)
{
this.container = container;
this.begin = begin;
this.end = end;
}

this(CXDiagnosticSet set)
{
container = makeContainer(set);
begin = 0;
end = clang_getNumDiagnosticsInSet(container.set);
}

@property bool empty() const
{
return begin >= end;
}

@property Diagnostic front()
{
return Diagnostic(clang_getDiagnosticInSet(container.set, cast(uint) begin));
}

@property Diagnostic back()
{
return Diagnostic(clang_getDiagnosticInSet(container.set, cast(uint) (end - 1)));
}

@property void popFront()
{
++begin;
}

@property void popBack()
{
--end;
}

@property DiagnosticSet save()
{
return this;
}

@property size_t length() const
{
return end - begin;
}

Diagnostic opIndex(size_t index)
{
return Diagnostic(clang_getDiagnosticInSet(container.set, cast(uint) (begin + index)));
}

DiagnosticSet opSlice(size_t begin, size_t end)
{
return DiagnosticSet(container, this.begin + begin, this.begin + end);
}

size_t opDollar() const
{
return length;
}
}
25 changes: 23 additions & 2 deletions clang/Token.d
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Version: Initial created: Feb 14, 2016
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
*/

module clang.Token;

import std.typecons;
Expand Down Expand Up @@ -97,7 +97,18 @@ struct TokenRange
return result;
}

this(CXTranslationUnit translationUnit,
private this(
const RefCounted!(Token.Container) container,
size_t begin,
size_t end)
{
this.container = container;
this.begin = begin;
this.end = end;
}

this(
CXTranslationUnit translationUnit,
CXToken* tokens,
ulong numTokens)
{
Expand Down Expand Up @@ -145,4 +156,14 @@ struct TokenRange
{
return Token(container, begin + index);
}

TokenRange opSlice(size_t begin, size_t end) const
{
return TokenRange(container, this.begin + begin, this.begin + end);
}

size_t opDollar() const
{
return length;
}
}
44 changes: 43 additions & 1 deletion clang/TranslationUnit.d
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ struct TranslationUnit
{
mixin CX;

static TranslationUnit parse (Index index, string sourceFilename, string[] commandLineArgs,
static TranslationUnit parse (
Index index,
string sourceFilename,
string[] commandLineArgs,
CXUnsavedFile[] unsavedFiles = null,
uint options = CXTranslationUnit_Flags.CXTranslationUnit_DetailedPreprocessingRecord)
{
Expand Down Expand Up @@ -74,6 +77,16 @@ struct TranslationUnit
return DiagnosticVisitor(cx);
}

@property DiagnosticSet diagnosticSet ()
{
return DiagnosticSet(clang_getDiagnosticSetFromTU(cx));
}

@property size_t numDiagnostics ()
{
return clang_getNumDiagnostics(cx);
}

@property DeclarationVisitor declarations ()
{
return DeclarationVisitor(clang_getTranslationUnitCursor(cx));
Expand All @@ -84,11 +97,40 @@ struct TranslationUnit
return File(clang_getFile(cx, filename.toStringz));
}

@property string spelling ()
{
return toD(clang_getTranslationUnitSpelling(cx));
}

File file ()
{
return file(spelling);
}

@property Cursor cursor ()
{
auto r = clang_getTranslationUnitCursor(cx);
return Cursor(r);
}

string dumpAST (bool skipIncluded = false)
{
import std.array : appender;

auto result = appender!string();

if (skipIncluded)
{
File file = this.file;
cursor.dumpAST(result, 0, &file);
}
else
{
cursor.dumpAST(result, 0);
}

return result.data;
}
}

struct DiagnosticVisitor
Expand Down
2 changes: 1 addition & 1 deletion clang/Util.d
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class NamedTempFileException : object.Exception
}
}

File namedTempFile(string prefix, string suffix)
File namedTempFile(string prefix, string suffix)
{
import std.random;
import std.file;
Expand Down
Loading

0 comments on commit 951a36c

Please sign in to comment.