[Syntax] Fix macro-arg handling in TokenBuffer::spelledForExpanded
authorSam McCall <sam.mccall@gmail.com>
Mon, 26 Sep 2022 01:22:09 +0000 (03:22 +0200)
committerSam McCall <sam.mccall@gmail.com>
Wed, 5 Oct 2022 16:04:39 +0000 (18:04 +0200)
commit67268ee11c220b1dfdf84afb10a12371c5ae6400
tree2f075ecdf68f9a437727aad5fbf21a72b9bcd034
parentbc8f007fe045139dde244b2374b851f93cc7ed3c
[Syntax] Fix macro-arg handling in TokenBuffer::spelledForExpanded

A few cases were not handled correctly. Notably:
  #define ID(X) X
  #define HIDE a ID(b)
  HIDE
spelledForExpanded() would claim HIDE is an equivalent range of the 'b' it
contains, despite the fact that HIDE also covers 'a'.

While trying to fix this bug, I found findCommonRangeForMacroArgs hard
to understand (both the implementation and how it's used in spelledForExpanded).
It relies on details of the SourceLocation graph that are IMO fairly obscure.
So I've added/revised quite a lot of comments and made some naming tweaks.

Fixes https://github.com/clangd/clangd/issues/1289

Differential Revision: https://reviews.llvm.org/D134618
clang/lib/Tooling/Syntax/Tokens.cpp
clang/unittests/Tooling/Syntax/TokensTest.cpp