<div dir="ltr"><div>Hi Eli and Aaron,</div><div><br></div><div>Thank you for your help! This makes a lot of sense, and thank you for citing the standard :). For pointer types I used dyn_cast (and so far it's been working well) and so in my opinion the getAsArrayType() function seems a bit of an unintuitive edge case (but I'm sure there were justifiable reasons for it). For anyone who might run into this issue in the future, if you perform dyn_cast instead of getAsArrayType(), the qualifiers on the elements will be dropped.</div><div><br></div><div>Thanks!</div><div>Ray<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Oct 9, 2020 at 1:02 PM Eli Friedman <<a href="mailto:efriedma@quicinc.com">efriedma@quicinc.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">





<div lang="EN-US">
<div class="gmail-m_-7703568366389173814WordSection1">
<p class="MsoNormal">The reason it’s represented this way is to preserve the source as written.  Suppose you have something like the following:<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">typedef int a[10];<u></u><u></u></p>
<p class="MsoNormal">typedef const a b;<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">We want to preserve the TypedefType, so the const has to stay outside it.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">Because of this, you usually want to use ASTContext:: getAsArrayType(), not dyn_cast<ArrayType>(MyQualType).  The ASTContext method will rewrite the element type so it has the right qualifiers.<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<p class="MsoNormal">-Eli<u></u><u></u></p>
<p class="MsoNormal"><u></u> <u></u></p>
<div style="border-color:rgb(225,225,225) currentcolor currentcolor;border-style:solid none none;border-width:1pt medium medium;padding:3pt 0in 0in">
<p class="MsoNormal"><b>From:</b> cfe-dev <<a href="mailto:cfe-dev-bounces@lists.llvm.org" target="_blank">cfe-dev-bounces@lists.llvm.org</a>> <b>On Behalf Of
</b>Ray Zhang via cfe-dev<br>
<b>Sent:</b> Friday, October 9, 2020 1:23 AM<br>
<b>To:</b> Clang Dev <<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>><br>
<b>Subject:</b> [EXT] [cfe-dev] Why qualifiers for array elements on the array itself?<u></u><u></u></p>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<div>
<p class="MsoNormal">Hi,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">I'm currently working on a clang cast tool and ran into a small oddity. This might be a stupid mistake but I'd love some pointers(no pun intended) or insight into it:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">const int arr[2] {1,2};<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">should be "an array of fixed size 2 of constant integers". The QualType associated with arr is an array type, and we can observe that with Expr->isArrayType(). I checked the qualifiers and found that the qualifier for const is actually
 on this level, and not for the individual elements of ints. I found the elements inside of the array with the following:<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">const ArrayType* ArrType = dyn_cast<ArrayType>(MyQualType);<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">ArrType->getElementType();<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">which I was hoping could give me the const qualifier for the integers. If it helps at all, I'm using the getLocalFastQualifiers() function to return an unsigned int, and I'm seeing if 0x1 is masked for constness.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Since arrays can't really have qualifiers(the only example I see is on the<a href="https://en.cppreference.com/w/c/language/array" target="_blank"> standards</a> for function parameters for C99 and not C++), was there a reason for why clang decided to
 put the qualifiers on the array qualtype rather than the elements' qualtype?<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">If I made any unreasonable assumptions to reach this conclusion, please let me know :)<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<div>
<p class="MsoNormal">Best,<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal">Ray<u></u><u></u></p>
</div>
</div>
</div>
</div>

</blockquote></div>