Page MenuHomePhabricator

Please use GitHub pull requests for new patches. Avoid migrating existing patches. Phabricator shutdown timeline

[clang-tidy] Allow access to the SourceManager in clang-tidy checks

Authored by nicovank on Jun 15 2022, 11:20 AM.



I am writing a prototype check that needs source file and line numbers to map code to external data. As far as I know, the only way to achieve that is to expose the SourceManager which is so far only used to emit diagnostics.

Test Plan:
On a blank new check, MatchedDecl->getBeginLoc().dump(Context->getSourceManager()); does properly show the matched declaration's origin filename and line number.

Diff Detail

Event Timeline

nicovank created this revision.Jun 15 2022, 11:20 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2022, 11:20 AM
nicovank published this revision for review.Jun 15 2022, 11:33 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2022, 11:33 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript

Could you provide more context? There seems to be plenty of ways to access the SourceManager from a ClangTidy check, for example, see clang-tools-extra/clang-tidy/readability/ElseAfterReturnCheck.cpp

nicovank planned changes to this revision.Jun 15 2022, 11:53 AM

Thanks! Looks like registerPPCallbacks might be what I am looking for, I was not aware of it. I will mark this as planning changes temporarily, then abandon if solved.

nicovank abandoned this revision.Jun 16 2022, 8:40 AM

Looks like registerPPCallbacks / Result.Context->getSourceManager() gives me the information I need. Thank you @gribozavr2 for your help!