blob: 52a6c2ec4b13d207bd250a1caf14c365ac92b358 [file] [log] [blame]
Alexey Samsonovbb1071c2012-11-06 15:09:03 +00001//===--- SanitizerArgs.h - Arguments for sanitizer tools -------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#ifndef CLANG_LIB_DRIVER_SANITIZERARGS_H_
10#define CLANG_LIB_DRIVER_SANITIZERARGS_H_
11
12#include "clang/Driver/ArgList.h"
13
14namespace clang {
15namespace driver {
16
17class SanitizerArgs {
18 /// Assign ordinals to sanitizer flags. We'll use the ordinal values as
19 /// bit positions within \c Kind.
20 enum SanitizeOrdinal {
21#define SANITIZER(NAME, ID) SO_##ID,
22#include "clang/Basic/Sanitizers.def"
23 SO_Count
24 };
25
26 /// Bugs to catch at runtime.
27 enum SanitizeKind {
28#define SANITIZER(NAME, ID) ID = 1 << SO_##ID,
29#define SANITIZER_GROUP(NAME, ID, ALIAS) ID = ALIAS,
30#include "clang/Basic/Sanitizers.def"
31 NeedsAsanRt = Address,
32 NeedsTsanRt = Thread,
Will Dietzb8540362012-11-27 15:01:55 +000033 NeedsUbsanRt = (Undefined & ~Bounds) | Integer
Alexey Samsonovbb1071c2012-11-06 15:09:03 +000034 };
35 unsigned Kind;
36
37 public:
38 SanitizerArgs() : Kind(0) {}
39 /// Parses the sanitizer arguments from an argument list.
40 SanitizerArgs(const Driver &D, const ArgList &Args);
41
42 bool needsAsanRt() const { return Kind & NeedsAsanRt; }
43 bool needsTsanRt() const { return Kind & NeedsTsanRt; }
44 bool needsUbsanRt() const { return Kind & NeedsUbsanRt; }
45
46 bool sanitizesVptr() const { return Kind & Vptr; }
47
48 void addArgs(const ArgList &Args, ArgStringList &CmdArgs) const {
49 if (!Kind)
50 return;
51 llvm::SmallString<256> SanitizeOpt("-fsanitize=");
52#define SANITIZER(NAME, ID) \
53 if (Kind & ID) \
54 SanitizeOpt += NAME ",";
55#include "clang/Basic/Sanitizers.def"
56 SanitizeOpt.pop_back();
57 CmdArgs.push_back(Args.MakeArgString(SanitizeOpt));
58 }
59
60 private:
61 /// Parse a single value from a -fsanitize= or -fno-sanitize= value list.
62 /// Returns a member of the \c SanitizeKind enumeration, or \c 0 if \p Value
63 /// is not known.
64 static unsigned parse(const char *Value) {
65 return llvm::StringSwitch<SanitizeKind>(Value)
66#define SANITIZER(NAME, ID) .Case(NAME, ID)
67#define SANITIZER_GROUP(NAME, ID, ALIAS) .Case(NAME, ID)
68#include "clang/Basic/Sanitizers.def"
69 .Default(SanitizeKind());
70 }
71
72 /// Parse a -fsanitize= or -fno-sanitize= argument's values, diagnosing any
73 /// invalid components.
Alexey Samsonov3325b162012-11-28 17:34:24 +000074 static unsigned parse(const Driver &D, const Arg *A, bool DiagnoseErrors) {
Alexey Samsonovbb1071c2012-11-06 15:09:03 +000075 unsigned Kind = 0;
76 for (unsigned I = 0, N = A->getNumValues(); I != N; ++I) {
77 if (unsigned K = parse(A->getValue(I)))
78 Kind |= K;
Alexey Samsonov3325b162012-11-28 17:34:24 +000079 else if (DiagnoseErrors)
Alexey Samsonovbb1071c2012-11-06 15:09:03 +000080 D.Diag(diag::err_drv_unsupported_option_argument)
81 << A->getOption().getName() << A->getValue(I);
82 }
83 return Kind;
84 }
85
Alexey Samsonov3325b162012-11-28 17:34:24 +000086 /// Parse a single flag of the form -f[no]sanitize=, or
87 /// -f*-sanitizer. Sets the masks defining required change of Kind value.
88 /// Returns true if the flag was parsed successfully.
89 static bool parse(const Driver &D, const ArgList &Args, const Arg *A,
90 unsigned &Add, unsigned &Remove, bool DiagnoseErrors) {
91 Add = 0;
92 Remove = 0;
93 const char *DeprecatedReplacement = 0;
94 if (A->getOption().matches(options::OPT_faddress_sanitizer)) {
95 Add = Address;
96 DeprecatedReplacement = "-fsanitize=address";
97 } else if (A->getOption().matches(options::OPT_fno_address_sanitizer)) {
98 Remove = Address;
99 DeprecatedReplacement = "-fno-sanitize=address";
100 } else if (A->getOption().matches(options::OPT_fthread_sanitizer)) {
101 Add = Thread;
102 DeprecatedReplacement = "-fsanitize=thread";
103 } else if (A->getOption().matches(options::OPT_fno_thread_sanitizer)) {
104 Remove = Thread;
105 DeprecatedReplacement = "-fno-sanitize=thread";
106 } else if (A->getOption().matches(options::OPT_fcatch_undefined_behavior)) {
107 Add = Undefined;
108 DeprecatedReplacement = "-fsanitize=undefined";
109 } else if (A->getOption().matches(options::OPT_fbounds_checking) ||
110 A->getOption().matches(options::OPT_fbounds_checking_EQ)) {
111 Add = Bounds;
112 DeprecatedReplacement = "-fsanitize=bounds";
113 } else if (A->getOption().matches(options::OPT_fsanitize_EQ)) {
114 Add = parse(D, A, DiagnoseErrors);
115 } else if (A->getOption().matches(options::OPT_fno_sanitize_EQ)) {
116 Remove = parse(D, A, DiagnoseErrors);
117 } else {
118 // Flag is not relevant to sanitizers.
119 return false;
120 }
121 // If this is a deprecated synonym, produce a warning directing users
122 // towards the new spelling.
123 if (DeprecatedReplacement && DiagnoseErrors)
124 D.Diag(diag::warn_drv_deprecated_arg)
125 << A->getAsString(Args) << DeprecatedReplacement;
126 return true;
127 }
128
129 /// Produce an argument string from ArgList \p Args, which shows how it
130 /// provides a sanitizer kind in \p Mask. For example, the argument list
131 /// "-fsanitize=thread,vptr -faddress-sanitizer" with mask \c NeedsUbsanRt
132 /// would produce "-fsanitize=vptr".
133 static std::string lastArgumentForKind(const Driver &D, const ArgList &Args,
134 unsigned Kind) {
135 for (ArgList::const_reverse_iterator I = Args.rbegin(), E = Args.rend();
136 I != E; ++I) {
137 unsigned Add, Remove;
138 if (parse(D, Args, *I, Add, Remove, false) &&
139 (Add & Kind))
140 return describeSanitizeArg(Args, *I, Kind);
141 Kind &= ~Remove;
142 }
143 llvm_unreachable("arg list didn't provide expected value");
144 }
145
Alexey Samsonovbb1071c2012-11-06 15:09:03 +0000146 /// Produce an argument string from argument \p A, which shows how it provides
147 /// a value in \p Mask. For instance, the argument
148 /// "-fsanitize=address,alignment" with mask \c NeedsUbsanRt would produce
149 /// "-fsanitize=alignment".
150 static std::string describeSanitizeArg(const ArgList &Args, const Arg *A,
151 unsigned Mask) {
152 if (!A->getOption().matches(options::OPT_fsanitize_EQ))
153 return A->getAsString(Args);
154
155 for (unsigned I = 0, N = A->getNumValues(); I != N; ++I)
156 if (parse(A->getValue(I)) & Mask)
157 return std::string("-fsanitize=") + A->getValue(I);
158
159 llvm_unreachable("arg didn't provide expected value");
160 }
161};
162
163} // namespace driver
164} // namespace clang
165
166#endif // CLANG_LIB_DRIVER_SANITIZERARGS_H_