Skip to content

Commit 9555a11

Browse files
committed
[APINotes] NFC: refactor, reduce code duplication
rdar://113403829
1 parent 24f78c4 commit 9555a11

File tree

3 files changed

+20
-40
lines changed

3 files changed

+20
-40
lines changed

clang/lib/APINotes/APINotesFormat.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,17 @@ struct StoredObjCSelector {
249249
unsigned NumPieces;
250250
llvm::SmallVector<IdentifierID, 2> Identifiers;
251251
};
252+
253+
inline std::tuple<uint32_t, uint8_t, uint32_t>
254+
getTableKey(std::optional<Context> context, IdentifierID nameID) {
255+
std::tuple<uint32_t, uint8_t, uint32_t> key;
256+
if (context)
257+
key = {context->id.Value, (uint8_t)context->kind, nameID};
258+
else
259+
key = {(uint32_t)-1, (uint8_t)-1, nameID};
260+
return key;
261+
}
262+
252263
} // namespace api_notes
253264
} // namespace clang
254265

clang/lib/APINotes/APINotesReader.cpp

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,11 +1949,7 @@ auto APINotesReader::lookupGlobalVariable(std::optional<Context> context,
19491949
if (!nameID)
19501950
return None;
19511951

1952-
std::tuple<uint32_t, uint8_t, uint32_t> key;
1953-
if (context)
1954-
key = {context->id.Value, (uint8_t)context->kind, *nameID};
1955-
else
1956-
key = {-1, (uint8_t)-1, *nameID};
1952+
std::tuple<uint32_t, uint8_t, uint32_t> key = getTableKey(context, *nameID);
19571953

19581954
auto known = Impl.GlobalVariableTable->find(key);
19591955
if (known == Impl.GlobalVariableTable->end())
@@ -1972,11 +1968,7 @@ auto APINotesReader::lookupGlobalFunction(std::optional<Context> context,
19721968
if (!nameID)
19731969
return None;
19741970

1975-
std::tuple<uint32_t, uint8_t, uint32_t> key;
1976-
if (context)
1977-
key = {context->id.Value, (uint8_t)context->kind, *nameID};
1978-
else
1979-
key = {-1, (uint8_t)-1, *nameID};
1971+
std::tuple<uint32_t, uint8_t, uint32_t> key = getTableKey(context, *nameID);
19801972

19811973
auto known = Impl.GlobalFunctionTable->find(key);
19821974
if (known == Impl.GlobalFunctionTable->end())
@@ -2010,11 +2002,7 @@ auto APINotesReader::lookupTag(std::optional<Context> context, StringRef name)
20102002
if (!nameID)
20112003
return None;
20122004

2013-
std::tuple<uint32_t, uint8_t, uint32_t> key;
2014-
if (context)
2015-
key = {context->id.Value, (uint8_t)context->kind, *nameID};
2016-
else
2017-
key = {-1, (uint8_t)-1, *nameID};
2005+
std::tuple<uint32_t, uint8_t, uint32_t> key = getTableKey(context, *nameID);
20182006

20192007
auto known = Impl.TagTable->find(key);
20202008
if (known == Impl.TagTable->end())
@@ -2033,11 +2021,7 @@ auto APINotesReader::lookupTypedef(std::optional<Context> context,
20332021
if (!nameID)
20342022
return None;
20352023

2036-
std::tuple<uint32_t, uint8_t, uint32_t> key;
2037-
if (context)
2038-
key = {context->id.Value, (uint8_t)context->kind, *nameID};
2039-
else
2040-
key = {-1, (uint8_t)-1, *nameID};
2024+
std::tuple<uint32_t, uint8_t, uint32_t> key = getTableKey(context, *nameID);
20412025

20422026
auto known = Impl.TypedefTable->find(key);
20432027
if (known == Impl.TypedefTable->end())

clang/lib/APINotes/APINotesWriter.cpp

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1328,11 +1328,8 @@ void APINotesWriter::addGlobalVariable(std::optional<Context> context,
13281328
const GlobalVariableInfo &info,
13291329
VersionTuple swiftVersion) {
13301330
IdentifierID variableID = Impl.getIdentifier(name);
1331-
std::tuple<uint32_t, uint8_t, uint32_t> key;
1332-
if (context)
1333-
key = {context->id.Value, (uint8_t)context->kind, variableID};
1334-
else
1335-
key = {-1, (uint8_t)-1, variableID};
1331+
std::tuple<uint32_t, uint8_t, uint32_t> key =
1332+
getTableKey(context, variableID);
13361333
Impl.GlobalVariables[key].push_back({swiftVersion, info});
13371334
}
13381335

@@ -1341,11 +1338,7 @@ void APINotesWriter::addGlobalFunction(std::optional<Context> context,
13411338
const GlobalFunctionInfo &info,
13421339
VersionTuple swiftVersion) {
13431340
IdentifierID nameID = Impl.getIdentifier(name);
1344-
std::tuple<uint32_t, uint8_t, uint32_t> key;
1345-
if (context)
1346-
key = {context->id.Value, (uint8_t)context->kind, nameID};
1347-
else
1348-
key = {-1, (uint8_t)-1, nameID};
1341+
std::tuple<uint32_t, uint8_t, uint32_t> key = getTableKey(context, nameID);
13491342
Impl.GlobalFunctions[key].push_back({swiftVersion, info});
13501343
}
13511344

@@ -1360,23 +1353,15 @@ void APINotesWriter::addTag(std::optional<Context> context,
13601353
llvm::StringRef name, const TagInfo &info,
13611354
VersionTuple swiftVersion) {
13621355
IdentifierID tagID = Impl.getIdentifier(name);
1363-
std::tuple<uint32_t, uint8_t, uint32_t> key;
1364-
if (context)
1365-
key = {context->id.Value, (uint8_t)context->kind, tagID};
1366-
else
1367-
key = {-1, (uint8_t)-1, tagID};
1356+
std::tuple<uint32_t, uint8_t, uint32_t> key = getTableKey(context, tagID);
13681357
Impl.Tags[key].push_back({swiftVersion, info});
13691358
}
13701359

13711360
void APINotesWriter::addTypedef(std::optional<Context> context,
13721361
llvm::StringRef name, const TypedefInfo &info,
13731362
VersionTuple swiftVersion) {
13741363
IdentifierID typedefID = Impl.getIdentifier(name);
1375-
std::tuple<uint32_t, uint8_t, uint32_t> key;
1376-
if (context)
1377-
key = {context->id.Value, (uint8_t)context->kind, typedefID};
1378-
else
1379-
key = {-1, (uint8_t)-1, typedefID};
1364+
std::tuple<uint32_t, uint8_t, uint32_t> key = getTableKey(context, typedefID);
13801365
Impl.Typedefs[key].push_back({swiftVersion, info});
13811366
}
13821367

0 commit comments

Comments
 (0)