This is an archive of the discontinued LLVM Phabricator instance.

[DWARFYAML] Use override instead of virtual for better safety.
ClosedPublic

Authored by Higuoxing on Jul 8 2020, 8:53 PM.

Details

Summary

Functions in DWARFYML::FixupVisitor are declared as virtual functions in its base class DWARFYAML::Visitor. We should use the mordern "override" keyword instead of "virtual" for virtual functions in subclasses for better safety.

Besides, the visibility is changed from private to protected to make it consistent with DWARFYAML::FixupVisitor class and DWARFYAML::Visitor class.

Diff Detail

Event Timeline

Higuoxing created this revision.Jul 8 2020, 8:53 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 8 2020, 8:53 PM
grimar added a comment.Jul 9 2020, 1:39 AM

Changing virtual -> override looks fine to me, but why did you change private -> protected?

grimar added a comment.Jul 9 2020, 1:43 AM

Also, I find the description a bit confusing:

Virtual functions should be overridden in the derived class

Isn't this change (virtual->override) here a no-op to improve the code style?
All these funtions are anyways overriden, with the override word or without it.

Some points:

  1. The virtual on the old version didn't declare a new virtual class. In face, it was actually superfluous from a strict point of view. virtual is propagated to all sub-class versions of functions with the same signature, regardless of accessor type. Prior to C++11, it was traditionally used to show that a sub-class function was an implementation of a virtual function in the point.
  2. C++11 introduced the override specifier. It doesn't make a function any more or less virtual than it would have been before. All it does is require that the function overrides a virtual function in the parent class.
  3. There's no need to change from private to protected. This just affects how functions can be called from the outside world - it doesn't affect the virtual nature of functions.

That all being said, there's nothing wrong with this change, since you're working in the area, in my opinion. Using override is always a good idea since it provides safety guarantees (rather than potentially accidentally creating a new virtual function). You just need to update your description and summary accordingly (something like "use override instead of virtual for better safety").

Also, I find the description a bit confusing:

Virtual functions should be overridden in the derived class

Isn't this change (virtual->override) here a no-op to improve the code style?
All these funtions are anyways overriden, with the override word or without it.

(I'm working on the assumption that @Higuoxing misunderstood the meaning of virtual and override - see my comment that landed at the same time as yours)

Also, I find the description a bit confusing:

Virtual functions should be overridden in the derived class

Isn't this change (virtual->override) here a no-op to improve the code style?
All these funtions are anyways overriden, with the override word or without it.

Yes, I know these functions are overridden with or without the override key word. Because the DIEFixupVisitor class is derived from DWARFYAML::Vistor, and those onXXXX() functions are declared as virtual functions in DWARFYAML::Vistor class. I want to make it stricter since if we use override keyword to override an non-existing virtual function, compiler will complain about it. For example, if we change the signature of functions in derived class by mistake, the compiler will spot it for us.

As for the change from private: to protected:, that's because these functions in DWARFYAML::Vistor and DWARFYAML::DumpVistor are declared in protected keyword, I want to make them consistent. Sorry for my poor expressions :( Does it make sense now?

Some points:

  1. The virtual on the old version didn't declare a new virtual class. In face, it was actually superfluous from a strict point of view. virtual is propagated to all sub-class versions of functions with the same signature, regardless of accessor type. Prior to C++11, it was traditionally used to show that a sub-class function was an implementation of a virtual function in the point.
  2. C++11 introduced the override specifier. It doesn't make a function any more or less virtual than it would have been before. All it does is require that the function overrides a virtual function in the parent class.

Yes! That's exactly what I want to do! See my comments for @grimar.

  1. There's no need to change from private to protected. This just affects how functions can be called from the outside world - it doesn't affect the virtual nature of functions.

I want to make the coding style consistent since these functions in DWARFYAML::DIEFixupVisitor and DWARFYAML::Visitor are declared in protected keyword.

That all being said, there's nothing wrong with this change, since you're working in the area, in my opinion. Using override is always a good idea since it provides safety guarantees (rather than potentially accidentally creating a new virtual function). You just need to update your description and summary accordingly (something like "use override instead of virtual for better safety").

Okay, please update the summary and description to clarify this, because it looks like a number of people were confused!

Higuoxing retitled this revision from [DWARFYAML] Virtual functions should be overridden in derived class. to [DWARFYAML] Use override instead of virtual for better safety..Jul 9 2020, 2:17 AM
Higuoxing edited the summary of this revision. (Show Details)

Okay, please update the summary and description to clarify this, because it looks like a number of people were confused!

Sorry for the inconvenience. Does it look better now?

We should override those functions rather than declare a new set of virtual functions for better safety.

We're still not declaring a new set of virtual functions. Just say something like "We should use the modern override keyword instead of virtual for virtual functions in subclasses."

Higuoxing edited the summary of this revision. (Show Details)Jul 9 2020, 2:27 AM

We should override those functions rather than declare a new set of virtual functions for better safety.

We're still not declaring a new set of virtual functions. Just say something like "We should use the modern override keyword instead of virtual for virtual functions in subclasses."

Thanks a lot! I've changed the description.

This revision is now accepted and ready to land.Jul 9 2020, 2:41 AM
This revision was automatically updated to reflect the committed changes.