API.h 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ExtractAPI/API.h -----------------------------------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file defines the APIRecord-based structs and the APISet class.
  16. ///
  17. /// Clang ExtractAPI is a tool to collect API information from a given set of
  18. /// header files. The structures in this file describe data representations of
  19. /// the API information collected for various kinds of symbols.
  20. ///
  21. //===----------------------------------------------------------------------===//
  22. #ifndef LLVM_CLANG_EXTRACTAPI_API_H
  23. #define LLVM_CLANG_EXTRACTAPI_API_H
  24. #include "clang/AST/Decl.h"
  25. #include "clang/AST/DeclObjC.h"
  26. #include "clang/AST/RawCommentList.h"
  27. #include "clang/Basic/SourceLocation.h"
  28. #include "clang/ExtractAPI/AvailabilityInfo.h"
  29. #include "clang/ExtractAPI/DeclarationFragments.h"
  30. #include "llvm/ADT/MapVector.h"
  31. #include "llvm/ADT/StringRef.h"
  32. #include "llvm/ADT/Triple.h"
  33. #include "llvm/Support/Allocator.h"
  34. #include "llvm/Support/Casting.h"
  35. #include <memory>
  36. #include <type_traits>
  37. namespace clang {
  38. namespace extractapi {
  39. /// DocComment is a vector of RawComment::CommentLine.
  40. ///
  41. /// Each line represents one line of striped documentation comment,
  42. /// with source range information. This simplifies calculating the source
  43. /// location of a character in the doc comment for pointing back to the source
  44. /// file.
  45. /// e.g.
  46. /// \code
  47. /// /// This is a documentation comment
  48. /// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' First line.
  49. /// /// with multiple lines.
  50. /// ^~~~~~~~~~~~~~~~~~~~~~~' Second line.
  51. /// \endcode
  52. using DocComment = std::vector<RawComment::CommentLine>;
  53. // Classes deriving from APIRecord need to have USR be the first constructor
  54. // argument. This is so that they are compatible with `addTopLevelRecord`
  55. // defined in API.cpp
  56. /// The base representation of an API record. Holds common symbol information.
  57. struct APIRecord {
  58. /// Discriminator for LLVM-style RTTI (dyn_cast<> et al.)
  59. enum RecordKind {
  60. RK_Unknown,
  61. RK_GlobalFunction,
  62. RK_GlobalVariable,
  63. RK_EnumConstant,
  64. RK_Enum,
  65. RK_StructField,
  66. RK_Struct,
  67. RK_ObjCInstanceProperty,
  68. RK_ObjCClassProperty,
  69. RK_ObjCIvar,
  70. RK_ObjCClassMethod,
  71. RK_ObjCInstanceMethod,
  72. RK_ObjCInterface,
  73. RK_ObjCCategory,
  74. RK_ObjCProtocol,
  75. RK_MacroDefinition,
  76. RK_Typedef,
  77. };
  78. /// Stores information about the context of the declaration of this API.
  79. /// This is roughly analogous to the DeclContext hierarchy for an AST Node.
  80. struct HierarchyInformation {
  81. /// The USR of the parent API.
  82. StringRef ParentUSR;
  83. /// The name of the parent API.
  84. StringRef ParentName;
  85. /// The record kind of the parent API.
  86. RecordKind ParentKind = RK_Unknown;
  87. /// A pointer to the parent APIRecord if known.
  88. APIRecord *ParentRecord = nullptr;
  89. HierarchyInformation() = default;
  90. HierarchyInformation(StringRef ParentUSR, StringRef ParentName,
  91. RecordKind Kind, APIRecord *ParentRecord = nullptr)
  92. : ParentUSR(ParentUSR), ParentName(ParentName), ParentKind(Kind),
  93. ParentRecord(ParentRecord) {}
  94. bool empty() const {
  95. return ParentUSR.empty() && ParentName.empty() &&
  96. ParentKind == RK_Unknown && ParentRecord == nullptr;
  97. }
  98. };
  99. StringRef USR;
  100. StringRef Name;
  101. PresumedLoc Location;
  102. AvailabilitySet Availabilities;
  103. LinkageInfo Linkage;
  104. /// Documentation comment lines attached to this symbol declaration.
  105. DocComment Comment;
  106. /// Declaration fragments of this symbol declaration.
  107. DeclarationFragments Declaration;
  108. /// SubHeading provides a more detailed representation than the plain
  109. /// declaration name.
  110. ///
  111. /// SubHeading is an array of declaration fragments of tagged declaration
  112. /// name, with potentially more tokens (for example the \c +/- symbol for
  113. /// Objective-C class/instance methods).
  114. DeclarationFragments SubHeading;
  115. /// Information about the parent record of this record.
  116. HierarchyInformation ParentInformation;
  117. /// Whether the symbol was defined in a system header.
  118. bool IsFromSystemHeader;
  119. private:
  120. const RecordKind Kind;
  121. public:
  122. RecordKind getKind() const { return Kind; }
  123. APIRecord() = delete;
  124. APIRecord(RecordKind Kind, StringRef USR, StringRef Name,
  125. PresumedLoc Location, AvailabilitySet Availabilities,
  126. LinkageInfo Linkage, const DocComment &Comment,
  127. DeclarationFragments Declaration, DeclarationFragments SubHeading,
  128. bool IsFromSystemHeader)
  129. : USR(USR), Name(Name), Location(Location),
  130. Availabilities(std::move(Availabilities)), Linkage(Linkage),
  131. Comment(Comment), Declaration(Declaration), SubHeading(SubHeading),
  132. IsFromSystemHeader(IsFromSystemHeader), Kind(Kind) {}
  133. // Pure virtual destructor to make APIRecord abstract
  134. virtual ~APIRecord() = 0;
  135. };
  136. /// This holds information associated with global functions.
  137. struct GlobalFunctionRecord : APIRecord {
  138. FunctionSignature Signature;
  139. GlobalFunctionRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  140. AvailabilitySet Availabilities, LinkageInfo Linkage,
  141. const DocComment &Comment,
  142. DeclarationFragments Declaration,
  143. DeclarationFragments SubHeading,
  144. FunctionSignature Signature, bool IsFromSystemHeader)
  145. : APIRecord(RK_GlobalFunction, USR, Name, Loc, std::move(Availabilities),
  146. Linkage, Comment, Declaration, SubHeading,
  147. IsFromSystemHeader),
  148. Signature(Signature) {}
  149. static bool classof(const APIRecord *Record) {
  150. return Record->getKind() == RK_GlobalFunction;
  151. }
  152. private:
  153. virtual void anchor();
  154. };
  155. /// This holds information associated with global functions.
  156. struct GlobalVariableRecord : APIRecord {
  157. GlobalVariableRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  158. AvailabilitySet Availabilities, LinkageInfo Linkage,
  159. const DocComment &Comment,
  160. DeclarationFragments Declaration,
  161. DeclarationFragments SubHeading, bool IsFromSystemHeader)
  162. : APIRecord(RK_GlobalVariable, USR, Name, Loc, std::move(Availabilities),
  163. Linkage, Comment, Declaration, SubHeading,
  164. IsFromSystemHeader) {}
  165. static bool classof(const APIRecord *Record) {
  166. return Record->getKind() == RK_GlobalVariable;
  167. }
  168. private:
  169. virtual void anchor();
  170. };
  171. /// This holds information associated with enum constants.
  172. struct EnumConstantRecord : APIRecord {
  173. EnumConstantRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  174. AvailabilitySet Availabilities, const DocComment &Comment,
  175. DeclarationFragments Declaration,
  176. DeclarationFragments SubHeading, bool IsFromSystemHeader)
  177. : APIRecord(RK_EnumConstant, USR, Name, Loc, std::move(Availabilities),
  178. LinkageInfo::none(), Comment, Declaration, SubHeading,
  179. IsFromSystemHeader) {}
  180. static bool classof(const APIRecord *Record) {
  181. return Record->getKind() == RK_EnumConstant;
  182. }
  183. private:
  184. virtual void anchor();
  185. };
  186. /// This holds information associated with enums.
  187. struct EnumRecord : APIRecord {
  188. SmallVector<std::unique_ptr<EnumConstantRecord>> Constants;
  189. EnumRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  190. AvailabilitySet Availabilities, const DocComment &Comment,
  191. DeclarationFragments Declaration, DeclarationFragments SubHeading,
  192. bool IsFromSystemHeader)
  193. : APIRecord(RK_Enum, USR, Name, Loc, std::move(Availabilities),
  194. LinkageInfo::none(), Comment, Declaration, SubHeading,
  195. IsFromSystemHeader) {}
  196. static bool classof(const APIRecord *Record) {
  197. return Record->getKind() == RK_Enum;
  198. }
  199. private:
  200. virtual void anchor();
  201. };
  202. /// This holds information associated with struct fields.
  203. struct StructFieldRecord : APIRecord {
  204. StructFieldRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  205. AvailabilitySet Availabilities, const DocComment &Comment,
  206. DeclarationFragments Declaration,
  207. DeclarationFragments SubHeading, bool IsFromSystemHeader)
  208. : APIRecord(RK_StructField, USR, Name, Loc, std::move(Availabilities),
  209. LinkageInfo::none(), Comment, Declaration, SubHeading,
  210. IsFromSystemHeader) {}
  211. static bool classof(const APIRecord *Record) {
  212. return Record->getKind() == RK_StructField;
  213. }
  214. private:
  215. virtual void anchor();
  216. };
  217. /// This holds information associated with structs.
  218. struct StructRecord : APIRecord {
  219. SmallVector<std::unique_ptr<StructFieldRecord>> Fields;
  220. StructRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  221. AvailabilitySet Availabilities, const DocComment &Comment,
  222. DeclarationFragments Declaration,
  223. DeclarationFragments SubHeading, bool IsFromSystemHeader)
  224. : APIRecord(RK_Struct, USR, Name, Loc, std::move(Availabilities),
  225. LinkageInfo::none(), Comment, Declaration, SubHeading,
  226. IsFromSystemHeader) {}
  227. static bool classof(const APIRecord *Record) {
  228. return Record->getKind() == RK_Struct;
  229. }
  230. private:
  231. virtual void anchor();
  232. };
  233. /// This holds information associated with Objective-C properties.
  234. struct ObjCPropertyRecord : APIRecord {
  235. /// The attributes associated with an Objective-C property.
  236. enum AttributeKind : unsigned {
  237. NoAttr = 0,
  238. ReadOnly = 1,
  239. Dynamic = 1 << 2,
  240. };
  241. AttributeKind Attributes;
  242. StringRef GetterName;
  243. StringRef SetterName;
  244. bool IsOptional;
  245. ObjCPropertyRecord(RecordKind Kind, StringRef USR, StringRef Name,
  246. PresumedLoc Loc, AvailabilitySet Availabilities,
  247. const DocComment &Comment,
  248. DeclarationFragments Declaration,
  249. DeclarationFragments SubHeading, AttributeKind Attributes,
  250. StringRef GetterName, StringRef SetterName,
  251. bool IsOptional, bool IsFromSystemHeader)
  252. : APIRecord(Kind, USR, Name, Loc, std::move(Availabilities),
  253. LinkageInfo::none(), Comment, Declaration, SubHeading,
  254. IsFromSystemHeader),
  255. Attributes(Attributes), GetterName(GetterName), SetterName(SetterName),
  256. IsOptional(IsOptional) {}
  257. bool isReadOnly() const { return Attributes & ReadOnly; }
  258. bool isDynamic() const { return Attributes & Dynamic; }
  259. virtual ~ObjCPropertyRecord() = 0;
  260. };
  261. struct ObjCInstancePropertyRecord : ObjCPropertyRecord {
  262. ObjCInstancePropertyRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  263. AvailabilitySet Availabilities,
  264. const DocComment &Comment,
  265. DeclarationFragments Declaration,
  266. DeclarationFragments SubHeading,
  267. AttributeKind Attributes, StringRef GetterName,
  268. StringRef SetterName, bool IsOptional,
  269. bool IsFromSystemHeader)
  270. : ObjCPropertyRecord(RK_ObjCInstanceProperty, USR, Name, Loc,
  271. std::move(Availabilities), Comment, Declaration,
  272. SubHeading, Attributes, GetterName, SetterName,
  273. IsOptional, IsFromSystemHeader) {}
  274. static bool classof(const APIRecord *Record) {
  275. return Record->getKind() == RK_ObjCInstanceProperty;
  276. }
  277. private:
  278. virtual void anchor();
  279. };
  280. struct ObjCClassPropertyRecord : ObjCPropertyRecord {
  281. ObjCClassPropertyRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  282. AvailabilitySet Availabilities,
  283. const DocComment &Comment,
  284. DeclarationFragments Declaration,
  285. DeclarationFragments SubHeading,
  286. AttributeKind Attributes, StringRef GetterName,
  287. StringRef SetterName, bool IsOptional,
  288. bool IsFromSystemHeader)
  289. : ObjCPropertyRecord(RK_ObjCClassProperty, USR, Name, Loc,
  290. std::move(Availabilities), Comment, Declaration,
  291. SubHeading, Attributes, GetterName, SetterName,
  292. IsOptional, IsFromSystemHeader) {}
  293. static bool classof(const APIRecord *Record) {
  294. return Record->getKind() == RK_ObjCClassProperty;
  295. }
  296. private:
  297. virtual void anchor();
  298. };
  299. /// This holds information associated with Objective-C instance variables.
  300. struct ObjCInstanceVariableRecord : APIRecord {
  301. using AccessControl = ObjCIvarDecl::AccessControl;
  302. AccessControl Access;
  303. ObjCInstanceVariableRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  304. AvailabilitySet Availabilities,
  305. const DocComment &Comment,
  306. DeclarationFragments Declaration,
  307. DeclarationFragments SubHeading,
  308. AccessControl Access, bool IsFromSystemHeader)
  309. : APIRecord(RK_ObjCIvar, USR, Name, Loc, std::move(Availabilities),
  310. LinkageInfo::none(), Comment, Declaration, SubHeading,
  311. IsFromSystemHeader),
  312. Access(Access) {}
  313. static bool classof(const APIRecord *Record) {
  314. return Record->getKind() == RK_ObjCIvar;
  315. }
  316. private:
  317. virtual void anchor();
  318. };
  319. /// This holds information associated with Objective-C methods.
  320. struct ObjCMethodRecord : APIRecord {
  321. FunctionSignature Signature;
  322. ObjCMethodRecord() = delete;
  323. ObjCMethodRecord(RecordKind Kind, StringRef USR, StringRef Name,
  324. PresumedLoc Loc, AvailabilitySet Availabilities,
  325. const DocComment &Comment, DeclarationFragments Declaration,
  326. DeclarationFragments SubHeading, FunctionSignature Signature,
  327. bool IsFromSystemHeader)
  328. : APIRecord(Kind, USR, Name, Loc, std::move(Availabilities),
  329. LinkageInfo::none(), Comment, Declaration, SubHeading,
  330. IsFromSystemHeader),
  331. Signature(Signature) {}
  332. virtual ~ObjCMethodRecord() = 0;
  333. };
  334. struct ObjCInstanceMethodRecord : ObjCMethodRecord {
  335. ObjCInstanceMethodRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  336. AvailabilitySet Availabilities,
  337. const DocComment &Comment,
  338. DeclarationFragments Declaration,
  339. DeclarationFragments SubHeading,
  340. FunctionSignature Signature, bool IsFromSystemHeader)
  341. : ObjCMethodRecord(RK_ObjCInstanceMethod, USR, Name, Loc,
  342. std::move(Availabilities), Comment, Declaration,
  343. SubHeading, Signature, IsFromSystemHeader) {}
  344. static bool classof(const APIRecord *Record) {
  345. return Record->getKind() == RK_ObjCInstanceMethod;
  346. }
  347. private:
  348. virtual void anchor();
  349. };
  350. struct ObjCClassMethodRecord : ObjCMethodRecord {
  351. ObjCClassMethodRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  352. AvailabilitySet Availabilities,
  353. const DocComment &Comment,
  354. DeclarationFragments Declaration,
  355. DeclarationFragments SubHeading,
  356. FunctionSignature Signature, bool IsFromSystemHeader)
  357. : ObjCMethodRecord(RK_ObjCClassMethod, USR, Name, Loc,
  358. std::move(Availabilities), Comment, Declaration,
  359. SubHeading, Signature, IsFromSystemHeader) {}
  360. static bool classof(const APIRecord *Record) {
  361. return Record->getKind() == RK_ObjCClassMethod;
  362. }
  363. private:
  364. virtual void anchor();
  365. };
  366. /// This represents a reference to another symbol that might come from external
  367. /// sources.
  368. struct SymbolReference {
  369. StringRef Name;
  370. StringRef USR;
  371. /// The source project/module/product of the referred symbol.
  372. StringRef Source;
  373. SymbolReference() = default;
  374. SymbolReference(StringRef Name, StringRef USR = "", StringRef Source = "")
  375. : Name(Name), USR(USR), Source(Source) {}
  376. SymbolReference(const APIRecord &Record)
  377. : Name(Record.Name), USR(Record.USR) {}
  378. /// Determine if this SymbolReference is empty.
  379. ///
  380. /// \returns true if and only if all \c Name, \c USR, and \c Source is empty.
  381. bool empty() const { return Name.empty() && USR.empty() && Source.empty(); }
  382. };
  383. /// The base representation of an Objective-C container record. Holds common
  384. /// information associated with Objective-C containers.
  385. struct ObjCContainerRecord : APIRecord {
  386. SmallVector<std::unique_ptr<ObjCMethodRecord>> Methods;
  387. SmallVector<std::unique_ptr<ObjCPropertyRecord>> Properties;
  388. SmallVector<std::unique_ptr<ObjCInstanceVariableRecord>> Ivars;
  389. SmallVector<SymbolReference> Protocols;
  390. ObjCContainerRecord() = delete;
  391. ObjCContainerRecord(RecordKind Kind, StringRef USR, StringRef Name,
  392. PresumedLoc Loc, AvailabilitySet Availabilities,
  393. LinkageInfo Linkage, const DocComment &Comment,
  394. DeclarationFragments Declaration,
  395. DeclarationFragments SubHeading, bool IsFromSystemHeader)
  396. : APIRecord(Kind, USR, Name, Loc, std::move(Availabilities), Linkage,
  397. Comment, Declaration, SubHeading, IsFromSystemHeader) {}
  398. virtual ~ObjCContainerRecord() = 0;
  399. };
  400. /// This holds information associated with Objective-C categories.
  401. struct ObjCCategoryRecord : ObjCContainerRecord {
  402. SymbolReference Interface;
  403. ObjCCategoryRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  404. AvailabilitySet Availabilities, const DocComment &Comment,
  405. DeclarationFragments Declaration,
  406. DeclarationFragments SubHeading, SymbolReference Interface,
  407. bool IsFromSystemHeader)
  408. : ObjCContainerRecord(RK_ObjCCategory, USR, Name, Loc,
  409. std::move(Availabilities), LinkageInfo::none(),
  410. Comment, Declaration, SubHeading,
  411. IsFromSystemHeader),
  412. Interface(Interface) {}
  413. static bool classof(const APIRecord *Record) {
  414. return Record->getKind() == RK_ObjCCategory;
  415. }
  416. private:
  417. virtual void anchor();
  418. };
  419. /// This holds information associated with Objective-C interfaces/classes.
  420. struct ObjCInterfaceRecord : ObjCContainerRecord {
  421. SymbolReference SuperClass;
  422. // ObjCCategoryRecord%s are stored in and owned by APISet.
  423. SmallVector<ObjCCategoryRecord *> Categories;
  424. ObjCInterfaceRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  425. AvailabilitySet Availabilities, LinkageInfo Linkage,
  426. const DocComment &Comment,
  427. DeclarationFragments Declaration,
  428. DeclarationFragments SubHeading,
  429. SymbolReference SuperClass, bool IsFromSystemHeader)
  430. : ObjCContainerRecord(RK_ObjCInterface, USR, Name, Loc,
  431. std::move(Availabilities), Linkage, Comment,
  432. Declaration, SubHeading, IsFromSystemHeader),
  433. SuperClass(SuperClass) {}
  434. static bool classof(const APIRecord *Record) {
  435. return Record->getKind() == RK_ObjCInterface;
  436. }
  437. private:
  438. virtual void anchor();
  439. };
  440. /// This holds information associated with Objective-C protocols.
  441. struct ObjCProtocolRecord : ObjCContainerRecord {
  442. ObjCProtocolRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  443. AvailabilitySet Availabilities, const DocComment &Comment,
  444. DeclarationFragments Declaration,
  445. DeclarationFragments SubHeading, bool IsFromSystemHeader)
  446. : ObjCContainerRecord(RK_ObjCProtocol, USR, Name, Loc,
  447. std::move(Availabilities), LinkageInfo::none(),
  448. Comment, Declaration, SubHeading,
  449. IsFromSystemHeader) {}
  450. static bool classof(const APIRecord *Record) {
  451. return Record->getKind() == RK_ObjCProtocol;
  452. }
  453. private:
  454. virtual void anchor();
  455. };
  456. /// This holds information associated with macro definitions.
  457. struct MacroDefinitionRecord : APIRecord {
  458. MacroDefinitionRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  459. DeclarationFragments Declaration,
  460. DeclarationFragments SubHeading,
  461. bool IsFromSystemHeader)
  462. : APIRecord(RK_MacroDefinition, USR, Name, Loc, AvailabilitySet(),
  463. LinkageInfo(), {}, Declaration, SubHeading,
  464. IsFromSystemHeader) {}
  465. static bool classof(const APIRecord *Record) {
  466. return Record->getKind() == RK_MacroDefinition;
  467. }
  468. private:
  469. virtual void anchor();
  470. };
  471. /// This holds information associated with typedefs.
  472. ///
  473. /// Note: Typedefs for anonymous enums and structs typically don't get emitted
  474. /// by the serializers but still get a TypedefRecord. Instead we use the
  475. /// typedef name as a name for the underlying anonymous struct or enum.
  476. struct TypedefRecord : APIRecord {
  477. SymbolReference UnderlyingType;
  478. TypedefRecord(StringRef USR, StringRef Name, PresumedLoc Loc,
  479. AvailabilitySet Availabilities, const DocComment &Comment,
  480. DeclarationFragments Declaration,
  481. DeclarationFragments SubHeading, SymbolReference UnderlyingType,
  482. bool IsFromSystemHeader)
  483. : APIRecord(RK_Typedef, USR, Name, Loc, std::move(Availabilities),
  484. LinkageInfo(), Comment, Declaration, SubHeading,
  485. IsFromSystemHeader),
  486. UnderlyingType(UnderlyingType) {}
  487. static bool classof(const APIRecord *Record) {
  488. return Record->getKind() == RK_Typedef;
  489. }
  490. private:
  491. virtual void anchor();
  492. };
  493. /// Check if a record type has a function signature mixin.
  494. ///
  495. /// This is denoted by the record type having a ``Signature`` field of type
  496. /// FunctionSignature.
  497. template <typename RecordTy>
  498. struct has_function_signature : public std::false_type {};
  499. template <>
  500. struct has_function_signature<GlobalFunctionRecord> : public std::true_type {};
  501. template <>
  502. struct has_function_signature<ObjCMethodRecord> : public std::true_type {};
  503. template <>
  504. struct has_function_signature<ObjCInstanceMethodRecord>
  505. : public std::true_type {};
  506. template <>
  507. struct has_function_signature<ObjCClassMethodRecord> : public std::true_type {};
  508. /// APISet holds the set of API records collected from given inputs.
  509. class APISet {
  510. public:
  511. /// Create and add a global variable record into the API set.
  512. ///
  513. /// Note: the caller is responsible for keeping the StringRef \p Name and
  514. /// \p USR alive. APISet::copyString provides a way to copy strings into
  515. /// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
  516. /// to generate the USR for \c D and keep it alive in APISet.
  517. GlobalVariableRecord *
  518. addGlobalVar(StringRef Name, StringRef USR, PresumedLoc Loc,
  519. AvailabilitySet Availability, LinkageInfo Linkage,
  520. const DocComment &Comment, DeclarationFragments Declaration,
  521. DeclarationFragments SubHeadin, bool IsFromSystemHeaderg);
  522. /// Create and add a function record into the API set.
  523. ///
  524. /// Note: the caller is responsible for keeping the StringRef \p Name and
  525. /// \p USR alive. APISet::copyString provides a way to copy strings into
  526. /// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
  527. /// to generate the USR for \c D and keep it alive in APISet.
  528. GlobalFunctionRecord *
  529. addGlobalFunction(StringRef Name, StringRef USR, PresumedLoc Loc,
  530. AvailabilitySet Availability, LinkageInfo Linkage,
  531. const DocComment &Comment, DeclarationFragments Declaration,
  532. DeclarationFragments SubHeading,
  533. FunctionSignature Signature, bool IsFromSystemHeader);
  534. /// Create and add an enum constant record into the API set.
  535. ///
  536. /// Note: the caller is responsible for keeping the StringRef \p Name and
  537. /// \p USR alive. APISet::copyString provides a way to copy strings into
  538. /// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
  539. /// to generate the USR for \c D and keep it alive in APISet.
  540. EnumConstantRecord *
  541. addEnumConstant(EnumRecord *Enum, StringRef Name, StringRef USR,
  542. PresumedLoc Loc, AvailabilitySet Availability,
  543. const DocComment &Comment, DeclarationFragments Declaration,
  544. DeclarationFragments SubHeading, bool IsFromSystemHeader);
  545. /// Create and add an enum record into the API set.
  546. ///
  547. /// Note: the caller is responsible for keeping the StringRef \p Name and
  548. /// \p USR alive. APISet::copyString provides a way to copy strings into
  549. /// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
  550. /// to generate the USR for \c D and keep it alive in APISet.
  551. EnumRecord *addEnum(StringRef Name, StringRef USR, PresumedLoc Loc,
  552. AvailabilitySet Availability, const DocComment &Comment,
  553. DeclarationFragments Declaration,
  554. DeclarationFragments SubHeading, bool IsFromSystemHeader);
  555. /// Create and add a struct field record into the API set.
  556. ///
  557. /// Note: the caller is responsible for keeping the StringRef \p Name and
  558. /// \p USR alive. APISet::copyString provides a way to copy strings into
  559. /// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
  560. /// to generate the USR for \c D and keep it alive in APISet.
  561. StructFieldRecord *
  562. addStructField(StructRecord *Struct, StringRef Name, StringRef USR,
  563. PresumedLoc Loc, AvailabilitySet Availability,
  564. const DocComment &Comment, DeclarationFragments Declaration,
  565. DeclarationFragments SubHeading, bool IsFromSystemHeader);
  566. /// Create and add a struct record into the API set.
  567. ///
  568. /// Note: the caller is responsible for keeping the StringRef \p Name and
  569. /// \p USR alive. APISet::copyString provides a way to copy strings into
  570. /// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
  571. /// to generate the USR for \c D and keep it alive in APISet.
  572. StructRecord *addStruct(StringRef Name, StringRef USR, PresumedLoc Loc,
  573. AvailabilitySet Availability,
  574. const DocComment &Comment,
  575. DeclarationFragments Declaration,
  576. DeclarationFragments SubHeading,
  577. bool IsFromSystemHeader);
  578. /// Create and add an Objective-C category record into the API set.
  579. ///
  580. /// Note: the caller is responsible for keeping the StringRef \p Name and
  581. /// \p USR alive. APISet::copyString provides a way to copy strings into
  582. /// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
  583. /// to generate the USR for \c D and keep it alive in APISet.
  584. ObjCCategoryRecord *
  585. addObjCCategory(StringRef Name, StringRef USR, PresumedLoc Loc,
  586. AvailabilitySet Availability, const DocComment &Comment,
  587. DeclarationFragments Declaration,
  588. DeclarationFragments SubHeading, SymbolReference Interface,
  589. bool IsFromSystemHeader);
  590. /// Create and add an Objective-C interface record into the API set.
  591. ///
  592. /// Note: the caller is responsible for keeping the StringRef \p Name and
  593. /// \p USR alive. APISet::copyString provides a way to copy strings into
  594. /// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
  595. /// to generate the USR for \c D and keep it alive in APISet.
  596. ObjCInterfaceRecord *
  597. addObjCInterface(StringRef Name, StringRef USR, PresumedLoc Loc,
  598. AvailabilitySet Availability, LinkageInfo Linkage,
  599. const DocComment &Comment, DeclarationFragments Declaration,
  600. DeclarationFragments SubHeading, SymbolReference SuperClass,
  601. bool IsFromSystemHeader);
  602. /// Create and add an Objective-C method record into the API set.
  603. ///
  604. /// Note: the caller is responsible for keeping the StringRef \p Name and
  605. /// \p USR alive. APISet::copyString provides a way to copy strings into
  606. /// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
  607. /// to generate the USR for \c D and keep it alive in APISet.
  608. ObjCMethodRecord *
  609. addObjCMethod(ObjCContainerRecord *Container, StringRef Name, StringRef USR,
  610. PresumedLoc Loc, AvailabilitySet Availability,
  611. const DocComment &Comment, DeclarationFragments Declaration,
  612. DeclarationFragments SubHeading, FunctionSignature Signature,
  613. bool IsInstanceMethod, bool IsFromSystemHeader);
  614. /// Create and add an Objective-C property record into the API set.
  615. ///
  616. /// Note: the caller is responsible for keeping the StringRef \p Name and
  617. /// \p USR alive. APISet::copyString provides a way to copy strings into
  618. /// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
  619. /// to generate the USR for \c D and keep it alive in APISet.
  620. ObjCPropertyRecord *
  621. addObjCProperty(ObjCContainerRecord *Container, StringRef Name, StringRef USR,
  622. PresumedLoc Loc, AvailabilitySet Availability,
  623. const DocComment &Comment, DeclarationFragments Declaration,
  624. DeclarationFragments SubHeading,
  625. ObjCPropertyRecord::AttributeKind Attributes,
  626. StringRef GetterName, StringRef SetterName, bool IsOptional,
  627. bool IsInstanceProperty, bool IsFromSystemHeader);
  628. /// Create and add an Objective-C instance variable record into the API set.
  629. ///
  630. /// Note: the caller is responsible for keeping the StringRef \p Name and
  631. /// \p USR alive. APISet::copyString provides a way to copy strings into
  632. /// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
  633. /// to generate the USR for \c D and keep it alive in APISet.
  634. ObjCInstanceVariableRecord *addObjCInstanceVariable(
  635. ObjCContainerRecord *Container, StringRef Name, StringRef USR,
  636. PresumedLoc Loc, AvailabilitySet Availability, const DocComment &Comment,
  637. DeclarationFragments Declaration, DeclarationFragments SubHeading,
  638. ObjCInstanceVariableRecord::AccessControl Access,
  639. bool IsFromSystemHeader);
  640. /// Create and add an Objective-C protocol record into the API set.
  641. ///
  642. /// Note: the caller is responsible for keeping the StringRef \p Name and
  643. /// \p USR alive. APISet::copyString provides a way to copy strings into
  644. /// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
  645. /// to generate the USR for \c D and keep it alive in APISet.
  646. ObjCProtocolRecord *
  647. addObjCProtocol(StringRef Name, StringRef USR, PresumedLoc Loc,
  648. AvailabilitySet Availability, const DocComment &Comment,
  649. DeclarationFragments Declaration,
  650. DeclarationFragments SubHeading, bool IsFromSystemHeader);
  651. /// Create a macro definition record into the API set.
  652. ///
  653. /// Note: the caller is responsible for keeping the StringRef \p Name and
  654. /// \p USR alive. APISet::copyString provides a way to copy strings into
  655. /// APISet itself, and APISet::recordUSRForMacro(StringRef Name,
  656. /// SourceLocation SL, const SourceManager &SM) is a helper method to generate
  657. /// the USR for the macro and keep it alive in APISet.
  658. MacroDefinitionRecord *addMacroDefinition(StringRef Name, StringRef USR,
  659. PresumedLoc Loc,
  660. DeclarationFragments Declaration,
  661. DeclarationFragments SubHeading,
  662. bool IsFromSystemHeader);
  663. /// Create a typedef record into the API set.
  664. ///
  665. /// Note: the caller is responsible for keeping the StringRef \p Name and
  666. /// \p USR alive. APISet::copyString provides a way to copy strings into
  667. /// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
  668. /// to generate the USR for \c D and keep it alive in APISet.
  669. TypedefRecord *
  670. addTypedef(StringRef Name, StringRef USR, PresumedLoc Loc,
  671. AvailabilitySet Availability, const DocComment &Comment,
  672. DeclarationFragments Declaration, DeclarationFragments SubHeading,
  673. SymbolReference UnderlyingType, bool IsFromSystemHeader);
  674. /// A mapping type to store a set of APIRecord%s with the USR as the key.
  675. template <typename RecordTy,
  676. typename =
  677. std::enable_if_t<std::is_base_of<APIRecord, RecordTy>::value>>
  678. using RecordMap = llvm::MapVector<StringRef, std::unique_ptr<RecordTy>>;
  679. /// Get the target triple for the ExtractAPI invocation.
  680. const llvm::Triple &getTarget() const { return Target; }
  681. /// Get the language used by the APIs.
  682. Language getLanguage() const { return Lang; }
  683. const RecordMap<GlobalFunctionRecord> &getGlobalFunctions() const {
  684. return GlobalFunctions;
  685. }
  686. const RecordMap<GlobalVariableRecord> &getGlobalVariables() const {
  687. return GlobalVariables;
  688. }
  689. const RecordMap<EnumRecord> &getEnums() const { return Enums; }
  690. const RecordMap<StructRecord> &getStructs() const { return Structs; }
  691. const RecordMap<ObjCCategoryRecord> &getObjCCategories() const {
  692. return ObjCCategories;
  693. }
  694. const RecordMap<ObjCInterfaceRecord> &getObjCInterfaces() const {
  695. return ObjCInterfaces;
  696. }
  697. const RecordMap<ObjCProtocolRecord> &getObjCProtocols() const {
  698. return ObjCProtocols;
  699. }
  700. const RecordMap<MacroDefinitionRecord> &getMacros() const { return Macros; }
  701. const RecordMap<TypedefRecord> &getTypedefs() const { return Typedefs; }
  702. /// Finds the APIRecord for a given USR.
  703. ///
  704. /// \returns a pointer to the APIRecord associated with that USR or nullptr.
  705. APIRecord *findRecordForUSR(StringRef USR) const;
  706. /// Generate and store the USR of declaration \p D.
  707. ///
  708. /// Note: The USR string is stored in and owned by Allocator.
  709. ///
  710. /// \returns a StringRef of the generated USR string.
  711. StringRef recordUSR(const Decl *D);
  712. /// Generate and store the USR for a macro \p Name.
  713. ///
  714. /// Note: The USR string is stored in and owned by Allocator.
  715. ///
  716. /// \returns a StringRef to the generate USR string.
  717. StringRef recordUSRForMacro(StringRef Name, SourceLocation SL,
  718. const SourceManager &SM);
  719. /// Copy \p String into the Allocator in this APISet.
  720. ///
  721. /// \returns a StringRef of the copied string in APISet::Allocator.
  722. StringRef copyString(StringRef String);
  723. APISet(const llvm::Triple &Target, Language Lang,
  724. const std::string &ProductName)
  725. : Target(Target), Lang(Lang), ProductName(ProductName) {}
  726. private:
  727. /// BumpPtrAllocator to store generated/copied strings.
  728. ///
  729. /// Note: The main use for this is being able to deduplicate strings.
  730. llvm::BumpPtrAllocator StringAllocator;
  731. const llvm::Triple Target;
  732. const Language Lang;
  733. llvm::DenseMap<StringRef, APIRecord *> USRBasedLookupTable;
  734. RecordMap<GlobalFunctionRecord> GlobalFunctions;
  735. RecordMap<GlobalVariableRecord> GlobalVariables;
  736. RecordMap<EnumRecord> Enums;
  737. RecordMap<StructRecord> Structs;
  738. RecordMap<ObjCCategoryRecord> ObjCCategories;
  739. RecordMap<ObjCInterfaceRecord> ObjCInterfaces;
  740. RecordMap<ObjCProtocolRecord> ObjCProtocols;
  741. RecordMap<MacroDefinitionRecord> Macros;
  742. RecordMap<TypedefRecord> Typedefs;
  743. public:
  744. const std::string ProductName;
  745. };
  746. } // namespace extractapi
  747. } // namespace clang
  748. #endif // LLVM_CLANG_EXTRACTAPI_API_H
  749. #ifdef __GNUC__
  750. #pragma GCC diagnostic pop
  751. #endif