10#include "../utils/OptionsUtils.h"
11#include "clang/AST/ASTContext.h"
12#include "clang/ASTMatchers/ASTMatchFinder.h"
13#include "clang/Tooling/FixIt.h"
21 return Node.getValue().getZExtValue() > N;
25 "::std::basic_string;::std::basic_string_view";
27static std::vector<StringRef>
28removeNamespaces(
const std::vector<StringRef> &Names) {
29 std::vector<StringRef> Result;
30 Result.reserve(Names.size());
31 for (StringRef
Name : Names) {
32 std::string::size_type ColonPos =
Name.rfind(
':');
34 Name.substr(ColonPos == std::string::npos ? 0 : ColonPos + 1));
44 IsStringviewNullptrCheckEnabled(
45 Context->isCheckEnabled(
"bugprone-stringview-nullptr")),
46 WarnOnLargeLength(Options.get(
"WarnOnLargeLength", true)),
47 LargeLengthThreshold(Options.get(
"LargeLengthThreshold", 0x800000)),
48 StringNames(utils::options::parseStringList(
49 Options.get(
"StringNames", DefaultStringNames))) {}
52 Options.
store(Opts,
"WarnOnLargeLength", WarnOnLargeLength);
53 Options.
store(Opts,
"LargeLengthThreshold", LargeLengthThreshold);
58 const auto ZeroExpr = expr(ignoringParenImpCasts(integerLiteral(equals(0))));
59 const auto CharExpr = expr(ignoringParenImpCasts(characterLiteral()));
60 const auto NegativeExpr = expr(ignoringParenImpCasts(
61 unaryOperator(hasOperatorName(
"-"),
62 hasUnaryOperand(integerLiteral(unless(equals(0)))))));
63 const auto LargeLengthExpr = expr(ignoringParenImpCasts(
64 integerLiteral(isBiggerThan(LargeLengthThreshold))));
65 const auto CharPtrType = type(anyOf(pointerType(), arrayType()));
68 const auto BoundStringLiteral = stringLiteral().bind(
"str");
69 const auto ConstStrLiteralDecl = varDecl(
70 isDefinition(), hasType(constantArrayType()), hasType(isConstQualified()),
71 hasInitializer(ignoringParenImpCasts(BoundStringLiteral)));
72 const auto ConstPtrStrLiteralDecl = varDecl(
74 hasType(pointerType(pointee(isAnyCharacter(), isConstQualified()))),
75 hasInitializer(ignoringParenImpCasts(BoundStringLiteral)));
76 const auto ConstStrLiteral = expr(ignoringParenImpCasts(anyOf(
77 BoundStringLiteral, declRefExpr(hasDeclaration(anyOf(
78 ConstPtrStrLiteralDecl, ConstStrLiteralDecl))))));
84 hasDeclaration(cxxMethodDecl(hasName(
"basic_string"))),
85 hasArgument(0, hasType(qualType(isInteger()))),
86 hasArgument(1, hasType(qualType(isInteger()))),
89 hasArgument(0, CharExpr.bind(
"swapped-parameter")),
91 hasArgument(0, ZeroExpr.bind(
"empty-string")),
93 hasArgument(0, NegativeExpr.bind(
"negative-length")),
95 hasArgument(0, LargeLengthExpr.bind(
"large-length"))))
103 hasDeclaration(cxxConstructorDecl(ofClass(
104 cxxRecordDecl(hasAnyName(removeNamespaces(StringNames)))))),
105 hasArgument(0, hasType(CharPtrType)),
106 hasArgument(1, hasType(isInteger())),
109 hasArgument(1, ZeroExpr.bind(
"empty-string")),
111 hasArgument(1, NegativeExpr.bind(
"negative-length")),
113 hasArgument(1, LargeLengthExpr.bind(
"large-length")),
115 allOf(hasArgument(0, ConstStrLiteral.bind(
"literal-with-length")),
116 hasArgument(1, ignoringParenImpCasts(
117 integerLiteral().bind(
"int"))))))
118 .bind(
"constructor"),
127 hasDeclaration(cxxConstructorDecl(ofClass(anyOf(
128 cxxRecordDecl(hasName(
"basic_string_view"))
129 .bind(
"basic_string_view_decl"),
130 cxxRecordDecl(hasAnyName(removeNamespaces(StringNames))))))),
131 hasArgument(0, expr().bind(
"from-ptr")),
135 anyOf(hasArgument(1, unless(hasType(isInteger()))),
137 .bind(
"constructor")),
142 const ASTContext &Ctx = *Result.Context;
143 const auto *
E = Result.Nodes.getNodeAs<CXXConstructExpr>(
"constructor");
144 assert(
E &&
"missing constructor expression");
145 SourceLocation
Loc =
E->getBeginLoc();
147 if (Result.Nodes.getNodeAs<Expr>(
"swapped-parameter")) {
148 const Expr *P0 =
E->getArg(0);
149 const Expr *P1 =
E->getArg(1);
150 diag(
Loc,
"string constructor parameters are probably swapped;"
151 " expecting string(count, character)")
152 << tooling::fixit::createReplacement(*P0, *P1, Ctx)
153 << tooling::fixit::createReplacement(*P1, *P0, Ctx);
154 }
else if (Result.Nodes.getNodeAs<Expr>(
"empty-string")) {
155 diag(
Loc,
"constructor creating an empty string");
156 }
else if (Result.Nodes.getNodeAs<Expr>(
"negative-length")) {
157 diag(
Loc,
"negative value used as length parameter");
158 }
else if (Result.Nodes.getNodeAs<Expr>(
"large-length")) {
159 if (WarnOnLargeLength)
160 diag(
Loc,
"suspicious large length parameter");
161 }
else if (Result.Nodes.getNodeAs<Expr>(
"literal-with-length")) {
162 const auto *Str = Result.Nodes.getNodeAs<StringLiteral>(
"str");
163 const auto *Lit = Result.Nodes.getNodeAs<IntegerLiteral>(
"int");
164 if (Lit->getValue().ugt(Str->getLength())) {
165 diag(
Loc,
"length is bigger than string literal size");
167 }
else if (
const auto *Ptr = Result.Nodes.getNodeAs<Expr>(
"from-ptr")) {
168 Expr::EvalResult ConstPtr;
169 if (!Ptr->isInstantiationDependent() &&
170 Ptr->EvaluateAsRValue(ConstPtr, Ctx) &&
171 ((ConstPtr.Val.isInt() && ConstPtr.Val.getInt().isZero()) ||
172 (ConstPtr.Val.isLValue() && ConstPtr.Val.isNullPointer()))) {
173 if (IsStringviewNullptrCheckEnabled &&
174 Result.Nodes.getNodeAs<CXXRecordDecl>(
"basic_string_view_decl")) {
179 diag(
Loc,
"constructing string from nullptr is undefined behaviour");
llvm::SmallString< 256U > Name
::clang::DynTypedNode Node
void store(ClangTidyOptions::OptionMap &Options, StringRef LocalName, StringRef Value) const
Stores an option with the check-local name LocalName with string value Value to Options.
Base class for all clang-tidy checks.
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
Every ClangTidyCheck reports errors through a DiagnosticsEngine provided by this context.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
StringConstructorCheck(StringRef Name, ClangTidyContext *Context)
void storeOptions(ClangTidyOptions::OptionMap &Opts) override
Should store all options supported by this check with their current values or default values for opti...
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
AST_MATCHER_P(FunctionDecl, parameterCountGE, unsigned, N)
Matches functions that have at least the specified amount of parameters.
const char DefaultStringNames[]
llvm::StringMap< ClangTidyValue > OptionMap