[clang-tools-extra] [llvm] [lld] [flang] [clang] [mlir] [compiler-rt] [libc] Make clang report garbage target versions. (PR #75373)
https://github.com/ZijunZhaoCCK updated https://github.com/llvm/llvm-project/pull/75373 >From 74f256d8a77ee2ba8e0d5bbb6519aa2729cf94d5 Mon Sep 17 00:00:00 2001 From: zijunzhao Date: Wed, 13 Dec 2023 20:07:45 + Subject: [PATCH 1/6] Make clang report garbage target versions. Clang always silently ignores garbage target versions and this makes debug harder. So clang will report when target versions are invalid. --- llvm/lib/TargetParser/Triple.cpp | 7 ++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp index c5e9ad43d22588..335253194d1cf8 100644 --- a/llvm/lib/TargetParser/Triple.cpp +++ b/llvm/lib/TargetParser/Triple.cpp @@ -11,6 +11,7 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/raw_ostream.h" #include "llvm/Support/SwapByteOrder.h" #include "llvm/Support/VersionTuple.h" #include "llvm/TargetParser/ARMTargetParser.h" @@ -1199,7 +1200,11 @@ StringRef Triple::getOSAndEnvironmentName() const { static VersionTuple parseVersionFromName(StringRef Name) { VersionTuple Version; - Version.tryParse(Name); + if (Version.tryParse(Name)) { +errs() << "The input is "<< Name << " and it is invalid. Should pass an "<< +"integer or integer combination! e.g. 2, 9.5 or 3.4.5\n"; +exit(1); + } return Version.withoutBuild(); } >From 0d159b2a9b76e233e020ac0aef15b49b03f4d86c Mon Sep 17 00:00:00 2001 From: zijunzhao Date: Wed, 13 Dec 2023 20:23:54 + Subject: [PATCH 2/6] rephrase --- llvm/lib/TargetParser/Triple.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp index 335253194d1cf8..713ca447403d50 100644 --- a/llvm/lib/TargetParser/Triple.cpp +++ b/llvm/lib/TargetParser/Triple.cpp @@ -1201,8 +1201,7 @@ StringRef Triple::getOSAndEnvironmentName() const { static VersionTuple parseVersionFromName(StringRef Name) { VersionTuple Version; if (Version.tryParse(Name)) { -errs() << "The input is "<< Name << " and it is invalid. Should pass an "<< -"integer or integer combination! e.g. 2, 9.5 or 3.4.5\n"; +errs() << "version "<< Name << " is invalid\n"; exit(1); } return Version.withoutBuild(); >From b2dda9ce95804783c59aa1ca4e81a7941aae805d Mon Sep 17 00:00:00 2001 From: zijunzhao Date: Wed, 13 Dec 2023 20:07:45 + Subject: [PATCH 3/6] Make clang report garbage target versions. Clang always silently ignores garbage target versions and this makes debug harder. So clang will report when target versions are invalid. --- clang/lib/Basic/Targets/OSTargets.h | 5 + llvm/include/llvm/TargetParser/Triple.h | 4 llvm/lib/TargetParser/Triple.cpp| 8 3 files changed, 17 insertions(+) diff --git a/clang/lib/Basic/Targets/OSTargets.h b/clang/lib/Basic/Targets/OSTargets.h index 342af4bbc42b7b..bc28066019971c 100644 --- a/clang/lib/Basic/Targets/OSTargets.h +++ b/clang/lib/Basic/Targets/OSTargets.h @@ -323,6 +323,11 @@ class LLVM_LIBRARY_VISIBILITY LinuxTargetInfo : public OSTargetInfo { // This historical but ambiguous name for the minSdkVersion macro. Keep // defined for compatibility. Builder.defineMacro("__ANDROID_API__", "__ANDROID_MIN_SDK_VERSION__"); + } else { +llvm::errs() << "version "<< Triple.getVersionName() << +" in triple " << Triple.getArchName() << "-" << Triple.getVendorName() +<< "-" << Triple.getOSAndEnvironmentName() << " is invalid\n"; +exit(1); } } else { Builder.defineMacro("__gnu_linux__"); diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h index 47904621c0967f..05df1c489ad06e 100644 --- a/llvm/include/llvm/TargetParser/Triple.h +++ b/llvm/include/llvm/TargetParser/Triple.h @@ -434,6 +434,10 @@ class Triple { /// string (separated by a '-' if the environment component is present). StringRef getOSAndEnvironmentName() const; + /// Get the version component of the environment component as a single + /// string (the version after the environment). + StringRef getVersionName() const; + /// @} /// @name Convenience Predicates /// @{ diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp index 49bc24ffbfae6c..db4ba7100781bc 100644 --- a/llvm/lib/TargetParser/Triple.cpp +++ b/llvm/lib/TargetParser/Triple.cpp @@ -1199,6 +1199,14 @@ StringRef Triple::getOSAndEnvironmentName() const { return Tmp.split('-').second; // Strip second component } +StringRef Triple::getVersionName() const { + StringRef VersionName = getEnvironmentName(); + StringRef EnvironmentTypeName = getEnvironmentTypeName(getEnvironment()); + if (VersionName.startswith(EnvironmentTypeName)) +return VersionName.substr(EnvironmentTypeName.size()); + return VersionName; +} + static Versio
[clang-tools-extra] [llvm] [lld] [flang] [clang] [mlir] [compiler-rt] [libc] Make clang report garbage target versions. (PR #75373)
https://github.com/ZijunZhaoCCK updated https://github.com/llvm/llvm-project/pull/75373 >From 74f256d8a77ee2ba8e0d5bbb6519aa2729cf94d5 Mon Sep 17 00:00:00 2001 From: zijunzhao Date: Wed, 13 Dec 2023 20:07:45 + Subject: [PATCH 1/7] Make clang report garbage target versions. Clang always silently ignores garbage target versions and this makes debug harder. So clang will report when target versions are invalid. --- llvm/lib/TargetParser/Triple.cpp | 7 ++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp index c5e9ad43d22588..335253194d1cf8 100644 --- a/llvm/lib/TargetParser/Triple.cpp +++ b/llvm/lib/TargetParser/Triple.cpp @@ -11,6 +11,7 @@ #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/Support/ErrorHandling.h" +#include "llvm/Support/raw_ostream.h" #include "llvm/Support/SwapByteOrder.h" #include "llvm/Support/VersionTuple.h" #include "llvm/TargetParser/ARMTargetParser.h" @@ -1199,7 +1200,11 @@ StringRef Triple::getOSAndEnvironmentName() const { static VersionTuple parseVersionFromName(StringRef Name) { VersionTuple Version; - Version.tryParse(Name); + if (Version.tryParse(Name)) { +errs() << "The input is "<< Name << " and it is invalid. Should pass an "<< +"integer or integer combination! e.g. 2, 9.5 or 3.4.5\n"; +exit(1); + } return Version.withoutBuild(); } >From 0d159b2a9b76e233e020ac0aef15b49b03f4d86c Mon Sep 17 00:00:00 2001 From: zijunzhao Date: Wed, 13 Dec 2023 20:23:54 + Subject: [PATCH 2/7] rephrase --- llvm/lib/TargetParser/Triple.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp index 335253194d1cf8..713ca447403d50 100644 --- a/llvm/lib/TargetParser/Triple.cpp +++ b/llvm/lib/TargetParser/Triple.cpp @@ -1201,8 +1201,7 @@ StringRef Triple::getOSAndEnvironmentName() const { static VersionTuple parseVersionFromName(StringRef Name) { VersionTuple Version; if (Version.tryParse(Name)) { -errs() << "The input is "<< Name << " and it is invalid. Should pass an "<< -"integer or integer combination! e.g. 2, 9.5 or 3.4.5\n"; +errs() << "version "<< Name << " is invalid\n"; exit(1); } return Version.withoutBuild(); >From b2dda9ce95804783c59aa1ca4e81a7941aae805d Mon Sep 17 00:00:00 2001 From: zijunzhao Date: Wed, 13 Dec 2023 20:07:45 + Subject: [PATCH 3/7] Make clang report garbage target versions. Clang always silently ignores garbage target versions and this makes debug harder. So clang will report when target versions are invalid. --- clang/lib/Basic/Targets/OSTargets.h | 5 + llvm/include/llvm/TargetParser/Triple.h | 4 llvm/lib/TargetParser/Triple.cpp| 8 3 files changed, 17 insertions(+) diff --git a/clang/lib/Basic/Targets/OSTargets.h b/clang/lib/Basic/Targets/OSTargets.h index 342af4bbc42b7b..bc28066019971c 100644 --- a/clang/lib/Basic/Targets/OSTargets.h +++ b/clang/lib/Basic/Targets/OSTargets.h @@ -323,6 +323,11 @@ class LLVM_LIBRARY_VISIBILITY LinuxTargetInfo : public OSTargetInfo { // This historical but ambiguous name for the minSdkVersion macro. Keep // defined for compatibility. Builder.defineMacro("__ANDROID_API__", "__ANDROID_MIN_SDK_VERSION__"); + } else { +llvm::errs() << "version "<< Triple.getVersionName() << +" in triple " << Triple.getArchName() << "-" << Triple.getVendorName() +<< "-" << Triple.getOSAndEnvironmentName() << " is invalid\n"; +exit(1); } } else { Builder.defineMacro("__gnu_linux__"); diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h index 47904621c0967f..05df1c489ad06e 100644 --- a/llvm/include/llvm/TargetParser/Triple.h +++ b/llvm/include/llvm/TargetParser/Triple.h @@ -434,6 +434,10 @@ class Triple { /// string (separated by a '-' if the environment component is present). StringRef getOSAndEnvironmentName() const; + /// Get the version component of the environment component as a single + /// string (the version after the environment). + StringRef getVersionName() const; + /// @} /// @name Convenience Predicates /// @{ diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp index 49bc24ffbfae6c..db4ba7100781bc 100644 --- a/llvm/lib/TargetParser/Triple.cpp +++ b/llvm/lib/TargetParser/Triple.cpp @@ -1199,6 +1199,14 @@ StringRef Triple::getOSAndEnvironmentName() const { return Tmp.split('-').second; // Strip second component } +StringRef Triple::getVersionName() const { + StringRef VersionName = getEnvironmentName(); + StringRef EnvironmentTypeName = getEnvironmentTypeName(getEnvironment()); + if (VersionName.startswith(EnvironmentTypeName)) +return VersionName.substr(EnvironmentTypeName.size()); + return VersionName; +} + static Versio