[cfe-dev] Can a Clang plugin see names of source files?

Arthur O'Dwyer arthur at push.am
Fri Apr 6 02:59:38 PDT 2012


Hi, first-time Clang-hacker and list-user here, so be gentle. :)

I'm trying to write a program to extract the names and signatures of
all functions declared in a given source file (but exclude any
declarations that come from #included files). I'm pretty sure I want
to write a plugin to do this, because if I make a whole standalone
"compiler-ish" program to do the parsing, I'll have to worry about
getting all the preprocessor macros defined correctly, whereas a
plugin can just plug straight into the clang I'm already using to
compile the source files in the first place. The worst scenario would
be that my tool extract *different* function signatures than were
actually present on my target, because of inconsistencies in
preprocessor behavior.

Unfortunately there seem to be only two extant examples of Clang
plugins (PrintFunctionNames and analyzer-plugin), and neither of them
tries to do anything complicated nor document its code. Particularly
relevant to my problem, PrintFunctionNames does not try to find out
the name of the source file where each NamedDecl was encountered.

It seems like there's an API for getting the name of the current file
from a SourceLocation, but only if you *also* have a SourceManager
object, which I don't, and from inside the plugin I don't know how to
get hold of the one that the compiler is using. Seems like there ought
to be a singleton SourceManager somewhere that I could just grab, but
I can't find any documentation about such a thing (although there are
intriguing references to a "class The"; see
http://clang.llvm.org/doxygen/classThe.html).

I feel like it ought to be pretty easy to get the function signature
of each declaration, once I can actually weed out the declarations I'm
interested in (i.e., the ones in the current file) --- but certainly
if anyone wants to give me some pointers on how it's done, I'd gladly
accept the help. :)

Off-topic, but related to Clang newbie issues, I'd love to see some
documentation and/or code-comments added to
https://github.com/loarabia/Clang-tutorial.

Thanks,
-Arthur



More information about the cfe-dev mailing list