Skip to content

Commit 216e08e

Browse files
bob80905yrnkrn
andauthored
Remove unnecessary in C++11 c_str() calls (microsoft#5932)
While theoratically required in pre-C++11 to avoid re-allocation upon call, C++11 guarantees that c_str() returns a pointer to the internal array so pre-calling c_str() is no longer required. llvm-svn: 242983 --------- Co-authored-by: Yaron Keren <[email protected]>
1 parent ea4ff29 commit 216e08e

File tree

1 file changed

+2
-19
lines changed

1 file changed

+2
-19
lines changed

lib/Support/Unix/Signals.inc

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,6 @@ static void RemoveFilesToRemove() {
144144
// memory.
145145
std::vector<std::string>& FilesToRemoveRef = *FilesToRemove;
146146
for (unsigned i = 0, e = FilesToRemoveRef.size(); i != e; ++i) {
147-
// We rely on a std::string implementation for which repeated calls to
148-
// 'c_str()' don't allocate memory. We pre-call 'c_str()' on all of these
149-
// strings to try to ensure this is safe.
150147
const char *path = FilesToRemoveRef[i].c_str();
151148

152149
// Get the status so we can determine if it's a file or directory. If we
@@ -235,21 +232,7 @@ bool llvm::sys::RemoveFileOnSignal(StringRef Filename,
235232
std::string* ErrMsg) {
236233
{
237234
sys::SmartScopedLock<true> Guard(*SignalsMutex);
238-
std::vector<std::string>& FilesToRemoveRef = *FilesToRemove;
239-
std::string *OldPtr =
240-
FilesToRemoveRef.empty() ? nullptr : &FilesToRemoveRef[0];
241-
FilesToRemoveRef.push_back(Filename);
242-
243-
// We want to call 'c_str()' on every std::string in this vector so that if
244-
// the underlying implementation requires a re-allocation, it happens here
245-
// rather than inside of the signal handler. If we see the vector grow, we
246-
// have to call it on every entry. If it remains in place, we only need to
247-
// call it on the latest one.
248-
if (OldPtr == &FilesToRemoveRef[0])
249-
FilesToRemoveRef.back().c_str();
250-
else
251-
for (unsigned i = 0, e = FilesToRemoveRef.size(); i != e; ++i)
252-
FilesToRemoveRef[i].c_str();
235+
FilesToRemove->push_back(Filename);
253236
}
254237

255238
RegisterHandlers();
@@ -270,7 +253,7 @@ void llvm::sys::DontRemoveFileOnSignal(StringRef Filename) {
270253
// requires a reallocation on the first call may have had the call to c_str()
271254
// made on insertion become invalid by being copied down an element.
272255
for (std::vector<std::string>::iterator E = FilesToRemove->end(); I != E; ++I)
273-
I->c_str();
256+
(void)I->c_str();
274257
}
275258

276259
/// AddSignalHandler - Add a function to be called when a signal is delivered

0 commit comments

Comments
 (0)