[llvm] [clang-tools-extra] [clang] Make clang report garbage target versions. (PR #75373)

via cfe-commits cfe-commits at lists.llvm.org
Wed Dec 13 17:42:00 PST 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (ZijunZhaoCCK)

<details>
<summary>Changes</summary>

Clang always silently ignores garbage target versions and this makes debug harder. So clang will report when target versions are invalid.

---
Full diff: https://github.com/llvm/llvm-project/pull/75373.diff


3 Files Affected:

- (modified) clang/lib/Basic/Targets/OSTargets.h (+5) 
- (modified) llvm/include/llvm/TargetParser/Triple.h (+4) 
- (modified) llvm/lib/TargetParser/Triple.cpp (+13-1) 


``````````diff
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<Target> {
         // 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 ac04dab0489717..db4ba7100781bc 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"
@@ -1198,9 +1199,20 @@ 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 VersionTuple parseVersionFromName(StringRef Name) {
   VersionTuple Version;
-  Version.tryParse(Name);
+  if (Version.tryParse(Name)) {
+    errs() << "version "<< Name << " is invalid\n";
+    exit(1);
+  }
   return Version.withoutBuild();
 }
 

``````````

</details>


https://github.com/llvm/llvm-project/pull/75373


More information about the cfe-commits mailing list