From 771cdf39d9c1e142269e2cafc4365d0d68e05f9a Mon Sep 17 00:00:00 2001 From: Martin Nowack Date: Mon, 10 Aug 2015 08:59:00 +0200 Subject: Refactoring: Moving klee_warning/_error functions to ErrorHandling in Support directory --- include/klee/Internal/Support/ErrorHandling.h | 51 ++++++++ lib/Core/Common.cpp | 168 -------------------------- lib/Core/Common.h | 56 --------- lib/Core/Executor.cpp | 2 +- lib/Core/ExecutorTimers.cpp | 3 +- lib/Core/Memory.cpp | 3 +- lib/Core/MemoryManager.cpp | 3 +- lib/Core/Searcher.cpp | 3 +- lib/Core/SeedInfo.cpp | 3 +- lib/Core/SpecialFunctionHandler.cpp | 3 +- lib/Core/StatsTracker.cpp | 3 +- lib/Core/UserSearcher.cpp | 3 +- lib/Module/KModule.cpp | 4 +- lib/Module/ModuleUtil.cpp | 4 +- lib/Solver/CexCachingSolver.cpp | 2 + lib/Support/ErrorHandling.cpp | 166 +++++++++++++++++++++++++ tools/klee/main.cpp | 4 +- 17 files changed, 231 insertions(+), 250 deletions(-) create mode 100644 include/klee/Internal/Support/ErrorHandling.h delete mode 100644 lib/Core/Common.cpp delete mode 100644 lib/Core/Common.h create mode 100644 lib/Support/ErrorHandling.cpp diff --git a/include/klee/Internal/Support/ErrorHandling.h b/include/klee/Internal/Support/ErrorHandling.h new file mode 100644 index 00000000..330985e9 --- /dev/null +++ b/include/klee/Internal/Support/ErrorHandling.h @@ -0,0 +1,51 @@ +//===-- ErrorHandling.h -----------------------------------------*- C++ -*-===// +// +// The KLEE Symbolic Virtual Machine +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#ifndef __KLEE_ERROR_HANDLING_H__ +#define __KLEE_ERROR_HANDLING_H__ + +#ifdef __CYGWIN__ +#ifndef WINDOWS +#define WINDOWS +#endif +#endif + +#include + +namespace klee { + +extern FILE *klee_warning_file; +extern FILE *klee_message_file; + +/// Print "KLEE: ERROR" followed by the msg in printf format and a +/// newline on stderr and to warnings.txt, then exit with an error. +void klee_error(const char *msg, ...) + __attribute__((format(printf, 1, 2), noreturn)); + +/// Print "KLEE: " followed by the msg in printf format and a +/// newline on stderr and to messages.txt. +void klee_message(const char *msg, ...) __attribute__((format(printf, 1, 2))); + +/// Print "KLEE: " followed by the msg in printf format and a +/// newline to messages.txt. +void klee_message_to_file(const char *msg, ...) + __attribute__((format(printf, 1, 2))); + +/// Print "KLEE: WARNING" followed by the msg in printf format and a +/// newline on stderr and to warnings.txt. +void klee_warning(const char *msg, ...) __attribute__((format(printf, 1, 2))); + +/// Print "KLEE: WARNING" followed by the msg in printf format and a +/// newline on stderr and to warnings.txt. However, the warning is only +/// printed once for each unique (id, msg) pair (as pointers). +void klee_warning_once(const void *id, const char *msg, ...) + __attribute__((format(printf, 2, 3))); +} + +#endif /* __KLEE_ERROR_HANDLING_H__ */ diff --git a/lib/Core/Common.cpp b/lib/Core/Common.cpp deleted file mode 100644 index c58e121a..00000000 --- a/lib/Core/Common.cpp +++ /dev/null @@ -1,168 +0,0 @@ -//===-- Common.cpp --------------------------------------------------------===// -// -// The KLEE Symbolic Virtual Machine -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "Common.h" -#include "llvm/ADT/StringRef.h" -#include "llvm/Support/raw_ostream.h" - -#include -#include -#include -#include -#include - -#include - -using namespace klee; - -FILE* klee::klee_warning_file = NULL; -FILE* klee::klee_message_file = NULL; - -static const char* warningPrefix = "WARNING"; -static const char* warningOncePrefix = "WARNING ONCE"; -static const char* errorPrefix = "ERROR"; -static const char* notePrefix = "NOTE"; - -static bool shouldSetColor(const char* pfx, const char* msg, const char* prefixToSearchFor) -{ - if (pfx && strcmp(pfx, prefixToSearchFor) == 0) - return true; - - if (llvm::StringRef(msg).startswith(prefixToSearchFor)) - return true; - - return false; -} - -static void klee_vfmessage(FILE *fp, const char *pfx, const char *msg, - va_list ap) { - if (!fp) - return; - - llvm::raw_fd_ostream fdos(fileno(fp), /*shouldClose=*/false, /*unbuffered=*/ true); - bool modifyConsoleColor = fdos.is_displayed() && (fp == stderr); - - if (modifyConsoleColor) { - - // Warnings - if (shouldSetColor(pfx, msg, warningPrefix)) - fdos.changeColor(llvm::raw_ostream::MAGENTA, - /*bold=*/ false, - /*bg=*/ false); - - // Once warning - if (shouldSetColor(pfx, msg, warningOncePrefix)) - fdos.changeColor(llvm::raw_ostream::MAGENTA, - /*bold=*/ true, - /*bg=*/ false); - - // Errors - if (shouldSetColor(pfx, msg, errorPrefix)) - fdos.changeColor(llvm::raw_ostream::RED, - /*bold=*/ true, - /*bg=*/ false); - - // Notes - if (shouldSetColor(pfx, msg, notePrefix)) - fdos.changeColor(llvm::raw_ostream::WHITE, - /*bold=*/ true, - /*bg=*/ false); - - } - - fdos << "KLEE: "; - if (pfx) fdos << pfx << ": "; - - // FIXME: Can't use fdos here because we need to print - // a variable number of arguments and do substitution - vfprintf(fp, msg, ap); - fflush(fp); - - fdos << "\n"; - - if (modifyConsoleColor) - fdos.resetColor(); - - fdos.flush(); -} - -/* Prints a message/warning. - - If pfx is NULL, this is a regular message, and it's sent to - klee_message_file (messages.txt). Otherwise, it is sent to - klee_warning_file (warnings.txt). - - Iff onlyToFile is false, the message is also printed on stderr. -*/ -static void klee_vmessage(const char *pfx, bool onlyToFile, const char *msg, - va_list ap) { - if (!onlyToFile) { - va_list ap2; - va_copy(ap2, ap); - klee_vfmessage(stderr, pfx, msg, ap2); - va_end(ap2); - } - - klee_vfmessage(pfx ? klee_warning_file : klee_message_file, pfx, msg, ap); -} - - -void klee::klee_message(const char *msg, ...) { - va_list ap; - va_start(ap, msg); - klee_vmessage(NULL, false, msg, ap); - va_end(ap); -} - -/* Message to be written only to file */ -void klee::klee_message_to_file(const char *msg, ...) { - va_list ap; - va_start(ap, msg); - klee_vmessage(NULL, true, msg, ap); - va_end(ap); -} - -void klee::klee_error(const char *msg, ...) { - va_list ap; - va_start(ap, msg); - klee_vmessage(errorPrefix, false, msg, ap); - va_end(ap); - exit(1); -} - -void klee::klee_warning(const char *msg, ...) { - va_list ap; - va_start(ap, msg); - klee_vmessage(warningPrefix, false, msg, ap); - va_end(ap); -} - - -/* Prints a warning once per message. */ -void klee::klee_warning_once(const void *id, const char *msg, ...) { - static std::set< std::pair > keys; - std::pair key; - - - /* "calling external" messages contain the actual arguments with - which we called the external function, so we need to ignore them - when computing the key. */ - if (strncmp(msg, "calling external", strlen("calling external")) != 0) - key = std::make_pair(id, msg); - else key = std::make_pair(id, "calling external"); - - if (!keys.count(key)) { - keys.insert(key); - - va_list ap; - va_start(ap, msg); - klee_vmessage(warningOncePrefix, false, msg, ap); - va_end(ap); - } -} diff --git a/lib/Core/Common.h b/lib/Core/Common.h deleted file mode 100644 index ce05b536..00000000 --- a/lib/Core/Common.h +++ /dev/null @@ -1,56 +0,0 @@ -//===-- Common.h ------------------------------------------------*- C++ -*-===// -// -// The KLEE Symbolic Virtual Machine -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#ifndef __KLEE_COMMON_H__ -#define __KLEE_COMMON_H__ - -#ifdef __CYGWIN__ -#ifndef WINDOWS -#define WINDOWS -#endif -#endif - -#include - -// XXX ugh -namespace klee { - class Solver; - - extern FILE* klee_warning_file; - extern FILE* klee_message_file; - - /// Print "KLEE: ERROR" followed by the msg in printf format and a - /// newline on stderr and to warnings.txt, then exit with an error. - void klee_error(const char *msg, ...) - __attribute__ ((format (printf, 1, 2), noreturn)); - - /// Print "KLEE: " followed by the msg in printf format and a - /// newline on stderr and to messages.txt. - void klee_message(const char *msg, ...) - __attribute__ ((format (printf, 1, 2))); - - /// Print "KLEE: " followed by the msg in printf format and a - /// newline to messages.txt. - void klee_message_to_file(const char *msg, ...) - __attribute__ ((format (printf, 1, 2))); - - /// Print "KLEE: WARNING" followed by the msg in printf format and a - /// newline on stderr and to warnings.txt. - void klee_warning(const char *msg, ...) - __attribute__ ((format (printf, 1, 2))); - - /// Print "KLEE: WARNING" followed by the msg in printf format and a - /// newline on stderr and to warnings.txt. However, the warning is only - /// printed once for each unique (id, msg) pair (as pointers). - void klee_warning_once(const void *id, - const char *msg, ...) - __attribute__ ((format (printf, 2, 3))); -} - -#endif /* __KLEE_COMMON_H__ */ diff --git a/lib/Core/Executor.cpp b/lib/Core/Executor.cpp index 49e526f5..0b34c357 100644 --- a/lib/Core/Executor.cpp +++ b/lib/Core/Executor.cpp @@ -7,7 +7,6 @@ // //===----------------------------------------------------------------------===// -#include "Common.h" #include "Executor.h" #include "Context.h" #include "CoreStats.h" @@ -44,6 +43,7 @@ #include "klee/Internal/Module/InstructionInfoTable.h" #include "klee/Internal/Module/KInstruction.h" #include "klee/Internal/Module/KModule.h" +#include "klee/Internal/Support/ErrorHandling.h" #include "klee/Internal/Support/FloatEvaluation.h" #include "klee/Internal/System/Time.h" #include "klee/Internal/System/MemoryUsage.h" diff --git a/lib/Core/ExecutorTimers.cpp b/lib/Core/ExecutorTimers.cpp index e4622d85..38411c4f 100644 --- a/lib/Core/ExecutorTimers.cpp +++ b/lib/Core/ExecutorTimers.cpp @@ -7,8 +7,6 @@ // //===----------------------------------------------------------------------===// -#include "Common.h" - #include "CoreStats.h" #include "Executor.h" #include "PTree.h" @@ -20,6 +18,7 @@ #include "klee/Internal/Module/KInstruction.h" #include "klee/Internal/Module/KModule.h" #include "klee/Internal/System/Time.h" +#include "klee/Internal/Support/ErrorHandling.h" #if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3) #include "llvm/IR/Function.h" diff --git a/lib/Core/Memory.cpp b/lib/Core/Memory.cpp index 07c292a0..50e9aa9f 100644 --- a/lib/Core/Memory.cpp +++ b/lib/Core/Memory.cpp @@ -7,14 +7,13 @@ // //===----------------------------------------------------------------------===// -#include "Common.h" - #include "Memory.h" #include "Context.h" #include "klee/Expr.h" #include "klee/Solver.h" #include "klee/util/BitArray.h" +#include "klee/Internal/Support/ErrorHandling.h" #include "ObjectHolder.h" #include "MemoryManager.h" diff --git a/lib/Core/MemoryManager.cpp b/lib/Core/MemoryManager.cpp index a1198007..7c76d480 100644 --- a/lib/Core/MemoryManager.cpp +++ b/lib/Core/MemoryManager.cpp @@ -7,8 +7,6 @@ // //===----------------------------------------------------------------------===// -#include "Common.h" - #include "CoreStats.h" #include "Memory.h" #include "MemoryManager.h" @@ -16,6 +14,7 @@ #include "klee/ExecutionState.h" #include "klee/Expr.h" #include "klee/Solver.h" +#include "klee/Internal/Support/ErrorHandling.h" #include "llvm/Support/CommandLine.h" diff --git a/lib/Core/Searcher.cpp b/lib/Core/Searcher.cpp index e33d2a56..cbb88727 100644 --- a/lib/Core/Searcher.cpp +++ b/lib/Core/Searcher.cpp @@ -7,8 +7,6 @@ // //===----------------------------------------------------------------------===// -#include "Common.h" - #include "Searcher.h" #include "CoreStats.h" @@ -25,6 +23,7 @@ #include "klee/Internal/ADT/RNG.h" #include "klee/Internal/Support/ModuleUtil.h" #include "klee/Internal/System/Time.h" +#include "klee/Internal/Support/ErrorHandling.h" #if LLVM_VERSION_CODE >= LLVM_VERSION(3, 3) #include "llvm/IR/Constants.h" #include "llvm/IR/Instructions.h" diff --git a/lib/Core/SeedInfo.cpp b/lib/Core/SeedInfo.cpp index b540d271..90de17ff 100644 --- a/lib/Core/SeedInfo.cpp +++ b/lib/Core/SeedInfo.cpp @@ -7,8 +7,6 @@ // //===----------------------------------------------------------------------===// -#include "Common.h" - #include "Memory.h" #include "SeedInfo.h" #include "TimingSolver.h" @@ -17,6 +15,7 @@ #include "klee/Expr.h" #include "klee/util/ExprUtil.h" #include "klee/Internal/ADT/KTest.h" +#include "klee/Internal/Support/ErrorHandling.h" using namespace klee; diff --git a/lib/Core/SpecialFunctionHandler.cpp b/lib/Core/SpecialFunctionHandler.cpp index fdd4cdd9..caec5e39 100644 --- a/lib/Core/SpecialFunctionHandler.cpp +++ b/lib/Core/SpecialFunctionHandler.cpp @@ -7,8 +7,6 @@ // //===----------------------------------------------------------------------===// -#include "Common.h" - #include "Memory.h" #include "SpecialFunctionHandler.h" #include "TimingSolver.h" @@ -18,6 +16,7 @@ #include "klee/Internal/Module/KInstruction.h" #include "klee/Internal/Module/KModule.h" #include "klee/Internal/Support/Debug.h" +#include "klee/Internal/Support/ErrorHandling.h" #include "Executor.h" #include "MemoryManager.h" diff --git a/lib/Core/StatsTracker.cpp b/lib/Core/StatsTracker.cpp index cf8a1654..4633a5c5 100644 --- a/lib/Core/StatsTracker.cpp +++ b/lib/Core/StatsTracker.cpp @@ -7,8 +7,6 @@ // //===----------------------------------------------------------------------===// -#include "Common.h" - #include "StatsTracker.h" #include "klee/ExecutionState.h" @@ -19,6 +17,7 @@ #include "klee/Internal/Module/KInstruction.h" #include "klee/Internal/Support/ModuleUtil.h" #include "klee/Internal/System/Time.h" +#include "klee/Internal/Support/ErrorHandling.h" #include "CallPathManager.h" #include "CoreStats.h" diff --git a/lib/Core/UserSearcher.cpp b/lib/Core/UserSearcher.cpp index 72f3351f..fcda26f2 100644 --- a/lib/Core/UserSearcher.cpp +++ b/lib/Core/UserSearcher.cpp @@ -7,13 +7,12 @@ // //===----------------------------------------------------------------------===// -#include "Common.h" - #include "UserSearcher.h" #include "Searcher.h" #include "Executor.h" +#include "klee/Internal/Support/ErrorHandling.h" #include "llvm/Support/CommandLine.h" using namespace llvm; diff --git a/lib/Module/KModule.cpp b/lib/Module/KModule.cpp index 1334b58c..01165e94 100644 --- a/lib/Module/KModule.cpp +++ b/lib/Module/KModule.cpp @@ -7,11 +7,9 @@ // //===----------------------------------------------------------------------===// -// FIXME: This does not belong here. -#include "../Core/Common.h" - #define DEBUG_TYPE "KModule" #include "klee/Internal/Module/KModule.h" +#include "klee/Internal/Support/ErrorHandling.h" #include "Passes.h" diff --git a/lib/Module/ModuleUtil.cpp b/lib/Module/ModuleUtil.cpp index 1cf9c35c..aabc0a57 100644 --- a/lib/Module/ModuleUtil.cpp +++ b/lib/Module/ModuleUtil.cpp @@ -11,9 +11,7 @@ #include "klee/Config/Version.h" #include "klee/Internal/Support/Debug.h" - -// FIXME: This does not belong here. -#include "../Core/Common.h" +#include "klee/Internal/Support/ErrorHandling.h" #include "../Core/SpecialFunctionHandler.h" #if LLVM_VERSION_CODE >= LLVM_VERSION(3, 4) diff --git a/lib/Solver/CexCachingSolver.cpp b/lib/Solver/CexCachingSolver.cpp index d51c1695..8b626c04 100644 --- a/lib/Solver/CexCachingSolver.cpp +++ b/lib/Solver/CexCachingSolver.cpp @@ -20,6 +20,8 @@ #include "SolverStats.h" +#include "klee/Internal/Support/ErrorHandling.h" + #include "llvm/Support/CommandLine.h" using namespace klee; diff --git a/lib/Support/ErrorHandling.cpp b/lib/Support/ErrorHandling.cpp new file mode 100644 index 00000000..7ca223e5 --- /dev/null +++ b/lib/Support/ErrorHandling.cpp @@ -0,0 +1,166 @@ +//===-- ErrorHandling.cpp -------------------------------------------------===// +// +// The KLEE Symbolic Virtual Machine +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +#include "klee/Internal/Support/ErrorHandling.h" +#include "llvm/ADT/StringRef.h" +#include "llvm/Support/raw_ostream.h" + +#include +#include +#include +#include +#include + +#include + +using namespace klee; + +FILE *klee::klee_warning_file = NULL; +FILE *klee::klee_message_file = NULL; + +static const char *warningPrefix = "WARNING"; +static const char *warningOncePrefix = "WARNING ONCE"; +static const char *errorPrefix = "ERROR"; +static const char *notePrefix = "NOTE"; + +static bool shouldSetColor(const char *pfx, const char *msg, + const char *prefixToSearchFor) { + if (pfx && strcmp(pfx, prefixToSearchFor) == 0) + return true; + + if (llvm::StringRef(msg).startswith(prefixToSearchFor)) + return true; + + return false; +} + +static void klee_vfmessage(FILE *fp, const char *pfx, const char *msg, + va_list ap) { + if (!fp) + return; + + llvm::raw_fd_ostream fdos(fileno(fp), /*shouldClose=*/false, + /*unbuffered=*/true); + bool modifyConsoleColor = fdos.is_displayed() && (fp == stderr); + + if (modifyConsoleColor) { + + // Warnings + if (shouldSetColor(pfx, msg, warningPrefix)) + fdos.changeColor(llvm::raw_ostream::MAGENTA, + /*bold=*/false, + /*bg=*/false); + + // Once warning + if (shouldSetColor(pfx, msg, warningOncePrefix)) + fdos.changeColor(llvm::raw_ostream::MAGENTA, + /*bold=*/true, + /*bg=*/false); + + // Errors + if (shouldSetColor(pfx, msg, errorPrefix)) + fdos.changeColor(llvm::raw_ostream::RED, + /*bold=*/true, + /*bg=*/false); + + // Notes + if (shouldSetColor(pfx, msg, notePrefix)) + fdos.changeColor(llvm::raw_ostream::WHITE, + /*bold=*/true, + /*bg=*/false); + } + + fdos << "KLEE: "; + if (pfx) + fdos << pfx << ": "; + + // FIXME: Can't use fdos here because we need to print + // a variable number of arguments and do substitution + vfprintf(fp, msg, ap); + fflush(fp); + + fdos << "\n"; + + if (modifyConsoleColor) + fdos.resetColor(); + + fdos.flush(); +} + +/* Prints a message/warning. + + If pfx is NULL, this is a regular message, and it's sent to + klee_message_file (messages.txt). Otherwise, it is sent to + klee_warning_file (warnings.txt). + + Iff onlyToFile is false, the message is also printed on stderr. +*/ +static void klee_vmessage(const char *pfx, bool onlyToFile, const char *msg, + va_list ap) { + if (!onlyToFile) { + va_list ap2; + va_copy(ap2, ap); + klee_vfmessage(stderr, pfx, msg, ap2); + va_end(ap2); + } + + klee_vfmessage(pfx ? klee_warning_file : klee_message_file, pfx, msg, ap); +} + +void klee::klee_message(const char *msg, ...) { + va_list ap; + va_start(ap, msg); + klee_vmessage(NULL, false, msg, ap); + va_end(ap); +} + +/* Message to be written only to file */ +void klee::klee_message_to_file(const char *msg, ...) { + va_list ap; + va_start(ap, msg); + klee_vmessage(NULL, true, msg, ap); + va_end(ap); +} + +void klee::klee_error(const char *msg, ...) { + va_list ap; + va_start(ap, msg); + klee_vmessage(errorPrefix, false, msg, ap); + va_end(ap); + exit(1); +} + +void klee::klee_warning(const char *msg, ...) { + va_list ap; + va_start(ap, msg); + klee_vmessage(warningPrefix, false, msg, ap); + va_end(ap); +} + +/* Prints a warning once per message. */ +void klee::klee_warning_once(const void *id, const char *msg, ...) { + static std::set > keys; + std::pair key; + + /* "calling external" messages contain the actual arguments with + which we called the external function, so we need to ignore them + when computing the key. */ + if (strncmp(msg, "calling external", strlen("calling external")) != 0) + key = std::make_pair(id, msg); + else + key = std::make_pair(id, "calling external"); + + if (!keys.count(key)) { + keys.insert(key); + va_list ap; + va_start(ap, msg); + klee_vmessage(warningOncePrefix, false, msg, ap); + va_end(ap); + } +} diff --git a/tools/klee/main.cpp b/tools/klee/main.cpp index 0a292500..debbe7d2 100644 --- a/tools/klee/main.cpp +++ b/tools/klee/main.cpp @@ -1,8 +1,5 @@ /* -*- mode: c++; c-basic-offset: 2; -*- */ -// FIXME: This does not belong here. -#include "../lib/Core/Common.h" - #include "klee/ExecutionState.h" #include "klee/Expr.h" #include "klee/Interpreter.h" @@ -14,6 +11,7 @@ #include "klee/Internal/Support/ModuleUtil.h" #include "klee/Internal/System/Time.h" #include "klee/Internal/Support/PrintVersion.h" +#include "klee/Internal/Support/ErrorHandling.h" #if LLVM_VERSION_CODE > LLVM_VERSION(3, 2) #include "llvm/IR/Constants.h" -- cgit 1.4.1