diff --git a/ClangSharp.Test/TranslationUnit.cs b/ClangSharp.Test/TranslationUnit.cs index a129ae11..ce7417d9 100644 --- a/ClangSharp.Test/TranslationUnit.cs +++ b/ClangSharp.Test/TranslationUnit.cs @@ -36,5 +36,35 @@ public void Basic(string name) Directory.Delete(dir, true); } } + + [Theory] + [InlineData("basic")] + [InlineData("example with spaces")] + [InlineData("♫")] + public void BasicWrapper(string name) + { + // Create a unique directory + var dir = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + Directory.CreateDirectory(dir); + + try + { + // Create a file with the right name + var file = new FileInfo(Path.Combine(dir, name + ".c")); + File.WriteAllText(file.FullName, "int main() { return 0; }"); + + var index = CXIndex.Create(); + var translationUnit = CXTranslationUnit.Parse(index, file.FullName, Array.Empty(), Array.Empty(), CXTranslationUnit_Flags.CXTranslationUnit_None); + var clangFile = translationUnit.GetFile(file.FullName); + var clangFileName = clangFile.Name; + var clangFileNameString = clangFileName.CString; + + Assert.Equal(file.FullName, clangFileNameString); + } + finally + { + Directory.Delete(dir, true); + } + } } } diff --git a/ClangSharp/ClangSharp.csproj b/ClangSharp/ClangSharp.csproj index 939a2ed9..7bbc7cf1 100644 --- a/ClangSharp/ClangSharp.csproj +++ b/ClangSharp/ClangSharp.csproj @@ -1,6 +1,7 @@  + true netstandard2.0 diff --git a/ClangSharp/Extensions/CXComment.cs b/ClangSharp/Extensions/CXComment.cs new file mode 100644 index 00000000..df03bb1d --- /dev/null +++ b/ClangSharp/Extensions/CXComment.cs @@ -0,0 +1,73 @@ +namespace ClangSharp +{ + public partial struct CXComment + { + public CXString BlockCommandComment_CommandName => clang.BlockCommandComment_getCommandName(this); + + public uint BlockCommandComment_NumArgs => clang.BlockCommandComment_getNumArgs(this); + + public CXComment BlockCommandComment_Paragraph => clang.BlockCommandComment_getParagraph(this); + + public CXString FullComment_AsHtml => clang.FullComment_getAsHTML(this); + + public CXString FullComment_AsXml => clang.FullComment_getAsXML(this); + + public uint HtmlStartTag_NumAttrs => clang.HTMLStartTag_getNumAttrs(this); + + public bool HtmlStartTagComment_IsSelfClosing => clang.HTMLStartTagComment_isSelfClosing(this) != 0; + + public CXString HtmlTagComment_AsString => clang.HTMLTagComment_getAsString(this); + + public CXString HtmlTagComment_TagName => clang.HTMLTagComment_getTagName(this); + + public CXString InlineCommandComment_CommandName => clang.InlineCommandComment_getCommandName(this); + + public uint InlineCommandComment_NumArgs => clang.InlineCommandComment_getNumArgs(this); + + public CXCommentInlineCommandRenderKind InlineCommandComment_RenderKind => clang.InlineCommandComment_getRenderKind(this); + + public bool InlineContentComment_HasTrailingNewline => clang.InlineContentComment_hasTrailingNewline(this) != 0; + + public bool IsWhitesapce => clang.Comment_isWhitespace(this) != 0; + + public CXCommentKind Kind => clang.Comment_getKind(this); + + public uint NumChildren => clang.Comment_getNumChildren(this); + + public CXCommentParamPassDirection ParamCommandComment_Direction => clang.ParamCommandComment_getDirection(this); + + public bool ParamCommandComment_IsDirectionExplicit => clang.ParamCommandComment_isDirectionExplicit(this) != 0; + + public bool ParamCommandComment_IsParamIndexValid => clang.ParamCommandComment_isParamIndexValid(this) != 0; + + public uint ParamCommandComment_ParamIndex => clang.ParamCommandComment_getParamIndex(this); + + public CXString ParamCommandComment_ParamName => clang.ParamCommandComment_getParamName(this); + + public CXString TextComment_Text => clang.TextComment_getText(this); + + public uint TParamCommandComment_Depth => clang.TParamCommandComment_getDepth(this); + + public CXString TParamCommandComment_ParamName => clang.TParamCommandComment_getParamName(this); + + public bool TParamCommandComment_IsParamPositionValid => clang.TParamCommandComment_isParamPositionValid(this) != 0; + + public CXString VerbatimBlockLineComment_Text => clang.VerbatimBlockLineComment_getText(this); + + public CXString VerbatimLineComment_Text => clang.VerbatimLineComment_getText(this); + + public CXString BlockCommandComment_GetArgText(uint index) => clang.BlockCommandComment_getArgText(this, index); + + public CXComment GetChild(uint index) => clang.Comment_getChild(this, index); + + public CXTranslationUnit GetTranslationUnit() => new CXTranslationUnit(TranslationUnit); + + public CXString HtmlStartTag_GetAttrName(uint index) => clang.HTMLStartTag_getAttrName(this, index); + + public CXString HtmlStartTag_GetAttrValue(uint index) => clang.HTMLStartTag_getAttrValue(this, index); + + public CXString InlineCommandComment_GetArgText(uint index) => clang.InlineCommandComment_getArgText(this, index); + + public uint TParamCommandComment_GetIndex(uint depth) => clang.TParamCommandComment_getIndex(this, depth); + } +} diff --git a/ClangSharp/Extensions/CXCompletionString.cs b/ClangSharp/Extensions/CXCompletionString.cs new file mode 100644 index 00000000..19579127 --- /dev/null +++ b/ClangSharp/Extensions/CXCompletionString.cs @@ -0,0 +1,25 @@ +namespace ClangSharp +{ + public partial struct CXCompletionString + { + public CXAvailabilityKind Availability => clang.getCompletionAvailability(this); + + public CXString BriefComment => clang.getCompletionBriefComment(this); + + public uint NumAnnotations => clang.getCompletionNumAnnotations(this); + + public uint NumChunks => clang.getNumCompletionChunks(this); + + public uint Priority => clang.getCompletionPriority(this); + + public CXString GetAnnotation(uint index) => clang.getCompletionAnnotation(this, index); + + public CXCompletionString GetChunkCompletionString(uint index) => clang.getCompletionChunkCompletionString(this, index); + + public CXCompletionChunkKind GetChunkKind(uint index) => clang.getCompletionChunkKind(this, index); + + public CXString GetChunkText(uint index) => clang.getCompletionChunkText(this, index); + + public CXString GetParent(ref CXCursorKind kind) => clang.getCompletionParent(this, ref kind); + } +} diff --git a/ClangSharp/Extensions/CXCursor.cs b/ClangSharp/Extensions/CXCursor.cs index c059e4b2..b46ef8a4 100644 --- a/ClangSharp/Extensions/CXCursor.cs +++ b/ClangSharp/Extensions/CXCursor.cs @@ -6,44 +6,215 @@ public partial struct CXCursor : IEquatable { public static CXCursor Null => clang.getNullCursor(); + public CXAvailabilityKind Availability => clang.getCursorAvailability(this); + + public CXString BriefCommentText => clang.Cursor_getBriefCommentText(this); + + public CXCursor CanonicalCursor => clang.getCanonicalCursor(this); + public CXSourceRange CommentRange => clang.Cursor_getCommentRange(this); - public CXType EnumDeclIntegerType => clang.getEnumDeclIntegerType(this); + public CXCompletionString CompletionString => clang.getCursorCompletionString(this); + + public CX_CXXAccessSpecifier CXXAccessSpecifier => clang.getCXXAccessSpecifier(this); + + public bool CXXConstructor_IsConvertingConstructor => clang.CXXConstructor_isConvertingConstructor(this) != 0; + + public bool CXXConstructor_IsCopyConstructor => clang.CXXConstructor_isCopyConstructor(this) != 0; + + public bool CXXConstructor_IsDefaultConstructor => clang.CXXConstructor_isDefaultConstructor(this) != 0; + + public bool CXXConstructor_IsMoveConstructor => clang.CXXConstructor_isMoveConstructor(this) != 0; + + public bool CXXField_IsMutable => clang.CXXField_isMutable(this) != 0; + + public unsafe ref CXStringSet CXXManglings => ref *(CXStringSet*)clang.Cursor_getCXXManglings(this); + + public bool CXXMethod_IsConst => clang.CXXMethod_isConst(this) != 0; + + public bool CXXMethod_IsDefaulted => clang.CXXMethod_isDefaulted(this) != 0; + + public bool CXXMethod_IsPureVirtual => clang.CXXMethod_isPureVirtual(this) != 0; + + public bool CXXMethod_IsStatic => clang.CXXMethod_isStatic(this) != 0; + + public bool CXXMethod_IsVirtual => clang.CXXMethod_isVirtual(this) != 0; + + public bool CXXRecord_IsAbstract => clang.CXXRecord_isAbstract(this) != 0; + + public CXString DeclObjCTypeEncoding => clang.getDeclObjCTypeEncoding(this); + + public CXCursor Definition => clang.getCursorDefinition(this); + + public CXString DisplayName => clang.getCursorDisplayName(this); + + public ulong EnumConstantDeclUnsignedValue => clang.getEnumConstantDeclUnsignedValue(this); + + public long EnumConstantDeclValue => clang.getEnumConstantDeclValue(this); + + public CXType EnumDecl_IntegerType => clang.getEnumDeclIntegerType(this); + + public bool EnumDecl_IsScoped => clang.EnumDecl_isScoped(this) != 0; + + public CXEvalResult Evaluate => clang.Cursor_Evaluate(this); + + public int ExceptionSpecificationType => clang.getCursorExceptionSpecificationType(this); public CXSourceRange Extent => clang.getCursorExtent(this); + public int FieldDeclBitWidth => clang.getFieldDeclBitWidth(this); + + public bool HasAttrs => clang.Cursor_hasAttrs(this) != 0; + public CXType IBOutletCollectionType => clang.getIBOutletCollectionType(this); + public CXFile IncludedFile => clang.getIncludedFile(this); + + public bool IsAnonymous => clang.Cursor_isAnonymous(this) != 0; + + public bool IsAttribute => clang.isAttribute(Kind) != 0; + + public bool IsBitField => clang.Cursor_isBitField(this) != 0; + + public bool IsDeclaration => clang.isDeclaration(Kind) != 0; + + public bool IsDefinition => clang.isCursorDefinition(this) != 0; + + public bool IsDynamicCall => clang.Cursor_isDynamicCall(this) != 0; + + public bool IsExpression => clang.isExpression(Kind) != 0; + + public bool IsFunctionInlined => clang.Cursor_isFunctionInlined(this) != 0; + + public bool IsInvalid => clang.isInvalid(Kind) != 0; + + public bool IsInvalidDeclaration => clang.isInvalidDeclaration(this) != 0; + public bool IsNull => clang.Cursor_isNull(this) != 0; + public bool IsMacroBuiltIn => clang.Cursor_isMacroBuiltin(this) != 0; + + public bool IsMacroFunctionLike => clang.Cursor_isMacroFunctionLike(this) != 0; + + public bool IsObjCOptional => clang.Cursor_isObjCOptional(this) != 0; + + public bool IsPreProcessing => clang.isPreprocessing(Kind) != 0; + + public bool IsReference => clang.isReference(Kind) != 0; + + public bool IsStatement => clang.isStatement(Kind) != 0; + + public bool IsTranslationUnit => clang.isTranslationUnit(Kind) != 0; + + public bool IsUnexposed => clang.isUnexposed(Kind) != 0; + + public bool IsVariadic => clang.Cursor_isVariadic(this) != 0; + + public bool IsVirtualBase => clang.isVirtualBase(this) != 0; + public CXCursorKind Kind => clang.getCursorKind(this); + public CXString KindSpelling => clang.getCursorKindSpelling(Kind); + + public CXLanguageKind Language => clang.getCursorLanguage(this); + + public CXCursor LexicalParent => clang.getCursorLexicalParent(this); + + public CXLinkageKind Linkage => clang.getCursorLinkage(this); + public CXSourceLocation Location => clang.getCursorLocation(this); - public CXType TypedefDeclUnderlyingType => clang.getTypedefDeclUnderlyingType(this); + public CXString Mangling => clang.Cursor_getMangling(this); + + public CXModule Module => clang.Cursor_getModule(this); + + public int NumArguments => clang.Cursor_getNumArguments(this); + + public uint NumOverloadedDecls => clang.getNumOverloadedDecls(this); + + public int NumTemplateArguments => clang.Cursor_getNumTemplateArguments(this); + + public CXObjCDeclQualifierKind ObjCDeclQualifiers => (CXObjCDeclQualifierKind)clang.Cursor_getObjCDeclQualifiers(this); + + public unsafe ref CXStringSet ObjCManglings => ref *(CXStringSet*)clang.Cursor_getObjCManglings(this); + + public CXString ObjCPropertyGetterName => clang.Cursor_getObjCPropertyGetterName(this); + + public CXString ObjCPropertySetterName => clang.Cursor_getObjCPropertySetterName(this); + + public int ObjCSelectorIndex => clang.Cursor_getObjCSelectorIndex(this); + + public long OffsetOfField => clang.Cursor_getOffsetOfField(this); + + public CXComment ParsedComment => clang.Cursor_getParsedComment(this); + + public CXString RawCommentText => clang.Cursor_getRawCommentText(this); public CXType RecieverType => clang.Cursor_getReceiverType(this); + public CXCursor Referenced => clang.getCursorReferenced(this); + public CXType ResultType => clang.getCursorResultType(this); + public CXCursor SemanticParent => clang.getCursorSemanticParent(this); + + public CXCursor SpecializedCursorTemplate => clang.getSpecializedCursorTemplate(this); + public CXString Spelling => clang.getCursorSpelling(this); + public CX_StorageClass StorageClass => clang.Cursor_getStorageClass(this); + + public CXCursorKind TemplateCursorKind => clang.getTemplateCursorKind(this); + + public CXTLSKind TlsKind => clang.getCursorTLSKind(this); + public CXTranslationUnit TranslationUnit => clang.Cursor_getTranslationUnit(this); public CXType Type => clang.getCursorType(this); + public CXType TypedefDeclUnderlyingType => clang.getTypedefDeclUnderlyingType(this); + + public CXString UnifiedSymbolResolution => clang.getCursorUSR(this); + + public CXVisibilityKind Visibility => clang.getCursorVisibility(this); + public override bool Equals(object obj) => (obj is CXCursor other) && Equals(other); public bool Equals(CXCursor other) => clang.equalCursors(this, other) != 0; + public CXResult FindReferenceInFile(CXFile file, CXCursorAndRangeVisitor visitor) => clang.findReferencesInFile(this, file, visitor); + + public CXCursor GetArgument(uint index) => clang.Cursor_getArgument(this, index); + public override int GetHashCode() => (int)clang.hashCursor(this); + public bool GetIsExternalSymbol(out CXString language, out CXString definedIn, out bool isGenerated) + { + var result = clang.Cursor_isExternalSymbol(this, out language, out definedIn, out uint isGeneratedOut); + isGenerated = isGeneratedOut != 0; + return result != 0; + } + + public CXObjCPropertyAttrKind GetObjCPropertyAttributes(uint reserved) => (CXObjCPropertyAttrKind)clang.Cursor_getObjCPropertyAttributes(this, reserved); + + public CXCursor GetOverloadedDecl(uint index) => clang.getOverloadedDecl(this, index); + + public int GetPlatformAvailability(out bool alwaysDeprecated, out CXString deprecatedMessage, out bool alwaysUnavailable, out CXString unavailableMessage, CXPlatformAvailability[] availability) => clang.getCursorPlatformAvailability(this, out alwaysDeprecated, out deprecatedMessage, out alwaysUnavailable, out unavailableMessage, availability, availability.Length); + public CXSourceRange GetReferenceNameRange(CXNameRefFlags nameFlags, uint pieceIndex) => clang.getCursorReferenceNameRange(this, (uint)nameFlags, pieceIndex); + public CXSourceRange GetSpellingNameRange(uint pieceIndex, uint options) => clang.Cursor_getSpellingNameRange(this, pieceIndex, options); + + public CXTemplateArgumentKind GetTemplateArgumentKind(uint i) => clang.Cursor_getTemplateArgumentKind(this, i); + public CXType GetTemplateArgumentType(uint i) => clang.Cursor_getTemplateArgumentType(this, i); - public CXSourceRange GetSpellingNameRange(uint pieceIndex, uint options) => clang.Cursor_getSpellingNameRange(this, pieceIndex, options); + public ulong GetTemplateArgumentUnsignedValue(uint i) => clang.Cursor_getTemplateArgumentUnsignedValue(this, i); + + public long GetTemplateArgumentValue(uint i) => clang.Cursor_getTemplateArgumentValue(this, i); public override string ToString() => Spelling.ToString(); + + public CXChildVisitResult VisitChildren(CXCursorVisitor visitor, CXClientData clientData) => (CXChildVisitResult)clang.visitChildren(this, visitor, clientData); } } diff --git a/ClangSharp/Extensions/CXEvalResult.cs b/ClangSharp/Extensions/CXEvalResult.cs new file mode 100644 index 00000000..563a24af --- /dev/null +++ b/ClangSharp/Extensions/CXEvalResult.cs @@ -0,0 +1,23 @@ +using System; + +namespace ClangSharp +{ + public partial struct CXEvalResult : IDisposable + { + public double AsDouble => clang.EvalResult_getAsDouble(this); + + public int AsInt => clang.EvalResult_getAsInt(this); + + public long AsLongLong => clang.EvalResult_getAsLongLong(this); + + public string AsStr => clang.EvalResult_getAsStr(this); + + public ulong AsUnsigned => clang.EvalResult_getAsUnsigned(this); + + public bool IsUnsignedInt => clang.EvalResult_isUnsignedInt(this) != 0; + + public CXEvalResultKind Kind => clang.EvalResult_getKind(this); + + public void Dispose() => clang.EvalResult_dispose(this); + } +} diff --git a/ClangSharp/Extensions/CXFile.cs b/ClangSharp/Extensions/CXFile.cs index 0d3394ba..619be6f9 100644 --- a/ClangSharp/Extensions/CXFile.cs +++ b/ClangSharp/Extensions/CXFile.cs @@ -12,13 +12,7 @@ public partial struct CXFile : IEquatable public bool Equals(CXFile other) => clang.File_isEqual(this, other) != 0; - public string GetContents(CXTranslationUnit translationUnit, out ulong size) => clang.getFileContents(translationUnit, this, out size); - - public CXSourceLocation GetLocation(CXTranslationUnit translationUnit, uint line, uint column) => clang.getLocation(translationUnit, this, line, column); - - public CXSourceLocation GetLocationForOffset(CXTranslationUnit translationUnit, uint offset) => clang.getLocationForOffset(translationUnit, this, offset); - - public bool IsMultipleIncludeGuarded(CXTranslationUnit translationUnit) => clang.isFileMultipleIncludeGuarded(translationUnit, this) != 0; + public bool GetUniqueId(out CXFileUniqueID id) => clang.getFileUniqueID(this, out id) != 0; public override string ToString() => Name.ToString(); diff --git a/ClangSharp/Extensions/CXIndex.cs b/ClangSharp/Extensions/CXIndex.cs index 87c5a32a..157a7181 100644 --- a/ClangSharp/Extensions/CXIndex.cs +++ b/ClangSharp/Extensions/CXIndex.cs @@ -4,7 +4,7 @@ namespace ClangSharp { public partial struct CXIndex : IDisposable { - public CXIndex Create(bool excludeDeclarationsFromPch = false, bool displayDiagnostics = false) => clang.createIndex(excludeDeclarationsFromPch ? 1 : 0, displayDiagnostics ? 1 : 0); + public static CXIndex Create(bool excludeDeclarationsFromPch = false, bool displayDiagnostics = false) => clang.createIndex(excludeDeclarationsFromPch ? 1 : 0, displayDiagnostics ? 1 : 0); public CXGlobalOptFlags GlobalOptions { diff --git a/ClangSharp/Extensions/CXModule.cs b/ClangSharp/Extensions/CXModule.cs new file mode 100644 index 00000000..c4877656 --- /dev/null +++ b/ClangSharp/Extensions/CXModule.cs @@ -0,0 +1,19 @@ +namespace ClangSharp +{ + public partial struct CXModule + { + public CXFile AstFile => clang.Module_getASTFile(this); + + public CXString FullName => clang.Module_getFullName(this); + + public bool IsSystem => clang.Module_isSystem(this) != 0; + + public CXString Name => clang.Module_getName(this); + + public CXModule Parent => clang.Module_getParent(this); + + public uint GetNumTopLevelHeaders(CXTranslationUnit translationUnit) => clang.Module_getNumTopLevelHeaders(translationUnit, this); + + public CXFile GetTopLevelHeader(CXTranslationUnit translationUnit, uint index) => clang.Module_getTopLevelHeader(translationUnit, this, index); + } +} diff --git a/ClangSharp/Extensions/CXPlatformAvailability.cs b/ClangSharp/Extensions/CXPlatformAvailability.cs new file mode 100644 index 00000000..77cb3156 --- /dev/null +++ b/ClangSharp/Extensions/CXPlatformAvailability.cs @@ -0,0 +1,9 @@ +using System; + +namespace ClangSharp +{ + public partial struct CXPlatformAvailability : IDisposable + { + public void Dispose() => clang.disposeCXPlatformAvailability(ref this); + } +} diff --git a/ClangSharp/Extensions/CXSourceRangeList.cs b/ClangSharp/Extensions/CXSourceRangeList.cs new file mode 100644 index 00000000..f2ef11ca --- /dev/null +++ b/ClangSharp/Extensions/CXSourceRangeList.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace ClangSharp +{ + public partial struct CXSourceRangeList : IDisposable, IReadOnlyCollection + { + public unsafe CXSourceRange this[uint index] => ((CXSourceRange*)ranges)[index]; + + public int Count => (int)count; + + public void Dispose() => clang.disposeSourceRangeList(ref this); + + public IEnumerator GetEnumerator() + { + var count = (uint)Count; + + for (var index = 0u; index < count; index++) + { + yield return this[index]; + } + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } +} diff --git a/ClangSharp/Extensions/CXStringSet.cs b/ClangSharp/Extensions/CXStringSet.cs new file mode 100644 index 00000000..e4252198 --- /dev/null +++ b/ClangSharp/Extensions/CXStringSet.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace ClangSharp +{ + public partial struct CXStringSet : IDisposable, IReadOnlyCollection + { + public unsafe CXString this[uint index] => ((CXString*)Strings)[index]; + + int IReadOnlyCollection.Count => (int)Count; + + public void Dispose() => clang.disposeStringSet(ref this); + + public IEnumerator GetEnumerator() + { + var count = (uint)Count; + + for (var index = 0u; index < count; index++) + { + yield return this[index]; + } + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } +} diff --git a/ClangSharp/Extensions/CXTUResourceUsage.cs b/ClangSharp/Extensions/CXTUResourceUsage.cs new file mode 100644 index 00000000..7187992b --- /dev/null +++ b/ClangSharp/Extensions/CXTUResourceUsage.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections; +using System.Collections.Generic; + +namespace ClangSharp +{ + public partial struct CXTUResourceUsage : IDisposable, IReadOnlyCollection + { + public unsafe CXTUResourceUsageEntry this[uint index] => ((CXTUResourceUsageEntry*)entries)[index]; + + public int Count => (int)numEntries; + + public void Dispose() => clang.disposeCXTUResourceUsage(this); + + public IEnumerator GetEnumerator() + { + var count = (uint)Count; + + for (var index = 0u; index < count; index++) + { + yield return this[index]; + } + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + } +} diff --git a/ClangSharp/Extensions/CXTUResourceUsageEntry.cs b/ClangSharp/Extensions/CXTUResourceUsageEntry.cs new file mode 100644 index 00000000..555416fb --- /dev/null +++ b/ClangSharp/Extensions/CXTUResourceUsageEntry.cs @@ -0,0 +1,7 @@ +namespace ClangSharp +{ + public partial struct CXTUResourceUsageEntry + { + public string Name => clang.getTUResourceUsageName(kind); + } +} diff --git a/ClangSharp/Extensions/CXTargetInfo.cs b/ClangSharp/Extensions/CXTargetInfo.cs new file mode 100644 index 00000000..544b5094 --- /dev/null +++ b/ClangSharp/Extensions/CXTargetInfo.cs @@ -0,0 +1,13 @@ +using System; + +namespace ClangSharp +{ + public partial struct CXTargetInfo : IDisposable + { + public int PointerWidth => clang.TargetInfo_getPointerWidth(this); + + public CXString Triple => clang.TargetInfo_getTriple(this); + + public void Dispose() => clang.TargetInfo_dispose(this); + } +} diff --git a/ClangSharp/Extensions/CXToken.cs b/ClangSharp/Extensions/CXToken.cs new file mode 100644 index 00000000..c6f2ae58 --- /dev/null +++ b/ClangSharp/Extensions/CXToken.cs @@ -0,0 +1,13 @@ +namespace ClangSharp +{ + public partial struct CXToken + { + public CXTokenKind Kind => clang.getTokenKind(this); + + public CXSourceRange GetExtent(CXTranslationUnit translationUnit) => clang.getTokenExtent(translationUnit, this); + + public CXSourceLocation GetLocation(CXTranslationUnit translationUnit) => clang.getTokenLocation(translationUnit, this); + + public CXString GetSpelling(CXTranslationUnit translationUnit) => clang.getTokenSpelling(translationUnit, this); + } +} diff --git a/ClangSharp/Extensions/CXTranslationUnit.cs b/ClangSharp/Extensions/CXTranslationUnit.cs index b928a4e8..21063483 100644 --- a/ClangSharp/Extensions/CXTranslationUnit.cs +++ b/ClangSharp/Extensions/CXTranslationUnit.cs @@ -26,22 +26,53 @@ public partial struct CXTranslationUnit : IDisposable public uint NumDiagnostics => clang.getNumDiagnostics(this); + public CXTUResourceUsage ResourceUsage => clang.getCXTUResourceUsage(this); + public CXString Spelling => clang.getTranslationUnitSpelling(this); + public CXTargetInfo TargetInfo => clang.getTranslationUnitTargetInfo(this); + + public void AnnotateTokens(CXToken[] tokens, CXCursor[] cursors) => clang.annotateTokens(this, tokens, (uint)tokens.Length, cursors); + public void Dispose() => clang.disposeTranslationUnit(this); + public void DisposeTokens(CXToken[] tokens) => clang.disposeTokens(this, tokens, (uint)tokens.Length); + + public CXResult FindIncludesInFile(CXFile file, CXCursorAndRangeVisitor visitor) => clang.findIncludesInFile(this, file, visitor); + + public unsafe ref CXSourceRangeList GetAllSkippedRanges() => ref *(CXSourceRangeList*)clang.getAllSkippedRanges(this); + public CXCursor GetCursor(CXSourceLocation location) => clang.getCursor(this, location); public CXDiagnostic GetDiagnostic(uint index) => clang.getDiagnostic(this, index); public CXFile GetFile(string fileName) => clang.getFile(this, fileName); + public string GetFileContents(CXFile file, out ulong size) => clang.getFileContents(this, file, out size); + + public void GetInclusions(CXInclusionVisitor visitor, CXClientData clientData) => clang.getInclusions(this, visitor, clientData); + + public CXSourceLocation GetLocation(CXFile file, uint line, uint column) => clang.getLocation(this, file, line, column); + + public CXSourceLocation GetLocationForOffset(CXFile file, uint offset) => clang.getLocationForOffset(this, file, offset); + + public CXModule GetModuleForFile(CXFile file) => clang.getModuleForFile(this, file); + + public unsafe ref CXSourceRangeList GetSkippedRanges(CXFile file) => ref *(CXSourceRangeList*)clang.getSkippedRanges(this, file); + + public unsafe ref CXToken GetToken(CXSourceLocation sourceLocation) => ref *(CXToken*)clang.getToken(this, sourceLocation); + + public bool IsFileMultipleIncludeGuarded(CXFile file) => clang.isFileMultipleIncludeGuarded(this, file) != 0; + public CXErrorCode Reparse(CXUnsavedFile[] unsavedFiles, CXReparse_Flags options) => (CXErrorCode)clang.reparseTranslationUnit(this, (uint)unsavedFiles.Length, unsavedFiles, (uint)options); public CXSaveError Save(string fileName, CXSaveTranslationUnit_Flags options) => (CXSaveError)clang.saveTranslationUnit(this, fileName, (uint)options); public bool Suspend() => clang.suspendTranslationUnit(this) != 0; + public void Tokenize(CXSourceRange sourceRange, out CXToken[] tokens) => clang.tokenize(this, sourceRange, out tokens, out _); + public override string ToString() => Spelling.ToString(); + } } diff --git a/ClangSharp/Extensions/CXType.cs b/ClangSharp/Extensions/CXType.cs index 3b756315..46951aa2 100644 --- a/ClangSharp/Extensions/CXType.cs +++ b/ClangSharp/Extensions/CXType.cs @@ -86,6 +86,6 @@ public partial struct CXType : IEquatable public override string ToString() => Spelling.ToString(); - public uint VisitFields(CXFieldVisitor visitor, CXClientData clientData) => clang.Type_visitFields(this, visitor, clientData); + public CXVisitorResult VisitFields(CXFieldVisitor visitor, CXClientData clientData) => (CXVisitorResult)clang.Type_visitFields(this, visitor, clientData); } } diff --git a/ClangSharp/Generated.Custom.cs b/ClangSharp/Generated.Custom.cs index 2fc4eb0f..30505236 100644 --- a/ClangSharp/Generated.Custom.cs +++ b/ClangSharp/Generated.Custom.cs @@ -216,6 +216,30 @@ public static void index_setClientEntity(ref CXIdxEntityInfo @param0, CXIdxClien } } + [DllImport(libraryPath, EntryPoint = "clang_disposeSourceRangeList", CallingConvention = CallingConvention.Cdecl)] + public static extern void disposeSourceRangeList(ref CXSourceRangeList @ranges); + + [DllImport(libraryPath, EntryPoint = "clang_annotateTokens", CallingConvention = CallingConvention.Cdecl)] + public static extern void annotateTokens(CXTranslationUnit @TU, [MarshalAs(UnmanagedType.LPArray)] CXToken[] @Tokens, uint @NumTokens, [MarshalAs(UnmanagedType.LPArray)] CXCursor[] @Cursors); + + [DllImport(libraryPath, EntryPoint = "clang_disposeTokens", CallingConvention = CallingConvention.Cdecl)] + public static extern void disposeTokens(CXTranslationUnit @TU, [MarshalAs(UnmanagedType.LPArray)] CXToken[] @Tokens, uint @NumTokens); + + [DllImport(libraryPath, EntryPoint = "clang_tokenize", CallingConvention = CallingConvention.Cdecl)] + public static extern void tokenize(CXTranslationUnit @TU, CXSourceRange @Range, [MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 3)] out CXToken[] @Tokens, out uint @NumTokens); + + [DllImport(libraryPath, EntryPoint = "clang_disposeStringSet", CallingConvention = CallingConvention.Cdecl)] + public static extern void disposeStringSet(ref CXStringSet @set); + + [DllImport(libraryPath, EntryPoint = "clang_getCompletionParent", CallingConvention = CallingConvention.Cdecl)] + public static extern CXString getCompletionParent(CXCompletionString @completion_string, ref CXCursorKind @kind); + + [DllImport(libraryPath, EntryPoint = "clang_disposeCXPlatformAvailability", CallingConvention = CallingConvention.Cdecl)] + public static extern void disposeCXPlatformAvailability(ref CXPlatformAvailability @availability); + + [DllImport(libraryPath, EntryPoint = "clang_getCursorPlatformAvailability", CallingConvention = CallingConvention.Cdecl)] + public static extern int getCursorPlatformAvailability(CXCursor @cursor, [MarshalAs(UnmanagedType.Bool)] out bool @always_deprecated, out CXString @deprecated_message, [MarshalAs(UnmanagedType.Bool)] out bool @always_unavailable, out CXString @unavailable_message, [MarshalAs(UnmanagedType.LPArray)] CXPlatformAvailability[] @availability, int @availability_size); + [DllImport(libraryPath, EntryPoint = "clang_createTranslationUnitFromSourceFile", CallingConvention = CallingConvention.Cdecl)] private static extern CXTranslationUnit createTranslationUnitFromSourceFile(CXIndex @CIdx, [MarshalAs(UnmanagedType.CustomMarshaler, MarshalTypeRef = typeof(StringMarshaler))] string @source_filename, int @num_clang_command_line_args, string[] @clang_command_line_args, uint @num_unsaved_files, [MarshalAs(UnmanagedType.LPArray)] _CXUnsavedFile[] @unsaved_files); diff --git a/ClangSharp/Generated.cs b/ClangSharp/Generated.cs index 4d93abce..4ea27c46 100644 --- a/ClangSharp/Generated.cs +++ b/ClangSharp/Generated.cs @@ -1460,9 +1460,6 @@ public static partial class clang [DllImport(libraryPath, EntryPoint = "clang_disposeString", CallingConvention = CallingConvention.Cdecl)] public static extern void disposeString(CXString @string); - [DllImport(libraryPath, EntryPoint = "clang_disposeStringSet", CallingConvention = CallingConvention.Cdecl)] - public static extern void disposeStringSet(out CXStringSet @set); - [DllImport(libraryPath, EntryPoint = "clang_getBuildSessionTimestamp", CallingConvention = CallingConvention.Cdecl)] public static extern ulong getBuildSessionTimestamp(); @@ -1596,9 +1593,6 @@ public static partial class clang [DllImport(libraryPath, EntryPoint = "clang_getAllSkippedRanges", CallingConvention = CallingConvention.Cdecl)] public static extern IntPtr getAllSkippedRanges(CXTranslationUnit @tu); - [DllImport(libraryPath, EntryPoint = "clang_disposeSourceRangeList", CallingConvention = CallingConvention.Cdecl)] - public static extern void disposeSourceRangeList(out CXSourceRangeList @ranges); - [DllImport(libraryPath, EntryPoint = "clang_getNumDiagnosticsInSet", CallingConvention = CallingConvention.Cdecl)] public static extern uint getNumDiagnosticsInSet(CXDiagnosticSet @Diags); @@ -1774,12 +1768,6 @@ public static partial class clang [DllImport(libraryPath, EntryPoint = "clang_getCursorAvailability", CallingConvention = CallingConvention.Cdecl)] public static extern CXAvailabilityKind getCursorAvailability(CXCursor @cursor); - [DllImport(libraryPath, EntryPoint = "clang_getCursorPlatformAvailability", CallingConvention = CallingConvention.Cdecl)] - public static extern int getCursorPlatformAvailability(CXCursor @cursor, out int @always_deprecated, out CXString @deprecated_message, out int @always_unavailable, out CXString @unavailable_message, out CXPlatformAvailability @availability, int @availability_size); - - [DllImport(libraryPath, EntryPoint = "clang_disposeCXPlatformAvailability", CallingConvention = CallingConvention.Cdecl)] - public static extern void disposeCXPlatformAvailability(out CXPlatformAvailability @availability); - [DllImport(libraryPath, EntryPoint = "clang_getCursorLanguage", CallingConvention = CallingConvention.Cdecl)] public static extern CXLanguageKind getCursorLanguage(CXCursor @cursor); @@ -2221,15 +2209,6 @@ public static partial class clang [DllImport(libraryPath, EntryPoint = "clang_getTokenExtent", CallingConvention = CallingConvention.Cdecl)] public static extern CXSourceRange getTokenExtent(CXTranslationUnit @param0, CXToken @param1); - [DllImport(libraryPath, EntryPoint = "clang_tokenize", CallingConvention = CallingConvention.Cdecl)] - public static extern void tokenize(CXTranslationUnit @TU, CXSourceRange @Range, out IntPtr @Tokens, out uint @NumTokens); - - [DllImport(libraryPath, EntryPoint = "clang_annotateTokens", CallingConvention = CallingConvention.Cdecl)] - public static extern void annotateTokens(CXTranslationUnit @TU, out CXToken @Tokens, uint @NumTokens, out CXCursor @Cursors); - - [DllImport(libraryPath, EntryPoint = "clang_disposeTokens", CallingConvention = CallingConvention.Cdecl)] - public static extern void disposeTokens(CXTranslationUnit @TU, out CXToken @Tokens, uint @NumTokens); - [DllImport(libraryPath, EntryPoint = "clang_getCursorKindSpelling", CallingConvention = CallingConvention.Cdecl)] public static extern CXString getCursorKindSpelling(CXCursorKind @Kind); @@ -2266,9 +2245,6 @@ public static partial class clang [DllImport(libraryPath, EntryPoint = "clang_getCompletionAnnotation", CallingConvention = CallingConvention.Cdecl)] public static extern CXString getCompletionAnnotation(CXCompletionString @completion_string, uint @annotation_number); - [DllImport(libraryPath, EntryPoint = "clang_getCompletionParent", CallingConvention = CallingConvention.Cdecl)] - public static extern CXString getCompletionParent(CXCompletionString @completion_string, out CXCursorKind @kind); - [DllImport(libraryPath, EntryPoint = "clang_getCompletionBriefComment", CallingConvention = CallingConvention.Cdecl)] public static extern CXString getCompletionBriefComment(CXCompletionString @completion_string); diff --git a/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj b/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj index 72d493c5..8c78dfab 100644 --- a/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj +++ b/ClangSharpPInvokeGenerator/ClangSharpPInvokeGenerator.csproj @@ -1,4 +1,4 @@ - + @@ -21,7 +21,7 @@ - + diff --git a/ClangSharpPInvokeGenerator/EnumVisitor.cs b/ClangSharpPInvokeGenerator/EnumVisitor.cs index e652eefc..f5cb50a7 100644 --- a/ClangSharpPInvokeGenerator/EnumVisitor.cs +++ b/ClangSharpPInvokeGenerator/EnumVisitor.cs @@ -23,11 +23,11 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) return CXChildVisitResult.CXChildVisit_Continue; } - CXCursorKind curKind = clang.getCursorKind(cursor); + CXCursorKind curKind = cursor.Kind; if (curKind == CXCursorKind.CXCursor_EnumDecl) { string inheritedEnumType; - CXTypeKind kind = clang.getEnumDeclIntegerType(cursor).kind; + CXTypeKind kind = cursor.EnumDecl_IntegerType.kind; switch (kind) { @@ -65,11 +65,11 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) bool hasOneValue = false; long minValue = long.MaxValue; long maxValue = long.MinValue; - clang.visitChildren(cursor, (cxCursor, _, __) => + cursor.VisitChildren((cxCursor, _, __) => { hasOneValue = true; - long value = clang.getEnumConstantDeclValue(cxCursor); + long value = cxCursor.EnumConstantDeclValue; minValue = Math.Min(minValue, value); maxValue = Math.Max(maxValue, value); @@ -82,7 +82,7 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) } } - var enumName = clang.getCursorSpelling(cursor).ToString(); + var enumName = cursor.Spelling.ToString(); // enumName can be empty because of typedef enum { .. } enumName; // so we have to find the sibling, and this is the only way I've found @@ -90,8 +90,8 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) if (string.IsNullOrEmpty(enumName)) { var forwardDeclaringVisitor = new ForwardDeclarationVisitor(cursor); - clang.visitChildren(clang.getCursorLexicalParent(cursor), forwardDeclaringVisitor.Visit, new CXClientData(IntPtr.Zero)); - enumName = clang.getCursorSpelling(forwardDeclaringVisitor.ForwardDeclarationCursor).ToString(); + cursor.LexicalParent.VisitChildren(forwardDeclaringVisitor.Visit, new CXClientData(IntPtr.Zero)); + enumName = forwardDeclaringVisitor.ForwardDeclarationCursor.Spelling.ToString(); if (string.IsNullOrEmpty(enumName)) { @@ -111,9 +111,9 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) this.tw.WriteLine(" {"); // visit all the enum values - clang.visitChildren(cursor, (cxCursor, _, __) => + cursor.VisitChildren((cxCursor, _, __) => { - this.tw.WriteLine(" @" + clang.getCursorSpelling(cxCursor).ToString() + " = " + clang.getEnumConstantDeclValue(cxCursor) + ","); + this.tw.WriteLine(" @" + cxCursor.Spelling.ToString() + " = " + cxCursor.EnumConstantDeclValue + ","); return CXChildVisitResult.CXChildVisit_Continue; }, new CXClientData(IntPtr.Zero)); diff --git a/ClangSharpPInvokeGenerator/Extensions.cs b/ClangSharpPInvokeGenerator/Extensions.cs index bb0f2227..66fbb095 100644 --- a/ClangSharpPInvokeGenerator/Extensions.cs +++ b/ClangSharpPInvokeGenerator/Extensions.cs @@ -9,28 +9,18 @@ internal static class Extensions { public static bool IsInSystemHeader(this CXCursor cursor) { - return clang.Location_isInSystemHeader(clang.getCursorLocation(cursor)) != 0; + return cursor.Location.IsInSystemHeader; } public static bool IsPtrToConstChar(this CXType type) { - var pointee = clang.getPointeeType(type); - - if (clang.isConstQualifiedType(pointee) != 0) - { - switch (pointee.kind) - { - case CXTypeKind.CXType_Char_S: - return true; - } - } - - return false; + var pointee = type.PointeeType; + return pointee.IsConstQualified && (pointee.kind == CXTypeKind.CXType_Char_S); } public static string ToPlainTypeString(this CXType type, string unknownType = "UnknownType") { - var canonical = clang.getCanonicalType(type); + var canonical = type.CanonicalType;; switch (type.kind) { case CXTypeKind.CXType_Bool: @@ -69,7 +59,7 @@ public static string ToPlainTypeString(this CXType type, string unknownType = "U case CXTypeKind.CXType_Unexposed: if (canonical.kind == CXTypeKind.CXType_Unexposed) { - return clang.getTypeSpelling(canonical).ToString(); + return canonical.Spelling.ToString(); } return canonical.ToPlainTypeString(); default: @@ -79,13 +69,13 @@ public static string ToPlainTypeString(this CXType type, string unknownType = "U public static string ToMarshalString(this CXCursor cursor, string cursorSpelling) { - var canonical = clang.getCanonicalType(clang.getCursorType(cursor)); + var canonical = cursor.Type.CanonicalType; switch (canonical.kind) { case CXTypeKind.CXType_ConstantArray: - long arraySize = clang.getArraySize(canonical); - var elementType = clang.getCanonicalType(clang.getArrayElementType(canonical)); + long arraySize = canonical.ArraySize; + var elementType = canonical.ArrayElementType.CanonicalType; var sb = new StringBuilder(); for (int i = 0; i < arraySize; ++i) @@ -95,7 +85,7 @@ public static string ToMarshalString(this CXCursor cursor, string cursorSpelling return sb.ToString().TrimEnd(); case CXTypeKind.CXType_Pointer: - var pointeeType = clang.getCanonicalType(clang.getPointeeType(canonical)); + var pointeeType = canonical.PointeeType.CanonicalType; switch (pointeeType.kind) { case CXTypeKind.CXType_Char_S: @@ -107,7 +97,7 @@ public static string ToMarshalString(this CXCursor cursor, string cursorSpelling } case CXTypeKind.CXType_Record: case CXTypeKind.CXType_Enum: - return "public " + clang.getTypeSpelling(canonical).ToString() + " @" + cursorSpelling + ";"; + return "public " + canonical.Spelling.ToString() + " @" + cursorSpelling + ";"; default: return "public " + canonical.ToPlainTypeString() + " @" + cursorSpelling + ";"; } @@ -115,9 +105,9 @@ public static string ToMarshalString(this CXCursor cursor, string cursorSpelling public static void WriteFunctionInfoHelper(CXCursor cursor, TextWriter tw, string prefixStrip) { - var functionType = clang.getCursorType(cursor); - var functionName = clang.getCursorSpelling(cursor).ToString(); - var resultType = clang.getCursorResultType(cursor); + var functionType = cursor.Type; + var functionName = cursor.Spelling.ToString(); + var resultType = cursor.ResultType; tw.WriteLine(" [DllImport(libraryPath, EntryPoint = \"" + functionName + "\", CallingConvention = " + functionType.CallingConventionSpelling() + ")]"); if (resultType.IsPtrToConstChar()) @@ -134,11 +124,11 @@ public static void WriteFunctionInfoHelper(CXCursor cursor, TextWriter tw, strin tw.Write(" " + functionName + "("); - int numArgTypes = clang.getNumArgTypes(functionType); + int numArgTypes = functionType.NumArgTypes; for (uint i = 0; i < numArgTypes; ++i) { - ArgumentHelper(functionType, clang.Cursor_getArgument(cursor, i), tw, i); + ArgumentHelper(functionType, cursor.GetArgument(i), tw, i); } tw.WriteLine(");"); @@ -147,7 +137,7 @@ public static void WriteFunctionInfoHelper(CXCursor cursor, TextWriter tw, strin public static string CallingConventionSpelling(this CXType type) { - var callingConvention = clang.getFunctionTypeCallingConv(type); + var callingConvention = type.FunctionTypeCallingConv; switch (callingConvention) { case CXCallingConv.CXCallingConv_X86StdCall: @@ -173,11 +163,11 @@ public static void ReturnTypeHelper(CXType resultType, TextWriter tw) public static void ArgumentHelper(CXType functionType, CXCursor paramCursor, TextWriter tw, uint index) { - var numArgTypes = clang.getNumArgTypes(functionType); - var type = clang.getArgType(functionType, index); - var cursorType = clang.getCursorType(paramCursor); + var numArgTypes = functionType.NumArgTypes; + var type = functionType.GetArgType(index); + var cursorType = paramCursor.Type; - var spelling = clang.getCursorSpelling(paramCursor).ToString(); + var spelling = paramCursor.Spelling.ToString(); if (string.IsNullOrEmpty(spelling)) { spelling = "param" + index; @@ -186,14 +176,14 @@ public static void ArgumentHelper(CXType functionType, CXCursor paramCursor, Tex switch (type.kind) { case CXTypeKind.CXType_Pointer: - var pointee = clang.getPointeeType(type); + var pointee = type.PointeeType; switch (pointee.kind) { case CXTypeKind.CXType_Pointer: - tw.Write(pointee.IsPtrToConstChar() && clang.isConstQualifiedType(pointee) != 0 ? "string[]" : "out IntPtr"); + tw.Write(pointee.IsPtrToConstChar() && pointee.IsConstQualified ? "string[]" : "out IntPtr"); break; case CXTypeKind.CXType_FunctionProto: - tw.Write(clang.getTypeSpelling(cursorType).ToString()); + tw.Write(cursorType.Spelling.ToString()); break; case CXTypeKind.CXType_Void: tw.Write("IntPtr"); @@ -225,23 +215,23 @@ public static void ArgumentHelper(CXType functionType, CXCursor paramCursor, Tex private static void CommonTypeHandling(CXType type, TextWriter tw, string outParam = "") { - bool isConstQualifiedType = clang.isConstQualifiedType(type) != 0; + bool isConstQualifiedType = type.IsConstQualified; string spelling; switch (type.kind) { // Need to unwrap elaborated types case CXTypeKind.CXType_Elaborated: - CommonTypeHandling(clang.Type_getNamedType(type), tw, outParam); + CommonTypeHandling(type.NamedType, tw, outParam); return; case CXTypeKind.CXType_Typedef: - var cursor = clang.getTypeDeclaration(type); - var location = clang.getCursorLocation(cursor); + var cursor = type.Declaration; + var location = cursor.Location; // For some reason size_t isn't considered as within a system header. // We work around this by asking for the file name - if it's unknown, probably it's a system header - var isInSystemHeader = clang.Location_isInSystemHeader(clang.getCursorLocation(cursor)) != 0; - clang.getPresumedLocation(clang.getCursorLocation(cursor), out CXString @filename, out uint @line, out uint @column); + var isInSystemHeader = cursor.IsInSystemHeader(); + cursor.Location.GetPresumedLocation(out CXString @filename, out uint @line, out uint @column); isInSystemHeader |= filename.ToString() == string.Empty; if (isInSystemHeader) @@ -250,25 +240,25 @@ private static void CommonTypeHandling(CXType type, TextWriter tw, string outPar // Getting the actual type of a typedef is painful, since platforms don't even agree on the meaning of types; // 64-bit is "long long" on Windows but "long" on Linux, for historical reasons. // The easiest way is to just get the size & signed-ness and write the type ourselves - var size = clang.Type_getSizeOf(type); - var signed = !clang.getTypedefDeclUnderlyingType(cursor).ToString().Contains("unsigned"); + var size = type.SizeOf; + var signed = !cursor.TypedefDeclUnderlyingType.ToString().Contains("unsigned"); spelling = GetTypeName(size, signed); } else { - spelling = clang.getCursorSpelling(cursor).ToString(); + spelling = cursor.Spelling.ToString(); } break; case CXTypeKind.CXType_Record: case CXTypeKind.CXType_Enum: - spelling = clang.getTypeSpelling(type).ToString(); + spelling = type.Spelling.ToString(); break; case CXTypeKind.CXType_IncompleteArray: - CommonTypeHandling(clang.getArrayElementType(type), tw); + CommonTypeHandling(type.ArrayElementType, tw); spelling = "[]"; break; case CXTypeKind.CXType_Unexposed: // Often these are enums and canonical type gets you the enum spelling - var canonical = clang.getCanonicalType(type); + var canonical = type.CanonicalType; // unexposed decl which turns into a function proto seems to be an un-typedef'd fn pointer if (canonical.kind == CXTypeKind.CXType_FunctionProto) { @@ -276,11 +266,11 @@ private static void CommonTypeHandling(CXType type, TextWriter tw, string outPar } else { - spelling = clang.getTypeSpelling(canonical).ToString(); + spelling = canonical.Spelling.ToString(); } break; default: - spelling = clang.getCanonicalType(type).ToPlainTypeString(); + spelling = type.CanonicalType.ToPlainTypeString(); break; } diff --git a/ClangSharpPInvokeGenerator/ForwardDeclarationVisitor.cs b/ClangSharpPInvokeGenerator/ForwardDeclarationVisitor.cs index c1a41a43..fd82c32a 100644 --- a/ClangSharpPInvokeGenerator/ForwardDeclarationVisitor.cs +++ b/ClangSharpPInvokeGenerator/ForwardDeclarationVisitor.cs @@ -23,7 +23,7 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) return CXChildVisitResult.CXChildVisit_Continue; } - if (clang.equalCursors(cursor, this.beginningCursor) != 0) + if (cursor.Equals(beginningCursor)) { this.beginningCursorReached = true; return CXChildVisitResult.CXChildVisit_Continue; diff --git a/ClangSharpPInvokeGenerator/FunctionVisitor.cs b/ClangSharpPInvokeGenerator/FunctionVisitor.cs index 1e598f62..aa522604 100644 --- a/ClangSharpPInvokeGenerator/FunctionVisitor.cs +++ b/ClangSharpPInvokeGenerator/FunctionVisitor.cs @@ -37,12 +37,12 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) return CXChildVisitResult.CXChildVisit_Continue; } - CXCursorKind curKind = clang.getCursorKind(cursor); + CXCursorKind curKind = cursor.kind; // look only at function decls if (curKind == CXCursorKind.CXCursor_FunctionDecl) { - var functionName = clang.getCursorSpelling(cursor).ToString(); + var functionName = cursor.Spelling.ToString(); if (this.visitedFunctions.Contains(functionName)) { diff --git a/ClangSharpPInvokeGenerator/Program.cs b/ClangSharpPInvokeGenerator/Program.cs index 646d9730..a4af095c 100644 --- a/ClangSharpPInvokeGenerator/Program.cs +++ b/ClangSharpPInvokeGenerator/Program.cs @@ -116,7 +116,7 @@ public static void Main(string[] args) excludeFunctionsArray = excludeFunctions.Split(',').Select(x => x.Trim()).ToArray(); } - var createIndex = clang.createIndex(0, 0); + var createIndex = CXIndex.Create(); string[] arr = { "-x", "c++" }; arr = arr.Concat(includeDirs.Select(x => "-I" + x)).ToArray(); @@ -129,18 +129,18 @@ public static void Main(string[] args) { CXTranslationUnit translationUnit; CXUnsavedFile[] unsavedFile = new CXUnsavedFile[0]; - var translationUnitError = clang.parseTranslationUnit2(createIndex, file, arr, arr.Length, unsavedFile, 0, 0, out translationUnit); + var translationUnitError = CXTranslationUnit.Parse(createIndex, file, arr, unsavedFile, CXTranslationUnit_Flags.CXTranslationUnit_None, out translationUnit); if (translationUnitError != CXErrorCode.CXError_Success) { Console.WriteLine("Error: " + translationUnitError); - var numDiagnostics = clang.getNumDiagnostics(translationUnit); + var numDiagnostics = translationUnit.NumDiagnostics; for (uint i = 0; i < numDiagnostics; ++i) { - var diagnostic = clang.getDiagnostic(translationUnit, i); - Console.WriteLine(clang.getDiagnosticSpelling(diagnostic).ToString()); - clang.disposeDiagnostic(diagnostic); + var diagnostic = translationUnit.GetDiagnostic(i); + Console.WriteLine(diagnostic.Spelling.ToString()); + diagnostic.Dispose(); } } @@ -161,19 +161,19 @@ public static void Main(string[] args) var structVisitor = new StructVisitor(sw); foreach (var tu in translationUnits) { - clang.visitChildren(clang.getTranslationUnitCursor(tu), structVisitor.Visit, new CXClientData(IntPtr.Zero)); + tu.Cursor.VisitChildren(structVisitor.Visit, new CXClientData(IntPtr.Zero)); } var typeDefVisitor = new TypeDefVisitor(sw); foreach (var tu in translationUnits) { - clang.visitChildren(clang.getTranslationUnitCursor(tu), typeDefVisitor.Visit, new CXClientData(IntPtr.Zero)); + tu.Cursor.VisitChildren(typeDefVisitor.Visit, new CXClientData(IntPtr.Zero)); } var enumVisitor = new EnumVisitor(sw); foreach (var tu in translationUnits) { - clang.visitChildren(clang.getTranslationUnitCursor(tu), enumVisitor.Visit, new CXClientData(IntPtr.Zero)); + tu.Cursor.VisitChildren(enumVisitor.Visit, new CXClientData(IntPtr.Zero)); } sw.WriteLine(" public static partial class " + methodClassName); @@ -182,7 +182,7 @@ public static void Main(string[] args) var functionVisitor = new FunctionVisitor(sw, libraryPath, prefixStrip, excludeFunctionsArray); foreach (var tu in translationUnits) { - clang.visitChildren(clang.getTranslationUnitCursor(tu), functionVisitor.Visit, new CXClientData(IntPtr.Zero)); + tu.Cursor.VisitChildren(functionVisitor.Visit, new CXClientData(IntPtr.Zero)); } } sw.WriteLine(" }"); @@ -191,10 +191,10 @@ public static void Main(string[] args) foreach (var tu in translationUnits) { - clang.disposeTranslationUnit(tu); + tu.Dispose(); } - clang.disposeIndex(createIndex); + createIndex.Dispose(); } } } diff --git a/ClangSharpPInvokeGenerator/StructVisitor.cs b/ClangSharpPInvokeGenerator/StructVisitor.cs index 04392f97..bd148ff6 100644 --- a/ClangSharpPInvokeGenerator/StructVisitor.cs +++ b/ClangSharpPInvokeGenerator/StructVisitor.cs @@ -29,18 +29,18 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) return CXChildVisitResult.CXChildVisit_Continue; } - CXCursorKind curKind = clang.getCursorKind(cursor); + CXCursorKind curKind = cursor.Kind; if (curKind == CXCursorKind.CXCursor_StructDecl) { this.fieldPosition = 0; - var structName = clang.getCursorSpelling(cursor).ToString(); + var structName = cursor.Spelling.ToString(); // struct names can be empty, and so we visit its sibling to find the name if (string.IsNullOrEmpty(structName)) { var forwardDeclaringVisitor = new ForwardDeclarationVisitor(cursor); - clang.visitChildren(clang.getCursorSemanticParent(cursor), forwardDeclaringVisitor.Visit, new CXClientData(IntPtr.Zero)); - structName = clang.getCursorSpelling(forwardDeclaringVisitor.ForwardDeclarationCursor).ToString(); + cursor.SemanticParent.VisitChildren(forwardDeclaringVisitor.Visit, new CXClientData(IntPtr.Zero)); + structName = forwardDeclaringVisitor.ForwardDeclarationCursor.Spelling.ToString(); if (string.IsNullOrEmpty(structName)) { @@ -54,7 +54,7 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) this.IndentedWriteLine("{"); this.indentLevel++; - clang.visitChildren(cursor, this.Visit, new CXClientData(IntPtr.Zero)); + cursor.VisitChildren(Visit, new CXClientData(IntPtr.Zero)); this.indentLevel--; this.IndentedWriteLine("}"); @@ -68,7 +68,7 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) if (curKind == CXCursorKind.CXCursor_FieldDecl) { - var fieldName = clang.getCursorSpelling(cursor).ToString(); + var fieldName = cursor.Spelling.ToString(); if (string.IsNullOrEmpty(fieldName)) { fieldName = "field" + this.fieldPosition; // what if they have fields called field*? :) diff --git a/ClangSharpPInvokeGenerator/TypeDefVisitor.cs b/ClangSharpPInvokeGenerator/TypeDefVisitor.cs index 7caffb02..93230550 100644 --- a/ClangSharpPInvokeGenerator/TypeDefVisitor.cs +++ b/ClangSharpPInvokeGenerator/TypeDefVisitor.cs @@ -23,10 +23,10 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) return CXChildVisitResult.CXChildVisit_Continue; } - CXCursorKind curKind = clang.getCursorKind(cursor); + CXCursorKind curKind = cursor.kind; if (curKind == CXCursorKind.CXCursor_TypedefDecl) { - var spelling = clang.getCursorSpelling(cursor).ToString(); + var spelling = cursor.Spelling.ToString(); if (this.visitedTypeDefs.Contains(spelling)) { @@ -35,7 +35,7 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) this.visitedTypeDefs.Add(spelling); - CXType type = clang.getCanonicalType(clang.getTypedefDeclUnderlyingType(cursor)); + CXType type = cursor.TypedefDeclUnderlyingType.CanonicalType; // we handle enums and records in struct and enum visitors with forward declarations also if (type.kind == CXTypeKind.CXType_Record || type.kind == CXTypeKind.CXType_Enum) @@ -46,7 +46,7 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) // no idea what this is? -- template stuff? if (type.kind == CXTypeKind.CXType_Unexposed) { - var canonical = clang.getCanonicalType(type); + var canonical = type.CanonicalType; if (canonical.kind == CXTypeKind.CXType_Unexposed) { return CXChildVisitResult.CXChildVisit_Continue; @@ -55,7 +55,7 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) if (type.kind == CXTypeKind.CXType_Pointer) { - var pointee = clang.getPointeeType(type); + var pointee = type.PointeeType; if (pointee.kind == CXTypeKind.CXType_Record || pointee.kind == CXTypeKind.CXType_Void) { this.tw.WriteLine(" public partial struct " + spelling); @@ -76,14 +76,14 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) { this.tw.WriteLine(" [UnmanagedFunctionPointer(" + pointee.CallingConventionSpelling() + ")]"); this.tw.Write(" public delegate "); - Extensions.ReturnTypeHelper(clang.getResultType(pointee), tw); + Extensions.ReturnTypeHelper(pointee.ResultType, tw); this.tw.Write(" "); this.tw.Write(spelling); this.tw.Write("("); uint argumentCounter = 0; - clang.visitChildren(cursor, delegate (CXCursor cxCursor, CXCursor parent1, IntPtr ptr) + cursor.VisitChildren(delegate (CXCursor cxCursor, CXCursor parent1, IntPtr ptr) { if (cxCursor.kind == CXCursorKind.CXCursor_ParmDecl) { @@ -100,7 +100,7 @@ public CXChildVisitResult Visit(CXCursor cursor, CXCursor parent, IntPtr data) } } - if (clang.isPODType(type) != 0) + if (type.IsPODType) { var podType = type.ToPlainTypeString(); this.tw.WriteLine(" public partial struct " + spelling);