clang-tools 23.0.0git
Representation.cpp
Go to the documentation of this file.
1///===-- Representation.cpp - ClangDoc Representation -----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines the merging of different types of infos. The data in the
10// calling Info is preserved during a merge unless that field is empty or
11// default. In that case, the data from the parameter Info is used to replace
12// the empty or default data.
13//
14// For most fields, the first decl seen provides the data. Exceptions to this
15// include the location and description fields, which are collections of data on
16// all decls related to a given definition. All other fields are ignored in new
17// decls unless the first seen decl didn't, for whatever reason, incorporate
18// data on that field (e.g. a forward declared class wouldn't have information
19// on members on the forward declaration, but would have the class name).
20//
21//===----------------------------------------------------------------------===//
22#include "Representation.h"
23#include "llvm/ADT/StringMap.h"
24#include "llvm/Support/Error.h"
25#include "llvm/Support/Path.h"
26
27namespace clang {
28namespace doc {
29
30// Thread local arenas usable in each thread pool
31thread_local llvm::BumpPtrAllocator TransientArena;
32thread_local llvm::BumpPtrAllocator PersistentArena;
33
35 static ConcurrentStringPool GlobalPool;
36 return GlobalPool;
37}
38
39CommentKind stringToCommentKind(llvm::StringRef KindStr) {
40 static const llvm::StringMap<CommentKind> KindMap = {
41 {"FullComment", CommentKind::CK_FullComment},
42 {"ParagraphComment", CommentKind::CK_ParagraphComment},
43 {"TextComment", CommentKind::CK_TextComment},
44 {"InlineCommandComment", CommentKind::CK_InlineCommandComment},
45 {"HTMLStartTagComment", CommentKind::CK_HTMLStartTagComment},
46 {"HTMLEndTagComment", CommentKind::CK_HTMLEndTagComment},
47 {"BlockCommandComment", CommentKind::CK_BlockCommandComment},
48 {"ParamCommandComment", CommentKind::CK_ParamCommandComment},
49 {"TParamCommandComment", CommentKind::CK_TParamCommandComment},
50 {"VerbatimBlockComment", CommentKind::CK_VerbatimBlockComment},
51 {"VerbatimBlockLineComment", CommentKind::CK_VerbatimBlockLineComment},
52 {"VerbatimLineComment", CommentKind::CK_VerbatimLineComment},
53 };
54
55 auto It = KindMap.find(KindStr);
56 if (It != KindMap.end()) {
57 return It->second;
58 }
60}
61
62llvm::StringRef commentKindToString(CommentKind Kind) {
63 switch (Kind) {
65 return "FullComment";
67 return "ParagraphComment";
69 return "TextComment";
71 return "InlineCommandComment";
73 return "HTMLStartTagComment";
75 return "HTMLEndTagComment";
77 return "BlockCommandComment";
79 return "ParamCommandComment";
81 return "TParamCommandComment";
83 return "VerbatimBlockComment";
85 return "VerbatimBlockLineComment";
87 return "VerbatimLineComment";
89 return "Unknown";
90 }
91 llvm_unreachable("Unhandled CommentKind");
92}
93
94const SymbolID EmptySID = SymbolID();
95
96template <typename T>
97static llvm::Expected<OwnedPtr<Info>> reduce(OwningPtrArray<Info> &Values) {
98 if (Values.empty() || !Values[0])
99 return llvm::createStringError(llvm::inconvertibleErrorCode(),
100 "no value to reduce");
101 OwnedPtr<Info> Merged = allocatePtr<T>(Values[0]->USR);
102 T *Tmp = static_cast<T *>(getPtr(Merged));
103 for (auto &I : Values)
104 Tmp->merge(std::move(*static_cast<T *>(getPtr(I))));
105 return std::move(Merged);
106}
107
108// Return the index of the matching child in the vector, or -1 if merge is not
109// necessary.
110template <typename T>
111static int getChildIndexIfExists(OwningVec<T> &Children, T &ChildToMerge) {
112 for (unsigned long I = 0; I < Children.size(); I++) {
113 if (ChildToMerge.USR == Children[I].USR)
114 return I;
115 }
116 return -1;
117}
118
119template <typename T>
120static void reduceChildren(llvm::simple_ilist<T> &Children,
121 llvm::simple_ilist<T> &&ChildrenToMerge) {
122 while (!ChildrenToMerge.empty()) {
123 T *ChildToMerge = &ChildrenToMerge.front();
124 ChildrenToMerge.pop_front();
125
126 auto It = llvm::find_if(
127 Children, [&](const T &C) { return C.USR == ChildToMerge->USR; });
128 if (It == Children.end()) {
129 Children.push_back(*ChildToMerge);
130 } else {
131 It->merge(std::move(*ChildToMerge));
132 }
133 }
134}
135
136template <typename T>
137static void reduceChildren(OwningVec<T> &Children,
138 OwningVec<T> &&ChildrenToMerge) {
139 for (auto &ChildToMerge : ChildrenToMerge) {
140 int MergeIdx = getChildIndexIfExists(Children, ChildToMerge);
141 if (MergeIdx == -1) {
142 Children.push_back(std::move(ChildToMerge));
143 continue;
144 }
145 Children[MergeIdx].merge(std::move(ChildToMerge));
146 }
147}
148
149template <typename Container>
150static void mergeUnkeyed(Container &Target, Container &&Source) {
151 for (auto &Item : Source) {
152 if (llvm::none_of(Target, [&](const auto &E) { return E == Item; }))
153 Target.push_back(std::move(Item));
154 }
155}
156
157// Dispatch function.
158llvm::Expected<OwnedPtr<Info>> mergeInfos(OwningPtrArray<Info> &Values) {
159 if (Values.empty() || !Values[0])
160 return llvm::createStringError(llvm::inconvertibleErrorCode(),
161 "no info values to merge");
162
163 switch (Values[0]->IT) {
165 return reduce<NamespaceInfo>(Values);
167 return reduce<RecordInfo>(Values);
169 return reduce<EnumInfo>(Values);
171 return reduce<FunctionInfo>(Values);
173 return reduce<TypedefInfo>(Values);
175 return reduce<ConceptInfo>(Values);
177 return reduce<VarInfo>(Values);
179 return reduce<FriendInfo>(Values);
181 return llvm::createStringError(llvm::inconvertibleErrorCode(),
182 "unexpected info type");
183 }
184 llvm_unreachable("unhandled enumerator");
185}
186
187bool CommentInfo::operator==(const CommentInfo &Other) const {
188 auto FirstCI = std::tie(Kind, Text, Name, Direction, ParamName, CloseName,
190 auto SecondCI =
191 std::tie(Other.Kind, Other.Text, Other.Name, Other.Direction,
192 Other.ParamName, Other.CloseName, Other.SelfClosing,
193 Other.Explicit, Other.AttrKeys, Other.AttrValues, Other.Args);
194
195 if (FirstCI != SecondCI || Children.size() != Other.Children.size())
196 return false;
197
198 return std::equal(Children.begin(), Children.end(), Other.Children.begin(),
199 Other.Children.end());
200}
201
202bool CommentInfo::operator<(const CommentInfo &Other) const {
203 auto FirstCI = std::tie(Kind, Text, Name, Direction, ParamName, CloseName,
205 auto SecondCI =
206 std::tie(Other.Kind, Other.Text, Other.Name, Other.Direction,
207 Other.ParamName, Other.CloseName, Other.SelfClosing,
208 Other.Explicit, Other.AttrKeys, Other.AttrValues, Other.Args);
209
210 if (FirstCI < SecondCI)
211 return true;
212
213 if (FirstCI == SecondCI) {
214 return std::lexicographical_compare(Children.begin(), Children.end(),
215 Other.Children.begin(),
216 Other.Children.end());
217 }
218
219 return false;
220}
221
222static llvm::SmallString<64>
223calculateRelativeFilePath(const InfoType &Type, const StringRef &Path,
224 const StringRef &Name, const StringRef &CurrentPath) {
225 llvm::SmallString<64> FilePath;
226
227 if (CurrentPath != Path) {
228 // iterate back to the top
229 for (llvm::sys::path::const_iterator I =
230 llvm::sys::path::begin(CurrentPath);
231 I != llvm::sys::path::end(CurrentPath); ++I)
232 llvm::sys::path::append(FilePath, "..");
233 llvm::sys::path::append(FilePath, Path);
234 }
235
236 // Namespace references have a Path to the parent namespace, but
237 // the file is actually in the subdirectory for the namespace.
238 if (Type == doc::InfoType::IT_namespace)
239 llvm::sys::path::append(FilePath, Name);
240
241 return llvm::sys::path::relative_path(FilePath);
242}
243
244StringRef Reference::getRelativeFilePath(const StringRef &CurrentPath) const {
245 return internString(
247}
248
249StringRef Reference::getFileBaseName() const {
251 return "index";
252
253 return Name;
254}
255
256StringRef Info::getRelativeFilePath(const StringRef &CurrentPath) const {
257 return internString(
258 calculateRelativeFilePath(IT, Path, extractName(), CurrentPath));
259}
260
261StringRef Info::getFileBaseName() const {
263 return "index";
264
265 return extractName();
266}
267
268bool Reference::mergeable(const Reference &Other) {
269 return RefType == Other.RefType && USR == Other.USR;
270}
271
273 assert(mergeable(Other));
274 if (Name.empty())
275 Name = Other.Name;
276 if (Path.empty())
277 Path = Other.Path;
278 if (DocumentationFileName.empty())
279 DocumentationFileName = Other.DocumentationFileName;
280}
281
283 return Ref.USR == Other.Ref.USR && Ref.Name == Other.Ref.Name;
284}
285
287 assert(mergeable(Other));
288 Ref.merge(std::move(Other.Ref));
289 SymbolInfo::merge(std::move(Other));
290}
291
292void Info::mergeBase(Info &&Other) {
293 assert(mergeable(Other));
294 if (USR == EmptySID)
295 USR = Other.USR;
296 if (Name == "")
297 Name = Other.Name;
298 if (Path == "")
299 Path = Other.Path;
300 if (Namespace.empty())
301 Namespace = std::move(Other.Namespace);
302 // Unconditionally extend the description, since each decl may have a comment.
303 mergeUnkeyed(Description, std::move(Other.Description));
304 if (ParentUSR == EmptySID)
305 ParentUSR = Other.ParentUSR;
306 if (DocumentationFileName.empty())
307 DocumentationFileName = Other.DocumentationFileName;
308}
309
310bool Info::mergeable(const Info &Other) {
311 return IT == Other.IT && USR == Other.USR;
312}
313
314void SymbolInfo::merge(SymbolInfo &&Other) {
315 assert(mergeable(Other));
316 if (!DefLoc)
317 DefLoc = std::move(Other.DefLoc);
318 // Unconditionally extend the list of locations, since we want all of them.
319 mergeUnkeyed(Loc, std::move(Other.Loc));
320 mergeBase(std::move(Other));
321 if (MangledName.empty())
322 MangledName = std::move(Other.MangledName);
323}
324
327
329 assert(mergeable(Other));
330 // Reduce children if necessary.
331 reduceChildren(Children.Namespaces, std::move(Other.Children.Namespaces));
332 reduceChildren(Children.Records, std::move(Other.Children.Records));
333 reduceChildren(Children.Functions, std::move(Other.Children.Functions));
334 reduceChildren(Children.Enums, std::move(Other.Children.Enums));
335 reduceChildren(Children.Typedefs, std::move(Other.Children.Typedefs));
336 reduceChildren(Children.Concepts, std::move(Other.Children.Concepts));
337 reduceChildren(Children.Variables, std::move(Other.Children.Variables));
338 mergeBase(std::move(Other));
339}
340
343
345 assert(mergeable(Other));
346 if (!llvm::to_underlying(TagType))
347 TagType = Other.TagType;
348 IsTypeDef = IsTypeDef || Other.IsTypeDef;
349 if (Members.empty())
350 Members = std::move(Other.Members);
351 if (Bases.empty())
352 Bases = std::move(Other.Bases);
353 if (Parents.empty())
354 Parents = std::move(Other.Parents);
355 if (VirtualParents.empty())
356 VirtualParents = std::move(Other.VirtualParents);
357 if (Friends.empty())
358 Friends = std::move(Other.Friends);
359 // Reduce children if necessary.
360 reduceChildren(Children.Records, std::move(Other.Children.Records));
361 reduceChildren(Children.Functions, std::move(Other.Children.Functions));
362 reduceChildren(Children.Enums, std::move(Other.Children.Enums));
363 reduceChildren(Children.Typedefs, std::move(Other.Children.Typedefs));
364 SymbolInfo::merge(std::move(Other));
365 if (!Template)
366 Template = Other.Template;
367}
368
370 assert(mergeable(Other));
371 if (!Scoped)
372 Scoped = Other.Scoped;
373 if (Members.empty())
374 Members = std::move(Other.Members);
375 SymbolInfo::merge(std::move(Other));
376}
377
379 assert(mergeable(Other));
380 if (!IsMethod)
381 IsMethod = Other.IsMethod;
382 if (!Access)
383 Access = Other.Access;
384 if (ReturnType.Type.USR == EmptySID && ReturnType.Type.Name == "")
385 ReturnType = std::move(Other.ReturnType);
386 if (Parent.USR == EmptySID && Parent.Name == "")
387 Parent = std::move(Other.Parent);
388 if (Params.empty())
389 Params = std::move(Other.Params);
390 SymbolInfo::merge(std::move(Other));
391 if (!Template)
392 Template = Other.Template;
393}
394
396 assert(mergeable(Other));
397 if (!IsUsing)
398 IsUsing = Other.IsUsing;
399 if (Underlying.Type.Name == "")
400 Underlying = Other.Underlying;
401 if (!Template)
402 Template = Other.Template;
403 SymbolInfo::merge(std::move(Other));
404}
405
407 assert(mergeable(Other));
408 if (!IsType)
409 IsType = Other.IsType;
410 if (ConstraintExpression.empty())
411 ConstraintExpression = std::move(Other.ConstraintExpression);
412 if (Template.Constraints.empty())
413 Template.Constraints = std::move(Other.Template.Constraints);
414 if (Template.Params.empty())
415 Template.Params = std::move(Other.Template.Params);
416 SymbolInfo::merge(std::move(Other));
417}
418
419void VarInfo::merge(VarInfo &&Other) {
420 assert(mergeable(Other));
421 if (!IsStatic)
422 IsStatic = Other.IsStatic;
423 if (Type.Type.USR == EmptySID && Type.Type.Name == "")
424 Type = std::move(Other.Type);
425 SymbolInfo::merge(std::move(Other));
426}
427
429
431 bool IsVirtual, AccessSpecifier Access,
432 bool IsParent)
435
436StringRef Info::extractName() const {
437 if (!Name.empty())
438 return Name;
439
440 switch (IT) {
442 // Cover the case where the project contains a base namespace called
443 // 'GlobalNamespace' (i.e. a namespace at the same level as the global
444 // namespace, which would conflict with the hard-coded global namespace name
445 // below.)
446 if (Name == "GlobalNamespace" && Namespace.empty())
447 return "@GlobalNamespace";
448 // The case of anonymous namespaces is taken care of in serialization,
449 // so here we can safely assume an unnamed namespace is the global
450 // one.
451 return "GlobalNamespace";
453 return internString("@nonymous_record_" + toHex(llvm::toStringRef(USR)));
455 return internString("@nonymous_enum_" + toHex(llvm::toStringRef(USR)));
457 return internString("@nonymous_typedef_" + toHex(llvm::toStringRef(USR)));
459 return internString("@nonymous_function_" + toHex(llvm::toStringRef(USR)));
461 return internString("@nonymous_concept_" + toHex(llvm::toStringRef(USR)));
463 return internString("@nonymous_variable_" + toHex(llvm::toStringRef(USR)));
465 return internString("@nonymous_friend_" + toHex(llvm::toStringRef(USR)));
467 return internString("@nonymous_" + toHex(llvm::toStringRef(USR)));
468 }
469 llvm_unreachable("Invalid InfoType.");
470 return "";
471}
472
473// Order is based on the Name attribute: case insensitive order
474bool Index::operator<(const Index &Other) const {
475 // Start with case-insensitive (e.g., 'apple' < 'Zebra').
476 // This prevents 'Zebra' from appearing before 'apple' due to ASCII values,
477 // where uppercase letters have a lower numeric value than lowercase.
478 int Cmp = Name.compare_insensitive(Other.Name);
479 if (Cmp != 0)
480 return Cmp < 0;
481
482 // If names are identical, we fall back to standard string comparison where
483 // uppercase precedes lowercase (e.g., 'Apple' < 'apple').
484 return Name < Other.Name;
485}
486
488 OwningVec<const Index *> SortedChildren;
489 SortedChildren.reserve(Children.size());
490 for (const auto &[_, C] : Children)
491 SortedChildren.push_back(&C);
492 llvm::sort(SortedChildren,
493 [](const Index *A, const Index *B) { return *A < *B; });
494 return SortedChildren;
495}
496
498 for (auto &[_, C] : Children)
499 C.sort();
500}
501
502ClangDocContext::ClangDocContext(tooling::ExecutionContext *ECtx,
503 StringRef ProjectName, bool PublicOnly,
504 StringRef OutDirectory, StringRef SourceRoot,
505 StringRef RepositoryUrl,
506 StringRef RepositoryLinePrefix, StringRef Base,
507 std::vector<std::string> UserStylesheets,
508 clang::DiagnosticsEngine &Diags,
514 llvm::SmallString<128> SourceRootDir(SourceRoot);
515 if (SourceRoot.empty())
516 // If no SourceRoot was provided the current path is used as the default
517 llvm::sys::fs::current_path(SourceRootDir);
518 this->SourceRoot = std::string(SourceRootDir);
519 if (!RepositoryUrl.empty()) {
520 this->RepositoryUrl = std::string(RepositoryUrl);
521 if (!RepositoryUrl.empty() && !RepositoryUrl.starts_with("http://") &&
522 !RepositoryUrl.starts_with("https://"))
523 this->RepositoryUrl->insert(0, "https://");
524
525 if (!RepositoryLinePrefix.empty())
526 this->RepositoryLinePrefix = std::string(RepositoryLinePrefix);
527 }
528}
529
531 Namespaces.sort();
532 llvm::sort(Records);
533 llvm::sort(Functions);
534 llvm::sort(Enums);
535 llvm::sort(Typedefs);
536 llvm::sort(Concepts);
537 llvm::sort(Variables);
538}
539} // namespace doc
540} // namespace clang
llvm::Expected< OwnedPtr< Info > > mergeInfos(OwningPtrArray< Info > &Values)
static llvm::SmallString< 64 > calculateRelativeFilePath(const InfoType &Type, const StringRef &Path, const StringRef &Name, const StringRef &CurrentPath)
std::unique_ptr< T > OwnedPtr
thread_local llvm::BumpPtrAllocator PersistentArena
static void reduceChildren(llvm::simple_ilist< T > &Children, llvm::simple_ilist< T > &&ChildrenToMerge)
T * getPtr(const OwnedPtr< T > &O)
static int getChildIndexIfExists(OwningVec< T > &Children, T &ChildToMerge)
ConcurrentStringPool & getGlobalStringPool()
CommentKind stringToCommentKind(llvm::StringRef KindStr)
std::vector< OwnedPtr< T > > OwningPtrArray
thread_local llvm::BumpPtrAllocator TransientArena
StringRef internString(const Twine &T)
static llvm::Expected< OwnedPtr< Info > > reduce(OwningPtrArray< Info > &Values)
std::vector< T > OwningVec
OwnedPtr< T > allocatePtr(Args &&...args)
std::array< uint8_t, 20 > SymbolID
llvm::StringRef commentKindToString(CommentKind Kind)
static void mergeUnkeyed(Container &Target, Container &&Source)
static const SymbolID EmptySID
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::optional< std::string > RepositoryUrl
std::vector< std::string > UserStylesheets
ClangDocContext(tooling::ExecutionContext *ECtx, StringRef ProjectName, bool PublicOnly, StringRef OutDirectory, StringRef SourceRoot, StringRef RepositoryUrl, StringRef RepositoryCodeLinePrefix, StringRef Base, std::vector< std::string > UserStylesheets, clang::DiagnosticsEngine &Diags, OutputFormatTy Format, bool FTimeTrace=false)
tooling::ExecutionContext * ECtx
clang::DiagnosticsEngine & Diags
std::optional< std::string > RepositoryLinePrefix
llvm::ArrayRef< StringRef > Args
llvm::ArrayRef< StringRef > AttrValues
llvm::ArrayRef< CommentInfo > Children
bool operator<(const CommentInfo &Other) const
bool operator==(const CommentInfo &Other) const
llvm::ArrayRef< StringRef > AttrKeys
void merge(ConceptInfo &&I)
llvm::SmallVector< EnumValueInfo, 4 > Members
void merge(EnumInfo &&I)
void merge(FriendInfo &&Other)
bool mergeable(const FriendInfo &Other)
FunctionInfo(SymbolID USR=SymbolID())
llvm::SmallVector< FieldTypeInfo, 4 > Params
void merge(FunctionInfo &&I)
std::optional< TemplateInfo > Template
OwningVec< const Index * > getSortedChildren() const
bool operator<(const Index &Other) const
llvm::StringMap< Index > Children
StringRef getRelativeFilePath(const StringRef &CurrentPath) const
Returns the file path for this Info relative to CurrentPath.
Info(InfoType IT=InfoType::IT_default, SymbolID USR=SymbolID(), StringRef Name=StringRef(), StringRef Path=StringRef())
bool mergeable(const Info &Other)
OwningVec< CommentInfo > Description
StringRef extractName() const
StringRef DocumentationFileName
void mergeBase(Info &&I)
StringRef getFileBaseName() const
Returns the basename that should be used for this Info.
llvm::SmallVector< Reference, 4 > Namespace
NamespaceInfo(SymbolID USR=SymbolID(), StringRef Name=StringRef(), StringRef Path=StringRef())
void merge(NamespaceInfo &&I)
OwningVec< BaseRecordInfo > Bases
llvm::SmallVector< MemberTypeInfo, 4 > Members
RecordInfo(SymbolID USR=SymbolID(), StringRef Name=StringRef(), StringRef Path=StringRef())
std::optional< TemplateInfo > Template
llvm::SmallVector< Reference, 4 > VirtualParents
llvm::SmallVector< Reference, 4 > Parents
void merge(RecordInfo &&I)
OwningVec< FriendInfo > Friends
void merge(Reference &&I)
Reference(SymbolID USR=SymbolID(), StringRef Name=StringRef(), InfoType IT=InfoType::IT_default)
bool mergeable(const Reference &Other)
StringRef getFileBaseName() const
Returns the basename that should be used for this Reference.
StringRef getRelativeFilePath(const StringRef &CurrentPath) const
Returns the path for this Reference relative to CurrentPath.
OwningVec< FunctionInfo > Functions
OwningVec< TypedefInfo > Typedefs
OwningVec< EnumInfo > Enums
OwningVec< Reference > Records
OwningVec< ConceptInfo > Concepts
OwningVec< VarInfo > Variables
llvm::simple_ilist< Reference > Namespaces
SymbolInfo(InfoType IT, SymbolID USR=SymbolID(), StringRef Name=StringRef(), StringRef Path=StringRef())
llvm::SmallVector< Location, 2 > Loc
std::optional< Location > DefLoc
void merge(TypedefInfo &&I)
std::optional< TemplateInfo > Template
TypedefInfo(SymbolID USR=SymbolID())
void merge(VarInfo &&I)