llvm-project/clang/lib/Basic/SanitizerSpecialCaseList.cpp
Ellis Hoag 8eb34700c2 [SpecialCaseList] Add option to use Globs instead of Regex to match patterns
Add an option in `SpecialCaseList` to use Globs instead of Regex to match patterns. `GlobPattern` was extended in https://reviews.llvm.org/D153587 to support brace expansions which allows us to use patterns like `*/src/foo.{c,cpp}`. It turns out that most patterns only take advantage of `*` so using Regex was overkill and required lots of escaping in practice. This often led to bugs due to forgetting to escape special characters.

Since this would be a breaking change, we temporarily support Regex by default and use Globs when `#!special-case-list-v2` is the first line in the file. Users should switch to the glob format described in https://llvm.org/doxygen/classllvm_1_1GlobPattern.html. For example, `(abc|def)` should become `{abc,def}`.

See discussion in https://reviews.llvm.org/D152762 and https://discourse.llvm.org/t/use-glob-instead-of-regex-for-specialcaselists/71666.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D154014
2023-09-01 09:06:11 -07:00

67 lines
2.3 KiB
C++

//===--- SanitizerSpecialCaseList.cpp - SCL for sanitizers ----------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// An extension of SpecialCaseList to allowing querying sections by
// SanitizerMask.
//
//===----------------------------------------------------------------------===//
#include "clang/Basic/SanitizerSpecialCaseList.h"
using namespace clang;
std::unique_ptr<SanitizerSpecialCaseList>
SanitizerSpecialCaseList::create(const std::vector<std::string> &Paths,
llvm::vfs::FileSystem &VFS,
std::string &Error) {
std::unique_ptr<clang::SanitizerSpecialCaseList> SSCL(
new SanitizerSpecialCaseList());
if (SSCL->createInternal(Paths, VFS, Error)) {
SSCL->createSanitizerSections();
return SSCL;
}
return nullptr;
}
std::unique_ptr<SanitizerSpecialCaseList>
SanitizerSpecialCaseList::createOrDie(const std::vector<std::string> &Paths,
llvm::vfs::FileSystem &VFS) {
std::string Error;
if (auto SSCL = create(Paths, VFS, Error))
return SSCL;
llvm::report_fatal_error(StringRef(Error));
}
void SanitizerSpecialCaseList::createSanitizerSections() {
for (auto &It : Sections) {
auto &S = It.second;
SanitizerMask Mask;
#define SANITIZER(NAME, ID) \
if (S.SectionMatcher->match(NAME)) \
Mask |= SanitizerKind::ID;
#define SANITIZER_GROUP(NAME, ID, ALIAS) SANITIZER(NAME, ID)
#include "clang/Basic/Sanitizers.def"
#undef SANITIZER
#undef SANITIZER_GROUP
SanitizerSections.emplace_back(Mask, S.Entries);
}
}
bool SanitizerSpecialCaseList::inSection(SanitizerMask Mask, StringRef Prefix,
StringRef Query,
StringRef Category) const {
for (auto &S : SanitizerSections)
if ((S.Mask & Mask) &&
SpecialCaseList::inSectionBlame(S.Entries, Prefix, Query, Category))
return true;
return false;
}