LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

Hello.cpp

Go to the documentation of this file.
00001 //===- Hello.cpp - Example code from "Writing an LLVM Pass" ---------------===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements two versions of the LLVM "Hello World" pass described
00011 // in docs/WritingAnLLVMPass.html
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/Pass.h"
00016 #include "llvm/Function.h"
00017 #include <iostream>
00018 using namespace llvm;
00019 
00020 namespace {
00021   // Hello - The first implementation, without getAnalysisUsage.
00022   struct Hello : public FunctionPass {
00023     virtual bool runOnFunction(Function &F) {
00024       std::cerr << "Hello: " << F.getName() << "\n";
00025       return false;
00026     }
00027   }; 
00028   RegisterOpt<Hello> X("hello", "Hello World Pass");
00029 
00030   // Hello2 - The second implementation with getAnalysisUsage implemented.
00031   struct Hello2 : public FunctionPass {
00032     virtual bool runOnFunction(Function &F) {
00033       std::cerr << "Hello: " << F.getName() << "\n";
00034       return false;
00035     }
00036 
00037     // We don't modify the program, so we preserve all analyses
00038     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
00039       AU.setPreservesAll();
00040     };
00041   }; 
00042   RegisterOpt<Hello2> Y("hello2", "Hello World Pass (with getAnalysisUsage implemented)");
00043 }