[PATCH] D101341: Initialize optional members of ELFYAML types.

James Henderson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 11 00:51:39 PDT 2021


jhenderson added inline comments.


================
Comment at: llvm/include/llvm/ObjectYAML/ELFYAML.h:661
 struct MipsABIFlags : Section {
-  llvm::yaml::Hex16 Version;
+  llvm::yaml::Hex16 Version{0};
   MIPS_ISA ISALevel;
----------------
vzakhari wrote:
> MaskRay wrote:
> > vzakhari wrote:
> > > vzakhari wrote:
> > > > MaskRay wrote:
> > > > > Let strong typedef Hex16/MIPS_AFL_REG initialize the base value
> > > > Sorry, I do not understand this.
> > > > 
> > > > Are you suggesting to let Hex16's default constructor to initialize the actual value?  I believe there is no default constructor that would do that.
> > > > 
> > > > 
> > > > ```
> > > >  #define LLVM_YAML_STRONG_TYPEDEF(_base, _type)                                 \
> > > >      struct _type {                                                             \
> > > >          _type() = default;                                                     \
> > > >          _type(const _base v) : value(v) {}                                     \
> > > >          _type(const _type &v) = default;                                       \
> > > >          _type &operator=(const _type &rhs) = default;                          \
> > > >          _type &operator=(const _base &rhs) { value = rhs; return *this; }      \
> > > >          operator const _base & () const { return value; }                      \
> > > >          bool operator==(const _type &rhs) const { return value == rhs.value; } \
> > > >          bool operator==(const _base &rhs) const { return value == rhs; }       \
> > > >          bool operator<(const _type &rhs) const { return value < rhs.value; }   \
> > > >          _base value;                                                           \
> > > >          using BaseType = _base;                                                \
> > > >      };
> > > > ```
> > > > 
> > > > Default construction will leave `value` uninitialized for POD `_base` type.  Zero-initializaton will set `value` to 0.
> > > Just to clarify, I preferred value-initialization over zero-initialization to make all the initializations consistent, and since not all default values may be 0.
> > You can initialize `_base value; `, then you don't need to spread `{0}` everywhere.
> Can you please suggest the exact change?  Do you mean something like `_base value = _base()`?
> 
> In general, `_base` type may have a deleted default constructor, so this may not work.
Are there any actual cases in current practice where the default constructor has been deleted?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D101341/new/

https://reviews.llvm.org/D101341



More information about the llvm-commits mailing list