Skip to content

Instantly share code, notes, and snippets.

@chrisforbes
Created July 5, 2010 01:13
Show Gist options
  • Save chrisforbes/463906 to your computer and use it in GitHub Desktop.
Save chrisforbes/463906 to your computer and use it in GitHub Desktop.
// C# bindings for the Clang C API
// requires clang.dll from llvm to work.
using System;
using System.Runtime.InteropServices;
namespace ClangApi
{
using Index = IntPtr;
using File = IntPtr;
using TranslationUnit = IntPtr;
using ClientData = IntPtr;
using Diagnostic = IntPtr;
using CompletionString = IntPtr;
static class Native
{
[StructLayout(LayoutKind.Sequential)]
public struct String
{
IntPtr Spelling;
int MustFreeString;
}
[StructLayout(LayoutKind.Sequential)]
public struct CXUnsavedFile
{
public IntPtr Filename;
public IntPtr Contents;
public uint Length;
}
[DllImport("clang.dll")]
public static extern string clang_getCString( String str );
[DllImport("clang.dll")]
public static extern void clang_disposeString(String str);
[DllImport("clang.dll")]
public static extern Index clang_createIndex(int excludeDeclarationsFromPCH, int displayDiagnostics);
[DllImport("clang.dll")]
public static extern void clang_disposeIndex(Index index);
[DllImport("clang.dll")]
public static extern void clang_setUseExternalASTGeneration(Index index, int value);
[DllImport("clang.dll")]
public static extern String clang_getFilename(File file);
[DllImport("clang.dll")]
public static extern long clang_getFiletime(File file);
[DllImport("clang.dll")]
public static extern File clang_getFile(TranslationUnit tu, string filename);
[StructLayout(LayoutKind.Sequential)]
public struct SourceLocation
{
IntPtr data0;
IntPtr data1;
uint data2;
}
[StructLayout(LayoutKind.Sequential)]
public struct SourceRange
{
IntPtr data0;
IntPtr data1;
uint beginIntData;
uint endIntData;
}
[DllImport("clang.dll")]
public static extern SourceLocation clang_getNullLocation();
[DllImport("clang.dll")]
public static extern uint clang_equalLocations(SourceLocation loc1, SourceLocation loc2);
[DllImport("clang.dll")]
public static extern SourceLocation clang_getLocation(TranslationUnit tu, File file, uint line, uint column);
[DllImport("clang.dll")]
public static extern SourceRange clang_getNullRange();
[DllImport("clang.dll")]
public static extern SourceRange clang_getRange(SourceLocation begin, SourceLocation end);
[DllImport("clang.dll")]
public static extern void clang_getInstantiationLocation(SourceLocation location,
File file, out uint line, out uint column, out uint offset);
[DllImport("clang.dll")]
public static extern SourceLocation clang_getRangeStart(SourceRange range);
[DllImport("clang.dll")]
public static extern SourceLocation clang_getRangeEnd(SourceRange range);
public enum DiagnosticSeverity { Ignored, Note, Warning, Error, Fatal }
[DllImport("clang.dll")]
public static extern uint clang_getNumDiagnostics(TranslationUnit tu);
[DllImport("clang.dll")]
public static extern Diagnostic clang_getDiagnostic(TranslationUnit tu, uint index);
[DllImport("clang.dll")]
public static extern void clang_disposeDiagnostic(Diagnostic diag);
[Flags]
public enum DiagnosticDisplayOptions
{
DisplaySourceLocation = 1,
DisplayColumn = 2,
DisplaySourceRanges = 4
};
[DllImport("clang.dll")]
public static extern String clang_formatDiagnostic(Diagnostic diag, DiagnosticDisplayOptions options);
[DllImport("clang.dll")]
public static extern uint clang_defaultDiagnosticDisplayOptions();
[DllImport("clang.dll")]
public static extern DiagnosticSeverity clang_getDiagnosticSeverity(Diagnostic diag);
[DllImport("clang.dll")]
public static extern SourceLocation clang_getDiagnosticLocation(Diagnostic diag);
[DllImport("clang.dll")]
public static extern String clang_getDiagnosticSpelling(Diagnostic diag);
[DllImport("clang.dll")]
public static extern uint clang_getDiagnosticNumranges(Diagnostic diag);
[DllImport("clang.dll")]
public static extern SourceRange clang_getDiagnosticRange(Diagnostic diag, uint range);
[DllImport("clang.dll")]
public static extern uint clang_getDiagnosticNumFixIts(Diagnostic diag);
[DllImport("clang.dll")]
public static extern String clang_getDiagnosticFixIt(Diagnostic diag, uint fixit,
out SourceRange replacementRange);
[DllImport("clang.dll")]
public static extern String clang_getTranslationUnitSpelling(TranslationUnit tu);
[DllImport("clang.dll")]
public static extern TranslationUnit clang_createTranslationUnitFromSourceFile(
Index index, string source_filename, int num_clang_command_line_args,
string[] clang_command_line_args,
uint num_unsaved_files,
CXUnsavedFile[] unsavedFiles);
[DllImport("clang.dll")]
public static extern TranslationUnit clang_createTranslationUnit(Index index, string ast_filename);
[DllImport("clang.dll")]
public static extern void clang_disposeTranslationUnit(TranslationUnit tu);
public enum CursorKind
{
UnexposedDecl = 1,
StructDecl,
UnionDecl,
ClassDecl,
EnumDecl,
FieldDecl,
EnumConstantDecl,
FunctionDecl,
VarDecl,
ParmDecl,
ObjCInterfaceDecl,
ObjCCategoryDecl,
ObjCProtcolDecl,
ObjCPropertyDecl,
ObjCIvarDecl,
ObjCInstanceMethodDecl,
ObjCClassMethodDecl,
ObjCImplementationDecl,
ObjCCategoryImplDecl,
TypedefDecl,
CxxMethod,
Namespace,
LinkageSpec,
FirstDecl = UnexposedDecl,
LastDecl = LinkageSpec,
ObjCSuperClassRef = 40,
ObjCProtocolRef,
ObjCClassRef,
TypeRef,
FirstRef = ObjCSuperClassRef,
LastRef = TypeRef,
InvalidFile = 70,
NoDeclFound,
NotImplemented,
InvalidCode,
FirstInvalid = InvalidFile,
LastInvalid = InvalidCode,
UnexposedExpr = 100,
DeclRefExpr,
MemberRefExpr,
CallExpr,
ObjCMessageExpr,
BlockExpr,
FirstExpr = UnexposedExpr,
LastExpr = BlockExpr,
UnexposedStmt = 200,
FirstStmt = UnexposedStmt,
LastStmt = UnexposedStmt,
TranslationUnit = 300,
UnexposedAttr = 400,
IBActionAttr,
IBOutletAttr,
IBOutletCollectionAttr,
FirstAttr = UnexposedAttr,
LastAttr = IBOutletCollectionAttr,
PreprocessingDirective = 500,
MacroDefinition,
MacroInstantiation,
FirstPreprocessing = PreprocessingDirective,
LastPreprocessing = MacroInstantiation
}
[StructLayout(LayoutKind.Sequential)]
public struct Cursor
{
CursorKind kind;
IntPtr data0, data1, data2;
};
[DllImport("clang.dll")]
public static extern Cursor clang_getNullCursor();
[DllImport("clang.dll")]
public static extern Cursor clang_getTranslationUnitCursor(TranslationUnit tu);
[DllImport("clang.dll")]
public static extern uint clang_equalCursors(Cursor a, Cursor b);
[DllImport("clang.dll")]
public static extern CursorKind clang_getCursorKind(Cursor c);
[DllImport("clang.dll")]
public static extern uint clang_isDeclaration(CursorKind ck);
[DllImport("clang.dll")]
public static extern uint clang_isReference(CursorKind ck);
[DllImport("clang.dll")]
public static extern uint clang_isExpression(CursorKind ck);
[DllImport("clang.dll")]
public static extern uint clang_isStatement(CursorKind ck);
[DllImport("clang.dll")]
public static extern uint clang_isInvalid(CursorKind ck);
[DllImport("clang.dll")]
public static extern uint clang_isTranslationUnit(CursorKind ck);
[DllImport("clang.dll")]
public static extern uint clang_isPreprocessing(CursorKind ck);
[DllImport("clang.dll")]
public static extern uint clang_isUnexposed(CursorKind ck);
public enum LinkageKind { Invalid, NoLinkage, Internal, UniqueExternal, External };
[DllImport("clang.dll")]
public static extern LinkageKind clang_getCursorLinkage(Cursor cursor);
public enum LanguageKind { Invalid = 0, C, ObjC, CPlusPlus };
[DllImport("clang.dll")]
public static extern LanguageKind clang_getCursorLanguage(Cursor cursor);
[DllImport("clang.dll")]
public static extern Cursor clang_getCursor(TranslationUnit tu, SourceLocation loc);
[DllImport("clang.dll")]
public static extern SourceLocation clang_getCursorLocation(Cursor cursor);
[DllImport("clang.dll")]
public static extern SourceRange clang_getCursorExtent(Cursor cursor);
public enum TypeKind
{
Invalid = 0,
Unexposed,
Void,
Bool,
Char_U,
UChar,
Char16,
Char32,
UShort,
UInt,
ULong,
ULongLong,
UInt128,
Char_S,
SChar,
WChar,
Short,
Int,
Long,
LongLong,
Int128,
Float,
Double,
LongDouble,
NullPtr,
Overload,
Dependent,
ObjCId,
ObjCClass,
ObjCSel,
FirstBuiltin = Void,
LastBuiltin = ObjCSel,
Complex = 100,
Pointer,
BlockPointer,
LValueReference,
RValueReference,
Record,
Enum,
Typedef,
ObjCInterface,
ObjCObjectPointer,
FunctionNoProto,
FunctionProto
}
[StructLayout(LayoutKind.Sequential)]
public struct Type
{
public TypeKind kind;
IntPtr data0, data1;
};
[DllImport("clang.dll")]
public static extern Type clang_getCursorType(Cursor c);
[DllImport("clang.dll")]
public static extern uint clang_equalTypes(Type a, Type b);
[DllImport("clang.dll")]
public static extern Type clang_getCanonicalType(Type t);
[DllImport("clang.dll")]
public static extern Type clang_getPointeeType(Type t);
[DllImport("clang.dll")]
public static extern Cursor clang_getTypeDeclaration(Type t);
[DllImport("clang.dll")]
public static extern String clang_getTypeKindSpelling(TypeKind k);
[DllImport("clang.dll")]
public static extern Type clang_getResultType(Type t);
[DllImport("clang.dll")]
public static extern Type clang_getCursorResultType(Cursor c);
public enum ChildVisitResult
{
Break,
Continue,
Recurse,
};
public delegate ChildVisitResult CursorVisitor(Cursor cursor, Cursor parent, ClientData data);
[DllImport("clang.dll")]
public static extern String clang_getCursorUSR(Cursor c);
[DllImport("clang.dll")]
public static extern String clang_constructUSR_ObjCClass(string className);
[DllImport("clang.dll")]
public static extern String clang_constructUSR_ObjCCategory(string className, string categoryName);
[DllImport("clang.dll")]
public static extern String clang_constructUSR_ObjCProtocol(string protocolName);
[DllImport("clang.dll")]
public static extern String clang_constructUSR_ObjCIvar(string name, String classUSR);
[DllImport("clang.dll")]
public static extern String clang_constructUSR_ObjCMethod(string name, uint isInstanceMethod,
String classUSR);
[DllImport("clang.dll")]
public static extern String clang_constructUSR_ObjCProperty(string prop, String classUSR);
[DllImport("clang.dll")]
public static extern String clang_getCursorSpelling(Cursor c);
[DllImport("clang.dll")]
public static extern Cursor clang_getCursorReferenced(Cursor c);
[DllImport("clang.dll")]
public static extern Cursor clang_getCursorDefinition(Cursor c);
[DllImport("clang.dll")]
public static extern uint clang_isCursorDefinition(Cursor c);
[DllImport("clang.dll")]
public static extern uint clang_CXXMethod_isStatic(Cursor c);
public enum TokenKind
{
Punctuation,
Keyword,
Identifier,
Literal,
Comment
}
[StructLayout(LayoutKind.Sequential)]
public struct Token
{
uint data0, data1, data2, data3;
IntPtr ptr;
}
[DllImport("clang.dll")]
public static extern TokenKind clang_getTokenKind(Token t);
[DllImport("clang.dll")]
public static extern String clang_getTokenSpelling(TranslationUnit tu, Token t);
[DllImport("clang.dll")]
public static extern SourceLocation clang_getTokenLocation(TranslationUnit tu, Token t);
[DllImport("clang.dll")]
public static extern SourceRange clang_getTokenExtend(TranslationUnit tu, Token t);
[DllImport("clang.dll")] // out Token[] tokens?
public static extern void clang_tokenize(TranslationUnit tu, SourceRange range,
out IntPtr tokens, out uint numTokens);
[DllImport("clang.dll")]
public static extern void clang_annotateTokens(TranslationUnit tu, IntPtr tokens, uint numTokens,
IntPtr cursors);
[DllImport("clang.dll")]
public static extern void clang_disposeTokens(TranslationUnit tu, IntPtr tokens, uint numTokens);
[DllImport("clang.dll")]
public static extern String clang_getCursorKindSpelling(CursorKind ck);
[StructLayout(LayoutKind.Sequential)]
public struct CompletionResult
{
public CursorKind kind;
public CompletionString completionString;
}
public enum CompletionChunkKind
{
Optional,
TypedText,
Text,
Placeholder,
Informative,
CurrentParameter,
LeftParen,
RightParen,
LeftBracket,
RightBracket,
LeftBrace,
RightBrace,
LeftAngle,
RightAngle,
Comma,
ResultType,
Colon,
Semicolon,
Equal,
HorizontalSpace,
VerticalSpace,
}
[DllImport("clang.dll")]
public static extern CompletionChunkKind clang_getCompletionChunkKind(CompletionString cs,
uint chunk);
[DllImport("clang.dll")]
public static extern String clang_getCompletionChunkText(CompletionString cs, uint chunk);
[DllImport("clang.dll")]
public static extern CompletionString clang_getCompletionChunkCompletionString(CompletionString cs,
uint chunk);
[DllImport("clang.dll")]
public static extern uint clang_getNumCompletionChunks(CompletionString cs);
[DllImport("clang.dll")]
public static extern uint clang_getCompletionPriority(CompletionString cs);
[StructLayout(LayoutKind.Sequential)]
public struct CodeCompleteResults
{
public IntPtr results;
public uint NumResults;
}
[DllImport("clang.dll")]
public static extern IntPtr clang_codeComplete(
Index index, string source_filename, int num_clang_command_line_args,
string[] clang_command_line_args,
uint num_unsaved_files,
CXUnsavedFile[] unsavedFiles,
string complete_filename,
uint complete_line,
uint complete_column);
[DllImport("clang.dll")]
public static extern void clang_disposeCodeCompleteResults(IntPtr results);
[DllImport("clang.dll")]
public static extern uint clang_codeCompleteGetNumDiagnostics(IntPtr results);
[DllImport("clang.dll")]
public static extern Diagnostic clang_codeCompleteGetDiagnostic(IntPtr results,
uint index);
[DllImport("clang.dll")]
public static extern String clang_getClangVersion();
public delegate void InclusionVisitor(File included_file,
IntPtr inclusionStack,
uint stackSize,
ClientData clientData);
[DllImport("clang.dll")]
public static extern void clang_getInclusions(TranslationUnit tu,
InclusionVisitor visitor, ClientData data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment