[PATCH] D126183: Implement soft reset of the diagnostics engine.

2022-06-14 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak updated this revision to Diff 436750.
tapaswenipathak edited the summary of this revision.
tapaswenipathak added a comment.

Addresses review comment by @v.g.vassilev.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126183/new/

https://reviews.llvm.org/D126183

Files:
  clang/include/clang/Basic/Diagnostic.h
  clang/lib/Basic/Diagnostic.cpp
  clang/lib/Interpreter/IncrementalParser.cpp
  clang/test/Interpreter/error-recovery-pragmas.cpp
  clang/tools/clang-repl/ClangRepl.cpp
  clang/unittests/Basic/DiagnosticTest.cpp

Index: clang/unittests/Basic/DiagnosticTest.cpp
===
--- clang/unittests/Basic/DiagnosticTest.cpp
+++ clang/unittests/Basic/DiagnosticTest.cpp
@@ -14,6 +14,15 @@
 using namespace llvm;
 using namespace clang;
 
+void clang::DiagnosticsTestHelper(DiagnosticsEngine &diag) {
+  unsigned delayedDiagID = 0U;
+
+  EXPECT_EQ(diag.DelayedDiagID, delayedDiagID);
+  EXPECT_FALSE(diag.DiagStates.empty());
+  EXPECT_TRUE(diag.DiagStatesByLoc.empty());
+  EXPECT_TRUE(diag.DiagStateOnPushStack.empty());
+}
+
 namespace {
 
 // Check that DiagnosticErrorTrap works with SuppressAllDiagnostics.
@@ -71,6 +80,32 @@
 EXPECT_EQ(Diags.getNumWarnings(), FatalsAsError);
   }
 }
+
+// Check that soft RESET works as intended
+TEST(DiagnosticTest, softReset) {
+  DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
+  new IgnoringDiagConsumer());
+
+  unsigned numWarnings = 0U, numErrors = 0U;
+
+  Diags.Reset(true);
+  // Check For ErrorOccurred and TrapNumErrorsOccurred
+  EXPECT_FALSE(Diags.hasErrorOccurred());
+  EXPECT_FALSE(Diags.hasFatalErrorOccurred());
+  EXPECT_FALSE(Diags.hasUncompilableErrorOccurred());
+  // Check for UnrecoverableErrorOccurred and TrapNumUnrecoverableErrorsOccurred
+  EXPECT_FALSE(Diags.hasUnrecoverableErrorOccurred());
+
+  EXPECT_EQ(Diags.getNumWarnings(), numWarnings);
+  EXPECT_EQ(Diags.getNumErrors(), numErrors);
+
+  // Check for private variables of DiagnosticsEngine differentiating soft reset
+  DiagnosticsTestHelper(Diags);
+
+  EXPECT_FALSE(Diags.isDiagnosticInFlight());
+  EXPECT_TRUE(Diags.isLastDiagnosticIgnored());
+}
+
 TEST(DiagnosticTest, diagnosticError) {
   DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
   new IgnoringDiagConsumer());
Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -49,6 +49,23 @@
   exit(GenCrashDiag ? 70 : 1);
 }
 
+// If we are running with -verify a reported has to be returned as unsuccess.
+// This is relevant especially for the test suite.
+static int checkDiagErrors(const clang::CompilerInstance *CI) {
+  unsigned Errs = CI->getDiagnostics().getClient()->getNumErrors();
+  if (CI->getDiagnosticOpts().VerifyDiagnostics) {
+// If there was an error that came from the verifier we must return 1 as
+// an exit code for the process. This will make the test fail as expected.
+clang::DiagnosticConsumer *Client = CI->getDiagnostics().getClient();
+Client->EndSourceFile();
+Errs = Client->getNumErrors();
+
+// The interpreter expects BeginSourceFile/EndSourceFiles to be balanced.
+Client->BeginSourceFile(CI->getLangOpts(), &CI->getPreprocessor());
+  }
+  return Errs ? EXIT_FAILURE : EXIT_SUCCESS;
+}
+
 llvm::ExitOnError ExitOnErr;
 int main(int argc, const char **argv) {
   ExitOnErr.setBanner("clang-repl: ");
@@ -104,5 +121,5 @@
 
   llvm::llvm_shutdown();
 
-  return 0;
+  return checkDiagErrors(Interp->getCompilerInstance());
 }
Index: clang/test/Interpreter/error-recovery-pragmas.cpp
===
--- /dev/null
+++ clang/test/Interpreter/error-recovery-pragmas.cpp
@@ -0,0 +1,7 @@
+// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -verify
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wmultichar"
+// Reset should not delete #pragmas
+error; // expected-error {{a type specifier is required for all declarations}}
+void no_diag_multichar(void) { char c = (char)'ab'; }
+quit
Index: clang/lib/Interpreter/IncrementalParser.cpp
===
--- clang/lib/Interpreter/IncrementalParser.cpp
+++ clang/lib/Interpreter/IncrementalParser.cpp
@@ -197,8 +197,7 @@
   }
 }
 
-// FIXME: Do not reset the pragma handlers.
-Diags.Reset();
+Diags.Reset(/*soft=*/true);
 return llvm::make_error("Parsing failed.",
std::error_code());
   }
Index: clang/lib/Basic/Diagnostic.cpp
===
--- clang/lib/Basic/Diagnostic.cpp
+++ clang/lib/Basic/Diagnostic.cpp
@@ -130,7 +130,7 @@
   return true;
 }
 
-void DiagnosticsEngine::Reset() {
+void Diagnos

[PATCH] D126266: Mark the file entry invalid, until reread. Invalidate SLocEntry cache, readd it on reread. Do not use translateFile, because it pulls in parts of the pch.

2022-06-20 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak updated this revision to Diff 438535.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126266/new/

https://reviews.llvm.org/D126266

Files:
  clang/include/clang/Basic/FileEntry.h
  clang/include/clang/Basic/FileManager.h
  clang/include/clang/Basic/SourceManager.h
  clang/lib/Basic/FileManager.cpp
  clang/lib/Basic/SourceManager.cpp
  clang/unittests/Basic/FileManagerTest.cpp
  clang/unittests/Basic/SourceManagerTest.cpp

Index: clang/unittests/Basic/SourceManagerTest.cpp
===
--- clang/unittests/Basic/SourceManagerTest.cpp
+++ clang/unittests/Basic/SourceManagerTest.cpp
@@ -26,6 +26,14 @@
 
 using namespace clang;
 
+void clang::SourceManagerTestHelper(SourceManager &SourceMgr, FileManager &FileMgr) {
+  FileID mainFileID = SourceMgr.getMainFileID();
+  const SrcMgr::ContentCache *Cache =
+&(SourceMgr.getSLocEntry(mainFileID).getFile().getContentCache());
+  //EXPECT_FALSE(Cache->Buffer);
+  ASSERT_TRUE(FileMgr.FileEntriesToReread.empty());
+}
+
 namespace {
 
 // The test fixture.
@@ -51,6 +59,36 @@
   IntrusiveRefCntPtr Target;
 };
 
+// Test for invalidate cache success, making the file entry invalid, until
+// reread
+TEST_F(SourceManagerTest, invalidateCacheSuccess) {
+  const char *Source = "int x;";
+
+  std::unique_ptr Buf =
+  llvm::MemoryBuffer::getMemBuffer(Source);
+  const FileEntry *SourceFile =
+  FileMgr.getVirtualFile("mainFile.cpp", Buf->getBufferSize(), 0);
+
+  FileID mainFileID = SourceMgr.createFileID(std::move(Buf));
+  SourceMgr.overrideFileContents(SourceFile, std::move(Buf));
+  SourceMgr.setMainFileID(mainFileID);
+
+  SourceMgr.invalidateCache(mainFileID);
+
+  EXPECT_FALSE(SourceFile->isValid());
+  EXPECT_FALSE(mainFileID.isInvalid());
+
+  const SrcMgr::ContentCache *Cache =
+&(SourceMgr.getSLocEntry(mainFileID).getFile().getContentCache());
+
+  EXPECT_EQ(SourceMgr.getNonBuiltinFilenameForID(mainFileID), None);
+  //EXPECT_EQ(SourceMgr.getBufferDataIfLoaded(mainFileID), None);
+
+  EXPECT_FALSE(Cache->IsBufferInvalid);
+
+  SourceManagerTestHelper(SourceMgr, FileMgr);
+}
+
 TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
   const char *source =
 "#define M(x) [x]\n"
Index: clang/unittests/Basic/FileManagerTest.cpp
===
--- clang/unittests/Basic/FileManagerTest.cpp
+++ clang/unittests/Basic/FileManagerTest.cpp
@@ -18,6 +18,10 @@
 using namespace llvm;
 using namespace clang;
 
+void clang::FileManagerTestHelper(FileManager &manager){
+  ASSERT_TRUE(!manager.FileEntriesToReread.empty());
+}
+
 namespace {
 
 // Used to create a fake file system for running the tests with such
@@ -99,6 +103,29 @@
   FileManager manager;
 };
 
+// If file entry valid, mark the file entry invalid, until reread.
+TEST_F(FileManagerTest, invalidateCacheSuccess) {
+  auto statCache = std::make_unique();
+  statCache->InjectFile("file.cpp", 42);
+
+  manager.setStatCache(std::move(statCache));
+  manager.getVirtualFile("file.cpp", 100, 0);
+  auto file = manager.getFile("file.cpp");
+
+  // Check for file null assertion success
+  //manager.invalidateCache(NULL);
+
+  auto FileRef = manager.getFileRef("file.cpp");
+
+  ASSERT_FALSE(!FileRef);
+
+  FileEntry *FileEntryObj = const_cast(&FileRef->getFileEntry());
+  manager.invalidateCache(FileEntryObj);
+  FileManagerTestHelper(manager);
+
+  ASSERT_FALSE(FileEntryObj->isValid());
+}
+
 // When a virtual file is added, its getDir() field has correct name.
 TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
   FileEntryRef file = manager.getVirtualFileRef("foo.cpp", 42, 0);
Index: clang/lib/Basic/SourceManager.cpp
===
--- clang/lib/Basic/SourceManager.cpp
+++ clang/lib/Basic/SourceManager.cpp
@@ -358,6 +358,25 @@
   return false;
 }
 
+void SourceManager::invalidateCache(FileID FID) {
+  const FileEntry* Entry = getFileEntryForID(FID);
+  if (!Entry)
+return;
+  if (ContentCache *&E = FileInfos[Entry]) {
+E->setBuffer(nullptr);
+E = 0;
+  }
+  if (!FID.isInvalid()) {
+const SrcMgr::SLocEntry& SLocE = getSLocEntry(FID);
+if (SLocE.isFile()) {
+  SrcMgr::ContentCache& CC =
+const_cast(SLocE.getFile().getContentCache());
+  CC.setBuffer(nullptr);
+}
+  }
+  getFileManager().invalidateCache(const_cast(Entry));
+}
+
 void SourceManager::initializeForReplay(const SourceManager &Old) {
   assert(MainFileID.isInvalid() && "expected uninitialized SourceManager");
 
Index: clang/lib/Basic/FileManager.cpp
===
--- clang/lib/Basic/FileManager.cpp
+++ clang/lib/Basic/FileManager.cpp
@@ -635,6 +635,12 @@
   return std::error_code();
 }
 
+void FileManager::invalidateCache(FileEntry *Entry) {
+  assert(Entry && "Cannot invalidate a NULL FileEntry");
+  FileEntriesToReread.inse

[PATCH] D126266: Mark the file entry invalid, until reread. Invalidate SLocEntry cache, readd it on reread. Do not use translateFile, because it pulls in parts of the pch.

2022-06-20 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak updated this revision to Diff 438541.
tapaswenipathak added a comment.

fix for build failure:

  cmdline: git reset --hard
  stderr: 'fatal: Unable to create 
'/var/lib/buildkite-agent/builds/llvm-project-fork/.git/index.lock': File 
exists.

https://buildkite.com/llvm-project/diff-checks/builds/110904#01818451-4c05-49bb-9f93-ea6d0480f9ec

could be temporary, or could be because i didn't run a git pull for a few days.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126266/new/

https://reviews.llvm.org/D126266

Files:
  clang/include/clang/Basic/FileEntry.h
  clang/include/clang/Basic/FileManager.h
  clang/include/clang/Basic/SourceManager.h
  clang/lib/Basic/FileManager.cpp
  clang/lib/Basic/SourceManager.cpp
  clang/lib/Lex/PPDirectives.cpp
  clang/unittests/Basic/FileManagerTest.cpp
  clang/unittests/Basic/SourceManagerTest.cpp

Index: clang/unittests/Basic/SourceManagerTest.cpp
===
--- clang/unittests/Basic/SourceManagerTest.cpp
+++ clang/unittests/Basic/SourceManagerTest.cpp
@@ -26,6 +26,14 @@
 
 using namespace clang;
 
+void clang::SourceManagerTestHelper(SourceManager &SourceMgr, FileManager &FileMgr) {
+  FileID mainFileID = SourceMgr.getMainFileID();
+  const SrcMgr::ContentCache *Cache =
+&(SourceMgr.getSLocEntry(mainFileID).getFile().getContentCache());
+  //EXPECT_FALSE(Cache->Buffer);
+  ASSERT_TRUE(FileMgr.FileEntriesToReread.empty());
+}
+
 namespace {
 
 // The test fixture.
@@ -51,6 +59,36 @@
   IntrusiveRefCntPtr Target;
 };
 
+// Test for invalidate cache success, making the file entry invalid, until
+// reread
+TEST_F(SourceManagerTest, invalidateCacheSuccess) {
+  const char *Source = "int x;";
+
+  std::unique_ptr Buf =
+  llvm::MemoryBuffer::getMemBuffer(Source);
+  const FileEntry *SourceFile =
+  FileMgr.getVirtualFile("mainFile.cpp", Buf->getBufferSize(), 0);
+
+  FileID mainFileID = SourceMgr.createFileID(std::move(Buf));
+  SourceMgr.overrideFileContents(SourceFile, std::move(Buf));
+  SourceMgr.setMainFileID(mainFileID);
+
+  SourceMgr.invalidateCache(mainFileID);
+
+  EXPECT_FALSE(SourceFile->isValid());
+  EXPECT_FALSE(mainFileID.isInvalid());
+
+  const SrcMgr::ContentCache *Cache =
+&(SourceMgr.getSLocEntry(mainFileID).getFile().getContentCache());
+
+  EXPECT_EQ(SourceMgr.getNonBuiltinFilenameForID(mainFileID), None);
+  //EXPECT_EQ(SourceMgr.getBufferDataIfLoaded(mainFileID), None);
+
+  EXPECT_FALSE(Cache->IsBufferInvalid);
+
+  SourceManagerTestHelper(SourceMgr, FileMgr);
+}
+
 TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
   const char *source =
 "#define M(x) [x]\n"
Index: clang/unittests/Basic/FileManagerTest.cpp
===
--- clang/unittests/Basic/FileManagerTest.cpp
+++ clang/unittests/Basic/FileManagerTest.cpp
@@ -18,6 +18,10 @@
 using namespace llvm;
 using namespace clang;
 
+void clang::FileManagerTestHelper(FileManager &manager){
+  ASSERT_TRUE(!manager.FileEntriesToReread.empty());
+}
+
 namespace {
 
 // Used to create a fake file system for running the tests with such
@@ -99,6 +103,29 @@
   FileManager manager;
 };
 
+// If file entry valid, mark the file entry invalid, until reread.
+TEST_F(FileManagerTest, invalidateCacheSuccess) {
+  auto statCache = std::make_unique();
+  statCache->InjectFile("file.cpp", 42);
+
+  manager.setStatCache(std::move(statCache));
+  manager.getVirtualFile("file.cpp", 100, 0);
+  auto file = manager.getFile("file.cpp");
+
+  // Check for file null assertion success
+  //manager.invalidateCache(NULL);
+
+  auto FileRef = manager.getFileRef("file.cpp");
+
+  ASSERT_FALSE(!FileRef);
+
+  FileEntry *FileEntryObj = const_cast(&FileRef->getFileEntry());
+  manager.invalidateCache(FileEntryObj);
+  FileManagerTestHelper(manager);
+
+  ASSERT_FALSE(FileEntryObj->isValid());
+}
+
 // When a virtual file is added, its getDir() field has correct name.
 TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
   FileEntryRef file = manager.getVirtualFileRef("foo.cpp", 42, 0);
Index: clang/lib/Lex/PPDirectives.cpp
===
--- clang/lib/Lex/PPDirectives.cpp
+++ clang/lib/Lex/PPDirectives.cpp
@@ -821,7 +821,8 @@
 ConstSearchDirIterator *CurDirArg, SmallVectorImpl *SearchPath,
 SmallVectorImpl *RelativePath,
 ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped,
-bool *IsFrameworkFound, bool SkipCache) {
+bool *IsFrameworkFound, bool SkipCache, bool OpenFile,
+   bool CacheFailures) {
   ConstSearchDirIterator CurDirLocal = nullptr;
   ConstSearchDirIterator &CurDir = CurDirArg ? *CurDirArg : CurDirLocal;
 
@@ -900,7 +901,7 @@
   Optional FE = HeaderInfo.LookupFile(
   Filename, FilenameLoc, isAngled, FromDir, &CurDir, Includers, SearchPath,
   RelativePath, RequestingModule, SuggestedModule, IsMapped,
-  IsFrameworkFound, 

[PATCH] D126266: Mark the file entry invalid, until reread. Invalidate SLocEntry cache, readd it on reread. Do not use translateFile, because it pulls in parts of the pch.

2022-06-20 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak updated this revision to Diff 438550.
tapaswenipathak added a comment.

omit an unrelated change.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126266/new/

https://reviews.llvm.org/D126266

Files:
  clang/include/clang/Basic/FileEntry.h
  clang/include/clang/Basic/FileManager.h
  clang/include/clang/Basic/SourceManager.h
  clang/lib/Basic/FileManager.cpp
  clang/lib/Basic/SourceManager.cpp
  clang/unittests/Basic/FileManagerTest.cpp
  clang/unittests/Basic/SourceManagerTest.cpp

Index: clang/unittests/Basic/SourceManagerTest.cpp
===
--- clang/unittests/Basic/SourceManagerTest.cpp
+++ clang/unittests/Basic/SourceManagerTest.cpp
@@ -26,6 +26,14 @@
 
 using namespace clang;
 
+void clang::SourceManagerTestHelper(SourceManager &SourceMgr, FileManager &FileMgr) {
+  FileID mainFileID = SourceMgr.getMainFileID();
+  const SrcMgr::ContentCache *Cache =
+&(SourceMgr.getSLocEntry(mainFileID).getFile().getContentCache());
+  //EXPECT_FALSE(Cache->Buffer);
+  ASSERT_TRUE(FileMgr.FileEntriesToReread.empty());
+}
+
 namespace {
 
 // The test fixture.
@@ -51,6 +59,36 @@
   IntrusiveRefCntPtr Target;
 };
 
+// Test for invalidate cache success, making the file entry invalid, until
+// reread
+TEST_F(SourceManagerTest, invalidateCacheSuccess) {
+  const char *Source = "int x;";
+
+  std::unique_ptr Buf =
+  llvm::MemoryBuffer::getMemBuffer(Source);
+  const FileEntry *SourceFile =
+  FileMgr.getVirtualFile("mainFile.cpp", Buf->getBufferSize(), 0);
+
+  FileID mainFileID = SourceMgr.createFileID(std::move(Buf));
+  SourceMgr.overrideFileContents(SourceFile, std::move(Buf));
+  SourceMgr.setMainFileID(mainFileID);
+
+  SourceMgr.invalidateCache(mainFileID);
+
+  EXPECT_FALSE(SourceFile->isValid());
+  EXPECT_FALSE(mainFileID.isInvalid());
+
+  const SrcMgr::ContentCache *Cache =
+&(SourceMgr.getSLocEntry(mainFileID).getFile().getContentCache());
+
+  EXPECT_EQ(SourceMgr.getNonBuiltinFilenameForID(mainFileID), None);
+  //EXPECT_EQ(SourceMgr.getBufferDataIfLoaded(mainFileID), None);
+
+  EXPECT_FALSE(Cache->IsBufferInvalid);
+
+  SourceManagerTestHelper(SourceMgr, FileMgr);
+}
+
 TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
   const char *source =
 "#define M(x) [x]\n"
Index: clang/unittests/Basic/FileManagerTest.cpp
===
--- clang/unittests/Basic/FileManagerTest.cpp
+++ clang/unittests/Basic/FileManagerTest.cpp
@@ -18,6 +18,10 @@
 using namespace llvm;
 using namespace clang;
 
+void clang::FileManagerTestHelper(FileManager &manager){
+  ASSERT_TRUE(!manager.FileEntriesToReread.empty());
+}
+
 namespace {
 
 // Used to create a fake file system for running the tests with such
@@ -99,6 +103,29 @@
   FileManager manager;
 };
 
+// If file entry valid, mark the file entry invalid, until reread.
+TEST_F(FileManagerTest, invalidateCacheSuccess) {
+  auto statCache = std::make_unique();
+  statCache->InjectFile("file.cpp", 42);
+
+  manager.setStatCache(std::move(statCache));
+  manager.getVirtualFile("file.cpp", 100, 0);
+  auto file = manager.getFile("file.cpp");
+
+  // Check for file null assertion success
+  //manager.invalidateCache(NULL);
+
+  auto FileRef = manager.getFileRef("file.cpp");
+
+  ASSERT_FALSE(!FileRef);
+
+  FileEntry *FileEntryObj = const_cast(&FileRef->getFileEntry());
+  manager.invalidateCache(FileEntryObj);
+  FileManagerTestHelper(manager);
+
+  ASSERT_FALSE(FileEntryObj->isValid());
+}
+
 // When a virtual file is added, its getDir() field has correct name.
 TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
   FileEntryRef file = manager.getVirtualFileRef("foo.cpp", 42, 0);
Index: clang/lib/Basic/SourceManager.cpp
===
--- clang/lib/Basic/SourceManager.cpp
+++ clang/lib/Basic/SourceManager.cpp
@@ -358,6 +358,25 @@
   return false;
 }
 
+void SourceManager::invalidateCache(FileID FID) {
+  const FileEntry* Entry = getFileEntryForID(FID);
+  if (!Entry)
+return;
+  if (ContentCache *&E = FileInfos[Entry]) {
+E->setBuffer(nullptr);
+E = 0;
+  }
+  if (!FID.isInvalid()) {
+const SrcMgr::SLocEntry& SLocE = getSLocEntry(FID);
+if (SLocE.isFile()) {
+  SrcMgr::ContentCache& CC =
+const_cast(SLocE.getFile().getContentCache());
+  CC.setBuffer(nullptr);
+}
+  }
+  getFileManager().invalidateCache(const_cast(Entry));
+}
+
 void SourceManager::initializeForReplay(const SourceManager &Old) {
   assert(MainFileID.isInvalid() && "expected uninitialized SourceManager");
 
Index: clang/lib/Basic/FileManager.cpp
===
--- clang/lib/Basic/FileManager.cpp
+++ clang/lib/Basic/FileManager.cpp
@@ -635,6 +635,12 @@
   return std::error_code();
 }
 
+void FileManager::invalidateCache(FileEntry *Entry) {
+  assert(Entry && "Cann

[PATCH] D126266: Mark the file entry invalid, until reread. Invalidate SLocEntry cache, readd it on reread. Do not use translateFile, because it pulls in parts of the pch.

2022-06-21 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak updated this revision to Diff 438597.
tapaswenipathak added a comment.

git clang-format HEAD~1


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126266/new/

https://reviews.llvm.org/D126266

Files:
  clang/include/clang/Basic/FileEntry.h
  clang/include/clang/Basic/FileManager.h
  clang/include/clang/Basic/SourceManager.h
  clang/lib/Basic/FileManager.cpp
  clang/lib/Basic/SourceManager.cpp
  clang/unittests/Basic/FileManagerTest.cpp
  clang/unittests/Basic/SourceManagerTest.cpp

Index: clang/unittests/Basic/SourceManagerTest.cpp
===
--- clang/unittests/Basic/SourceManagerTest.cpp
+++ clang/unittests/Basic/SourceManagerTest.cpp
@@ -26,6 +26,15 @@
 
 using namespace clang;
 
+void clang::SourceManagerTestHelper(SourceManager &SourceMgr,
+FileManager &FileMgr) {
+  FileID mainFileID = SourceMgr.getMainFileID();
+  const SrcMgr::ContentCache *Cache =
+  &(SourceMgr.getSLocEntry(mainFileID).getFile().getContentCache());
+  // EXPECT_FALSE(Cache->Buffer);
+  ASSERT_TRUE(FileMgr.FileEntriesToReread.empty());
+}
+
 namespace {
 
 // The test fixture.
@@ -51,6 +60,36 @@
   IntrusiveRefCntPtr Target;
 };
 
+// Test for invalidate cache success, making the file entry invalid, until
+// reread
+TEST_F(SourceManagerTest, invalidateCacheSuccess) {
+  const char *Source = "int x;";
+
+  std::unique_ptr Buf =
+  llvm::MemoryBuffer::getMemBuffer(Source);
+  const FileEntry *SourceFile =
+  FileMgr.getVirtualFile("mainFile.cpp", Buf->getBufferSize(), 0);
+
+  FileID mainFileID = SourceMgr.createFileID(std::move(Buf));
+  SourceMgr.overrideFileContents(SourceFile, std::move(Buf));
+  SourceMgr.setMainFileID(mainFileID);
+
+  SourceMgr.invalidateCache(mainFileID);
+
+  EXPECT_FALSE(SourceFile->isValid());
+  EXPECT_FALSE(mainFileID.isInvalid());
+
+  const SrcMgr::ContentCache *Cache =
+  &(SourceMgr.getSLocEntry(mainFileID).getFile().getContentCache());
+
+  EXPECT_EQ(SourceMgr.getNonBuiltinFilenameForID(mainFileID), None);
+  // EXPECT_EQ(SourceMgr.getBufferDataIfLoaded(mainFileID), None);
+
+  EXPECT_FALSE(Cache->IsBufferInvalid);
+
+  SourceManagerTestHelper(SourceMgr, FileMgr);
+}
+
 TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
   const char *source =
 "#define M(x) [x]\n"
Index: clang/unittests/Basic/FileManagerTest.cpp
===
--- clang/unittests/Basic/FileManagerTest.cpp
+++ clang/unittests/Basic/FileManagerTest.cpp
@@ -18,6 +18,10 @@
 using namespace llvm;
 using namespace clang;
 
+void clang::FileManagerTestHelper(FileManager &manager) {
+  ASSERT_TRUE(!manager.FileEntriesToReread.empty());
+}
+
 namespace {
 
 // Used to create a fake file system for running the tests with such
@@ -99,6 +103,29 @@
   FileManager manager;
 };
 
+// If file entry valid, mark the file entry invalid, until reread.
+TEST_F(FileManagerTest, invalidateCacheSuccess) {
+  auto statCache = std::make_unique();
+  statCache->InjectFile("file.cpp", 42);
+
+  manager.setStatCache(std::move(statCache));
+  manager.getVirtualFile("file.cpp", 100, 0);
+  auto file = manager.getFile("file.cpp");
+
+  // Check for file null assertion success
+  // manager.invalidateCache(NULL);
+
+  auto FileRef = manager.getFileRef("file.cpp");
+
+  ASSERT_FALSE(!FileRef);
+
+  FileEntry *FileEntryObj = const_cast(&FileRef->getFileEntry());
+  manager.invalidateCache(FileEntryObj);
+  FileManagerTestHelper(manager);
+
+  ASSERT_FALSE(FileEntryObj->isValid());
+}
+
 // When a virtual file is added, its getDir() field has correct name.
 TEST_F(FileManagerTest, getVirtualFileSetsTheDirFieldCorrectly) {
   FileEntryRef file = manager.getVirtualFileRef("foo.cpp", 42, 0);
Index: clang/lib/Basic/SourceManager.cpp
===
--- clang/lib/Basic/SourceManager.cpp
+++ clang/lib/Basic/SourceManager.cpp
@@ -358,6 +358,25 @@
   return false;
 }
 
+void SourceManager::invalidateCache(FileID FID) {
+  const FileEntry *Entry = getFileEntryForID(FID);
+  if (!Entry)
+return;
+  if (ContentCache *&E = FileInfos[Entry]) {
+E->setBuffer(nullptr);
+E = 0;
+  }
+  if (!FID.isInvalid()) {
+const SrcMgr::SLocEntry &SLocE = getSLocEntry(FID);
+if (SLocE.isFile()) {
+  SrcMgr::ContentCache &CC =
+  const_cast(SLocE.getFile().getContentCache());
+  CC.setBuffer(nullptr);
+}
+  }
+  getFileManager().invalidateCache(const_cast(Entry));
+}
+
 void SourceManager::initializeForReplay(const SourceManager &Old) {
   assert(MainFileID.isInvalid() && "expected uninitialized SourceManager");
 
Index: clang/lib/Basic/FileManager.cpp
===
--- clang/lib/Basic/FileManager.cpp
+++ clang/lib/Basic/FileManager.cpp
@@ -635,6 +635,12 @@
   return std::error_code();
 }
 
+void FileManager::invalidateCache

[PATCH] D128257: invalidateCache in addModule and removeModules

2022-06-21 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak created this revision.
tapaswenipathak added a reviewer: v.g.vassilev.
tapaswenipathak added a project: clang.
Herald added a project: All.
tapaswenipathak requested review of this revision.
Herald added a subscriber: cfe-commits.

Ref: https://github.com/vgvassilev/clang/commit/758e42e823.

It can have a test adding `FileManagerTestHelper` as a `friend` function and 
checking on a few variables. Should it be added?

Pre: https://reviews.llvm.org/D126266.

Authored-by: Vassil Vassilev 
Submitted-by: Tapasweni Pathak 


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128257

Files:
  clang/lib/Serialization/ModuleManager.cpp


Index: clang/lib/Serialization/ModuleManager.cpp
===
--- clang/lib/Serialization/ModuleManager.cpp
+++ clang/lib/Serialization/ModuleManager.cpp
@@ -232,9 +232,15 @@
   // Read the signature eagerly now so that we can check it.  Avoid calling
   // ReadSignature unless there's something to check though.
   if (ExpectedSignature && checkSignature(ReadSignature(NewModule->Data),
-  ExpectedSignature, ErrorStr))
+  ExpectedSignature, ErrorStr)) {
+// Try to remove the buffer.  If it can't be removed, then it was already
+// validated by this process.
+if (!getModuleCache().tryToDropPCM(NewModule->FileName)) {
+  const FileEntry *FE = NewModule->File;
+  FileMgr.invalidateCache(const_cast(FE));
+}
 return OutOfDate;
-
+  }
   // We're keeping this module.  Store it everywhere.
   Module = Modules[Entry] = NewModule.get();
 
@@ -284,6 +290,8 @@
   for (ModuleIterator victim = First; victim != Last; ++victim) {
 Modules.erase(victim->File);
 
+const FileEntry *FE = victim->File;
+FileMgr.invalidateCache(const_cast(FE));
 if (modMap) {
   StringRef ModuleName = victim->ModuleName;
   if (Module *mod = modMap->findModule(ModuleName)) {


Index: clang/lib/Serialization/ModuleManager.cpp
===
--- clang/lib/Serialization/ModuleManager.cpp
+++ clang/lib/Serialization/ModuleManager.cpp
@@ -232,9 +232,15 @@
   // Read the signature eagerly now so that we can check it.  Avoid calling
   // ReadSignature unless there's something to check though.
   if (ExpectedSignature && checkSignature(ReadSignature(NewModule->Data),
-  ExpectedSignature, ErrorStr))
+  ExpectedSignature, ErrorStr)) {
+// Try to remove the buffer.  If it can't be removed, then it was already
+// validated by this process.
+if (!getModuleCache().tryToDropPCM(NewModule->FileName)) {
+  const FileEntry *FE = NewModule->File;
+  FileMgr.invalidateCache(const_cast(FE));
+}
 return OutOfDate;
-
+  }
   // We're keeping this module.  Store it everywhere.
   Module = Modules[Entry] = NewModule.get();
 
@@ -284,6 +290,8 @@
   for (ModuleIterator victim = First; victim != Last; ++victim) {
 Modules.erase(victim->File);
 
+const FileEntry *FE = victim->File;
+FileMgr.invalidateCache(const_cast(FE));
 if (modMap) {
   StringRef ModuleName = victim->ModuleName;
   if (Module *mod = modMap->findModule(ModuleName)) {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D128319: Survive #pragma once from virtual file.

2022-06-21 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak created this revision.
tapaswenipathak added a reviewer: v.g.vassilev.
tapaswenipathak added a project: clang.
Herald added a project: All.
tapaswenipathak requested review of this revision.
Herald added a subscriber: cfe-commits.

Add check before marking file as a once-only file.

Ref: https://github.com/vgvassilev/clang/commit/728e1b3286.

Authored-by: Vassil Vassilev 
Submitted-by: Tapasweni Pathak 


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D128319

Files:
  clang/lib/Lex/Pragma.cpp


Index: clang/lib/Lex/Pragma.cpp
===
--- clang/lib/Lex/Pragma.cpp
+++ clang/lib/Lex/Pragma.cpp
@@ -412,9 +412,11 @@
 return;
   }
 
-  // Get the current file lexer we're looking at.  Ignore _Pragma 'files' etc.
-  // Mark the file as a once-only file now.
-  HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
+  if (getCurrentFileLexer()->getFileEntry()) {
+// Get the current file lexer we're looking at.  Ignore _Pragma 'files' 
etc.
+// Mark the file as a once-only file now.
+HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
+  }
 }
 
 void Preprocessor::HandlePragmaMark(Token &MarkTok) {


Index: clang/lib/Lex/Pragma.cpp
===
--- clang/lib/Lex/Pragma.cpp
+++ clang/lib/Lex/Pragma.cpp
@@ -412,9 +412,11 @@
 return;
   }
 
-  // Get the current file lexer we're looking at.  Ignore _Pragma 'files' etc.
-  // Mark the file as a once-only file now.
-  HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
+  if (getCurrentFileLexer()->getFileEntry()) {
+// Get the current file lexer we're looking at.  Ignore _Pragma 'files' etc.
+// Mark the file as a once-only file now.
+HeaderInfo.MarkFileIncludeOnce(getCurrentFileLexer()->getFileEntry());
+  }
 }
 
 void Preprocessor::HandlePragmaMark(Token &MarkTok) {
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D128257: invalidateCache in addModule and removeModules

2022-06-25 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak added a comment.

https://github.com/llvm/llvm-project/commit/ca39214f4e9fddf247e880ef9562865be850fce6#diff-a59fa24a712b79c57b4d922d0d407b8f63a9a88fa79ba0d85b0063e1a2a6fda3L157

closing, now.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D128257/new/

https://reviews.llvm.org/D128257

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D126183: Implement soft reset of the diagnostics engine.

2022-05-28 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak updated this revision to Diff 432711.
tapaswenipathak added a comment.

Addresses review comment by @rsmith: https://reviews.llvm.org/D126183#3534846.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126183/new/

https://reviews.llvm.org/D126183

Files:
  include/clang/Basic/Diagnostic.h
  lib/Basic/Diagnostic.cpp
  test/Interpreter/error-recovery-pragmas.cpp
  tools/clang-repl/ClangRepl.cpp
  unittests/Basic/DiagnosticTest.cpp

Index: unittests/Basic/DiagnosticTest.cpp
===
--- unittests/Basic/DiagnosticTest.cpp
+++ unittests/Basic/DiagnosticTest.cpp
@@ -14,8 +14,18 @@
 using namespace llvm;
 using namespace clang;
 
+
 namespace {
 
+void DiagnosticsEngine::DiagnosticTestHelper() {
+  unsigned delayedDiagID = 0U;
+
+  EXPECT_EQ(this.DelayedDiagID, delayedDiagID);
+  EXPECT_FALSE(this.DiagStates.empty());
+  EXPECT_FALSE(this.DiagStatesByLoc.empty());
+  EXPECT_FALSE(this.DiagStateOnPushStack.empty());
+}
+
 // Check that DiagnosticErrorTrap works with SuppressAllDiagnostics.
 TEST(DiagnosticTest, suppressAndTrap) {
   DiagnosticsEngine Diags(new DiagnosticIDs(),
@@ -71,6 +81,32 @@
 EXPECT_EQ(Diags.getNumWarnings(), FatalsAsError);
   }
 }
+
+// Check that soft RESET works as intended
+TEST(DiagnosticTest, softReset) {
+  DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
+  new IgnoringDiagConsumer());
+
+  unsigned numWarnings = 0U, numErrors = 0U;
+
+  Diags.Reset(true);
+  // Check For ErrorOccurred and TrapNumErrorsOccurred
+  EXPECT_FALSE(Diags.hasErrorOccurred());
+  EXPECT_FALSE(Diags.hasFatalErrorOccurred());
+  EXPECT_FALSE(Diags.hasUncompilableErrorOccurred());
+  // Check for UnrecoverableErrorOccurred and TrapNumUnrecoverableErrorsOccurred
+  EXPECT_FALSE(Diags.hasUnrecoverableErrorOccurred());
+
+  EXPECT_EQ(Diags.getNumWarnings(), numWarnings);
+  EXPECT_EQ(Diags.getNumErrors(), numErrors);
+
+  // Check for private variables of DiagnosticsEngine differentiating soft reset
+  Diags.DiagnosticTestHelper();
+
+  EXPECT_FALSE(Diags.isDiagnosticInFlight());
+  EXPECT_TRUE(Diags.isLastDiagnosticIgnored());
+}
+
 TEST(DiagnosticTest, diagnosticError) {
   DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
   new IgnoringDiagConsumer());
Index: tools/clang-repl/ClangRepl.cpp
===
--- tools/clang-repl/ClangRepl.cpp
+++ tools/clang-repl/ClangRepl.cpp
@@ -86,14 +86,17 @@
   llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
   }
 
+  bool hadErrors = false;
   if (OptInputs.empty()) {
 llvm::LineEditor LE("clang-repl");
 // FIXME: Add LE.setListCompleter
 while (llvm::Optional Line = LE.readLine()) {
   if (*Line == "quit")
 break;
-  if (auto Err = Interp->ParseAndExecute(*Line))
+  if (auto Err = Interp->ParseAndExecute(*Line)) {
 llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
+hadErrors = true;
+  }
 }
   }
 
@@ -104,5 +107,5 @@
 
   llvm::llvm_shutdown();
 
-  return 0;
+  return hadErrors;
 }
Index: test/Interpreter/error-recovery-pragmas.cpp
===
--- /dev/null
+++ test/Interpreter/error-recovery-pragmas.cpp
@@ -0,0 +1,6 @@
+// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -verify
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wmultichar"
+// Reset should not delete #pragmas
+error; // expected-error {{use of undeclared identifier}}
+void no_diag_multichar(void) { char c = (char)'ab'; }
Index: lib/Basic/Diagnostic.cpp
===
--- lib/Basic/Diagnostic.cpp
+++ lib/Basic/Diagnostic.cpp
@@ -130,7 +130,7 @@
   return true;
 }
 
-void DiagnosticsEngine::Reset() {
+void DiagnosticsEngine::Reset(bool soft /*=false*/) {
   ErrorOccurred = false;
   UncompilableErrorOccurred = false;
   FatalErrorOccurred = false;
@@ -145,15 +145,17 @@
   LastDiagLevel = DiagnosticIDs::Ignored;
   DelayedDiagID = 0;
 
-  // Clear state related to #pragma diagnostic.
-  DiagStates.clear();
-  DiagStatesByLoc.clear();
-  DiagStateOnPushStack.clear();
+  if (!soft) {
+// Clear state related to #pragma diagnostic.
+DiagStates.clear();
+DiagStatesByLoc.clear();
+DiagStateOnPushStack.clear();
 
-  // Create a DiagState and DiagStatePoint representing diagnostic changes
-  // through command-line.
-  DiagStates.emplace_back();
-  DiagStatesByLoc.appendFirst(&DiagStates.back());
+// Create a DiagState and DiagStatePoint representing diagnostic changes
+// through command-line.
+DiagStates.emplace_back();
+DiagStatesByLoc.appendFirst(&DiagStates.back());
+  }
 }
 
 void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Index: include/clang/Basic/Diagnostic.h

[PATCH] D126183: Implement soft reset of the diagnostics engine.

2022-05-28 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak updated this revision to Diff 432712.
tapaswenipathak added a comment.

runs clang-format on the diff.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126183/new/

https://reviews.llvm.org/D126183

Files:
  include/clang/Basic/Diagnostic.h
  lib/Basic/Diagnostic.cpp
  test/Interpreter/error-recovery-pragmas.cpp
  tools/clang-repl/ClangRepl.cpp
  unittests/Basic/DiagnosticTest.cpp

Index: unittests/Basic/DiagnosticTest.cpp
===
--- unittests/Basic/DiagnosticTest.cpp
+++ unittests/Basic/DiagnosticTest.cpp
@@ -16,6 +16,15 @@
 
 namespace {
 
+void DiagnosticsEngine::DiagnosticTestHelper() {
+  unsigned delayedDiagID = 0U;
+
+  EXPECT_EQ(this.DelayedDiagID, delayedDiagID);
+  EXPECT_FALSE(this.DiagStates.empty());
+  EXPECT_FALSE(this.DiagStatesByLoc.empty());
+  EXPECT_FALSE(this.DiagStateOnPushStack.empty());
+}
+
 // Check that DiagnosticErrorTrap works with SuppressAllDiagnostics.
 TEST(DiagnosticTest, suppressAndTrap) {
   DiagnosticsEngine Diags(new DiagnosticIDs(),
@@ -71,6 +80,32 @@
 EXPECT_EQ(Diags.getNumWarnings(), FatalsAsError);
   }
 }
+
+// Check that soft RESET works as intended
+TEST(DiagnosticTest, softReset) {
+  DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
+  new IgnoringDiagConsumer());
+
+  unsigned numWarnings = 0U, numErrors = 0U;
+
+  Diags.Reset(true);
+  // Check For ErrorOccurred and TrapNumErrorsOccurred
+  EXPECT_FALSE(Diags.hasErrorOccurred());
+  EXPECT_FALSE(Diags.hasFatalErrorOccurred());
+  EXPECT_FALSE(Diags.hasUncompilableErrorOccurred());
+  // Check for UnrecoverableErrorOccurred and TrapNumUnrecoverableErrorsOccurred
+  EXPECT_FALSE(Diags.hasUnrecoverableErrorOccurred());
+
+  EXPECT_EQ(Diags.getNumWarnings(), numWarnings);
+  EXPECT_EQ(Diags.getNumErrors(), numErrors);
+
+  // Check for private variables of DiagnosticsEngine differentiating soft reset
+  Diags.DiagnosticTestHelper();
+
+  EXPECT_FALSE(Diags.isDiagnosticInFlight());
+  EXPECT_TRUE(Diags.isLastDiagnosticIgnored());
+}
+
 TEST(DiagnosticTest, diagnosticError) {
   DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
   new IgnoringDiagConsumer());
Index: tools/clang-repl/ClangRepl.cpp
===
--- tools/clang-repl/ClangRepl.cpp
+++ tools/clang-repl/ClangRepl.cpp
@@ -86,14 +86,17 @@
   llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
   }
 
