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

Conversation

qinkunbao
Copy link
Member

@qinkunbao qinkunbao commented May 29, 2025

See #142006 and #139128

qinkunbao added 2 commits May 29, 2025 19:58
Created using spr 1.3.6
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 29, 2025
@llvmbot
Copy link
Member

llvmbot commented May 29, 2025

@llvm/pr-subscribers-clang

Author: Qinkun Bao (qinkunbao)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/142027.diff

2 Files Affected:

  • (modified) clang/include/clang/Basic/NoSanitizeList.h (+2-1)
  • (modified) clang/lib/Basic/NoSanitizeList.cpp (+11-10)
diff --git a/clang/include/clang/Basic/NoSanitizeList.h b/clang/include/clang/Basic/NoSanitizeList.h
index 43415859fcd54..4a546351bb66d 100644
--- a/clang/include/clang/Basic/NoSanitizeList.h
+++ b/clang/include/clang/Basic/NoSanitizeList.h
@@ -29,7 +29,8 @@ class SanitizerSpecialCaseList;
 class NoSanitizeList {
   std::unique_ptr<SanitizerSpecialCaseList> SSCL;
   SourceManager &SM;
-
+  bool containsPrefix(SanitizerMask Mask,StringRef Prefix, StringRef Name,
+                      StringRef Category = StringRef()) const;
 public:
   NoSanitizeList(const std::vector<std::string> &NoSanitizeListPaths,
                  SourceManager &SM);
diff --git a/clang/lib/Basic/NoSanitizeList.cpp b/clang/lib/Basic/NoSanitizeList.cpp
index 9f0f1c64995cb..671430dfe6293 100644
--- a/clang/lib/Basic/NoSanitizeList.cpp
+++ b/clang/lib/Basic/NoSanitizeList.cpp
@@ -27,6 +27,15 @@ NoSanitizeList::NoSanitizeList(const std::vector<std::string> &NoSanitizePaths,
 
 NoSanitizeList::~NoSanitizeList() = default;
 
+bool NoSanitizeList::containsPrefix(SanitizerMask Mask, StringRef Prefix,
+                                    StringRef Name, StringRef Category) const {
+  auto NoSan = SSCL->inSectionBlame(Mask, Prefix, Name, Category);
+  if (NoSan == llvm::SpecialCaseList::NotFound)
+    return false;
+  auto San = SSCL->inSectionBlame(Mask, Prefix, Name, "sanitize");
+  return San == llvm::SpecialCaseList::NotFound || NoSan > San;
+}
+
 bool NoSanitizeList::containsGlobal(SanitizerMask Mask, StringRef GlobalName,
                                     StringRef Category) const {
   return SSCL->inSection(Mask, "global", GlobalName, Category);
@@ -34,11 +43,7 @@ bool NoSanitizeList::containsGlobal(SanitizerMask Mask, StringRef GlobalName,
 
 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,
@@ -48,11 +53,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);
 }
 
 bool NoSanitizeList::containsMainFile(SanitizerMask Mask, StringRef FileName,

Created using spr 1.3.6
@qinkunbao qinkunbao requested a review from vitalybuka May 29, 2025 20:01
@qinkunbao qinkunbao marked this pull request as draft May 29, 2025 20:15
Copy link
Contributor

@JustinStitt JustinStitt left a comment

Choose a reason for hiding this comment

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

LGTM

@qinkunbao qinkunbao changed the base branch from users/qinkunbao/spr/main.nosanitizelistnfi-add-containsprefix-to-remove-duplicated-logics to main May 29, 2025 23:39
Copy link

github-actions bot commented May 29, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Created using spr 1.3.6
@qinkunbao qinkunbao marked this pull request as ready for review May 29, 2025 23:52
@qinkunbao qinkunbao requested a review from Copilot May 29, 2025 23:52
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a new helper function, containsPrefix, to deduplicate the logic used for checking sanitizer entries based on a prefix and category. The changes update the containsType and containsFile functions to reuse the new helper function.

  • Added containsPrefix method in NoSanitizeList.cpp and its declaration in NoSanitizeList.h.
  • Refactored containsType and containsFile to call containsPrefix.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
clang/lib/Basic/NoSanitizeList.cpp Added containsPrefix and refactored containsType/containsFile functions
clang/include/clang/Basic/NoSanitizeList.h Declared the new containsPrefix method

Created using spr 1.3.6
@qinkunbao qinkunbao requested a review from Copilot May 30, 2025 00:29
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the NoSanitizeList logic by introducing a new containsPrefix method to remove duplicated logic in checks for types and files.

  • Adds containsPrefix to encapsulate common logic for section blame comparisons.
  • Updates containsType and containsFile to use the new containsPrefix method.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
clang/lib/Basic/NoSanitizeList.cpp Adds the containsPrefix method and refactors methods to use it.
clang/include/clang/Basic/NoSanitizeList.h Adds method declaration for containsPrefix.

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.

@qinkunbao qinkunbao merged commit 2a3afa2 into main May 30, 2025
11 checks passed
@qinkunbao qinkunbao deleted the users/qinkunbao/spr/nosanitizelistnfi-add-containsprefix-to-remove-duplicated-logics branch May 30, 2025 09:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants