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