summaryrefslogtreecommitdiffstats
path: root/clang-tools-extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp
diff options
context:
space:
mode:
authorAdam Balogh <adam.balogh@ericsson.com>2019-05-21 07:25:06 +0000
committerAdam Balogh <adam.balogh@ericsson.com>2019-05-21 07:25:06 +0000
commit5f3deb9bb5a7a6d84d46f4567872899c1ae29860 (patch)
tree440893fdd6536c9d3a32d746e73bf44d17f03d2a /clang-tools-extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp
parent03c4bf73eae69fece93d77a21cb3d354476e7878 (diff)
downloadbcm5719-llvm-5f3deb9bb5a7a6d84d46f4567872899c1ae29860.tar.gz
bcm5719-llvm-5f3deb9bb5a7a6d84d46f4567872899c1ae29860.zip
[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 llvm-svn: 361225
Diffstat (limited to 'clang-tools-extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp')
-rw-r--r--clang-tools-extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp18
1 files changed, 16 insertions, 2 deletions
diff --git a/clang-tools-extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp b/clang-tools-extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp
index b7077a1225f..7b1f7eafe1e 100644
--- a/clang-tools-extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp
+++ b/clang-tools-extra/clang-tidy/misc/ThrowByValueCatchByReferenceCheck.cpp
@@ -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::diagnoseCatchLocations(
// 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);
+ }
}
}
OpenPOWER on IntegriCloud