[PATCH] D12411: [PATCH] Expose AST language options to checkers

Aaron Ballman via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 27 12:22:22 PDT 2015


aaron.ballman created this revision.
aaron.ballman added reviewers: alexfh, djasper.
aaron.ballman added a subscriber: cfe-commits.

Some of the checkers need to know what language options are enabled in order to behave sensibly. For instance, the modernize-use-nullptr checker should not suggest use of nullptr when the file being checked is a C file, not a C++ file.

This patch exposes the language options to checkers, and corrects the bug with modernize-use-nullptr by only registering the matcher if C++11 language features are enabled.

~Aaron

http://reviews.llvm.org/D12411

Files:
  clang-tidy/ClangTidy.h
  clang-tidy/ClangTidyDiagnosticConsumer.cpp
  clang-tidy/ClangTidyDiagnosticConsumer.h
  clang-tidy/modernize/UseNullptrCheck.cpp
  test/clang-tidy/modernize-use-nullptr.c

Index: test/clang-tidy/modernize-use-nullptr.c
===================================================================
--- test/clang-tidy/modernize-use-nullptr.c
+++ test/clang-tidy/modernize-use-nullptr.c
@@ -0,0 +1,10 @@
+// RUN: clang-tidy %s -checks=-*,modernize-use-nullptr -- | count 0
+
+// Note: this test expects no diagnostics, but FileCheck cannot handle that,
+// hence the use of | count 0.
+
+#define NULL 0
+void f(void) {
+  char *str = NULL; // ok
+  (void)str;
+}
Index: clang-tidy/modernize/UseNullptrCheck.cpp
===================================================================
--- clang-tidy/modernize/UseNullptrCheck.cpp
+++ clang-tidy/modernize/UseNullptrCheck.cpp
@@ -453,7 +453,9 @@
 }
 
 void UseNullptrCheck::registerMatchers(MatchFinder *Finder) {
-  Finder->addMatcher(makeCastSequenceMatcher(), this);
+  // Only register the matcher for C++11.
+  if (getLangOpts().CPlusPlus11)
+    Finder->addMatcher(makeCastSequenceMatcher(), this);
 }
 
 void UseNullptrCheck::check(const MatchFinder::MatchResult &Result) {
Index: clang-tidy/ClangTidyDiagnosticConsumer.h
===================================================================
--- clang-tidy/ClangTidyDiagnosticConsumer.h
+++ clang-tidy/ClangTidyDiagnosticConsumer.h
@@ -149,6 +149,9 @@
   /// \brief Sets ASTContext for the current translation unit.
   void setASTContext(ASTContext *Context);
 
+  /// \brief Gets the language options from the AST context
+  LangOptions getLangOpts() const { return LangOpts; }
+
   /// \brief Returns the name of the clang-tidy check which produced this
   /// diagnostic ID.
   StringRef getCheckName(unsigned DiagnosticID) const;
@@ -198,6 +201,8 @@
   ClangTidyOptions CurrentOptions;
   std::unique_ptr<GlobList> CheckFilter;
 
+  LangOptions LangOpts;
+
   ClangTidyStats Stats;
 
   llvm::DenseMap<unsigned, std::string> CheckNamesByDiagnosticID;
Index: clang-tidy/ClangTidyDiagnosticConsumer.cpp
===================================================================
--- clang-tidy/ClangTidyDiagnosticConsumer.cpp
+++ clang-tidy/ClangTidyDiagnosticConsumer.cpp
@@ -212,6 +212,7 @@
 
 void ClangTidyContext::setASTContext(ASTContext *Context) {
   DiagEngine->SetArgToStringFn(&FormatASTNodeDiagnosticArgument, Context);
+  LangOpts = Context->getLangOpts();
 }
 
 const ClangTidyGlobalOptions &ClangTidyContext::getGlobalOptions() const {
Index: clang-tidy/ClangTidy.h
===================================================================
--- clang-tidy/ClangTidy.h
+++ clang-tidy/ClangTidy.h
@@ -158,6 +158,8 @@
   OptionsView Options;
   /// \brief Returns the main file name of the current translation unit.
   StringRef getCurrentMainFile() const { return Context->getCurrentFile(); }
+  /// \brief Returns the language options from the context.
+  LangOptions getLangOpts() const { return Context->getLangOpts(); }
 };
 
 class ClangTidyCheckFactories;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D12411.33346.patch
Type: text/x-patch
Size: 2876 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150827/9679803a/attachment.bin>


More information about the cfe-commits mailing list