<div dir="ltr"><div>Hello cfe-dev.</div><div><br></div><div>I am trying to perform additional checks on the alignment of C++ variables (outputting diagnostic messages if the checks trigger), and the place where it feels more natural to do so is in Sema::ActOnVariableDeclarator. There is already a check for under-alignment there (a call to Sema::CheckAlignasUnderalignment) so I thought I was on the right track.</div>
<div>However I am finding that my checks don't trigger when the variable being declared/defined is a template instantiation.</div><div><br></div><div>Suppose for the sake of example that I wanted to add a check that warns if the alignment exceeds a certain number of CharUnits, say 32. If I put the check in ActOnVariableDeclarator right after the under-alignment check, it does catch this case:</div>
<div><br></div><div>struct __attribute__(( aligned(64) )) my_aligned { int x; }</div><div>// ...</div><div>my_aligned my_variable; // Diagnostic here</div><div><br></div><div>However it does not catch this case:</div><div>
<br></div><div>template <typename T> class __attribute__(( aligned(64) )) my_aligned_template {</div><div>    int x;</div><div>    void foo (T param) { /* some code */ }</div><div>}</div><div>// ...</div><div>my_aligned_template<float> my_variable; // No diagnostic here, but the IR has the right alignment (64)</div>
<div><br></div><div>I am using ASTContext::getDeclAlign to get the alignment of the variable in the declaration, as the documentation says that it returns a conservative alignment for the variable, and it's actually the same function used at CodeGen time to determine the alignment.</div>
<div>However I seem to understand that at that point (when ActOnVariableDeclarator runs) the template instantiation hasn't been processed yet and therefore the type of the variable is still a dependent type (despite the fact that the layout of my_aligned_template does not in fact depend on T, but I accept that in the general case it can do). This means that the alignment is unknown at that stage and I can't really check it.</div>
<div>In fact, I have noticed that Sema::CheckAlignasUnderalignment (which is my reference for implementing the checks) also skips dependent and incomplete types.</div><div><br></div><div>Of course when ASTContext::getDeclAlign is used later on in CodeGen everything is known about variables and their types, so I could move my checks there, but it would feel wrong to do the checks while IR is being generated, I think Sema should be the right place to perform semantic checks.</div>
<div><br></div><div>Can anyone point me in the right direction? Is there a more sensible place than ActOnVariableDeclarator where I can put my additional alignment checks?</div><div><br></div><div>Thanks,</div><div>    Dario Domizioli</div>
<div>    SN Systems - Sony Computer Entertainment Group</div></div>