llvm-project/clang-tools-extra/clang-tidy/utils/ExceptionSpecAnalyzer.cpp
Matheus Izvekov 91cdd35008
[clang] Improve nested name specifier AST representation (#147835)
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:

![image](https://github.com/user-attachments/assets/700dce98-2cab-4aa8-97d1-b038c0bee831)

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
2025-08-09 05:06:53 -03:00

288 lines
8.9 KiB
C++

//===--- ExceptionSpecAnalyzer.cpp - clang-tidy ---------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "ExceptionSpecAnalyzer.h"
#include "clang/AST/Expr.h"
namespace clang::tidy::utils {
ExceptionSpecAnalyzer::State
ExceptionSpecAnalyzer::analyze(const FunctionDecl *FuncDecl) {
// Check if function exist in cache or add temporary value to cache to protect
// against endless recursion.
const auto [CacheEntry, NotFound] =
FunctionCache.try_emplace(FuncDecl, State::NotThrowing);
if (NotFound) {
ExceptionSpecAnalyzer::State State = analyzeImpl(FuncDecl);
// Update result with calculated value
FunctionCache[FuncDecl] = State;
return State;
}
return CacheEntry->getSecond();
}
ExceptionSpecAnalyzer::State
ExceptionSpecAnalyzer::analyzeUnresolvedOrDefaulted(
const CXXMethodDecl *MethodDecl, const FunctionProtoType *FuncProto) {
if (!FuncProto || !MethodDecl)
return State::Unknown;
const DefaultableMemberKind Kind = getDefaultableMemberKind(MethodDecl);
if (Kind == DefaultableMemberKind::None)
return State::Unknown;
return analyzeRecord(MethodDecl->getParent(), Kind, SkipMethods::Yes);
}
ExceptionSpecAnalyzer::State
ExceptionSpecAnalyzer::analyzeFieldDecl(const FieldDecl *FDecl,
DefaultableMemberKind Kind) {
if (!FDecl)
return State::Unknown;
if (const CXXRecordDecl *RecDecl =
FDecl->getType()->getUnqualifiedDesugaredType()->getAsCXXRecordDecl())
return analyzeRecord(RecDecl, Kind);
// Trivial types do not throw
if (FDecl->getType().isTrivialType(FDecl->getASTContext()))
return State::NotThrowing;
return State::Unknown;
}
ExceptionSpecAnalyzer::State
ExceptionSpecAnalyzer::analyzeBase(const CXXBaseSpecifier &Base,
DefaultableMemberKind Kind) {
const auto *RecType = Base.getType()->getAs<RecordType>();
if (!RecType)
return State::Unknown;
const auto *BaseClass =
cast<CXXRecordDecl>(RecType->getOriginalDecl())->getDefinitionOrSelf();
return analyzeRecord(BaseClass, Kind);
}
ExceptionSpecAnalyzer::State
ExceptionSpecAnalyzer::analyzeRecord(const CXXRecordDecl *RecordDecl,
DefaultableMemberKind Kind,
SkipMethods SkipMethods) {
if (!RecordDecl)
return State::Unknown;
// Trivial implies noexcept
if (hasTrivialMemberKind(RecordDecl, Kind))
return State::NotThrowing;
if (SkipMethods == SkipMethods::No)
for (const auto *MethodDecl : RecordDecl->methods())
if (getDefaultableMemberKind(MethodDecl) == Kind)
return analyze(MethodDecl);
for (const auto &BaseSpec : RecordDecl->bases()) {
State Result = analyzeBase(BaseSpec, Kind);
if (Result == State::Throwing || Result == State::Unknown)
return Result;
}
for (const auto &BaseSpec : RecordDecl->vbases()) {
State Result = analyzeBase(BaseSpec, Kind);
if (Result == State::Throwing || Result == State::Unknown)
return Result;
}
for (const auto *FDecl : RecordDecl->fields())
if (!FDecl->isInvalidDecl() && !FDecl->isUnnamedBitField()) {
State Result = analyzeFieldDecl(FDecl, Kind);
if (Result == State::Throwing || Result == State::Unknown)
return Result;
}
return State::NotThrowing;
}
ExceptionSpecAnalyzer::State
ExceptionSpecAnalyzer::analyzeImpl(const FunctionDecl *FuncDecl) {
const auto *FuncProto = FuncDecl->getType()->getAs<FunctionProtoType>();
if (!FuncProto)
return State::Unknown;
const ExceptionSpecificationType EST = FuncProto->getExceptionSpecType();
if (EST == EST_Unevaluated || (EST == EST_None && FuncDecl->isDefaulted()))
return analyzeUnresolvedOrDefaulted(cast<CXXMethodDecl>(FuncDecl),
FuncProto);
return analyzeFunctionEST(FuncDecl, FuncProto);
}
ExceptionSpecAnalyzer::State
ExceptionSpecAnalyzer::analyzeFunctionEST(const FunctionDecl *FuncDecl,
const FunctionProtoType *FuncProto) {
if (!FuncDecl || !FuncProto)
return State::Unknown;
if (isUnresolvedExceptionSpec(FuncProto->getExceptionSpecType()))
return State::Unknown;
// A non defaulted destructor without the noexcept specifier is still noexcept
if (isa<CXXDestructorDecl>(FuncDecl) &&
FuncDecl->getExceptionSpecType() == EST_None)
return State::NotThrowing;
switch (FuncProto->canThrow()) {
case CT_Cannot:
return State::NotThrowing;
case CT_Dependent: {
const Expr *NoexceptExpr = FuncProto->getNoexceptExpr();
if (!NoexceptExpr)
return State::NotThrowing;
// We can't resolve value dependence so just return unknown
if (NoexceptExpr->isValueDependent())
return State::Unknown;
// Try to evaluate the expression to a boolean value
bool Result = false;
if (NoexceptExpr->EvaluateAsBooleanCondition(
Result, FuncDecl->getASTContext(), true))
return Result ? State::NotThrowing : State::Throwing;
// The noexcept expression is not value dependent but we can't evaluate it
// as a boolean condition so we have no idea if its throwing or not
return State::Unknown;
}
default:
return State::Throwing;
};
}
bool ExceptionSpecAnalyzer::hasTrivialMemberKind(const CXXRecordDecl *RecDecl,
DefaultableMemberKind Kind) {
if (!RecDecl)
return false;
switch (Kind) {
case DefaultableMemberKind::DefaultConstructor:
return RecDecl->hasTrivialDefaultConstructor();
case DefaultableMemberKind::CopyConstructor:
return RecDecl->hasTrivialCopyConstructor();
case DefaultableMemberKind::MoveConstructor:
return RecDecl->hasTrivialMoveConstructor();
case DefaultableMemberKind::CopyAssignment:
return RecDecl->hasTrivialCopyAssignment();
case DefaultableMemberKind::MoveAssignment:
return RecDecl->hasTrivialMoveAssignment();
case DefaultableMemberKind::Destructor:
return RecDecl->hasTrivialDestructor();
default:
return false;
}
}
bool ExceptionSpecAnalyzer::isConstructor(DefaultableMemberKind Kind) {
switch (Kind) {
case DefaultableMemberKind::DefaultConstructor:
case DefaultableMemberKind::CopyConstructor:
case DefaultableMemberKind::MoveConstructor:
return true;
default:
return false;
}
}
bool ExceptionSpecAnalyzer::isSpecialMember(DefaultableMemberKind Kind) {
switch (Kind) {
case DefaultableMemberKind::DefaultConstructor:
case DefaultableMemberKind::CopyConstructor:
case DefaultableMemberKind::MoveConstructor:
case DefaultableMemberKind::CopyAssignment:
case DefaultableMemberKind::MoveAssignment:
case DefaultableMemberKind::Destructor:
return true;
default:
return false;
}
}
bool ExceptionSpecAnalyzer::isComparison(DefaultableMemberKind Kind) {
switch (Kind) {
case DefaultableMemberKind::CompareEqual:
case DefaultableMemberKind::CompareNotEqual:
case DefaultableMemberKind::CompareRelational:
case DefaultableMemberKind::CompareThreeWay:
return true;
default:
return false;
}
}
ExceptionSpecAnalyzer::DefaultableMemberKind
ExceptionSpecAnalyzer::getDefaultableMemberKind(const FunctionDecl *FuncDecl) {
if (const auto *MethodDecl = dyn_cast<CXXMethodDecl>(FuncDecl)) {
if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(FuncDecl)) {
if (Ctor->isDefaultConstructor())
return DefaultableMemberKind::DefaultConstructor;
if (Ctor->isCopyConstructor())
return DefaultableMemberKind::CopyConstructor;
if (Ctor->isMoveConstructor())
return DefaultableMemberKind::MoveConstructor;
}
if (MethodDecl->isCopyAssignmentOperator())
return DefaultableMemberKind::CopyAssignment;
if (MethodDecl->isMoveAssignmentOperator())
return DefaultableMemberKind::MoveAssignment;
if (isa<CXXDestructorDecl>(FuncDecl))
return DefaultableMemberKind::Destructor;
}
const LangOptions &LangOpts = FuncDecl->getLangOpts();
switch (FuncDecl->getDeclName().getCXXOverloadedOperator()) {
case OO_EqualEqual:
return DefaultableMemberKind::CompareEqual;
case OO_ExclaimEqual:
return DefaultableMemberKind::CompareNotEqual;
case OO_Spaceship:
// No point allowing this if <=> doesn't exist in the current language mode.
if (!LangOpts.CPlusPlus20)
break;
return DefaultableMemberKind::CompareThreeWay;
case OO_Less:
case OO_LessEqual:
case OO_Greater:
case OO_GreaterEqual:
// No point allowing this if <=> doesn't exist in the current language mode.
if (!LangOpts.CPlusPlus20)
break;
return DefaultableMemberKind::CompareRelational;
default:
break;
}
// Not a defaultable member kind
return DefaultableMemberKind::None;
}
} // namespace clang::tidy::utils