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

via libc-commits libc-commits at lists.llvm.org
Tue Dec 19 17:25:28 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/9] 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/9] 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/9] 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/9] 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/9] 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/9] 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/9] 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;

>From 1fdd39005e46fbc9c3d9b65f1fb3fc86cf4e195a Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Wed, 13 Dec 2023 20:07:45 +0000
Subject: [PATCH 8/9] 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/Basic/DiagnosticDriverKinds.td      |  3 ++
 clang/lib/Driver/Driver.cpp                   | 16 +++++++
 clang/unittests/Driver/CMakeLists.txt         |  1 +
 clang/unittests/Driver/DriverTest.cpp         | 48 +++++++++++++++++++
 4 files changed, 68 insertions(+)
 create mode 100644 clang/unittests/Driver/DriverTest.cpp

diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 676f1a62b49dd0..250115cff80d19 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -786,4 +786,7 @@ def warn_android_unversioned_fallback : Warning<
   " directories will not be used in Clang 19. Provide a versioned directory"
   " for the target version or lower instead.">,
   InGroup<DiagGroup<"android-unversioned-fallback">>;
+
+def err_android_version_invalid : Error<
+  "Version %0 in triple %1-%2-%3 is invalid.">;
 }
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index ff95c899c5f3d4..55d773d101f89a 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1424,6 +1424,22 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
   const ToolChain &TC = getToolChain(
       *UArgs, computeTargetTriple(*this, TargetTriple, *UArgs));
 
+  if (TC.getTriple().isAndroid()) {
+    llvm::Triple Triple = TC.getTriple();
+    unsigned TripleVersion = Triple.getEnvironmentVersion().getMajor();
+    StringRef TripleVersionName = Triple.getVersionName();
+    StringRef TripleArch = Triple.getArchName();
+    StringRef TripleEnvironmentType = Triple.getEnvironmentTypeName(Triple.getEnvironment());
+
+    if (TripleVersion == 0 && TripleVersionName != "" && TripleEnvironmentType == "android") {
+        Diags.Report(diag::err_android_version_invalid) << Triple.getVersionName()
+                                                        << Triple.getArchName()
+                                                        << Triple.getVendorName()
+                                                        << Triple.getOSAndEnvironmentName();
+        ContainsError = true;
+    }
+  }
+
   // Report warning when arm64EC option is overridden by specified target
   if ((TC.getTriple().getArch() != llvm::Triple::aarch64 ||
        TC.getTriple().getSubArch() != llvm::Triple::AArch64SubArch_arm64ec) &&
diff --git a/clang/unittests/Driver/CMakeLists.txt b/clang/unittests/Driver/CMakeLists.txt
index 752037f78fb147..769f4d5e342443 100644
--- a/clang/unittests/Driver/CMakeLists.txt
+++ b/clang/unittests/Driver/CMakeLists.txt
@@ -8,6 +8,7 @@ set(LLVM_LINK_COMPONENTS
 
 add_clang_unittest(ClangDriverTests
   DistroTest.cpp
+  DriverTest.cpp
   DXCModeTest.cpp
   GCCVersionTest.cpp
   ToolChainTest.cpp
diff --git a/clang/unittests/Driver/DriverTest.cpp b/clang/unittests/Driver/DriverTest.cpp
new file mode 100644
index 00000000000000..eb3623bef531a5
--- /dev/null
+++ b/clang/unittests/Driver/DriverTest.cpp
@@ -0,0 +1,48 @@
+//===- unittests/Driver/DriverTest.cpp --- Driver tests -------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Unit tests for Driver.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Driver/ToolChain.h"
+#include "clang/Basic/DiagnosticIDs.h"
+#include "clang/Basic/DiagnosticOptions.h"
+#include "clang/Basic/LLVM.h"
+#include "clang/Basic/TargetOptions.h"
+#include "clang/Driver/Compilation.h"
+#include "clang/Driver/Driver.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "llvm/ADT/ArrayRef.h"
+#include "llvm/ADT/StringExtras.h"
+#include "llvm/MC/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
+#include "llvm/Support/VirtualFileSystem.h"
+#include "llvm/Support/raw_ostream.h"
+#include "gmock/gmock.h"
+#include "gtest/gtest.h"
+#include <memory>
+
+#include "SimpleDiagnosticConsumer.h"
+using namespace clang;
+using namespace clang::driver;
+
+namespace {
+
+TEST(DriverTest, InvalidAndroidVersion) {
+  IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
+  struct TestDiagnosticConsumer : public DiagnosticConsumer {};
+  IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
+  DiagnosticsEngine Diags(DiagID, &*DiagOpts, new TestDiagnosticConsumer);
+  Driver TheDriver("/bin/clang", "aarch64-linux-androidabiS", Diags);
+  std::unique_ptr<Compilation> C(TheDriver.BuildCompilation({"/bin/clang", "foo.cpp"}));
+  EXPECT_TRUE(C);
+  EXPECT_TRUE(C->containsError());
+}
+
+} // end anonymous namespace.

>From 752df27fa396d2f61e79333c9e1a516673a3cb13 Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Wed, 13 Dec 2023 20:07:45 +0000
Subject: [PATCH 9/9] Make clang report invalid target versions.

Clang always silently ignores garbage target versions and this makes
debug harder. So clang will report when target versions are invalid.
---
 clang/include/clang/Basic/DiagnosticDriverKinds.td |  2 +-
 clang/lib/Driver/Driver.cpp                        | 11 +++++++----
 clang/lib/Driver/ToolChain.cpp                     |  8 --------
 3 files changed, 8 insertions(+), 13 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 250115cff80d19..7846103e5c7544 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -788,5 +788,5 @@ def warn_android_unversioned_fallback : Warning<
   InGroup<DiagGroup<"android-unversioned-fallback">>;
 
 def err_android_version_invalid : Error<
-  "Version %0 in triple %1-%2-%3 is invalid.">;
+  "Version %0 in triple %1-%2-%3-%4 is invalid.">;
 }
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 55d773d101f89a..c0326674e6dce4 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1429,13 +1429,16 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
     unsigned TripleVersion = Triple.getEnvironmentVersion().getMajor();
     StringRef TripleVersionName = Triple.getVersionName();
     StringRef TripleArch = Triple.getArchName();
+    StringRef TripleOS = Triple.getOSName();
     StringRef TripleEnvironmentType = Triple.getEnvironmentTypeName(Triple.getEnvironment());
 
-    if (TripleVersion == 0 && TripleVersionName != "" && TripleEnvironmentType == "android") {
-        Diags.Report(diag::err_android_version_invalid) << Triple.getVersionName()
-                                                        << Triple.getArchName()
+    if (TripleVersion == 0 && TripleVersionName != "" && TripleEnvironmentType == "android"
+      && TripleOS != "unknown") {
+        Diags.Report(diag::err_android_version_invalid) << TripleVersionName
+                                                        << TripleArch
                                                         << Triple.getVendorName()
-                                                        << Triple.getOSAndEnvironmentName();
+                                                        << TripleOS
+                                                        << TripleEnvironmentType;
         ContainsError = true;
     }
   }
diff --git a/clang/lib/Driver/ToolChain.cpp b/clang/lib/Driver/ToolChain.cpp
index a0b500b9b590a0..96a57927339a97 100644
--- a/clang/lib/Driver/ToolChain.cpp
+++ b/clang/lib/Driver/ToolChain.cpp
@@ -688,14 +688,6 @@ 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