Skip to content

[WIP][DO NOT MERGE][Clang][Driver] Emit warning when -fsanitize-trap=<...> is passed without associated -fsanitize=<...> #147997

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

anthonyhatran
Copy link

@anthonyhatran anthonyhatran commented Jul 10, 2025

This PR is a work-in-progress (WIP) and should not be merged.

This is a sketch PR that will accompany an RFC about changing Clang's behavior handling -fsanitize-trap= without the corresponding sanitizer being enabled.

[Description will be rewritten later]
Spreading this patch out across multiple commits for better practice.

Part of a GSoC 2025 Project

Copy link

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jul 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Jul 10, 2025

@llvm/pr-subscribers-clang-driver

@llvm/pr-subscribers-clang

Author: Anthony Tran (anthonyhatran)

Changes

Part of a GSoC 2025 Project


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

2 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticDriverKinds.td (+5)
  • (modified) clang/include/clang/Basic/DiagnosticGroups.td (+3)
diff --git a/clang/include/clang/Basic/DiagnosticDriverKinds.td b/clang/include/clang/Basic/DiagnosticDriverKinds.td
index 34b6c0d7a8acd..49a8bdf06bda4 100644
--- a/clang/include/clang/Basic/DiagnosticDriverKinds.td
+++ b/clang/include/clang/Basic/DiagnosticDriverKinds.td
@@ -874,4 +874,9 @@ def warn_drv_openacc_without_cir
     : Warning<"OpenACC directives will result in no runtime behavior; use "
               "-fclangir to enable runtime effect">,
       InGroup<SourceUsesOpenACC>;
+
+def warn_drv_sanitize_trap_mismatch : Warning<
+  "-fsanitize-trap=%0 has no effect because the matching sanitizer is not enabled; "
+  "did you mean to pass \"-fsanitize=%0\" as well?">,
+  InGroup<SanitizeTrapMismatch>;
 }
diff --git a/clang/include/clang/Basic/DiagnosticGroups.td b/clang/include/clang/Basic/DiagnosticGroups.td
index f54a830b0103e..a79562cd9c2e0 100644
--- a/clang/include/clang/Basic/DiagnosticGroups.td
+++ b/clang/include/clang/Basic/DiagnosticGroups.td
@@ -1752,3 +1752,6 @@ def ExplicitSpecializationStorageClass : DiagGroup<"explicit-specialization-stor
 
 // A warning for options that enable a feature that is not yet complete
 def ExperimentalOption : DiagGroup<"experimental-option">;
+
+// Warnings for sanitizer arguments
+def SanitizeTrapMismatch : DiagGroup<"sanitize-trap-mismatch">;

@llvmbot llvmbot added the clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' label Jul 10, 2025
@anthonyhatran anthonyhatran changed the title [Clang][Driver] Emit warning when -fsanitize-trap=undefined is passed without -fsanitize=undefined [Clang][Driver] Emit warning when -fsanitize-trap=<...> is passed without associated -fsanitize=<...> Jul 10, 2025
@MaskRay
Copy link
Member

MaskRay commented Jul 11, 2025

The current -fsanitize-trap behavior is intentional. See
https://discourse.llvm.org/t/clang-gsoc-2025-usability-improvements-for-trapping-undefined-behavior-sanitizer/84568/11

I believe Google actually depended on -fsanitize-trap=undefined not leading to warnings/errors when -fsanitize=undefined is not specified.

@MaskRay MaskRay closed this Jul 11, 2025
@Michael137
Copy link
Member

@MaskRay i think the intention of this PR was to write up an accompanying RFC. I don't think the linked Discourse thread had consensus on whether this was something we want to change or not

(summoning @delcypher)

Re-opening for now.

@Michael137 Michael137 reopened this Jul 11, 2025
@delcypher delcypher changed the title [Clang][Driver] Emit warning when -fsanitize-trap=<...> is passed without associated -fsanitize=<...> [WIP][DO NOT MERGE][Clang][Driver] Emit warning when -fsanitize-trap=<...> is passed without associated -fsanitize=<...> Jul 11, 2025
@delcypher
Copy link
Contributor

@MaskRay

The current -fsanitize-trap behavior is intentional. See
https://discourse.llvm.org/t/clang-gsoc-2025-usability-improvements-for-trapping-undefined-behavior-sanitizer/84568/11

Yes we aware the current behavior is intentional. That however doesn't mean it's the right behavior. Our intention is to post a corresponding RFC but as part of the RFC we also wanted a sketch patch of what we are proposing so we can be concrete about our proposed alternative. We (myself & @Michael137) are working with a @anthonyhatran a Google Summer of Code mentee to get the sketch patch in good shape and then we will work on an RFC and then post it.

Although I understand you have good intentions behind proactively closing the PR please do not close our mentee's PR again. If it's ultimately rejected, we'll do it.

Copy link
Contributor

@delcypher delcypher left a comment

Choose a reason for hiding this comment

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

Looks good for the sketch patch. I have a few minor questions and nits that I've left. Please add a few driver test cases to illustrate how you expect this to work (including a test case that suppresses the warning).

@MaskRay
Copy link
Member

MaskRay commented Jul 12, 2025

Thank you for your efforts to enhance sanitizers. However, the current implementation aligns with the preferences of many, including GCC contributors who introduced -fsanitize-trap in 2022. Speaking in my capacity as a clang driver maintainer, sanitizer contributor, and GCC committer, gaining consensus to modify -fsanitize-trap to issue warnings or errors seems unlikely, so closing this issue appears to be the appropriate action.

The proposed change could serve an educational purpose by illustrating modifications to the driver and sanitizer components. However, pursuing its development within llvm/llvm-project risks causing confusion among contributors.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' 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.

5 participants