Clang ASTVisitor Extern Variable usage

Hello All,

For a clang tool based on RecursiveASTVisitor, would like to detect the definition of an extern variable.

For the below code file samples, while traversing the Main.c, can we detect the “var” is defined & initialized in test.c

Below are the file definition

<<<<Main.c>>>>

#include “h1.h”

int main()

{

var = 10;

}

<<<<h1.h>>>>

extern int var;

<<<<test.c>>>>

#include “h1.h”

int var = 11;

I believe the short answer is “no”.

AST is per “compilation unit”, and as such, test.c and Main.c is two compilation units. You will need to write some code of your own to analyze the total set of AST’s from all your compilation units to determine that. Which may not be so hard for this simple example, but gets quite complex if you start having many and large compilation units - and want to determine ALL global variables are initialized [and of course, “not initializing” global variables is well-defined by the C and C++ specifications - in other words, int var; and int var = 0; are equivalent constructs].

I’m interpreting the legal terms of your footer of your email to mean that you intended to send this to the clang mailing list, and as such I am an intended recipient and have been authorized by KPIT Technologies Ltd, to read and retain your email. If this is not a correct interpretation, please destroy this email.