【问题标题】:Replace expression with macro using clang AST使用 clang AST 用宏替换表达式
【发布时间】:2019-05-06 21:59:50
【问题描述】:

我希望在 clang ast 匹配器的帮助下更改以下代码。

foo(NUM << DEV_SHIFT | DEVICE);

foo(ADDR(NUM, DEVICE));

#define ADDR(a, b) (((a) << NUM_SHIFT) | (b))

我有以下 AST 匹配器,似乎可以很好地识别代码。

Finder->addMatcher(
       callExpr(hasArgument(                                                                                                                                                             
           0, binaryOperator(hasOperatorName("|"),
                             hasLHS(ignoringParenImpCasts(
                                 binaryOperator(hasOperatorName("<<")))))
                  .bind("replaceWithMacro"))),
       this);

但是我在理解如何写支票和翻译时遇到了问题。我目前坚持使用此代码:

void FirstCheckCheck::check(const MatchFinder::MatchResult &Result) {
  // FIXME: Add callback implementation.
  if (const auto MatchedDecl =
          Result.Nodes.getNodeAs<CallExpr>("replaceWithMacro")) {
    diag(MatchedDecl->getExprLoc(), "CallExp");
  } else if (const auto MatchedDecl =
                 Result.Nodes.getNodeAs<Expr>("replaceWithMacro")) {
    diag(MatchedDecl->getExprLoc(), "Expr");
    diag(MatchedDecl->getBeginLoc(), "BeginLOC");
    diag(MatchedDecl->getEndLoc(), "EndLOC");
  }

我不知道如何将这两个变量提取为字符串。 我正在查看 Expr 类 (http://clang.llvm.org/doxygen/classclang_1_1Expr.html) 的文档,但找不到有用的东西。

如果有人能指出我正确的方向,将不胜感激。

添加编辑。

【问题讨论】:

  • 始终在扩展中的宏参数名称周围加上括号(在处理算术或逻辑表达式时)。因此:#define ADDR(a, b) (((a) &lt;&lt; NUM_SHIFT) | (b))。它可能不是您的问题的一个因素,但它是基本的 C 代码卫生。 (规则有例外,例如在处理字符串时,但在明智的情况下遵守准则 - 这是一个明智的地方。)
  • 在我看来,您正在尝试从 Expr 中提取两个变量。为什么不添加两个额外的绑定?您已经制定了匹配器,只需修改一下以绑定“

标签: c abstract-syntax-tree clang-ast-matchers


【解决方案1】:

这是我非常满意的解决方案。它基于来自 cmets 的 boq 的建议。

//===--- FirstCheckCheck.cpp - clang-tidy ---------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "FirstCheckCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"

using namespace clang::ast_matchers;

namespace clang {
namespace tidy {
namespace misc {

void FirstCheckCheck::registerMatchers(MatchFinder *Finder) {
  // FIXME: Add matchers.
  //
  Finder->addMatcher(
      callExpr(hasArgument(
          1,
          binaryOperator(
              hasOperatorName("|"),
              hasRHS(ignoringImplicit(
                  anyOf(declRefExpr().bind("moduleNum"), integerLiteral().bind(
                      "moduleNum")))),
              hasLHS(ignoringParens(
                  binaryOperator(hasOperatorName("<<"),
                                 hasLHS(ignoringImplicit(anyOf(
                                     declRefExpr().bind("deviceNum"),
                                     integerLiteral().bind("deviceNum"))))))))
              .bind("replaceWithMacro"))),
      this);
}

void FirstCheckCheck::check(const MatchFinder::MatchResult &Result) {
  // FIXME: Add callback implementation.
    std::string deviceNumString;
    std::string moduleNumString;
    std::string ReplacementText;

  if (const auto MatchedDecl =
          Result.Nodes.getNodeAs<Expr>("deviceNum")) {

    const LangOptions &Opts = getLangOpts();

    /* get device string */
    deviceNumString = Lexer::getSourceText(
        CharSourceRange::getTokenRange(MatchedDecl->getSourceRange()),
        *Result.SourceManager, Opts);
  }
  /* ((uint16_t)(deviceNum << 8 | moduleNum)) */
  if (const auto MatchedDecl =
          Result.Nodes.getNodeAs<Expr>("moduleNum")) {

    const LangOptions &Opts = getLangOpts();

    moduleNumString = Lexer::getSourceText(
        CharSourceRange::getTokenRange(MatchedDecl->getSourceRange()),
        *Result.SourceManager, Opts);
  }
  if (const auto MatchedDecl =
                 Result.Nodes.getNodeAs<Expr>("replaceWithMacro")) {
    const LangOptions &Opts = getLangOpts();

    ReplacementText = Lexer::getSourceText(
        CharSourceRange::getTokenRange(MatchedDecl->getSourceRange()),
        *Result.SourceManager, Opts);

    std::string replacementString =
        "ADDR(" + deviceNumString + ", " + moduleNumString + ")";

    FixItHint Hint = FixItHint::CreateReplacement(
        MatchedDecl->getSourceRange(), replacementString);

    diag(MatchedDecl->getBeginLoc(), "Replace with ADDR() macro") << Hint;
  }

  /* diag(MatchedDecl->getLocation(), "insert 'awesome'", DiagnosticIDs::Note)
   */
  /*     << FixItHint::CreateInsertion(MatchedDecl->getLocation(), "awesome_");
   */
}

} // namespace misc
} // namespace tidy
} // namespace clang

【讨论】:

    猜你喜欢
    • 2019-05-11
    • 2020-06-21
    • 2021-04-28
    • 2019-06-17
    • 1970-01-01
    • 1970-01-01
    • 2014-06-01
    • 2011-07-05
    • 1970-01-01
    相关资源
    最近更新 更多