Fix GNU coding style for G_.
[official-gcc.git] / libsanitizer / sanitizer_common / sanitizer_flags.cc
blob5f69e58ffb27222a04b42870ffb40c88fbb6a4fb
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"
16 #include "sanitizer_list.h"
17 #include "sanitizer_flag_parser.h"
19 namespace __sanitizer {
21 CommonFlags common_flags_dont_use;
23 struct FlagDescription {
24 const char *name;
25 const char *description;
26 FlagDescription *next;
29 IntrusiveList<FlagDescription> flag_descriptions;
31 void CommonFlags::SetDefaults() {
32 #define COMMON_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
33 #include "sanitizer_flags.inc"
34 #undef COMMON_FLAG
37 void CommonFlags::CopyFrom(const CommonFlags &other) {
38 internal_memcpy(this, &other, sizeof(*this));
41 // Copy the string from "s" to "out", making the following substitutions:
42 // %b = binary basename
43 // %p = pid
44 void SubstituteForFlagValue(const char *s, char *out, uptr out_size) {
45 char *out_end = out + out_size;
46 while (*s && out < out_end - 1) {
47 if (s[0] != '%') {
48 *out++ = *s++;
49 continue;
51 switch (s[1]) {
52 case 'b': {
53 const char *base = GetProcessName();
54 CHECK(base);
55 while (*base && out < out_end - 1)
56 *out++ = *base++;
57 s += 2; // skip "%b"
58 break;
60 case 'p': {
61 int pid = internal_getpid();
62 char buf[32];
63 char *buf_pos = buf + 32;
64 do {
65 *--buf_pos = (pid % 10) + '0';
66 pid /= 10;
67 } while (pid);
68 while (buf_pos < buf + 32 && out < out_end - 1)
69 *out++ = *buf_pos++;
70 s += 2; // skip "%p"
71 break;
73 default:
74 *out++ = *s++;
75 break;
78 CHECK(out < out_end - 1);
79 *out = '\0';
82 class FlagHandlerInclude : public FlagHandlerBase {
83 FlagParser *parser_;
84 bool ignore_missing_;
86 public:
87 explicit FlagHandlerInclude(FlagParser *parser, bool ignore_missing)
88 : parser_(parser), ignore_missing_(ignore_missing) {}
89 bool Parse(const char *value) final {
90 if (internal_strchr(value, '%')) {
91 char *buf = (char *)MmapOrDie(kMaxPathLength, "FlagHandlerInclude");
92 SubstituteForFlagValue(value, buf, kMaxPathLength);
93 bool res = parser_->ParseFile(buf, ignore_missing_);
94 UnmapOrDie(buf, kMaxPathLength);
95 return res;
97 return parser_->ParseFile(value, ignore_missing_);
101 void RegisterIncludeFlags(FlagParser *parser, CommonFlags *cf) {
102 FlagHandlerInclude *fh_include = new (FlagParser::Alloc) // NOLINT
103 FlagHandlerInclude(parser, /*ignore_missing*/ false);
104 parser->RegisterHandler("include", fh_include,
105 "read more options from the given file");
106 FlagHandlerInclude *fh_include_if_exists = new (FlagParser::Alloc) // NOLINT
107 FlagHandlerInclude(parser, /*ignore_missing*/ true);
108 parser->RegisterHandler(
109 "include_if_exists", fh_include_if_exists,
110 "read more options from the given file (if it exists)");
113 void RegisterCommonFlags(FlagParser *parser, CommonFlags *cf) {
114 #define COMMON_FLAG(Type, Name, DefaultValue, Description) \
115 RegisterFlag(parser, #Name, Description, &cf->Name);
116 #include "sanitizer_flags.inc"
117 #undef COMMON_FLAG
119 RegisterIncludeFlags(parser, cf);
122 void InitializeCommonFlags(CommonFlags *cf) {
123 // need to record coverage to generate coverage report.
124 cf->coverage |= cf->html_cov_report;
125 SetVerbosity(cf->verbosity);
128 } // namespace __sanitizer