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.get("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()); 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: 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: 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 ntei(0); NegativeTemplateExisting nted(0); + +// This resulted in a warning by default. +#define MACRO() \ + struct MacroS { \ + void *P; \ + MacroS() : P(nullptr) {} \ + }; + +MACRO();