@@ -1120,97 +1120,50 @@ adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
1120
1120
}
1121
1121
1122
1122
std::pair<ASTFileSignature, ASTFileSignature>
1123
- ASTWriter::createSignature () const {
1124
- StringRef AllBytes (Buffer.data (), Buffer.size ());
1125
-
1123
+ ASTWriter::createSignature (StringRef AllBytes, StringRef ASTBlockBytes) {
1126
1124
llvm::SHA1 Hasher;
1127
- Hasher.update (AllBytes. slice (ASTBlockRange. first , ASTBlockRange. second ) );
1125
+ Hasher.update (ASTBlockBytes );
1128
1126
ASTFileSignature ASTBlockHash = ASTFileSignature::create (Hasher.result ());
1129
1127
1130
- // Add the remaining bytes:
1131
- // 1. Before the unhashed control block.
1132
- Hasher.update (AllBytes. slice ( 0 , UnhashedControlBlockRange. first ));
1133
- // 2. Between the unhashed control block and the AST block.
1128
+ // Add the remaining bytes (i.e. bytes before the unhashed control block that
1129
+ // are not part of the AST block) .
1130
+ Hasher.update (
1131
+ AllBytes. take_front (ASTBlockBytes. bytes_end () - AllBytes. bytes_begin ()));
1134
1132
Hasher.update (
1135
- AllBytes.slice (UnhashedControlBlockRange.second , ASTBlockRange.first ));
1136
- // 3. After the AST block.
1137
- Hasher.update (AllBytes.slice (ASTBlockRange.second , StringRef::npos));
1133
+ AllBytes.take_back (AllBytes.bytes_end () - ASTBlockBytes.bytes_end ()));
1138
1134
ASTFileSignature Signature = ASTFileSignature::create (Hasher.result ());
1139
1135
1140
1136
return std::make_pair (ASTBlockHash, Signature);
1141
1137
}
1142
1138
1143
- ASTFileSignature ASTWriter::backpatchSignature () {
1144
- if (!WritingModule ||
1145
- !PP->getHeaderSearchInfo ().getHeaderSearchOpts ().ModulesHashContent )
1146
- return {};
1147
-
1148
- // For implicit modules, write the hash of the PCM as its signature.
1149
-
1150
- auto BackpatchSignatureAt = [&](const ASTFileSignature &S, uint64_t BitNo) {
1151
- using WordT = unsigned ;
1152
- std::array<WordT, sizeof (ASTFileSignature) / sizeof (WordT)> Words;
1153
- static_assert (sizeof (Words) == sizeof (S));
1154
- std::memcpy (Words.data (), S.data (), sizeof (ASTFileSignature));
1155
- for (WordT Word : Words) {
1156
- Stream.BackpatchWord (BitNo, Word);
1157
- BitNo += sizeof (WordT) * 8 ;
1158
- }
1159
- };
1160
-
1161
- ASTFileSignature ASTBlockHash;
1162
- ASTFileSignature Signature;
1163
- std::tie (ASTBlockHash, Signature) = createSignature ();
1164
-
1165
- BackpatchSignatureAt (ASTBlockHash, ASTBlockHashOffset);
1166
- BackpatchSignatureAt (Signature, SignatureOffset);
1167
-
1168
- return Signature;
1169
- }
1170
-
1171
- void ASTWriter::writeUnhashedControlBlock (Preprocessor &PP,
1172
- ASTContext &Context) {
1139
+ ASTFileSignature ASTWriter::writeUnhashedControlBlock (Preprocessor &PP,
1140
+ ASTContext &Context) {
1173
1141
using namespace llvm ;
1174
1142
1175
1143
// Flush first to prepare the PCM hash (signature).
1176
1144
Stream.FlushToWord ();
1177
- UnhashedControlBlockRange. first = Stream.GetCurrentBitNo () >> 3 ;
1145
+ auto StartOfUnhashedControl = Stream.GetCurrentBitNo () >> 3 ;
1178
1146
1179
1147
// Enter the block and prepare to write records.
1180
1148
RecordData Record;
1181
1149
Stream.EnterSubblock (UNHASHED_CONTROL_BLOCK_ID, 5 );
1182
1150
1183
1151
// For implicit modules, write the hash of the PCM as its signature.
1152
+ ASTFileSignature Signature;
1184
1153
if (WritingModule &&
1185
1154
PP.getHeaderSearchInfo ().getHeaderSearchOpts ().ModulesHashContent ) {
1186
- // At this point, we don't know the actual signature of the file or the AST
1187
- // block - we're only able to compute those at the end of the serialization
1188
- // process. Let's store dummy signatures for now, and replace them with the
1189
- // real ones later on.
1190
- // The bitstream VBR-encodes record elements, which makes backpatching them
1191
- // really difficult. Let's store the signatures as blobs instead - they are
1192
- // guaranteed to be word-aligned, and we control their format/encoding.
1193
- auto Dummy = ASTFileSignature::createDummy ();
1194
- SmallString<128 > Blob{Dummy.begin (), Dummy.end ()};
1195
-
1196
- auto Abbrev = std::make_shared<BitCodeAbbrev>();
1197
- Abbrev->Add (BitCodeAbbrevOp (AST_BLOCK_HASH));
1198
- Abbrev->Add (BitCodeAbbrevOp (BitCodeAbbrevOp::Blob));
1199
- unsigned ASTBlockHashAbbrev = Stream.EmitAbbrev (std::move (Abbrev));
1200
-
1201
- Abbrev = std::make_shared<BitCodeAbbrev>();
1202
- Abbrev->Add (BitCodeAbbrevOp (SIGNATURE));
1203
- Abbrev->Add (BitCodeAbbrevOp (BitCodeAbbrevOp::Blob));
1204
- unsigned SignatureAbbrev = Stream.EmitAbbrev (std::move (Abbrev));
1205
-
1206
- Record.push_back (AST_BLOCK_HASH);
1207
- Stream.EmitRecordWithBlob (ASTBlockHashAbbrev, Record, Blob);
1208
- ASTBlockHashOffset = Stream.GetCurrentBitNo () - Blob.size () * 8 ;
1155
+ ASTFileSignature ASTBlockHash;
1156
+ auto ASTBlockStartByte = ASTBlockRange.first >> 3 ;
1157
+ auto ASTBlockByteLength = (ASTBlockRange.second >> 3 ) - ASTBlockStartByte;
1158
+ std::tie (ASTBlockHash, Signature) = createSignature (
1159
+ StringRef (Buffer.begin (), StartOfUnhashedControl),
1160
+ StringRef (Buffer.begin () + ASTBlockStartByte, ASTBlockByteLength));
1161
+
1162
+ Record.append (ASTBlockHash.begin (), ASTBlockHash.end ());
1163
+ Stream.EmitRecord (AST_BLOCK_HASH, Record);
1209
1164
Record.clear ();
1210
-
1211
- Record.push_back (SIGNATURE);
1212
- Stream.EmitRecordWithBlob (SignatureAbbrev, Record, Blob);
1213
- SignatureOffset = Stream.GetCurrentBitNo () - Blob.size () * 8 ;
1165
+ Record.append (Signature.begin (), Signature.end ());
1166
+ Stream.EmitRecord (SIGNATURE, Record);
1214
1167
Record.clear ();
1215
1168
}
1216
1169
@@ -1279,7 +1232,7 @@ void ASTWriter::writeUnhashedControlBlock(Preprocessor &PP,
1279
1232
1280
1233
// Leave the options block.
1281
1234
Stream.ExitBlock ();
1282
- UnhashedControlBlockRange. second = Stream. GetCurrentBitNo () >> 3 ;
1235
+ return Signature ;
1283
1236
}
1284
1237
1285
1238
// / Write the control block.
@@ -4737,7 +4690,6 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4737
4690
4738
4691
ASTContext &Context = SemaRef.Context ;
4739
4692
Preprocessor &PP = SemaRef.PP ;
4740
- writeUnhashedControlBlock (PP, Context);
4741
4693
4742
4694
collectNonAffectingInputFiles ();
4743
4695
@@ -4886,7 +4838,7 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4886
4838
4887
4839
// Write the remaining AST contents.
4888
4840
Stream.FlushToWord ();
4889
- ASTBlockRange.first = Stream.GetCurrentBitNo () >> 3 ;
4841
+ ASTBlockRange.first = Stream.GetCurrentBitNo ();
4890
4842
Stream.EnterSubblock (AST_BLOCK_ID, 5 );
4891
4843
ASTBlockStartOffset = Stream.GetCurrentBitNo ();
4892
4844
@@ -5239,13 +5191,13 @@ ASTFileSignature ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
5239
5191
Stream.EmitRecord (STATISTICS, Record);
5240
5192
Stream.ExitBlock ();
5241
5193
Stream.FlushToWord ();
5242
- ASTBlockRange.second = Stream.GetCurrentBitNo () >> 3 ;
5194
+ ASTBlockRange.second = Stream.GetCurrentBitNo ();
5243
5195
5244
5196
// Write the module file extension blocks.
5245
5197
for (const auto &ExtWriter : ModuleFileExtensionWriters)
5246
5198
WriteModuleFileExtension (SemaRef, *ExtWriter);
5247
5199
5248
- return backpatchSignature ( );
5200
+ return writeUnhashedControlBlock (PP, Context );
5249
5201
}
5250
5202
5251
5203
void ASTWriter::WriteDeclUpdatesBlocks (RecordDataImpl &OffsetsRecord) {
0 commit comments