On 09/05/2012 09:07 PM, Gregory Szorc wrote:
I like it! Thank you for doing this.

Specifically, I like how it defaults to strict and users must explicitly
choose to pull out a foot gun.

There's probably potential for hooking up clang_getClangVersion() to
make the compatibility check even stricter. I'm think that the Python
bindings would contain a minimal libclang version. This way, if function
semantics ever change between libclang releases (I know the API is
supposed to be backwards compatible, but you never know), we can detect
that. Don't let this hold up committing this patch though.

Thanks, committed in r163238.

Regarding using clang_getClangVersion(). The comment says:

 * \brief Return a version string, suitable for showing to a user, but
 *        not intended to be parsed (the format is not guaranteed to be
 *        stable).

So parsing it to check the version sounds dangerous? We may need to establish another way to check for the libclang version? Maybe using a function, that just gives a version number?

Another idea, I was playing with can be seen in the attached patch. I did not finish thinking about it, so I don't yet submit it as an official patch, but you may want to have a look.

Tobi
>From 43c92297bc06423f77015d2dc1ee586b543124c1 Mon Sep 17 00:00:00 2001
From: Tobias Grosser <[email protected]>
Date: Fri, 31 Aug 2012 14:10:22 +0200
Subject: [PATCH] Report function when something is failing

---
 bindings/python/clang/cindex.py |   59 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 59 insertions(+)

diff --git a/bindings/python/clang/cindex.py b/bindings/python/clang/cindex.py
index d265f7e..e4c69cf 100644
--- a/bindings/python/clang/cindex.py
+++ b/bindings/python/clang/cindex.py
@@ -2946,6 +2946,64 @@ functionList = [
    c_uint),
 ]
 
+AddedInVersion = {
+  "clang_CompilationDatabase_dispose" : "3.2",
+  "clang_CompilationDatabase_fromDirectory" : "3.2",
+  "clang_CompilationDatabase_getCompileCommands" : "3.2",
+  "clang_CompileCommands_dispose" : "3.2",
+  "clang_CompileCommands_getCommand" : "3.2",
+  "clang_CompileCommands_getSize" : "3.2",
+  "clang_CompileCommand_getArg" : "3.2",
+  "clang_CompileCommand_getDirectory" : "3.2",
+  "clang_CompileCommand_getNumArgs" : "3.2",
+  "clang_getArgType" : "3.1",
+  "clang_getElementType" : "3.1",
+  "clang_getEnumConstantDeclUnsignedValue" : "3.1",
+  "clang_getEnumConstantDeclValue" : "3.1",
+  "clang_getEnumDeclIntegerType" : "3.1",
+  "clang_getNumArgTypes" : "3.1",
+  "clang_getNumElements" : "3.1",
+  "clang_getTypedefDeclUnderlyingType" : "3.1",
+  "clang_isFunctionTypeVariadic" : "3.1",
+  "clang_CXXMethod_isVirtual" : "3.0",
+  "clang_equalRanges" : "3.0",
+  "clang_getArrayElementType" : "3.0",
+  "clang_getArraySize" : "3.0",
+  "clang_getCursorReferenceNameRange" : "3.0",
+  "clang_getTUResourceUsageName" : "3.0",
+  "clang_isAttribute" : "3.0",
+  "clang_isFileMultipleIncludeGuarded" : "3.0",
+  "clang_getCanonicalCursor" : "2.9",
+  "clang_getCursorDisplayName" : "2.9",
+  "clang_getCursorLexicalParent" : "2.9",
+  "clang_getCursorSemanticParent" : "2.9",
+  "clang_getDeclObjCTypeEncoding" : "2.9",
+  "clang_getDiagnosticCategory" : "2.9",
+  "clang_getDiagnosticCategoryName" : "2.9",
+  "clang_getDiagnosticOption" : "2.9",
+  "clang_getIncludedFile" : "2.9",
+  "clang_getLocationForOffset" : "2.9",
+  "clang_getNumOverloadedDecls" : "2.9",
+  "clang_getOverloadedDecl" : "2.9",
+  "clang_hashCursor" : "2.9",
+  "clang_isConstQualifiedType" : "2.9",
+  "clang_isRestrictQualifiedType" : "2.9",
+  "clang_isVolatileQualifiedType" : "2.9",
+}
+
+class FunctionMissingError(Exception):
+    def __init__(self, name):
+        self.name = name
+
+    def __str__(self):
+        return "Function '" + self.name + "' not available in this version of "\
+               "libclang. The function was added with clang " + \
+               str(AddedInVersion[self.name])
+
+    def __call__(self, *arg):
+        raise self
+
+
 class LibclangError(Exception):
     def __init__(self, message):
         self.m = message
@@ -2962,6 +3020,7 @@ def register_function(lib, item, ignore_errors):
         msg = str(e) + ". Please ensure that your python bindings are "\
                        "compatible with your libclang.so version."
         if ignore_errors:
+            setattr(lib, item[0], FunctionMissingError(item[0]))
             return
         raise LibclangError(msg)
 
-- 
1.7.9.5

_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

Reply via email to