[PATCH] D32945: clang-tidy: add IgnoreMacros option to modernize-use-default-member-init
Miklos Vajna via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Sun May 7 13:20:37 PDT 2017
vmiklos updated this revision to Diff 98108.
https://reviews.llvm.org/D32945
Files:
clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
clang-tidy/modernize/UseDefaultMemberInitCheck.h
docs/clang-tidy/checks/modernize-use-default-member-init.rst
test/clang-tidy/modernize-use-default-member-init-macros.cpp
test/clang-tidy/modernize-use-default-member-init.cpp
Index: test/clang-tidy/modernize-use-default-member-init.cpp
===================================================================
--- test/clang-tidy/modernize-use-default-member-init.cpp
+++ test/clang-tidy/modernize-use-default-member-init.cpp
@@ -380,3 +380,12 @@
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();
Index: test/clang-tidy/modernize-use-default-member-init-macros.cpp
===================================================================
--- /dev/null
+++ test/clang-tidy/modernize-use-default-member-init-macros.cpp
@@ -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) {}
+};
Index: docs/clang-tidy/checks/modernize-use-default-member-init.rst
===================================================================
--- docs/clang-tidy/checks/modernize-use-default-member-init.rst
+++ docs/clang-tidy/checks/modernize-use-default-member-init.rst
@@ -47,3 +47,8 @@
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.
Index: clang-tidy/modernize/UseDefaultMemberInitCheck.h
===================================================================
--- clang-tidy/modernize/UseDefaultMemberInitCheck.h
+++ clang-tidy/modernize/UseDefaultMemberInitCheck.h
@@ -36,6 +36,7 @@
const CXXCtorInitializer *Init);
const bool UseAssignment;
+ const bool IgnoreMacros;
};
} // namespace modernize
Index: clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
===================================================================
--- clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
+++ clang-tidy/modernize/UseDefaultMemberInitCheck.cpp
@@ -139,11 +139,13 @@
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 @@
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());
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D32945.98108.patch
Type: text/x-patch
Size: 3491 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170507/bd3ff626/attachment.bin>
More information about the cfe-commits
mailing list