[clang] f6dbd4c - Make clang report invalid target versions. (#75373)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Jan 8 20:46:11 PST 2024
Author: ZijunZhaoCCK
Date: 2024-01-08T20:46:05-08:00
New Revision: f6dbd4cc5f52b6d40f98cf09af22b276b8e1f289
URL: https://github.com/llvm/llvm-project/commit/f6dbd4cc5f52b6d40f98cf09af22b276b8e1f289
DIFF: https://github.com/llvm/llvm-project/commit/f6dbd4cc5f52b6d40f98cf09af22b276b8e1f289.diff
LOG: Make clang report invalid target versions. (#75373)
Clang always silently ignores garbage target versions and this makes
debug harder. So clang will report when target versions are invalid.
Added:
clang/test/Driver/android-version.cpp
Modified:
clang/include/clang/Basic/DiagnosticDriverKinds.td
clang/lib/Driver/Driver.cpp
clang/test/CodeGen/aarch64-fix-cortex-a53-835769.c
clang/test/Driver/aarch64-fix-cortex-a53-835769.c
llvm/include/llvm/TargetParser/Triple.h
llvm/lib/TargetParser/Triple.cpp
Removed:
################################################################################
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 676f1a62b49dd0..0a8a77fadbeb1b 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_drv_triple_version_invalid : Error<
+ "version '%0' in target triple '%1' is invalid">;
}
diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp
index 9b2f2a37480983..1889ea28079df1 100644
--- a/clang/lib/Driver/Driver.cpp
+++ b/clang/lib/Driver/Driver.cpp
@@ -1430,6 +1430,17 @@ 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();
+ StringRef TripleVersionName = Triple.getEnvironmentVersionString();
+
+ if (Triple.getEnvironmentVersion().empty() && TripleVersionName != "") {
+ Diags.Report(diag::err_drv_triple_version_invalid)
+ << TripleVersionName << TC.getTripleString();
+ 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/test/CodeGen/aarch64-fix-cortex-a53-835769.c b/clang/test/CodeGen/aarch64-fix-cortex-a53-835769.c
index 3d1a2c7aceb1f1..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-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..d365b701c0223b
--- /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 target triple 'aarch64-unknown-linux-androidS' 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 target triple 'armv7-unknown-linux-androidS' 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"
diff --git a/llvm/include/llvm/TargetParser/Triple.h b/llvm/include/llvm/TargetParser/Triple.h
index 47904621c0967f..95014a546f7245 100644
--- a/llvm/include/llvm/TargetParser/Triple.h
+++ b/llvm/include/llvm/TargetParser/Triple.h
@@ -434,6 +434,12 @@ 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).
+ ///
+ /// 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 e93502187b5492..b9971c25af71f3 100644
--- a/llvm/lib/TargetParser/Triple.cpp
+++ b/llvm/lib/TargetParser/Triple.cpp
@@ -1206,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 {
More information about the cfe-commits
mailing list