10 #include "../utils/Matchers.h" 11 #include "clang/AST/ASTContext.h" 12 #include "clang/ASTMatchers/ASTMatchFinder.h" 21 const char *BindingStr) {
22 const CallExpr *MatchedCall = cast<CallExpr>(
23 (Result.Nodes.getNodeAs<BinaryOperator>(BindingStr))->getLHS());
24 const SourceManager &SM = *Result.SourceManager;
26 MatchedCall->getCallee()->getSourceRange()),
27 SM, Result.Context->getLangOpts());
30 void PosixReturnCheck::registerMatchers(MatchFinder *Finder) {
34 hasLHS(callExpr(callee(functionDecl(
35 anyOf(matchesName(
"^::posix_"), matchesName(
"^::pthread_")),
36 unless(hasName(
"::posix_openpt")))))),
37 hasRHS(integerLiteral(equals(0))))
42 hasOperatorName(
">="),
43 hasLHS(callExpr(callee(functionDecl(
44 anyOf(matchesName(
"^::posix_"), matchesName(
"^::pthread_")),
45 unless(hasName(
"::posix_openpt")))))),
46 hasRHS(integerLiteral(equals(0))))
51 anyOf(hasOperatorName(
"=="), hasOperatorName(
"!="),
52 hasOperatorName(
"<="), hasOperatorName(
"<")),
53 hasLHS(callExpr(callee(functionDecl(
54 anyOf(matchesName(
"^::posix_"), matchesName(
"^::pthread_")),
55 unless(hasName(
"::posix_openpt")))))),
56 hasRHS(unaryOperator(hasOperatorName(
"-"),
57 hasUnaryOperand(integerLiteral()))))
62 void PosixReturnCheck::check(
const MatchFinder::MatchResult &Result) {
63 if (
const auto *LessThanZeroOp =
64 Result.Nodes.getNodeAs<BinaryOperator>(
"ltzop")) {
65 SourceLocation OperatorLoc = LessThanZeroOp->getOperatorLoc();
66 diag(OperatorLoc,
"the comparison always evaluates to false because %0 " 67 "always returns non-negative values")
69 << FixItHint::CreateReplacement(OperatorLoc, Twine(
">").str());
72 if (
const auto *AlwaysTrueOp =
73 Result.Nodes.getNodeAs<BinaryOperator>(
"atop")) {
74 diag(AlwaysTrueOp->getOperatorLoc(),
75 "the comparison always evaluates to true because %0 always returns " 76 "non-negative values")
80 const auto *BinOp = Result.Nodes.getNodeAs<BinaryOperator>(
"binop");
81 diag(BinOp->getOperatorLoc(),
"%0 only returns non-negative values")
llvm::Optional< Range > getTokenRange(const SourceManager &SM, const LangOptions &LangOpts, SourceLocation TokLoc)
Returns the taken range at TokLoc.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
static StringRef getFunctionSpelling(const MatchFinder::MatchResult &Result, const char *BindingStr)