Skip to content

Commit 05dcfa4

Browse files
authored
[clang] [Gnu] Improve GCCVersion parsing to match versions such as "10-win32" (llvm#69079)
In earlier GCC versions, the Debian/Ubuntu provided mingw toolchains were packaged in /usr/lib/gcc/<triple> with version strings such as "5.3-win32", which were matched and found since 6afcd64. However in recent versions, they have stopped including the minor version number and only have version strings such as "10-win32" and "10-posix". Generalize the parsing code to tolerate the patch suffix to be present on a version number with only a major number. Refactor the string parsing code to highlight the overall structure of the parsing. This implementation should yield the same result as before, except for when there's only one segment and it has trailing, non-number contents. This allows Clang to find the GCC libraries and headers in Debian/Ubuntu provided MinGW cross compilers.
1 parent 18775a4 commit 05dcfa4

File tree

2 files changed

+56
-28
lines changed

2 files changed

+56
-28
lines changed

clang/lib/Driver/ToolChains/Gnu.cpp

Lines changed: 55 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,45 +2007,72 @@ Generic_GCC::GCCVersion Generic_GCC::GCCVersion::Parse(StringRef VersionText) {
20072007
std::pair<StringRef, StringRef> First = VersionText.split('.');
20082008
std::pair<StringRef, StringRef> Second = First.second.split('.');
20092009

2010-
GCCVersion GoodVersion = {VersionText.str(), -1, -1, -1, "", "", ""};
2011-
if (First.first.getAsInteger(10, GoodVersion.Major) || GoodVersion.Major < 0)
2012-
return BadVersion;
2013-
GoodVersion.MajorStr = First.first.str();
2014-
if (First.second.empty())
2015-
return GoodVersion;
2010+
StringRef MajorStr = First.first;
20162011
StringRef MinorStr = Second.first;
2017-
if (Second.second.empty()) {
2018-
if (size_t EndNumber = MinorStr.find_first_not_of("0123456789")) {
2019-
GoodVersion.PatchSuffix = std::string(MinorStr.substr(EndNumber));
2020-
MinorStr = MinorStr.slice(0, EndNumber);
2021-
}
2022-
}
2023-
if (MinorStr.getAsInteger(10, GoodVersion.Minor) || GoodVersion.Minor < 0)
2024-
return BadVersion;
2025-
GoodVersion.MinorStr = MinorStr.str();
2012+
StringRef PatchStr = Second.second;
20262013

2027-
// First look for a number prefix and parse that if present. Otherwise just
2028-
// stash the entire patch string in the suffix, and leave the number
2029-
// unspecified. This covers versions strings such as:
2030-
// 5 (handled above)
2014+
GCCVersion GoodVersion = {VersionText.str(), -1, -1, -1, "", "", ""};
2015+
2016+
// Parse version number strings such as:
2017+
// 5
20312018
// 4.4
20322019
// 4.4-patched
20332020
// 4.4.0
20342021
// 4.4.x
20352022
// 4.4.2-rc4
20362023
// 4.4.x-patched
2037-
// And retains any patch number it finds.
2038-
StringRef PatchText = Second.second;
2039-
if (!PatchText.empty()) {
2040-
if (size_t EndNumber = PatchText.find_first_not_of("0123456789")) {
2041-
// Try to parse the number and any suffix.
2042-
if (PatchText.slice(0, EndNumber).getAsInteger(10, GoodVersion.Patch) ||
2043-
GoodVersion.Patch < 0)
2044-
return BadVersion;
2045-
GoodVersion.PatchSuffix = std::string(PatchText.substr(EndNumber));
2024+
// 10-win32
2025+
// Split on '.', handle 1, 2 or 3 such segments. Each segment must contain
2026+
// purely a number, except for the last one, where a non-number suffix
2027+
// is stored in PatchSuffix. The third segment is allowed to not contain
2028+
// a number at all.
2029+
2030+
auto TryParseLastNumber = [&](StringRef Segment, int &Number,
2031+
std::string &OutStr) -> bool {
2032+
// Look for a number prefix and parse that, and split out any trailing
2033+
// string into GoodVersion.PatchSuffix.
2034+
2035+
if (size_t EndNumber = Segment.find_first_not_of("0123456789")) {
2036+
StringRef NumberStr = Segment.slice(0, EndNumber);
2037+
if (NumberStr.getAsInteger(10, Number) || Number < 0)
2038+
return false;
2039+
OutStr = NumberStr;
2040+
GoodVersion.PatchSuffix = Segment.substr(EndNumber);
2041+
return true;
20462042
}
2043+
return false;
2044+
};
2045+
auto TryParseNumber = [](StringRef Segment, int &Number) -> bool {
2046+
if (Segment.getAsInteger(10, Number) || Number < 0)
2047+
return false;
2048+
return true;
2049+
};
2050+
2051+
if (MinorStr.empty()) {
2052+
// If no minor string, major is the last segment
2053+
if (!TryParseLastNumber(MajorStr, GoodVersion.Major, GoodVersion.MajorStr))
2054+
return BadVersion;
2055+
return GoodVersion;
20472056
}
20482057

2058+
if (!TryParseNumber(MajorStr, GoodVersion.Major))
2059+
return BadVersion;
2060+
GoodVersion.MajorStr = MajorStr;
2061+
2062+
if (PatchStr.empty()) {
2063+
// If no patch string, minor is the last segment
2064+
if (!TryParseLastNumber(MinorStr, GoodVersion.Minor, GoodVersion.MinorStr))
2065+
return BadVersion;
2066+
return GoodVersion;
2067+
}
2068+
2069+
if (!TryParseNumber(MinorStr, GoodVersion.Minor))
2070+
return BadVersion;
2071+
GoodVersion.MinorStr = MinorStr;
2072+
2073+
// For the last segment, tolerate a missing number.
2074+
std::string DummyStr;
2075+
TryParseLastNumber(PatchStr, GoodVersion.Patch, DummyStr);
20492076
return GoodVersion;
20502077
}
20512078

clang/unittests/Driver/GCCVersionTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ const VersionParseTest TestCases[] = {
3939
{"4.4.2-rc4", 4, 4, 2, "4", "4", "-rc4"},
4040
{"4.4.x-patched", 4, 4, -1, "4", "4", ""},
4141
{"not-a-version", -1, -1, -1, "", "", ""},
42+
{"10-win32", 10, -1, -1, "10", "", "-win32"},
4243
};
4344

4445
TEST(GCCVersionTest, Parse) {

0 commit comments

Comments
 (0)