blob: 6d34617d479588ae5948955f4d1c02fe0ab0d9d2 [file] [log] [blame]
Fangrui Song524b3c12019-03-01 06:49:51 +00001//===-- XRayLists.cpp - XRay automatic-attribution ------------------------===//
Dean Michael Berris835832d2017-03-30 00:29:36 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Dean Michael Berris835832d2017-03-30 00:29:36 +00006//
7//===----------------------------------------------------------------------===//
8//
9// User-provided filters for always/never XRay instrumenting certain functions.
10//
11//===----------------------------------------------------------------------===//
Reid Kleckner04da3df2020-02-26 17:24:52 -080012
Dean Michael Berris835832d2017-03-30 00:29:36 +000013#include "clang/Basic/XRayLists.h"
Reid Klecknere08464f2020-02-29 09:10:42 -080014#include "clang/Basic/FileManager.h"
Reid Kleckner04da3df2020-02-26 17:24:52 -080015#include "clang/Basic/SourceManager.h"
16#include "llvm/Support/SpecialCaseList.h"
Dean Michael Berris835832d2017-03-30 00:29:36 +000017
18using namespace clang;
19
20XRayFunctionFilter::XRayFunctionFilter(
21 ArrayRef<std::string> AlwaysInstrumentPaths,
Dean Michael Berris20dc6ef2018-04-09 04:02:09 +000022 ArrayRef<std::string> NeverInstrumentPaths,
23 ArrayRef<std::string> AttrListPaths, SourceManager &SM)
Ilya Biryukovaa981c12019-11-21 11:32:17 +010024 : AlwaysInstrument(llvm::SpecialCaseList::createOrDie(
25 AlwaysInstrumentPaths, SM.getFileManager().getVirtualFileSystem())),
26 NeverInstrument(llvm::SpecialCaseList::createOrDie(
27 NeverInstrumentPaths, SM.getFileManager().getVirtualFileSystem())),
28 AttrList(llvm::SpecialCaseList::createOrDie(
29 AttrListPaths, SM.getFileManager().getVirtualFileSystem())),
30 SM(SM) {}
Dean Michael Berris835832d2017-03-30 00:29:36 +000031
Reid Kleckner04da3df2020-02-26 17:24:52 -080032XRayFunctionFilter::~XRayFunctionFilter() = default;
33
Dean Michael Berris835832d2017-03-30 00:29:36 +000034XRayFunctionFilter::ImbueAttribute
35XRayFunctionFilter::shouldImbueFunction(StringRef FunctionName) const {
36 // First apply the always instrument list, than if it isn't an "always" see
37 // whether it's treated as a "never" instrument function.
Dean Michael Berris20dc6ef2018-04-09 04:02:09 +000038 // TODO: Remove these as they're deprecated; use the AttrList exclusively.
Vlad Tsyrklevich2eccdab2017-09-25 22:11:12 +000039 if (AlwaysInstrument->inSection("xray_always_instrument", "fun", FunctionName,
Dean Michael Berris20dc6ef2018-04-09 04:02:09 +000040 "arg1") ||
41 AttrList->inSection("always", "fun", FunctionName, "arg1"))
Dean Michael Berris170429e2017-05-24 05:46:36 +000042 return ImbueAttribute::ALWAYS_ARG1;
Vlad Tsyrklevich2eccdab2017-09-25 22:11:12 +000043 if (AlwaysInstrument->inSection("xray_always_instrument", "fun",
Dean Michael Berris20dc6ef2018-04-09 04:02:09 +000044 FunctionName) ||
45 AttrList->inSection("always", "fun", FunctionName))
Dean Michael Berris835832d2017-03-30 00:29:36 +000046 return ImbueAttribute::ALWAYS;
Dean Michael Berris20dc6ef2018-04-09 04:02:09 +000047
48 if (NeverInstrument->inSection("xray_never_instrument", "fun",
49 FunctionName) ||
50 AttrList->inSection("never", "fun", FunctionName))
Dean Michael Berris835832d2017-03-30 00:29:36 +000051 return ImbueAttribute::NEVER;
Dean Michael Berris20dc6ef2018-04-09 04:02:09 +000052
Dean Michael Berris835832d2017-03-30 00:29:36 +000053 return ImbueAttribute::NONE;
54}
55
56XRayFunctionFilter::ImbueAttribute
57XRayFunctionFilter::shouldImbueFunctionsInFile(StringRef Filename,
58 StringRef Category) const {
Vlad Tsyrklevich2eccdab2017-09-25 22:11:12 +000059 if (AlwaysInstrument->inSection("xray_always_instrument", "src", Filename,
Dean Michael Berris20dc6ef2018-04-09 04:02:09 +000060 Category) ||
61 AttrList->inSection("always", "src", Filename, Category))
Dean Michael Berris835832d2017-03-30 00:29:36 +000062 return ImbueAttribute::ALWAYS;
Vlad Tsyrklevich2eccdab2017-09-25 22:11:12 +000063 if (NeverInstrument->inSection("xray_never_instrument", "src", Filename,
Dean Michael Berris20dc6ef2018-04-09 04:02:09 +000064 Category) ||
65 AttrList->inSection("never", "src", Filename, Category))
Dean Michael Berris835832d2017-03-30 00:29:36 +000066 return ImbueAttribute::NEVER;
67 return ImbueAttribute::NONE;
68}
69
70XRayFunctionFilter::ImbueAttribute
71XRayFunctionFilter::shouldImbueLocation(SourceLocation Loc,
72 StringRef Category) const {
73 if (!Loc.isValid())
74 return ImbueAttribute::NONE;
75 return this->shouldImbueFunctionsInFile(SM.getFilename(SM.getFileLoc(Loc)),
76 Category);
77}