+  bool hadErrors = false;
   if (OptInputs.empty()) {
 llvm::LineEditor LE("clang-repl");
 // FIXME: Add LE.setListCompleter
 while (llvm::Optional Line = LE.readLine()) {
   if (*Line == "quit")
 break;
-  if (auto Err = Interp->ParseAndExecute(*Line))
+  if (auto Err = Interp->ParseAndExecute(*Line)) {
 llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
+hadErrors = true;
+  }
 }
   }
 
@@ -104,5 +107,5 @@
 
   llvm::llvm_shutdown();
 
-  return 0;
+  return hadErrors;
 }
Index: test/Interpreter/error-recovery-pragmas.cpp
===
--- /dev/null
+++ test/Interpreter/error-recovery-pragmas.cpp
@@ -0,0 +1,6 @@
+// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -verify
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wmultichar"
+// Reset should not delete #pragmas
+error; // expected-error {{use of undeclared identifier}}
+void no_diag_multichar(void) { char c = (char)'ab'; }
Index: lib/Basic/Diagnostic.cpp
===
--- lib/Basic/Diagnostic.cpp
+++ lib/Basic/Diagnostic.cpp
@@ -130,7 +130,7 @@
   return true;
 }
 
-void DiagnosticsEngine::Reset() {
+void DiagnosticsEngine::Reset(bool soft /*=false*/) {
   ErrorOccurred = false;
   UncompilableErrorOccurred = false;
   FatalErrorOccurred = false;
@@ -145,15 +145,17 @@
   LastDiagLevel = DiagnosticIDs::Ignored;
   DelayedDiagID = 0;
 
-  // Clear state related to #pragma diagnostic.
-  DiagStates.clear();
-  DiagStatesByLoc.clear();
-  DiagStateOnPushStack.clear();
+  if (!soft) {
+// Clear state related to #pragma diagnostic.
+DiagStates.clear();
+DiagStatesByLoc.clear();
+DiagStateOnPushStack.clear();
 
-  // Create a DiagState and DiagStatePoint representing diagnostic changes
-  // through command-line.
-  DiagStates.emplace_back();
-  DiagStatesByLoc.appendFirst(&DiagStates.back());
+// Create a DiagState and DiagStatePoint representing diagnostic changes
+// through command-line.
+DiagStates.emplace_back();
+DiagStatesByLoc.appendFirst(&DiagStates.back());
+  }
 }
 
 void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Index: include/clang/Basic/Diagnostic.h
===
--- include/clang/Basic/Diagnostic.h
+++ 

[PATCH] D126183: Implement soft reset of the diagnostics engine.

2022-05-28 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak updated this revision to Diff 432723.
tapaswenipathak added a comment.

Fixes F23230194: Screenshot 2022-05-28 at 7.18.51 PM.png 
.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126183/new/

https://reviews.llvm.org/D126183

Files:
  clang/include/clang/Basic/Diagnostic.h
  clang/lib/Basic/Diagnostic.cpp
  clang/test/Interpreter/error-recovery-pragmas.cpp
  clang/tools/clang-repl/ClangRepl.cpp
  clang/unittests/Basic/DiagnosticTest.cpp

Index: clang/unittests/Basic/DiagnosticTest.cpp
===
--- clang/unittests/Basic/DiagnosticTest.cpp
+++ clang/unittests/Basic/DiagnosticTest.cpp
@@ -14,6 +14,15 @@
 using namespace llvm;
 using namespace clang;
 
+void clang::DiagnosticsTestHelper(DiagnosticsEngine &diag) {
+  unsigned delayedDiagID = 0U;
+
+  EXPECT_EQ(diag.DelayedDiagID, delayedDiagID);
+  EXPECT_FALSE(diag.DiagStates.empty());
+  EXPECT_TRUE(diag.DiagStatesByLoc.empty());
+  EXPECT_TRUE(diag.DiagStateOnPushStack.empty());
+}
+
 namespace {
 
 // Check that DiagnosticErrorTrap works with SuppressAllDiagnostics.
@@ -71,6 +80,32 @@
 EXPECT_EQ(Diags.getNumWarnings(), FatalsAsError);
   }
 }
+
+// Check that soft RESET works as intended
+TEST(DiagnosticTest, softReset) {
+  DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
+  new IgnoringDiagConsumer());
+
+  unsigned numWarnings = 0U, numErrors = 0U;
+
+  Diags.Reset(true);
+  // Check For ErrorOccurred and TrapNumErrorsOccurred
+  EXPECT_FALSE(Diags.hasErrorOccurred());
+  EXPECT_FALSE(Diags.hasFatalErrorOccurred());
+  EXPECT_FALSE(Diags.hasUncompilableErrorOccurred());
+  // Check for UnrecoverableErrorOccurred and TrapNumUnrecoverableErrorsOccurred
+  EXPECT_FALSE(Diags.hasUnrecoverableErrorOccurred());
+
+  EXPECT_EQ(Diags.getNumWarnings(), numWarnings);
+  EXPECT_EQ(Diags.getNumErrors(), numErrors);
+
+  // Check for private variables of DiagnosticsEngine differentiating soft reset
+  DiagnosticTestHelper(Diags);
+
+  EXPECT_FALSE(Diags.isDiagnosticInFlight());
+  EXPECT_TRUE(Diags.isLastDiagnosticIgnored());
+}
+
 TEST(DiagnosticTest, diagnosticError) {
   DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
   new IgnoringDiagConsumer());
Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -86,14 +86,17 @@
   llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
   }
 
+  bool hadErrors = false;
   if (OptInputs.empty()) {
 llvm::LineEditor LE("clang-repl");
 // FIXME: Add LE.setListCompleter
 while (llvm::Optional Line = LE.readLine()) {
   if (*Line == "quit")
 break;
-  if (auto Err = Interp->ParseAndExecute(*Line))
+  if (auto Err = Interp->ParseAndExecute(*Line)) {
 llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
+hadErrors = true;
+  }
 }
   }
 
@@ -104,5 +107,5 @@
 
   llvm::llvm_shutdown();
 
-  return 0;
+  return hadErrors;
 }
Index: clang/test/Interpreter/error-recovery-pragmas.cpp
===
--- /dev/null
+++ clang/test/Interpreter/error-recovery-pragmas.cpp
@@ -0,0 +1,6 @@
+// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -verify
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wmultichar"
+// Reset should not delete #pragmas
+error; // expected-error {{use of undeclared identifier}}
+void no_diag_multichar(void) { char c = (char)'ab'; }
Index: clang/lib/Basic/Diagnostic.cpp
===
--- clang/lib/Basic/Diagnostic.cpp
+++ clang/lib/Basic/Diagnostic.cpp
@@ -130,7 +130,7 @@
   return true;
 }
 
