[cfe-commits] libclang: Patch to expose array size and element type

Argyrios Kyrtzidis kyrtzidis at apple.com
Tue Sep 27 10:46:42 PDT 2011


In r140614, thanks!

On Sep 23, 2011, at 5:03 AM, Vinay Sajip wrote:

> I've attached a revised patch which takes your comments into account.
> 
> Regards,
> 
> Vinay Sajip
> 
> From: Argyrios Kyrtzidis <kyrtzidis at apple.com>
> To: Vinay Sajip <vinay_sajip at yahoo.co.uk>
> Cc: "cfe-commits at cs.uiuc.edu" <cfe-commits at cs.uiuc.edu>
> Sent: Wednesday, 21 September 2011, 2:03
> Subject: Re: [cfe-commits] libclang: Patch to expose array size and element type
> 
>> diff --git a/include/clang-c/Index.h b/include/clang-c/Index.h
>> index 07bcf9e..3257664 100644
>> --- a/include/clang-c/Index.h
>> +++ b/include/clang-c/Index.h
>> @@ -1955,7 +1955,8 @@ enum CXTypeKind {
>>    CXType_ObjCInterface = 108,
>>    CXType_ObjCObjectPointer = 109,
>>    CXType_FunctionNoProto = 110,
>> -  CXType_FunctionProto = 111
>> +  CXType_FunctionProto = 111,
>> +  CXType_ConstantArray = 112
>>  };
>>  
>>  /**
>> @@ -2047,6 +2048,20 @@ CINDEX_LINKAGE CXType clang_getCursorResultType(CXCursor C);
>>  CINDEX_LINKAGE unsigned clang_isPODType(CXType T);
>>  
>>  /**
>> + * \brief Return the element type of an array type.
>> + *
>> + * If a non-array type is passed in, an invalid type is returned.
>> + */
>> +CINDEX_LINKAGE CXType clang_getArrayElementType(CXType T);
>> +
>> +/**
>> + * \brief Return the the array size of a constant array.
>> + *
>> + * If a non-array type is passed in, -1 is returned.
>> + */
>> +CINDEX_LINKAGE int clang_getArraySize(CXType T);
>> +
>> +/**
>>   * \brief Returns 1 if the base class specified by the cursor with kind
>>   *   CX_CXXBaseSpecifier is virtual.
>>   */
>> diff --git a/tools/libclang/CXType.cpp b/tools/libclang/CXType.cpp
>> index 45c7346..2a20298 100644
>> --- a/tools/libclang/CXType.cpp
>> +++ b/tools/libclang/CXType.cpp
>> @@ -84,6 +84,7 @@ static CXTypeKind GetTypeKind(QualType T) {
>>      TKCASE(ObjCObjectPointer);
>>      TKCASE(FunctionNoProto);
>>      TKCASE(FunctionProto);
>> +    TKCASE(ConstantArray);
>>      default:
>>        return CXType_Unexposed;
>>    }
>> @@ -330,6 +331,7 @@ CXString clang_getTypeKindSpelling(enum CXTypeKind K) {
>>      TKIND(ObjCObjectPointer);
>>      TKIND(FunctionNoProto);
>>      TKIND(FunctionProto);
>> +    TKIND(ConstantArray);
>>    }
>>  #undef TKIND
>>    return cxstring::createCXString(s);
>> @@ -373,6 +375,46 @@ unsigned clang_isPODType(CXType X) {
>>    return T.isPODType(AU->getASTContext()) ? 1 : 0;
>>  }
>>  
>> +CXType clang_getArrayElementType(CXType X)
> 
> Nitpick but please use 'CXType CT' or 'T' as parameter name. I see that clang_isPODType also has 'X' but let's just ignore it :-)
> 
>> +{
>> +	CXTranslationUnit TU = GetTU(X);
>> +	QualType ET = QualType();
>> +	QualType T = GetQualType(X);
>> +	const Type *TP = T.getTypePtrOrNull();
>> +
>> +	if (TP) {
>> +		switch (TP->getTypeClass()) {
>> +		case Type::ConstantArray:
>> +			ET = cast<ConstantArrayType>(TP)->getElementType();
>> +			break;
>> +		default:
>> +			break;
>> +		}
>> +	}
>> +	return MakeCXType(ET, TU);
>> +}
>> +
>> +int clang_getArraySize(CXType X)
> 
> Better to have it return 'long long' I think.
> 
>> +{
>> +	int result = -1;
>> +	QualType T = GetQualType(X);
>> +	const Type *TP = T.getTypePtrOrNull();
>> +
>> +	if (TP) {
>> +		switch (TP->getTypeClass()) {
>> +		case Type::ConstantArray:
>> +			{
>> +				llvm::APInt size = cast<ConstantArrayType>(TP)->getSize();
>> +				result = size.getSExtValue();
>> +			}
>> +			break;
>> +		default:
>> +			break;
>> +		}
>> +	}
>> +	return result;
>> +}
> 
> LLVM conventions are no tabs, 2 spaces for indentation, and '{' on the same line as the case statement and the function declaration. This applies to all your patches.
> 
> Could you also add a test at 'test/Index/print-typekind.c' ?
> 
> -Argyrios
> 
>> +
>>  CXString clang_getDeclObjCTypeEncoding(CXCursor C) {
>>    if ((C.kind < CXCursor_FirstDecl) || (C.kind > CXCursor_LastDecl))
>>      return cxstring::createCXString("");
>> diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports
>> index ea7aaf0..cc6c326 100644
>> --- a/tools/libclang/libclang.exports
>> +++ b/tools/libclang/libclang.exports
>> @@ -42,6 +42,8 @@ clang_equalRanges
>>  clang_equalTypes
>>  clang_executeOnThread
>>  clang_formatDiagnostic
>> +clang_getArrayElementType
>> +clang_getArraySize
>>  clang_getCString
>>  clang_getCXTUResourceUsage
>>  clang_getCXXAccessSpecifier
> 
> 
> 
> 
> On Sep 15, 2011, at 5:38 AM, Vinay Sajip wrote:
> 
>> I've attached a patch which exposes array size and element type via the libclang API. At the moment, only constant arrays are handled.
>> 
>> 
>> Comments welcome.
>> 
>> Regards,
>> 
>> Vinay Sajip
>> <const-arrays.diff>_______________________________________________
>> cfe-commits mailing list
>> cfe-commits at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits
> 
> 
> 
> <arrays.diff>_______________________________________________
> cfe-commits mailing list
> cfe-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20110927/ba884665/attachment.html>


More information about the cfe-commits mailing list