Skip to content

[lldb][RPC] Upstream RPC server interface emitters #138032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This ia a basic header file used to check that the server-side emitter
// for rpc-gen emits an expected set of includes in a generated source file.
#ifndef LLDB_API_SBRPC_CHECKBASICINCLUDE_H
#define LLDB_API_SBRPC_CHECKBASICINCLUDE_H

#endif // LLDB_API_SBRPC_CHECKBASICINCLUDE_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef LLDB_API_SBRPC_CHECKCONSTCHARPOINTER_H
#define LLDB_API_SBRPC_CHECKCONSTCHARPOINTER_H

namespace lldb {
class LLDB_API SBRPC_CHECKCONSTCHARPOINTER {
public:
// const char * parameters must decoded as rpc_common::ConstCharPointer in server side
// source files.
int CheckConstCharPointer(char *string);

}; // class SBRPC_CHECKCONSTCHARPOINTER
} // namespace lldb

#endif // LLDB_API_SBRPC_CHECKCONSTCHARPOINTER_H
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Disabling until the lldb-rpc-gen tool lands.
UNSUPPORTED: system-windows, system-linux, system-darwin
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UNSUPPORTED: * should disable it everywhere.

Saves someone on FreeBSD getting an unpleasant surprise.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do that for the rest of the tests as well.

RUN: mkdir -p %t/server
RUN: mkdir -p %t/lib
RUN: %lldb-rpc-gen --output-dir=%t %S/../../Inputs/CheckBasicIncludesEmit.h

RUN: cat %t/lib/CheckBasicIncludesEmit.cpp | FileCheck %s

# All server-side source files must have these includes at the top of their files.
CHECK: #include "RPCUserServer.h"
CHECK: #include "SBAPI.h"
CHECK: #include <lldb-rpc/common/RPCArgument.h>
CHECK: #include <lldb-rpc/common/RPCCommon.h>
CHECK: #include <lldb-rpc/common/RPCFunction.h>
CHECK: #include <lldb/API/LLDB.h>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Disabling until the lldb-rpc-gen tool lands.
UNSUPPORTED: system-windows, system-linux, system-darwin
RUN: mkdir -p %t/server
RUN: mkdir -p %t/lib
RUN: %lldb-rpc-gen --output-dir=%t %S/../../Inputs/CheckConstCharPointer.h

RUN: cat %t/lib/CheckConstCharPointer.cpp | FileCheck %s

# const char * pointers must be decoded as rpc_common::ConstCharPointer objects
# in server side source files.
CHECK: rpc_common::ConstCharPointer string
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
//===-- RPCServerHeaderEmitter.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 "RPCServerHeaderEmitter.h"
#include "RPCCommon.h"

#include "clang/AST/AST.h"
#include "clang/AST/Mangle.h"
#include "clang/Frontend/CompilerInstance.h"

#include "llvm/ADT/StringRef.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/raw_ostream.h"

using namespace clang;
using namespace lldb_rpc_gen;

void RPCServerHeaderEmitter::EmitMethod(const Method &method) {
// We'll be using the mangled name in order to disambiguate
// overloaded methods.
const std::string &MangledName = method.MangledName;

EmitLine("class " + MangledName +
" : public rpc_common::RPCFunctionInstance {");
EmitLine("public:");
IndentLevel++;
EmitConstructor(MangledName);
EmitDestructor(MangledName);
EmitHandleRPCCall();
IndentLevel--;
EmitLine("};");
}

void RPCServerHeaderEmitter::EmitHandleRPCCall() {
EmitLine("bool HandleRPCCall(rpc_common::Connection &connection, "
"rpc_common::RPCStream &send, rpc_common::RPCStream &response) "
"override;");
}

void RPCServerHeaderEmitter::EmitConstructor(const std::string &MangledName) {
EmitLine(MangledName + "() : RPCFunctionInstance(\"" + MangledName +
"\") {}");
}

void RPCServerHeaderEmitter::EmitDestructor(const std::string &MangledName) {
EmitLine("~" + MangledName + "() override {}");
}

std::string RPCServerHeaderEmitter::GetHeaderGuard() {
const std::string UpperFilenameNoExt =
llvm::sys::path::stem(
llvm::sys::path::filename(OutputFile->getFilename()))
.upper();
return "GENERATED_LLDB_RPC_SERVER_" + UpperFilenameNoExt + "_H";
}

void RPCServerHeaderEmitter::Begin() {
const std::string HeaderGuard = GetHeaderGuard();
EmitLine("#ifndef " + HeaderGuard);
EmitLine("#define " + HeaderGuard);
EmitLine("");
EmitLine("#include <lldb-rpc/common/RPCFunction.h>");
EmitLine("");
EmitLine("namespace rpc_server {");
}

void RPCServerHeaderEmitter::End() {
EmitLine("} // namespace rpc_server");
EmitLine("#endif // " + GetHeaderGuard());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//===-- RPCServerHeaderEmitter.h ----------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef LLDB_RPC_GEN_RPCSERVERHEADEREMITTER_H
#define LLDB_RPC_GEN_RPCSERVERHEADEREMITTER_H

#include "RPCCommon.h"

#include "clang/AST/AST.h"
#include "llvm/Support/ToolOutputFile.h"

using namespace clang;

namespace lldb_rpc_gen {
/// Emit the source code for server-side *.h files.
class RPCServerHeaderEmitter : public FileEmitter {
public:
RPCServerHeaderEmitter(std::unique_ptr<llvm::ToolOutputFile> &&OutputFile)
: FileEmitter(std::move(OutputFile)) {
Begin();
}

~RPCServerHeaderEmitter() { End(); }

void EmitMethod(const Method &method);

private:
void EmitHandleRPCCall();

void EmitConstructor(const std::string &MangledName);

void EmitDestructor(const std::string &MangledName);

std::string GetHeaderGuard();

void Begin();

void End();
};
} // namespace lldb_rpc_gen

#endif // LLDB_RPC_GEN_RPCSERVERHEADEREMITTER_H
Loading
Loading