Skip to content

Commit

Permalink
Updating ClangSharp to use libClangSharp where applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
tannergooding committed Nov 18, 2019
1 parent 9d4ab6c commit 08c7c2d
Show file tree
Hide file tree
Showing 16 changed files with 99 additions and 158 deletions.
3 changes: 2 additions & 1 deletion Directory.Build.targets
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@

<!-- Package versions for package references across all projects -->
<ItemGroup>
<PackageReference Update="libclang" Version="9.0.0" />
<PackageReference Update="libClang" Version="9.0.0" />
<PackageReference Update="libClangSharp" Version="9.0.0-beta1" />
<PackageReference Update="Microsoft.Bcl.HashCode" Version="1.0.0-preview6.19303.8" />
<PackageReference Update="Microsoft.Net.Compilers.Toolset" Version="3.2.0-beta1-final" />
<PackageReference Update="Microsoft.NET.Test.Sdk" Version="16.1.1" />
Expand Down
76 changes: 48 additions & 28 deletions sources/ClangSharp.PInvokeGenerator/PInvokeGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ private string GetCursorName(NamedDecl namedDecl)
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported anonymous named declaration: '{namedDecl.KindSpelling}'.", namedDecl);
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported anonymous named declaration: '{namedDecl.CursorKindSpelling}'.", namedDecl);
}
}

Expand Down Expand Up @@ -968,7 +968,7 @@ private void Visit(Cursor cursor)
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported cursor: '{cursor.KindSpelling}'. Generated bindings may be incomplete.", cursor);
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported cursor: '{cursor.CursorKindSpelling}'. Generated bindings may be incomplete.", cursor);
}
}

Expand All @@ -981,7 +981,7 @@ private void VisitBinaryOperator(BinaryOperator binaryOperator)
{
Visit(binaryOperator.LHS);
_outputBuilder.Write(' ');
_outputBuilder.Write(binaryOperator.Opcode);
_outputBuilder.Write(binaryOperator.OpcodeStr);
_outputBuilder.Write(' ');
Visit(binaryOperator.RHS);
}
Expand All @@ -1006,7 +1006,7 @@ private void VisitCallExpr(CallExpr callExpr)
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported callee declaration: '{calleeDecl.KindSpelling}'. Generated bindings may be incomplete.", calleeDecl);
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported callee declaration: '{calleeDecl.CursorKindSpelling}'. Generated bindings may be incomplete.", calleeDecl);
}
}

Expand All @@ -1033,13 +1033,13 @@ private void VisitDecl(Decl decl)
{
VisitNamedDecl(namedDecl);
}
else if (decl.Kind == CXCursorKind.CXCursor_UnexposedDecl)
else if (decl.CursorKind == CXCursorKind.CXCursor_UnexposedDecl)
{
VisitUnexposedDecl(decl);
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported declaration: '{decl.KindSpelling}'. Generated bindings may be incomplete.", decl);
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported declaration: '{decl.CursorKindSpelling}'. Generated bindings may be incomplete.", decl);
}
}

Expand All @@ -1059,7 +1059,7 @@ private void VisitDeclaratorDecl(DeclaratorDecl declaratorDecl)
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported declarator declaration: '{declaratorDecl.KindSpelling}'. Generated bindings may be incomplete.", declaratorDecl);
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported declarator declaration: '{declaratorDecl.CursorKindSpelling}'. Generated bindings may be incomplete.", declaratorDecl);
}
}

Expand Down Expand Up @@ -1154,13 +1154,13 @@ private void VisitExpr(Expr expr)
{
VisitUnaryOperator(unaryOperator);
}
else if (expr.Kind == CXCursorKind.CXCursor_UnexposedExpr)
else if (expr.CursorKind == CXCursorKind.CXCursor_UnexposedExpr)
{
VisitUnexposedExpr(expr);
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported expression: '{expr.KindSpelling}'. Generated bindings may be incomplete.", expr);
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported expression: '{expr.CursorKindSpelling}'. Generated bindings may be incomplete.", expr);
}
}

Expand Down Expand Up @@ -1330,7 +1330,7 @@ private void VisitNamedDecl(NamedDecl namedDecl)
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported named declaration: '{namedDecl.KindSpelling}'. Generated bindings may be incomplete.", namedDecl);
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported named declaration: '{namedDecl.CursorKindSpelling}'. Generated bindings may be incomplete.", namedDecl);
}
}

