[clang] [clang] improve diagnostics for enums (PR #204682)

Corentin Jabot via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 19 00:14:02 PDT 2026


================
@@ -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;
----------------
cor3ntin wrote:

llvm::find_if?

https://github.com/llvm/llvm-project/pull/204682


More information about the cfe-commits mailing list