diff options
author | Aaron Ballman <aaron@aaronballman.com> | 2016-01-08 15:50:51 +0000 |
---|---|---|
committer | Aaron Ballman <aaron@aaronballman.com> | 2016-01-08 15:50:51 +0000 |
commit | c00ad6c5fb7740c18f324928f2add503fee752d1 (patch) | |
tree | 72529270b8a26f403c67fe2bff3c06d9e8998f7c | |
parent | b1b2f87e37774ee61dc5c6ea3aa04a99a1adda66 (diff) | |
download | bcm5719-llvm-c00ad6c5fb7740c18f324928f2add503fee752d1.tar.gz bcm5719-llvm-c00ad6c5fb7740c18f324928f2add503fee752d1.zip |
Disable part of the misc-move-constructor-init checker when the check is enabled through cert-oop11-cpp. The CERT guideline does not cover moveable parameters as part of the OOP11-CPP recommendation, just copy construction from move constructors.
llvm-svn: 257177
5 files changed, 51 insertions, 16 deletions
diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp index e6c181273b4..ac216753fe9 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp @@ -233,6 +233,10 @@ GlobList &ClangTidyContext::getChecksFilter() { return *CheckFilter; } +bool ClangTidyContext::isCheckEnabled(StringRef CheckName) const { + return CheckFilter->contains(CheckName); +} + /// \brief Store a \c ClangTidyError. void ClangTidyContext::storeError(const ClangTidyError &Error) { Errors.push_back(Error); diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h index 65d7e4eba73..780f8a32254 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h +++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h @@ -161,6 +161,9 @@ public: /// The \c CurrentFile can be changed using \c setCurrentFile. GlobList &getChecksFilter(); + /// \brief Returns true if the check name is enabled for the \c CurrentFile. + bool isCheckEnabled(StringRef CheckName) const; + /// \brief Returns global options. const ClangTidyGlobalOptions &getGlobalOptions() const; diff --git a/clang-tools-extra/clang-tidy/misc/MoveConstructorInitCheck.cpp b/clang-tools-extra/clang-tidy/misc/MoveConstructorInitCheck.cpp index ba182f3962e..eeebcf692bc 100644 --- a/clang-tools-extra/clang-tidy/misc/MoveConstructorInitCheck.cpp +++ b/clang-tools-extra/clang-tidy/misc/MoveConstructorInitCheck.cpp @@ -42,7 +42,8 @@ MoveConstructorInitCheck::MoveConstructorInitCheck(StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), IncludeStyle(IncludeSorter::parseIncludeStyle( - Options.get("IncludeStyle", "llvm"))) {} + Options.get("IncludeStyle", "llvm"))), + UseCERTSemantics(Context->isCheckEnabled("cert-oop11-cpp")) {} void MoveConstructorInitCheck::registerMatchers(MatchFinder *Finder) { // Only register the matchers for C++11; the functionality currently does not @@ -67,21 +68,26 @@ void MoveConstructorInitCheck::registerMatchers(MatchFinder *Finder) { hasDeclaration(cxxRecordDecl(hasMethod(cxxConstructorDecl( isMoveConstructor(), unless(isDeleted()))))), matchers::isExpensiveToCopy())); - Finder->addMatcher( - cxxConstructorDecl( - allOf( - unless(isMoveConstructor()), - hasAnyConstructorInitializer(withInitializer(cxxConstructExpr( - hasDeclaration(cxxConstructorDecl(isCopyConstructor())), - hasArgument( - 0, declRefExpr( - to(parmVarDecl( - hasType( - NonConstValueMovableAndExpensiveToCopy)) - .bind("movable-param"))) - .bind("init-arg"))))))) - .bind("ctor-decl"), - this); + + // This checker is also used to implement cert-oop11-cpp, but when using that + // form of the checker, we do not want to diagnose movable parameters. + if (!UseCERTSemantics) + Finder->addMatcher( + cxxConstructorDecl( + allOf( + unless(isMoveConstructor()), + hasAnyConstructorInitializer(withInitializer(cxxConstructExpr( + hasDeclaration(cxxConstructorDecl(isCopyConstructor())), + hasArgument( + 0, + declRefExpr( + to(parmVarDecl( + hasType( + NonConstValueMovableAndExpensiveToCopy)) + .bind("movable-param"))) + .bind("init-arg"))))))) + .bind("ctor-decl"), + this); } void MoveConstructorInitCheck::check(const MatchFinder::MatchResult &Result) { diff --git a/clang-tools-extra/clang-tidy/misc/MoveConstructorInitCheck.h b/clang-tools-extra/clang-tidy/misc/MoveConstructorInitCheck.h index 8898f2e3121..c9bac19e02e 100644 --- a/clang-tools-extra/clang-tidy/misc/MoveConstructorInitCheck.h +++ b/clang-tools-extra/clang-tidy/misc/MoveConstructorInitCheck.h @@ -39,6 +39,7 @@ private: std::unique_ptr<IncludeInserter> Inserter; const IncludeSorter::IncludeStyle IncludeStyle; + const bool UseCERTSemantics; }; } // namespace tidy diff --git a/clang-tools-extra/test/clang-tidy/cert-oop11-cpp.cpp b/clang-tools-extra/test/clang-tidy/cert-oop11-cpp.cpp new file mode 100644 index 00000000000..a4493bdc55a --- /dev/null +++ b/clang-tools-extra/test/clang-tidy/cert-oop11-cpp.cpp @@ -0,0 +1,21 @@ +// RUN: %check_clang_tidy %s cert-oop11-cpp %t -- -- -std=c++11 + +struct B { + B(B&&) noexcept = default; + + B(const B &) = default; + B& operator=(const B&) = default; + ~B() {} +}; + +struct D { + B b; + + // CHECK-MESSAGES: :[[@LINE+1]]:14: warning: move constructor initializes class member by calling a copy constructor [cert-oop11-cpp] + D(D &&d) : b(d.b) {} + + // This should not produce a diagnostic because it is not covered under + // the CERT guideline for OOP11-CPP. However, this will produce a diagnostic + // under misc-move-constructor-init. + D(B b) : b(b) {} +}; |