如何在LLVM IR中获取字符串文字的值
How to get the value of a string literal in LLVM IR?
我是LLVM的新手。我正在尝试编写一个基本的Pass,当它被赋予中间表示时,它将检查printf
调用的参数
如果格式字符串不是字符串文字,那么我当然不能检查它。但通常情况下,它是。
我试图检查的样品IR是:
@.str = private unnamed_addr constant [7 x i8] c"Hi %u A 0", align 1
define i32 @main() nounwind {
entry:
%retval = alloca i32, align 4
store i32 0, i32* %retval
%call = call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([7 x i8]* @.str, i32 0, i32 0), i32 1)
ret i32 0
}
declare i32 @printf(i8*, ...)
我发现了之前存在的名为ExternalFunctionsPassedConstants
的通行证,它似乎很相关:
struct ExternalFunctionsPassedConstants : public ModulePass {
static char ID; // Pass ID, replacement for typeid
ExternalFunctionsPassedConstants() : ModulePass(ID) {}
virtual bool runOnModule(Module &M) {
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) {
if (!I->isDeclaration()) continue;
bool PrintedFn = false;
for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
UI != E; ++UI) {
Instruction *User = dyn_cast<Instruction>(*UI);
if (!User) continue;
CallSite CS(cast<Value>(User));
if (!CS) continue;
...
所以我添加了代码:
if (I->getName() == "printf") {
errs() << "printf() arg0 type: "
<< CS.getArgument(0)->getType()->getTypeID() << "n";
}
到目前为止,很好——我看到类型ID是14,这意味着它是一个PointerTyID
。
但是现在,我如何获得作为参数传递的字符串文本的内容,以便根据实际给定的数量验证预期参数的数量?
CS.getArgument(0)
表示GetElementPtrConstantExpr
i8* getelementptr inbounds ([7 x i8]* @.str, i32 0, i32 0)
,它是一个User对象。您想要的字符串(即@.str)是GetElementPtrConstantExpr的第一个操作数。
因此,您可以通过获取字符串文字
CS.getArgument(0).getOperand(0)
但是,我还没有测试过这个代码。如果有任何错误,请告诉我。
相关文章:
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 如何使用字符串文字作为宏参数
- 有没有办法从非C/C++文件中读取C++原始字符串文字的内容
- 构造<int>具有 2 个字符串文字的向量
- 初始化或分配空字符串文字到指向 C 中的 char 的指针或指向 C++ 中 const char 的指针的原因是什么
- 方便地对C++中的所有字符串文字进行模糊处理
- 比较 std::string 和 C 样式字符串文字
- 如何从char16_t字符串文字中读取双精度?
- 如果我在块中编写字符串文字,是否会从数据部分复制整个字符串数据?
- C++ 不能在cout中使用向量和字符串文字
- 为什么必须将 const 添加到 constexpr 中才能进行字符串文字声明?
- C++:Unicode 字符串文字的可移植性
- 返回空字符串文字 VS. 返回空点 - 它们是一样的吗?
- 确保字符指针始终指向相同的字符串文字
- 如何按照 Google C++风格指南连接字符串文字?
- 通过 constexpr 中的 'const char *' glvalue 访问字符串文字的值会出错
- 字符串文字到 char 数组的转换如何在C++中实际工作
- 另一个字符串文字,UDL 迷宫
- strlen 是否针对字符串文字进行了优化?
- 用户定义的字符串文字为字符串null终止