@@ -205,20 +205,25 @@ enum class LineType {
205205static bool parseTypeCountMap (StringRef Input,
206206 DenseMap<StringRef, uint64_t > &TypeCountMap) {
207207 for (size_t Index = Input.find_first_not_of (' ' ); Index != StringRef::npos;) {
208- size_t n1 = Input.find (' :' , Index);
209- if (n1 == StringRef::npos)
208+ size_t ColonIndex = Input.find (' :' , Index);
209+ if (ColonIndex == StringRef::npos)
210210 return false ; // No colon found, invalid format.
211- StringRef TypeName = Input.substr (Index, n1 - Index);
212- // n2 is the start index of count.
213- size_t n2 = n1 + 1 ;
214- // n3 is the start index after the 'target:count' pair.
215- size_t n3 = Input.find_first_of (' ' , n2 );
211+ StringRef TypeName = Input.substr (Index, ColonIndex - Index);
212+ // CountIndex is the start index of count.
213+ size_t CountStartIndex = ColonIndex + 1 ;
214+ // NextIndex is the start index after the 'target:count' pair.
215+ size_t NextIndex = Input.find_first_of (' ' , CountStartIndex );
216216 uint64_t Count;
217- if (Input.substr (n2, n3 - n2).getAsInteger (10 , Count))
217+ if (Input.substr (CountStartIndex, NextIndex - CountStartIndex)
218+ .getAsInteger (10 , Count))
218219 return false ; // Invalid count.
219- TypeCountMap[TypeName] = Count;
220- Index = (n3 == StringRef::npos) ? StringRef::npos
221- : Input.find_first_not_of (' ' , n3);
220+ // Error on duplicated type names in one line of input.
221+ auto [Iter, Inserted] = TypeCountMap.insert ({TypeName, Count});
222+ if (!Inserted)
223+ return false ;
224+ Index = (NextIndex == StringRef::npos)
225+ ? StringRef::npos
226+ : Input.find_first_not_of (' ' , NextIndex);
222227 }
223228 return true ;
224229}
@@ -409,6 +414,8 @@ std::error_code SampleProfileReaderText::readImpl() {
409414 uint64_t FunctionHash = 0 ;
410415 uint32_t Attributes = 0 ;
411416 bool IsFlat = false ;
417+ // TODO: Update ParseLine to return an error code instead of a bool and
418+ // report it.
412419 if (!ParseLine (*LineIt, LineTy, Depth, NumSamples, LineOffset,
413420 Discriminator, FName, TargetCountMap, TypeCountMap,
414421 FunctionHash, Attributes, IsFlat)) {
0 commit comments