https://github.com/devajithvs updated https://github.com/llvm/llvm-project/pull/182044
>From 086c96aba28c8f0d19926e1a513817e39abd7b87 Mon Sep 17 00:00:00 2001 From: Devajith Valaparambil Sreeramaswamy <[email protected]> Date: Wed, 18 Feb 2026 16:19:30 +0100 Subject: [PATCH] [clang-repl] Enable `VerifyDiagnostic` support for virtual buffers In clang-repl, input buffers (e.g., 'input_line_1') are virtual and not tracked by the FileManager. This patch enables us to use input line numbers when verifying tests in clang-repl. --- .../lib/Frontend/VerifyDiagnosticConsumer.cpp | 46 ++++++++++++++++--- clang/test/Interpreter/verify-diagnostics.cpp | 13 ++++++ 2 files changed, 52 insertions(+), 7 deletions(-) create mode 100644 clang/test/Interpreter/verify-diagnostics.cpp diff --git a/clang/lib/Frontend/VerifyDiagnosticConsumer.cpp b/clang/lib/Frontend/VerifyDiagnosticConsumer.cpp index e263e10bb065a..73abb538f35e0 100644 --- a/clang/lib/Frontend/VerifyDiagnosticConsumer.cpp +++ b/clang/lib/Frontend/VerifyDiagnosticConsumer.cpp @@ -556,17 +556,41 @@ static bool ParseDirective(StringRef S, ExpectedData *ED, SourceManager &SM, OptionalFileEntryRef File = PP->LookupFile(Pos, Filename, false, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr); - if (!File) { + + FileID FID; + if (File) { + FID = SM.translateFile(*File); + if (FID.isInvalid()) { + FID = SM.createFileID(*File, Pos, SrcMgr::C_User); + } + } else if (Filename.starts_with("input_line_")) { + // clang-repl buffers (e.g., input_line_1) are virtual and not + // tracked by the FileManager. We need to manually scan the + // SourceManager's local SLocEntry table to find the buffer matching + // this name. + for (unsigned i = 0; i < SM.local_sloc_entry_size(); ++i) { + const SrcMgr::SLocEntry &Entry = SM.getLocalSLocEntry(i); + if (!Entry.isFile()) + continue; + if (auto Buffer = + Entry.getFile().getContentCache().getBufferIfLoaded()) { + if (Buffer->getBufferIdentifier() == Filename) { + SourceLocation EntryLoc = + SourceLocation::getFromRawEncoding(Entry.getOffset()); + FID = SM.getFileID(EntryLoc); + break; + } + } + } + } + + if (FID.isInvalid()) { Diags.Report(Pos.getLocWithOffset(PH.C - PH.Begin), diag::err_verify_missing_file) << Filename << KindStr; continue; } - FileID FID = SM.translateFile(*File); - if (FID.isInvalid()) - FID = SM.createFileID(*File, Pos, SrcMgr::C_User); - if (PH.Next(Line) && Line > 0) ExpectedLoc = SM.translateLineCol(FID, Line, 1); else if (PH.Next("*")) { @@ -949,11 +973,19 @@ static bool IsFromSameFile(SourceManager &SM, SourceLocation DirectiveLoc, if (SM.isWrittenInSameFile(DirectiveLoc, DiagnosticLoc)) return true; - const FileEntry *DiagFile = SM.getFileEntryForID(SM.getFileID(DiagnosticLoc)); + FileID DiagFID = SM.getFileID(DiagnosticLoc); + FileID DirFID = SM.getFileID(DirectiveLoc); + + // If they are the exact same buffer, we're done. + if (DiagFID == DirFID) + return true; + + const FileEntry *DiagFile = SM.getFileEntryForID(DiagFID); + if (!DiagFile && SM.isWrittenInMainFile(DirectiveLoc)) return true; - return (DiagFile == SM.getFileEntryForID(SM.getFileID(DirectiveLoc))); + return DiagFile == SM.getFileEntryForID(DirFID); } /// CheckLists - Compare expected to seen diagnostic lists and return the diff --git a/clang/test/Interpreter/verify-diagnostics.cpp b/clang/test/Interpreter/verify-diagnostics.cpp new file mode 100644 index 0000000000000..f64655575314c --- /dev/null +++ b/clang/test/Interpreter/verify-diagnostics.cpp @@ -0,0 +1,13 @@ +// REQUIRES: host-supports-jit +// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -verify + +auto x = unknown_val; +// expected-error@input_line_3:1 {{use of undeclared identifier 'unknown_val'}} + +int get_int() { return 42; } +char* ptr = get_int(); +// expected-error@input_line_7:1 {{cannot initialize a variable of type 'char *' with an rvalue of type 'int'}} + +// Verify without input_line_* +int y = ; +// expected-error {{expected expression}} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
