================
@@ -1843,6 +1843,21 @@ void 
StmtPrinter::VisitMatrixElementExpr(MatrixElementExpr *Node) {
 }
 
 void StmtPrinter::VisitCStyleCastExpr(CStyleCastExpr *Node) {
+  // special case enums to avoid producing cast expressions when naming
+  // an enumerator would suffice
+  if (QualType T = Node->getType(); T->isEnumeralType()) {
+    const auto *IL = dyn_cast<IntegerLiteral>(Node->getSubExpr());
+    const auto *ED = T->getAsEnumDecl();
+    if (IL && ED) {
+      llvm::APInt Val = IL->getValue();
+      for (const EnumConstantDecl *ECD : ED->enumerators()) {
+        if (llvm::APInt::isSameValue(ECD->getInitVal(), Val)) {
+          ECD->printQualifiedName(OS, Policy);
+          return;
----------------
Tsche wrote:

```cpp
const auto* it = llvm::find_if(ED->enumerators(), [&](const EnumConstantDecl* 
ECD) {
  return llvm::APInt::isSameValue(ECD->getInitVal(), Val);
});
if (it != ED->enumerator_end()) {
  it->printQualifiedName(OS, Policy);
  return;
}
```

doesn't seem terribly cleaner to me. What do you think?

https://github.com/llvm/llvm-project/pull/204682
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to