[cfe-dev] Determining whether to print references for array type

David Rector via cfe-dev cfe-dev at lists.llvm.org
Wed Apr 21 10:25:09 PDT 2021


I would directly use John McCall’s suggested code in the review (`getPointerDepth(QualType type)` etc) to simply compare pointer depths of the arg and param types, instead of comparing the results of `getInnermostPointerType(…)` — not sure what that function is doing, particularly given it uses getPointeeOrArrayElementType to look through pointers, which seems to defeat the purpose of the comparison (to print arrays different from pointers).  

> On Apr 20, 2021, at 2:33 PM, Pratyush Das via cfe-dev <cfe-dev at lists.llvm.org> wrote:
> 
> Hi,
> 
> My question is motivated by https://reviews.llvm.org/D36368 <https://reviews.llvm.org/D36368> (current WIP diff - https://github.com/llvm/llvm-project/compare/main...reikdas:array-axel <https://github.com/llvm/llvm-project/compare/main...reikdas:array-axel>).
> 
> For the following code snippet - 
> 
> constexpr const char **kZero[] = {};
> 
> // Let's call this (1)
> template <const char *const *const *, typename T> class Jolumn {};
> Jolumn<kZero, double>();
> 
> // Let's call this (2)
> template <const char** const (*)[0], typename T> class Iolumn {};
> Iolumn<&kZero, double>();
> 
> I want to be able to detect if an ampersand should be printed for (1) and (2) (it shouldn't be for (1) and should be for (2)).
> 
> Looking at the type dumps,
> the parameter type(the first parameter type for both class Jolumn and class Iolumn) is represented as - 
> ConstantArrayType 0x560d4eab8000 'const char **const [0]' 0 
> `-QualType 0x560d4eab7de1 'const char **const' const
>   `-PointerType 0x560d4eab7de0 'const char **'
>     `-PointerType 0x560d4ea72930 'const char *'
>       `-QualType 0x560d4ea71e61 'const char' const
>         `-BuiltinType 0x560d4ea71e60 'char'

In my local clang the above is the type of only the Iolumn parameter; the Jolumn parameter is 
PointerType 0x7ff4ed061b30 'const char *const *const *'
`-…etc

If those types are indeed the same in yours, that is a problem, so double check those types if it still doesn’t work.

> 
> the argument type for kZero in (1) is represented as -
> PointerType 0x5560fe683130 'const char *const *const *'
> `-QualType 0x5560fe683101 'const char *const *const' const
>   `-PointerType 0x5560fe683100 'const char *const *'
>     `-QualType 0x5560fe63d951 'const char *const' const
>       `-PointerType 0x5560fe63d950 'const char *'
>         `-QualType 0x5560fe63ce81 'const char' const
>           `-BuiltinType 0x5560fe63ce80 'char'
> 
> the argument type for &kZero in (2) is represented as -
> PointerType 0x560d4eab8100 'const char **const (*)[0]'
> `-QualType 0x560d4eab7fc1 'const char **const[0]' const
>   `-ConstantArrayType 0x560d4eab7fc0 'const char **[0]' 0 
>     `-PointerType 0x560d4eab7de0 'const char **'
>       `-PointerType 0x560d4ea72930 'const char *'
>         `-QualType 0x560d4ea71e61 'const char' const
>           `-BuiltinType 0x560d4ea71e60 'char'
> 
> From the clang::Type, how do I detect that an ampersand should not be printed for the argument kZero (1), but should be printed for the argument &kZero (2)?
> 
> Comparing the pointer depths of the two as suggested in the review or comparing the innermost clang::PointerType give the same result for both the argument types.
> I also cycled through the relevant boolean methods in https://clang.llvm.org/doxygen/classclang_1_1Type.html <https://clang.llvm.org/doxygen/classclang_1_1Type.html>, but the methods gave the same results for both the argument types.
> 
> I would really appreciate any help. 
> 
> Thanks!
> 
> 
> -- 
> Pratyush Das
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20210421/58259eb7/attachment.html>


More information about the cfe-dev mailing list