如何比较clang中的两个源位置?

How can I compare two source locations in clang?

本文关键字:两个 位置 何比较 比较 clang      更新时间:2023-10-16

这似乎更像是一个c++问题,而不是Clang问题…

我必须使用c++来编写OCLint(静态代码分析器)规则。

我希望比较Clang库中具有"SourceLocation"类型的两个对象。

这个类型提供了关于位置的信息(基本上是一行&代码中对象(语句、声明等)的列。

基本上,我想知道语句A是在语句b之前开始和结束,还是在语句b之后。

在伪代码中,这意味着我想从 中获得一个布尔值:

(stmt_A->getLocBegin() <例如,stmt_B->getLocBegin())。当然,这不能编译,因为"<"操作符没有在两个类型为"SourceLocation"的对象之间定义。

我在Clang文档中找到了一个方法,但是,由于我不经常使用c++,我没有找到使用它的方法,下面是这个方法:

http://clang.llvm.org/doxygen/classclang_1_1BeforeThanCompare_3_01SourceLocation_01_4.html

clang::BeforeThanCompare<SourceLocation>::BeforeThanCompare (SourceManager &SM)

bool clang::BeforeThanCompare< SourceLocation >::operator()(SourceLocation LHS, SourceLocation RHS)  const [inline]

我不知道如何使用SourceManager,或者简单地如何获得上面的这个布尔值

最后的代码展示了如何在Clang库中使用SourceManager,以及如何比较两个SourceLocation:

// Declaration of a SourceManager
SourceManager & loc_SM = _carrier->getSourceManager();
// Declaration of an object BeforeThanCompare<SourceLocation>
BeforeThanCompare<SourceLocation> isBefore(loc_SM); SourceLocation stmt_A, stmt_B;
// Get whether stmt_A is before or after Stmt_B 
bool A_before_B = isBefore(stmt_A,stmt_B);