ProjectExplorer: Fix compiler executable duplication

... in findCompilerCandidates().
While at it, also improve the readability, as I had surprisingly big
difficulties understanding the code:
  - Try to use more self-documenting constructs.
  - Add more comments.

Change-Id: I5ee9c795b915fb80f5912c98170348c3e5d403ad
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Christian Kandeler
2024-12-06 12:47:49 +01:00
parent f14066428e
commit 4212350c10

View File

@@ -1390,7 +1390,7 @@ static FilePaths findCompilerCandidates(OsType os,
{ {
// We expect the following patterns: // We expect the following patterns:
// compilerName "clang", "gcc" // compilerName "clang", "gcc"
// compilerName + "-[1-9]*" "clang-8", "gcc-5" // compilerName + "-[1-9]*" "clang-18", "gcc-5"
// "*-" + compilerName "avr-gcc", "avr32-gcc" // "*-" + compilerName "avr-gcc", "avr32-gcc"
// "arm-none-eabi-gcc" // "arm-none-eabi-gcc"
// "x86_64-pc-linux-gnu-gcc" // "x86_64-pc-linux-gnu-gcc"
@@ -1400,39 +1400,45 @@ static FilePaths findCompilerCandidates(OsType os,
// but not "c89-gcc" or "c99-gcc" // but not "c89-gcc" or "c99-gcc"
FilePaths compilerPaths; FilePaths compilerPaths;
const int cl = compilerName.size(); const int nameLen = compilerName.size();
for (const FilePath &executable : executables) { for (const FilePath &executable : executables) {
QStringView fileName = executable.fileNameView(); QStringView fileName = executable.fileNameView();
if (os == OsTypeWindows && fileName.endsWith(u".exe", Qt::CaseInsensitive)) if (os == OsTypeWindows && fileName.endsWith(u".exe", Qt::CaseInsensitive))
fileName.chop(4); fileName.chop(4);
// Do not `continue`, proceed to detect further variants // Exact file (base) name match with no prefix or suffix, e.g. "/usr/bin/gcc" for "gcc".
if (fileName == compilerName) if (fileName == compilerName) {
compilerPaths << executable; compilerPaths << executable;
continue;
}
// Not an exact match and we are only interested in those.
if (!detectVariants) if (!detectVariants)
continue; continue;
// These are always links intended for more generic tools.
if (fileName == u"c89-gcc" || fileName == u"c99-gcc") if (fileName == u"c89-gcc" || fileName == u"c99-gcc")
continue; continue;
int pos = fileName.indexOf(compilerName); const int nameOffset = fileName.indexOf(compilerName);
if (pos == -1)
// No match at all.
if (nameOffset == -1)
continue; continue;
// if not at the beginning, it must be preceded by a hyphen. // If there is a prefix, it must end with a hyphen.
if (pos > 0 && fileName.at(pos - 1) != '-') if (nameOffset > 0 && fileName.at(nameOffset - 1) != '-')
continue; continue;
// if not at the end, it must by followed by a hyphen and a digit between 1 and 9 // If there is a suffix, it must start with a hyphen followed by a digit between 1 and 9.
pos += cl; const QStringView suffix = fileName.sliced(nameOffset + nameLen);
if (pos != fileName.size()) { switch (suffix.size()) {
if (pos + 1 >= fileName.size()) case 0: break;
case 1: continue;
default:
if (suffix.first() != '-')
continue; continue;
if (fileName.at(pos) != '-') if (const QChar c = suffix.at(1); c < '1' || c > '9')
continue;
const QChar c = fileName.at(pos + 1);
if (c < '1' || c > '9')
continue; continue;
} }
@@ -1442,7 +1448,6 @@ static FilePaths findCompilerCandidates(OsType os,
return compilerPaths; return compilerPaths;
} }
Toolchains GccToolchainFactory::autoDetect(const ToolchainDetector &detector) const Toolchains GccToolchainFactory::autoDetect(const ToolchainDetector &detector) const
{ {
QTC_ASSERT(detector.device, return {}); QTC_ASSERT(detector.device, return {});