Coverage Report

Created: 2022-01-25 06:29

/Users/buildslave/jenkins/workspace/coverage/llvm-project/clang/include/clang/Analysis/CloneDetection.h
Line
Count
Source (jump to first uncovered line)
1
//===--- CloneDetection.h - Finds code clones in an AST ---------*- 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
/// This file defines classes for searching and analyzing source code clones.
11
///
12
//===----------------------------------------------------------------------===//
13
14
#ifndef LLVM_CLANG_ANALYSIS_CLONEDETECTION_H
15
#define LLVM_CLANG_ANALYSIS_CLONEDETECTION_H
16
17
#include "clang/AST/StmtVisitor.h"
18
#include "llvm/Support/Regex.h"
19
#include <vector>
20
21
namespace clang {
22
23
class Stmt;
24
class Decl;
25
class VarDecl;
26
class ASTContext;
27
class CompoundStmt;
28
29
/// Identifies a list of statements.
30
///
31
/// Can either identify a single arbitrary Stmt object, a continuous sequence of
32
/// child statements inside a CompoundStmt or no statements at all.
33
class StmtSequence {
34
  /// If this object identifies a sequence of statements inside a CompoundStmt,
35
  /// S points to this CompoundStmt. If this object only identifies a single
36
  /// Stmt, then S is a pointer to this Stmt.
37
  const Stmt *S;
38
39
  /// The declaration that contains the statements.
40
  const Decl *D;
41
42
  /// If EndIndex is non-zero, then S is a CompoundStmt and this StmtSequence
43
  /// instance is representing the CompoundStmt children inside the array
44
  /// [StartIndex, EndIndex).
45
  unsigned StartIndex;
46
  unsigned EndIndex;
47
48
public:
49
  /// Constructs a StmtSequence holding multiple statements.
50
  ///
51
  /// The resulting StmtSequence identifies a continuous sequence of statements
52
  /// in the body of the given CompoundStmt. Which statements of the body should
53
  /// be identified needs to be specified by providing a start and end index
54
  /// that describe a non-empty sub-array in the body of the given CompoundStmt.
55
  ///
56
  /// \param Stmt A CompoundStmt that contains all statements in its body.
57
  /// \param D The Decl containing this Stmt.
58
  /// \param StartIndex The inclusive start index in the children array of
59
  ///                   \p Stmt
60
  /// \param EndIndex The exclusive end index in the children array of \p Stmt.
61
  StmtSequence(const CompoundStmt *Stmt, const Decl *D, unsigned StartIndex,
62
               unsigned EndIndex);
63
64
  /// Constructs a StmtSequence holding a single statement.
65
  ///
66
  /// \param Stmt An arbitrary Stmt.
67
  /// \param D The Decl containing this Stmt.
68
  StmtSequence(const Stmt *Stmt, const Decl *D);
69
70
  /// Constructs an empty StmtSequence.
71
  StmtSequence();
72
73
  typedef const Stmt *const *iterator;
74
75
  /// Returns an iterator pointing to the first statement in this sequence.
76
  iterator begin() const;
77
78
  /// Returns an iterator pointing behind the last statement in this sequence.
79
  iterator end() const;
80
81
  /// Returns the first statement in this sequence.
82
  ///
83
  /// This method should only be called on a non-empty StmtSequence object.
84
8.35k
  const Stmt *front() const {
85
8.35k
    assert(!empty());
86
0
    return begin()[0];
87
8.35k
  }
88
89
  /// Returns the last statement in this sequence.
90
  ///
91
  /// This method should only be called on a non-empty StmtSequence object.
92
871
  const Stmt *back() const {
93
871
    assert(!empty());
94
0
    return begin()[size() - 1];
95
871
  }
96
97
  /// Returns the number of statements this object holds.
98
13.4k
  unsigned size() const {
99
13.4k
    if (holdsSequence())
100
2.55k
      return EndIndex - StartIndex;
101
10.9k
    if (S == nullptr)
102
0
      return 0;
103
10.9k
    return 1;
104
10.9k
  }
105
106
  /// Returns true if and only if this StmtSequence contains no statements.
107
12.6k
  bool empty() const { return size() == 0; }
108
109
  /// Returns the related ASTContext for the stored Stmts.
110
  ASTContext &getASTContext() const;
111
112
  /// Returns the declaration that contains the stored Stmts.
113
9.14k
  const Decl *getContainingDecl() const {
114
9.14k
    assert(D);
115
0
    return D;
116
9.14k
  }
117
118
  /// Returns true if this objects holds a list of statements.
119
39.0k
  bool holdsSequence() const { return EndIndex != 0; }
120
121
  /// Returns the start sourcelocation of the first statement in this sequence.
122
  ///
123
  /// This method should only be called on a non-empty StmtSequence object.
124
  SourceLocation getBeginLoc() const;
125
126
  /// Returns the end sourcelocation of the last statement in this sequence.
127
  ///
128
  /// This method should only be called on a non-empty StmtSequence object.
129
  SourceLocation getEndLoc() const;
130
131
  /// Returns the source range of the whole sequence - from the beginning
132
  /// of the first statement to the end of the last statement.
133
  SourceRange getSourceRange() const;
134
135
0
  bool operator==(const StmtSequence &Other) const {
136
0
    return std::tie(S, StartIndex, EndIndex) ==
137
0
           std::tie(Other.S, Other.StartIndex, Other.EndIndex);
138
0
  }
139
140
0
  bool operator!=(const StmtSequence &Other) const {
141
0
    return std::tie(S, StartIndex, EndIndex) !=
142
0
           std::tie(Other.S, Other.StartIndex, Other.EndIndex);
143
0
  }
144
145
  /// Returns true if and only if this sequence covers a source range that
146
  /// contains the source range of the given sequence \p Other.
147
  ///
148
  /// This method should only be called on a non-empty StmtSequence object
149
  /// and passed a non-empty StmtSequence object.
150
  bool contains(const StmtSequence &Other) const;
151
};
152
153
/// Searches for similar subtrees in the AST.
154
///
155
/// First, this class needs several declarations with statement bodies which
156
/// can be passed via analyzeCodeBody. Afterwards all statements can be
157
/// searched for clones by calling findClones with a given list of constraints
158
/// that should specify the wanted properties of the clones.
159
///
160
/// The result of findClones can be further constrained with the constrainClones
161
/// method.
162
///
163
/// This class only searches for clones in executable source code
164
/// (e.g. function bodies). Other clones (e.g. cloned comments or declarations)
165
/// are not supported.
166
class CloneDetector {
167
168
public:
169
  /// A collection of StmtSequences that share an arbitrary property.
170
  typedef llvm::SmallVector<StmtSequence, 8> CloneGroup;
171
172
  /// Generates and stores search data for all statements in the body of
173
  /// the given Decl.
174
  void analyzeCodeBody(const Decl *D);
175
176
  /// Constrains the given list of clone groups with the given constraint.
177
  ///
178
  /// The constraint is expected to have a method with the signature
179
  ///     `void constrain(std::vector<CloneDetector::CloneGroup> &Sequences)`
180
  /// as this is the interface that the CloneDetector uses for applying the
181
  /// constraint. The constraint is supposed to directly modify the passed list
182
  /// so that all clones in the list fulfill the specific property this
183
  /// constraint ensures.
184
  template <typename T>
185
232
  static void constrainClones(std::vector<CloneGroup> &CloneGroups, T C) {
186
232
    C.constrain(CloneGroups);
187
232
  }
void clang::CloneDetector::constrainClones<clang::FilenamePatternConstraint>(std::__1::vector<llvm::SmallVector<clang::StmtSequence, 8u>, std::__1::allocator<llvm::SmallVector<clang::StmtSequence, 8u> > >&, clang::FilenamePatternConstraint)
Line
Count
Source
185
28
  static void constrainClones(std::vector<CloneGroup> &CloneGroups, T C) {
186
28
    C.constrain(CloneGroups);
187
28
  }
void clang::CloneDetector::constrainClones<clang::RecursiveCloneTypeIIHashConstraint>(std::__1::vector<llvm::SmallVector<clang::StmtSequence, 8u>, std::__1::allocator<llvm::SmallVector<clang::StmtSequence, 8u> > >&, clang::RecursiveCloneTypeIIHashConstraint)
Line
Count
Source
185
30
  static void constrainClones(std::vector<CloneGroup> &CloneGroups, T C) {
186
30
    C.constrain(CloneGroups);
187
30
  }
void clang::CloneDetector::constrainClones<clang::MinGroupSizeConstraint>(std::__1::vector<llvm::SmallVector<clang::StmtSequence, 8u>, std::__1::allocator<llvm::SmallVector<clang::StmtSequence, 8u> > >&, clang::MinGroupSizeConstraint)
Line
Count
Source
185
57
  static void constrainClones(std::vector<CloneGroup> &CloneGroups, T C) {
186
57
    C.constrain(CloneGroups);
187
57
  }
void clang::CloneDetector::constrainClones<clang::MinComplexityConstraint>(std::__1::vector<llvm::SmallVector<clang::StmtSequence, 8u>, std::__1::allocator<llvm::SmallVector<clang::StmtSequence, 8u> > >&, clang::MinComplexityConstraint)
Line
Count
Source
185
30
  static void constrainClones(std::vector<CloneGroup> &CloneGroups, T C) {
186
30
    C.constrain(CloneGroups);
187
30
  }
void clang::CloneDetector::constrainClones<clang::RecursiveCloneTypeIIVerifyConstraint>(std::__1::vector<llvm::SmallVector<clang::StmtSequence, 8u>, std::__1::allocator<llvm::SmallVector<clang::StmtSequence, 8u> > >&, clang::RecursiveCloneTypeIIVerifyConstraint)
Line
Count
Source
185
30
  static void constrainClones(std::vector<CloneGroup> &CloneGroups, T C) {
186
30
    C.constrain(CloneGroups);
187
30
  }
void clang::CloneDetector::constrainClones<clang::OnlyLargestCloneConstraint>(std::__1::vector<llvm::SmallVector<clang::StmtSequence, 8u>, std::__1::allocator<llvm::SmallVector<clang::StmtSequence, 8u> > >&, clang::OnlyLargestCloneConstraint)
Line
Count
Source
185
30
  static void constrainClones(std::vector<CloneGroup> &CloneGroups, T C) {
186
30
    C.constrain(CloneGroups);
187
30
  }
void clang::CloneDetector::constrainClones<clang::MatchingVariablePatternConstraint>(std::__1::vector<llvm::SmallVector<clang::StmtSequence, 8u>, std::__1::allocator<llvm::SmallVector<clang::StmtSequence, 8u> > >&, clang::MatchingVariablePatternConstraint)
Line
Count
Source
185
27
  static void constrainClones(std::vector<CloneGroup> &CloneGroups, T C) {
186
27
    C.constrain(CloneGroups);
187
27
  }
188
189
  /// Constrains the given list of clone groups with the given list of
190
  /// constraints.
191
  ///
192
  /// The constraints are applied in sequence in the order in which they are
193
  /// passed to this function.
194
  template <typename T1, typename... Ts>
195
  static void constrainClones(std::vector<CloneGroup> &CloneGroups, T1 C,
196
169
                              Ts... ConstraintList) {
197
169
    constrainClones(CloneGroups, C);
198
169
    constrainClones(CloneGroups, ConstraintList...);
199
169
  }
void clang::CloneDetector::constrainClones<clang::FilenamePatternConstraint, clang::RecursiveCloneTypeIIHashConstraint, clang::MinGroupSizeConstraint, clang::MinComplexityConstraint, clang::RecursiveCloneTypeIIVerifyConstraint, clang::OnlyLargestCloneConstraint>(std::__1::vector<llvm::SmallVector<clang::StmtSequence, 8u>, std::__1::allocator<llvm::SmallVector<clang::StmtSequence, 8u> > >&, clang::FilenamePatternConstraint, clang::RecursiveCloneTypeIIHashConstraint, clang::MinGroupSizeConstraint, clang::MinComplexityConstraint, clang::RecursiveCloneTypeIIVerifyConstraint, clang::OnlyLargestCloneConstraint)
Line
Count
Source
196
28
                              Ts... ConstraintList) {
197
28
    constrainClones(CloneGroups, C);
198
28
    constrainClones(CloneGroups, ConstraintList...);
199
28
  }
void clang::CloneDetector::constrainClones<clang::RecursiveCloneTypeIIHashConstraint, clang::MinGroupSizeConstraint, clang::MinComplexityConstraint, clang::RecursiveCloneTypeIIVerifyConstraint, clang::OnlyLargestCloneConstraint>(std::__1::vector<llvm::SmallVector<clang::StmtSequence, 8u>, std::__1::allocator<llvm::SmallVector<clang::StmtSequence, 8u> > >&, clang::RecursiveCloneTypeIIHashConstraint, clang::MinGroupSizeConstraint, clang::MinComplexityConstraint, clang::RecursiveCloneTypeIIVerifyConstraint, clang::OnlyLargestCloneConstraint)
Line
Count
Source
196
28
                              Ts... ConstraintList) {
197
28
    constrainClones(CloneGroups, C);
198
28
    constrainClones(CloneGroups, ConstraintList...);
199
28
  }
