Bug 1318004 - Converts for(...; ...; ...) loops to use the new range-based loops in C++11. draft
authorSylvestre Ledru <sledru@mozilla.com>
Wed, 16 Nov 2016 17:12:13 +0100
changeset 442864 ab7ce6871e3fe983ee50ec7e83e0eee435a86288
parent 442082 b6c7e6fabe09aeec413ef45c4fc8692d318ad263
child 537919 ac72f44ced2b6d84282809d283283054e4903b11
push id36851
push usersledru@mozilla.com
push dateWed, 23 Nov 2016 12:54:25 +0000
bugs1318004
milestone53.0a1
Bug 1318004 - Converts for(...; ...; ...) loops to use the new range-based loops in C++11. MozReview-Commit-ID: lk5eJXUno9
toolkit/components/telemetry/Telemetry.cpp
toolkit/components/telemetry/TelemetryHistogram.cpp
toolkit/components/terminator/nsTerminator.cpp
toolkit/system/gnome/nsGConfService.cpp
toolkit/system/gnome/nsGSettingsService.cpp
toolkit/system/gnome/nsPackageKitService.cpp
toolkit/xre/nsAppRunner.cpp
--- a/toolkit/components/telemetry/Telemetry.cpp
+++ b/toolkit/components/telemetry/Telemetry.cpp
@@ -173,19 +173,17 @@ CombinedStacks::SizeOfExcludingThis() co
   // aMallocSizeOf(&mModules[0]), but on linux aMallocSizeOf will call
   // malloc_usable_size which is only safe on the pointers returned by malloc.
   // While it works on current libstdc++, it is better to be safe and not assume
   // that &vec[0] points to one. We could use a custom allocator, but
   // it doesn't seem worth it.
   size_t n = 0;
   n += mModules.capacity() * sizeof(Telemetry::ProcessedStack::Module);
   n += mStacks.capacity() * sizeof(Stack);
-  for (std::vector<Stack>::const_iterator i = mStacks.begin(),
-         e = mStacks.end(); i != e; ++i) {
-    const Stack& s = *i;
+  for (const auto & s : mStacks) {
     n += s.capacity() * sizeof(Telemetry::ProcessedStack::Frame);
   }
   return n;
 }
 
 // This utility function generates a string key that is used to index the annotations
 // in a hash map from |HangReports::AddHang|.
 nsresult
