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