Make CanFallThrough more intelligent (so it can handle blocks with (e.g.) no
[oota-llvm.git] / lib / Debugger / ProgramInfo.cpp
index 29195bd159e8a7bcf5c8d56a43e1da4b6a922377..7e12cf80d6d7cb60cadffa64678ecaee2a724c4f 100644 (file)
@@ -1,12 +1,12 @@
 //===-- ProgramInfo.cpp - Compute and cache info about a program ----------===//
-// 
+//
 //                     The LLVM Compiler Infrastructure
 //
 // This file was developed by the LLVM research group and is distributed under
 // the University of Illinois Open Source License. See LICENSE.TXT for details.
-// 
+//
 //===----------------------------------------------------------------------===//
-// 
+//
 // This file implements the ProgramInfo and related classes, by sorting through
 // the loaded Module.
 //
 #include "llvm/Constants.h"
 #include "llvm/DerivedTypes.h"
 #include "llvm/Intrinsics.h"
-#include "llvm/iOther.h"
+#include "llvm/IntrinsicInst.h"
+#include "llvm/Instructions.h"
 #include "llvm/Module.h"
 #include "llvm/Debugger/SourceFile.h"
 #include "llvm/Debugger/SourceLanguage.h"
-#include "Support/FileUtilities.h"
-#include "Support/SlowOperationInformer.h"
-#include "Support/STLExtras.h"
+#include "llvm/Support/SlowOperationInformer.h"
+#include "llvm/ADT/STLExtras.h"
+#include <iostream>
+
 using namespace llvm;
 
 /// getGlobalVariablesUsing - Return all of the global variables which have the
@@ -37,45 +39,6 @@ static void getGlobalVariablesUsing(Value *V,
   }
 }
 
-/// getStringValue - Turn an LLVM constant pointer that eventually points to a
-/// global into a string value.  Return an empty string if we can't do it.
-///
-static std::string getStringValue(Value *V, unsigned Offset = 0) {
-  if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
-    if (GV->hasInitializer() && isa<ConstantArray>(GV->getInitializer())) {
-      ConstantArray *Init = cast<ConstantArray>(GV->getInitializer());
-      if (Init->isString()) {
-        std::string Result = Init->getAsString();
-        if (Offset < Result.size()) {
-          // If we are pointing INTO The string, erase the beginning...
-          Result.erase(Result.begin(), Result.begin()+Offset);
-
-          // Take off the null terminator, and any string fragments after it.
-          std::string::size_type NullPos = Result.find_first_of((char)0);
-          if (NullPos != std::string::npos)
-            Result.erase(Result.begin()+NullPos, Result.end());
-          return Result;
-        }
-      }
-    }
-  } else if (Constant *C = dyn_cast<Constant>(V)) {
-    if (ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(C))
-      return getStringValue(CPR->getValue(), Offset);
-    else if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
-      if (CE->getOpcode() == Instruction::GetElementPtr) {
-        // Turn a gep into the specified offset.
-        if (CE->getNumOperands() == 3 &&
-            cast<Constant>(CE->getOperand(1))->isNullValue() &&
-            isa<ConstantInt>(CE->getOperand(2))) {
-          return getStringValue(CE->getOperand(0),
-                   Offset+cast<ConstantInt>(CE->getOperand(2))->getRawValue());
-        }
-      }
-    }
-  }
-  return "";
-}
-
 /// getNextStopPoint - Follow the def-use chains of the specified LLVM value,
 /// traversing the use chains until we get to a stoppoint.  When we do, return
 /// the source location of the stoppoint.  If we don't find a stoppoint, return
