Code Monkey home page Code Monkey logo

nclang's Introduction

Annoying CI settings noise on top of README (skip this section if you are a human being.)

Build Status

What is NClang?

NClang is a C# binding to LibClang. LibClang is a language service for C, C++ and Objective-C. A language service is a service (mostly as a library) that provides functionality to support writing programs in certain languages, such as a database for user program and its included files, or code completion engine that works against unsaved sources (text strings).

LibClang is part of Clang project which is part of LLVM project. Clang itself is a C/C++/ObjC compiler frontend for LLVM, and LLVM is the compiler infrastructure for several language frontends and backend binary formats.

NClang so far targets only LibClang. Note that Clang project has more language service like tools and libraries such as LibTooling (written in C++).

NClang is a hand-written C# binding that aims to bind every LibClang C API using P/Invoke (DllImport). The public API is to offer somewhat wrapped library around the native P/Invoke API which is internal within NClang.dll. The primary namespace is NClang.

The native binding API is based on the documentation. http://clang.llvm.org/doxygen/group__CINDEX.html

Clang API is documented per feature category, by the Clang dev. team. NClang API is designed to match its subject (as .NET is basically an object oriented infrastructure). There is most likely CXFooBar internal marshal class for each ClangFooBar public class. Every function that does not take Clang-specific type is collected into ClangService static class.

Some important classes are:

  • ClangIndex: represents an API entrypoint for LibClang, mostly to create TranslationUnit or IndexAction. CXIndex holds a set of translation units.
  • ClangTranslationUnit: represents an abstract parsed source.
  • ClangUnsavedFile represents a virtual "file" resources. It consists of a filename and its content string.
  • ClangCursor: represents a "cursor" in a source document.
  • ClangSourceLocation: represents a location in a source document, indicating file, line number and column number, and/or offset in a file.
  • ClangSourceRange: represents a range between two source locations.
  • ClangDiagnostic/-Set: give source analysis results as in error, warning and fix candidates.
  • ClangCodeCompleteResults, ClangCompletionResult and ClangCompletionString: support code completion.
  • ClangCompilationDatabase, ClangCompileCommands and ClangCompileCommand: seem to support "compilation database" which can be built from e.g. CMake output "compile_commands.json".
  • ClangIndexAction and ClangIndexerCallbacks: used to construct code index database, using callbacks that run through a file (either via filename or a ClangTranslationUnit) to be parsed.
  • ClangIndexInfo and all those derived classes: are passed as arguments in the callbacks represented as ClangIndexerCallbacks, to represent the node information to be indexed.
  • ClangToken/-Set: are returned as results of tokenization from translation unit.
  • ClangString: represents a string (CXString in native API), that wraps libclang's conceptual "string" that offers the actual string content.

Typically a ClangTranslationUnit is parsed from files (either filename string or ClangUnsavedFile objects) using ClangIndex.ParseTranslationUint() method.

The public API is designed to be close to the native ones, but sometimes that results in annoyance (e.g. ClangIndexerCallback expects IntPtr and some types that takes cryptic IntPtr thing).

What we can (and cannot) do with NClang?

NClang is just a wrapper around LibClang, and LibClang is not an ultimate language service implementation. It has pretty much limited functionality. What it supports are:

  • code completion, via ClangTranslationUnit.CodeCompleteAt()
  • tokenization, via ClangTranslationUnit.Tokenize() that would help semantic syntax highlighting
  • reference finder via ClangCursor.FindReferencesInFile()
  • code issue finder, via ClangTranslationUnit.DiagnosticSet
  • goto definition (indirectly), via ClangIndexAction.IndexSourceFile()
  • member list toolbar, via ClangIndexAction.IndexSourceFile()

What LibClang does NOT support:

  • comprehensive unsaved buffer support, probably. CXUnsavedFile only contains "contents" string without solid document structure.
  • any refactoring operation that involves updating sources: (1)code formatting (clang::format) (2)renaming
  • (more to list up, but you'd get the point)

Some features are supported in other parts of Clang such as LibTooling (clang::tooling namespace) but since there is no P/Invoke-able API, I cannot bind them right now. Probably CppSharp or Swig is required.

nclang's People

Contributors

atsushieno avatar danwalmsley avatar fealebenpae avatar kekyo avatar yavor87 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nclang's Issues

X64 compatiblity

The library does not work on X64 machines. I will check into a seperate branch a fix for this, its up to you if you wish to merge the changes. Dan

fully generated clang P/Invokes

split from #11.

Right now there are NClang.Natives/*.cs which are manual bindings for libclang C API. Since we have PInvokeGenerator it should be possible to completely generate these classes from C headers.

There is a work in progress branch generate-natives. So far it already builds, but causes run-time failures.

There is actually little benefit. The clang-c API is quite stable and does not break. There hasn't been a lot of additional API in clang-c. And automatic code generation results in silly hand-bound marshaling.

Is it still worth achieving this goal? Maybe. There should be some mapping support by external sources e.g. to determine which function parameters are mere pointers, arrays, or "out" or "ref" parameters, etc. And it will become a good dogfooding for that.

-x objective-c throws System.ArgumentNullException

How bug is triggered

A code snipet:

var index = ClangService.CreateIndex();
     var trans = index.ParseTranslationUnit(objectivecfile, args, //that are actually containing "-x objective-c"
    null, 
    TranslationUnitFlags.SingleFileParse);

What does it outputs ?

Value cannot be null.
Parameter name: handle

Stacktrace:

  at NClang.ClangObject..ctor (System.IntPtr handle) [0x00017] in <8ad3b26ee11343b1bd9b256eebdaec77>:0 
  at NClang.ClangTranslationUnit..ctor (System.IntPtr handle) [0x00000] in <8ad3b26ee11343b1bd9b256eebdaec77>:0 
  at NClang.ClangIndex.ParseTranslationUnit (System.String sourceFilename, System.String[] commandLineArgs, NClang.ClangUnsavedFile[] unsavedFiles, NClang.TranslationUnitFlags options) [0x00056] in <8ad3b26ee11343b1bd9b256eebdaec77>:0 
  at DemoCodeSnippet.Trigger [0x0005c] in the path of the file 
  at System.Threading.ThreadHelper.ThreadStart_Context (System.Object state) [0x00025] in <285579f54af44a2ca048dad6be20e190>:0 
  at System.Threading.ExecutionContext.RunInternal (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00071] in <285579f54af44a2ca048dad6be20e190>:0 
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state, System.Boolean preserveSyncCtx) [0x00000] in <285579f54af44a2ca048dad6be20e190>:0 
  at System.Threading.ExecutionContext.Run (System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, System.Object state) [0x0002b] in <285579f54af44a2ca048dad6be20e190>:0 
  at System.Threading.ThreadHelper.ThreadStart (System.Object obj) [0x0000f] in <285579f54af44a2ca048dad6be20e190>:0 

System configuration:

OS : Linux x64, Ubuntu derrivatives - KUbuntu 19.04

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.