Author: baloghadamsoftware Date: Tue May 21 00:25:06 2019 New Revision: 361225
URL: http://llvm.org/viewvc/llvm-project?rev=361225&view=rev Log: [clang-tidy] New option for misc-throw-by-value-catch-by-reference Catching trivial objects by value is not dangerous but may be inefficient if they are too large. This patch adds an option `WarnOnLargeObject` to the checker to also warn if such an object is caught by value. An object is considered as "large" if its size is greater than `MaxSize` which is another option. Default value is the machine word of the architecture (size of the type `size_t`). Differential Revision: https://reviews.llvm.org/D61851 Modified: clang-tools-extra/trunk/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp clang-tools-extra/trunk/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.h clang-tools-extra/trunk/docs/ReleaseNotes.rst clang-tools-extra/trunk/docs/clang-tidy/checks/misc-throw-by-value-catch-by-reference.rst Modified: clang-tools-extra/trunk/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp?rev=361225&r1=361224&r2=361225&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp (original) +++ clang-tools-extra/trunk/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp Tue May 21 00:25:06 2019 @@ -20,7 +20,10 @@ namespace misc { ThrowByValueCatchByReferenceCheck::ThrowByValueCatchByReferenceCheck( StringRef Name, ClangTidyContext *Context) : ClangTidyCheck(Name, Context), - CheckAnonymousTemporaries(Options.get("CheckThrowTemporaries", true)) {} + CheckAnonymousTemporaries(Options.get("CheckThrowTemporaries", true)), + WarnOnLargeObject(Options.get("WarnOnLargeObject", false)), + // Cannot access `ASTContext` from here so set it to an extremal value. + MaxSize(Options.get("MaxSize", std::numeric_limits<uint64_t>::max())) {} void ThrowByValueCatchByReferenceCheck::registerMatchers(MatchFinder *Finder) { // This is a C++ only check thus we register the matchers only for C++ @@ -150,8 +153,19 @@ void ThrowByValueCatchByReferenceCheck:: // If it's not a pointer and not a reference then it must be caught "by // value". In this case we should emit a diagnosis message unless the type // is trivial. - if (!caughtType.isTrivialType(context)) + if (!caughtType.isTrivialType(context)) { diag(varDecl->getBeginLoc(), diagMsgCatchReference); + } else if (WarnOnLargeObject) { + // If the type is trivial, then catching it by reference is not dangerous. + // However, catching large objects by value decreases the performance. + + // We can now access `ASTContext` so if `MaxSize` is an extremal value + // then set it to the size of `size_t`. + if (MaxSize == std::numeric_limits<uint64_t>::max()) + MaxSize = context.getTypeSize(context.getSizeType()); + if (context.getTypeSize(caughtType) > MaxSize) + diag(varDecl->getBeginLoc(), diagMsgCatchReference); + } } } Modified: clang-tools-extra/trunk/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.h URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.h?rev=361225&r1=361224&r2=361225&view=diff ============================================================================== --- clang-tools-extra/trunk/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.h (original) +++ clang-tools-extra/trunk/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.h Tue May 21 00:25:06 2019 @@ -41,6 +41,8 @@ private: bool isCatchVariable(const DeclRefExpr *declRefExpr); bool isFunctionOrCatchVar(const DeclRefExpr *declRefExpr); const bool CheckAnonymousTemporaries; + const bool WarnOnLargeObject; + uint64_t MaxSize; // No `const` because we have to set it in two steps. }; } // namespace misc Modified: clang-tools-extra/trunk/docs/ReleaseNotes.rst URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/ReleaseNotes.rst?rev=361225&r1=361224&r2=361225&view=diff ============================================================================== --- clang-tools-extra/trunk/docs/ReleaseNotes.rst (original) +++ clang-tools-extra/trunk/docs/ReleaseNotes.rst Tue May 21 00:25:06 2019 @@ -170,6 +170,11 @@ Improvements to clang-tidy Rewrites function signatures to use a trailing return type. +- The :doc:`misc-throw-by-value-catch-by-reference + <clang-tidy/misc-throw-by-value-catch-by-reference.rst>` now supports + `WarnOnLargeObject` and `MaxSize` options to warn on any large trivial + object caught by value. + Improvements to include-fixer ----------------------------- Modified: clang-tools-extra/trunk/docs/clang-tidy/checks/misc-throw-by-value-catch-by-reference.rst URL: http://llvm.org/viewvc/llvm-project/clang-tools-extra/trunk/docs/clang-tidy/checks/misc-throw-by-value-catch-by-reference.rst?rev=361225&r1=361224&r2=361225&view=diff ============================================================================== --- clang-tools-extra/trunk/docs/clang-tidy/checks/misc-throw-by-value-catch-by-reference.rst (original) +++ clang-tools-extra/trunk/docs/clang-tidy/checks/misc-throw-by-value-catch-by-reference.rst Tue May 21 00:25:06 2019 @@ -32,3 +32,18 @@ Options <https://www.securecoding.cert.org/confluence/display/cplusplus/ERR09-CPP.+Throw+anonymous+temporaries>`_. Default is `1`. +.. option:: WarnOnLargeObject + + Also warns for any large, trivial object caught by value. Catching a large + object by value is not dangerous but affects the performance negatively. The + maximum size of an object allowed to be caught without warning can be set + using the `MaxSize` option. + Default is `0`. + +.. option:: MaxSize + + Determines the maximum size of an object allowed to be caught without + warning. Only applicable if `WarnOnLargeObject` is set to `1`. If option is + set by the user to `std::numeric_limits<uint64_t>::max()` then it reverts to + the default value. + Default is the size of `size_t`. _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits