Skip to content

[NoSanitizeList][NFI] Add containsPrefix to remove duplicated logics. #142027

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions clang/include/clang/Basic/NoSanitizeList.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class SanitizerSpecialCaseList;
class NoSanitizeList {
std::unique_ptr<SanitizerSpecialCaseList> SSCL;
SourceManager &SM;
bool containsPrefix(SanitizerMask Mask, StringRef Prefix, StringRef Name,
StringRef Category) const;

public:
NoSanitizeList(const std::vector<std::string> &NoSanitizeListPaths,
Expand Down
27 changes: 17 additions & 10 deletions clang/lib/Basic/NoSanitizeList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,29 @@ NoSanitizeList::NoSanitizeList(const std::vector<std::string> &NoSanitizePaths,

NoSanitizeList::~NoSanitizeList() = default;

bool NoSanitizeList::containsPrefix(SanitizerMask Mask, StringRef Prefix,
StringRef Name, StringRef Category) const {
std::pair<unsigned, unsigned> NoSan =
SSCL->inSectionBlame(Mask, Prefix, Name, Category);
if (NoSan == llvm::SpecialCaseList::NotFound)
return false;
std::pair<unsigned, unsigned> San =
SSCL->inSectionBlame(Mask, Prefix, Name, "sanitize");
// The statement evaluates to true under the following conditions:
// 1. The string "prefix:*=sanitize" is absent.
// 2. If "prefix:*=sanitize" is present, its (File Index, Line Number) is less
// than that of "prefix:*".
return San == llvm::SpecialCaseList::NotFound || NoSan > San;
}

bool NoSanitizeList::containsGlobal(SanitizerMask Mask, StringRef GlobalName,
StringRef Category) const {
return SSCL->inSection(Mask, "global", GlobalName, Category);
}

bool NoSanitizeList::containsType(SanitizerMask Mask, StringRef MangledTypeName,
StringRef Category) const {
auto NoSan = SSCL->inSectionBlame(Mask, "type", MangledTypeName, Category);
if (NoSan == llvm::SpecialCaseList::NotFound)
return false;
auto San = SSCL->inSectionBlame(Mask, "type", MangledTypeName, "sanitize");
return San == llvm::SpecialCaseList::NotFound || NoSan > San;
return containsPrefix(Mask, "type", MangledTypeName, Category);
}

bool NoSanitizeList::containsFunction(SanitizerMask Mask,
Expand All @@ -48,11 +59,7 @@ bool NoSanitizeList::containsFunction(SanitizerMask Mask,

bool NoSanitizeList::containsFile(SanitizerMask Mask, StringRef FileName,
StringRef Category) const {
auto NoSan = SSCL->inSectionBlame(Mask, "src", FileName, Category);
if (NoSan == llvm::SpecialCaseList::NotFound)
return false;
auto San = SSCL->inSectionBlame(Mask, "src", FileName, "sanitize");
return San == llvm::SpecialCaseList::NotFound || NoSan > San;
return containsPrefix(Mask, "src", FileName, Category);
Copy link
Collaborator

@vitalybuka vitalybuka May 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update all other cases in the next patch

Fell free to have a single trivial test per prefix, e.g. just check that makes a difference.
fun:*=sanitize

Copy link
Member Author

@qinkunbao qinkunbao May 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I plan to update one prefix one PR. Do you think will it be a good idea to update all other cases in one PR?
Currently, I have a pull request updating "fun" and am running tests before sending it to you for review.
#142074

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have a preference, change is trivial per prefix.

}

bool NoSanitizeList::containsMainFile(SanitizerMask Mask, StringRef FileName,
Expand Down