xref: /netbsd-src/external/apache2/llvm/dist/clang/lib/Basic/XRayLists.cpp (revision e038c9c4676b0f19b1b7dd08a940c6ed64a6d5ae)
17330f729Sjoerg //===-- XRayLists.cpp - XRay automatic-attribution ------------------------===//
27330f729Sjoerg //
37330f729Sjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47330f729Sjoerg // See https://llvm.org/LICENSE.txt for license information.
57330f729Sjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67330f729Sjoerg //
77330f729Sjoerg //===----------------------------------------------------------------------===//
87330f729Sjoerg //
97330f729Sjoerg // User-provided filters for always/never XRay instrumenting certain functions.
107330f729Sjoerg //
117330f729Sjoerg //===----------------------------------------------------------------------===//
12*e038c9c4Sjoerg 
137330f729Sjoerg #include "clang/Basic/XRayLists.h"
14*e038c9c4Sjoerg #include "clang/Basic/FileManager.h"
15*e038c9c4Sjoerg #include "clang/Basic/SourceManager.h"
16*e038c9c4Sjoerg #include "llvm/Support/SpecialCaseList.h"
177330f729Sjoerg 
187330f729Sjoerg using namespace clang;
197330f729Sjoerg 
XRayFunctionFilter(ArrayRef<std::string> AlwaysInstrumentPaths,ArrayRef<std::string> NeverInstrumentPaths,ArrayRef<std::string> AttrListPaths,SourceManager & SM)207330f729Sjoerg XRayFunctionFilter::XRayFunctionFilter(
217330f729Sjoerg     ArrayRef<std::string> AlwaysInstrumentPaths,
227330f729Sjoerg     ArrayRef<std::string> NeverInstrumentPaths,
237330f729Sjoerg     ArrayRef<std::string> AttrListPaths, SourceManager &SM)
24*e038c9c4Sjoerg     : AlwaysInstrument(llvm::SpecialCaseList::createOrDie(
25*e038c9c4Sjoerg           AlwaysInstrumentPaths, SM.getFileManager().getVirtualFileSystem())),
26*e038c9c4Sjoerg       NeverInstrument(llvm::SpecialCaseList::createOrDie(
27*e038c9c4Sjoerg           NeverInstrumentPaths, SM.getFileManager().getVirtualFileSystem())),
28*e038c9c4Sjoerg       AttrList(llvm::SpecialCaseList::createOrDie(
29*e038c9c4Sjoerg           AttrListPaths, SM.getFileManager().getVirtualFileSystem())),
30*e038c9c4Sjoerg       SM(SM) {}
31*e038c9c4Sjoerg 
32*e038c9c4Sjoerg XRayFunctionFilter::~XRayFunctionFilter() = default;
337330f729Sjoerg 
347330f729Sjoerg XRayFunctionFilter::ImbueAttribute
shouldImbueFunction(StringRef FunctionName) const357330f729Sjoerg XRayFunctionFilter::shouldImbueFunction(StringRef FunctionName) const {
367330f729Sjoerg   // First apply the always instrument list, than if it isn't an "always" see
377330f729Sjoerg   // whether it's treated as a "never" instrument function.
387330f729Sjoerg   // TODO: Remove these as they're deprecated; use the AttrList exclusively.
397330f729Sjoerg   if (AlwaysInstrument->inSection("xray_always_instrument", "fun", FunctionName,
407330f729Sjoerg                                   "arg1") ||
417330f729Sjoerg       AttrList->inSection("always", "fun", FunctionName, "arg1"))
427330f729Sjoerg     return ImbueAttribute::ALWAYS_ARG1;
437330f729Sjoerg   if (AlwaysInstrument->inSection("xray_always_instrument", "fun",
447330f729Sjoerg                                   FunctionName) ||
457330f729Sjoerg       AttrList->inSection("always", "fun", FunctionName))
467330f729Sjoerg     return ImbueAttribute::ALWAYS;
477330f729Sjoerg 
487330f729Sjoerg   if (NeverInstrument->inSection("xray_never_instrument", "fun",
497330f729Sjoerg                                  FunctionName) ||
507330f729Sjoerg       AttrList->inSection("never", "fun", FunctionName))
517330f729Sjoerg     return ImbueAttribute::NEVER;
527330f729Sjoerg 
537330f729Sjoerg   return ImbueAttribute::NONE;
547330f729Sjoerg }
557330f729Sjoerg 
567330f729Sjoerg XRayFunctionFilter::ImbueAttribute
shouldImbueFunctionsInFile(StringRef Filename,StringRef Category) const577330f729Sjoerg XRayFunctionFilter::shouldImbueFunctionsInFile(StringRef Filename,
587330f729Sjoerg                                                StringRef Category) const {
597330f729Sjoerg   if (AlwaysInstrument->inSection("xray_always_instrument", "src", Filename,
607330f729Sjoerg                                   Category) ||
617330f729Sjoerg       AttrList->inSection("always", "src", Filename, Category))
627330f729Sjoerg     return ImbueAttribute::ALWAYS;
637330f729Sjoerg   if (NeverInstrument->inSection("xray_never_instrument", "src", Filename,
647330f729Sjoerg                                  Category) ||
657330f729Sjoerg       AttrList->inSection("never", "src", Filename, Category))
667330f729Sjoerg     return ImbueAttribute::NEVER;
677330f729Sjoerg   return ImbueAttribute::NONE;
687330f729Sjoerg }
697330f729Sjoerg 
707330f729Sjoerg XRayFunctionFilter::ImbueAttribute
shouldImbueLocation(SourceLocation Loc,StringRef Category) const717330f729Sjoerg XRayFunctionFilter::shouldImbueLocation(SourceLocation Loc,
727330f729Sjoerg                                         StringRef Category) const {
737330f729Sjoerg   if (!Loc.isValid())
747330f729Sjoerg     return ImbueAttribute::NONE;
757330f729Sjoerg   return this->shouldImbueFunctionsInFile(SM.getFilename(SM.getFileLoc(Loc)),
767330f729Sjoerg                                           Category);
777330f729Sjoerg }
78