Skip to content

Instantly share code, notes, and snippets.

@bgdncz
Created August 2, 2015 17:50
Show Gist options
  • Save bgdncz/a881d4ed87087651974f to your computer and use it in GitHub Desktop.
Save bgdncz/a881d4ed87087651974f to your computer and use it in GitHub Desktop.
@[Link("clang-3.5")]
lib LibClang
alias CXIndex = Void*
alias CXClientData = Void*
enum CXCursorKind
CXCursor_UnexposedDecl
CXCursor_StructDecl
CXCursor_UnionDecl
CXCursor_ClassDecl
CXCursor_EnumDecl
CXCursor_FieldDecl
CXCursor_EnumConstantDecl
CXCursor_FunctionDecl
CXCursor_VarDecl
CXCursor_ParmDecl
CXCursor_ObjCInterfaceDecl
CXCursor_ObjCCategoryDecl
CXCursor_ObjCProtocolDecl
CXCursor_ObjCPropertyDecl
CXCursor_ObjCIvarDecl
CXCursor_ObjCInstanceMethodDecl
CXCursor_ObjCClassMethodDecl
CXCursor_ObjCImplementationDecl
CXCursor_ObjCCategoryImplDecl
CXCursor_TypedefDecl
CXCursor_CXXMethod
CXCursor_Namespace
CXCursor_LinkageSpec
CXCursor_Constructor
CXCursor_Destructor
CXCursor_ConversionFunction
CXCursor_TemplateTypeParameter
CXCursor_NonTypeTemplateParameter
CXCursor_TemplateTemplateParameter
CXCursor_FunctionTemplate
CXCursor_ClassTemplate
CXCursor_ClassTemplatePartialSpecialization
CXCursor_NamespaceAlias
CXCursor_UsingDirective
CXCursor_UsingDeclaration
CXCursor_TypeAliasDecl
CXCursor_ObjCSynthesizeDecl
CXCursor_ObjCDynamicDecl
CXCursor_CXXAccessSpecifier
CXCursor_FirstDecl
CXCursor_LastDecl
CXCursor_FirstRef
CXCursor_ObjCSuperClassRef
CXCursor_ObjCProtocolRef
CXCursor_ObjCClassRef
CXCursor_TypeRef
CXCursor_CXXBaseSpecifier
CXCursor_TemplateRef
CXCursor_NamespaceRef
CXCursor_MemberRef
CXCursor_LabelRef
CXCursor_OverloadedDeclRef
CXCursor_VariableRef
CXCursor_LastRef
CXCursor_FirstInvalid
CXCursor_InvalidFile
CXCursor_NoDeclFound
CXCursor_NotImplemented
CXCursor_InvalidCode
CXCursor_LastInvalid
CXCursor_FirstExpr
CXCursor_UnexposedExpr
CXCursor_DeclRefExpr
CXCursor_MemberRefExpr
CXCursor_CallExpr
CXCursor_ObjCMessageExpr
CXCursor_BlockExpr
CXCursor_IntegerLiteral
CXCursor_FloatingLiteral
CXCursor_ImaginaryLiteral
CXCursor_StringLiteral
CXCursor_CharacterLiteral
CXCursor_ParenExpr
CXCursor_UnaryOperator
CXCursor_ArraySubscriptExpr
CXCursor_BinaryOperator
CXCursor_CompoundAssignOperator
CXCursor_ConditionalOperator
CXCursor_CStyleCastExpr
CXCursor_CompoundLiteralExpr
CXCursor_InitListExpr
CXCursor_AddrLabelExpr
CXCursor_StmtExpr
CXCursor_GenericSelectionExpr
CXCursor_GNUNullExpr
CXCursor_CXXStaticCastExpr
CXCursor_CXXDynamicCastExpr
CXCursor_CXXReinterpretCastExpr
CXCursor_CXXConstCastExpr
CXCursor_CXXFunctionalCastExpr
CXCursor_CXXTypeidExpr
CXCursor_CXXBoolLiteralExpr
CXCursor_CXXNullPtrLiteralExpr
CXCursor_CXXThisExpr
CXCursor_CXXThrowExpr
CXCursor_CXXNewExpr
CXCursor_CXXDeleteExpr
CXCursor_UnaryExpr
CXCursor_ObjCStringLiteral
CXCursor_ObjCEncodeExpr
CXCursor_ObjCSelectorExpr
CXCursor_ObjCProtocolExpr
CXCursor_ObjCBridgedCastExpr
CXCursor_PackExpansionExpr
CXCursor_SizeOfPackExpr
CXCursor_LambdaExpr
CXCursor_ObjCBoolLiteralExpr
CXCursor_ObjCSelfExpr
CXCursor_LastExpr
CXCursor_FirstStmt
CXCursor_UnexposedStmt
CXCursor_LabelStmt
CXCursor_CompoundStmt
CXCursor_CaseStmt
CXCursor_DefaultStmt
CXCursor_IfStmt
CXCursor_SwitchStmt
CXCursor_WhileStmt
CXCursor_DoStmt
CXCursor_ForStmt
CXCursor_GotoStmt
CXCursor_IndirectGotoStmt
CXCursor_ContinueStmt
CXCursor_BreakStmt
CXCursor_ReturnStmt
CXCursor_GCCAsmStmt
CXCursor_AsmStmt
CXCursor_ObjCAtTryStmt
CXCursor_ObjCAtCatchStmt
CXCursor_ObjCAtFinallyStmt
CXCursor_ObjCAtThrowStmt
CXCursor_ObjCAtSynchronizedStmt
CXCursor_ObjCAutoreleasePoolStmt
CXCursor_ObjCForCollectionStmt
CXCursor_CXXCatchStmt
CXCursor_CXXTryStmt
CXCursor_CXXForRangeStmt
CXCursor_SEHTryStmt
CXCursor_SEHExceptStmt
CXCursor_SEHFinallyStmt
CXCursor_MSAsmStmt
CXCursor_NullStmt
CXCursor_DeclStmt
CXCursor_OMPParallelDirective
CXCursor_OMPSimdDirective
CXCursor_OMPForDirective
CXCursor_OMPSectionsDirective
CXCursor_OMPSectionDirective
CXCursor_OMPSingleDirective
CXCursor_OMPParallelForDirective
CXCursor_OMPParallelSectionsDirective
CXCursor_OMPTaskDirective
CXCursor_OMPMasterDirective
CXCursor_OMPCriticalDirective
CXCursor_OMPTaskyieldDirective
CXCursor_OMPBarrierDirective
CXCursor_OMPTaskwaitDirective
CXCursor_OMPFlushDirective
CXCursor_SEHLeaveStmt
CXCursor_OMPOrderedDirective
CXCursor_OMPAtomicDirective
CXCursor_OMPForSimdDirective
CXCursor_OMPParallelForSimdDirective
CXCursor_OMPTargetDirective
CXCursor_OMPTeamsDirective
CXCursor_OMPTaskgroupDirective
CXCursor_OMPCancellationPointDirective
CXCursor_OMPCancelDirective
CXCursor_OMPTargetDataDirective
CXCursor_LastStmt
CXCursor_TranslationUnit
CXCursor_FirstAttr
CXCursor_UnexposedAttr
CXCursor_IBActionAttr
CXCursor_IBOutletAttr
CXCursor_IBOutletCollectionAttr
CXCursor_CXXFinalAttr
CXCursor_CXXOverrideAttr
CXCursor_AnnotateAttr
CXCursor_AsmLabelAttr
CXCursor_PackedAttr
CXCursor_PureAttr
CXCursor_ConstAttr
CXCursor_NoDuplicateAttr
CXCursor_CUDAConstantAttr
CXCursor_CUDADeviceAttr
CXCursor_CUDAGlobalAttr
CXCursor_CUDAHostAttr
CXCursor_CUDASharedAttr
CXCursor_LastAttr
CXCursor_PreprocessingDirective
CXCursor_MacroDefinition
CXCursor_MacroExpansion
CXCursor_MacroInstantiation
CXCursor_InclusionDirective
CXCursor_FirstPreprocessing
CXCursor_LastPreprocessing
CXCursor_ModuleImportDecl
CXCursor_FirstExtraDecl
CXCursor_LastExtraDecl
CXCursor_OverloadCandidate
end
enum CXChildVisitResult
CXChildVisit_Break
CXChildVisit_Continue
CXChildVisit_Recurse
end
struct CXUnsavedFile
filename, contents : LibC::Char*
length : LibC::ULong
end
struct CXString
data : Void*
private_flags : LibC::UInt
end
struct CXTranslationUnitImpl
tudata, stringpool : Void*
end
struct CXCursor
kind : CXCursorKind
xdata : LibC::Int
data : Void*[3]
end
alias CXTranslationUnit = CXTranslationUnitImpl*
alias CXCursorVisitor = (CXCursor, CXCursor, CXClientData) -> CXChildVisitResult
fun clang_createIndex(excludeDeclarationsFromPCH : LibC::Int, displayDiagnostics : LibC::Int) : CXIndex
fun clang_parseTranslationUnit(cIdx : CXIndex, source_filename : LibC::Char*, command_line_args : LibC::Char*, num_command_line_args : LibC::Int, unsaved_files : CXUnsavedFile*, num_unsaved_files : LibC::UInt, options : LibC::UInt) : CXTranslationUnit
fun clang_getTranslationUnitSpelling(cTUnit : CXTranslationUnit) : CXString
fun clang_getTranslationUnitCursor(arg : CXTranslationUnit) : CXCursor
fun clang_getCString(strng : CXString) : LibC::Char*
fun clang_disposeString(string : CXString)
fun clang_getCursorKind(cursor : CXCursor) : CXCursorKind
fun clang_getCursorSpelling(cursor : CXCursor) : CXString
fun clang_visitChildren(parent : CXCursor, visitor : CXCursorVisitor, client_data : CXClientData) : LibC::UInt
fun clang_getNullCursor() : CXCursor
end
class Cursor
def initialize(cursor : LibClang::CXCursor)
@cursor = cursor
@kind = LibClang.clang_getCursorKind(cursor)
str = LibClang.clang_getCursorSpelling(@cursor)
@spelling = String.new(LibClang.clang_getCString(str))
LibClang.clang_disposeString(str)
visitor = -> (child : LibClang::CXCursor, parent : LibClang::CXCursor, children : LibClang::CXClientData) {
if child != LibClang.clang_getNullCursor()
arr = children as Pointer(Array(Cursor))
arr.value << Cursor.new child
return LibClang::CXChildVisitResult::CXChildVisit_Continue
end
return LibClang::CXChildVisitResult::CXChildVisit_Break
}
children = [] of Cursor
LibClang.clang_visitChildren(@cursor, visitor, children.to_unsafe as Void*)
@children = children
end
def kind
@kind
end
def spelling
@spelling
end
def children
@children
end
end
def parse(file_name)
index = LibClang.clang_createIndex(0, 0)
test = LibClang::CXUnsavedFile.new
foo = 0_u8
unit = LibClang.clang_parseTranslationUnit(index, file_name, pointerof(foo), 0, pointerof(test), 0_u32, 0_u32)
cursor = LibClang.clang_getTranslationUnitCursor(unit)
cursor = Cursor.new cursor
end
file_name = ARGV[0]
parse file_name
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment