mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
There are a few environment variables used to enable debugging options in the runtime, and we'll likely add more over time. These are implemented with scattered getenv() calls at the point of use. This is inefficient, as most/all OSes have to do a linear scan of the environment for each call. It's also not discoverable, since the only way to find these variables is to inspect the source. This commit places all of these variables in a central location. stdlib/public/runtime/EnvironmentVariables.def defines all of the debug variables including their name, type, default value, and a help string. On OSes which make an `environ` array available, the entire array is scanned in a single pass the first time any debug variable is requested. By quickly rejecting variables that do not start with `SWIFT_`, we optimize for the common case where no debug variables are set. We also have a fallback to repeated `getenv()` calls when a full scan is not possible. Setting `SWIFT_HELP=YES` will print out all available debug variables along with a brief description of what they do.
42 lines
1.4 KiB
C++
42 lines
1.4 KiB
C++
//===--- EnvironmentVariables.h - Debug variables. --------------*- C++ -*-===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See https://swift.org/LICENSE.txt for license information
|
|
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// Debug behavior conditionally enabled using environment variables.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "../Basic/Lazy.h"
|
|
|
|
namespace swift {
|
|
namespace runtime {
|
|
namespace environment {
|
|
|
|
void initialize(void *);
|
|
|
|
extern OnceToken_t initializeToken;
|
|
|
|
// Declare backing variables.
|
|
#define VARIABLE(name, type, defaultValue, help) extern type name ## _variable;
|
|
#include "../../../stdlib/public/runtime/EnvironmentVariables.def"
|
|
|
|
// Define getter functions.
|
|
#define VARIABLE(name, type, defaultValue, help) \
|
|
inline type name() { \
|
|
SWIFT_ONCE_F(initializeToken, initialize, nullptr); \
|
|
return name ## _variable; \
|
|
}
|
|
#include "../../../stdlib/public/runtime/EnvironmentVariables.def"
|
|
|
|
} // end namespace environment
|
|
} // end namespace runtime
|
|
} // end namespace Swift
|