[cfe-dev] [PATCH] libclang: report error code for bad PCH files

Argyrios Kyrtzidis kyrtzidis at apple.com
Tue Feb 11 18:05:01 PST 2014


On Feb 11, 2014, at 9:41 AM, Dmitri Gribenko <gribozavr at gmail.com> wrote:

> Hello,
> 
> The attached patch improves libclang to report the error condition
> when CXTranslationUnit can not be created because of a stale PCH file.
> This allows the caller, for example, to rebuild the PCH file and
> retry the request.
> 
> There are APIs like clang_parseTranslationUnit(), which don't support
> reporting detailed errors (the only error condition is a NULL result).
> Because of this, in order to add this feature without breaking binary
> compatibility, I propose to add a new API,
> clang_TranslationUnit_hasFailure(), which checks if the TU is usable.
> Then other APIs will return a non-NULL TU that is marked as erroneous,
> and passing the erroneous TU to any API is a no-op.
> 
> My only concern is about clang_indexSourceFile function.  On error, it
> used to return NULL in out_TU.  We might have clients that assume this
> and don't dispose the returned TU if error is signaled.  Now the
> function can return an erroneous TU that should be disposed, otherwise
> it is leaked.
> 
> As an alternative, in order not to introduce a possible memory leak in
> clang_indexSourceFile, we could do the following.  Instead of
> allocating a block of memory to return as an erroneous TU, we could
> use a magic pointer value, or an address of a static constant
> "TheErroneousTU".
> 
> Argyrios, which way do you prefer?

This seems more complicated than it’s worth, particularly since we do add a new API that the caller must be aware of, anyway.

How about for the functions that don’t support detailed error codes we just introduce a new variant that does support error codes ?
Clients interested in the error codes can move to calling the new function (whether we should deprecate the existing one or not is a different question).


> 
> Dmitri
> 
> -- 
> main(i,j){for(i=2;;i++){for(j=2;j<i;j++){if(!(i%j)){j=0;break;}}if
> (j){printf("%d\n",i);}}} /*Dmitri Gribenko <gribozavr at gmail.com>*/
> <libclang-report-error-code-for-bad-pch-v1.patch>





More information about the cfe-dev mailing list