blob: 0e0fe6ffa41c670476af71eeb7e3bf0278cb360b [file] [log] [blame]
Dean Michael Berris835832d2017-03-30 00:29:36 +00001//===--- XRayArgs.cpp - Arguments for XRay --------------------------------===//
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#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/SpecialCaseList.h"
Dean Michael Berris00ad3d52017-03-30 01:05:09 +000020#include "llvm/Support/ScopedPrinter.h"
Dean Michael Berris835832d2017-03-30 00:29:36 +000021
22using namespace clang;
23using namespace clang::driver;
24using namespace llvm::opt;
25
26namespace {
27constexpr char XRayInstrumentOption[] = "-fxray-instrument";
28constexpr char XRayInstructionThresholdOption[] =
29 "-fxray-instruction-threshold=";
30constexpr char XRayAlwaysInstrumentOption[] = "-fxray-always-instrument=";
31constexpr char XRayNeverInstrumentOption[] = "-fxray-never-instrument=";
32} // namespace
33
34XRayArgs::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:
49 break;
50 default:
51 D.Diag(diag::err_drv_clang_unsupported)
52 << (std::string(XRayInstrumentOption) + " on " + Triple.str());
53 }
54 else
55 D.Diag(diag::err_drv_clang_unsupported)
56 << (std::string(XRayInstrumentOption) + " on non-Linux target OS");
57 XRayInstrument = true;
58 if (const Arg *A =
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;
64 }
65
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);
73 } else
74 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
75 }
76
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);
82 } else
83 D.Diag(clang::diag::err_drv_no_such_file) << Filename;
84 }
85 }
86}
87
88void XRayArgs::addArgs(const ToolChain &TC, const ArgList &Args,
89 ArgStringList &CmdArgs, types::ID InputType) const {
90 if (!XRayInstrument)
91 return;
92
93 CmdArgs.push_back(XRayInstrumentOption);
94 CmdArgs.push_back(Args.MakeArgString(XRayInstructionThresholdOption +
Dean Michael Berris00ad3d52017-03-30 01:05:09 +000095 llvm::to_string(InstructionThreshold)));
Dean Michael Berris835832d2017-03-30 00:29:36 +000096
97 for (const auto &Always : AlwaysInstrumentFiles) {
98 SmallString<64> AlwaysInstrumentOpt(XRayAlwaysInstrumentOption);
99 AlwaysInstrumentOpt += Always;
100 CmdArgs.push_back(Args.MakeArgString(AlwaysInstrumentOpt));
101 }
102
103 for (const auto &Never : NeverInstrumentFiles) {
104 SmallString<64> NeverInstrumentOpt(XRayNeverInstrumentOption);
105 NeverInstrumentOpt += Never;
106 CmdArgs.push_back(Args.MakeArgString(NeverInstrumentOpt));
107 }
108
109 for (const auto &Dep : ExtraDeps) {
110 SmallString<64> ExtraDepOpt("-fdepfile-entry=");
111 ExtraDepOpt += Dep;
112 CmdArgs.push_back(Args.MakeArgString(ExtraDepOpt));
113 }
114}