Skip to content

Commit

Permalink
[Clang] Fix : More Detailed "No expected directives found" (#78338)
Browse files Browse the repository at this point in the history
Updated the error message to use the proper prefix when
no expected directives are found by changing the hard coded expected in
the message to a dynamic value in two error messages.

Fixes #58290
  • Loading branch information
Sh0g0-1758 committed Feb 7, 2024
1 parent 50d38cf commit 4e58f03
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 6 deletions.
6 changes: 3 additions & 3 deletions clang/include/clang/Basic/DiagnosticFrontendKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -176,10 +176,10 @@ def err_verify_inconsistent_diags : Error<
"'%0' diagnostics %select{expected|seen}1 but not %select{seen|expected}1: "
"%2">;
def err_verify_invalid_no_diags : Error<
"%select{expected|'expected-no-diagnostics'}0 directive cannot follow "
"%select{'expected-no-diagnostics' directive|other expected directives}0">;
"%select{expected|'%0-no-diagnostics'}1 directive cannot follow "
"%select{'%0-no-diagnostics' directive|other expected directives}1">;
def err_verify_no_directives : Error<
"no expected directives found: consider use of 'expected-no-diagnostics'">;
"no expected directives found: consider use of '%0-no-diagnostics'">;
def err_verify_nonconst_addrspace : Error<
"qualifier 'const' is needed for variables in address space '%0'">;

Expand Down
13 changes: 10 additions & 3 deletions clang/lib/Frontend/VerifyDiagnosticConsumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,12 @@ class VerifyDiagnosticConsumer::MarkerTracker {
}
};

static std::string DetailedErrorString(const DiagnosticsEngine &Diags) {
if (Diags.getDiagnosticOptions().VerifyPrefixes.empty())
return "expected";
return *Diags.getDiagnosticOptions().VerifyPrefixes.begin();
}

/// ParseDirective - Go through the comment and see if it indicates expected
/// diagnostics. If so, then put them in the appropriate directive list.
///
Expand Down Expand Up @@ -478,14 +484,14 @@ static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM,
if (NoDiag) {
if (Status == VerifyDiagnosticConsumer::HasOtherExpectedDirectives)
Diags.Report(Pos, diag::err_verify_invalid_no_diags)
<< /*IsExpectedNoDiagnostics=*/true;
<< DetailedErrorString(Diags) << /*IsExpectedNoDiagnostics=*/true;
else
Status = VerifyDiagnosticConsumer::HasExpectedNoDiagnostics;
continue;
}
if (Status == VerifyDiagnosticConsumer::HasExpectedNoDiagnostics) {
Diags.Report(Pos, diag::err_verify_invalid_no_diags)
<< /*IsExpectedNoDiagnostics=*/false;
<< DetailedErrorString(Diags) << /*IsExpectedNoDiagnostics=*/false;
continue;
}
Status = VerifyDiagnosticConsumer::HasOtherExpectedDirectives;
Expand Down Expand Up @@ -1104,7 +1110,8 @@ void VerifyDiagnosticConsumer::CheckDiagnostics() {
// Produce an error if no expected-* directives could be found in the
// source file(s) processed.
if (Status == HasNoDirectives) {
Diags.Report(diag::err_verify_no_directives).setForceEmit();
Diags.Report(diag::err_verify_no_directives).setForceEmit()
<< DetailedErrorString(Diags);
++NumErrors;
Status = HasNoDirectivesReported;
}
Expand Down
29 changes: 29 additions & 0 deletions clang/test/Frontend/verify.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,32 @@ unexpected b; // expected-error@33 1-1 {{unknown type}}
#endif

#endif

#ifdef TEST10
// RUN: not %clang_cc1 -DTEST10 -verify=foo %s 2>&1 | FileCheck -check-prefix=CHECK10 %s

// CHECK10: error: no expected directives found: consider use of 'foo-no-diagnostics'
#endif

#ifdef TEST11
// RUN: not %clang_cc1 -DTEST11 -verify=foo %s 2>&1 | FileCheck -check-prefix=CHECK11 %s

// foo-no-diagnostics
// foo-note {{}}

// CHECK11: error: 'foo-error' diagnostics seen but not expected:
// CHECK11-NEXT: Line 201: expected directive cannot follow 'foo-no-diagnostics' directive
// CHECK11-NEXT: 1 error generated.
#endif

#ifdef TEST12
// RUN: not %clang_cc1 -DTEST12 -verify=foo %s 2>&1 | FileCheck -check-prefix=CHECK12 %s

#warning X
// foo-warning@-1 {{X}}
// foo-no-diagnostics

// CHECK12: error: 'foo-error' diagnostics seen but not expected:
// CHECK12-NEXT: Line 213: 'foo-no-diagnostics' directive cannot follow other expected directives
// CHECK12-NEXT: 1 error generated.
#endif

0 comments on commit 4e58f03

Please sign in to comment.