簡體   English   中英

如何將可變數量的參數傳遞給LLVM opt pass?

[英]How do I pass a variable number of arguments to a 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);

但是,如果我現在調用選擇:

foo@foo-Ubuntu:~/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

,然后我得到錯誤,opt將接受最多2個位置參數。

我究竟做錯了什么?

我認為問題是opt已經在解析自己的參數,並且已經將bitcode文件作為位置參數進行處理,因此具有多個位置參數會產生歧義。

該文檔解釋了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::OneOrMore Files2 of Files2選項到cl::ZeroOrMore Files2 ,你將得到一個錯誤

$ 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參數選項,並執行以下操作:

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

正如@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列表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM