njames93 created this revision.
Herald added subscribers: dexonsmith, asbirlea, hiraditya.
njames93 published this revision for review.
Herald added projects: clang, LLDB, LLVM.
Herald added subscribers: llvm-commits, lldb-commits, cfe-commits.

Also replace a few calls to resize followed by zeroing the data to calling 
assign(size, 0).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D93733

Files:
  clang/lib/CodeGen/CodeGenFunction.h
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/Format/UnwrappedLineParser.cpp
  clang/lib/Lex/Lexer.cpp
  clang/lib/Lex/LiteralSupport.cpp
  clang/lib/Lex/Preprocessor.cpp
  clang/lib/Sema/SemaExpr.cpp
  lldb/source/Core/ValueObject.cpp
  lldb/source/Host/common/LZMA.cpp
  lldb/source/Utility/VASprintf.cpp
  llvm/lib/CodeGen/MachineLICM.cpp
  llvm/lib/CodeGen/TargetSchedule.cpp
  llvm/lib/DebugInfo/CodeView/TypeStreamMerger.cpp
  llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
  llvm/lib/MC/MCAsmStreamer.cpp
  llvm/lib/MC/MCDwarf.cpp
  llvm/lib/MC/MCParser/AsmParser.cpp
  llvm/lib/MC/MCParser/MasmParser.cpp
  llvm/lib/MC/MCStreamer.cpp
  llvm/lib/MC/StringTableBuilder.cpp
  llvm/lib/MC/WinCOFFObjectWriter.cpp
  llvm/lib/Object/IRSymtab.cpp
  llvm/lib/Object/MachOObjectFile.cpp
  llvm/lib/Support/ConvertUTFWrapper.cpp
  llvm/lib/Support/PrettyStackTrace.cpp
  llvm/lib/Support/raw_ostream.cpp

Index: llvm/lib/Support/raw_ostream.cpp
===================================================================
--- llvm/lib/Support/raw_ostream.cpp
+++ llvm/lib/Support/raw_ostream.cpp
@@ -331,7 +331,7 @@
   SmallVector<char, 128> V;
 
   while (true) {
-    V.resize(NextBufferSize);
+    V.resize_for_overwrite(NextBufferSize);
 
     // Try formatting into the SmallVector.
     size_t BytesUsed = Fmt.print(V.data(), NextBufferSize);
Index: llvm/lib/Support/PrettyStackTrace.cpp
===================================================================
--- llvm/lib/Support/PrettyStackTrace.cpp
+++ llvm/lib/Support/PrettyStackTrace.cpp
@@ -243,7 +243,7 @@
   }
 
   const int Size = SizeOrError + 1; // '\0'
-  Str.resize(Size);
+  Str.resize_for_overwrite(Size);
   va_start(AP, Format);
   vsnprintf(Str.data(), Size, Format, AP);
   va_end(AP);
Index: llvm/lib/Support/ConvertUTFWrapper.cpp
===================================================================
--- llvm/lib/Support/ConvertUTFWrapper.cpp
+++ llvm/lib/Support/ConvertUTFWrapper.cpp
@@ -160,7 +160,7 @@
   // UTF-8 encoding.  Allocate one extra byte for the null terminator though,
   // so that someone calling DstUTF16.data() gets a null terminated string.
   // We resize down later so we don't have to worry that this over allocates.
-  DstUTF16.resize(SrcUTF8.size()+1);
+  DstUTF16.resize_for_overwrite(SrcUTF8.size() + 1);
   UTF16 *Dst = &DstUTF16[0];
   UTF16 *DstEnd = Dst + DstUTF16.size();
 
Index: llvm/lib/Object/MachOObjectFile.cpp
===================================================================
--- llvm/lib/Object/MachOObjectFile.cpp
+++ llvm/lib/Object/MachOObjectFile.cpp
@@ -867,7 +867,7 @@
                           " LC_BUILD_VERSION_COMMAND has incorrect cmdsize");
 
   auto Start = Load.Ptr + sizeof(MachO::build_version_command);
-  BuildTools.resize(BVC.ntools);
+  BuildTools.resize_for_overwrite(BVC.ntools);
   for (unsigned i = 0; i < BVC.ntools; ++i)
     BuildTools[i] = Start + i * sizeof(MachO::build_tool_version);
 
Index: llvm/lib/Object/IRSymtab.cpp
===================================================================
--- llvm/lib/Object/IRSymtab.cpp
+++ llvm/lib/Object/IRSymtab.cpp
@@ -327,7 +327,7 @@
 
   // We are about to fill in the header's range fields, so reserve space for it
   // and copy it in afterwards.
-  Symtab.resize(sizeof(storage::Header));
+  Symtab.resize_for_overwrite(sizeof(storage::Header));
   writeRange(Hdr.Modules, Mods);
   writeRange(Hdr.Comdats, Comdats);
   writeRange(Hdr.Symbols, Syms);
@@ -370,7 +370,7 @@
     return std::move(E);
 
   StrtabBuilder.finalizeInOrder();
-  FC.Strtab.resize(StrtabBuilder.getSize());
+  FC.Strtab.resize_for_overwrite(StrtabBuilder.getSize());
   StrtabBuilder.write((uint8_t *)FC.Strtab.data());
 
   FC.TheReader = {{FC.Symtab.data(), FC.Symtab.size()},
Index: llvm/lib/MC/WinCOFFObjectWriter.cpp
===================================================================
--- llvm/lib/MC/WinCOFFObjectWriter.cpp
+++ llvm/lib/MC/WinCOFFObjectWriter.cpp
@@ -860,7 +860,7 @@
     COFFSymbol *File = createSymbol(".file");
     File->Data.SectionNumber = COFF::IMAGE_SYM_DEBUG;
     File->Data.StorageClass = COFF::IMAGE_SYM_CLASS_FILE;
-    File->Aux.resize(Count);
+    File->Aux.resize_for_overwrite(Count);
 
     unsigned Offset = 0;
     unsigned Length = Name.size();
Index: llvm/lib/MC/StringTableBuilder.cpp
===================================================================
--- llvm/lib/MC/StringTableBuilder.cpp
+++ llvm/lib/MC/StringTableBuilder.cpp
@@ -59,7 +59,7 @@
 void StringTableBuilder::write(raw_ostream &OS) const {
   assert(isFinalized());
   SmallString<0> Data;
-  Data.resize(getSize());
+  Data.resize_for_overwrite(getSize());
   write((uint8_t *)Data.data());
   OS << Data;
 }
Index: llvm/lib/MC/MCStreamer.cpp
===================================================================
--- llvm/lib/MC/MCStreamer.cpp
+++ llvm/lib/MC/MCStreamer.cpp
@@ -150,7 +150,7 @@
   const APInt Swapped = ShouldSwap ? Value.byteSwap() : Value;
   const unsigned Size = Value.getBitWidth() / 8;
   SmallString<10> Tmp;
-  Tmp.resize(Size);
+  Tmp.resize_for_overwrite(Size);
   StoreIntToMemory(Swapped, reinterpret_cast<uint8_t *>(Tmp.data()), Size);
   emitBytes(Tmp.str());
 }