void clang::CloneDetector::constrainClones<clang::MinGroupSizeConstraint, clang::MinComplexityConstraint, clang::RecursiveCloneTypeIIVerifyConstraint, clang::OnlyLargestCloneConstraint>(std::__1::vector<llvm::SmallVector<clang::StmtSequence, 8u>, std::__1::allocator<llvm::SmallVector<clang::StmtSequence, 8u> > >&, clang::MinGroupSizeConstraint, clang::MinComplexityConstraint, clang::RecursiveCloneTypeIIVerifyConstraint, clang::OnlyLargestCloneConstraint)
Line
Count
Source
196
28
                              Ts... ConstraintList) {
197
28
    constrainClones(CloneGroups, C);
198
28
    constrainClones(CloneGroups, ConstraintList...);
199
28
  }
void clang::CloneDetector::constrainClones<clang::MinComplexityConstraint, clang::RecursiveCloneTypeIIVerifyConstraint, clang::OnlyLargestCloneConstraint>(std::__1::vector<llvm::SmallVector<clang::StmtSequence, 8u>, std::__1::allocator<llvm::SmallVector<clang::StmtSequence, 8u> > >&, clang::MinComplexityConstraint, clang::RecursiveCloneTypeIIVerifyConstraint, clang::OnlyLargestCloneConstraint)
Line
Count
Source
196
28
                              Ts... ConstraintList) {
197
28
    constrainClones(CloneGroups, C);
198
28
    constrainClones(CloneGroups, ConstraintList...);
199
28
  }