@@ -95,30 +58,25 @@ static const GlobalVariable *getNextStopPoint(const Value *V, unsigned &LineNo,
       // Infinite loops == bad, ignore PHI nodes.
       ShouldRecurse = false;
     } else if (const CallInst *CI = dyn_cast<CallInst>(*UI)) {
+      
       // If we found a stop point, check to see if it is earlier than what we
       // already have.  If so, remember it.
       if (const Function *F = CI->getCalledFunction())
-        if (F->getIntrinsicID() == Intrinsic::dbg_stoppoint) {
-          unsigned CurLineNo = ~0, CurColNo = ~0;
+        if (const DbgStopPointInst *SPI = dyn_cast<DbgStopPointInst>(CI)) {
+          unsigned CurLineNo = SPI->getLine();
+          unsigned CurColNo = SPI->getColumn();
           const GlobalVariable *CurDesc = 0;
-          if (const ConstantInt *C = dyn_cast<ConstantInt>(CI->getOperand(2)))
-            CurLineNo = C->getRawValue();
-          if (const ConstantInt *C = dyn_cast<ConstantInt>(CI->getOperand(3)))
-            CurColNo = C->getRawValue();
-          const Value *Op = CI->getOperand(4);
-          if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(Op))
-            Op = CPR->getValue();
-          
+          const Value *Op = SPI->getContext();
+
           if ((CurDesc = dyn_cast<GlobalVariable>(Op)) &&
               (LineNo < LastLineNo ||
                (LineNo == LastLineNo && ColNo < LastColNo))) {
             LastDesc = CurDesc;
             LastLineNo = CurLineNo;
-            LastColNo = CurColNo;            
+            LastColNo = CurColNo;
           }
           ShouldRecurse = false;
         }
-
     }
 
     // If this is not a phi node or a stopping point, recursively scan the users
@@ -129,12 +87,12 @@ static const GlobalVariable *getNextStopPoint(const Value *V, unsigned &LineNo,
         if (LineNo < LastLineNo || (LineNo == LastLineNo && ColNo < LastColNo)){
           LastDesc = GV;
           LastLineNo = CurLineNo;
-          LastColNo = CurColNo;            
+          LastColNo = CurColNo;
         }
       }
     }
   }
-  
+
   if (LastDesc) {
     LineNo = LastLineNo != ~0U ? LastLineNo : 0;
     ColNo  = LastColNo  != ~0U ? LastColNo : 0;
@@ -156,11 +114,11 @@ SourceFileInfo::SourceFileInfo(const GlobalVariable *Desc,
   if (Desc && Desc->hasInitializer())
     if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
       if (CS->getNumOperands() > 4) {
-        if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(CS->getOperand(1)))
-          Version = CUI->getValue();
-        
-        BaseName  = getStringValue(CS->getOperand(3));
-        Directory = getStringValue(CS->getOperand(4));
+        if (ConstantInt *CUI = dyn_cast<ConstantInt>(CS->getOperand(1)))
+          Version = CUI->getZExtValue();
+
+        BaseName  = CS->getOperand(3)->getStringValue();
+        Directory = CS->getOperand(4)->getStringValue();
       }
 }
 
