Skip to content
Snippets Groups Projects
llvm9-0002-libclang-Add-support-for-obtaining-fully-qualified-n.patch 5.97 KiB
Newer Older
From e673a5527dd2df322884eb2498736483df05957d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Johann=20Kl=C3=A4hn?= <johann@jklaehn.de>
Date: Fri, 3 Nov 2017 11:17:59 +0100
Subject: [PATCH 2/5] [libclang] Add support for obtaining fully qualified
 names of types

This patch allows retrieving the fully qualified names of types
through libclang and clang.cindex (Python).
---
 clang/bindings/python/clang/cindex.py         | 13 +++++++++++
 .../python/tests/cindex/test_cursor.py        |  8 +++++++
 clang/include/clang-c/Index.h                 | 10 ++++++++-
 clang/tools/libclang/CMakeLists.txt           |  1 +
 clang/tools/libclang/CXType.cpp               | 22 +++++++++++++++++++
 clang/tools/libclang/libclang.exports         |  1 +
 6 files changed, 54 insertions(+), 1 deletion(-)

diff --git a/tools/clang/bindings/python/clang/cindex.py b/tools/clang/bindings/python/clang/cindex.py
index 8e5a9fe0068..c309f7017b2 100644
--- a/tools/clang/bindings/python/clang/cindex.py
+++ b/tools/clang/bindings/python/clang/cindex.py
@@ -2427,6 +2427,14 @@ class Type(Structure):
         """Retrieve the spelling of this Type."""
         return conf.lib.clang_getTypeSpelling(self)
 
+    @property
+    def fully_qualified_name(self):
+        """Retrieve the fully qualified name of this Type."""
+        if not hasattr(self, '_fully_qualified_name'):
+            self._fully_qualified_name = conf.lib.clang_getFullyQualifiedTypeName(self)
+
+        return self._fully_qualified_name
+
     def __eq__(self, other):
         if type(other) != type(self):
             return False
@@ -3869,6 +3877,11 @@ functionList = [
    _CXString,
    _CXString.from_result),
 
+  ("clang_getFullyQualifiedTypeName",
+   [Type],
+   _CXString,
+   _CXString.from_result),
+
   ("clang_hashCursor",
    [Cursor],
    c_uint),
diff --git a/tools/clang/bindings/python/tests/cindex/test_cursor.py b/tools/clang/bindings/python/tests/cindex/test_cursor.py
index ef875e97247..6a53c7205df 100644
--- a/tools/clang/bindings/python/tests/cindex/test_cursor.py
+++ b/tools/clang/bindings/python/tests/cindex/test_cursor.py
@@ -316,6 +316,14 @@ class TestCursor(unittest.TestCase):
         underlying = typedef.underlying_typedef_type
         self.assertEqual(underlying.kind, TypeKind.INT)
 
+    def test_fully_qualified_type_name():
+        source = 'namespace uiae { struct X { typedef int sometype; }; }'
+        tu = get_tu(source, lang='cpp')
+
+        cls = get_cursor(tu, 'sometype')
+        fqn = cls.type.fully_qualified_name
+        self.assertTrue(fqn.endswith("uiae::X::sometype"), fqn)
+
     def test_semantic_parent(self):
         tu = get_tu(kParentTest, 'cpp')
         curs = get_cursors(tu, 'f')
diff --git a/tools/clang/include/clang-c/Index.h b/tools/clang/include/clang-c/Index.h
index 74badac740b..b0c62fe948e 100644
--- a/tools/clang/include/clang-c/Index.h
+++ b/tools/clang/include/clang-c/Index.h
@@ -32,7 +32,7 @@
  * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
  */
 #define CINDEX_VERSION_MAJOR 0
-#define CINDEX_VERSION_MINOR 59
+#define CINDEX_VERSION_MINOR 60
 
 #define CINDEX_VERSION_ENCODE(major, minor) ( \
       ((major) * 10000)                       \
