Skip to content

Commit

Permalink
Make clang report invalid target versions. (llvm#75373)
Browse files Browse the repository at this point in the history
Clang always silently ignores garbage target versions and this makes
debug harder. So clang will report when target versions are invalid.
  • Loading branch information
ZijunZhaoCCK authored and justinfargnoli committed Jan 28, 2024
1 parent 00c466d commit 50d1796
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 6 deletions.
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticDriverKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -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">;
}
11 changes: 11 additions & 0 deletions clang/lib/Driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) &&
Expand Down
6 changes: 3 additions & 3 deletions clang/test/CodeGen/aarch64-fix-cortex-a53-835769.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion clang/test/Driver/aarch64-fix-cortex-a53-835769.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
16 changes: 16 additions & 0 deletions clang/test/Driver/android-version.cpp
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 6 additions & 0 deletions llvm/include/llvm/TargetParser/Triple.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// @{
Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/TargetParser/Triple.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 50d1796

Please sign in to comment.