On Mon, Mar 18, 2013 at 6:18 PM, Jordan Rose <span dir="ltr"><<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word"><div><div class="h5"><br><div><div>On Mar 18, 2013, at 9:15 , Sebastian Hagedorn <<a href="mailto:hagi.dd@web.de" target="_blank">hagi.dd@web.de</a>> wrote:</div><br><blockquote type="cite">
<div style="word-wrap:break-word"><div>On 15/03/2013, at 17:11 , Jordan Rose <<a href="mailto:jordan_rose@apple.com" target="_blank">jordan_rose@apple.com</a>> wrote:</div><div><br><blockquote type="cite"><div style="word-wrap:break-word">
<br><div><div>On Mar 15, 2013, at 7:27 , Sebastian Hagedorn <<a href="mailto:hagi.dd@web.de" target="_blank">hagi.dd@web.de</a>> wrote:</div><br><blockquote type="cite"><div style="word-wrap:break-word">I'm currently working on a Clang modification that analyses the (iOS SDK) version in which certain symbols were introduced. Most of it is based on code that looks similar to this:<div>
<br></div><div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(195,89,0)"><span>decl-></span><span style="color:#587ea8">getAttr</span><span><</span>AvailabilityAttr<span>>()</span><span>-></span><span style="color:rgb(88,126,168)">getIntroduced</span><span>()</span><span>;</span></div>
</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(195,89,0)"><span><br></span></div><div style="margin:0px">So far, this works fine for ObjC methods, properties, classes, and C functions. One missing piece are enumerations, but I've found that they seem to lack availability information alltogether. Let's pick an example, I'm trying to use the <span style="font-family:Menlo;font-size:11px">PKPassKitErrorCode</span> enum that was introduced in iOS 6. The framework header declares the following:</div>
<div style="margin:0px"><br></div><div style="margin:0px"><div style="margin:0px;font-size:11px;font-family:Menlo"><span style="color:#35568a">typedef</span> <span style="color:#777997">NS_ENUM</span>(NSInteger, PKPassKitErrorCode) {</div>
<div style="margin:0px;font-size:11px;font-family:Menlo">    PKUnknownError = -<span style="color:#35568a">1</span>,</div><div style="margin:0px;font-size:11px;font-family:Menlo">    PKInvalidDataError = <span style="color:#35568a">1</span>,</div>
<div style="margin:0px;font-size:11px;font-family:Menlo">    PKUnsupportedVersionError,</div><div style="margin:0px;font-size:11px;font-family:Menlo">    PKInvalidSignature,</div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(119,121,151)">
<span>} </span>NS_ENUM_AVAILABLE_IOS<span>(</span><span style="color:#35568a">6</span><span>_0);</span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(119,121,151)"><span><br></span></div><div style="margin:0px;font-size:11px;font-family:Menlo;color:rgb(119,121,151)">
<span style="font-family:Helvetica;font-size:medium">Obviously, availability information is provided using the usual macros. This is what the preprocessor generates:</span></div></div><div style="margin:0px"><br></div><div style="margin:0px">
<div style="margin:0px;font-size:11px;font-family:Menlo">typedef enum PKPassKitErrorCode : NSInteger PKPassKitErrorCode; enum PKPassKitErrorCode : NSInteger {</div><div style="margin:0px;font-size:11px;font-family:Menlo">
    PKUnknownError = -<span style="color:#35568a">1</span>,</div><div style="margin:0px;font-size:11px;font-family:Menlo">    PKInvalidDataError = <span style="color:#35568a">1</span>,</div><div style="margin:0px;font-size:11px;font-family:Menlo">
    PKUnsupportedVersionError,</div><div style="margin:0px;font-size:11px;font-family:Menlo">    PKInvalidSignature,</div><div style="margin:0px;font-size:11px;font-family:Menlo">} __attribute__((availability(ios,introduced=<span style="color:#35568a">6.0</span>)));</div>
