[clang-tidy] Add modernize-use-equals-default.IgnoreMacros option

llvm-svn: 311136
This commit is contained in:
Alexander Kornienko 2017-08-17 23:07:59 +00:00
parent 51f52c4fca
commit dea43983ae
5 changed files with 64 additions and 35 deletions

View File

@ -197,36 +197,46 @@ static bool bodyEmpty(const ASTContext *Context, const CompoundStmt *Body) {
return !Invalid && std::strspn(Text.data(), " \t\r\n") == Text.size();
}
UseEqualsDefaultCheck::UseEqualsDefaultCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
IgnoreMacros(Options.getLocalOrGlobal("IgnoreMacros", true) != 0) {}
void UseEqualsDefaultCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
Options.store(Opts, "IgnoreMacros", IgnoreMacros);
}
void UseEqualsDefaultCheck::registerMatchers(MatchFinder *Finder) {
if (getLangOpts().CPlusPlus) {
// Destructor.
Finder->addMatcher(cxxDestructorDecl(isDefinition()).bind(SpecialFunction),
this);
Finder->addMatcher(
cxxConstructorDecl(
isDefinition(),
anyOf(
// Default constructor.
allOf(unless(hasAnyConstructorInitializer(isWritten())),
parameterCountIs(0)),
// Copy constructor.
allOf(isCopyConstructor(),
// Discard constructors that can be used as a copy
// constructor because all the other arguments have
// default values.
parameterCountIs(1))))
.bind(SpecialFunction),
this);
// Copy-assignment operator.
Finder->addMatcher(
cxxMethodDecl(isDefinition(), isCopyAssignmentOperator(),
// isCopyAssignmentOperator() allows the parameter to be
// passed by value, and in this case it cannot be
// defaulted.
hasParameter(0, hasType(lValueReferenceType())))
.bind(SpecialFunction),
this);
}
if (!getLangOpts().CPlusPlus)
return;
// Destructor.
Finder->addMatcher(cxxDestructorDecl(isDefinition()).bind(SpecialFunction),
this);
Finder->addMatcher(
cxxConstructorDecl(
isDefinition(),
anyOf(
// Default constructor.
allOf(unless(hasAnyConstructorInitializer(isWritten())),
parameterCountIs(0)),
// Copy constructor.
allOf(isCopyConstructor(),
// Discard constructors that can be used as a copy
// constructor because all the other arguments have
// default values.
parameterCountIs(1))))
.bind(SpecialFunction),
this);
// Copy-assignment operator.
Finder->addMatcher(
cxxMethodDecl(isDefinition(), isCopyAssignmentOperator(),
// isCopyAssignmentOperator() allows the parameter to be
// passed by value, and in this case it cannot be
// defaulted.
hasParameter(0, hasType(lValueReferenceType())))
.bind(SpecialFunction),
this);
}
void UseEqualsDefaultCheck::check(const MatchFinder::MatchResult &Result) {
@ -236,6 +246,9 @@ void UseEqualsDefaultCheck::check(const MatchFinder::MatchResult &Result) {
const auto *SpecialFunctionDecl =
Result.Nodes.getNodeAs<CXXMethodDecl>(SpecialFunction);
if (IgnoreMacros && SpecialFunctionDecl->getLocation().isMacroID())
return;
// Discard explicitly deleted/defaulted special member functions and those
// that are not user-provided (automatically generated).
if (SpecialFunctionDecl->isDeleted() ||

View File

@ -37,10 +37,13 @@ namespace modernize {
/// http://clang.llvm.org/extra/clang-tidy/checks/modernize-use-equals-default.html
class UseEqualsDefaultCheck : public ClangTidyCheck {
public:
UseEqualsDefaultCheck(StringRef Name, ClangTidyContext *Context)
: ClangTidyCheck(Name, Context) {}
UseEqualsDefaultCheck(StringRef Name, ClangTidyContext *Context);
void storeOptions(ClangTidyOptions::OptionMap &Opts) override;
void registerMatchers(ast_matchers::MatchFinder *Finder) override;
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
const bool IgnoreMacros;
};
} // namespace modernize

View File

@ -1,4 +1,6 @@
// RUN: %check_clang_tidy %s modernize-use-equals-default %t -- -- -std=c++11 -fno-delayed-template-parsing -fexceptions
// RUN: %check_clang_tidy %s modernize-use-equals-default %t -- \
// RUN: -config="{CheckOptions: [{key: modernize-use-equals-default.IgnoreMacros, value: 0}]}" \
// RUN: -- -std=c++11 -fno-delayed-template-parsing -fexceptions
// Out of line definition.
struct OL {

View File

@ -0,0 +1,13 @@
// RUN: %check_clang_tidy %s modernize-use-equals-default %t -- \
// RUN: -config="{CheckOptions: [{key: modernize-use-equals-default.IgnoreMacros, value: 0}]}" \
// RUN: -- -std=c++11
#define STRUCT_WITH_DEFAULT(_base, _type) \
struct _type { \
_type() {} \
_base value; \
};
STRUCT_WITH_DEFAULT(unsigned char, InMacro)
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use '= default' to define a trivial default constructor
// CHECK-MESSAGES: :[[@LINE-6]]:13: note:

View File

@ -204,6 +204,4 @@ OTC::~OTC() try {} catch(...) {}
_base value; \
};
STRUCT_WITH_DEFAULT(unsigned char, Hex8Default)
// CHECK-MESSAGES: :[[@LINE-1]]:1: warning: use '= default' to define a trivial default constructor
// CHECK-MESSAGES: :[[@LINE-6]]:13: note:
STRUCT_WITH_DEFAULT(unsigned char, InMacro)