-void DiagnosticsEngine::Reset() {
+void DiagnosticsEngine::Reset(bool soft /*=false*/) {
   ErrorOccurred = false;
   UncompilableErrorOccurred = false;
   FatalErrorOccurred = false;
@@ -145,15 +145,17 @@
   LastDiagLevel = DiagnosticIDs::Ignored;
   DelayedDiagID = 0;
 
-  // Clear state related to #pragma diagnostic.
-  DiagStates.clear();
-  DiagStatesByLoc.clear();
-  DiagStateOnPushStack.clear();
+  if (!soft) {
+// Clear state related to #pragma diagnostic.
+DiagStates.clear();
+DiagStatesByLoc.clear();
+DiagStateOnPushStack.clear();
 
-  // Create a DiagState and DiagStatePoint representing diagnostic changes
-  // through command-line.
-  DiagStates.emplace_back();
-  DiagStatesByLoc.appendFirst(&DiagStates.back());
+// Create a DiagState and DiagStatePoint representing diagnostic changes
+// through command-line.
+DiagStates.emplace_back();
+DiagStatesByLoc.appendFirst(&DiagStates.back());
+  }
 }
 
 void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Index: clang/include/

[PATCH] D126183: Implement soft reset of the diagnostics engine.

2022-05-28 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak updated this revision to Diff 432730.
tapaswenipathak added a comment.

Fixes: 
https://buildkite.com/llvm-project/premerge-checks/builds/95001#01810b0b-6313-400f-aaf0-35855916ec93

I pasted the wrong diff. sorry!

(have multiple build repository in local)

F23230857: Screenshot 2022-05-28 at 9.19.48 PM.png 



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126183/new/

https://reviews.llvm.org/D126183

Files:
  clang/include/clang/Basic/Diagnostic.h
  clang/lib/Basic/Diagnostic.cpp
  clang/test/Interpreter/error-recovery-pragmas.cpp
  clang/tools/clang-repl/ClangRepl.cpp
  clang/unittests/Basic/DiagnosticTest.cpp

Index: clang/unittests/Basic/DiagnosticTest.cpp
===
--- clang/unittests/Basic/DiagnosticTest.cpp
+++ clang/unittests/Basic/DiagnosticTest.cpp
@@ -14,6 +14,15 @@
 using namespace llvm;
 using namespace clang;
 
+void clang::DiagnosticsTestHelper(DiagnosticsEngine &diag) {
+  unsigned delayedDiagID = 0U;
+
+  EXPECT_EQ(diag.DelayedDiagID, delayedDiagID);
+  EXPECT_FALSE(diag.DiagStates.empty());
+  EXPECT_TRUE(diag.DiagStatesByLoc.empty());
+  EXPECT_TRUE(diag.DiagStateOnPushStack.empty());
+}
+
 namespace {
 
 // Check that DiagnosticErrorTrap works with SuppressAllDiagnostics.
@@ -71,6 +80,32 @@
 EXPECT_EQ(Diags.getNumWarnings(), FatalsAsError);
   }
 }
+
+// Check that soft RESET works as intended
+TEST(DiagnosticTest, softReset) {
+  DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
+  new IgnoringDiagConsumer());
+
+  unsigned numWarnings = 0U, numErrors = 0U;
+
+  Diags.Reset(true);
+  // Check For ErrorOccurred and TrapNumErrorsOccurred
+  EXPECT_FALSE(Diags.hasErrorOccurred());
+  EXPECT_FALSE(Diags.hasFatalErrorOccurred());
+  EXPECT_FALSE(Diags.hasUncompilableErrorOccurred());
+  // Check for UnrecoverableErrorOccurred and TrapNumUnrecoverableErrorsOccurred
+  EXPECT_FALSE(Diags.hasUnrecoverableErrorOccurred());
+
+  EXPECT_EQ(Diags.getNumWarnings(), numWarnings);
+  EXPECT_EQ(Diags.getNumErrors(), numErrors);
+
+  // Check for private variables of DiagnosticsEngine differentiating soft reset
+  DiagnosticsTestHelper(Diags);
+
+  EXPECT_FALSE(Diags.isDiagnosticInFlight());
+  EXPECT_TRUE(Diags.isLastDiagnosticIgnored());
+}
+
 TEST(DiagnosticTest, diagnosticError) {
   DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
   new IgnoringDiagConsumer());
Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -86,14 +86,17 @@
   llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
   }
 
+  bool hadErrors = false;
   if (OptInputs.empty()) {
 llvm::LineEditor LE("clang-repl");
 // FIXME: Add LE.setListCompleter
 while (llvm::Optional Line = LE.readLine()) {
   if (*Line == "quit")
 break;
-  if (auto Err = Interp->ParseAndExecute(*Line))
+  if (auto Err = Interp->ParseAndExecute(*Line)) {
 llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
+hadErrors = true;
+  }
 }
   }
 
@@ -104,5 +107,5 @@
 
   llvm::llvm_shutdown();
 
-  return 0;
+  return hadErrors;
 }
Index: clang/test/Interpreter/error-recovery-pragmas.cpp
===
--- /dev/null
+++ clang/test/Interpreter/error-recovery-pragmas.cpp
@@ -0,0 +1,6 @@
+// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -verify
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wmultichar"
+// Reset should not delete #pragmas
+error; // expected-error {{use of undeclared identifier}}
+void no_diag_multichar(void) { char c = (char)'ab'; }
Index: clang/lib/Basic/Diagnostic.cpp
===
--- clang/lib/Basic/Diagnostic.cpp
+++ clang/lib/Basic/Diagnostic.cpp
@@ -130,7 +130,7 @@
   return true;
 }
 
-void DiagnosticsEngine::Reset() {
+void DiagnosticsEngine::Reset(bool soft /*=false*/) {
   ErrorOccurred = false;
   UncompilableErrorOccurred = false;
   FatalErrorOccurred = false;
@@ -145,15 +145,17 @@
   LastDiagLevel = DiagnosticIDs::Ignored;
   DelayedDiagID = 0;
 
-  // Clear state related to #pragma diagnostic.
-  DiagStates.clear();
-  DiagStatesByLoc.clear();
-  DiagStateOnPushStack.clear();
+  if (!soft) {
+// Clear state related to #pragma diagnostic.
+DiagStates.clear();
+DiagStatesByLoc.clear();
+DiagStateOnPushStack.clear();
 
-  // Create a DiagState and DiagStatePoint representing diagnostic changes
-  // through command-line.
-  DiagStates.emplace_back();
-  DiagStatesByLoc.appendFirst(&DiagStates.back());
+// Create a DiagState and DiagStatePoint representing diagnostic changes
+// through command-line.
+DiagStates.

[PATCH] D126600: Allow interfaces to operate on in-memory buffers with no source location info.

2022-05-28 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak created this revision.
tapaswenipathak added a reviewer: v.g.vassilev.
tapaswenipathak added a project: clang.
Herald added a project: All.
tapaswenipathak requested review of this revision.
Herald added a subscriber: cfe-commits.

Allow interfaces to operate on in-memory buffers with no source location info.

  

This patch avoids an assert PresumedLoc::getFilename if it is invalid.

  

Add unit tests for allowing the interface to operate on in-memory buffers with 
no
source location info.

It addresses all review comments of https://reviews.llvm.org/D88780.

Ref: https://reviews.llvm.org/D126271.

  

Co-authored-by: Vassil Vassilev 


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D126600

Files:
  clang/include/clang/Basic/SourceManager.h
  clang/unittests/Basic/SourceManagerTest.cpp

Index: clang/unittests/Basic/SourceManagerTest.cpp
===
--- clang/unittests/Basic/SourceManagerTest.cpp
+++ clang/unittests/Basic/SourceManagerTest.cpp
@@ -51,6 +51,73 @@
   IntrusiveRefCntPtr Target;
 };
 
+TEST_F(SourceManagerTest, isInMemoryBuffersNoSourceLocationInfo) {
+  // Check for invalid source location for each method
+  SourceLocation LocEmpty;
+  bool isWrittenInBuiltInFileFalse = SourceMgr.isWrittenInBuiltinFile(LocEmpty);
+  bool isWrittenInCommandLineFileFalse =
+  SourceMgr.isWrittenInCommandLineFile(LocEmpty);
+  bool isWrittenInScratchSpaceFalse =
+  SourceMgr.isWrittenInScratchSpace(LocEmpty);
+
+  EXPECT_FALSE(isWrittenInBuiltInFileFalse);
+  EXPECT_FALSE(isWrittenInCommandLineFileFalse);
+  EXPECT_FALSE(isWrittenInScratchSpaceFalse);
+
+  // Check for valid source location per filename for each method
+  const char *Source = "int x";
+
+  std::unique_ptr BuiltInBuf =
+  llvm::MemoryBuffer::getMemBuffer(Source);
+  const FileEntry *BuiltInFile =
+  FileMgr.getVirtualFile("", BuiltInBuf->getBufferSize(), 0);
+  SourceMgr.overrideFileContents(BuiltInFile, std::move(BuiltInBuf));
+  FileID BuiltInFileID =
+  SourceMgr.getOrCreateFileID(BuiltInFile, SrcMgr::C_User);
+  SourceMgr.setMainFileID(BuiltInFileID);
+  SourceLocation LocBuiltIn =
+  SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
+  bool isWrittenInBuiltInFileTrue =
+  SourceMgr.isWrittenInBuiltinFile(LocBuiltIn);
+
+  std::unique_ptr CommandLineBuf =
+  llvm::MemoryBuffer::getMemBuffer(Source);
+  const FileEntry *CommandLineFile = FileMgr.getVirtualFile(
+  "", CommandLineBuf->getBufferSize(), 0);
+  SourceMgr.overrideFileContents(CommandLineFile, std::move(CommandLineBuf));
+  FileID CommandLineFileID =
+  SourceMgr.getOrCreateFileID(CommandLineFile, SrcMgr::C_User);
+  SourceMgr.setMainFileID(CommandLineFileID);
+  SourceLocation LocCommandLine =
+  SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
+  bool isWrittenInCommandLineFileTrue =
+  SourceMgr.isWrittenInCommandLineFile(LocCommandLine);
+
+  std::unique_ptr ScratchSpaceBuf =
+  llvm::MemoryBuffer::getMemBuffer(Source);
+  const FileEntry *ScratchSpaceFile = FileMgr.getVirtualFile(
+  "", ScratchSpaceBuf->getBufferSize(), 0);
+  SourceMgr.overrideFileContents(ScratchSpaceFile, std::move(ScratchSpaceBuf));
+  FileID ScratchSpaceFileID =
+  SourceMgr.getOrCreateFileID(ScratchSpaceFile, SrcMgr::C_User);
+  SourceMgr.setMainFileID(ScratchSpaceFileID);
+  SourceLocation LocScratchSpace =
+  SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
+  bool isWrittenInScratchSpaceTrue =
+  SourceMgr.isWrittenInScratchSpace(LocScratchSpace);
+
+  EXPECT_TRUE(isWrittenInBuiltInFileTrue);
+  EXPECT_TRUE(isWrittenInCommandLineFileTrue);
+  EXPECT_TRUE(isWrittenInScratchSpaceTrue);
+}
+
+TEST_F(SourceManagerTest, isInSystemHeader) {
+  // Check for invalid source location
+  SourceLocation LocEmpty;
+  bool isInSystemHeaderFalse = SourceMgr.isInSystemHeader(LocEmpty);
+  ASSERT_FALSE(isInSystemHeaderFalse);
+}
+
 TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
   const char *source =
 "#define M(x) [x]\n"
@@ -84,11 +151,11 @@
   ASSERT_EQ(tok::l_square, toks[0].getKind());
   ASSERT_EQ(tok::identifier, toks[1].getKind());
   ASSERT_EQ(tok::r_square, toks[2].getKind());
-  
+
   SourceLocation lsqrLoc = toks[0].getLocation();
   SourceLocation idLoc = toks[1].getLocation();
   SourceLocation rsqrLoc = toks[2].getLocation();
-  
+
   SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1);
   SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6);
   ASSERT_TRUE(macroExpStartLoc.isFileID());
