PrintFunctionNames.cpp revision 0e2c34f92f00628d48968dfea096d36381f494cb
19a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar//===- PrintFunctionNames.cpp ---------------------------------------------===// 29a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar// 39a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar// The LLVM Compiler Infrastructure 49a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar// 59a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar// This file is distributed under the University of Illinois Open Source 69a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar// License. See LICENSE.TXT for details. 79a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar// 89a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar//===----------------------------------------------------------------------===// 99a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar// 10dd63b28107f21692b5065588f0e90b4534946f93Daniel Dunbar// Example clang plugin which simply prints the names of all the top-level decls 11dd63b28107f21692b5065588f0e90b4534946f93Daniel Dunbar// in the input file. 129a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar// 139a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar//===----------------------------------------------------------------------===// 149a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar 159a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar#include "clang/Frontend/FrontendPluginRegistry.h" 169a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar#include "clang/AST/AST.h" 17e0c6e93dddf4503d9b12ce09d7ab6ea5d523499fChandler Carruth#include "clang/AST/ASTConsumer.h" 18f56a488a6bdfe56ca814f37d384afa67c67f9dd5Daniel Dunbar#include "clang/Frontend/CompilerInstance.h" 199a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar#include "llvm/Support/raw_ostream.h" 209a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbarusing namespace clang; 219a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar 229a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbarnamespace { 239a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar 249a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbarclass PrintFunctionsConsumer : public ASTConsumer { 259a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbarpublic: 261a026803b7886ed265fc63dfddaaa5d3712760e3Douglas Gregor virtual bool HandleTopLevelDecl(DeclGroupRef DG) { 279a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) { 289a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar const Decl *D = *i; 299a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar if (const NamedDecl *ND = dyn_cast<NamedDecl>(D)) 309a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n"; 319a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar } 321a026803b7886ed265fc63dfddaaa5d3712760e3Douglas Gregor 331a026803b7886ed265fc63dfddaaa5d3712760e3Douglas Gregor return true; 349a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar } 35f56a488a6bdfe56ca814f37d384afa67c67f9dd5Daniel Dunbar}; 369a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar 373177aae51a21f61ab483c52f97124bdb707da7f1Daniel Dunbarclass PrintFunctionNamesAction : public PluginASTAction { 389a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbarprotected: 39176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &CI, 40176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines llvm::StringRef) { 41176edba5311f6eff0cad2631449885ddf4fbc9eaStephen Hines return llvm::make_unique<PrintFunctionsConsumer>(); 429a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar } 433177aae51a21f61ab483c52f97124bdb707da7f1Daniel Dunbar 44f56a488a6bdfe56ca814f37d384afa67c67f9dd5Daniel Dunbar bool ParseArgs(const CompilerInstance &CI, 45f56a488a6bdfe56ca814f37d384afa67c67f9dd5Daniel Dunbar const std::vector<std::string>& args) { 46f56a488a6bdfe56ca814f37d384afa67c67f9dd5Daniel Dunbar for (unsigned i = 0, e = args.size(); i != e; ++i) { 473177aae51a21f61ab483c52f97124bdb707da7f1Daniel Dunbar llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n"; 48f56a488a6bdfe56ca814f37d384afa67c67f9dd5Daniel Dunbar 49f56a488a6bdfe56ca814f37d384afa67c67f9dd5Daniel Dunbar // Example error handling. 50f56a488a6bdfe56ca814f37d384afa67c67f9dd5Daniel Dunbar if (args[i] == "-an-error") { 51d3c1661593e246f9cd25dc9da27e54b0a61ffe0dEli Friedman DiagnosticsEngine &D = CI.getDiagnostics(); 52651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines unsigned DiagID = D.getCustomDiagID(DiagnosticsEngine::Error, 53651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines "invalid argument '%0'"); 54651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines D.Report(DiagID) << args[i]; 55f56a488a6bdfe56ca814f37d384afa67c67f9dd5Daniel Dunbar return false; 56f56a488a6bdfe56ca814f37d384afa67c67f9dd5Daniel Dunbar } 57f56a488a6bdfe56ca814f37d384afa67c67f9dd5Daniel Dunbar } 580e2c34f92f00628d48968dfea096d36381f494cbStephen Hines if (!args.empty() && args[0] == "help") 593177aae51a21f61ab483c52f97124bdb707da7f1Daniel Dunbar PrintHelp(llvm::errs()); 603177aae51a21f61ab483c52f97124bdb707da7f1Daniel Dunbar 613177aae51a21f61ab483c52f97124bdb707da7f1Daniel Dunbar return true; 623177aae51a21f61ab483c52f97124bdb707da7f1Daniel Dunbar } 63f56a488a6bdfe56ca814f37d384afa67c67f9dd5Daniel Dunbar void PrintHelp(llvm::raw_ostream& ros) { 643177aae51a21f61ab483c52f97124bdb707da7f1Daniel Dunbar ros << "Help for PrintFunctionNames plugin goes here\n"; 653177aae51a21f61ab483c52f97124bdb707da7f1Daniel Dunbar } 663177aae51a21f61ab483c52f97124bdb707da7f1Daniel Dunbar 679a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar}; 689a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar 699a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar} 709a69387f5c88c73638ae5155a398477d9c7ac87fDaniel Dunbar 71981d1ec3fd2d33fbb1b47c9fdae9e5a0ea241bc4Manuel Klimekstatic FrontendPluginRegistry::Add<PrintFunctionNamesAction> 729a69387f5c88c73638ae5155a398477d9c7ac87fDaniel DunbarX("print-fns", "print function names"); 73