[Lldb-commits] [lldb] [LLDB] Add Lexer (with tests) for DIL (Data Inspection Language). (PR #123521)

Pavel Labath via lldb-commits lldb-commits at lists.llvm.org
Mon Jan 27 04:37:26 PST 2025


================
@@ -0,0 +1,218 @@
+//===-- DILLexerTests.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
+//
+//===----------------------------------------------------------------------===//
+
+#include "lldb/ValueObject/DILLexer.h"
+#include "llvm/ADT/StringRef.h"
+#include "gtest/gtest.h"
+#include <string>
+
+using llvm::StringRef;
+
+bool VerifyExpectedTokens(
+    lldb_private::dil::DILLexer &lexer,
+    std::vector<std::pair<lldb_private::dil::Token::Kind, std::string>>
+        exp_tokens,
+    uint32_t start_pos) {
+  if (lexer.NumLexedTokens() - start_pos < exp_tokens.size())
+    return false;
+
+  if (start_pos > 0)
+    lexer.ResetTokenIdx(start_pos -
+                        1); // GetNextToken increments the idx first.
+  for (const auto &pair : exp_tokens) {
+    lldb_private::dil::Token token = lexer.GetNextToken();
+    if (token.GetKind() != pair.first || token.GetSpelling() != pair.second)
+      return false;
+  }
+
+  return true;
+}
+
+TEST(DILLexerTests, SimpleTest) {
+  StringRef input_expr("simple_var");
+  uint32_t tok_len = 10;
+  lldb_private::dil::DILLexer lexer(input_expr);
+  lldb_private::dil::Token token;
+  token.SetKind(lldb_private::dil::Token::unknown);
+  EXPECT_EQ(token.GetKind(), lldb_private::dil::Token::unknown);
+  auto success = lexer.LexAll();
+
+  if (!success) {
+    EXPECT_TRUE(false);
+  }
----------------
labath wrote:

```suggestion
  ASSERT_THAT_EXPECTED(lexer.LexAll(), llvm::HasValue(true));`
```

https://github.com/llvm/llvm-project/pull/123521


More information about the lldb-commits mailing list