[clang] 9679075 - [clang][deps] NFC: Simplify worker loop

Jan Svoboda via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 2 13:50:13 PST 2023


Author: Jan Svoboda
Date: 2023-03-02T13:50:08-08:00
New Revision: 9679075a1ae6a20ed102597bf166b3f3adc95499

URL: https://github.com/llvm/llvm-project/commit/9679075a1ae6a20ed102597bf166b3f3adc95499
DIFF: https://github.com/llvm/llvm-project/commit/9679075a1ae6a20ed102597bf166b3f3adc95499.diff

LOG: [clang][deps] NFC: Simplify worker loop

This patch simplifies the loop inside each worker by extracting index retrieval into a lambda function.

Reviewed By: benlangmuir

Differential Revision: https://reviews.llvm.org/D145101

Added: 
    

Modified: 
    clang/tools/clang-scan-deps/ClangScanDeps.cpp

Removed: 
    


################################################################################
diff  --git a/clang/tools/clang-scan-deps/ClangScanDeps.cpp b/clang/tools/clang-scan-deps/ClangScanDeps.cpp
index f200c8d6d8b71..2f0c4858c4c97 100644
--- a/clang/tools/clang-scan-deps/ClangScanDeps.cpp
+++ b/clang/tools/clang-scan-deps/ClangScanDeps.cpp
@@ -783,8 +783,15 @@ int main(int argc, const char **argv) {
   std::atomic<bool> HadErrors(false);
   std::optional<FullDeps> FD;
   P1689Deps PD;
+
   std::mutex Lock;
   size_t Index = 0;
+  auto GetNextInputIndex = [&]() -> std::optional<size_t> {
+    std::unique_lock<std::mutex> LockGuard(Lock);
+    if (Index < Inputs.size())
+      return Index++;
+    return {};
+  };
 
   if (Format == ScanningOutputFormat::Full)
     FD.emplace(ModuleName.empty() ? Inputs.size() : 0);
@@ -794,24 +801,14 @@ int main(int argc, const char **argv) {
                  << " files using " << Pool.getThreadCount() << " workers\n";
   }
   for (unsigned I = 0; I < Pool.getThreadCount(); ++I) {
-    Pool.async([I, &Lock, &Index, &Inputs, &HadErrors, &FD, &PD, &WorkerTools,
-                &DependencyOS, &Errs]() {
+    Pool.async([&, I]() {
       llvm::StringSet<> AlreadySeenModules;
-      while (true) {
-        const tooling::CompileCommand *Input;
-        std::string Filename;
-        std::string CWD;
-        size_t LocalIndex;
-        // Take the next input.
-        {
-          std::unique_lock<std::mutex> LockGuard(Lock);
-          if (Index >= Inputs.size())
-            return;
-          LocalIndex = Index;
-          Input = &Inputs[Index++];
-          Filename = std::move(Input->Filename);
-          CWD = std::move(Input->Directory);
-        }
+      while (auto MaybeInputIndex = GetNextInputIndex()) {
+        size_t LocalIndex = *MaybeInputIndex;
+        const tooling::CompileCommand *Input = &Inputs[LocalIndex];
+        std::string Filename = std::move(Input->Filename);
+        std::string CWD = std::move(Input->Directory);
+
         std::optional<StringRef> MaybeModuleName;
         if (!ModuleName.empty())
           MaybeModuleName = ModuleName;


        


More information about the cfe-commits mailing list