【问题标题】:How can I compare two source locations in clang?如何比较 clang 中的两个源位置?
【发布时间】:2014-07-11 06:26:32
【问题描述】:

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

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

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

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

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

在伪代码中,这意味着我想从以下位置获取布尔值:

( stmt_A->getLocBegin() getLocBegin() ),例如。当然,这不会编译,因为“

我在 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,或者根本不知道如何获取上面的这个布尔值。

【问题讨论】:

  • 不确定您的应用程序是什么,但很好奇您有一个“SourceLocation”类型。一旦我创建了这样一个东西并称之为codeplace--也许这会很有趣:hoist.hostilefork.com
  • @HostileFork 是clang::SourceLocation,是clang的一部分。
  • @Agnew 谢谢,我去查一下,没听说过!
  • 要从BeforeThanCompare&lt;SourceLocation&gt; 调用执行比较的operator(),您需要使用SourceManager 对象对其进行初始化。根据其docs,该“类处理将源文件加载和缓存到内存中”。我假设您的项目中必须已经有这样一个对象,所以只需使用它来构建比较器。
  • 事实上,我的代码中没有 SourceManager 对象(以前的代码中也没有),因为我不知道如何使用它。我只需要申报一个吗?或者是否有某种程序将其与我的 SourceLocation 链接?

标签: c++ c++11 clang oclint


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-08-13
    • 2013-08-11
    • 2011-11-20
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 2019-03-25
    • 1970-01-01
    相关资源
    最近更新 更多