llvm-project/clang-tools-extra/clang-tidy/misc/UnusedUsingDeclsCheck.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

228 lines
8.5 KiB
C++

//===--- UnusedUsingDeclsCheck.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 "UnusedUsingDeclsCheck.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/Lex/Lexer.h"
using namespace clang::ast_matchers;
namespace clang::tidy::misc {
namespace {
AST_MATCHER_P(DeducedTemplateSpecializationType, refsToTemplatedDecl,
clang::ast_matchers::internal::Matcher<NamedDecl>, DeclMatcher) {
if (const auto *TD = Node.getTemplateName().getAsTemplateDecl())
return DeclMatcher.matches(*TD, Finder, Builder);
return false;
}
AST_MATCHER_P(Type, asTagDecl, clang::ast_matchers::internal::Matcher<TagDecl>,
DeclMatcher) {
if (const TagDecl *ND = Node.getAsTagDecl())
return DeclMatcher.matches(*ND, Finder, Builder);
return false;
}
} // namespace
// A function that helps to tell whether a TargetDecl in a UsingDecl will be
// checked. Only variable, function, function template, class template, class,
// enum declaration and enum constant declaration are considered.
static bool shouldCheckDecl(const Decl *TargetDecl) {
return isa<RecordDecl>(TargetDecl) || isa<ClassTemplateDecl>(TargetDecl) ||
isa<FunctionDecl>(TargetDecl) || isa<VarDecl>(TargetDecl) ||
isa<FunctionTemplateDecl>(TargetDecl) || isa<EnumDecl>(TargetDecl) ||
isa<EnumConstantDecl>(TargetDecl);
}
UnusedUsingDeclsCheck::UnusedUsingDeclsCheck(StringRef Name,
ClangTidyContext *Context)
: ClangTidyCheck(Name, Context),
HeaderFileExtensions(Context->getHeaderFileExtensions()) {}
void UnusedUsingDeclsCheck::registerMatchers(MatchFinder *Finder) {
// We don't emit warnings on unused-using-decls from headers, so bail out if
// the main file is a header.
if (utils::isFileExtension(getCurrentMainFile(), HeaderFileExtensions))
return;
Finder->addMatcher(usingDecl(isExpansionInMainFile()).bind("using"), this);
auto DeclMatcher = hasDeclaration(namedDecl().bind("used"));
Finder->addMatcher(loc(templateSpecializationType(DeclMatcher)), this);
Finder->addMatcher(loc(deducedTemplateSpecializationType(
refsToTemplatedDecl(namedDecl().bind("used")))),
this);
Finder->addMatcher(callExpr(callee(unresolvedLookupExpr().bind("used"))),
this);
Finder->addMatcher(
callExpr(hasDeclaration(functionDecl(
forEachTemplateArgument(templateArgument().bind("used"))))),
this);
Finder->addMatcher(loc(templateSpecializationType(forEachTemplateArgument(
templateArgument().bind("used")))),
this);
Finder->addMatcher(userDefinedLiteral().bind("used"), this);
Finder->addMatcher(loc(asTagDecl(tagDecl().bind("used"))), this);
// Cases where we can identify the UsingShadowDecl directly, rather than
// just its target.
// FIXME: cover more cases in this way, as the AST supports it.
auto ThroughShadowMatcher = throughUsingDecl(namedDecl().bind("usedShadow"));
Finder->addMatcher(declRefExpr(ThroughShadowMatcher), this);
Finder->addMatcher(loc(usingType(ThroughShadowMatcher)), this);
}
void UnusedUsingDeclsCheck::check(const MatchFinder::MatchResult &Result) {
if (Result.Context->getDiagnostics().hasUncompilableErrorOccurred())
return;
if (const auto *Using = Result.Nodes.getNodeAs<UsingDecl>("using")) {
// Ignores using-declarations defined in macros.
if (Using->getLocation().isMacroID())
return;
// Ignores using-declarations defined in class definition.
if (isa<CXXRecordDecl>(Using->getDeclContext()))
return;
// FIXME: We ignore using-decls defined in function definitions at the
// moment because of false positives caused by ADL and different function
// scopes.
if (isa<FunctionDecl>(Using->getDeclContext()))
return;
UsingDeclContext Context(Using);
Context.UsingDeclRange = CharSourceRange::getCharRange(
Using->getBeginLoc(),
Lexer::findLocationAfterToken(
Using->getEndLoc(), tok::semi, *Result.SourceManager, getLangOpts(),
/*SkipTrailingWhitespaceAndNewLine=*/true));
for (const auto *UsingShadow : Using->shadows()) {
const auto *TargetDecl = UsingShadow->getTargetDecl()->getCanonicalDecl();
if (shouldCheckDecl(TargetDecl)) {
Context.UsingTargetDecls.insert(TargetDecl);
UsingTargetDeclsCache.insert(TargetDecl);
}
}
if (!Context.UsingTargetDecls.empty())
Contexts.push_back(Context);
return;
}
// Mark a corresponding using declaration as used.
auto RemoveNamedDecl = [&](const NamedDecl *Used) {
removeFromFoundDecls(Used);
// Also remove variants of Used.
if (const auto *FD = dyn_cast<FunctionDecl>(Used)) {
removeFromFoundDecls(FD->getPrimaryTemplate());
return;
}
if (const auto *Specialization =
dyn_cast<ClassTemplateSpecializationDecl>(Used)) {
removeFromFoundDecls(Specialization->getSpecializedTemplate());
return;
}
if (const auto *ECD = dyn_cast<EnumConstantDecl>(Used)) {
if (const auto *ET = ECD->getType()->getAs<EnumType>())
removeFromFoundDecls(ET->getOriginalDecl());
}
};
// We rely on the fact that the clang AST is walked in order, usages are only
// marked after a corresponding using decl has been found.
if (const auto *Used = Result.Nodes.getNodeAs<NamedDecl>("used")) {
RemoveNamedDecl(Used);
return;
}
if (const auto *UsedShadow =
Result.Nodes.getNodeAs<UsingShadowDecl>("usedShadow")) {
removeFromFoundDecls(UsedShadow->getTargetDecl());
return;
}
if (const auto *Used = Result.Nodes.getNodeAs<TemplateArgument>("used")) {
if (Used->getKind() == TemplateArgument::Template) {
if (const auto *TD = Used->getAsTemplate().getAsTemplateDecl())
removeFromFoundDecls(TD);
return;
}
if (Used->getKind() == TemplateArgument::Type) {
if (auto *RD = Used->getAsType()->getAsCXXRecordDecl())
removeFromFoundDecls(RD);
return;
}
if (Used->getKind() == TemplateArgument::Declaration) {
RemoveNamedDecl(Used->getAsDecl());
}
return;
}
if (const auto *DRE = Result.Nodes.getNodeAs<DeclRefExpr>("used")) {
RemoveNamedDecl(DRE->getDecl());
return;
}
// Check the uninstantiated template function usage.
if (const auto *ULE = Result.Nodes.getNodeAs<UnresolvedLookupExpr>("used")) {
for (const NamedDecl *ND : ULE->decls()) {
if (const auto *USD = dyn_cast<UsingShadowDecl>(ND))
removeFromFoundDecls(USD->getTargetDecl()->getCanonicalDecl());
}
return;
}
// Check user-defined literals
if (const auto *UDL = Result.Nodes.getNodeAs<UserDefinedLiteral>("used")) {
const Decl *CalleeDecl = UDL->getCalleeDecl();
if (const auto *FD = dyn_cast<FunctionDecl>(CalleeDecl)) {
if (const FunctionTemplateDecl *FPT = FD->getPrimaryTemplate()) {
removeFromFoundDecls(FPT);
return;
}
}
removeFromFoundDecls(CalleeDecl);
}
}
void UnusedUsingDeclsCheck::removeFromFoundDecls(const Decl *D) {
if (!D)
return;
const Decl *CanonicalDecl = D->getCanonicalDecl();
if (!UsingTargetDeclsCache.contains(CanonicalDecl))
return;
// FIXME: Currently, we don't handle the using-decls being used in different
// scopes (such as different namespaces, different functions). Instead of
// giving an incorrect message, we mark all of them as used.
for (auto &Context : Contexts) {
if (Context.IsUsed)
continue;
if (Context.UsingTargetDecls.contains(CanonicalDecl))
Context.IsUsed = true;
}
}
void UnusedUsingDeclsCheck::onEndOfTranslationUnit() {
for (const auto &Context : Contexts) {
if (!Context.IsUsed) {
diag(Context.FoundUsingDecl->getLocation(), "using decl %0 is unused")
<< Context.FoundUsingDecl;
// Emit a fix and a fix description of the check;
diag(Context.FoundUsingDecl->getLocation(),
/*Description=*/"remove the using", DiagnosticIDs::Note)
<< FixItHint::CreateRemoval(Context.UsingDeclRange);
}
}
Contexts.clear();
UsingTargetDeclsCache.clear();
}
} // namespace clang::tidy::misc