<div class="gmail_quote">On Wed, Jul 25, 2012 at 3:03 PM, Richard Smith <span dir="ltr"><<a href="mailto:richard@metafoo.co.uk" target="_blank">richard@metafoo.co.uk</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div>+ // Use to store the smallest APSInt size that can represent all the elements.</div><div>+ bool signedness = false;</div><div>+ unsigned bitwidth = 0;</div><div>+</div><div>+ // Skip diagnostic if previous error were found with the enum.</div>
<div>+ for (unsigned i = 0; i != NumElements; ++i) {</div><div>+ EnumConstantDecl *ECD = cast<EnumConstantDecl>(Elements[i]);</div><div>+ if (!ECD)</div><div>+ return;</div><div>+</div><div>+ const llvm::APSInt& Val = ECD->getInitVal();</div>
<div>+ if (!signedness && Val.isSigned()) {</div><div>+ signedness = true;</div><div>+ ++bitwidth;</div><div>+ }</div><div>+</div><div>+ unsigned ValWidth;</div><div>+ if (Val.isUnsigned())</div>
<div>+ ValWidth = Val.getActiveBits() + signedness;</div><div>+ else if (Val.isNonNegative())</div><div>+ ValWidth = Val.getActiveBits() + 1;</div><div>+ else</div><div>+ ValWidth = Val.getBitWidth() - Val.countLeadingOnes() + 1;</div>
<div>+</div><div>+ if (bitwidth < ValWidth)</div><div>+ bitwidth = ValWidth;</div><div>+ }</div><div><br></div><div>You can get these directly from the EnumDecl: see getNumPositiveBits, getNumNegativeBits.</div>
<div><br></div><div><div>+ // Store a map of values to decls. Values are extended to a common size</div><div>+ // first to for comparisons.</div><div>+ std::map<llvm::APSInt, EnumConstantDecl*> ValueMap;</div></div>
<div><br></div><div>How about building a SmallVector of EnumConstantDecl*, sorting it by init value then by whether there is an init expression, and finally performing your check as a single linear pass over the vector? That should be a bit quicker than the repeated map lookups and heap allocations you're currently doing.</div>
</blockquote><div><br></div><div>I'd also suggest disabling the warning if your enumeration doesn't fit in 64 bits, and using getZExtValue rather than extOrTrunc.</div></div>