================
@@ -74,6 +75,34 @@ static bool isInitializedByLiteral(const EnumConstantDecl
*Enumerator) {
return Init->isIntegerConstantExpr(Enumerator->getASTContext());
}
+/// Returns true if all but the last enumerator are explicitly initialized
+/// with consecutive (potentially negated) integer literal values and the
+/// last enumerator's value is left implicit, e.g.
+/// enum E { A = 10, B = 11, C = 12, Size };
+static bool hasConsecutiveInitialValuesExceptLast(const EnumDecl &Node) {
+ const llvm::SmallVector<const EnumConstantDecl *> Enumerators(
----------------
zeyi2 wrote:
Nit: I was wondering if we could avoid the SmallVector copy without sacrificing
readability. Here is my attempt:
```cpp
static bool hasConsecutiveInitialValuesExceptLast(const EnumDecl &Node) {
const auto Enumerators = Node.enumerators();
auto It = Enumerators.begin();
const auto End = Enumerators.end();
if (It == End || !isInitializedByLiteral(*It))
return false;
llvm::APSInt PrevValue = (*It)->getInitVal();
while (++It != End) {
const EnumConstantDecl *Enumerator = *It;
if (Enumerator->getInitVal() != ++PrevValue)
return false;
if (std::next(It) == End)
return Enumerator->getInitExpr() == nullptr;
if (!isInitializedByLiteral(Enumerator))
return false;
}
return false;
}
```
That said, I doubt there's any noticeable performance difference here, so feel
free to ignore this.
https://github.com/llvm/llvm-project/pull/228399
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits