简体   繁体   English

如何检查迭代器的相等性?

[英]How to check equality of iterator?

In my llvm code, I try to check if the iterator InsertPos is pointing to the last instruction of a basic block, with the following code. 在我的llvm代码中,我尝试使用以下代码检查迭代器InsertPos是否指向基本块的最后一条指令。

  BasicBlock::iterator InsertPos = BB->begin();
  LLVMContext &Context = BB->getContext();

  while ( !( isa<CallInst>(InsertPos) || 
      ( InsertPos == BB->getTerminator() ) ) ) // <-- Error here
    ++InsertPos;

However, clang gives me the following error. 但是, clang给我以下错误。

Basic2.cpp:82:54: error: use of overloaded operator '==' is ambiguous (with operand types 'BasicBlock::iterator' (aka 'ilist_iterator<llvm::Instruction>')
      and 'llvm::TerminatorInst *')
  while ( !( isa<CallInst>(InsertPos) || ( InsertPos == BB->getTerminator() ) ) ) 
                                           ~~~~~~~~~ ^  ~~~~~~~~~~~~~~~~~~~
/home/hmushtaq/llvm/include/llvm/ADT/ilist.h:204:8: note: candidate function
  bool operator==(const ilist_iterator &RHS) const {
       ^
Basic2.cpp:82:54: note: built-in candidate operator==(class llvm::Instruction *, class llvm::Instruction *)
  while ( !( isa<CallInst>(InsertPos) || ( InsertPos == BB->getTerminator() ) ) ) 
                                                     ^
    Basic2.cpp:82:54: note: built-in candidate operator==(restrict class llvm::Instruction    *, restrict class llvm::Instruction *)
    Basic2.cpp:82:54: note: built-in candidate operator==(const restrict class llvm::Instruction *, const restrict class llvm::Instruction *)
    Basic2.cpp:82:54: note: built-in candidate operator==(volatile class llvm::Instruction *, volatile class llvm::Instruction *)
    Basic2.cpp:82:54: note: built-in candidate operator==(const volatile class llvm::Instruction *, const volatile class llvm::Instruction *)
    Basic2.cpp:82:54: note: built-in candidate operator==(volatile restrict class llvm::Instruction *, volatile restrict class llvm::Instruction *)
    Basic2.cpp:82:54: note: built-in candidate operator==(const class llvm::Instruction *, const class llvm::Instruction *)
    Basic2.cpp:82:54: note: built-in candidate operator==(const volatile restrict class llvm::Instruction *, const volatile restrict class llvm::Instruction *)

What is the right way of doing this? 正确的做法是什么?

BB->getTerminator() returns a pointer ( TerminatorInst * ), while InsertPos is an iterator ( BasicBlock::iterator ). BB->getTerminator()返回一个指针( TerminatorInst * ),而InsertPos是一个迭代器( BasicBlock::iterator )。 The types do no match. 类型不匹配。

Maybe you could try 也许你可以尝试

&*InsertPos == BB->getTerminator()

You could also check if an Instruction is a terminator with the isTerminator() method . 您还可以使用isTerminator()方法检查指令是否为终止符。

InsertPos->isTerminator()

I solved it like this. 我这样解决了。

BasicBlock::iterator InsertPos = BB->begin();
BasicBlock::iterator TermPos = BB->getTerminator(); // <-- Added this variable

while ( !( isa<CallInst>(InsertPos) || ( InsertPos == TermPos ) ) ) 
  ++InsertPos;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM