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

Nicolas van Kempen via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jun 15 11:33:45 PDT 2022


nicovank created this revision.
Herald added subscribers: carlosgalvezp, xazax.hun.
Herald added a project: All.
nicovank added reviewers: alexfh, aaron.ballman, njames93.
nicovank published this revision for review.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D127886

Files:
  clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
  clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h


Index: clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
+++ clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h
@@ -121,6 +121,9 @@
   /// This is called from the \c ClangTidyCheck base class.
   void setSourceManager(SourceManager *SourceMgr);
 
+  /// Gets the \c SourceManager from the used \c DiagnosticsEngine.
+  SourceManager &getSourceManager();
+
   /// Should be called when starting to process new translation unit.
   void setCurrentFile(StringRef File);
 
Index: clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
===================================================================
--- clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
+++ clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp
@@ -221,6 +221,10 @@
   DiagEngine->setSourceManager(SourceMgr);
 }
 
+SourceManager &ClangTidyContext::getSourceManager() {
+  return DiagEngine->getSourceManager();
+}
+
 void ClangTidyContext::setCurrentFile(StringRef File) {
   CurrentFile = std::string(File);
   CurrentOptions = getOptionsForFile(CurrentFile);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D127886.437262.patch
Type: text/x-patch
Size: 1200 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220615/b9c2de87/attachment.bin>


More information about the cfe-commits mailing list