1e5dd7070Spatrick //===-- XRayLists.cpp - XRay automatic-attribution ------------------------===//
2e5dd7070Spatrick //
3e5dd7070Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e5dd7070Spatrick // See https://llvm.org/LICENSE.txt for license information.
5e5dd7070Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6e5dd7070Spatrick //
7e5dd7070Spatrick //===----------------------------------------------------------------------===//
8e5dd7070Spatrick //
9e5dd7070Spatrick // User-provided filters for always/never XRay instrumenting certain functions.
10e5dd7070Spatrick //
11e5dd7070Spatrick //===----------------------------------------------------------------------===//
12*ec727ea7Spatrick
13e5dd7070Spatrick #include "clang/Basic/XRayLists.h"
14*ec727ea7Spatrick #include "clang/Basic/FileManager.h"
15*ec727ea7Spatrick #include "clang/Basic/SourceManager.h"
16*ec727ea7Spatrick #include "llvm/Support/SpecialCaseList.h"
17e5dd7070Spatrick
18e5dd7070Spatrick using namespace clang;
19e5dd7070Spatrick
XRayFunctionFilter(ArrayRef<std::string> AlwaysInstrumentPaths,ArrayRef<std::string> NeverInstrumentPaths,ArrayRef<std::string> AttrListPaths,SourceManager & SM)20e5dd7070Spatrick XRayFunctionFilter::XRayFunctionFilter(
21e5dd7070Spatrick ArrayRef<std::string> AlwaysInstrumentPaths,
22e5dd7070Spatrick ArrayRef<std::string> NeverInstrumentPaths,
23e5dd7070Spatrick ArrayRef<std::string> AttrListPaths, SourceManager &SM)
24e5dd7070Spatrick : AlwaysInstrument(llvm::SpecialCaseList::createOrDie(
25e5dd7070Spatrick AlwaysInstrumentPaths, SM.getFileManager().getVirtualFileSystem())),
26e5dd7070Spatrick NeverInstrument(llvm::SpecialCaseList::createOrDie(
27e5dd7070Spatrick NeverInstrumentPaths, SM.getFileManager().getVirtualFileSystem())),
28e5dd7070Spatrick AttrList(llvm::SpecialCaseList::createOrDie(
29e5dd7070Spatrick AttrListPaths, SM.getFileManager().getVirtualFileSystem())),
30e5dd7070Spatrick SM(SM) {}
31e5dd7070Spatrick
32*ec727ea7Spatrick XRayFunctionFilter::~XRayFunctionFilter() = default;
33*ec727ea7Spatrick
34e5dd7070Spatrick XRayFunctionFilter::ImbueAttribute
shouldImbueFunction(StringRef FunctionName) const35e5dd7070Spatrick XRayFunctionFilter::shouldImbueFunction(StringRef FunctionName) const {
36e5dd7070Spatrick // First apply the always instrument list, than if it isn't an "always" see
37e5dd7070Spatrick // whether it's treated as a "never" instrument function.
38e5dd7070Spatrick // TODO: Remove these as they're deprecated; use the AttrList exclusively.
39e5dd7070Spatrick if (AlwaysInstrument->inSection("xray_always_instrument", "fun", FunctionName,
40e5dd7070Spatrick "arg1") ||
41e5dd7070Spatrick AttrList->inSection("always", "fun", FunctionName, "arg1"))
42e5dd7070Spatrick return ImbueAttribute::ALWAYS_ARG1;
43e5dd7070Spatrick if (AlwaysInstrument->inSection("xray_always_instrument", "fun",
44e5dd7070Spatrick FunctionName) ||
45e5dd7070Spatrick AttrList->inSection("always", "fun", FunctionName))
46e5dd7070Spatrick return ImbueAttribute::ALWAYS;
47e5dd7070Spatrick
48e5dd7070Spatrick if (NeverInstrument->inSection("xray_never_instrument", "fun",
49e5dd7070Spatrick FunctionName) ||
50e5dd7070Spatrick AttrList->inSection("never", "fun", FunctionName))
51e5dd7070Spatrick return ImbueAttribute::NEVER;
52e5dd7070Spatrick
53e5dd7070Spatrick return ImbueAttribute::NONE;
54e5dd7070Spatrick }
55e5dd7070Spatrick
56e5dd7070Spatrick XRayFunctionFilter::ImbueAttribute
shouldImbueFunctionsInFile(StringRef Filename,StringRef Category) const57e5dd7070Spatrick XRayFunctionFilter::shouldImbueFunctionsInFile(StringRef Filename,
58e5dd7070Spatrick StringRef Category) const {
59e5dd7070Spatrick if (AlwaysInstrument->inSection("xray_always_instrument", "src", Filename,
60e5dd7070Spatrick Category) ||
61e5dd7070Spatrick AttrList->inSection("always", "src", Filename, Category))
62e5dd7070Spatrick return ImbueAttribute::ALWAYS;
63e5dd7070Spatrick if (NeverInstrument->inSection("xray_never_instrument", "src", Filename,
64e5dd7070Spatrick Category) ||
65e5dd7070Spatrick AttrList->inSection("never", "src", Filename, Category))
66e5dd7070Spatrick return ImbueAttribute::NEVER;
67e5dd7070Spatrick return ImbueAttribute::NONE;
68e5dd7070Spatrick }
69e5dd7070Spatrick
70e5dd7070Spatrick XRayFunctionFilter::ImbueAttribute
shouldImbueLocation(SourceLocation Loc,StringRef Category) const71e5dd7070Spatrick XRayFunctionFilter::shouldImbueLocation(SourceLocation Loc,
72e5dd7070Spatrick StringRef Category) const {
73e5dd7070Spatrick if (!Loc.isValid())
74e5dd7070Spatrick return ImbueAttribute::NONE;
75e5dd7070Spatrick return this->shouldImbueFunctionsInFile(SM.getFilename(SM.getFileLoc(Loc)),
76e5dd7070Spatrick Category);
77e5dd7070Spatrick }
78