[clang-tools-extra] 4c5818d - [clang-tidy] Fix potential assert in use-noexcept check

Nathan James via cfe-commits cfe-commits at lists.llvm.org
Sun May 24 06:41:06 PDT 2020


Author: Nathan James
Date: 2020-05-24T14:40:58+01:00
New Revision: 4c5818dd8cd9336136a80a02b262b501b23f6492

URL: https://github.com/llvm/llvm-project/commit/4c5818dd8cd9336136a80a02b262b501b23f6492
DIFF: https://github.com/llvm/llvm-project/commit/4c5818dd8cd9336136a80a02b262b501b23f6492.diff

LOG: [clang-tidy] Fix potential assert in use-noexcept check

Summary: Fix a potential assert in use-noexcept check if there is an issue getting the `TypeSourceInfo` as well as a small clean up.

Reviewers: aaron.ballman, alexfh, gribozavr2

Reviewed By: aaron.ballman

Subscribers: xazax.hun, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D80371

Added: 
    clang-tools-extra/test/clang-tidy/checkers/modernize-use-noexcept-error.cpp

Modified: 
    clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.cpp
    clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.h

Removed: 
    


################################################################################
diff  --git a/clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.cpp b/clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.cpp
index bbda8d58f103..cc4bc05a35dd 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.cpp
+++ b/clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.cpp
@@ -16,6 +16,10 @@ namespace clang {
 namespace tidy {
 namespace modernize {
 
+namespace {
+AST_MATCHER(NamedDecl, isValid) { return !Node.isInvalidDecl(); }
+} // namespace
+
 UseNoexceptCheck::UseNoexceptCheck(StringRef Name, ClangTidyContext *Context)
     : ClangTidyCheck(Name, Context),
       NoexceptMacro(Options.get("ReplacementString", "")),
@@ -29,20 +33,12 @@ void UseNoexceptCheck::storeOptions(ClangTidyOptions::OptionMap &Opts) {
 void UseNoexceptCheck::registerMatchers(MatchFinder *Finder) {
   Finder->addMatcher(
       functionDecl(
-          cxxMethodDecl(
-              hasTypeLoc(loc(functionProtoType(hasDynamicExceptionSpec()))),
-              anyOf(hasOverloadedOperatorName("delete[]"),
-                    hasOverloadedOperatorName("delete"), cxxDestructorDecl()))
-              .bind("del-dtor"))
-          .bind("funcDecl"),
-      this);
-
-  Finder->addMatcher(
-      functionDecl(
+          isValid(),
           hasTypeLoc(loc(functionProtoType(hasDynamicExceptionSpec()))),
-          unless(anyOf(hasOverloadedOperatorName("delete[]"),
-                       hasOverloadedOperatorName("delete"),
-                       cxxDestructorDecl())))
+          optionally(cxxMethodDecl(anyOf(hasAnyOverloadedOperatorName(
+                                             "delete[]", "delete"),
+                                         cxxDestructorDecl()))
+                         .bind("del-dtor")))
           .bind("funcDecl"),
       this);
 
@@ -80,6 +76,9 @@ void UseNoexceptCheck::check(const MatchFinder::MatchResult &Result) {
                   .castAs<FunctionProtoTypeLoc>()
                   .getExceptionSpecRange();
   }
+
+  assert(Range.isValid() && "Exception Source Range is invalid.");
+
   CharSourceRange CRange = Lexer::makeFileCharRange(
       CharSourceRange::getTokenRange(Range), *Result.SourceManager,
       Result.Context->getLangOpts());

diff  --git a/clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.h b/clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.h
index 4f3ba321483e..b87d3e629ff6 100644
--- a/clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.h
+++ b/clang-tools-extra/clang-tidy/modernize/UseNoexceptCheck.h
@@ -41,7 +41,7 @@ class UseNoexceptCheck : public ClangTidyCheck {
 
 private:
   const std::string NoexceptMacro;
-  bool UseNoexceptFalse;
+  const bool UseNoexceptFalse;
 };
 
 } // namespace modernize

diff  --git a/clang-tools-extra/test/clang-tidy/checkers/modernize-use-noexcept-error.cpp b/clang-tools-extra/test/clang-tidy/checkers/modernize-use-noexcept-error.cpp
new file mode 100644
index 000000000000..9a80b075d65e
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/modernize-use-noexcept-error.cpp
@@ -0,0 +1,6 @@
+// RUN: %check_clang_tidy -expect-clang-tidy-error %s modernize-use-noexcept %t
+
+// We're not interested in the check issuing a warning here, just making sure
+// clang-tidy doesn't assert.
+undefined_type doesThrow() throw();
+// CHECK-MESSAGES: :[[@LINE-1]]:1: error: unknown type name 'undefined_type' [clang-diagnostic-error]


        


More information about the cfe-commits mailing list