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

via flang-commits flang-commits at lists.llvm.org
Mon Jan 8 16:01:16 PST 2024


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 01/14] 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 02/14] 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 03/14] 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 04/14] 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 05/14] 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 06/14] 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 07/14] 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 08/14] 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 09/14] 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;

>From 1136d390f19da78a39bb30b55f2e3046f05d9fe6 Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Wed, 13 Dec 2023 20:07:45 +0000
Subject: [PATCH 10/14] 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/lib/Driver/Driver.cpp | 14 ++++++--------
 1 file changed, 6 insertions(+), 8 deletions(-)

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index c0326674e6dce4..e1a413da002c06 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1432,14 +1432,12 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
     StringRef TripleOS = Triple.getOSName();
     StringRef TripleEnvironmentType = Triple.getEnvironmentTypeName(Triple.getEnvironment());
 
-    if (TripleVersion == 0 && TripleVersionName != "" && TripleEnvironmentType == "android"
-      && TripleOS != "unknown") {
-        Diags.Report(diag::err_android_version_invalid) << TripleVersionName
-                                                        << TripleArch
-                                                        << Triple.getVendorName()
-                                                        << TripleOS
-                                                        << TripleEnvironmentType;
-        ContainsError = true;
+    if (TripleVersion == 0 && TripleVersionName != "" &&
+        TripleEnvironmentType == "android" && TripleOS != "unknown") {
+      Diags.Report(diag::err_android_version_invalid)
+          << TripleVersionName << TripleArch << Triple.getVendorName()
+          << TripleOS << TripleEnvironmentType;
+      ContainsError = true;
     }
   }
 

>From e889780602d43f5bc0888acc8b809ad50f2e2a4c Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Tue, 2 Jan 2024 20:14:18 +0000
Subject: [PATCH 11/14] reformat

---
 clang/lib/Driver/Driver.cpp           | 3 ++-
 clang/unittests/Driver/DriverTest.cpp | 7 ++++---
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index b5a3cb7ff2ebf7..aa00b0a2fd24da 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1436,7 +1436,8 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
     StringRef TripleVersionName = Triple.getVersionName();
     StringRef TripleArch = Triple.getArchName();
     StringRef TripleOS = Triple.getOSName();
