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=";
30 constexpr char XRayAlwaysInstrumentOption[] = "-fxray-always-instrument=";
31 constexpr char XRayNeverInstrumentOption[] = "-fxray-never-instrument=";
34 XRayArgs::XRayArgs(const ToolChain &TC, const ArgList &Args) {
35 const Driver &D = TC.getDriver();
36 const llvm::Triple &Triple = TC.getTriple();
37 if (Args.hasFlag(options::OPT_fxray_instrument,
38 options::OPT_fnoxray_instrument, false)) {
39 if (Triple.getOS() == llvm::Triple::Linux)
40 switch (Triple.getArch()) {
41 case llvm::Triple::x86_64:
42 case llvm::Triple::arm:
43 case llvm::Triple::aarch64:
44 case llvm::Triple::ppc64le:
45 case llvm::Triple::mips:
46 case llvm::Triple::mipsel:
47 case llvm::Triple::mips64:
48 case llvm::Triple::mips64el:
51 D.Diag(diag::err_drv_clang_unsupported)
52 << (std::string(XRayInstrumentOption) + " on " + Triple.str());
55 D.Diag(diag::err_drv_clang_unsupported)
56 << (std::string(XRayInstrumentOption) + " on non-Linux target OS");
57 XRayInstrument = true;
59 Args.getLastArg(options::OPT_fxray_instruction_threshold_,
60 options::OPT_fxray_instruction_threshold_EQ)) {
61 StringRef S = A->getValue();
62 if (S.getAsInteger(0, InstructionThreshold) || InstructionThreshold < 0)
63 D.Diag(clang::diag::err_drv_invalid_value) << A->getAsString(Args) << S;
66 // Validate the always/never attribute files. We also make sure that they
67 // are treated as actual dependencies.
68 for (const auto &Filename :
69 Args.getAllArgValues(options::OPT_fxray_always_instrument)) {
70 if (llvm::sys::fs::exists(Filename)) {
71 AlwaysInstrumentFiles.push_back(Filename);
72 ExtraDeps.push_back(Filename);
74 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
77 for (const auto &Filename :
78 Args.getAllArgValues(options::OPT_fxray_never_instrument)) {
79 if (llvm::sys::fs::exists(Filename)) {
80 NeverInstrumentFiles.push_back(Filename);
81 ExtraDeps.push_back(Filename);
83 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
88 void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
89 ArgStringList &CmdArgs, types::ID InputType) const {
93 CmdArgs.push_back(XRayInstrumentOption);
94 CmdArgs.push_back(Args.MakeArgString(Twine(XRayInstructionThresholdOption) +
95 Twine(InstructionThreshold)));
97 for (const auto &Always : AlwaysInstrumentFiles) {
98 SmallString<64> AlwaysInstrumentOpt(XRayAlwaysInstrumentOption);
99 AlwaysInstrumentOpt += Always;
100 CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));
103 for (const auto &Never : NeverInstrumentFiles) {
104 SmallString<64> NeverInstrumentOpt(XRayNeverInstrumentOption);
105 NeverInstrumentOpt += Never;
106 CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
109 for (const auto &Dep : ExtraDeps) {
110 SmallString<64> ExtraDepOpt("-fdepfile-entry=");
112 CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));