void clang::CloneDetector::constrainClones<clang::RecursiveCloneTypeIIVerifyConstraint, clang::OnlyLargestCloneConstraint>(std::__1::vector<llvm::SmallVector<clang::StmtSequence, 8u>, std::__1::allocator<llvm::SmallVector<clang::StmtSequence, 8u> > >&, clang::RecursiveCloneTypeIIVerifyConstraint, clang::OnlyLargestCloneConstraint)
Line
Count
Source
196
30
                              Ts... ConstraintList) {
197
30
    constrainClones(CloneGroups, C);
198
30
    constrainClones(CloneGroups, ConstraintList...);
199
30
  }
void clang::CloneDetector::constrainClones<clang::MatchingVariablePatternConstraint, clang::MinGroupSizeConstraint>(std::__1::vector<llvm::SmallVector<clang::StmtSequence, 8u>, std::__1::allocator<llvm::SmallVector<clang::StmtSequence, 8u> > >&, clang::MatchingVariablePatternConstraint, clang::MinGroupSizeConstraint)
Line
Count
Source
196
27
                              Ts... ConstraintList) {
197
27
    constrainClones(CloneGroups, C);
198
27
    constrainClones(CloneGroups, ConstraintList...);
199
27
  }
200
201
  /// Searches for clones in all previously passed statements.
