Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/tools/clang/lib/Driver/XRayArgs.cpp
Line
Count
Source (jump to first uncovered line)
1
//===--- XRayArgs.cpp - Arguments for XRay --------------------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
#include "clang/Driver/XRayArgs.h"
9
#include "ToolChains/CommonArgs.h"
10
#include "clang/Driver/Driver.h"
11
#include "clang/Driver/DriverDiagnostic.h"
12
#include "clang/Driver/Options.h"
13
#include "clang/Driver/ToolChain.h"
14
#include "llvm/ADT/StringExtras.h"
15
#include "llvm/ADT/StringSwitch.h"
16
#include "llvm/Support/FileSystem.h"
17
#include "llvm/Support/Path.h"
18
#include "llvm/Support/ScopedPrinter.h"
19
#include "llvm/Support/SpecialCaseList.h"
20
21
using namespace clang;
22
using namespace clang::driver;
23
using namespace llvm::opt;
24
25
namespace {
26
constexpr char XRayInstrumentOption[] = "-fxray-instrument";
27
constexpr char XRayInstructionThresholdOption[] =
28
    "-fxray-instruction-threshold=";
29
constexpr const char *const XRaySupportedModes[] = {"xray-fdr", "xray-basic"};
30
} // namespace
31
32
30.8k
XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
33
30.8k
  const Driver &D = TC.getDriver();
34
30.8k
  const llvm::Triple &Triple = TC.getTriple();
35
30.8k
  if (Args.hasFlag(options::OPT_fxray_instrument,
36
30.8k
                   options::OPT_fnoxray_instrument, false)) {
37
5
    if (Triple.getOS() == llvm::Triple::Linux) {
38
0
      switch (Triple.getArch()) {
39
0
      case llvm::Triple::x86_64:
40
0
      case llvm::Triple::arm:
41
0
      case llvm::Triple::aarch64:
42
0
      case llvm::Triple::ppc64le:
43
0
      case llvm::Triple::mips:
44
0
      case llvm::Triple::mipsel:
45
0
      case llvm::Triple::mips64:
46
0
      case llvm::Triple::mips64el:
47
0
        break;
48
0
      default:
49
0
        D.Diag(diag::err_drv_clang_unsupported)
50
0
            << (std::string(XRayInstrumentOption) + " on " + Triple.str());
51
0
      }
52
5
    } else if (Triple.isOSFreeBSD() ||
53
5
               Triple.isOSOpenBSD() ||
54
5
               Triple.isOSNetBSD() ||
55
5
               Triple.getOS() == llvm::Triple::Darwin) {
56
3
      if (Triple.getArch() != llvm::Triple::x86_64) {
57
1
        D.Diag(diag::err_drv_clang_unsupported)
58
1
            << (std::string(XRayInstrumentOption) + " on " + Triple.str());
59
1
      }
60
3
    } else 
if (2
Triple.getOS() == llvm::Triple::Fuchsia2
) {
61
2
      switch (Triple.getArch()) {
62
2
      case llvm::Triple::x86_64:
63
2
      case llvm::Triple::aarch64:
64
2
        break;
65
2
      default:
66
0
        D.Diag(diag::err_drv_clang_unsupported)
67
0
            << (std::string(XRayInstrumentOption) + " on " + Triple.str());
68
2
      }
69
2
    } else {
70
0
      D.Diag(diag::err_drv_clang_unsupported)
71
0
          << (std::string(XRayInstrumentOption) + " on " + Triple.str());
72
0
    }
73
5
    XRayInstrument = true;
74
5
    if (const Arg *A =
75
0
            Args.getLastArg(options::OPT_fxray_instruction_threshold_,
76
0
                            options::OPT_fxray_instruction_threshold_EQ)) {
77
0
      StringRef S = A->getValue();
78
0
      if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0)
79
0
        D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
80
0
    }
81
5
82
5
    // By default, the back-end will not emit the lowering for XRay customevent
83
5
    // calls if the function is not instrumented. In the future we will change
84
5
    // this default to be the reverse, but in the meantime we're going to
85
5
    // introduce the new functionality behind a flag.
86
5
    if (Args.hasFlag(options::OPT_fxray_always_emit_customevents,
87
5
                     options::OPT_fnoxray_always_emit_customevents, false))
88
0
      XRayAlwaysEmitCustomEvents = true;
89
5
90
5
    if (Args.hasFlag(options::OPT_fxray_always_emit_typedevents,
91
5
                     options::OPT_fnoxray_always_emit_typedevents, false))
92
0
      XRayAlwaysEmitTypedEvents = true;
93
5
94
5
    if (!Args.hasFlag(options::OPT_fxray_link_deps,
95
5
                      options::OPT_fnoxray_link_deps, true))
96
0
      XRayRT = false;
97
5
98
5
    auto Bundles =
99
5
        Args.getAllArgValues(options::OPT_fxray_instrumentation_bundle);
100
5
    if (Bundles.empty())
101
5
      InstrumentationBundle.Mask = XRayInstrKind::All;
102
0
    else
103
0
      for (const auto &B : Bundles) {
104
0
        llvm::SmallVector<StringRef, 2> BundleParts;
105
0
        llvm::SplitString(B, BundleParts, ",");
106
0
        for (const auto &P : BundleParts) {
107
0
          // TODO: Automate the generation of the string case table.
108
0
          auto Valid = llvm::StringSwitch<bool>(P)
109
0
                           .Cases("none", "all", "function", "custom", true)
110
0
                           .Default(false);
111
0
112
0
          if (!Valid) {
113
0
            D.Diag(clang::diag::err_drv_invalid_value)
114
0
                << "-fxray-instrumentation-bundle=" << P;
115
0
            continue;
116
0
          }
117
0
118
0
          auto Mask = parseXRayInstrValue(P);
119
0
          if (Mask == XRayInstrKind::None) {
120
0
            InstrumentationBundle.clear();
121
0
            break;
122
0
          }
123
0
124
0
          InstrumentationBundle.Mask |= Mask;
125
0
        }
126
0
      }
