LLVM 23.0.0git
InstrumentorConfigFile.cpp
Go to the documentation of this file.
1//===-- InstrumentorConfigFile.cpp ----------------------------------------===//
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//
9// The implementation of the utilities for the Instrumentor JSON configuration
10// file.
11//
12//===----------------------------------------------------------------------===//
13
15
16#include "llvm/ADT/StringMap.h"
17#include "llvm/ADT/StringRef.h"
18#include "llvm/ADT/StringSet.h"
20#include "llvm/IR/LLVMContext.h"
22#include "llvm/Support/JSON.h"
24#include "llvm/Support/Path.h"
27
28#include <string>
29
30using namespace llvm;
31
34 auto BufferOrErr = Filename.str() == "-" ? MemoryBuffer::getSTDIN()
35 : FS.getBufferForFile(Filename);
36 if (std::error_code EC = BufferOrErr.getError())
37 return errorCodeToError(EC);
38 return std::move(BufferOrErr.get());
39}
40
41namespace llvm {
42namespace instrumentor {
43
45 LLVMContext &Ctx) {
46 if (OutputFile.empty())
47 return;
48
49 std::error_code EC;
50 raw_fd_stream OS(OutputFile, EC);
51 if (EC) {
53 Twine("failed to open instrumentor configuration file for writing: ") +
54 EC.message(),
55 DS_Warning));
56 return;
57 }
58
59 json::OStream J(OS, 2);
60 J.objectBegin();
61
62 J.attributeBegin("configuration");
63 J.objectBegin();
64 for (auto *BaseCO : IConf.BaseConfigurationOptions) {
65 switch (BaseCO->Kind) {
67 J.attribute(BaseCO->Name, BaseCO->getString());
68 break;
70 J.attribute(BaseCO->Name, BaseCO->getBool());
71 break;
72 }
73 if (!BaseCO->Description.empty())
74 J.attribute(std::string(BaseCO->Name) + ".description",
75 BaseCO->Description);
76 }
77 J.objectEnd();
78 J.attributeEnd();
79
80 for (unsigned KindVal = 0; KindVal <= InstrumentationLocation::Last;
81 ++KindVal) {
82 auto Kind = InstrumentationLocation::KindTy(KindVal);
83
84 auto &KindChoices = IConf.IChoices[Kind];
85 if (KindChoices.empty())
86 continue;
87
89 J.objectBegin();
90 for (auto &[Name, Choice] : KindChoices) {
91 J.attributeBegin(Name);
92 J.objectBegin();
93 J.attribute("enabled", Choice->Enabled);
94 J.attribute("filter", Choice->Filter);
95 J.attribute("filter.description",
96 "Static property filter to exclude instrumentation.");
97 for (auto &ArgIt : Choice->IRTArgs) {
98 J.attribute(ArgIt.Name, ArgIt.Enabled);
99 if ((ArgIt.Flags & IRTArg::REPLACABLE) ||
100 (ArgIt.Flags & IRTArg::REPLACABLE_CUSTOM))
101 J.attribute(std::string(ArgIt.Name) + ".replace", true);
102 if (!ArgIt.Description.empty())
103 J.attribute(std::string(ArgIt.Name) + ".description",
104 ArgIt.Description);
105 }
106 J.objectEnd();
107 J.attributeEnd();
108 }
109 J.objectEnd();
110 J.attributeEnd();
111 }
112
113 J.objectEnd();
114}
115
116template <typename Map>
117static StringRef closestOption(const Map &Options, StringRef Missing) {
118 uint32_t MaxEdit = 5;
119 StringRef Closest;
120 for (const auto &Key : Options.keys()) {
121 auto EditDist = Missing.edit_distance_insensitive(Key, true, MaxEdit);
122 if (EditDist < MaxEdit) {
123 Closest = Key;
124 MaxEdit = EditDist;
125 }
126 }
127 return Closest;
128}
129
131 LLVMContext &Ctx, vfs::FileSystem &FS) {
132 if (InputFile.empty())
133 return true;
134
135 auto BufferOrErr = setupMemoryBuffer(InputFile, FS);
136 if (Error E = BufferOrErr.takeError()) {
138 Twine("failed to open instrumentor configuration file for reading: ") +
139 toString(std::move(E)),
140 DS_Warning));
141 return false;
142 }
143 auto Buffer = std::move(BufferOrErr.get());
144 json::Path::Root NullRoot;
145 auto Parsed = json::parse(Buffer->getBuffer());
146 if (!Parsed) {
148 Twine("failed to parse instrumentor configuration file: ") +
149 toString(Parsed.takeError()),
150 DS_Warning));
151 return false;
152 }
153 auto *Config = Parsed->getAsObject();
154 if (!Config) {
156 "failed to parse instrumentor configuration file, expected an object "
157 "'{ ... }'",
158 DS_Warning));
159 return false;
160 }
161
163 for (auto *BO : IConf.BaseConfigurationOptions)
164 BCOMap[BO->Name] = BO;
165
167 for (auto &It : *Config) {
168 auto *Obj = It.second.getAsObject();
169 if (!Obj) {
171 "malformed JSON configuration, expected an object", DS_Warning));
172 continue;
173 }
174 if (It.first == "configuration") {
175 for (auto &ObjIt : *Obj) {
176 if (auto *BO = BCOMap.lookup(ObjIt.first)) {
177 switch (BO->Kind) {
179 if (auto V = ObjIt.second.getAsString()) {
180 BO->setString(IConf.SS.save(*V));
181 } else {
183 Twine("configuration key '") + StringRef(ObjIt.first) +
184 Twine("' expects a string, value ignored"),
185 DS_Warning));
186 }
187 break;
189 if (auto V = ObjIt.second.getAsBoolean())
190 BO->setBool(*V);
191 else {
193 Twine("configuration key '") + StringRef(ObjIt.first) +
194 Twine("' expects a boolean, value ignored"),
195 DS_Warning));
196 }
197 break;
198 }
199 } else if (!StringRef(ObjIt.first).ends_with(".description")) {
200 std::string Diag = "configuration key '" + ObjIt.first.str() +
201 "' not found and ignored";
202 StringRef Closest = closestOption(BCOMap, ObjIt.first);
203 if (!Closest.empty())
204 Diag += "; did you mean '" + Closest.str() + "'?";
205 Ctx.diagnose(DiagnosticInfoInstrumentation(Diag, DS_Warning));
206 }
207 }
208 continue;
209 }
210
211 auto &IChoiceMap =
213 for (auto &ObjIt : *Obj) {
214 auto *InnerObj = ObjIt.second.getAsObject();
215 if (!InnerObj) {
217 "malformed JSON configuration, expected an object", DS_Warning));
218 continue;
219 }
220 auto *IO = IChoiceMap.lookup(ObjIt.first);
221 if (!IO) {
222 std::string Diag =
223 "malformed JSON configuration, expected an object matching "
224 "an instrumentor choice, got '" +
225 ObjIt.first.str() + "'";
226 StringRef Closest = closestOption(IChoiceMap, ObjIt.first);
227 if (!Closest.empty())
228 Diag += "; did you mean '" + Closest.str() + "'?";
229 Ctx.diagnose(DiagnosticInfoInstrumentation(Diag, DS_Warning));
230 continue;
231 }
232 SeenIOs.insert(IO);
234 StringRef FilterStr;
235 StringSet<> IOOpts;
236 IOOpts.insert("enabled");
237 IOOpts.insert("filter");
238 for (auto &IRArg : IO->IRTArgs)
239 IOOpts.insert(IRArg.Name);
240 for (auto &InnerObjIt : *InnerObj) {
241 auto Name = StringRef(InnerObjIt.first);
242 if (Name == "filter") {
243 if (auto V = InnerObjIt.second.getAsString())
244 FilterStr = IConf.SS.save(*V);
245 } else if (Name.consume_back(".replace")) {
246 ReplaceMap[Name] = InnerObjIt.second.getAsBoolean().value_or(false);
247 } else {
248 ValueMap[Name] = InnerObjIt.second.getAsBoolean().value_or(false);
249 }
250 if (!IOOpts.contains(Name)) {
251 std::string Diag = "unrecognized JSON property '" + Name.str() +
252 "' in configuration for '" + IO->getName().str() +
253 "'";
254 StringRef Closest = closestOption(IOOpts, Name);
255 if (!Closest.empty())
256 Diag += "; did you mean '" + Closest.str() + "'?";
257 Ctx.diagnose(DiagnosticInfoInstrumentation(Diag, DS_Warning));
258 }
259 }
260 IO->Enabled = ValueMap["enabled"];
261 IO->Filter = FilterStr;
262 for (auto &IRArg : IO->IRTArgs) {
263 IRArg.Enabled = ValueMap[IRArg.Name];
264 if (!ReplaceMap.lookup(IRArg.Name)) {
265 IRArg.Flags &= ~IRTArg::REPLACABLE;
266 IRArg.Flags &= ~IRTArg::REPLACABLE_CUSTOM;
267 }
268 }
269 }
270 }
271
272 for (auto &IChoiceMap : IConf.IChoices)
273 for (auto &It : IChoiceMap)
274 if (!SeenIOs.count(It.second))
275 It.second->Enabled = false;
276
277 return true;
278}
279
281 LLVMContext &Ctx, vfs::FileSystem &FS) {
282 if (InputFile.empty())
283 return true;
284
285 auto BufferOrErr = setupMemoryBuffer(InputFile, FS);
286 if (Error E = BufferOrErr.takeError()) {
288 Twine("failed to open instrumentor configuration paths file for "
289 "reading: ") +
290 toString(std::move(E)),
291 DS_Warning));
292 return false;
293 }
294
295 StringRef InputFilePath(sys::path::parent_path(InputFile));
296
297 auto Buffer = std::move(BufferOrErr.get());
298 StringRef Content = Buffer->getBuffer();
299 StringRef EOL = Content.detectEOL();
300 do {
301 auto [LHS, RHS] = Content.split(EOL);
302 std::string ConfigPath = LHS.trim().str();
303 if (!sys::path::is_absolute(ConfigPath)) {
304 SmallString<128> InputFilePathStringVec(InputFilePath);
305 sys::path::append(InputFilePathStringVec, ConfigPath);
306 ConfigPath = InputFilePathStringVec.c_str();
307 }
308 Configs.push_back(ConfigPath);
309 Content = RHS.trim();
310 } while (!Content.empty());
311
312 return true;
313}
314
315} // end namespace instrumentor
316} // end namespace llvm
This file defines the StringMap class.
static constexpr std::pair< StringLiteral, StringLiteral > ReplaceMap[]
This file supports working with JSON data.
static LVOptions Options
Definition LVOptions.cpp:25
static constexpr StringLiteral Filename
StringSet - A set-like wrapper for the StringMap.
Defines the virtual file system interface vfs::FileSystem.
Diagnostic information for IR instrumentation reporting.
Lightweight error class with error context and mandatory checking.
Definition Error.h:159
Tagged union holding either a T or a Error.
Definition Error.h:485
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
static ErrorOr< std::unique_ptr< MemoryBuffer > > getSTDIN()
Read all of stdin into a file buffer, and return it.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
const char * c_str()
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:128
ValueTy lookup(StringRef Key) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition StringMap.h:249
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:736
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
StringRef detectEOL() const
Detect the line ending style of the string.
Definition StringRef.h:866
StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
Definition StringRef.h:850
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition StringRef.h:270
StringRef save(const char *S)
Definition StringSaver.h:31
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition StringSet.h:25
bool contains(StringRef key) const
Check if the set contains the given key.
Definition StringSet.h:60
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition StringSet.h:39
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
See the file comment.
Definition ValueMap.h:84
void push_back(const DataType &value)
json::OStream allows writing well-formed JSON without materializing all structures as json::Value ahe...
Definition JSON.h:982
LLVM_ABI void attributeBegin(llvm::StringRef Key)
Definition JSON.cpp:883
void attribute(llvm::StringRef Key, const Value &Contents)
Emit an attribute whose value is self-contained (number, vector<int> etc).
Definition JSON.h:1037
LLVM_ABI void objectBegin()
Definition JSON.cpp:864
LLVM_ABI void attributeEnd()
Definition JSON.cpp:903
LLVM_ABI void objectEnd()
Definition JSON.cpp:872
The root is the trivial Path to the root value.
Definition JSON.h:699
A raw_ostream of a file for reading/writing/seeking.
The virtual file system interface.
LLVM_ABI void writeConfigToJSON(InstrumentationConfig &IConf, StringRef OutputFile, LLVMContext &Ctx)
Write the configuration in /p IConf to the file with path OutputFile.
static StringRef closestOption(const Map &Options, StringRef Missing)
LLVM_ABI bool readConfigPathsFile(StringRef InputFile, cl::list< std::string > &Configs, LLVMContext &Ctx, vfs::FileSystem &FS)
Read the configuration paths from the file with path InputFile into Configs.
LLVM_ABI bool readConfigFromJSON(InstrumentationConfig &IConf, StringRef InputFile, LLVMContext &Ctx, vfs::FileSystem &FS)
Read the configuration from the file with path InputFile into /p IConf.
LLVM_ABI llvm::Expected< Value > parse(llvm::StringRef JSON)
Parses the provided JSON source, or returns a ParseError.
Definition JSON.cpp:681
LLVM_ABI StringRef parent_path(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Get parent path.
Definition Path.cpp:478
LLVM_ABI bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition Path.cpp:688
LLVM_ABI void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
Definition Path.cpp:467
This is an optimization pass for GlobalISel generic memory operations.
static Expected< std::unique_ptr< MemoryBuffer > > setupMemoryBuffer(const Twine &Filename, vfs::FileSystem &FS)
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
LLVM_ABI Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition Error.cpp:107
unsigned Flags
The flags that describe the properties of the argument.
The class that contains the configuration for the instrumentor.
EnumeratedArray< MapVector< StringRef, InstrumentationOpportunity * >, InstrumentationLocation::KindTy > IChoices
The map registered instrumentation opportunities.
SmallVector< BaseConfigurationOption * > BaseConfigurationOptions
The list of enabled base configuration options.
static KindTy getKindFromStr(StringRef S)
Return the location kind described by a string.
static StringRef getKindStr(KindTy Kind)
Return the string representation given a location kind.
KindTy
The supported location kinds, which are composed of a opportunity type and position.