Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/StaticAnalyzer/Checkers/CloneChecker.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- CloneChecker.cpp - Clone detection checker -------------*- C++ -*-===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
///
9
/// \file
10
/// CloneChecker is a checker that reports clones in the current translation
11
/// unit.
12
///
13
//===----------------------------------------------------------------------===//
14
15
#include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h"
16
#include "clang/Analysis/CloneDetection.h"
17
#include "clang/Basic/Diagnostic.h"
18
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
19
#include "clang/StaticAnalyzer/Core/Checker.h"
20
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
21
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h"
22
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23
24
using namespace clang;
25
using namespace ento;
26
27
namespace {
28
class CloneChecker
29
    : public Checker<check::ASTCodeBody, check::EndOfTranslationUnit> {
30
public:
31
  // Checker options.
32
  int MinComplexity;
33
  bool ReportNormalClones;
34
  StringRef IgnoredFilesPattern;
35
36
private:
37
  mutable CloneDetector Detector;
38
  mutable std::unique_ptr<BugType> BT_Exact, BT_Suspicious;
39
40
public:
41
  void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
42
                        BugReporter &BR) const;
43
44
  void checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
45
                                 AnalysisManager &Mgr, BugReporter &BR) const;
46
47
  /// Reports all clones to the user.
48
  void reportClones(BugReporter &BR, AnalysisManager &Mgr,
49
                    std::vector<CloneDetector::CloneGroup> &CloneGroups) const;
50
51
  /// Reports only suspicious clones to the user along with information
52
  /// that explain why they are suspicious.
53
  void reportSuspiciousClones(
54
      BugReporter &BR, AnalysisManager &Mgr,
55
      std::vector<CloneDetector::CloneGroup> &CloneGroups) const;
