Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clang AST : extern LinkageSpec issue

I'm currently writing a static C++ code analyser using clang's python bindings and for some reason, I can not find wether something is extern or not in the AST eventhough there is a CursorKind which seems to be apropriate (CXCursor_LinkageSpec)

What I mean is that when parsing code like extern int foo; I will only find my variable foo in the AST and not a single clue of its linkage specifications.

What am I missing?
Regards

like image 877
user1584775 Avatar asked Feb 04 '26 13:02

user1584775


2 Answers

class VarDecl has a member function: bool hasExternalStorage () const which tells you whether the variable is extern or not.

I'm using clang's C++ lib. Hope it will help with your python work.

like image 162
wolf5x Avatar answered Feb 06 '26 02:02

wolf5x


Bit of a necroanswer but if you go in to clang\lib\Sema\SemaCodeComplete.cpp(in \llvm\tools\ if you follow llvm's installation instructions) and add the following line:

case Decl::LinkageSpec:  return CXCursor_LinkageSpec;

To the switch in:

CXCursorKind clang::getCursorKindForDecl(const Decl *D)

It should resolve the issue of clang's Python binder returning UNEXPOSED_DECL instead of the correct LINKAGE_SPEC. This change was made at revision 183352(2013-06-05).

Example from my version:

CXCursorKind clang::getCursorKindForDecl(const Decl *D) {
if (!D)
    return CXCursor_UnexposedDecl;

switch (D->getKind()) {
    case Decl::Enum:               return CXCursor_EnumDecl; 
    case Decl::LinkageSpec:  return CXCursor_LinkageSpec;
   // ......
like image 39
Jerdak Avatar answered Feb 06 '26 03:02

Jerdak