//==- NonnullGlobalConstantsChecker.cpp ---------------------------*- C++ -*--// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // This checker adds an assumption that constant globals of certain types* are // non-null, as otherwise they generally do not convey any useful information. // The assumption is useful, as many framework use e. g. global const strings, // and the analyzer might not be able to infer the global value if the // definition is in a separate translation unit. // The following types (and their typedef aliases) are considered to be // non-null: // - `char* const` // - `const CFStringRef` from CoreFoundation // - `NSString* const` from Foundation // - `CFBooleanRef` from Foundation // //===----------------------------------------------------------------------===// #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" #include "clang/StaticAnalyzer/Core/Checker.h" #include "clang/StaticAnalyzer/Core/CheckerManager.h" #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" #include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h" #include <optional> usingnamespaceclang; usingnamespaceento; namespace { class NonnullGlobalConstantsChecker : public Checker<check::Location> { … }; } // namespace /// Lazily initialize cache for required identifier information. void NonnullGlobalConstantsChecker::initIdentifierInfo(ASTContext &Ctx) const { … } /// Add an assumption that const string-like globals are non-null. void NonnullGlobalConstantsChecker::checkLocation(SVal location, bool isLoad, const Stmt *S, CheckerContext &C) const { … } /// \param V loaded lvalue. /// \return whether @c val is a string-like const global. bool NonnullGlobalConstantsChecker::isGlobalConstString(SVal V) const { … } /// \return whether @c type is extremely unlikely to be null bool NonnullGlobalConstantsChecker::isNonnullType(QualType Ty) const { … } void ento::registerNonnullGlobalConstantsChecker(CheckerManager &Mgr) { … } bool ento::shouldRegisterNonnullGlobalConstantsChecker(const CheckerManager &mgr) { … }