Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make clang report invalid target versions. #75373

Merged
merged 36 commits into from
Jan 9, 2024

Conversation

ZijunZhaoCCK
Copy link
Contributor

Clang always silently ignores garbage target versions and this makes debug harder. So clang will report when target versions are invalid.

Clang always silently ignores garbage target versions and this makes
debug harder. So clang will report when target versions are invalid.
Copy link

github-actions bot commented Dec 13, 2023

✅ With the latest revision this PR passed the C/C++ code formatter.

@pirama-arumuga-nainar
Copy link
Collaborator

@llvm/clang-vendors since this change can affect downstream components.

Can we do this change in two steps?

  1. Since we want this error for Android, let's make a PR that restricts the error only to Android.
  2. Make a follow-up PR to report error for all environments. We can revert/iterate on this PR until all downstream errors are handled.

@rnk
Copy link
Collaborator

rnk commented Dec 14, 2023

This change requires tests

ZijunZhaoCCK and others added 2 commits December 13, 2023 17:40
Clang always silently ignores garbage target versions and this makes
debug harder. So clang will report when target versions are invalid.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Dec 14, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Dec 14, 2023

@llvm/pr-subscribers-clang

Author: None (ZijunZhaoCCK)

Changes

Clang always silently ignores garbage target versions and this makes debug harder. So clang will report when target versions are invalid.


Full diff: https://github.com/llvm/llvm-project/pull/75373.diff

3 Files Affected:

  • (modified) clang/lib/Basic/Targets/OSTargets.h (+5)
  • (modified) llvm/include/llvm/TargetParser/Triple.h (+4)
  • (modified) llvm/lib/TargetParser/Triple.cpp (+13-1)
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 ac04dab0489717..db4ba7100781bc 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"
@@ -1198,9 +1199,20 @@ 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);
+  if (Version.tryParse(Name)) {
+    errs() << "version "<< Name << " is invalid\n";
+    exit(1);
+  }
   return Version.withoutBuild();
 }
 

ZijunZhaoCCK and others added 6 commits December 13, 2023 17:52
Clang always silently ignores garbage target versions and this makes
debug harder. So clang will report when target versions are invalid.
Clang always silently ignores garbage target versions and this makes
debug harder. So clang will report when target versions are invalid.
Clang always silently ignores garbage target versions and this makes
debug harder. So clang will report when target versions are invalid.
@tru
Copy link
Collaborator

tru commented Dec 14, 2023

This needs to be cleaned up before it can be merged. There needs to be only one commit in this PR without the merge commits from main.

@tru
Copy link
Collaborator

tru commented Dec 14, 2023

It also needs tests.

@ZijunZhaoCCK ZijunZhaoCCK marked this pull request as draft December 14, 2023 07:34
@zmodem
Copy link
Collaborator

zmodem commented Dec 14, 2023

Thanks for looking at this. Silently ignoring bad inputs is usually not a good idea.

However, it would seem better to emit a proper diagnostic from the driver rather than calling exit in getOSDefines(). That way the regular diagnostic mechanisms can be used to decide what to do with it (treat as error, ignore, etc.).

@MaskRay
Copy link
Member

MaskRay commented Dec 14, 2023

In addition, calling exit from library code is probably not good. (Think of a service that constructs a triple for each request. It doesn't want to just quit before of an invalid input!) The error can be surfaced by a very top-level component (e.g. driver as @zmodem mentioned).

Copy link
Collaborator

@pirama-arumuga-nainar pirama-arumuga-nainar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also wait for @MaskRay's LGTM before merging.

@ZijunZhaoCCK ZijunZhaoCCK merged commit f6dbd4c into llvm:main Jan 9, 2024
4 checks passed
@ZijunZhaoCCK ZijunZhaoCCK deleted the clang-warning branch January 9, 2024 04:46
ZijunZhaoCCK added a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Jan 19, 2024
Followup for llvm#75373

1. Make this feature not just available for android, but everyone.
2. Correct some target triples/
3. Add opencl to the environment type list.
ZijunZhaoCCK added a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Jan 19, 2024
Followup for llvm#75373

1. Make this feature not just available for android, but everyone.
2. Correct some target triples.
3. Add opencl to the environment type list.
ZijunZhaoCCK added a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Jan 24, 2024
Followup for llvm#75373

1. Make this feature not just available for android, but everyone.
2. Correct some target triples.
3. Add opencl to the environment type list.
ZijunZhaoCCK added a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Jan 24, 2024
Followup for llvm#75373

1. Make this feature not just available for android, but everyone.
2. Correct some target triples.
3. Add opencl to the environment type list.
ZijunZhaoCCK added a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Jan 26, 2024
Followup for llvm#75373

1. Make this feature not just available for android, but everyone.
2. Correct some target triples.
3. Add opencl to the environment type list.
justinfargnoli pushed a commit to justinfargnoli/llvm-project that referenced this pull request Jan 28, 2024
Clang always silently ignores garbage target versions and this makes
debug harder. So clang will report when target versions are invalid.
ZijunZhaoCCK added a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Feb 1, 2024
Followup for llvm#75373

1. Make this feature not just available for android, but everyone.
2. Correct some target triples.
3. Add opencl to the environment type list.
ZijunZhaoCCK added a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Feb 2, 2024
Followup for llvm#75373

1. Make this feature not just available for android, but everyone.
2. Correct some target triples.
3. Add opencl to the environment type list.
ZijunZhaoCCK added a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Feb 2, 2024
Followup for llvm#75373

1. Make this feature not just available for android, but everyone.
2. Correct some target triples.
3. Add opencl to the environment type list.
ZijunZhaoCCK added a commit that referenced this pull request Feb 5, 2024
…pes. (#78655)

Followup for #75373

1. Make this feature not just available for android, but everyone.
2. Correct some target triples.
3. Add opencl to the environment type list.
agozillon pushed a commit to agozillon/llvm-project that referenced this pull request Feb 5, 2024
…pes. (llvm#78655)

Followup for llvm#75373

1. Make this feature not just available for android, but everyone.
2. Correct some target triples.
3. Add opencl to the environment type list.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants