Do some more private->public decl mappings for iterator

types.  This one only shows up with reverse_iterator<list>.

R=wan
DELTA=69  (65 added, 0 deleted, 4 changed)


Revision created by MOE tool push_codebase.
MOE_MIGRATION=1611
This commit is contained in:
csilvers+iwyu 2011-04-26 23:28:20 +00:00
parent 0e8123bc34
commit c6841a1146
2 changed files with 69 additions and 4 deletions

22
iwyu.cc
View File

@ -1277,10 +1277,14 @@ class IwyuBaseAstVisitor : public BaseAstVisitor<Derived> {
// vector<> class includes the typedef. Likewise, we map any free
// function taking a __normal_iterator<foo, vector> (such as
// operator==) to vector<>, assuming that that (templatized)
// function is instantiated as part of the vector class. If the
// input decl does not correspond to a private decl, we return NULL.
// This method is actually a helper for MapPrivateDeclToPublicDecl()
// and MapPrivateTypeToPublicType().
// function is instantiated as part of the vector class.
// We do something similar for _List_iterator and
// _List_const_iterator. These private names are defined in stl_list.h,
// so we don't need to re-map them, but we do want to re-map
// reverse_iterator<_List_iterator> to something in stl_list.h.
// If the input decl does not correspond to one of these private
// decls, we return NULL. This method is actually a helper for
// MapPrivateDeclToPublicDecl() and MapPrivateTypeToPublicType().
const Type* MapPrivateDeclToPublicType(const NamedDecl* decl) const {
const NamedDecl* class_decl = decl;
// If we're a member method, then the __normal_iterator will be
@ -1304,12 +1308,22 @@ class IwyuBaseAstVisitor : public BaseAstVisitor<Derived> {
const Type* ni_type = GetTplTypeArg(class_decl, 0);
// Gets class_decl to be '__normal_iterator<x>'.
class_decl = TypeToDeclAsWritten(ni_type);
// If it's reverse_iterator<_List_iterator>, map to
// _List_iterator, which is defined in stl_list like we want.
if (DeclIsTemplateWithNameAndNumArgsAndTypeArg(
class_decl, "std::_List_iterator", 1, 0) ||
DeclIsTemplateWithNameAndNumArgsAndTypeArg(
class_decl, "std::_List_const_iterator", 1, 0)) {
return ni_type;
}
}
if (DeclIsTemplateWithNameAndNumArgsAndTypeArg(
class_decl, "__gnu_cxx::__normal_iterator", 2, 1)) {
return GetTplTypeArg(class_decl, 1);
}
return NULL;
}

51
tests/iterator.cc Normal file
View File

@ -0,0 +1,51 @@
//===--- iterator.cc - test input file for iwyu ---------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// We have special-case code to deal with foo::iterator, for various
// STL classes foo. They are complicated because they often map to
// __normal_iterator<foo>, and we have to map back. There are also
// issues with reverse_iterator.
//
// Basically, we don't want any of the code below to result in an
// #include of <iterator>
#include <algorithm>
#include <vector>
#include <list>
std::vector<int> vi;
std::vector<int>::iterator vi_begin = vi.begin();
std::vector<int>::const_iterator vi_cbegin = vi.begin();
std::vector<int>::reverse_iterator vi_rbegin = vi.rbegin();
std::vector<int>::const_reverse_iterator vi_crbegin = vi.rbegin();
void VectorFns() {
// Tricky issue with deduced template args.
std::find(vi_begin, vi_begin, 5);
// Issues with operator!=, operator bool, etc.
for (std::vector<int>::iterator it = vi_begin; it != vi_begin; ++it) ;
for (std::vector<int>::reverse_iterator it = vi_rbegin; it != vi_rbegin; ++it)
;
}
std::list<int> li;
std::list<int>::iterator li_begin = li.begin();
std::list<int>::const_iterator li_cbegin = li.begin();
std::list<int>::reverse_iterator li_rbegin = li.rbegin();
std::list<int>::const_reverse_iterator li_crbegin = li.rbegin();
void ListFns() {
std::find(li_begin, li_begin, 5);
for (std::list<int>::iterator it = li_begin; it != li_begin; ++it) ;
for (std::list<int>::reverse_iterator it = li_rbegin; it != li_rbegin; ++it) ;
}
/**** IWYU_SUMMARY
(tests/iterator.cc has correct #includes/fwd-decls)
***** IWYU_SUMMARY */