clang -cc1 -triple x86_64-pc-linux-gnu -analyze -disable-free -disable-llvm-verifier -discard-value-names -main-file-name InheritViz.cpp -analyzer-store=region -analyzer-opt-analyze-nested-blocks -analyzer-checker=core -analyzer-checker=apiModeling -analyzer-checker=unix -analyzer-checker=deadcode -analyzer-checker=cplusplus -analyzer-checker=security.insecureAPI.UncheckedReturn -analyzer-checker=security.insecureAPI.getpw -analyzer-checker=security.insecureAPI.gets -analyzer-checker=security.insecureAPI.mktemp -analyzer-checker=security.insecureAPI.mkstemp -analyzer-checker=security.insecureAPI.vfork -analyzer-checker=nullability.NullPassedToNonnull -analyzer-checker=nullability.NullReturnedFromNonnull -analyzer-output plist -w -analyzer-config-compatibility-mode=true -mrelocation-model pic -pic-level 2 -mthread-model posix -mframe-pointer=none -relaxed-aliasing -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -ffunction-sections -fdata-sections -resource-dir /usr/lib/llvm-10/lib/clang/10.0.0 -D CLANG_VENDOR="Debian " -D _DEBUG -D _GNU_SOURCE -D __STDC_CONSTANT_MACROS -D __STDC_FORMAT_MACROS -D __STDC_LIMIT_MACROS -I /build/llvm-toolchain-snapshot-10~svn373517/build-llvm/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST -I /build/llvm-toolchain-snapshot-10~svn373517/tools/clang/include -I /build/llvm-toolchain-snapshot-10~svn373517/build-llvm/tools/clang/include -I /build/llvm-toolchain-snapshot-10~svn373517/build-llvm/include -I /build/llvm-toolchain-snapshot-10~svn373517/include -U NDEBUG -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/x86_64-linux-gnu/c++/6.3.0 -internal-isystem /usr/lib/gcc/x86_64-linux-gnu/6.3.0/../../../../include/c++/6.3.0/backward -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-10/lib/clang/10.0.0/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -O2 -Wno-unused-parameter -Wwrite-strings -Wno-missing-field-initializers -Wno-long-long -Wno-maybe-uninitialized -Wno-comment -std=c++14 -fdeprecated-macro -fdebug-compilation-dir /build/llvm-toolchain-snapshot-10~svn373517/build-llvm/tools/clang/lib/AST -fdebug-prefix-map=/build/llvm-toolchain-snapshot-10~svn373517=. -ferror-limit 19 -fmessage-length 0 -fvisibility-inlines-hidden -stack-protector 2 -fobjc-runtime=gcc -fno-common -fdiagnostics-show-option -vectorize-loops -vectorize-slp -analyzer-output=html -analyzer-config stable-report-filename=true -faddrsig -o /tmp/scan-build-2019-10-02-234743-9763-1 -x c++ /build/llvm-toolchain-snapshot-10~svn373517/tools/clang/lib/AST/InheritViz.cpp
1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | #include "clang/AST/ASTContext.h" |
16 | #include "clang/AST/Decl.h" |
17 | #include "clang/AST/DeclCXX.h" |
18 | #include "clang/AST/TypeOrdering.h" |
19 | #include "llvm/Support/FileSystem.h" |
20 | #include "llvm/Support/GraphWriter.h" |
21 | #include "llvm/Support/raw_ostream.h" |
22 | #include <map> |
23 | #include <set> |
24 | using namespace clang; |
25 | |
26 | namespace { |
27 | |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | class InheritanceHierarchyWriter { |
34 | ASTContext& Context; |
35 | raw_ostream &Out; |
36 | std::map<QualType, int, QualTypeOrdering> DirectBaseCount; |
37 | std::set<QualType, QualTypeOrdering> KnownVirtualBases; |
38 | |
39 | public: |
40 | InheritanceHierarchyWriter(ASTContext& Context, raw_ostream& Out) |
41 | : Context(Context), Out(Out) { } |
42 | |
43 | void WriteGraph(QualType Type) { |
44 | Out << "digraph \"" << llvm::DOT::EscapeString(Type.getAsString()) |
45 | << "\" {\n"; |
46 | WriteNode(Type, false); |
| 3 | | Calling 'InheritanceHierarchyWriter::WriteNode' | |
|
47 | Out << "}\n"; |
48 | } |
49 | |
50 | protected: |
51 | |
52 | |
53 | void WriteNode(QualType Type, bool FromVirtual); |
54 | |
55 | |
56 | |
57 | |
58 | raw_ostream& WriteNodeReference(QualType Type, bool FromVirtual); |
59 | }; |
60 | } |
61 | |
62 | void InheritanceHierarchyWriter::WriteNode(QualType Type, bool FromVirtual) { |
63 | QualType CanonType = Context.getCanonicalType(Type); |
64 | |
65 | if (FromVirtual) { |
| |
66 | if (KnownVirtualBases.find(CanonType) != KnownVirtualBases.end()) |
67 | return; |
68 | |
69 | |
70 | |
71 | KnownVirtualBases.insert(CanonType); |
72 | } |
73 | |
74 | |
75 | Out << " "; |
76 | WriteNodeReference(Type, FromVirtual); |
77 | |
78 | |
79 | std::string TypeName = Type.getAsString(); |
80 | Out << " [ shape=\"box\", label=\"" << llvm::DOT::EscapeString(TypeName); |
81 | |
82 | |
83 | |
84 | |
85 | if (TypeName != CanonType.getAsString()) { |
| |
86 | Out << "\\n(" << CanonType.getAsString() << ")"; |
87 | } |
88 | |
89 | |
90 | Out << " \"];\n"; |
91 | |
92 | |
93 | const CXXRecordDecl *Decl |
94 | = static_cast<const CXXRecordDecl *>(Type->getAs<RecordType>()->getDecl()); |
| 6 | | Assuming the object is not a 'RecordType' | |
|
| 7 | | Called C++ object pointer is null |
|
95 | for (const auto &Base : Decl->bases()) { |
96 | QualType CanonBaseType = Context.getCanonicalType(Base.getType()); |
97 | |
98 | |
99 | |
100 | if (!Base.isVirtual()) |
101 | ++DirectBaseCount[CanonBaseType]; |
102 | |
103 | |
104 | WriteNode(Base.getType(), Base.isVirtual()); |
105 | |
106 | |
107 | Out << " "; |
108 | WriteNodeReference(Type, FromVirtual); |
109 | Out << " -> "; |
110 | WriteNodeReference(Base.getType(), Base.isVirtual()); |
111 | |
112 | |
113 | if (Base.isVirtual()) { |
114 | Out << " [ style=\"dashed\" ]"; |
115 | } |
116 | Out << ";"; |
117 | } |
118 | } |
119 | |
120 | |
121 | |
122 | |
123 | raw_ostream& |
124 | InheritanceHierarchyWriter::WriteNodeReference(QualType Type, |
125 | bool FromVirtual) { |
126 | QualType CanonType = Context.getCanonicalType(Type); |
127 | |
128 | Out << "Class_" << CanonType.getAsOpaquePtr(); |
129 | if (!FromVirtual) |
130 | Out << "_" << DirectBaseCount[CanonType]; |
131 | return Out; |
132 | } |
133 | |
134 | |
135 | |
136 | void CXXRecordDecl::viewInheritance(ASTContext& Context) const { |
137 | QualType Self = Context.getTypeDeclType(this); |
138 | |
139 | int FD; |
140 | SmallString<128> Filename; |
141 | if (std::error_code EC = llvm::sys::fs::createTemporaryFile( |
| |
142 | Self.getAsString(), "dot", FD, Filename)) { |
143 | llvm::errs() << "Error: " << EC.message() << "\n"; |
144 | return; |
145 | } |
146 | |
147 | llvm::errs() << "Writing '" << Filename << "'... "; |
148 | |
149 | llvm::raw_fd_ostream O(FD, true); |
150 | |
151 | InheritanceHierarchyWriter Writer(Context, O); |
152 | Writer.WriteGraph(Self); |
| 2 | | Calling 'InheritanceHierarchyWriter::WriteGraph' | |
|
153 | llvm::errs() << " done. \n"; |
154 | |
155 | O.close(); |
156 | |
157 | |
158 | DisplayGraph(Filename); |
159 | } |