1 //===--- iwyu_cache.cc - cache of AST-derived information, for iwyu -------===//
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 #include "iwyu_cache.h"
11 
12 #include <set>
13 #include <string>
14 
15 #include "iwyu_ast_util.h"
16 #include "iwyu_stl_util.h"
17 #include "clang/AST/DeclTemplate.h"
18 #include "clang/AST/TemplateBase.h"
19 #include "clang/AST/Type.h"
20 
21 using clang::ClassTemplateSpecializationDecl;
22 using clang::NamedDecl;
23 using clang::Type;
24 using clang::TemplateArgument;
25 using clang::TemplateArgumentList;
26 using clang::TemplateSpecializationType;
27 using std::set;
28 using std::string;
29 
30 namespace include_what_you_use {
31 
32 // These are types that all have the same property: instantiating
33 // the type requires full use of all explicitly-listed template
34 // arguments, and full use of all default templated arguments that
35 // the class does not intend-to-provide.  (So vector<MyClass>
36 // required full use of MyClass, but not of allocator<MyClass>).
37 // TODO(csilvers): put this somewhere easier to modify, and add to it.
38 static const char* const kFullUseTypes[] = {
39   "__gnu_cxx::hash_map",
40   "__gnu_cxx::hash_multimap",
41   "__gnu_cxx::hash_multiset",
42   "__gnu_cxx::hash_set",
43   "std::deque",
44   "std::list",
45   "std::map",
46   "std::multimap",
47   "std::multiset",
48   "std::set",
49   "std::slist",
50   "std::vector",
51 };
52 
53 // If the passed-in tpl_decl is one of the classes we have hard-coded
54 // the full-use type information for, populate the cache with the
55 // appropriate full-use type information for the given instantiation.
56 // Note that we only populate the cache for class member uses, not
57 // function calls -- that is, we can use the hard-coded data to say
58 // full use-info for 'sizeof(vector<MyClass>)', but not for
59 // 'myclass_vector.clear();'.  This is because the former never tries
60 // to instantiate methods, making the hard-coding much easier.
GetPrecomputedResugarMap(const TemplateSpecializationType * tpl_type)61 map<const Type*, const Type*> FullUseCache::GetPrecomputedResugarMap(
62     const TemplateSpecializationType* tpl_type) {
63   static const int fulluse_size = (sizeof(kFullUseTypes) /
64                                    sizeof(*kFullUseTypes));
65   static const set<string> fulluse_types(kFullUseTypes,
66                                          kFullUseTypes + fulluse_size);
67 
68   const NamedDecl* tpl_decl = TypeToDeclAsWritten(tpl_type);
69   if (!ContainsKey(fulluse_types, GetWrittenQualifiedNameAsString(tpl_decl)))
70     return map<const Type*, const Type*>();
71 
72   // The code below doesn't handle template-template args/etc.  None
73   // of the types in kFullUseTypes should have those.  Just verify,
74   // when we can.
75   if (const ClassTemplateSpecializationDecl* tpl_spec_decl
76       = DynCastFrom(tpl_decl)) {
77     const TemplateArgumentList& all_tpl_args = tpl_spec_decl->getTemplateArgs();
78     for (unsigned i = 0; i < all_tpl_args.size(); ++i) {
79       CHECK_((all_tpl_args.get(i).getKind() == TemplateArgument::Type)
80              && "kFullUseType types must contain only 'type' template args");
81     }
82   }
83 
84   // The default resugar-map works correctly for all these types (by
85   // design): we fully use all template types.  (Note: we'll have to
86   // do something more clever here if any types in kFullUseTypes start
87   // accepting template-template types.)
88   return GetTplTypeResugarMapForClassNoComponentTypes(tpl_type);
89 }
90 
91 }  // namespace include_what_you_use
92