Skip to content

[include-cleaner] Respect the UsingShadowDecl when find headers for ambiguous std symbols. #66485

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions clang-tools-extra/include-cleaner/lib/FindHeaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclBase.h"
#include "clang/AST/DeclCXX.h"
#include "clang/Basic/Builtins.h"
#include "clang/Basic/FileEntry.h"
#include "clang/Basic/SourceLocation.h"
Expand Down Expand Up @@ -116,6 +117,8 @@ std::optional<tooling::stdlib::Header>
headerForAmbiguousStdSymbol(const NamedDecl *ND) {
if (!ND->isInStdNamespace())
return {};
if (auto* USD = llvm::dyn_cast<UsingShadowDecl>(ND))
ND = USD->getTargetDecl();
const auto *FD = ND->getAsFunction();
if (!FD)
return std::nullopt;
Expand Down
31 changes: 31 additions & 0 deletions clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include "clang-include-cleaner/Analysis.h"
#include "clang-include-cleaner/Record.h"
#include "clang-include-cleaner/Types.h"
#include "clang/AST/Expr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/FileEntry.h"
#include "clang/Basic/FileManager.h"
Expand Down Expand Up @@ -587,6 +588,36 @@ TEST_F(HeadersForSymbolTest, AmbiguousStdSymbols) {
}
}

TEST_F(HeadersForSymbolTest, AmbiguousStdSymbolsUsingShadow) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might be easier to test this in AnalysisTest, rather than writing a new ASTVisitor, but up to you.

Inputs.Code = R"cpp(
void remove(char*);
namespace std { using ::remove; }

void k() {
std::remove("abc");
}
)cpp";
buildAST();

// Find the DeclRefExpr in the std::remove("abc") function call.
struct Visitor : public RecursiveASTVisitor<Visitor> {
const DeclRefExpr *Out = nullptr;
bool VisitDeclRefExpr(const DeclRefExpr *DRE) {
EXPECT_TRUE(Out == nullptr) << "Found multiple DeclRefExpr!";
Out = DRE;
return true;
}
};
Visitor V;
V.TraverseDecl(AST->context().getTranslationUnitDecl());
ASSERT_TRUE(V.Out) << "Couldn't find a DeclRefExpr!";
EXPECT_THAT(headersForSymbol(*(V.Out->getFoundDecl()),
AST->sourceManager(), &PI),
UnorderedElementsAre(
Header(*tooling::stdlib::Header::named("<cstdio>"))));
}


TEST_F(HeadersForSymbolTest, StandardHeaders) {
Inputs.Code = "void assert();";
buildAST();
Expand Down