10b57cec5SDimitry Andric //===-- XRayLists.cpp - XRay automatic-attribution ------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // User-provided filters for always/never XRay instrumenting certain functions.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
12*5ffd83dbSDimitry Andric
130b57cec5SDimitry Andric #include "clang/Basic/XRayLists.h"
14*5ffd83dbSDimitry Andric #include "clang/Basic/FileManager.h"
15*5ffd83dbSDimitry Andric #include "clang/Basic/SourceManager.h"
16*5ffd83dbSDimitry Andric #include "llvm/Support/SpecialCaseList.h"
170b57cec5SDimitry Andric
180b57cec5SDimitry Andric using namespace clang;
190b57cec5SDimitry Andric
XRayFunctionFilter(ArrayRef<std::string> AlwaysInstrumentPaths,ArrayRef<std::string> NeverInstrumentPaths,ArrayRef<std::string> AttrListPaths,SourceManager & SM)200b57cec5SDimitry Andric XRayFunctionFilter::XRayFunctionFilter(
210b57cec5SDimitry Andric ArrayRef<std::string> AlwaysInstrumentPaths,
220b57cec5SDimitry Andric ArrayRef<std::string> NeverInstrumentPaths,
230b57cec5SDimitry Andric ArrayRef<std::string> AttrListPaths, SourceManager &SM)
24480093f4SDimitry Andric : AlwaysInstrument(llvm::SpecialCaseList::createOrDie(
25480093f4SDimitry Andric AlwaysInstrumentPaths, SM.getFileManager().getVirtualFileSystem())),
26480093f4SDimitry Andric NeverInstrument(llvm::SpecialCaseList::createOrDie(
27480093f4SDimitry Andric NeverInstrumentPaths, SM.getFileManager().getVirtualFileSystem())),
28480093f4SDimitry Andric AttrList(llvm::SpecialCaseList::createOrDie(
29480093f4SDimitry Andric AttrListPaths, SM.getFileManager().getVirtualFileSystem())),
30480093f4SDimitry Andric SM(SM) {}
310b57cec5SDimitry Andric
32*5ffd83dbSDimitry Andric XRayFunctionFilter::~XRayFunctionFilter() = default;
33*5ffd83dbSDimitry Andric
340b57cec5SDimitry Andric XRayFunctionFilter::ImbueAttribute
shouldImbueFunction(StringRef FunctionName) const350b57cec5SDimitry Andric XRayFunctionFilter::shouldImbueFunction(StringRef FunctionName) const {
360b57cec5SDimitry Andric // First apply the always instrument list, than if it isn't an "always" see
370b57cec5SDimitry Andric // whether it's treated as a "never" instrument function.
380b57cec5SDimitry Andric // TODO: Remove these as they're deprecated; use the AttrList exclusively.
390b57cec5SDimitry Andric if (AlwaysInstrument->inSection("xray_always_instrument", "fun", FunctionName,
400b57cec5SDimitry Andric "arg1") ||
410b57cec5SDimitry Andric AttrList->inSection("always", "fun", FunctionName, "arg1"))
420b57cec5SDimitry Andric return ImbueAttribute::ALWAYS_ARG1;
430b57cec5SDimitry Andric if (AlwaysInstrument->inSection("xray_always_instrument", "fun",
440b57cec5SDimitry Andric FunctionName) ||
450b57cec5SDimitry Andric AttrList->inSection("always", "fun", FunctionName))
460b57cec5SDimitry Andric return ImbueAttribute::ALWAYS;
470b57cec5SDimitry Andric
480b57cec5SDimitry Andric if (NeverInstrument->inSection("xray_never_instrument", "fun",
490b57cec5SDimitry Andric FunctionName) ||
500b57cec5SDimitry Andric AttrList->inSection("never", "fun", FunctionName))
510b57cec5SDimitry Andric return ImbueAttribute::NEVER;
520b57cec5SDimitry Andric
530b57cec5SDimitry Andric return ImbueAttribute::NONE;
540b57cec5SDimitry Andric }
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric XRayFunctionFilter::ImbueAttribute
shouldImbueFunctionsInFile(StringRef Filename,StringRef Category) const570b57cec5SDimitry Andric XRayFunctionFilter::shouldImbueFunctionsInFile(StringRef Filename,
580b57cec5SDimitry Andric StringRef Category) const {
590b57cec5SDimitry Andric if (AlwaysInstrument->inSection("xray_always_instrument", "src", Filename,
600b57cec5SDimitry Andric Category) ||
610b57cec5SDimitry Andric AttrList->inSection("always", "src", Filename, Category))
620b57cec5SDimitry Andric return ImbueAttribute::ALWAYS;
630b57cec5SDimitry Andric if (NeverInstrument->inSection("xray_never_instrument", "src", Filename,
640b57cec5SDimitry Andric Category) ||
650b57cec5SDimitry Andric AttrList->inSection("never", "src", Filename, Category))
660b57cec5SDimitry Andric return ImbueAttribute::NEVER;
670b57cec5SDimitry Andric return ImbueAttribute::NONE;
680b57cec5SDimitry Andric }
690b57cec5SDimitry Andric
700b57cec5SDimitry Andric XRayFunctionFilter::ImbueAttribute
shouldImbueLocation(SourceLocation Loc,StringRef Category) const710b57cec5SDimitry Andric XRayFunctionFilter::shouldImbueLocation(SourceLocation Loc,
720b57cec5SDimitry Andric StringRef Category) const {
730b57cec5SDimitry Andric if (!Loc.isValid())
740b57cec5SDimitry Andric return ImbueAttribute::NONE;
750b57cec5SDimitry Andric return this->shouldImbueFunctionsInFile(SM.getFilename(SM.getFileLoc(Loc)),
760b57cec5SDimitry Andric Category);
770b57cec5SDimitry Andric }
78