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/FileManager.h"
15#include "clang/Basic/SourceManager.h"
16#include "llvm/Support/SpecialCaseList.h"
17
18using namespace clang;
19
20XRayFunctionFilter::XRayFunctionFilter(
21 ArrayRef<std::string> AlwaysInstrumentPaths,
22 ArrayRef<std::string> NeverInstrumentPaths,
23 ArrayRef<std::string> AttrListPaths, SourceManager &SM)
24 : AlwaysInstrument(llvm::SpecialCaseList::createOrDie(
25 Paths: AlwaysInstrumentPaths, FS&: SM.getFileManager().getVirtualFileSystem())),
26 NeverInstrument(llvm::SpecialCaseList::createOrDie(
27 Paths: NeverInstrumentPaths, FS&: SM.getFileManager().getVirtualFileSystem())),
28 AttrList(llvm::SpecialCaseList::createOrDie(
29 Paths: AttrListPaths, FS&: SM.getFileManager().getVirtualFileSystem())),
30 SM(SM) {}
31
32XRayFunctionFilter::~XRayFunctionFilter() = default;
33
34XRayFunctionFilter::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.
38 // TODO: Remove these as they're deprecated; use the AttrList exclusively.
39 if (AlwaysInstrument->inSection(Section: "xray_always_instrument", Prefix: "fun", Query: FunctionName,
40 Category: "arg1") ||
41 AttrList->inSection(Section: "always", Prefix: "fun", Query: FunctionName, Category: "arg1"))
42 return ImbueAttribute::ALWAYS_ARG1;
43 if (AlwaysInstrument->inSection(Section: "xray_always_instrument", Prefix: "fun",
44 Query: FunctionName) ||
45 AttrList->inSection(Section: "always", Prefix: "fun", Query: FunctionName))
46 return ImbueAttribute::ALWAYS;
47
48 if (NeverInstrument->inSection(Section: "xray_never_instrument", Prefix: "fun",
49 Query: FunctionName) ||
50 AttrList->inSection(Section: "never", Prefix: "fun", Query: FunctionName))
51 return ImbueAttribute::NEVER;
52
53 return ImbueAttribute::NONE;
54}
55
56XRayFunctionFilter::ImbueAttribute
57XRayFunctionFilter::shouldImbueFunctionsInFile(StringRef Filename,
58 StringRef Category) const {
59 if (AlwaysInstrument->inSection(Section: "xray_always_instrument", Prefix: "src", Query: Filename,
60 Category) ||
61 AttrList->inSection(Section: "always", Prefix: "src", Query: Filename, Category))
62 return ImbueAttribute::ALWAYS;
63 if (NeverInstrument->inSection(Section: "xray_never_instrument", Prefix: "src", Query: Filename,
64 Category) ||
65 AttrList->inSection(Section: "never", Prefix: "src", Query: Filename, Category))
66 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(Filename: SM.getFilename(SpellingLoc: SM.getFileLoc(Loc)),
76 Category);
77}
78

source code of clang/lib/Basic/XRayLists.cpp