clang-tools 24.0.0git
Representation.h
Go to the documentation of this file.
1///===-- Representation.h - 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 internal representations of different declaration
10// types for the clang-doc tool.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_REPRESENTATION_H
15#define LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_REPRESENTATION_H
16
18#include "clang/AST/Type.h"
19#include "clang/Basic/Diagnostic.h"
20#include "clang/Basic/Specifiers.h"
21#include "clang/Tooling/Execution.h"
22#include "llvm/ADT/SmallString.h"
23#include "llvm/ADT/SmallVector.h"
24#include "llvm/ADT/ilist_node.h"
25#include "llvm/ADT/simple_ilist.h"
26#include "llvm/Support/Allocator.h"
27#include "llvm/Support/Casting.h"
28
29#include <array>
30#include <memory>
31#include <optional>
32#include <string>
33
34namespace clang {
35namespace doc {
36
38
39llvm::BumpPtrAllocator &getTransientArena();
40llvm::BumpPtrAllocator &getPersistentArena();
41
42inline StringRef internString(const Twine &T) {
43 return getGlobalStringPool().save(T);
44}
45
46template <typename T>
48 llvm::BumpPtrAllocator &Alloc) {
49 if (V.empty())
50 return llvm::ArrayRef<T>();
51 T *Allocated = static_cast<T *>(Alloc.Allocate<T>(V.size()));
52 std::uninitialized_move(V.begin(), V.end(), Allocated);
53 return llvm::ArrayRef<T>(Allocated, V.size());
54}
55
56template <typename T>
57llvm::ArrayRef<T> allocateArray(llvm::ArrayRef<T> V,
58 llvm::BumpPtrAllocator &Alloc) {
59 if (V.empty())
60 return llvm::ArrayRef<T>();
61 T *Allocated = static_cast<T *>(Alloc.Allocate<T>(V.size()));
62 std::uninitialized_move(V.begin(), V.end(), Allocated);
63 return llvm::ArrayRef<T>(Allocated, V.size());
64}
65
66template <typename T>
67llvm::ArrayRef<T> deepCopyArray(llvm::ArrayRef<T> V,
68 llvm::BumpPtrAllocator &Alloc) {
69 if (V.empty())
70 return llvm::ArrayRef<T>();
71 T *Allocated = static_cast<T *>(Alloc.Allocate<T>(V.size()));
72 for (size_t Idx = 0; Idx < V.size(); ++Idx) {
73 new (Allocated + Idx) T(V[Idx], Alloc);
74 }
75 return llvm::ArrayRef<T>(Allocated, V.size());
76}
77
78// A helper function to create an owned pointer, abstracting away the memory
79// allocation mechanism.
80template <typename T, typename... Args> T *allocateTransient(Args &&...args) {
81 return new (getTransientArena().Allocate<T>()) T(std::forward<Args>(args)...);
82}
83
84// A helper function to create memory allocated in the TransientArena.
85template <typename T, typename... Args> T *allocatePersistent(Args &&...args) {
86 return new (getPersistentArena().Allocate<T>())
87 T(std::forward<Args>(args)...);
88}
89
90// An overload to explicitly allocate on an arena, returning a bare pointer.
91template <typename T, typename... Args>
92T *allocatePtr(llvm::BumpPtrAllocator &Alloc, Args &&...args) {
93 return new (Alloc.Allocate<T>()) T(std::forward<Args>(args)...);
94}
95
96template <typename T> struct InfoNode : public llvm::ilist_node<InfoNode<T>> {
97 InfoNode(T *P) : Ptr(P) {}
98 T *Ptr = nullptr;
99
100 operator T &() { return *Ptr; }
101 operator const T &() const { return *Ptr; }
102
103 T &operator*() { return *Ptr; }
104 const T &operator*() const { return *Ptr; }
105 T *operator->() { return Ptr; }
106 const T *operator->() const { return Ptr; }
107
108 bool operator==(const InfoNode<T> &Other) const {
109 if (!Ptr || !Other.Ptr)
110 return Ptr == Other.Ptr;
111 return *Ptr == *Other.Ptr;
112 }
113
114 bool operator!=(const InfoNode<T> &Other) const { return !(*this == Other); }
115
116 bool operator<(const InfoNode<T> &Other) const {
117 if (!Ptr || !Other.Ptr)
118 return Ptr < Other.Ptr;
119 return *Ptr < *Other.Ptr;
120 }
121};
122
123template <typename T, typename... Args>
124InfoNode<T> *allocateListNode(llvm::BumpPtrAllocator &Alloc, Args &&...args) {
125 T *Item = allocatePtr<T>(Alloc, std::forward<Args>(args)...);
126 return allocatePtr<InfoNode<T>>(Alloc, Item);
127}
128
129template <typename T, typename... Args>
131 return allocateListNode<T>(getTransientArena(), std::forward<Args>(args)...);
132}
133
134template <typename T>
135InfoNode<T> *allocateListNode(llvm::BumpPtrAllocator &Alloc, T *Item) {
136 return allocatePtr<InfoNode<T>>(Alloc, Item);
137}
138
139template <typename T> InfoNode<T> *allocateListNodeTransient(T *Item) {
141}
142
143template <typename T, typename... Args>
145 return allocateListNode<T>(getPersistentArena(), std::forward<Args>(args)...);
146}
147
148template <typename T> InfoNode<T> *allocateListNodePersistent(T *Item) {
150}
151
152// An abstraction for lists that are dynamically managed (inserted/removed).
153// To be eventually transitioned to llvm::simple_ilist.
154template <typename T> using DocList = llvm::simple_ilist<InfoNode<T>>;
155
156// SHA1'd hash of a USR.
157using SymbolID = std::array<uint8_t, 20>;
158
159constexpr SymbolID GlobalNamespaceID = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
160 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
161
162struct BaseRecordInfo;
163struct EnumInfo;
164struct FunctionInfo;
165struct Info;
166struct TypedefInfo;
167struct ConceptInfo;
168struct VarInfo;
169
181
197
199
200CommentKind stringToCommentKind(llvm::StringRef KindStr);
201llvm::StringRef commentKindToString(CommentKind Kind);
202
203struct CommentInfo;
204
205// A representation of a parsed comment.
207 CommentInfo() = default;
208 CommentInfo(const CommentInfo &Other) = default;
209 CommentInfo &operator=(const CommentInfo &Other) = default;
210 CommentInfo(const CommentInfo &Other, llvm::BumpPtrAllocator &Arena);
211 CommentInfo(CommentInfo &&Other) = default;
212 CommentInfo &operator=(CommentInfo &&Other) = default;
213
214 CommentInfo(CommentKind Kind, llvm::ArrayRef<CommentInfo> Children = {},
215 StringRef Text = StringRef(), StringRef Name = StringRef(),
216 StringRef CloseName = StringRef(),
217 StringRef Direction = StringRef(),
218 StringRef ParamName = StringRef(), bool Explicit = false,
219 bool SelfClosing = false, llvm::ArrayRef<StringRef> AttrKeys = {},
220 llvm::ArrayRef<StringRef> AttrValues = {})
223 AttrKeys(AttrKeys), AttrValues(AttrValues), Kind(Kind),
225
226 bool operator==(const CommentInfo &Other) const;
227
228 // This operator is used to sort a vector of CommentInfos.
229 // No specific order (attributes more important than others) is required. Any
230 // sort is enough, the order is only needed to call std::unique after sorting
231 // the vector.
232 bool operator<(const CommentInfo &Other) const;
233
234 // List of child comments for this CommentInfo.
235 ArrayRef<CommentInfo> Children = {};
236
237 // Parameter direction (for (T)ParamCommand).
238 StringRef Direction = {};
239
240 // Name of the comment (for Verbatim and HTML).
241 StringRef Name = {};
242
243 // Parameter name (for (T)ParamCommand).
244 StringRef ParamName = {};
245
246 // Closing tag name (for VerbatimBlock).
247 StringRef CloseName = {};
248
249 // Text of the comment.
250 StringRef Text = {};
251
252 // List of attribute keys (for HTML).
253 ArrayRef<StringRef> AttrKeys = {};
254
255 // List of attribute values for each key (for HTML).
256 ArrayRef<StringRef> AttrValues = {};
257
258 // List of arguments to commands (for InlineCommand).
259 ArrayRef<StringRef> Args = {};
260
261 // Type of comment. Unknown by default.
263
264 // Indicates if tag is self-closing (for HTML).
265 bool SelfClosing = false;
266
267 // Indicates if the direction of a param is explicit (for (T)ParamCommand).
268 bool Explicit = false;
269};
270
271struct Reference {
272 // This variant (that takes no qualified name parameter) uses the Name as the
273 // QualName (very useful in unit tests to reduce verbosity). This can't use an
274 // empty string to indicate the default because we need to accept the empty
275 // string as a valid input for the global namespace (it will have
276 // "GlobalNamespace" as the name, but an empty QualName).
277 Reference(SymbolID USR = SymbolID(), StringRef Name = StringRef(),
279 : USR(USR), RefType(IT), Name(internString(Name)),
281 Reference(SymbolID USR, StringRef Name, InfoType IT, StringRef QualName,
282 StringRef Path = StringRef())
283 : USR(USR), RefType(IT), Name(internString(Name)),
290
291 bool operator==(const Reference &Other) const {
292 return std::tie(USR, Name, QualName, RefType) ==
293 std::tie(Other.USR, Other.Name, Other.QualName, Other.RefType);
294 }
295
296 bool mergeable(const Reference &Other);
297 void merge(Reference &&I);
298 bool operator<(const Reference &Other) const { return Name < Other.Name; }
299
300 /// Returns the path for this Reference relative to CurrentPath.
301 StringRef getRelativeFilePath(const StringRef &CurrentPath) const;
302
303 /// Returns the basename that should be used for this Reference.
304 StringRef getFileBaseName() const;
305
306 SymbolID USR = SymbolID(); // Unique identifier for referenced decl
307
308 InfoType RefType = InfoType::IT_default; // Indicates the type of this
309 // Reference (namespace, record,
310 // function, enum, default).
311
312 // Name of type (possibly unresolved). Not including namespaces or template
313 // parameters (so for a std::vector<int> this would be "vector"). See also
314 // QualName.
315 StringRef Name = {};
316
317 // Full qualified name of this type, including namespaces and template
318 // parameter (for example this could be "std::vector<int>"). Contrast to
319 // Name.
320 StringRef QualName = {};
321
322 // Path of directory where the clang-doc generated file will be saved
323 // (possibly unresolved)
324 StringRef Path = {};
325 StringRef DocumentationFileName = {};
326};
327
328// A Context is a reference that holds a relative path from a certain Info's
329// location.
330struct Context : public Reference {
331 Context(SymbolID USR, StringRef Name, InfoType IT, StringRef QualName,
332 StringRef Path, StringRef DocumentationFileName)
334 explicit Context(const Info &I);
335 StringRef RelativePath = {};
336};
337
338// Holds the children of a record or namespace.
340 // Namespaces and Records are references because they will be properly
341 // documented in their own info, while the entirety of Functions and Enums are
342 // included here because they should not have separate documentation from
343 // their scope.
344 //
345 // Namespaces are not syntactically valid as children of records, but making
346 // this general for all possible container types reduces code complexity.
354
355 void sort();
356};
357
358// A base struct for TypeInfos
359struct TypeInfo {
360 TypeInfo() = default;
361 TypeInfo(const Reference &R) : Type(R) {}
362
363 // Convenience constructor for when there is no symbol ID or info type
364 // (normally used for built-in types in tests).
365 TypeInfo(StringRef Name, StringRef Path = StringRef())
366 : Type(SymbolID(), Name, InfoType::IT_default, Name, Path) {}
367
368 bool operator==(const TypeInfo &Other) const { return Type == Other.Type; }
369
370 Reference Type; // Referenced type in this info.
371
372 bool IsTemplate = false;
373 bool IsBuiltIn = false;
374};
375
376// Represents one template parameter.
377//
378// This is a very simple serialization of the text of the source code of the
379// template parameter. It is saved in a struct so there is a place to add the
380// name and default values in the future if needed.
382 TemplateParamInfo() = default;
385
386 // The literal contents of the code for that specifies this template parameter
387 // for this declaration. Typical values will be "class T" and
388 // "typename T = int".
389 StringRef Contents = {};
390};
391
395 llvm::BumpPtrAllocator &Arena);
396
397 // Indicates the declaration that this specializes.
399
400 // Template parameters applying to the specialized record/function.
401 llvm::ArrayRef<TemplateParamInfo> Params = {};
402};
403
405 ConstraintInfo() = default;
406 ConstraintInfo(SymbolID USR, StringRef Name)
407 : ConceptRef(USR, Name, InfoType::IT_concept) {}
409
410 StringRef ConstraintExpr = {};
411};
412
413// Records the template information for a struct or function that is a template
414// or an explicit template specialization.
416 TemplateInfo() = default;
417 TemplateInfo(const TemplateInfo &Other, llvm::BumpPtrAllocator &Arena);
418
419 // May be empty for non-partial specializations.
420 llvm::ArrayRef<TemplateParamInfo> Params = {};
421
422 // Set when this is a specialization of another record/function.
423 std::optional<TemplateSpecializationInfo> Specialization;
424 llvm::ArrayRef<ConstraintInfo> Constraints = {};
425};
426
427// Info for field types.
428struct FieldTypeInfo : public TypeInfo {
429 FieldTypeInfo() = default;
430 FieldTypeInfo(const TypeInfo &TI, StringRef Name = StringRef(),
431 StringRef DefaultValue = StringRef())
434
435 bool operator==(const FieldTypeInfo &Other) const {
436 return std::tie(Type, Name, DefaultValue) ==
437 std::tie(Other.Type, Other.Name, Other.DefaultValue);
438 }
439
440 StringRef Name = {}; // Name associated with this info.
441
442 // When used for function parameters, contains the string representing the
443 // expression of the default value, if any.
444 StringRef DefaultValue = {};
445};
446
447// Info for member types.
449 MemberTypeInfo() = default;
450 MemberTypeInfo(const MemberTypeInfo &Other, llvm::BumpPtrAllocator &Arena);
451 MemberTypeInfo(const TypeInfo &TI, StringRef Name, AccessSpecifier Access,
452 bool IsStatic = false)
454
455 bool operator==(const MemberTypeInfo &Other) const {
456 if (std::tie(Type, Name, Access, IsStatic) !=
457 std::tie(Other.Type, Other.Name, Other.Access, Other.IsStatic))
458 return false;
459 return std::equal(Description.begin(), Description.end(),
460 Other.Description.begin(), Other.Description.end());
461 }
462
464
465 // Access level associated with this info (public, protected, private, none).
466 // AS_public is set as default because the bitcode writer requires the enum
467 // with value 0 to be used as the default.
468 // (AS_public = 0, AS_protected = 1, AS_private = 2, AS_none = 3)
469 AccessSpecifier Access = AccessSpecifier::AS_public;
470 bool IsStatic = false;
471};
472
473struct Location {
478
479 bool operator==(const Location &Other) const {
480 return std::tie(StartLineNumber, EndLineNumber, Filename) ==
481 std::tie(Other.StartLineNumber, Other.EndLineNumber, Other.Filename);
482 }
483
484 bool operator!=(const Location &Other) const { return !(*this == Other); }
485
486 // This operator is used to sort a vector of Locations.
487 // No specific order (attributes more important than others) is required. Any
488 // sort is enough, the order is only needed to call std::unique after sorting
489 // the vector.
490 bool operator<(const Location &Other) const {
491 return std::tie(StartLineNumber, EndLineNumber, Filename) <
492 std::tie(Other.StartLineNumber, Other.EndLineNumber, Other.Filename);
493 }
494
495 StringRef Filename = {};
498 bool IsFileInRootDir = false;
499};
500
501/// A base struct for Infos.
502struct Info {
504 StringRef Name = StringRef(), StringRef Path = StringRef())
506
507 Info(const Info &Other, llvm::BumpPtrAllocator &Arena);
508 Info(const Info &Other) = delete;
509 Info(Info &&Other) = default;
510
511 Info &operator=(Info &&Other) = default;
512
513 void mergeBase(Info &&I);
514 bool mergeable(const Info &Other);
515
516 StringRef extractName() const;
517
518 /// Returns the file path for this Info relative to CurrentPath.
519 StringRef getRelativeFilePath(const StringRef &CurrentPath) const;
520
521 /// Returns the basename that should be used for this Info.
522 StringRef getFileBaseName() const;
523
524 // Path of directory where the clang-doc generated file will be saved.
525 StringRef Path = {};
526
527 // Unqualified name of the decl.
528 StringRef Name = {};
529
530 // The name used for the file that this info is documented in.
531 // In the JSON generator, infos are documented in files with mangled names.
532 // Thus, we keep track of the physical filename for linking purposes.
533 StringRef DocumentationFileName = {};
534
535 // List of parent namespaces for this decl.
536 llvm::ArrayRef<Reference> Namespace;
537
538 // Unique identifier for the decl described by this Info.
540
541 // Currently only used for namespaces and records.
543
544 // InfoType of this particular Info.
546
547 // Comment description of this decl.
549};
550
551inline Context::Context(const Info &I)
552 : Reference(I.USR, I.Name, I.IT, I.Name, I.Path, I.DocumentationFileName) {}
553
554// Info for namespaces.
555struct NamespaceInfo : public Info {
556 NamespaceInfo(SymbolID USR = SymbolID(), StringRef Name = StringRef(),
557 StringRef Path = StringRef());
558
559 static bool classof(const Info *I) { return I->IT == InfoType::IT_namespace; }
560
561 void merge(NamespaceInfo &&I);
562
564};
565
566// Info for symbols.
567struct SymbolInfo : public Info {
569 StringRef Name = StringRef(), StringRef Path = StringRef())
570 : Info(IT, USR, Name, Path) {}
571
572 SymbolInfo(const SymbolInfo &Other, llvm::BumpPtrAllocator &Arena);
573
574 // SymbolInfo is an intermediate base without its own InfoType. It covers
575 // every Info* kind that relates to symbols, which mostly just excludes
576 // Namespace in the current schema.
577 static bool classof(const Info *I) {
578 switch (I->IT) {
586 return true;
587 default:
588 return false;
589 }
590 }
591
592 void merge(SymbolInfo &&I);
593
594 bool operator<(const SymbolInfo &Other) const {
595 // Sort by declaration location since we want the doc to be
596 // generated in the order of the source code.
597 // If the declaration location is the same, or not present
598 // we sort by defined location otherwise fallback to the extracted name
599 if (!Loc.empty() && !Other.Loc.empty() && Loc.front() != Other.Loc.front())
600 return Loc.front() < Other.Loc.front();
601
602 if (DefLoc && Other.DefLoc && *DefLoc != *Other.DefLoc)
603 return *DefLoc < *Other.DefLoc;
604
605 return extractName() < Other.extractName();
606 }
607
608 std::optional<Location> DefLoc; // Location where this decl is defined.
609 DocList<Location> Loc = {}; // Locations where this decl is declared.
610 StringRef MangledName = {};
611 bool IsStatic = false;
612};
613
614struct FriendInfo : public SymbolInfo {
618 const StringRef Name = StringRef())
619 : SymbolInfo(IT, USR, Name) {}
620 FriendInfo(const FriendInfo &Other, llvm::BumpPtrAllocator &Arena);
621
622 static bool classof(const Info *I) { return I->IT == InfoType::IT_friend; }
623
624 bool mergeable(const FriendInfo &Other);
625 void merge(FriendInfo &&Other);
626
628 std::optional<TemplateInfo> Template;
629 std::optional<TypeInfo> ReturnType;
630 llvm::ArrayRef<FieldTypeInfo> Params = {};
631 bool IsClass = false;
632};
633
634struct VarInfo : public SymbolInfo {
637
638 static bool classof(const Info *I) { return I->IT == InfoType::IT_variable; }
639
640 void merge(VarInfo &&I);
641
643};
644
645// TODO: Expand to allow for documenting templating and default args.
646// Info for functions.
647struct FunctionInfo : public SymbolInfo {
650
651 static bool classof(const Info *I) { return I->IT == InfoType::IT_function; }
652
653 void merge(FunctionInfo &&I);
654
657 llvm::ArrayRef<FieldTypeInfo> Params = {};
658 StringRef Prototype = {};
659
660 // When present, this function is a template or specialization.
661 std::optional<TemplateInfo> Template;
662
663 // Access level for this method (public, private, protected, none).
664 // AS_public is set as default because the bitcode writer requires the enum
665 // with value 0 to be used as the default.
666 // (AS_public = 0, AS_protected = 1, AS_private = 2, AS_none = 3)
667 AccessSpecifier Access = AccessSpecifier::AS_public;
668
669 bool IsMethod = false;
670};
671
672// TODO: Expand to allow for documenting templating, inheritance access,
673// friend classes
674// Info for types.
675struct RecordInfo : public SymbolInfo {
676 RecordInfo(SymbolID USR = SymbolID(), StringRef Name = StringRef(),
677 StringRef Path = StringRef());
678
679 RecordInfo(const RecordInfo &Other, llvm::BumpPtrAllocator &Arena);
680
681 static bool classof(const Info *I) { return I->IT == InfoType::IT_record; }
682
683 void merge(RecordInfo &&I);
684
685 // Type of this record (struct, class, union, interface).
686 TagTypeKind TagType = TagTypeKind::Struct;
687
688 // Indicates if the record was declared using a typedef. Things like anonymous
689 // structs in a typedef:
690 // typedef struct { ... } foo_t;
691 // are converted into records with the typedef as the Name + this flag set.
692 bool IsTypeDef = false;
693
694 // When present, this record is a template or specialization.
695 std::optional<TemplateInfo> Template;
696
697 llvm::ArrayRef<MemberTypeInfo> Members =
698 {}; // List of info about record members.
699 llvm::ArrayRef<Reference> Parents =
700 {}; // List of base/parent records (does not include virtual parents).
701 llvm::ArrayRef<Reference> VirtualParents =
702 {}; // List of virtual base/parent records.
703
704 llvm::ArrayRef<BaseRecordInfo> Bases =
705 {}; // List of base/parent records; this includes inherited methods and
706 // attributes
707
708 llvm::ArrayRef<FriendInfo> Friends = {};
709
711};
712
713// Info for typedef and using statements.
714struct TypedefInfo : public SymbolInfo {
717
718 static bool classof(const Info *I) { return I->IT == InfoType::IT_typedef; }
719
720 void merge(TypedefInfo &&I);
721
723
724 // Only type aliases can be templates.
725 std::optional<TemplateInfo> Template;
726
727 // Underlying type declaration
728 StringRef TypeDeclaration = {};
729
730 // Indicates if this is a new C++ "using"-style typedef:
731 // using MyVector = std::vector<int>
732 // False means it's a C-style typedef:
733 // typedef std::vector<int> MyVector;
734 bool IsUsing = false;
735};
736
737struct BaseRecordInfo : public RecordInfo {
739 BaseRecordInfo(const BaseRecordInfo &Other, llvm::BumpPtrAllocator &Arena);
740 BaseRecordInfo(SymbolID USR, StringRef Name, StringRef Path, bool IsVirtual,
741 AccessSpecifier Access, bool IsParent);
742
743 // Access level associated with this inherited info (public, protected,
744 // private).
745 AccessSpecifier Access = AccessSpecifier::AS_public;
746 // Indicates if base corresponds to a virtual inheritance
747 bool IsVirtual = false;
748 bool IsParent = false; // Indicates if this base is a direct parent
749};
750
751// Information for a single possible value of an enumeration.
753 explicit EnumValueInfo(StringRef Name = StringRef(),
754 StringRef Value = StringRef("0"),
755 StringRef ValueExpr = StringRef())
758
759 EnumValueInfo(const EnumValueInfo &Other, llvm::BumpPtrAllocator &Arena);
760
761 bool operator==(const EnumValueInfo &Other) const {
762 return std::tie(Name, Value, ValueExpr) ==
763 std::tie(Other.Name, Other.Value, Other.ValueExpr);
764 }
765
766 StringRef Name = {};
767
768 // The computed value of the enumeration constant. This could be the result of
769 // evaluating the ValueExpr, or it could be automatically generated according
770 // to C rules.
771 StringRef Value = {};
772
773 // Stores the user-supplied initialization expression for this enumeration
774 // constant. This will be empty for implicit enumeration values.
775 StringRef ValueExpr = {};
776
777 /// Comment description of this field.
779};
780
781// TODO: Expand to allow for documenting templating.
782// Info for types.
783struct EnumInfo : public SymbolInfo {
786
787 static bool classof(const Info *I) { return I->IT == InfoType::IT_enum; }
788
789 void merge(EnumInfo &&I);
790
791 // Indicates whether this enum is scoped (e.g. enum class).
792 bool Scoped = false;
793
794 // Set to nonempty to the type when this is an explicitly typed enum. For
795 // enum Foo : short { ... };
796 // this will be "short".
797 std::optional<TypeInfo> BaseType;
798
799 llvm::ArrayRef<EnumValueInfo> Members = {}; // List of enum members.
800};
801
802struct ConceptInfo : public SymbolInfo {
805
806 static bool classof(const Info *I) { return I->IT == InfoType::IT_concept; }
807
808 void merge(ConceptInfo &&I);
809
810 bool IsType = false;
812 StringRef ConstraintExpression = {};
813};
814
815struct Index : public Reference {
816 Index() = default;
817 Index(StringRef Name) : Reference(SymbolID(), Name) {}
820 Index(SymbolID USR, StringRef Name, InfoType IT, StringRef Path)
821 : Reference(USR, Name, IT, Name, Path) {}
822 // This is used to look for a USR in a vector of Indexes using std::find
823 bool operator==(const SymbolID &Other) const { return USR == Other; }
824 bool operator<(const Index &Other) const;
825
826 std::optional<StringRef> JumpToSection;
827 llvm::StringMap<Index> Children;
828
829 std::vector<const Index *> getSortedChildren() const;
830 void sort();
831};
832
833// TODO: Add functionality to include separate markdown pages.
834
835// A standalone function to call to merge a vector of infos into one.
836// This assumes that all infos in the vector are of the same type, and will fail
837// if they are different.
838llvm::Expected<Info *> mergeInfos(SmallVectorImpl<Info *> &Values);
839
840// Merges a single new Info into an existing Reduced Info (allocating it if
841// needed).
842llvm::Error mergeSingleInfo(doc::Info *&Reduced, doc::Info *NewInfo,
843 llvm::BumpPtrAllocator &Arena);
844
846 ClangDocContext(tooling::ExecutionContext *ECtx, StringRef ProjectName,
847 bool PublicOnly, StringRef OutDirectory, StringRef SourceRoot,
848 StringRef RepositoryUrl, StringRef RepositoryCodeLinePrefix,
849 StringRef Base, std::vector<std::string> UserStylesheets,
850 clang::DiagnosticsEngine &Diags, OutputFormatTy Format,
851 bool FTimeTrace = false, bool Pretty = false);
852 tooling::ExecutionContext *ECtx;
853 std::string ProjectName; // Name of project clang-doc is documenting.
854 std::string OutDirectory; // Directory for outputting generated files.
855 std::string SourceRoot; // Directory where processed files are stored. Links
856 // to definition locations will only be generated if
857 // the file is in this dir.
858 // URL of repository that hosts code used for links to definition locations.
859 std::optional<std::string> RepositoryUrl;
860 // Prefix of line code for repository.
861 std::optional<std::string> RepositoryLinePrefix;
862 // Path of CSS stylesheets that will be copied to OutDirectory and used to
863 // style all HTML files.
864 std::vector<std::string> UserStylesheets;
865 // JavaScript files that will be imported in all HTML files.
866 std::vector<std::string> JsScripts;
867 // Base directory for remote repositories.
868 StringRef Base;
869 // Maps mustache template types to specific mustache template files.
870 // Ex. comment-template -> /path/to/comment-template.mustache
871 llvm::StringMap<std::string> MustacheTemplates;
872 // A pointer to a DiagnosticsEngine for error reporting.
873 clang::DiagnosticsEngine &Diags;
876 int Granularity; // Granularity of ftime trace
877 bool PublicOnly; // Indicates if only public declarations are documented.
878 bool FTimeTrace; // Indicates if ftime trace is turned on
879 bool Pretty; // Indicates if JSON is emitted with whitespace.
880};
881
882// Ensure arena allocated types remain safe to allocate in the arena.
883// Only trivially destructible types are safe, so enforce that at compile-time.
884static_assert(std::is_trivially_destructible_v<CommentInfo>);
885static_assert(std::is_trivially_destructible_v<ConceptInfo>);
886static_assert(std::is_trivially_destructible_v<ConstraintInfo>);
887static_assert(std::is_trivially_destructible_v<EnumInfo>);
888static_assert(std::is_trivially_destructible_v<FieldTypeInfo>);
889static_assert(std::is_trivially_destructible_v<FriendInfo>);
890static_assert(std::is_trivially_destructible_v<FunctionInfo>);
891static_assert(std::is_trivially_destructible_v<Info>);
892static_assert(std::is_trivially_destructible_v<Location>);
893static_assert(std::is_trivially_destructible_v<MemberTypeInfo>);
894static_assert(std::is_trivially_destructible_v<NamespaceInfo>);
895static_assert(std::is_trivially_destructible_v<RecordInfo>);
896static_assert(std::is_trivially_destructible_v<Reference>);
897static_assert(std::is_trivially_destructible_v<ScopeChildren>);
898static_assert(std::is_trivially_destructible_v<SymbolInfo>);
899static_assert(std::is_trivially_destructible_v<TemplateInfo>);
900static_assert(std::is_trivially_destructible_v<TemplateParamInfo>);
901static_assert(std::is_trivially_destructible_v<TemplateSpecializationInfo>);
902static_assert(std::is_trivially_destructible_v<TypeInfo>);
903static_assert(std::is_trivially_destructible_v<TypedefInfo>);
904static_assert(std::is_trivially_destructible_v<VarInfo>);
905
906} // namespace doc
907} // namespace clang
908
909#endif // LLVM_CLANG_TOOLS_EXTRA_CLANG_DOC_REPRESENTATION_H
static llvm::cl::opt< std::string > RepositoryCodeLinePrefix("repository-line-prefix", llvm::cl::desc("Prefix of line code for repository."), llvm::cl::cat(ClangDocCategory))
This file defines ConcurrentStringPool, a lock-free deduplicating string saver for clang-doc.
clang::find_all_symbols::SymbolInfo SymbolInfo
llvm::StringRef save(const char *S)
Save string S in the arena.
T * allocatePtr(llvm::BumpPtrAllocator &Alloc, Args &&...args)
llvm::simple_ilist< InfoNode< T > > DocList
llvm::BumpPtrAllocator & getPersistentArena()
T * allocatePersistent(Args &&...args)
T * allocateTransient(Args &&...args)
ConcurrentStringPoolImpl<> ConcurrentStringPool
ConcurrentStringPool & getGlobalStringPool()
CommentKind stringToCommentKind(llvm::StringRef KindStr)
llvm::ArrayRef< T > deepCopyArray(llvm::ArrayRef< T > V, llvm::BumpPtrAllocator &Alloc)
InfoNode< T > * allocateListNodePersistent(Args &&...args)
InfoNode< T > * allocateListNode(llvm::BumpPtrAllocator &Alloc, Args &&...args)
StringRef internString(const Twine &T)
llvm::ArrayRef< T > allocateArray(llvm::SmallVectorImpl< T > &V, llvm::BumpPtrAllocator &Alloc)
llvm::Expected< Info * > mergeInfos(SmallVectorImpl< Info * > &Values)
InfoNode< T > * allocateListNodeTransient(Args &&...args)
constexpr SymbolID GlobalNamespaceID
llvm::BumpPtrAllocator & getTransientArena()
std::array< uint8_t, 20 > SymbolID
llvm::StringRef commentKindToString(CommentKind Kind)
llvm::Error mergeSingleInfo(doc::Info *&Reduced, doc::Info *NewInfo, llvm::BumpPtrAllocator &Arena)
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
std::optional< std::string > RepositoryUrl
std::vector< std::string > UserStylesheets
llvm::StringMap< std::string > MustacheTemplates
std::vector< std::string > JsScripts
tooling::ExecutionContext * ECtx
clang::DiagnosticsEngine & Diags
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, bool Pretty=false)
std::optional< std::string > RepositoryLinePrefix
ArrayRef< StringRef > Args
CommentInfo & operator=(CommentInfo &&Other)=default
CommentInfo(CommentKind Kind, llvm::ArrayRef< CommentInfo > Children={}, StringRef Text=StringRef(), StringRef Name=StringRef(), StringRef CloseName=StringRef(), StringRef Direction=StringRef(), StringRef ParamName=StringRef(), bool Explicit=false, bool SelfClosing=false, llvm::ArrayRef< StringRef > AttrKeys={}, llvm::ArrayRef< StringRef > AttrValues={})
bool operator<(const CommentInfo &Other) const
CommentInfo(CommentInfo &&Other)=default
bool operator==(const CommentInfo &Other) const
CommentInfo(const CommentInfo &Other)=default
ArrayRef< CommentInfo > Children
CommentInfo & operator=(const CommentInfo &Other)=default
ArrayRef< StringRef > AttrKeys
ArrayRef< StringRef > AttrValues
static bool classof(const Info *I)
void merge(ConceptInfo &&I)
ConstraintInfo(SymbolID USR, StringRef Name)
Context(SymbolID USR, StringRef Name, InfoType IT, StringRef QualName, StringRef Path, StringRef DocumentationFileName)
llvm::ArrayRef< EnumValueInfo > Members
void merge(EnumInfo &&I)
std::optional< TypeInfo > BaseType
static bool classof(const Info *I)
DocList< CommentInfo > Description
Comment description of this field.
bool operator==(const EnumValueInfo &Other) const
EnumValueInfo(StringRef Name=StringRef(), StringRef Value=StringRef("0"), StringRef ValueExpr=StringRef())
FieldTypeInfo(const TypeInfo &TI, StringRef Name=StringRef(), StringRef DefaultValue=StringRef())
bool operator==(const FieldTypeInfo &Other) const
FriendInfo(const InfoType IT, const SymbolID &USR, const StringRef Name=StringRef())
std::optional< TypeInfo > ReturnType
void merge(FriendInfo &&Other)
llvm::ArrayRef< FieldTypeInfo > Params
static bool classof(const Info *I)
std::optional< TemplateInfo > Template
bool mergeable(const FriendInfo &Other)
FunctionInfo(SymbolID USR=SymbolID())
static bool classof(const Info *I)
llvm::ArrayRef< FieldTypeInfo > Params
void merge(FunctionInfo &&I)
std::optional< TemplateInfo > Template
Index(StringRef Name, StringRef JumpToSection)
bool operator<(const Index &Other) const
std::optional< StringRef > JumpToSection
bool operator==(const SymbolID &Other) const
Index(SymbolID USR, StringRef Name, InfoType IT, StringRef Path)
Index(StringRef Name)
llvm::StringMap< Index > Children
std::vector< const Index * > getSortedChildren() const
bool operator<(const InfoNode< T > &Other) const
bool operator!=(const InfoNode< T > &Other) const
const T * operator->() const
const T & operator*() const
bool operator==(const InfoNode< T > &Other) const
A base struct for Infos.
StringRef getRelativeFilePath(const StringRef &CurrentPath) const
Returns the file path for this Info relative to CurrentPath.
Info & operator=(Info &&Other)=default
Info(InfoType IT=InfoType::IT_default, SymbolID USR=SymbolID(), StringRef Name=StringRef(), StringRef Path=StringRef())
bool mergeable(const Info &Other)
DocList< CommentInfo > Description
llvm::ArrayRef< Reference > Namespace
StringRef extractName() const
StringRef DocumentationFileName
void mergeBase(Info &&I)
Info(Info &&Other)=default
Info(const Info &Other)=delete
StringRef getFileBaseName() const
Returns the basename that should be used for this Info.
bool operator==(const Location &Other) const
Location(int StartLineNumber=0, int EndLineNumber=0, StringRef Filename=StringRef(), bool IsFileInRootDir=false)
bool operator<(const Location &Other) const
bool operator!=(const Location &Other) const
MemberTypeInfo(const TypeInfo &TI, StringRef Name, AccessSpecifier Access, bool IsStatic=false)
DocList< CommentInfo > Description
bool operator==(const MemberTypeInfo &Other) const
NamespaceInfo(SymbolID USR=SymbolID(), StringRef Name=StringRef(), StringRef Path=StringRef())
static bool classof(const Info *I)
void merge(NamespaceInfo &&I)
llvm::ArrayRef< BaseRecordInfo > Bases
llvm::ArrayRef< FriendInfo > Friends
RecordInfo(SymbolID USR=SymbolID(), StringRef Name=StringRef(), StringRef Path=StringRef())
llvm::ArrayRef< Reference > VirtualParents
std::optional< TemplateInfo > Template
static bool classof(const Info *I)
llvm::ArrayRef< Reference > Parents
llvm::ArrayRef< MemberTypeInfo > Members
void merge(RecordInfo &&I)
Reference(SymbolID USR, StringRef Name, InfoType IT, StringRef QualName, StringRef Path, StringRef DocumentationFileName)
void merge(Reference &&I)
Reference(SymbolID USR, StringRef Name, InfoType IT, StringRef QualName, StringRef Path=StringRef())
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.
bool operator<(const Reference &Other) const
bool operator==(const Reference &Other) const
DocList< Reference > Records
DocList< EnumInfo > Enums
DocList< ConceptInfo > Concepts
DocList< VarInfo > Variables
DocList< FunctionInfo > Functions
DocList< Reference > Namespaces
DocList< TypedefInfo > Typedefs
SymbolInfo(InfoType IT, SymbolID USR=SymbolID(), StringRef Name=StringRef(), StringRef Path=StringRef())
static bool classof(const Info *I)
DocList< Location > Loc
bool operator<(const SymbolInfo &Other) const
std::optional< Location > DefLoc
void merge(SymbolInfo &&I)
llvm::ArrayRef< TemplateParamInfo > Params
llvm::ArrayRef< ConstraintInfo > Constraints
std::optional< TemplateSpecializationInfo > Specialization
TemplateParamInfo(StringRef Contents)
llvm::ArrayRef< TemplateParamInfo > Params
TypeInfo(StringRef Name, StringRef Path=StringRef())
TypeInfo(const Reference &R)
bool operator==(const TypeInfo &Other) const
static bool classof(const Info *I)
void merge(TypedefInfo &&I)
std::optional< TemplateInfo > Template
TypedefInfo(SymbolID USR=SymbolID())
VarInfo(SymbolID USR)
void merge(VarInfo &&I)
static bool classof(const Info *I)