[cfe-users] Clang-tidy lexer util issue.

Lewis, Cannada via cfe-users cfe-users at lists.llvm.org
Tue Feb 18 09:29:31 PST 2020

I was able to solve my problem by making sure to get the expansion location before calling getPreviousToken.

On Feb 17, 2020, at 5:42 PM, Lewis, Cannada <canlewi at sandia.gov<mailto:canlewi at sandia.gov>> wrote:

I am trying to use the getPreviousToken function from clang-tools-extra<https://github.com/llvm/llvm-project/tree/master/clang-tools-extra>/clang-tidy<https://github.com/llvm/llvm-project/tree/master/clang-tools-extra/clang-tidy>/utils<https://github.com/llvm/llvm-project/tree/master/clang-tools-extra/clang-tidy/utils>/LexerUtils.cpp, but am having issues when the SourceLocation comes from a macro.  Let’s say I have the following functions:

#include <Kokkos_Core.hpp>
#include <string>

// The macros are  defined such that KOKKOS_FUNCTION is empty and KOKKOS_INLINE_FUNCITON = “inline”

void useString(std::string const& s, int i){ }

void useString2(std::string const& s, int i){ }

If I take the FunctionDecl for useString and use getBeginLoc, I will get a location that corresponds to the token void right before useString, I then use getPreviousToken to find the KOKKOS_FUNCTION macro and all is well.

But if I call getBeginLoc on useString2 my location starts off in the macro (I’m not really sure exactly what that means, my guess is that it starts on the inline keyword) and if I use that location for getPreviousToken, I don’t get the ‘}' from the line with useString, instead I get some location from other headers included in Kokkos_Core.hpp.  I don’t think that this is the intended behavior, but I don’t have a minimal working example at the moment. I did track it down to  where getPreviousToken calls getWithOffset(-1) on the source location and it is that new location which is the unexpected location.

Any help would be appreciated.

-Cannada Lewis

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20200218/c50eac02/attachment.html>

More information about the cfe-users mailing list