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"
19#include "llvm/IR/LLVMContext.h"
21#include "llvm/Support/JSON.h"
23#include "llvm/Support/Path.h"
26
27#include <string>
28
29using namespace llvm;
30
33 auto BufferOrErr = Filename.str() == "-" ? MemoryBuffer::getSTDIN()
34 : FS.getBufferForFile(Filename);
35 if (std::error_code EC = BufferOrErr.getError())
36 return errorCodeToError(EC);
37 return std::move(BufferOrErr.get());
38}
39
40namespace llvm {
41namespace instrumentor {
42
44 LLVMContext &Ctx) {
45 if (OutputFile.empty())
46 return;
47
48 std::error_code EC;
49 raw_fd_stream OS(OutputFile, EC);
50 if (EC) {
52 Twine("failed to open instrumentor configuration file for writing: ") +
53 EC.message(),
54 DS_Warning));
55 return;
56 }
57
58 json::OStream J(OS, 2);
59 J.objectBegin();
60
61 J.attributeBegin("configuration");
62 J.objectBegin();
63 for (auto *BaseCO : IConf.BaseConfigurationOptions) {
64 switch (BaseCO->Kind) {
66 J.attribute(BaseCO->Name, BaseCO->getString());
67 break;
69 J.attribute(BaseCO->Name, BaseCO->getBool());
70 break;
71 }
72 if (!BaseCO->Description.empty())
73 J.attribute(std::string(BaseCO->Name) + ".description",
74 BaseCO->Description);
75 }
76 J.objectEnd();
77 J.attributeEnd();
78
79 for (unsigned KindVal = 0; KindVal <= InstrumentationLocation::Last;
80 ++KindVal) {
81 auto Kind = InstrumentationLocation::KindTy(KindVal);
82
83 auto &KindChoices = IConf.IChoices[Kind];
84 if (KindChoices.empty())
85 continue;
86
88 J.objectBegin();
89 for (auto &ChoiceIt : KindChoices) {
90 J.attributeBegin(ChoiceIt.getKey());
91 J.objectBegin();
92 J.attribute("enabled", ChoiceIt.second->Enabled);
93 J.attribute("filter", ChoiceIt.second->Filter);
94 J.attribute("filter.description",
95 "Static property filter to exclude instrumentation.");
96 for (auto &ArgIt : ChoiceIt.second->IRTArgs) {
97 J.attribute(ArgIt.Name, ArgIt.Enabled);
98 if ((ArgIt.Flags & IRTArg::REPLACABLE) ||
99 (ArgIt.Flags & IRTArg::REPLACABLE_CUSTOM))
100 J.attribute(std::string(ArgIt.Name) + ".replace", true);
101 if (!ArgIt.Description.empty())
102 J.attribute(std::string(ArgIt.Name) + ".description",
103 ArgIt.Description);
104 }
105 J.objectEnd();
106 J.attributeEnd();
107 }
108 J.objectEnd();
109 J.attributeEnd();
110 }
111
112 J.objectEnd();
113}
114
116 LLVMContext &Ctx, vfs::FileSystem &FS) {
117 if (InputFile.empty())
118 return true;
119
120 auto BufferOrErr = setupMemoryBuffer(InputFile, FS);
121 if (Error E = BufferOrErr.takeError()) {
123 Twine("failed to open instrumentor configuration file for reading: ") +
124 toString(std::move(E)),
125 DS_Warning));
126 return false;
127 }
128 auto Buffer = std::move(BufferOrErr.get());
129 json::Path::Root NullRoot;
130 auto Parsed = json::parse(Buffer->getBuffer());
131 if (!Parsed) {
133 Twine("failed to parse instrumentor configuration file: ") +
134 toString(Parsed.takeError()),
135 DS_Warning));
136 return false;
137 }
138 auto *Config = Parsed->getAsObject();
139 if (!Config) {
141 "failed to parse instrumentor configuration file, expected an object "
142 "'{ ... }'",
143 DS_Warning));
144 return false;
145 }
146
148 for (auto *BO : IConf.BaseConfigurationOptions)
149 BCOMap[BO->Name] = BO;
150
152 for (auto &It : *Config) {
153 auto *Obj = It.second.getAsObject();
154 if (!Obj) {
156 "malformed JSON configuration, expected an object", DS_Warning));
157 continue;
158 }
159 if (It.first == "configuration") {
160 for (auto &ObjIt : *Obj) {
161 if (auto *BO = BCOMap.lookup(ObjIt.first)) {
162 switch (BO->Kind) {
164 if (auto V = ObjIt.second.getAsString()) {
165 BO->setString(IConf.SS.save(*V));
166 } else {
168 Twine("configuration key '") + ObjIt.first.str() +
169 Twine("' expects a string, value ignored"),
170 DS_Warning));
171 }
172 break;
174 if (auto V = ObjIt.second.getAsBoolean())
175 BO->setBool(*V);
176 else {
178 Twine("configuration key '") + ObjIt.first.str() +
179 Twine("' expects a boolean, value ignored"),
180 DS_Warning));
181 }
182 break;
183 }
184 } else if (!StringRef(ObjIt.first).ends_with(".description")) {
186 Twine("configuration key '") + ObjIt.first.str() +
187 Twine("' not found and ignored"),
188 DS_Warning));
189 }
190 }
191 continue;
192 }
193
194 auto &IChoiceMap =
196 for (auto &ObjIt : *Obj) {
197 auto *InnerObj = ObjIt.second.getAsObject();
198 if (!InnerObj) {
200 "malformed JSON configuration, expected an object", DS_Warning));
201 continue;
202 }
203 auto *IO = IChoiceMap.lookup(ObjIt.first);
204 if (!IO) {
206 Twine("malformed JSON configuration, expected an object matching "
207 "an instrumentor choice, got ") +
208 ObjIt.first.str(),
209 DS_Warning));
210 continue;
211 }
212 SeenIOs.insert(IO);
214 StringRef FilterStr;
215 for (auto &InnerObjIt : *InnerObj) {
216 auto Name = StringRef(InnerObjIt.first);
217 if (Name == "filter") {
218 if (auto V = InnerObjIt.second.getAsString())
219 FilterStr = IConf.SS.save(*V);
220 } else if (Name.consume_back(".replace")) {
221 ReplaceMap[Name] = InnerObjIt.second.getAsBoolean().value_or(false);
222 } else {
223 ValueMap[Name] = InnerObjIt.second.getAsBoolean().value_or(false);
224 }
225 }
226 IO->Enabled = ValueMap["enabled"];
227 IO->Filter = FilterStr;
228 for (auto &IRArg : IO->IRTArgs) {
229 IRArg.Enabled = ValueMap[IRArg.Name];
230 if (!ReplaceMap.lookup(IRArg.Name)) {
231 IRArg.Flags &= ~IRTArg::REPLACABLE;
232 IRArg.Flags &= ~IRTArg::REPLACABLE_CUSTOM;
233 }
234 }
235 }
236 }
237
238 for (auto &IChoiceMap : IConf.IChoices)
239 for (auto &It : IChoiceMap)
240 if (!SeenIOs.count(It.second))
241 It.second->Enabled = false;
242
243 return true;
244}
245
247 LLVMContext &Ctx, vfs::FileSystem &FS) {
248 if (InputFile.empty())
249 return true;
250
251 auto BufferOrErr = setupMemoryBuffer(InputFile, FS);
252 if (Error E = BufferOrErr.takeError()) {
254 Twine("failed to open instrumentor configuration paths file for "
255 "reading: ") +
256 toString(std::move(E)),
257 DS_Warning));
258 return false;
259 }
260
261 StringRef InputFilePath(sys::path::parent_path(InputFile));
262
263 auto Buffer = std::move(BufferOrErr.get());
264 StringRef Content = Buffer->getBuffer();
265 StringRef EOL = Content.detectEOL();
266 do {
267 auto [LHS, RHS] = Content.split(EOL);
268 std::string ConfigPath = LHS.trim().str();
269 if (!sys::path::is_absolute(ConfigPath)) {
270 SmallString<128> InputFilePathStringVec(InputFilePath);
271 sys::path::append(InputFilePathStringVec, ConfigPath);
272 ConfigPath = InputFilePathStringVec.c_str();
273 }
274 Configs.push_back(ConfigPath);
275 Content = RHS.trim();
276 } while (!Content.empty());
277
278 return true;
279}
280
281} // end namespace instrumentor
282} // end namespace llvm
This file defines the StringMap class.
static constexpr std::pair< StringLiteral, StringLiteral > ReplaceMap[]
This file supports working with JSON data.
static constexpr StringLiteral Filename
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:133
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:260
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:730
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:860
StringRef trim(char Char) const
Return string with consecutive Char characters starting from the left and right removed.
Definition StringRef.h:844
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
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
LLVM_ABI std::string str() const
Return the twine contents as a std::string.
Definition Twine.cpp:17
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.
void writeConfigToJSON(InstrumentationConfig &IConf, StringRef OutputFile, LLVMContext &Ctx)
Write the configuration in /p IConf to the file with path OutputFile.
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.
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:468
LLVM_ABI bool is_absolute(const Twine &path, Style style=Style::native)
Is path absolute?
Definition Path.cpp:678
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:457
This is an optimization pass for GlobalISel generic memory operations.
static Expected< std::unique_ptr< MemoryBuffer > > setupMemoryBuffer(const Twine &Filename, vfs::FileSystem &FS)
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.
SmallVector< BaseConfigurationOption * > BaseConfigurationOptions
The list of enabled base configuration options.
EnumeratedArray< StringMap< InstrumentationOpportunity * >, InstrumentationLocation::KindTy > IChoices
The map registered instrumentation opportunities.
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.