about summary refs log tree commit diff homepage
path: root/tools
diff options
context:
space:
mode:
authorDan Liew <daniel.liew@imperial.ac.uk>2014-04-02 21:44:51 +0100
committerMartin Nowack <martin@se.inf.tu-dresden.de>2014-04-14 10:34:54 +0200
commit4a9a908739d7d7c833e7985ea7465d95c0dd0b82 (patch)
tree34943eae75ca7471ff91534acde5bc4578a7d530 /tools
parentf2ce7b5b53d78c370b01f5f219df0ea0021c7bb2 (diff)
downloadklee-4a9a908739d7d7c833e7985ea7465d95c0dd0b82.tar.gz
Tidy up code by using LLVM's V2 path API only and removing uses
of old V1 path API.

LLVM2.9 supports LLVM's V2 path API. Because that is the minimum
version we support we should just use this API everywhere so we
reduce the number of #if LLVM_VERSION_CODE macros and duplicated
code.
Diffstat (limited to 'tools')
-rw-r--r--tools/klee/main.cpp173
1 files changed, 11 insertions, 162 deletions
diff --git a/tools/klee/main.cpp b/tools/klee/main.cpp
index e1219747..ff90a644 100644
--- a/tools/klee/main.cpp
+++ b/tools/klee/main.cpp
@@ -37,6 +37,7 @@
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
+#include "llvm/Support/raw_ostream.h"
 
 // FIXME: Ugh, this is gross. But otherwise our config.h conflicts with LLVMs.
 #undef PACKAGE_BUGREPORT
@@ -222,11 +223,8 @@ private:
   TreeStreamWriter *m_pathWriter, *m_symPathWriter;
   std::ostream *m_infoFile;
 
-#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4)
   SmallString<128> m_outputDirectory;
-#else
-  sys::Path m_outputDirectory;
-#endif
+
   unsigned m_testIndex;  // number of tests written so far
   unsigned m_pathsExplored; // number of paths explored so far
 
@@ -265,11 +263,7 @@ public:
   static void getOutFiles(std::string path,
 			  std::vector<std::string> &results);
 
-#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4)
   static std::string getRunTimeLibraryPath(const char* argv0, void *MainExecAddr);
-#else
-  static llvm::sys::Path getRunTimeLibraryPath(const char* argv0, void *MainExecAddr);
-#endif
 };
 
 KleeHandler::KleeHandler(int argc, char **argv) 
