Skip to content

Commit 89f6b26

Browse files
committed
[clang][extract-api] Refactor ExtractAPI and improve docs
- The name SymbolGraph is inappropriate and confusing for the new library for clang-extract-api. Refactor and rename things to make it clear that ExtractAPI is the core functionality and SymbolGraph is one serializer for the API information. - Add documentation comments to ExtractAPI classes and methods to improve readability and clearness of the ExtractAPI work. Differential Revision: https://reviews.llvm.org/D122160
1 parent 57d0290 commit 89f6b26

File tree

18 files changed

+893
-475
lines changed

18 files changed

+893
-475
lines changed

clang/include/clang/SymbolGraph/API.h renamed to clang/include/clang/ExtractAPI/API.h

Lines changed: 89 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- SymbolGraph/API.h ----------------------------------------*- C++ -*-===//
1+
//===- ExtractAPI/API.h -----------------------------------------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -7,18 +7,22 @@
77
//===----------------------------------------------------------------------===//
88
///
99
/// \file
10-
/// \brief Defines SymbolGraph API records.
10+
/// This file defines the APIRecord-based structs and the APISet class.
11+
///
12+
/// Clang ExtractAPI is a tool to collect API information from a given set of
13+
/// header files. The structures in this file describe data representations of
14+
/// the API information collected for various kinds of symbols.
1115
///
1216
//===----------------------------------------------------------------------===//
1317

14-
#ifndef LLVM_CLANG_SYMBOLGRAPH_API_H
15-
#define LLVM_CLANG_SYMBOLGRAPH_API_H
18+
#ifndef LLVM_CLANG_EXTRACTAPI_API_H
19+
#define LLVM_CLANG_EXTRACTAPI_API_H
1620

1721
#include "clang/AST/Decl.h"
1822
#include "clang/AST/RawCommentList.h"
1923
#include "clang/Basic/SourceLocation.h"
20-
#include "clang/SymbolGraph/AvailabilityInfo.h"
21-
#include "clang/SymbolGraph/DeclarationFragments.h"
24+
#include "clang/ExtractAPI/AvailabilityInfo.h"
25+
#include "clang/ExtractAPI/DeclarationFragments.h"
2226
#include "llvm/ADT/MapVector.h"
2327
#include "llvm/ADT/StringRef.h"
2428
#include "llvm/ADT/Triple.h"
@@ -27,18 +31,43 @@
2731
#include <memory>
2832

