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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/lib/Basic/SourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ void SourceManager::clearIDTables() {
LastLineNoContentCache = nullptr;
LastFileIDLookup = FileID();

IncludedLocMap.clear();
if (LineTable)
LineTable->clear();

Expand Down
60 changes: 60 additions & 0 deletions clang/unittests/Basic/SourceManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "clang/Lex/PreprocessorOptions.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Process.h"
#include "gtest/gtest.h"
#include <cstddef>
Expand Down Expand Up @@ -453,6 +454,65 @@ TEST_F(SourceManagerTest, loadedSLocEntryIsInTheSameTranslationUnit) {

#if defined(LLVM_ON_UNIX)

// A single SourceManager instance is sometimes reused across multiple
// compilations. This test makes sure we're resetting caches built for tracking
// include locations that are based on FileIDs, to make sure we don't report
// wrong include locations when FileIDs coincide between two different runs.
TEST_F(SourceManagerTest, ResetsIncludeLocMap) {
auto ParseFile = [&] {
TrivialModuleLoader ModLoader;
HeaderSearch HeaderInfo(std::make_shared<HeaderSearchOptions>(), SourceMgr,
Diags, LangOpts, &*Target);
Preprocessor PP(std::make_shared<PreprocessorOptions>(), Diags, LangOpts,
SourceMgr, HeaderInfo, ModLoader,
/*IILookup =*/nullptr,
/*OwnsHeaderSearch =*/false);
PP.Initialize(*Target);
PP.EnterMainSourceFile();
PP.LexTokensUntilEOF();
EXPECT_FALSE(Diags.hasErrorOccurred());
};

auto Buf = llvm::MemoryBuffer::getMemBuffer("");
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);
SourceMgr.overrideFileContents(BarFile, std::move(Buf));
SourceMgr.createFileID(BarFile, {}, clang::SrcMgr::C_User);

Buf = llvm::MemoryBuffer::getMemBuffer(R"cpp(#include "/foo.h")cpp");
FileID MFID = SourceMgr.createFileID(std::move(Buf));
SourceMgr.setMainFileID(MFID);

ParseFile();
auto FooFID = SourceMgr.getOrCreateFileID(HeaderFile, clang::SrcMgr::C_User);
auto IncFID = SourceMgr.getDecomposedIncludedLoc(FooFID).first;
EXPECT_EQ(IncFID, MFID);

// Clean up source-manager state before we start next parse.
SourceMgr.clearIDTables();

// Set up a new main file.
Buf = llvm::MemoryBuffer::getMemBuffer(R"cpp(
// silly comment 42
#include "/bar.h")cpp");
MFID = SourceMgr.createFileID(std::move(Buf));
SourceMgr.setMainFileID(MFID);

ParseFile();
// Make sure foo.h got the same file-id in both runs.
EXPECT_EQ(FooFID,
SourceMgr.getOrCreateFileID(HeaderFile, clang::SrcMgr::C_User));
auto BarFID = SourceMgr.getOrCreateFileID(BarFile, clang::SrcMgr::C_User);
IncFID = SourceMgr.getDecomposedIncludedLoc(FooFID).first;
// Check that includer is bar.h during this run.
EXPECT_EQ(IncFID, BarFID);
}

TEST_F(SourceManagerTest, getMacroArgExpandedLocation) {
const char *header =
"#define FM(x,y) x\n";
Expand Down
Loading