Skip to content

optionally export private static fields of classes with friends #55

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
23 changes: 20 additions & 3 deletions Sources/idt/idt.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ apply_fixits("apply-fixits", llvm::cl::init(false),
llvm::cl::desc("Apply suggested changes to decorate interfaces"),
llvm::cl::cat(idt::category));

llvm::cl::opt<bool>
friendly_fields("friendly-fields", llvm::cl::init(false),
llvm::cl::desc("Export private fields of classes with friends"),
llvm::cl::cat(idt::category));

llvm::cl::opt<bool>
inplace("inplace", llvm::cl::init(false),
llvm::cl::desc("Apply suggested changes in-place"),
Expand Down Expand Up @@ -291,6 +296,16 @@ class visitor : public clang::RecursiveASTVisitor<visitor> {
return false;
}

template <typename Decl_>
bool parent_record_has_friends(const Decl_ *D) const {
if (auto *ParentRecord = llvm::dyn_cast<clang::CXXRecordDecl>(D->getDeclContext()))
for (auto *PD : ParentRecord->decls())
if (llvm::dyn_cast<clang::FriendDecl>(PD))
return true;

return false;
}

// Determine if a function needs exporting and add the export annotation as
// required.
void export_function_if_needed(const clang::FunctionDecl *FD) {
Expand Down Expand Up @@ -549,9 +564,11 @@ class visitor : public clang::RecursiveASTVisitor<visitor> {
// VisitVarDecl will visit all variable declarations as well as static fields
// in classes and structs. Non-static fields are not visited by this method.
bool VisitVarDecl(clang::VarDecl *VD) {
// Ignore private static field declarations. Any that require export will be
// identified by VisitDeclRefExpr.
if (VD->getAccess() == clang::AccessSpecifier::AS_private)
// Ignore private static field declarations unless the class has friends
// that may need access to them. Any other private fields requiring export
// will be identified by VisitDeclRefExpr.
if (VD->getAccess() == clang::AccessSpecifier::AS_private &&
(!friendly_fields || !parent_record_has_friends(VD)))
return true;

export_variable_if_needed(VD);
Expand Down
14 changes: 14 additions & 0 deletions Tests/FriendReadsPrivateStaticField.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// RUN: %idt --friendly-fields --export-macro IDT_TEST_ABI %s 2>&1 | %FileCheck %s

struct KeyType {};

template <typename T> struct FriendTemplate {
static KeyType *getKey() { return &T::Key; }
};

class ClassWithPrivateStaticField : public FriendTemplate<ClassWithPrivateStaticField> {
friend FriendTemplate<ClassWithPrivateStaticField>;

// CHECK: FriendReadsPrivateStaticField.hh:[[@LINE+1]]:3: remark: unexported public interface 'Key'
static KeyType Key;
};
Loading