[clang-tools-extra] r302429 - clang-tidy: add IgnoreMacros option to modernize-use-default-member-init

Miklos Vajna via cfe-commits cfe-commits at lists.llvm.org
Mon May 8 08:13:31 PDT 2017


Author: vmiklos
Date: Mon May  8 10:13:31 2017
New Revision: 302429

URL: http://llvm.org/viewvc/llvm-project?rev=302429&view=rev
Log:
clang-tidy: add IgnoreMacros option to modernize-use-default-member-init

Summary:
And also enable it by default to be consistent with e.g.
modernize-use-using.

This helps e.g. when running this check on cppunit client code where the
macro is provided by the system, so there is no easy way to modify it.

Reviewers: alexfh, malcolm.parsons

Reviewed By: malcolm.parsons

Subscribers: cfe-commits

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

Added:
    clang-tools-extra/trunk/test/clang-tidy/modernize-use-default-member-init-macros.cpp
Modified:
    clang-tools-extra/trunk/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
    clang-tools-extra/trunk/clang-tidy/modernize/UseDefaultMemberInitCheck.h
    clang-tools-extra/trunk/clang-tidy/modernize/UseUsingCheck.cpp
    clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-use-default-member-init.rst
    clang-tools-extra/trunk/test/clang-tidy/modernize-use-default-member-init.cpp

Modified: clang-tools-extra/trunk/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp?rev=302429&r1=302428&r2=302429&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/UseDefaultMemberInitCheck.cpp Mon May  8 10:13:31 2017
@@ -139,11 +139,13 @@ static bool sameValue(const Expr *E1, co
 UseDefaultMemberInitCheck::UseDefaultMemberInitCheck(StringRef Name,
                                                      ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
-      UseAssignment(Options.get("UseAssignment", 0) != 0) {}
+      UseAssignment(Options.get("UseAssignment", 0) != 0),
+      IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", 1) != 0) {}
 
 void UseDefaultMemberInitCheck::storeOptions(
     ClangTidyOptions::OptionMap &Opts) {
   Options.store(Opts, "UseAssignment", UseAssignment);
+  Options.store(Opts, "IgnoreMacros", IgnoreMacros);
 }
 
 void UseDefaultMemberInitCheck::registerMatchers(MatchFinder *Finder) {
@@ -197,6 +199,10 @@ void UseDefaultMemberInitCheck::checkDef
     const MatchFinder::MatchResult &Result, const CXXCtorInitializer *Init) {
   const FieldDecl *Field = Init->getMember();
 
+  SourceLocation StartLoc = Field->getLocStart();
+  if (StartLoc.isMacroID() && IgnoreMacros)
+    return;
+
   SourceLocation FieldEnd =
       Lexer::getLocForEndOfToken(Field->getSourceRange().getEnd(), 0,
                                  *Result.SourceManager, getLangOpts());

Modified: clang-tools-extra/trunk/clang-tidy/modernize/UseDefaultMemberInitCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseDefaultMemberInitCheck.h?rev=302429&r1=302428&r2=302429&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/UseDefaultMemberInitCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/UseDefaultMemberInitCheck.h Mon May  8 10:13:31 2017
@@ -36,6 +36,7 @@ private:
                          const CXXCtorInitializer *Init);
 
   const bool UseAssignment;
+  const bool IgnoreMacros;
 };
 
 } // namespace modernize

Modified: clang-tools-extra/trunk/clang-tidy/modernize/UseUsingCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseUsingCheck.cpp?rev=302429&r1=302428&r2=302429&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/UseUsingCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/UseUsingCheck.cpp Mon May  8 10:13:31 2017
@@ -19,7 +19,7 @@ namespace modernize {
 
 UseUsingCheck::UseUsingCheck(StringRef Name, ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
-      IgnoreMacros(Options.get("IgnoreMacros", true)) {}
+      IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true)) {}
 
 void UseUsingCheck::registerMatchers(MatchFinder *Finder) {
   if (!getLangOpts().CPlusPlus11)

Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-use-default-member-init.rst
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-use-default-member-init.rst?rev=302429&r1=302428&r2=302429&view=diff
==============================================================================
--- clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-use-default-member-init.rst (original)
+++ clang-tools-extra/trunk/docs/clang-tidy/checks/modernize-use-default-member-init.rst Mon May  8 10:13:31 2017
@@ -47,3 +47,8 @@ Options
     int i = 5;
     double j = 10.0;
   };
+
+.. option:: IgnoreMacros
+
+   If this option is set to non-zero (default is `1`), the check will not warn
+   about members declared inside macros.

Added: clang-tools-extra/trunk/test/clang-tidy/modernize-use-default-member-init-macros.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-default-member-init-macros.cpp?rev=302429&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-use-default-member-init-macros.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-default-member-init-macros.cpp Mon May  8 10:13:31 2017
@@ -0,0 +1,18 @@
+// RUN: %check_clang_tidy %s modernize-use-default-member-init %t -- \
+// RUN:   -config="{CheckOptions: [{key: modernize-use-default-member-init.IgnoreMacros, value: 0}]}" \
+// RUN:   -- -std=c++11
+
+#define MACRO() \
+  struct S { \
+    void *P; \
+    S() : P(nullptr) {} \
+  };
+
+MACRO();
+// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use default member initializer for 'P'
+
+struct S2 {
+  void *P;
+  // CHECK-MESSAGES: :[[@LINE-1]]:9: warning: use default member initializer for 'P'
+  S2() : P(nullptr) {}
+};

Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-use-default-member-init.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-default-member-init.cpp?rev=302429&r1=302428&r2=302429&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-use-default-member-init.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-default-member-init.cpp Mon May  8 10:13:31 2017
@@ -380,3 +380,12 @@ struct NegativeTemplateExisting {
 
 NegativeTemplateExisting<int> ntei(0);
 NegativeTemplateExisting<double> nted(0);
+
+// This resulted in a warning by default.
+#define MACRO() \
+  struct MacroS { \
+    void *P; \
+    MacroS() : P(nullptr) {} \
+  };
+
+MACRO();




More information about the cfe-commits mailing list