1 //===-- Serializer.cpp - ClangDoc Serializer --------------------*- 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 #include "Serialize.h" 11 #include "BitcodeWriter.h" 12 #include "clang/AST/Comment.h" 13 #include "clang/Index/USRGeneration.h" 14 #include "llvm/ADT/Hashing.h" 15 #include "llvm/ADT/StringExtras.h" 16 #include "llvm/Support/SHA1.h" 17 18 using clang::comments::FullComment; 19 20 namespace clang { 21 namespace doc { 22 namespace serialize { 23 24 SymbolID hashUSR(llvm::StringRef USR) { 25 return llvm::SHA1::hash(arrayRefFromStringRef(USR)); 26 } 27 28 class ClangDocCommentVisitor 29 : public ConstCommentVisitor<ClangDocCommentVisitor> { 30 public: 31 ClangDocCommentVisitor(CommentInfo &CI) : CurrentCI(CI) {} 32 33 void parseComment(const comments::Comment *C); 34 35 void visitTextComment(const TextComment *C); 36 void visitInlineCommandComment(const InlineCommandComment *C); 37 void visitHTMLStartTagComment(const HTMLStartTagComment *C); 38 void visitHTMLEndTagComment(const HTMLEndTagComment *C); 39 void visitBlockCommandComment(const BlockCommandComment *C); 40 void visitParamCommandComment(const ParamCommandComment *C); 41 void visitTParamCommandComment(const TParamCommandComment *C); 42 void visitVerbatimBlockComment(const VerbatimBlockComment *C); 43 void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C); 44 void visitVerbatimLineComment(const VerbatimLineComment *C); 45 46 private: 47 std::string getCommandName(unsigned CommandID) const; 48 bool isWhitespaceOnly(StringRef S) const; 49 50 CommentInfo &CurrentCI; 51 }; 52 53 void ClangDocCommentVisitor::parseComment(const comments::Comment *C) { 54 CurrentCI.Kind = C->getCommentKindName(); 55 ConstCommentVisitor<ClangDocCommentVisitor>::visit(C); 56 for (comments::Comment *Child : 57 llvm::make_range(C->child_begin(), C->child_end())) { 58 CurrentCI.Children.emplace_back(llvm::make_unique<CommentInfo>()); 59 ClangDocCommentVisitor Visitor(*CurrentCI.Children.back()); 60 Visitor.parseComment(Child); 61 } 62 } 63 64 void ClangDocCommentVisitor::visitTextComment(const TextComment *C) { 65 if (!isWhitespaceOnly(C->getText())) 66 CurrentCI.Text = C->getText(); 67 } 68 69 void ClangDocCommentVisitor::visitInlineCommandComment( 70 const InlineCommandComment *C) { 71 CurrentCI.Name = getCommandName(C->getCommandID()); 72 for (unsigned I = 0, E = C->getNumArgs(); I != E; ++I) 73 CurrentCI.Args.push_back(C->getArgText(I)); 74 } 75 76 void ClangDocCommentVisitor::visitHTMLStartTagComment( 77 const HTMLStartTagComment *C) { 78 CurrentCI.Name = C->getTagName(); 79 CurrentCI.SelfClosing = C->isSelfClosing(); 80 for (unsigned I = 0, E = C->getNumAttrs(); I < E; ++I) { 81 const HTMLStartTagComment::Attribute &Attr = C->getAttr(I); 82 CurrentCI.AttrKeys.push_back(Attr.Name); 83 CurrentCI.AttrValues.push_back(Attr.Value); 84 } 85 } 86 87 void ClangDocCommentVisitor::visitHTMLEndTagComment( 88 const HTMLEndTagComment *C) { 89 CurrentCI.Name = C->getTagName(); 90 CurrentCI.SelfClosing = true; 91 } 92 93 void ClangDocCommentVisitor::visitBlockCommandComment( 94 const BlockCommandComment *C) { 95 CurrentCI.Name = getCommandName(C->getCommandID()); 96 for (unsigned I = 0, E = C->getNumArgs(); I < E; ++I) 97 CurrentCI.Args.push_back(C->getArgText(I)); 98 } 99 100 void ClangDocCommentVisitor::visitParamCommandComment( 101 const ParamCommandComment *C) { 102 CurrentCI.Direction = 103 ParamCommandComment::getDirectionAsString(C->getDirection()); 104 CurrentCI.Explicit = C->isDirectionExplicit(); 105 if (C->hasParamName()) 106 CurrentCI.ParamName = C->getParamNameAsWritten(); 107 } 108 109 void ClangDocCommentVisitor::visitTParamCommandComment( 110 const TParamCommandComment *C) { 111 if (C->hasParamName()) 112 CurrentCI.ParamName = C->getParamNameAsWritten(); 113 } 114 115 void ClangDocCommentVisitor::visitVerbatimBlockComment( 116 const VerbatimBlockComment *C) { 117 CurrentCI.Name = getCommandName(C->getCommandID()); 118 CurrentCI.CloseName = C->getCloseName(); 119 } 120 121 void ClangDocCommentVisitor::visitVerbatimBlockLineComment( 122 const VerbatimBlockLineComment *C) { 123 if (!isWhitespaceOnly(C->getText())) 124 CurrentCI.Text = C->getText(); 125 } 126 127 void ClangDocCommentVisitor::visitVerbatimLineComment( 128 const VerbatimLineComment *C) { 129 if (!isWhitespaceOnly(C->getText())) 130 CurrentCI.Text = C->getText(); 131 } 132 133 bool ClangDocCommentVisitor::isWhitespaceOnly(llvm::StringRef S) const { 134 return std::all_of(S.begin(), S.end(), isspace); 135 } 136 137 std::string ClangDocCommentVisitor::getCommandName(unsigned CommandID) const { 138 const CommandInfo *Info = CommandTraits::getBuiltinCommandInfo(CommandID); 139 if (Info) 140 return Info->Name; 141 // TODO: Add parsing for \file command. 142 return "<not a builtin command>"; 143 } 144 145 // Serializing functions. 146 147 template <typename T> static std::string serialize(T &I) { 148 SmallString<2048> Buffer; 149 llvm::BitstreamWriter Stream(Buffer); 150 ClangDocBitcodeWriter Writer(Stream); 151 Writer.emitBlock(I); 152 return Buffer.str().str(); 153 } 154 155 static void parseFullComment(const FullComment *C, CommentInfo &CI) { 156 ClangDocCommentVisitor Visitor(CI); 157 Visitor.parseComment(C); 158 } 159 160 static SymbolID getUSRForDecl(const Decl *D) { 161 llvm::SmallString<128> USR; 162 if (index::generateUSRForDecl(D, USR)) 163 return SymbolID(); 164 return hashUSR(USR); 165 } 166 167 static RecordDecl *getDeclForType(const QualType &T) { 168 auto *Ty = T->getAs<RecordType>(); 169 if (!Ty) 170 return nullptr; 171 return Ty->getDecl()->getDefinition(); 172 } 173 174 static void parseFields(RecordInfo &I, const RecordDecl *D) { 175 for (const FieldDecl *F : D->fields()) { 176 if (const auto *T = getDeclForType(F->getTypeSourceInfo()->getType())) { 177 // Use getAccessUnsafe so that we just get the default AS_none if it's not 178 // valid, as opposed to an assert. 179 if (const auto *N = dyn_cast<EnumDecl>(T)) { 180 I.Members.emplace_back(getUSRForDecl(T), N->getNameAsString(), 181 InfoType::IT_enum, F->getNameAsString(), 182 N->getAccessUnsafe()); 183 continue; 184 } else if (const auto *N = dyn_cast<RecordDecl>(T)) { 185 I.Members.emplace_back(getUSRForDecl(T), N->getNameAsString(), 186 InfoType::IT_record, F->getNameAsString(), 187 N->getAccessUnsafe()); 188 continue; 189 } 190 } 191 I.Members.emplace_back(F->getTypeSourceInfo()->getType().getAsString(), 192 F->getNameAsString(), F->getAccessUnsafe()); 193 } 194 } 195 196 static void parseEnumerators(EnumInfo &I, const EnumDecl *D) { 197 for (const EnumConstantDecl *E : D->enumerators()) 198 I.Members.emplace_back(E->getNameAsString()); 199 } 200 201 static void parseParameters(FunctionInfo &I, const FunctionDecl *D) { 202 for (const ParmVarDecl *P : D->parameters()) { 203 if (const auto *T = getDeclForType(P->getOriginalType())) { 204 if (const auto *N = dyn_cast<EnumDecl>(T)) { 205 I.Params.emplace_back(getUSRForDecl(N), N->getNameAsString(), 206 InfoType::IT_enum, P->getNameAsString()); 207 continue; 208 } else if (const auto *N = dyn_cast<RecordDecl>(T)) { 209 I.Params.emplace_back(getUSRForDecl(N), N->getNameAsString(), 210 InfoType::IT_record, P->getNameAsString()); 211 continue; 212 } 213 } 214 I.Params.emplace_back(P->getOriginalType().getAsString(), 215 P->getNameAsString()); 216 } 217 } 218 219 static void parseBases(RecordInfo &I, const CXXRecordDecl *D) { 220 for (const CXXBaseSpecifier &B : D->bases()) { 221 if (B.isVirtual()) 222 continue; 223 if (const auto *P = getDeclForType(B.getType())) 224 I.Parents.emplace_back(getUSRForDecl(P), P->getNameAsString(), 225 InfoType::IT_record); 226 else 227 I.Parents.emplace_back(B.getType().getAsString()); 228 } 229 for (const CXXBaseSpecifier &B : D->vbases()) { 230 if (const auto *P = getDeclForType(B.getType())) 231 I.VirtualParents.emplace_back(getUSRForDecl(P), P->getNameAsString(), 232 InfoType::IT_record); 233 else 234 I.VirtualParents.emplace_back(B.getType().getAsString()); 235 } 236 } 237 238 template <typename T> 239 static void 240 populateParentNamespaces(llvm::SmallVector<Reference, 4> &Namespaces, 241 const T *D) { 242 const auto *DC = dyn_cast<DeclContext>(D); 243 while ((DC = DC->getParent())) { 244 if (const auto *N = dyn_cast<NamespaceDecl>(DC)) 245 Namespaces.emplace_back(getUSRForDecl(N), N->getNameAsString(), 246 InfoType::IT_namespace); 247 else if (const auto *N = dyn_cast<RecordDecl>(DC)) 248 Namespaces.emplace_back(getUSRForDecl(N), N->getNameAsString(), 249 InfoType::IT_record); 250 else if (const auto *N = dyn_cast<FunctionDecl>(DC)) 251 Namespaces.emplace_back(getUSRForDecl(N), N->getNameAsString(), 252 InfoType::IT_function); 253 else if (const auto *N = dyn_cast<EnumDecl>(DC)) 254 Namespaces.emplace_back(getUSRForDecl(N), N->getNameAsString(), 255 InfoType::IT_enum); 256 } 257 } 258 259 template <typename T> 260 static void populateInfo(Info &I, const T *D, const FullComment *C) { 261 I.USR = getUSRForDecl(D); 262 I.Name = D->getNameAsString(); 263 populateParentNamespaces(I.Namespace, D); 264 if (C) { 265 I.Description.emplace_back(); 266 parseFullComment(C, I.Description.back()); 267 } 268 } 269 270 template <typename T> 271 static void populateSymbolInfo(SymbolInfo &I, const T *D, const FullComment *C, 272 int LineNumber, StringRef Filename) { 273 populateInfo(I, D, C); 274 if (D->isThisDeclarationADefinition()) 275 I.DefLoc.emplace(LineNumber, Filename); 276 else 277 I.Loc.emplace_back(LineNumber, Filename); 278 } 279 280 static void populateFunctionInfo(FunctionInfo &I, const FunctionDecl *D, 281 const FullComment *FC, int LineNumber, 282 StringRef Filename) { 283 populateSymbolInfo(I, D, FC, LineNumber, Filename); 284 if (const auto *T = getDeclForType(D->getReturnType())) { 285 if (dyn_cast<EnumDecl>(T)) 286 I.ReturnType = 287 TypeInfo(getUSRForDecl(T), T->getNameAsString(), InfoType::IT_enum); 288 else if (dyn_cast<RecordDecl>(T)) 289 I.ReturnType = 290 TypeInfo(getUSRForDecl(T), T->getNameAsString(), InfoType::IT_record); 291 } else { 292 I.ReturnType = TypeInfo(D->getReturnType().getAsString()); 293 } 294 parseParameters(I, D); 295 } 296 297 std::string emitInfo(const NamespaceDecl *D, const FullComment *FC, 298 int LineNumber, llvm::StringRef File) { 299 NamespaceInfo I; 300 populateInfo(I, D, FC); 301 return serialize(I); 302 } 303 304 std::string emitInfo(const RecordDecl *D, const FullComment *FC, int LineNumber, 305 llvm::StringRef File) { 306 RecordInfo I; 307 populateSymbolInfo(I, D, FC, LineNumber, File); 308 I.TagType = D->getTagKind(); 309 parseFields(I, D); 310 if (const auto *C = dyn_cast<CXXRecordDecl>(D)) 311 parseBases(I, C); 312 return serialize(I); 313 } 314 315 std::string emitInfo(const FunctionDecl *D, const FullComment *FC, 316 int LineNumber, llvm::StringRef File) { 317 FunctionInfo I; 318 populateFunctionInfo(I, D, FC, LineNumber, File); 319 I.Access = clang::AccessSpecifier::AS_none; 320 return serialize(I); 321 } 322 323 std::string emitInfo(const CXXMethodDecl *D, const FullComment *FC, 324 int LineNumber, llvm::StringRef File) { 325 FunctionInfo I; 326 populateFunctionInfo(I, D, FC, LineNumber, File); 327 I.IsMethod = true; 328 I.Parent = Reference{getUSRForDecl(D->getParent()), 329 D->getParent()->getNameAsString(), InfoType::IT_record}; 330 I.Access = D->getAccess(); 331 return serialize(I); 332 } 333 334 std::string emitInfo(const EnumDecl *D, const FullComment *FC, int LineNumber, 335 llvm::StringRef File) { 336 EnumInfo I; 337 populateSymbolInfo(I, D, FC, LineNumber, File); 338 I.Scoped = D->isScoped(); 339 parseEnumerators(I, D); 340 return serialize(I); 341 } 342 343 } // namespace serialize 344 } // namespace doc 345 } // namespace clang 346