Index: clang/include/clang/Basic/SourceManager.h
===
--- clang/include/clang/Basic/SourceManager.h
+++ clang/include/clang/Basic/SourceManager.h
@@ -1470,24 +1470,35 @@
 
   /// Returns whether \p Loc is located in a  file.
   bool isWrittenInBuiltinFile(SourceLocation Loc) const {
-StringRef Fil

[PATCH] D88780: Allow interfaces to operate on in-memory buffers with no source location info.

2022-05-28 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak commandeered this revision.
tapaswenipathak added a reviewer: reikdas.
tapaswenipathak added a comment.
Herald added a project: All.

Ref: https://reviews.llvm.org/D126600.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D88780/new/

https://reviews.llvm.org/D88780

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D88780: Allow interfaces to operate on in-memory buffers with no source location info.

2022-05-28 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak updated this revision to Diff 432746.
tapaswenipathak added a comment.

Allow interfaces to operate on in-memory buffers with no source location info.

  

This patch avoids an assert PresumedLoc::getFilename if it is invalid.

  

Add unit tests for allowing the interface to operate on in-memory buffers with 
no
source location info.

It addresses all review comments of https://reviews.llvm.org/D88780.

Ref: https://reviews.llvm.org/D126271.

  

Co-authored-by: Vassil Vassilev 


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D88780/new/

https://reviews.llvm.org/D88780

Files:
  clang/include/clang/Basic/SourceManager.h
  clang/unittests/Basic/SourceManagerTest.cpp

Index: clang/unittests/Basic/SourceManagerTest.cpp
===
--- clang/unittests/Basic/SourceManagerTest.cpp
+++ clang/unittests/Basic/SourceManagerTest.cpp
@@ -51,6 +51,73 @@
   IntrusiveRefCntPtr Target;
 };
 
+TEST_F(SourceManagerTest, isInMemoryBuffersNoSourceLocationInfo) {
+  // Check for invalid source location for each method
+  SourceLocation LocEmpty;
+  bool isWrittenInBuiltInFileFalse = SourceMgr.isWrittenInBuiltinFile(LocEmpty);
+  bool isWrittenInCommandLineFileFalse =
+  SourceMgr.isWrittenInCommandLineFile(LocEmpty);
+  bool isWrittenInScratchSpaceFalse =
+  SourceMgr.isWrittenInScratchSpace(LocEmpty);
+
+  EXPECT_FALSE(isWrittenInBuiltInFileFalse);
+  EXPECT_FALSE(isWrittenInCommandLineFileFalse);
+  EXPECT_FALSE(isWrittenInScratchSpaceFalse);
+
+  // Check for valid source location per filename for each method
+  const char *Source = "int x";
+
+  std::unique_ptr BuiltInBuf =
+  llvm::MemoryBuffer::getMemBuffer(Source);
+  const FileEntry *BuiltInFile =
+  FileMgr.getVirtualFile("", BuiltInBuf->getBufferSize(), 0);
+  SourceMgr.overrideFileContents(BuiltInFile, std::move(BuiltInBuf));
+  FileID BuiltInFileID =
+  SourceMgr.getOrCreateFileID(BuiltInFile, SrcMgr::C_User);
+  SourceMgr.setMainFileID(BuiltInFileID);
+  SourceLocation LocBuiltIn =
+  SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
+  bool isWrittenInBuiltInFileTrue =
+  SourceMgr.isWrittenInBuiltinFile(LocBuiltIn);
+
+  std::unique_ptr CommandLineBuf =
+  llvm::MemoryBuffer::getMemBuffer(Source);
+  const FileEntry *CommandLineFile = FileMgr.getVirtualFile(
+  "", CommandLineBuf->getBufferSize(), 0);
+  SourceMgr.overrideFileContents(CommandLineFile, std::move(CommandLineBuf));
+  FileID CommandLineFileID =
+  SourceMgr.getOrCreateFileID(CommandLineFile, SrcMgr::C_User);
+  SourceMgr.setMainFileID(CommandLineFileID);
+  SourceLocation LocCommandLine =
+  SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
+  bool isWrittenInCommandLineFileTrue =
+  SourceMgr.isWrittenInCommandLineFile(LocCommandLine);
+
+  std::unique_ptr ScratchSpaceBuf =
+  llvm::MemoryBuffer::getMemBuffer(Source);
+  const FileEntry *ScratchSpaceFile = FileMgr.getVirtualFile(
+  "", ScratchSpaceBuf->getBufferSize(), 0);
+  SourceMgr.overrideFileContents(ScratchSpaceFile, std::move(ScratchSpaceBuf));
+  FileID ScratchSpaceFileID =
+  SourceMgr.getOrCreateFileID(ScratchSpaceFile, SrcMgr::C_User);
+  SourceMgr.setMainFileID(ScratchSpaceFileID);
+  SourceLocation LocScratchSpace =
+  SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID());
+  bool isWrittenInScratchSpaceTrue =
+  SourceMgr.isWrittenInScratchSpace(LocScratchSpace);
+
+  EXPECT_TRUE(isWrittenInBuiltInFileTrue);
+  EXPECT_TRUE(isWrittenInCommandLineFileTrue);
+  EXPECT_TRUE(isWrittenInScratchSpaceTrue);
+}
+
+TEST_F(SourceManagerTest, isInSystemHeader) {
+  // Check for invalid source location
+  SourceLocation LocEmpty;
+  bool isInSystemHeaderFalse = SourceMgr.isInSystemHeader(LocEmpty);
+  ASSERT_FALSE(isInSystemHeaderFalse);
+}
+
 TEST_F(SourceManagerTest, isBeforeInTranslationUnit) {
   const char *source =
 "#define M(x) [x]\n"
@@ -84,11 +151,11 @@
   ASSERT_EQ(tok::l_square, toks[0].getKind());
   ASSERT_EQ(tok::identifier, toks[1].getKind());
   ASSERT_EQ(tok::r_square, toks[2].getKind());
-  
+
   SourceLocation lsqrLoc = toks[0].getLocation();
   SourceLocation idLoc = toks[1].getLocation();
   SourceLocation rsqrLoc = toks[2].getLocation();
-  
+
   SourceLocation macroExpStartLoc = SourceMgr.translateLineCol(mainFileID, 2, 1);
   SourceLocation macroExpEndLoc = SourceMgr.translateLineCol(mainFileID, 2, 6);
   ASSERT_TRUE(macroExpStartLoc.isFileID());
Index: clang/include/clang/Basic/SourceManager.h
===
--- clang/include/clang/Basic/SourceManager.h
+++ clang/include/clang/Basic/SourceManager.h
@@ -1470,24 +1470,35 @@
 
   /// Returns whether \p Loc is located in a  file.
   bool isWrittenInBuiltinFile(SourceLocation Loc) const {
-StringRef Filename(getPresumedLoc(Loc).getFilename());
+PresumedLoc Presumed = getPresumedLoc(Loc);
+   

[PATCH] D126600: Allow interfaces to operate on in-memory buffers with no source location info.

2022-05-28 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak abandoned this revision.
tapaswenipathak added a comment.

Ref: dup: https://reviews.llvm.org/D88780.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126600/new/

https://reviews.llvm.org/D126600

___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D126183: Implement soft reset of the diagnostics engine.

2022-05-28 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak updated this revision to Diff 432747.
tapaswenipathak added a comment.

runs `make check-clang`, `check-clang-tools`, `check-all`.

prev ran: `make check-clang-unit`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126183/new/

https://reviews.llvm.org/D126183

Files:
  clang/include/clang/Basic/Diagnostic.h
  clang/lib/Basic/Diagnostic.cpp
  clang/test/Interpreter/error-recovery-pragmas.cpp
  clang/tools/clang-repl/ClangRepl.cpp
  clang/unittests/Basic/DiagnosticTest.cpp

Index: clang/unittests/Basic/DiagnosticTest.cpp
===
--- clang/unittests/Basic/DiagnosticTest.cpp
+++ clang/unittests/Basic/DiagnosticTest.cpp
@@ -14,6 +14,15 @@
 using namespace llvm;
 using namespace clang;
 
+void clang::DiagnosticsTestHelper(DiagnosticsEngine &diag) {
+  unsigned delayedDiagID = 0U;
+
+  EXPECT_EQ(diag.DelayedDiagID, delayedDiagID);
+  EXPECT_FALSE(diag.DiagStates.empty());
+  EXPECT_TRUE(diag.DiagStatesByLoc.empty());
+  EXPECT_TRUE(diag.DiagStateOnPushStack.empty());
+}
+
 namespace {
 
 // Check that DiagnosticErrorTrap works with SuppressAllDiagnostics.
@@ -71,6 +80,32 @@
 EXPECT_EQ(Diags.getNumWarnings(), FatalsAsError);
   }
 }
+
+// Check that soft RESET works as intended
+TEST(DiagnosticTest, softReset) {
+  DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
+  new IgnoringDiagConsumer());
+
+  unsigned numWarnings = 0U, numErrors = 0U;
+
+  Diags.Reset(true);
+  // Check For ErrorOccurred and TrapNumErrorsOccurred
+  EXPECT_FALSE(Diags.hasErrorOccurred());
+  EXPECT_FALSE(Diags.hasFatalErrorOccurred());
+  EXPECT_FALSE(Diags.hasUncompilableErrorOccurred());
+  // Check for UnrecoverableErrorOccurred and TrapNumUnrecoverableErrorsOccurred
+  EXPECT_FALSE(Diags.hasUnrecoverableErrorOccurred());
+
+  EXPECT_EQ(Diags.getNumWarnings(), numWarnings);
+  EXPECT_EQ(Diags.getNumErrors(), numErrors);
+
+  // Check for private variables of DiagnosticsEngine differentiating soft reset
+  DiagnosticsTestHelper(Diags);
+
+  EXPECT_FALSE(Diags.isDiagnosticInFlight());
+  EXPECT_TRUE(Diags.isLastDiagnosticIgnored());
+}
+
 TEST(DiagnosticTest, diagnosticError) {
   DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
   new IgnoringDiagConsumer());
Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -86,14 +86,17 @@
   llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
   }
 
