Skip to content

[clang] Cleanup IncludeLocMap #106241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 30, 2024
Merged

Conversation

kadircet
Copy link
Member

CompilerInstance can re-use same SourceManager across multiple
frontendactions. During this process it calls
SourceManager::clearIDTables to reset any caches based on FileIDs.

It didn't reset IncludeLocMap, resulting in wrong include locations for
workflows that triggered multiple frontend-actions through same
CompilerInstance.

@kadircet kadircet requested a review from ilya-biryukov August 27, 2024 15:59
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 27, 2024
@llvmbot
Copy link
Member

llvmbot commented Aug 27, 2024

@llvm/pr-subscribers-clang

Author: kadir çetinkaya (kadircet)

Changes

CompilerInstance can re-use same SourceManager across multiple
frontendactions. During this process it calls
SourceManager::clearIDTables to reset any caches based on FileIDs.

It didn't reset IncludeLocMap, resulting in wrong include locations for
workflows that triggered multiple frontend-actions through same
CompilerInstance.


Full diff: https://github.com/llvm/llvm-project/pull/106241.diff

1 Files Affected:

  • (modified) clang/lib/Basic/SourceManager.cpp (+1)
diff --git a/clang/lib/Basic/SourceManager.cpp b/clang/lib/Basic/SourceManager.cpp
index b0256a8ce9ed04..d6ec26af80aadd 100644
--- a/clang/lib/Basic/SourceManager.cpp
+++ b/clang/lib/Basic/SourceManager.cpp
@@ -350,6 +350,7 @@ void SourceManager::clearIDTables() {
   LastLineNoContentCache = nullptr;
   LastFileIDLookup = FileID();
 
+  IncludedLocMap.clear();
   if (LineTable)
     LineTable->clear();
 

Copy link
Contributor

@ilya-biryukov ilya-biryukov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good, but I wanted to explore if we could write a unit test for this...

@kadircet kadircet force-pushed the cleanup_inc_loc_cache branch from ab986c7 to f8fb043 Compare August 29, 2024 11:27
@kadircet kadircet requested a review from ilya-biryukov August 29, 2024 11:27
Copy link

github-actions bot commented Aug 29, 2024

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff b294951e3967730ffad14d51297694b1411d7af6 6f874afb3fac6a373c94897a5d17c4cd4d1653a6 --extensions cpp -- clang/lib/Basic/SourceManager.cpp clang/unittests/Basic/SourceManagerTest.cpp
View the diff from clang-format here.
diff --git a/clang/unittests/Basic/SourceManagerTest.cpp b/clang/unittests/Basic/SourceManagerTest.cpp
index c683bc2919..0f2476bd8b 100644
--- a/clang/unittests/Basic/SourceManagerTest.cpp
+++ b/clang/unittests/Basic/SourceManagerTest.cpp
@@ -474,13 +474,13 @@ TEST_F(SourceManagerTest, ResetsIncludeLocMap) {
   };
 
   auto Buf = llvm::MemoryBuffer::getMemBuffer("");
-  FileEntryRef HeaderFile = FileMgr.getVirtualFileRef(
-      "/foo.h", Buf->getBufferSize(), 0);
+  FileEntryRef HeaderFile =
+      FileMgr.getVirtualFileRef("/foo.h", Buf->getBufferSize(), 0);
   SourceMgr.overrideFileContents(HeaderFile, std::move(Buf));
 
   Buf = llvm::MemoryBuffer::getMemBuffer(R"cpp(#include "/foo.h")cpp");
-  FileEntryRef BarFile = FileMgr.getVirtualFileRef(
-      "/bar.h", Buf->getBufferSize(), 0);
+  FileEntryRef BarFile =
+      FileMgr.getVirtualFileRef("/bar.h", Buf->getBufferSize(), 0);
   SourceMgr.overrideFileContents(BarFile, std::move(Buf));
   SourceMgr.createFileID(BarFile, {}, clang::SrcMgr::C_User);
 

Copy link
Contributor

@ilya-biryukov ilya-biryukov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGMT with a small suggestion.

Thanks for getting the test in!

@kadircet kadircet force-pushed the cleanup_inc_loc_cache branch from f8fb043 to 6f874af Compare August 30, 2024 09:40
CompilerInstance can re-use same SourceManager across multiple
frontendactions. During this process it calls
`SourceManager::clearIDTables` to reset any caches based on FileIDs.

It didn't reset IncludeLocMap, resulting in wrong include locations for
workflows that triggered multiple frontend-actions through same
CompilerInstance.
@kadircet kadircet force-pushed the cleanup_inc_loc_cache branch from 6f874af to c8c29ea Compare August 30, 2024 09:57
@kadircet kadircet merged commit a2a93f0 into llvm:main Aug 30, 2024
4 of 5 checks passed
@kadircet kadircet deleted the cleanup_inc_loc_cache branch August 30, 2024 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants