Access specifiers of NamedDecl

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Access specifiers of NamedDecl

Fangrui Song via cfe-dev
Hi,

I'm writing here because I have a problem with clang tooling :)

I'm developing a tool for my company which uses clang to parse a C++ header,
and get some information about it. I would like to find out if a NamedDecl is
accessible in a given scope.

e.g. given the following source code, i would like to know the accessibility
from f() of the class returned by each public method in A
(A::B, A::C<int> and A::D)

```
class A {
private:
   class B { public: class B2 {}; };
   template<typename T>
   class C {};

public:
   class D {};

// --- some getters
   B getB();
   B::B2 getB2();
   C<int> getC();
   D getD();
};

void f() {
   // queries from here
   // e.g:
   A::B x; // error: B is private
   A::C<int> y; // error: C is private
   A::D z; // ok, A::D is public
   
   // i'm not interested in auto X = A().getB();, which I know it works
}
```

In this example I would like to have these results:
A::B -> no
A::B::B2 -> no
A::C<int> -> no
A::D -> yes

I'm using an ASTVisitor to visit each CXXRecordDecl.
As a first approach, I tried to use getAccess() on each CXXRecordDecl obtained
with getReturnType().
It works almost everywhere, expect when the type is template specialization
(e.g. A::C<int>), which access happens to be AS_none and not AS_private as
I would expect.

I have seen that there is SemaAccess.cpp with a lot of functions to query the
accessbility but i'm not able to get it to work, I'm not even sure that this is
the right way to go.

You can find below the visitor I used to test this idea, compiled with:
clang++ -Wall -std=c++17 visitor.cpp -lclang -lclangAST -lclangTooling \
         -lclangFrontend -lclangSerialization -lclangBasic -lLLVM

Source code: https://pastebin.com/hirj5cYk

and the output is:

---
B: Acc=AS_private; Visible=0
B2: Acc=AS_public; Visible=0
C: Acc=AS_none; Visible=1 <- unexpected, C is private in A
D: Acc=AS_public; Visible=1
---

I would really appreciate your help to understand if my approach is right
or if there is something already implemented which I can use in Sema.

Thanks,
Alain

_______________________________________________
cfe-dev mailing list
[hidden email]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev