xref: /freebsd-src/contrib/llvm-project/lldb/include/lldb/Interpreter/Interfaces/ScriptedInterface.h (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 
12 #include "lldb/Core/StructuredDataImpl.h"
13 #include "lldb/Utility/LLDBLog.h"
14 #include "lldb/Utility/Log.h"
15 #include "lldb/Utility/UnimplementedError.h"
16 #include "lldb/lldb-private.h"
17 
18 #include "llvm/Support/Compiler.h"
19 
20 #include <string>
21 
22 namespace lldb_private {
23 class ScriptedInterface {
24 public:
25   ScriptedInterface() = default;
26   virtual ~ScriptedInterface() = default;
27 
28   StructuredData::GenericSP GetScriptObjectInstance() {
29     return m_object_instance_sp;
30   }
31 
32   virtual llvm::SmallVector<llvm::StringLiteral> GetAbstractMethods() const = 0;
33 
34   template <typename Ret>
35   static Ret ErrorWithMessage(llvm::StringRef caller_name,
36                               llvm::StringRef error_msg, Status &error,
37                               LLDBLog log_caterogy = LLDBLog::Process) {
38     LLDB_LOGF(GetLog(log_caterogy), "%s ERROR = %s", caller_name.data(),
39               error_msg.data());
40     std::string full_error_message =
41         llvm::Twine(caller_name + llvm::Twine(" ERROR = ") +
42                     llvm::Twine(error_msg))
43             .str();
44     if (const char *detailed_error = error.AsCString())
45       full_error_message +=
46           llvm::Twine(llvm::Twine(" (") + llvm::Twine(detailed_error) +
47                       llvm::Twine(")"))
48               .str();
49     error.SetErrorString(full_error_message);
50     return {};
51   }
52 
53   template <typename T = StructuredData::ObjectSP>
54   static bool CheckStructuredDataObject(llvm::StringRef caller, T obj,
55                                         Status &error) {
56     if (!obj)
57       return ErrorWithMessage<bool>(caller, "Null Structured Data object",
58                                     error);
59 
60     if (!obj->IsValid()) {
61       return ErrorWithMessage<bool>(caller, "Invalid StructuredData object",
62                                     error);
63     }
64 
65     if (error.Fail())
66       return ErrorWithMessage<bool>(caller, error.AsCString(), error);
67 
68     return true;
69   }
70 
71 protected:
72   StructuredData::GenericSP m_object_instance_sp;
73 };
74 } // namespace lldb_private
75 #endif // LLDB_INTERPRETER_INTERFACES_SCRIPTEDINTERFACE_H
76