This is an archive of the discontinued LLVM Phabricator instance.

[analyzer] CloneDetector allows comparing clones for suspicious variable pattern errors.
ClosedPublic

Authored by teemperor on Aug 9 2016, 6:45 AM.

Details

Summary

One of the goals of the project was to find bugs caused by copy-pasting, which happen when a piece of code is copied but not all variables in this piece of code are correctly adapted to the new environment (e.g. like this 'if (mutex1.try_lock()) { foo(); mutex2.unlock(); }' where the mutex2 variable is a leftover from the original source code).

This patch adds support vor analyzing the pattern of the referenced variables for similar errors. So far only 'one-off' errors are supported, i.e. pattern errors where only one variable is not following the pattern of its related clones.

Additionally, the CloneChecker - which is currently the primary user of the CloneDetector API - now also supports reporting these suspicious clones.

Diff Detail

Event Timeline

teemperor updated this revision to Diff 67324.Aug 9 2016, 6:45 AM
teemperor retitled this revision from to [analyzer] CloneDetector allows comparing clones for suspicious variable pattern errors..
teemperor updated this object.
teemperor added reviewers: v.g.vassilev, NoQ, zaks.anna.
teemperor added subscribers: cfe-commits, vsk.
v.g.vassilev requested changes to this revision.Aug 9 2016, 1:18 PM
v.g.vassilev edited edge metadata.
v.g.vassilev added inline comments.
lib/StaticAnalyzer/Checkers/CloneChecker.cpp
83

More common in clang is to use "Did you mean to use...". If it is a warning it should be saying something like: "Suspicious code clone detected; did you mean to use %0"

86

Typo: usage.

89

I think this should be worded a bit better, too.

test/Analysis/copypaste/suspicious-clones.cpp
11

This note here is very obscure. It doesn't contain a lot of meaningful information. It should give a hint where is this similar piece of code.

This revision now requires changes to proceed.Aug 9 2016, 1:18 PM
NoQ edited edge metadata.Aug 15 2016, 6:55 AM

I couldn't find any real problems - the code's getting mature! Please accept a few very important "please rename this variable" comments :)

include/clang/Analysis/CloneDetection.h
236

It surprised me that you cannot really retrieve the clone (i.e. statement sequence) from the SuspiciousClone object. Maybe rename to eg. SuspiciousCloneInfo?

lib/Analysis/CloneDetection.cpp
92

It's surprising that Location is not of type SourceLocation, so much that i'd agree to rename it to Range, no matter how generic this word is.

174

Because this function doesn't return pattern differences, maybe rename to countPatternDifferences()? And the optional parameter kinda comes as a bonus feature.

183

Maybe reduce indent here through if (... == ...) continue, like in other places?

lib/StaticAnalyzer/Checkers/CloneChecker.cpp
39

Maybe move those out-of-line? They look pretty big.

43

I renamed the member variable to Detector in r276791, because it caused compile errors on buildbots (because class and member have the same name), so this patch would need to be patched up a bit - here and in one more place.

97

This line probably explains why i'm so much into renaming variables: it reads as if we're putting the location of the clone into the report, even though we're putting the location of a variable reference into it. Pair.FirstCloneInfo.VarRange would have been much more intuitive.

teemperor updated this revision to Diff 68422.Aug 17 2016, 2:23 PM
teemperor edited edge metadata.
teemperor marked 11 inline comments as done.
  • Made warning messages more 'clangish' as pointed out by Vassil (Thanks!)
  • Improved the class/variable names as pointed out by Artem (Thanks a lot!)
  • CloneChecker's functions are now out-of-line.
test/Analysis/copypaste/suspicious-clones.cpp
11

The note is paired with a warning as discussed in last meeting, so I'll mark this as done :).

v.g.vassilev accepted this revision.Aug 18 2016, 2:33 AM
v.g.vassilev edited edge metadata.

LGTM.

This revision is now accepted and ready to land.Aug 18 2016, 2:33 AM
This revision was automatically updated to reflect the committed changes.