[clang-tools-extra] 81c330e - Filter string_view from the nullptr diagnosis of bugprone-string-constructor to prevent duplicate warnings with bugprone-stringview-nullptr
CJ Johnson via cfe-commits
cfe-commits at lists.llvm.org
Wed Jan 12 14:04:48 PST 2022
Author: CJ Johnson
Date: 2022-01-12T17:04:44-05:00
New Revision: 81c330e23dd4a14736b977e246bdca25be9b5770
URL: https://github.com/llvm/llvm-project/commit/81c330e23dd4a14736b977e246bdca25be9b5770
DIFF: https://github.com/llvm/llvm-project/commit/81c330e23dd4a14736b977e246bdca25be9b5770.diff
LOG: Filter string_view from the nullptr diagnosis of bugprone-string-constructor to prevent duplicate warnings with bugprone-stringview-nullptr
Updates the check and tests to not diagnose the null case for string_view (but retains it for string). This prevents the check from giving duplicate warnings that are caught by bugprone-stringview-nullptr ([[ https://reviews.llvm.org/D113148 | D113148 ]]).
Reviewed By: ymandel
Differential Revision: https://reviews.llvm.org/D114823
Added:
Modified:
clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp
clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp
index beca3933bcd2..b55b282c41aa 100644
--- a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.cpp
@@ -43,6 +43,8 @@ removeNamespaces(const std::vector<std::string> &Names) {
StringConstructorCheck::StringConstructorCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
+ IsStringviewNullptrCheckEnabled(
+ Context->isCheckEnabled("bugprone-stringview-nullptr")),
WarnOnLargeLength(Options.get("WarnOnLargeLength", true)),
LargeLengthThreshold(Options.get("LargeLengthThreshold", 0x800000)),
StringNames(utils::options::parseStringList(
@@ -121,17 +123,20 @@ void StringConstructorCheck::registerMatchers(MatchFinder *Finder) {
// Check the literal string constructor with char pointer.
// [i.e. string (const char* s);]
Finder->addMatcher(
- traverse(TK_AsIs,
- cxxConstructExpr(
- hasDeclaration(cxxConstructorDecl(ofClass(cxxRecordDecl(
- hasAnyName(removeNamespaces(StringNames)))))),
- hasArgument(0, expr().bind("from-ptr")),
- // do not match std::string(ptr, int)
- // match std::string(ptr, alloc)
- // match std::string(ptr)
- anyOf(hasArgument(1, unless(hasType(isInteger()))),
- argumentCountIs(1)))
- .bind("constructor")),
+ traverse(
+ TK_AsIs,
+ cxxConstructExpr(
+ hasDeclaration(cxxConstructorDecl(ofClass(anyOf(
+ cxxRecordDecl(hasName("basic_string_view"))
+ .bind("basic_string_view_decl"),
+ cxxRecordDecl(hasAnyName(removeNamespaces(StringNames))))))),
+ hasArgument(0, expr().bind("from-ptr")),
+ // do not match std::string(ptr, int)
+ // match std::string(ptr, alloc)
+ // match std::string(ptr)
+ anyOf(hasArgument(1, unless(hasType(isInteger()))),
+ argumentCountIs(1)))
+ .bind("constructor")),
this);
}
@@ -167,6 +172,12 @@ void StringConstructorCheck::check(const MatchFinder::MatchResult &Result) {
Ptr->EvaluateAsRValue(ConstPtr, Ctx) &&
((ConstPtr.Val.isInt() && ConstPtr.Val.getInt().isZero()) ||
(ConstPtr.Val.isLValue() && ConstPtr.Val.isNullPointer()))) {
+ if (IsStringviewNullptrCheckEnabled &&
+ Result.Nodes.getNodeAs<CXXRecordDecl>("basic_string_view_decl")) {
+ // Filter out `basic_string_view` to avoid conflicts with
+ // `bugprone-stringview-nullptr`
+ return;
+ }
diag(Loc, "constructing string from nullptr is undefined behaviour");
}
}
diff --git a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
index 50338f8abead..d4877fbaa235 100644
--- a/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
+++ b/clang-tools-extra/clang-tidy/bugprone/StringConstructorCheck.h
@@ -30,6 +30,7 @@ class StringConstructorCheck : public ClangTidyCheck {
void check(const ast_matchers::MatchFinder::MatchResult &Result) override;
private:
+ const bool IsStringviewNullptrCheckEnabled;
const bool WarnOnLargeLength;
const unsigned int LargeLengthThreshold;
std::vector<std::string> StringNames;
More information about the cfe-commits
mailing list