Skip to content

fix: replace report_fatal_error with Diags and exit #147959

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 5 commits into
base: main
Choose a base branch
from

Conversation

woruyu
Copy link
Contributor

@woruyu woruyu commented Jul 10, 2025

Summary
This PR resolves #147187

@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: woruyu (woruyu)

Changes

Summary
This PR resolves #147187


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

3 Files Affected:

  • (modified) clang/include/clang/Basic/SanitizerSpecialCaseList.h (+3-2)
  • (modified) clang/lib/Basic/NoSanitizeList.cpp (+2-1)
  • (modified) clang/lib/Basic/SanitizerSpecialCaseList.cpp (+7-2)
diff --git a/clang/include/clang/Basic/SanitizerSpecialCaseList.h b/clang/include/clang/Basic/SanitizerSpecialCaseList.h
index cf7485909e409..72cdcf7c467f0 100644
--- a/clang/include/clang/Basic/SanitizerSpecialCaseList.h
+++ b/clang/include/clang/Basic/SanitizerSpecialCaseList.h
@@ -14,6 +14,7 @@
 #ifndef LLVM_CLANG_BASIC_SANITIZERSPECIALCASELIST_H
 #define LLVM_CLANG_BASIC_SANITIZERSPECIALCASELIST_H
 
+#include "clang/Basic/Diagnostic.h"
 #include "clang/Basic/LLVM.h"
 #include "clang/Basic/Sanitizers.h"
 #include "llvm/ADT/StringRef.h"
@@ -37,8 +38,8 @@ class SanitizerSpecialCaseList : public llvm::SpecialCaseList {
          std::string &Error);
 
   static std::unique_ptr<SanitizerSpecialCaseList>
-  createOrDie(const std::vector<std::string> &Paths,
-              llvm::vfs::FileSystem &VFS);
+  createOrDie(const std::vector<std::string> &Paths, llvm::vfs::FileSystem &VFS,
+              DiagnosticsEngine &Diags);
 
   // Query ignorelisted entries if any bit in Mask matches the entry's section.
   bool inSection(SanitizerMask Mask, StringRef Prefix, StringRef Query,
diff --git a/clang/lib/Basic/NoSanitizeList.cpp b/clang/lib/Basic/NoSanitizeList.cpp
index 96f79fb2a2a29..1ae304fbd2132 100644
--- a/clang/lib/Basic/NoSanitizeList.cpp
+++ b/clang/lib/Basic/NoSanitizeList.cpp
@@ -22,7 +22,8 @@ using namespace clang;
 NoSanitizeList::NoSanitizeList(const std::vector<std::string> &NoSanitizePaths,
                                SourceManager &SM)
     : SSCL(SanitizerSpecialCaseList::createOrDie(
-          NoSanitizePaths, SM.getFileManager().getVirtualFileSystem())),
+          NoSanitizePaths, SM.getFileManager().getVirtualFileSystem(),
+          SM.getDiagnostics())),
       SM(SM) {}
 
 NoSanitizeList::~NoSanitizeList() = default;
diff --git a/clang/lib/Basic/SanitizerSpecialCaseList.cpp b/clang/lib/Basic/SanitizerSpecialCaseList.cpp
index f7bc1d5545d75..51a09b341f495 100644
--- a/clang/lib/Basic/SanitizerSpecialCaseList.cpp
+++ b/clang/lib/Basic/SanitizerSpecialCaseList.cpp
@@ -30,11 +30,16 @@ SanitizerSpecialCaseList::create(const std::vector<std::string> &Paths,
 
 std::unique_ptr<SanitizerSpecialCaseList>
 SanitizerSpecialCaseList::createOrDie(const std::vector<std::string> &Paths,
-                                      llvm::vfs::FileSystem &VFS) {
+                                      llvm::vfs::FileSystem &VFS,
+                                      DiagnosticsEngine &Diags) {
   std::string Error;
   if (auto SSCL = create(Paths, VFS, Error))
     return SSCL;
-  llvm::report_fatal_error(StringRef(Error));
+  unsigned DiagID = Diags.getCustomDiagID(clang::DiagnosticsEngine::Error,
+                                          "failed to load NoSanitize file: %0");
+
+  Diags.Report(DiagID) << Error;
+  exit(1);
 }
 
 void SanitizerSpecialCaseList::createSanitizerSections() {

@woruyu
Copy link
Contributor Author

woruyu commented Jul 10, 2025

@hstk30-hw This patch is now ready for review.

@hstk30-hw
Copy link
Contributor

Need a test case at least.

@llvmbot llvmbot added the clang:driver 'clang' and 'clang++' user-facing binaries. Not 'clang-cl' label Jul 11, 2025
@woruyu
Copy link
Contributor Author

woruyu commented Jul 11, 2025

Need a test case at least.

Done!

@hstk30-hw
Copy link
Contributor

@shafik Take a look, pls.

@woruyu woruyu force-pushed the fix/report-error-sanitize-file branch from 816fe1e to a4236f9 Compare July 11, 2025 03:50
@woruyu woruyu force-pushed the fix/report-error-sanitize-file branch from a4236f9 to 4920812 Compare July 11, 2025 06:20
@woruyu
Copy link
Contributor Author

woruyu commented Jul 16, 2025

Hi,@hstk30-hw, @shafik, @AaronBallman just wanted to check if there’s anything further needed from my side for this patch. I’d be happy to help clarify or adjust anything.

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

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

Ugh, sorry for the delayed review! I had a comment several days ago but forgot to hit Submit. :-/

@woruyu woruyu requested review from MaskRay and AaronBallman July 21, 2025 08:03
SourceManager &SM) {
std::string Error;
if (!NoSanitizeL->init(LangOpts.NoSanitizeFiles, Error)) {
const std::string &Path = LangOpts.NoSanitizeFiles.front();
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this assumes that the first file in the list is the one we cannot read; what happens if we can read that one fine but a subsequent file is where we struggle? We'd report the wrong path to the user in that case, which would be hard for them to reason about.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There are two kind of error for NoSanitizeL. The first is can't open file, the second is error parsing file, so I think using CustomDiagID is better!

Copy link
Collaborator

Choose a reason for hiding this comment

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

so I think using CustomDiagID is better!

Custom diagnostics are not an acceptable solution IMO; the reason is because they don't get a warning group associated with them and so users have no way to silence those diagnostics. We have some uses of the API in Clang but in every case they're the wrong tool to use.

We actually document this on the API itself, but not very clearly:

// TODO: Deprecate this once all uses are removed from Clang.
(I'll fix up the comment)

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.

Clang-21 Crash: fatal error: error in backend: can't open file './xxxxxx': No such file or directory
5 participants