Hello.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. //===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements two versions of the LLVM "Hello World" pass described
  11. // in docs/WritingAnLLVMPass.html
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #define DEBUG_TYPE "hello"
  15. #include "llvm/Pass.h"
  16. #include "llvm/Function.h"
  17. #include "llvm/ADT/StringExtras.h"
  18. #include "llvm/Support/Streams.h"
  19. #include "llvm/ADT/Statistic.h"
  20. using namespace llvm;
  21. STATISTIC(HelloCounter, "Counts number of functions greeted");
  22. namespace {
  23. // Hello - The first implementation, without getAnalysisUsage.
  24. struct Hello : public FunctionPass {
  25. static char ID; // Pass identification, replacement for typeid
  26. Hello() : FunctionPass(&ID) {}
  27. virtual bool runOnFunction(Function &F) {
  28. HelloCounter++;
  29. std::string fname = F.getName();
  30. EscapeString(fname);
  31. cerr << "Hello: " << fname << "\n";
  32. return false;
  33. }
  34. };
  35. }
  36. char Hello::ID = 0;
  37. static RegisterPass<Hello> X("hello", "Hello World Pass");
  38. namespace {
  39. // Hello2 - The second implementation with getAnalysisUsage implemented.
  40. struct Hello2 : public FunctionPass {
  41. static char ID; // Pass identification, replacement for typeid
  42. Hello2() : FunctionPass(&ID) {}
  43. virtual bool runOnFunction(Function &F) {
  44. HelloCounter++;
  45. std::string fname = F.getName();
  46. EscapeString(fname);
  47. cerr << "Hello: " << fname << "\n";
  48. return false;
  49. }
  50. // We don't modify the program, so we preserve all analyses
  51. virtual void getAnalysisUsage(AnalysisUsage &AU) const {
  52. AU.setPreservesAll();
  53. };
  54. };
  55. }
  56. char Hello2::ID = 0;
  57. static RegisterPass<Hello2>
  58. Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");