mikecrowe created this revision. mikecrowe added a reviewer: PiotrZSL. Herald added subscribers: carlosgalvezp, xazax.hun. Herald added a reviewer: njames93. Herald added a project: All. mikecrowe requested review of this revision. Herald added a project: clang-tools-extra. Herald added a subscriber: cfe-commits.
If MatchesAnyListedNameMatcher::NameMatcher::match() is called in MatchMode::MatchUnqualified mode with a NamedDecl that has no name then calling NamedDecl::getName() will assert with: `Name.isIdentifier() && "Name is not a simple identifier"' This situation can be reproduced by running: clang-tidy '-checks=-*,modernize-use-std-print' -fix -config="{CheckOptions: [[:] + [modernize-use-std-print.PrintfLikeFunctions,] + [value:] + ['printf']]}" on: void A(const std::string &in) { "A" + in; } It seems unfair to force all matchers using matchers::matchesAnyListedName to defend against this, particularly since test cases are unlikely to provoke the problem. Let's just check whether the identifier has a name before attempting to use it instead. Add test case that reproduces the problem to the use-std-print-custom.cpp lit check. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D154884 Files: clang-tools-extra/clang-tidy/utils/Matchers.h clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-custom.cpp Index: clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-custom.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-custom.cpp +++ clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-custom.cpp @@ -3,7 +3,7 @@ // RUN: [ \ // RUN: { \ // RUN: key: modernize-use-std-print.PrintfLikeFunctions, \ -// RUN: value: '::myprintf; mynamespace::myprintf2' \ +// RUN: value: 'unqualified_printf;::myprintf; mynamespace::myprintf2' \ // RUN: }, \ // RUN: { \ // RUN: key: modernize-use-std-print.FprintfLikeFunctions, \ @@ -14,7 +14,7 @@ // RUN: -- -isystem %clang_tidy_headers #include <cstdio> -#include <string.h> +#include <string> int myprintf(const char *, ...); int myfprintf(FILE *fp, const char *, ...); @@ -85,3 +85,10 @@ // CHECK-MESSAGES-NOT: [[@LINE-1]]:10: warning: use 'std::println' instead of 'myprintf' [modernize-use-std-print] // CHECK-FIXES-NOT: std::println(stderr, "return value {}", i); } + +// Ensure that MatchesAnyListedNameMatcher::NameMatcher::match() can cope with a +// NamedDecl that has no name when we're trying to match unqualified_printf. +void no_name(const std::string &in) +{ + "A" + in; +} Index: clang-tools-extra/clang-tidy/utils/Matchers.h =================================================================== --- clang-tools-extra/clang-tidy/utils/Matchers.h +++ clang-tools-extra/clang-tidy/utils/Matchers.h @@ -112,7 +112,9 @@ case MatchMode::MatchFullyQualified: return Regex.match("::" + ND.getQualifiedNameAsString()); default: - return Regex.match(ND.getName()); + if (const IdentifierInfo *II = ND.getIdentifier(); II) + return Regex.match(II->getName()); + return false; } }
Index: clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-custom.cpp =================================================================== --- clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-custom.cpp +++ clang-tools-extra/test/clang-tidy/checkers/modernize/use-std-print-custom.cpp @@ -3,7 +3,7 @@ // RUN: [ \ // RUN: { \ // RUN: key: modernize-use-std-print.PrintfLikeFunctions, \ -// RUN: value: '::myprintf; mynamespace::myprintf2' \ +// RUN: value: 'unqualified_printf;::myprintf; mynamespace::myprintf2' \ // RUN: }, \ // RUN: { \ // RUN: key: modernize-use-std-print.FprintfLikeFunctions, \ @@ -14,7 +14,7 @@ // RUN: -- -isystem %clang_tidy_headers #include <cstdio> -#include <string.h> +#include <string> int myprintf(const char *, ...); int myfprintf(FILE *fp, const char *, ...); @@ -85,3 +85,10 @@ // CHECK-MESSAGES-NOT: [[@LINE-1]]:10: warning: use 'std::println' instead of 'myprintf' [modernize-use-std-print] // CHECK-FIXES-NOT: std::println(stderr, "return value {}", i); } + +// Ensure that MatchesAnyListedNameMatcher::NameMatcher::match() can cope with a +// NamedDecl that has no name when we're trying to match unqualified_printf. +void no_name(const std::string &in) +{ + "A" + in; +} Index: clang-tools-extra/clang-tidy/utils/Matchers.h =================================================================== --- clang-tools-extra/clang-tidy/utils/Matchers.h +++ clang-tools-extra/clang-tidy/utils/Matchers.h @@ -112,7 +112,9 @@ case MatchMode::MatchFullyQualified: return Regex.match("::" + ND.getQualifiedNameAsString()); default: - return Regex.match(ND.getName()); + if (const IdentifierInfo *II = ND.getIdentifier(); II) + return Regex.match(II->getName()); + return false; } }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits