Can a Clang plugin see names of source files?

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

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. :slight_smile:

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

Hi,

chromium's style plugin does this, see
ChromeClassTester::InBannedDirectory in
http://src.chromium.org/viewvc/chrome/trunk/src/tools/clang/plugins/ChromeClassTester.cpp?view=markup
See chromium - An open-source project to help move the web forward. - Monorail and
Chromium Style Checker Errors
for background on chromium's style plugin.

Searching for "clang::" at http://codesearch.chromium.org will give
you some more example code.

Ah, I totally missed that the "factory" method
PluginASTAction::CreateASTConsumer() takes an argument of type
CompilerInstance& and can pass it along into the ASTConsumer at
construction time. That makes everything a *lot* easier! :wink:

And CreateASTConsumer() *also* takes a string argument that holds the
name of the top-level input file, which was exactly the other piece of
information I needed.

[I think I'm well on my way now. I still don't understand the
difference between ASTConsumer::HandleTopLevelDecl() and
ASTConsumer::HandleInterestingDecl(), but since the latter never
triggers on my source code at all, I'm guessing that I can ignore it.]

Thanks!
-Arthur