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

280 lines
10 KiB
C++

//===--- AvoidCStyleCastsCheck.cpp - clang-tidy -----------------*- 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
//
//===----------------------------------------------------------------------===//
#include "AvoidCStyleCastsCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang::tidy::google::readability {
void AvoidCStyleCastsCheck::registerMatchers(
ast_matchers::MatchFinder *Finder) {
Finder->addMatcher(
cStyleCastExpr(
// Filter out (EnumType)IntegerLiteral construct, which is generated
// for non-type template arguments of enum types.
// FIXME: Remove this once this is fixed in the AST.
unless(hasParent(substNonTypeTemplateParmExpr())))
.bind("cast"),
this);
Finder->addMatcher(
cxxFunctionalCastExpr(
hasDestinationType(hasCanonicalType(anyOf(
builtinType(), references(qualType()), pointsTo(qualType())))),
unless(
hasSourceExpression(anyOf(cxxConstructExpr(), initListExpr()))))
.bind("cast"),
this);
}
static bool needsConstCast(QualType SourceType, QualType DestType) {
while ((SourceType->isPointerType() && DestType->isPointerType()) ||
(SourceType->isReferenceType() && DestType->isReferenceType())) {
SourceType = SourceType->getPointeeType();
DestType = DestType->getPointeeType();
if (SourceType.isConstQualified() && !DestType.isConstQualified()) {
return (SourceType->isPointerType() == DestType->isPointerType()) &&
(SourceType->isReferenceType() == DestType->isReferenceType());
}
}
return false;
}
static bool pointedUnqualifiedTypesAreEqual(QualType T1, QualType T2) {
while ((T1->isPointerType() && T2->isPointerType()) ||
(T1->isReferenceType() && T2->isReferenceType())) {
T1 = T1->getPointeeType();
T2 = T2->getPointeeType();
}
return T1.getUnqualifiedType() == T2.getUnqualifiedType();
}
static clang::CharSourceRange getReplaceRange(const ExplicitCastExpr *Expr) {
if (const auto *CastExpr = dyn_cast<CStyleCastExpr>(Expr))
return CharSourceRange::getCharRange(
CastExpr->getLParenLoc(),
CastExpr->getSubExprAsWritten()->getBeginLoc());
if (const auto *CastExpr = dyn_cast<CXXFunctionalCastExpr>(Expr))
return CharSourceRange::getCharRange(CastExpr->getBeginLoc(),
CastExpr->getLParenLoc());
llvm_unreachable("Unsupported CastExpr");
}
static StringRef getDestTypeString(const SourceManager &SM,
const LangOptions &LangOpts,
const ExplicitCastExpr *Expr) {
SourceLocation BeginLoc;
SourceLocation EndLoc;
if (const auto *CastExpr = dyn_cast<CStyleCastExpr>(Expr)) {
BeginLoc = CastExpr->getLParenLoc().getLocWithOffset(1);
EndLoc = CastExpr->getRParenLoc().getLocWithOffset(-1);
} else if (const auto *CastExpr = dyn_cast<CXXFunctionalCastExpr>(Expr)) {
BeginLoc = CastExpr->getBeginLoc();
EndLoc = CastExpr->getLParenLoc().getLocWithOffset(-1);
} else
llvm_unreachable("Unsupported CastExpr");
return Lexer::getSourceText(CharSourceRange::getTokenRange(BeginLoc, EndLoc),
SM, LangOpts);
}
static bool sameTypeAsWritten(QualType X, QualType Y) {
if (X.getCanonicalType() != Y.getCanonicalType())
return false;
auto TC = X->getTypeClass();
if (TC != Y->getTypeClass())
return false;
switch (TC) {
case Type::Typedef:
return declaresSameEntity(cast<TypedefType>(X)->getDecl(),
cast<TypedefType>(Y)->getDecl());
case Type::Pointer:
return sameTypeAsWritten(cast<PointerType>(X)->getPointeeType(),
cast<PointerType>(Y)->getPointeeType());
case Type::RValueReference:
case Type::LValueReference:
return sameTypeAsWritten(cast<ReferenceType>(X)->getPointeeType(),
cast<ReferenceType>(Y)->getPointeeType());
default:
return true;
}
}
void AvoidCStyleCastsCheck::check(const MatchFinder::MatchResult &Result) {
const auto *CastExpr = Result.Nodes.getNodeAs<ExplicitCastExpr>("cast");
// Ignore casts in macros.
if (CastExpr->getExprLoc().isMacroID())
return;
// Casting to void is an idiomatic way to mute "unused variable" and similar
// warnings.
if (CastExpr->getCastKind() == CK_ToVoid)
return;
auto IsFunction = [](QualType T) {
T = T.getCanonicalType().getNonReferenceType();
return T->isFunctionType() || T->isFunctionPointerType() ||
T->isMemberFunctionPointerType();
};
const QualType DestTypeAsWritten =
CastExpr->getTypeAsWritten().getUnqualifiedType();
const QualType SourceTypeAsWritten =
CastExpr->getSubExprAsWritten()->getType().getUnqualifiedType();
const QualType SourceType = SourceTypeAsWritten.getCanonicalType();
const QualType DestType = DestTypeAsWritten.getCanonicalType();
CharSourceRange ReplaceRange = getReplaceRange(CastExpr);
bool FnToFnCast =
IsFunction(SourceTypeAsWritten) && IsFunction(DestTypeAsWritten);
const bool ConstructorCast = !CastExpr->getTypeAsWritten().hasQualifiers() &&
DestTypeAsWritten->isRecordType() &&
!DestTypeAsWritten->isElaboratedTypeSpecifier();
if (CastExpr->getCastKind() == CK_NoOp && !FnToFnCast) {
// Function pointer/reference casts may be needed to resolve ambiguities in
// case of overloaded functions, so detection of redundant casts is trickier
// in this case. Don't emit "redundant cast" warnings for function
// pointer/reference types.
if (sameTypeAsWritten(SourceTypeAsWritten, DestTypeAsWritten)) {
diag(CastExpr->getBeginLoc(), "redundant cast to the same type")
<< FixItHint::CreateRemoval(ReplaceRange);
return;
}
}
// The rest of this check is only relevant to C++.
// We also disable it for Objective-C++.
if (!getLangOpts().CPlusPlus || getLangOpts().ObjC)
return;
// Ignore code inside extern "C" {} blocks.
if (!match(expr(hasAncestor(linkageSpecDecl())), *CastExpr, *Result.Context)
.empty())
return;
// Ignore code in .c files and headers included from them, even if they are
// compiled as C++.
if (getCurrentMainFile().ends_with(".c"))
return;
SourceManager &SM = *Result.SourceManager;
// Ignore code in .c files #included in other files (which shouldn't be done,
// but people still do this for test and other purposes).
if (SM.getFilename(SM.getSpellingLoc(CastExpr->getBeginLoc()))
.ends_with(".c"))
return;
// Leave type spelling exactly as it was (unlike
// getTypeAsWritten().getAsString() which would spell enum types 'enum X').
StringRef DestTypeString = getDestTypeString(SM, getLangOpts(), CastExpr);
auto Diag =
diag(CastExpr->getBeginLoc(), "C-style casts are discouraged; use %0");
auto ReplaceWithCast = [&](std::string CastText) {
const Expr *SubExpr = CastExpr->getSubExprAsWritten()->IgnoreImpCasts();
if (!isa<ParenExpr>(SubExpr) && !isa<CXXFunctionalCastExpr>(CastExpr)) {
CastText.push_back('(');
Diag << FixItHint::CreateInsertion(
Lexer::getLocForEndOfToken(SubExpr->getEndLoc(), 0, SM,
getLangOpts()),
")");
}
Diag << FixItHint::CreateReplacement(ReplaceRange, CastText);
};
auto ReplaceWithNamedCast = [&](StringRef CastType) {
Diag << CastType;
ReplaceWithCast((CastType + "<" + DestTypeString + ">").str());
};
auto ReplaceWithConstructorCall = [&]() {
Diag << "constructor call syntax";
// FIXME: Validate DestTypeString, maybe.
ReplaceWithCast(DestTypeString.str());
};
// Suggest appropriate C++ cast. See [expr.cast] for cast notation semantics.
switch (CastExpr->getCastKind()) {
case CK_FunctionToPointerDecay:
ReplaceWithNamedCast("static_cast");
return;
case CK_ConstructorConversion:
if (ConstructorCast) {
ReplaceWithConstructorCall();
} else {
ReplaceWithNamedCast("static_cast");
}
return;
case CK_NoOp:
if (FnToFnCast) {
ReplaceWithNamedCast("static_cast");
return;
}
if (SourceType == DestType) {
Diag << "static_cast (if needed, the cast may be redundant)";
ReplaceWithCast(("static_cast<" + DestTypeString + ">").str());
return;
}
if (needsConstCast(SourceType, DestType) &&
pointedUnqualifiedTypesAreEqual(SourceType, DestType)) {
ReplaceWithNamedCast("const_cast");
return;
}
if (ConstructorCast) {
ReplaceWithConstructorCall();
return;
}
if (DestType->isReferenceType()) {
QualType Dest = DestType.getNonReferenceType();
QualType Source = SourceType.getNonReferenceType();
if (Source == Dest.withConst() ||
SourceType.getNonReferenceType() == DestType.getNonReferenceType()) {
ReplaceWithNamedCast("const_cast");
return;
}
break;
}
[[fallthrough]];
case clang::CK_IntegralCast:
// Convert integral and no-op casts between builtin types and enums to
// static_cast. A cast from enum to integer may be unnecessary, but it's
// still retained.
if ((SourceType->isBuiltinType() || SourceType->isEnumeralType()) &&
(DestType->isBuiltinType() || DestType->isEnumeralType())) {
ReplaceWithNamedCast("static_cast");
return;
}
break;
case CK_BitCast:
// FIXME: Suggest const_cast<...>(reinterpret_cast<...>(...)) replacement.
if (!needsConstCast(SourceType, DestType)) {
if (SourceType->isVoidPointerType())
ReplaceWithNamedCast("static_cast");
else
ReplaceWithNamedCast("reinterpret_cast");
return;
}
break;
default:
break;
}
Diag << "static_cast/const_cast/reinterpret_cast";
}
} // namespace clang::tidy::google::readability