Skip to content

Commit 2e7a890

Browse files
committed
[APINotes] Handle global functions in namespaces
rdar://113403829
1 parent e7dc05e commit 2e7a890

File tree

8 files changed

+48
-16
lines changed

8 files changed

+48
-16
lines changed

clang/include/clang/APINotes/APINotesReader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ class APINotesReader {
182182
/// \param name The name of the global function.
183183
///
184184
/// \returns information about the global function, if known.
185-
VersionedInfo<GlobalFunctionInfo> lookupGlobalFunction(llvm::StringRef name);
185+
VersionedInfo<GlobalFunctionInfo>
186+
lookupGlobalFunction(std::optional<Context> context, llvm::StringRef name);
186187

187188
/// Look for information regarding the given enumerator.
188189
///

clang/include/clang/APINotes/APINotesWriter.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ class APINotesWriter {
9292
///
9393
/// \param name The name of this global function.
9494
/// \param info Information about this global function.
95-
void addGlobalFunction(llvm::StringRef name, const GlobalFunctionInfo &info,
95+
void addGlobalFunction(std::optional<Context> context, llvm::StringRef name,
96+
const GlobalFunctionInfo &info,
9697
llvm::VersionTuple swiftVersion);
9798

9899
/// Add information about an enumerator.

clang/lib/APINotes/APINotesReader.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -450,12 +450,15 @@ namespace {
450450

451451
/// Used to deserialize the on-disk global function table.
452452
class GlobalFunctionTableInfo
453-
: public VersionedTableInfo<GlobalFunctionTableInfo, unsigned,
454-
GlobalFunctionInfo> {
453+
: public VersionedTableInfo<GlobalFunctionTableInfo,
454+
std::tuple<uint32_t, uint8_t, uint32_t>,
455+
GlobalFunctionInfo> {
455456
public:
456457
static internal_key_type ReadKey(const uint8_t *data, unsigned length) {
458+
auto contextID = endian::readNext<uint32_t, little, unaligned>(data);
459+
auto contextKind = endian::readNext<uint8_t, little, unaligned>(data);
457460
auto nameID = endian::readNext<uint32_t, little, unaligned>(data);
458-
return nameID;
461+
return {contextID, contextKind, nameID};
459462
}
460463

461464
static GlobalFunctionInfo readUnversioned(internal_key_type key,
@@ -1950,7 +1953,8 @@ auto APINotesReader::lookupGlobalVariable(
19501953
return { Impl.SwiftVersion, *known };
19511954
}
19521955

1953-
auto APINotesReader::lookupGlobalFunction(StringRef name)
1956+
auto APINotesReader::lookupGlobalFunction(std::optional<Context> context,
1957+
StringRef name)
19541958
-> VersionedInfo<GlobalFunctionInfo> {
19551959
if (!Impl.GlobalFunctionTable)
19561960
return None;
@@ -1959,7 +1963,13 @@ auto APINotesReader::lookupGlobalFunction(StringRef name)
19591963
if (!nameID)
19601964
return None;
19611965

1962-
auto known = Impl.GlobalFunctionTable->find(*nameID);
1966+
std::tuple<uint32_t, uint8_t, uint32_t> key;
1967+
if (context)
1968+
key = {context->id.Value, (uint8_t)context->kind, *nameID};
1969+
else
1970+
key = {-1, (uint8_t)-1, *nameID};
1971+
1972+
auto known = Impl.GlobalFunctionTable->find(key);
19631973
if (known == Impl.GlobalFunctionTable->end())
19641974
return None;
19651975

clang/lib/APINotes/APINotesWriter.cpp

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,8 @@ class APINotesWriter::Implementation {
101101

102102
/// Information about global functions.
103103
///
104-
/// Indexed by the identifier ID.
105-
llvm::DenseMap<unsigned,
104+
/// Indexed by the context ID, contextKind, identifier ID.
105+
llvm::DenseMap<std::tuple<uint32_t, uint8_t, uint32_t>,
106106
llvm::SmallVector<std::pair<VersionTuple, GlobalFunctionInfo>,
107107
1>>
108108
GlobalFunctions;
@@ -957,16 +957,18 @@ namespace {
957957
/// Used to serialize the on-disk global function table.
958958
class GlobalFunctionTableInfo
959959
: public VersionedTableInfo<GlobalFunctionTableInfo,
960-
unsigned,
960+
std::tuple<uint32_t, uint8_t, uint32_t>,
961961
GlobalFunctionInfo> {
962962
public:
963963
unsigned getKeyLength(key_type_ref) {
964-
return sizeof(uint32_t);
964+
return sizeof(uint32_t) + sizeof(uint8_t) + sizeof(uint32_t);
965965
}
966966

967967
void EmitKey(raw_ostream &out, key_type_ref key, unsigned len) {
968968
endian::Writer writer(out, little);
969-
writer.write<uint32_t>(key);
969+
writer.write<uint32_t>(std::get<0>(key));
970+
writer.write<uint8_t>(std::get<1>(key));
971+
writer.write<uint32_t>(std::get<2>(key));
970972
}
971973

972974
unsigned getUnversionedInfoSize(const GlobalFunctionInfo &info) {
@@ -1326,11 +1328,17 @@ void APINotesWriter::addGlobalVariable(llvm::StringRef name,
13261328
Impl.GlobalVariables[variableID].push_back({swiftVersion, info});
13271329
}
13281330

1329-
void APINotesWriter::addGlobalFunction(llvm::StringRef name,
1331+
void APINotesWriter::addGlobalFunction(std::optional<Context> context,
1332+
llvm::StringRef name,
13301333
const GlobalFunctionInfo &info,
13311334
VersionTuple swiftVersion) {
13321335
IdentifierID nameID = Impl.getIdentifier(name);
1333-
Impl.GlobalFunctions[nameID].push_back({swiftVersion, info});
1336+
std::tuple<uint32_t, uint8_t, uint32_t> key;
1337+
if (context)
1338+
key = {context->id.Value, (uint8_t)context->kind, nameID};
1339+
else
1340+
key = {-1, (uint8_t)-1, nameID};
1341+
Impl.GlobalFunctions[key].push_back({swiftVersion, info});
13341342
}
13351343

13361344
void APINotesWriter::addEnumConstant(llvm::StringRef name,

clang/lib/APINotes/APINotesYAMLCompiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1097,7 +1097,7 @@ namespace {
10971097
info, function.Name);
10981098
info.ResultType = std::string(function.ResultType);
10991099
info.setRetainCountConvention(function.RetainCountConvention);
1100-
Writer->addGlobalFunction(function.Name, info, swiftVersion);
1100+
Writer->addGlobalFunction(context, function.Name, info, swiftVersion);
11011101
}
11021102

11031103
// Write all enumerators.

clang/lib/Sema/SemaAPINotes.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,8 @@ void Sema::ProcessAPINotes(Decl *D) {
858858
if (auto FD = dyn_cast<FunctionDecl>(D)) {
859859
if (FD->getDeclName().isIdentifier()) {
860860
for (auto Reader : APINotes.findAPINotes(D->getLocation())) {
861-
auto Info = Reader->lookupGlobalFunction(FD->getName());
861+
auto Info =
862+
Reader->lookupGlobalFunction(APINotesContext, FD->getName());
862863
ProcessVersionedAPINotes(*this, FD, Info);
863864
}
864865
}

clang/test/APINotes/Inputs/Frameworks/CXXInteropKit.framework/Headers/CXXInteropKit.apinotes

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,27 @@ Classes:
1010
Enumerators:
1111
- Name: SomeClassRed
1212
SwiftName: red
13+
Functions:
14+
- Name: funcInNamespace
15+
SwiftName: inWrongContext()
1316
Tags:
1417
- Name: NSSomeEnumOptions
1518
SwiftName: SomeEnum.Options
1619
- Name: char_box
1720
SwiftName: InWrongContext
1821
Namespaces:
1922
- Name: Namespace1
23+
Functions:
24+
- Name: funcInNamespace
25+
SwiftName: swiftFuncInNamespace()
2026
Tags:
2127
- Name: char_box
2228
SwiftName: CharBox
2329
Namespaces:
2430
- Name: Nested1
31+
Functions:
32+
- Name: funcInNestedNamespace
33+
SwiftName: swiftFuncInNestedNamespace(_:)
2534
Tags:
2635
- Name: char_box
2736
SwiftName: NestedCharBox

clang/test/APINotes/Inputs/Frameworks/CXXInteropKit.framework/Headers/CXXInteropKit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ typedef NS_OPTIONS(NSUInteger, NSSomeEnumOptions) {
2424

2525
namespace Namespace1 {
2626
struct char_box { char c; };
27+
void funcInNamespace();
2728

2829
namespace Nested1 {
2930
struct char_box { char c; };
31+
void funcInNestedNamespace(int i);
3032

3133
namespace Namespace1 {
3234
struct char_box { char c; };

0 commit comments

Comments
 (0)