Disable asserts, avoid line numbers in runtime checks. (#2216)

* Disable asserts, avoid line numbers in runtime checks.

* Rework assertion enabling.

* Review feedback.
This commit is contained in:
Nikolay Igotti
2018-10-17 08:42:45 +03:00
committed by GitHub
parent 44d0f5b795
commit b2fa9df629
3 changed files with 28 additions and 13 deletions
@@ -385,6 +385,7 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
BitcodeEmbedding.processModule(context.llvm)
appendDebugSelector()
appendLlvmUsed("llvm.used", context.llvm.usedFunctions + context.llvm.usedGlobals)
appendLlvmUsed("llvm.compiler.used", context.llvm.compilerUsedGlobals)
appendStaticInitializers()
@@ -2626,14 +2627,21 @@ internal class CodeGeneratorVisitor(val context: Context, val lifetimes: Map<IrE
private fun appendLlvmUsed(name: String, args: List<LLVMValueRef>) {
if (args.isEmpty()) return
memScoped {
val argsCasted = args.map { it -> constPointer(it).bitcast(int8TypePtr) }
val llvmUsedGlobal =
val argsCasted = args.map { it -> constPointer(it).bitcast(int8TypePtr) }
val llvmUsedGlobal =
context.llvm.staticData.placeGlobalArray(name, int8TypePtr, argsCasted)
LLVMSetLinkage(llvmUsedGlobal.llvmGlobal, LLVMLinkage.LLVMAppendingLinkage)
LLVMSetSection(llvmUsedGlobal.llvmGlobal, "llvm.metadata")
}
LLVMSetLinkage(llvmUsedGlobal.llvmGlobal, LLVMLinkage.LLVMAppendingLinkage)
LLVMSetSection(llvmUsedGlobal.llvmGlobal, "llvm.metadata")
}
private fun appendDebugSelector() {
if (!context.config.produce.isNativeBinary) return
val llvmDebugSelector =
context.llvm.staticData.placeGlobal("KonanNeedDebugInfo",
Int32(if (context.shouldContainDebugInfo()) 1 else 0))
llvmDebugSelector.setConstant(true)
llvmDebugSelector.setLinkage(LLVMLinkage.LLVMExternalLinkage)
}
//-------------------------------------------------------------------------//
+4 -1
View File
@@ -20,7 +20,10 @@
void RuntimeAssertFailed(const char* location, const char* message) {
// TODO: produce stacktrace and such.
char buf[1024];
konan::snprintf(buf, sizeof(buf), "%s: runtime assert: %s\n", location, message);
if (location != nullptr)
konan::snprintf(buf, sizeof(buf), "%s: runtime assert: %s\n", location, message);
else
konan::snprintf(buf, sizeof(buf), "runtime assert: %s\n", message);
konan::consoleErrorUtf8(buf, konan::strnlen(buf, sizeof(buf)));
konan::abort();
}
+10 -6
View File
@@ -27,21 +27,25 @@
RUNTIME_NORETURN void RuntimeAssertFailed(const char* location, const char* message);
// During codegeneration we set this constant to 1 or 0 to allow bitcode optimizer
// to get rid of code behind condition.
extern "C" const int KonanNeedDebugInfo;
#if KONAN_ENABLE_ASSERT
// Use RuntimeAssert() in internal state checks, which could be ignored in production.
#define RuntimeAssert(condition, message) \
if (!(condition)) { \
#define RuntimeAssert(condition, message) \
if (KonanNeedDebugInfo && (!(condition))) { \
RuntimeAssertFailed( __FILE__ ":" TOSTRING(__LINE__), message); \
}
}
#else
#define RuntimeAssert(condition, message)
#endif
// Use RuntimeCheck() in runtime checks that could fail due to external condition and shall lead
// to program termination. Never compiled out.
#define RuntimeCheck(condition, message) \
if (!(condition)) { \
RuntimeAssertFailed( __FILE__ ":" TOSTRING(__LINE__), message); \
#define RuntimeCheck(condition, message) \
if (!(condition)) { \
RuntimeAssertFailed(nullptr, message); \
}
#endif // RUNTIME_ASSERT_H