[Patch] Libclang add function clang_Type_isAnonymousStructOrUnion

Stephen Fewer stephen_fewer at harmonysecurity.com
Thu Apr 2 13:12:04 PDT 2015


Hi,

This is a small patch for libclang to see if a CXType is an anonymous
struct or union via RecordDecl isAnonymousStructOrUnion()

Thanks,

- Stephen.
-------------- next part --------------
Index: include/clang-c/Index.h
===================================================================
--- include/clang-c/Index.h	(revision 233794)
+++ include/clang-c/Index.h	(working copy)
@@ -32,7 +32,7 @@
  * compatible, thus CINDEX_VERSION_MAJOR is expected to remain stable.
  */
 #define CINDEX_VERSION_MAJOR 0
-#define CINDEX_VERSION_MINOR 29
+#define CINDEX_VERSION_MINOR 30
 
 #define CINDEX_VERSION_ENCODE(major, minor) ( \
       ((major) * 10000)                       \
@@ -3290,7 +3290,21 @@
   CXRefQualifier_RValue
 };
 
+enum CXAnonymousKind {
+	/** \brief Unable to determine if anonymous or not, type was not a struct or union. */
+	CXAnonymous_None = 0,
+	/** \brief Yes this type is an anonymous struct or union. */
+	CXAnonymous_Yes,
+	/** \brief No this type is not an anonymous struct or union. */
+	CXAnonymous_No
+};
+
 /**
+ * \brief Returns if this struct of union type is an anonymous declaration or not.
+ */
+CINDEX_LINKAGE enum CXAnonymousKind clang_Type_isAnonymousStructOrUnion(CXType T);
+
+/**
  * \brief Returns the number of template arguments for given class template
  * specialization, or -1 if type \c T is not a class template specialization.
  *
Index: tools/libclang/CXType.cpp
===================================================================
--- tools/libclang/CXType.cpp	(revision 233794)
+++ tools/libclang/CXType.cpp	(working copy)
@@ -819,6 +819,18 @@
   return CXTypeLayoutError_InvalidFieldName;
 }
 
+enum CXAnonymousKind clang_Type_isAnonymousStructOrUnion(CXType T) {
+	CXCursor PC = clang_getTypeDeclaration(T);
+	if (clang_isInvalid(PC.kind))
+		return CXAnonymousKind::CXAnonymous_None;
+	if (PC.kind != CXCursor_StructDecl && PC.kind != CXCursor_UnionDecl)
+		return CXAnonymousKind::CXAnonymous_None;
+	const RecordDecl * RD = dyn_cast_or_null<RecordDecl>(cxcursor::getCursorDecl(PC));
+	if (!RD || RD->isInvalidDecl())
+		return CXAnonymousKind::CXAnonymous_None;
+	return (RD->isAnonymousStructOrUnion() ? CXAnonymousKind::CXAnonymous_Yes : CXAnonymousKind::CXAnonymous_No);
+}
+
 enum CXRefQualifierKind clang_Type_getCXXRefQualifier(CXType T) {
   QualType QT = GetQualType(T);
   if (QT.isNull())


More information about the cfe-commits mailing list