Skip to content

CXTranslationUnit.Tokenize always crashes #234

@f1nalspace

Description

@f1nalspace

The Tokenize() function in CXTranslationUnit does not work and always crashes with:

System.ArgumentException: 'Cannot use type 'ClangSharp.Interop.CXToken'. Only value types without pointers or references are supported.'

Even parsing one simple function wont work:

using CXIndex index = CXIndex.Create();

// We only parse single C files
CXTranslationUnit_Flags flags = CXTranslationUnit_Flags.CXTranslationUnit_SingleFileParse;

// Parse all comments
ReadOnlySpan<string> arguments = new ReadOnlySpan<string>(new[] { "-fparse-all-comments" });

// Very simple C code for testing
string testContent = "/*! @brief The main entry point */\nint main(int argc, char **argv) {\n return 0;\n}";

// We load the content ourself, so we use CXUnsavedFile for parsing the actual content
// Filename is just a "identifier" for GetFile() later
ReadOnlySpan<CXUnsavedFile> unsavedFiles = new ReadOnlySpan<CXUnsavedFile>(new[] { CXUnsavedFile.Create(filename, testContent) });

using CXTranslationUnit translationUnit = CXTranslationUnit.Parse(index, filename, arguments, unsavedFiles, flags);

// Create the full range for the entire file
CXFile firstFile = translationUnit.GetFile(filename);
CXSourceLocation firstLoc = translationUnit.GetLocation(firstFile, 1, 1);
CXSourceLocation lastLoc = translationUnit.GetLocationForOffset(firstFile, (uint)content.Length);
CXSourceRange range = CXSourceRange.Create(firstLoc, lastLoc);

 // Always crashes with System.ArgumentException: 'Cannot use type 'ClangSharp.Interop.CXToken'. Only value types without pointers or references are supported.'
Span<CXToken> tokens = translationUnit.Tokenize(range);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions