Skip to content

Commit adaf790

Browse files
committed
SerializeLoc: asserts USR id and basic decl location are always in sync
1 parent 5c092e7 commit adaf790

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

lib/Serialization/SerializeDoc.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "DocFormat.h"
1414
#include "Serialization.h"
1515
#include "SourceInfoFormat.h"
16+
#include "swift/Basic/Defer.h"
1617
#include "swift/AST/ASTContext.h"
1718
#include "swift/AST/ASTWalker.h"
1819
#include "swift/AST/DiagnosticsCommon.h"
@@ -556,6 +557,7 @@ class DeclUSRsTableWriter {
556557
llvm::OnDiskChainedHashTableGenerator<USRTableInfo> generator;
557558
uint32_t CurId = 0;
558559
public:
560+
uint32_t peekNextId() const { return CurId; }
559561
Optional<uint32_t> getNewUSRID(StringRef USR) {
560562
if (USRMap.find(USR) == USRMap.end()) {
561563
generator.insert(USRMap.insert(std::make_pair(USR, CurId)).first->getKey(), CurId);
@@ -690,6 +692,10 @@ Result.X.Column = Locs->X.Column;
690692
}
691693

692694
bool walkToDeclPre(Decl *D) override {
695+
SWIFT_DEFER {
696+
assert(USRWriter.peekNextId() * sizeof(DeclLocationsTableData) == Buffer.size() &&
697+
"USR Id has a one-to-one mapping with DeclLocationsTableData");
698+
};
693699
// We shouldn't expose any Decls that .swiftdoc file isn't willing to expose.
694700
// .swiftdoc doesn't include comments for double underscored symbols, but for .swiftsourceinfo,
695701
// having the source location for these symbols isn't a concern becuase these
@@ -706,9 +712,6 @@ Result.X.Column = Locs->X.Column;
706712
auto USR = calculateNewUSRId(D);
707713
if (!USR.hasValue())
708714
return true;
709-
// OK, we get a new USR, now append the associated source location data.
710-
assert(*USR * sizeof(DeclLocationsTableData) == Buffer.size() &&
711-
"USR id is used as an index to access basic location array");
712715
appendToBuffer(*LocData);
713716
return true;
714717
}

0 commit comments

Comments
 (0)