+  bool hadErrors = false;
   if (OptInputs.empty()) {
 llvm::LineEditor LE("clang-repl");
 // FIXME: Add LE.setListCompleter
 while (llvm::Optional Line = LE.readLine()) {
   if (*Line == "quit")
 break;
-  if (auto Err = Interp->ParseAndExecute(*Line))
+  if (auto Err = Interp->ParseAndExecute(*Line)) {
 llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
+hadErrors = true;
+  }
 }
   }
 
@@ -104,5 +107,5 @@
 
   llvm::llvm_shutdown();
 
-  return 0;
+  return hadErrors;
 }
Index: clang/test/Interpreter/error-recovery-pragmas.cpp
===
--- /dev/null
+++ clang/test/Interpreter/error-recovery-pragmas.cpp
@@ -0,0 +1,7 @@
+// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -verify
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wmultichar"
+// Reset should not delete #pragmas
+error; // expected-error {{use of undeclared identifier}}
+void no_diag_multichar(void) { char c = (char)'ab'; }
+quit
Index: clang/lib/Basic/Diagnostic.cpp
===
--- clang/lib/Basic/Diagnostic.cpp
+++ clang/lib/Basic/Diagnostic.cpp
@@ -130,7 +130,7 @@
   return true;
 }
 
-void DiagnosticsEngine::Reset() {
+void DiagnosticsEngine::Reset(bool soft /*=false*/) {
   ErrorOccurred = false;
   UncompilableErrorOccurred = false;
   FatalErrorOccurred = false;
@@ -145,15 +145,17 @@
   LastDiagLevel = DiagnosticIDs::Ignored;
   DelayedDiagID = 0;
 
-  // Clear state related to #pragma diagnostic.
-  DiagStates.clear();
-  DiagStatesByLoc.clear();
-  DiagStateOnPushStack.clear();
+  if (!soft) {
+// Clear state related to #pragma diagnostic.
+DiagStates.clear();
+DiagStatesByLoc.clear();
+DiagStateOnPushStack.clear();
 
-  // Create a DiagState and DiagStatePoint representing diagnostic changes
-  // through command-line.
-  DiagStates.emplace_back();
-  DiagStatesByLoc.appendFirst(&DiagStates.back());
+// Create a DiagState and DiagStatePoint representing diagnostic changes
+// through command-line.
+DiagStates.emplace_back();
+DiagStatesByLoc.appendFirst(&DiagStates.back());
+  }
 }
 
 void DiagnosticsEngine::SetDelayedDiagnostic(unsigned DiagID, StringRef Arg1,
Index: clang/in

[PATCH] D126183: Implement soft reset of the diagnostics engine.

2022-06-01 Thread Tapasweni Pathak via Phabricator via cfe-commits
tapaswenipathak updated this revision to Diff 433362.
tapaswenipathak added a comment.

this should be green. :)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126183/new/

https://reviews.llvm.org/D126183

Files:
  clang/include/clang/Basic/Diagnostic.h
  clang/lib/Basic/Diagnostic.cpp
  clang/lib/Interpreter/IncrementalParser.cpp
  clang/test/Interpreter/error-recovery-pragmas.cpp
  clang/tools/clang-repl/ClangRepl.cpp
  clang/unittests/Basic/DiagnosticTest.cpp

Index: clang/unittests/Basic/DiagnosticTest.cpp
===
--- clang/unittests/Basic/DiagnosticTest.cpp
+++ clang/unittests/Basic/DiagnosticTest.cpp
@@ -14,6 +14,15 @@
 using namespace llvm;
 using namespace clang;
 
+void clang::DiagnosticsTestHelper(DiagnosticsEngine &diag) {
+  unsigned delayedDiagID = 0U;
+
+  EXPECT_EQ(diag.DelayedDiagID, delayedDiagID);
+  EXPECT_FALSE(diag.DiagStates.empty());
+  EXPECT_TRUE(diag.DiagStatesByLoc.empty());
+  EXPECT_TRUE(diag.DiagStateOnPushStack.empty());
+}
+
 namespace {
 
 // Check that DiagnosticErrorTrap works with SuppressAllDiagnostics.
@@ -71,6 +80,32 @@
 EXPECT_EQ(Diags.getNumWarnings(), FatalsAsError);
   }
 }
+
+// Check that soft RESET works as intended
+TEST(DiagnosticTest, softReset) {
+  DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
+  new IgnoringDiagConsumer());
+
+  unsigned numWarnings = 0U, numErrors = 0U;
+
+  Diags.Reset(true);
+  // Check For ErrorOccurred and TrapNumErrorsOccurred
+  EXPECT_FALSE(Diags.hasErrorOccurred());
+  EXPECT_FALSE(Diags.hasFatalErrorOccurred());
+  EXPECT_FALSE(Diags.hasUncompilableErrorOccurred());
+  // Check for UnrecoverableErrorOccurred and TrapNumUnrecoverableErrorsOccurred
+  EXPECT_FALSE(Diags.hasUnrecoverableErrorOccurred());
+
+  EXPECT_EQ(Diags.getNumWarnings(), numWarnings);
+  EXPECT_EQ(Diags.getNumErrors(), numErrors);
+
+  // Check for private variables of DiagnosticsEngine differentiating soft reset
+  DiagnosticsTestHelper(Diags);
+
+  EXPECT_FALSE(Diags.isDiagnosticInFlight());
+  EXPECT_TRUE(Diags.isLastDiagnosticIgnored());
+}
+
 TEST(DiagnosticTest, diagnosticError) {
   DiagnosticsEngine Diags(new DiagnosticIDs(), new DiagnosticOptions,
   new IgnoringDiagConsumer());
Index: clang/tools/clang-repl/ClangRepl.cpp
===
--- clang/tools/clang-repl/ClangRepl.cpp
+++ clang/tools/clang-repl/ClangRepl.cpp
@@ -49,6 +49,23 @@
   exit(GenCrashDiag ? 70 : 1);
 }
 
+// If we are running with -verify a report has to be returned as unsuccess.
+// This is relevant especially for the test suite.
+static int checkDiagErrors(const clang::CompilerInstance *CI) {
+  unsigned Errs = CI->getDiagnostics().getClient()->getNumErrors();
+  if (CI->getDiagnosticOpts().VerifyDiagnostics) {
+// If there was an error that came from the verifier we must return 1 as
+// an exit code for the process. This will make the test fail as expected.
+clang::DiagnosticConsumer *Client = CI->getDiagnostics().getClient();
+Client->EndSourceFile();
+Errs = Client->getNumErrors();
+
+// The interpreter expects BeginSourceFile/EndSourceFiles to be balanced.
+Client->BeginSourceFile(CI->getLangOpts(), &CI->getPreprocessor());
+  }
+  return Errs ? EXIT_FAILURE : EXIT_SUCCESS;
+}
+
 llvm::ExitOnError ExitOnErr;
 int main(int argc, const char **argv) {
   ExitOnErr.setBanner("clang-repl: ");
@@ -104,5 +121,5 @@
 
   llvm::llvm_shutdown();
 
-  return 0;
+  return checkDiagErrors(Interp->getCompilerInstance());
 }
Index: clang/test/Interpreter/error-recovery-pragmas.cpp
===
--- /dev/null
+++ clang/test/Interpreter/error-recovery-pragmas.cpp
@@ -0,0 +1,7 @@
+// RUN: cat %s | clang-repl -Xcc -Xclang -Xcc -verify
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wmultichar"
+// Reset should not delete #pragmas
+error; // expected-error {{a type specifier is required for all declarations}}
+void no_diag_multichar(void) { char c = (char)'ab'; }
+quit
Index: clang/lib/Interpreter/IncrementalParser.cpp
===
--- clang/lib/Interpreter/IncrementalParser.cpp
+++ clang/lib/Interpreter/IncrementalParser.cpp
@@ -198,7 +198,7 @@
 }
 
 // FIXME: Do not reset the pragma handlers.
-Diags.Reset();
+Diags.Reset(true);
 return llvm::make_error("Parsing failed.",
std::error_code());
   }
Index: clang/lib/Basic/Diagnostic.cpp
===
--- clang/lib/Basic/Diagnostic.cpp
+++ clang/lib/Basic/Diagnostic.cpp
@@ -130,7 +130,7 @@
   return true;
 }
 
-void DiagnosticsEngine::Reset() {
+void DiagnosticsEngine::Reset(bool soft /*=false*/) {
   ErrorOccurred = false;
   UncompilableErrorOcc