127
5
128
5
    // Validate the always/never attribute files. We also make sure that they
129
5
    // are treated as actual dependencies.
130
5
    for (const auto &Filename :
131
5
         Args.getAllArgValues(options::OPT_fxray_always_instrument)) {
132
0
      if (llvm::sys::fs::exists(Filename)) {
133
0
        AlwaysInstrumentFiles.push_back(Filename);
134
0
        ExtraDeps.push_back(Filename);
135
0
      } else
136
0
        D.Diag(clang::diag::err_drv_no_such_file) << Filename;
137
0
    }
138
5
139
5
    for (const auto &Filename :
140
5
         Args.getAllArgValues(options::OPT_fxray_never_instrument)) {
141
0
      if (llvm::sys::fs::exists(Filename)) {
142
0
        NeverInstrumentFiles.push_back(Filename);
143
0
        ExtraDeps.push_back(Filename);
144
0
      } else
145
0
        D.Diag(clang::diag::err_drv_no_such_file) << Filename;
146
0
    }
147
5
148
5
    for (const auto &Filename :
149
5
         Args.getAllArgValues(options::OPT_fxray_attr_list)) {
150
0
      if (llvm::sys::fs::exists(Filename)) {
151
0
        AttrListFiles.push_back(Filename);
152
0
        ExtraDeps.push_back(Filename);
153
0
      } else
154
0
        D.Diag(clang::diag::err_drv_no_such_file) << Filename;
155
0
    }
156
5
157
5
    // Get the list of modes we want to support.
158
5
    auto SpecifiedModes = Args.getAllArgValues(options::OPT_fxray_modes);
159
5
    if (SpecifiedModes.empty())
160
3
      llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
161
2
    else
162
2
      for (const auto &Arg : SpecifiedModes) {
163
2
        // Parse CSV values for -fxray-modes=...
164
2
        llvm::SmallVector<StringRef, 2> ModeParts;
165
2
        llvm::SplitString(Arg, ModeParts, ",");
166
2
        for (const auto &M : ModeParts)
167
2
          if (M == "none")
168
0
            Modes.clear();
169
2
          else if (M == "all")
170
0
            llvm::copy(XRaySupportedModes, std::back_inserter(Modes));
171
2
          else
172
2
            Modes.push_back(M);
173
2
      }
174
5
175
5
    // Then we want to sort and unique the modes we've collected.
176
5
    llvm::sort(Modes);
177
5
    Modes.erase(std::unique(Modes.begin(), Modes.end()), Modes.end());
178
5
  }
179
30.8k
}
180
181
void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
182
29.5k
                       ArgStringList &CmdArgs, types::ID InputType) const {
183
29.5k
  if (!XRayInstrument)
184
29.5k
    return;
185
6
186
6
  CmdArgs.push_back(XRayInstrumentOption);
187
6
188
6
  if (XRayAlwaysEmitCustomEvents)
189
0
    CmdArgs.push_back("-fxray-always-emit-customevents");
190
6
191
6
  if (XRayAlwaysEmitTypedEvents)
192
0
    CmdArgs.push_back("-fxray-always-emit-typedevents");
193
6
194
6
  CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) +
195
6
                                       Twine(InstructionThreshold)));
196
6
197
6
  for (const auto &Always : AlwaysInstrumentFiles) {
198
0
    SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument=");
199
0
    AlwaysInstrumentOpt += Always;
200
0
    CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));
201
0
  }
202
6
203
6
  for (const auto &Never : NeverInstrumentFiles) {
204
0
    SmallString<64> NeverInstrumentOpt("-fxray-never-instrument=");
205
0
    NeverInstrumentOpt += Never;
206
0
    CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
207
0
  }
208
6
209
6
  for (const auto &AttrFile : AttrListFiles) {
210
0
    SmallString<64> AttrListFileOpt("-fxray-attr-list=");
211
0
    AttrListFileOpt += AttrFile;
212
0
    CmdArgs.push_back(Args.MakeArgString(AttrListFileOpt));
213
0
  }
214
6
215
6
  for (const auto &Dep : ExtraDeps) {
216
0
    SmallString<64> ExtraDepOpt("-fdepfile-entry=");
217
0
    ExtraDepOpt += Dep;
218
0
    CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
219
0
  }
220
6
221
8
  for (const auto &Mode : Modes) {
222
8
    SmallString<64> ModeOpt("-fxray-modes=");
223
8
    ModeOpt += Mode;
224
8
    CmdArgs.push_back(Args.MakeArgString(ModeOpt));
225
8
  }
226
6
227
6
  SmallString<64> Bundle("-fxray-instrumentation-bundle=");
228
6
  if (InstrumentationBundle.full()) {
229
5
    Bundle += "all";
230
5
  } else 
if (1
InstrumentationBundle.empty()1
) {
231
0
    Bundle += "none";
232
1
  } else {
233
1
    if (InstrumentationBundle.has(XRayInstrKind::Function))
234
0
      Bundle += "function";
235
1
    if (InstrumentationBundle.has(XRayInstrKind::Custom))
236
0
      Bundle += "custom";
237
1
    if (InstrumentationBundle.has(XRayInstrKind::Typed))
238
0
      Bundle += "typed";
239
1
  }
240
6
  CmdArgs.push_back(Args.MakeArgString(Bundle));
241
6
}