[PATCH] D139188: [Draft] Sanitize clang target triple

serge via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Dec 2 03:27:30 PST 2022


serge-sans-paille created this revision.
serge-sans-paille added a reviewer: aaron.ballman.
Herald added a project: All.
serge-sans-paille requested review of this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Warn when meeting an unknown vendor / os / environment


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D139188

Files:
  clang/include/clang/Basic/DiagnosticCommonKinds.td
  clang/lib/Basic/Targets.cpp


Index: clang/lib/Basic/Targets.cpp
===================================================================
--- clang/lib/Basic/Targets.cpp
+++ clang/lib/Basic/Targets.cpp
@@ -691,6 +691,25 @@
 } // namespace targets
 } // namespace clang
 
+
+static
+void CheckTriple(DiagnosticsEngine &Diags, llvm::Triple const& Triple)
+{
+  auto IsUnrecognizedValue = [](StringRef Value) {
+    return !Value.empty() && Value != "unknown" && Value != "none";
+  };
+
+  if (Triple.getVendor() == llvm::Triple::UnknownVendor &&
+      IsUnrecognizedValue(Triple.getVendorName()))
+    Diags.Report(diag::warn_target_unknown_triple_components) << "vendor" << Triple.getVendorName();
+  if (Triple.getOS() == llvm::Triple::UnknownOS &&
+      IsUnrecognizedValue(Triple.getOSName()))
+    Diags.Report(diag::warn_target_unknown_triple_components) << "os" << Triple.getOSName();
+  if (Triple.getEnvironment() == llvm::Triple::UnknownEnvironment &&
+      IsUnrecognizedValue(Triple.getEnvironmentName()))
+    Diags.Report(diag::warn_target_unknown_triple_components) << "environment" << Triple.getEnvironmentName();
+}
+
 using namespace clang::targets;
 /// CreateTargetInfo - Return the target info object for the specified target
 /// options.
@@ -707,6 +726,9 @@
   }
   Target->TargetOpts = Opts;
 
+  // Check after we created the target has it only raises warnings.
+  CheckTriple(Diags, Triple);
+
   // Set the target CPU if specified.
   if (!Opts->CPU.empty() && !Target->setCPU(Opts->CPU)) {
     Diags.Report(diag::err_target_unknown_cpu) << Opts->CPU;
Index: clang/include/clang/Basic/DiagnosticCommonKinds.td
===================================================================
--- clang/include/clang/Basic/DiagnosticCommonKinds.td
+++ clang/include/clang/Basic/DiagnosticCommonKinds.td
@@ -297,6 +297,8 @@
 
 // Targets
 
+def warn_target_unknown_triple_components : Warning<
+  "unknown triple %0 '%1'">;
 def err_target_unknown_triple : Error<
   "unknown target triple '%0', please use -triple or -arch">;
 def err_target_unknown_cpu : Error<"unknown target CPU '%0'">;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D139188.479583.patch
Type: text/x-patch
Size: 2070 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221202/2651a9a9/attachment.bin>


More information about the cfe-commits mailing list