cmStateDirectory: Clarify relative path top selection logic

Re-implement the same algorithm using direct iteration without
collecting a vector first.
This commit is contained in:
Brad King
2021-05-14 14:58:45 -04:00
parent 2d9109df7c
commit ea9b1d36b8
+18 -38
View File
@@ -26,57 +26,37 @@ static std::string const kSUBDIRECTORIES = "SUBDIRECTORIES";
void cmStateDirectory::ComputeRelativePathTopSource() void cmStateDirectory::ComputeRelativePathTopSource()
{ {
cmStateSnapshot snapshot = this->Snapshot_;
std::vector<cmStateSnapshot> snapshots;
snapshots.push_back(snapshot);
while (true) {
snapshot = snapshot.GetBuildsystemDirectoryParent();
if (snapshot.IsValid()) {
snapshots.push_back(snapshot);
} else {
break;
}
}
std::string result = snapshots.front().GetDirectory().GetCurrentSource();
// Walk up the buildsystem directory tree to find the highest source // Walk up the buildsystem directory tree to find the highest source
// directory that contains the current source directory. // directory that contains the current source directory.
for (cmStateSnapshot const& snp : cmMakeRange(snapshots).advance(1)) { cmStateSnapshot snapshot = this->Snapshot_;
std::string currentSource = snp.GetDirectory().GetCurrentSource(); for (cmStateSnapshot parent = snapshot.GetBuildsystemDirectoryParent();
if (cmSystemTools::IsSubDirectory(result, currentSource)) { parent.IsValid(); parent = parent.GetBuildsystemDirectoryParent()) {
result = currentSource; if (cmSystemTools::IsSubDirectory(
snapshot.GetDirectory().GetCurrentSource(),
parent.GetDirectory().GetCurrentSource())) {
snapshot = parent;
} }
} }
this->DirectoryState->RelativePathTopSource = result; this->DirectoryState->RelativePathTopSource =
snapshot.GetDirectory().GetCurrentSource();
} }
void cmStateDirectory::ComputeRelativePathTopBinary() void cmStateDirectory::ComputeRelativePathTopBinary()
{ {
cmStateSnapshot snapshot = this->Snapshot_;
std::vector<cmStateSnapshot> snapshots;
snapshots.push_back(snapshot);
while (true) {
snapshot = snapshot.GetBuildsystemDirectoryParent();
if (snapshot.IsValid()) {
snapshots.push_back(snapshot);
} else {
break;
}
}
std::string result = snapshots.front().GetDirectory().GetCurrentBinary();
// Walk up the buildsystem directory tree to find the highest binary // Walk up the buildsystem directory tree to find the highest binary
// directory that contains the current binary directory. // directory that contains the current binary directory.
for (cmStateSnapshot const& snp : cmMakeRange(snapshots).advance(1)) { cmStateSnapshot snapshot = this->Snapshot_;
std::string currentBinary = snp.GetDirectory().GetCurrentBinary(); for (cmStateSnapshot parent = snapshot.GetBuildsystemDirectoryParent();
if (cmSystemTools::IsSubDirectory(result, currentBinary)) { parent.IsValid(); parent = parent.GetBuildsystemDirectoryParent()) {
result = currentBinary; if (cmSystemTools::IsSubDirectory(
snapshot.GetDirectory().GetCurrentBinary(),
parent.GetDirectory().GetCurrentBinary())) {
snapshot = parent;
} }
} }
this->DirectoryState->RelativePathTopBinary = result; this->DirectoryState->RelativePathTopBinary =
snapshot.GetDirectory().GetCurrentBinary();
} }
std::string const& cmStateDirectory::GetCurrentSource() const std::string const& cmStateDirectory::GetCurrentSource() const