gpt4 book ai didi

c++ - clang - 获取函数体

转载 作者:太空宇宙 更新时间:2023-11-04 14:07:10 24 4
gpt4 key购买 nike

我正在尝试获取函数体的完整代码。

我有以下代码

    bool VisitFunctionDecl(FunctionDecl *f) {
Stmt *FuncBody = f->getBody();
stringstream SSAfter;
SSAfter << f->getBody();
SourceLocation ST = f->getSourceRange().getBegin();
ST = FuncBody->getLocEnd().getLocWithOffset(1);
TheRewriter.InsertText(ST, SSAfter.str(), true, true);
}

我有以下代码示例

int multiplyOrSum (int a, int b, bool t)
{
int c=0;
if (a<=0){
return 0;
}
if (t){
c= a*b;
}
else {
c = a+b;
}
__asm
{
jz _P01
jnz _P01
// _emit 0e9h
_P01:
}

return (c + multiplyOrSum(a-1, b, t)+ loopOne (-1));
}

用我提供的代码解析后。结果是

int multiplyOrSum (int a, int b, bool t)

{
int c=0;
if (a<=0){
return 0;
}
if (t){
c= a*b;
}
else {
c = a+b;
}
__asm
{
jz _P01
jnz _P01
// _emit 0e9h
_P01:
}
return (c + multiplyOrSum(a-1, b, t)+ loopOne (-1));
}
02053B28

我要实现的是

int multiplyOrSum (int a, int b, bool t)

{
int c=0;
if (a<=0){
return 0;
}
if (t){
c= a*b;
}
else {
c = a+b;
}
__asm
{
jz _P01
jnz _P01
// _emit 0e9h
_P01:
}
return (c + multiplyOrSum(a-1, b, t)+ loopOne (-1));
}

{
int c=0;
if (a<=0){
return 0;
}
if (t){
c= a*b;
}
else {
c = a+b;
}
__asm
{
jz _P01
jnz _P01
// _emit 0e9h
_P01:
}
return (c + multiplyOrSum(a-1, b, t)+ loopOne (-1));
}

请指教。非常感谢

编辑:基本上我想做的是复制函数体。

编辑 2:

我尝试了以下代码并对我的原始代码进行了一些编辑

bool VisitFunctionDecl(FunctionDecl *f) {
Stmt *FuncBody = f->getBody();
stringstream SSAfter;
SSAfter << f->getBody();
LangOptions LangOpts;
LangOpts.CPlusPlus = true;
PrintingPolicy Policy(LangOpts);
std::string s;
llvm::raw_string_ostream as(s);
FuncBody->printPretty(as, 0, Policy);
SSAfter << as.str() << "\n";
SourceLocation ST = f->getSourceRange().getBegin();
ST = FuncBody->getLocEnd().getLocWithOffset(1);
TheRewriter.InsertText(ST, SSAfter.str(), true, true);
}

我现在的输出是

int multiplyOrSum (int a, int b, bool t)
{
int c=0;
if (a<=0){
return 0;
}
if (t){
c= a*b;
}
else {
c = a+b;
}
__asm
{
jz _P01
jnz _P01
// _emit 0e9h
_P01:
}
return (c + multiplyOrSum(a-1, b, t)+ loopOne (-1));
}
{
int c = 0;
if (a <= 0) {
return 0;
}
}

我要如何获得函数的整个主体?我现在已经得到了第一部分。但是剩下的呢?

最佳答案

希望对您有所帮助。

string getExprAsString(Expr *expression)
{
// Might return until the beginning of the last token though
SourceRange exprRange = expression->getSourceRange();
string exprString;

int rangeSize = TheRewriter.getRangeSize(exprRange);
if (rangeSize == -1) {
return "";
}

SourceLocation startLoc = exprRange.getBegin();
const char *strStart = TheSourceMgr.getCharacterData(startLoc);

exprString.assign(strStart, rangeSize);

return exprString;
}

bool VisitFunctionDecl(FunctionDecl *f) {
string funcBody = getExprAsString(f->getBody());

SourceLocation ST = f->getBody()->getSourceRange().getBegin();
ST = f->getBody()->getLocEnd().getLocWithOffset(1);

// not sure if it contains the { } so added extra, just in case
TheRewriter.InsertText(ST, "\n{\n" + funcBody + "\n}\n" , true, true);
}

关于c++ - clang - 获取函数体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16412181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com