diff options
5 files changed, 21 insertions, 1 deletions
diff --git a/clang-tools-extra/clang-tidy/ClangTidy.h b/clang-tools-extra/clang-tidy/ClangTidy.h index 2df17c2ef43..a2dbcbc540e 100644 --- a/clang-tools-extra/clang-tidy/ClangTidy.h +++ b/clang-tools-extra/clang-tidy/ClangTidy.h @@ -158,6 +158,8 @@ protected: 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; diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp index c383440f8be..e19011b2961 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp +++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp @@ -212,6 +212,7 @@ void ClangTidyContext::setCurrentFile(StringRef File) { void ClangTidyContext::setASTContext(ASTContext *Context) { DiagEngine->SetArgToStringFn(&FormatASTNodeDiagnosticArgument, Context); + LangOpts = Context->getLangOpts(); } const ClangTidyGlobalOptions &ClangTidyContext::getGlobalOptions() const { diff --git a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h index f1ab4942c07..a452d68cc84 100644 --- a/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h +++ b/clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h @@ -149,6 +149,9 @@ public: /// \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 @@ private: ClangTidyOptions CurrentOptions; std::unique_ptr<GlobList> CheckFilter; + LangOptions LangOpts; + ClangTidyStats Stats; llvm::DenseMap<unsigned, std::string> CheckNamesByDiagnosticID; diff --git a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp index ea3bd95013f..3a3b0054422 100644 --- a/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp +++ b/clang-tools-extra/clang-tidy/modernize/UseNullptrCheck.cpp @@ -453,7 +453,9 @@ void UseNullptrCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) { } 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) { diff --git a/clang-tools-extra/test/clang-tidy/modernize-use-nullptr.c b/clang-tools-extra/test/clang-tidy/modernize-use-nullptr.c new file mode 100644 index 00000000000..c2ccbbd8117 --- /dev/null +++ b/clang-tools-extra/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; +} |