llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.81k stars 11.91k forks source link

[clang-tidy] Print instantiation stack #107381

Open carlosgalvezp opened 1 month ago

carlosgalvezp commented 1 month ago

Often times we have core, templated library functions that are used across the project. Sometimes these functions are used incorrectly by the clients, leading to clang-tidy warnings in the library code. It is however not possible to quickly pinpoint where in the client code the mistakes were made.

This could be solved by printing the instantiation stack of where the error comes from, similar to how it's done for Clang compiler errors.

I opened a discussion here, with no comments so far: https://discourse.llvm.org/t/print-template-instantiation-stack-on-warnings/81046

llvmbot commented 1 month ago

@llvm/issue-subscribers-clang-tidy

Author: Carlos Galvez (carlosgalvezp)

Often times we have core, templated library functions that are used across the project. Sometimes these functions are used incorrectly by the clients, leading to clang-tidy warnings in the library code. It is however not possible to quickly pinpoint where in the client code the mistakes were made. This could be solved by printing the instantiation stack of where the error comes from, similar to how it's done for Clang compiler errors. I opened a discussion here, with no comments so far: https://discourse.llvm.org/t/print-template-instantiation-stack-on-warnings/81046
firewave commented 1 month ago

See #73270 for an example. That ticket is closed since it also contained a false positive but the actual issue I posted it for was not fixed. I haven't gotten around to opening it again but this new ticket will do now.

firewave commented 1 month ago

That example actually shows the issue in a self-contained file. So it does not just happen with shared code.

5chmidti commented 1 month ago

Sounds like a good idea, the reason why and where something happened is not always evident from the diagnostic alone.

carlosgalvezp commented 1 month ago

I believe in Clang this is achieved by Sema::PrintInstantiationStack(). I'm not sure if/how it would be possible to re-use or port that over to clang-tidy. @AaronBallman do you have some insights or pointers that we could look into? Do you think this is a solvable problem?

AaronBallman commented 1 month ago

I don't think it's solvable in the short-term; in the long term, we may be able to get there. The problem is that we need semantic information to print the correct instantiation stack and clang-tidy purposefully has no access to Sema because tidy is expected to work on completed ASTs. I don't think we want to expose all of Sema because that's ripe for accidental misuse (it's pretty tightly coupled with expectations from the parser and so it's easy to hit assertions or other misbehaviors), but I think we may eventually need to expose some amount of Sema because of the C++ reflection feature (where evaluating a constant expression may have side effects that include introducing new AST nodes which require semantic analysis). However, how that works is very much an open question we don't have answers for.