Path: blob/main/contrib/llvm-project/clang/lib/Analysis/FlowSensitive/Models/ChromiumCheckModel.cpp
35323 views
//===-- ChromiumCheckModel.cpp ----------------------------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "clang/Analysis/FlowSensitive/Models/ChromiumCheckModel.h"9#include "clang/AST/Decl.h"10#include "clang/AST/DeclCXX.h"11#include "llvm/ADT/DenseSet.h"1213namespace clang {14namespace dataflow {1516/// Determines whether `D` is one of the methods used to implement Chromium's17/// `CHECK` macros. Populates `CheckDecls`, if empty.18bool isCheckLikeMethod(llvm::SmallDenseSet<const CXXMethodDecl *> &CheckDecls,19const CXXMethodDecl &D) {20// All of the methods of interest are static, so avoid any lookup for21// non-static methods (the common case).22if (!D.isStatic())23return false;2425if (CheckDecls.empty()) {26// Attempt to initialize `CheckDecls` with the methods in class27// `CheckError`.28const CXXRecordDecl *ParentClass = D.getParent();29if (ParentClass == nullptr || !ParentClass->getDeclName().isIdentifier() ||30ParentClass->getName() != "CheckError")31return false;3233// Check whether namespace is "logging".34const auto *N =35dyn_cast_or_null<NamespaceDecl>(ParentClass->getDeclContext());36if (N == nullptr || !N->getDeclName().isIdentifier() ||37N->getName() != "logging")38return false;3940// Check whether "logging" is a top-level namespace.41if (N->getParent() == nullptr || !N->getParent()->isTranslationUnit())42return false;4344for (const CXXMethodDecl *M : ParentClass->methods())45if (M->getDeclName().isIdentifier() && M->getName().ends_with("Check"))46CheckDecls.insert(M);47}4849return CheckDecls.contains(&D);50}5152bool ChromiumCheckModel::transfer(const CFGElement &Element, Environment &Env) {53auto CS = Element.getAs<CFGStmt>();54if (!CS)55return false;56auto Stmt = CS->getStmt();57if (const auto *Call = dyn_cast<CallExpr>(Stmt)) {58if (const auto *M = dyn_cast<CXXMethodDecl>(Call->getDirectCallee())) {59if (isCheckLikeMethod(CheckDecls, *M)) {60// Mark this branch as unreachable.61Env.assume(Env.arena().makeLiteral(false));62return true;63}64}65}66return false;67}6869} // namespace dataflow70} // namespace clang717273