Unverified Commit f089c0fe authored by Amir Ayupov's avatar Amir Ayupov Committed by GitHub
Browse files

[llvm-profgen][NFC] Factor out parseAddress (#191594)

Replace `StringRef::getAsInteger(16)` calls with explicit `parseAddress`
to make it easier to support buildid-prefixed addresses in a follow-up
(#190863).
parent 2dca9228
Loading
Loading
Loading
Loading
+12 −5
Original line number Diff line number Diff line
@@ -664,6 +664,13 @@ void HybridPerfReader::unwindSamples() {
                     "frame to match.");
}

/// Parse a hex address from \p Str.
static bool parseAddress(StringRef Str, uint64_t &Addr, bool HasPrefix) {
  if (Str.consume_front("0x") != HasPrefix)
    return true;
  return Str.getAsInteger(16, Addr);
}

bool PerfScriptReader::extractLBRStack(TraceStream &TraceIt,
                                       SmallVectorImpl<LBREntry> &LBRStack) {
  // The raw format of LBR stack is like:
@@ -682,7 +689,7 @@ bool PerfScriptReader::extractLBRStack(TraceStream &TraceIt,
  size_t Index = 0;
  uint64_t LeadingAddr;
  if (!Records.empty() && !Records[0].contains('/')) {
    if (Records[0].getAsInteger(16, LeadingAddr)) {
    if (parseAddress(Records[0], LeadingAddr, false)) {
      WarnInvalidLBR(TraceIt);
      TraceIt.advance();
      return false;
@@ -704,8 +711,8 @@ bool PerfScriptReader::extractLBRStack(TraceStream &TraceIt,
    uint64_t Dst;

    // Stop at broken LBR records.
    if (Addresses.size() < 2 || Addresses[0].substr(2).getAsInteger(16, Src) ||
        Addresses[1].substr(2).getAsInteger(16, Dst)) {
    if (Addresses.size() < 2 || parseAddress(Addresses[0], Src, true) ||
        parseAddress(Addresses[1], Dst, true)) {
      WarnInvalidLBR(TraceIt);
      break;
    }
@@ -741,7 +748,7 @@ bool PerfScriptReader::extractCallstack(TraceStream &TraceIt,
  while (!TraceIt.isAtEoF() && !TraceIt.getCurrentLine().starts_with(" 0x")) {
    StringRef FrameStr = TraceIt.getCurrentLine().ltrim();
    uint64_t FrameAddr = 0;
    if (FrameStr.getAsInteger(16, FrameAddr)) {
    if (parseAddress(FrameStr, FrameAddr, false)) {
      // We might parse a non-perf sample line like empty line and comments,
      // skip it
      TraceIt.advance();
@@ -1210,7 +1217,7 @@ PerfContent PerfScriptReader::checkPerfScriptType(StringRef FileName) {
    // Detect sample with call stack
    int32_t Count = 0;
    while (!TraceIt.isAtEoF() &&
           !TraceIt.getCurrentLine().ltrim().getAsInteger(16, FrameAddr)) {
           !parseAddress(TraceIt.getCurrentLine().ltrim(), FrameAddr, false)) {
      Count++;
      TraceIt.advance();
    }