10#include "clang/AST/ASTContext.h"
11#include "clang/ASTMatchers/ASTMatchFinder.h"
19 for (
size_t I = 0; I <
Node.getLength(); ++I)
20 if (
Node.getCodeUnit(I) ==
'\0')
29 Finder->addMatcher(stringLiteral(containsNul()).bind(
"strlit"),
this);
35 const auto StrLitWithNul =
36 ignoringParenImpCasts(stringLiteral(containsNul()).bind(
"truncated"));
39 const auto StringConstructorExpr = expr(anyOf(
40 cxxConstructExpr(argumentCountIs(1),
41 hasDeclaration(cxxMethodDecl(hasName(
"basic_string")))),
44 cxxConstructExpr(argumentCountIs(2),
45 hasDeclaration(cxxMethodDecl(hasName(
"basic_string"))),
46 hasArgument(1, cxxDefaultArgExpr()))));
50 Finder->addMatcher(traverse(TK_AsIs,
51 cxxConstructExpr(StringConstructorExpr, hasArgument(0, StrLitWithNul))),
55 Finder->addMatcher(cxxOperatorCallExpr(hasAnyArgument(StrLitWithNul)),
this);
59 const MatchFinder::MatchResult &Result) {
60 if (
const auto *SL = Result.Nodes.getNodeAs<StringLiteral>(
"strlit")) {
65 SL->getCodeUnit(
Offset + 1) ==
'x' &&
66 isDigit(SL->getCodeUnit(
Offset + 2)) &&
67 isDigit(SL->getCodeUnit(
Offset + 3))) {
68 diag(SL->getBeginLoc(),
"suspicious embedded NUL character");
74 if (
const auto *SL = Result.Nodes.getNodeAs<StringLiteral>(
"truncated")) {
75 diag(SL->getBeginLoc(),
76 "truncated string literal with embedded NUL character");
::clang::DynTypedNode Node
DiagnosticBuilder diag(SourceLocation Loc, StringRef Description, DiagnosticIDs::Level Level=DiagnosticIDs::Warning)
Add a diagnostic with the check's name.
const LangOptions & getLangOpts() const
Returns the language options from the context.
void registerMatchers(ast_matchers::MatchFinder *Finder) override
Override this to register AST matchers with Finder.
void check(const ast_matchers::MatchFinder::MatchResult &Result) override
ClangTidyChecks that register ASTMatchers should do the actual work in here.
AST_MATCHER(clang::VarDecl, hasConstantDeclaration)