[PATCH] D81703: [Clang] Don't leave Expr::Classification fields uninitialized in default constructor

Gui Andrade via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jun 11 17:38:50 PDT 2020


guiand created this revision.
guiand added a reviewer: rsmith.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
guiand edited the summary of this revision.

This changes `Expr::Classification::{Kind, Modifiable}` fields so that they are initialized as `Unknown` rather than left uninitialized. This way, if the default `Classification` is accidentally used, Clang will fire an assertion.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D81703

Files:
  clang/include/clang/AST/Expr.h
  clang/lib/AST/ExprClassification.cpp


Index: clang/lib/AST/ExprClassification.cpp
===================================================================
--- clang/lib/AST/ExprClassification.cpp
+++ clang/lib/AST/ExprClassification.cpp
@@ -65,6 +65,8 @@
   case Cl::CL_ArrayTemporary:
   case Cl::CL_ObjCMessageRValue:
   case Cl::CL_PRValue: assert(getValueKind() == VK_RValue); break;
+  case Cl::CL_Unknown:
+    llvm_unreachable("Tried to classify unknown class!");
   }
 
   Cl::ModifiableType modifiable = Cl::CM_Untested;
@@ -678,6 +680,7 @@
   case Cl::CL_ArrayTemporary: return LV_ArrayTemporary;
   case Cl::CL_ObjCMessageRValue: return LV_InvalidMessageExpression;
   case Cl::CL_PRValue: return LV_InvalidExpression;
+  case Cl::CL_Unknown: break;
   }
   llvm_unreachable("Unhandled kind");
 }
@@ -701,6 +704,7 @@
   case Cl::CL_PRValue:
     return VC.getModifiable() == Cl::CM_LValueCast ?
       MLV_LValueCast : MLV_InvalidExpression;
+  case Cl::CL_Unknown: break;
   }
   assert(VC.getKind() == Cl::CL_LValue && "Unhandled kind");
   switch (VC.getModifiable()) {
@@ -716,6 +720,7 @@
   case Cl::CM_ConstAddrSpace: return MLV_ConstAddrSpace;
   case Cl::CM_ArrayType: return MLV_ArrayType;
   case Cl::CM_IncompleteType: return MLV_IncompleteType;
+  case Cl::CM_Unknown: break;
   }
   llvm_unreachable("Unhandled modifiable type");
 }
Index: clang/include/clang/AST/Expr.h
===================================================================
--- clang/include/clang/AST/Expr.h
+++ clang/include/clang/AST/Expr.h
@@ -326,7 +326,8 @@
       CL_ClassTemporary, // A temporary of class type, or subobject thereof.
       CL_ArrayTemporary, // A temporary of array type.
       CL_ObjCMessageRValue, // ObjC message is an rvalue
-      CL_PRValue // A prvalue for any other reason, of any other type
+      CL_PRValue, // A prvalue for any other reason, of any other type
+      CL_Unknown // Null enumeration
     };
     /// The results of modification testing.
     enum ModifiableType {
@@ -340,14 +341,15 @@
       CM_ConstQualifiedField,
       CM_ConstAddrSpace,
       CM_ArrayType,
-      CM_IncompleteType
+      CM_IncompleteType,
+      CM_Unknown // Null enumeration
     };
 
   private:
     friend class Expr;
 
-    unsigned short Kind;
-    unsigned short Modifiable;
+    unsigned short Kind = CL_Unknown;
+    unsigned short Modifiable = CM_Unknown;
 
     explicit Classification(Kinds k, ModifiableType m)
       : Kind(k), Modifiable(m)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D81703.270268.patch
Type: text/x-patch
Size: 2433 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200612/8b831346/attachment.bin>


More information about the cfe-commits mailing list