New assertions support

This adds three new assertion macros:
* `ASSERT` - always compiled in, always checked
* `CONDITIONAL_ASSERT` - always compiled in, checked whenever the `-compiler-assertions` flag is provided
* `DEBUG_ASSERT` - only compiled into debug builds, always checked when compiled in  (functionally the same as Standard C `assert`)

The new `-compiler-assertions` flag is recognized by both `swift-frontend` and
`swiftc`.

The goal is to eventually replace every use of `assert` in the compiler with one of the above:
* Most assertions will use `ASSERT` (most assertions should always be present and checked, even in release builds)
* Expensive assertions can use `CONDITIONAL_ASSERT` to be suppressed by default
* A few very expensive and/or brittle assertions can use `DEBUG_ASSERT` to be compiled out of release builds

This should:
* Improve quality by catching errors earlier,
* Accelerate compiler triage and debugging by providing more accurate crash dumps by default, and
* Allow compiler engineers and end users alike to add `-compiler-assertions` to get more accurate failure diagnostics with any compiler
This commit is contained in:
Tim Kientzle
2024-01-04 09:08:55 -08:00
parent face056ec3
commit 5b49a04ddc
8 changed files with 252 additions and 25 deletions

View File

@@ -15,6 +15,7 @@
#include "ArgsToFrontendOptionsConverter.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/Basic/Assertions.h"
#include "swift/Basic/Feature.h"
#include "swift/Basic/Platform.h"
#include "swift/Option/Options.h"
@@ -328,6 +329,13 @@ bool CompilerInvocation::setModuleAliasMap(std::vector<std::string> args,
return ModuleAliasesConverter::computeModuleAliases(args, FrontendOpts, diags);
}
static void ParseAssertionArgs(ArgList &args) {
using namespace options;
if (args.hasArg(OPT_compiler_assertions)) {
CONDITIONAL_ASSERT_Global_enable_flag = 1;
}
}
static bool ParseFrontendArgs(
FrontendOptions &opts, ArgList &args, DiagnosticEngine &diags,
SmallVectorImpl<std::unique_ptr<llvm::MemoryBuffer>> *buffers) {
@@ -3392,6 +3400,8 @@ bool CompilerInvocation::parseArgs(
return true;
}
ParseAssertionArgs(ParsedArgs);
if (ParseLangArgs(LangOpts, ParsedArgs, Diags, FrontendOpts)) {
return true;
}