[clang] 4e58f03 - [Clang] Fix : More Detailed "No expected directives found" (#78338)

via cfe-commits cfe-commits at lists.llvm.org
Wed Feb 7 06:10:21 PST 2024


Author: Shourya Goel
Date: 2024-02-07T09:10:17-05:00
New Revision: 4e58f03f298eb5c74877942c1c68911341c678bd

URL: https://github.com/llvm/llvm-project/commit/4e58f03f298eb5c74877942c1c68911341c678bd
DIFF: https://github.com/llvm/llvm-project/commit/4e58f03f298eb5c74877942c1c68911341c678bd.diff

LOG: [Clang] Fix : More Detailed "No expected directives found" (#78338)

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

Added: 
    

Modified: 
    clang/include/clang/Basic/DiagnosticFrontendKinds.td
    clang/lib/Frontend/VerifyDiagnosticConsumer.cpp
    clang/test/Frontend/verify.c

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/Basic/DiagnosticFrontendKinds.td b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
index 85ecfdf9de62d4..b1a282f5164a2a 100644
--- a/clang/include/clang/Basic/DiagnosticFrontendKinds.td
+++ b/clang/include/clang/Basic/DiagnosticFrontendKinds.td
@@ -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'">;
 

diff  --git a/clang/lib/Frontend/VerifyDiagnosticConsumer.cpp b/clang/lib/Frontend/VerifyDiagnosticConsumer.cpp
index 304935a0a90b8e..48330e93617181 100644
--- a/clang/lib/Frontend/VerifyDiagnosticConsumer.cpp
+++ b/clang/lib/Frontend/VerifyDiagnosticConsumer.cpp
@@ -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.
 ///
@@ -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;
@@ -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;
     }

diff  --git a/clang/test/Frontend/verify.c b/clang/test/Frontend/verify.c
index c549011d7b7a9a..afd1c7d6907e20 100644
--- a/clang/test/Frontend/verify.c
+++ b/clang/test/Frontend/verify.c
@@ -187,3 +187,32 @@ unexpected b; // expected-error at 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 at -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


        


More information about the cfe-commits mailing list