[Lldb-commits] [lldb] [LLDB] Add DIL code for handling plain variable names. (PR #120971)
via lldb-commits
lldb-commits at lists.llvm.org
Mon Feb 17 16:48:46 PST 2025
================
@@ -0,0 +1,308 @@
+//===-- DILParser.cpp -----------------------------------------------------===//
+//
+// 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
+//
+// This implements the recursive descent parser for the Data Inspection
+// Language (DIL), and its helper functions, which will eventually underlie the
+// 'frame variable' command. The language that this parser recognizes is
+// described in lldb/docs/dil-expr-lang.ebnf
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/ValueObject/DILParser.h"
+#include "lldb/Target/ExecutionContextScope.h"
+#include "lldb/ValueObject/DILAST.h"
+#include "lldb/ValueObject/DILEval.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/Support/FormatAdapters.h"
+#include <limits.h>
+#include <memory>
+#include <sstream>
+#include <stdlib.h>
+#include <string>
+
+namespace lldb_private::dil {
+
+inline void TokenKindsJoinImpl(std::ostringstream &os, Token::Kind k) {
+ os << "'" << Token::GetTokenName(k).str() << "'";
+}
+
+template <typename... Ts>
+inline void TokenKindsJoinImpl(std::ostringstream &os, Token::Kind k,
+ Ts... ks) {
+ TokenKindsJoinImpl(os, k);
+ os << ", ";
+ TokenKindsJoinImpl(os, ks...);
+}
+
+template <typename... Ts>
+inline std::string TokenKindsJoin(Token::Kind k, Ts... ks) {
+ std::ostringstream os;
+ TokenKindsJoinImpl(os, k, ks...);
+
+ return os.str();
+}
+
+std::string FormatDiagnostics(llvm::StringRef text, const std::string &message,
+ uint32_t loc) {
+ // Get the source buffer and the location of the current token.
+ size_t loc_offset = (size_t)loc;
+
+ // Look for the start of the line.
+ size_t line_start = text.rfind('\n', loc_offset);
+ line_start = line_start == llvm::StringRef::npos ? 0 : line_start + 1;
+
+ // Look for the end of the line.
+ size_t line_end = text.find('\n', loc_offset);
+ line_end = line_end == llvm::StringRef::npos ? text.size() : line_end;
+
+ // Get a view of the current line in the source code and the position of the
+ // diagnostics pointer.
+ llvm::StringRef line = text.slice(line_start, line_end);
+ int32_t arrow = loc + 1; // Column offset starts at 1, not 0.
+
+ // Calculate the padding in case we point outside of the expression (this can
+ // happen if the parser expected something, but got EOF).˚
+ size_t expr_rpad = std::max(0, arrow - static_cast<int32_t>(line.size()));
+ size_t arrow_rpad = std::max(0, static_cast<int32_t>(line.size()) - arrow);
+
+ return llvm::formatv("<expr:1:{0}>: {1}\n{2}\n{3}", loc, message,
+ llvm::fmt_pad(line, 0, expr_rpad),
+ llvm::fmt_pad("^", arrow - 1, arrow_rpad));
----------------
cmtice wrote:
llvm::fmt_pad requires a right-padding argument, so we need to supply one.
https://github.com/llvm/llvm-project/pull/120971
More information about the lldb-commits
mailing list