Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 1 | //===--- CloneDetection.cpp - Finds code clones in an AST -------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | /// |
| 10 | /// This file implements classes for searching and anlyzing source code clones. |
| 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Analysis/CloneDetection.h" |
| 15 | |
| 16 | #include "clang/AST/ASTContext.h" |
| 17 | #include "clang/AST/RecursiveASTVisitor.h" |
| 18 | #include "clang/AST/Stmt.h" |
Artem Dergachev | 78692ea | 2016-08-02 12:21:09 +0000 | [diff] [blame] | 19 | #include "clang/AST/StmtVisitor.h" |
Artem Dergachev | 51b9a0e | 2016-08-20 10:06:59 +0000 | [diff] [blame^] | 20 | #include "clang/Lex/Lexer.h" |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/StringRef.h" |
Artem Dergachev | 51b9a0e | 2016-08-20 10:06:59 +0000 | [diff] [blame^] | 22 | #include "llvm/Support/raw_ostream.h" |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | StmtSequence::StmtSequence(const CompoundStmt *Stmt, ASTContext &Context, |
| 27 | unsigned StartIndex, unsigned EndIndex) |
| 28 | : S(Stmt), Context(&Context), StartIndex(StartIndex), EndIndex(EndIndex) { |
| 29 | assert(Stmt && "Stmt must not be a nullptr"); |
| 30 | assert(StartIndex < EndIndex && "Given array should not be empty"); |
| 31 | assert(EndIndex <= Stmt->size() && "Given array too big for this Stmt"); |
| 32 | } |
| 33 | |
| 34 | StmtSequence::StmtSequence(const Stmt *Stmt, ASTContext &Context) |
| 35 | : S(Stmt), Context(&Context), StartIndex(0), EndIndex(0) {} |
| 36 | |
| 37 | StmtSequence::StmtSequence() |
| 38 | : S(nullptr), Context(nullptr), StartIndex(0), EndIndex(0) {} |
| 39 | |
| 40 | bool StmtSequence::contains(const StmtSequence &Other) const { |
| 41 | // If both sequences reside in different translation units, they can never |
| 42 | // contain each other. |
| 43 | if (Context != Other.Context) |
| 44 | return false; |
| 45 | |
| 46 | const SourceManager &SM = Context->getSourceManager(); |
| 47 | |
| 48 | // Otherwise check if the start and end locations of the current sequence |
| 49 | // surround the other sequence. |
| 50 | bool StartIsInBounds = |
| 51 | SM.isBeforeInTranslationUnit(getStartLoc(), Other.getStartLoc()) || |
| 52 | getStartLoc() == Other.getStartLoc(); |
| 53 | if (!StartIsInBounds) |
| 54 | return false; |
| 55 | |
| 56 | bool EndIsInBounds = |
| 57 | SM.isBeforeInTranslationUnit(Other.getEndLoc(), getEndLoc()) || |
| 58 | Other.getEndLoc() == getEndLoc(); |
| 59 | return EndIsInBounds; |
| 60 | } |
| 61 | |
| 62 | StmtSequence::iterator StmtSequence::begin() const { |
| 63 | if (!holdsSequence()) { |
| 64 | return &S; |
| 65 | } |
| 66 | auto CS = cast<CompoundStmt>(S); |
| 67 | return CS->body_begin() + StartIndex; |
| 68 | } |
| 69 | |
| 70 | StmtSequence::iterator StmtSequence::end() const { |
| 71 | if (!holdsSequence()) { |
Vassil Vassilev | 5721e0f | 2016-08-09 10:00:23 +0000 | [diff] [blame] | 72 | return reinterpret_cast<StmtSequence::iterator>(&S) + 1; |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 73 | } |
| 74 | auto CS = cast<CompoundStmt>(S); |
| 75 | return CS->body_begin() + EndIndex; |
| 76 | } |
| 77 | |
| 78 | SourceLocation StmtSequence::getStartLoc() const { |
| 79 | return front()->getLocStart(); |
| 80 | } |
| 81 | |
| 82 | SourceLocation StmtSequence::getEndLoc() const { return back()->getLocEnd(); } |
| 83 | |
| 84 | namespace { |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 85 | |
| 86 | /// \brief Analyzes the pattern of the referenced variables in a statement. |
| 87 | class VariablePattern { |
| 88 | |
| 89 | /// \brief Describes an occurence of a variable reference in a statement. |
| 90 | struct VariableOccurence { |
| 91 | /// The index of the associated VarDecl in the Variables vector. |
| 92 | size_t KindID; |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 93 | /// The source range in the code where the variable was referenced. |
| 94 | SourceRange Range; |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 95 | |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 96 | VariableOccurence(size_t KindID, SourceRange Range) |
| 97 | : KindID(KindID), Range(Range) {} |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | /// All occurences of referenced variables in the order of appearance. |
| 101 | std::vector<VariableOccurence> Occurences; |
| 102 | /// List of referenced variables in the order of appearance. |
| 103 | /// Every item in this list is unique. |
| 104 | std::vector<const VarDecl *> Variables; |
| 105 | |
| 106 | /// \brief Adds a new variable referenced to this pattern. |
| 107 | /// \param VarDecl The declaration of the variable that is referenced. |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 108 | /// \param Range The SourceRange where this variable is referenced. |
| 109 | void addVariableOccurence(const VarDecl *VarDecl, SourceRange Range) { |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 110 | // First check if we already reference this variable |
| 111 | for (size_t KindIndex = 0; KindIndex < Variables.size(); ++KindIndex) { |
| 112 | if (Variables[KindIndex] == VarDecl) { |
| 113 | // If yes, add a new occurence that points to the existing entry in |
| 114 | // the Variables vector. |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 115 | Occurences.emplace_back(KindIndex, Range); |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 116 | return; |
| 117 | } |
| 118 | } |
| 119 | // If this variable wasn't already referenced, add it to the list of |
| 120 | // referenced variables and add a occurence that points to this new entry. |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 121 | Occurences.emplace_back(Variables.size(), Range); |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 122 | Variables.push_back(VarDecl); |
| 123 | } |
| 124 | |
| 125 | /// \brief Adds each referenced variable from the given statement. |
| 126 | void addVariables(const Stmt *S) { |
| 127 | // Sometimes we get a nullptr (such as from IfStmts which often have nullptr |
| 128 | // children). We skip such statements as they don't reference any |
| 129 | // variables. |
| 130 | if (!S) |
| 131 | return; |
| 132 | |
| 133 | // Check if S is a reference to a variable. If yes, add it to the pattern. |
| 134 | if (auto D = dyn_cast<DeclRefExpr>(S)) { |
| 135 | if (auto VD = dyn_cast<VarDecl>(D->getDecl()->getCanonicalDecl())) |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 136 | addVariableOccurence(VD, D->getSourceRange()); |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | // Recursively check all children of the given statement. |
| 140 | for (const Stmt *Child : S->children()) { |
| 141 | addVariables(Child); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | public: |
| 146 | /// \brief Creates an VariablePattern object with information about the given |
| 147 | /// StmtSequence. |
| 148 | VariablePattern(const StmtSequence &Sequence) { |
| 149 | for (const Stmt *S : Sequence) |
| 150 | addVariables(S); |
| 151 | } |
| 152 | |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 153 | /// \brief Counts the differences between this pattern and the given one. |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 154 | /// \param Other The given VariablePattern to compare with. |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 155 | /// \param FirstMismatch Output parameter that will be filled with information |
| 156 | /// about the first difference between the two patterns. This parameter |
| 157 | /// can be a nullptr, in which case it will be ignored. |
| 158 | /// \return Returns the number of differences between the pattern this object |
| 159 | /// is following and the given VariablePattern. |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 160 | /// |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 161 | /// For example, the following statements all have the same pattern and this |
| 162 | /// function would return zero: |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 163 | /// |
| 164 | /// if (a < b) return a; return b; |
| 165 | /// if (x < y) return x; return y; |
| 166 | /// if (u2 < u1) return u2; return u1; |
| 167 | /// |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 168 | /// But the following statement has a different pattern (note the changed |
| 169 | /// variables in the return statements) and would have two differences when |
| 170 | /// compared with one of the statements above. |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 171 | /// |
| 172 | /// if (a < b) return b; return a; |
| 173 | /// |
| 174 | /// This function should only be called if the related statements of the given |
| 175 | /// pattern and the statements of this objects are clones of each other. |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 176 | unsigned countPatternDifferences( |
| 177 | const VariablePattern &Other, |
| 178 | CloneDetector::SuspiciousClonePair *FirstMismatch = nullptr) { |
| 179 | unsigned NumberOfDifferences = 0; |
| 180 | |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 181 | assert(Other.Occurences.size() == Occurences.size()); |
| 182 | for (unsigned i = 0; i < Occurences.size(); ++i) { |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 183 | auto ThisOccurence = Occurences[i]; |
| 184 | auto OtherOccurence = Other.Occurences[i]; |
| 185 | if (ThisOccurence.KindID == OtherOccurence.KindID) |
| 186 | continue; |
| 187 | |
| 188 | ++NumberOfDifferences; |
| 189 | |
| 190 | // If FirstMismatch is not a nullptr, we need to store information about |
| 191 | // the first difference between the two patterns. |
| 192 | if (FirstMismatch == nullptr) |
| 193 | continue; |
| 194 | |
| 195 | // Only proceed if we just found the first difference as we only store |
| 196 | // information about the first difference. |
| 197 | if (NumberOfDifferences != 1) |
| 198 | continue; |
| 199 | |
| 200 | const VarDecl *FirstSuggestion = nullptr; |
| 201 | // If there is a variable available in the list of referenced variables |
| 202 | // which wouldn't break the pattern if it is used in place of the |
| 203 | // current variable, we provide this variable as the suggested fix. |
| 204 | if (OtherOccurence.KindID < Variables.size()) |
| 205 | FirstSuggestion = Variables[OtherOccurence.KindID]; |
| 206 | |
| 207 | // Store information about the first clone. |
| 208 | FirstMismatch->FirstCloneInfo = |
| 209 | CloneDetector::SuspiciousClonePair::SuspiciousCloneInfo( |
| 210 | Variables[ThisOccurence.KindID], ThisOccurence.Range, |
| 211 | FirstSuggestion); |
| 212 | |
| 213 | // Same as above but with the other clone. We do this for both clones as |
| 214 | // we don't know which clone is the one containing the unintended |
| 215 | // pattern error. |
| 216 | const VarDecl *SecondSuggestion = nullptr; |
| 217 | if (ThisOccurence.KindID < Other.Variables.size()) |
| 218 | SecondSuggestion = Other.Variables[ThisOccurence.KindID]; |
| 219 | |
| 220 | // Store information about the second clone. |
| 221 | FirstMismatch->SecondCloneInfo = |
| 222 | CloneDetector::SuspiciousClonePair::SuspiciousCloneInfo( |
| 223 | Variables[ThisOccurence.KindID], OtherOccurence.Range, |
| 224 | SecondSuggestion); |
| 225 | |
| 226 | // SuspiciousClonePair guarantees that the first clone always has a |
| 227 | // suggested variable associated with it. As we know that one of the two |
| 228 | // clones in the pair always has suggestion, we swap the two clones |
| 229 | // in case the first clone has no suggested variable which means that |
| 230 | // the second clone has a suggested variable and should be first. |
| 231 | if (!FirstMismatch->FirstCloneInfo.Suggestion) |
| 232 | std::swap(FirstMismatch->FirstCloneInfo, |
| 233 | FirstMismatch->SecondCloneInfo); |
| 234 | |
| 235 | // This ensures that we always have at least one suggestion in a pair. |
| 236 | assert(FirstMismatch->FirstCloneInfo.Suggestion); |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 237 | } |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 238 | |
| 239 | return NumberOfDifferences; |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 240 | } |
| 241 | }; |
| 242 | } |
| 243 | |
Artem Dergachev | 51b9a0e | 2016-08-20 10:06:59 +0000 | [diff] [blame^] | 244 | /// \brief Prints the macro name that contains the given SourceLocation into |
| 245 | /// the given raw_string_ostream. |
| 246 | static void printMacroName(llvm::raw_string_ostream &MacroStack, |
| 247 | ASTContext &Context, SourceLocation Loc) { |
| 248 | MacroStack << Lexer::getImmediateMacroName(Loc, Context.getSourceManager(), |
| 249 | Context.getLangOpts()); |
| 250 | |
| 251 | // Add an empty space at the end as a padding to prevent |
| 252 | // that macro names concatenate to the names of other macros. |
| 253 | MacroStack << " "; |
| 254 | } |
| 255 | |
| 256 | /// \brief Returns a string that represents all macro expansions that |
| 257 | /// expanded into the given SourceLocation. |
| 258 | /// |
| 259 | /// If 'getMacroStack(A) == getMacroStack(B)' is true, then the SourceLocations |
| 260 | /// A and B are expanded from the same macros in the same order. |
| 261 | static std::string getMacroStack(SourceLocation Loc, ASTContext &Context) { |
| 262 | std::string MacroStack; |
| 263 | llvm::raw_string_ostream MacroStackStream(MacroStack); |
| 264 | SourceManager &SM = Context.getSourceManager(); |
| 265 | |
| 266 | // Iterate over all macros that expanded into the given SourceLocation. |
| 267 | while (Loc.isMacroID()) { |
| 268 | // Add the macro name to the stream. |
| 269 | printMacroName(MacroStackStream, Context, Loc); |
| 270 | Loc = SM.getImmediateMacroCallerLoc(Loc); |
| 271 | } |
| 272 | MacroStackStream.flush(); |
| 273 | return MacroStack; |
| 274 | } |
| 275 | |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 276 | namespace { |
Artem Dergachev | 78692ea | 2016-08-02 12:21:09 +0000 | [diff] [blame] | 277 | /// \brief Collects the data of a single Stmt. |
| 278 | /// |
| 279 | /// This class defines what a code clone is: If it collects for two statements |
| 280 | /// the same data, then those two statements are considered to be clones of each |
| 281 | /// other. |
| 282 | class StmtDataCollector : public ConstStmtVisitor<StmtDataCollector> { |
| 283 | |
| 284 | ASTContext &Context; |
| 285 | std::vector<CloneDetector::DataPiece> &CollectedData; |
| 286 | |
| 287 | public: |
| 288 | /// \brief Collects data of the given Stmt. |
| 289 | /// \param S The given statement. |
| 290 | /// \param Context The ASTContext of S. |
| 291 | /// \param D The given data vector to which all collected data is appended. |
| 292 | StmtDataCollector(const Stmt *S, ASTContext &Context, |
| 293 | std::vector<CloneDetector::DataPiece> &D) |
| 294 | : Context(Context), CollectedData(D) { |
| 295 | Visit(S); |
| 296 | } |
| 297 | |
| 298 | // Below are utility methods for appending different data to the vector. |
| 299 | |
| 300 | void addData(CloneDetector::DataPiece Integer) { |
| 301 | CollectedData.push_back(Integer); |
| 302 | } |
| 303 | |
| 304 | // FIXME: The functions below add long strings to the data vector which are |
| 305 | // probably not good for performance. Replace the strings with pointer values |
| 306 | // or a some other unique integer. |
| 307 | |
| 308 | void addData(llvm::StringRef Str) { |
| 309 | if (Str.empty()) |
| 310 | return; |
| 311 | |
| 312 | const size_t OldSize = CollectedData.size(); |
| 313 | |
| 314 | const size_t PieceSize = sizeof(CloneDetector::DataPiece); |
| 315 | // Calculate how many vector units we need to accomodate all string bytes. |
| 316 | size_t RoundedUpPieceNumber = (Str.size() + PieceSize - 1) / PieceSize; |
| 317 | // Allocate space for the string in the data vector. |
| 318 | CollectedData.resize(CollectedData.size() + RoundedUpPieceNumber); |
| 319 | |
| 320 | // Copy the string to the allocated space at the end of the vector. |
| 321 | std::memcpy(CollectedData.data() + OldSize, Str.data(), Str.size()); |
| 322 | } |
| 323 | |
| 324 | void addData(const QualType &QT) { addData(QT.getAsString()); } |
| 325 | |
| 326 | // The functions below collect the class specific data of each Stmt subclass. |
| 327 | |
| 328 | // Utility macro for defining a visit method for a given class. This method |
| 329 | // calls back to the ConstStmtVisitor to visit all parent classes. |
| 330 | #define DEF_ADD_DATA(CLASS, CODE) \ |
| 331 | void Visit##CLASS(const CLASS *S) { \ |
| 332 | CODE; \ |
| 333 | ConstStmtVisitor<StmtDataCollector>::Visit##CLASS(S); \ |
| 334 | } |
| 335 | |
Artem Dergachev | 51b9a0e | 2016-08-20 10:06:59 +0000 | [diff] [blame^] | 336 | DEF_ADD_DATA(Stmt, { |
| 337 | addData(S->getStmtClass()); |
| 338 | // This ensures that macro generated code isn't identical to macro-generated |
| 339 | // code. |
| 340 | addData(getMacroStack(S->getLocStart(), Context)); |
| 341 | addData(getMacroStack(S->getLocEnd(), Context)); |
| 342 | }) |
Artem Dergachev | 78692ea | 2016-08-02 12:21:09 +0000 | [diff] [blame] | 343 | DEF_ADD_DATA(Expr, { addData(S->getType()); }) |
| 344 | |
| 345 | //--- Builtin functionality ----------------------------------------------// |
| 346 | DEF_ADD_DATA(ArrayTypeTraitExpr, { addData(S->getTrait()); }) |
| 347 | DEF_ADD_DATA(ExpressionTraitExpr, { addData(S->getTrait()); }) |
| 348 | DEF_ADD_DATA(PredefinedExpr, { addData(S->getIdentType()); }) |
| 349 | DEF_ADD_DATA(TypeTraitExpr, { |
| 350 | addData(S->getTrait()); |
| 351 | for (unsigned i = 0; i < S->getNumArgs(); ++i) |
| 352 | addData(S->getArg(i)->getType()); |
| 353 | }) |
| 354 | |
| 355 | //--- Calls --------------------------------------------------------------// |
Artem Dergachev | cad1514 | 2016-08-10 16:25:16 +0000 | [diff] [blame] | 356 | DEF_ADD_DATA(CallExpr, { |
| 357 | // Function pointers don't have a callee and we just skip hashing it. |
Artem Dergachev | 5183888 | 2016-08-20 09:57:21 +0000 | [diff] [blame] | 358 | if (const FunctionDecl *D = S->getDirectCallee()) { |
| 359 | // If the function is a template instantiation, we also need to handle |
| 360 | // the template arguments as they are no included in the qualified name. |
| 361 | if (D->isTemplateInstantiation()) { |
| 362 | auto Args = D->getTemplateSpecializationArgs(); |
| 363 | std::string ArgString; |
| 364 | |
| 365 | // Print all template arguments into ArgString |
| 366 | llvm::raw_string_ostream OS(ArgString); |
| 367 | for (unsigned i = 0; i < Args->size(); ++i) { |
| 368 | Args->get(i).print(Context.getLangOpts(), OS); |
| 369 | // Add a padding character so that 'foo<X, XX>()' != 'foo<XX, X>()'. |
| 370 | OS << '\n'; |
| 371 | } |
| 372 | OS.flush(); |
| 373 | |
| 374 | addData(ArgString); |
| 375 | } |
| 376 | addData(D->getQualifiedNameAsString()); |
| 377 | } |
Artem Dergachev | cad1514 | 2016-08-10 16:25:16 +0000 | [diff] [blame] | 378 | }) |
Artem Dergachev | 78692ea | 2016-08-02 12:21:09 +0000 | [diff] [blame] | 379 | |
| 380 | //--- Exceptions ---------------------------------------------------------// |
| 381 | DEF_ADD_DATA(CXXCatchStmt, { addData(S->getCaughtType()); }) |
| 382 | |
| 383 | //--- C++ OOP Stmts ------------------------------------------------------// |
| 384 | DEF_ADD_DATA(CXXDeleteExpr, { |
| 385 | addData(S->isArrayFormAsWritten()); |
| 386 | addData(S->isGlobalDelete()); |
| 387 | }) |
| 388 | |
| 389 | //--- Casts --------------------------------------------------------------// |
| 390 | DEF_ADD_DATA(ObjCBridgedCastExpr, { addData(S->getBridgeKind()); }) |
| 391 | |
| 392 | //--- Miscellaneous Exprs ------------------------------------------------// |
| 393 | DEF_ADD_DATA(BinaryOperator, { addData(S->getOpcode()); }) |
| 394 | DEF_ADD_DATA(UnaryOperator, { addData(S->getOpcode()); }) |
| 395 | |
| 396 | //--- Control flow -------------------------------------------------------// |
| 397 | DEF_ADD_DATA(GotoStmt, { addData(S->getLabel()->getName()); }) |
| 398 | DEF_ADD_DATA(IndirectGotoStmt, { |
| 399 | if (S->getConstantTarget()) |
| 400 | addData(S->getConstantTarget()->getName()); |
| 401 | }) |
| 402 | DEF_ADD_DATA(LabelStmt, { addData(S->getDecl()->getName()); }) |
| 403 | DEF_ADD_DATA(MSDependentExistsStmt, { addData(S->isIfExists()); }) |
| 404 | DEF_ADD_DATA(AddrLabelExpr, { addData(S->getLabel()->getName()); }) |
| 405 | |
| 406 | //--- Objective-C --------------------------------------------------------// |
| 407 | DEF_ADD_DATA(ObjCIndirectCopyRestoreExpr, { addData(S->shouldCopy()); }) |
| 408 | DEF_ADD_DATA(ObjCPropertyRefExpr, { |
| 409 | addData(S->isSuperReceiver()); |
| 410 | addData(S->isImplicitProperty()); |
| 411 | }) |
| 412 | DEF_ADD_DATA(ObjCAtCatchStmt, { addData(S->hasEllipsis()); }) |
| 413 | |
| 414 | //--- Miscellaneous Stmts ------------------------------------------------// |
| 415 | DEF_ADD_DATA(CXXFoldExpr, { |
| 416 | addData(S->isRightFold()); |
| 417 | addData(S->getOperator()); |
| 418 | }) |
| 419 | DEF_ADD_DATA(GenericSelectionExpr, { |
| 420 | for (unsigned i = 0; i < S->getNumAssocs(); ++i) { |
| 421 | addData(S->getAssocType(i)); |
| 422 | } |
| 423 | }) |
| 424 | DEF_ADD_DATA(LambdaExpr, { |
| 425 | for (const LambdaCapture &C : S->captures()) { |
| 426 | addData(C.isPackExpansion()); |
| 427 | addData(C.getCaptureKind()); |
| 428 | if (C.capturesVariable()) |
| 429 | addData(C.getCapturedVar()->getType()); |
| 430 | } |
| 431 | addData(S->isGenericLambda()); |
| 432 | addData(S->isMutable()); |
| 433 | }) |
| 434 | DEF_ADD_DATA(DeclStmt, { |
| 435 | auto numDecls = std::distance(S->decl_begin(), S->decl_end()); |
| 436 | addData(static_cast<CloneDetector::DataPiece>(numDecls)); |
| 437 | for (const Decl *D : S->decls()) { |
| 438 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 439 | addData(VD->getType()); |
| 440 | } |
| 441 | } |
| 442 | }) |
| 443 | DEF_ADD_DATA(AsmStmt, { |
| 444 | addData(S->isSimple()); |
| 445 | addData(S->isVolatile()); |
| 446 | addData(S->generateAsmString(Context)); |
| 447 | for (unsigned i = 0; i < S->getNumInputs(); ++i) { |
| 448 | addData(S->getInputConstraint(i)); |
| 449 | } |
| 450 | for (unsigned i = 0; i < S->getNumOutputs(); ++i) { |
| 451 | addData(S->getOutputConstraint(i)); |
| 452 | } |
| 453 | for (unsigned i = 0; i < S->getNumClobbers(); ++i) { |
| 454 | addData(S->getClobber(i)); |
| 455 | } |
| 456 | }) |
| 457 | DEF_ADD_DATA(AttributedStmt, { |
| 458 | for (const Attr *A : S->getAttrs()) { |
| 459 | addData(std::string(A->getSpelling())); |
| 460 | } |
| 461 | }) |
| 462 | }; |
| 463 | } // end anonymous namespace |
| 464 | |
| 465 | namespace { |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 466 | /// Generates CloneSignatures for a set of statements and stores the results in |
| 467 | /// a CloneDetector object. |
| 468 | class CloneSignatureGenerator { |
| 469 | |
| 470 | CloneDetector &CD; |
| 471 | ASTContext &Context; |
| 472 | |
| 473 | /// \brief Generates CloneSignatures for all statements in the given statement |
| 474 | /// tree and stores them in the CloneDetector. |
| 475 | /// |
| 476 | /// \param S The root of the given statement tree. |
Artem Dergachev | 51b9a0e | 2016-08-20 10:06:59 +0000 | [diff] [blame^] | 477 | /// \param ParentMacroStack A string representing the macros that generated |
| 478 | /// the parent statement or an empty string if no |
| 479 | /// macros generated the parent statement. |
| 480 | /// See getMacroStack() for generating such a string. |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 481 | /// \return The CloneSignature of the root statement. |
Artem Dergachev | 51b9a0e | 2016-08-20 10:06:59 +0000 | [diff] [blame^] | 482 | CloneDetector::CloneSignature |
| 483 | generateSignatures(const Stmt *S, const std::string &ParentMacroStack) { |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 484 | // Create an empty signature that will be filled in this method. |
| 485 | CloneDetector::CloneSignature Signature; |
| 486 | |
Artem Dergachev | 78692ea | 2016-08-02 12:21:09 +0000 | [diff] [blame] | 487 | // Collect all relevant data from S and put it into the empty signature. |
| 488 | StmtDataCollector(S, Context, Signature.Data); |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 489 | |
Artem Dergachev | 51b9a0e | 2016-08-20 10:06:59 +0000 | [diff] [blame^] | 490 | // Look up what macros expanded into the current statement. |
| 491 | std::string StartMacroStack = getMacroStack(S->getLocStart(), Context); |
| 492 | std::string EndMacroStack = getMacroStack(S->getLocEnd(), Context); |
| 493 | |
| 494 | // First, check if ParentMacroStack is not empty which means we are currently |
| 495 | // dealing with a parent statement which was expanded from a macro. |
| 496 | // If this parent statement was expanded from the same macros as this |
| 497 | // statement, we reduce the initial complexity of this statement to zero. |
| 498 | // This causes that a group of statements that were generated by a single |
| 499 | // macro expansion will only increase the total complexity by one. |
| 500 | // Note: This is not the final complexity of this statement as we still |
| 501 | // add the complexity of the child statements to the complexity value. |
| 502 | if (!ParentMacroStack.empty() && (StartMacroStack == ParentMacroStack && |
| 503 | EndMacroStack == ParentMacroStack)) { |
| 504 | Signature.Complexity = 0; |
| 505 | } |
| 506 | |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 507 | // Storage for the signatures of the direct child statements. This is only |
| 508 | // needed if the current statement is a CompoundStmt. |
| 509 | std::vector<CloneDetector::CloneSignature> ChildSignatures; |
| 510 | const CompoundStmt *CS = dyn_cast<const CompoundStmt>(S); |
| 511 | |
| 512 | // The signature of a statement includes the signatures of its children. |
| 513 | // Therefore we create the signatures for every child and add them to the |
| 514 | // current signature. |
| 515 | for (const Stmt *Child : S->children()) { |
| 516 | // Some statements like 'if' can have nullptr children that we will skip. |
| 517 | if (!Child) |
| 518 | continue; |
| 519 | |
| 520 | // Recursive call to create the signature of the child statement. This |
| 521 | // will also create and store all clone groups in this child statement. |
Artem Dergachev | 51b9a0e | 2016-08-20 10:06:59 +0000 | [diff] [blame^] | 522 | // We pass only the StartMacroStack along to keep things simple. |
| 523 | auto ChildSignature = generateSignatures(Child, StartMacroStack); |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 524 | |
| 525 | // Add the collected data to the signature of the current statement. |
| 526 | Signature.add(ChildSignature); |
| 527 | |
| 528 | // If the current statement is a CompoundStatement, we need to store the |
| 529 | // signature for the generation of the sub-sequences. |
| 530 | if (CS) |
| 531 | ChildSignatures.push_back(ChildSignature); |
| 532 | } |
| 533 | |
| 534 | // If the current statement is a CompoundStmt, we also need to create the |
| 535 | // clone groups from the sub-sequences inside the children. |
| 536 | if (CS) |
| 537 | handleSubSequences(CS, ChildSignatures); |
| 538 | |
| 539 | // Save the signature for the current statement in the CloneDetector object. |
| 540 | CD.add(StmtSequence(S, Context), Signature); |
| 541 | |
| 542 | return Signature; |
| 543 | } |
| 544 | |
| 545 | /// \brief Adds all possible sub-sequences in the child array of the given |
| 546 | /// CompoundStmt to the CloneDetector. |
| 547 | /// \param CS The given CompoundStmt. |
| 548 | /// \param ChildSignatures A list of calculated signatures for each child in |
| 549 | /// the given CompoundStmt. |
| 550 | void handleSubSequences( |
| 551 | const CompoundStmt *CS, |
| 552 | const std::vector<CloneDetector::CloneSignature> &ChildSignatures) { |
| 553 | |
| 554 | // FIXME: This function has quadratic runtime right now. Check if skipping |
| 555 | // this function for too long CompoundStmts is an option. |
| 556 | |
| 557 | // The length of the sub-sequence. We don't need to handle sequences with |
| 558 | // the length 1 as they are already handled in CollectData(). |
| 559 | for (unsigned Length = 2; Length <= CS->size(); ++Length) { |
| 560 | // The start index in the body of the CompoundStmt. We increase the |
| 561 | // position until the end of the sub-sequence reaches the end of the |
| 562 | // CompoundStmt body. |
| 563 | for (unsigned Pos = 0; Pos <= CS->size() - Length; ++Pos) { |
| 564 | // Create an empty signature and add the signatures of all selected |
| 565 | // child statements to it. |
| 566 | CloneDetector::CloneSignature SubSignature; |
| 567 | |
| 568 | for (unsigned i = Pos; i < Pos + Length; ++i) { |
| 569 | SubSignature.add(ChildSignatures[i]); |
| 570 | } |
| 571 | |
| 572 | // Save the signature together with the information about what children |
| 573 | // sequence we selected. |
| 574 | CD.add(StmtSequence(CS, Context, Pos, Pos + Length), SubSignature); |
| 575 | } |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | public: |
| 580 | explicit CloneSignatureGenerator(CloneDetector &CD, ASTContext &Context) |
| 581 | : CD(CD), Context(Context) {} |
| 582 | |
| 583 | /// \brief Generates signatures for all statements in the given function body. |
Artem Dergachev | 51b9a0e | 2016-08-20 10:06:59 +0000 | [diff] [blame^] | 584 | void consumeCodeBody(const Stmt *S) { generateSignatures(S, ""); } |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 585 | }; |
| 586 | } // end anonymous namespace |
| 587 | |
| 588 | void CloneDetector::analyzeCodeBody(const Decl *D) { |
| 589 | assert(D); |
| 590 | assert(D->hasBody()); |
| 591 | CloneSignatureGenerator Generator(*this, D->getASTContext()); |
| 592 | Generator.consumeCodeBody(D->getBody()); |
| 593 | } |
| 594 | |
| 595 | void CloneDetector::add(const StmtSequence &S, |
| 596 | const CloneSignature &Signature) { |
| 597 | // StringMap only works with StringRefs, so we create one for our data vector. |
| 598 | auto &Data = Signature.Data; |
| 599 | StringRef DataRef = StringRef(reinterpret_cast<const char *>(Data.data()), |
| 600 | Data.size() * sizeof(unsigned)); |
| 601 | |
| 602 | // Search with the help of the signature if we already have encountered a |
| 603 | // clone of the given StmtSequence. |
| 604 | auto I = CloneGroupIndexes.find(DataRef); |
| 605 | if (I == CloneGroupIndexes.end()) { |
| 606 | // We haven't found an existing clone group, so we create a new clone group |
| 607 | // for this StmtSequence and store the index of it in our search map. |
| 608 | CloneGroupIndexes[DataRef] = CloneGroups.size(); |
| 609 | CloneGroups.emplace_back(S, Signature.Complexity); |
| 610 | return; |
| 611 | } |
| 612 | |
| 613 | // We have found an existing clone group and can expand it with the given |
| 614 | // StmtSequence. |
| 615 | CloneGroups[I->getValue()].Sequences.push_back(S); |
| 616 | } |
| 617 | |
| 618 | namespace { |
| 619 | /// \brief Returns true if and only if \p Stmt contains at least one other |
| 620 | /// sequence in the \p Group. |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 621 | bool containsAnyInGroup(StmtSequence &Stmt, CloneDetector::CloneGroup &Group) { |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 622 | for (StmtSequence &GroupStmt : Group.Sequences) { |
| 623 | if (Stmt.contains(GroupStmt)) |
| 624 | return true; |
| 625 | } |
| 626 | return false; |
| 627 | } |
| 628 | |
| 629 | /// \brief Returns true if and only if all sequences in \p OtherGroup are |
| 630 | /// contained by a sequence in \p Group. |
| 631 | bool containsGroup(CloneDetector::CloneGroup &Group, |
| 632 | CloneDetector::CloneGroup &OtherGroup) { |
| 633 | // We have less sequences in the current group than we have in the other, |
| 634 | // so we will never fulfill the requirement for returning true. This is only |
| 635 | // possible because we know that a sequence in Group can contain at most |
| 636 | // one sequence in OtherGroup. |
| 637 | if (Group.Sequences.size() < OtherGroup.Sequences.size()) |
| 638 | return false; |
| 639 | |
| 640 | for (StmtSequence &Stmt : Group.Sequences) { |
| 641 | if (!containsAnyInGroup(Stmt, OtherGroup)) |
| 642 | return false; |
| 643 | } |
| 644 | return true; |
| 645 | } |
| 646 | } // end anonymous namespace |
| 647 | |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 648 | /// \brief Finds all actual clone groups in a single group of presumed clones. |
| 649 | /// \param Result Output parameter to which all found groups are added. Every |
| 650 | /// clone in a group that was added this way follows the same |
| 651 | /// variable pattern as the other clones in its group. |
| 652 | /// \param Group A group of clones. The clones are allowed to have a different |
| 653 | /// variable pattern. |
| 654 | static void createCloneGroups(std::vector<CloneDetector::CloneGroup> &Result, |
| 655 | const CloneDetector::CloneGroup &Group) { |
| 656 | // We remove the Sequences one by one, so a list is more appropriate. |
| 657 | std::list<StmtSequence> UnassignedSequences(Group.Sequences.begin(), |
| 658 | Group.Sequences.end()); |
| 659 | |
| 660 | // Search for clones as long as there could be clones in UnassignedSequences. |
| 661 | while (UnassignedSequences.size() > 1) { |
| 662 | |
| 663 | // Pick the first Sequence as a protoype for a new clone group. |
| 664 | StmtSequence Prototype = UnassignedSequences.front(); |
| 665 | UnassignedSequences.pop_front(); |
| 666 | |
| 667 | CloneDetector::CloneGroup FilteredGroup(Prototype, Group.Complexity); |
| 668 | |
| 669 | // Analyze the variable pattern of the prototype. Every other StmtSequence |
| 670 | // needs to have the same pattern to get into the new clone group. |
| 671 | VariablePattern PrototypeFeatures(Prototype); |
| 672 | |
| 673 | // Search all remaining StmtSequences for an identical variable pattern |
| 674 | // and assign them to our new clone group. |
| 675 | auto I = UnassignedSequences.begin(), E = UnassignedSequences.end(); |
| 676 | while (I != E) { |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 677 | |
| 678 | if (VariablePattern(*I).countPatternDifferences(PrototypeFeatures) == 0) { |
Artem Dergachev | 7a0088b | 2016-08-04 19:37:00 +0000 | [diff] [blame] | 679 | FilteredGroup.Sequences.push_back(*I); |
| 680 | I = UnassignedSequences.erase(I); |
| 681 | continue; |
| 682 | } |
| 683 | ++I; |
| 684 | } |
| 685 | |
| 686 | // Add a valid clone group to the list of found clone groups. |
| 687 | if (!FilteredGroup.isValid()) |
| 688 | continue; |
| 689 | |
| 690 | Result.push_back(FilteredGroup); |
| 691 | } |
| 692 | } |
| 693 | |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 694 | void CloneDetector::findClones(std::vector<CloneGroup> &Result, |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 695 | unsigned MinGroupComplexity, |
| 696 | bool CheckPatterns) { |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 697 | // Add every valid clone group that fulfills the complexity requirement. |
| 698 | for (const CloneGroup &Group : CloneGroups) { |
| 699 | if (Group.isValid() && Group.Complexity >= MinGroupComplexity) { |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 700 | if (CheckPatterns) |
| 701 | createCloneGroups(Result, Group); |
| 702 | else |
| 703 | Result.push_back(Group); |
Artem Dergachev | ba81632 | 2016-07-26 18:13:12 +0000 | [diff] [blame] | 704 | } |
| 705 | } |
| 706 | |
| 707 | std::vector<unsigned> IndexesToRemove; |
| 708 | |
| 709 | // Compare every group in the result with the rest. If one groups contains |
| 710 | // another group, we only need to return the bigger group. |
| 711 | // Note: This doesn't scale well, so if possible avoid calling any heavy |
| 712 | // function from this loop to minimize the performance impact. |
| 713 | for (unsigned i = 0; i < Result.size(); ++i) { |
| 714 | for (unsigned j = 0; j < Result.size(); ++j) { |
| 715 | // Don't compare a group with itself. |
| 716 | if (i == j) |
| 717 | continue; |
| 718 | |
| 719 | if (containsGroup(Result[j], Result[i])) { |
| 720 | IndexesToRemove.push_back(i); |
| 721 | break; |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | // Erasing a list of indexes from the vector should be done with decreasing |
| 727 | // indexes. As IndexesToRemove is constructed with increasing values, we just |
| 728 | // reverse iterate over it to get the desired order. |
| 729 | for (auto I = IndexesToRemove.rbegin(); I != IndexesToRemove.rend(); ++I) { |
| 730 | Result.erase(Result.begin() + *I); |
| 731 | } |
| 732 | } |
Artem Dergachev | 2fc1985 | 2016-08-18 12:29:41 +0000 | [diff] [blame] | 733 | |
| 734 | void CloneDetector::findSuspiciousClones( |
| 735 | std::vector<CloneDetector::SuspiciousClonePair> &Result, |
| 736 | unsigned MinGroupComplexity) { |
| 737 | std::vector<CloneGroup> Clones; |
| 738 | // Reuse the normal search for clones but specify that the clone groups don't |
| 739 | // need to have a common referenced variable pattern so that we can manually |
| 740 | // search for the kind of pattern errors this function is supposed to find. |
| 741 | findClones(Clones, MinGroupComplexity, false); |
| 742 | |
| 743 | for (const CloneGroup &Group : Clones) { |
| 744 | for (unsigned i = 0; i < Group.Sequences.size(); ++i) { |
| 745 | VariablePattern PatternA(Group.Sequences[i]); |
| 746 | |
| 747 | for (unsigned j = i + 1; j < Group.Sequences.size(); ++j) { |
| 748 | VariablePattern PatternB(Group.Sequences[j]); |
| 749 | |
| 750 | CloneDetector::SuspiciousClonePair ClonePair; |
| 751 | // For now, we only report clones which break the variable pattern just |
| 752 | // once because multiple differences in a pattern are an indicator that |
| 753 | // those differences are maybe intended (e.g. because it's actually |
| 754 | // a different algorithm). |
| 755 | // TODO: In very big clones even multiple variables can be unintended, |
| 756 | // so replacing this number with a percentage could better handle such |
| 757 | // cases. On the other hand it could increase the false-positive rate |
| 758 | // for all clones if the percentage is too high. |
| 759 | if (PatternA.countPatternDifferences(PatternB, &ClonePair) == 1) { |
| 760 | Result.push_back(ClonePair); |
| 761 | break; |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | } |
| 766 | } |