[PATCH] D148457: [clangd] Support macro evaluation on hover

Younan Zhang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 28 04:15:16 PDT 2023


zyounan added a comment.

Oops, an inline comment disappears accidentally.

>   #define PLUS_TWO + 2
>   int x = 40 PLUS_TW^O;  // huh, the value of "+ 2" is "42"?

The reason is we're getting a `SelectionNode` with a wider range. Here is the layout of the tree:

  txt
  Built selection tree
   TranslationUnitDecl 
     FunctionDecl void check()
       CompoundStmt { …
        .BinaryOperator 40 + 2
          *IntegerLiteral 2

The dot prior to AST node kind indicates partial selection and the asterisk indicates complete selection according to SelectionTree::print <https://searchfox.org/llvm/rev/be9c91843bab5bb46574c27836bfcd9ad6fc9ef5/clang-tools-extra/clangd/Selection.cpp#1009>.

By invoking `Tree.commonAncestor()` it would stop at `BinaryOperator` despite the fact that we're expecting `IntegerLiteral`, that represents the number being accordance with the macro expansion.

In order to address such case, I think we could go down the `SelectionTree` a little further and start evaluating at the node with complete selection.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D148457/new/

https://reviews.llvm.org/D148457



More information about the cfe-commits mailing list