-    StringRef TripleEnvironmentType = Triple.getEnvironmentTypeName(Triple.getEnvironment());
+    StringRef TripleEnvironmentType =
+        Triple.getEnvironmentTypeName(Triple.getEnvironment());
 
     if (TripleVersion == 0 && TripleVersionName != "" &&
         TripleEnvironmentType == "android" && TripleOS != "unknown") {
diff --git a/clang/unittests/Driver/DriverTest.cpp b/clang/unittests/Driver/DriverTest.cpp
index eb3623bef531a5..3c35a0268d43d8 100644
--- a/clang/unittests/Driver/DriverTest.cpp
+++ b/clang/unittests/Driver/DriverTest.cpp
@@ -10,13 +10,13 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "clang/Driver/ToolChain.h"
+#include "clang/Driver/Driver.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/Driver/ToolChain.h"
 #include "clang/Frontend/CompilerInstance.h"
 #include "llvm/ADT/ArrayRef.h"
 #include "llvm/ADT/StringExtras.h"
@@ -40,7 +40,8 @@ TEST(DriverTest, InvalidAndroidVersion) {
   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"}));
+  std::unique_ptr<Compilation> C(
+      gitTheDriver.BuildCompilation({"/bin/clang", "foo.cpp"}));
   EXPECT_TRUE(C);
   EXPECT_TRUE(C->containsError());
 }

>From bc0ea977dc83706b32fc59e2659d709673dadee5 Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Thu, 4 Jan 2024 00:34:00 +0000
Subject: [PATCH 12/14] 1. update tests 2. update getEnvironmentVersion()

---
 clang/lib/Driver/Driver.cpp                   | 16 +++---
 .../CodeGen/aarch64-fix-cortex-a53-835769.c   |  6 +--
 .../Driver/aarch64-fix-cortex-a53-835769.c    |  2 +-
 clang/test/Driver/android-version.cpp         | 16 ++++++
 clang/unittests/Driver/CMakeLists.txt         |  1 -
 clang/unittests/Driver/DriverTest.cpp         | 49 -------------------
 llvm/include/llvm/TargetParser/Triple.h       |  4 +-
 llvm/lib/TargetParser/Triple.cpp              | 15 ++----
 8 files changed, 34 insertions(+), 75 deletions(-)
 create mode 100644 clang/test/Driver/android-version.cpp
 delete mode 100644 clang/unittests/Driver/DriverTest.cpp

diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index aa00b0a2fd24da..a8b597cd5c70b1 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1433,17 +1433,13 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
   if (TC.getTriple().isAndroid()) {
     llvm::Triple Triple = TC.getTriple();
     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" && TripleOS != "unknown") {
+    StringRef TripleVersionName = Triple.getEnvironmentVersionString();
+
+    if (TripleVersion == 0 && TripleVersionName != "") {
       Diags.Report(diag::err_android_version_invalid)
-          << TripleVersionName << TripleArch << Triple.getVendorName()
-          << TripleOS << TripleEnvironmentType;
+          << TripleVersionName << Triple.getArchName()
+          << Triple.getVendorName() << Triple.getOSName()
+          << Triple.getEnvironmentTypeName(Triple.getEnvironment());
       ContainsError = true;
     }
   }
diff --git a/clang/test/CodeGen/aarch64-fix-cortex-a53-835769.c b/clang/test/CodeGen/aarch64-fix-cortex-a53-835769.c
index 3d1a2c7aceb1f1..df43260ff69d18 100644
--- a/clang/test/CodeGen/aarch64-fix-cortex-a53-835769.c
+++ b/clang/test/CodeGen/aarch64-fix-cortex-a53-835769.c
@@ -5,13 +5,13 @@
 // RUN: %clang -O3 -target aarch64-linux-eabi -mno-fix-cortex-a53-835769 %s -S -o- 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-NO --check-prefix=CHECK %s
 
-// RUN: %clang -O3 -target aarch64-android-eabi %s -S -o- \
+// RUN: %clang -O3 -target aarch64-linux-androideabi %s -S -o- \
 // RUN:   | FileCheck --check-prefix=CHECK-YES --check-prefix=CHECK %s
 // RUN: %clang -O3 -target aarch64-linux-ohos %s -S -o- \
 // RUN:   | FileCheck --check-prefix=CHECK-YES --check-prefix=CHECK %s
-// RUN: %clang -O3 -target aarch64-android-eabi -mfix-cortex-a53-835769 %s -S -o- \
+// RUN: %clang -O3 -target aarch64-linux-androideabi -mfix-cortex-a53-835769 %s -S -o- \
 // RUN:   | FileCheck --check-prefix=CHECK-YES --check-prefix=CHECK %s
-// RUN: %clang -O3 -target aarch64-android-eabi -mno-fix-cortex-a53-835769 %s -S -o- \
+// RUN: %clang -O3 -target aarch64-linux-androideabi -mno-fix-cortex-a53-835769 %s -S -o- \
 // RUN:   | FileCheck --check-prefix=CHECK-NO --check-prefix=CHECK %s
 
 // REQUIRES: aarch64-registered-target
diff --git a/clang/test/Driver/aarch64-fix-cortex-a53-835769.c b/clang/test/Driver/aarch64-fix-cortex-a53-835769.c
index a854920f3e6ef3..d7a2ad9112611b 100644
--- a/clang/test/Driver/aarch64-fix-cortex-a53-835769.c
+++ b/clang/test/Driver/aarch64-fix-cortex-a53-835769.c
@@ -5,7 +5,7 @@
 // RUN: %clang --target=aarch64-linux-eabi -mno-fix-cortex-a53-835769 %s -### 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-NO %s
 
-// RUN: %clang --target=aarch64-android-eabi %s -### 2>&1 \
+// RUN: %clang --target=aarch64-linux-androideabi %s -### 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-YES %s
 
 // RUN: %clang --target=aarch64-fuchsia %s -### 2>&1 \
diff --git a/clang/test/Driver/android-version.cpp b/clang/test/Driver/android-version.cpp
new file mode 100644
index 00000000000000..62b5ef88d2bce3
--- /dev/null
+++ b/clang/test/Driver/android-version.cpp
@@ -0,0 +1,16 @@
+// Check that we get the right Android version.
+
+// RUN: not %clang -target aarch64-linux-androidS -c %s -### 2>&1 | \
+// RUN:   FileCheck --check-prefix=CHECK-ERROR %s
+
+// CHECK-ERROR: error: Version S in triple aarch64-unknown-linux-android is invalid.
+
+// RUN: not %clang -target armv7-linux-androideabiS -c %s -### 2>&1 | \
+// RUN:   FileCheck --check-prefix=CHECK-ERROR1 %s
+
+// CHECK-ERROR1: error: Version S in triple armv7-unknown-linux-android is invalid.
+
+// RUN: %clang -target aarch64-linux-android31 -c %s -### 2>&1 | \
+// RUN:   FileCheck --check-prefix=CHECK-TARGET %s
+
+// CHECK-TARGET: "aarch64-unknown-linux-android31"
\ No newline at end of file
diff --git a/clang/unittests/Driver/CMakeLists.txt b/clang/unittests/Driver/CMakeLists.txt
index 769f4d5e342443..752037f78fb147 100644
--- a/clang/unittests/Driver/CMakeLists.txt
+++ b/clang/unittests/Driver/CMakeLists.txt
@@ -8,7 +8,6 @@ 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
deleted file mode 100644
index 3c35a0268d43d8..00000000000000
--- a/clang/unittests/Driver/DriverTest.cpp
+++ /dev/null
@@ -1,49 +0,0 @@
-//===- 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/Driver.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/ToolChain.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(
-      gitTheDriver.BuildCompilation({"/bin/clang", "foo.cpp"}));
-  EXPECT_TRUE(C);
-  EXPECT_TRUE(C->containsError());
-}
-
-} // end anonymous namespace.
diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h
index 05df1c489ad06e..95014a546f7245 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -436,7 +436,9 @@ class Triple {
 
   /// Get the version component of the environment component as a single
   /// string (the version after the environment).
-  StringRef getVersionName() const;
+  ///
+  /// For example, "fooos1.2.3" would return "1.2.3".
+  StringRef getEnvironmentVersionString() const;
 
   /// @}
   /// @name Convenience Predicates
diff --git a/llvm/lib/TargetParser/Triple.cpp b/llvm/lib/TargetParser/Triple.cpp
index 254c572857b4ef..b9971c25af71f3 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -1199,14 +1199,6 @@ 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);
@@ -1214,11 +1206,14 @@ static VersionTuple parseVersionFromName(StringRef Name) {
 }
 
 VersionTuple Triple::getEnvironmentVersion() const {
+  return parseVersionFromName(getEnvironmentVersionString());
+}
+
+StringRef Triple::getEnvironmentVersionString() const {
   StringRef EnvironmentName = getEnvironmentName();
   StringRef EnvironmentTypeName = getEnvironmentTypeName(getEnvironment());
   EnvironmentName.consume_front(EnvironmentTypeName);
-
-  return parseVersionFromName(EnvironmentName);
+  return EnvironmentName;
 }
 
 VersionTuple Triple::getOSVersion() const {

>From 917c7eae216003068aed994b6af5928e642116f8 Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Thu, 4 Jan 2024 00:39:32 +0000
Subject: [PATCH 13/14] add newline

---
 clang/test/Driver/android-version.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/clang/test/Driver/android-version.cpp b/clang/test/Driver/android-version.cpp
index 62b5ef88d2bce3..d703e537de293e 100644
--- a/clang/test/Driver/android-version.cpp
+++ b/clang/test/Driver/android-version.cpp
@@ -13,4 +13,4 @@
 // RUN: %clang -target aarch64-linux-android31 -c %s -### 2>&1 | \
 // RUN:   FileCheck --check-prefix=CHECK-TARGET %s
 
-// CHECK-TARGET: "aarch64-unknown-linux-android31"
\ No newline at end of file
+// CHECK-TARGET: "aarch64-unknown-linux-android31"

>From ff429315277804c32840e4115dca3f5bdbcb3c00 Mon Sep 17 00:00:00 2001
From: zijunzhao <zijunzhao at google.com>
Date: Mon, 8 Jan 2024 21:52:32 +0000
Subject: [PATCH 14/14] update tests

---
 clang/include/clang/Basic/DiagnosticDriverKinds.td |  2 +-
 clang/lib/Driver/Driver.cpp                        |  4 +---
 clang/test/CodeGen/aarch64-fix-cortex-a53-835769.c |  6 +++---
 clang/test/Driver/android-version.cpp              | 10 +++++-----
 4 files changed, 10 insertions(+), 12 deletions(-)

diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 7846103e5c7544..b4ca78cb12f8c9 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-%4 is invalid.">;
+  "Version %0 in triple %1 is invalid.">;
 }
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index a8b597cd5c70b1..cbc566af99761e 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1437,9 +1437,7 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
 
     if (TripleVersion == 0 && TripleVersionName != "") {
       Diags.Report(diag::err_android_version_invalid)
-          << TripleVersionName << Triple.getArchName()
-          << Triple.getVendorName() << Triple.getOSName()
-          << Triple.getEnvironmentTypeName(Triple.getEnvironment());
+          << TripleVersionName << TC.getTripleString();
       ContainsError = true;
     }
   }