Expand All @@ -1355,7 +1355,7 @@ private void VisitParmVarDecl(ParmVarDecl parmVarDecl)
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported parameter variable declaration parent: '{cursorParent.KindSpelling}'. Generated bindings may be incomplete.", cursorParent);
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported parameter variable declaration parent: '{cursorParent.CursorKindSpelling}'. Generated bindings may be incomplete.", cursorParent);
}
}

Expand Down Expand Up @@ -1532,7 +1532,7 @@ private void VisitRecordDecl(RecordDecl recordDecl)

private void VisitRef(Ref @ref)
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported reference: '{@ref.KindSpelling}'. Generated bindings may be incomplete.", @ref);
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported reference: '{@ref.CursorKindSpelling}'. Generated bindings may be incomplete.", @ref);
}

private void VisitReturnStmt(ReturnStmt returnStmt)
Expand Down Expand Up @@ -1562,7 +1562,7 @@ private void VisitStmt(Stmt stmt)
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported statement: '{stmt.KindSpelling}'. Generated bindings may be incomplete.", stmt);
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported statement: '{stmt.CursorKindSpelling}'. Generated bindings may be incomplete.", stmt);
}
}

Expand All @@ -1589,7 +1589,7 @@ private void VisitTagDecl(TagDecl tagDecl)
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported tag declaration: '{tagDecl.KindSpelling}'. Generated bindings may be incomplete.", tagDecl);
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported tag declaration: '{tagDecl.CursorKindSpelling}'. Generated bindings may be incomplete.", tagDecl);
}
}

Expand All @@ -1605,7 +1605,7 @@ private void VisitTypeDecl(TypeDecl typeDecl)
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported type declaration: '{typeDecl.KindSpelling}'. Generated bindings may be incomplete.", typeDecl);
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported type declaration: '{typeDecl.CursorKindSpelling}'. Generated bindings may be incomplete.", typeDecl);
}
}

Expand All @@ -1617,7 +1617,7 @@ private void VisitTypedefNameDecl(TypedefNameDecl typedefNameDecl)
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported typedef name declaration: '{typedefNameDecl.KindSpelling}'. Generated bindings may be incomplete.", typedefNameDecl);
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported typedef name declaration: '{typedefNameDecl.CursorKindSpelling}'. Generated bindings may be incomplete.", typedefNameDecl);
}
}

Expand Down Expand Up @@ -1705,15 +1705,35 @@ private void VisitTypedefDeclForPointeeType(TypedefDecl typedefDecl, Type pointe

private void VisitUnaryOperator(UnaryOperator unaryOperator)
{
if (unaryOperator.IsPrefix)
switch (unaryOperator.Opcode)
{
_outputBuilder.Write(unaryOperator.Opcode);
Visit(unaryOperator.SubExpr);
}
else
{
Visit(unaryOperator.SubExpr);
_outputBuilder.Write(unaryOperator.Opcode);
case CX_UnaryOperatorKind.CX_UO_PostInc:
case CX_UnaryOperatorKind.CX_UO_PostDec:
{
Visit(unaryOperator.SubExpr);
_outputBuilder.Write(unaryOperator.OpcodeStr);
break;
}

case CX_UnaryOperatorKind.CX_UO_PreInc:
case CX_UnaryOperatorKind.CX_UO_PreDec:
case CX_UnaryOperatorKind.CX_UO_AddrOf:
case CX_UnaryOperatorKind.CX_UO_Deref:
case CX_UnaryOperatorKind.CX_UO_Plus:
case CX_UnaryOperatorKind.CX_UO_Minus:
case CX_UnaryOperatorKind.CX_UO_Not:
case CX_UnaryOperatorKind.CX_UO_LNot:
{
_outputBuilder.Write(unaryOperator.OpcodeStr);
Visit(unaryOperator.SubExpr);
break;
}

default:
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported unary operator opcode: '{unaryOperator.OpcodeStr}'. Generated bindings may be incomplete.", unaryOperator);
break;
}
}
}

Expand All @@ -1729,7 +1749,7 @@ private void VisitValueDecl(ValueDecl valueDecl)
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported value declaration: '{valueDecl.KindSpelling}'. Generated bindings may be incomplete.", valueDecl);
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported value declaration: '{valueDecl.CursorKindSpelling}'. Generated bindings may be incomplete.", valueDecl);
}
}