202
  /// \param Result Output parameter to which all created clone groups are
203
  ///               added.
204
  /// \param ConstraintList The constraints that should be applied to the
205
  //         result.
206
  template <typename... Ts>
207
28
  void findClones(std::vector<CloneGroup> &Result, Ts... ConstraintList) {
208
    // The initial assumption is that there is only one clone group and every
209
    // statement is a clone of the others. This clone group will then be
210
    // split up with the help of the constraints.
211
28
    CloneGroup AllClones;
212
28
    AllClones.reserve(Sequences.size());
213
93
    for (const auto &C : Sequences) {
214
93
      AllClones.push_back(C);
215
93
    }
216
217
28
    Result.push_back(AllClones);
218
219
28
    constrainClones(Result, ConstraintList...);
220
28
  }
221
222
private:
223
  CloneGroup Sequences;
224
};
225
226
/// This class is a utility class that contains utility functions for building
227
/// custom constraints.
228
class CloneConstraint {
229
public:
230
  /// Removes all groups by using a filter function.
231
  /// \param CloneGroups The list of CloneGroups that is supposed to be
232
  ///                    filtered.
233
  /// \param Filter The filter function that should return true for all groups
234
  ///               that should be removed from the list.
235
  static void filterGroups(
236
      std::vector<CloneDetector::CloneGroup> &CloneGroups,
237
115
      llvm::function_ref<bool(const CloneDetector::CloneGroup &)> Filter) {
238
115
    llvm::erase_if(CloneGroups, Filter);
239
115
  }
240
241
  /// Splits the given CloneGroups until the given Compare function returns true
242
  /// for all clones in a single group.
243
  /// \param CloneGroups A list of CloneGroups that should be modified.
244
  /// \param Compare The comparison function that all clones are supposed to
245
  ///                pass. Should return true if and only if two clones belong
246
  ///                to the same CloneGroup.
247
  static void splitCloneGroups(
248
      std::vector<CloneDetector::CloneGroup> &CloneGroups,
249
      llvm::function_ref<bool(const StmtSequence &, const StmtSequence &)>
250
          Compare);
251
};
252
253
/// This constraint moves clones into clone groups of type II via hashing.
254
///
255
/// Clones with different hash values are moved into separate clone groups.
256
/// Collisions are possible, and this constraint does nothing to address this
257
/// them. Add the slower RecursiveCloneTypeIIVerifyConstraint later in the
258
/// constraint chain, not necessarily immediately, to eliminate hash collisions
259
/// through a more detailed analysis.
260
class RecursiveCloneTypeIIHashConstraint {
261
public:
262
  void constrain(std::vector<CloneDetector::CloneGroup> &Sequences);
263
};
264
265
/// This constraint moves clones into clone groups of type II by comparing them.
266
///
267
/// Clones that aren't type II clones are moved into separate clone groups.
268
/// In contrast to the RecursiveCloneTypeIIHashConstraint, all clones in a clone
269
/// group are guaranteed to be be type II clones of each other, but it is too
270
/// slow to efficiently handle large amounts of clones.
271
class RecursiveCloneTypeIIVerifyConstraint {
272
public:
273
  void constrain(std::vector<CloneDetector::CloneGroup> &Sequences);
274
};
275
276
/// Ensures that every clone has at least the given complexity.
277
///
278
/// Complexity is here defined as the total amount of children of a statement.
279
/// This constraint assumes the first statement in the group is representative
280
/// for all other statements in the group in terms of complexity.
281
class MinComplexityConstraint {
282
  unsigned MinComplexity;
283
284
public:
285
  MinComplexityConstraint(unsigned MinComplexity)
286
30
      : MinComplexity(MinComplexity) {}
287
288
  /// Calculates the complexity of the given StmtSequence.
289
  /// \param Limit The limit of complexity we probe for. After reaching
290
  ///              this limit during calculation, this method is exiting
291
  ///              early to improve performance and returns this limit.
292
  size_t calculateStmtComplexity(const StmtSequence &Seq, std::size_t Limit,
293
                                 const std::string &ParentMacroStack = "");
294
295
30
  void constrain(std::vector<CloneDetector::CloneGroup> &CloneGroups) {
296
30
    CloneConstraint::filterGroups(
297
416
        CloneGroups, [this](const CloneDetector::CloneGroup &A) {
298
416
          if (!A.empty())
299
416
            return calculateStmtComplexity(A.front(), MinComplexity) <
300
416
                   MinComplexity;
301
0
          else
302
0
            return false;
303
416
        });
304
30
  }
305
};
306
307
/// Ensures that all clone groups contain at least the given amount of clones.
308
class MinGroupSizeConstraint {
309
  unsigned MinGroupSize;
310
311
public:
312
  MinGroupSizeConstraint(unsigned MinGroupSize = 2)
313
57
      : MinGroupSize(MinGroupSize) {}
314
315
57
  void constrain(std::vector<CloneDetector::CloneGroup> &CloneGroups) {
316
57
    CloneConstraint::filterGroups(CloneGroups,
317
703
                                  [this](const CloneDetector::CloneGroup &A) {
318
703
                                    return A.size() < MinGroupSize;
319
703
                                  });
320
57
  }
321
};
322
323
/// Ensures that no clone group fully contains another clone group.
324
struct OnlyLargestCloneConstraint {
325
  void constrain(std::vector<CloneDetector::CloneGroup> &Result);
326
};
327
328
struct FilenamePatternConstraint {
329
  StringRef IgnoredFilesPattern;
330
  std::shared_ptr<llvm::Regex> IgnoredFilesRegex;
331
332
  FilenamePatternConstraint(StringRef IgnoredFilesPattern)
333
28
      : IgnoredFilesPattern(IgnoredFilesPattern) {
334
28
    IgnoredFilesRegex = std::make_shared<llvm::Regex>("^(" +
335
28
        IgnoredFilesPattern.str() + "$)");
336
28
  }
337
338
  bool isAutoGenerated(const CloneDetector::CloneGroup &Group);
339
340
28
  void constrain(std::vector<CloneDetector::CloneGroup> &CloneGroups) {
341
28
    CloneConstraint::filterGroups(
342
28
        CloneGroups, [this](const CloneDetector::CloneGroup &Group) {
343
28
          return isAutoGenerated(Group);
344
28
        });
345
28
  }
346
};
347
348
/// Analyzes the pattern of the referenced variables in a statement.
349
class VariablePattern {
350
351
  /// Describes an occurrence of a variable reference in a statement.
352
  struct VariableOccurence {
353
    /// The index of the associated VarDecl in the Variables vector.
354
    size_t KindID;
355
    /// The statement in the code where the variable was referenced.
356
    const Stmt *Mention;
357
358
    VariableOccurence(size_t KindID, const Stmt *Mention)
359
549
        : KindID(KindID), Mention(Mention) {}
360
  };
361
362
  /// All occurrences of referenced variables in the order of appearance.
363
  std::vector<VariableOccurence> Occurences;
364
  /// List of referenced variables in the order of appearance.
365
  /// Every item in this list is unique.
366
  std::vector<const VarDecl *> Variables;
367
368
  /// Adds a new variable referenced to this pattern.
369
  /// \param VarDecl The declaration of the variable that is referenced.
370
  /// \param Mention The SourceRange where this variable is referenced.
371
  void addVariableOccurence(const VarDecl *VarDecl, const Stmt *Mention);
372
373
  /// Adds each referenced variable from the given statement.
374
  void addVariables(const Stmt *S);
375
376
public:
377
  /// Creates an VariablePattern object with information about the given
378
  /// StmtSequence.
379
109
  VariablePattern(const StmtSequence &Sequence) {
380
109
    for (const Stmt *S : Sequence)
381
119
      addVariables(S);
382
109
  }
383
384
  /// Describes two clones that reference their variables in a different pattern
385
  /// which could indicate a programming error.
386
  struct SuspiciousClonePair {
387
    /// Utility class holding the relevant information about a single
388
    /// clone in this pair.
389
    struct SuspiciousCloneInfo {
390
      /// The variable which referencing in this clone was against the pattern.
391
      const VarDecl *Variable;
392
      /// Where the variable was referenced.
393
      const Stmt *Mention;
394
      /// The variable that should have been referenced to follow the pattern.
395
      /// If Suggestion is a nullptr then it's not possible to fix the pattern
396
      /// by referencing a different variable in this clone.
397
      const VarDecl *Suggestion;
398
      SuspiciousCloneInfo(const VarDecl *Variable, const Stmt *Mention,
399
                          const VarDecl *Suggestion)
400
16
          : Variable(Variable), Mention(Mention), Suggestion(Suggestion) {}
401
52
      SuspiciousCloneInfo() {}
402
    };
403
    /// The first clone in the pair which always has a suggested variable.
404
    SuspiciousCloneInfo FirstCloneInfo;
405
    /// This other clone in the pair which can have a suggested variable.
406
    SuspiciousCloneInfo SecondCloneInfo;
407
  };
408
409
  /// Counts the differences between this pattern and the given one.
410
  /// \param Other The given VariablePattern to compare with.
411
  /// \param FirstMismatch Output parameter that will be filled with information
412
  ///        about the first difference between the two patterns. This parameter
413
  ///        can be a nullptr, in which case it will be ignored.
414
  /// \return Returns the number of differences between the pattern this object
415
  ///         is following and the given VariablePattern.
416
  ///
417
  /// For example, the following statements all have the same pattern and this
418
  /// function would return zero:
419
  ///
420
  ///   if (a < b) return a; return b;
421
  ///   if (x < y) return x; return y;
422
  ///   if (u2 < u1) return u2; return u1;
423
  ///
424
  /// But the following statement has a different pattern (note the changed
425
  /// variables in the return statements) and would have two differences when
426
  /// compared with one of the statements above.
427
  ///
428
  ///   if (a < b) return b; return a;
429
  ///
430
  /// This function should only be called if the related statements of the given
431
  /// pattern and the statements of this objects are clones of each other.
432
  unsigned countPatternDifferences(
433
      const VariablePattern &Other,
434
      VariablePattern::SuspiciousClonePair *FirstMismatch = nullptr);
435
};
436
437
/// Ensures that all clones reference variables in the same pattern.
438
struct MatchingVariablePatternConstraint {
439
  void constrain(std::vector<CloneDetector::CloneGroup> &CloneGroups);
440
};
441
442
} // end namespace clang
443
444
#endif // LLVM_CLANG_ANALYSIS_CLONEDETECTION_H