PiotrZSL updated this revision to Diff 513871.
PiotrZSL marked an inline comment as done.
PiotrZSL added a comment.

Remove of fix start/end marker.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D147924

Files:
  clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
  clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.h
  clang-tools-extra/docs/ReleaseNotes.rst
  clang-tools-extra/docs/clang-tidy/checks/modernize/use-override.rst
  
clang-tools-extra/test/clang-tidy/checkers/modernize/use-override-templates.cpp

Index: clang-tools-extra/test/clang-tidy/checkers/modernize/use-override-templates.cpp
===================================================================
--- /dev/null
+++ clang-tools-extra/test/clang-tidy/checkers/modernize/use-override-templates.cpp
@@ -0,0 +1,47 @@
+// RUN: %check_clang_tidy %s modernize-use-override %t -- \
+// RUN:   -config="{CheckOptions: [{key: modernize-use-override.IgnoreTemplateInstantiations, value: true}]}"
+
+struct Base {
+  virtual void foo();
+};
+
+struct Base2 {
+  virtual void foo2();
+};
+
+template<typename T>
+struct Derived : T {
+  // should not warn, comes from template instance
+  virtual void foo();
+  virtual void foo2();
+};
+
+void test() {
+  Derived<Base> b;
+  Derived<Base2> b2;
+}
+
+template<typename T>
+struct BaseS {
+  virtual void boo();
+};
+
+template<>
+struct BaseS<int> {
+  virtual void boo2();
+};
+
+struct BaseU {
+  virtual void boo3();
+};
+
+template<typename T>
+struct Derived2 : BaseS<T>, BaseU {
+  // should not warn, comes from template instance
+  virtual void boo();
+  virtual void boo2();
+  // should warn, comes from non-template BaseU
+  virtual void boo3();
+  // CHECK-MESSAGES: :[[@LINE-1]]:16: warning: prefer using 'override' or (rarely) 'final' instead of 'virtual' [modernize-use-override]
+  // CHECK-FIXES: void boo3() override;
+};
Index: clang-tools-extra/docs/clang-tidy/checks/modernize/use-override.rst
===================================================================
--- clang-tools-extra/docs/clang-tidy/checks/modernize/use-override.rst
+++ clang-tools-extra/docs/clang-tidy/checks/modernize/use-override.rst
@@ -27,6 +27,11 @@
 
    If set to `true`, this check will not diagnose destructors. Default is `false`.
 
+.. option:: IgnoreTemplateInstantiations
+
+   If set to `true`, instructs this check to ignore virtual function overrides
+   that are part of template instantiations. Default is `false`.
+
 .. option:: AllowOverrideAndFinal
 
    If set to `true`, this check will not diagnose ``override`` as redundant
Index: clang-tools-extra/docs/ReleaseNotes.rst
===================================================================
--- clang-tools-extra/docs/ReleaseNotes.rst
+++ clang-tools-extra/docs/ReleaseNotes.rst
@@ -246,6 +246,11 @@
   functions containing macros or preprocessor directives, and out-of-line special
   member functions in unions.
 
+- Improved :doc:`modernize-use-override
+  <clang-tidy/checks/modernize/use-override>` check with new
+  `IgnoreTemplateInstantiations` option to optionally ignore virtual function
+  overrides that are part of template instantiations.
+
 - Fixed reading `HungarianNotation.CString.*` options in
   :doc:`readability-identifier-naming
   <clang-tidy/checks/readability/identifier-naming>` check.
Index: clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.h
===================================================================
--- clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.h
+++ clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.h
@@ -27,6 +27,7 @@
 
 private:
   const bool IgnoreDestructors;
+  const bool IgnoreTemplateInstantiations;
   const bool AllowOverrideAndFinal;
   const StringRef OverrideSpelling;
   const StringRef FinalSpelling;
Index: clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
===================================================================
--- clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
+++ clang-tools-extra/clang-tidy/modernize/UseOverrideCheck.cpp
@@ -18,24 +18,35 @@
 UseOverrideCheck::UseOverrideCheck(StringRef Name, ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
       IgnoreDestructors(Options.get("IgnoreDestructors", false)),
+      IgnoreTemplateInstantiations(
+          Options.get("IgnoreTemplateInstantiations", false)),
       AllowOverrideAndFinal(Options.get("AllowOverrideAndFinal", false)),
       OverrideSpelling(Options.get("OverrideSpelling", "override")),
       FinalSpelling(Options.get("FinalSpelling", "final")) {}
 
 void UseOverrideCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "IgnoreDestructors", IgnoreDestructors);
+  Options.store(Opts, "IgnoreTemplateInstantiations",
+                IgnoreTemplateInstantiations);
   Options.store(Opts, "AllowOverrideAndFinal", AllowOverrideAndFinal);
   Options.store(Opts, "OverrideSpelling", OverrideSpelling);
   Options.store(Opts, "FinalSpelling", FinalSpelling);
 }
 
 void UseOverrideCheck::registerMatchers(MatchFinder *Finder) {
-  if (IgnoreDestructors)
-    Finder->addMatcher(
-        cxxMethodDecl(isOverride(), unless(cxxDestructorDecl())).bind("method"),
-        this);
-  else
-    Finder->addMatcher(cxxMethodDecl(isOverride()).bind("method"), this);
+
+  auto IgnoreDestructorMatcher =
+      IgnoreDestructors ? cxxMethodDecl(unless(cxxDestructorDecl()))
+                        : cxxMethodDecl();
+  auto IgnoreTemplateInstantiationsMatcher =
+      IgnoreTemplateInstantiations
+          ? cxxMethodDecl(unless(ast_matchers::isTemplateInstantiation()))
+          : cxxMethodDecl();
+  Finder->addMatcher(cxxMethodDecl(isOverride(),
+                                   IgnoreTemplateInstantiationsMatcher,
+                                   IgnoreDestructorMatcher)
+                         .bind("method"),
+                     this);
 }
 
 // Re-lex the tokens to get precise locations to insert 'override' and remove
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to