[llvm] 797faba - [Analysis] Avoid virtual dtor. NFC.

Michael Kruse via llvm-commits llvm-commits at lists.llvm.org
Wed May 18 15:43:16 PDT 2022


Author: Michael Kruse
Date: 2022-05-18T17:41:17-05:00
New Revision: 797fabaab2a5e02d00de4755ef8f5a38127110df

URL: https://github.com/llvm/llvm-project/commit/797fabaab2a5e02d00de4755ef8f5a38127110df
DIFF: https://github.com/llvm/llvm-project/commit/797fabaab2a5e02d00de4755ef8f5a38127110df.diff

LOG: [Analysis] Avoid virtual dtor. NFC.

Replace virtual destructor by a protected non-virtual one. Additionally also making derived structs as virtual avoids the warning from reappearing.

Also see the mailing list discussion: https://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20220516/1038290.html

Reviewed By: dblaikie, YangKeao

Differential Revision: https://reviews.llvm.org/D125830

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/DOTGraphTraitsPass.h
    llvm/include/llvm/Analysis/DomPrinter.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/DOTGraphTraitsPass.h b/llvm/include/llvm/Analysis/DOTGraphTraitsPass.h
index 30216c87bc21c..c35e189de6fc6 100644
--- a/llvm/include/llvm/Analysis/DOTGraphTraitsPass.h
+++ b/llvm/include/llvm/Analysis/DOTGraphTraitsPass.h
@@ -44,7 +44,6 @@ struct DOTGraphTraitsViewer
     : PassInfoMixin<DOTGraphTraitsViewer<AnalysisT, IsSimple, GraphT,
                                          AnalysisGraphTraitsT>> {
   DOTGraphTraitsViewer(StringRef GraphName) : Name(GraphName) {}
-  virtual ~DOTGraphTraitsViewer() {}
 
   /// Return true if this function should be processed.
   ///
@@ -68,6 +67,18 @@ struct DOTGraphTraitsViewer
     return PreservedAnalyses::all();
   };
 
+protected:
+  /// Avoid compiler warning "has virtual functions but non-virtual destructor
+  /// [-Wnon-virtual-dtor]" in derived classes.
+  ///
+  /// DOTGraphTraitsViewer is also used as a mixin for avoiding repeated
+  /// implementation of viewer passes, ie there should be no
+  /// runtime-polymorphisms/downcasting involving this class and hence no
+  /// virtual destructor needed. Making this dtor protected stops accidental
+  /// invocation when the derived class destructor should have been called.
+  /// Those derived classes sould be marked final to avoid the warning.
+  ~DOTGraphTraitsViewer() {}
+
 private:
   StringRef Name;
 };
@@ -99,7 +110,6 @@ struct DOTGraphTraitsPrinter
     : PassInfoMixin<DOTGraphTraitsPrinter<AnalysisT, IsSimple, GraphT,
                                           AnalysisGraphTraitsT>> {
   DOTGraphTraitsPrinter(StringRef GraphName) : Name(GraphName) {}
-  virtual ~DOTGraphTraitsPrinter() {}
 
   /// Return true if this function should be processed.
   ///
@@ -124,6 +134,18 @@ struct DOTGraphTraitsPrinter
     return PreservedAnalyses::all();
   };
 
+protected:
+  /// Avoid compiler warning "has virtual functions but non-virtual destructor
+  /// [-Wnon-virtual-dtor]" in derived classes.
+  ///
+  /// DOTGraphTraitsPrinter is also used as a mixin for avoiding repeated
+  /// implementation of printer passes, ie there should be no
+  /// runtime-polymorphisms/downcasting involving this class and hence no
+  /// virtual destructor needed. Making this dtor protected stops accidental
+  /// invocation when the derived class destructor should have been called.
+  /// Those derived classes sould be marked final to avoid the warning.
+  ~DOTGraphTraitsPrinter() {}
+
 private:
   StringRef Name;
 };

diff  --git a/llvm/include/llvm/Analysis/DomPrinter.h b/llvm/include/llvm/Analysis/DomPrinter.h
index 5ba717f67403e..83fe721346ab2 100644
--- a/llvm/include/llvm/Analysis/DomPrinter.h
+++ b/llvm/include/llvm/Analysis/DomPrinter.h
@@ -74,46 +74,45 @@ struct DOTGraphTraits<PostDominatorTree *>
   }
 };
 
-struct DomViewer : public DOTGraphTraitsViewer<DominatorTreeAnalysis, false> {
+struct DomViewer final : DOTGraphTraitsViewer<DominatorTreeAnalysis, false> {
   DomViewer() : DOTGraphTraitsViewer<DominatorTreeAnalysis, false>("dom") {}
 };
 
-struct DomOnlyViewer
-    : public DOTGraphTraitsViewer<DominatorTreeAnalysis, true> {
+struct DomOnlyViewer final : DOTGraphTraitsViewer<DominatorTreeAnalysis, true> {
   DomOnlyViewer()
       : DOTGraphTraitsViewer<DominatorTreeAnalysis, true>("domonly") {}
 };
 
-struct PostDomViewer
-    : public DOTGraphTraitsViewer<PostDominatorTreeAnalysis, false> {
+struct PostDomViewer final
+    : DOTGraphTraitsViewer<PostDominatorTreeAnalysis, false> {
   PostDomViewer()
       : DOTGraphTraitsViewer<PostDominatorTreeAnalysis, false>("postdom") {}
 };
 
-struct PostDomOnlyViewer
-    : public DOTGraphTraitsViewer<PostDominatorTreeAnalysis, true> {
+struct PostDomOnlyViewer final
+    : DOTGraphTraitsViewer<PostDominatorTreeAnalysis, true> {
   PostDomOnlyViewer()
       : DOTGraphTraitsViewer<PostDominatorTreeAnalysis, true>("postdomonly") {}
 };
 
-struct DomPrinter : public DOTGraphTraitsPrinter<DominatorTreeAnalysis, false> {
+struct DomPrinter final : DOTGraphTraitsPrinter<DominatorTreeAnalysis, false> {
   DomPrinter() : DOTGraphTraitsPrinter<DominatorTreeAnalysis, false>("dom") {}
 };
 
-struct DomOnlyPrinter
-    : public DOTGraphTraitsPrinter<DominatorTreeAnalysis, true> {
+struct DomOnlyPrinter final
+    : DOTGraphTraitsPrinter<DominatorTreeAnalysis, true> {
   DomOnlyPrinter()
       : DOTGraphTraitsPrinter<DominatorTreeAnalysis, true>("domonly") {}
 };
 
-struct PostDomPrinter
-    : public DOTGraphTraitsPrinter<PostDominatorTreeAnalysis, false> {
+struct PostDomPrinter final
+    : DOTGraphTraitsPrinter<PostDominatorTreeAnalysis, false> {
   PostDomPrinter()
       : DOTGraphTraitsPrinter<PostDominatorTreeAnalysis, false>("postdom") {}
 };
 
-struct PostDomOnlyPrinter
-    : public DOTGraphTraitsPrinter<PostDominatorTreeAnalysis, true> {
+struct PostDomOnlyPrinter final
+    : DOTGraphTraitsPrinter<PostDominatorTreeAnalysis, true> {
   PostDomOnlyPrinter()
       : DOTGraphTraitsPrinter<PostDominatorTreeAnalysis, true>("postdomonly") {}
 };


        


More information about the llvm-commits mailing list