summaryrefslogtreecommitdiffstats
path: root/clang/lib/Analysis/CloneDetection.cpp
diff options
context:
space:
mode:
authorRaphael Isemann <teemperor@gmail.com>2017-09-03 13:45:33 +0000
committerRaphael Isemann <teemperor@gmail.com>2017-09-03 13:45:33 +0000
commit785e8161ad9ad61dbcfa6eb9197b319391a057c7 (patch)
tree59e76a29d4529cde733b4b20ff1f8baef20c2c03 /clang/lib/Analysis/CloneDetection.cpp
parentac12849d329986cfd77a26ee78f54f072b46a136 (diff)
downloadbcm5719-llvm-785e8161ad9ad61dbcfa6eb9197b319391a057c7.tar.gz
bcm5719-llvm-785e8161ad9ad61dbcfa6eb9197b319391a057c7.zip
[analyzer] MinComplexityConstraint now early exits and only does one macro stack lookup
Summary: This patch contains performance improvements for the `MinComplexityConstraint`. It reduces the constraint time when running on the SQLite codebase by around 43% (from 0.085s down to 0.049s). The patch is essentially doing two things: * It introduces a possibility for the complexity value to early exit when reaching the limit we were checking for. This means that once we noticed that the current clone is larger than the limit the user has set, we instantly exit and no longer traverse the tree or do further expensive lookups in the macro stack. * It also removes half of the macro stack lookups we do so far. Previously we always checked the start and the end location of a Stmt for macros, which was only a middle way between checking all locations of the Stmt and just checking one location. In practice I rarely found cases where it really matters if we check start/end or just the start of a statement as code with lots of macros that somehow just produce half a statement are very rare. Reviewers: NoQ Subscribers: cfe-commits, xazax.hun, v.g.vassilev Differential Revision: https://reviews.llvm.org/D34361 llvm-svn: 312440
Diffstat (limited to 'clang/lib/Analysis/CloneDetection.cpp')
-rw-r--r--clang/lib/Analysis/CloneDetection.cpp18
1 files changed, 10 insertions, 8 deletions
diff --git a/clang/lib/Analysis/CloneDetection.cpp b/clang/lib/Analysis/CloneDetection.cpp
index 8c833f47f04..acc1525ce4c 100644
--- a/clang/lib/Analysis/CloneDetection.cpp
+++ b/clang/lib/Analysis/CloneDetection.cpp
@@ -422,7 +422,8 @@ void RecursiveCloneTypeIIVerifyConstraint::constrain(
}
size_t MinComplexityConstraint::calculateStmtComplexity(
- const StmtSequence &Seq, const std::string &ParentMacroStack) {
+ const StmtSequence &Seq, std::size_t Limit,
+ const std::string &ParentMacroStack) {
if (Seq.empty())
return 0;
@@ -431,10 +432,8 @@ size_t MinComplexityConstraint::calculateStmtComplexity(
ASTContext &Context = Seq.getASTContext();
// Look up what macros expanded into the current statement.
- std::string StartMacroStack =
+ std::string MacroStack =
data_collection::getMacroStack(Seq.getStartLoc(), Context);
- std::string EndMacroStack =
- data_collection::getMacroStack(Seq.getEndLoc(), Context);
// First, check if ParentMacroStack is not empty which means we are currently
// dealing with a parent statement which was expanded from a macro.
@@ -444,8 +443,7 @@ size_t MinComplexityConstraint::calculateStmtComplexity(
// macro expansion will only increase the total complexity by one.
// Note: This is not the final complexity of this statement as we still
// add the complexity of the child statements to the complexity value.
- if (!ParentMacroStack.empty() && (StartMacroStack == ParentMacroStack &&
- EndMacroStack == ParentMacroStack)) {
+ if (!ParentMacroStack.empty() && MacroStack == ParentMacroStack) {
Complexity = 0;
}
@@ -454,12 +452,16 @@ size_t MinComplexityConstraint::calculateStmtComplexity(
if (Seq.holdsSequence()) {
for (const Stmt *S : Seq) {
Complexity += calculateStmtComplexity(
- StmtSequence(S, Seq.getContainingDecl()), StartMacroStack);
+ StmtSequence(S, Seq.getContainingDecl()), Limit, MacroStack);
+ if (Complexity >= Limit)
+ return Limit;
}
} else {
for (const Stmt *S : Seq.front()->children()) {
Complexity += calculateStmtComplexity(
- StmtSequence(S, Seq.getContainingDecl()), StartMacroStack);
+ StmtSequence(S, Seq.getContainingDecl()), Limit, MacroStack);
+ if (Complexity >= Limit)
+ return Limit;
}
}
return Complexity;
OpenPOWER on IntegriCloud