| 1 | //===- FileLineColLocBreakpointManager.cpp - MLIR Optimizer Driver --------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "mlir/Debug/BreakpointManagers/FileLineColLocBreakpointManager.h" |
| 10 | #include "mlir/IR/Diagnostics.h" |
| 11 | #include "llvm/Support/raw_ostream.h" |
| 12 | |
| 13 | using namespace mlir; |
| 14 | using namespace mlir::tracing; |
| 15 | |
| 16 | FailureOr<std::tuple<StringRef, int64_t, int64_t>> |
| 17 | FileLineColLocBreakpoint::parseFromString(StringRef str, |
| 18 | function_ref<void(Twine)> diag) { |
| 19 | // Watch at debug locations arguments are expected to be in the form: |
| 20 | // `fileName:line:col`, `fileName:line`, or `fileName`. |
| 21 | |
| 22 | if (str.empty()) { |
| 23 | if (diag) |
| 24 | diag("error: initializing FileLineColLocBreakpoint with empty file name" ); |
| 25 | return failure(); |
| 26 | } |
| 27 | |
| 28 | // This logic is complex because on Windows `:` is a comment valid path |
| 29 | // character: `C:\...`. |
| 30 | auto [fileLine, colStr] = str.rsplit(Separator: ':'); |
| 31 | auto [file, lineStr] = fileLine.rsplit(Separator: ':'); |
| 32 | // Extract the line and column value |
| 33 | int64_t line = -1, col = -1; |
| 34 | if (lineStr.empty()) { |
| 35 | // No candidate for line number, try to use the column string as line |
| 36 | // instead. |
| 37 | file = fileLine; |
| 38 | if (!colStr.empty() && colStr.getAsInteger(Radix: 0, Result&: line)) |
| 39 | file = str; |
| 40 | } else { |
| 41 | if (lineStr.getAsInteger(Radix: 0, Result&: line)) { |
| 42 | // Failed to parse a line number, try to use the column string as line |
| 43 | // instead. If this failed as well, the entire string is the file name. |
| 44 | file = fileLine; |
| 45 | if (colStr.getAsInteger(Radix: 0, Result&: line)) |
| 46 | file = str; |
| 47 | } else { |
| 48 | // We successfully parsed a line number, try to parse the column number. |
| 49 | // This shouldn't fail, or the entire string is the file name. |
| 50 | if (colStr.getAsInteger(Radix: 0, Result&: col)) { |
| 51 | file = str; |
| 52 | line = -1; |
| 53 | } |
| 54 | } |
| 55 | } |
| 56 | return std::tuple<StringRef, int64_t, int64_t>{file, line, col}; |
| 57 | } |
| 58 | |