diff --git a/clang/test/CodeGen/aarch64-fix-cortex-a53-835769.c b/clang/test/CodeGen/aarch64-fix-cortex-a53-835769.c
index df43260ff69d18..e5d70564d57b30 100644
--- a/clang/test/CodeGen/aarch64-fix-cortex-a53-835769.c
+++ b/clang/test/CodeGen/aarch64-fix-cortex-a53-835769.c
@@ -5,13 +5,13 @@
 // RUN: %clang -O3 -target aarch64-linux-eabi -mno-fix-cortex-a53-835769 %s -S -o- 2>&1 \
 // RUN:   | FileCheck --check-prefix=CHECK-NO --check-prefix=CHECK %s
 
-// RUN: %clang -O3 -target aarch64-linux-androideabi %s -S -o- \
+// RUN: %clang -O3 --target=aarch64-linux-androideabi %s -S -o- \
 // RUN:   | FileCheck --check-prefix=CHECK-YES --check-prefix=CHECK %s
 // RUN: %clang -O3 -target aarch64-linux-ohos %s -S -o- \
 // RUN:   | FileCheck --check-prefix=CHECK-YES --check-prefix=CHECK %s
-// RUN: %clang -O3 -target aarch64-linux-androideabi -mfix-cortex-a53-835769 %s -S -o- \
+// RUN: %clang -O3 --target=aarch64-linux-androideabi -mfix-cortex-a53-835769 %s -S -o- \
 // RUN:   | FileCheck --check-prefix=CHECK-YES --check-prefix=CHECK %s
