gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:43:43 25 4
gpt4 key购买 nike

这似乎更像是一个 C++ 问题而不是 Clang 问题...

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

我想比较 Clang 库中类型为“SourceLocation”的两个对象。

此类型提供有关代码中对象(语句、声明等)的位置(基本上是行和列)的信息。

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

在伪代码中,这意味着我想从中获取一个 bool 值:

( 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,或者只是不知道如何获取上面的 bool 值。

最佳答案

这是展示如何在 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);

关于c++ - 如何比较 clang 中的两个源位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24691332/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com