[libc-commits] [clang-tools-extra] [libc] [clang] [compiler-rt] [mlir] [lld] [llvm] [flang] Make clang report garbage target versions. (PR #75373)

via libc-commits libc-commits at lists.llvm.org
Tue Dec 19 16:10:15 PST 2023


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

>From 74f256d8a77ee2ba8e0d5bbb6519aa2729cf94d5 Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Wed, 13 Dec 2023 20:07:45 +0000
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 <zijunzhao at google.com>
Date: Wed, 13 Dec 2023 20:23:54 +0000
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 <zijunzhao at google.com>
Date: Wed, 13 Dec 2023 20:07:45 +0000
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<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 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 VersionTuple parseVersionFromName(StringRef Name) {
   VersionTuple Version;
   if (Version.tryParse(Name)) {

>From 8519713b7832a06ac756b84b9ba48e0d0f74e9da Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Wed, 13 Dec 2023 20:07:45 +0000
Subject: [PATCH 4/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 | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index db4ba7100781bc..801f72aef804dd 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -11,7 +11,6 @@
 #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"
@@ -1209,10 +1208,7 @@ StringRef Triple::getVersionName() const {
 
 static VersionTuple parseVersionFromName(StringRef Name) {
   VersionTuple Version;
-  if (Version.tryParse(Name)) {
-    errs() << "version "<< Name << " is invalid\n";
-    exit(1);
-  }
+  Version.tryParse(Name);
   return Version.withoutBuild();
 }
 

>From a726fe2ba0db8a11089a1496c51f0c2d539677bc Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Wed, 13 Dec 2023 20:07:45 +0000
Subject: [PATCH 5/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 | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/clang/lib/Basic/Targets/OSTargets.h b/clang/lib/Basic/Targets/OSTargets.h
index bc28066019971c..c11f7dc0e2d8d0 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -324,9 +324,10 @@ class LLVM_LIBRARY_VISIBILITY LinuxTargetInfo : public OSTargetInfo<Target> {
         // 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";
+        llvm::errs() << "version "<< Triple.getVersionName() << " in triple "
+                     << Triple.getArchName() << "-" << Triple.getVendorName()
+                     << "-" << Triple.getOSAndEnvironmentName()
+                     << " is invalid\n";
         exit(1);
       }
     } else {

>From 8ed23efe08a752a5fd568327b2f15739d6645ab8 Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Wed, 13 Dec 2023 20:07:45 +0000
Subject: [PATCH 6/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 | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/lib/Basic/Targets/OSTargets.h b/clang/lib/Basic/Targets/OSTargets.h
index c11f7dc0e2d8d0..c5b3f0293ab8ad 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -324,7 +324,7 @@ class LLVM_LIBRARY_VISIBILITY LinuxTargetInfo : public OSTargetInfo<Target> {
         // defined for compatibility.
         Builder.defineMacro("__ANDROID_API__", "__ANDROID_MIN_SDK_VERSION__");
       } else {
-        llvm::errs() << "version "<< Triple.getVersionName() << " in triple "
+        llvm::errs() << "version " << Triple.getVersionName() << " in triple "
                      << Triple.getArchName() << "-" << Triple.getVendorName()
                      << "-" << Triple.getOSAndEnvironmentName()
                      << " is invalid\n";

>From 01128aa6abcbd90f0a42ff2dc01408f55e9e3010 Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Wed, 13 Dec 2023 20:07:45 +0000
Subject: [PATCH 7/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 | 6 ------
 clang/lib/Driver/ToolChain.cpp      | 8 ++++++++
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/clang/lib/Basic/Targets/OSTargets.h b/clang/lib/Basic/Targets/OSTargets.h
index c5b3f0293ab8ad..342af4bbc42b7b 100644
--- a/clang/lib/Basic/Targets/OSTargets.h
+++ b/clang/lib/Basic/Targets/OSTargets.h
@@ -323,12 +323,6 @@ 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/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index 96a57927339a97..a0b500b9b590a0 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -688,6 +688,14 @@ ToolChain::getFallbackAndroidTargetPath(StringRef BaseDir) const {
   unsigned TripleVersion = getTriple().getEnvironmentVersion().getMajor();
   unsigned BestVersion = 0;
 
+  if (TripleVersion == 0) {
+    return llvm::createStringError("version " + getTriple().getVersionName() +
+                                   " in triple " + getTriple().getArchName() +
+                                   "-" + getTriple().getVendorName() + "-" +
+                                   getTriple().getOSAndEnvironmentName() +
+                                   " is invalid\n";);
+  }
+
   SmallString<32> TripleDir;
   bool UsingUnversionedDir = false;
   std::error_code EC;



More information about the libc-commits mailing list