[clang-tools-extra] [clang][include-cleaner]skip stdlib recogntion only when there are defintion with body in main file. (PR #95797)
Congcong Cai via cfe-commits
cfe-commits at lists.llvm.org
Mon Jun 17 07:46:15 PDT 2024
https://github.com/HerrCai0907 created https://github.com/llvm/llvm-project/pull/95797
When a definition is being located, standard library function recognition is skipped if the function has a definition with body in the main file
>From 71a2c0d29058edac2539fbbe406890e6a6dc875c Mon Sep 17 00:00:00 2001
From: Congcong Cai <congcongcai0907 at 163.com>
Date: Mon, 17 Jun 2024 14:44:22 +0000
Subject: [PATCH] [clang][include-cleaner]skip stdlib recogntion only when
there are defintion with body in main file.
When a definition is being located, standard library function recognition is skipped if the function has a definition with body in the main file
---
.../include-cleaner/lib/LocateSymbol.cpp | 19 ++++++++++++-------
.../unittests/FindHeadersTest.cpp | 9 +++------
2 files changed, 15 insertions(+), 13 deletions(-)
diff --git a/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp b/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp
index 9148d36a5038f..2ddc807871af1 100644
--- a/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp
+++ b/clang-tools-extra/include-cleaner/lib/LocateSymbol.cpp
@@ -13,6 +13,7 @@
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/Tooling/Inclusions/StandardLibrary.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/raw_ostream.h"
#include <utility>
@@ -39,13 +40,16 @@ Hints declHints(const Decl *D) {
}
std::vector<Hinted<SymbolLocation>> locateDecl(const Decl &D) {
- std::vector<Hinted<SymbolLocation>> Result;
- // FIXME: Should we also provide physical locations?
- if (auto SS = tooling::stdlib::Recognizer()(&D)) {
- Result.push_back({*SS, Hints::CompleteSymbol});
- if (!D.hasBody())
- return Result;
- }
+ SourceManager &SM = D.getASTContext().getSourceManager();
+ bool HasBodyInMainFile = llvm::any_of(D.redecls(), [&](Decl *Redecl) {
+ return Redecl->hasBody() && SM.isInMainFile(Redecl->getLocation());
+ });
+ // if decl has body and in main file, we don't need to do further search.
+ if (!HasBodyInMainFile)
+ // FIXME: Should we also provide physical locations?
+ if (auto SS = tooling::stdlib::Recognizer()(&D))
+ return {{*SS, Hints::CompleteSymbol}};
+
// FIXME: Signal foreign decls, e.g. a forward declaration not owned by a
// library. Some useful signals could be derived by checking the DeclContext.
// Most incidental forward decls look like:
@@ -53,6 +57,7 @@ std::vector<Hinted<SymbolLocation>> locateDecl(const Decl &D) {
// class SourceManager; // likely an incidental forward decl.
// namespace my_own_ns {}
// }
+ std::vector<Hinted<SymbolLocation>> Result;
for (auto *Redecl : D.redecls())
Result.push_back({Redecl->getLocation(), declHints(Redecl)});
return Result;
diff --git a/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp b/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
index fdcbf25fd628c..f1eadd113dead 100644
--- a/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
+++ b/clang-tools-extra/include-cleaner/unittests/FindHeadersTest.cpp
@@ -631,12 +631,9 @@ TEST_F(HeadersForSymbolTest, StandardHeaders) {
TEST_F(HeadersForSymbolTest, NonStandardHeaders) {
Inputs.Code = "void assert() {}";
buildAST();
- EXPECT_THAT(
- headersFor("assert"),
- // Respect the ordering from the stdlib mapping.
- UnorderedElementsAre(physicalHeader("input.mm"),
- tooling::stdlib::Header::named("<cassert>"),
- tooling::stdlib::Header::named("<assert.h>")));
+ EXPECT_THAT(headersFor("assert"),
+ // Respect the ordering from the stdlib mapping.
+ UnorderedElementsAre(physicalHeader("input.mm")));
}
TEST_F(HeadersForSymbolTest, ExporterNoNameMatch) {
More information about the cfe-commits
mailing list