1 //===--- XRayArgs.cpp - Arguments for XRay --------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
9 #include "clang/Driver/XRayArgs.h"
10 #include "ToolChains/CommonArgs.h"
11 #include "clang/Driver/Driver.h"
12 #include "clang/Driver/DriverDiagnostic.h"
13 #include "clang/Driver/Options.h"
14 #include "clang/Driver/ToolChain.h"
15 #include "llvm/ADT/StringExtras.h"
16 #include "llvm/ADT/StringSwitch.h"
17 #include "llvm/Support/FileSystem.h"
18 #include "llvm/Support/Path.h"
19 #include "llvm/Support/ScopedPrinter.h"
20 #include "llvm/Support/SpecialCaseList.h"
22 using namespace clang;
23 using namespace clang::driver;
24 using namespace llvm::opt;
27 constexpr char XRayInstrumentOption[] = "-fxray-instrument";
28 constexpr char XRayInstructionThresholdOption[] =
29 "-fxray-instruction-threshold=";
32 XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
33 const Driver &D = TC.getDriver();
34 const llvm::Triple &Triple = TC.getTriple();
35 if (Args.hasFlag(options::OPT_fxray_instrument,
36 options::OPT_fnoxray_instrument, false)) {
37 if (Triple.getOS() == llvm::Triple::Linux)
38 switch (Triple.getArch()) {
39 case llvm::Triple::x86_64:
40 case llvm::Triple::arm:
41 case llvm::Triple::aarch64:
42 case llvm::Triple::ppc64le:
43 case llvm::Triple::mips:
44 case llvm::Triple::mipsel:
45 case llvm::Triple::mips64:
46 case llvm::Triple::mips64el:
49 D.Diag(diag::err_drv_clang_unsupported)
50 << (std::string(XRayInstrumentOption) + " on " + Triple.str());
53 D.Diag(diag::err_drv_clang_unsupported)
54 << (std::string(XRayInstrumentOption) + " on non-Linux target OS");
55 XRayInstrument = true;
57 Args.getLastArg(options::OPT_fxray_instruction_threshold_,
58 options::OPT_fxray_instruction_threshold_EQ)) {
59 StringRef S = A->getValue();
60 if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0)
61 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
64 // By default, the back-end will not emit the lowering for XRay customevent
65 // calls if the function is not instrumented. In the future we will change
66 // this default to be the reverse, but in the meantime we're going to
67 // introduce the new functionality behind a flag.
68 if (Args.hasFlag(options::OPT_fxray_always_emit_customevents,
69 options::OPT_fnoxray_always_emit_customevents, false))
70 XRayAlwaysEmitCustomEvents = true;
72 // Validate the always/never attribute files. We also make sure that they
73 // are treated as actual dependencies.
74 for (const auto &Filename :
75 Args.getAllArgValues(options::OPT_fxray_always_instrument)) {
76 if (llvm::sys::fs::exists(Filename)) {
77 AlwaysInstrumentFiles.push_back(Filename);
78 ExtraDeps.push_back(Filename);
80 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
83 for (const auto &Filename :
84 Args.getAllArgValues(options::OPT_fxray_never_instrument)) {
85 if (llvm::sys::fs::exists(Filename)) {
86 NeverInstrumentFiles.push_back(Filename);
87 ExtraDeps.push_back(Filename);
89 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
94 void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
95 ArgStringList &CmdArgs, types::ID InputType) const {
99 CmdArgs.push_back(XRayInstrumentOption);
101 if (XRayAlwaysEmitCustomEvents)
102 CmdArgs.push_back("-fxray-always-emit-customevents");
104 CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) +
105 Twine(InstructionThreshold)));
107 for (const auto &Always : AlwaysInstrumentFiles) {
108 SmallString<64> AlwaysInstrumentOpt("-fxray-always-instrument=");
109 AlwaysInstrumentOpt += Always;
110 CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));
113 for (const auto &Never : NeverInstrumentFiles) {
114 SmallString<64> NeverInstrumentOpt("-fxray-never-instrument=");
115 NeverInstrumentOpt += Never;
116 CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
119 for (const auto &Dep : ExtraDeps) {
120 SmallString<64> ExtraDepOpt("-fdepfile-entry=");
122 CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));