@@ -350,7 +350,7 @@
 static void copyBytesForDefRange(SmallString<20> &BytePrefix,
                                  codeview::SymbolKind SymKind,
                                  const T &DefRangeHeader) {
-  BytePrefix.resize(2 + sizeof(T));
+  BytePrefix.resize_for_overwrite(2 + sizeof(T));
   codeview::ulittle16_t SymKindLE = codeview::ulittle16_t(SymKind);
   memcpy(&BytePrefix[0], &SymKindLE, 2);
   memcpy(&BytePrefix[2], &DefRangeHeader, sizeof(T));
Index: llvm/lib/MC/MCParser/MasmParser.cpp
===================================================================
--- llvm/lib/MC/MCParser/MasmParser.cpp
+++ llvm/lib/MC/MCParser/MasmParser.cpp
@@ -7139,8 +7139,8 @@
   // Merge the various outputs and inputs.  Output are expected first.
   if (NumOutputs || NumInputs) {
     unsigned NumExprs = NumOutputs + NumInputs;
-    OpDecls.resize(NumExprs);
-    Constraints.resize(NumExprs);
+    OpDecls.resize_for_overwrite(NumExprs);
+    Constraints.resize_for_overwrite(NumExprs);
     for (unsigned i = 0; i < NumOutputs; ++i) {
       OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
       Constraints[i] = OutputConstraints[i];
Index: llvm/lib/MC/MCParser/AsmParser.cpp
===================================================================
--- llvm/lib/MC/MCParser/AsmParser.cpp
+++ llvm/lib/MC/MCParser/AsmParser.cpp
@@ -5995,8 +5995,8 @@
   // Merge the various outputs and inputs.  Output are expected first.
   if (NumOutputs || NumInputs) {
     unsigned NumExprs = NumOutputs + NumInputs;
-    OpDecls.resize(NumExprs);
-    Constraints.resize(NumExprs);
+    OpDecls.resize_for_overwrite(NumExprs);
+    Constraints.resize_for_overwrite(NumExprs);
     for (unsigned i = 0; i < NumOutputs; ++i) {
       OpDecls[i] = std::make_pair(OutputDecls[i], OutputDeclsAddressOf[i]);
       Constraints[i] = OutputConstraints[i];
Index: llvm/lib/MC/MCDwarf.cpp
===================================================================
--- llvm/lib/MC/MCDwarf.cpp
+++ llvm/lib/MC/MCDwarf.cpp
@@ -330,7 +330,7 @@
   // Emit the strings without perturbing the offsets we used.
   LineStrings.finalizeInOrder();
   SmallString<0> Data;
-  Data.resize(LineStrings.getSize());
+  Data.resize_for_overwrite(LineStrings.getSize());
   LineStrings.write((uint8_t *)Data.data());
   MCOS->emitBinaryData(Data.str());
 }
Index: llvm/lib/MC/MCAsmStreamer.cpp
===================================================================
--- llvm/lib/MC/MCAsmStreamer.cpp
+++ llvm/lib/MC/MCAsmStreamer.cpp
@@ -2027,9 +2027,7 @@
   // representation. We do this by making a per-bit map to the fixup item index,
   // then trying to display it as nicely as possible.
   SmallVector<uint8_t, 64> FixupMap;
-  FixupMap.resize(Code.size() * 8);
-  for (unsigned i = 0, e = Code.size() * 8; i != e; ++i)
-    FixupMap[i] = 0;
+  FixupMap.assign(Code.size() * 8, 0);
 
   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
     MCFixup &F = Fixups[i];
Index: llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
===================================================================
--- llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
+++ llvm/lib/DebugInfo/DWARF/DWARFAcceleratorTable.cpp
@@ -403,7 +403,7 @@
   if (!AS.isValidOffsetForDataOfSize(C.tell(), AugmentationStringSize))
     return HeaderError(createStringError(errc::illegal_byte_sequence,
                                          "cannot read header augmentation"));
-  AugmentationString.resize(AugmentationStringSize);
+  AugmentationString.resize_for_overwrite(AugmentationStringSize);
   AS.getU8(C, reinterpret_cast<uint8_t *>(AugmentationString.data()),
            AugmentationStringSize);
   *Offset = C.tell();
Index: llvm/lib/DebugInfo/CodeView/TypeStreamMerger.cpp
===================================================================
--- llvm/lib/DebugInfo/CodeView/TypeStreamMerger.cpp
+++ llvm/lib/DebugInfo/CodeView/TypeStreamMerger.cpp
@@ -371,7 +371,7 @@
       MergingTypeTableBuilder &Dest =
           isIdRecord(Type.kind()) ? *DestIdStream : *DestTypeStream;
 
-      RemapStorage.resize(AlignedSize);
+      RemapStorage.resize_for_overwrite(AlignedSize);
       ArrayRef<uint8_t> Result = DoSerialize(RemapStorage);
       if (!Result.empty())
         DestIdx = Dest.insertRecordBytes(Result);
Index: llvm/lib/CodeGen/TargetSchedule.cpp
===================================================================
--- llvm/lib/CodeGen/TargetSchedule.cpp
+++ llvm/lib/CodeGen/TargetSchedule.cpp
@@ -67,7 +67,7 @@
   STI->initInstrItins(InstrItins);
 
   unsigned NumRes = SchedModel.getNumProcResourceKinds();
-  ResourceFactors.resize(NumRes);
+  ResourceFactors.resize_for_overwrite(NumRes);
   ResourceLCM = SchedModel.IssueWidth;
   for (unsigned Idx = 0; Idx < NumRes; ++Idx) {
     unsigned NumUnits = SchedModel.getProcResource(Idx)->NumUnits;
Index: llvm/lib/CodeGen/MachineLICM.cpp
===================================================================
--- llvm/lib/CodeGen/MachineLICM.cpp
+++ llvm/lib/CodeGen/MachineLICM.cpp
@@ -357,9 +357,8 @@
   if (PreRegAlloc) {
     // Estimate register pressure during pre-regalloc pass.
     unsigned NumRPS = TRI->getNumRegPressureSets();
-    RegPressure.resize(NumRPS);
-    std::fill(RegPressure.begin(), RegPressure.end(), 0);
-    RegLimit.resize(NumRPS);
+    RegPressure.assign(NumRPS, 0);
+    RegLimit.resize_for_overwrite(NumRPS);
     for (unsigned i = 0, e = NumRPS; i != e; ++i)
       RegLimit[i] = TRI->getRegPressureSetLimit(MF, i);
   }
Index: lldb/source/Utility/VASprintf.cpp
===================================================================
--- lldb/source/Utility/VASprintf.cpp
+++ lldb/source/Utility/VASprintf.cpp
@@ -25,7 +25,7 @@
   va_list copy_args;
   va_copy(copy_args, args);
 
-  buf.resize(buf.capacity());
+  buf.resize_for_overwrite(buf.capacity());
   // Write up to `capacity` bytes, ignoring the current size.
   int length = ::vsnprintf(buf.data(), buf.size(), fmt, args);
   if (length < 0) {
@@ -37,7 +37,7 @@
   if (size_t(length) >= buf.size()) {
     // The error formatted string didn't fit into our buffer, resize it to the
     // exact needed size, and retry
-    buf.resize(length + 1);
+    buf.resize_for_overwrite(length + 1);
     length = ::vsnprintf(buf.data(), buf.size(), fmt, copy_args);
     if (length < 0) {
       buf = error;
Index: lldb/source/Host/common/LZMA.cpp
===================================================================
--- lldb/source/Host/common/LZMA.cpp
+++ lldb/source/Host/common/LZMA.cpp
@@ -122,7 +122,7 @@
   if (auto err = uncompressedSize.takeError())
     return err;
 
-  Uncompressed.resize(*uncompressedSize);
+  Uncompressed.resize_for_overwrite(*uncompressedSize);
 
   // Decompress xz buffer to buffer.
   uint64_t memlimit = UINT64_MAX;
Index: lldb/source/Core/ValueObject.cpp
===================================================================
--- lldb/source/Core/ValueObject.cpp
+++ lldb/source/Core/ValueObject.cpp
@@ -192,7 +192,7 @@
 
       if (!first_update && CanProvideValue()) {
         need_compare_checksums = true;
-        old_checksum.resize(m_value_checksum.size());
+        old_checksum.resize_for_overwrite(m_value_checksum.size());
         std::copy(m_value_checksum.begin(), m_value_checksum.end(),
                   old_checksum.begin());
       }
Index: clang/lib/Sema/SemaExpr.cpp
===================================================================
--- clang/lib/Sema/SemaExpr.cpp
+++ clang/lib/Sema/SemaExpr.cpp
@@ -3447,7 +3447,7 @@
 
 static void ConvertUTF8ToWideString(unsigned CharByteWidth, StringRef Source,
                                     SmallString<32> &Target) {
-  Target.resize(CharByteWidth * (Source.size() + 1));
+  Target.resize_for_overwrite(CharByteWidth * (Source.size() + 1));
   char *ResultPtr = &Target[0];
   const llvm::UTF8 *ErrorPtr;
   bool success =
@@ -3663,7 +3663,7 @@
   // the buffer in case the token is copied to the buffer.  If getSpelling()
   // returns a StringRef to the memory buffer, it should have a null char at
   // the EOF, so it is also safe.
-  SpellingBuffer.resize(Tok.getLength() + 1);
+  SpellingBuffer.resize_for_overwrite(Tok.getLength() + 1);
 
   // Get the spelling of the token, which eliminates trigraphs, etc.
   bool Invalid = false;
Index: clang/lib/Lex/Preprocessor.cpp
===================================================================
--- clang/lib/Lex/Preprocessor.cpp
+++ clang/lib/Lex/Preprocessor.cpp
@@ -472,7 +472,7 @@
 
   // Resize the buffer if we need to copy into it.
   if (Tok.needsCleaning())
-    Buffer.resize(Tok.getLength());
+    Buffer.resize_for_overwrite(Tok.getLength());
 
   const char *Ptr = Buffer.data();
   unsigned Len = getSpelling(Tok, Ptr, Invalid);
Index: clang/lib/Lex/LiteralSupport.cpp
===================================================================
--- clang/lib/Lex/LiteralSupport.cpp
+++ clang/lib/Lex/LiteralSupport.cpp
@@ -1281,7 +1281,7 @@
          "Assumes sizeof(wchar) on target is <= 64");
 
   SmallVector<uint32_t, 4> codepoint_buffer;
-  codepoint_buffer.resize(end - begin);
+  codepoint_buffer.resize_for_overwrite(end - begin);
   uint32_t *buffer_begin = &codepoint_buffer.front();
   uint32_t *buffer_end = buffer_begin + codepoint_buffer.size();
 
@@ -1543,11 +1543,11 @@
   SizeBound *= CharByteWidth;
 
   // Size the temporary buffer to hold the result string data.
-  ResultBuf.resize(SizeBound);
+  ResultBuf.resize_for_overwrite(SizeBound);
 
   // Likewise, but for each string piece.
   SmallString<512> TokenBuf;
-  TokenBuf.resize(MaxTokenLength);
+  TokenBuf.resize_for_overwrite(MaxTokenLength);
 
   // Loop over all the strings, getting their spelling, and expanding them to
   // wide strings as appropriate.
@@ -1835,7 +1835,7 @@
                                                     unsigned ByteNo) const {
   // Get the spelling of the token.
   SmallString<32> SpellingBuffer;
-  SpellingBuffer.resize(Tok.getLength());
+  SpellingBuffer.resize_for_overwrite(Tok.getLength());
 
   bool StringInvalid = false;
   const char *SpellingPtr = &SpellingBuffer[0];
Index: clang/lib/Lex/Lexer.cpp
===================================================================
--- clang/lib/Lex/Lexer.cpp
+++ clang/lib/Lex/Lexer.cpp
@@ -355,7 +355,7 @@
     return StringRef(tokenBegin, length);
 
   // Hard case, we need to relex the characters into the string.
-  buffer.resize(length);
+  buffer.resize_for_overwrite(length);
   buffer.resize(getSpellingSlow(token, tokenBegin, options, buffer.data()));
   return StringRef(buffer.data(), buffer.size());
 }
Index: clang/lib/Format/UnwrappedLineParser.cpp
===================================================================
--- clang/lib/Format/UnwrappedLineParser.cpp
+++ clang/lib/Format/UnwrappedLineParser.cpp
@@ -285,8 +285,8 @@
     Lines.clear();
     while (!PPLevelBranchIndex.empty() &&
            PPLevelBranchIndex.back() + 1 >= PPLevelBranchCount.back()) {
-      PPLevelBranchIndex.resize(PPLevelBranchIndex.size() - 1);
-      PPLevelBranchCount.resize(PPLevelBranchCount.size() - 1);
+      PPLevelBranchIndex.pop_back();
+      PPLevelBranchCount.pop_back();
     }
     if (!PPLevelBranchIndex.empty()) {
       ++PPLevelBranchIndex.back();
Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -2090,7 +2090,7 @@
 
   // Convert List to what ConstantArray needs.
   SmallVector<llvm::Constant*, 8> UsedArray;
-  UsedArray.resize(List.size());
+  UsedArray.resize_for_overwrite(List.size());
   for (unsigned i = 0, e = List.size(); i != e; ++i) {
     UsedArray[i] =
         llvm::ConstantExpr::getPointerBitCastOrAddrSpaceCast(
Index: clang/lib/CodeGen/CodeGenFunction.h
===================================================================
--- clang/lib/CodeGen/CodeGenFunction.h
+++ clang/lib/CodeGen/CodeGenFunction.h
@@ -722,7 +722,7 @@
                                             ActiveFlag.isValid()};
 
     size_t OldSize = LifetimeExtendedCleanupStack.size();
-    LifetimeExtendedCleanupStack.resize(
+    LifetimeExtendedCleanupStack.resize_for_overwrite(
         LifetimeExtendedCleanupStack.size() + sizeof(Header) + Header.Size +
         (Header.IsConditional ? sizeof(ActiveFlag) : 0));
 
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to