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