LLDB mainline
ScriptedInterface.h
Go to the documentation of this file.
1//===-- ScriptedInterface.h -------------------------------------*- C++ -*-===//
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#ifndef LLDB_INTERPRETER_INTERFACES_SCRIPTEDINTERFACE_H
10#define LLDB_INTERPRETER_INTERFACES_SCRIPTEDINTERFACE_H
11
13
16#include "lldb/Utility/Log.h"
19#include "lldb/lldb-private.h"
20
21#include "llvm/Support/Compiler.h"
22
23#include <optional>
24#include <string>
25
26namespace lldb_private {
28public:
29 ScriptedInterface() = default;
30 virtual ~ScriptedInterface() = default;
31
35
36 const std::optional<ScriptedMetadata> &GetScriptedMetadata() const {
38 }
39
41 llvm::StringLiteral name;
42 size_t min_arg_count = 0;
43 };
44
45 virtual llvm::SmallVector<AbstractMethodRequirement>
47
48 virtual llvm::Expected<FileSpec> GetScriptedModulePath() {
49 return llvm::make_error<UnimplementedError>();
50 }
51
52 llvm::SmallVector<llvm::StringLiteral> const GetAbstractMethods() const {
53 llvm::SmallVector<llvm::StringLiteral> abstract_methods;
54 llvm::transform(GetAbstractMethodRequirements(), abstract_methods.begin(),
55 [](const AbstractMethodRequirement &requirement) {
56 return requirement.name;
57 });
58 return abstract_methods;
59 }
60
61 template <typename Ret>
62 static Ret ErrorWithMessage(llvm::StringRef caller_name,
63 llvm::StringRef user_msg, Status &error,
64 LLDBLog log_category = LLDBLog::Process) {
65 LLDB_LOGF(GetLog(log_category), "%s ERROR = %s", caller_name.data(),
66 user_msg.data());
67
68 // If `error` already has detailed content (e.g. a Python traceback),
69 // prepend this call's friendlier message to it instead of discarding
70 // either one.
71 std::string existing_error = error.Fail() ? error.AsCString() : "";
72 if (existing_error.empty())
73 error = Status::FromErrorString(user_msg.data());
74 else
75 error = Status::FromErrorStringWithFormatv("{0}: {1}", user_msg,
76 existing_error);
77
78 return {};
79 }
80
81 template <typename T = StructuredData::ObjectSP>
82 static bool CheckStructuredDataObject(llvm::StringRef caller, T obj,
83 Status &error) {
84 if (!obj)
85 return ErrorWithMessage<bool>(caller, "Null Structured Data object",
86 error);
87
88 if (!obj->IsValid()) {
89 return ErrorWithMessage<bool>(caller, "Invalid StructuredData object",
90 error);
91 }
92
93 if (error.Fail())
94 return ErrorWithMessage<bool>(caller, error.AsCString(), error);
95
96 return true;
97 }
98
101 return false;
102 }
103
104protected:
106 std::optional<ScriptedMetadata> m_scripted_metadata;
107};
108} // namespace lldb_private
109
110#endif // LLDB_INTERPRETER_INTERFACES_SCRIPTEDINTERFACE_H
static llvm::raw_ostream & error(Stream &strm)
#define LLDB_LOGF(log,...)
Definition Log.h:389
static Ret ErrorWithMessage(llvm::StringRef caller_name, llvm::StringRef user_msg, Status &error, LLDBLog log_category=LLDBLog::Process)
virtual ~ScriptedInterface()=default
std::optional< ScriptedMetadata > m_scripted_metadata
llvm::SmallVector< llvm::StringLiteral > const GetAbstractMethods() const
virtual llvm::SmallVector< AbstractMethodRequirement > GetAbstractMethodRequirements() const =0
static bool CheckStructuredDataObject(llvm::StringRef caller, T obj, Status &error)
virtual llvm::Expected< FileSpec > GetScriptedModulePath()
StructuredData::GenericSP GetScriptObjectInstance()
static bool CreateInstance(lldb::ScriptLanguage language, ScriptedInterfaceUsages usages)
const std::optional< ScriptedMetadata > & GetScriptedMetadata() const
StructuredData::GenericSP m_object_instance_sp
An error handling class.
Definition Status.h:118
static Status FromErrorString(const char *str)
Definition Status.h:141
static Status static Status FromErrorStringWithFormatv(const char *format, Args &&...args)
Definition Status.h:151
std::shared_ptr< Generic > GenericSP
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Definition Log.h:338
ScriptLanguage
Script interpreter types.