Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

some work on clang

  • Loading branch information...
commit c866f07e0ae2da8022abe00ddea9d713f9f720db 1 parent 53f0ece
Brandon Benvie authored
Showing with 128 additions and 59 deletions.
  1. +104 −59 libclang.js → bindings.js
  2. +24 −0 clang.js
163 libclang.js → bindings.js
View
@@ -1,9 +1,13 @@
-var api = require('windows/api');
+var path = require('path'),
+ fs = require('fs'),
+ api = require('windows/api'),
+ decorate = require('windows/utils').decorate;
var
+ Library = api.Library,
CallbackT = api.CallbackT,
+ BitfieldT = api.BitfieldT,
StructT = api.StructT,
- Library = api.Library,
ArrayT = api.ArrayT,
EnumT = api.EnumT,
NULL = api.NULL;
@@ -71,7 +75,7 @@ var CXDiagnosticSeverity = new EnumT('CXDiagnosticSeverity', {
Fatal: 4
});
-var CXDiagnosticDisplayOptions = new EnumT('CXDiagnosticDisplayOptions', {
+var CXDiagnosticDisplayOptions = new BitfieldT('CXDiagnosticDisplayOptions', uint, {
SourceLocation: 0x01,
Column: 0x02,
SourceRanges: 0x04,
@@ -80,7 +84,7 @@ var CXDiagnosticDisplayOptions = new EnumT('CXDiagnosticDisplayOptions', {
CategoryName: 0x20
});
-var CXTranslationUnit_Flags = new EnumT('CXTranslationUnit_Flags', {
+var CXTranslationUnit_Flags = new BitfieldT('CXTranslationUnit_Flags', uint, {
None: 0x0,
DetailedPreprocessingRecord: 0x01,
Incomplete: 0x02,
@@ -92,7 +96,7 @@ var CXTranslationUnit_Flags = new EnumT('CXTranslationUnit_Flags', {
NestedMacroInstantiations: 8
});
-var CXSaveTranslationUnit_Flags = new EnumT('CXSaveTranslationUnit_Flags', {
+var CXSaveTranslationUnit_Flags = new BitfieldT('CXSaveTranslationUnit_Flags', uint, {
None: 0x0
});
@@ -103,7 +107,7 @@ var CXSaveError = new EnumT('CXSaveError', {
InvalidTu: 3
});
-var CXReparse_Flags = new EnumT('CXReparse_Flags', {
+var CXReparse_Flags = new BitfieldT('CXReparse_Flags', uint, {
None: 0x0
});
@@ -362,7 +366,7 @@ var CXChildVisitResult = new EnumT('CXChildVisitResult', {
Recurse: 2
});
-var CXNameRefFlags = new EnumT('CXNameRefFlags', {
+var CXNameRefFlags = new BitfieldT('CXNameRefFlags', uint, {
RangeWantQualifier: 0x1,
RangeWantTemplateArgs: 0x2,
RangeWantSinglePiece: 0x4
@@ -400,7 +404,7 @@ var CXCompletionChunkKind = new EnumT('CXCompletionChunkKind', {
VerticalSpace: 20
});
-var CXCodeComplete_Flags = new EnumT('CXCodeComplete_Flags', {
+var CXCodeComplete_Flags = new BitfieldT('CXCodeComplete_Flags', uint, {
IncludeMacros: 0x01,
IncludePatterns: 0x02
});
@@ -494,18 +498,19 @@ var CXCodeCompleteResults = new StructT('CXCodeCompleteResults', {
});
+var
+ CXCursorVisitor = new CallbackT('CXCursorVisitor', CXChildVisitResult, [CXCursor, CXCursor, VoidT]),
+ CXInclusionVisitor = new CallbackT('CXInclusionVisitor', _void, [VoidT, CXSourceLocation.Δ, uint, VoidT]),
+ CXVisitor = new CallbackT('CXVisitor', CXVisitorResult, [VoidT, CXCursor, CXSourceRange]),
+ TheadedCallback = new CallbackT('TheadedCallback', _void, [VoidT]);
var CXCursorAndRangeVisitor = new StructT('CXCursorAndRangeVisitor', {
context: VoidT,
- visit: new CallbackT('visit', CXVisitorResult, [VoidT, CXCursor, CXSourceRange])
+ visit: CXVisitor
});
-var
- CXCursorVisitor = new CallbackT('CXCursorVisitor', CXChildVisitResult, [CXCursor, CXCursor, VoidT]),
- CXInclusionVisitor = new CallbackT('CXInclusionVisitor', _void, [VoidT, CXSourceLocation.Δ, uint, VoidT]);
-
var clang = new Library('libclang', 'clang_', {
clang_getCString: [ CString, { string: CXString } ],
@@ -534,7 +539,7 @@ var clang = new Library('libclang', 'clang_', {
clang_getDiagnostic: [ CXDiagnostic, { Unit: CXTranslationUnit, Index: uint } ],
clang_disposeDiagnostic: [ _void, { Diagnostic: CXDiagnostic } ],
clang_formatDiagnostic: [ CXString, { Diagnostic: CXDiagnostic, Options: uint } ],
- clang_defaultDiagnosticDisplayOptions: [ uint, { } ],
+ clang_defaultDiagnosticDisplayOptions: [ CXDiagnosticDisplayOptions, { } ],
clang_getDiagnosticSeverity: [ CXDiagnosticSeverity, { CXDiagnostic: CXDiagnostic } ],
clang_getDiagnosticLocation: [ CXSourceLocation, { CXDiagnostic: CXDiagnostic } ],
clang_getDiagnosticSpelling: [ CXString, { CXDiagnostic: CXDiagnostic } ],
@@ -548,13 +553,13 @@ var clang = new Library('libclang', 'clang_', {
clang_getTranslationUnitSpelling: [ CXString, { CTUnit: CXTranslationUnit } ],
clang_createTranslationUnitFromSourceFile: [ CXTranslationUnit, { CIdx: CXIndex, source_filename: CString, num_clang_command_line_args: int, clang_command_line_args: CStringArray, num_unsaved_files: uint, unsaved_files: CXUnsavedFile.Δ } ],
clang_createTranslationUnit: [ CXTranslationUnit, { CXIndex: CXIndex, ast_filename: CString } ],
- clang_defaultEditingTranslationUnitOptions: [ uint, { } ],
- clang_parseTranslationUnit: [ CXTranslationUnit, { CIdx: CXIndex, source_filename: CString, command_line_args: CStringArray, num_command_line_args: int, unsaved_files: CXUnsavedFile.Δ, num_unsaved_files: uint, options: uint } ],
- clang_defaultSaveOptions: [ uint, { TU: CXTranslationUnit } ],
- clang_saveTranslationUnit: [ int, { TU: CXTranslationUnit, FileName: CString, options: uint } ],
+ clang_defaultEditingTranslationUnitOptions: [ CXTranslationUnit_Flags, { } ],
+ clang_parseTranslationUnit: [ CXTranslationUnit, { CIdx: CXIndex, source_filename: CString, command_line_args: CStringArray, num_command_line_args: int, unsaved_files: CXUnsavedFile.Δ, num_unsaved_files: uint, options: CXTranslationUnit_Flags } ],
+ clang_defaultSaveOptions: [ CXSaveTranslationUnit_Flags, { TU: CXTranslationUnit } ],
+ clang_saveTranslationUnit: [ int, { TU: CXTranslationUnit, FileName: CString, options: CXSaveTranslationUnit_Flags } ],
clang_disposeTranslationUnit: [ _void, { CXTranslationUnit: CXTranslationUnit } ],
- clang_defaultReparseOptions: [ uint, { TU: CXTranslationUnit } ],
- clang_reparseTranslationUnit: [ int, { TU: CXTranslationUnit, num_unsaved_files: uint, unsaved_files: CXUnsavedFile.Δ, options: uint } ],
+ clang_defaultReparseOptions: [ CXReparse_Flags, { TU: CXTranslationUnit } ],
+ clang_reparseTranslationUnit: [ int, { TU: CXTranslationUnit, num_unsaved_files: uint, unsaved_files: CXUnsavedFile.Δ, options: CXReparse_Flags } ],
clang_getTUResourceUsageName: [ CString, { kind: CXTUResourceUsageKind } ],
clang_getCXTUResourceUsage: [ CXTUResourceUsage, { TU: CXTranslationUnit } ],
clang_disposeCXTUResourceUsage: [ _void, { usage: CXTUResourceUsage } ],
@@ -627,7 +632,7 @@ var clang = new Library('libclang', 'clang_', {
clang_CXXMethod_isVirtual: [ bool, { C: CXCursor } ],
clang_getTemplateCursorKind: [ CXCursorKind, { C: CXCursor } ],
clang_getSpecializedCursorTemplate: [ CXCursor, { C: CXCursor } ],
- clang_getCursorReferenceNameRange: [ CXSourceRange, { C: CXCursor, NameFlags: uint, PieceIndex: uint } ],
+ clang_getCursorReferenceNameRange: [ CXSourceRange, { C: CXCursor, NameFlags: CXNameRefFlags, PieceIndex: uint } ],
clang_getTokenKind: [ CXTokenKind, { CXToken: CXToken } ],
clang_getTokenSpelling: [ CXString, { CXToken: CXToken } ],
clang_getTokenLocation: [ CXSourceLocation, { CXToken: CXToken } ],
@@ -638,7 +643,7 @@ var clang = new Library('libclang', 'clang_', {
clang_getCursorKindSpelling: [ CXString, { Kind: CXCursorKind } ],
clang_getDefinitionSpellingAndExtent: [ _void, { CXCursor: CXCursor, startBuf: CStringArray, endBuf: CStringArray, startLine: uint.Δ, startColumn: uint.Δ, endLine: uint.Δ, endColumn: uint.Δ } ],
clang_enableStackTraces: [ _void, { } ],
- clang_executeOnThread: [ _void, { fn: new CallbackT('fn', _void, [VoidT]).Δ, user_data: VoidT, stack_size: uint } ],
+ clang_executeOnThread: [ _void, { fn: TheadedCallback.Δ, user_data: VoidT, stack_size: uint } ],
clang_getCompletionChunkKind: [ CXCompletionChunkKind, { completion_string: CXCompletionString, chunk_number: uint } ],
clang_getCompletionChunkText: [ CXString, { completion_string: CXCompletionString, chunk_number: uint } ],
clang_getCompletionChunkCompletionString: [ CXCompletionString, { completion_string: CXCompletionString, chunk_number: uint } ],
@@ -648,8 +653,8 @@ var clang = new Library('libclang', 'clang_', {
clang_getCompletionNumAnnotations: [ uint, { completion_string: CXCompletionString } ],
clang_getCompletionAnnotation: [ CXString, { completion_string: CXCompletionString, annotation_number: uint } ],
clang_getCursorCompletionString: [ CXCompletionString, { cursor: CXCursor } ],
- clang_defaultCodeCompleteOptions: [ uint, { } ],
- clang_codeCompleteAt: [ CXCodeCompleteResults, { TU: CXTranslationUnit, complete_filename: CString, complete_line: uint, complete_column: uint, unsaved_files: CXUnsavedFile.Δ, num_unsaved_files: uint, options: uint } ],
+ clang_defaultCodeCompleteOptions: [ CXCodeComplete_Flags, { } ],
+ clang_codeCompleteAt: [ CXCodeCompleteResults, { TU: CXTranslationUnit, complete_filename: CString, complete_line: uint, complete_column: uint, unsaved_files: CXUnsavedFile.Δ, num_unsaved_files: uint, options: CXCodeComplete_Flags } ],
clang_sortCodeCompletionResults: [ _void, { Results: CXCompletionResult, NumResults: uint } ],
clang_disposeCodeCompleteResults: [ _void, { Results: CXCodeCompleteResults } ],
clang_codeCompleteGetNumDiagnostics: [ int, { Results: CXCodeCompleteResults } ],
@@ -685,38 +690,24 @@ function addAccessor(o, name, get, set){
Object.defineProperty(o, name, desc);
}
-
-
-
-
-var crashRecovery = false;
-
-var libclang = {
- get version(){
- var vers = clang.getClangVersion();
- var ret = vers.getCString();
- vers.dispose();
- return ret;
+decorate(CXString.prototype, [
+ function deref(){
+ return clang.getCString(this);
},
- get crashRecovery(){ return crashRecovery },
- set crashRecovery(v){ clang.toggleCrashRecovery(crashRecovery = !!v) },
- enableStackTraces: function enableStackTraces(){ clang.enableStackTraces() },
- getRemappings: function getRemappings(path){
- return clang.getRemappings(path);
+ function valueOf(){
+ return clang.getCString(this);
},
- createIndex: function createIndex(excludeDeclsFromPCH, displayDiagnostics){
- return clang.createIndex(+excludeDeclsFromPCH || 0, +displayDiagnostics || 0);
+ function toString(){
+ return clang.getCString(this);
}
-};
+]);
addMethod(CXString.prototype, 'getCString', clang.getCString);
addMethod(CXString.prototype, 'dispose', clang.disposeString);
-CXIndex.create = clang.createIndex;
-CXIndex.executeOnThread = clang.executeOnThread;
addMethod(CXIndex.prototype, 'dispose', clang.disposeIndex);
addMethod(CXIndex.prototype, 'createTranslationUnit', clang.createTranslationUnit);
-addMethod(CXIndex.prototype, 'createTranslationUnitFromFile', clang.createTranslationUnitFromFile);
+addMethod(CXIndex.prototype, 'createTranslationUnitFromFile', clang.createTranslationUnitFromSourceFile);
addMethod(CXIndex.prototype, 'parseTranslationUnit', clang.parseTranslationUnit);
addAccessor(CXFile.prototype, 'filename', clang.getFileName);
@@ -729,16 +720,16 @@ addMethod(CXTranslationUnit.prototype, 'getLocationForOffset', clang.getLocation
addMethod(CXTranslationUnit.prototype, 'getDiagnostic', clang.getDiagnostic);
addMethod(CXTranslationUnit.prototype, 'save', clang.saveTranslationUnit);
addMethod(CXTranslationUnit.prototype, 'dispose', clang.disposeTranslationUnit);
-addMethod(CXTranslationUnit.prototype, 'reparse', clang.disposeTranslationUnit);
+addMethod(CXTranslationUnit.prototype, 'reparse', clang.clang_reparseTranslationUnit);
addMethod(CXTranslationUnit.prototype, 'tokenize', clang.tokenize);
addMethod(CXTranslationUnit.prototype, 'annotateTokens', clang.annotateTokens);
addMethod(CXTranslationUnit.prototype, 'disposeTokens', clang.disposeTokens);
addMethod(CXTranslationUnit.prototype, 'codeCompleteAt', clang.codeCompleteAt);
addMethod(CXTranslationUnit.prototype, 'getInclusions', clang.getInclusions);
+addMethod(CXTranslationUnit.prototype, 'defaultSaveOptions', clang.defaultSaveOptions);
+addMethod(CXTranslationUnit.prototype, 'defaultReparseOptions', clang.defaultReparseOptions);
addAccessor(CXTranslationUnit.prototype, 'numDiagnostics', clang.getNumDiagnostics);
addAccessor(CXTranslationUnit.prototype, 'spelling', clang.getTranslationUnitSpelling);
-addAccessor(CXTranslationUnit.prototype, 'defaultSaveOptions', clang.defaultSaveOptions);
-addAccessor(CXTranslationUnit.prototype, 'defaultReparseOptions', clang.defaultReparseOptions);
addAccessor(CXTranslationUnit.prototype, 'resourceUsage', clang.getCXTUResourceUsage);
addAccessor(CXTranslationUnit.prototype, 'cursor', clang.getTranslationUnitCursor);
@@ -752,7 +743,6 @@ addMethod(CXSourceLocation.prototype, 'getInstantiationLocation', clang.getInsta
addMethod(CXSourceLocation.prototype, 'getSpellingLocation', clang.getSpellingLocation);
addAccessor(CXSourceLocation.prototype, 'cursor', clang.getCursor);
-CXDiagnostic.defaultDisplayOptions = clang.defaultDiagnosticDisplayOptions;
addMethod(CXDiagnostic.prototype, 'format', clang.formatDiagnostic);
addMethod(CXDiagnostic.prototype, 'dispose', clang.disposeDiagnostic);
addMethod(CXDiagnostic.prototype, 'getLocation', clang.getDiagnosticLocation);
@@ -766,9 +756,6 @@ addAccessor(CXDiagnostic.prototype, 'numRanges', clang.getDiagnosticNumRanges);
addAccessor(CXDiagnostic.prototype, 'range', clang.getDiagnosticRange);
addAccessor(CXDiagnostic.prototype, 'numFixIts', clang.getDiagnosticNumFixIts);
-CXCursor.nullCursor = clang.getNullCursor;
-CXCursor.isEqual = clang.equalCursors;
-CXCursor.disposeOverriddenCursors = clang.disposeOverriddenCursors;
addMethod(CXCursor.prototype, 'getOverriddenCursors', clang.getOverriddenCursors);
addMethod(CXCursor.prototype, 'getIncludedFile', clang.getIncludedFile);
addMethod(CXCursor.prototype, 'getOverloadedDecl', clang.getOverloadedDecl);
@@ -780,10 +767,10 @@ addMethod(CXCursor.prototype, 'CXXMethod_isStatic', clang.CXXMethod_isStatic);
addMethod(CXCursor.prototype, 'CXXMethod_isVirtual', clang.CXXMethod_isVirtual);
addMethod(CXCursor.prototype, 'getTemplateKind', clang.getTemplateCursorKind);
addMethod(CXCursor.prototype, 'getSpecializedTemplate', clang.getSpecializedCursorTemplate);
+addMethod(CXCursor.prototype, 'findReferencesInFile', clang.findReferencesInFile);
addMethod(CXCursor.prototype, 'getReferenceNameRange', clang.getCursorReferenceNameRange);
addMethod(CXCursor.prototype, 'getDefinitionSpellingAndExtent', clang.getDefinitionSpellingAndExtent);
addMethod(CXCursor.prototype, 'getCursorCompletionString', clang.getCursorCompletionString);
-addMethod(CXCursor.prototype, 'getCursorCompletionString', clang.getCursorCompletionString);
addAccessor(CXCursor.prototype, 'isNull', clang.Cursor_isNull);
addAccessor(CXCursor.prototype, 'isVirtualBase', clang.isVirtualBase);
addAccessor(CXCursor.prototype, 'hash', clang.hashCursor);
@@ -804,9 +791,7 @@ addAccessor(CXCursor.prototype, 'displayName', clang.getCursorDisplayName);
addAccessor(CXCursor.prototype, 'referenced', clang.getCursorReferenced);
addAccessor(CXCursor.prototype, 'definition', clang.getCursorDefinition);
addAccessor(CXCursor.prototype, 'isDefinition', clang.isCursorDefinition);
-addAccessor(CXCursor.prototype, 'findReferencesInFile', clang.findReferencesInFile);
-CXCursorSet.create = clang.createCXCursorSet;
addMethod(CXCursorSet.prototype, 'dispose', clang.disposeCXCursorSet);
addMethod(CXCursorSet.prototype, 'contains', clang.CXCursorSet_contains);
addMethod(CXCursorSet.prototype, 'insert', clang.CXCursorSet_insert);
@@ -822,7 +807,6 @@ addAccessor(CXCursorKind.prototype, 'isPreprocessing', clang.isPreprocessing);
addAccessor(CXCursorKind.prototype, 'isUnexposed', clang.isUnexposed);
addAccessor(CXCursorKind.prototype, 'spelling', clang.getCursorKindSpelling);
-CXType.equalTypes = clang.equalTypes;
addMethod(CXType.prototype, 'getArrayElementType', clang.getArrayElementType);
addMethod(CXType.prototype, 'getArraySize', clang.getArraySize);
addAccessor(CXType.prototype, 'canonical', clang.getCanonicalType);
@@ -851,7 +835,6 @@ addAccessor(CXToken.prototype, 'spelling', clang.getTokenSpelling);
addAccessor(CXToken.prototype, 'location', clang.getTokenLocation);
addAccessor(CXToken.prototype, 'extent', clang.getTokenExtent);
-CXCompletionString.defaultCodeCompleteOptions = clang.defaultCodeCompleteOptions;
addMethod(CXCompletionString.prototype, 'chunkKind', clang.getCompletionChunkKind);
addMethod(CXCompletionString.prototype, 'chunkText', clang.getCompletionChunkText);
addMethod(CXCompletionString.prototype, 'chunkCompletionString', clang.getCompletionChunkCompletionString);
@@ -873,3 +856,65 @@ addAccessor(CXCodeCompleteResults.prototype, 'objCSelector', clang.codeCompleteG
addMethod(CXRemapping.prototype, 'dispose', clang.remap_dispose);
addMethod(CXRemapping.prototype, 'getFilenames', clang.remap_getFilenames);
addAccessor(CXRemapping.prototype, 'numFiles', clang.remap_getNumFiles);
+
+
+CXType.prototype.isEqual = function isEqual(type){
+ return clang.equalTypes(this, type);
+};
+
+CXCursor.prototype.isEqual = function isEqual(cursor){
+ return clang.equalCursors(this, type);
+};
+
+
+
+module.exports = libclang;
+
+function libclang(file, debug){
+ var index = clang.createIndex(0, +!!debug);
+ if (typeof file === 'string') {
+ file = path.resolve(file);
+ if (fs.existsSync(file)) {
+ return index.createTranslationUnitFromFile(file, 0, NULL, 0, NULL);
+ }
+ }
+}
+
+decorate(libclang, [
+ function version(){
+ return clang.getClangVersion();
+ },
+ function setCrashRecovery(v){
+ clang.toggleCrashRecovery(!!v);
+ },
+ function enableStackTraces(){
+ clang.enableStackTraces();
+ },
+ function getRemappings(path){
+ return clang.getRemappings(path);
+ },
+ function createIndex(excludeDeclsFromPCH, displayDiagnostics){
+ return clang.createIndex(+excludeDeclsFromPCH || 0, +displayDiagnostics || 0);
+ },
+ function defaultEditingTranslationUnitOptions(){
+ return clang.defaultEditingTranslationUnitOptions();
+ },
+ function defaultDiagnosticDisplayOptions(){
+ return clang.defaultDiagnosticDisplayOptions();
+ },
+ function defaultCodeCompleteOptions(){
+ return clang.defaultCodeCompleteOptions();
+ },
+ function getNullLocation(){
+ return clang.getNullLocation();
+ },
+ function getNullRange(){
+ return clang.getNullRange();
+ },
+ function getNullCursor(){
+ return clang.getNullCursor();
+ },
+ function createCursorSet(){
+ return clang.createCXCursorSet();
+ }
+]);
24 clang.js
View
@@ -0,0 +1,24 @@
+var ReferenceType = require('windows/ReferenceType');
+var clang = require('./bindings');
+
+
+// var window = appjs.createWindow();
+// window.on('create', function(){
+// window.frame.show();
+// });
+
+// window.on('ready', function(){
+// window.module = module;
+// window.require = require;
+// window.clang = clang;
+// window.h = clang('./libclang.h');
+// window.frame.openDevTools();
+// });
+
+//var h = clang('./libclang.h');
+
+var tu = clang('./libclang.h');
+var file = tu.getFile('libclang.h');
+var cursor = tu.cursor;
+
+console.log(cursor.kind);
Please sign in to comment.
Something went wrong with that request. Please try again.