浏览代码

cmComputeComponentGraph: use `size_t` for component indices

This avoids using casts everywhere when dealing with the sizes.
Ben Boeckel 2 年之前
父节点
当前提交
91a26ce041

+ 15 - 12
Source/cmComputeComponentGraph.cxx

@@ -4,8 +4,11 @@
 
 #include <algorithm>
 #include <cassert>
+#include <cstddef>
+#include <limits>
 
-const int cmComputeComponentGraph::INVALID_COMPONENT = -1;
+const size_t cmComputeComponentGraph::INVALID_COMPONENT =
+  std::numeric_limits<size_t>::max();
 
 cmComputeComponentGraph::cmComputeComponentGraph(Graph const& input)
   : InputGraph(input)
@@ -27,7 +30,7 @@ void cmComputeComponentGraph::Compute()
 
 void cmComputeComponentGraph::Tarjan()
 {
-  int n = static_cast<int>(this->InputGraph.size());
+  size_t n = this->InputGraph.size();
   TarjanEntry entry = { 0, 0 };
   this->TarjanEntries.resize(0);
   this->TarjanEntries.resize(n, entry);
@@ -36,7 +39,7 @@ void cmComputeComponentGraph::Tarjan()
   this->TarjanWalkId = 0;
   this->TarjanVisited.resize(0);
   this->TarjanVisited.resize(n, 0);
-  for (int i = 0; i < n; ++i) {
+  for (size_t i = 0; i < n; ++i) {
     // Start a new DFS from this node if it has never been visited.
     if (!this->TarjanVisited[i]) {
       assert(this->TarjanStack.empty());
@@ -47,7 +50,7 @@ void cmComputeComponentGraph::Tarjan()
   }
 }
 
-void cmComputeComponentGraph::TarjanVisit(int i)
+void cmComputeComponentGraph::TarjanVisit(size_t i)
 {
   // We are now visiting this node.
   this->TarjanVisited[i] = this->TarjanWalkId;
@@ -61,7 +64,7 @@ void cmComputeComponentGraph::TarjanVisit(int i)
   // Follow outgoing edges.
   EdgeList const& nl = this->InputGraph[i];
   for (cmGraphEdge const& ni : nl) {
-    int j = ni;
+    size_t j = ni;
 
     // Ignore edges to nodes that have been reached by a previous DFS
     // walk.  Since we did not reach the current node from that walk
@@ -90,12 +93,12 @@ void cmComputeComponentGraph::TarjanVisit(int i)
   // Check if we have found a component.
   if (this->TarjanEntries[i].Root == i) {
     // Yes.  Create it.
-    int c = static_cast<int>(this->Components.size());
+    size_t c = this->Components.size();
     this->Components.emplace_back();
     NodeList& component = this->Components[c];
 
     // Populate the component list.
-    int j;
+    size_t j;
     do {
       // Get the next member of the component.
       j = this->TarjanStack.top();
@@ -118,13 +121,13 @@ void cmComputeComponentGraph::TransferEdges()
 {
   // Map inter-component edges in the original graph to edges in the
   // component graph.
-  int n = static_cast<int>(this->InputGraph.size());
-  for (int i = 0; i < n; ++i) {
-    int i_component = this->TarjanComponents[i];
+  size_t n = this->InputGraph.size();
+  for (size_t i = 0; i < n; ++i) {
+    size_t i_component = this->TarjanComponents[i];
     EdgeList const& nl = this->InputGraph[i];
     for (cmGraphEdge const& ni : nl) {
-      int j = ni;
-      int j_component = this->TarjanComponents[j];
+      size_t j = ni;
+      size_t j_component = this->TarjanComponents[j];
       if (i_component != j_component) {
         // We do not attempt to combine duplicate edges, but instead
         // store the inter-component edges with suitable multiplicity.

+ 13 - 12
Source/cmComputeComponentGraph.h

@@ -4,6 +4,7 @@
 
 #include "cmConfigure.h" // IWYU pragma: keep
 
+#include <cstddef>
 #include <stack>
 #include <vector>
 
@@ -35,7 +36,7 @@ public:
 
   /** Get the adjacency list of the component graph.  */
   Graph const& GetComponentGraph() const { return this->ComponentGraph; }
-  EdgeList const& GetComponentGraphEdges(int c) const
+  EdgeList const& GetComponentGraphEdges(size_t c) const
   {
     return this->ComponentGraph[c];
   }
@@ -45,15 +46,15 @@ public:
   {
     return this->Components;
   }
-  NodeList const& GetComponent(int c) const { return this->Components[c]; }
+  NodeList const& GetComponent(size_t c) const { return this->Components[c]; }
 
   /** Get map from original node index to component index.  */
-  std::vector<int> const& GetComponentMap() const
+  std::vector<size_t> const& GetComponentMap() const
   {
     return this->TarjanComponents;
   }
 
-  static const int INVALID_COMPONENT;
+  static const size_t INVALID_COMPONENT;
 
 private:
   void TransferEdges();
@@ -64,18 +65,18 @@ private:
   // Tarjan's algorithm.
   struct TarjanEntry
   {
-    int Root;
-    int VisitIndex;
+    size_t Root;
+    size_t VisitIndex;
   };
-  std::vector<int> TarjanVisited;
-  std::vector<int> TarjanComponents;
+  std::vector<size_t> TarjanVisited;
+  std::vector<size_t> TarjanComponents;
   std::vector<TarjanEntry> TarjanEntries;
   std::vector<NodeList> Components;
-  std::stack<int> TarjanStack;
-  int TarjanWalkId;
-  int TarjanIndex;
+  std::stack<size_t> TarjanStack;
+  size_t TarjanWalkId;
+  size_t TarjanIndex;
   void Tarjan();
-  void TarjanVisit(int i);
+  void TarjanVisit(size_t i);
 
   // Connected components.
 };

+ 53 - 50
Source/cmComputeLinkDepends.cxx

@@ -4,6 +4,7 @@
 
 #include <algorithm>
 #include <cassert>
+#include <cstddef>
 #include <cstdio>
 #include <iterator>
 #include <sstream>
@@ -375,8 +376,8 @@ cmComputeLinkDepends::Compute()
   // Compute the final set of link entries.
   // Iterate in reverse order so we can keep only the last occurrence
   // of a shared library.
-  std::set<int> emitted;
-  for (int i : cmReverseRange(this->FinalLinkOrder)) {
+  std::set<size_t> emitted;
+  for (size_t i : cmReverseRange(this->FinalLinkOrder)) {
     LinkEntry const& e = this->EntryList[i];
     cmGeneratorTarget const* t = e.Target;
     // Entries that we know the linker will re-use do not need to be repeated.
@@ -388,7 +389,7 @@ cmComputeLinkDepends::Compute()
   // Place explicitly linked object files in the front.  The linker will
   // always use them anyway, and they may depend on symbols from libraries.
   // Append in reverse order since we reverse the final order below.
-  for (int i : cmReverseRange(this->ObjectEntries)) {
+  for (size_t i : cmReverseRange(this->ObjectEntries)) {
     this->FinalLinkEntries.emplace_back(this->EntryList[i]);
   }
   // Reverse the resulting order since we iterated in reverse.
@@ -432,11 +433,11 @@ std::string const& cmComputeLinkDepends::GetCurrentFeature(
   return it == this->LinkLibraryOverride.end() ? defaultFeature : it->second;
 }
 
-std::pair<std::map<cmLinkItem, int>::iterator, bool>
+std::pair<std::map<cmLinkItem, size_t>::iterator, bool>
 cmComputeLinkDepends::AllocateLinkEntry(cmLinkItem const& item)
 {
-  std::map<cmLinkItem, int>::value_type index_entry(
-    item, static_cast<int>(this->EntryList.size()));
+  std::map<cmLinkItem, size_t>::value_type index_entry(
+    item, static_cast<size_t>(this->EntryList.size()));
   auto lei = this->LinkEntryIndex.insert(index_entry);
   if (lei.second) {
     this->EntryList.emplace_back();
@@ -446,8 +447,8 @@ cmComputeLinkDepends::AllocateLinkEntry(cmLinkItem const& item)
   return lei;
 }
 
-std::pair<int, bool> cmComputeLinkDepends::AddLinkEntry(cmLinkItem const& item,
-                                                        int groupIndex)
+std::pair<size_t, bool> cmComputeLinkDepends::AddLinkEntry(
+  cmLinkItem const& item, size_t groupIndex)
 {
   // Allocate a spot for the item entry.
   auto lei = this->AllocateLinkEntry(item);
@@ -459,7 +460,7 @@ std::pair<int, bool> cmComputeLinkDepends::AddLinkEntry(cmLinkItem const& item,
   }
 
   // Initialize the item entry.
-  int index = lei.first->second;
+  size_t index = lei.first->second;
   LinkEntry& entry = this->EntryList[index];
   entry.Item = BT<std::string>(item.AsStr(), item.Backtrace);
   entry.Target = item.Target;
@@ -506,7 +507,7 @@ void cmComputeLinkDepends::AddLinkObject(cmLinkItem const& item)
   }
 
   // Initialize the item entry.
-  int index = lei.first->second;
+  size_t index = lei.first->second;
   LinkEntry& entry = this->EntryList[index];
   entry.Item = BT<std::string>(item.AsStr(), item.Backtrace);
   entry.Kind = LinkEntry::Object;
@@ -518,7 +519,7 @@ void cmComputeLinkDepends::AddLinkObject(cmLinkItem const& item)
 void cmComputeLinkDepends::FollowLinkEntry(BFSEntry qe)
 {
   // Get this entry representation.
-  int depender_index =
+  size_t depender_index =
     qe.GroupIndex == cmComputeComponentGraph::INVALID_COMPONENT
     ? qe.Index
     : qe.GroupIndex;
@@ -559,7 +560,7 @@ void cmComputeLinkDepends::FollowLinkEntry(BFSEntry qe)
   }
 }
 
-void cmComputeLinkDepends::FollowSharedDeps(int depender_index,
+void cmComputeLinkDepends::FollowSharedDeps(size_t depender_index,
                                             cmLinkInterface const* iface,
                                             bool follow_interface)
 {
@@ -573,7 +574,7 @@ void cmComputeLinkDepends::FollowSharedDeps(int depender_index,
 }
 
 void cmComputeLinkDepends::QueueSharedDependencies(
-  int depender_index, std::vector<cmLinkItem> const& deps)
+  size_t depender_index, std::vector<cmLinkItem> const& deps)
 {
   for (cmLinkItem const& li : deps) {
     SharedDepEntry qe;
@@ -587,7 +588,7 @@ void cmComputeLinkDepends::HandleSharedDependency(SharedDepEntry const& dep)
 {
   // Allocate a spot for the item entry.
   auto lei = this->AllocateLinkEntry(dep.Item);
-  int index = lei.first->second;
+  size_t index = lei.first->second;
 
   // Check if the target does not already has an entry.
   if (lei.second) {
@@ -620,7 +621,7 @@ void cmComputeLinkDepends::HandleSharedDependency(SharedDepEntry const& dep)
   }
 }
 
-void cmComputeLinkDepends::AddVarLinkEntries(int depender_index,
+void cmComputeLinkDepends::AddVarLinkEntries(size_t depender_index,
                                              const char* value)
 {
   // This is called to add the dependencies named by
@@ -699,17 +700,18 @@ void cmComputeLinkDepends::AddDirectLinkEntries()
 }
 
 template <typename T>
-void cmComputeLinkDepends::AddLinkEntries(int depender_index,
+void cmComputeLinkDepends::AddLinkEntries(size_t depender_index,
                                           std::vector<T> const& libs)
 {
   // Track inferred dependency sets implied by this list.
-  std::map<int, DependSet> dependSets;
+  std::map<size_t, DependSet> dependSets;
   std::string feature = LinkEntry::DEFAULT;
 
   bool inGroup = false;
-  std::pair<int, bool> groupIndex{ cmComputeComponentGraph::INVALID_COMPONENT,
-                                   false };
-  std::vector<int> groupItems;
+  std::pair<size_t, bool> groupIndex{
+    cmComputeComponentGraph::INVALID_COMPONENT, false
+  };
+  std::vector<size_t> groupItems;
 
   // Loop over the libraries linked directly by the depender.
   for (T const& l : libs) {
@@ -768,7 +770,7 @@ void cmComputeLinkDepends::AddLinkEntries(int depender_index,
       continue;
     }
 
-    int dependee_index;
+    size_t dependee_index;
 
     if (cmHasPrefix(item.AsStr(), LG_END) && cmHasSuffix(item.AsStr(), '>')) {
       dependee_index = groupIndex.first;
@@ -902,7 +904,7 @@ void cmComputeLinkDepends::AddLinkEntries(int depender_index,
       // store item index for dependencies handling
       groupItems.push_back(dependee_index);
     } else {
-      std::vector<int> indexes;
+      std::vector<size_t> indexes;
       bool entryHandled = false;
       // search any occurrence of the library in already defined groups
       for (const auto& group : this->GroupItems) {
@@ -964,7 +966,7 @@ void cmComputeLinkDepends::AddLinkObjects(std::vector<cmLinkItem> const& objs)
   }
 }
 
-cmLinkItem cmComputeLinkDepends::ResolveLinkItem(int depender_index,
+cmLinkItem cmComputeLinkDepends::ResolveLinkItem(size_t depender_index,
                                                  const std::string& name)
 {
   // Look for a target in the scope of the depender.
@@ -983,7 +985,7 @@ void cmComputeLinkDepends::InferDependencies()
   // The inferred dependency sets for each item list the possible
   // dependencies.  The intersection of the sets for one item form its
   // inferred dependencies.
-  for (unsigned int depender_index = 0;
+  for (size_t depender_index = 0;
        depender_index < this->InferredDependSets.size(); ++depender_index) {
     // Skip items for which dependencies do not need to be inferred or
     // for which the inferred dependency sets are empty.
@@ -1020,7 +1022,7 @@ void cmComputeLinkDepends::UpdateGroupDependencies()
   // over raw items by the group it belongs to, if any.
   for (auto& edgeList : this->EntryConstraintGraph) {
     for (auto& edge : edgeList) {
-      int index = edge;
+      size_t index = edge;
       if (this->EntryList[index].Kind == LinkEntry::Group ||
           this->EntryList[index].Kind == LinkEntry::Flag ||
           this->EntryList[index].Kind == LinkEntry::Object) {
@@ -1056,8 +1058,8 @@ void cmComputeLinkDepends::CleanConstraintGraph()
 bool cmComputeLinkDepends::CheckCircularDependencies() const
 {
   std::vector<NodeList> const& components = this->CCG->GetComponents();
-  int nc = static_cast<int>(components.size());
-  for (int c = 0; c < nc; ++c) {
+  size_t nc = components.size();
+  for (size_t c = 0; c < nc; ++c) {
     // Get the current component.
     NodeList const& nl = components[c];
 
@@ -1068,7 +1070,7 @@ bool cmComputeLinkDepends::CheckCircularDependencies() const
 
     // no group must be evolved
     bool cycleDetected = false;
-    for (int ni : nl) {
+    for (size_t ni : nl) {
       if (this->EntryList[ni].Kind == LinkEntry::Group) {
         cycleDetected = true;
         break;
@@ -1096,8 +1098,8 @@ bool cmComputeLinkDepends::CheckCircularDependencies() const
       << this->Target->GetName()
       << "\", contains the following strongly connected component "
          "(cycle):\n";
-    std::vector<int> const& cmap = this->CCG->GetComponentMap();
-    for (int i : nl) {
+    std::vector<size_t> const& cmap = this->CCG->GetComponentMap();
+    for (size_t i : nl) {
       // Get the depender.
       LinkEntry const& depender = this->EntryList[i];
 
@@ -1107,7 +1109,7 @@ bool cmComputeLinkDepends::CheckCircularDependencies() const
       // List its dependencies that are inside the component.
       EdgeList const& el = this->EntryConstraintGraph[i];
       for (cmGraphEdge const& ni : el) {
-        int j = ni;
+        size_t j = ni;
         if (cmap[j] == c) {
           LinkEntry const& dependee = this->EntryList[j];
           e << "    depends on " << formatItem(dependee) << "\n";
@@ -1127,7 +1129,7 @@ void cmComputeLinkDepends::DisplayConstraintGraph()
 {
   // Display the graph nodes and their edges.
   std::ostringstream e;
-  for (unsigned int i = 0; i < this->EntryConstraintGraph.size(); ++i) {
+  for (size_t i = 0; i < this->EntryConstraintGraph.size(); ++i) {
     EdgeList const& nl = this->EntryConstraintGraph[i];
     e << "item " << i << " is [" << this->EntryList[i].Item << "]\n";
     e << cmWrap("  item ", nl, " must follow it", "\n") << "\n";
@@ -1141,13 +1143,14 @@ void cmComputeLinkDepends::OrderLinkEntries()
   // from every entry to compute a topological order for the
   // components.
   Graph const& cgraph = this->CCG->GetComponentGraph();
-  int n = static_cast<int>(cgraph.size());
+  size_t n = cgraph.size();
   this->ComponentVisited.resize(cgraph.size(), 0);
   this->ComponentOrder.resize(cgraph.size(), n);
   this->ComponentOrderId = n;
   // Run in reverse order so the topological order will preserve the
   // original order where there are no constraints.
-  for (int c = n - 1; c != cmComputeComponentGraph::INVALID_COMPONENT; --c) {
+  for (size_t c = n - 1; c != cmComputeComponentGraph::INVALID_COMPONENT;
+       --c) {
     this->VisitComponent(c);
   }
 
@@ -1157,7 +1160,7 @@ void cmComputeLinkDepends::OrderLinkEntries()
   }
 
   // Start with the original link line.
-  for (int originalEntry : this->OriginalEntries) {
+  for (size_t originalEntry : this->OriginalEntries) {
     this->VisitEntry(originalEntry);
   }
 
@@ -1168,7 +1171,7 @@ void cmComputeLinkDepends::OrderLinkEntries()
     // logic will update the pending components accordingly.  Since
     // the pending components are kept in topological order this will
     // not repeat one.
-    int e = *this->PendingComponents.begin()->second.Entries.begin();
+    size_t e = *this->PendingComponents.begin()->second.Entries.begin();
     this->VisitEntry(e);
   }
 }
@@ -1177,24 +1180,24 @@ void cmComputeLinkDepends::DisplayComponents()
 {
   fprintf(stderr, "The strongly connected components are:\n");
   std::vector<NodeList> const& components = this->CCG->GetComponents();
-  for (unsigned int c = 0; c < components.size(); ++c) {
-    fprintf(stderr, "Component (%u):\n", c);
+  for (size_t c = 0; c < components.size(); ++c) {
+    fprintf(stderr, "Component (%zu):\n", c);
     NodeList const& nl = components[c];
-    for (int i : nl) {
-      fprintf(stderr, "  item %d [%s]\n", i,
+    for (size_t i : nl) {
+      fprintf(stderr, "  item %zu [%s]\n", i,
               this->EntryList[i].Item.Value.c_str());
     }
     EdgeList const& ol = this->CCG->GetComponentGraphEdges(c);
     for (cmGraphEdge const& oi : ol) {
-      int i = oi;
-      fprintf(stderr, "  followed by Component (%d)\n", i);
+      size_t i = oi;
+      fprintf(stderr, "  followed by Component (%zu)\n", i);
     }
-    fprintf(stderr, "  topo order index %d\n", this->ComponentOrder[c]);
+    fprintf(stderr, "  topo order index %zu\n", this->ComponentOrder[c]);
   }
   fprintf(stderr, "\n");
 }
 
-void cmComputeLinkDepends::VisitComponent(unsigned int c)
+void cmComputeLinkDepends::VisitComponent(size_t c)
 {
   // Check if the node has already been visited.
   if (this->ComponentVisited[c]) {
@@ -1216,14 +1219,14 @@ void cmComputeLinkDepends::VisitComponent(unsigned int c)
   this->ComponentOrder[c] = --this->ComponentOrderId;
 }
 
-void cmComputeLinkDepends::VisitEntry(int index)
+void cmComputeLinkDepends::VisitEntry(size_t index)
 {
   // Include this entry on the link line.
   this->FinalLinkOrder.push_back(index);
 
   // This entry has now been seen.  Update its component.
   bool completed = false;
-  int component = this->CCG->GetComponentMap()[index];
+  size_t component = this->CCG->GetComponentMap()[index];
   auto mi = this->PendingComponents.find(this->ComponentOrder[component]);
   if (mi != this->PendingComponents.end()) {
     // The entry is in an already pending component.
@@ -1274,7 +1277,7 @@ void cmComputeLinkDepends::VisitEntry(int index)
 }
 
 cmComputeLinkDepends::PendingComponent&
-cmComputeLinkDepends::MakePendingComponent(unsigned int component)
+cmComputeLinkDepends::MakePendingComponent(size_t component)
 {
   // Create an entry (in topological order) for the component.
   PendingComponent& pc =
@@ -1307,10 +1310,10 @@ cmComputeLinkDepends::MakePendingComponent(unsigned int component)
   return pc;
 }
 
-int cmComputeLinkDepends::ComputeComponentCount(NodeList const& nl)
+size_t cmComputeLinkDepends::ComputeComponentCount(NodeList const& nl)
 {
-  unsigned int count = 2;
-  for (int ni : nl) {
+  size_t count = 2;
+  for (size_t ni : nl) {
     if (cmGeneratorTarget const* target = this->EntryList[ni].Target) {
       if (cmLinkInterface const* iface =
             target->GetLinkInterface(this->Config, this->Target)) {

+ 30 - 29
Source/cmComputeLinkDepends.h

@@ -4,6 +4,7 @@
 
 #include "cmConfigure.h" // IWYU pragma: keep
 
+#include <cstddef>
 #include <map>
 #include <memory>
 #include <queue>
@@ -12,12 +13,12 @@
 #include <utility>
 #include <vector>
 
+#include "cmComputeComponentGraph.h"
 #include "cmGraphAdjacencyList.h"
 #include "cmLinkItem.h"
 #include "cmListFileCache.h"
 #include "cmTargetLinkLibraryType.h"
 
-class cmComputeComponentGraph;
 class cmGeneratorTarget;
 class cmGlobalGenerator;
 class cmMakefile;
@@ -91,31 +92,31 @@ private:
   std::string const& GetCurrentFeature(
     std::string const& item, std::string const& defaultFeature) const;
 
-  std::pair<std::map<cmLinkItem, int>::iterator, bool> AllocateLinkEntry(
+  std::pair<std::map<cmLinkItem, size_t>::iterator, bool> AllocateLinkEntry(
     cmLinkItem const& item);
-  std::pair<int, bool> AddLinkEntry(
+  std::pair<size_t, bool> AddLinkEntry(
     cmLinkItem const& item,
-    int groupIndex = cmComputeComponentGraph::INVALID_COMPONENT);
+    size_t groupIndex = cmComputeComponentGraph::INVALID_COMPONENT);
   void AddLinkObject(cmLinkItem const& item);
-  void AddVarLinkEntries(int depender_index, const char* value);
+  void AddVarLinkEntries(size_t depender_index, const char* value);
   void AddDirectLinkEntries();
   template <typename T>
-  void AddLinkEntries(int depender_index, std::vector<T> const& libs);
+  void AddLinkEntries(size_t depender_index, std::vector<T> const& libs);
   void AddLinkObjects(std::vector<cmLinkItem> const& objs);
-  cmLinkItem ResolveLinkItem(int depender_index, const std::string& name);
+  cmLinkItem ResolveLinkItem(size_t depender_index, const std::string& name);
 
   // One entry for each unique item.
   std::vector<LinkEntry> EntryList;
-  std::map<cmLinkItem, int> LinkEntryIndex;
+  std::map<cmLinkItem, size_t> LinkEntryIndex;
 
   // map storing, for each group, the list of items
-  std::map<int, std::vector<int>> GroupItems;
+  std::map<size_t, std::vector<size_t>> GroupItems;
 
   // BFS of initial dependencies.
   struct BFSEntry
   {
-    int Index;
-    int GroupIndex;
+    size_t Index;
+    size_t GroupIndex;
     const char* LibDepends;
   };
   std::queue<BFSEntry> BFSQueue;
@@ -127,18 +128,18 @@ private:
   struct SharedDepEntry
   {
     cmLinkItem Item;
-    int DependerIndex;
+    size_t DependerIndex;
   };
   std::queue<SharedDepEntry> SharedDepQueue;
-  std::set<int> SharedDepFollowed;
-  void FollowSharedDeps(int depender_index, cmLinkInterface const* iface,
+  std::set<size_t> SharedDepFollowed;
+  void FollowSharedDeps(size_t depender_index, cmLinkInterface const* iface,
                         bool follow_interface = false);
-  void QueueSharedDependencies(int depender_index,
+  void QueueSharedDependencies(size_t depender_index,
                                std::vector<cmLinkItem> const& deps);
   void HandleSharedDependency(SharedDepEntry const& dep);
 
   // Dependency inferral for each link item.
-  struct DependSet : public std::set<int>
+  struct DependSet : public std::set<size_t>
   {
   };
   struct DependSetList : public std::vector<DependSet>
@@ -163,41 +164,41 @@ private:
   // Ordering algorithm.
   void OrderLinkEntries();
   std::vector<char> ComponentVisited;
-  std::vector<int> ComponentOrder;
+  std::vector<size_t> ComponentOrder;
 
   struct PendingComponent
   {
     // The real component id.  Needed because the map is indexed by
     // component topological index.
-    int Id;
+    size_t Id;
 
     // The number of times the component needs to be seen.  This is
     // always 1 for trivial components and is initially 2 for
     // non-trivial components.
-    int Count;
+    size_t Count;
 
     // The entries yet to be seen to complete the component.
-    std::set<int> Entries;
+    std::set<size_t> Entries;
   };
-  std::map<int, PendingComponent> PendingComponents;
+  std::map<size_t, PendingComponent> PendingComponents;
   std::unique_ptr<cmComputeComponentGraph> CCG;
-  std::vector<int> FinalLinkOrder;
+  std::vector<size_t> FinalLinkOrder;
   void DisplayComponents();
-  void VisitComponent(unsigned int c);
-  void VisitEntry(int index);
-  PendingComponent& MakePendingComponent(unsigned int component);
-  int ComputeComponentCount(NodeList const& nl);
+  void VisitComponent(size_t c);
+  void VisitEntry(size_t index);
+  PendingComponent& MakePendingComponent(size_t component);
+  size_t ComputeComponentCount(NodeList const& nl);
   void DisplayFinalEntries();
 
   // Record of the original link line.
-  std::vector<int> OriginalEntries;
+  std::vector<size_t> OriginalEntries;
   std::set<cmGeneratorTarget const*> OldWrongConfigItems;
   void CheckWrongConfigItem(cmLinkItem const& item);
 
   // Record of explicitly linked object files.
-  std::vector<int> ObjectEntries;
+  std::vector<size_t> ObjectEntries;
 
-  int ComponentOrderId;
+  size_t ComponentOrderId;
   cmTargetLinkLibraryType LinkType;
   bool HasConfig;
   bool DebugMode;

+ 57 - 56
Source/cmComputeTargetDepends.cxx

@@ -3,6 +3,7 @@
 #include "cmComputeTargetDepends.h"
 
 #include <cassert>
+#include <cstddef>
 #include <cstdio>
 #include <memory>
 #include <sstream>
@@ -159,7 +160,7 @@ void cmComputeTargetDepends::GetTargetDirectDepends(cmGeneratorTarget const* t,
   // this point.
   auto tii = this->TargetIndex.find(t);
   assert(tii != this->TargetIndex.end());
-  int i = tii->second;
+  size_t i = tii->second;
 
   // Get its final dependencies.
   EdgeList const& nl = this->FinalGraph[i];
@@ -178,7 +179,7 @@ void cmComputeTargetDepends::CollectTargets()
   auto const& lgens = this->GlobalGenerator->GetLocalGenerators();
   for (const auto& lgen : lgens) {
     for (const auto& ti : lgen->GetGeneratorTargets()) {
-      int index = static_cast<int>(this->Targets.size());
+      size_t index = this->Targets.size();
       this->TargetIndex[ti.get()] = index;
       this->Targets.push_back(ti.get());
     }
@@ -191,12 +192,12 @@ void cmComputeTargetDepends::CollectDepends()
   this->InitialGraph.resize(this->Targets.size());
 
   // Compute each dependency list.
-  for (unsigned int i = 0; i < this->Targets.size(); ++i) {
+  for (size_t i = 0; i < this->Targets.size(); ++i) {
     this->CollectTargetDepends(i);
   }
 }
 
-void cmComputeTargetDepends::CollectTargetDepends(int depender_index)
+void cmComputeTargetDepends::CollectTargetDepends(size_t depender_index)
 {
   // Get the depender.
   cmGeneratorTarget const* depender = this->Targets[depender_index];
@@ -261,7 +262,7 @@ void cmComputeTargetDepends::CollectTargetDepends(int depender_index)
 }
 
 void cmComputeTargetDepends::AddInterfaceDepends(
-  int depender_index, const cmGeneratorTarget* dependee,
+  size_t depender_index, const cmGeneratorTarget* dependee,
   cmListFileBacktrace const& dependee_backtrace, const std::string& config,
   std::set<cmLinkItem>& emitted)
 {
@@ -290,7 +291,7 @@ void cmComputeTargetDepends::AddInterfaceDepends(
 }
 
 void cmComputeTargetDepends::AddInterfaceDepends(
-  int depender_index, cmLinkItem const& dependee_name,
+  size_t depender_index, cmLinkItem const& dependee_name,
   const std::string& config, std::set<cmLinkItem>& emitted)
 {
   cmGeneratorTarget const* depender = this->Targets[depender_index];
@@ -312,7 +313,7 @@ void cmComputeTargetDepends::AddInterfaceDepends(
   }
 }
 
-void cmComputeTargetDepends::AddObjectDepends(int depender_index,
+void cmComputeTargetDepends::AddObjectDepends(size_t depender_index,
                                               cmSourceFile const* o,
                                               std::set<cmLinkItem>& emitted)
 {
@@ -340,7 +341,7 @@ void cmComputeTargetDepends::AddObjectDepends(int depender_index,
   }
 }
 
-void cmComputeTargetDepends::AddTargetDepend(int depender_index,
+void cmComputeTargetDepends::AddTargetDepend(size_t depender_index,
                                              cmLinkItem const& dependee_name,
                                              bool linking, bool cross)
 {
@@ -394,7 +395,7 @@ void cmComputeTargetDepends::AddTargetDepend(int depender_index,
 }
 
 void cmComputeTargetDepends::AddTargetDepend(
-  int depender_index, cmGeneratorTarget const* dependee,
+  size_t depender_index, cmGeneratorTarget const* dependee,
   cmListFileBacktrace const& dependee_backtrace, bool linking, bool cross)
 {
   if (!dependee->IsInBuildSystem()) {
@@ -412,7 +413,7 @@ void cmComputeTargetDepends::AddTargetDepend(
     // this point.
     auto tii = this->TargetIndex.find(dependee);
     assert(tii != this->TargetIndex.end());
-    int dependee_index = tii->second;
+    size_t dependee_index = tii->second;
 
     // Add this entry to the dependency graph.
     this->InitialGraph[depender_index].emplace_back(dependee_index, !linking,
@@ -425,15 +426,15 @@ void cmComputeTargetDepends::CollectSideEffects()
   this->SideEffects.resize(0);
   this->SideEffects.resize(this->InitialGraph.size());
 
-  int n = static_cast<int>(this->InitialGraph.size());
-  std::set<int> visited;
-  for (int i = 0; i < n; ++i) {
+  size_t n = this->InitialGraph.size();
+  std::set<size_t> visited;
+  for (size_t i = 0; i < n; ++i) {
     this->CollectSideEffectsForTarget(visited, i);
   }
 }
 
 void cmComputeTargetDepends::CollectSideEffectsForTarget(
-  std::set<int>& visited, int depender_index)
+  std::set<size_t>& visited, size_t depender_index)
 {
   if (!visited.count(depender_index)) {
     auto& se = this->SideEffects[depender_index];
@@ -461,8 +462,8 @@ void cmComputeTargetDepends::ComputeIntermediateGraph()
   this->IntermediateGraph.resize(0);
   this->IntermediateGraph.resize(this->InitialGraph.size());
 
-  int n = static_cast<int>(this->InitialGraph.size());
-  for (int i = 0; i < n; ++i) {
+  size_t n = this->InitialGraph.size();
+  for (size_t i = 0; i < n; ++i) {
     auto const& initialEdges = this->InitialGraph[i];
     auto& intermediateEdges = this->IntermediateGraph[i];
     cmGeneratorTarget const* gt = this->Targets[i];
@@ -488,7 +489,7 @@ void cmComputeTargetDepends::OptimizeLinkDependencies(
   cmGeneratorTarget const* gt, cmGraphEdgeList& outputEdges,
   cmGraphEdgeList const& inputEdges)
 {
-  std::set<int> emitted;
+  std::set<size_t> emitted;
   for (auto const& edge : inputEdges) {
     if (edge.IsStrong()) {
       // Preserve strong edges
@@ -529,16 +530,16 @@ void cmComputeTargetDepends::DisplayGraph(Graph const& graph,
                                           const std::string& name)
 {
   fprintf(stderr, "The %s target dependency graph is:\n", name.c_str());
-  int n = static_cast<int>(graph.size());
-  for (int depender_index = 0; depender_index < n; ++depender_index) {
+  size_t n = graph.size();
+  for (size_t depender_index = 0; depender_index < n; ++depender_index) {
     EdgeList const& nl = graph[depender_index];
     cmGeneratorTarget const* depender = this->Targets[depender_index];
-    fprintf(stderr, "target %d is [%s]\n", depender_index,
+    fprintf(stderr, "target %zu is [%s]\n", depender_index,
             depender->GetName().c_str());
     for (cmGraphEdge const& ni : nl) {
-      int dependee_index = ni;
+      size_t dependee_index = ni;
       cmGeneratorTarget const* dependee = this->Targets[dependee_index];
-      fprintf(stderr, "  depends on target %d [%s] (%s)\n", dependee_index,
+      fprintf(stderr, "  depends on target %zu [%s] (%s)\n", dependee_index,
               dependee->GetName().c_str(), ni.IsStrong() ? "strong" : "weak");
     }
   }
@@ -548,16 +549,16 @@ void cmComputeTargetDepends::DisplayGraph(Graph const& graph,
 void cmComputeTargetDepends::DisplaySideEffects()
 {
   fprintf(stderr, "The side effects are:\n");
-  int n = static_cast<int>(this->SideEffects.size());
-  for (int depender_index = 0; depender_index < n; ++depender_index) {
+  size_t n = this->SideEffects.size();
+  for (size_t depender_index = 0; depender_index < n; ++depender_index) {
     cmGeneratorTarget const* depender = this->Targets[depender_index];
-    fprintf(stderr, "target %d is [%s]\n", depender_index,
+    fprintf(stderr, "target %zu is [%s]\n", depender_index,
             depender->GetName().c_str());
     if (!this->SideEffects[depender_index].CustomCommandSideEffects.empty()) {
       fprintf(stderr, "  custom commands\n");
       for (auto const* gt :
            this->SideEffects[depender_index].CustomCommandSideEffects) {
-        fprintf(stderr, "    from target %d [%s]\n", this->TargetIndex[gt],
+        fprintf(stderr, "    from target %zu [%s]\n", this->TargetIndex[gt],
                 gt->GetName().c_str());
       }
     }
@@ -565,7 +566,7 @@ void cmComputeTargetDepends::DisplaySideEffects()
          this->SideEffects[depender_index].LanguageSideEffects) {
       fprintf(stderr, "  language %s\n", it.first.c_str());
       for (auto const* gt : it.second) {
-        fprintf(stderr, "    from target %d [%s]\n", this->TargetIndex[gt],
+        fprintf(stderr, "    from target %zu [%s]\n", this->TargetIndex[gt],
                 gt->GetName().c_str());
       }
     }
@@ -579,12 +580,12 @@ void cmComputeTargetDepends::DisplayComponents(
   fprintf(stderr, "The strongly connected components for the %s graph are:\n",
           name.c_str());
   std::vector<NodeList> const& components = ccg.GetComponents();
-  int n = static_cast<int>(components.size());
-  for (int c = 0; c < n; ++c) {
+  size_t n = components.size();
+  for (size_t c = 0; c < n; ++c) {
     NodeList const& nl = components[c];
-    fprintf(stderr, "Component (%d):\n", c);
-    for (int i : nl) {
-      fprintf(stderr, "  contains target %d [%s]\n", i,
+    fprintf(stderr, "Component (%zu):\n", c);
+    for (size_t i : nl) {
+      fprintf(stderr, "  contains target %zu [%s]\n", i,
               this->Targets[i]->GetName().c_str());
     }
   }
@@ -597,8 +598,8 @@ bool cmComputeTargetDepends::CheckComponents(
   // All non-trivial components should consist only of static
   // libraries.
   std::vector<NodeList> const& components = ccg.GetComponents();
-  int nc = static_cast<int>(components.size());
-  for (int c = 0; c < nc; ++c) {
+  size_t nc = components.size();
+  for (size_t c = 0; c < nc; ++c) {
     // Get the current component.
     NodeList const& nl = components[c];
 
@@ -614,7 +615,7 @@ bool cmComputeTargetDepends::CheckComponents(
     }
 
     // Make sure the component is all STATIC_LIBRARY targets.
-    for (int ni : nl) {
+    for (size_t ni : nl) {
       if (this->Targets[ni]->GetType() != cmStateEnums::STATIC_LIBRARY) {
         this->ComplainAboutBadComponent(ccg, c);
         return false;
@@ -625,16 +626,16 @@ bool cmComputeTargetDepends::CheckComponents(
 }
 
 void cmComputeTargetDepends::ComplainAboutBadComponent(
-  cmComputeComponentGraph const& ccg, int c, bool strong)
+  cmComputeComponentGraph const& ccg, size_t c, bool strong)
 {
   // Construct the error message.
   std::ostringstream e;
   e << "The inter-target dependency graph contains the following "
     << "strongly connected component (cycle):\n";
   std::vector<NodeList> const& components = ccg.GetComponents();
-  std::vector<int> const& cmap = ccg.GetComponentMap();
+  std::vector<size_t> const& cmap = ccg.GetComponentMap();
   NodeList const& cl = components[c];
-  for (int i : cl) {
+  for (size_t i : cl) {
     // Get the depender.
     cmGeneratorTarget const* depender = this->Targets[i];
 
@@ -645,7 +646,7 @@ void cmComputeTargetDepends::ComplainAboutBadComponent(
     // List its dependencies that are inside the component.
     EdgeList const& nl = this->InitialGraph[i];
     for (cmGraphEdge const& ni : nl) {
-      int j = ni;
+      size_t j = ni;
       if (cmap[j] == c) {
         cmGeneratorTarget const* dependee = this->Targets[j];
         e << "    depends on \"" << dependee->GetName() << "\""
@@ -669,10 +670,10 @@ void cmComputeTargetDepends::ComplainAboutBadComponent(
   cmSystemTools::Error(e.str());
 }
 
-bool cmComputeTargetDepends::IntraComponent(std::vector<int> const& cmap,
-                                            int c, int i, int* head,
-                                            std::set<int>& emitted,
-                                            std::set<int>& visited)
+bool cmComputeTargetDepends::IntraComponent(std::vector<size_t> const& cmap,
+                                            size_t c, size_t i, size_t* head,
+                                            std::set<size_t>& emitted,
+                                            std::set<size_t>& visited)
 {
   if (!visited.insert(i).second) {
     // Cycle in utility depends!
@@ -682,7 +683,7 @@ bool cmComputeTargetDepends::IntraComponent(std::vector<int> const& cmap,
     // Honor strong intra-component edges in the final order.
     EdgeList const& el = this->InitialGraph[i];
     for (cmGraphEdge const& edge : el) {
-      int j = edge;
+      size_t j = edge;
       if (cmap[j] == c && edge.IsStrong()) {
         this->FinalGraph[i].emplace_back(j, true, edge.IsCross(),
                                          edge.GetBacktrace());
@@ -716,16 +717,16 @@ bool cmComputeTargetDepends::ComputeFinalDepends(
   this->FinalGraph.resize(this->InitialGraph.size());
 
   // Choose intra-component edges to linearize dependencies.
-  std::vector<int> const& cmap = ccg.GetComponentMap();
+  std::vector<size_t> const& cmap = ccg.GetComponentMap();
   this->ComponentHead.resize(components.size());
   this->ComponentTail.resize(components.size());
-  int nc = static_cast<int>(components.size());
-  for (int c = 0; c < nc; ++c) {
-    int head = cmComputeComponentGraph::INVALID_COMPONENT;
-    std::set<int> emitted;
+  size_t nc = components.size();
+  for (size_t c = 0; c < nc; ++c) {
+    size_t head = cmComputeComponentGraph::INVALID_COMPONENT;
+    std::set<size_t> emitted;
     NodeList const& nl = components[c];
-    for (int ni : cmReverseRange(nl)) {
-      std::set<int> visited;
+    for (size_t ni : cmReverseRange(nl)) {
+      std::set<size_t> visited;
       if (!this->IntraComponent(cmap, c, ni, &head, emitted, visited)) {
         // Cycle in add_dependencies within component!
         this->ComplainAboutBadComponent(ccg, c, true);
@@ -736,14 +737,14 @@ bool cmComputeTargetDepends::ComputeFinalDepends(
   }
 
   // Convert inter-component edges to connect component tails to heads.
-  int n = static_cast<int>(cgraph.size());
-  for (int depender_component = 0; depender_component < n;
+  size_t n = cgraph.size();
+  for (size_t depender_component = 0; depender_component < n;
        ++depender_component) {
-    int depender_component_tail = this->ComponentTail[depender_component];
+    size_t depender_component_tail = this->ComponentTail[depender_component];
     EdgeList const& nl = cgraph[depender_component];
     for (cmGraphEdge const& ni : nl) {
-      int dependee_component = ni;
-      int dependee_component_head = this->ComponentHead[dependee_component];
+      size_t dependee_component = ni;
+      size_t dependee_component_head = this->ComponentHead[dependee_component];
       this->FinalGraph[depender_component_tail].emplace_back(
         dependee_component_head, ni.IsStrong(), ni.IsCross(),
         ni.GetBacktrace());

+ 18 - 13
Source/cmComputeTargetDepends.h

@@ -4,6 +4,7 @@
 
 #include "cmConfigure.h" // IWYU pragma: keep
 
+#include <cstddef>
 #include <map>
 #include <set>
 #include <string>
@@ -51,28 +52,31 @@ private:
 
   void CollectTargets();
   void CollectDepends();
-  void CollectTargetDepends(int depender_index);
-  void AddTargetDepend(int depender_index, cmLinkItem const& dependee_name,
+  void CollectTargetDepends(size_t depender_index);
+  void AddTargetDepend(size_t depender_index, cmLinkItem const& dependee_name,
                        bool linking, bool cross);
-  void AddTargetDepend(int depender_index, cmGeneratorTarget const* dependee,
+  void AddTargetDepend(size_t depender_index,
+                       cmGeneratorTarget const* dependee,
                        cmListFileBacktrace const& dependee_backtrace,
                        bool linking, bool cross);
   void CollectSideEffects();
-  void CollectSideEffectsForTarget(std::set<int>& visited, int depender_index);
+  void CollectSideEffectsForTarget(std::set<size_t>& visited,
+                                   size_t depender_index);
   void ComputeIntermediateGraph();
   void OptimizeLinkDependencies(cmGeneratorTarget const* gt,
                                 cmGraphEdgeList& outputEdges,
                                 cmGraphEdgeList const& inputEdges);
   bool ComputeFinalDepends(cmComputeComponentGraph const& ccg);
-  void AddInterfaceDepends(int depender_index, cmLinkItem const& dependee_name,
+  void AddInterfaceDepends(size_t depender_index,
+                           cmLinkItem const& dependee_name,
                            const std::string& config,
                            std::set<cmLinkItem>& emitted);
-  void AddInterfaceDepends(int depender_index,
+  void AddInterfaceDepends(size_t depender_index,
                            cmGeneratorTarget const* dependee,
                            cmListFileBacktrace const& dependee_backtrace,
                            const std::string& config,
                            std::set<cmLinkItem>& emitted);
-  void AddObjectDepends(int depender_index, cmSourceFile const* o,
+  void AddObjectDepends(size_t depender_index, cmSourceFile const* o,
                         std::set<cmLinkItem>& emitted);
   cmGlobalGenerator* GlobalGenerator;
   bool DebugMode;
@@ -80,7 +84,7 @@ private:
 
   // Collect all targets.
   std::vector<cmGeneratorTarget const*> Targets;
-  std::map<cmGeneratorTarget const*, int> TargetIndex;
+  std::map<cmGeneratorTarget const*, size_t> TargetIndex;
 
   // Represent the target dependency graph.  The entry at each
   // top-level index corresponds to a depender whose dependencies are
@@ -99,11 +103,12 @@ private:
   void DisplayComponents(cmComputeComponentGraph const& ccg,
                          const std::string& name);
   bool CheckComponents(cmComputeComponentGraph const& ccg);
-  void ComplainAboutBadComponent(cmComputeComponentGraph const& ccg, int c,
+  void ComplainAboutBadComponent(cmComputeComponentGraph const& ccg, size_t c,
                                  bool strong = false);
 
-  std::vector<int> ComponentHead;
-  std::vector<int> ComponentTail;
-  bool IntraComponent(std::vector<int> const& cmap, int c, int i, int* head,
-                      std::set<int>& emitted, std::set<int>& visited);
+  std::vector<size_t> ComponentHead;
+  std::vector<size_t> ComponentTail;
+  bool IntraComponent(std::vector<size_t> const& cmap, size_t c, size_t i,
+                      size_t* head, std::set<size_t>& emitted,
+                      std::set<size_t>& visited);
 };

+ 5 - 4
Source/cmGraphAdjacencyList.h

@@ -4,6 +4,7 @@
 
 #include "cmConfigure.h" // IWYU pragma: keep
 
+#include <cstddef>
 #include <utility>
 #include <vector>
 
@@ -17,14 +18,14 @@
 class cmGraphEdge
 {
 public:
-  cmGraphEdge(int n, bool s, bool c, cmListFileBacktrace bt)
+  cmGraphEdge(size_t n, bool s, bool c, cmListFileBacktrace bt)
     : Dest(n)
     , Strong(s)
     , Cross(c)
     , Backtrace(std::move(bt))
   {
   }
-  operator int() const { return this->Dest; }
+  operator size_t() const { return this->Dest; }
 
   bool IsStrong() const { return this->Strong; }
 
@@ -33,7 +34,7 @@ public:
   cmListFileBacktrace const& GetBacktrace() const { return this->Backtrace; }
 
 private:
-  int Dest;
+  size_t Dest;
   bool Strong;
   bool Cross;
   cmListFileBacktrace Backtrace;
@@ -41,7 +42,7 @@ private:
 struct cmGraphEdgeList : public std::vector<cmGraphEdge>
 {
 };
-struct cmGraphNodeList : public std::vector<int>
+struct cmGraphNodeList : public std::vector<size_t>
 {
 };
 struct cmGraphAdjacencyList : public std::vector<cmGraphEdgeList>