2933
namespace clang {
30-
namespace symbolgraph {
34+
namespace extractapi {
3135

36+
/// DocComment is a vector of RawComment::CommentLine.
37+
///
38+
/// Each line represents one line of striped documentation comment,
39+
/// with source range information. This simplifies calculating the source
40+
/// location of a character in the doc comment for pointing back to the source
41+
/// file.
42+
/// e.g.
43+
/// \code
44+
/// /// This is a documentation comment
45+
/// ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~' First line.
46+
/// /// with multiple lines.
47+
/// ^~~~~~~~~~~~~~~~~~~~~~~' Second line.
48+
/// \endcode
3249
using DocComment = std::vector<RawComment::CommentLine>;
3350

51+
/// The base representation of an API record. Holds common symbol information.
3452
struct APIRecord {
3553
StringRef Name;
3654
StringRef USR;
3755
PresumedLoc Location;
3856
AvailabilityInfo Availability;
3957
LinkageInfo Linkage;
58+
59+
/// Documentation comment lines attached to this symbol declaration.
4060
DocComment Comment;
61+
62+
/// Declaration fragments of this symbol declaration.
4163
DeclarationFragments Declaration;
64+
65+
/// SubHeading provides a more detailed representation than the plain
66+
/// declaration name.
67+
///
68+
/// SubHeading is an array of declaration fragments of tagged declaration
69+
/// name, with potentially more tokens (for example the \c +/- symbol for
70+
/// Objective-C class/instance methods).
4271
DeclarationFragments SubHeading;
4372

4473
/// Discriminator for LLVM-style RTTI (dyn_cast<> et al.)
@@ -66,14 +95,18 @@ struct APIRecord {
6695
virtual ~APIRecord() = 0;
6796
};
6897

98+
/// The kind of a global record.
6999
enum class GVKind : uint8_t {
70100
Unknown = 0,
71101
Variable = 1,
72102
Function = 2,
73103
};
74104

105+
/// This holds information associated with global variables or functions.
75106
struct GlobalRecord : APIRecord {
76107
GVKind GlobalKind;
108+
109+
/// The function signature of the record if it is a function.
77110
FunctionSignature Signature;
78111

79112
GlobalRecord(GVKind Kind, StringRef Name, StringRef USR, PresumedLoc Loc,
@@ -87,40 +120,52 @@ struct GlobalRecord : APIRecord {
87120
static bool classof(const APIRecord *Record) {
88121
return Record->getKind() == RK_Global;
89122
}
123+
124+
private:
125+
virtual void anchor();
90126
};
91127

128+
/// APISet holds the set of API records collected from given inputs.
92129
class APISet {
93130
public:
94-
APISet(const llvm::Triple &Target, const LangOptions &LangOpts)
95-
: Target(Target), LangOpts(LangOpts) {}
96-
97-
const llvm::Triple &getTarget() const { return Target; }
98-
const LangOptions &getLangOpts() const { return LangOpts; }
99-
131+
/// Create and add a GlobalRecord of kind \p Kind into the API set.
132+
///
133+
/// Note: the caller is responsible for keeping the StringRef \p Name and
134+
/// \p USR alive. APISet::copyString provides a way to copy strings into
135+
/// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
136+
/// to generate the USR for \c D and keep it alive in APISet.
100137
GlobalRecord *addGlobal(GVKind Kind, StringRef Name, StringRef USR,
101138
PresumedLoc Loc, const AvailabilityInfo &Availability,
102139
LinkageInfo Linkage, const DocComment &Comment,
103140
DeclarationFragments Declaration,
104141
DeclarationFragments SubHeading,
105142
FunctionSignature Signature);
106143

144+
/// Create and add a global variable record into the API set.
145+
///
146+
/// Note: the caller is responsible for keeping the StringRef \p Name and
147+
/// \p USR alive. APISet::copyString provides a way to copy strings into
148+
/// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
149+
/// to generate the USR for \c D and keep it alive in APISet.
107150
GlobalRecord *addGlobalVar(StringRef Name, StringRef USR, PresumedLoc Loc,
108151
const AvailabilityInfo &Availability,
109152
LinkageInfo Linkage, const DocComment &Comment,
110153
DeclarationFragments Declaration,
111154
DeclarationFragments SubHeading);
112155

156+
/// Create and add a function record into the API set.
157+
///
158+
/// Note: the caller is responsible for keeping the StringRef \p Name and
159+
/// \p USR alive. APISet::copyString provides a way to copy strings into
160+
/// APISet itself, and APISet::recordUSR(const Decl *D) is a helper method
161+
/// to generate the USR for \c D and keep it alive in APISet.
113162
GlobalRecord *addFunction(StringRef Name, StringRef USR, PresumedLoc Loc,
114163
const AvailabilityInfo &Availability,
115164
LinkageInfo Linkage, const DocComment &Comment,
116165
DeclarationFragments Declaration,
117166
DeclarationFragments SubHeading,
118167
FunctionSignature Signature);
119168

120-
StringRef recordUSR(const Decl *D);
121-
StringRef copyString(StringRef String, llvm::BumpPtrAllocator &Allocator);
122-
StringRef copyString(StringRef String);
123-
124169
private:
125170
/// \brief A custom deleter used for ``std::unique_ptr`` to APIRecords stored
126171
/// in the BumpPtrAllocator.
@@ -138,20 +183,45 @@ class APISet {
138183
using APIRecordUniquePtr =
139184
std::unique_ptr<T, UniquePtrBumpPtrAllocatorDeleter<T>>;
140185

186+
/// A map to store the set of GlobalRecord%s with the declaration name as the
187+
/// key.
141188
using GlobalRecordMap =
142189
llvm::MapVector<StringRef, APIRecordUniquePtr<GlobalRecord>>;
143190

191+
/// Get the target triple for the ExtractAPI invocation.
192+
const llvm::Triple &getTarget() const { return Target; }
193+
194+
/// Get the language options used to parse the APIs.
195+
const LangOptions &getLangOpts() const { return LangOpts; }
196+
144197
const GlobalRecordMap &getGlobals() const { return Globals; }
145198

199+
/// Generate and store the USR of declaration \p D.
200+
///
201+
/// Note: The USR string is stored in and owned by Allocator.
202+
///
203+
/// \returns a StringRef of the generated USR string.
204+
StringRef recordUSR(const Decl *D);
205+
206+
/// Copy \p String into the Allocator in this APISet.
207+
///
208+
/// \returns a StringRef of the copied string in APISet::Allocator.
209+
StringRef copyString(StringRef String);
210+
211+
APISet(const llvm::Triple &Target, const LangOptions &LangOpts)
212+
: Target(Target), LangOpts(LangOpts) {}
213+
146214
private:
215+
/// BumpPtrAllocator to store APIRecord%s and generated/copied strings.
147216
llvm::BumpPtrAllocator Allocator;
217+
148218
const llvm::Triple Target;
149219
const LangOptions LangOpts;
150220

151221
GlobalRecordMap Globals;
152222
};
153223

154-
} // namespace symbolgraph
224+
} // namespace extractapi
155225
} // namespace clang
156226

157-
#endif // LLVM_CLANG_SYMBOLGRAPH_API_H
227+
#endif // LLVM_CLANG_EXTRACTAPI_API_H
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//===- SymbolGraph/AvailabilityInfo.h - Availability Info -------*- C++ -*-===//
1+
//===- ExtractAPI/AvailabilityInfo.h - Availability Info --------*- C++ -*-===//
22
//
33
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44
// See https://llvm.org/LICENSE.txt for license information.
@@ -7,12 +7,13 @@
77
//===----------------------------------------------------------------------===//
88
///
99
/// \file
10-
/// \brief Defines the Availability Info for a declaration.
10+
/// This file defines the AvailabilityInfo struct that collects availability
11+
/// attributes of a symbol.
1112
///
1213
//===----------------------------------------------------------------------===//
1314

14-
#ifndef LLVM_CLANG_SYMBOLGRAPH_AVAILABILITY_INFO_H
15-
#define LLVM_CLANG_SYMBOLGRAPH_AVAILABILITY_INFO_H
15+
#ifndef LLVM_CLANG_EXTRACTAPI_AVAILABILITY_INFO_H
16+
#define LLVM_CLANG_EXTRACTAPI_AVAILABILITY_INFO_H
1617

1718
#include "llvm/Support/Error.h"
1819
#include "llvm/Support/VersionTuple.h"
@@ -21,8 +22,9 @@
2122
using llvm::VersionTuple;
2223

2324
namespace clang {
24-
namespace symbolgraph {
25+
namespace extractapi {
2526

27+
/// Stores availability attributes of a symbol.
2628
struct AvailabilityInfo {
2729
VersionTuple Introduced;
2830
VersionTuple Deprecated;
@@ -31,21 +33,31 @@ struct AvailabilityInfo {
3133
bool UnconditionallyDeprecated{false};
3234
bool UnconditionallyUnavailable{false};
3335

34-
explicit AvailabilityInfo(bool Unavailable = false)
35-
: Unavailable(Unavailable) {}
36-
37-
AvailabilityInfo(VersionTuple I, VersionTuple D, VersionTuple O, bool U,
38-
bool UD, bool UU)
39-
: Introduced(I), Deprecated(D), Obsoleted(O), Unavailable(U),
40-
UnconditionallyDeprecated(UD), UnconditionallyUnavailable(UU) {}
41-
36+
/// Determine if this AvailabilityInfo represents the default availability.
4237
bool isDefault() const { return *this == AvailabilityInfo(); }
38+
39+
/// Check if the symbol is unavailable.
4340
bool isUnavailable() const { return Unavailable; }
41+
42+
/// Check if the symbol is unconditionally deprecated.
43+
///
44+
/// i.e. \code __attribute__((deprecated)) \endcode
4445
bool isUnconditionallyDeprecated() const { return UnconditionallyDeprecated; }
46+
47+
/// Check if the symbol is unconditionally unavailable.
48+
///
49+
/// i.e. \code __attribute__((unavailable)) \endcode
4550
bool isUnconditionallyUnavailable() const {
4651
return UnconditionallyUnavailable;
4752
}
4853

54+
AvailabilityInfo() = default;
55+
56+
AvailabilityInfo(VersionTuple I, VersionTuple D, VersionTuple O, bool U,
57+
bool UD, bool UU)
58+
: Introduced(I), Deprecated(D), Obsoleted(O), Unavailable(U),
59+
UnconditionallyDeprecated(UD), UnconditionallyUnavailable(UU) {}
60+
4961
friend bool operator==(const AvailabilityInfo &Lhs,
5062
const AvailabilityInfo &Rhs);
5163
};
@@ -60,7 +72,7 @@ inline bool operator==(const AvailabilityInfo &Lhs,
6072
Rhs.UnconditionallyUnavailable);
6173
}
6274

63-
} // namespace symbolgraph
75+
} // namespace extractapi
6476
} // namespace clang
6577

66-
#endif // LLVM_CLANG_SYMBOLGRAPH_AVAILABILITY_INFO_H
78+
#endif // LLVM_CLANG_EXTRACTAPI_AVAILABILITY_INFO_H

0 commit comments

Comments
 (0)