summaryrefslogtreecommitdiff
path: root/clang/examples/PrintFunctionNames/PrintFunctionNames.cpp
diff options
context:
space:
mode:
authorCarlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au>2012-10-15 17:10:06 +1100
committerCarlo Zancanaro <carlo@pc-4w14-0.cs.usyd.edu.au>2012-10-15 17:10:06 +1100
commitbe1de4be954c80875ad4108e0a33e8e131b2f2c0 (patch)
tree1fbbecf276bf7c7bdcbb4dd446099d6d90eaa516 /clang/examples/PrintFunctionNames/PrintFunctionNames.cpp
parentc4626a62754862d20b41e8a46a3574264ea80e6d (diff)
parentf1bd2e48c5324d3f7cda4090c87f8a5b6f463ce2 (diff)
Merge branch 'master' of ssh://bitbucket.org/czan/honours
Diffstat (limited to 'clang/examples/PrintFunctionNames/PrintFunctionNames.cpp')
-rw-r--r--clang/examples/PrintFunctionNames/PrintFunctionNames.cpp71
1 files changed, 71 insertions, 0 deletions
diff --git a/clang/examples/PrintFunctionNames/PrintFunctionNames.cpp b/clang/examples/PrintFunctionNames/PrintFunctionNames.cpp
new file mode 100644
index 0000000..ce8f208
--- /dev/null
+++ b/clang/examples/PrintFunctionNames/PrintFunctionNames.cpp
@@ -0,0 +1,71 @@
+//===- PrintFunctionNames.cpp ---------------------------------------------===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Example clang plugin which simply prints the names of all the top-level decls
+// in the input file.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Frontend/FrontendPluginRegistry.h"
+#include "clang/AST/ASTConsumer.h"
+#include "clang/AST/AST.h"
+#include "clang/Frontend/CompilerInstance.h"
+#include "llvm/Support/raw_ostream.h"
+using namespace clang;
+
+namespace {
+
+class PrintFunctionsConsumer : public ASTConsumer {
+public:
+ virtual bool HandleTopLevelDecl(DeclGroupRef DG) {
+ for (DeclGroupRef::iterator i = DG.begin(), e = DG.end(); i != e; ++i) {
+ const Decl *D = *i;
+ if (const NamedDecl *ND = dyn_cast<NamedDecl>(D))
+ llvm::errs() << "top-level-decl: \"" << ND->getNameAsString() << "\"\n";
+ }
+
+ return true;
+ }
+};
+
+class PrintFunctionNamesAction : public PluginASTAction {
+protected:
+ ASTConsumer *CreateASTConsumer(CompilerInstance &CI, llvm::StringRef) {
+ return new PrintFunctionsConsumer();
+ }
+
+ bool ParseArgs(const CompilerInstance &CI,
+ const std::vector<std::string>& args) {
+ for (unsigned i = 0, e = args.size(); i != e; ++i) {
+ llvm::errs() << "PrintFunctionNames arg = " << args[i] << "\n";
+
+ // Example error handling.
+ if (args[i] == "-an-error") {
+ DiagnosticsEngine &D = CI.getDiagnostics();
+ unsigned DiagID = D.getCustomDiagID(
+ DiagnosticsEngine::Error, "invalid argument '" + args[i] + "'");
+ D.Report(DiagID);
+ return false;
+ }
+ }
+ if (args.size() && args[0] == "help")
+ PrintHelp(llvm::errs());
+
+ return true;
+ }
+ void PrintHelp(llvm::raw_ostream& ros) {
+ ros << "Help for PrintFunctionNames plugin goes here\n";
+ }
+
+};
+
+}
+
+static FrontendPluginRegistry::Add<PrintFunctionNamesAction>
+X("print-fns", "print function names");