[clang-tools-extra] r311136 - [clang-tidy] Add modernize-use-equals-default.IgnoreMacros option
Alexander Kornienko via cfe-commits
cfe-commits at lists.llvm.org
Thu Aug 17 16:07:59 PDT 2017
Author: alexfh
Date: Thu Aug 17 16:07:59 2017
New Revision: 311136
URL: http://llvm.org/viewvc/llvm-project?rev=311136&view=rev
Log:
[clang-tidy] Add modernize-use-equals-default.IgnoreMacros option
Added:
clang-tools-extra/trunk/test/clang-tidy/modernize-use-equals-default-macros.cpp
Modified:
clang-tools-extra/trunk/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
clang-tools-extra/trunk/clang-tidy/modernize/UseEqualsDefaultCheck.h
clang-tools-extra/trunk/test/clang-tidy/modernize-use-equals-default-copy.cpp
clang-tools-extra/trunk/test/clang-tidy/modernize-use-equals-default.cpp
Modified: clang-tools-extra/trunk/clang-tidy/modernize/UseEqualsDefaultCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseEqualsDefaultCheck.cpp?rev=311136&r1=311135&r2=311136&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/UseEqualsDefaultCheck.cpp (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/UseEqualsDefaultCheck.cpp Thu Aug 17 16:07:59 2017
@@ -197,36 +197,46 @@ static bool bodyEmpty(const ASTContext *
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
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() ||
Modified: clang-tools-extra/trunk/clang-tidy/modernize/UseEqualsDefaultCheck.h
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/modernize/UseEqualsDefaultCheck.h?rev=311136&r1=311135&r2=311136&view=diff
==============================================================================
--- clang-tools-extra/trunk/clang-tidy/modernize/UseEqualsDefaultCheck.h (original)
+++ clang-tools-extra/trunk/clang-tidy/modernize/UseEqualsDefaultCheck.h Thu Aug 17 16:07:59 2017
@@ -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
Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-use-equals-default-copy.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-equals-default-copy.cpp?rev=311136&r1=311135&r2=311136&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-use-equals-default-copy.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-equals-default-copy.cpp Thu Aug 17 16:07:59 2017
@@ -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 {
Added: clang-tools-extra/trunk/test/clang-tidy/modernize-use-equals-default-macros.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-equals-default-macros.cpp?rev=311136&view=auto
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-use-equals-default-macros.cpp (added)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-equals-default-macros.cpp Thu Aug 17 16:07:59 2017
@@ -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:
Modified: clang-tools-extra/trunk/test/clang-tidy/modernize-use-equals-default.cpp
URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/test/clang-tidy/modernize-use-equals-default.cpp?rev=311136&r1=311135&r2=311136&view=diff
==============================================================================
--- clang-tools-extra/trunk/test/clang-tidy/modernize-use-equals-default.cpp (original)
+++ clang-tools-extra/trunk/test/clang-tidy/modernize-use-equals-default.cpp Thu Aug 17 16:07:59 2017
@@ -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)
More information about the cfe-commits
mailing list