From: Artem Dergachev Date: Sat, 20 Aug 2016 09:57:21 +0000 (+0000) Subject: [analyzer] Make CloneDetector consider template arguments. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5a3db3cf93d33f4082f33f8d90eefdc63d37446a;p=clang [analyzer] Make CloneDetector consider template arguments. For example, code samples `isa(S)' and `isa(S)' are no longer considered to be clones. Patch by Raphael Isemann! Differential Revision: https://reviews.llvm.org/D23555 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@279366 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Analysis/CloneDetection.cpp b/lib/Analysis/CloneDetection.cpp index a26409f950..ee1c6943f2 100644 --- a/lib/Analysis/CloneDetection.cpp +++ b/lib/Analysis/CloneDetection.cpp @@ -315,8 +315,26 @@ public: //--- 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 ---------------------------------------------------------// diff --git a/test/Analysis/copypaste/call.cpp b/test/Analysis/copypaste/call.cpp index 0c10262a31..46df02280e 100644 --- a/test/Analysis/copypaste/call.cpp +++ b/test/Analysis/copypaste/call.cpp @@ -34,3 +34,57 @@ bool fooPtr1(int x) { 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; +}