2017-04-20 104 views
3

我想将可变数量的参数传递给我的LLVM opt pass。如何将可变数量的参数传递给LLVM opt pass?

要做到这一点,我做这样的事情:

static cl::list<std::string> Files(cl::Positional, cl::OneOrMore); 
static cl::list<std::string> Libraries("l", cl::ZeroOrMore); 

但是,如果我现在调用选择,如:

[email protected]:~/llvm-ir-obfuscation$ opt -load cmake-build-debug/water/libMapInstWMPass.so -mapiWM programs/ll/sum100.ll -S 2 3 4 -o foo.ll 
opt: Too many positional arguments specified! 
Can specify at most 2 positional arguments: See: opt -help 

,然后我得到的是选择将接受最多2个位置参数错误。

我在做什么错?

+0

S标志不是传递的选项,它是一个选项,使其自己使其输出LLVM程序集。 – Shuzheng

+0

我该如何做第一件作品(定位)? – Shuzheng

+0

你能解释'-mapiWM'选项吗? – hailinzeng

回答

3

我认为问题在于opt已经解析了它自己的参数,并且已经将位码文件作为位置参数进行处理,因此具有多个位置参数会产生不明确性。

该文档解释了API,就像它在独立应用程序中使用一样。因此,举例来说,如果你做这样的事情:

int main(int argc, char *argv[]) { 
    cl::list<std::string> Files(cl::Positional, cl::OneOrMore); 
    cl::list<std::string> Files2(cl::Positional, cl::OneOrMore); 
    cl::list<std::string> Libraries("l", cl::ZeroOrMore); 
    cl::ParseCommandLineOptions(argc, argv); 

    for(auto &e : Libraries) outs() << e << "\n"; 
    outs() << "....\n"; 
    for(auto &e : Files) outs() << e << "\n"; 
    outs() << "....\n"; 
    for(auto &e : Files2) outs() << e << "\n"; 
    outs() << "....\n"; 
} 

你得到的东西是这样的:

$ foo -l one two three four five six 

one 
.... 
two 
three 
four 
five 
.... 
six 
.... 

现在,如果你身边两个位置参数的定义交换,甚至可以改变的cl::OneOrMoreFiles2选项cl::ZeroOrMore,你会得到一个错误

$ option: error - option can never match, because another positional argument will match an unbounded number of values, and this option does not require a value! 

个人而言,当我使用opt我放弃了positiontal ARG ument选项,做这样的事情:

cl::list<std::string> Lists("lists", cl::desc("Specify names"), cl::OneOrMore); 

,让我做到这一点:

opt -load ./fooPass.so -foo -o out.bc -lists one ./in.bc -lists two 

并遍历std::string列表以同样的方式获得:

one 
two 
+0

谢谢兄弟!作为最后一个问题,在我将您的答案标记为已接受之前:您是否知道如何查看在终端上输出的通行证的选项描述?我如何告诉'opt'列出,例如,'cl :: desc(“指定姓名”)为我的通行证? – Shuzheng

+0

不是'-help'为你做的吗?如[在此]所述(http://llvm.org/docs/CommandLine.html#cl-desc) – compor

+0

不,它只是列出了“opt”的帮助菜单 - 而不是传递本身。 – Shuzheng

2

正如@ compor表示,这可能与将opt和你自己的传球交织在一起的论点有关。 CommandLine库主要是为LLVM框架内的独立应用程序编写的。

但是,可以这样做:

static cl::list<std::string> Args1("args1", cl::Positional, cl::CommaSeparated); 
static cl::list<std::string> Args2("args2", cl::ZeroOrMore); 

这具有的优点是,可以使用任一逗号命令行,例如,arg1,arg2,...或通过使用所述识别符-args1 arg1 arg2 ...输入多个参数;并将它们插入到Args1列表中。如果您只是在命令行上提供单个位置参数arg,则Args1将只包含此参数。

此外,您可以在命令行上指定-args2 arg,无论您身在何处(命名为非定位选项)。这些将进入Args2列表。

相关问题