[cfe-dev] clang checker is not called back on goto statements

Sheng Chen via cfe-dev cfe-dev at lists.llvm.org
Sun Aug 27 15:13:58 PDT 2017


Hi everyone,

I have just got started with clang development. My first project would
extract the return value from each path. I can get the return value quite
easily, but have some problem with path representation. For example, given
the following very simple function,

1:   int jump(int x){
2: int result = x;

4: if (x < 5) {
result = -1;
6: goto out;
}

9: result = 0;

out:
12: return result;
}

I would like to return two paths and their corresponding return values. I
use line numbers to represent each path.

Path                      Return value
----------                 --------------------
L6, L12                 -1
L12                       0

The problem I am having now is that I am not able to get line numbers of
goto statements, although my checker writes callback functions for goto
statements. My code is as follows. I have tried to add callback functions
for PreStmt on both GotoStmt and IndirectGotoStmt and also PostStmt on
them, but never got line numbers for goto statements. I can get line
numbers for return statements with the following code.

Does anybody have any idea about why this happens?

Thank you for your help.
Sheng

namespace {
class CheckReturnValue : public Checker< check::PreStmt<ReturnStmt>,
check::PreStmt<IndirectGotoStmt>,
check::PostStmt<GotoStmt>> {

public:
  void checkPreStmt(const ReturnStmt *DS, CheckerContext &C) const;
  void checkPreStmt(const IndirectGotoStmt *DS, CheckerContext &C) const;
  void checkPostStmt(const GotoStmt *DS, CheckerContext &C) const;
private:
  static bool isNegative(CheckerContext &C, const Expr *E);
};
} // end anonymous namespace

void CheckReturnValue::checkPostStmt(const GotoStmt *DS, CheckerContext &C)
const{

  llvm :: outs() << "Goto statement\t";
  DS -> getLocStart().print(llvm::outs(),C.getSourceManager()) ;

}

void CheckReturnValue::checkPreStmt(const IndirectGotoStmt *DS,
CheckerContext &C) const {
  llvm :: outs() << "Indirect goto statement\t";
  DS -> getLocStart().print(llvm::outs(),C.getSourceManager()) ;

}

void CheckReturnValue::checkPreStmt(const ReturnStmt *DS, CheckerContext
&C) const {
  const Expr* retExp = DS -> getRetValue() ;
  retExp -> printPretty(llvm::outs(), NULL, PrintingPolicy(LangOptions())) ;

  llvm :: outs() << "\tLocation\t";
  DS -> getLocStart().print(llvm::outs(),C.getSourceManager()) ;
}



Dr. Sheng Chen
Assistant Professor
The Center for Advanced Computer Studies
UL Lafayette
http://www.ucs.louisiana.edu/~sxc2311/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20170827/ee00aab2/attachment.html>


More information about the cfe-dev mailing list