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) {
31 Finder->addMatcher(binaryOperator(hasOperatorName(
"<"),
32 hasLHS(callExpr(callee(functionDecl(
33 matchesName(
"^::posix_"),
34 unless(hasName(
"::posix_openpt")))))),
35 hasRHS(integerLiteral(equals(0))))
38 Finder->addMatcher(binaryOperator(hasOperatorName(
">="),
39 hasLHS(callExpr(callee(functionDecl(
40 matchesName(
"^::posix_"),
41 unless(hasName(
"::posix_openpt")))))),
42 hasRHS(integerLiteral(equals(0))))
47 anyOf(hasOperatorName(
"=="), hasOperatorName(
"!="),
48 hasOperatorName(
"<="), hasOperatorName(
"<")),
49 hasLHS(callExpr(callee(functionDecl(
50 matchesName(
"^::posix_"), unless(hasName(
"::posix_openpt")))))),
51 hasRHS(unaryOperator(hasOperatorName(
"-"),
52 hasUnaryOperand(integerLiteral()))))
57 void PosixReturnCheck::check(
const MatchFinder::MatchResult &
Result) {
58 if (
const auto *LessThanZeroOp =
59 Result.Nodes.getNodeAs<BinaryOperator>(
"ltzop")) {
60 SourceLocation OperatorLoc = LessThanZeroOp->getOperatorLoc();
61 diag(OperatorLoc,
"the comparison always evaluates to false because %0 " 62 "always returns non-negative values")
64 << FixItHint::CreateReplacement(OperatorLoc, Twine(
">").str());
67 if (
const auto *AlwaysTrueOp =
68 Result.Nodes.getNodeAs<BinaryOperator>(
"atop")) {
69 diag(AlwaysTrueOp->getOperatorLoc(),
70 "the comparison always evaluates to true because %0 always returns " 71 "non-negative values")
75 const auto *BinOp = Result.Nodes.getNodeAs<BinaryOperator>(
"binop");
76 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++ -*-===//
llvm::Optional< llvm::Expected< tooling::AtomicChanges > > Result
static StringRef getFunctionSpelling(const MatchFinder::MatchResult &Result, const char *BindingStr)