clang: lib/AST/InheritViz.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14

19#include "llvm/Support/FileSystem.h"

20#include "llvm/Support/GraphWriter.h"

21#include "llvm/Support/raw_ostream.h"

22#include

23#include

24using namespace clang;

25

26namespace {

27

28

29

30

31

32

33class InheritanceHierarchyWriter {

35 raw_ostream &Out;

36 std::map<QualType, int, QualTypeOrdering> DirectBaseCount;

37 std::set<QualType, QualTypeOrdering> KnownVirtualBases;

38

39public:

40 InheritanceHierarchyWriter(ASTContext& Context, raw_ostream& Out)

41 : Context(Context), Out(Out) { }

42

44 Out << "digraph \"" << llvm::DOT::EscapeString(Type.getAsString())

45 << "\" {\n";

46 WriteNode(Type, false);

47 Out << "}\n";

48 }

49

50protected:

51

52

53 void WriteNode(QualType Type, bool FromVirtual);

54

55

56

57

58 raw_ostream& WriteNodeReference(QualType Type, bool FromVirtual);

59};

60}

61

62void InheritanceHierarchyWriter::WriteNode(QualType Type, bool FromVirtual) {

64

65 if (FromVirtual) {

66 if (!KnownVirtualBases.insert(CanonType).second)

67 return;

68

69

70

71 }

72

73

74 Out << " ";

75 WriteNodeReference(Type, FromVirtual);

76

77

79 Out << " [ shape=\"box\", label=\"" << llvm::DOT::EscapeString(TypeName);

80

81

82

83

84 if (TypeName != CanonType.getAsString()) {

85 Out << "\\n(" << CanonType.getAsString() << ")";

86 }

87

88

89 Out << " \"];\n";

90

91

92 const auto *Decl =

94 for (const auto &Base : Decl->bases()) {

96

97

98

99 if (Base.isVirtual())

100 ++DirectBaseCount[CanonBaseType];

101

102

103 WriteNode(Base.getType(), Base.isVirtual());

104

105

106 Out << " ";

107 WriteNodeReference(Type, FromVirtual);

108 Out << " -> ";

109 WriteNodeReference(Base.getType(), Base.isVirtual());

110

111

112 if (Base.isVirtual()) {

113 Out << " [ style=\"dashed\" ]";

114 }

115 Out << ";";

116 }

117}

118

119

120

121

122raw_ostream&

123InheritanceHierarchyWriter::WriteNodeReference(QualType Type,

124 bool FromVirtual) {

126

128 if (!FromVirtual)

129 Out << "_" << DirectBaseCount[CanonType];

130 return Out;

131}

132

133

134

137

138 int FD;

140 if (std::error_code EC = llvm::sys::fs::createTemporaryFile(

142 llvm::errs() << "Error: " << EC.message() << "\n";

143 return;

144 }

145

146 llvm::errs() << "Writing '" << Filename << "'... ";

147

148 llvm::raw_fd_ostream O(FD, true);

149

150 InheritanceHierarchyWriter Writer(Context, O);

151 Writer.WriteGraph(Self);

152 llvm::errs() << " done. \n";

153

154 O.close();

155

156

158}

Defines the clang::ASTContext interface.

Defines the C++ Decl subclasses, other than those for templates (found in DeclTemplate....

Allows QualTypes to be sorted and hence used in maps and sets.

Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...

CanQualType getCanonicalType(QualType T) const

Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...

QualType getTypeDeclType(const TypeDecl *Decl, const TypeDecl *PrevDecl=nullptr) const

Return the unique reference to the type for the specified type declaration.

Represents a C++ struct/union/class.

void viewInheritance(ASTContext &Context) const

Renders and displays an inheritance diagram for this C++ class and all of its base classes (transitiv...

Decl - This represents one declaration (or definition), e.g.

A (possibly-)qualified type.

void * getAsOpaquePtr() const

static std::string getAsString(SplitQualType split, const PrintingPolicy &Policy)

A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...

The base class of the type hierarchy.

const T * castAs() const

Member-template castAs.

The JSON file list parser is used to communicate input to InstallAPI.

@ Self

'self' clause, allowed on Compute and Combined Constructs, plus 'update'.