[libc-commits] [llvm] [lldb] [lld] [openmp] [compiler-rt] [mlir] [libc] [clang-tools-extra] [clang] [libcxx] [flang] Make clang report invalid target versions for all environment types. (PR #78655)

Fangrui Song via libc-commits libc-commits at lists.llvm.org
Fri Feb 2 12:52:20 PST 2024


================
@@ -1219,8 +1222,25 @@ VersionTuple Triple::getEnvironmentVersion() const {
 
 StringRef Triple::getEnvironmentVersionString() const {
   StringRef EnvironmentName = getEnvironmentName();
+
+  // none is a valid environment type - it basically amounts to a freestanding
+  // environment.
+  if (EnvironmentName == "none")
+    return "";
+
   StringRef EnvironmentTypeName = getEnvironmentTypeName(getEnvironment());
   EnvironmentName.consume_front(EnvironmentTypeName);
+
+  if (EnvironmentName.contains("-")) {
+    // -obj is the suffix
+    if (getObjectFormat() != Triple::UnknownObjectFormat) {
+      StringRef ObjectFormatTypeName =
+          getObjectFormatTypeName(getObjectFormat());
+      const std::string &tmp = (Twine("-") + ObjectFormatTypeName).str();
----------------
MaskRay wrote:

Just use `const std::string`. The lifetime extension due to `const &` is subtle and fragile, and does not give any benefit here.

https://en.cppreference.com/w/cpp/language/reference_initialization#Lifetime_of_a_temporary
https://abseil.io/tips/107

Then `EnvironmentName.consume_back(tmp);`

Avoid the used-once `StringRef Suffix = tmp;`

https://github.com/llvm/llvm-project/pull/78655


More information about the libc-commits mailing list