
This is a major change on how we represent nested name qualifications in the AST. * The nested name specifier itself and how it's stored is changed. The prefixes for types are handled within the type hierarchy, which makes canonicalization for them super cheap, no memory allocation required. Also translating a type into nested name specifier form becomes a no-op. An identifier is stored as a DependentNameType. The nested name specifier gains a lightweight handle class, to be used instead of passing around pointers, which is similar to what is implemented for TemplateName. There is still one free bit available, and this handle can be used within a PointerUnion and PointerIntPair, which should keep bit-packing aficionados happy. * The ElaboratedType node is removed, all type nodes in which it could previously apply to can now store the elaborated keyword and name qualifier, tail allocating when present. * TagTypes can now point to the exact declaration found when producing these, as opposed to the previous situation of there only existing one TagType per entity. This increases the amount of type sugar retained, and can have several applications, for example in tracking module ownership, and other tools which care about source file origins, such as IWYU. These TagTypes are lazily allocated, in order to limit the increase in AST size. This patch offers a great performance benefit. It greatly improves compilation time for [stdexec](https://github.com/NVIDIA/stdexec). For one datapoint, for `test_on2.cpp` in that project, which is the slowest compiling test, this patch improves `-c` compilation time by about 7.2%, with the `-fsyntax-only` improvement being at ~12%. This has great results on compile-time-tracker as well:  This patch also further enables other optimziations in the future, and will reduce the performance impact of template specialization resugaring when that lands. It has some other miscelaneous drive-by fixes. About the review: Yes the patch is huge, sorry about that. Part of the reason is that I started by the nested name specifier part, before the ElaboratedType part, but that had a huge performance downside, as ElaboratedType is a big performance hog. I didn't have the steam to go back and change the patch after the fact. There is also a lot of internal API changes, and it made sense to remove ElaboratedType in one go, versus removing it from one type at a time, as that would present much more churn to the users. Also, the nested name specifier having a different API avoids missing changes related to how prefixes work now, which could make existing code compile but not work. How to review: The important changes are all in `clang/include/clang/AST` and `clang/lib/AST`, with also important changes in `clang/lib/Sema/TreeTransform.h`. The rest and bulk of the changes are mostly consequences of the changes in API. PS: TagType::getDecl is renamed to `getOriginalDecl` in this patch, just for easier to rebasing. I plan to rename it back after this lands. Fixes #136624 Fixes https://github.com/llvm/llvm-project/issues/43179 Fixes https://github.com/llvm/llvm-project/issues/68670 Fixes https://github.com/llvm/llvm-project/issues/92757
154 lines
5.1 KiB
C++
154 lines
5.1 KiB
C++
//==- 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/Checker.h"
|
|
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
|
|
#include "clang/StaticAnalyzer/Core/PathSensitive/ExprEngine.h"
|
|
#include <optional>
|
|
|
|
using namespace clang;
|
|
using namespace ento;
|
|
|
|
namespace {
|
|
|
|
class NonnullGlobalConstantsChecker : public Checker<check::Location> {
|
|
mutable IdentifierInfo *NSStringII = nullptr;
|
|
mutable IdentifierInfo *CFStringRefII = nullptr;
|
|
mutable IdentifierInfo *CFBooleanRefII = nullptr;
|
|
mutable IdentifierInfo *CFNullRefII = nullptr;
|
|
|
|
public:
|
|
NonnullGlobalConstantsChecker() {}
|
|
|
|
void checkLocation(SVal l, bool isLoad, const Stmt *S,
|
|
CheckerContext &C) const;
|
|
|
|
private:
|
|
void initIdentifierInfo(ASTContext &Ctx) const;
|
|
|
|
bool isGlobalConstString(SVal V) const;
|
|
|
|
bool isNonnullType(QualType Ty) const;
|
|
};
|
|
|
|
} // namespace
|
|
|
|
/// Lazily initialize cache for required identifier information.
|
|
void NonnullGlobalConstantsChecker::initIdentifierInfo(ASTContext &Ctx) const {
|
|
if (NSStringII)
|
|
return;
|
|
|
|
NSStringII = &Ctx.Idents.get("NSString");
|
|
CFStringRefII = &Ctx.Idents.get("CFStringRef");
|
|
CFBooleanRefII = &Ctx.Idents.get("CFBooleanRef");
|
|
CFNullRefII = &Ctx.Idents.get("CFNullRef");
|
|
}
|
|
|
|
/// Add an assumption that const string-like globals are non-null.
|
|
void NonnullGlobalConstantsChecker::checkLocation(SVal location, bool isLoad,
|
|
const Stmt *S,
|
|
CheckerContext &C) const {
|
|
initIdentifierInfo(C.getASTContext());
|
|
if (!isLoad || !location.isValid())
|
|
return;
|
|
|
|
ProgramStateRef State = C.getState();
|
|
|
|
if (isGlobalConstString(location)) {
|
|
SVal V = State->getSVal(location.castAs<Loc>());
|
|
std::optional<DefinedOrUnknownSVal> Constr =
|
|
V.getAs<DefinedOrUnknownSVal>();
|
|
|
|
if (Constr) {
|
|
|
|
// Assume that the variable is non-null.
|
|
ProgramStateRef OutputState = State->assume(*Constr, true);
|
|
C.addTransition(OutputState);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// \param V loaded lvalue.
|
|
/// \return whether @c val is a string-like const global.
|
|
bool NonnullGlobalConstantsChecker::isGlobalConstString(SVal V) const {
|
|
std::optional<loc::MemRegionVal> RegionVal = V.getAs<loc::MemRegionVal>();
|
|
if (!RegionVal)
|
|
return false;
|
|
auto *Region = dyn_cast<VarRegion>(RegionVal->getAsRegion());
|
|
if (!Region)
|
|
return false;
|
|
const VarDecl *Decl = Region->getDecl();
|
|
|
|
if (!Decl->hasGlobalStorage())
|
|
return false;
|
|
|
|
QualType Ty = Decl->getType();
|
|
bool HasConst = Ty.isConstQualified();
|
|
if (isNonnullType(Ty) && HasConst)
|
|
return true;
|
|
|
|
// Look through the typedefs.
|
|
while (const Type *T = Ty.getTypePtr()) {
|
|
if (const auto *AT = dyn_cast<AttributedType>(T)) {
|
|
if (AT->getAttrKind() == attr::TypeNonNull)
|
|
return true;
|
|
Ty = AT->getModifiedType();
|
|
} else if (const auto *TT = dyn_cast<TypedefType>(T)) {
|
|
Ty = TT->getDecl()->getUnderlyingType();
|
|
// It is sufficient for any intermediate typedef
|
|
// to be classified const.
|
|
HasConst = HasConst || Ty.isConstQualified();
|
|
if (isNonnullType(Ty) && HasConst)
|
|
return true;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/// \return whether @c type is extremely unlikely to be null
|
|
bool NonnullGlobalConstantsChecker::isNonnullType(QualType Ty) const {
|
|
|
|
if (Ty->isPointerType() && Ty->getPointeeType()->isCharType())
|
|
return true;
|
|
|
|
if (auto *T = dyn_cast<ObjCObjectPointerType>(Ty)) {
|
|
return T->getInterfaceDecl() &&
|
|
T->getInterfaceDecl()->getIdentifier() == NSStringII;
|
|
} else if (auto *T = Ty->getAs<TypedefType>()) {
|
|
IdentifierInfo* II = T->getDecl()->getIdentifier();
|
|
return II == CFStringRefII || II == CFBooleanRefII || II == CFNullRefII;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
void ento::registerNonnullGlobalConstantsChecker(CheckerManager &Mgr) {
|
|
Mgr.registerChecker<NonnullGlobalConstantsChecker>();
|
|
}
|
|
|
|
bool ento::shouldRegisterNonnullGlobalConstantsChecker(const CheckerManager &mgr) {
|
|
return true;
|
|
}
|