<div dir="ltr">It seems to be libclang or libclang+JNI problem as i'm experienced with JNI and i checked packing/unpacking using my concrete class.</div><div class="gmail_extra"><br><br><div class="gmail_quote">2013/10/11 Anton Smirnov <span dir="ltr"><<a href="mailto:dev@antonsmirnov.name" target="_blank">dev@antonsmirnov.name</a>></span><br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"> Well, that sounds like it will not work because of JNI?<div><br></div><div>In few words, i'm trying to invoke indexing, parsing and tokenizing from java using libclang.</div>
<div><br></div><div><div>// create index</div>
<div>    native public static Index createIndex(boolean excludeDeclarationsFromPCH);</div><div><br></div><div>    // parse translation unit from file</div><div>    native public static TranslationUnit parseTranslationUnit(Index index, String filename, String[] commandLineArgs, UnsavedFile[] unsavedFiles);</div>

<div><br></div><div>    // tokenize</div><div>    native public static Token[] tokenize(TranslationUnit translationUnit, String filename, int filesize);</div><div><br></div><div>    // dispose translation unit</div><div>
    native public static void dispose(TranslationUnit translationUnit);</div>
<div><br></div><div>    // dispose index</div><div>    native public static void dispose(Index index);</div></div><div><br></div><div>for this methods there is native code with according methods and java nandles invocation from java code to native:</div>

<div><br></div><div><div>/*</div><div> * Class:     name_antonsmirnov_clang_clang_wrapper</div><div> * Method:    createIndex</div><div> * Signature: (Z)Lname/antonsmirnov/clang/dto/Index;</div><div> */</div><div>JNIEXPORT jobject JNICALL Java_name_antonsmirnov_clang_clang_1wrapper_createIndex</div>

<div>  (JNIEnv *env, jobject obj, jboolean excludeDeclarationsFromPCH)</div><div>  {</div></div><div>...</div><div>}</div><div><br></div><div><div>/*</div><div> * Class:     name_antonsmirnov_clang_clang_wrapper</div><div>

 * Method:    parseTranslationUnit</div><div> * Signature: (Lname/antonsmirnov/clang/dto/Index;Ljava/lang/String;[Lname/antonsmirnov/clang/dto/UnsavedFile;[Ljava/lang/String;)Lname/antonsmirnov/clang/dto/TranslationUnit;</div>

<div> */</div><div>JNIEXPORT jobject Java_name_antonsmirnov_clang_clang_1wrapper_parseTranslationUnit</div><div>  (JNIEnv *env, jobject obj, jobject jindex, jstring jfilename, jobjectArray commandLineArgs, jobjectArray unsavedFiles)</div>

<div>  {</div></div><div><br></div><div>...</div><div>}</div><div><br></div><div>so i map classes from java to native code and vice versa.</div><div>In native methods i invoke libclang-c methods.</div><div><br></div><div>

The problem is that CXIndex and CXTranslationUnit should be passed in the next methods so i have to keep them in memory in native code,</div><div>pass pointer to java as long and then return back with casting long as pointer.</div>

<div>I'm absolutely sure that pointers are packed/unpacked (long <--> pointer) correctly.</div><div><br></div><div>The problem is that if i do in the same invocation from java (f.e. in single Java_name_antonsmirnov_clang_clang_1wrapper_tokenize() function) - it works good.</div>

<div>For example if i just remember parameters and then create index, parse, tokenize (clang-c invocations) within tokenize() method it's okay.</div><div><br></div><div>If i do everything in separate native methods (clang-c invocation in separate native calls from java) (as it's designed to do) pointers are corrupted.</div>

<div>So unpacked CXIndex in parse() invocation is corrupted even if it was packed/unpacked correctly and the pointer is the same.</div><div><br></div><div>Any thoughts? Is is libclang problem, jni problem or i'm doing smth wrong?<br>

</div><div><br></div></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra"><br><br><div class="gmail_quote">2013/10/11 Renato Golin <span dir="ltr"><<a href="mailto:renato.golin@linaro.org" target="_blank">renato.golin@linaro.org</a>></span><br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>On 11 October 2013 08:02, Anton Smirnov <span dir="ltr"><<a href="mailto:dev@antonsmirnov.name" target="_blank">dev@antonsmirnov.name</a>></span> wrote:<br>

<div class="gmail_extra"><div class="gmail_quote">
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div>Are there any obvious limitations or anybody with such experience?</div></div></blockquote><div></div>


</div><br></div></div><div class="gmail_extra">Yes, JNI.</div><div class="gmail_extra"><br></div><div class="gmail_extra">JNI is a poor implementation of C code, trying to deal with the poor definitions of Java code, at the same time as trying to be 100% cross-platform. It fails on all three levels.</div>


<div class="gmail_extra"><br></div><div class="gmail_extra">Stack corruption in JNI is surprisingly common, especially on Windows (but also on Linux), and a few tricks must be used if you pass more than just a char pointer. On the top of my head I remember I had to define a large array on the stack, so that the stack pointer wouldn't segfault. </div>


<div class="gmail_extra"><br></div><div class="gmail_extra">JNI is also surprisingly poor in propagating errors from the C layer to the Java layer (if at all), so catching errors and debugging the interface is downright nightmare.</div>


<div class="gmail_extra"><br></div><div class="gmail_extra">You would hope that, after all these years, someone would come up with a fix, or a better infrastructure for inter-operating C code with Java...</div><div class="gmail_extra">


<br></div><div class="gmail_extra">This is a long shot, but have you tried using the VMKit and sharing C and Java code via LLVM's Execution Engine? I heard of a project that managed to do that with Python+C, including exception handling, so it should be possible to do that with Java.</div>


<div class="gmail_extra"><br></div><div class="gmail_extra">cheers,</div><div class="gmail_extra">--renato</div></div>
</blockquote></div><br></div>
</div></div></blockquote></div><br></div>