Add support for variable argument functions!
[oota-llvm.git] / lib / Analysis / IPA / FindUnsafePointerTypes.cpp
index 48e474f6b23fc29196f3ce282852dad67581d016..5d9745cd7a4778142ef9ecab90f3743c69919fb5 100644 (file)
@@ -1,4 +1,4 @@
-//===- SafePointerAccess.cpp - Check pointer usage safety -------------------=//
+//===- FindUnsafePointerTypes.cpp - Check pointer usage safety --------------=//
 //
 // This file defines a pass that can be used to determine, interprocedurally, 
 // which pointer types are accessed unsafely in a program.  If there is an
 
 #include "llvm/Analysis/FindUnsafePointerTypes.h"
 #include "llvm/Assembly/CachedWriter.h"
-#include "llvm/Support/CommandLine.h"
 #include "llvm/Type.h"
+#include "llvm/Module.h"
+#include "llvm/Support/InstIterator.h"
+#include "Support/CommandLine.h"
+
+static RegisterAnalysis<FindUnsafePointerTypes>
+X("unsafepointertypes", "Find Unsafe Pointer Types");
 
 // Provide a command line option to turn on printing of which instructions cause
 // a type to become invalid
 //
-static cl::Flag 
-PrintFailures("printunsafeptrinst", "Print Unsafe Pointer Access Instructions",
-              cl::Hidden, false);
+static cl::opt<bool> 
+PrintFailures("printunsafeptrinst", cl::Hidden,
+              cl::desc("Print Unsafe Pointer Access Instructions"));
 
 static inline bool isSafeInstruction(const Instruction *I) {
   switch (I->getOpcode()) {
@@ -45,26 +50,23 @@ static inline bool isSafeInstruction(const Instruction *I) {
 }
 
 
-// doPerMethodWork - Inspect the operations that the specified method does on
-// values of various types.  If they are deemed to be 'unsafe' note that the
-// type is not safe to transform.
-//
-bool FindUnsafePointerTypes::doPerMethodWork(Method *Meth) {
-  const Method *M = Meth;  // We don't need/want write access
-  for (Method::inst_const_iterator I = M->inst_begin(), E = M->inst_end();
-       I != E; ++I) {
-    const Instruction *Inst = *I;
-    const Type *ITy = Inst->getType();
-    if (ITy->isPointerType() && !UnsafeTypes.count((PointerType*)ITy))
-      if (!isSafeInstruction(Inst)) {
-        UnsafeTypes.insert((PointerType*)ITy);
+bool FindUnsafePointerTypes::run(Module &Mod) {
+  for (Module::iterator FI = Mod.begin(), E = Mod.end();
+       FI != E; ++FI) {
+    const Function *F = FI;  // We don't need/want write access
+    for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
+      const Type *ITy = I->getType();
+      if (isa<PointerType>(ITy) && !UnsafeTypes.count((PointerType*)ITy))
+        if (!isSafeInstruction(*I)) {
+          UnsafeTypes.insert((PointerType*)ITy);
 
-        if (PrintFailures) {
-          CachedWriter CW(M->getParent(), cerr);
-          CW << "FindUnsafePointerTypes: Type '" << ITy
-             << "' marked unsafe by:\n" << Inst;
+          if (PrintFailures) {
+            CachedWriter CW(F->getParent(), std::cerr);
+            CW << "FindUnsafePointerTypes: Type '" << ITy
+               << "' marked unsafe in '" << F->getName() << "' by:\n" << **I;
+          }
         }
-      }
+    }
   }
 
   return false;
@@ -74,7 +76,7 @@ bool FindUnsafePointerTypes::doPerMethodWork(Method *Meth) {
 // printResults - Loop over the results of the analysis, printing out unsafe
 // types.
 //
-void FindUnsafePointerTypes::printResults(const Module *M, ostream &o) {
+void FindUnsafePointerTypes::print(std::ostream &o, const Module *M) const {
   if (UnsafeTypes.empty()) {
     o << "SafePointerAccess Analysis: No unsafe types found!\n";
     return;
@@ -84,9 +86,9 @@ void FindUnsafePointerTypes::printResults(const Module *M, ostream &o) {
 
   CW << "SafePointerAccess Analysis: Found these unsafe types:\n";
   unsigned Counter = 1;
-  for (set<PointerType*>::const_iterator I = getUnsafeTypes().begin(), 
+  for (std::set<PointerType*>::const_iterator I = getUnsafeTypes().begin(), 
          E = getUnsafeTypes().end(); I != E; ++I, ++Counter) {
     
-    CW << " #" << Counter << ". " << (Value*)*I << endl;
+    CW << " #" << Counter << ". " << (Value*)*I << "\n";
   }
 }