================
@@ -3471,32 +3171,574 @@ static int order_main() {
   return 0;
 }
 
-int main(int argc, const char *argv[]) {
-  InitLLVM X(argc, argv);
-  StringRef ProgName(sys::path::filename(argv[0]));
+static void reportCmdLineError(const Twine &Message) {
+  WithColor::error(errs(), ProgramName) << Message << "\n";
+}
+
+template <typename T>
+static bool parseNumericOption(const opt::Arg *A, T &Value) {
+  if (!A)
+    return true;
+  StringRef V = A->getValue();
+  T Parsed{};
+  if (!llvm::to_integer(V, Parsed, 0)) {
+    if (!std::numeric_limits<T>::is_signed && V == "-1") {
+      Value = std::numeric_limits<T>::max();
+      return true;
+    }
+    reportCmdLineError(Twine("invalid argument '") + V + "' for option '" +
+                       A->getSpelling() + "'");
+    return false;
+  }
+  Value = Parsed;
+  return true;
+}
+
+static bool applyLibraryOptions(const opt::InputArgList &Args) {
+  SmallVector<std::string, 16> CLStrings;
+  CLStrings.push_back(ProgramName);
+
+  auto AddFlag = [&](unsigned OptID, StringRef Spelling) {
+    if (Args.hasArg(OptID))
+      CLStrings.push_back(Spelling.str());
+  };
+  auto AddUInt = [&](unsigned OptID, StringRef Spelling) -> bool {
+    if (const opt::Arg *A = Args.getLastArg(OptID)) {
+      uint64_t Value = 0;
+      if (!parseNumericOption(A, Value))
+        return false;
+      CLStrings.push_back((Spelling + Twine("=") + Twine(Value)).str());
+    }
+    return true;
+  };
+  auto AddInt = [&](unsigned OptID, StringRef Spelling) -> bool {
+    if (const opt::Arg *A = Args.getLastArg(OptID)) {
+      int Value = 0;
+      if (!parseNumericOption(A, Value))
+        return false;
+      CLStrings.push_back((Spelling + Twine("=") + Twine(Value)).str());
+    }
+    return true;
+  };
+
+  AddFlag(OPT_profile_isfs, "--profile-isfs");
+  AddFlag(OPT_generate_merged_base_profiles, 
"--generate-merged-base-profiles");
+  if (!AddUInt(OPT_profile_symbol_list_cutoff, "--profile-symbol-list-cutoff"))
+    return false;
+  AddFlag(OPT_extbinary_write_vtable_type_prof,
+          "--extbinary-write-vtable-type-prof");
+
+  AddFlag(OPT_profile_summary_contextless, "--profile-summary-contextless");
+  if (!AddInt(OPT_profile_summary_cutoff_hot, "--profile-summary-cutoff-hot"))
+    return false;
+  if (!AddInt(OPT_profile_summary_cutoff_cold, 
"--profile-summary-cutoff-cold"))
+    return false;
+  if (!AddUInt(OPT_profile_summary_hot_count, "--profile-summary-hot-count"))
+    return false;
+  if (!AddUInt(OPT_profile_summary_cold_count, "--profile-summary-cold-count"))
+    return false;
+  if (!AddUInt(OPT_profile_summary_huge_working_set_size_threshold,
+               "--profile-summary-huge-working-set-size-threshold"))
+    return false;
+  if (!AddUInt(OPT_profile_summary_large_working_set_size_threshold,
+               "--profile-summary-large-working-set-size-threshold"))
+    return false;
+
+  if (CLStrings.size() == 1)
+    return true;
+
+  SmallVector<char *, 16> CLArgs;
+  for (std::string &S : CLStrings)
+    CLArgs.push_back(S.data());
+
+  return cl::ParseCommandLineOptions(CLArgs.size(), CLArgs.data(),
+                                     /*Overview=*/"", /*Errs=*/nullptr,
+                                     /*VFS=*/nullptr,
+                                     /*EnvVar=*/nullptr,
+                                     /*LongOptionsUseDoubleDash=*/false);
+}
+
+static bool parseFloatOption(const opt::Arg *A, float &Value) {
+  if (!A)
+    return true;
+  StringRef V = A->getValue();
+  double Parsed;
+  if (V.getAsDouble(Parsed)) {
+    reportCmdLineError(Twine("invalid argument '") + V + "' for option '" +
+                       A->getSpelling() + "'");
+    return false;
+  }
+  Value = static_cast<float>(Parsed);
+  return true;
+}
+
+static bool parseCutoffValues(const opt::InputArgList &Args,
+                              std::vector<uint32_t> &Cutoffs) {
+  Cutoffs.clear();
+  for (const opt::Arg *A : Args.filtered(OPT_detailed_summary_cutoffs)) {
+    SmallVector<StringRef, 4> Parts;
+    StringRef(A->getValue())
+        .split(Parts, ',', /*MaxSplit=*/-1,
+               /*KeepEmpty=*/false);
+    for (StringRef Part : Parts) {
+      uint32_t Parsed;
+      if (!llvm::to_integer(Part, Parsed, 0)) {
+        reportCmdLineError(Twine("invalid argument '") + Part +
+                           "' for option '" + A->getSpelling() + "'");
+        return false;
+      }
+      Cutoffs.push_back(Parsed);
+    }
+  }
+  return true;
+}
+
+static bool parseFSDiscriminatorPassArg(const opt::InputArgList &Args) {
----------------
dzbarsky wrote:

not sure if OptTable has a builtin way to model this?

https://github.com/llvm/llvm-project/pull/177868
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to