Skip to content

Commit f6dbd4c

Browse files
authored
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.
1 parent 49c35f6 commit f6dbd4c

File tree

7 files changed

+45
-6
lines changed

7 files changed

+45
-6
lines changed

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -786,4 +786,7 @@ def warn_android_unversioned_fallback : Warning<
786786
" directories will not be used in Clang 19. Provide a versioned directory"
787787
" for the target version or lower instead.">,
788788
InGroup<DiagGroup<"android-unversioned-fallback">>;
789+
790+
def err_drv_triple_version_invalid : Error<
791+
"version '%0' in target triple '%1' is invalid">;
789792
}

clang/lib/Driver/Driver.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1430,6 +1430,17 @@ Compilation *Driver::BuildCompilation(ArrayRef<const char *> ArgList) {
14301430
const ToolChain &TC = getToolChain(
14311431
*UArgs, computeTargetTriple(*this, TargetTriple, *UArgs));
14321432

1433+
if (TC.getTriple().isAndroid()) {
1434+
llvm::Triple Triple = TC.getTriple();
1435+
StringRef TripleVersionName = Triple.getEnvironmentVersionString();
1436+
1437+
if (Triple.getEnvironmentVersion().empty() && TripleVersionName != "") {
1438+
Diags.Report(diag::err_drv_triple_version_invalid)
1439+
<< TripleVersionName << TC.getTripleString();
1440+
ContainsError = true;
1441+
}
1442+
}
1443+
14331444
// Report warning when arm64EC option is overridden by specified target
14341445
if ((TC.getTriple().getArch() != llvm::Triple::aarch64 ||
14351446
TC.getTriple().getSubArch() != llvm::Triple::AArch64SubArch_arm64ec) &&

clang/test/CodeGen/aarch64-fix-cortex-a53-835769.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
// RUN: %clang -O3 -target aarch64-linux-eabi -mno-fix-cortex-a53-835769 %s -S -o- 2>&1 \
66
// RUN: | FileCheck --check-prefix=CHECK-NO --check-prefix=CHECK %s
77

8-
// RUN: %clang -O3 -target aarch64-android-eabi %s -S -o- \
8+
// RUN: %clang -O3 --target=aarch64-linux-androideabi %s -S -o- \
99
// RUN: | FileCheck --check-prefix=CHECK-YES --check-prefix=CHECK %s
1010
// RUN: %clang -O3 -target aarch64-linux-ohos %s -S -o- \
1111
// RUN: | FileCheck --check-prefix=CHECK-YES --check-prefix=CHECK %s
12-
// RUN: %clang -O3 -target aarch64-android-eabi -mfix-cortex-a53-835769 %s -S -o- \
12+
// RUN: %clang -O3 --target=aarch64-linux-androideabi -mfix-cortex-a53-835769 %s -S -o- \
1313
// RUN: | FileCheck --check-prefix=CHECK-YES --check-prefix=CHECK %s
14-
// RUN: %clang -O3 -target aarch64-android-eabi -mno-fix-cortex-a53-835769 %s -S -o- \
14+
// RUN: %clang -O3 --target=aarch64-linux-androideabi -mno-fix-cortex-a53-835769 %s -S -o- \
1515
// RUN: | FileCheck --check-prefix=CHECK-NO --check-prefix=CHECK %s
1616

1717
// REQUIRES: aarch64-registered-target

clang/test/Driver/aarch64-fix-cortex-a53-835769.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// RUN: %clang --target=aarch64-linux-eabi -mno-fix-cortex-a53-835769 %s -### 2>&1 \
66
// RUN: | FileCheck --check-prefix=CHECK-NO %s
77

8-
// RUN: %clang --target=aarch64-android-eabi %s -### 2>&1 \
8+
// RUN: %clang --target=aarch64-linux-androideabi %s -### 2>&1 \
99
// RUN: | FileCheck --check-prefix=CHECK-YES %s
1010

1111
// RUN: %clang --target=aarch64-fuchsia %s -### 2>&1 \

clang/test/Driver/android-version.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Check that we get the right Android version.
2+
3+
// RUN: not %clang --target=aarch64-linux-androidS -c %s -### 2>&1 | \
4+
// RUN: FileCheck --check-prefix=CHECK-ERROR %s
5+
6+
// CHECK-ERROR: error: version 'S' in target triple 'aarch64-unknown-linux-androidS' is invalid
7+
8+
// RUN: not %clang --target=armv7-linux-androideabiS -c %s -### 2>&1 | \
9+
// RUN: FileCheck --check-prefix=CHECK-ERROR1 %s
10+
11+
// CHECK-ERROR1: error: version 'S' in target triple 'armv7-unknown-linux-androidS' is invalid
12+
13+
// RUN: %clang --target=aarch64-linux-android31 -c %s -### 2>&1 | \
14+
// RUN: FileCheck --check-prefix=CHECK-TARGET %s
15+
16+
// CHECK-TARGET: "aarch64-unknown-linux-android31"

llvm/include/llvm/TargetParser/Triple.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,12 @@ class Triple {
434434
/// string (separated by a '-' if the environment component is present).
435435
StringRef getOSAndEnvironmentName() const;
436436

437+
/// Get the version component of the environment component as a single
438+
/// string (the version after the environment).
439+
///
440+
/// For example, "fooos1.2.3" would return "1.2.3".
441+
StringRef getEnvironmentVersionString() const;
442+
437443
/// @}
438444
/// @name Convenience Predicates
439445
/// @{

llvm/lib/TargetParser/Triple.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,11 +1206,14 @@ static VersionTuple parseVersionFromName(StringRef Name) {
12061206
}
12071207

12081208
VersionTuple Triple::getEnvironmentVersion() const {
1209+
return parseVersionFromName(getEnvironmentVersionString());
1210+
}
1211+
1212+
StringRef Triple::getEnvironmentVersionString() const {
12091213
StringRef EnvironmentName = getEnvironmentName();
12101214
StringRef EnvironmentTypeName = getEnvironmentTypeName(getEnvironment());
12111215
EnvironmentName.consume_front(EnvironmentTypeName);
1212-
1213-
return parseVersionFromName(EnvironmentName);
1216+
return EnvironmentName;
12141217
}
12151218

12161219
VersionTuple Triple::getOSVersion() const {

0 commit comments

Comments
 (0)