clang  15.0.0git
ChromiumCheckModel.cpp
Go to the documentation of this file.
1 //===-- ChromiumCheckModel.cpp ----------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
10 #include "clang/AST/Decl.h"
11 #include "clang/AST/DeclCXX.h"
12 #include "llvm/ADT/DenseSet.h"
13 
14 namespace clang {
15 namespace dataflow {
16 
17 /// Determines whether `D` is one of the methods used to implement Chromium's
18 /// `CHECK` macros. Populates `CheckDecls`, if empty.
19 bool isCheckLikeMethod(llvm::SmallDenseSet<const CXXMethodDecl *> &CheckDecls,
20  const CXXMethodDecl &D) {
21  // All of the methods of interest are static, so avoid any lookup for
22  // non-static methods (the common case).
23  if (!D.isStatic())
24  return false;
25 
26  if (CheckDecls.empty()) {
27  // Attempt to initialize `CheckDecls` with the methods in class
28  // `CheckError`.
29  const CXXRecordDecl *ParentClass = D.getParent();
30  if (ParentClass == nullptr || !ParentClass->getDeclName().isIdentifier() ||
31  ParentClass->getName() != "CheckError")
32  return false;
33 
34  // Check whether namespace is "logging".
35  const auto *N =
36  dyn_cast_or_null<NamespaceDecl>(ParentClass->getDeclContext());
37  if (N == nullptr || !N->getDeclName().isIdentifier() ||
38  N->getName() != "logging")
39  return false;
40 
41  // Check whether "logging" is a top-level namespace.
42  if (N->getParent() == nullptr || !N->getParent()->isTranslationUnit())
43  return false;
44 
45  for (const CXXMethodDecl *M : ParentClass->methods())
46  if (M->getDeclName().isIdentifier() && M->getName().endswith("Check"))
47  CheckDecls.insert(M);
48  }
49 
50  return CheckDecls.contains(&D);
51 }
52 
54  if (const auto *Call = dyn_cast<CallExpr>(Stmt)) {
55  if (const auto *M = dyn_cast<CXXMethodDecl>(Call->getDirectCallee())) {
56  if (isCheckLikeMethod(CheckDecls, *M)) {
57  // Mark this branch as unreachable.
59  return true;
60  }
61  }
62  }
63  return false;
64 }
65 
66 } // namespace dataflow
67 } // namespace clang
clang::CXXMethodDecl::isStatic
bool isStatic() const
Definition: DeclCXX.cpp:2113
clang::dataflow::Environment::getBoolLiteralValue
AtomicBoolValue & getBoolLiteralValue(bool Value) const
Returns a symbolic boolean value that models a boolean literal equal to Value
Definition: DataflowEnvironment.h:261
DeclCXX.h
clang::dataflow::Environment::addToFlowCondition
void addToFlowCondition(BoolValue &Val)
Adds Val to the set of clauses that constitute the flow condition.
Definition: DataflowEnvironment.cpp:507
Decl.h
ChromiumCheckModel.h
clang::CXXRecordDecl::methods
method_range methods() const
Definition: DeclCXX.h:638
clang::CXXRecordDecl
Represents a C++ struct/union/class.
Definition: DeclCXX.h:254
clang::DeclarationName::isIdentifier
bool isIdentifier() const
Predicate functions for querying what type of name this is.
Definition: DeclarationName.h:384
clang
Definition: CalledOnceCheck.h:17
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:70
clang::NamedDecl::getDeclName
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition: Decl.h:311
clang::dataflow::ChromiumCheckModel::transfer
bool transfer(const Stmt *Stmt, Environment &Env) override
Return value indicates whether the model processed the Stmt.
Definition: ChromiumCheckModel.cpp:53
clang::dataflow::Environment
Holds the state of the program (store and heap) at a given program point.
Definition: DataflowEnvironment.h:57
clang::dataflow::isCheckLikeMethod
bool isCheckLikeMethod(llvm::SmallDenseSet< const CXXMethodDecl * > &CheckDecls, const CXXMethodDecl &D)
Determines whether D is one of the methods used to implement Chromium's CHECK macros.
Definition: ChromiumCheckModel.cpp:19
clang::CXXMethodDecl::getParent
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition: DeclCXX.h:2096
clang::CXXMethodDecl
Represents a static or instance method of a struct/union/class.
Definition: DeclCXX.h:1981
clang::Decl::getDeclContext
DeclContext * getDeclContext()
Definition: DeclBase.h:441
clang::NamedDecl::getName
StringRef getName() const
Get the name of identifier for this declaration as a StringRef.
Definition: Decl.h:274