1 //===-- sanitizer_flag_parser.h ---------------------------------*- C++ -*-===//
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
6 //===----------------------------------------------------------------------===//
8 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
10 //===----------------------------------------------------------------------===//
12 #ifndef SANITIZER_FLAG_REGISTRY_H
13 #define SANITIZER_FLAG_REGISTRY_H
15 #include "sanitizer_internal_defs.h"
16 #include "sanitizer_libc.h"
17 #include "sanitizer_common.h"
19 namespace __sanitizer
{
21 class FlagHandlerBase
{
23 virtual bool Parse(const char *value
) { return false; }
27 class FlagHandler
: public FlagHandlerBase
{
31 explicit FlagHandler(T
*t
) : t_(t
) {}
32 bool Parse(const char *value
) final
;
36 inline bool FlagHandler
<bool>::Parse(const char *value
) {
37 if (internal_strcmp(value
, "0") == 0 ||
38 internal_strcmp(value
, "no") == 0 ||
39 internal_strcmp(value
, "false") == 0) {
43 if (internal_strcmp(value
, "1") == 0 ||
44 internal_strcmp(value
, "yes") == 0 ||
45 internal_strcmp(value
, "true") == 0) {
49 Printf("ERROR: Invalid value for bool option: '%s'\n", value
);
54 inline bool FlagHandler
<const char *>::Parse(const char *value
) {
55 *t_
= internal_strdup(value
);
60 inline bool FlagHandler
<int>::Parse(const char *value
) {
62 *t_
= internal_simple_strtoll(value
, &value_end
, 10);
63 bool ok
= *value_end
== 0;
64 if (!ok
) Printf("ERROR: Invalid value for int option: '%s'\n", value
);
69 inline bool FlagHandler
<uptr
>::Parse(const char *value
) {
71 *t_
= internal_simple_strtoll(value
, &value_end
, 10);
72 bool ok
= *value_end
== 0;
73 if (!ok
) Printf("ERROR: Invalid value for uptr option: '%s'\n", value
);
78 static const int kMaxFlags
= 200;
82 FlagHandlerBase
*handler
;
91 void RegisterHandler(const char *name
, FlagHandlerBase
*handler
,
93 void ParseString(const char *s
);
94 bool ParseFile(const char *path
, bool ignore_missing
);
95 void PrintFlagDescriptions();
97 static LowLevelAllocator Alloc
;
100 void fatal_error(const char *err
);
101 bool is_space(char c
);
102 void skip_whitespace();
105 bool run_handler(const char *name
, const char *value
);
106 char *ll_strndup(const char *s
, uptr n
);
109 template <typename T
>
110 static void RegisterFlag(FlagParser
*parser
, const char *name
, const char *desc
,
112 FlagHandler
<T
> *fh
= new (FlagParser::Alloc
) FlagHandler
<T
>(var
); // NOLINT
113 parser
->RegisterHandler(name
, fh
, desc
);
116 void ReportUnrecognizedFlags();
118 } // namespace __sanitizer
120 #endif // SANITIZER_FLAG_REGISTRY_H