Fix i686-pc-cygwin build failure.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_flag_parser.h
blob7827d7357706c077c9edb6820fe837ae5af763c3
1 //===-- sanitizer_flag_parser.h ---------------------------------*- C++ -*-===//
2 //
3 // This file is distributed under the University of Illinois Open Source
4 // License. See LICENSE.TXT for details.
5 //
6 //===----------------------------------------------------------------------===//
7 //
8 // This file is a part of ThreadSanitizer/AddressSanitizer runtime.
9 //
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 {
22 public:
23 virtual bool Parse(const char *value) { return false; }
26 template <typename T>
27 class FlagHandler : public FlagHandlerBase {
28 T *t_;
30 public:
31 explicit FlagHandler(T *t) : t_(t) {}
32 bool Parse(const char *value) final;
35 template <>
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) {
40 *t_ = false;
41 return true;
43 if (internal_strcmp(value, "1") == 0 ||
44 internal_strcmp(value, "yes") == 0 ||
45 internal_strcmp(value, "true") == 0) {
46 *t_ = true;
47 return true;
49 Printf("ERROR: Invalid value for bool option: '%s'\n", value);
50 return false;
53 template <>
54 inline bool FlagHandler<const char *>::Parse(const char *value) {
55 *t_ = internal_strdup(value);
56 return true;
59 template <>
60 inline bool FlagHandler<int>::Parse(const char *value) {
61 char *value_end;
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);
65 return ok;
68 template <>
69 inline bool FlagHandler<uptr>::Parse(const char *value) {
70 char *value_end;
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);
74 return ok;
77 class FlagParser {
78 static const int kMaxFlags = 200;
79 struct Flag {
80 const char *name;
81 const char *desc;
82 FlagHandlerBase *handler;
83 } *flags_;
84 int n_flags_;
86 const char *buf_;
87 uptr pos_;
89 public:
90 FlagParser();
91 void RegisterHandler(const char *name, FlagHandlerBase *handler,
92 const char *desc);
93 void ParseString(const char *s);
94 bool ParseFile(const char *path, bool ignore_missing);
95 void PrintFlagDescriptions();
97 static LowLevelAllocator Alloc;
99 private:
100 void fatal_error(const char *err);
101 bool is_space(char c);
102 void skip_whitespace();
103 void parse_flags();
104 void parse_flag();
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,
111 T *var) {
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