* config/rs6000/rs6000.md (largetoc_low): Revert.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_flags.cc
blob837738ceb81cceab0adf54534fb22be5267eb1ea
1 //===-- sanitizer_flags.cc ------------------------------------------------===//
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 #include "sanitizer_flags.h"
14 #include "sanitizer_common.h"
15 #include "sanitizer_libc.h"
17 namespace __sanitizer {
19 static bool GetFlagValue(const char *env, const char *name,
20 const char **value, int *value_length) {
21 if (env == 0)
22 return false;
23 const char *pos = internal_strstr(env, name);
24 const char *end;
25 if (pos == 0)
26 return false;
27 pos += internal_strlen(name);
28 if (pos[0] != '=') {
29 end = pos;
30 } else {
31 pos += 1;
32 if (pos[0] == '"') {
33 pos += 1;
34 end = internal_strchr(pos, '"');
35 } else if (pos[0] == '\'') {
36 pos += 1;
37 end = internal_strchr(pos, '\'');
38 } else {
39 end = internal_strchr(pos, ' ');
41 if (end == 0)
42 end = pos + internal_strlen(pos);
44 *value = pos;
45 *value_length = end - pos;
46 return true;
49 static bool StartsWith(const char *flag, int flag_length, const char *value) {
50 if (!flag || !value)
51 return false;
52 int value_length = internal_strlen(value);
53 return (flag_length >= value_length) &&
54 (0 == internal_strncmp(flag, value, value_length));
57 void ParseFlag(const char *env, bool *flag, const char *name) {
58 const char *value;
59 int value_length;
60 if (!GetFlagValue(env, name, &value, &value_length))
61 return;
62 if (StartsWith(value, value_length, "0") ||
63 StartsWith(value, value_length, "no") ||
64 StartsWith(value, value_length, "false"))
65 *flag = false;
66 if (StartsWith(value, value_length, "1") ||
67 StartsWith(value, value_length, "yes") ||
68 StartsWith(value, value_length, "true"))
69 *flag = true;
72 void ParseFlag(const char *env, int *flag, const char *name) {
73 const char *value;
74 int value_length;
75 if (!GetFlagValue(env, name, &value, &value_length))
76 return;
77 *flag = internal_atoll(value);
80 static LowLevelAllocator allocator_for_flags;
82 void ParseFlag(const char *env, const char **flag, const char *name) {
83 const char *value;
84 int value_length;
85 if (!GetFlagValue(env, name, &value, &value_length))
86 return;
87 // Copy the flag value. Don't use locks here, as flags are parsed at
88 // tool startup.
89 char *value_copy = (char*)(allocator_for_flags.Allocate(value_length + 1));
90 internal_memcpy(value_copy, value, value_length);
91 value_copy[value_length] = '\0';
92 *flag = value_copy;
95 } // namespace __sanitizer