[PATCH] D138678: [include-cleaner] Capture private headers in PragmaIncludes.
Viktoriia Bakalova via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Thu Nov 24 08:56:14 PST 2022
VitaNuo created this revision.
Herald added a project: All.
VitaNuo requested review of this revision.
Herald added a project: clang-tools-extra.
Herald added a subscriber: cfe-commits.
Save file IDs of IWYU private headers and report them as private.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D138678
Files:
clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h
clang-tools-extra/include-cleaner/lib/Record.cpp
clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
Index: clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
===================================================================
--- clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
+++ clang-tools-extra/include-cleaner/unittests/RecordTest.cpp
@@ -368,11 +368,18 @@
Inputs.Code = R"cpp(
#include "public.h"
)cpp";
- Inputs.ExtraFiles["public.h"] = "#include \"private.h\"";
+ Inputs.ExtraFiles["public.h"] = R"cpp(
+ #include "private.h";
+ #include "private2.h";
+ )cpp";
Inputs.ExtraFiles["private.h"] = R"cpp(
// IWYU pragma: private, include "public2.h"
class Private {};
)cpp";
+ Inputs.ExtraFiles["private2.h"] = R"cpp(
+ // IWYU pragma: private
+ class Private {};
+ )cpp";
TestAST Processed = build();
auto PrivateFE = Processed.fileManager().getFile("private.h");
assert(PrivateFE);
@@ -380,6 +387,10 @@
auto PublicFE = Processed.fileManager().getFile("public.h");
assert(PublicFE);
EXPECT_EQ(PI.getPublic(PublicFE.get()), ""); // no mapping.
+
+ auto Private2FE = Processed.fileManager().getFile("private2.h");
+ assert(Private2FE);
+ EXPECT_TRUE(PI.isPrivate(Private2FE.get()));
}
TEST_F(PragmaIncludeTest, IWYUExport) {
Index: clang-tools-extra/include-cleaner/lib/Record.cpp
===================================================================
--- clang-tools-extra/include-cleaner/lib/Record.cpp
+++ clang-tools-extra/include-cleaner/lib/Record.cpp
@@ -263,6 +263,8 @@
assert(ExportStack.back().Block);
ExportStack.pop_back();
}
+ } else if (Pragma->startswith("private")) {
+ Out->IWYUPrivate.insert(SM.getFileEntryForID(CommentFID)->getUniqueID());
}
if (InMainFile) {
@@ -346,6 +348,10 @@
return !NonSelfContainedFiles.contains(FE->getUniqueID());
}
+bool PragmaIncludes::isPrivate(const FileEntry *FE) const {
+ return IWYUPrivate.contains(FE->getUniqueID());
+}
+
std::unique_ptr<ASTConsumer> RecordedAST::record() {
class Recorder : public ASTConsumer {
RecordedAST *Out;
Index: clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h
===================================================================
--- clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h
+++ clang-tools-extra/include-cleaner/include/clang-include-cleaner/Record.h
@@ -73,6 +73,10 @@
/// Returns true if the given file is a self-contained file.
bool isSelfContained(const FileEntry *File) const;
+ /// Returns true if the given file is marked with the IWYU private pragma
+ /// without public mapping.
+ bool isPrivate(const FileEntry *File) const;
+
private:
class RecordPragma;
/// 1-based Line numbers for the #include directives of the main file that
@@ -88,6 +92,10 @@
llvm::DenseMap<llvm::sys::fs::UniqueID, llvm::StringRef /*VerbatimSpelling*/>
IWYUPublic;
+ /// Contains all files marked with the IWYU private pragma that
+ /// do not have public header mapping
+ llvm::DenseSet<llvm::sys::fs::UniqueID> IWYUPrivate;
+
/// A reverse map from the underlying header to its exporter headers.
//
// There's no way to get a FileEntry from a UniqueID, especially when it
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D138678.477815.patch
Type: text/x-patch
Size: 3190 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20221124/d6d38700/attachment.bin>
More information about the cfe-commits
mailing list