Index: cfe/trunk/lib/Analysis/CloneDetection.cpp =================================================================== --- cfe/trunk/lib/Analysis/CloneDetection.cpp +++ cfe/trunk/lib/Analysis/CloneDetection.cpp @@ -315,8 +315,26 @@ //--- Calls --------------------------------------------------------------// DEF_ADD_DATA(CallExpr, { // Function pointers don't have a callee and we just skip hashing it. - if (S->getDirectCallee()) - addData(S->getDirectCallee()->getQualifiedNameAsString()); + if (const FunctionDecl *D = S->getDirectCallee()) { + // If the function is a template instantiation, we also need to handle + // the template arguments as they are no included in the qualified name. + if (D->isTemplateInstantiation()) { + auto Args = D->getTemplateSpecializationArgs(); + std::string ArgString; + + // Print all template arguments into ArgString + llvm::raw_string_ostream OS(ArgString); + for (unsigned i = 0; i < Args->size(); ++i) { + Args->get(i).print(Context.getLangOpts(), OS); + // Add a padding character so that 'foo()' != 'foo()'. + OS << '\n'; + } + OS.flush(); + + addData(ArgString); + } + addData(D->getQualifiedNameAsString()); + } }) //--- Exceptions ---------------------------------------------------------// Index: cfe/trunk/test/Analysis/copypaste/call.cpp =================================================================== --- cfe/trunk/test/Analysis/copypaste/call.cpp +++ cfe/trunk/test/Analysis/copypaste/call.cpp @@ -34,3 +34,57 @@ return funcPtr(1); return true; } + +// Test that we respect the template arguments of function templates + +template +bool templateFunc() { unsigned i = N; return false; } + +bool fooTemplate1(int x) { + if (x > 0) + return false; + else if (x < 0) + return templateFunc(); + return true; +} + +bool fooTemplate2(int x) { + if (x > 0) + return false; + else if (x < 0) + return templateFunc(); + return true; +} + +bool fooTemplate3(int x) { + if (x > 0) + return false; + else if (x < 0) + return templateFunc(); + return true; +} + +// Test that we don't just concatenate the template arguments into a string +// without having any padding between them (e.g. foo() != foo()). + +class X {}; +class XX {}; + +template +bool templatePaddingFunc() { return false; } + +bool fooTemplatePadding1(int x) { + if (x > 0) + return false; + else if (x < 0) + return templatePaddingFunc(); + return true; +} + +bool fooTemplatePadding2(int x) { + if (x > 0) + return false; + else if (x < 0) + return templatePaddingFunc(); + return true; +}