-// RUN: %clang -O3 -target aarch64-linux-androideabi -mno-fix-cortex-a53-835769 %s -S -o- \
+// RUN: %clang -O3 --target=aarch64-linux-androideabi -mno-fix-cortex-a53-835769 %s -S -o- \
 // RUN:   | FileCheck --check-prefix=CHECK-NO --check-prefix=CHECK %s
 
 // REQUIRES: aarch64-registered-target
diff --git a/clang/test/Driver/android-version.cpp b/clang/test/Driver/android-version.cpp
index d703e537de293e..3eef772186a5f9 100644
--- a/clang/test/Driver/android-version.cpp
+++ b/clang/test/Driver/android-version.cpp
@@ -1,16 +1,16 @@
 // Check that we get the right Android version.
 
-// RUN: not %clang -target aarch64-linux-androidS -c %s -### 2>&1 | \
+// RUN: not %clang --target=aarch64-linux-androidS -c %s -### 2>&1 | \
 // RUN:   FileCheck --check-prefix=CHECK-ERROR %s
 
-// CHECK-ERROR: error: Version S in triple aarch64-unknown-linux-android is invalid.
+// CHECK-ERROR: error: Version S in triple aarch64-unknown-linux-androidS is invalid.
 
-// RUN: not %clang -target armv7-linux-androideabiS -c %s -### 2>&1 | \
+// RUN: not %clang --target=armv7-linux-androideabiS -c %s -### 2>&1 | \
 // RUN:   FileCheck --check-prefix=CHECK-ERROR1 %s
 
-// CHECK-ERROR1: error: Version S in triple armv7-unknown-linux-android is invalid.
+// CHECK-ERROR1: error: Version S in triple armv7-unknown-linux-androidS is invalid.
 
-// RUN: %clang -target aarch64-linux-android31 -c %s -### 2>&1 | \
+// RUN: %clang --target=aarch64-linux-android31 -c %s -### 2>&1 | \
 // RUN:   FileCheck --check-prefix=CHECK-TARGET %s
 
 // CHECK-TARGET: "aarch64-unknown-linux-android31"



More information about the flang-commits mailing list