clang-tools 8.0.0

Representation.cpp
Go to the documentation of this file.
1 ///===-- Representation.cpp - ClangDoc Representation -----------*- C++ -*-===//
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 // This file defines the merging of different types of infos. The data in the
11 // calling Info is preserved during a merge unless that field is empty or
12 // default. In that case, the data from the parameter Info is used to replace
13 // the empty or default data.
14 //
15 // For most fields, the first decl seen provides the data. Exceptions to this
16 // include the location and description fields, which are collections of data on
17 // all decls related to a given definition. All other fields are ignored in new
18 // decls unless the first seen decl didn't, for whatever reason, incorporate
19 // data on that field (e.g. a forward declared class wouldn't have information
20 // on members on the forward declaration, but would have the class name).
21 //
22 //===----------------------------------------------------------------------===//
23 #include "Representation.h"
24 #include "llvm/Support/Error.h"
25
26 namespace clang {
27 namespace doc {
28
29 namespace {
30
31 const SymbolID EmptySID = SymbolID();
32
33 template <typename T>
34 llvm::Expected<std::unique_ptr<Info>>
35 reduce(std::vector<std::unique_ptr<Info>> &Values) {
36 if (Values.empty())
37 return llvm::make_error<llvm::StringError>(" No values to reduce.\n",
38 llvm::inconvertibleErrorCode());
39 std::unique_ptr<Info> Merged = llvm::make_unique<T>(Values[0]->USR);
40 T *Tmp = static_cast<T *>(Merged.get());
41 for (auto &I : Values)
42 Tmp->merge(std::move(*static_cast<T *>(I.get())));
43 return std::move(Merged);
44 }
45
46 // Return the index of the matching child in the vector, or -1 if merge is not
47 // necessary.
48 template <typename T>
49 int getChildIndexIfExists(std::vector<T> &Children, T &ChildToMerge) {
50 for (unsigned long I = 0; I < Children.size(); I++) {
51 if (ChildToMerge.USR == Children[I].USR)
52 return I;
53 }
54 return -1;
55 }
56
57 // For References, we don't need to actually merge them, we just don't want
58 // duplicates.
59 void reduceChildren(std::vector<Reference> &Children,
60 std::vector<Reference> &&ChildrenToMerge) {
61 for (auto &ChildToMerge : ChildrenToMerge) {
62 if (getChildIndexIfExists(Children, ChildToMerge) == -1)
63 Children.push_back(std::move(ChildToMerge));
64 }
65 }
66
67 void reduceChildren(std::vector<FunctionInfo> &Children,
68 std::vector<FunctionInfo> &&ChildrenToMerge) {
69 for (auto &ChildToMerge : ChildrenToMerge) {
70 int mergeIdx = getChildIndexIfExists(Children, ChildToMerge);
71 if (mergeIdx == -1) {
72 Children.push_back(std::move(ChildToMerge));
73 continue;
74 }
75 Children[mergeIdx].merge(std::move(ChildToMerge));
76 }
77 }
78
79 void reduceChildren(std::vector<EnumInfo> &Children,
80 std::vector<EnumInfo> &&ChildrenToMerge) {
81 for (auto &ChildToMerge : ChildrenToMerge) {
82 int mergeIdx = getChildIndexIfExists(Children, ChildToMerge);
83 if (mergeIdx == -1) {
84 Children.push_back(std::move(ChildToMerge));
85 continue;
86 }
87 Children[mergeIdx].merge(std::move(ChildToMerge));
88 }
89 }
90
91 } // namespace
92
93 // Dispatch function.
94 llvm::Expected<std::unique_ptr<Info>>
95 mergeInfos(std::vector<std::unique_ptr<Info>> &Values) {
96 if (Values.empty())
97 return llvm::make_error<llvm::StringError>("No info values to merge.\n",
98 llvm::inconvertibleErrorCode());
99
100 switch (Values[0]->IT) {
101 case InfoType::IT_namespace:
102 return reduce<NamespaceInfo>(Values);
103 case InfoType::IT_record:
104 return reduce<RecordInfo>(Values);
105 case InfoType::IT_enum:
106 return reduce<EnumInfo>(Values);
107 case InfoType::IT_function:
108 return reduce<FunctionInfo>(Values);
109 default:
110 return llvm::make_error<llvm::StringError>("Unexpected info type.\n",
111 llvm::inconvertibleErrorCode());
112 }
113 }
114
115 void Info::mergeBase(Info &&Other) {
116 assert(mergeable(Other));
117 if (USR == EmptySID)
118 USR = Other.USR;
119 if (Name == "")
120 Name = Other.Name;
121 if (Namespace.empty())
122 Namespace = std::move(Other.Namespace);
123 // Unconditionally extend the description, since each decl may have a comment.
124 std::move(Other.Description.begin(), Other.Description.end(),
125 std::back_inserter(Description));
126 }
127
128 bool Info::mergeable(const Info &Other) {
129 return IT == Other.IT && USR == Other.USR;
130 }
131
132 void SymbolInfo::merge(SymbolInfo &&Other) {
133 assert(mergeable(Other));
134 if (!DefLoc)
135 DefLoc = std::move(Other.DefLoc);
136 // Unconditionally extend the list of locations, since we want all of them.
137 std::move(Other.Loc.begin(), Other.Loc.end(), std::back_inserter(Loc));
138 mergeBase(std::move(Other));
139 }
140
141 void NamespaceInfo::merge(NamespaceInfo &&Other) {
142 assert(mergeable(Other));
143 // Reduce children if necessary.
144 reduceChildren(ChildNamespaces, std::move(Other.ChildNamespaces));
145 reduceChildren(ChildRecords, std::move(Other.ChildRecords));
146 reduceChildren(ChildFunctions, std::move(Other.ChildFunctions));
147 reduceChildren(ChildEnums, std::move(Other.ChildEnums));
148 mergeBase(std::move(Other));
149 }
150
151 void RecordInfo::merge(RecordInfo &&Other) {
152 assert(mergeable(Other));
153 if (!TagType)
154 TagType = Other.TagType;
155 if (Members.empty())
156 Members = std::move(Other.Members);
157 if (Parents.empty())
158 Parents = std::move(Other.Parents);
159 if (VirtualParents.empty())
160 VirtualParents = std::move(Other.VirtualParents);
161 // Reduce children if necessary.
162 reduceChildren(ChildRecords, std::move(Other.ChildRecords));
163 reduceChildren(ChildFunctions, std::move(Other.ChildFunctions));
164 reduceChildren(ChildEnums, std::move(Other.ChildEnums));
165 SymbolInfo::merge(std::move(Other));
166 }
167
168 void EnumInfo::merge(EnumInfo &&Other) {
169 assert(mergeable(Other));
170 if (!Scoped)
171 Scoped = Other.Scoped;
172 if (Members.empty())
173 Members = std::move(Other.Members);
174 SymbolInfo::merge(std::move(Other));
175 }
176
177 void FunctionInfo::merge(FunctionInfo &&Other) {
178 assert(mergeable(Other));
179 if (!IsMethod)
180 IsMethod = Other.IsMethod;
181 if (!Access)
182 Access = Other.Access;
183 if (ReturnType.Type.USR == EmptySID && ReturnType.Type.Name == "")
184 ReturnType = std::move(Other.ReturnType);
185 if (Parent.USR == EmptySID && Parent.Name == "")
186 Parent = std::move(Other.Parent);
187 if (Params.empty())
188 Params = std::move(Other.Params);
189 SymbolInfo::merge(std::move(Other));
190 }
191
192 } // namespace doc
193 } // namespace clang
Loc
SourceLocation Loc
&#39;#&#39; location in the include directive
Definition: IncludeOrderCheck.cpp:37
Representation.h
clang::doc::Info::Namespace
llvm::SmallVector< Reference, 4 > Namespace
Definition: Representation.h:173
clang::doc::EmptySID
static const SymbolID EmptySID
Definition: BitcodeWriter.cpp:18
clang::doc::InfoType::IT_function
clang::doc::InfoType::IT_enum
clang::doc::InfoType::IT_record
clang::doc::InfoType::IT_namespace
clang::doc::NamespaceInfo::merge
void merge(NamespaceInfo &&I)
Definition: Representation.cpp:141
clang::doc::EnumInfo::merge
void merge(EnumInfo &&I)
Definition: Representation.cpp:168
clang::doc::FunctionInfo
Definition: Representation.h:217
clang::doc::SymbolInfo
Definition: Representation.h:204
clang::doc::Info::mergeable
bool mergeable(const Info &Other)
Definition: Representation.cpp:128
clang::doc::Info::Description
std::vector< CommentInfo > Description
Definition: Representation.h:174
clang::doc::NamespaceInfo
Definition: Representation.h:185
clang::doc::RecordInfo
Definition: Representation.h:234
clang::doc::mergeInfos
llvm::Expected< std::unique_ptr< Info > > mergeInfos(std::vector< std::unique_ptr< Info >> &Values)
Definition: Representation.cpp:95
clang::doc::Info::Name
SmallString< 16 > Name
Definition: Representation.h:171
clang::doc::Info
A base struct for Infos.
Definition: Representation.h:157
ReturnType
std::string ReturnType
Definition: CodeComplete.cpp:417
clang::doc::SymbolInfo::merge
void merge(SymbolInfo &&I)
Definition: Representation.cpp:132
clang::doc::EnumInfo
Definition: Representation.h:264
clang
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Definition: ChangeNamespace.cpp:18
clang::doc::FunctionInfo::merge
void merge(FunctionInfo &&I)
Definition: Representation.cpp:177
clang::doc::Info::IT
const InfoType IT
Definition: Representation.h:170
clang::doc::Info::USR
SymbolID USR
Definition: Representation.h:168
clang::doc::SymbolID
std::array< uint8_t, 20 > SymbolID
Definition: Representation.h:31
clang::doc::RecordInfo::merge
void merge(RecordInfo &&I)
Definition: Representation.cpp:151
clang::doc::Info::mergeBase
void mergeBase(Info &&I)
Definition: Representation.cpp:115