Skip to content

Commit c209f35

Browse files
committed
[lldb[RPC] Upstream RPC server interface emitters
This commit upstreams the LLDB RPC server interface emitters. These emitters generate the server-side API interfaces for RPC, which communicate directly with liblldb itself out of process using the SB API. https://discourse.llvm.org/t/rfc-upstreaming-lldb-rpc/85804
1 parent 27e8e08 commit c209f35

File tree

8 files changed

+833
-0
lines changed

8 files changed

+833
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// This ia a basic header file used to check that the server-side emitter
2+
// for rpc-gen emits an expected set of includes in a generated source file.
3+
#ifndef LLDB_API_SBRPC_CHECKBASICINCLUDE_H
4+
#define LLDB_API_SBRPC_CHECKBASICINCLUDE_H
5+
6+
#endif // LLDB_API_SBRPC_CHECKBASICINCLUDE_H
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef LLDB_API_SBRPC_CHECKCONSTCHARPOINTER_H
2+
#define LLDB_API_SBRPC_CHECKCONSTCHARPOINTER_H
3+
4+
namespace lldb {
5+
class LLDB_API SBRPC_CHECKCONSTCHARPOINTER {
6+
public:
7+
// const char * parameters must decoded as rpc_common::ConstCharPointer in server side
8+
// source files.
9+
int CheckConstCharPointer(char *string);
10+
11+
}; // class SBRPC_CHECKCONSTCHARPOINTER
12+
} // namespace lldb
13+
14+
#endif // LLDB_API_SBRPC_CHECKCONSTCHARPOINTER_H
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Disabling until the lldb-rpc-gen tool lands.
2+
UNSUPPORTED: system-windows, system-linux, system-darwin
3+
RUN: mkdir -p %t/server
4+
RUN: mkdir -p %t/lib
5+
RUN: %lldb-rpc-gen --output-dir=%t %S/../../Inputs/CheckBasicIncludesEmit.h
6+
7+
RUN: cat %t/lib/CheckBasicIncludesEmit.cpp | FileCheck %s
8+
9+
# All server-side source files must have these includes at the top of their files.
10+
CHECK: #include "RPCUserServer.h"
11+
CHECK: #include "SBAPI.h"
12+
CHECK: #include <lldb-rpc/common/RPCArgument.h>
13+
CHECK: #include <lldb-rpc/common/RPCCommon.h>
14+
CHECK: #include <lldb-rpc/common/RPCFunction.h>
15+
CHECK: #include <lldb/API/LLDB.h>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Disabling until the lldb-rpc-gen tool lands.
2+
UNSUPPORTED: system-windows, system-linux, system-darwin
3+
RUN: mkdir -p %t/server
4+
RUN: mkdir -p %t/lib
5+
RUN: %lldb-rpc-gen --output-dir=%t %S/../../Inputs/CheckConstCharPointer.h
6+
7+
RUN: cat %t/lib/CheckConstCharPointer.cpp | FileCheck %s
8+
9+
# const char * pointers must be decoded as rpc_common::ConstCharPointer objects
10+
# in server side source files.
11+
CHECK: rpc_common::ConstCharPointer string
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
//===-- RPCServerHeaderEmitter.cpp ----------------------------------------===//
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 "RPCServerHeaderEmitter.h"
10+
#include "RPCCommon.h"
11+
12+
#include "clang/AST/AST.h"
13+
#include "clang/AST/Mangle.h"
14+
#include "clang/Frontend/CompilerInstance.h"
15+
16+
#include "llvm/ADT/StringRef.h"
17+
#include "llvm/Support/ToolOutputFile.h"
18+
#include "llvm/Support/raw_ostream.h"
19+
20+
using namespace clang;
21+
using namespace lldb_rpc_gen;
22+
23+
void RPCServerHeaderEmitter::EmitMethod(const Method &method) {
24+
// We'll be using the mangled name in order to disambiguate
25+
// overloaded methods.
26+
const std::string &MangledName = method.MangledName;
27+
28+
EmitLine("class " + MangledName +
29+
" : public rpc_common::RPCFunctionInstance {");
30+
EmitLine("public:");
31+
IndentLevel++;
32+
EmitConstructor(MangledName);
33+
EmitDestructor(MangledName);
34+
EmitHandleRPCCall();
35+
IndentLevel--;
36+
EmitLine("};");
37+
}
38+
39+
void RPCServerHeaderEmitter::EmitHandleRPCCall() {
40+
EmitLine("bool HandleRPCCall(rpc_common::Connection &connection, "
41+
"rpc_common::RPCStream &send, rpc_common::RPCStream &response) "
42+
"override;");
43+
}
44+
45+
void RPCServerHeaderEmitter::EmitConstructor(const std::string &MangledName) {
46+
EmitLine(MangledName + "() : RPCFunctionInstance(\"" + MangledName +
47+
"\") {}");
48+
}
49+
50+
void RPCServerHeaderEmitter::EmitDestructor(const std::string &MangledName) {
51+
EmitLine("~" + MangledName + "() override {}");
52+
}
53+
54+
std::string RPCServerHeaderEmitter::GetHeaderGuard() {
55+
const std::string UpperFilenameNoExt =
56+
llvm::sys::path::stem(
57+
llvm::sys::path::filename(OutputFile->getFilename()))
58+
.upper();
59+
return "GENERATED_LLDB_RPC_SERVER_" + UpperFilenameNoExt + "_H";
60+
}
61+
62+
void RPCServerHeaderEmitter::Begin() {
63+
const std::string HeaderGuard = GetHeaderGuard();
64+
EmitLine("#ifndef " + HeaderGuard);
65+
EmitLine("#define " + HeaderGuard);
66+
EmitLine("");
67+
EmitLine("#include <lldb-rpc/common/RPCFunction.h>");
68+
EmitLine("");
69+
EmitLine("namespace rpc_server {");
70+
}
71+
72+
void RPCServerHeaderEmitter::End() {
73+
EmitLine("} // namespace rpc_server");
74+
EmitLine("#endif // " + GetHeaderGuard());
75+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//===-- RPCServerHeaderEmitter.h ----------------------------------------===//
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_RPC_GEN_RPCSERVERHEADEREMITTER_H
10+
#define LLDB_RPC_GEN_RPCSERVERHEADEREMITTER_H
11+
12+
#include "RPCCommon.h"
13+
14+
#include "clang/AST/AST.h"
15+
#include "llvm/Support/ToolOutputFile.h"
16+
17+
using namespace clang;
18+
19+
namespace lldb_rpc_gen {
20+
/// Emit the source code for server-side *.h files.
21+
class RPCServerHeaderEmitter : public FileEmitter {
22+
public:
23+
RPCServerHeaderEmitter(std::unique_ptr<llvm::ToolOutputFile> &&OutputFile)
24+
: FileEmitter(std::move(OutputFile)) {
25+
Begin();
26+
}
27+
28+
~RPCServerHeaderEmitter() { End(); }
29+
30+
void EmitMethod(const Method &method);
31+
32+
private:
33+
void EmitHandleRPCCall();
34+
35+
void EmitConstructor(const std::string &MangledName);
36+
37+
void EmitDestructor(const std::string &MangledName);
38+
39+
std::string GetHeaderGuard();
40+
41+
void Begin();
42+
43+
void End();
44+
};
45+
} // namespace lldb_rpc_gen
46+
47+
#endif // LLDB_RPC_GEN_RPCSERVERHEADEREMITTER_H

0 commit comments

Comments
 (0)