@@ -283,7 +277,6 @@ KleeHandler::KleeHandler(int argc, char **argv)
     m_argc(argc),
     m_argv(argv) {
 
-#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4)
   if (OutputDir=="") {
 
     SmallString<128> directory(InputFile);
@@ -346,72 +339,6 @@ KleeHandler::KleeHandler(int argc, char **argv)
     sys::path::append(cwd, m_outputDirectory.str());
     m_outputDirectory = cwd;
   }
-#else
-  if (OutputDir=="") {
-
-    llvm::sys::Path directory(InputFile);
-    std::stringstream dirname;
-    directory.eraseComponent();
-    
-    if (directory.isEmpty())
-      directory.set(".");
-    
-    for (int i = 0; i< INT_MAX ; i++) {
-      dirname << "klee-out-";
-      dirname << i;
-
-      m_outputDirectory = llvm::sys::Path(directory); // Copy
-      if (!m_outputDirectory.appendComponent(dirname.str()))
-        klee_error("Failed to append \"%s\" to \"%s\"", dirname.str().c_str(), directory.c_str());
-      
-      bool isDir = true;
-      llvm::error_code e = llvm::sys::fs::exists(m_outputDirectory.str(), isDir);
-      if ( e != llvm::errc::success )
-        klee_error("Failed to check if \"%s\" exists.", m_outputDirectory.str().c_str());
-
-      if (!isDir)
-      {
-        break; // Found an available directory name
-      }
-
-      // Warn the user if the klee-out-* exists but is not a directory
-      e = llvm::sys::fs::is_directory(m_outputDirectory.str(), isDir);
-      if ( e == llvm::errc::success && !isDir )
-        klee_warning("A file \"%s\" exists, but it is not a directory",
-                     m_outputDirectory.str().c_str());
-
-      dirname.str(""); // Clear
-      m_outputDirectory.clear();
-    }    
-
-    if (m_outputDirectory.empty())
-      klee_error("Failed to find available output directory in %s", dirname.str().c_str());
-
-    std::cerr << "KLEE: output directory = \"" << dirname.str() << "\"\n";
-
-    llvm::sys::Path klee_last(directory);
-    if(!klee_last.appendComponent("klee-last"))
-      klee_error("cannot create path name for klee-last");
-
-    if ((unlink(klee_last.c_str()) < 0) && (errno != ENOENT))
-      klee_error("cannot unlink klee-last: %s", strerror(errno));
-    
-    if (symlink(dirname.str().c_str(), klee_last.c_str()) < 0)
-      klee_error("cannot create klee-last symlink: %s", strerror(errno));
-
-  } else {
-    if (!m_outputDirectory.set(OutputDir))
-      klee_error("cannot use klee output directory: %s", OutputDir.c_str());
-  }
-  
-  if (!sys::path::is_absolute(m_outputDirectory.c_str())) {
-    sys::Path cwd = sys::Path::GetCurrentDirectory();
-    if(!cwd.appendComponent( m_outputDirectory.c_str()))
-      klee_error("cannot create absolute path name for output directory");
-
-    m_outputDirectory = cwd;
-  }
-#endif
 
   if (mkdir(m_outputDirectory.c_str(), 0775) < 0)
     klee_error("cannot create directory \"%s\": %s", m_outputDirectory.c_str(), strerror(errno));
@@ -452,18 +379,9 @@ void KleeHandler::setInterpreter(Interpreter *i) {
 }
 
 std::string KleeHandler::getOutputFilename(const std::string &filename) {
-#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4)
   SmallString<128> path = m_outputDirectory;
   sys::path::append(path,filename);
   return path.str();
-#else
-  sys::Path path(m_outputDirectory);
-  if(!path.appendComponent(filename)) {
-    klee_error("cannot create path name for \"%s\"", filename.c_str());
-  }
-
-  return path.str();
-#endif
 }
 
 std::ostream *KleeHandler::openOutputFile(const std::string &filename) {
@@ -637,7 +555,6 @@ void KleeHandler::loadPathFile(std::string name,
 
 void KleeHandler::getOutFiles(std::string path,
 			      std::vector<std::string> &results) {
-#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4)
   error_code ec;
   for (llvm::sys::fs::directory_iterator i(path,ec),e; i!=e && !ec; i.increment(ec)){
     std::string f = (*i).path();
@@ -651,37 +568,17 @@ void KleeHandler::getOutFiles(std::string path,
                << ": " << ec.message() << "\n";
     exit(1);
   }
-#else
-
-  llvm::sys::Path p(path);
-  std::set<llvm::sys::Path> contents;
-  std::string error;
-  if (p.getDirectoryContents(contents, &error)) {
-    std::cerr << "ERROR: unable to read output directory: " << path 
-               << ": " << error << "\n";
-    exit(1);
-  }
-  for (std::set<llvm::sys::Path>::iterator it = contents.begin(),
-         ie = contents.end(); it != ie; ++it) {
-    std::string f = it->str();
-    if (f.substr(f.size()-6,f.size()) == ".ktest") {
-      results.push_back(f);
-    }
-  }
-#endif
-
 }
 
-#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4)
-std::string
-#else
-llvm::sys::Path
-#endif
-KleeHandler::getRunTimeLibraryPath(const char* argv0, void* MainExecAddr)
+std::string KleeHandler::getRunTimeLibraryPath(const char* argv0, void* MainExecAddr)
 {
-#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4)
   SmallString<128> toolRoot(
-      llvm::sys::fs::getMainExecutable(argv0, MainExecAddr));
+      #if LLVM_VERSION_CODE >= LLVM_VERSION(3,4)
+      llvm::sys::fs::getMainExecutable(argv0, MainExecAddr)
+      #else
+      llvm::sys::Path::GetMainExecutable(argv0, MainExecAddr).str()
+      #endif
+      );
 
   // Strip off executable so we have a directory path
   llvm::sys::path::remove_filename(toolRoot);
@@ -706,29 +603,6 @@ KleeHandler::getRunTimeLibraryPath(const char* argv0, void* MainExecAddr)
   DEBUG_WITH_TYPE("klee_runtime", llvm::dbgs() <<
                   libDir.c_str() << "\n");
   return libDir.str();
-#else
-  llvm::sys::Path toolRoot = llvm::sys::Path::GetMainExecutable(argv0, MainExecAddr);
-  toolRoot.eraseComponent(); // Strip off executable so we have a directory path
-
-  llvm::sys::Path libDir;
-
-  if ( strcmp(toolRoot.c_str(), KLEE_INSTALL_BIN_DIR ) == 0)
-  {
-    DEBUG_WITH_TYPE("klee_runtime", llvm::dbgs() <<
-                    "Using installed KLEE library runtime: ");
-    libDir = llvm::sys::Path( KLEE_INSTALL_LIB_DIR );
-  }
-  else
-  {
-    DEBUG_WITH_TYPE("klee_runtime", llvm::dbgs() <<
-                    "Using build directory KLEE library runtime :");
-    libDir = llvm::sys::Path(KLEE_DIR "/" RUNTIME_CONFIGURATION "/lib");
-  }
-
-  DEBUG_WITH_TYPE("klee_runtime", llvm::dbgs() <<
-                  libDir.c_str() << "\n");
-  return libDir;
-#endif
 }
 
 //===----------------------------------------------------------------------===//
@@ -1162,20 +1036,10 @@ static void replaceOrRenameFunction(llvm::Module *module,
     }
   }
 }
-#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4)
 static llvm::Module *linkWithUclibc(llvm::Module *mainModule, StringRef libDir) {
-#else
-static llvm::Module *linkWithUclibc(llvm::Module *mainModule, llvm::sys::Path libDir) {
-#endif
-
   // Ensure that klee-uclibc exists
-#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4)
   SmallString<128> uclibcBCA(libDir);
   llvm::sys::path::append(uclibcBCA, KLEE_UCLIBC_BCA_NAME);
-#else
-  llvm::sys::Path uclibcBCA(libDir);
-  uclibcBCA.appendComponent(KLEE_UCLIBC_BCA_NAME);
-#endif
 
   bool uclibcExists=false;
   llvm::sys::fs::exists(uclibcBCA.c_str(), uclibcExists);
@@ -1401,13 +1265,8 @@ int main(int argc, char **argv, char **envp) {
       return r;
   }
 
-#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4)
   std::string LibraryDir = KleeHandler::getRunTimeLibraryPath(argv[0],
                               reinterpret_cast<void*>(main));
-#else
-  llvm::sys::Path LibraryDir = KleeHandler::getRunTimeLibraryPath(argv[0], 
-                              reinterpret_cast<void*>(main));
-#endif
   Interpreter::ModuleOptions Opts(LibraryDir.c_str(),
                                   /*Optimize=*/OptimizeModule, 
                                   /*CheckDivZero=*/CheckDivZero,
@@ -1419,16 +1278,11 @@ int main(int argc, char **argv, char **envp) {
 
   case KleeLibc: {
     // FIXME: Find a reasonable solution for this.
-#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4)
     SmallString<128> Path(Opts.LibraryDir);
+#if LLVM_VERSION_CODE >= LLVM_VERSION(3,3)
     llvm::sys::path::append(Path, "klee-libc.bc");
 #else
-    llvm::sys::Path Path(Opts.LibraryDir);
-#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3)
-    Path.appendComponent("klee-libc.bc");
-#else
-    Path.appendComponent("libklee-libc.bca");
-#endif
+    llvm::sys::path::append(Path, "libklee-libc.bca");
 #endif
     mainModule = klee::linkWithLibrary(mainModule, Path.c_str());
     assert(mainModule && "unable to link with klee-libc");
@@ -1441,13 +1295,8 @@ int main(int argc, char **argv, char **envp) {
   }
 
   if (WithPOSIXRuntime) {
-#if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4)
     SmallString<128> Path(Opts.LibraryDir);
     llvm::sys::path::append(Path, "libkleeRuntimePOSIX.bca");
-#else
-    llvm::sys::Path Path(Opts.LibraryDir);
-    Path.appendComponent("libkleeRuntimePOSIX.bca");
-#endif
     klee_message("NOTE: Using model: %s", Path.c_str());
     mainModule = klee::linkWithLibrary(mainModule, Path.c_str());
     assert(mainModule && "unable to link with simple model");