[llvm] [OptBisect] Add support for selecting ranges of passes and refactor DebugCounter to use a shared Range API. (PR #152393)
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri Sep 5 08:03:28 PDT 2025
================
@@ -0,0 +1,153 @@
+//===- llvm/Support/Range.cpp - Range parsing utility ---------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Support/Range.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/Regex.h"
+#include "llvm/Support/raw_ostream.h"
+#include <sstream>
+
+using namespace llvm;
+
+Expected<RangeUtils::RangeList> RangeUtils::parseRanges(const StringRef Str,
+ const char Separator) {
+ RangeList Ranges;
+
+ if (Str.empty())
+ return std::move(Ranges);
+
+ // Split by the specified separator
+ SmallVector<StringRef, 8> Parts;
+ Str.split(Parts, Separator, -1, false);
+
+ // Regex to match either single number or range "num1-num2"
+ const Regex RangeRegex("^([0-9]+)(-([0-9]+))?$");
+
+ for (StringRef Part : Parts) {
+ Part = Part.trim();
+ if (Part.empty())
+ continue;
+
+ SmallVector<StringRef, 4> Matches;
+ if (!RangeRegex.match(Part, &Matches))
+ return createStringError(std::errc::invalid_argument,
+ "Invalid range format: '%s'",
+ Part.str().c_str());
+
+ int64_t Begin, End;
+ if (Matches[1].getAsInteger(10, Begin))
+ return createStringError(std::errc::invalid_argument,
+ "Failed to parse number: '%s'",
+ Matches[1].str().c_str());
+
+ if (!Matches[3].empty()) {
+ // Range format "begin-end"
+ if (Matches[3].getAsInteger(10, End))
+ return createStringError(std::errc::invalid_argument,
+ "Failed to parse number: '%s'",
+ Matches[3].str().c_str());
+ if (Begin >= End)
+ return createStringError(std::errc::invalid_argument,
+ "Invalid range: %lld >= %lld", Begin, End);
+ } else
+ // Single number
+ End = Begin;
+
+ // Check ordering constraint (ranges must be in increasing order)
+ if (!Ranges.empty() && Begin <= Ranges.back().End)
+ return createStringError(
+ std::errc::invalid_argument,
+ "Expected ranges to be in increasing order: %lld <= %lld", Begin,
+ Ranges.back().End);
+
+ Ranges.push_back(Range(Begin, End));
+ }
+
+ return std::move(Ranges);
----------------
nikic wrote:
Shouldn't need std::move here due to NRVO?
https://github.com/llvm/llvm-project/pull/152393
More information about the llvm-commits
mailing list