Add a simple breakpoint location printer. This will be used by upcoming "debug info...
authorDevang Patel <dpatel@apple.com>
Tue, 7 Dec 2010 00:33:43 +0000 (00:33 +0000)
committerDevang Patel <dpatel@apple.com>
Tue, 7 Dec 2010 00:33:43 +0000 (00:33 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@121078 91177308-0d34-0410-b5e6-96231b3b80d8

tools/opt/opt.cpp

index 2b6109f2bc168bba2326a0bf58cee50aadb1ad19..c1e0ad128bc2b009cbd9f6a29d5a2dbb020fbea1 100644 (file)
@@ -18,6 +18,7 @@
 #include "llvm/CallGraphSCCPass.h"
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/Assembly/PrintModulePass.h"
+#include "llvm/Analysis/DebugInfo.h"
 #include "llvm/Analysis/Verifier.h"
 #include "llvm/Analysis/LoopPass.h"
 #include "llvm/Analysis/RegionPass.h"
@@ -128,6 +129,10 @@ QuietA("quiet", cl::desc("Alias for -q"), cl::aliasopt(Quiet));
 static cl::opt<bool>
 AnalyzeOnly("analyze", cl::desc("Only perform analysis, no optimization"));
 
+static cl::opt<bool>
+PrintBreakpoints("print-breakpoints-for-testing", 
+                 cl::desc("Print select breakpoints location for testing"));
+
 static cl::opt<std::string>
 DefaultDataLayout("default-data-layout", 
           cl::desc("data layout string to use if not specified by module"),
@@ -334,6 +339,41 @@ struct BasicBlockPassPrinter : public BasicBlockPass {
 };
 
 char BasicBlockPassPrinter::ID = 0;
+
+struct BreakpointPrinter : public FunctionPass {
+  raw_ostream &Out;
+  static char ID;
+
+  BreakpointPrinter(raw_ostream &out)
+    : FunctionPass(ID), Out(out) {
+    }
+
+  virtual bool runOnFunction(Function &F) {
+    for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
+      BasicBlock::const_iterator BI = I->end();
+      --BI;
+      do {
+        const Instruction *In = BI;
+        const DebugLoc DL = In->getDebugLoc();
+        if (!DL.isUnknown()) {
+          DIScope S(DL.getScope(getGlobalContext()));
+          Out << S.getFilename() << " " << DL.getLine() << "\n";
+          break;
+        }
+        --BI;
+      } while (BI != I->begin());
+      break;
+    }
+    return false;
+  }
+
+  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
+    AU.setPreservesAll();
+  }
+};
+
+char BreakpointPrinter::ID = 0;
+
 inline void addPass(PassManagerBase &PM, Pass *P) {
   // Add the pass to the pass manager...
   PM.add(P);
@@ -509,6 +549,24 @@ int main(int argc, char **argv) {
       FPasses->add(new TargetData(*TD));
   }
 
+  if (PrintBreakpoints) {
+    // Default to standard output.
+    if (!Out) {
+      if (OutputFilename.empty())
+        OutputFilename = "-";
+      
+      std::string ErrorInfo;
+      Out.reset(new tool_output_file(OutputFilename.c_str(), ErrorInfo,
+                                     raw_fd_ostream::F_Binary));
+      if (!ErrorInfo.empty()) {
+        errs() << ErrorInfo << '\n';
+        return 1;
+      }
+    }
+    Passes.add(new BreakpointPrinter(Out->os()));
+    NoOutput = true;
+  }
+
   // If the -strip-debug command line option was specified, add it.  If
   // -std-compile-opts was also specified, it will handle StripDebug.
   if (StripDebug && !StandardCompileOpts)
@@ -623,7 +681,7 @@ int main(int argc, char **argv) {
   Passes.run(*M.get());
 
   // Declare success.
-  if (!NoOutput)
+  if (!NoOutput || PrintBreakpoints)
     Out->keep();
 
   return 0;