@@ -3389,6 +3389,14 @@ CINDEX_LINKAGE CXType clang_getCursorType(CXCursor C);
  */
 CINDEX_LINKAGE CXString clang_getTypeSpelling(CXType CT);
 
+/**
+ * Retrieve the fully qualified name of the underlying type.
+ * This includes full qualification of all template parameters etc.
+ *
+ * If the type is invalid, an empty string is returned.
+ */
+CINDEX_LINKAGE CXString clang_getFullyQualifiedTypeName(CXType CT);
+
 /**
  * Retrieve the underlying type of a typedef declaration.
  *
diff --git a/tools/clang/tools/libclang/CMakeLists.txt b/tools/clang/tools/libclang/CMakeLists.txt
index 613ead1a36b..a583fa206d1 100644
--- a/tools/clang/tools/libclang/CMakeLists.txt
+++ b/tools/clang/tools/libclang/CMakeLists.txt
@@ -43,6 +43,7 @@ set(LIBS
   clangSema
   clangSerialization
   clangTooling
+  clangToolingCore
 )
 
 if (CLANG_ENABLE_ARCMT)
diff --git a/tools/clang/tools/libclang/CXType.cpp b/tools/clang/tools/libclang/CXType.cpp
index acecf87d0cd..afdeb467769 100644
--- a/tools/clang/tools/libclang/CXType.cpp
+++ b/tools/clang/tools/libclang/CXType.cpp
@@ -19,6 +19,7 @@
 #include "clang/AST/DeclObjC.h"
 #include "clang/AST/DeclTemplate.h"
 #include "clang/AST/Expr.h"
+#include "clang/AST/QualTypeNames.h"
 #include "clang/AST/Type.h"
 #include "clang/Basic/AddressSpaces.h"
 #include "clang/Frontend/ASTUnit.h"
@@ -302,6 +303,27 @@ CXString clang_getTypeSpelling(CXType CT) {
   return cxstring::createDup(OS.str());
 }
 
+CXString clang_getFullyQualifiedTypeName(CXType CT) {
+  QualType T = GetQualType(CT);
+  if (T.isNull())
+    return cxstring::createEmpty();
+
+  // For builtin types (but not typedefs pointing to builtin types) return their
+  // spelling.  Otherwise "bool" will be turned into "_Bool".
+  const Type *TP = T.getTypePtrOrNull();
+  if (TP && TP->isBuiltinType() && T->getAs<TypedefType>() == nullptr)
+    return clang_getTypeSpelling(CT);
+
+  CXTranslationUnit TU = GetTU(CT);
+  ASTContext &Ctx = cxtu::getASTUnit(TU)->getASTContext();
+  PrintingPolicy Policy(Ctx.getPrintingPolicy());
+  Policy.SuppressScope = false;
+  Policy.AnonymousTagLocations = false;
+  Policy.PolishForDeclaration = true;
+  std::string name = TypeName::getFullyQualifiedName(T, Ctx, Policy, /*WithGlobalNsPrefix=*/true);
+  return cxstring::createDup(name.c_str());
+}
+
 CXType clang_getTypedefDeclUnderlyingType(CXCursor C) {
   using namespace cxcursor;
   CXTranslationUnit TU = cxcursor::getCursorTU(C);
diff --git a/tools/clang/tools/libclang/libclang.exports b/tools/clang/tools/libclang/libclang.exports
index 3c76090d64f..6e860e7263e 100644
--- a/tools/clang/tools/libclang/libclang.exports
+++ b/tools/clang/tools/libclang/libclang.exports
@@ -241,6 +241,7 @@ clang_getFileLocation
 clang_getFileName
 clang_getFileTime
 clang_getFileUniqueID
+clang_getFullyQualifiedTypeName
 clang_getFunctionTypeCallingConv
 clang_getIBOutletCollectionType
 clang_getIncludedFile
-- 
2.23.0