Skip to content

Commit 9d3dd8e

Browse files
authored
fix: replace report_fatal_error with Diags and exit (#147959)
report_fatal_error is not a good way to report diagnostics to the users, so this switches to using actual diagnostic reporting mechanisms instead. Fixes #147187
1 parent c1545b6 commit 9d3dd8e

File tree

14 files changed

+81
-41
lines changed

14 files changed

+81
-41
lines changed

clang/include/clang/AST/ASTContext.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,8 @@ class ASTContext : public RefCountedBase<ASTContext> {
634634
void setRelocationInfoForCXXRecord(const CXXRecordDecl *,
635635
CXXRecordDeclRelocationInfo);
636636

637+
void initSanitizers(const LangOptions &LangOpts, SourceManager &SM);
638+
637639
/// Examines a given type, and returns whether the type itself
638640
/// is address discriminated, or any transitively embedded types
639641
/// contain data that is address discriminated. This includes

clang/include/clang/Basic/DiagnosticDriverKinds.td

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,13 +252,13 @@ def err_drv_invalid_argument_to_option : Error<
252252
def err_drv_missing_sanitizer_ignorelist : Error<
253253
"missing sanitizer ignorelist: '%0'">;
254254
def err_drv_malformed_sanitizer_ignorelist : Error<
255-
"malformed sanitizer ignorelist: '%0'">;
255+
"failed to %select{load|parse}0 malformed sanitizer ignorelist: '%1'">;
256256
def err_drv_malformed_sanitizer_coverage_allowlist : Error<
257-
"malformed sanitizer coverage allowlist: '%0'">;
257+
"failed to %select{load|parse}0 malformed sanitizer coverage allowlist: '%1'">;
258258
def err_drv_malformed_sanitizer_coverage_ignorelist : Error<
259-
"malformed sanitizer coverage ignorelist: '%0'">;
259+
"failed to %select{load|parse}0 malformed sanitizer coverage ignorelist: '%1'">;
260260
def err_drv_malformed_sanitizer_metadata_ignorelist : Error<
261-
"malformed sanitizer metadata ignorelist: '%0'">;
261+
"failed to %select{load|parse}0 malformed sanitizer metadata ignorelist: '%1'">;
262262
def err_drv_unsupported_static_sanitizer_darwin : Error<
263263
"static %0 runtime is not supported on darwin">;
264264
def err_drv_duplicate_config : Error<

clang/include/clang/Basic/DiagnosticFrontendKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,8 @@ def warn_profile_data_misexpect : Warning<
370370
"potential performance regression from use of __builtin_expect(): "
371371
"annotation was correct on %0 of profiled executions">,
372372
BackendInfo, InGroup<MisExpect>;
373+
def err_sanitize_ignorelist_failure : Error<
374+
"failed to %select{load|parse}0 sanitizer ignorelist file: '%1'">;
373375
} // end of instrumentation issue category
374376

375377
def err_extract_api_ignores_file_not_found :

clang/include/clang/Basic/NoSanitizeList.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ class NoSanitizeList {
3333
StringRef Category) const;
3434

3535
public:
36-
NoSanitizeList(const std::vector<std::string> &NoSanitizeListPaths,
37-
SourceManager &SM);
36+
NoSanitizeList(SourceManager &SM);
3837
~NoSanitizeList();
38+
bool init(const std::vector<std::string> &Paths,
39+
std::pair<unsigned, std::string> &Error);
3940
bool containsGlobal(SanitizerMask Mask, StringRef GlobalName,
4041
StringRef Category = StringRef()) const;
4142
bool containsType(SanitizerMask Mask, StringRef MangledTypeName,

clang/include/clang/Basic/SanitizerSpecialCaseList.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef LLVM_CLANG_BASIC_SANITIZERSPECIALCASELIST_H
1515
#define LLVM_CLANG_BASIC_SANITIZERSPECIALCASELIST_H
1616

17+
#include "clang/Basic/Diagnostic.h"
1718
#include "clang/Basic/LLVM.h"
1819
#include "clang/Basic/Sanitizers.h"
1920
#include "llvm/ADT/StringRef.h"
@@ -34,11 +35,7 @@ class SanitizerSpecialCaseList : public llvm::SpecialCaseList {
3435
public:
3536
static std::unique_ptr<SanitizerSpecialCaseList>
3637
create(const std::vector<std::string> &Paths, llvm::vfs::FileSystem &VFS,
37-
std::string &Error);
38-
39-
static std::unique_ptr<SanitizerSpecialCaseList>
40-
createOrDie(const std::vector<std::string> &Paths,
41-
llvm::vfs::FileSystem &VFS);
38+
std::pair<unsigned, std::string> &Error);
4239

4340
// Query ignorelisted entries if any bit in Mask matches the entry's section.
4441
bool inSection(SanitizerMask Mask, StringRef Prefix, StringRef Query,

clang/lib/AST/ASTContext.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "clang/Basic/AddressSpaces.h"
5151
#include "clang/Basic/Builtins.h"
5252
#include "clang/Basic/CommentOptions.h"
53+
#include "clang/Basic/DiagnosticFrontend.h"
5354
#include "clang/Basic/ExceptionSpecificationType.h"
5455
#include "clang/Basic/IdentifierTable.h"
5556
#include "clang/Basic/LLVM.h"
@@ -944,7 +945,7 @@ ASTContext::ASTContext(LangOptions &LOpts, SourceManager &SM,
944945
DependentBitIntTypes(this_()), SubstTemplateTemplateParmPacks(this_()),
945946
DeducedTemplates(this_()), ArrayParameterTypes(this_()),
946947
CanonTemplateTemplateParms(this_()), SourceMgr(SM), LangOpts(LOpts),
947-
NoSanitizeL(new NoSanitizeList(LangOpts.NoSanitizeFiles, SM)),
948+
NoSanitizeL(new NoSanitizeList(SM)),
948949
XRayFilter(new XRayFunctionFilter(LangOpts.XRayAlwaysInstrumentFiles,
949950
LangOpts.XRayNeverInstrumentFiles,
950951
LangOpts.XRayAttrListFiles, SM)),
@@ -1697,6 +1698,15 @@ ASTContext::getRelocationInfoForCXXRecord(const CXXRecordDecl *RD) const {
16971698
return std::nullopt;
16981699
}
16991700

1701+
void ASTContext::initSanitizers(const LangOptions &LangOpts,
1702+
SourceManager &SM) {
1703+
std::pair<unsigned, std::string> Error;
1704+
if (!NoSanitizeL->init(LangOpts.NoSanitizeFiles, Error)) {
1705+
SM.getDiagnostics().Report(diag::err_sanitize_ignorelist_failure)
1706+
<< Error.first << Error.second;
1707+
}
1708+
}
1709+
17001710
void ASTContext::setRelocationInfoForCXXRecord(
17011711
const CXXRecordDecl *RD, CXXRecordDeclRelocationInfo Info) {
17021712
assert(RD);

clang/lib/Basic/NoSanitizeList.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,7 @@
1919

2020
using namespace clang;
2121

22-
NoSanitizeList::NoSanitizeList(const std::vector<std::string> &NoSanitizePaths,
23-
SourceManager &SM)
24-
: SSCL(SanitizerSpecialCaseList::createOrDie(
25-
NoSanitizePaths, SM.getFileManager().getVirtualFileSystem())),
26-
SM(SM) {}
22+
NoSanitizeList::NoSanitizeList(SourceManager &SM) : SM(SM) {}
2723

2824
NoSanitizeList::~NoSanitizeList() = default;
2925

@@ -42,6 +38,13 @@ bool NoSanitizeList::containsPrefix(SanitizerMask Mask, StringRef Prefix,
4238
return San == llvm::SpecialCaseList::NotFound || NoSan > San;
4339
}
4440

41+
bool NoSanitizeList::init(const std::vector<std::string> &Paths,
42+
std::pair<unsigned, std::string> &Error) {
43+
SSCL = SanitizerSpecialCaseList::create(
44+
Paths, SM.getFileManager().getVirtualFileSystem(), Error);
45+
return SSCL != nullptr;
46+
}
47+
4548
bool NoSanitizeList::containsGlobal(SanitizerMask Mask, StringRef GlobalName,
4649
StringRef Category) const {
4750
return containsPrefix(Mask, "global", GlobalName, Category);

clang/lib/Basic/ProfileList.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class ProfileSpecialCaseList : public llvm::SpecialCaseList {
2626
public:
2727
static std::unique_ptr<ProfileSpecialCaseList>
2828
create(const std::vector<std::string> &Paths, llvm::vfs::FileSystem &VFS,
29-
std::string &Error);
29+
std::pair<unsigned, std::string> &Error);
3030

3131
static std::unique_ptr<ProfileSpecialCaseList>
3232
createOrDie(const std::vector<std::string> &Paths,
@@ -44,7 +44,8 @@ class ProfileSpecialCaseList : public llvm::SpecialCaseList {
4444

4545
std::unique_ptr<ProfileSpecialCaseList>
4646
ProfileSpecialCaseList::create(const std::vector<std::string> &Paths,
47-
llvm::vfs::FileSystem &VFS, std::string &Error) {
47+
llvm::vfs::FileSystem &VFS,
48+
std::pair<unsigned, std::string> &Error) {
4849
auto PSCL = std::make_unique<ProfileSpecialCaseList>();
4950
if (PSCL->createInternal(Paths, VFS, Error))
5051
return PSCL;
@@ -54,10 +55,11 @@ ProfileSpecialCaseList::create(const std::vector<std::string> &Paths,
5455
std::unique_ptr<ProfileSpecialCaseList>
5556
ProfileSpecialCaseList::createOrDie(const std::vector<std::string> &Paths,
5657
llvm::vfs::FileSystem &VFS) {
57-
std::string Error;
58+
std::pair<unsigned, std::string> Error;
5859
if (auto PSCL = create(Paths, VFS, Error))
5960
return PSCL;
60-
llvm::report_fatal_error(llvm::Twine(Error));
61+
// TODO: add init function and use diagnose instead fo report_fatal_error
62+
llvm::report_fatal_error(llvm::Twine(Error.second));
6163
}
6264

6365
} // namespace clang

clang/lib/Basic/SanitizerSpecialCaseList.cpp

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ using namespace clang;
1818
std::unique_ptr<SanitizerSpecialCaseList>
1919
SanitizerSpecialCaseList::create(const std::vector<std::string> &Paths,
2020
llvm::vfs::FileSystem &VFS,
21-
std::string &Error) {
21+
std::pair<unsigned, std::string> &Error) {
2222
std::unique_ptr<clang::SanitizerSpecialCaseList> SSCL(
2323
new SanitizerSpecialCaseList());
2424
if (SSCL->createInternal(Paths, VFS, Error)) {
@@ -28,15 +28,6 @@ SanitizerSpecialCaseList::create(const std::vector<std::string> &Paths,
2828
return nullptr;
2929
}
3030

31-
std::unique_ptr<SanitizerSpecialCaseList>
32-
SanitizerSpecialCaseList::createOrDie(const std::vector<std::string> &Paths,
33-
llvm::vfs::FileSystem &VFS) {
34-
std::string Error;
35-
if (auto SSCL = create(Paths, VFS, Error))
36-
return SSCL;
37-
llvm::report_fatal_error(StringRef(Error));
38-
}
39-
4031
void SanitizerSpecialCaseList::createSanitizerSections() {
4132
for (auto &S : Sections) {
4233
SanitizerMask Mask;

clang/lib/Driver/SanitizerArgs.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,11 @@ static void validateSpecialCaseListFormat(const Driver &D,
181181
if (SCLFiles.empty())
182182
return;
183183

184-
std::string BLError;
184+
std::pair<unsigned, std::string> BLError;
185185
std::unique_ptr<llvm::SpecialCaseList> SCL(
186186
llvm::SpecialCaseList::create(SCLFiles, D.getVFS(), BLError));
187187
if (!SCL && DiagnoseErrors)
188-
D.Diag(MalformedSCLErrorDiagID) << BLError;
188+
D.Diag(MalformedSCLErrorDiagID) << BLError.first << BLError.second;
189189
}
190190

191191
static void addDefaultIgnorelists(const Driver &D, SanitizerMask Kinds,

0 commit comments

Comments
 (0)