Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 4672477

Browse files
authored
Merge pull request #29 from DougGregor/versioned-apinotes-3_1
[API Notes] Add basic support for versioned API notes
2 parents d8f1983 + 15be175 commit 4672477

File tree

21 files changed

+1307
-812
lines changed

21 files changed

+1307
-812
lines changed

include/clang/APINotes/APINotesManager.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "clang/Basic/SourceLocation.h"
1818
#include "clang/Basic/Module.h"
19+
#include "clang/Basic/VersionTuple.h"
1920
#include "llvm/ADT/ArrayRef.h"
2021
#include "llvm/ADT/DenseMap.h"
2122
#include "llvm/ADT/PointerUnion.h"
@@ -55,6 +56,9 @@ class APINotesManager {
5556
/// source file from which an entity was declared.
5657
bool ImplicitAPINotes;
5758

59+
/// The Swift version to use when interpreting versioned API notes.
60+
VersionTuple SwiftVersion;
61+
5862
/// API notes readers for the current module.
5963
///
6064
/// There can be up to two of these, one for public headers and one
@@ -109,6 +113,11 @@ class APINotesManager {
109113
APINotesManager(SourceManager &sourceMgr, const LangOptions &langOpts);
110114
~APINotesManager();
111115

116+
/// Set the Swift version to use when filtering API notes.
117+
void setSwiftVersion(VersionTuple swiftVersion) {
118+
SwiftVersion = swiftVersion;
119+
}
120+
112121
/// Load the API notes for the current module.
113122
///
114123
/// \param module The current module.

include/clang/APINotes/APINotesOptions.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_CLANG_APINOTES_APINOTESOPTIONS_H
1515
#define LLVM_CLANG_APINOTES_APINOTESOPTIONS_H
1616

17+
#include "clang/Basic/VersionTuple.h"
1718
#include <string>
1819
#include <vector>
1920

@@ -23,6 +24,9 @@ namespace clang {
2324
/// notes are found and handled.
2425
class APINotesOptions {
2526
public:
27+
/// The Swift version which should be used for API notes.
28+
VersionTuple SwiftVersion;
29+
2630
/// The set of search paths where we API notes can be found for
2731
/// particular modules.
2832
///

include/clang/APINotes/APINotesReader.h

Lines changed: 104 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#define LLVM_CLANG_API_NOTES_READER_H
1818

1919
#include "clang/APINotes/Types.h"
20+
#include "clang/Basic/VersionTuple.h"
2021
#include "llvm/ADT/Optional.h"
2122
#include "llvm/Support/MemoryBuffer.h"
2223
#include <memory>
@@ -32,22 +33,24 @@ class APINotesReader {
3233
Implementation &Impl;
3334

3435
APINotesReader(llvm::MemoryBuffer *inputBuffer, bool ownsInputBuffer,
35-
bool &failed);
36+
VersionTuple swiftVersion, bool &failed);
3637

3738
public:
3839
/// Create a new API notes reader from the given member buffer, which
3940
/// contains the contents of a binary API notes file.
4041
///
4142
/// \returns the new API notes reader, or null if an error occurred.
4243
static std::unique_ptr<APINotesReader>
43-
get(std::unique_ptr<llvm::MemoryBuffer> inputBuffer);
44+
get(std::unique_ptr<llvm::MemoryBuffer> inputBuffer,
45+
VersionTuple swiftVersion);
4446

4547
/// Create a new API notes reader from the given member buffer, which
4648
/// contains the contents of a binary API notes file.
4749
///
4850
/// \returns the new API notes reader, or null if an error occurred.
4951
static std::unique_ptr<APINotesReader>
50-
getUnmanaged(llvm::MemoryBuffer *inputBuffer);
52+
getUnmanaged(llvm::MemoryBuffer *inputBuffer,
53+
VersionTuple swiftVersion);
5154

5255
~APINotesReader();
5356

@@ -65,21 +68,83 @@ class APINotesReader {
6568
/// Retrieve the module options
6669
ModuleOptions getModuleOptions() const;
6770

71+
/// Captures the completed versioned information for a particular part of
72+
/// API notes, including both unversioned API notes and each versioned API
73+
/// note for that particular entity.
74+
template<typename T>
75+
class VersionedInfo {
76+
/// The complete set of results.
77+
SmallVector<std::pair<VersionTuple, T>, 1> Results;
78+
79+
/// The index of the result that is the "selected" set based on the desired
80+
/// Swift version, or \c Results.size() if nothing matched.
81+
unsigned Selected;
82+
83+
public:
84+
/// Form an empty set of versioned information.
85+
VersionedInfo(llvm::NoneType) : Selected(0) { }
86+
87+
/// Form a versioned info set given the desired version and a set of
88+
/// results.
89+
VersionedInfo(VersionTuple version,
90+
SmallVector<std::pair<VersionTuple, T>, 1> results);
91+
92+
/// Determine whether there is a result that should be applied directly
93+
/// to the AST.
94+
explicit operator bool() const { return Selected != size(); }
95+
96+
/// Retrieve the information to apply directly to the AST.
97+
const T& operator*() const {
98+
assert(*this && "No result to apply directly");
99+
return (*this)[Selected].second;
100+
}
101+
102+
/// Retrieve the selected index in the result set.
103+
Optional<unsigned> getSelected() const {
104+
if (Selected == Results.size()) return None;
105+
return Selected;
106+
}
107+
108+
/// Return the number of versioned results we know about.
109+
unsigned size() const { return Results.size(); }
110+
111+
/// Access all versioned results.
112+
const std::pair<VersionTuple, T> *begin() const { return Results.begin(); }
113+
const std::pair<VersionTuple, T> *end() const { return Results.end(); }
114+
115+
/// Access a specific versioned result.
116+
const std::pair<VersionTuple, T> &operator[](unsigned index) const {
117+
return Results[index];
118+
}
119+
};
120+
121+
/// Look for the context ID of the given Objective-C class.
122+
///
123+
/// \param name The name of the class we're looking for.
124+
///
125+
/// \returns The ID, if known.
126+
Optional<ContextID> lookupObjCClassID(StringRef name);
127+
68128
/// Look for information regarding the given Objective-C class.
69129
///
70130
/// \param name The name of the class we're looking for.
71131
///
72-
/// \returns The ID and information about the class, if known.
73-
Optional<std::pair<ContextID, ObjCContextInfo>>
74-
lookupObjCClass(StringRef name);
132+
/// \returns The information about the class, if known.
133+
VersionedInfo<ObjCContextInfo> lookupObjCClassInfo(StringRef name);
134+
135+
/// Look for the context ID of the given Objective-C protocol.
136+
///
137+
/// \param name The name of the protocol we're looking for.
138+
///
139+
/// \returns The ID of the protocol, if known.
140+
Optional<ContextID> lookupObjCProtocolID(StringRef name);
75141

76142
/// Look for information regarding the given Objective-C protocol.
77143
///
78144
/// \param name The name of the protocol we're looking for.
79145
///
80-
/// \returns The ID and information about the protocol, if known.
81-
Optional<std::pair<ContextID, ObjCContextInfo>>
82-
lookupObjCProtocol(StringRef name);
146+
/// \returns The information about the protocol, if known.
147+
VersionedInfo<ObjCContextInfo> lookupObjCProtocolInfo(StringRef name);
83148

84149
/// Look for information regarding the given Objective-C property in
85150
/// the given context.
@@ -88,11 +153,12 @@ class APINotesReader {
88153
/// \param name The name of the property we're looking for.
89154
/// \param isInstance Whether we are looking for an instance property (vs.
90155
/// a class property).
156+
/// \param swiftVersion The Swift version to filter for, if any.
91157
///
92158
/// \returns Information about the property, if known.
93-
Optional<ObjCPropertyInfo> lookupObjCProperty(ContextID contextID,
94-
StringRef name,
95-
bool isInstance);
159+
VersionedInfo<ObjCPropertyInfo> lookupObjCProperty(ContextID contextID,
160+
StringRef name,
161+
bool isInstance);
96162

97163
/// Look for information regarding the given Objective-C method in
98164
/// the given context.
@@ -102,45 +168,45 @@ class APINotesReader {
102168
/// \param isInstanceMethod Whether we are looking for an instance method.
103169
///
104170
/// \returns Information about the method, if known.
105-
Optional<ObjCMethodInfo> lookupObjCMethod(ContextID contextID,
106-
ObjCSelectorRef selector,
107-
bool isInstanceMethod);
171+
VersionedInfo<ObjCMethodInfo> lookupObjCMethod(ContextID contextID,
172+
ObjCSelectorRef selector,
173+
bool isInstanceMethod);
108174

109175
/// Look for information regarding the given global variable.
110176
///
111177
/// \param name The name of the global variable.
112178
///
113179
/// \returns information about the global variable, if known.
114-
Optional<GlobalVariableInfo> lookupGlobalVariable(StringRef name);
180+
VersionedInfo<GlobalVariableInfo> lookupGlobalVariable(StringRef name);
115181

116182
/// Look for information regarding the given global function.
117183
///
118184
/// \param name The name of the global function.
119185
///
120186
/// \returns information about the global function, if known.
121-
Optional<GlobalFunctionInfo> lookupGlobalFunction(StringRef name);
187+
VersionedInfo<GlobalFunctionInfo> lookupGlobalFunction(StringRef name);
122188

123189
/// Look for information regarding the given enumerator.
124190
///
125191
/// \param name The name of the enumerator.
126192
///
127193
/// \returns information about the enumerator, if known.
128-
Optional<EnumConstantInfo> lookupEnumConstant(StringRef name);
194+
VersionedInfo<EnumConstantInfo> lookupEnumConstant(StringRef name);
129195

130196
/// Look for information regarding the given tag
131197
/// (struct/union/enum/C++ class).
132198
///
133199
/// \param name The name of the tag.
134200
///
135201
/// \returns information about the tag, if known.
136-
Optional<TagInfo> lookupTag(StringRef name);
202+
VersionedInfo<TagInfo> lookupTag(StringRef name);
137203

138204
/// Look for information regarding the given typedef.
139205
///
140206
/// \param name The name of the typedef.
141207
///
142208
/// \returns information about the typedef, if known.
143-
Optional<TypedefInfo> lookupTypedef(StringRef name);
209+
VersionedInfo<TypedefInfo> lookupTypedef(StringRef name);
144210

145211
/// Visitor used when walking the contents of the API notes file.
146212
class Visitor {
@@ -149,39 +215,48 @@ class APINotesReader {
149215

150216
/// Visit an Objective-C class.
151217
virtual void visitObjCClass(ContextID contextID, StringRef name,
152-
const ObjCContextInfo &info);
218+
const ObjCContextInfo &info,
219+
VersionTuple swiftVersion);
153220

154221
/// Visit an Objective-C protocol.
155222
virtual void visitObjCProtocol(ContextID contextID, StringRef name,
156-
const ObjCContextInfo &info);
223+
const ObjCContextInfo &info,
224+
VersionTuple swiftVersion);
157225

158226
/// Visit an Objective-C method.
159227
virtual void visitObjCMethod(ContextID contextID, StringRef selector,
160228
bool isInstanceMethod,
161-
const ObjCMethodInfo &info);
229+
const ObjCMethodInfo &info,
230+
VersionTuple swiftVersion);
162231

163232
/// Visit an Objective-C property.
164233
virtual void visitObjCProperty(ContextID contextID, StringRef name,
165234
bool isInstance,
166-
const ObjCPropertyInfo &info);
235+
const ObjCPropertyInfo &info,
236+
VersionTuple swiftVersion);
167237

168238
/// Visit a global variable.
169239
virtual void visitGlobalVariable(StringRef name,
170-
const GlobalVariableInfo &info);
240+
const GlobalVariableInfo &info,
241+
VersionTuple swiftVersion);
171242

172243
/// Visit a global function.
173244
virtual void visitGlobalFunction(StringRef name,
174-
const GlobalFunctionInfo &info);
245+
const GlobalFunctionInfo &info,
246+
VersionTuple swiftVersion);
175247

176248
/// Visit an enumerator.
177249
virtual void visitEnumConstant(StringRef name,
178-
const EnumConstantInfo &info);
250+
const EnumConstantInfo &info,
251+
VersionTuple swiftVersion);
179252

180253
/// Visit a tag.
181-
virtual void visitTag(StringRef name, const TagInfo &info);
254+
virtual void visitTag(StringRef name, const TagInfo &info,
255+
VersionTuple swiftVersion);
182256

183257
/// Visit a typedef.
184-
virtual void visitTypedef(StringRef name, const TypedefInfo &info);
258+
virtual void visitTypedef(StringRef name, const TypedefInfo &info,
259+
VersionTuple swiftVersion);
185260
};
186261

187262
/// Visit the contents of the API notes file, passing each entity to the

0 commit comments

Comments
 (0)