@@ -1422,18 +1420,17 @@ CreateJSStackObject(JSContext *cx, const
   return ret;
 }
 
 static bool
 IsValidBreakpadId(const std::string &breakpadId) {
   if (breakpadId.size() < 33) {
     return false;
   }
-  for (unsigned i = 0, n = breakpadId.size(); i < n; ++i) {
-    char c = breakpadId[i];
+  for (char c : breakpadId) {
     if ((c < '0' || c > '9') && (c < 'A' || c > 'F')) {
       return false;
     }
   }
   return true;
 }
 
 // Read a stack from the given file name. In case of any error, aStack is
@@ -1586,23 +1583,21 @@ CreateJSHangAnnotations(JSContext* cx, c
   if (!annotationsArray) {
     returnedObject.set(nullptr);
     return;
   }
   // We keep track of the annotations we reported in this hash set, so we can
   // discard duplicated ones.
   nsTHashtable<nsStringHashKey> reportedAnnotations;
   size_t annotationIndex = 0;
-  for (const HangAnnotationsPtr *i = annotations.begin(), *e = annotations.end();
-       i != e; ++i) {
+  for (const auto & curAnnotations : annotations) {
     JS::RootedObject jsAnnotation(cx, JS_NewPlainObject(cx));
     if (!jsAnnotation) {
       continue;
     }
-    const HangAnnotationsPtr& curAnnotations = *i;
     // Build a key to index the current annotations in our hash set.
     nsAutoString annotationsKey;
     nsresult rv = ComputeAnnotationsKey(curAnnotations, annotationsKey);
     if (NS_FAILED(rv)) {
       continue;
     }
     // Check if the annotations are in the set. If that's the case, don't double report.
     if (reportedAnnotations.GetEntry(annotationsKey)) {
@@ -1727,19 +1722,19 @@ TelemetryImpl::GetThreadHangStats(JSCont
       if (!JS_DefineElement(cx, retObj, threadIndex++, obj, JSPROP_ENUMERATE)) {
         return NS_ERROR_FAILURE;
       }
     }
   }
 
   // Add saved threads next
   MutexAutoLock autoLock(mThreadHangStatsMutex);
-  for (size_t i = 0; i < mThreadHangStats.length(); i++) {
+  for (auto & stat : mThreadHangStats) {
     JS::RootedObject obj(cx,
-      CreateJSThreadHangStats(cx, mThreadHangStats[i]));
+      CreateJSThreadHangStats(cx, stat));
     if (!JS_DefineElement(cx, retObj, threadIndex++, obj, JSPROP_ENUMERATE)) {
       return NS_ERROR_FAILURE;
     }
   }
   ret.setObject(*retObj);
   return NS_OK;
 }
 
@@ -2651,19 +2646,17 @@ GetStackAndModules(const std::vector<uin
     rawStack[stackIndex].mPC = std::numeric_limits<uintptr_t>::max();
   }
 
   std::sort(rawStack.begin(), rawStack.end(), CompareByIndex);
 #endif
 
   // Copy the information to the return value.
   ProcessedStack Ret;
-  for (std::vector<StackFrame>::iterator i = rawStack.begin(),
-         e = rawStack.end(); i != e; ++i) {
-    const StackFrame &rawFrame = *i;
+  for (auto & rawFrame : rawStack) {
     mozilla::Telemetry::ProcessedStack::Frame frame = { rawFrame.mPC, rawFrame.mModIndex };
     Ret.AddFrame(frame);
   }
 
 #ifdef MOZ_ENABLE_PROFILER_SPS
   for (unsigned i = 0, n = rawModules.GetSize(); i != n; ++i) {
     const SharedLibrary &info = rawModules.GetEntry(i);
     const std::string &name = info.GetName();
@@ -2723,20 +2716,20 @@ HangStack::AppendViaBuffer(const char* a
 
   // Include null-terminator in length count.
   if (!mBuffer.reserve(mBuffer.length() + aLength + 1)) {
     return nullptr;
   }
 
   if (prevStart != mBuffer.begin()) {
     // The buffer has moved; we have to adjust pointers in the stack.
-    for (const char** entry = this->begin(); entry != this->end(); entry++) {
-      if (*entry >= prevStart && *entry < prevEnd) {
+    for (auto & entry : *this) {
+      if (entry >= prevStart && entry < prevEnd) {
         // Move from old buffer to new buffer.
-        *entry += mBuffer.begin() - prevStart;
+        entry += mBuffer.begin() - prevStart;
       }
     }
   }
 
   return InfallibleAppendViaBuffer(aText, aLength);
 }
 
 uint32_t
--- a/toolkit/components/telemetry/TelemetryHistogram.cpp
+++ b/toolkit/components/telemetry/TelemetryHistogram.cpp
@@ -276,18 +276,17 @@ internal_IsExpired(const Histogram *hist
 }
 
 nsresult
 internal_GetRegisteredHistogramIds(bool keyed, uint32_t dataset,
                                    uint32_t *aCount, char*** aHistograms)
 {
   nsTArray<char*> collection;
 
-  for (size_t i = 0; i < mozilla::ArrayLength(gHistograms); ++i) {
-    const HistogramInfo& h = gHistograms[i];
+  for (const auto & h : gHistograms) {
     if (IsExpiredVersion(h.expiration()) ||
         h.keyed != keyed ||
         !IsInDataset(h.dataset, dataset)) {
       continue;
     }
 
     const char* id = h.id();
     const size_t len = strlen(id);
@@ -746,19 +745,17 @@ internal_HistogramClear(Histogram& aHist
 
 namespace {
 
 void internal_Accumulate(mozilla::Telemetry::ID aHistogram, uint32_t aSample);
 
 void
 internal_IdentifyCorruptHistograms(StatisticsRecorder::Histograms &hs)
 {
-  for (HistogramIterator it = hs.begin(); it != hs.end(); ++it) {
-    Histogram *h = *it;
-
+  for (auto h : hs) {
     mozilla::Telemetry::ID id;
     nsresult rv = internal_GetHistogramEnumId(h->histogram_name().c_str(), &id);
     // This histogram isn't a static histogram, just ignore it.
     if (NS_FAILED(rv)) {
       continue;
     }
 
     if (gCorruptHistograms[id]) {
@@ -2059,18 +2056,17 @@ void TelemetryHistogram::InitializeGloba
 
 #ifdef DEBUG
   gHistogramMap.MarkImmutable();
 #endif
 
   mozilla::PodArrayZero(gCorruptHistograms);
 
   // Create registered keyed histograms
-  for (size_t i = 0; i < mozilla::ArrayLength(gHistograms); ++i) {
-    const HistogramInfo& h = gHistograms[i];
+  for (const auto & h : gHistograms) {
     if (!h.keyed) {
       continue;
     }
 
     const nsDependentCString id(h.id());
     const nsDependentCString expiration(h.expiration());
     gKeyedHistograms.Put(id, new KeyedHistogram(id, expiration, h.histogramType,
                                                 h.min, h.max, h.bucketCount, h.dataset));
@@ -2160,19 +2156,18 @@ TelemetryHistogram::SetCanRecordExtended
   gCanRecordExtended = b;
 }
 
 
 void
 TelemetryHistogram::InitHistogramRecordingEnabled()
 {
   StaticMutexAutoLock locker(gTelemetryHistogramMutex);
-  const size_t length = mozilla::ArrayLength(kRecordingInitiallyDisabledIDs);
-  for (size_t i = 0; i < length; i++) {
-    internal_SetHistogramRecordingEnabled(kRecordingInitiallyDisabledIDs[i],
+  for (auto recordingInitiallyDisabledID : kRecordingInitiallyDisabledIDs) {
+    internal_SetHistogramRecordingEnabled(recordingInitiallyDisabledID,
                                           false);
   }
 }
 
 void
 TelemetryHistogram::SetHistogramRecordingEnabled(mozilla::Telemetry::ID aID,
                                                  bool aEnabled)
 {
@@ -2397,18 +2392,17 @@ TelemetryHistogram::CreateHistogramSnaps
   // depend on histogram enumeration order.
   //
   // Of course, we hope that all of these corruption-statistics
   // histograms are not themselves corrupt...
   internal_IdentifyCorruptHistograms(hs);
 
   // OK, now we can actually reflect things.
   JS::Rooted<JSObject*> hobj(cx);
-  for (HistogramIterator it = hs.begin(); it != hs.end(); ++it) {
-    Histogram *h = *it;
+  for (auto h : hs) {
     if (!internal_ShouldReflectHistogram(h) || internal_IsEmpty(h) ||
         internal_IsExpired(h)) {
       continue;
     }
 
     Histogram* original = h;
 #if !defined(MOZ_WIDGET_GONK) && !defined(MOZ_WIDGET_ANDROID)
     if (subsession) {
@@ -2638,18 +2632,17 @@ TelemetryHistogram::GetMapShallowSizesOf
 size_t
 TelemetryHistogram::GetHistogramSizesofIncludingThis(mozilla::MallocSizeOf
                                                      aMallocSizeOf)
 {
   StaticMutexAutoLock locker(gTelemetryHistogramMutex);
   StatisticsRecorder::Histograms hs;
   StatisticsRecorder::GetHistograms(&hs);
   size_t n = 0;
-  for (HistogramIterator it = hs.begin(); it != hs.end(); ++it) {
-    Histogram *h = *it;
+  for (auto h : hs) {
     n += h->SizeOfIncludingThis(aMallocSizeOf);
   }
   return n;
 }
 
 // This method takes the lock only to double-buffer the batched telemetry.
 // It releases the lock before calling out to IPC code which can (and does)
 // Accumulate (which would deadlock)
--- a/toolkit/components/terminator/nsTerminator.cpp
+++ b/toolkit/components/terminator/nsTerminator.cpp
@@ -336,18 +336,18 @@ nsTerminator::nsTerminator()
 nsresult
 nsTerminator::SelfInit()
 {
   nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
   if (!os) {
     return NS_ERROR_UNEXPECTED;
   }
 
-  for (size_t i = 0; i < ArrayLength(sShutdownSteps); ++i) {
-    DebugOnly<nsresult> rv = os->AddObserver(this, sShutdownSteps[i].mTopic, false);
+  for (auto& shutdownStep : sShutdownSteps) {
+    DebugOnly<nsresult> rv = os->AddObserver(this, shutdownStep.mTopic, false);
     NS_WARNING_ASSERTION(NS_SUCCEEDED(rv), "AddObserver failed");
   }
 
   return NS_OK;
 }
 
 // Actually launch these threads. This takes place at the first sign of shutdown.
 void
@@ -501,32 +501,28 @@ nsTerminator::UpdateTelemetry()
   // late to record such data into Telemetry, so we write it to disk
   // and read it upon the next startup.
   //
 
   // Build JSON.
   UniquePtr<nsCString> telemetryData(new nsCString());
   telemetryData->AppendLiteral("{");
   size_t fields = 0;
-  for (size_t i = 0; i < ArrayLength(sShutdownSteps); ++i) {
-    if (sShutdownSteps[i].mTicks < 0) {
+  for (auto& shutdownStep : sShutdownSteps) {
+    if (shutdownStep.mTicks < 0) {
       // Ignore this field.
       continue;
     }
     if (fields++ > 0) {
       telemetryData->Append(", ");
     }
-    telemetryData->AppendLiteral("\"");
-    telemetryData->Append(sShutdownSteps[i].mTopic);
-    telemetryData->AppendLiteral("\": ");
-    telemetryData->AppendInt(sShutdownSteps[i].mTicks);
     telemetryData->AppendLiteral(R"(")");
-    telemetryData->Append(sShutdownStep.mTopic);
+    telemetryData->Append(shutdownStep.mTopic);
     telemetryData->AppendLiteral(R"(": )");
-    telemetryData->AppendInt(sShutdownStep.mTicks);
+    telemetryData->AppendInt(shutdownStep.mTicks);
   }
   telemetryData->AppendLiteral("}");
 
   if (fields == 0) {
     // Nothing to write
       return;
   }
 
--- a/toolkit/system/gnome/nsGConfService.cpp
+++ b/toolkit/system/gnome/nsGConfService.cpp
@@ -77,20 +77,20 @@ nsGConfService::Init()
 #undef FUNC
 
   if (!gconfLib) {
     gconfLib = PR_LoadLibrary("libgconf-2.so.4");
     if (!gconfLib)
       return NS_ERROR_FAILURE;
   }
 
-  for (uint32_t i = 0; i < ArrayLength(kGConfSymbols); i++) {
-    *kGConfSymbols[i].function =
-      PR_FindFunctionSymbol(gconfLib, kGConfSymbols[i].functionName);
-    if (!*kGConfSymbols[i].function) {
+  for (auto GConfSymbol : kGConfSymbols) {
+    *GConfSymbol.function =
+      PR_FindFunctionSymbol(gconfLib, GConfSymbol.functionName);
+    if (!*GConfSymbol.function) {
       return NS_ERROR_FAILURE;
     }
   }
 
   mClient = gconf_client_get_default();
   return mClient ? NS_OK : NS_ERROR_FAILURE;
 }
 
--- a/toolkit/system/gnome/nsGSettingsService.cpp
+++ b/toolkit/system/gnome/nsGSettingsService.cpp
@@ -303,20 +303,20 @@ nsGSettingsService::Init()
 #undef FUNC
 
   if (!gioLib) {
     gioLib = PR_LoadLibrary("libgio-2.0.so.0");
     if (!gioLib)
       return NS_ERROR_FAILURE;
   }
 
-  for (uint32_t i = 0; i < ArrayLength(kGSettingsSymbols); i++) {
-    *kGSettingsSymbols[i].function =
-      PR_FindFunctionSymbol(gioLib, kGSettingsSymbols[i].functionName);
-    if (!*kGSettingsSymbols[i].function) {
+  for (auto GSettingsSymbol : kGSettingsSymbols) {
+    *GSettingsSymbol.function =
+      PR_FindFunctionSymbol(gioLib, GSettingsSymbol.functionName);
+    if (!*GSettingsSymbol.function) {
       return NS_ERROR_FAILURE;
     }
   }
 
   return NS_OK;
 }
 
 NS_IMPL_ISUPPORTS(nsGSettingsService, nsIGSettingsService)
--- a/toolkit/system/gnome/nsPackageKitService.cpp
+++ b/toolkit/system/gnome/nsPackageKitService.cpp
@@ -88,20 +88,20 @@ nsPackageKitService::Init()
 #undef FUNC
 
   if (!gioLib) {
     gioLib = PR_LoadLibrary("libgio-2.0.so.0");
     if (!gioLib)
       return NS_ERROR_FAILURE;
   }
 
-  for (uint32_t i = 0; i < ArrayLength(kGDBusSymbols); i++) {
-    *kGDBusSymbols[i].function =
-      PR_FindFunctionSymbol(gioLib, kGDBusSymbols[i].functionName);
-    if (!*kGDBusSymbols[i].function) {
+  for (auto GDBusSymbol : kGDBusSymbols) {
+    *GDBusSymbol.function =
+      PR_FindFunctionSymbol(gioLib, GDBusSymbol.functionName);
+    if (!*GDBusSymbol.function) {
       return NS_ERROR_FAILURE;
     }
   }
 
   return NS_OK;
 }
 
 NS_IMPL_ISUPPORTS(nsPackageKitService, nsIPackageKitService)
--- a/toolkit/xre/nsAppRunner.cpp
+++ b/toolkit/xre/nsAppRunner.cpp
@@ -2743,28 +2743,28 @@ static struct SavedVar {
   const char *name;
   char *value;
 } gSavedVars[] = {
   {"XUL_APP_FILE", nullptr}
 };
 
 static void SaveStateForAppInitiatedRestart()
 {
-  for (size_t i = 0; i < ArrayLength(gSavedVars); ++i) {
-    const char *s = PR_GetEnv(gSavedVars[i].name);
+  for (auto & savedVar : gSavedVars) {
+    const char *s = PR_GetEnv(savedVar.name);
     if (s)
-      gSavedVars[i].value = PR_smprintf("%s=%s", gSavedVars[i].name, s);
+      savedVar.value = PR_smprintf("%s=%s", savedVar.name, s);
   }
 }
 
 static void RestoreStateForAppInitiatedRestart()
 {
-  for (size_t i = 0; i < ArrayLength(gSavedVars); ++i) {
-    if (gSavedVars[i].value)
-      PR_SetEnv(gSavedVars[i].value);
+  for (auto & savedVar : gSavedVars) {
+    if (savedVar.value)
+      PR_SetEnv(savedVar.value);
   }
 }
 
 #ifdef MOZ_CRASHREPORTER
 // When we first initialize the crash reporter we don't have a profile,
 // so we set the minidump path to $TEMP.  Once we have a profile,
 // we set it to $PROFILE/minidumps, creating the directory
 // if needed.