[clang][dataflow] Print the source line if we saw unexpected diagnostics in tests.
authorMartin Braenne <mboehme@google.com>
Thu, 20 Jul 2023 08:53:41 +0000 (08:53 +0000)
committerMartin Braenne <mboehme@google.com>
Thu, 20 Jul 2023 12:40:22 +0000 (12:40 +0000)
This makes it easier to determine which line the unexpected happened on; previously, we would only get the line number.

Reviewed By: ymandel

Differential Revision: https://reviews.llvm.org/D155802

clang/unittests/Analysis/FlowSensitive/UncheckedOptionalAccessModelTest.cpp

index 4bac015..a181f28 100644 (file)
@@ -12,6 +12,7 @@
 #include "clang/AST/ASTContext.h"
 #include "clang/ASTMatchers/ASTMatchers.h"
 #include "clang/Basic/SourceLocation.h"
+#include "clang/Frontend/TextDiagnostic.h"
 #include "clang/Tooling/Tooling.h"
 #include "llvm/ADT/DenseSet.h"
 #include "llvm/ADT/STLExtras.h"
@@ -1334,7 +1335,17 @@ protected:
           auto &SrcMgr = AO.ASTCtx.getSourceManager();
           llvm::DenseSet<unsigned> DiagnosticLines;
           for (SourceLocation &Loc : Diagnostics) {
-            DiagnosticLines.insert(SrcMgr.getPresumedLineNumber(Loc));
+            unsigned Line = SrcMgr.getPresumedLineNumber(Loc);
+            DiagnosticLines.insert(Line);
+            if (!AnnotationLines.contains(Line)) {
+              IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts(
+                  new DiagnosticOptions());
+              TextDiagnostic TD(llvm::errs(), AO.ASTCtx.getLangOpts(),
+                                DiagOpts.get());
+              TD.emitDiagnostic(
+                  FullSourceLoc(Loc, SrcMgr), DiagnosticsEngine::Error,
+                  "unexpected diagnostic", std::nullopt, std::nullopt);
+            }
           }
 
           EXPECT_THAT(DiagnosticLines, ContainerEq(AnnotationLines));