[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-08-01 Thread Vassil Vassilev via Phabricator via cfe-commits
v.g.vassilev added a comment.

@tapaswenipathak, ping...


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D126266

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


[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-07-14 Thread Vassil Vassilev via Phabricator via cfe-commits
v.g.vassilev added a comment.

@tapaswenipathak, ping.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D126266

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


[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-07-06 Thread Vassil Vassilev via Phabricator via cfe-commits
v.g.vassilev added a comment.

In D126266#3629145 , @vsapsai wrote:

> Hmm, I'm concerned with the pieces added purely for testing. Specifically, 
> `FileEntriesToReread` and TestHelper friend functions. Need to think how else 
> we can test it.

I am not thrilled about that either. We have discussed this approach here 
https://reviews.llvm.org/D126183 and we have past experience going that route. 
I would be happy to drop this test in favor of the mentioned clang-repl test. 
Should be enough to cover the usecase.

> Do you intend to support only the error cases like
>
>   clang-repl> #include "file_with_error.h"
>   // error is printed, we edit the file and include it again:
>   clang-repl> #include "file_with_error.h"
>
> or do you want to handle re-including files? Something like
>
>   clang-repl> #include "experiments.h"
>   // edit the file and include it again:
>   clang-repl> #include "experiments.h"
>
> Asking because maybe in the error case we commit to some state too eagerly 
> and fixing that sticky eagerness is another option (just guessing, have no 
> idea if it is an actual option and if it is "better").

We want both. The error case is somewhat easier to deal with. However, in case 
we have error in the logic in `#include "experiments.h"` we want to execute and 
then re-execute the new code.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D126266

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


[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-07-04 Thread Volodymyr Sapsai via Phabricator via cfe-commits
vsapsai added a comment.

Hmm, I'm concerned with the pieces added purely for testing. Specifically, 
`FileEntriesToReread` and TestHelper friend functions. Need to think how else 
we can test it.

Do you intend to support only the error cases like

  clang-repl> #include "file_with_error.h"
  // error is printed, we edit the file and include it again:
  clang-repl> #include "file_with_error.h"

or do you want to handle re-including files? Something like

  clang-repl> #include "experiments.h"
  // edit the file and include it again:
  clang-repl> #include "experiments.h"

Asking because maybe in the error case we commit to some state too eagerly and 
fixing that sticky eagerness is another option (just guessing, have no idea if 
it is an actual option and if it is "better").


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D126266

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


[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-25 Thread Vassil Vassilev via Phabricator via cfe-commits
v.g.vassilev added a comment.

After landing https://reviews.llvm.org/D126682 we might be able to write a test 
in clang-repl such as:

  cpp
  clang-repl> #include 
  clang-repl> #include 
  clang-repl>using namespace std;
  clang-repl> int write_str (const char* str) {
ofstream myfile;
myfile.open ("a.h");
myfile << str<<"\n";
myfile.close();
return 0;
  }
  clang-repl> auto r1 = write_str("int i = 42");
  clang-repl> extern "C" int printf(const char*,...);
  clang-repl> #include "a.h"
  clang-repl> auto r2 = printf("i=%d\n", i);
  clang-repl>%undo
  clang-repl>%undo 
  clang-repl> auto r3 = write_str("int i = 0");
  clang-repl> #include "a.h"
  clang-repl> auto r2 = printf("i=%d\n", i);
  // Here we should print `i=0`.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D126266

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


[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 ,
+FileManager ) {
+  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 ) {
+  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(>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 * = FileInfos[Entry]) {
+E->setBuffer(nullptr);
+E = 0;
+  }
+  if (!FID.isInvalid()) {
+const SrcMgr::SLocEntry  = getSLocEntry(FID);
+if (SLocE.isFile()) {
+  SrcMgr::ContentCache  =
+  const_cast(SLocE.getFile().getContentCache());
+  CC.setBuffer(nullptr);
+}
+  }
+  getFileManager().invalidateCache(const_cast(Entry));
+}
+
 void SourceManager::initializeForReplay(const SourceManager ) {
   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 

[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 , FileManager ) {
+  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 ){
+  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(>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 * = 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 ) {
   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");
+  

[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 , FileManager ) {
+  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 ){
+  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(>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  = CurDirArg ? *CurDirArg : CurDirLocal;
 
@@ -900,7 +901,7 @@
   Optional FE = HeaderInfo.LookupFile(
   Filename, FilenameLoc, isAngled, FromDir, , Includers, SearchPath,
   RelativePath, RequestingModule, SuggestedModule, IsMapped,
-  IsFrameworkFound, SkipCache, BuildSystemModule);
+  

[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 , FileManager ) {
+  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 ){
+  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(>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 * = 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 ) {
   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.insert(Entry->getLastRef());
+  

[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-05-23 Thread Vassil Vassilev via Phabricator via cfe-commits
v.g.vassilev added a subscriber: cfe-commits.
v.g.vassilev added reviewers: rsmith, vsapsai.
v.g.vassilev added a comment.

Just to add that the `invalidateCache` is important for cling and clang-repl 
where we do something like:

clang-repl> #include "file_with_error.h"
// error is printed, we edit the file and include it again:
clang-repl> #include "file_with_error.h"

I am not sure if we can write such a test for clang-repl easily using the lit 
infrastructure...


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D126266

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