<div style="margin:0px;font-size:11px;font-family:Menlo"><br></div><div style="margin:0px">Still looks ok to me, all the information I'm looking for is included. However, when I try to get the availability information during the compilation process (I'm modifying <span style="font-family:Menlo;font-size:11px;color:rgb(195,89,0)">Sema</span><span style="font-family:Menlo;font-size:11px">::DiagnoseUseOfDecl()</span> in SemaExpr.cpp), there is none – <span style="font-family:Menlo;font-size:11px;color:rgb(88,126,168)">getAttr</span><span style="font-family:Menlo;font-size:11px"><</span><span style="color:rgb(195,89,0);font-family:Menlo;font-size:11px">AvailabilityAttr</span><span style="font-family:Menlo;font-size:11px">>()</span> returns NULL for EnumConstantDecls.</div>
<div style="margin:0px"><br></div><div style="margin:0px">I have not started examining the parsing of the declaration, but wanted to ask first whether there's an obvious reason why the information is missing, or there's an obvious mistake/misassumption I'm making?</div>
</div></div></blockquote><br></div><div>The attribute here isn't <i>on</i> the EnumConstantDecls; it's on the EnumDecl as a whole. You'll also have to be careful about redeclarations; in this case I would guess that the first EnumDecl (inside the typedef) doesn't have the attribute, but the second one (where the enumerators are actually defined) does.</div>
<div><br></div><div>Jordan</div><br></div></blockquote></div><br><div>Thanks for your response!</div><div><br></div><div>Re:EnumConstantDecl vs. EnumDecl... my bad. I actually tested both though, and neither the constants nor the declaration have availability information.</div>
<div><br></div><div>Re:Redeclarations: How is this handled in Clang? After looking at the EnumDecl class and the TagDecl class specifically, I assumed Clang keeps all the (re)declarations it finds and makes them accessible via iterators. So I tried to acces the most recent declaration (which should be the one with the constants defined), but also go through all previous declarations and always test whether there's an availability attribute. Looks like this...</div>
<div><br></div><div><div style="margin:0px;font-size:11px;font-family:Menlo"><span style="color:#c35900">EnumDecl</span> *enumD = <span style="color:#587ea8">dyn_cast_or_null</span><<span style="color:#c35900">EnumDecl</span>>(decl);</div>
<div style="margin:0px;font-size:11px;font-family:Menlo"><div style="margin:0px;color:rgb(53,86,138)">bool<span> hit = </span>false<span>;</span></div></div><div style="margin:0px;font-size:11px;font-family:Menlo"><span style="color:rgb(53,86,138)">if</span> (enumD) {</div>
<div style="margin:0px;font-size:11px;font-family:Menlo">    <span style="color:rgb(195,89,0)">EnumDecl</span> *prev = enumD-><span style="color:rgb(88,126,168)">getMostRecentDecl</span>();</div><div style="margin:0px;font-size:11px;font-family:Menlo">
    <span style="color:#35568a">while</span> (prev) {</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo">      <span style="color:#35568a">if</span> (prev-><span style="color:#587ea8">getAttr</span><<span style="color:#c35900">AvailabilityAttr</span>>()) {</div>
<div style="margin:0px;font-size:11px;font-family:Menlo">        hit = <span style="color:rgb(53,86,138)">true</span>;</div><div style="margin:0px;font-size:11px;font-family:Menlo">      }</div><div style="margin:0px;font-size:11px;font-family:Menlo;min-height:13px">
<br></div><div style="margin:0px;font-size:11px;font-family:Menlo">      prev = prev-><span style="color:#587ea8">getPreviousDecl</span>();</div><div style="margin:0px;font-size:11px;font-family:Menlo">    }</div><div style="margin:0px;font-size:11px;font-family:Menlo">
}</div></div><div style="margin:0px;font-size:11px;font-family:Menlo"><br></div><div style="margin:0px;font-size:11px;font-family:Menlo"><span style="font-family:Helvetica;font-size:medium">Unfortunately, </span>prev-><span style="color:rgb(88,126,168)">getPreviousDecl</span>()<span style="font-family:Helvetica;font-size:medium"> always returns NULL, so the while loop executes only once. And it does not find availability information for the most recent declaration.</span></div>
<div><br></div><div>Is there another way to check all (re)declarations?</div><div><br></div></div></blockquote></div><br></div></div><div>Hm, that looks correct to me. It does seem strange to me that the forward-declared enum does not get its own declaration, but even then I'm not sure it would matter.</div>
</div></blockquote><div><br></div><div>When built in C++11 mode, we get this, which looks much more reasonable:</div><div><br></div><div><div>TranslationUnitDecl 0x5e9b0d0 <<invalid sloc>></div><div>|-TypedefDecl 0x5e9b610 <<invalid sloc>> __int128_t '__int128'</div>
<div>|-TypedefDecl 0x5e9b670 <<invalid sloc>> __uint128_t 'unsigned __int128'</div><div>|-TypedefDecl 0x5e9ba30 <<invalid sloc>> __builtin_va_list '__va_list_tag [1]'</div><div>|-TypedefDecl 0x5e9ba90 <tmp.c:1:1, col:14> NSInteger 'long'</div>
<div>|-<b>EnumDecl 0x5e9bb10 <line:2:9, col:14> PKPassKitErrorCode 'NSInteger':'long'</b></div><div>|-TypedefDecl 0x5e9bc10 <col:1, col:45> PKPassKitErrorCode 'enum PKPassKitErrorCode':'enum PKPassKitErrorCode'</div>
<div>`-<b>EnumDecl 0x5e9bca0 prev 0x5e9bb10 <col:65, line:7:1> PKPassKitErrorCode 'NSInteger':'long'</b></div><div>  |-<b>AvailabilityAttr 0x5ecea50 <col:18, col:49> ios 6.0 0 0 ""</b></div>
<div>  |-EnumConstantDecl 0x5e9bd80 <line:3:5, col:23> PKUnknownError 'enum PKPassKitErrorCode'</div><div>  | `-ImplicitCastExpr 0x5e9bd68 <col:22, col:23> 'NSInteger':'long' <IntegralCast></div>
<div>  |   `-UnaryOperator 0x5e9bd48 <col:22, col:23> 'int' prefix '-'</div><div>  |     `-IntegerLiteral 0x5e9bd28 <col:23> 'int' 1</div><div>  |-EnumConstantDecl 0x5ece960 <line:4:5, col:26> PKInvalidDataError 'enum PKPassKitErrorCode'</div>
<div>  | `-ImplicitCastExpr 0x5ece940 <col:26> 'NSInteger':'long' <IntegralCast></div><div>  |   `-IntegerLiteral 0x5e9bdd0 <col:26> 'int' 1</div><div>  |-EnumConstantDecl 0x5ece9b0 <line:5:5> PKUnsupportedVersionError 'enum PKPassKitErrorCode'</div>
<div>  `-EnumConstantDecl 0x5ecea00 <line:6:5> PKInvalidSignature 'enum PKPassKitErrorCode'</div></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div style="word-wrap:break-word"><div>Here's what Clang's internal -ast-dump option prints out for this code:</div><div><br></div><div><div></div></div><blockquote type="cite"><div><div>typedef long NSInteger;</div>
<div class="im"><div>typedef enum PKPassKitErrorCode : NSInteger PKPassKitErrorCode; enum PKPassKitErrorCode : NSInteger {</div><div>    PKUnknownError = -1,</div><div>    PKInvalidDataError = 1,</div><div>    PKUnsupportedVersionError,</div>
<div>    PKInvalidSignature,</div><div>} __attribute__((availability(ios,introduced=6.0)));</div></div></div></blockquote><div><br></div><div><div>TranslationUnitDecl 0x1020232d0 <<invalid sloc>></div><div>|-TypedefDecl 0x1020237b0 <<invalid sloc>> __int128_t '__int128'</div>
<div>|-TypedefDecl 0x102023810 <<invalid sloc>> __uint128_t 'unsigned __int128'</div><div>|-TypedefDecl 0x1020238a0 <<invalid sloc>> SEL 'SEL *'</div><div>|-TypedefDecl 0x102023970 <<invalid sloc>> id 'id'</div>
<div>|-TypedefDecl 0x102023a40 <<invalid sloc>> Class 'Class *'</div><div>|-ObjCInterfaceDecl 0x102023a90 <<invalid sloc>> Protocol</div><div>|-TypedefDecl 0x102023e30 <<invalid sloc>> __builtin_va_list '__va_list_tag [1]'</div>
<div>|-TypedefDecl 0x102023e90 <<stdin>:1:1, col:14> NSInteger 'long'</div><div>|-TypedefDecl 0x102058630 <line:2:1, col:45> PKPassKitErrorCode 'enum PKPassKitErrorCode':'enum PKPassKitErrorCode'</div>
<div>`-EnumDecl 0x1020586a0 <col:65, line:7:1> PKPassKitErrorCode 'NSInteger':'long'</div><div>  |-<b>AvailabilityAttr 0x102058900 <col:18, col:49> ios 6.0 0 0 ""</b></div><div>  |-EnumConstantDecl 0x102058780 <line:3:5, col:23> PKUnknownError 'NSInteger':'long'</div>
<div>  | `-ImplicitCastExpr 0x102058768 <col:22, col:23> 'NSInteger':'long' <IntegralCast></div><div>  |   `-UnaryOperator 0x102058748 <col:22, col:23> 'int' prefix '-'</div>
<div>  |     `-IntegerLiteral 0x102058728 <col:23> 'int' 1</div><div>  |-EnumConstantDecl 0x102058810 <line:4:5, col:26> PKInvalidDataError 'NSInteger':'long'</div><div>  | `-ImplicitCastExpr 0x1020587f0 <col:26> 'NSInteger':'long' <IntegralCast></div>
<div>  |   `-IntegerLiteral 0x1020587d0 <col:26> 'int' 1</div><div>  |-EnumConstantDecl 0x102058860 <line:5:5> PKUnsupportedVersionError 'NSInteger':'long'</div><div>  `-EnumConstantDecl 0x1020588b0 <line:6:5> PKInvalidSignature 'NSInteger':'long'</div>
</div><div><br></div><div>So maybe there's something else going on here, because the attribute is clearly there. You'll have to provide a more complete test case, and perhaps your modification, to see what's going on here.</div>
<span class="HOEnZb"><font color="#888888"><div><br></div><div>Jordan</div><div><br></div></font></span></div><br>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@cs.uiuc.edu">cfe-dev@cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br>