@@ -170,11 +128,16 @@ SourceFileInfo::~SourceFileInfo() {
 
 SourceFile &SourceFileInfo::getSourceText() const {
   // FIXME: this should take into account the source search directories!
-  if (SourceText == 0)  // Read the file in if we haven't already.
-    if (!Directory.empty() && FileOpenable(Directory+"/"+BaseName))
-      SourceText = new SourceFile(Directory+"/"+BaseName, Descriptor);
+  if (SourceText == 0) { // Read the file in if we haven't already.
+    sys::Path tmpPath;
+    if (!Directory.empty())
+      tmpPath.set(Directory);
+    tmpPath.appendComponent(BaseName);
+    if (tmpPath.canRead())
+      SourceText = new SourceFile(tmpPath.toString(), Descriptor);
     else
       SourceText = new SourceFile(BaseName, Descriptor);
+  }
   return *SourceText;
 }
 
@@ -190,14 +153,12 @@ SourceFunctionInfo::SourceFunctionInfo(ProgramInfo &PI,
     if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
       if (CS->getNumOperands() > 2) {
         // Entry #1 is the file descriptor.
-        if (const ConstantPointerRef *CPR =
-            dyn_cast<ConstantPointerRef>(CS->getOperand(1)))
-          if (const GlobalVariable *GV =
-              dyn_cast<GlobalVariable>(CPR->getValue()))
-            SourceFile = &PI.getSourceFile(GV);
+        if (const GlobalVariable *GV =
+            dyn_cast<GlobalVariable>(CS->getOperand(1)))
+          SourceFile = &PI.getSourceFile(GV);
 
         // Entry #2 is the function name.
-        Name = getStringValue(CS->getOperand(2));
+        Name = CS->getOperand(2)->getStringValue();
       }
 }
 
@@ -233,9 +194,11 @@ void SourceFunctionInfo::getSourceLocation(unsigned &RetLineNo,
 // ProgramInfo implementation
 //
 
-ProgramInfo::ProgramInfo(Module *m) : M(m) {
+ProgramInfo::ProgramInfo(Module *m) : M(m), ProgramTimeStamp(0,0) {
   assert(M && "Cannot create program information with a null module!");
-  ProgramTimeStamp = getFileTimestamp(M->getModuleIdentifier());
+  sys::FileStatus Stat;
+  if (!sys::Path(M->getModuleIdentifier()).getFileStatus(Stat))
+    ProgramTimeStamp = Stat.getTimestamp();
 
   SourceFilesIsComplete = false;
   SourceFunctionsIsComplete = false;
@@ -274,8 +237,8 @@ ProgramInfo::getSourceFile(const GlobalVariable *Desc) {
   if (Desc && Desc->hasInitializer())
     if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
       if (CS->getNumOperands() > 2)
-        if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(CS->getOperand(2)))
-          LangID = CUI->getValue();
+        if (ConstantInt *CUI = dyn_cast<ConstantInt>(CS->getOperand(2)))
+          LangID = CUI->getZExtValue();
 
   const SourceLanguage &Lang = SourceLanguage::get(LangID);
   SourceFileInfo *New = Lang.createSourceFileInfo(Desc, *this);
@@ -318,7 +281,8 @@ ProgramInfo::getSourceFiles(bool RequiresCompleteMap) {
   // mapping.
   for (unsigned i = 0, e = TranslationUnits.size(); i != e; ++i) {
     getSourceFile(TranslationUnits[i]);
-    SOI.progress(i+1, e);
+    if (SOI.progress(i+1, e))
+      throw "While building source files index, operation cancelled.";
   }
 
   // Ok, if we got this far, then we indexed the whole program.
@@ -335,7 +299,7 @@ const SourceFileInfo &ProgramInfo::getSourceFile(const std::string &Filename) {
   std::multimap<std::string, SourceFileInfo*>::const_iterator Start, End;
   getSourceFiles();
   tie(Start, End) = SourceFileIndex.equal_range(Filename);
-  
+
   if (Start == End) throw "Could not find source file '" + Filename + "'!";
   const SourceFileInfo &SFI = *Start->second;
   ++Start;
@@ -364,9 +328,9 @@ ProgramInfo::getFunction(const GlobalVariable *Desc) {
   if (Desc && Desc->hasInitializer())
     if (ConstantStruct *CS = dyn_cast<ConstantStruct>(Desc->getInitializer()))
       if (CS->getNumOperands() > 0)
-        if (const ConstantPointerRef *CPR =
-            dyn_cast<ConstantPointerRef>(CS->getOperand(1)))
-          SourceFileDesc = dyn_cast<GlobalVariable>(CPR->getValue());
+        if (const GlobalVariable *GV =
+            dyn_cast<GlobalVariable>(CS->getOperand(1)))
+          SourceFileDesc = GV;
 
   const SourceLanguage &Lang = getSourceFile(SourceFileDesc).getLanguage();
   return *(Result = Lang.createSourceFunctionInfo(Desc, *this));
@@ -399,7 +363,8 @@ ProgramInfo::getSourceFunctions(bool RequiresCompleteMap) {
   // Loop over all of the functions found, building the SourceFunctions mapping.
   for (unsigned i = 0, e = Functions.size(); i != e; ++i) {
     getFunction(Functions[i]);
-    SOI.progress(i+1, e);
+    if (SOI.progress(i+1, e))
+      throw "While functions index, operation cancelled.";
   }
 
   // Ok, if we got this far, then we indexed the whole program.