xref: /llvm-project/mlir/lib/Debug/BreakpointManagers/FileLineColLocBreakpointManager.cpp (revision 7f069f5ef4fee00520ed0c350dca42c3c4b72b61)
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>>
parseFromString(StringRef str,function_ref<void (Twine)> diag)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(':');
31   auto [file, lineStr] = fileLine.rsplit(':');
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(0, line))
39       file = str;
40   } else {
41     if (lineStr.getAsInteger(0, 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(0, 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(0, col)) {
51         file = str;
52         line = -1;
53       }
54     }
55   }
56   return std::tuple<StringRef, int64_t, int64_t>{file, line, col};
57 }
58