Expand All @@ -1741,7 +1761,7 @@ private void VisitValueStmt(ValueStmt valueStmt)
}
else
{
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported value statement: '{valueStmt.KindSpelling}'. Generated bindings may be incomplete.", valueStmt);
AddDiagnostic(DiagnosticLevel.Error, $"Unsupported value statement: '{valueStmt.CursorKindSpelling}'. Generated bindings may be incomplete.", valueStmt);
}
}

Expand Down Expand Up @@ -1774,7 +1794,7 @@ private void VisitVarDecl(VarDecl varDecl)

private void VisitUnexposedDecl(Decl unexposedDecl)
{
Debug.Assert(unexposedDecl.Kind == CXCursorKind.CXCursor_UnexposedDecl);
Debug.Assert(unexposedDecl.CursorKind == CXCursorKind.CXCursor_UnexposedDecl);

foreach (var decl in unexposedDecl.CursorChildren.OfType<Decl>())
{
Expand All @@ -1784,7 +1804,7 @@ private void VisitUnexposedDecl(Decl unexposedDecl)

private void VisitUnexposedExpr(Expr unexposedExpr)
{
Debug.Assert(unexposedExpr.Kind == CXCursorKind.CXCursor_UnexposedExpr);
Debug.Assert(unexposedExpr.CursorKind == CXCursorKind.CXCursor_UnexposedExpr);

foreach (var stmt in unexposedExpr.Children)
{
Expand Down
1 change: 1 addition & 0 deletions sources/ClangSharp/ClangSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<ItemGroup>
<PackageReference Include="libClang" />
<PackageReference Include="libClangSharp" />
<PackageReference Include="Microsoft.Bcl.HashCode" />
<PackageReference Include="System.Memory" />
</ItemGroup>
Expand Down
2 changes: 2 additions & 0 deletions sources/ClangSharp/Cursors/Attrs/Attr.cs
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,7 @@ private protected Attr(CXCursor handle, CXCursorKind expectedKind) : base(handle

return result;
}

public CX_AttrKind Kind => Handle.AttrKind;
}
}
20 changes: 6 additions & 14 deletions sources/ClangSharp/Cursors/Cursor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ private protected Cursor(CXCursor handle, CXCursorKind expectedKind)

public IReadOnlyList<Cursor> CursorChildren => _cursorChildren.Value;

public CXCursorKind CursorKind => Handle.Kind;

public string CursorKindSpelling => Handle.KindSpelling.ToString();

public Cursor CursorParent { get; private set; }

public CXSourceRange Extent => Handle.Extent;

public CXCursor Handle { get; }

public CXCursorKind Kind => Handle.Kind;

public string KindSpelling => Handle.KindSpelling.ToString();

public CXSourceLocation Location => Handle.Location;

public string Spelling => Handle.Spelling.ToString();
Expand All @@ -62,26 +62,18 @@ internal static Cursor Create(CXCursor handle)
{
Cursor result;

if (handle.IsDeclaration)
if (handle.IsDeclaration || handle.IsTranslationUnit)
{
result = Decl.Create(handle);
}
else if (handle.IsReference)
{
result = Ref.Create(handle);
}
else if (handle.IsExpression)
{
result = Expr.Create(handle);
}
else if (handle.IsStatement)
else if (handle.IsExpression || handle.IsStatement)
{
result = Stmt.Create(handle);
}
else if (handle.IsTranslationUnit)
{
result = new TranslationUnitDecl(handle);
}
else if (handle.IsAttribute)
{
result = Attr.Create(handle);
Expand Down
6 changes: 4 additions & 2 deletions sources/ClangSharp/Cursors/Decls/Decl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ private protected Decl(CXCursor handle, CXCursorKind expectedKind) : base(handle
{
_attrs = new Lazy<IReadOnlyList<Attr>>(() => CursorChildren.OfType<Attr>().ToList());
_canonicalDecl = new Lazy<Decl>(() => TranslationUnit.GetOrCreate<Decl>(Handle.CanonicalCursor));
_declContext = new Lazy<IDeclContext>(() => Create(Handle.SemanticParent) as IDeclContext);
_lexicalDeclContext = new Lazy<IDeclContext>(() => Create(Handle.LexicalParent) as IDeclContext);
_declContext = new Lazy<IDeclContext>(() => TranslationUnit.GetOrCreate<Decl>(Handle.SemanticParent) as IDeclContext);
_lexicalDeclContext = new Lazy<IDeclContext>(() => TranslationUnit.GetOrCreate<Decl>(Handle.LexicalParent) as IDeclContext);
_translationUnitDecl = new Lazy<TranslationUnitDecl>(() => TranslationUnit.GetOrCreate<TranslationUnitDecl>(Handle.TranslationUnit.Cursor));
}

Expand All @@ -41,6 +41,8 @@ private protected Decl(CXCursor handle, CXCursorKind expectedKind) : base(handle

public bool IsInvalidDecl => Handle.IsInvalidDeclaration;

public CX_DeclKind Kind => Handle.DeclKind;

public IDeclContext LexicalDeclContext => _lexicalDeclContext.Value;

public TranslationUnitDecl TranslationUnitDecl => _translationUnitDecl.Value;
Expand Down
4 changes: 2 additions & 2 deletions sources/ClangSharp/Cursors/Decls/ObjCMethodDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ internal ObjCMethodDecl(CXCursor handle, CXCursorKind expectedKind) : base(handl

public IReadOnlyList<Decl> Decls => _decls.Value;

public bool IsClassMethod() => Kind == CXCursorKind.CXCursor_ObjCClassMethodDecl;
public bool IsClassMethod() => CursorKind == CXCursorKind.CXCursor_ObjCClassMethodDecl;

public bool IsInstanceMethod() => Kind == CXCursorKind.CXCursor_ObjCInstanceMethodDecl;
public bool IsInstanceMethod() => CursorKind == CXCursorKind.CXCursor_ObjCInstanceMethodDecl;

public IDeclContext LexicalParent => LexicalDeclContext;

Expand Down
8 changes: 4 additions & 4 deletions sources/ClangSharp/Cursors/Decls/TagDecl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ private protected TagDecl(CXCursor handle, CXCursorKind expectedKind) : base(han

public TagDecl Definition => _definition.Value;

public bool IsClass => Kind == CXCursorKind.CXCursor_ClassDecl;
public bool IsClass => CursorKind == CXCursorKind.CXCursor_ClassDecl;

public bool IsEnum => Kind == CXCursorKind.CXCursor_EnumDecl;
public bool IsEnum => CursorKind == CXCursorKind.CXCursor_EnumDecl;

public bool IsStruct => Kind == CXCursorKind.CXCursor_StructDecl;
public bool IsStruct => CursorKind == CXCursorKind.CXCursor_StructDecl;

public bool IsUnion => Kind == CXCursorKind.CXCursor_UnionDecl;
public bool IsUnion => CursorKind == CXCursorKind.CXCursor_UnionDecl;

public IDeclContext LexicalParent => LexicalDeclContext;

Expand Down
19 changes: 3 additions & 16 deletions sources/ClangSharp/Cursors/Exprs/BinaryOperator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ namespace ClangSharp
public class BinaryOperator : Expr
{
private readonly Lazy<Expr> _lhs;
private readonly Lazy<string> _opcode;
private readonly Lazy<Expr> _rhs;

internal BinaryOperator(CXCursor handle) : this(handle, CXCursorKind.CXCursor_BinaryOperator)
Expand All @@ -22,27 +21,15 @@ private protected BinaryOperator(CXCursor handle, CXCursorKind expectedKind) : b
Debug.Assert(Children.Where((cursor) => cursor is Expr).Count() == 2);

_lhs = new Lazy<Expr>(() => Children.OfType<Expr>().First());
_opcode = new Lazy<string>(GetOpcode);
_rhs = new Lazy<Expr>(() => Children.OfType<Expr>().Last());
}

public Expr LHS => _lhs.Value;

public string Opcode => _opcode.Value;
public CX_BinaryOperatorKind Opcode => Handle.BinaryOperatorKind;

public Expr RHS => _rhs.Value;

protected virtual string GetOpcode()
{
var lhsTokens = Handle.TranslationUnit.Tokenize(LHS.Extent);

var tokens = Handle.TranslationUnit.Tokenize(Extent);
Debug.Assert(tokens.Length >= 3);
public string OpcodeStr => Handle.BinaryOperatorKindSpelling.ToString();

int operatorIndex = lhsTokens.Length;

Debug.Assert(tokens[operatorIndex].Kind == CXTokenKind.CXToken_Punctuation);
return tokens[operatorIndex].GetSpelling(Handle.TranslationUnit).ToString();
}
public Expr RHS => _rhs.Value;
}
}
Loading

0 comments on commit 08c7c2d

Please sign in to comment.