1 //===-- flags.cpp -----------------------------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "flags.h"
10 #include "common.h"
11 #include "flags_parser.h"
12 #include "interface.h"
13 
14 namespace scudo {
15 
16 Flags *getFlags() {
17   static Flags F;
18   return &F;
19 }
20 
21 void Flags::setDefaults() {
22 #define SCUDO_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
23 #include "flags.inc"
24 #undef SCUDO_FLAG
25 }
26 
27 void registerFlags(FlagParser *Parser, Flags *F) {
28 #define SCUDO_FLAG(Type, Name, DefaultValue, Description)                      \
29   Parser->registerFlag(#Name, Description, FlagType::FT_##Type,                \
30                        reinterpret_cast<void *>(&F->Name));
31 #include "flags.inc"
32 #undef SCUDO_FLAG
33 }
34 
35 static const char *getCompileDefinitionScudoDefaultOptions() {
36 #ifdef SCUDO_DEFAULT_OPTIONS
37   return STRINGIFY(SCUDO_DEFAULT_OPTIONS);
38 #else
39   return "";
40 #endif
41 }
42 
43 static const char *getScudoDefaultOptions() {
44   return (&__scudo_default_options) ? __scudo_default_options() : "";
45 }
46 
47 void initFlags() {
48   Flags *F = getFlags();
49   F->setDefaults();
50   FlagParser Parser;
51   registerFlags(&Parser, F);
52   Parser.parseString(getCompileDefinitionScudoDefaultOptions());
53   Parser.parseString(getScudoDefaultOptions());
54   Parser.parseString(getEnv("SCUDO_OPTIONS"));
55 }
56 
57 } // namespace scudo
58