56
};
57
} // end anonymous namespace
58
59
void CloneChecker::checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
60
93
                                    BugReporter &BR) const {
61
93
  // Every statement that should be included in the search for clones needs to
62
93
  // be passed to the CloneDetector.
63
93
  Detector.analyzeCodeBody(D);
64
93
}
65
66
void CloneChecker::checkEndOfTranslationUnit(const TranslationUnitDecl *TU,
67
                                             AnalysisManager &Mgr,
68
28
                                             BugReporter &BR) const {
69
28
  // At this point, every statement in the translation unit has been analyzed by
70
28
  // the CloneDetector. The only thing left to do is to report the found clones.
71
28
72
28
  // Let the CloneDetector create a list of clones from all the analyzed
73
28
  // statements. We don't filter for matching variable patterns at this point
74
28
  // because reportSuspiciousClones() wants to search them for errors.
75
28
  std::vector<CloneDetector::CloneGroup> AllCloneGroups;
76
28
77
28
  Detector.findClones(
78
28
      AllCloneGroups, FilenamePatternConstraint(IgnoredFilesPattern),
79
28
      RecursiveCloneTypeIIHashConstraint(), MinGroupSizeConstraint(2),
80
28
      MinComplexityConstraint(MinComplexity),
81
28
      RecursiveCloneTypeIIVerifyConstraint(), OnlyLargestCloneConstraint());
82
28
83
28
  reportSuspiciousClones(BR, Mgr, AllCloneGroups);
84
28
85
28
  // We are done for this translation unit unless we also need to report normal
86
28
  // clones.
87
28
  if (!ReportNormalClones)
88
1
    return;
89
27
90
27
  // Now that the suspicious clone detector has checked for pattern errors,
91
27
  // we also filter all clones who don't have matching patterns
92
27
  CloneDetector::constrainClones(AllCloneGroups,
93
27
                                 MatchingVariablePatternConstraint(),
94
27
                                 MinGroupSizeConstraint(2));
95
27
96
27
  reportClones(BR, Mgr, AllCloneGroups);
97
27
}
98
99
static PathDiagnosticLocation makeLocation(const StmtSequence &S,
100
25
                                           AnalysisManager &Mgr) {
101
25
  ASTContext &ACtx = Mgr.getASTContext();
102
25
  return PathDiagnosticLocation::createBegin(
103
25
      S.front(), ACtx.getSourceManager(),
104
25
      Mgr.getAnalysisDeclContext(ACtx.getTranslationUnitDecl()));
105
25
}
106
107
void CloneChecker::reportClones(
108
    BugReporter &BR, AnalysisManager &Mgr,
109
27
    std::vector<CloneDetector::CloneGroup> &CloneGroups) const {
110
27
111
27
  if (!BT_Exact)
112
27
    BT_Exact.reset(new BugType(this, "Exact code clone", "Code clone"));
113
27
114
27
  for (const CloneDetector::CloneGroup &Group : CloneGroups) {
115
12
    // We group the clones by printing the first as a warning and all others
116
12
    // as a note.
117
12
    auto R = llvm::make_unique<BugReport>(*BT_Exact, "Duplicate code detected",
118
12
                                          makeLocation(Group.front(), Mgr));
119
12
    R->addRange(Group.front().getSourceRange());
120
12
121
25
    for (unsigned i = 1; i < Group.size(); 
++i13
)
122
13
      R->addNote("Similar code here", makeLocation(Group[i], Mgr),
123
13
                 Group[i].getSourceRange());
124
12
    BR.emitReport(std::move(R));
125
12
  }
126
27
}
127
128
void CloneChecker::reportSuspiciousClones(
129
    BugReporter &BR, AnalysisManager &Mgr,
130
28
    std::vector<CloneDetector::CloneGroup> &CloneGroups) const {
131
28
  std::vector<VariablePattern::SuspiciousClonePair> Pairs;
132
28
133
28
  for (const CloneDetector::CloneGroup &Group : CloneGroups) {
134
54
    for (unsigned i = 0; i < Group.size(); 
++i37
) {
135
37
      VariablePattern PatternA(Group[i]);
136
37
137
53
      for (unsigned j = i + 1; j < Group.size(); 
++j16
) {
138
22
        VariablePattern PatternB(Group[j]);
139
22
140
22
        VariablePattern::SuspiciousClonePair ClonePair;
141
22
        // For now, we only report clones which break the variable pattern just
142
22
        // once because multiple differences in a pattern are an indicator that
143
22
        // those differences are maybe intended (e.g. because it's actually a
144
22
        // different algorithm).
145
22
        // FIXME: In very big clones even multiple variables can be unintended,
146
22
        // so replacing this number with a percentage could better handle such
147
22
        // cases. On the other hand it could increase the false-positive rate
148
22
        // for all clones if the percentage is too high.
149
22
        if (PatternA.countPatternDifferences(PatternB, &ClonePair) == 1) {
150
6
          Pairs.push_back(ClonePair);
151
6
          break;
152
6
        }
153
22
      }
154
37
    }
155
17
  }
156
28
157
28
  if (!BT_Suspicious)
158
28
    BT_Suspicious.reset(
159
28
        new BugType(this, "Suspicious code clone", "Code clone"));
160
28
161
28
  ASTContext &ACtx = BR.getContext();
162
28
  SourceManager &SM = ACtx.getSourceManager();
163
28
  AnalysisDeclContext *ADC =
164
28
      Mgr.getAnalysisDeclContext(ACtx.getTranslationUnitDecl());
165
28
166
28
  for (VariablePattern::SuspiciousClonePair &Pair : Pairs) {
167
6
    // FIXME: We are ignoring the suggestions currently, because they are
168
6
    // only 50% accurate (even if the second suggestion is unavailable),
169
6
    // which may confuse the user.
170
6
    // Think how to perform more accurate suggestions?
171
6
172
6
    auto R = llvm::make_unique<BugReport>(
173
6
        *BT_Suspicious,
174
6
        "Potential copy-paste error; did you really mean to use '" +
175
6
            Pair.FirstCloneInfo.Variable->getNameAsString() + "' here?",
176
6
        PathDiagnosticLocation::createBegin(Pair.FirstCloneInfo.Mention, SM,
177
6
                                            ADC));
178
6
    R->addRange(Pair.FirstCloneInfo.Mention->getSourceRange());
179
6
180
6
    R->addNote("Similar code using '" +
181
6
                   Pair.SecondCloneInfo.Variable->getNameAsString() + "' here",
182
6
               PathDiagnosticLocation::createBegin(Pair.SecondCloneInfo.Mention,
183
6
                                                   SM, ADC),
184
6
               Pair.SecondCloneInfo.Mention->getSourceRange());
185
6
186
6
    BR.emitReport(std::move(R));
187
6
  }
188
28
}
189
190
//===----------------------------------------------------------------------===//
191
// Register CloneChecker
192
//===----------------------------------------------------------------------===//
193
194
28
void ento::registerCloneChecker(CheckerManager &Mgr) {
195
28
  auto *Checker = Mgr.registerChecker<CloneChecker>();
196
28
197
28
  Checker->MinComplexity = Mgr.getAnalyzerOptions().getCheckerIntegerOption(
198
28
      Checker, "MinimumCloneComplexity");
199
28
200
28
  if (Checker->MinComplexity < 0)
201
0
    Mgr.reportInvalidCheckerOptionValue(
202
0
        Checker, "MinimumCloneComplexity", "a non-negative value");
203
28
204
28
  Checker->ReportNormalClones = Mgr.getAnalyzerOptions().getCheckerBooleanOption(
205
28
      Checker, "ReportNormalClones");
206
28
207
28
  Checker->IgnoredFilesPattern = Mgr.getAnalyzerOptions()
208
28
    .getCheckerStringOption(Checker, "IgnoredFilesPattern");
209
28
}
210
211
28
bool ento::shouldRegisterCloneChecker(const LangOptions &LO) {
212
28
  return true;
213
28
}