xref: /llvm-project/clang/lib/Basic/XRayLists.cpp (revision e08464fb450456881733c885267b32dc7339cf11)
1524b3c18SFangrui Song //===-- XRayLists.cpp - XRay automatic-attribution ------------------------===//
2835832d3SDean Michael Berris //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6835832d3SDean Michael Berris //
7835832d3SDean Michael Berris //===----------------------------------------------------------------------===//
8835832d3SDean Michael Berris //
9835832d3SDean Michael Berris // User-provided filters for always/never XRay instrumenting certain functions.
10835832d3SDean Michael Berris //
11835832d3SDean Michael Berris //===----------------------------------------------------------------------===//
1204da3dfeSReid Kleckner 
13835832d3SDean Michael Berris #include "clang/Basic/XRayLists.h"
14*e08464fbSReid Kleckner #include "clang/Basic/FileManager.h"
1504da3dfeSReid Kleckner #include "clang/Basic/SourceManager.h"
1604da3dfeSReid Kleckner #include "llvm/Support/SpecialCaseList.h"
17835832d3SDean Michael Berris 
18835832d3SDean Michael Berris using namespace clang;
19835832d3SDean Michael Berris 
XRayFunctionFilter(ArrayRef<std::string> AlwaysInstrumentPaths,ArrayRef<std::string> NeverInstrumentPaths,ArrayRef<std::string> AttrListPaths,SourceManager & SM)20835832d3SDean Michael Berris XRayFunctionFilter::XRayFunctionFilter(
21835832d3SDean Michael Berris     ArrayRef<std::string> AlwaysInstrumentPaths,
2220dc6ef7SDean Michael Berris     ArrayRef<std::string> NeverInstrumentPaths,
2320dc6ef7SDean Michael Berris     ArrayRef<std::string> AttrListPaths, SourceManager &SM)
24aa981c18SIlya Biryukov     : AlwaysInstrument(llvm::SpecialCaseList::createOrDie(
25aa981c18SIlya Biryukov           AlwaysInstrumentPaths, SM.getFileManager().getVirtualFileSystem())),
26aa981c18SIlya Biryukov       NeverInstrument(llvm::SpecialCaseList::createOrDie(
27aa981c18SIlya Biryukov           NeverInstrumentPaths, SM.getFileManager().getVirtualFileSystem())),
28aa981c18SIlya Biryukov       AttrList(llvm::SpecialCaseList::createOrDie(
29aa981c18SIlya Biryukov           AttrListPaths, SM.getFileManager().getVirtualFileSystem())),
30aa981c18SIlya Biryukov       SM(SM) {}
31835832d3SDean Michael Berris 
3204da3dfeSReid Kleckner XRayFunctionFilter::~XRayFunctionFilter() = default;
3304da3dfeSReid Kleckner 
34835832d3SDean Michael Berris XRayFunctionFilter::ImbueAttribute
shouldImbueFunction(StringRef FunctionName) const35835832d3SDean Michael Berris XRayFunctionFilter::shouldImbueFunction(StringRef FunctionName) const {
36835832d3SDean Michael Berris   // First apply the always instrument list, than if it isn't an "always" see
37835832d3SDean Michael Berris   // whether it's treated as a "never" instrument function.
3820dc6ef7SDean Michael Berris   // TODO: Remove these as they're deprecated; use the AttrList exclusively.
392eccdab3SVlad Tsyrklevich   if (AlwaysInstrument->inSection("xray_always_instrument", "fun", FunctionName,
4020dc6ef7SDean Michael Berris                                   "arg1") ||
4120dc6ef7SDean Michael Berris       AttrList->inSection("always", "fun", FunctionName, "arg1"))
42170429e2SDean Michael Berris     return ImbueAttribute::ALWAYS_ARG1;
432eccdab3SVlad Tsyrklevich   if (AlwaysInstrument->inSection("xray_always_instrument", "fun",
4420dc6ef7SDean Michael Berris                                   FunctionName) ||
4520dc6ef7SDean Michael Berris       AttrList->inSection("always", "fun", FunctionName))
46835832d3SDean Michael Berris     return ImbueAttribute::ALWAYS;
4720dc6ef7SDean Michael Berris 
4820dc6ef7SDean Michael Berris   if (NeverInstrument->inSection("xray_never_instrument", "fun",
4920dc6ef7SDean Michael Berris                                  FunctionName) ||
5020dc6ef7SDean Michael Berris       AttrList->inSection("never", "fun", FunctionName))
51835832d3SDean Michael Berris     return ImbueAttribute::NEVER;
5220dc6ef7SDean Michael Berris 
53835832d3SDean Michael Berris   return ImbueAttribute::NONE;
54835832d3SDean Michael Berris }
55835832d3SDean Michael Berris 
56835832d3SDean Michael Berris XRayFunctionFilter::ImbueAttribute
shouldImbueFunctionsInFile(StringRef Filename,StringRef Category) const57835832d3SDean Michael Berris XRayFunctionFilter::shouldImbueFunctionsInFile(StringRef Filename,
58835832d3SDean Michael Berris                                                StringRef Category) const {
592eccdab3SVlad Tsyrklevich   if (AlwaysInstrument->inSection("xray_always_instrument", "src", Filename,
6020dc6ef7SDean Michael Berris                                   Category) ||
6120dc6ef7SDean Michael Berris       AttrList->inSection("always", "src", Filename, Category))
62835832d3SDean Michael Berris     return ImbueAttribute::ALWAYS;
632eccdab3SVlad Tsyrklevich   if (NeverInstrument->inSection("xray_never_instrument", "src", Filename,
6420dc6ef7SDean Michael Berris                                  Category) ||
6520dc6ef7SDean Michael Berris       AttrList->inSection("never", "src", Filename, Category))
66835832d3SDean Michael Berris     return ImbueAttribute::NEVER;
67835832d3SDean Michael Berris   return ImbueAttribute::NONE;
68835832d3SDean Michael Berris }
69835832d3SDean Michael Berris 
70835832d3SDean Michael Berris XRayFunctionFilter::ImbueAttribute
shouldImbueLocation(SourceLocation Loc,StringRef Category) const71835832d3SDean Michael Berris XRayFunctionFilter::shouldImbueLocation(SourceLocation Loc,
72835832d3SDean Michael Berris                                         StringRef Category) const {
73835832d3SDean Michael Berris   if (!Loc.isValid())
74835832d3SDean Michael Berris     return ImbueAttribute::NONE;
75835832d3SDean Michael Berris   return this->shouldImbueFunctionsInFile(SM.getFilename(SM.getFileLoc(Loc)),
76835832d3SDean Michael Berris                                           Category);
77835832d3SDean Michael Berris }
78