This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Fix a false positive for explicit template instantiations in misc-unused-using-decls.
Needs RevisionPublic

Authored by hokein on Feb 16 2017, 2:19 AM.

Details

Reviewers
alexfh
rsmith
Summary

The AST for explicit template instantiations is slightly different
from the source code (see note below): there is no AST node for explicit template
instantiations, so regular ast matchers like typeLoc can't find the
type which is referenced in template arguments of an explicit template
instantiations.

Our workaround is to record all declarations referenced from template arguments
of explicit template instantiations and the instantiation source location of
instantiations, and assumes that the using-decl is used when the using-decl's
source location is before the instantiation source location.

While the workaround avoids false positives, it may introduce true negatives.
but this case rarely happens and users won't get bothered by true positives (No
warning message is print by the check).

Note:

// NamespaceDecl 0x7f875981e798 </tmp/t.cc:1:1, line:3:1> line:1:11 ns
// | `-CXXRecordDecl 0x7f875a02f400 <line:2:1, col:10> col:7 referenced class A definition
// |   `-CXXRecordDecl 0x7f875a02f520 <col:1, col:7> col:7 implicit class A
// |-FunctionTemplateDecl 0x7f875a02f768 <line:4:1, line:6:1> line:5:6 test
// | |-TemplateTypeParmDecl 0x7f875a02f5b8 <line:4:10, col:16> col:16 class T
// | |-FunctionDecl 0x7f875a02f6c0 <line:5:1, line:6:1> line:5:6 test 'void (void)'
// | | `-CompoundStmt 0x7f875a02f7f8 <col:13, line:6:1>
// | `-FunctionDecl 0x7f875a02f9d0 <line:5:1, line:6:1> line:5:6 test 'void (void)'
// |   |-TemplateArgument type 'class ns::A'
// |   `-CompoundStmt 0x7f875a02f7f8 <col:13, line:6:1>
// |-UsingDecl 0x7f875a02f830 <line:7:1, col:11> col:11 ns::A
// `-UsingShadowDecl 0x7f875a02f880 <col:11> col:11 implicit CXXRecord 0x7f875a02f400 'A'
//   `-RecordType 0x7f875a02f4a0 'class ns::A'
//       `-CXXRecord 0x7f875a02f400 'A'
namespace ns {
class A {};
}
template<class T>
void test() {
}
using ns::A;
template void test<A>();

Event Timeline

hokein created this revision.Feb 16 2017, 2:19 AM
hokein edited the summary of this revision. (Show Details)Feb 16 2017, 2:22 AM
hokein added a reviewer: alexfh.
hokein added a subscriber: cfe-commits.
alexfh edited edge metadata.Feb 16 2017, 2:59 AM

it may introduce true positives

True positives is all we need from clang-tidy checks ;) I guess, you meant "false negatives" (https://en.wikipedia.org/wiki/False_positives_and_false_negatives).

The main question here is whether we should extend the AST instead of hacking around its limitations. It's probably best to discuss with Richard Smith.

Richard, it seems like the AST could be improved here by adding nodes for the explicit instantiation declarations and definitions or using existing nodes, if there are suitable ones. What do you think?

hokein edited the summary of this revision. (Show Details)Feb 17 2017, 1:50 AM
alexfh requested changes to this revision.Mar 1 2017, 2:35 AM

I think, we decided to extend the AST instead of working around its incompleteness.

This revision now requires changes to proceed.Mar 1 2017, 2:35 AM