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 
13 #include "scudo/interface.h"
14 
15 namespace scudo {
16 
17 Flags *getFlags() {
18   static Flags F;
19   return &F;
20 }
21 
22 void Flags::setDefaults() {
23 #define SCUDO_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
24 #include "flags.inc"
25 #undef SCUDO_FLAG
26 }
27 
28 void registerFlags(FlagParser *Parser, Flags *F) {
29 #define SCUDO_FLAG(Type, Name, DefaultValue, Description)                      \
30   Parser->registerFlag(#Name, Description, FlagType::FT_##Type,                \
31                        reinterpret_cast<void *>(&F->Name));
32 #include "flags.inc"
33 #undef SCUDO_FLAG
34 }
35 
36 static const char *getCompileDefinitionScudoDefaultOptions() {
37 #ifdef SCUDO_DEFAULT_OPTIONS
38   return STRINGIFY(SCUDO_DEFAULT_OPTIONS);
39 #else
40   return "";
41 #endif
42 }
43 
44 static const char *getScudoDefaultOptions() {
45   return (&__scudo_default_options) ? __scudo_default_options() : "";
46 }
47 
48 void initFlags() {
49   Flags *F = getFlags();
50   F->setDefaults();
51   FlagParser Parser;
52   registerFlags(&Parser, F);
53   Parser.parseString(getCompileDefinitionScudoDefaultOptions());
54   Parser.parseString(getScudoDefaultOptions());
55   Parser.parseString(getEnv("SCUDO_OPTIONS"));
56 }
57 
58 } // namespace scudo
59